ci: add plugin http_proxy test case

This commit is contained in:
fatedier 2018-01-23 17:11:59 +08:00
parent a6f2736b80
commit ff28668cf2
4 changed files with 76 additions and 14 deletions

View File

@ -44,6 +44,9 @@ ci:
go test -v ./tests/... go test -v ./tests/...
cd ./tests && ./clean_test.sh && cd - cd ./tests && ./clean_test.sh && cd -
ciclean:
cd ./tests && ./clean_test.sh && cd -
alltest: gotest ci alltest: gotest ci
clean: clean:

View File

@ -1,5 +1,5 @@
[common] [common]
server_addr = 0.0.0.0 server_addr = 127.0.0.1
server_port = 10700 server_port = 10700
log_file = ./frpc.log log_file = ./frpc.log
# debug, info, warn, error # debug, info, warn, error
@ -156,3 +156,8 @@ type = udp
local_ip = 127.0.0.1 local_ip = 127.0.0.1
local_port = 10702 local_port = 10702
remote_port = 0 remote_port = 0
[http_proxy]
type = tcp
plugin = http_proxy
remote_port = 0

View File

@ -12,6 +12,7 @@ import (
"github.com/fatedier/frp/client" "github.com/fatedier/frp/client"
"github.com/fatedier/frp/server" "github.com/fatedier/frp/server"
"github.com/fatedier/frp/utils/net"
) )
var ( var (
@ -52,6 +53,8 @@ var (
ProxyUdpPortNotAllowed string = "udp_port_not_allowed" ProxyUdpPortNotAllowed string = "udp_port_not_allowed"
ProxyUdpPortNormal string = "udp_port_normal" ProxyUdpPortNormal string = "udp_port_normal"
ProxyUdpRandomPort string = "udp_random_port" ProxyUdpRandomPort string = "udp_random_port"
ProxyHttpProxy string = "http_proxy"
) )
func init() { func init() {
@ -122,67 +125,67 @@ func TestStcp(t *testing.T) {
func TestHttp(t *testing.T) { func TestHttp(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
// web01 // 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) { if assert.NoError(err) {
assert.Equal(200, code) assert.Equal(200, code)
assert.Equal(TEST_HTTP_NORMAL_STR, body) assert.Equal(TEST_HTTP_NORMAL_STR, body)
} }
// web02 // 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) { if assert.NoError(err) {
assert.Equal(200, code) assert.Equal(200, code)
assert.Equal(TEST_HTTP_NORMAL_STR, body) assert.Equal(TEST_HTTP_NORMAL_STR, body)
} }
// error host header // 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) { if assert.NoError(err) {
assert.Equal(404, code) assert.Equal(404, code)
} }
// web03 // 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) { if assert.NoError(err) {
assert.Equal(200, code) assert.Equal(200, code)
assert.Equal(TEST_HTTP_NORMAL_STR, body) 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) { if assert.NoError(err) {
assert.Equal(200, code) assert.Equal(200, code)
assert.Equal(TEST_HTTP_FOO_STR, body) assert.Equal(TEST_HTTP_FOO_STR, body)
} }
// web04 // 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) { if assert.NoError(err) {
assert.Equal(200, code) assert.Equal(200, code)
assert.Equal(TEST_HTTP_BAR_STR, body) assert.Equal(TEST_HTTP_BAR_STR, body)
} }
// web05 // 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) { if assert.NoError(err) {
assert.Equal(401, code) assert.Equal(401, code)
} }
header := make(map[string]string) header := make(map[string]string)
header["Authorization"] = basicAuth("test", "test") 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) { if assert.NoError(err) {
assert.Equal(401, code) assert.Equal(401, code)
} }
// subhost01 // 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) { if assert.NoError(err) {
assert.Equal(200, code) assert.Equal(200, code)
assert.Equal("test01.sub.com", body) assert.Equal("test01.sub.com", body)
} }
// subhost02 // 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) { if assert.NoError(err) {
assert.Equal(200, code) assert.Equal(200, code)
assert.Equal("test02.sub.com", body) assert.Equal("test02.sub.com", body)
@ -258,3 +261,28 @@ func TestRandomPort(t *testing.T) {
assert.Equal(TEST_UDP_ECHO_STR, res) 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)
}
}
}

View File

@ -8,6 +8,7 @@ import (
"io/ioutil" "io/ioutil"
"net" "net"
"net/http" "net/http"
"net/url"
"strings" "strings"
"time" "time"
@ -81,7 +82,10 @@ func sendTcpMsg(addr string, msg string) (res string, err error) {
return return
} }
defer c.Close() 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) timer := time.Now().Add(5 * time.Second)
c.SetDeadline(timer) c.SetDeadline(timer)
c.Write([]byte(msg)) c.Write([]byte(msg))
@ -122,8 +126,8 @@ func sendUdpMsg(addr string, msg string) (res string, err error) {
return string(buf[:n]), nil return string(buf[:n]), nil
} }
func sendHttpMsg(method, url string, host string, header map[string]string) (code int, body string, err error) { func sendHttpMsg(method, urlStr string, host string, header map[string]string, proxy string) (code int, body string, err error) {
req, errRet := http.NewRequest(method, url, nil) req, errRet := http.NewRequest(method, urlStr, nil)
if errRet != nil { if errRet != nil {
err = errRet err = errRet
return return
@ -135,7 +139,29 @@ func sendHttpMsg(method, url string, host string, header map[string]string) (cod
for k, v := range header { for k, v := range header {
req.Header.Set(k, v) 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 { if errRet != nil {
err = errRet err = errRet
return return