From b3813aaff44de1980bfa4179ac3a7743f8d9cdcb Mon Sep 17 00:00:00 2001 From: hedahong Date: Thu, 17 Sep 2020 16:02:37 +0800 Subject: [PATCH 1/4] test push to github --- a-test.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 a-test.txt 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 From 64c121aa61a1c25bef3b0929289bc59abfb76b90 Mon Sep 17 00:00:00 2001 From: hedahong Date: Thu, 17 Sep 2020 17:34:09 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0udp=20=E6=8B=A8=E5=8F=B7?= =?UTF-8?q?=E6=97=B6=20=E7=89=A9=E7=90=86=E7=BD=91=E7=BB=9C=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E7=BB=91=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/config.go | 2 ++ client/dial.go | 21 +++++++++++++++++++++ client/main.go | 25 +++++++++++++++++++++++++ go.sum | 2 +- 4 files changed, 49 insertions(+), 1 deletion(-) 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= From 1f81cbaa2d22914482dcde573a27237bdbd16e26 Mon Sep 17 00:00:00 2001 From: hedahong Date: Thu, 17 Sep 2020 17:37:40 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- a-test.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 a-test.txt diff --git a/a-test.txt b/a-test.txt deleted file mode 100644 index 2e65efe2a..000000000 --- a/a-test.txt +++ /dev/null @@ -1 +0,0 @@ -a \ No newline at end of file From 24db4e6941d8bb9fd837595d47f81b5d840edeb9 Mon Sep 17 00:00:00 2001 From: hedahong Date: Thu, 17 Sep 2020 17:38:56 +0800 Subject: [PATCH 4/4] =?UTF-8?q?Revert=20"=E5=88=A0=E9=99=A4=E6=97=A0?= =?UTF-8?q?=E7=94=A8=E6=96=87=E4=BB=B6"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 1f81cbaa2d22914482dcde573a27237bdbd16e26. --- a-test.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 a-test.txt 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