Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加udp 拨号时 物理网络接口绑定 64c121a #807

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions a-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
2 changes: 2 additions & 0 deletions client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 21 additions & 0 deletions client/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
25 changes: 25 additions & 0 deletions client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down