update
parent
c485d070a3
commit
d2feccf3b3
@ -1 +1,15 @@
|
||||
package net
|
||||
|
||||
import (
|
||||
"b612.me/apps/b612/netforward"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var Cmd = &cobra.Command{
|
||||
Use: "net",
|
||||
Short: "net tools",
|
||||
}
|
||||
|
||||
func init() {
|
||||
Cmd.AddCommand(netforward.CmdNetforward)
|
||||
}
|
||||
|
@ -1,16 +0,0 @@
|
||||
package net
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestForward(t *testing.T) {
|
||||
var f = NetForward{
|
||||
LocalAddr: "127.0.0.1",
|
||||
LocalPort: 22232,
|
||||
RemoteURI: "127.0.0.1:1127",
|
||||
EnableTCP: true,
|
||||
EnableUDP: true,
|
||||
DialTimeout: 0,
|
||||
UDPTimeout: 0,
|
||||
}
|
||||
f.Run()
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
package netforward
|
||||
|
||||
import (
|
||||
"b612.me/stario"
|
||||
"b612.me/starlog"
|
||||
"github.com/spf13/cobra"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var f = new(NetForward)
|
||||
var dialTimeout, udpTimeout int64
|
||||
|
||||
func init() {
|
||||
CmdNetforward.Flags().StringVarP(&f.LocalAddr, "local", "l", "0.0.0.0", "bind address")
|
||||
CmdNetforward.Flags().IntVarP(&f.LocalPort, "port", "p", 11270, "local listen port")
|
||||
CmdNetforward.Flags().BoolVarP(&f.EnableTCP, "enable-tcp-forward", "t", true, "enable tcp forward mode")
|
||||
CmdNetforward.Flags().BoolVarP(&f.EnableUDP, "enable-udp-forward", "u", true, "enable udp forward mode")
|
||||
CmdNetforward.Flags().Int64VarP(&dialTimeout, "dial-timeout", "d", 10000, "dial timeout milliseconds")
|
||||
CmdNetforward.Flags().Int64VarP(&udpTimeout, "udp-timeout", "D", 60000, "udp connection timeout milliseconds")
|
||||
}
|
||||
|
||||
var CmdNetforward = &cobra.Command{
|
||||
Use: "forward",
|
||||
Short: "net forward",
|
||||
Long: "forward tcp and udp packet",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if len(args) == 0 {
|
||||
starlog.Errorln("please enter a target uri")
|
||||
os.Exit(1)
|
||||
}
|
||||
f.RemoteURI = strings.TrimSpace(args[0])
|
||||
if dialTimeout == 0 {
|
||||
dialTimeout = 10000
|
||||
}
|
||||
if udpTimeout == 0 {
|
||||
udpTimeout = 60000
|
||||
}
|
||||
f.DialTimeout = time.Duration(dialTimeout) * time.Millisecond
|
||||
f.UDPTimeout = time.Duration(udpTimeout) * time.Millisecond
|
||||
if err := f.Run(); err != nil {
|
||||
starlog.Errorln("run net forward failed:", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
sign := make(chan os.Signal)
|
||||
signal.Notify(sign, os.Interrupt, os.Kill)
|
||||
for {
|
||||
select {
|
||||
case <-sign:
|
||||
starlog.Noticeln("Recv Stop Signal From User")
|
||||
f.stopFn()
|
||||
case <-stario.WaitUntilFinished(func() error {
|
||||
for {
|
||||
if f.Status() == 0 {
|
||||
return nil
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}):
|
||||
starlog.Infoln("Service Stoped")
|
||||
return
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package netforward
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestForward(t *testing.T) {
|
||||
var f = NetForward{
|
||||
LocalAddr: "127.0.0.1",
|
||||
LocalPort: 22232,
|
||||
RemoteURI: "192.168.2.1:80",
|
||||
EnableTCP: true,
|
||||
EnableUDP: true,
|
||||
DialTimeout: 6 * time.Second,
|
||||
UDPTimeout: 7 * time.Second,
|
||||
}
|
||||
f.Run()
|
||||
go func() {
|
||||
time.Sleep(time.Second * 10)
|
||||
fmt.Println("closing")
|
||||
f.Close()
|
||||
}()
|
||||
for {
|
||||
time.Sleep(time.Second * 2)
|
||||
if f.Status() > 0 {
|
||||
fmt.Println(f.Status())
|
||||
continue
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue