diff --git a/common/net.go b/common/net.go index 5db5ae8ff..4dd503127 100644 --- a/common/net.go +++ b/common/net.go @@ -28,7 +28,7 @@ func HumanFriendlyTraffic(bytes uint64) string { func PickPort(network string, host string) int { switch network { case "tcp": - for retry := 0; retry < 50; retry++ { + for retry := 0; retry < 16; retry++ { l, err := net.Listen("tcp", host+":0") if err != nil { continue @@ -41,7 +41,7 @@ func PickPort(network string, host string) int { return int(p) } case "udp": - for retry := 0; retry < 50; retry++ { + for retry := 0; retry < 16; retry++ { conn, err := net.ListenPacket("udp", host+":0") if err != nil { continue diff --git a/redirector/redirector_test.go b/redirector/redirector_test.go index 855aa28f4..4a2c662e0 100644 --- a/redirector/redirector_test.go +++ b/redirector/redirector_test.go @@ -1,15 +1,15 @@ package redirector import ( - "bytes" "context" - "io" + "fmt" "net" + "net/http" + "strings" "testing" "github.com/p4gefau1t/trojan-go/common" "github.com/p4gefau1t/trojan-go/test/util" - "github.com/p4gefau1t/trojan-go/tunnel" ) func TestRedirector(t *testing.T) { @@ -27,26 +27,37 @@ func TestRedirector(t *testing.T) { RedirectTo: fakeAddr, InboundConn: fakeConn, }) + redir.Redirect(&Redirection{ + Dial: nil, + RedirectTo: nil, + InboundConn: fakeConn, + }) + redir.Redirect(&Redirection{ + Dial: nil, + RedirectTo: fakeAddr, + InboundConn: nil, + }) l, err := net.Listen("tcp", "127.0.0.1:0") common.Must(err) conn1, err := net.Dial("tcp", l.Addr().String()) common.Must(err) conn2, err := l.Accept() common.Must(err) + redirAddr, err := net.ResolveTCPAddr("tcp", util.HTTPAddr) + common.Must(err) redir.Redirect(&Redirection{ Dial: nil, - RedirectTo: tunnel.NewAddressFromHostPort("tcp", "127.0.0.1", util.EchoPort), + RedirectTo: redirAddr, InboundConn: conn2, }) - payload := util.GeneratePayload(128) - common.Must2(conn1.Write(payload)) - buf := make([]byte, 128) - n, err := io.ReadFull(conn2, buf) - if n != 128 || err != nil { - t.Fatal(n, err) - } - if !bytes.Equal(buf, payload) { - t.Fatal("diff: ", payload, "\n", buf) + req, err := http.NewRequest("GET", "http://localhost/", nil) + common.Must(err) + req.Write(conn1) + buf := make([]byte, 1024) + conn1.Read(buf) + fmt.Println(string(buf)) + if !strings.HasPrefix(string(buf), "HTTP/1.1 200 OK") { + t.Fail() } cancel() conn1.Close() diff --git a/url/option_test.go b/url/option_test.go index 1f3b69ac7..36546ca83 100644 --- a/url/option_test.go +++ b/url/option_test.go @@ -9,9 +9,10 @@ import ( func TestUrl_Handle(t *testing.T) { urlCases := []string{ - "trojan-go://password@server.com/?type=ws&host=baidu.com&path=%2fwspath&encryption=ss%3Baes-256-gcm%3Bfuckgfw", "trojan-go://password@server.com", "trojan-go://password@server.com/?type=ws&host=baidu.com&path=%2fwspath", + "trojan-go://password@server.com/?encryption=ss%3Baes-256-gcm%3Bfuckgfw", + "trojan-go://password@server.com/?type=ws&host=baidu.com&path=%2fwspath&encryption=ss%3Baes-256-gcm%3Bfuckgfw", } optionCases := []string{ "mux=true;listen=127.0.0.1:0", @@ -23,6 +24,8 @@ func TestUrl_Handle(t *testing.T) { url: &s, option: &option, } + u.Name() + u.Priority() errChan := make(chan error, 1) go func() { errChan <- u.Handle() @@ -30,7 +33,7 @@ func TestUrl_Handle(t *testing.T) { select { case err := <-errChan: t.Fatal(err) - case <-time.After(time.Second * 2): + case <-time.After(time.Second * 1): } } }