frp/tests/func_test.go

313 lines
9.1 KiB
Go
Raw Normal View History

2017-03-09 17:52:32 +01:00
package tests
2016-05-17 13:13:37 +02:00
import (
"fmt"
2018-05-20 17:55:22 +02:00
"net/http"
2018-01-23 07:49:04 +01:00
"net/url"
2018-01-18 07:53:44 +01:00
"strings"
2016-05-17 13:13:37 +02:00
"testing"
"time"
2018-01-23 07:49:04 +01:00
"github.com/gorilla/websocket"
2017-12-16 16:59:46 +01:00
"github.com/stretchr/testify/assert"
2018-01-18 07:53:44 +01:00
"github.com/fatedier/frp/client"
"github.com/fatedier/frp/server"
2018-05-08 18:57:55 +02:00
gnet "github.com/fatedier/golib/net"
2016-05-17 13:13:37 +02:00
)
var (
2018-01-18 07:53:44 +01:00
SERVER_ADDR = "127.0.0.1"
ADMIN_ADDR = "127.0.0.1:10600"
ADMIN_USER = "abc"
ADMIN_PWD = "abc"
2017-12-16 16:59:46 +01:00
TEST_STR = "frp is a fast reverse proxy to help you expose a local server behind a NAT or firewall to the internet."
TEST_TCP_PORT int = 10701
TEST_TCP_FRP_PORT int = 10801
TEST_TCP_EC_FRP_PORT int = 10901
2017-12-16 16:59:46 +01:00
TEST_TCP_ECHO_STR string = "tcp type:" + TEST_STR
TEST_UDP_PORT int = 10702
TEST_UDP_FRP_PORT int = 10802
TEST_UDP_EC_FRP_PORT int = 10902
2017-12-18 12:35:09 +01:00
TEST_UDP_ECHO_STR string = "udp type:" + TEST_STR
2017-12-16 16:59:46 +01:00
TEST_UNIX_DOMAIN_ADDR string = "/tmp/frp_echo_server.sock"
TEST_UNIX_DOMAIN_FRP_PORT int = 10803
2017-12-16 16:59:46 +01:00
TEST_UNIX_DOMAIN_STR string = "unix domain type:" + TEST_STR
TEST_HTTP_PORT int = 10704
TEST_HTTP_FRP_PORT int = 10804
2017-12-18 12:35:09 +01:00
TEST_HTTP_NORMAL_STR string = "http normal string: " + TEST_STR
TEST_HTTP_FOO_STR string = "http foo string: " + TEST_STR
TEST_HTTP_BAR_STR string = "http bar string: " + TEST_STR
TEST_STCP_FRP_PORT int = 10805
TEST_STCP_EC_FRP_PORT int = 10905
2017-12-18 12:35:09 +01:00
TEST_STCP_ECHO_STR string = "stcp type:" + TEST_STR
2018-01-18 07:53:44 +01:00
ProxyTcpPortNotAllowed string = "tcp_port_not_allowed"
ProxyTcpPortUnavailable string = "tcp_port_unavailable"
ProxyTcpPortNormal string = "tcp_port_normal"
ProxyTcpRandomPort string = "tcp_random_port"
ProxyUdpPortNotAllowed string = "udp_port_not_allowed"
ProxyUdpPortNormal string = "udp_port_normal"
ProxyUdpRandomPort string = "udp_random_port"
ProxyHttpProxy string = "http_proxy"
2018-01-23 10:11:59 +01:00
ProxyRangeTcpPrefix string = "range_tcp"
2016-05-17 13:13:37 +02:00
)
2017-03-09 17:52:32 +01:00
func init() {
2017-12-16 16:59:46 +01:00
go StartTcpEchoServer()
2017-03-27 18:27:30 +02:00
go StartUdpEchoServer()
go StartUnixDomainServer()
2017-12-16 16:59:46 +01:00
go StartHttpServer()
2017-03-09 17:52:32 +01:00
time.Sleep(500 * time.Millisecond)
}
2017-12-18 12:35:09 +01:00
func TestTcp(t *testing.T) {
2017-12-16 16:59:46 +01:00
assert := assert.New(t)
// Normal
addr := fmt.Sprintf("127.0.0.1:%d", TEST_TCP_FRP_PORT)
res, err := sendTcpMsg(addr, TEST_TCP_ECHO_STR)
assert.NoError(err)
assert.Equal(TEST_TCP_ECHO_STR, res)
2016-05-17 13:13:37 +02:00
2017-12-16 16:59:46 +01:00
// Encrytion and compression
addr = fmt.Sprintf("127.0.0.1:%d", TEST_TCP_EC_FRP_PORT)
res, err = sendTcpMsg(addr, TEST_TCP_ECHO_STR)
assert.NoError(err)
assert.Equal(TEST_TCP_ECHO_STR, res)
}
2016-05-17 13:13:37 +02:00
2017-12-18 12:35:09 +01:00
func TestUdp(t *testing.T) {
2017-12-16 16:59:46 +01:00
assert := assert.New(t)
// Normal
addr := fmt.Sprintf("127.0.0.1:%d", TEST_UDP_FRP_PORT)
res, err := sendUdpMsg(addr, TEST_UDP_ECHO_STR)
assert.NoError(err)
assert.Equal(TEST_UDP_ECHO_STR, res)
2016-05-17 13:13:37 +02:00
2017-12-18 12:35:09 +01:00
// Encrytion and compression
addr = fmt.Sprintf("127.0.0.1:%d", TEST_UDP_EC_FRP_PORT)
res, err = sendUdpMsg(addr, TEST_UDP_ECHO_STR)
assert.NoError(err)
assert.Equal(TEST_UDP_ECHO_STR, res)
}
func TestUnixDomain(t *testing.T) {
2017-12-16 16:59:46 +01:00
assert := assert.New(t)
// Normal
addr := fmt.Sprintf("127.0.0.1:%d", TEST_UNIX_DOMAIN_FRP_PORT)
res, err := sendTcpMsg(addr, TEST_UNIX_DOMAIN_STR)
2017-12-18 12:35:09 +01:00
if assert.NoError(err) {
assert.Equal(TEST_UNIX_DOMAIN_STR, res)
}
}
func TestStcp(t *testing.T) {
assert := assert.New(t)
// Normal
addr := fmt.Sprintf("127.0.0.1:%d", TEST_STCP_FRP_PORT)
res, err := sendTcpMsg(addr, TEST_STCP_ECHO_STR)
if assert.NoError(err) {
assert.Equal(TEST_STCP_ECHO_STR, res)
}
// Encrytion and compression
addr = fmt.Sprintf("127.0.0.1:%d", TEST_STCP_EC_FRP_PORT)
res, err = sendTcpMsg(addr, TEST_STCP_ECHO_STR)
if assert.NoError(err) {
assert.Equal(TEST_STCP_ECHO_STR, res)
}
2016-05-17 13:13:37 +02:00
}
2017-12-18 12:35:09 +01:00
func TestHttp(t *testing.T) {
assert := assert.New(t)
// web01
2018-05-20 17:55:22 +02:00
code, body, _, err := sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "", nil, "")
2017-12-18 12:35:09 +01:00
if assert.NoError(err) {
assert.Equal(200, code)
assert.Equal(TEST_HTTP_NORMAL_STR, body)
}
// web02
2018-05-20 17:55:22 +02:00
code, body, _, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test2.frp.com", nil, "")
2017-12-18 12:35:09 +01:00
if assert.NoError(err) {
assert.Equal(200, code)
assert.Equal(TEST_HTTP_NORMAL_STR, body)
}
// error host header
2018-05-20 17:55:22 +02:00
code, body, _, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "errorhost.frp.com", nil, "")
2017-12-18 12:35:09 +01:00
if assert.NoError(err) {
assert.Equal(404, code)
}
// web03
2018-05-20 17:55:22 +02:00
code, body, _, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test3.frp.com", nil, "")
2017-12-18 12:35:09 +01:00
if assert.NoError(err) {
assert.Equal(200, code)
assert.Equal(TEST_HTTP_NORMAL_STR, body)
2016-05-17 13:13:37 +02:00
}
2017-12-18 12:35:09 +01:00
2018-05-20 17:55:22 +02:00
code, body, _, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d/foo", TEST_HTTP_FRP_PORT), "test3.frp.com", nil, "")
2017-12-18 12:35:09 +01:00
if assert.NoError(err) {
assert.Equal(200, code)
assert.Equal(TEST_HTTP_FOO_STR, body)
}
// web04
2018-05-20 17:55:22 +02:00
code, body, _, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d/bar", TEST_HTTP_FRP_PORT), "test3.frp.com", nil, "")
2017-12-18 12:35:09 +01:00
if assert.NoError(err) {
assert.Equal(200, code)
assert.Equal(TEST_HTTP_BAR_STR, body)
}
// web05
2018-05-20 17:55:22 +02:00
code, body, _, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test5.frp.com", nil, "")
2017-12-18 12:35:09 +01:00
if assert.NoError(err) {
assert.Equal(401, code)
}
2018-05-20 17:55:22 +02:00
headers := make(map[string]string)
headers["Authorization"] = basicAuth("test", "test")
code, body, _, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test5.frp.com", headers, "")
2017-12-18 12:35:09 +01:00
if assert.NoError(err) {
assert.Equal(401, code)
2016-05-17 13:13:37 +02:00
}
2018-01-22 07:16:46 +01:00
2018-05-20 17:55:22 +02:00
// web06
var header http.Header
code, body, header, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test6.frp.com", nil, "")
if assert.NoError(err) {
assert.Equal(200, code)
assert.Equal(TEST_HTTP_NORMAL_STR, body)
assert.Equal("true", header.Get("X-Header-Set"))
}
2018-01-22 07:16:46 +01:00
// subhost01
2018-05-20 17:55:22 +02:00
code, body, _, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test01.sub.com", nil, "")
2018-01-22 07:16:46 +01:00
if assert.NoError(err) {
assert.Equal(200, code)
assert.Equal("test01.sub.com", body)
}
// subhost02
2018-05-20 17:55:22 +02:00
code, body, _, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test02.sub.com", nil, "")
2018-01-22 07:16:46 +01:00
if assert.NoError(err) {
assert.Equal(200, code)
assert.Equal("test02.sub.com", body)
}
2016-05-17 13:13:37 +02:00
}
2018-01-18 07:53:44 +01:00
2018-01-23 07:49:04 +01:00
func TestWebSocket(t *testing.T) {
assert := assert.New(t)
u := url.URL{Scheme: "ws", Host: fmt.Sprintf("%s:%d", "127.0.0.1", TEST_HTTP_FRP_PORT), Path: "/ws"}
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
assert.NoError(err)
defer c.Close()
err = c.WriteMessage(websocket.TextMessage, []byte(TEST_HTTP_NORMAL_STR))
assert.NoError(err)
_, msg, err := c.ReadMessage()
assert.NoError(err)
assert.Equal(TEST_HTTP_NORMAL_STR, string(msg))
}
2018-04-23 19:26:05 +02:00
func TestAllowPorts(t *testing.T) {
2018-01-18 07:53:44 +01:00
assert := assert.New(t)
// Port not allowed
status, err := getProxyStatus(ProxyTcpPortNotAllowed)
if assert.NoError(err) {
assert.Equal(client.ProxyStatusStartErr, status.Status)
assert.True(strings.Contains(status.Err, server.ErrPortNotAllowed.Error()))
}
status, err = getProxyStatus(ProxyUdpPortNotAllowed)
if assert.NoError(err) {
assert.Equal(client.ProxyStatusStartErr, status.Status)
assert.True(strings.Contains(status.Err, server.ErrPortNotAllowed.Error()))
}
status, err = getProxyStatus(ProxyTcpPortUnavailable)
if assert.NoError(err) {
assert.Equal(client.ProxyStatusStartErr, status.Status)
assert.True(strings.Contains(status.Err, server.ErrPortUnAvailable.Error()))
}
// Port normal
status, err = getProxyStatus(ProxyTcpPortNormal)
if assert.NoError(err) {
assert.Equal(client.ProxyStatusRunning, status.Status)
}
status, err = getProxyStatus(ProxyUdpPortNormal)
if assert.NoError(err) {
assert.Equal(client.ProxyStatusRunning, status.Status)
}
}
func TestRandomPort(t *testing.T) {
assert := assert.New(t)
// tcp
status, err := getProxyStatus(ProxyTcpRandomPort)
if assert.NoError(err) {
addr := status.RemoteAddr
res, err := sendTcpMsg(addr, TEST_TCP_ECHO_STR)
assert.NoError(err)
assert.Equal(TEST_TCP_ECHO_STR, res)
}
// udp
status, err = getProxyStatus(ProxyUdpRandomPort)
if assert.NoError(err) {
addr := status.RemoteAddr
res, err := sendUdpMsg(addr, TEST_UDP_ECHO_STR)
assert.NoError(err)
assert.Equal(TEST_UDP_ECHO_STR, res)
}
}
2018-01-23 10:11:59 +01:00
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
2018-05-20 17:55:22 +02:00
code, body, _, err := sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT),
2018-01-23 10:11:59 +01:00
"", nil, "http://"+addr)
if assert.NoError(err) {
assert.Equal(200, code)
assert.Equal(TEST_HTTP_NORMAL_STR, body)
}
// connect method
2018-05-08 18:57:55 +02:00
conn, err := gnet.DialTcpByProxy("http://"+addr, fmt.Sprintf("127.0.0.1:%d", TEST_TCP_FRP_PORT))
2018-01-23 10:11:59 +01:00
if assert.NoError(err) {
res, err := sendTcpMsgByConn(conn, TEST_TCP_ECHO_STR)
assert.NoError(err)
assert.Equal(TEST_TCP_ECHO_STR, res)
}
}
}
func TestRangePortsMapping(t *testing.T) {
assert := assert.New(t)
for i := 0; i < 3; i++ {
name := fmt.Sprintf("%s_%d", ProxyRangeTcpPrefix, i)
status, err := getProxyStatus(name)
if assert.NoError(err) {
assert.Equal(client.ProxyStatusRunning, status.Status)
}
}
}