Skip to content

Commit

Permalink
feat: optimize code (#1)
Browse files Browse the repository at this point in the history
* feat: use library

---------

Co-authored-by: naison <895703375@qq.com>
  • Loading branch information
wencaiwulue authored Jan 25, 2024
1 parent 895fd9c commit 028cf0a
Show file tree
Hide file tree
Showing 40 changed files with 1,960 additions and 1,204 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
## TLS Tunnel
## SSH-VPN

A safety virtual personal network with TLS or DTLS
A safety virtual personal network over SSH and Gvisor
7 changes: 0 additions & 7 deletions TODO.md

This file was deleted.

62 changes: 0 additions & 62 deletions cmd/client/main.go

This file was deleted.

72 changes: 72 additions & 0 deletions cmd/cmds/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package cmds

import (
"context"
"os"
"os/signal"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/wencaiwulue/kubevpn/v2/pkg/util"

"github.com/wencaiwulue/tlstunnel/pkg/client"
"github.com/wencaiwulue/tlstunnel/pkg/config"
)

func CmdClient() *cobra.Command {
var mode config.ProxyType
var pacPath string
var extraCIDR []string
var sshConf util.SshConfig

cmd := &cobra.Command{
Use: "client",
Short: "client to connect server",
Long: `client to connect server`,
RunE: func(cmd *cobra.Command, args []string) error {
switch mode {
case config.ProxyTypeGlobe:
//if len(remote) == 0 {
// log.Fatal("Globe mode, remote ip should not be empty")
//}
case config.ProxyTypePAC:
if len(pacPath) == 0 {
log.Fatal("PAC mode, PAC path should not be empty")
}
default:
log.Fatal("Not support proxy mode " + mode)
}
ctx, cancelFunc := context.WithCancel(cmd.Context())
defer cancelFunc()
go func() {
signals := make(chan os.Signal)
signal.Notify(signals, os.Kill, os.Interrupt)
<-signals
cancelFunc()
}()
return client.Connect(ctx, extraCIDR, sshConf)
},
SilenceUsage: true,
}
cmd.Flags().StringVar((*string)(&mode), "mode", string(config.ProxyTypeGlobe), "Only support mode globe or pac")
_ = cmd.Flags().MarkHidden("mode")
cmd.Flags().StringVar(&pacPath, "pac", "", "The path of PAC, can be a url or local path")
_ = cmd.Flags().MarkHidden("pac")
cmd.Flags().IntVarP(&config.TCPPort, "tcp-port", "t", config.TCPPort, "The tcp port of remote linux server")
cmd.Flags().IntVarP(&config.UDPPort, "udp-port", "u", config.UDPPort, "The udp port of remote linux server")
cmd.Flags().StringArrayVar(&extraCIDR, "extra-cidr", []string{}, "CIDR string, eg: --cidr 192.168.0.159/24 --cidr 192.168.1.160/32")
addSshFlags(cmd, &sshConf)
return cmd
}

func addSshFlags(cmd *cobra.Command, sshConf *util.SshConfig) {
// for ssh jumper host
cmd.Flags().StringVar(&sshConf.Addr, "ssh-addr", "", "Optional ssh jump server address to dial as <hostname>:<port>, eg: 127.0.0.1:22")
cmd.Flags().StringVar(&sshConf.User, "ssh-username", "", "Optional username for ssh jump server")
cmd.Flags().StringVar(&sshConf.Password, "ssh-password", "", "Optional password for ssh jump server")
cmd.Flags().StringVar(&sshConf.Keyfile, "ssh-keyfile", "", "Optional file with private key for SSH authentication")
cmd.Flags().StringVar(&sshConf.ConfigAlias, "ssh-alias", "", "Optional config alias with ~/.ssh/config for SSH authentication")
cmd.Flags().StringVar(&sshConf.GSSAPIPassword, "gssapi-password", "", "GSSAPI password")
cmd.Flags().StringVar(&sshConf.GSSAPIKeytabConf, "gssapi-keytab", "", "GSSAPI keytab file path")
cmd.Flags().StringVar(&sshConf.GSSAPICacheFile, "gssapi-cache", "", "GSSAPI cache file path, use command `kinit -c /path/to/cache USERNAME@RELAM` to generate")
}
35 changes: 35 additions & 0 deletions cmd/cmds/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cmds

import (
"context"
"os"
"os/signal"

"github.com/spf13/cobra"

"github.com/wencaiwulue/tlstunnel/pkg/config"
"github.com/wencaiwulue/tlstunnel/pkg/server"
)

func CmdServer() *cobra.Command {
cmd := &cobra.Command{
Use: "server",
Short: "server",
Long: `server`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cancelFunc := context.WithCancel(cmd.Context())
defer cancelFunc()
go func() {
signals := make(chan os.Signal)
signal.Notify(signals, os.Kill, os.Interrupt)
<-signals
cancelFunc()
}()
return server.Serve(ctx, config.TCPPort, config.UDPPort)
},
SilenceUsage: true,
}
cmd.Flags().IntVarP(&config.TCPPort, "tcp-port", "t", config.TCPPort, "server listen tcp port")
cmd.Flags().IntVarP(&config.UDPPort, "udp-port", "u", config.UDPPort, "server listen udp port")
return cmd
}
34 changes: 34 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"context"
_ "net/http/pprof"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/wencaiwulue/tlstunnel/cmd/cmds"
)

func main() {
cmd := NewTunnelCommand()
logrus.SetLevel(logrus.DebugLevel)
cmd.AddCommand(
cmds.CmdServer(),
cmds.CmdClient(),
)
_ = cmd.ExecuteContext(context.Background())
}

func NewTunnelCommand() *cobra.Command {
return &cobra.Command{
Use: "tunnel",
Short: "connect to remote network",
Long: `
connect to remote network.
`,
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
}
129 changes: 0 additions & 129 deletions cmd/server/server.go

This file was deleted.

Loading

0 comments on commit 028cf0a

Please sign in to comment.