From dafc4e4193a9ad7f91d21e23252db738853d9658 Mon Sep 17 00:00:00 2001 From: naison <895703375@qq.com> Date: Sat, 17 Feb 2024 20:16:22 +0800 Subject: [PATCH] feat: optimize code (#3) --- cmd/cmds/server.go | 2 ++ go.mod | 2 +- go.sum | 6 ++++-- pkg/client/client.go | 18 ++++-------------- pkg/client/udphandler.go | 14 +++++++------- 5 files changed, 18 insertions(+), 24 deletions(-) diff --git a/cmd/cmds/server.go b/cmd/cmds/server.go index 78a4730..ec672b1 100644 --- a/cmd/cmds/server.go +++ b/cmd/cmds/server.go @@ -3,6 +3,7 @@ package cmds import ( "context" "os" + "os/exec" "os/signal" "github.com/spf13/cobra" @@ -25,6 +26,7 @@ func CmdServer() *cobra.Command { <-signals cancelFunc() }() + _ = exec.Command("ulimit", "-HSn", "102400").Run() return server.Serve(ctx, config.TCPPort, config.UDPPort) }, SilenceUsage: true, diff --git a/go.mod b/go.mod index d7bb1ac..0c59892 100755 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.7.0 - github.com/wencaiwulue/kubevpn/v2 v2.2.3-0.20240215134604-46fcf5521f27 + github.com/wencaiwulue/kubevpn/v2 v2.2.3-0.20240217121107-1f32a129b698 golang.org/x/crypto v0.18.0 golang.org/x/net v0.20.0 gvisor.dev/gvisor v0.0.0-20230603040744-5c9219dedd33 diff --git a/go.sum b/go.sum index efe5092..e9a045c 100755 --- a/go.sum +++ b/go.sum @@ -875,8 +875,10 @@ github.com/vishvananda/netlink v1.1.1-0.20201029203352-d40f9887b852/go.mod h1:tw github.com/vishvananda/netns v0.0.0-20180720170159-13995c7128cc/go.mod h1:ZjcWmFBXmLKZu9Nxj3WKYEafiSqer2rnvPr0en9UNpI= github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU= github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0= -github.com/wencaiwulue/kubevpn/v2 v2.2.3-0.20240215134604-46fcf5521f27 h1:5ZhSBUjB+jFouBE3vVkdw5ivPdH9Ui2K4sAsy9SaPTA= -github.com/wencaiwulue/kubevpn/v2 v2.2.3-0.20240215134604-46fcf5521f27/go.mod h1:zQ0OPDSjBv24+nfsWtNcb94fowfjk2i5bBSt1dBIfwE= +github.com/wencaiwulue/kubevpn/v2 v2.2.3-0.20240217103841-597023bdeabb h1:aHJJ0Wn6zLL4CULwkzC9ZmAEIeI8mujdrbd1TWVVJZs= +github.com/wencaiwulue/kubevpn/v2 v2.2.3-0.20240217103841-597023bdeabb/go.mod h1:zQ0OPDSjBv24+nfsWtNcb94fowfjk2i5bBSt1dBIfwE= +github.com/wencaiwulue/kubevpn/v2 v2.2.3-0.20240217121107-1f32a129b698 h1:0qg7Xm5dmb4J8H+0SskgY+/YXXViNVEvtjMJxLvaI5M= +github.com/wencaiwulue/kubevpn/v2 v2.2.3-0.20240217121107-1f32a129b698/go.mod h1:zQ0OPDSjBv24+nfsWtNcb94fowfjk2i5bBSt1dBIfwE= github.com/willf/bitset v1.1.11-0.20200630133818-d5bec3311243/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4= github.com/willf/bitset v1.1.11/go.mod h1:83CECat5yLh5zVOf4P1ErAgKA5UDvKtgyUABdr3+MjI= github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= diff --git a/pkg/client/client.go b/pkg/client/client.go index d41c82f..0653ed1 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -14,8 +14,6 @@ import ( log "github.com/sirupsen/logrus" pkgtun "github.com/wencaiwulue/kubevpn/v2/pkg/tun" pkgutil "github.com/wencaiwulue/kubevpn/v2/pkg/util" - "golang.org/x/crypto/ssh" - "github.com/wencaiwulue/tlstunnel/pkg/config" pkgdns "github.com/wencaiwulue/tlstunnel/pkg/dns" "github.com/wencaiwulue/tlstunnel/pkg/tun" @@ -39,7 +37,7 @@ func Connect(ctx context.Context, CIDRs []string, conf pkgutil.SshConfig) error if err != nil { return err } - err = portMap(ctx, client, portPair) + err = portMap(ctx, &conf, portPair) if err != nil { return err } @@ -78,20 +76,12 @@ func Connect(ctx context.Context, CIDRs []string, conf pkgutil.SshConfig) error }, }) } - routes = append(routes, - types.Route{ - Dst: net.IPNet{ - IP: net.ParseIP("142.250.0.0"), - Mask: net.CIDRMask(16, 32), - }, - }, - ) ipv4 := net.IPv4(223, 253, 0, 1) ipv6 := net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1} tunConf := pkgtun.Config{ Addr: (&net.IPNet{IP: ipv4, Mask: net.CIDRMask(32, 32)}).String(), Addr6: (&net.IPNet{IP: ipv6, Mask: net.CIDRMask(128, 128)}).String(), - MTU: 1350, + MTU: 1500, Routes: routes, } listener, err := pkgtun.Listener(tunConf) @@ -124,7 +114,7 @@ func Connect(ctx context.Context, CIDRs []string, conf pkgutil.SshConfig) error } // portPair is local:remote -func portMap(ctx context.Context, client *ssh.Client, portPair []string) error { +func portMap(ctx context.Context, conf *pkgutil.SshConfig, portPair []string) error { for _, s := range portPair { ports := strings.Split(s, ":") if len(ports) != 2 { @@ -139,7 +129,7 @@ func portMap(ctx context.Context, client *ssh.Client, portPair []string) error { if err != nil { return err } - err = pkgutil.PortMapUntil(ctx, client, remote, local) + err = pkgutil.PortMapUntil(ctx, conf, remote, local) if err != nil { return err } diff --git a/pkg/client/udphandler.go b/pkg/client/udphandler.go index 142d71d..21ad6c2 100644 --- a/pkg/client/udphandler.go +++ b/pkg/client/udphandler.go @@ -84,16 +84,16 @@ func UDPHandler(s *stack.Stack, device *net.Interface, udpAddr string) func(id s defer config.LPool.Put(i[:]) var written int for { - n, err3 := remote.Read(i[:]) - if err3 != nil { - errChan <- err3 + n, err := remote.Read(i[:]) + if err != nil { + errChan <- err break } written += n addRoute(i, n, r, device.Name) - _, err3 = conn.Write(i[:n]) - if err3 != nil { - errChan <- err3 + _, err = conn.Write(i[:n]) + if err != nil { + errChan <- err break } } @@ -101,7 +101,7 @@ func UDPHandler(s *stack.Stack, device *net.Interface, udpAddr string) func(id s }() err = <-errChan if err != nil && !errors.Is(err, io.EOF) { - log.Debugf("[TUN-UDP] Error: dsiconnect: %s >-<: %s: %v", conn.LocalAddr(), remote.RemoteAddr(), err) + log.Debugf("[TUN-UDP] Error: disconnect: %s >-<: %s: %v", conn.LocalAddr(), remote.RemoteAddr(), err) } }() }).HandlePacket