Skip to content

Commit

Permalink
add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ginuerzh committed Jan 5, 2019
1 parent 0f824ed commit 47220e0
Show file tree
Hide file tree
Showing 16 changed files with 577 additions and 171 deletions.
22 changes: 20 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"net"
"net/url"
"time"

"github.com/ginuerzh/gosocks5"
)

// Client is a proxy client.
Expand Down Expand Up @@ -236,8 +238,10 @@ func QUICConfigHandshakeOption(config *QUICConfig) HandshakeOption {

// ConnectOptions describes the options for Connector.Connect.
type ConnectOptions struct {
Addr string
Timeout time.Duration
Addr string
Timeout time.Duration
User *url.Userinfo
Selector gosocks5.Selector
}

// ConnectOption allows a common way to set ConnectOptions.
Expand All @@ -256,3 +260,17 @@ func TimeoutConnectOption(timeout time.Duration) ConnectOption {
opts.Timeout = timeout
}
}

// UserConnectOption specifies the user info for authentication.
func UserConnectOption(user *url.Userinfo) ConnectOption {
return func(opts *ConnectOptions) {
opts.User = user
}
}

// SelectorConnectOption specifies the SOCKS5 client selector.
func SelectorConnectOption(s gosocks5.Selector) ConnectOption {
return func(opts *ConnectOptions) {
opts.Selector = s
}
}
42 changes: 27 additions & 15 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"net/url"
"sync"
"time"

"github.com/go-log/log"
)

