more test case

This commit is contained in:
fatedier 2017-12-18 19:35:09 +08:00
parent ff4bdec3f7
commit 3bb404dfb5
10 changed files with 266 additions and 55 deletions

View File

@ -88,7 +88,7 @@ http_pwd = admin
# if domain for frps is frps.com, then you can access [web01] proxy by URL http://test.frps.com # if domain for frps is frps.com, then you can access [web01] proxy by URL http://test.frps.com
subdomain = web01 subdomain = web01
custom_domains = web02.yourdomain.com custom_domains = web02.yourdomain.com
# locations is only useful for http type # locations is only available for http type
locations = /,/pic locations = /,/pic
host_header_rewrite = example.com host_header_rewrite = example.com

View File

@ -10,5 +10,11 @@ if [ -n "${pid}" ]; then
kill ${pid} kill ${pid}
fi fi
pid=`ps aux|grep './../bin/frpc -c ./conf/auto_test_frpc_visitor.ini'|grep -v grep|awk {'print $2'}`
if [ -n "${pid}" ]; then
kill ${pid}
fi
rm -f ./frps.log rm -f ./frps.log
rm -f ./frpc.log rm -f ./frpc.log
rm -f ./frpc_visitor.log

View File

@ -6,30 +6,96 @@ log_file = ./frpc.log
log_level = debug log_level = debug
privilege_token = 123456 privilege_token = 123456
[echo] [tcp_normal]
type = tcp type = tcp
local_ip = 127.0.0.1 local_ip = 127.0.0.1
local_port = 10701 local_port = 10701
remote_port = 10711 remote_port = 10801
use_encryption = true
use_compression = true
[web] [tcp_ec]
type = http type = tcp
local_ip = 127.0.0.1 local_ip = 127.0.0.1
local_port = 10702 local_port = 10701
remote_port = 10901
use_encryption = true use_encryption = true
use_compression = true use_compression = true
custom_domains = 127.0.0.1
[udp] [udp_normal]
type = udp type = udp
local_ip = 127.0.0.1 local_ip = 127.0.0.1
local_port = 10703 local_port = 10702
remote_port = 10712 remote_port = 10802
[udp_ec]
type = udp
local_ip = 127.0.0.1
local_port = 10702
remote_port = 10902
use_encryption = true
use_compression = true
[unix_domain] [unix_domain]
type = tcp type = tcp
remote_port = 10704 remote_port = 10803
plugin = unix_domain_socket plugin = unix_domain_socket
plugin_unix_path = /tmp/frp_echo_server.sock plugin_unix_path = /tmp/frp_echo_server.sock
[stcp]
type = stcp
sk = abcdefg
local_ip = 127.0.0.1
local_port = 10701
[stcp_ec]
type = stcp
sk = abc
local_ip = 127.0.0.1
local_port = 10701
use_encryption = true
use_compression = true
[web01]
type = http
local_ip = 127.0.0.1
local_port = 10704
custom_domains = 127.0.0.1
[web02]
type = http
local_ip = 127.0.0.1
local_port = 10704
custom_domains = test2.frp.com
host_header_rewrite = test2.frp.com
use_encryption = true
use_compression = true
[web03]
type = http
local_ip = 127.0.0.1
local_port = 10704
custom_domains = test3.frp.com
use_encryption = true
use_compression = true
host_header_rewrite = test3.frp.com
locations = /,/foo
[web04]
type = http
local_ip = 127.0.0.1
local_port = 10704
custom_domains = test3.frp.com
use_encryption = true
use_compression = true
host_header_rewrite = test3.frp.com
locations = /bar
[web05]
type = http
local_ip = 127.0.0.1
local_port = 10704
custom_domains = test5.frp.com
host_header_rewrite = test5.frp.com
use_encryption = true
use_compression = true
http_user = test
http_user = test

View File

@ -0,0 +1,25 @@
[common]
server_addr = 0.0.0.0
server_port = 10700
log_file = ./frpc_visitor.log
# debug, info, warn, error
log_level = debug
privilege_token = 123456
[stcp_visitor]
type = stcp
role = visitor
server_name = stcp
sk = abcdefg
bind_addr = 127.0.0.1
bind_port = 10805
[stcp_ec_visitor]
type = stcp
role = visitor
server_name = stcp_ec
sk = abc
bind_addr = 127.0.0.1
bind_port = 10905
use_encryption = true
use_compression = true

View File

@ -1,7 +1,7 @@
[common] [common]
bind_addr = 0.0.0.0 bind_addr = 0.0.0.0
bind_port = 10700 bind_port = 10700
vhost_http_port = 10710 vhost_http_port = 10804
log_file = ./frps.log log_file = ./frps.log
log_level = debug log_level = debug
privilege_token = 123456 privilege_token = 123456

View File

