diff options
Diffstat (limited to 'client')
-rw-r--r-- | client/client.go | 55 | ||||
-rw-r--r-- | client/main.go | 102 |
2 files changed, 55 insertions, 102 deletions
diff --git a/client/client.go b/client/client.go new file mode 100644 index 0000000..c0a42e0 --- /dev/null +++ b/client/client.go @@ -0,0 +1,55 @@ +package client + +import ( + "fmt" + "log" + "net" + + "0xacab.org/leap/obfsvpn" + + "github.com/kalikaneko/socks5" + "github.com/xtaci/kcp-go" +) + +type Client struct { + kcp bool + socksAddr string + obfs4Cert string +} + +func NewClient(kcp bool, socksAddr, obfs4Cert string) *Client { + return &Client{ + kcp: kcp, + socksAddr: socksAddr, + obfs4Cert: obfs4Cert, + } +} + +func (c *Client) Start() bool { + server := &socks5.Server{ + Addr: c.socksAddr, + BindIP: "127.0.0.1", + } + + dialer, err := obfsvpn.NewDialerFromCert(c.obfs4Cert) + if err != nil { + log.Printf("Error getting dialer: %v\n", err) + return false + } + + if c.kcp { + dialer.DialFunc = func(network, address string) (net.Conn, error) { + log.Printf("Dialing kcp://%s\n", address) + return kcp.Dial(address) + } + } + + server.Dial = dialer.Dial + + fmt.Printf("[+] Starting socks5 proxy at %s\n", c.socksAddr) + if err := server.ListenAndServe(); err != nil { + log.Printf("error while listening: %v\n", err) + return false + } + return true +} diff --git a/client/main.go b/client/main.go deleted file mode 100644 index b536a81..0000000 --- a/client/main.go +++ /dev/null @@ -1,102 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "io" - "log" - "net" - "os" - - "0xacab.org/leap/obfsvpn" - - "github.com/kalikaneko/socks5" - "github.com/xtaci/kcp-go" -) - -func main() { - // Setup logging. - logger := log.New(os.Stderr, "", log.LstdFlags) - debug := log.New(io.Discard, "DEBUG ", log.LstdFlags) - - // setup command line flags. - var ( - verbose bool - obfs4Cert string - obfs4Remote string - socksPort string - socksHost string - ) - - socksPort = "8080" - socksHost = "127.0.0.1" - - flags := flag.NewFlagSet(os.Args[0], flag.ContinueOnError) - flags.BoolVar(&verbose, "v", verbose, "Enable verbose logging") - flags.StringVar(&obfs4Cert, "c", obfs4Cert, "The remote obfs4 certificate") - flags.StringVar(&obfs4Remote, "r", obfs4Remote, "The remote obfs4 endpoint (ip:port)") - flags.StringVar(&socksPort, "p", socksPort, "The port for the local socks5 proxy (default: 8080)") - flags.StringVar(&socksHost, "i", socksHost, "The host for the local socks5 proxy (default: localhost)") - err := flags.Parse(os.Args[1:]) - if err != nil { - logger.Fatalf("error parsing flags: %v", err) - } - // Configure logging. - if verbose { - debug.SetOutput(os.Stderr) - } - - kcpTransport := false - // TODO make this configurable via a Config struct - // TODO make sure we're disabling all the crypto options for KCP - if os.Getenv("KCP") == "1" { - kcpTransport = true - } - - socksAddr := net.JoinHostPort(socksHost, socksPort) - client := NewClient(kcpTransport, socksAddr, obfs4Cert) - client.Start() -} - -type Client struct { - kcp bool - socksAddr string - obfs4Cert string -} - -func NewClient(kcp bool, socksAddr, obfs4Cert string) *Client { - return &Client{ - kcp: kcp, - socksAddr: socksAddr, - obfs4Cert: obfs4Cert, - } -} - -func (c *Client) Start() bool { - server := &socks5.Server{ - Addr: c.socksAddr, - BindIP: "127.0.0.1", - } - - dialer, err := obfsvpn.NewDialerFromCert(c.obfs4Cert) - if err != nil { - log.Printf("Error getting dialer: %v\n", err) - return false - } - - if c.kcp { - dialer.DialFunc = func(network, address string) (net.Conn, error) { - log.Printf("Dialing kcp://%s\n", address) - return kcp.Dial(address) - } - } - - server.Dial = dialer.Dial - - fmt.Printf("[+] Starting socks5 proxy at %s\n", c.socksAddr) - if err := server.ListenAndServe(); err != nil { - log.Printf("error while listening: %v\n", err) - return false - } - return true -} |