From 7a2490134aacba8d635708d7adeb54cc524ffdb4 Mon Sep 17 00:00:00 2001 From: Jeffrey Zhang Date: Thu, 21 Sep 2023 10:30:03 +0800 Subject: [PATCH 1/7] support vsock --- cmd/gost/route.go | 4 +++ go.mod | 13 ++++---- go.sum | 15 ++++++++++ node.go | 1 + vsock.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 vsock.go diff --git a/cmd/gost/route.go b/cmd/gost/route.go index 7ebbdf9e..e6f0c633 100644 --- a/cmd/gost/route.go +++ b/cmd/gost/route.go @@ -241,6 +241,8 @@ func parseChainNode(ns string) (nodes []gost.Node, err error) { tr = gost.FakeTCPTransporter() case "udp": tr = gost.UDPTransporter() + case "vsock": + tr = gost.VSOCKTransporter() default: tr = gost.TCPTransporter() } @@ -489,6 +491,8 @@ func (r *route) GenRouters() ([]router, error) { chain.Nodes()[len(chain.Nodes())-1].Client.Transporter = gost.SSHForwardTransporter() } ln, err = gost.TCPListener(node.Addr) + case "vsock": + ln, err = gost.VSOCKListener(node.Addr) case "udp": ln, err = gost.UDPListener(node.Addr, &gost.UDPListenConfig{ TTL: ttl, diff --git a/go.mod b/go.mod index b006564e..e2b8810e 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/gobwas/glob v0.2.3 github.com/gorilla/websocket v1.4.2 github.com/klauspost/compress v1.13.6 + github.com/mdlayher/vsock v1.2.1 github.com/miekg/dns v1.1.47 github.com/quic-go/quic-go v0.32.0 github.com/ryanuber/go-glob v1.0.0 @@ -27,7 +28,7 @@ require ( github.com/xtaci/tcpraw v1.2.25 gitlab.com/yawning/obfs4.git v0.0.0-20220204003609-77af0cba934d golang.org/x/crypto v0.5.0 - golang.org/x/net v0.7.0 + golang.org/x/net v0.9.0 ) require ( @@ -41,6 +42,7 @@ require ( github.com/google/pprof v0.0.0-20230131232505-5a9e8f65f08f // indirect github.com/klauspost/cpuid/v2 v2.0.9 // indirect github.com/klauspost/reedsolomon v1.9.15 // indirect + github.com/mdlayher/socket v0.4.1 // indirect github.com/onsi/ginkgo/v2 v2.8.0 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/quic-go/qtls-go1-18 v0.2.0 // indirect @@ -53,8 +55,9 @@ require ( github.com/xtaci/lossyconn v0.0.0-20200209145036-adba10fffc37 // indirect gitlab.com/yawning/edwards25519-extra.git v0.0.0-20211229043746-2f91fcc9fbdb // indirect golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9 // indirect - golang.org/x/mod v0.7.0 // indirect - golang.org/x/sys v0.5.0 // indirect - golang.org/x/text v0.7.0 // indirect - golang.org/x/tools v0.5.0 // indirect + golang.org/x/mod v0.8.0 // indirect + golang.org/x/sync v0.1.0 // indirect + golang.org/x/sys v0.7.0 // indirect + golang.org/x/text v0.9.0 // indirect + golang.org/x/tools v0.6.0 // indirect ) diff --git a/go.sum b/go.sum index e6ef9f25..bf38f346 100644 --- a/go.sum +++ b/go.sum @@ -75,6 +75,10 @@ github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa02 github.com/klauspost/reedsolomon v1.9.9/go.mod h1:O7yFFHiQwDR6b2t63KPUpccPtNdp5ADgh1gg4fd12wo= github.com/klauspost/reedsolomon v1.9.15 h1:g2erWKD2M6rgnPf89fCji6jNlhMKMdXcuNHMW1SYCIo= github.com/klauspost/reedsolomon v1.9.15/go.mod h1:eqPAcE7xar5CIzcdfwydOEdcmchAKAP/qs14y4GCBOk= +github.com/mdlayher/socket v0.4.1 h1:eM9y2/jlbs1M615oshPQOHZzj6R6wMT7bX5NPiQvn2U= +github.com/mdlayher/socket v0.4.1/go.mod h1:cAqeGjoufqdxWkD7DkpyS+wcefOtmu5OQ8KuoJGIReA= +github.com/mdlayher/vsock v1.2.1 h1:pC1mTJTvjo1r9n9fbm7S1j04rCgCzhCOS5DY0zqHlnQ= +github.com/mdlayher/vsock v1.2.1/go.mod h1:NRfCibel++DgeMD8z/hP+PPTjlNJsdPOmxcnENvE+SE= github.com/miekg/dns v1.1.47 h1:J9bWiXbqMbnZPcY8Qi2E3EWIBsIm6MZzzJB9VRg5gL8= github.com/miekg/dns v1.1.47/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME= github.com/mmcloughlin/avo v0.0.0-20200803215136-443f81d77104/go.mod h1:wqKykBG2QzQDJEzvRkcS8x6MiSJkF52hXZsXcjaB3ls= @@ -156,6 +160,8 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA= golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -171,6 +177,8 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g= golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM= +golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -179,6 +187,7 @@ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -194,6 +203,8 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= +golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY= @@ -202,6 +213,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= @@ -215,6 +228,8 @@ golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.5.0 h1:+bSpV5HIeWkuvgaMfI3UmKRThoTA5ODJTUd8T17NO+4= golang.org/x/tools v0.5.0/go.mod h1:N+Kgy78s5I24c24dU8OfWNEotWjutIs8SnJvn5IDq+k= +golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/node.go b/node.go index f64afc43..f12c7450 100644 --- a/node.go +++ b/node.go @@ -90,6 +90,7 @@ func ParseNode(s string) (node Node, err error) { case "ftcp": // fake TCP case "dns": case "redu", "redirectu": // UDP tproxy + case "vsock": default: node.Transport = "tcp" } diff --git a/vsock.go b/vsock.go new file mode 100644 index 00000000..51aa6dea --- /dev/null +++ b/vsock.go @@ -0,0 +1,76 @@ +package gost + +import ( + "net" + "strconv" + + "github.com/mdlayher/vsock" +) + +// vsockTransporter is a raw VSOCK transporter. +type vsockTransporter struct{} + +// VSOCKTransporter creates a raw VSOCK client. +func VSOCKTransporter() Transporter { + return &vsockTransporter{} +} + +func (tr *vsockTransporter) Dial(addr string, options ...DialOption) (net.Conn, error) { + opts := &DialOptions{} + for _, option := range options { + option(opts) + } + if opts.Chain == nil { + vAddr, err := parseAddr(addr) + if err != nil { + return nil, err + } + return vsock.Dial(vAddr.ContextID, vAddr.Port, nil) + } + return opts.Chain.Dial(addr) +} + +func parseUint32(s string) (uint32, error ) { + n, err := strconv.ParseUint(s, 10, 32) + if err != nil { + return 0, err + } + return uint32(n), nil +} + +func parseAddr(addr string) (*vsock.Addr, error) { + hostStr, portStr, err := net.SplitHostPort(addr) + if err != nil { + return nil, err + } + host := uint32(0) + if hostStr != "" { + host, err = parseUint32(hostStr) + if err != nil { + return nil, err + } + } + + port, err := parseUint32(portStr) + if err != nil { + return nil, err + } + return &vsock.Addr{ContextID: host, Port: port}, nil +} + +func (tr *vsockTransporter) Handshake(conn net.Conn, options ...HandshakeOption) (net.Conn, error) { + return conn, nil +} + +func (tr *vsockTransporter) Multiplex() bool { + return false +} + +// VSOCKListener creates a Listener for VSOCK proxy server. +func VSOCKListener(addr string) (Listener, error) { + vAddr, err := parseAddr(addr) + if err != nil { + return nil, err + } + return vsock.Listen(vAddr.Port, nil) +} From 13b9748c9c25e26ead57d6a9273246b5a1e7b98c Mon Sep 17 00:00:00 2001 From: suguds <141613950+suguds@users.noreply.github.com> Date: Fri, 2 Feb 2024 14:08:21 +0800 Subject: [PATCH 2/7] update golang.org/x/crypto v0.5.0 to 0.17.0 --- go.mod | 8 ++++---- go.sum | 5 +++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index e2b8810e..cfd2b9e0 100644 --- a/go.mod +++ b/go.mod @@ -27,8 +27,8 @@ require ( github.com/xtaci/smux v1.5.16 github.com/xtaci/tcpraw v1.2.25 gitlab.com/yawning/obfs4.git v0.0.0-20220204003609-77af0cba934d - golang.org/x/crypto v0.5.0 - golang.org/x/net v0.9.0 + golang.org/x/crypto v0.17.0 + golang.org/x/net v0.10.0 ) require ( @@ -57,7 +57,7 @@ require ( golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9 // indirect golang.org/x/mod v0.8.0 // indirect golang.org/x/sync v0.1.0 // indirect - golang.org/x/sys v0.7.0 // indirect - golang.org/x/text v0.9.0 // indirect + golang.org/x/sys v0.15.0 // indirect + golang.org/x/text v0.14.0 // indirect golang.org/x/tools v0.6.0 // indirect ) diff --git a/go.sum b/go.sum index bf38f346..e22a2d0e 100644 --- a/go.sum +++ b/go.sum @@ -147,6 +147,8 @@ golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWP golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.5.0 h1:U/0M97KRkSFvyD/3FSmdP5W5swImpNgle/EHFhOsQPE= golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU= +golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= +golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9 h1:frX3nT9RkKybPnjyI+yvZh6ZucTZatCCEm9D47sZ2zo= golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= @@ -179,6 +181,7 @@ golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g= golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM= golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= +golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -205,6 +208,7 @@ golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY= @@ -215,6 +219,7 @@ golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= From 7e1af1b557b198ef314a143bfbed95bbea087d8b Mon Sep 17 00:00:00 2001 From: zzq Date: Wed, 17 Jan 2024 13:41:12 +0800 Subject: [PATCH 3/7] fix --- http.go | 2 +- http2.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/http.go b/http.go index 02a7ad32..8ef96e80 100644 --- a/http.go +++ b/http.go @@ -379,7 +379,7 @@ func (h *httpHandler) authenticate(conn net.Conn, req *http.Request, resp *http. } else { resp.Header = http.Header{} resp.Header.Set("Server", "nginx/1.14.1") - resp.Header.Set("Date", time.Now().Format(http.TimeFormat)) + resp.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat)) if resp.StatusCode == http.StatusOK { resp.Header.Set("Connection", "keep-alive") } diff --git a/http2.go b/http2.go index 72c48906..6587f032 100644 --- a/http2.go +++ b/http2.go @@ -539,7 +539,7 @@ func (h *http2Handler) authenticate(w http.ResponseWriter, r *http.Request, resp } else { resp.Header = http.Header{} resp.Header.Set("Server", "nginx/1.14.1") - resp.Header.Set("Date", time.Now().Format(http.TimeFormat)) + resp.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat)) if resp.ContentLength > 0 { resp.Header.Set("Content-Type", "text/html") } From fd57e80709aba9581757b1cd63b7d8f75e2385d2 Mon Sep 17 00:00:00 2001 From: guoguangwu Date: Mon, 19 Jun 2023 19:27:03 +0800 Subject: [PATCH 4/7] chore: remove refs to deprecated io/ioutil --- cmd/gost/cfg.go | 3 +-- cmd/gost/peer.go | 3 +-- common_test.go | 5 ++--- dns.go | 3 +-- http2.go | 3 +-- http2_test.go | 10 +++++----- http_test.go | 8 ++++---- resolver.go | 3 +-- sni_test.go | 4 ++-- ssh.go | 6 +++--- 10 files changed, 21 insertions(+), 27 deletions(-) diff --git a/cmd/gost/cfg.go b/cmd/gost/cfg.go index dc696c82..dddbdf9f 100644 --- a/cmd/gost/cfg.go +++ b/cmd/gost/cfg.go @@ -6,7 +6,6 @@ import ( "crypto/x509" "encoding/json" "errors" - "io/ioutil" "net" "net/url" "os" @@ -71,7 +70,7 @@ func loadCA(caFile string) (cp *x509.CertPool, err error) { return } cp = x509.NewCertPool() - data, err := ioutil.ReadFile(caFile) + data, err := os.ReadFile(caFile) if err != nil { return nil, err } diff --git a/cmd/gost/peer.go b/cmd/gost/peer.go index 9e1d00f2..6f5b32ce 100644 --- a/cmd/gost/peer.go +++ b/cmd/gost/peer.go @@ -5,7 +5,6 @@ import ( "bytes" "encoding/json" "io" - "io/ioutil" "strconv" "strings" "time" @@ -83,7 +82,7 @@ func (cfg *peerConfig) Reload(r io.Reader) error { } func (cfg *peerConfig) parse(r io.Reader) error { - data, err := ioutil.ReadAll(r) + data, err := io.ReadAll(r) if err != nil { return err } diff --git a/common_test.go b/common_test.go index 9e030d6a..28bcf7cc 100644 --- a/common_test.go +++ b/common_test.go @@ -7,7 +7,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net" "net/http" "net/url" @@ -36,7 +35,7 @@ func init() { var ( httpTestHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - data, _ := ioutil.ReadAll(r.Body) + data, _ := io.ReadAll(r.Body) if len(data) == 0 { data = []byte("Hello World!") } @@ -87,7 +86,7 @@ func httpRoundtrip(conn net.Conn, targetURL string, data []byte) (err error) { return errors.New(resp.Status) } - recv, err := ioutil.ReadAll(resp.Body) + recv, err := io.ReadAll(resp.Body) if err != nil { return } diff --git a/dns.go b/dns.go index 1b024040..cc67718b 100644 --- a/dns.go +++ b/dns.go @@ -7,7 +7,6 @@ import ( "encoding/base64" "errors" "io" - "io/ioutil" "net" "net/http" "strconv" @@ -267,7 +266,7 @@ func (l *dnsListener) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - buf, err = ioutil.ReadAll(r.Body) + buf, err = io.ReadAll(r.Body) if err != nil { http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return diff --git a/http2.go b/http2.go index 6587f032..de152eae 100644 --- a/http2.go +++ b/http2.go @@ -9,7 +9,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net" "net/http" "net/http/httputil" @@ -389,7 +388,7 @@ func (h *http2Handler) roundTrip(w http.ResponseWriter, r *http.Request) { ProtoMajor: 2, ProtoMinor: 0, Header: http.Header{}, - Body: ioutil.NopCloser(bytes.NewReader([]byte{})), + Body: io.NopCloser(bytes.NewReader([]byte{})), } if !h.authenticate(w, r, resp) { diff --git a/http2_test.go b/http2_test.go index 762c6dfb..c769d7a3 100644 --- a/http2_test.go +++ b/http2_test.go @@ -5,7 +5,7 @@ import ( "crypto/rand" "crypto/tls" "fmt" - "io/ioutil" + "io" "net" "net/http" "net/http/httptest" @@ -997,7 +997,7 @@ func TestHTTP2ProxyWithWebProbeResist(t *testing.T) { if err != nil { t.Error(err) } - recv, _ := ioutil.ReadAll(conn) + recv, _ := io.ReadAll(conn) if !bytes.Equal(recv, []byte("Hello World!")) { t.Error("data not equal") } @@ -1053,7 +1053,7 @@ func TestHTTP2ProxyWithHostProbeResist(t *testing.T) { Proto: "HTTP/2.0", ProtoMajor: 2, ProtoMinor: 0, - Body: ioutil.NopCloser(bytes.NewReader(sendData)), + Body: io.NopCloser(bytes.NewReader(sendData)), Host: "github.com:443", ContentLength: int64(len(sendData)), } @@ -1068,7 +1068,7 @@ func TestHTTP2ProxyWithHostProbeResist(t *testing.T) { t.Error("got non-200 status:", resp.Status) } - recv, _ := ioutil.ReadAll(resp.Body) + recv, _ := io.ReadAll(resp.Body) if !bytes.Equal(sendData, recv) { t.Error("data not equal") } @@ -1105,7 +1105,7 @@ func TestHTTP2ProxyWithFileProbeResist(t *testing.T) { if err != nil { t.Error(err) } - recv, _ := ioutil.ReadAll(conn) + recv, _ := io.ReadAll(conn) if !bytes.Equal(recv, []byte("Hello World!")) { t.Error("data not equal") } diff --git a/http_test.go b/http_test.go index a4f10411..dcc2e053 100644 --- a/http_test.go +++ b/http_test.go @@ -4,7 +4,7 @@ import ( "bytes" "crypto/rand" "fmt" - "io/ioutil" + "io" "net" "net/http" "net/http/httptest" @@ -249,7 +249,7 @@ func TestHTTPProxyWithWebProbeResist(t *testing.T) { t.Error("got status:", resp.Status) } - recv, _ := ioutil.ReadAll(resp.Body) + recv, _ := io.ReadAll(resp.Body) if !bytes.Equal(recv, []byte("Hello World!")) { t.Error("data not equal") } @@ -296,7 +296,7 @@ func TestHTTPProxyWithHostProbeResist(t *testing.T) { t.Error("got status:", resp.Status) } - recv, _ := ioutil.ReadAll(resp.Body) + recv, _ := io.ReadAll(resp.Body) if !bytes.Equal(sendData, recv) { t.Error("data not equal") } @@ -332,7 +332,7 @@ func TestHTTPProxyWithFileProbeResist(t *testing.T) { t.Error("got status:", resp.Status) } - recv, _ := ioutil.ReadAll(resp.Body) + recv, _ := io.ReadAll(resp.Body) if !bytes.Equal(recv, []byte("Hello World!")) { t.Error("data not equal, got:", string(recv)) } diff --git a/resolver.go b/resolver.go index 69e659dc..0f07cebc 100644 --- a/resolver.go +++ b/resolver.go @@ -8,7 +8,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net" "net/http" "net/url" @@ -915,7 +914,7 @@ func (ex *dohExchanger) Exchange(ctx context.Context, query []byte) ([]byte, err } // Read wireformat response from the body - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("failed to read the response body: %s", err) } diff --git a/sni_test.go b/sni_test.go index 0bfa9941..18cecbc9 100644 --- a/sni_test.go +++ b/sni_test.go @@ -7,7 +7,7 @@ import ( "crypto/tls" "errors" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "net/url" @@ -69,7 +69,7 @@ func sniRoundtrip(client *Client, server *Server, targetURL string, data []byte) return errors.New(resp.Status) } - recv, err := ioutil.ReadAll(resp.Body) + recv, err := io.ReadAll(resp.Body) if err != nil { return } diff --git a/ssh.go b/ssh.go index f772503a..17ad653f 100644 --- a/ssh.go +++ b/ssh.go @@ -6,7 +6,7 @@ import ( "encoding/binary" "errors" "fmt" - "io/ioutil" + "os" "net" "strconv" "strings" @@ -33,7 +33,7 @@ var ( // ParseSSHKeyFile parses ssh key file. func ParseSSHKeyFile(fp string) (ssh.Signer, error) { - key, err := ioutil.ReadFile(fp) + key, err := os.ReadFile(fp) if err != nil { return nil, err } @@ -42,7 +42,7 @@ func ParseSSHKeyFile(fp string) (ssh.Signer, error) { // ParseSSHAuthorizedKeysFile parses ssh Authorized Keys file. func ParseSSHAuthorizedKeysFile(fp string) (map[string]bool, error) { - authorizedKeysBytes, err := ioutil.ReadFile(fp) + authorizedKeysBytes, err := os.ReadFile(fp) if err != nil { return nil, err } From 2faecc1be8d72ee5eb1a24a671e2cfa75f8093df Mon Sep 17 00:00:00 2001 From: tiancheng91 Date: Mon, 16 Jan 2023 15:15:24 +0800 Subject: [PATCH 5/7] feat: support node sort by tcp ping latency --- cmd/gost/peer.go | 21 ++++++----- cmd/gost/route.go | 1 + selector.go | 89 +++++++++++++++++++++++++++++++++++++++++++++++ selector_test.go | 24 +++++++++++++ 4 files changed, 127 insertions(+), 8 deletions(-) diff --git a/cmd/gost/peer.go b/cmd/gost/peer.go index 6f5b32ce..af062c71 100644 --- a/cmd/gost/peer.go +++ b/cmd/gost/peer.go @@ -13,14 +13,16 @@ import ( ) type peerConfig struct { - Strategy string `json:"strategy"` - MaxFails int `json:"max_fails"` - FailTimeout time.Duration - period time.Duration // the period for live reloading - Nodes []string `json:"nodes"` - group *gost.NodeGroup - baseNodes []gost.Node - stopped chan struct{} + Strategy string `json:"strategy"` + MaxFails int `json:"max_fails"` + FastestCount int `json:"fastest_count"` // topN fastest node count + FailTimeout time.Duration + period time.Duration // the period for live reloading + + Nodes []string `json:"nodes"` + group *gost.NodeGroup + baseNodes []gost.Node + stopped chan struct{} } func newPeerConfig() *peerConfig { @@ -51,6 +53,7 @@ func (cfg *peerConfig) Reload(r io.Reader) error { FailTimeout: cfg.FailTimeout, }, &gost.InvalidFilter{}, + gost.NewFastestFilter(0, cfg.FastestCount), ), gost.WithStrategy(gost.NewStrategy(cfg.Strategy)), ) @@ -125,6 +128,8 @@ func (cfg *peerConfig) parse(r io.Reader) error { cfg.Strategy = ss[1] case "max_fails": cfg.MaxFails, _ = strconv.Atoi(ss[1]) + case "fastest_count": + cfg.FastestCount, _ = strconv.Atoi(ss[1]) case "fail_timeout": cfg.FailTimeout, _ = time.ParseDuration(ss[1]) case "reload": diff --git a/cmd/gost/route.go b/cmd/gost/route.go index e6f0c633..360bc2d1 100644 --- a/cmd/gost/route.go +++ b/cmd/gost/route.go @@ -66,6 +66,7 @@ func (r *route) parseChain() (*gost.Chain, error) { FailTimeout: nodes[0].GetDuration("fail_timeout"), }, &gost.InvalidFilter{}, + gost.NewFastestFilter(0, nodes[0].GetInt("fastest_count")), ), gost.WithStrategy(gost.NewStrategy(nodes[0].Get("strategy"))), ) diff --git a/selector.go b/selector.go index bff6d11f..a5f68b91 100644 --- a/selector.go +++ b/selector.go @@ -4,10 +4,13 @@ import ( "errors" "math/rand" "net" + "sort" "strconv" "sync" "sync/atomic" "time" + + "github.com/go-log/log" ) var ( @@ -205,6 +208,92 @@ func (f *FailFilter) String() string { return "fail" } +// FastestFilter filter the fastest node +type FastestFilter struct { + mu sync.Mutex + + pinger *net.Dialer + pingResult map[int]int + pingResultTTL map[int]int64 + + topCount int +} + +func NewFastestFilter(pingTimeOut int, topCount int) *FastestFilter { + if pingTimeOut == 0 { + pingTimeOut = 3000 // 3s + } + return &FastestFilter{ + mu: sync.Mutex{}, + pinger: &net.Dialer{Timeout: time.Millisecond * time.Duration(pingTimeOut)}, + pingResult: make(map[int]int, 0), + pingResultTTL: make(map[int]int64, 0), + topCount: topCount, + } +} + +func (f *FastestFilter) Filter(nodes []Node) []Node { + // disabled + if f.topCount == 0 { + return nodes + } + + // get latency with ttl cache + now := time.Now().Unix() + r := rand.New(rand.NewSource(time.Now().UnixNano())) + + var getNodeLatency = func(node Node) int { + if f.pingResultTTL[node.ID] < now { + f.mu.Lock() + f.pingResultTTL[node.ID] = now + 5 // tmp + defer f.mu.Unlock() + + // get latency + go func(node Node) { + latency := f.doTcpPing(node.Addr) + ttl := 300 - int64(60*r.Float64()) + + f.mu.Lock() + f.pingResult[node.ID] = latency + f.pingResultTTL[node.ID] = now + ttl + defer f.mu.Unlock() + }(node) + } + return f.pingResult[node.ID] + } + + // sort + sort.Slice(nodes, func(i, j int) bool { + return getNodeLatency(nodes[i]) < getNodeLatency(nodes[j]) + }) + + // split + if len(nodes) <= f.topCount { + return nodes + } + + return nodes[0:f.topCount] +} + +func (f *FastestFilter) String() string { + return "fastest" +} + +// doTcpPing +func (f *FastestFilter) doTcpPing(address string) int { + start := time.Now() + conn, err := f.pinger.Dial("tcp", address) + elapsed := time.Since(start) + + if err == nil { + _ = conn.Close() + } + + latency := int(elapsed.Milliseconds()) + log.Logf("pingDoTCP: %s, latency: %d", address, latency) + return latency +} + // InvalidFilter filters the invalid node. // A node is invalid if its port is invalid (negative or zero value). type InvalidFilter struct{} diff --git a/selector_test.go b/selector_test.go index 5da667cf..7cbbfd05 100644 --- a/selector_test.go +++ b/selector_test.go @@ -127,6 +127,30 @@ func TestFailFilter(t *testing.T) { } } +func TestFastestFilter(t *testing.T) { + nodes := []Node{ + Node{ID: 1, marker: &failMarker{}, Addr: "1.0.0.1:80"}, + Node{ID: 2, marker: &failMarker{}, Addr: "1.0.0.2:80"}, + Node{ID: 3, marker: &failMarker{}, Addr: "1.0.0.3:80"}, + } + filter := NewFastestFilter(0, 2) + + var print = func(nodes []Node) []string { + var rows []string + for _, node := range nodes { + rows = append(rows, node.Addr) + } + return rows + } + + result1 := filter.Filter(nodes) + t.Logf("result 1: %+v", print(result1)) + + time.Sleep(time.Second) + result2 := filter.Filter(nodes) + t.Logf("result 2: %+v", print(result2)) +} + func TestSelector(t *testing.T) { nodes := []Node{ Node{ID: 1, marker: &failMarker{}}, From 94f812b026df6197191e0ce6a8c3cdd7355fdff3 Mon Sep 17 00:00:00 2001 From: tiancheng91 Date: Mon, 16 Jan 2023 16:05:08 +0800 Subject: [PATCH 6/7] fix: ping result ttl random --- selector.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/selector.go b/selector.go index a5f68b91..67d83c15 100644 --- a/selector.go +++ b/selector.go @@ -240,7 +240,6 @@ func (f *FastestFilter) Filter(nodes []Node) []Node { // get latency with ttl cache now := time.Now().Unix() - r := rand.New(rand.NewSource(time.Now().UnixNano())) var getNodeLatency = func(node Node) int { if f.pingResultTTL[node.ID] < now { @@ -251,7 +250,8 @@ func (f *FastestFilter) Filter(nodes []Node) []Node { // get latency go func(node Node) { latency := f.doTcpPing(node.Addr) - ttl := 300 - int64(60*r.Float64()) + r := rand.New(rand.NewSource(time.Now().UnixNano())) + ttl := 300 - int64(120*r.Float64()) f.mu.Lock() f.pingResult[node.ID] = latency From 77262f24547bb9b2831a423a419532782e2be62b Mon Sep 17 00:00:00 2001 From: tiancheng91 Date: Tue, 17 Jan 2023 20:01:34 +0800 Subject: [PATCH 7/7] fix: concurrent map access --- selector.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/selector.go b/selector.go index 67d83c15..ccac77ea 100644 --- a/selector.go +++ b/selector.go @@ -242,10 +242,11 @@ func (f *FastestFilter) Filter(nodes []Node) []Node { now := time.Now().Unix() var getNodeLatency = func(node Node) int { + f.mu.Lock() + defer f.mu.Unlock() + if f.pingResultTTL[node.ID] < now { - f.mu.Lock() f.pingResultTTL[node.ID] = now + 5 // tmp - defer f.mu.Unlock() // get latency go func(node Node) { @@ -254,9 +255,10 @@ func (f *FastestFilter) Filter(nodes []Node) []Node { ttl := 300 - int64(120*r.Float64()) f.mu.Lock() + defer f.mu.Unlock() + f.pingResult[node.ID] = latency f.pingResultTTL[node.ID] = now + ttl - defer f.mu.Unlock() }(node) } return f.pingResult[node.ID]