@ -11,7 +11,7 @@ import (
) )
func StartTcpEchoServer() { func StartTcpEchoServer() {
l, err := frpNet.ListenTcp("127.0.0.1", TEST_TCP_ECHO_PORT) l, err := frpNet.ListenTcp("127.0.0.1", TEST_TCP_PORT)
if err != nil { if err != nil {
fmt.Printf("echo server listen error: %v\n", err) fmt.Printf("echo server listen error: %v\n", err)
return return
@ -29,7 +29,7 @@ func StartTcpEchoServer() {
} }
func StartUdpEchoServer() { func StartUdpEchoServer() {
l, err := frpNet.ListenUDP("127.0.0.1", TEST_UDP_ECHO_PORT) l, err := frpNet.ListenUDP("127.0.0.1", TEST_UDP_PORT)
if err != nil { if err != nil {
fmt.Printf("udp echo server listen error: %v\n", err) fmt.Printf("udp echo server listen error: %v\n", err)
return return

View File

@ -1,17 +1,10 @@
package tests package tests
import ( import (
"bufio"
"bytes"
"fmt" "fmt"
"io/ioutil"
"net"
"net/http"
"strings"
"testing" "testing"
"time" "time"
frpNet "github.com/fatedier/frp/utils/net"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -22,17 +15,24 @@ var (
TEST_TCP_EC_FRP_PORT int64 = 10901 TEST_TCP_EC_FRP_PORT int64 = 10901
TEST_TCP_ECHO_STR string = "tcp type:" + TEST_STR TEST_TCP_ECHO_STR string = "tcp type:" + TEST_STR
TEST_UDP_PORT int64 = 10702 TEST_UDP_PORT int64 = 10702
TEST_UDP_FRP_PORT int64 = 10802 TEST_UDP_FRP_PORT int64 = 10802
TEST_UDP_ECHO_STR string = "udp type:" + TEST_STR TEST_UDP_EC_FRP_PORT int64 = 10902
TEST_UDP_ECHO_STR string = "udp type:" + TEST_STR
TEST_UNIX_DOMAIN_ADDR string = "/tmp/frp_echo_server.sock" TEST_UNIX_DOMAIN_ADDR string = "/tmp/frp_echo_server.sock"
TEST_UNIX_DOMAIN_FRP_PORT int64 = 10803 TEST_UNIX_DOMAIN_FRP_PORT int64 = 10803
TEST_UNIX_DOMAIN_STR string = "unix domain type:" + TEST_STR TEST_UNIX_DOMAIN_STR string = "unix domain type:" + TEST_STR
TEST_HTTP_PORT int64 = 10704 TEST_HTTP_PORT int64 = 10704
TEST_HTTP_FRP_PORT int64 = 10804 TEST_HTTP_FRP_PORT int64 = 10804
TEST_HTTP_WEB01_STR string = "http web01:" + TEST_STR 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 int64 = 10805
TEST_STCP_EC_FRP_PORT int64 = 10905
TEST_STCP_ECHO_STR string = "stcp type:" + TEST_STR
) )
func init() { func init() {
@ -43,7 +43,7 @@ func init() {
time.Sleep(500 * time.Millisecond) time.Sleep(500 * time.Millisecond)
} }
func TestTcpServer(t *testing.T) { func TestTcp(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
// Normal // Normal
addr := fmt.Sprintf("127.0.0.1:%d", TEST_TCP_FRP_PORT) addr := fmt.Sprintf("127.0.0.1:%d", TEST_TCP_FRP_PORT)
@ -58,7 +58,7 @@ func TestTcpServer(t *testing.T) {
assert.Equal(TEST_TCP_ECHO_STR, res) assert.Equal(TEST_TCP_ECHO_STR, res)
} }
func TestUdpEchoServer(t *testing.T) { func TestUdp(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
// Normal // Normal
addr := fmt.Sprintf("127.0.0.1:%d", TEST_UDP_FRP_PORT) addr := fmt.Sprintf("127.0.0.1:%d", TEST_UDP_FRP_PORT)
@ -66,32 +66,92 @@ func TestUdpEchoServer(t *testing.T) {
assert.NoError(err) assert.NoError(err)
assert.Equal(TEST_UDP_ECHO_STR, res) assert.Equal(TEST_UDP_ECHO_STR, res)
func TestUnixDomainServer(t *testing.T) { // 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) {
assert := assert.New(t) assert := assert.New(t)
// Normal // Normal
addr := fmt.Sprintf("127.0.0.1:%d", TEST_UNIX_DOMAIN_FRP_PORT) addr := fmt.Sprintf("127.0.0.1:%d", TEST_UNIX_DOMAIN_FRP_PORT)
res, err := sendTcpMsg(addr, TEST_UNIX_DOMAIN_STR) res, err := sendTcpMsg(addr, TEST_UNIX_DOMAIN_STR)
assert.NoError(err) if assert.NoError(err) {
assert.Equal(TEST_UNIX_DOMAIN_STR, res) assert.Equal(TEST_UNIX_DOMAIN_STR, res)
}
} }
func TestHttpServer(t *testing.T) { func TestStcp(t *testing.T) {
client := &http.Client{} assert := assert.New(t)
req, _ := http.NewRequest("GET", fmt.Sprintf("http://127.0.0.1:%d", HTTP_PORT), nil) // Normal
res, err := client.Do(req) addr := fmt.Sprintf("127.0.0.1:%d", TEST_STCP_FRP_PORT)
if err != nil { res, err := sendTcpMsg(addr, TEST_STCP_ECHO_STR)
t.Fatalf("do http request error: %v", err) if assert.NoError(err) {
assert.Equal(TEST_STCP_ECHO_STR, res)
} }
if res.StatusCode == 200 {
body, err := ioutil.ReadAll(res.Body) // Encrytion and compression
if err != nil { addr = fmt.Sprintf("127.0.0.1:%d", TEST_STCP_EC_FRP_PORT)
t.Fatalf("read from http server error: %v", err) res, err = sendTcpMsg(addr, TEST_STCP_ECHO_STR)
} if assert.NoError(err) {
bodystr := string(body) assert.Equal(TEST_STCP_ECHO_STR, res)
if bodystr != HTTP_RES_STR { }
t.Fatalf("content from http server error [%s], correct string is [%s]", bodystr, HTTP_RES_STR) }
}
} else { func TestHttp(t *testing.T) {
t.Fatalf("http code from http server error [%d]", res.StatusCode) assert := assert.New(t)
// web01
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)
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)
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)
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)
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)
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)
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)
if assert.NoError(err) {
assert.Equal(401, code)
} }
} }

View File

@ -3,13 +3,30 @@ package tests
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"strings"
) )
func StartHttpServer() { func StartHttpServer() {
http.HandleFunc("/", request) http.HandleFunc("/", request)
http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", 10702), nil) http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", TEST_HTTP_PORT), nil)
} }
func request(w http.ResponseWriter, r *http.Request) { func request(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(HTTP_RES_STR)) if strings.Contains(r.Host, "127.0.0.1") || strings.Contains(r.Host, "test2.frp.com") ||
strings.Contains(r.Host, "test5.frp.com") {
w.WriteHeader(200)
w.Write([]byte(TEST_HTTP_NORMAL_STR))
} else if strings.Contains(r.Host, "test3.frp.com") {
w.WriteHeader(200)
if strings.Contains(r.URL.Path, "foo") {
w.Write([]byte(TEST_HTTP_FOO_STR))
} else if strings.Contains(r.URL.Path, "bar") {
w.Write([]byte(TEST_HTTP_BAR_STR))
} else {
w.Write([]byte(TEST_HTTP_NORMAL_STR))
}
} else {
w.WriteHeader(404)
}
return
} }

