diff --git a/a-test.txt b/a-test.txt new file mode 100644 index 000000000..2e65efe2a --- /dev/null +++ b/a-test.txt @@ -0,0 +1 @@ +a \ No newline at end of file diff --git a/client/config.go b/client/config.go index 3d807b60c..19f942d43 100644 --- a/client/config.go +++ b/client/config.go @@ -37,6 +37,8 @@ type Config struct { SnmpPeriod int `json:"snmpperiod"` Quiet bool `json:"quiet"` TCP bool `json:"tcp"` + BindUDP string `json:"bindudp"` + TCPBuf int `json:"tcpbuf"` } func parseJSONConfig(config *Config, path string) error { diff --git a/client/dial.go b/client/dial.go index 4b892831f..2c1e2afc5 100644 --- a/client/dial.go +++ b/client/dial.go @@ -4,6 +4,7 @@ import ( "github.com/pkg/errors" kcp "github.com/xtaci/kcp-go/v5" "github.com/xtaci/tcpraw" + "net" ) func dial(config *Config, block kcp.BlockCrypt) (*kcp.UDPSession, error) { @@ -14,5 +15,25 @@ func dial(config *Config, block kcp.BlockCrypt) (*kcp.UDPSession, error) { } return kcp.NewConn(config.RemoteAddr, block, config.DataShard, config.ParityShard, conn) } + if config.BindUDP != "" { + //log.Println("func dial() config.BindUDP = ", config.BindUDP) + return dialAndBindUdp(config, block) + } return kcp.DialWithOptions(config.RemoteAddr, block, config.DataShard, config.ParityShard) } + +func dialAndBindUdp(config *Config, block kcp.BlockCrypt) (*kcp.UDPSession, error) { + udpaddr, err := net.ResolveUDPAddr("udp", config.BindUDP) + if err != nil { + return nil, errors.WithStack(err) + } + network := "udp4" + if udpaddr.IP.To4() == nil { + network = "udp" + } + conn, err := net.ListenUDP(network, udpaddr) + if err != nil { + return nil, errors.WithStack(err) + } + return kcp.NewConn(config.RemoteAddr, block, config.DataShard, config.ParityShard, conn) +} diff --git a/client/main.go b/client/main.go index e6a792377..f9b20ff7e 100644 --- a/client/main.go +++ b/client/main.go @@ -238,6 +238,16 @@ func main() { Value: "", // when the value is not empty, the config path must exists Usage: "config from json file, which will override the command from shell", }, + cli.StringFlag{ + Name: "bindudp,b", //hedahong add + Value: "", // when the value is not empty, OS automatic route + Usage: "bind output network address for udp socket", + }, + cli.IntFlag{ + Name: "tcpbuf", //hedahong add + Value: 0, // tcp socket buffer size in bytes( linux defaults 2MB) //212992 + Usage: "per-socket buffer in bytes", + }, } myApp.Action = func(c *cli.Context) error { config := Config{} @@ -271,6 +281,8 @@ func main() { config.SnmpPeriod = c.Int("snmpperiod") config.Quiet = c.Bool("quiet") config.TCP = c.Bool("tcp") + config.BindUDP = c.String("bindudp") //hedahong add + config.TCPBuf = c.Int("tcpbuf") //hedahong add if c.String("c") != "" { err := parseJSONConfig(&config, c.String("c")) @@ -324,6 +336,8 @@ func main() { log.Println("snmpperiod:", config.SnmpPeriod) log.Println("quiet:", config.Quiet) log.Println("tcp:", config.TCP) + log.Println("---bindudp:", config.BindUDP) + log.Println("---tcpbuf:", config.TCPBuf) // parameters check if config.SmuxVer > maxSmuxVer { @@ -441,6 +455,17 @@ func main() { if err != nil { log.Fatalf("%+v", err) } + + //hedahong add + log.Println("new client:", p1.RemoteAddr(), "-->", p1.LocalAddr()) + if config.TCPBuf > 0 { + p1.SetReadBuffer(config.TCPBuf); // hedahong add + p1.SetWriteBuffer(config.TCPBuf); // hedahong add + //log.Println("config.TCPBuf === ",config.TCPBuf) + } else { + //log.Println("config.TCPBuf = 0 ,OS setting default") + } + idx := rr % numconn // do auto expiration && reconnection diff --git a/go.sum b/go.sum index 0207d15cd..7586af691 100644 --- a/go.sum +++ b/go.sum @@ -25,7 +25,7 @@ github.com/templexxx/xorsimd v0.4.1 h1:iUZcywbOYDRAZUasAs2eSCUW8eobuZDy0I9FJiORk github.com/templexxx/xorsimd v0.4.1/go.mod h1:W+ffZz8jJMH2SXwuKu9WhygqBMbFnp14G2fqEr8qaNo= github.com/tjfoc/gmsm v1.0.1 h1:R11HlqhXkDospckjZEihx9SW/2VW0RgdwrykyWMFOQU= github.com/tjfoc/gmsm v1.0.1/go.mod h1:XxO4hdhhrzAd+G4CjDqaOkd0hUzmtPR/d3EiBBMn/wc= -github.com/tjfoc/gmsm v1.3.0 h1:i7c6Za/IlgBvnGxYpfD7L3TGuaS+v6oGcgq+J9/ecEA= +github.com/tjfoc/gmsm v1.3.0 h1:qhgkrZru95jFP9NbVPknJvc9vgkMXhOEzkOASKdc0oQ= github.com/tjfoc/gmsm v1.3.0/go.mod h1:HaUcFuY0auTiaHB9MHFGCPx5IaLhTUd2atbCFBQXn9w= github.com/urfave/cli v1.21.0 h1:wYSSj06510qPIzGSua9ZqsncMmWE3Zr55KBERygyrxE= github.com/urfave/cli v1.21.0/go.mod h1:lxDj6qX9Q6lWQxIrbrT0nwecwUtRnhVZAJjJZrVUZZQ=