Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify testcases to avoid port confilict #65

Merged
merged 1 commit into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/nknorg/nconnect
go 1.20

require (
github.com/cakturk/go-netstat v0.0.0-20200220111822-e5b49efee7a5
github.com/eycorsican/go-tun2socks v1.16.11
github.com/gin-contrib/gzip v0.0.3
github.com/gin-gonic/gin v1.9.0
Expand All @@ -16,6 +17,7 @@ require (
github.com/nknorg/nkngomobile v0.0.0-20220615081414-671ad1afdfa9
github.com/nknorg/tuna v0.0.0-20230405223335-eb60c60c5953
github.com/shadowsocks/go-shadowsocks2 v0.1.2
github.com/stretchr/testify v1.8.1
github.com/txthinking/brook v0.0.0-20230418095906-76ced63f1803
github.com/txthinking/socks5 v0.0.0-20230307062227-0e1677eca4ba
golang.org/x/net v0.8.0
Expand All @@ -27,6 +29,7 @@ require (
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/bytedance/sonic v1.8.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gaukas/godicttls v0.0.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
Expand Down Expand Up @@ -62,6 +65,7 @@ require (
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/phuslu/iploc v1.0.20230201 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/quic-go/qtls-go1-18 v0.2.0 // indirect
github.com/quic-go/qtls-go1-19 v0.2.0 // indirect
github.com/quic-go/qtls-go1-20 v0.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHG
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.8.0 h1:ea0Xadu+sHlu7x5O3gKhRpQ1IKiMrSiHttPF0ybECuA=
github.com/bytedance/sonic v1.8.0/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
github.com/cakturk/go-netstat v0.0.0-20200220111822-e5b49efee7a5 h1:BjkPE3785EwPhhyuFkbINB+2a1xATwk8SNDWnJiD41g=
github.com/cakturk/go-netstat v0.0.0-20200220111822-e5b49efee7a5/go.mod h1:jtAfVaU/2cu1+wdSRPWE2c1N2qeAA3K4RH9pYgqwets=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
Expand Down
1 change: 1 addition & 0 deletions tests/client.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"tunaMinFee": "0.00001",
"tunaFeeRatio": 0.1,
"tunaGeoDBPath": ".",
"tunaDisableMeasureBandwidth": false,
"tunaMeasureStoragePath": ".",
"adminIdentifier": "nConnect",
"webRootPath": "web/dist",
Expand Down
1 change: 0 additions & 1 deletion tests/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tests

var port int = 1080
var proxyAddr string = "127.0.0.1:1080"

const (
numMsgs = 10
Expand Down
8 changes: 4 additions & 4 deletions tests/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package tests

import (
"fmt"
"time"

"github.com/txthinking/brook"
)

func dnsQuery() {
func dnsQuery() error {
proxyAddr := fmt.Sprintf("127.0.0.1:%v", port)
for i := 1; i <= numMsgs; i++ {
err := brook.Socks5Test(proxyAddr, "", "", "http3.ooo", "137.184.237.95", "8.8.8.8:53")
if err != nil {
fmt.Printf("TestDNSProxy try %v err: %v\n", i, err)
time.Sleep(time.Duration(i) * time.Second)
break
return err
}
}
return nil
}
58 changes: 54 additions & 4 deletions tests/main_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,67 @@
package tests

import (
"flag"
"fmt"
"log"
"os"
"testing"
"time"

"github.com/nknorg/tuna/types"
)

var remoteTuna = flag.Bool("remoteTuna", false, "use remote tuna node")
var tun = flag.Bool("tun", false, "use tun device")

func TestMain(m *testing.M) {
go StartTcpServer()
go StartWebServer()
go StartUdpServer()
flag.Parse()
if *remoteTuna {
fmt.Println("We are using remote tuna node")
} else {
fmt.Println("Using local tuna node. If want to use remote tuna node, please run: go test -v -remoteTuna .")
}

go func() {
err := StartTcpServer()
if err != nil {
log.Fatalf("StartTcpServer err %v", err)
return
}
}()
go func() {
err := StartWebServer()
if err != nil {
log.Fatalf("StartWebServer err %v", err)
return
}
}()
go func() {
err := StartUdpServer()
if err != nil {
log.Fatalf("StartUdpServer err %v", err)
return
}
}()

var tunaNode *types.Node
var err error
if !(*remoteTuna) {
tunaNode, err = getTunaNode()
if err != nil {
log.Fatalf("getTunaNode err %v", err)
return
}
}

go func() {
err := startNconnect("server.json", true, true, false, tunaNode)
if err != nil {
log.Fatalf("start nconnect server err: %v", err)
return
}
}()

go StartNconnectServerWithTunaNode(true, true, false)
time.Sleep(15 * time.Second)

exitVal := m.Run()
Expand Down
26 changes: 16 additions & 10 deletions tests/proxy_test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
package tests

import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/require"
)

// go test -v -run=TestProxy
func TestProxy(t *testing.T) {
tuna, udp, tun := true, true, false
go func() {
err := startNconnect("client.json", tuna, udp, tun, nil)
if err != nil {
fmt.Printf("start nconnect client err: %v\n", err)
return
}
require.NoError(t, err)
}()
time.Sleep(15 * time.Second)

dnsQuery()
time.Sleep(5 * time.Second)

err := waitSSAndTunaReady()
require.NoError(t, err)

err = dnsQuery()
require.NoError(t, err)
for _, server := range servers {
StartWebClient("http://" + server + httpPort + "/httpEcho")
StartTCPClient(server + tcpPort)
StartUDPClient(server + udpPort)
err := StartWebClient("http://" + server + httpPort + "/httpEcho")
require.NoError(t, err)
err = StartTCPClient(server + tcpPort)
require.NoError(t, err)
err = StartUDPClient(server + udpPort)
require.NoError(t, err)
}
}
83 changes: 64 additions & 19 deletions tests/pub.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"encoding/json"
"fmt"
"log"
"net"
"os"
"time"

"github.com/cakturk/go-netstat/netstat"
"github.com/nknorg/nconnect"
"github.com/nknorg/nconnect/config"
nkn "github.com/nknorg/nkn-sdk-go"
Expand Down Expand Up @@ -40,9 +43,11 @@ func startNconnect(configFile string, tuna, udp, tun bool, n *types.Node) error
}

if opts.Client {
proxyAddr = fmt.Sprintf("127.0.0.1:%v", port)
opts.LocalSocksAddr = proxyAddr
port++
port, err := getFreePort(port)
if err != nil {
return err
}
opts.LocalSocksAddr = fmt.Sprintf("127.0.0.1:%v", port)
}
fmt.Printf("opts.RemoteAdminAddr: %+v\n", opts.RemoteAdminAddr)

Expand All @@ -56,22 +61,6 @@ func startNconnect(configFile string, tuna, udp, tun bool, n *types.Node) error
return err
}

func StartNconnectServerWithTunaNode(tuna, udp, tun bool) {
tunaNode, err := getTunaNode()
if err != nil {
fmt.Printf("getTunaNode err %v\n", err)
return
}

go func() {
err := startNconnect("server.json", tuna, udp, tun, tunaNode)
if err != nil {
fmt.Printf("start nconnect server err: %v\n", err)
return
}
}()
}

func getTunaNode() (*types.Node, error) {
tunaSeed, _ := hex.DecodeString(seedHex)
acc, err := nkn.NewAccount(tunaSeed)
Expand Down Expand Up @@ -135,3 +124,59 @@ type Person struct {
Name string
Age int
}

func getFreePort(port int) (int, error) {
for i := 0; i < 100; i++ {
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("127.0.0.1:%v", port))
if err != nil {
return 0, err
}

l, err := net.ListenTCP("tcp", addr)
if err != nil {
port++
continue
}

defer l.Close()

return l.Addr().(*net.TCPAddr).Port, nil
}
return 0, fmt.Errorf("can't find free port")
}

func waitSSAndTunaReady() error {
ssIsReady := false
for i := 0; i < 100; i++ {
tabs, err := netstat.TCPSocks(func(s *netstat.SockTabEntry) bool {
return s.State == netstat.Listen && s.LocalAddr.Port == uint16(port)
})
if err != nil {
fmt.Printf("waitSSAndTunaReady err: %v\n", err)
}
if len(tabs) >= 1 {
ssIsReady = true
break
}
time.Sleep(2 * time.Second)
}

if !ssIsReady {
return fmt.Errorf("ss is not ready after 200 seconds, give up")
}

for i := 0; i < 100; i++ {
tabs, err := netstat.TCPSocks(func(s *netstat.SockTabEntry) bool {
return s.State == netstat.Established && s.RemoteAddr.Port == 30020
})
if err != nil {
fmt.Printf("waitSSAndTunaReady err: %v\n", err)
}
time.Sleep(2 * time.Second)
if len(tabs) >= 1 {
return nil
}
}

return fmt.Errorf("tuna is not connected after 200 seconds, give up")
}
1 change: 1 addition & 0 deletions tests/server.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"tunaFeeRatio": 0.1,
"tunaServiceName": "reverse",
"tunaGeoDBPath": ".",
"tunaDisableMeasureBandwidth": false,
"tunaMeasureStoragePath": ".",
"adminIdentifier": "nConnect",
"webRootPath": "web/dist",
Expand Down
Loading