func init() {
Expand Down Expand Up @@ -95,7 +97,7 @@ func httpRoundtrip(conn net.Conn, targetURL string, data []byte) (err error) {
return
}

func udpRoundtrip(client *Client, server *Server, host string, data []byte) (err error) {
func udpRoundtrip(logger log.Logger, client *Client, server *Server, host string, data []byte) (err error) {
conn, err := proxyConn(client, server)
if err != nil {
return
Expand All @@ -107,15 +109,17 @@ func udpRoundtrip(client *Client, server *Server, host string, data []byte) (err
return
}

conn.SetDeadline(time.Now().Add(3 * time.Second))
conn.SetDeadline(time.Now().Add(1 * time.Second))
defer conn.SetDeadline(time.Time{})

if _, err = conn.Write(data); err != nil {
logger.Logf("write to %s via %s: %s", host, server.Addr(), err)
return
}

recv := make([]byte, len(data))
if _, err = conn.Read(recv); err != nil {
logger.Logf("read from %s via %s: %s", host, server.Addr(), err)
return
}

Expand Down Expand Up @@ -143,7 +147,7 @@ func proxyRoundtrip(client *Client, server *Server, targetURL string, data []byt
return
}

conn.SetDeadline(time.Now().Add(500 * time.Millisecond))
conn.SetDeadline(time.Now().Add(1000 * time.Millisecond))
defer conn.SetDeadline(time.Time{})

return httpRoundtrip(conn, targetURL, data)
Expand All @@ -167,12 +171,13 @@ type udpHandlerFunc func(w io.Writer, r *udpRequest)

// udpTestServer is a UDP server for test.
type udpTestServer struct {
ln net.PacketConn
handler udpHandlerFunc
wg sync.WaitGroup
mu sync.Mutex // guards closed and conns
closed bool
exitChan chan struct{}
ln net.PacketConn
handler udpHandlerFunc
wg sync.WaitGroup
mu sync.Mutex // guards closed and conns
closed bool
startChan chan struct{}
exitChan chan struct{}
}

func newUDPTestServer(handler udpHandlerFunc) *udpTestServer {
Expand All @@ -181,23 +186,30 @@ func newUDPTestServer(handler udpHandlerFunc) *udpTestServer {
if err != nil {
panic(fmt.Sprintf("udptest: failed to listen on a port: %v", err))
}
ln.SetReadBuffer(1024 * 1024)
ln.SetWriteBuffer(1024 * 1024)

return &udpTestServer{
ln: ln,
handler: handler,
exitChan: make(chan struct{}),
ln: ln,
handler: handler,
startChan: make(chan struct{}),
exitChan: make(chan struct{}),
}
}

func (s *udpTestServer) Start() {
go s.serve()
<-s.startChan
}

func (s *udpTestServer) serve() {
select {
case <-s.startChan:
return
default:
close(s.startChan)
}

for {
data := make([]byte, 1024)
data := make([]byte, 32*1024)
n, raddr, err := s.ln.ReadFrom(data)
if err != nil {
break
Expand Down
4 changes: 2 additions & 2 deletions forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ func (l *tcpRemoteForwardListener) getSession() (s *muxSession, err error) {
conn.SetDeadline(time.Now().Add(HandshakeTimeout))
defer conn.SetDeadline(time.Time{})

conn, err = socks5Handshake(conn, l.chain.LastNode().User)
conn, err = socks5Handshake(conn, nil, l.chain.LastNode().User)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -822,7 +822,7 @@ func (l *tcpRemoteForwardListener) getSession() (s *muxSession, err error) {
}

func (l *tcpRemoteForwardListener) waitConnectSOCKS5(conn net.Conn) (net.Conn, error) {
conn, err := socks5Handshake(conn, l.chain.LastNode().User)
conn, err := socks5Handshake(conn, nil, l.chain.LastNode().User)
if err != nil {
return nil, err
}
Expand Down
12 changes: 6 additions & 6 deletions forward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func BenchmarkTCPDirectForwardParallel(b *testing.B) {
})
}

func udpDirectForwardRoundtrip(host string, data []byte) error {
func udpDirectForwardRoundtrip(t *testing.T, host string, data []byte) error {
ln, err := UDPDirectForwardListener("localhost:0", 0)
if err != nil {
return err
Expand All @@ -138,7 +138,7 @@ func udpDirectForwardRoundtrip(host string, data []byte) error {
go server.Run()
defer server.Close()

return udpRoundtrip(client, server, host, data)
return udpRoundtrip(t, client, server, host, data)
}

func TestUDPDirectForward(t *testing.T) {
Expand All @@ -148,7 +148,7 @@ func TestUDPDirectForward(t *testing.T) {

sendData := make([]byte, 128)
rand.Read(sendData)
err := udpDirectForwardRoundtrip(udpSrv.Addr(), sendData)
err := udpDirectForwardRoundtrip(t, udpSrv.Addr(), sendData)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -181,7 +181,7 @@ func BenchmarkUDPDirectForward(b *testing.B) {
defer server.Close()

for i := 0; i < b.N; i++ {
if err := udpRoundtrip(client, server, udpSrv.Addr(), sendData); err != nil {
if err := udpRoundtrip(b, client, server, udpSrv.Addr(), sendData); err != nil {
b.Error(err)
}
}
Expand Down Expand Up @@ -215,7 +215,7 @@ func BenchmarkUDPDirectForwardParallel(b *testing.B) {

b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if err := udpRoundtrip(client, server, udpSrv.Addr(), sendData); err != nil {
if err := udpRoundtrip(b, client, server, udpSrv.Addr(), sendData); err != nil {
b.Error(err)
}
}
Expand Down Expand Up @@ -281,7 +281,7 @@ func udpRemoteForwardRoundtrip(t *testing.T, host string, data []byte) error {
go server.Run()
defer server.Close()

return udpRoundtrip(client, server, host, data)
return udpRoundtrip(t, client, server, host, data)
}

func TestUDPRemoteForward(t *testing.T) {
Expand Down
11 changes: 8 additions & 3 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ func (c *httpConnector) Connect(conn net.Conn, addr string, options ...ConnectOp
req.Header.Set("User-Agent", DefaultUserAgent)
req.Header.Set("Proxy-Connection", "keep-alive")

if c.User != nil {
u := c.User.Username()
p, _ := c.User.Password()
user := opts.User
if user == nil {
user = c.User
}

if user != nil {
u := user.Username()
p, _ := user.Password()
req.Header.Set("Proxy-Authorization",
"Basic "+base64.StdEncoding.EncodeToString([]byte(u+":"+p)))
}
Expand Down
12 changes: 9 additions & 3 deletions http2.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,15 @@ func (c *http2Connector) Connect(conn net.Conn, addr string, options ...ConnectO
}
// TODO: use the standard CONNECT method.
req.Header.Set("Gost-Target", addr)
if c.User != nil {
u := c.User.Username()
p, _ := c.User.Password()

user := opts.User
if user == nil {
user = c.User
}

if user != nil {
u := user.Username()
p, _ := user.Password()
req.Header.Set("Proxy-Authorization",
"Basic "+base64.StdEncoding.EncodeToString([]byte(u+":"+p)))
}
Expand Down
42 changes: 41 additions & 1 deletion http2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/tls"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -36,7 +37,7 @@ func http2ProxyRoundtrip(targetURL string, data []byte, clientInfo *url.Userinfo
return proxyRoundtrip(client, server, targetURL, data)
}

func TestHTTP2Proxy(t *testing.T) {
func TestHTTP2ProxyAuth(t *testing.T) {
httpSrv := httptest.NewServer(httpTestHandler)
defer httpSrv.Close()

Expand Down Expand Up @@ -1108,3 +1109,42 @@ func TestHTTP2ProxyWithFileProbeResist(t *testing.T) {
t.Error("data not equal")
}
}

func TestHTTP2ProxyWithBypass(t *testing.T) {
httpSrv := httptest.NewServer(httpTestHandler)
defer httpSrv.Close()

sendData := make([]byte, 128)
rand.Read(sendData)

u, err := url.Parse(httpSrv.URL)
if err != nil {
t.Error(err)
}
ln, err := HTTP2Listener("", nil)
if err != nil {
t.Error(err)
}

client := &Client{
Connector: HTTP2Connector(nil),
Transporter: HTTP2Transporter(nil),
}

host := u.Host
if h, _, _ := net.SplitHostPort(u.Host); h != "" {
host = h
}
server := &Server{
Listener: ln,
Handler: HTTP2Handler(
BypassHandlerOption(NewBypassPatterns(false, host)),
),
}
go server.Run()
defer server.Close()

if err = proxyRoundtrip(client, server, httpSrv.URL, sendData); err == nil {
t.Error("should failed")
}
}
Loading

0 comments on commit 47220e0

Please sign in to comment.