Skip to content

Commit

Permalink
feat: optimize code (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
wencaiwulue authored Feb 17, 2024
1 parent d7ca7ba commit dafc4e4
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 24 deletions.
2 changes: 2 additions & 0 deletions cmd/cmds/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmds
import (
"context"
"os"
"os/exec"
"os/signal"

"github.com/spf13/cobra"
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
18 changes: 4 additions & 14 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/client/udphandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,24 @@ 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
}
}
log.Debugf("[TUN-UDP] Debug: read length %d data from remote", written)
}()
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
Expand Down

0 comments on commit dafc4e4

Please sign in to comment.