From 5b6f18cc07f6c068ca05c851c28442ff5e86f5a5 Mon Sep 17 00:00:00 2001 From: Antonio Date: Mon, 12 Jun 2023 10:42:05 -0500 Subject: [PATCH 01/11] fix: fixed dockerfile of sshd --- docker-compose.yaml | 15 ++++----------- sshd/Dockerfile | 24 ++++++++++++++++++++++++ sshd/default.go | 2 +- sshd/entrypoint.sh | 7 ------- 4 files changed, 29 insertions(+), 19 deletions(-) create mode 100644 sshd/Dockerfile delete mode 100644 sshd/entrypoint.sh diff --git a/docker-compose.yaml b/docker-compose.yaml index 1e51255..8e699b8 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -15,19 +15,12 @@ services: - "127.0.0.1:8000:80" command: ["-listen", ":80", "-text", "ECHO"] sshd: - image: panubo/sshd:latest + build: + context: sshd + dockerfile: Dockerfile container_name: fullproxy-sshd restart: unless-stopped networks: - fullproxy ports: - - "127.0.0.1:22222:22" - #- "[::1]:22222:22" - volumes: - - ./sshd/entrypoint.sh:/etc/entrypoint.d/entrypoint.sh:ro - environment: - - SSH_ENABLE_PASSWORD_AUTH=true - - GATEWAY_PORTS=true - - TCP_FORWARDING=true - - SSH_ENABLE_ROOT=true - - SSH_ENABLE_ROOT_PASSWORD_AUTH=true \ No newline at end of file + - "127.0.0.1:22222:22" \ No newline at end of file diff --git a/sshd/Dockerfile b/sshd/Dockerfile new file mode 100644 index 0000000..3c56015 --- /dev/null +++ b/sshd/Dockerfile @@ -0,0 +1,24 @@ +FROM alpine:latest + +RUN apk --update add --no-cache openssh bash +# Comment old configs +RUN sed -i 's/^PermitRootLogin.*/#PermitRootLogin yes/' /etc/ssh/sshd_config +RUN sed -i 's/^AllowTcpForwarding.*/#AllowTcpForwarding yes/' /etc/ssh/sshd_config +RUN sed -i 's/^PermitOpen.*/#PermitOpen any/' /etc/ssh/sshd_config +RUN sed -i 's/^PermitTunnel.*/#PermitTunnel yes/' /etc/ssh/sshd_config +RUN sed -i 's/^Port.*/#Port 22/g' /etc/ssh/sshd_config +# New Config +RUN echo "PermitRootLogin yes" >> /etc/ssh/sshd_config +RUN echo "AllowTcpForwarding yes" >> /etc/ssh/sshd_config +RUN echo "PermitOpen any" >> /etc/ssh/sshd_config +RUN echo "PermitTunnel yes" >> /etc/ssh/sshd_config +RUN echo "Port 22" >> /etc/ssh/sshd_config +# +RUN echo "root:password" | chpasswd +RUN rm -rf /var/cache/apk/* +RUN /usr/bin/ssh-keygen -A +RUN ssh-keygen -t rsa -b 4096 -f /etc/ssh/ssh_host_key +ENV NOTVISIBLE "in users profile" +RUN echo "export VISIBLE=now" >> /etc/profile +EXPOSE 22 +CMD ["/usr/sbin/sshd", "-D"] \ No newline at end of file diff --git a/sshd/default.go b/sshd/default.go index c36f0de..88becd7 100644 --- a/sshd/default.go +++ b/sshd/default.go @@ -9,7 +9,7 @@ import ( ) const ( - SSHDefaultUser = "low" + SSHDefaultUser = "root" SSHDefaultPassword = "password" DefaultAddr = "localhost:22222" ) diff --git a/sshd/entrypoint.sh b/sshd/entrypoint.sh deleted file mode 100644 index 611f9c4..0000000 --- a/sshd/entrypoint.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env bash - -set -e - -adduser -h /opt/low -s /bin/bash -D low - -echo "low:password" | chpasswd \ No newline at end of file From 067087baa2f53f89a4449dea44a639a67bafd3a7 Mon Sep 17 00:00:00 2001 From: Antonio Date: Mon, 12 Jun 2023 10:51:09 -0500 Subject: [PATCH 02/11] feat: added clean step to github actions --- .github/workflows/release.yml | 2 ++ .github/workflows/test.yml | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a6c54ae..664128e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -15,6 +15,8 @@ jobs: go-version: "1.20" - name: Set up docker environment run: docker compose up -d + - name: Clean + run: go clean && go clean -cache - name: Test run: go test -p 1 -count 1 -coverpkg ./... -coverprofile coverage.txt -covermode count ./... - uses: codecov/codecov-action@v3 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 97cacc3..c050c1b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -13,6 +13,8 @@ jobs: uses: actions/setup-go@v3 with: go-version: "1.20" + - name: Clean + run: go clean && go clean -cache - name: Build run: go build . @@ -26,6 +28,8 @@ jobs: go-version: "1.20" - name: Set up docker environment run: docker compose up -d + - name: Clean + run: go clean && go clean -cache - name: Test run: go test -p 1 -count 1 -v ./... - name: Clean docker environment From d4317207d81b665009164d81a71845e9b855d789 Mon Sep 17 00:00:00 2001 From: Antonio Date: Mon, 12 Jun 2023 11:04:12 -0500 Subject: [PATCH 03/11] fix: removed chan sync method from forward proxy test --- proxies/forward_test.go | 64 ++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/proxies/forward_test.go b/proxies/forward_test.go index 6203c81..7a28882 100644 --- a/proxies/forward_test.go +++ b/proxies/forward_test.go @@ -20,27 +20,32 @@ func TestForward_Addr(t *testing.T) { Dial: net.Dial, } defer f.Close() - doneChan := make(chan struct{}, 1) - defer close(doneChan) + go f.Serve() + assert.NotNil(t, f.Addr()) testMessage := []byte("TEST") go func() { - conn, aErr := service.Accept() - assert.Nil(t, aErr) + conn, err := service.Accept() + assert.Nil(t, err) defer conn.Close() - _, wErr := conn.Write(testMessage) - assert.Nil(t, wErr) - <-doneChan + // Write + _, err = conn.Write(testMessage) + assert.Nil(t, err) + // Read + buffer := make([]byte, len(testMessage)) + _, err = conn.Read(buffer) + assert.Nil(t, err) + assert.Equal(t, testMessage, buffer) }() - go f.Serve() - conn, dErr := net.Dial(listener.Addr().Network(), listener.Addr().String()) - assert.Nil(t, dErr) + conn := network.Dial(listener.Addr().String()) defer conn.Close() + // Read buffer := make([]byte, len(testMessage)) - _, rErr := conn.Read(buffer) - assert.Nil(t, rErr) + _, err := conn.Read(buffer) + assert.Nil(t, err) assert.Equal(t, testMessage, buffer) - assert.NotNil(t, f.Addr()) - doneChan <- struct{}{} + // Write + _, err = conn.Write(buffer) + assert.Nil(t, err) } func TestBasicLocalForward(t *testing.T) { @@ -55,25 +60,30 @@ func TestBasicLocalForward(t *testing.T) { Dial: net.Dial, } defer f.Close() - doneChan := make(chan struct{}, 1) - defer close(doneChan) + go f.Serve() testMessage := []byte("TEST") go func() { - conn, aErr := service.Accept() - assert.Nil(t, aErr) + conn, err := service.Accept() + assert.Nil(t, err) defer conn.Close() - _, wErr := conn.Write(testMessage) - assert.Nil(t, wErr) - <-doneChan + // Write + _, err = conn.Write(testMessage) + assert.Nil(t, err) + // Read + buffer := make([]byte, len(testMessage)) + _, err = conn.Read(buffer) + assert.Nil(t, err) + assert.Equal(t, testMessage, buffer) }() - go f.Serve() - conn, dErr := net.Dial(listener.Addr().Network(), listener.Addr().String()) - assert.Nil(t, dErr) + conn := network.Dial(listener.Addr().String()) defer conn.Close() + // Read buffer := make([]byte, len(testMessage)) - _, rErr := conn.Read(buffer) - assert.Nil(t, rErr) + _, err := conn.Read(buffer) + assert.Nil(t, err) assert.Equal(t, testMessage, buffer) - doneChan <- struct{}{} + // Write + _, err = conn.Write(testMessage) + assert.Nil(t, err) } From 800ae4fe0305621d2466330cd3f2f4e4ab322606 Mon Sep 17 00:00:00 2001 From: Antonio Date: Mon, 12 Jun 2023 11:13:31 -0500 Subject: [PATCH 04/11] fix: improved http proxy tests --- proxies/http_test.go | 64 +++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/proxies/http_test.go b/proxies/http_test.go index b26c99f..9f86e48 100644 --- a/proxies/http_test.go +++ b/proxies/http_test.go @@ -25,12 +25,13 @@ func TestHTTP_Addr(t *testing.T) { } func TestHTTP_Serve(t *testing.T) { - setupHTTP := func(tt *testing.T, proxy, service net.Listener) *httpexpect.Expect { - httputils.NewMux(service) + doProxyTest := func(tt *testing.T, proxy, targetService net.Listener) { + // Setup expect + httputils.NewMux(targetService) proxyUrl, _ := url.Parse("http://" + proxy.Addr().String()) - return httpexpect.WithConfig( + expect := httpexpect.WithConfig( httpexpect.Config{ - BaseURL: "http://" + service.Addr().String(), + BaseURL: "http://" + targetService.Addr().String(), Reporter: httpexpect.NewAssertReporter(t), Client: &http.Client{ Transport: &http.Transport{ @@ -39,53 +40,50 @@ func TestHTTP_Serve(t *testing.T) { }, }, ) - } - t.Run("Basic", func(tt *testing.T) { - service := network.ListenAny() - defer service.Close() - listener := network.ListenAny() - defer listener.Close() - expect := setupHTTP(tt, listener, service) + // HTTP h := HTTP{ - Listener: listener, + Listener: proxy, Dial: net.Dial, } defer h.Close() go h.Serve() expect.GET(httputils.EchoRoute).Expect().Status(http.StatusOK).Body().Contains(httputils.EchoMsg) - }) - t.Run("Reverse", func(tt *testing.T) { + } + t.Run("Basic", func(tt *testing.T) { + // - Proxy + listener := network.ListenAny() + defer listener.Close() + // - Service service := network.ListenAny() defer service.Close() + // Run test + doProxyTest(tt, listener, service) + }) + t.Run("Reverse", func(tt *testing.T) { + // - Proxy data := network.ListenAny() defer data.Close() control := network.ListenAny() defer control.Close() master := network.Dial(control.Addr().String()) defer master.Close() - controlCh := make(chan struct{}, 1) - defer close(controlCh) - go func() { - s := &reverse.Slave{ - Master: master, - } - defer s.Close() - go s.Serve() - <-controlCh - }() + // - Service + service := network.ListenAny() + defer service.Close() + // Slave + s := &reverse.Slave{ + Master: master, + } + defer s.Close() + go s.Serve() + // Master m := &reverse.Master{ Data: data, Control: control, } - expect := setupHTTP(tt, data, service) - h := HTTP{ - Listener: m, - Dial: net.Dial, - } - defer h.Close() - go h.Serve() - expect.GET(httputils.EchoRoute).Expect().Status(http.StatusOK).Body().Contains(httputils.EchoMsg) - controlCh <- struct{}{} + defer m.Close() + // Run test + doProxyTest(tt, data, service) }) } From a3260d83c40c7b9a2b0bafb130f9a72e8eb31895 Mon Sep 17 00:00:00 2001 From: Antonio Date: Mon, 12 Jun 2023 11:28:05 -0500 Subject: [PATCH 05/11] fix: improved socks5 proxy tests --- proxies/socks5_test.go | 170 ++++++++++++++++++++++++++--------------- 1 file changed, 107 insertions(+), 63 deletions(-) diff --git a/proxies/socks5_test.go b/proxies/socks5_test.go index 9119958..36c8666 100644 --- a/proxies/socks5_test.go +++ b/proxies/socks5_test.go @@ -23,91 +23,114 @@ func TestSocks5_Addr(t *testing.T) { } func TestSocks5_Listener(t *testing.T) { + // - Proxy l := network.ListenAny() defer l.Close() + // - Service service := network.ListenAny() defer service.Close() - msg := []byte("HELLO") - go func() { - conn, aErr := service.Accept() - assert.Nil(t, aErr) - _, wErr := conn.Write(msg) - assert.Nil(t, wErr) - }() + // Proxy s := Socks5{ Listener: l, Dial: net.Dial, } go s.Serve() - dialer, sErr := proxy.SOCKS5(l.Addr().Network(), l.Addr().String(), nil, nil) - assert.Nil(t, sErr) - conn, dErr := dialer.Dial(service.Addr().Network(), service.Addr().String()) - assert.Nil(t, dErr) - buffer := make([]byte, len(msg)) - _, rErr := conn.Read(buffer) - assert.Nil(t, rErr) - assert.Equal(t, msg, buffer) + // Test + testMsg := []byte("HELLO") + // - Producer + go func() { + conn, err := service.Accept() + assert.Nil(t, err) + _, err = conn.Write(testMsg) + assert.Nil(t, err) + buffer := make([]byte, len(testMsg)) + _, err = conn.Read(buffer) + assert.Nil(t, err) + }() + // - Consumer + // -- Connect proxy + dialer, err := proxy.SOCKS5(l.Addr().Network(), l.Addr().String(), nil, nil) + assert.Nil(t, err) + // -- Consume + conn, err := dialer.Dial(service.Addr().Network(), service.Addr().String()) + assert.Nil(t, err) + buffer := make([]byte, len(testMsg)) + _, err = conn.Read(buffer) + assert.Nil(t, err) + assert.Equal(t, testMsg, buffer) + // -- Send + _, err = conn.Write(buffer) + assert.Nil(t, err) } func TestSocks5_Reverse(t *testing.T) { + // - Proxy data := network.ListenAny() defer data.Close() control := network.ListenAny() defer control.Close() - master := network.Dial(control.Addr().String()) - defer master.Close() - doneChan := make(chan struct{}, 1) - defer close(doneChan) - go func() { - s := &reverse.Slave{ - Master: master, - Dial: net.Dial, - } - defer s.Close() - go s.Serve() - <-doneChan - }() - m := &reverse.Master{ + masterConn := network.Dial(control.Addr().String()) + defer masterConn.Close() + // - Service + service := network.ListenAny() + defer service.Close() + // - Slave + slave := &reverse.Slave{ + Master: masterConn, + Dial: net.Dial, + } + defer slave.Close() + go slave.Serve() + // - Master + master := &reverse.Master{ Data: data, Control: control, } - service := network.ListenAny() - defer service.Close() - msg := []byte("HELLO") + defer master.Close() + // - Setup Proxy + sockProxy := Socks5{ + Listener: master, + Dial: master.SlaveDial, + } + go sockProxy.Serve() + // - Producer + testMsg := []byte("HELLO") go func() { - conn, aErr := service.Accept() - assert.Nil(t, aErr) - _, wErr := conn.Write(msg) - assert.Nil(t, wErr) + conn, err := service.Accept() + assert.Nil(t, err) + // Write + _, err = conn.Write(testMsg) + assert.Nil(t, err) + buffer := make([]byte, len(testMsg)) + // Read + _, err = conn.Read(buffer) + assert.Nil(t, err) }() - s := Socks5{ - Listener: m, - Dial: m.SlaveDial, - } - go s.Serve() - dialer, sErr := proxy.SOCKS5(data.Addr().Network(), data.Addr().String(), nil, nil) - assert.Nil(t, sErr) - conn, dErr := dialer.Dial(service.Addr().Network(), service.Addr().String()) - assert.Nil(t, dErr) - buffer := make([]byte, len(msg)) - _, rErr := conn.Read(buffer) - assert.Nil(t, rErr) - assert.Equal(t, msg, buffer) - doneChan <- struct{}{} + // - Consumer + // -- Connect proxy + dialer, err := proxy.SOCKS5(data.Addr().Network(), data.Addr().String(), nil, nil) + assert.Nil(t, err) + // -- Connect service + conn, err := dialer.Dial(service.Addr().Network(), service.Addr().String()) + assert.Nil(t, err) + // -- Consume + buffer := make([]byte, len(testMsg)) + _, err = conn.Read(buffer) + assert.Nil(t, err) + assert.Equal(t, testMsg, buffer) + // -- Produce + _, err = conn.Write(buffer) + assert.Nil(t, err) } func TestSocks5_UsernamePassword(t *testing.T) { + // Proxy l := network.ListenAny() defer l.Close() + // Service service := network.ListenAny() defer service.Close() - msg := []byte("HELLO") - go func() { - conn, aErr := service.Accept() - assert.Nil(t, aErr) - _, wErr := conn.Write(msg) - assert.Nil(t, wErr) - }() + // Setup Proxy s := Socks5{ Listener: l, Dial: net.Dial, @@ -117,16 +140,37 @@ func TestSocks5_UsernamePassword(t *testing.T) { }, }, } + defer s.Close() go s.Serve() - dialer, sErr := proxy.SOCKS5(l.Addr().Network(), l.Addr().String(), &proxy.Auth{ + // Producer + testMsg := []byte("HELLO") + go func() { + conn, err := service.Accept() + assert.Nil(t, err) + // Write + _, err = conn.Write(testMsg) + assert.Nil(t, err) + // Read + buffer := make([]byte, len(testMsg)) + _, err = conn.Read(buffer) + assert.Nil(t, err) + }() + // Consumer + // Connect proxy + dialer, err := proxy.SOCKS5(l.Addr().Network(), l.Addr().String(), &proxy.Auth{ User: "username", Password: "password", }, nil) - assert.Nil(t, sErr) + assert.Nil(t, err) + // Connect service conn, dErr := dialer.Dial(service.Addr().Network(), service.Addr().String()) assert.Nil(t, dErr) - buffer := make([]byte, len(msg)) - _, rErr := conn.Read(buffer) - assert.Nil(t, rErr) - assert.Equal(t, msg, buffer) + // Consume + buffer := make([]byte, len(testMsg)) + _, err = conn.Read(buffer) + assert.Nil(t, err) + assert.Equal(t, testMsg, buffer) + // Produce + _, err = conn.Write(buffer) + assert.Nil(t, err) } From 380283a8b200e89e0ad499904938b64fa67fb018 Mon Sep 17 00:00:00 2001 From: Antonio Date: Mon, 12 Jun 2023 11:34:21 -0500 Subject: [PATCH 06/11] fix: ssh unit tests --- proxies/ssh_test.go | 47 ++++++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/proxies/ssh_test.go b/proxies/ssh_test.go index 19aa645..6ce21b6 100644 --- a/proxies/ssh_test.go +++ b/proxies/ssh_test.go @@ -3,7 +3,6 @@ package proxies import ( "net/http" "testing" - "time" "github.com/gavv/httpexpect/v2" "github.com/shoriwe/fullproxy/v4/reverse" @@ -14,7 +13,9 @@ import ( func TestSSH_Addr(t *testing.T) { listener := network.ListenAny() + defer listener.Close() client := sshd.DefaultClient(t) + defer client.Close() go sshd.KeepAlive(client) s := &Forward{ Listener: listener, @@ -26,7 +27,9 @@ func TestSSH_Addr(t *testing.T) { func TestSSH_Serve(t *testing.T) { t.Run("Trigger KeepAlive", func(tt *testing.T) { listener := network.ListenAny() + defer listener.Close() client := sshd.DefaultClient(tt) + defer client.Close() go sshd.KeepAlive(client) s := &Forward{ Network: "tcp", @@ -39,33 +42,18 @@ func TestSSH_Serve(t *testing.T) { expect := httpexpect.Default(tt, "http://"+listener.Addr().String()) expect.GET("/").Expect().Status(http.StatusOK).Body().Contains("ECHO") }) - t.Run("Error KeepAlive", func(tt *testing.T) { - listener := network.ListenAny() - client := sshd.DefaultClient(tt) - go sshd.KeepAlive(client) - s := &Forward{ - Network: "tcp", - Address: "echo:80", - Listener: listener, - Dial: client.Dial, - } - go s.Serve() - time.Sleep(2 * time.Second) - s.Close() - time.Sleep(2 * time.Second) - }) } func TestSSH_Serve_Local(t *testing.T) { t.Run("Basic", func(tt *testing.T) { listener := network.ListenAny() - client := sshd.DefaultClient(tt) - go sshd.KeepAlive(client) + sshClient := sshd.DefaultClient(tt) + go sshd.KeepAlive(sshClient) s := &Forward{ Network: "tcp", Address: "echo:80", Listener: listener, - Dial: client.Dial, + Dial: sshClient.Dial, } defer s.Close() go s.Serve() @@ -73,18 +61,23 @@ func TestSSH_Serve_Local(t *testing.T) { expect.GET("/").Expect().Status(http.StatusOK).Body().Contains("ECHO") }) t.Run("Reverse", func(tt *testing.T) { + // Master data := network.ListenAny() defer data.Close() control := network.ListenAny() defer control.Close() - master := network.Dial(control.Addr().String()) - defer master.Close() - listener := network.ListenAny() - defer listener.Close() + // Slave + masterConn := network.Dial(control.Addr().String()) + defer masterConn.Close() + slaveListener := network.ListenAny() + defer slaveListener.Close() + // SSH Conn + sshClient := sshd.DefaultClient(tt) + go sshd.KeepAlive(sshClient) // Slave slave := &reverse.Slave{ - Listener: listener, - Master: master, + Listener: slaveListener, + Master: masterConn, } defer slave.Close() go slave.Serve() @@ -95,13 +88,11 @@ func TestSSH_Serve_Local(t *testing.T) { } defer m.Close() // - client := sshd.DefaultClient(tt) - go sshd.KeepAlive(client) s := &Forward{ Network: "tcp", Address: "echo:80", Listener: m, - Dial: client.Dial, + Dial: sshClient.Dial, } defer s.Close() go s.Serve() From a1621a5d692e082a33c11fd9a782b953a7562c72 Mon Sep 17 00:00:00 2001 From: Antonio Date: Mon, 12 Jun 2023 11:37:37 -0500 Subject: [PATCH 07/11] fix: added conn close to socks5 proxy --- proxies/socks5_test.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/proxies/socks5_test.go b/proxies/socks5_test.go index 36c8666..24b8a96 100644 --- a/proxies/socks5_test.go +++ b/proxies/socks5_test.go @@ -41,8 +41,11 @@ func TestSocks5_Listener(t *testing.T) { go func() { conn, err := service.Accept() assert.Nil(t, err) + defer conn.Close() + // Write _, err = conn.Write(testMsg) assert.Nil(t, err) + // Read buffer := make([]byte, len(testMsg)) _, err = conn.Read(buffer) assert.Nil(t, err) @@ -51,9 +54,11 @@ func TestSocks5_Listener(t *testing.T) { // -- Connect proxy dialer, err := proxy.SOCKS5(l.Addr().Network(), l.Addr().String(), nil, nil) assert.Nil(t, err) - // -- Consume + // -- Dial service conn, err := dialer.Dial(service.Addr().Network(), service.Addr().String()) assert.Nil(t, err) + defer conn.Close() + // Consume buffer := make([]byte, len(testMsg)) _, err = conn.Read(buffer) assert.Nil(t, err) @@ -98,6 +103,7 @@ func TestSocks5_Reverse(t *testing.T) { go func() { conn, err := service.Accept() assert.Nil(t, err) + defer conn.Close() // Write _, err = conn.Write(testMsg) assert.Nil(t, err) @@ -113,6 +119,7 @@ func TestSocks5_Reverse(t *testing.T) { // -- Connect service conn, err := dialer.Dial(service.Addr().Network(), service.Addr().String()) assert.Nil(t, err) + defer conn.Close() // -- Consume buffer := make([]byte, len(testMsg)) _, err = conn.Read(buffer) @@ -147,6 +154,7 @@ func TestSocks5_UsernamePassword(t *testing.T) { go func() { conn, err := service.Accept() assert.Nil(t, err) + defer conn.Close() // Write _, err = conn.Write(testMsg) assert.Nil(t, err) @@ -165,6 +173,7 @@ func TestSocks5_UsernamePassword(t *testing.T) { // Connect service conn, dErr := dialer.Dial(service.Addr().Network(), service.Addr().String()) assert.Nil(t, dErr) + defer conn.Close() // Consume buffer := make([]byte, len(testMsg)) _, err = conn.Read(buffer) From 3686e4252ff9511235faed38074019f2e6b7f086 Mon Sep 17 00:00:00 2001 From: Antonio Date: Mon, 12 Jun 2023 11:47:42 -0500 Subject: [PATCH 08/11] fix: use waitgroup --- proxies/socks5_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/proxies/socks5_test.go b/proxies/socks5_test.go index 24b8a96..c4951f4 100644 --- a/proxies/socks5_test.go +++ b/proxies/socks5_test.go @@ -2,6 +2,7 @@ package proxies import ( "net" + "sync" "testing" "github.com/shoriwe/fullproxy/v4/reverse" @@ -38,7 +39,11 @@ func TestSocks5_Listener(t *testing.T) { // Test testMsg := []byte("HELLO") // - Producer + var wg sync.WaitGroup + defer wg.Wait() + wg.Add(1) go func() { + defer wg.Done() conn, err := service.Accept() assert.Nil(t, err) defer conn.Close() @@ -99,8 +104,12 @@ func TestSocks5_Reverse(t *testing.T) { } go sockProxy.Serve() // - Producer + var wg sync.WaitGroup + defer wg.Wait() testMsg := []byte("HELLO") + wg.Add(1) go func() { + defer wg.Done() conn, err := service.Accept() assert.Nil(t, err) defer conn.Close() @@ -151,7 +160,11 @@ func TestSocks5_UsernamePassword(t *testing.T) { go s.Serve() // Producer testMsg := []byte("HELLO") + var wg sync.WaitGroup + defer wg.Wait() + wg.Add(1) go func() { + defer wg.Done() conn, err := service.Accept() assert.Nil(t, err) defer conn.Close() From c42db26dc1d039948f38e77fb9230917e227f83f Mon Sep 17 00:00:00 2001 From: Antonio Date: Mon, 12 Jun 2023 11:49:47 -0500 Subject: [PATCH 09/11] fix: added waitgroup forward --- proxies/forward_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/proxies/forward_test.go b/proxies/forward_test.go index 7a28882..1529e47 100644 --- a/proxies/forward_test.go +++ b/proxies/forward_test.go @@ -2,6 +2,7 @@ package proxies import ( "net" + "sync" "testing" "github.com/shoriwe/fullproxy/v4/utils/network" @@ -23,7 +24,11 @@ func TestForward_Addr(t *testing.T) { go f.Serve() assert.NotNil(t, f.Addr()) testMessage := []byte("TEST") + var wg sync.WaitGroup + defer wg.Wait() + wg.Add(1) go func() { + defer wg.Done() conn, err := service.Accept() assert.Nil(t, err) defer conn.Close() @@ -62,7 +67,11 @@ func TestBasicLocalForward(t *testing.T) { defer f.Close() go f.Serve() testMessage := []byte("TEST") + var wg sync.WaitGroup + defer wg.Wait() + wg.Add(1) go func() { + defer wg.Done() conn, err := service.Accept() assert.Nil(t, err) defer conn.Close() From c09c444f44f985b1d534f2eb287aacfa9042e505 Mon Sep 17 00:00:00 2001 From: Antonio Date: Mon, 12 Jun 2023 12:27:19 -0500 Subject: [PATCH 10/11] fix: code format in master test --- reverse/master_test.go | 157 ++++++++++++++++++++++++----------------- 1 file changed, 92 insertions(+), 65 deletions(-) diff --git a/reverse/master_test.go b/reverse/master_test.go index 655c332..f0eb046 100644 --- a/reverse/master_test.go +++ b/reverse/master_test.go @@ -2,6 +2,7 @@ package reverse import ( "net" + "sync" "testing" "github.com/shoriwe/fullproxy/v4/utils/network" @@ -14,40 +15,40 @@ func TestMaster_init(t *testing.T) { defer data.Close() control := network.ListenAny() defer control.Close() - master := network.Dial(control.Addr().String()) - defer master.Close() + masterConn := network.Dial(control.Addr().String()) + defer masterConn.Close() go func() { - s := &Slave{Master: master, Dial: net.Dial} - defer s.Close() - assert.Nil(tt, s.init()) + slave := &Slave{Master: masterConn, Dial: net.Dial} + defer slave.Close() + assert.Nil(tt, slave.init()) }() - m := &Master{ + master := &Master{ Data: data, Control: control, } - defer m.Close() - assert.Nil(tt, m.init()) + defer master.Close() + assert.Nil(tt, master.init()) }) t.Run("Twice", func(tt *testing.T) { data := network.ListenAny() defer data.Close() control := network.ListenAny() defer control.Close() + masterConn := network.Dial(control.Addr().String()) + defer masterConn.Close() go func() { - master := network.Dial(control.Addr().String()) - defer master.Close() - s := &Slave{Master: master, Dial: net.Dial} - defer s.Close() - assert.Nil(tt, s.init()) - assert.Nil(tt, s.init()) + slave := &Slave{Master: masterConn, Dial: net.Dial} + defer slave.Close() + assert.Nil(tt, slave.init()) + assert.Nil(tt, slave.init()) }() - m := &Master{ + master := &Master{ Data: data, Control: control, } - defer m.Close() - assert.Nil(tt, m.init()) - assert.Nil(tt, m.init()) + defer master.Close() + assert.Nil(tt, master.init()) + assert.Nil(tt, master.init()) }) } @@ -56,11 +57,12 @@ func TestMaster_Addr(t *testing.T) { defer data.Close() control := network.ListenAny() defer control.Close() - m := &Master{ + master := &Master{ Data: data, Control: control, } - assert.NotNil(t, m.Addr()) + defer master.Close() + assert.NotNil(t, master.Addr()) } func TestMaster_Accept(t *testing.T) { t.Run("Succeed", func(tt *testing.T) { @@ -68,30 +70,46 @@ func TestMaster_Accept(t *testing.T) { defer data.Close() control := network.ListenAny() defer control.Close() - master := network.Dial(control.Addr().String()) - defer master.Close() - doneChan := make(chan struct{}, 2) - defer close(doneChan) - go func() { - s := &Slave{Master: master, Dial: net.Dial} - defer s.Close() - go s.Serve() - <-doneChan - }() - m := &Master{ + masterConn := network.Dial(control.Addr().String()) + defer masterConn.Close() + // Slave + slave := &Slave{Master: masterConn, Dial: net.Dial} + defer slave.Close() + go slave.Serve() + // Master + master := &Master{ Data: data, Control: control, } - defer m.Close() + defer master.Close() + // Producer + testMsg := []byte("MSG") + var wg sync.WaitGroup + defer wg.Wait() + wg.Add(1) go func() { - aConn, aErr := m.Accept() - assert.Nil(tt, aErr) - defer aConn.Close() - <-doneChan + defer wg.Done() + conn, err := master.Accept() + assert.Nil(tt, err) + defer conn.Close() + // Write + _, err = conn.Write(testMsg) + assert.Nil(tt, err) + // Read + buffer := make([]byte, len(testMsg)) + _, err = conn.Read(buffer) + assert.Nil(tt, err) }() - aConn := network.Dial(data.Addr().String()) - defer aConn.Close() - doneChan <- struct{}{} + // Consumer + conn := network.Dial(data.Addr().String()) + defer conn.Close() + // Read + buffer := make([]byte, len(testMsg)) + _, err := conn.Read(buffer) + assert.Nil(tt, err) + // Write + _, err = conn.Write(testMsg) + assert.Nil(tt, err) }) } @@ -101,41 +119,50 @@ func TestMaster_SlaveDial(t *testing.T) { defer data.Close() control := network.ListenAny() defer control.Close() - master := network.Dial(control.Addr().String()) - defer master.Close() + masterConn := network.Dial(control.Addr().String()) + defer masterConn.Close() service := network.ListenAny() defer service.Close() - doneChan := make(chan struct{}, 2) - defer close(doneChan) + // Slave + slave := &Slave{Master: masterConn, Dial: net.Dial} + defer slave.Close() + go slave.Serve() + // Master + master := &Master{ + Data: data, + Control: control, + } + defer master.Close() + // Producer testMessage := []byte("TEST") + var wg sync.WaitGroup + defer wg.Wait() + wg.Add(1) go func() { - c, err := service.Accept() + defer wg.Done() + conn, err := service.Accept() assert.Nil(tt, err) - defer c.Close() - _, err = c.Write(testMessage) + defer conn.Close() + // Write + _, err = conn.Write(testMessage) + assert.Nil(tt, err) + // Read + buffer := make([]byte, len(testMessage)) + _, err = conn.Read(buffer) assert.Nil(tt, err) - <-doneChan - }() - go func() { - s := &Slave{Master: master, Dial: net.Dial} - defer s.Close() - go s.Serve() - <-doneChan }() - m := &Master{ - Data: data, - Control: control, - } - defer m.Close() - serviceConn, dialErr := m.SlaveDial("tcp", service.Addr().String()) - assert.Nil(tt, dialErr) - defer serviceConn.Close() + // Consumer + conn, err := master.SlaveDial("tcp", service.Addr().String()) + assert.Nil(tt, err) + defer conn.Close() + // Read buffer := make([]byte, len(testMessage)) - _, rErr := serviceConn.Read(buffer) - assert.Nil(tt, rErr) + _, err = conn.Read(buffer) + assert.Nil(tt, err) assert.Equal(tt, testMessage, buffer) - doneChan <- struct{}{} - doneChan <- struct{}{} + // Write + _, err = conn.Write(testMessage) + assert.Nil(tt, err) }) t.Run("Not listening", func(tt *testing.T) { data := network.ListenAny() From 44904977bc841a5dfa9e97fb75383aa4070c756e Mon Sep 17 00:00:00 2001 From: Antonio Date: Mon, 12 Jun 2023 12:58:45 -0500 Subject: [PATCH 11/11] fix: compose network test improvement --- compose/network_test.go | 65 +++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/compose/network_test.go b/compose/network_test.go index 1f103e3..fe3719d 100644 --- a/compose/network_test.go +++ b/compose/network_test.go @@ -2,6 +2,7 @@ package compose import ( "net" + "sync" "testing" "github.com/shoriwe/fullproxy/v4/reverse" @@ -125,7 +126,7 @@ func TestNetwork_setupMasterListener(t *testing.T) { assert.NotNil(tt, err) }) t.Run("SlaveListener", func(tt *testing.T) { - l := Network{ + m := Network{ SlaveListener: new(bool), Data: &Network{ Type: NetworkBasic, @@ -138,47 +139,55 @@ func TestNetwork_setupMasterListener(t *testing.T) { Address: new(string), }, } - *l.SlaveListener = true - *l.Data.Network = "tcp" - *l.Data.Address = "localhost:0" - *l.Control.Network = "tcp" - *l.Control.Address = "localhost:0" - // Request the slave listener - ll, err := l.setupMasterListener() + *m.SlaveListener = true + *m.Data.Network = "tcp" + *m.Data.Address = "localhost:0" + *m.Control.Network = "tcp" + *m.Control.Address = "localhost:0" + // Master + master, err := m.setupMasterListener() assert.Nil(tt, err) - defer ll.Close() - // - checkCh := make(chan struct{}, 1) - ssL := network.ListenAny() - defer ssL.Close() - // Start slave - go func() { - masterConn := network.Dial(l.master.Control.Addr().String()) - s := &reverse.Slave{ - Listener: ssL, - Dial: net.Dial, - Master: masterConn, - } - go s.Serve() - <-checkCh - }() + defer master.Close() + // Slave + slaveSideListener := network.ListenAny() + defer slaveSideListener.Close() + masterConn := network.Dial(m.master.Control.Addr().String()) + defer masterConn.Close() + slave := &reverse.Slave{ + Listener: slaveSideListener, + Dial: net.Dial, + Master: masterConn, + } + go slave.Serve() + // Producer testMsg := []byte("TEST") - // Start Dial to slave listener + var wg sync.WaitGroup + defer wg.Wait() + wg.Add(1) go func() { - conn := network.Dial(ssL.Addr().String()) + defer wg.Done() + conn := network.Dial(slaveSideListener.Addr().String()) defer conn.Close() + // Write _, err := conn.Write(testMsg) assert.Nil(tt, err) + // Read + buffer := make([]byte, 1) + _, err = conn.Read(buffer) + assert.Nil(tt, err) }() // Accept connection - conn, err := ll.Accept() + conn, err := master.Accept() assert.Nil(tt, err) defer conn.Close() + // Read buffer := make([]byte, len(testMsg)) _, err = conn.Read(buffer) assert.Nil(tt, err) assert.Equal(tt, testMsg, buffer) - checkCh <- struct{}{} + // Write + _, err = conn.Write(buffer) + assert.Nil(tt, err) }) }