forked from p4gefau1t/trojan-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redirector_test.go
67 lines (64 loc) · 1.38 KB
/
redirector_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package redirector
import (
"context"
"fmt"
"net"
"net/http"
"strings"
"testing"
"time"
"github.com/p4gefau1t/trojan-go/common"
"github.com/p4gefau1t/trojan-go/test/util"
)
func TestRedirector(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
redir := NewRedirector(ctx)
redir.Redirect(&Redirection{
Dial: nil,
RedirectTo: nil,
InboundConn: nil,
})
var fakeAddr net.Addr
var fakeConn net.Conn
redir.Redirect(&Redirection{
Dial: nil,
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: redirAddr,
InboundConn: conn2,
})
time.Sleep(time.Second)
req, err := http.NewRequest("GET", "https://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()
conn2.Close()
}