View File

@ -3,6 +3,7 @@
./../bin/frps -c ./conf/auto_test_frps.ini & ./../bin/frps -c ./conf/auto_test_frps.ini &
sleep 1 sleep 1
./../bin/frpc -c ./conf/auto_test_frpc.ini & ./../bin/frpc -c ./conf/auto_test_frpc.ini &
./../bin/frpc -c ./conf/auto_test_frpc_visitor.ini &
# wait until proxies are connected # wait until proxies are connected
sleep 2 sleep 2

View File

@ -1,8 +1,11 @@
package test package tests
import ( import (
"encoding/base64"
"fmt" "fmt"
"io/ioutil"
"net" "net"
"net/http"
"time" "time"
frpNet "github.com/fatedier/frp/utils/net" frpNet "github.com/fatedier/frp/utils/net"
@ -10,11 +13,11 @@ import (
func sendTcpMsg(addr string, msg string) (res string, err error) { func sendTcpMsg(addr string, msg string) (res string, err error) {
c, err := frpNet.ConnectTcpServer(addr) c, err := frpNet.ConnectTcpServer(addr)
defer c.Close()
if err != nil { if err != nil {
err = fmt.Errorf("connect to tcp server error: %v", err) err = fmt.Errorf("connect to tcp server error: %v", err)
return return
} }
defer c.Close()
timer := time.Now().Add(5 * time.Second) timer := time.Now().Add(5 * time.Second)
c.SetDeadline(timer) c.SetDeadline(timer)
@ -55,3 +58,36 @@ 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) {
req, errRet := http.NewRequest(method, url, nil)
if errRet != nil {
err = errRet
return
}
if host != "" {
req.Host = host
}
for k, v := range header {
req.Header.Set(k, v)
}
resp, errRet := http.DefaultClient.Do(req)
if errRet != nil {
err = errRet
return
}
code = resp.StatusCode
buf, errRet := ioutil.ReadAll(resp.Body)
if errRet != nil {
err = errRet
return
}
body = string(buf)
return
}
func basicAuth(username, passwd string) string {
auth := username + ":" + passwd
return "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
}