From 0da16a351753eb1bd9b807a01d1b419711226b70 Mon Sep 17 00:00:00 2001
From: atanarjuat <atanarjuat@example.com>
Date: Mon, 23 May 2022 01:48:14 +0200
Subject: [PATCH] wip: use kcp in the server

---
 .gitignore     |  5 +++--
 client/main.go |  2 ++
 dialer.go      |  1 -
 listener.go    | 26 ++++++++++++++++++++------
 server/main.go |  6 +++++-
 5 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/.gitignore b/.gitignore
index b21dfe4..59de507 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
-obfsproxy/obfsproxy
-obfsclient/obfsclient
+client/client
+server/server
+obfsvpn-client
 *.swp
 *.swo
diff --git a/client/main.go b/client/main.go
index daf6ce4..80ebb35 100644
--- a/client/main.go
+++ b/client/main.go
@@ -51,6 +51,8 @@ func main() {
 		logger.Fatalf("cannot get dialer: %v", err)
 	}
 
+	// TODO make this configurable via a Config struct
+	// TODO make sure we're disabling the crypto options for KCP
 	if os.Getenv("KCP") == "1" {
 		dialer.DialFunc = func(network, address string) (net.Conn, error) {
 			return kcp.Dial(address)
diff --git a/dialer.go b/dialer.go
index a269c4f..db4c6ba 100644
--- a/dialer.go
+++ b/dialer.go
@@ -158,7 +158,6 @@ func (d *Dialer) dial(ctx context.Context, network, address string, f func(netwo
 		log.Println("REPLACING DIALFUNC")
 		f = d.DialFunc
 	}
-	log.Printf(">>> client factory dial. f=%v", f)
 
 	return d.clientFactory.Dial(network, address, f, args)
 }
diff --git a/listener.go b/listener.go
index 30ad401..70b4503 100644
--- a/listener.go
+++ b/listener.go
@@ -10,11 +10,16 @@ import (
 	"net"
 
 	pt "git.torproject.org/pluggable-transports/goptlib.git"
+	"github.com/xtaci/kcp-go"
 	"gitlab.com/yawning/obfs4.git/common/ntor"
 	"gitlab.com/yawning/obfs4.git/transports/base"
 	"gitlab.com/yawning/obfs4.git/transports/obfs4"
 )
 
+const (
+	netKCP = "kcp"
+)
+
 // ListenConfig contains options for listening to an address.
 // If Seed is not set it defaults to a randomized value.
 // If StateDir is not set the current working directory is used.
@@ -79,7 +84,7 @@ func NewListenConfigCert(cert string) (*ListenConfig, error) {
 }
 
 // Wrap takes an existing net.Listener and wraps it in a listener that is
-// configured to perform the ntor handshake.
+// configured to perform the ntor handshake and copy data through the obfuscated conn.
 // Values from the inner net.ListenConfig are ignored.
 func (lc *ListenConfig) Wrap(ctx context.Context, ln net.Listener) (*Listener, error) {
 	args := make(pt.Args)
@@ -105,13 +110,22 @@ func (lc *ListenConfig) Wrap(ctx context.Context, ln net.Listener) (*Listener, e
 	return &Listener{sf: sf, ln: ln}, nil
 }
 
-// Listen announces on the local network address.
-//
+// Listen listens on the local network address.
 // See func net.Dial for a description of the network and address parameters.
 func (lc *ListenConfig) Listen(ctx context.Context, network, address string) (*Listener, error) {
-	ln, err := lc.ListenConfig.Listen(ctx, network, address)
-	if err != nil {
-		return nil, err
+	var ln net.Listener
+	var err error
+	switch network {
+	case netKCP:
+		ln, err = kcp.Listen(address)
+		if err != nil {
+			return nil, err
+		}
+	default:
+		ln, err = lc.ListenConfig.Listen(ctx, network, address)
+		if err != nil {
+			return nil, err
+		}
 	}
 	return lc.Wrap(ctx, ln)
 }
diff --git a/server/main.go b/server/main.go
index 1b9f399..3ade4dc 100644
--- a/server/main.go
+++ b/server/main.go
@@ -85,17 +85,21 @@ func main() {
 	// Setup graceful shutdown.
 	ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
 
+	// TODO pass a "mode" ? (kcp)
 	listenConfig, err := obfsvpn.NewListenConfig(
 		cfg.NodeID, cfg.PrivateKey, cfg.PublicKey,
 		cfg.DRBGSeed,
-		stateDir)
+		stateDir,
+	)
 	if err != nil {
 		logger.Fatalf("Error creating listener from config: %v", err)
 	}
 
 	logger.Printf("DEBUG: %v", listenConfig)
 
+	// TODO: pass kcp mode
 	ln, err := listenConfig.Listen(ctx, "tcp", addr)
+
 	if err != nil {
 		logger.Fatalf("error binding to %s: %v", addr, err)
 	}
-- 
GitLab