1
0

func_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. package tests
  2. import (
  3. "fmt"
  4. "net/url"
  5. "strings"
  6. "testing"
  7. "time"
  8. "github.com/gorilla/websocket"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/fatedier/frp/client"
  11. "github.com/fatedier/frp/server"
  12. "github.com/fatedier/frp/utils/net"
  13. )
  14. var (
  15. SERVER_ADDR = "127.0.0.1"
  16. ADMIN_ADDR = "127.0.0.1:10600"
  17. ADMIN_USER = "abc"
  18. ADMIN_PWD = "abc"
  19. TEST_STR = "frp is a fast reverse proxy to help you expose a local server behind a NAT or firewall to the internet."
  20. TEST_TCP_PORT int = 10701
  21. TEST_TCP_FRP_PORT int = 10801
  22. TEST_TCP_EC_FRP_PORT int = 10901
  23. TEST_TCP_ECHO_STR string = "tcp type:" + TEST_STR
  24. TEST_UDP_PORT int = 10702
  25. TEST_UDP_FRP_PORT int = 10802
  26. TEST_UDP_EC_FRP_PORT int = 10902
  27. TEST_UDP_ECHO_STR string = "udp type:" + TEST_STR
  28. TEST_UNIX_DOMAIN_ADDR string = "/tmp/frp_echo_server.sock"
  29. TEST_UNIX_DOMAIN_FRP_PORT int = 10803
  30. TEST_UNIX_DOMAIN_STR string = "unix domain type:" + TEST_STR
  31. TEST_HTTP_PORT int = 10704
  32. TEST_HTTP_FRP_PORT int = 10804
  33. TEST_HTTP_NORMAL_STR string = "http normal string: " + TEST_STR
  34. TEST_HTTP_FOO_STR string = "http foo string: " + TEST_STR
  35. TEST_HTTP_BAR_STR string = "http bar string: " + TEST_STR
  36. TEST_STCP_FRP_PORT int = 10805
  37. TEST_STCP_EC_FRP_PORT int = 10905
  38. TEST_STCP_ECHO_STR string = "stcp type:" + TEST_STR
  39. ProxyTcpPortNotAllowed string = "tcp_port_not_allowed"
  40. ProxyTcpPortUnavailable string = "tcp_port_unavailable"
  41. ProxyTcpPortNormal string = "tcp_port_normal"
  42. ProxyTcpRandomPort string = "tcp_random_port"
  43. ProxyUdpPortNotAllowed string = "udp_port_not_allowed"
  44. ProxyUdpPortNormal string = "udp_port_normal"
  45. ProxyUdpRandomPort string = "udp_random_port"
  46. ProxyHttpProxy string = "http_proxy"
  47. ProxyRangeTcpPrefix string = "range_tcp"
  48. )
  49. func init() {
  50. go StartTcpEchoServer()
  51. go StartUdpEchoServer()
  52. go StartUnixDomainServer()
  53. go StartHttpServer()
  54. time.Sleep(500 * time.Millisecond)
  55. }
  56. func TestTcp(t *testing.T) {
  57. assert := assert.New(t)
  58. // Normal
  59. addr := fmt.Sprintf("127.0.0.1:%d", TEST_TCP_FRP_PORT)
  60. res, err := sendTcpMsg(addr, TEST_TCP_ECHO_STR)
  61. assert.NoError(err)
  62. assert.Equal(TEST_TCP_ECHO_STR, res)
  63. // Encrytion and compression
  64. addr = fmt.Sprintf("127.0.0.1:%d", TEST_TCP_EC_FRP_PORT)
  65. res, err = sendTcpMsg(addr, TEST_TCP_ECHO_STR)
  66. assert.NoError(err)
  67. assert.Equal(TEST_TCP_ECHO_STR, res)
  68. }
  69. func TestUdp(t *testing.T) {
  70. assert := assert.New(t)
  71. // Normal
  72. addr := fmt.Sprintf("127.0.0.1:%d", TEST_UDP_FRP_PORT)
  73. res, err := sendUdpMsg(addr, TEST_UDP_ECHO_STR)
  74. assert.NoError(err)
  75. assert.Equal(TEST_UDP_ECHO_STR, res)
  76. // Encrytion and compression
  77. addr = fmt.Sprintf("127.0.0.1:%d", TEST_UDP_EC_FRP_PORT)
  78. res, err = sendUdpMsg(addr, TEST_UDP_ECHO_STR)
  79. assert.NoError(err)
  80. assert.Equal(TEST_UDP_ECHO_STR, res)
  81. }
  82. func TestUnixDomain(t *testing.T) {
  83. assert := assert.New(t)
  84. // Normal
  85. addr := fmt.Sprintf("127.0.0.1:%d", TEST_UNIX_DOMAIN_FRP_PORT)
  86. res, err := sendTcpMsg(addr, TEST_UNIX_DOMAIN_STR)
  87. if assert.NoError(err) {
  88. assert.Equal(TEST_UNIX_DOMAIN_STR, res)
  89. }
  90. }
  91. func TestStcp(t *testing.T) {
  92. assert := assert.New(t)
  93. // Normal
  94. addr := fmt.Sprintf("127.0.0.1:%d", TEST_STCP_FRP_PORT)
  95. res, err := sendTcpMsg(addr, TEST_STCP_ECHO_STR)
  96. if assert.NoError(err) {
  97. assert.Equal(TEST_STCP_ECHO_STR, res)
  98. }
  99. // Encrytion and compression
  100. addr = fmt.Sprintf("127.0.0.1:%d", TEST_STCP_EC_FRP_PORT)
  101. res, err = sendTcpMsg(addr, TEST_STCP_ECHO_STR)
  102. if assert.NoError(err) {
  103. assert.Equal(TEST_STCP_ECHO_STR, res)
  104. }
  105. }
  106. func TestHttp(t *testing.T) {
  107. assert := assert.New(t)
  108. // web01
  109. code, body, err := sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "", nil, "")
  110. if assert.NoError(err) {
  111. assert.Equal(200, code)
  112. assert.Equal(TEST_HTTP_NORMAL_STR, body)
  113. }
  114. // web02
  115. code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test2.frp.com", nil, "")
  116. if assert.NoError(err) {
  117. assert.Equal(200, code)
  118. assert.Equal(TEST_HTTP_NORMAL_STR, body)
  119. }
  120. // error host header
  121. code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "errorhost.frp.com", nil, "")
  122. if assert.NoError(err) {
  123. assert.Equal(404, code)
  124. }
  125. // web03
  126. code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test3.frp.com", nil, "")
  127. if assert.NoError(err) {
  128. assert.Equal(200, code)
  129. assert.Equal(TEST_HTTP_NORMAL_STR, body)
  130. }
  131. code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d/foo", TEST_HTTP_FRP_PORT), "test3.frp.com", nil, "")
  132. if assert.NoError(err) {
  133. assert.Equal(200, code)
  134. assert.Equal(TEST_HTTP_FOO_STR, body)
  135. }
  136. // web04
  137. code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d/bar", TEST_HTTP_FRP_PORT), "test3.frp.com", nil, "")
  138. if assert.NoError(err) {
  139. assert.Equal(200, code)
  140. assert.Equal(TEST_HTTP_BAR_STR, body)
  141. }
  142. // web05
  143. code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test5.frp.com", nil, "")
  144. if assert.NoError(err) {
  145. assert.Equal(401, code)
  146. }
  147. header := make(map[string]string)
  148. header["Authorization"] = basicAuth("test", "test")
  149. code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test5.frp.com", header, "")
  150. if assert.NoError(err) {
  151. assert.Equal(401, code)
  152. }
  153. // subhost01
  154. code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test01.sub.com", nil, "")
  155. if assert.NoError(err) {
  156. assert.Equal(200, code)
  157. assert.Equal("test01.sub.com", body)
  158. }
  159. // subhost02
  160. code, body, err = sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT), "test02.sub.com", nil, "")
  161. if assert.NoError(err) {
  162. assert.Equal(200, code)
  163. assert.Equal("test02.sub.com", body)
  164. }
  165. }
  166. func TestWebSocket(t *testing.T) {
  167. assert := assert.New(t)
  168. u := url.URL{Scheme: "ws", Host: fmt.Sprintf("%s:%d", "127.0.0.1", TEST_HTTP_FRP_PORT), Path: "/ws"}
  169. c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
  170. assert.NoError(err)
  171. defer c.Close()
  172. err = c.WriteMessage(websocket.TextMessage, []byte(TEST_HTTP_NORMAL_STR))
  173. assert.NoError(err)
  174. _, msg, err := c.ReadMessage()
  175. assert.NoError(err)
  176. assert.Equal(TEST_HTTP_NORMAL_STR, string(msg))
  177. }
  178. func TestPrivilegeAllowPorts(t *testing.T) {
  179. assert := assert.New(t)
  180. // Port not allowed
  181. status, err := getProxyStatus(ProxyTcpPortNotAllowed)
  182. if assert.NoError(err) {
  183. assert.Equal(client.ProxyStatusStartErr, status.Status)
  184. assert.True(strings.Contains(status.Err, server.ErrPortNotAllowed.Error()))
  185. }
  186. status, err = getProxyStatus(ProxyUdpPortNotAllowed)
  187. if assert.NoError(err) {
  188. assert.Equal(client.ProxyStatusStartErr, status.Status)
  189. assert.True(strings.Contains(status.Err, server.ErrPortNotAllowed.Error()))
  190. }
  191. status, err = getProxyStatus(ProxyTcpPortUnavailable)
  192. if assert.NoError(err) {
  193. assert.Equal(client.ProxyStatusStartErr, status.Status)
  194. assert.True(strings.Contains(status.Err, server.ErrPortUnAvailable.Error()))
  195. }
  196. // Port normal
  197. status, err = getProxyStatus(ProxyTcpPortNormal)
  198. if assert.NoError(err) {
  199. assert.Equal(client.ProxyStatusRunning, status.Status)
  200. }
  201. status, err = getProxyStatus(ProxyUdpPortNormal)
  202. if assert.NoError(err) {
  203. assert.Equal(client.ProxyStatusRunning, status.Status)
  204. }
  205. }
  206. func TestRandomPort(t *testing.T) {
  207. assert := assert.New(t)
  208. // tcp
  209. status, err := getProxyStatus(ProxyTcpRandomPort)
  210. if assert.NoError(err) {
  211. addr := status.RemoteAddr
  212. res, err := sendTcpMsg(addr, TEST_TCP_ECHO_STR)
  213. assert.NoError(err)
  214. assert.Equal(TEST_TCP_ECHO_STR, res)
  215. }
  216. // udp
  217. status, err = getProxyStatus(ProxyUdpRandomPort)
  218. if assert.NoError(err) {
  219. addr := status.RemoteAddr
  220. res, err := sendUdpMsg(addr, TEST_UDP_ECHO_STR)
  221. assert.NoError(err)
  222. assert.Equal(TEST_UDP_ECHO_STR, res)
  223. }
  224. }
  225. func TestPluginHttpProxy(t *testing.T) {
  226. assert := assert.New(t)
  227. status, err := getProxyStatus(ProxyHttpProxy)
  228. if assert.NoError(err) {
  229. assert.Equal(client.ProxyStatusRunning, status.Status)
  230. // http proxy
  231. addr := status.RemoteAddr
  232. code, body, err := sendHttpMsg("GET", fmt.Sprintf("http://127.0.0.1:%d", TEST_HTTP_FRP_PORT),
  233. "", nil, "http://"+addr)
  234. if assert.NoError(err) {
  235. assert.Equal(200, code)
  236. assert.Equal(TEST_HTTP_NORMAL_STR, body)
  237. }
  238. // connect method
  239. conn, err := net.ConnectTcpServerByHttpProxy("http://"+addr, fmt.Sprintf("127.0.0.1:%d", TEST_TCP_FRP_PORT))
  240. if assert.NoError(err) {
  241. res, err := sendTcpMsgByConn(conn, TEST_TCP_ECHO_STR)
  242. assert.NoError(err)
  243. assert.Equal(TEST_TCP_ECHO_STR, res)
  244. }
  245. }
  246. }
  247. func TestRangePortsMapping(t *testing.T) {
  248. assert := assert.New(t)
  249. for i := 0; i < 3; i++ {
  250. name := fmt.Sprintf("%s_%d", ProxyRangeTcpPrefix, i)
  251. status, err := getProxyStatus(name)
  252. if assert.NoError(err) {
  253. assert.Equal(client.ProxyStatusRunning, status.Status)
  254. }
  255. }
  256. }