diff --git a/Makefile b/Makefile index 18b6aaf..6d16035 100644 --- a/Makefile +++ b/Makefile @@ -44,6 +44,9 @@ ci: go test -v ./tests/... cd ./tests && ./clean_test.sh && cd - +ciclean: + cd ./tests && ./clean_test.sh && cd - + alltest: gotest ci clean: diff --git a/tests/conf/auto_test_frpc.ini b/tests/conf/auto_test_frpc.ini index 777f8de..26da4c1 100644 --- a/tests/conf/auto_test_frpc.ini +++ b/tests/conf/auto_test_frpc.ini @@ -1,5 +1,5 @@ [common] -server_addr = 0.0.0.0 +server_addr = 127.0.0.1 server_port = 10700 log_file = ./frpc.log # debug, info, warn, error @@ -156,3 +156,8 @@ type = udp local_ip = 127.0.0.1 local_port = 10702 remote_port = 0 + +[http_proxy] +type = tcp +plugin = http_proxy +remote_port = 0 diff --git a/tests/func_test.go b/tests/func_test.go index f8f9c0a..4e03f2c 100644 --- a/tests/func_test.go +++ b/tests/func_test.go @@ -12,6 +12,7 @@ import ( "github.com/fatedier/frp/client" "github.com/fatedier/frp/server" + "github.com/fatedier/frp/utils/net" ) var ( @@ -52,6 +53,8 @@ var ( ProxyUdpPortNotAllowed string = "udp_port_not_allowed" ProxyUdpPortNormal string = "udp_port_normal" ProxyUdpRandomPort string = "udp_random_port" + + ProxyHttpProxy string = "http_proxy" ) func init() { @@ -122,67 +125,67 @@ func TestStcp(t *testing.T) { func TestHttp(t *testing.T) { assert := assert.New(t) // web01 - code, body, err := sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "", nil) + code, body, err := sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "", nil, "") if assert.NoError(err) { assert.Equal(200, code) assert.Equal(TEST_HTTP_NORMAL_STR, body) } // web02 - code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test2.frp.com", nil) + code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test2.frp.com", nil, "") if assert.NoError(err) { assert.Equal(200, code) assert.Equal(TEST_HTTP_NORMAL_STR, body) } // error host header - code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "errorhost.frp.com", nil) + code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "errorhost.frp.com", nil, "") if assert.NoError(err) { assert.Equal(404, code) } // web03 - code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test3.frp.com", nil) + code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test3.frp.com", nil, "") if assert.NoError(err) { assert.Equal(200, code) assert.Equal(TEST_HTTP_NORMAL_STR, body) } - code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d/foo", TEST_HTTP_FRP_PORT), "test3.frp.com", nil) + code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d/foo", TEST_HTTP_FRP_PORT), "test3.frp.com", nil, "") if assert.NoError(err) { assert.Equal(200, code) assert.Equal(TEST_HTTP_FOO_STR, body) } // web04 - code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d/bar", TEST_HTTP_FRP_PORT), "test3.frp.com", nil) + code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d/bar", TEST_HTTP_FRP_PORT), "test3.frp.com", nil, "") if assert.NoError(err) { assert.Equal(200, code) assert.Equal(TEST_HTTP_BAR_STR, body) } // web05 - code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test5.frp.com", nil) + code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test5.frp.com", nil, "") if assert.NoError(err) { assert.Equal(401, code) } header := make(map[string]string) header["Authorization"] = basicAuth("test", "test") - code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test5.frp.com", header) + code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test5.frp.com", header, "") if assert.NoError(err) { assert.Equal(401, code) } // subhost01 - code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test01.sub.com", nil) + code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test01.sub.com", nil, "") if assert.NoError(err) { assert.Equal(200, code) assert.Equal("test01.sub.com", body) } // subhost02 - code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test02.sub.com", nil) + code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test02.sub.com", nil, "") if assert.NoError(err) { assert.Equal(200, code) assert.Equal("test02.sub.com", body) @@ -258,3 +261,28 @@ func TestRandomPort(t *testing.T) { assert.Equal(TEST_UDP_ECHO_STR, res) } } + +func TestPluginHttpProxy(t *testing.T) { + assert := assert.New(t) + status, err := getProxyStatus(ProxyHttpProxy) + if assert.NoError(err) { + assert.Equal(client.ProxyStatusRunning, status.Status) + + // http proxy + addr := status.RemoteAddr + code, body, err := sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), + "", nil, "http://"+addr) + if assert.NoError(err) { + assert.Equal(200, code) + assert.Equal(TEST_HTTP_NORMAL_STR, body) + } + + // connect method + conn, err := net.ConnectTcpServerByHttpProxy("http://"+addr, fmt.Sprintf("127.0.0.1:%d", TEST_TCP_FRP_PORT)) + if assert.NoError(err) { + res, err := sendTcpMsgByConn(conn, TEST_TCP_ECHO_STR) + assert.NoError(err) + assert.Equal(TEST_TCP_ECHO_STR, res) + } + } +} diff --git a/tests/util.go b/tests/util.go index 5cf0f6a..ea512b9 100644 --- a/tests/util.go +++ b/tests/util.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "net" "net/http" + "net/url" "strings" "time" @@ -81,7 +82,10 @@ func sendTcpMsg(addr string, msg string) (res string, err error) { return } defer c.Close() + return sendTcpMsgByConn(c, msg) +} +func sendTcpMsgByConn(c net.Conn, msg string) (res string, err error) { timer := time.Now().Add(5 * time.Second) c.SetDeadline(timer) c.Write([]byte(msg)) @@ -122,8 +126,8 @@ func sendUdpMsg(addr string, msg string) (res string, err error) { return string(buf[:n]), nil } -func sendHttpMsg(method, url string, host string, header map[string]string) (code int, body string, err error) { - req, errRet := http.NewRequest(method, url, nil) +func sendHttpMsg(method, urlStr string, host string, header map[string]string, proxy string) (code int, body string, err error) { + req, errRet := http.NewRequest(method, urlStr, nil) if errRet != nil { err = errRet return @@ -135,7 +139,29 @@ func sendHttpMsg(method, url string, host string, header map[string]string) (cod for k, v := range header { req.Header.Set(k, v) } - resp, errRet := http.DefaultClient.Do(req) + + tr := &http.Transport{ + DialContext: (&net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, + DualStack: true, + }).DialContext, + MaxIdleConns: 100, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + } + + if len(proxy) != 0 { + tr.Proxy = func(req *http.Request) (*url.URL, error) { + return url.Parse(proxy) + } + } + client := http.Client{ + Transport: tr, + } + + resp, errRet := client.Do(req) if errRet != nil { err = errRet return