client_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. // Copyright 2020 The frp Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package config
  15. import (
  16. "testing"
  17. "github.com/fatedier/frp/pkg/auth"
  18. "github.com/fatedier/frp/pkg/consts"
  19. "github.com/stretchr/testify/assert"
  20. )
  21. const (
  22. testUser = "test"
  23. )
  24. var (
  25. testClientBytesWithFull = []byte(`
  26. # [common] is integral section
  27. [common]
  28. server_addr = 0.0.0.9
  29. server_port = 7009
  30. http_proxy = http://user:passwd@192.168.1.128:8080
  31. log_file = ./frpc.log9
  32. log_way = file
  33. log_level = info9
  34. log_max_days = 39
  35. disable_log_color = false
  36. authenticate_heartbeats = false
  37. authenticate_new_work_conns = false
  38. token = 12345678
  39. oidc_client_id = client-id
  40. oidc_client_secret = client-secret
  41. oidc_audience = audience
  42. oidc_token_endpoint_url = endpoint_url
  43. admin_addr = 127.0.0.9
  44. admin_port = 7409
  45. admin_user = admin9
  46. admin_pwd = admin9
  47. assets_dir = ./static9
  48. pool_count = 59
  49. tcp_mux
  50. user = your_name
  51. login_fail_exit
  52. protocol = tcp
  53. tls_enable = true
  54. tls_cert_file = client.crt
  55. tls_key_file = client.key
  56. tls_trusted_ca_file = ca.crt
  57. tls_server_name = example.com
  58. dns_server = 8.8.8.9
  59. start = ssh,dns
  60. heartbeat_interval = 39
  61. heartbeat_timeout = 99
  62. meta_var1 = 123
  63. meta_var2 = 234
  64. udp_packet_size = 1509
  65. # all proxy
  66. [ssh]
  67. type = tcp
  68. local_ip = 127.0.0.9
  69. local_port = 29
  70. bandwidth_limit = 19MB
  71. use_encryption
  72. use_compression
  73. remote_port = 6009
  74. group = test_group
  75. group_key = 123456
  76. health_check_type = tcp
  77. health_check_timeout_s = 3
  78. health_check_max_failed = 3
  79. health_check_interval_s = 19
  80. meta_var1 = 123
  81. meta_var2 = 234
  82. [ssh_random]
  83. type = tcp
  84. local_ip = 127.0.0.9
  85. local_port = 29
  86. remote_port = 9
  87. [range:tcp_port]
  88. type = tcp
  89. local_ip = 127.0.0.9
  90. local_port = 6010-6011,6019
  91. remote_port = 6010-6011,6019
  92. use_encryption = false
  93. use_compression = false
  94. [dns]
  95. type = udp
  96. local_ip = 114.114.114.114
  97. local_port = 59
  98. remote_port = 6009
  99. use_encryption
  100. use_compression
  101. [range:udp_port]
  102. type = udp
  103. local_ip = 114.114.114.114
  104. local_port = 6000,6010-6011
  105. remote_port = 6000,6010-6011
  106. use_encryption
  107. use_compression
  108. [web01]
  109. type = http
  110. local_ip = 127.0.0.9
  111. local_port = 89
  112. use_encryption
  113. use_compression
  114. http_user = admin
  115. http_pwd = admin
  116. subdomain = web01
  117. custom_domains = web02.yourdomain.com
  118. locations = /,/pic
  119. host_header_rewrite = example.com
  120. header_X-From-Where = frp
  121. health_check_type = http
  122. health_check_url = /status
  123. health_check_interval_s = 19
  124. health_check_max_failed = 3
  125. health_check_timeout_s = 3
  126. [web02]
  127. type = https
  128. local_ip = 127.0.0.9
  129. local_port = 8009
  130. use_encryption
  131. use_compression
  132. subdomain = web01
  133. custom_domains = web02.yourdomain.com
  134. proxy_protocol_version = v2
  135. [secret_tcp]
  136. type = stcp
  137. sk = abcdefg
  138. local_ip = 127.0.0.1
  139. local_port = 22
  140. use_encryption = false
  141. use_compression = false
  142. [p2p_tcp]
  143. type = xtcp
  144. sk = abcdefg
  145. local_ip = 127.0.0.1
  146. local_port = 22
  147. use_encryption = false
  148. use_compression = false
  149. [tcpmuxhttpconnect]
  150. type = tcpmux
  151. multiplexer = httpconnect
  152. local_ip = 127.0.0.1
  153. local_port = 10701
  154. custom_domains = tunnel1
  155. [plugin_unix_domain_socket]
  156. type = tcp
  157. remote_port = 6003
  158. plugin = unix_domain_socket
  159. plugin_unix_path = /var/run/docker.sock
  160. [plugin_http_proxy]
  161. type = tcp
  162. remote_port = 6004
  163. plugin = http_proxy
  164. plugin_http_user = abc
  165. plugin_http_passwd = abc
  166. [plugin_socks5]
  167. type = tcp
  168. remote_port = 6005
  169. plugin = socks5
  170. plugin_user = abc
  171. plugin_passwd = abc
  172. [plugin_static_file]
  173. type = tcp
  174. remote_port = 6006
  175. plugin = static_file
  176. plugin_local_path = /var/www/blog
  177. plugin_strip_prefix = static
  178. plugin_http_user = abc
  179. plugin_http_passwd = abc
  180. [plugin_https2http]
  181. type = https
  182. custom_domains = test.yourdomain.com
  183. plugin = https2http
  184. plugin_local_addr = 127.0.0.1:80
  185. plugin_crt_path = ./server.crt
  186. plugin_key_path = ./server.key
  187. plugin_host_header_rewrite = 127.0.0.1
  188. plugin_header_X-From-Where = frp
  189. [plugin_http2https]
  190. type = http
  191. custom_domains = test.yourdomain.com
  192. plugin = http2https
  193. plugin_local_addr = 127.0.0.1:443
  194. plugin_host_header_rewrite = 127.0.0.1
  195. plugin_header_X-From-Where = frp
  196. # visitor
  197. [secret_tcp_visitor]
  198. role = visitor
  199. type = stcp
  200. server_name = secret_tcp
  201. sk = abcdefg
  202. bind_addr = 127.0.0.1
  203. bind_port = 9000
  204. use_encryption = false
  205. use_compression = false
  206. [p2p_tcp_visitor]
  207. role = visitor
  208. type = xtcp
  209. server_name = p2p_tcp
  210. sk = abcdefg
  211. bind_addr = 127.0.0.1
  212. bind_port = 9001
  213. use_encryption = false
  214. use_compression = false
  215. `)
  216. )
  217. func Test_LoadClientCommonConf(t *testing.T) {
  218. assert := assert.New(t)
  219. expected := ClientCommonConf{
  220. ClientConfig: auth.ClientConfig{
  221. BaseConfig: auth.BaseConfig{
  222. AuthenticationMethod: "token",
  223. AuthenticateHeartBeats: false,
  224. AuthenticateNewWorkConns: false,
  225. },
  226. TokenConfig: auth.TokenConfig{
  227. Token: "12345678",
  228. },
  229. OidcClientConfig: auth.OidcClientConfig{
  230. OidcClientID: "client-id",
  231. OidcClientSecret: "client-secret",
  232. OidcAudience: "audience",
  233. OidcTokenEndpointURL: "endpoint_url",
  234. },
  235. },
  236. ServerAddr: "0.0.0.9",
  237. ServerPort: 7009,
  238. DialServerTimeout: 10,
  239. DialServerKeepAlive: 7200,
  240. HTTPProxy: "http://user:passwd@192.168.1.128:8080",
  241. LogFile: "./frpc.log9",
  242. LogWay: "file",
  243. LogLevel: "info9",
  244. LogMaxDays: 39,
  245. DisableLogColor: false,
  246. AdminAddr: "127.0.0.9",
  247. AdminPort: 7409,
  248. AdminUser: "admin9",
  249. AdminPwd: "admin9",
  250. AssetsDir: "./static9",
  251. PoolCount: 59,
  252. TCPMux: true,
  253. TCPMuxKeepaliveInterval: 60,
  254. User: "your_name",
  255. LoginFailExit: true,
  256. Protocol: "tcp",
  257. TLSEnable: true,
  258. TLSCertFile: "client.crt",
  259. TLSKeyFile: "client.key",
  260. TLSTrustedCaFile: "ca.crt",
  261. TLSServerName: "example.com",
  262. DNSServer: "8.8.8.9",
  263. Start: []string{"ssh", "dns"},
  264. HeartbeatInterval: 39,
  265. HeartbeatTimeout: 99,
  266. Metas: map[string]string{
  267. "var1": "123",
  268. "var2": "234",
  269. },
  270. UDPPacketSize: 1509,
  271. IncludeConfigFiles: []string{},
  272. }
  273. common, err := UnmarshalClientConfFromIni(testClientBytesWithFull)
  274. assert.NoError(err)
  275. assert.EqualValues(expected, common)
  276. }
  277. func Test_LoadClientBasicConf(t *testing.T) {
  278. assert := assert.New(t)
  279. proxyExpected := map[string]ProxyConf{
  280. testUser + ".ssh": &TCPProxyConf{
  281. BaseProxyConf: BaseProxyConf{
  282. ProxyName: testUser + ".ssh",
  283. ProxyType: consts.TCPProxy,
  284. UseCompression: true,
  285. UseEncryption: true,
  286. Group: "test_group",
  287. GroupKey: "123456",
  288. BandwidthLimit: MustBandwidthQuantity("19MB"),
  289. Metas: map[string]string{
  290. "var1": "123",
  291. "var2": "234",
  292. },
  293. LocalSvrConf: LocalSvrConf{
  294. LocalIP: "127.0.0.9",
  295. LocalPort: 29,
  296. },
  297. HealthCheckConf: HealthCheckConf{
  298. HealthCheckType: consts.TCPProxy,
  299. HealthCheckTimeoutS: 3,
  300. HealthCheckMaxFailed: 3,
  301. HealthCheckIntervalS: 19,
  302. HealthCheckAddr: "127.0.0.9:29",
  303. },
  304. },
  305. RemotePort: 6009,
  306. },
  307. testUser + ".ssh_random": &TCPProxyConf{
  308. BaseProxyConf: BaseProxyConf{
  309. ProxyName: testUser + ".ssh_random",
  310. ProxyType: consts.TCPProxy,
  311. LocalSvrConf: LocalSvrConf{
  312. LocalIP: "127.0.0.9",
  313. LocalPort: 29,
  314. },
  315. },
  316. RemotePort: 9,
  317. },
  318. testUser + ".tcp_port_0": &TCPProxyConf{
  319. BaseProxyConf: BaseProxyConf{
  320. ProxyName: testUser + ".tcp_port_0",
  321. ProxyType: consts.TCPProxy,
  322. LocalSvrConf: LocalSvrConf{
  323. LocalIP: "127.0.0.9",
  324. LocalPort: 6010,
  325. },
  326. },
  327. RemotePort: 6010,
  328. },
  329. testUser + ".tcp_port_1": &TCPProxyConf{
  330. BaseProxyConf: BaseProxyConf{
  331. ProxyName: testUser + ".tcp_port_1",
  332. ProxyType: consts.TCPProxy,
  333. LocalSvrConf: LocalSvrConf{
  334. LocalIP: "127.0.0.9",
  335. LocalPort: 6011,
  336. },
  337. },
  338. RemotePort: 6011,
  339. },
  340. testUser + ".tcp_port_2": &TCPProxyConf{
  341. BaseProxyConf: BaseProxyConf{
  342. ProxyName: testUser + ".tcp_port_2",
  343. ProxyType: consts.TCPProxy,
  344. LocalSvrConf: LocalSvrConf{
  345. LocalIP: "127.0.0.9",
  346. LocalPort: 6019,
  347. },
  348. },
  349. RemotePort: 6019,
  350. },
  351. testUser + ".dns": &UDPProxyConf{
  352. BaseProxyConf: BaseProxyConf{
  353. ProxyName: testUser + ".dns",
  354. ProxyType: consts.UDPProxy,
  355. UseEncryption: true,
  356. UseCompression: true,
  357. LocalSvrConf: LocalSvrConf{
  358. LocalIP: "114.114.114.114",
  359. LocalPort: 59,
  360. },
  361. },
  362. RemotePort: 6009,
  363. },
  364. testUser + ".udp_port_0": &UDPProxyConf{
  365. BaseProxyConf: BaseProxyConf{
  366. ProxyName: testUser + ".udp_port_0",
  367. ProxyType: consts.UDPProxy,
  368. UseEncryption: true,
  369. UseCompression: true,
  370. LocalSvrConf: LocalSvrConf{
  371. LocalIP: "114.114.114.114",
  372. LocalPort: 6000,
  373. },
  374. },
  375. RemotePort: 6000,
  376. },
  377. testUser + ".udp_port_1": &UDPProxyConf{
  378. BaseProxyConf: BaseProxyConf{
  379. ProxyName: testUser + ".udp_port_1",
  380. ProxyType: consts.UDPProxy,
  381. UseEncryption: true,
  382. UseCompression: true,
  383. LocalSvrConf: LocalSvrConf{
  384. LocalIP: "114.114.114.114",
  385. LocalPort: 6010,
  386. },
  387. },
  388. RemotePort: 6010,
  389. },
  390. testUser + ".udp_port_2": &UDPProxyConf{
  391. BaseProxyConf: BaseProxyConf{
  392. ProxyName: testUser + ".udp_port_2",
  393. ProxyType: consts.UDPProxy,
  394. UseEncryption: true,
  395. UseCompression: true,
  396. LocalSvrConf: LocalSvrConf{
  397. LocalIP: "114.114.114.114",
  398. LocalPort: 6011,
  399. },
  400. },
  401. RemotePort: 6011,
  402. },
  403. testUser + ".web01": &HTTPProxyConf{
  404. BaseProxyConf: BaseProxyConf{
  405. ProxyName: testUser + ".web01",
  406. ProxyType: consts.HTTPProxy,
  407. UseCompression: true,
  408. UseEncryption: true,
  409. LocalSvrConf: LocalSvrConf{
  410. LocalIP: "127.0.0.9",
  411. LocalPort: 89,
  412. },
  413. HealthCheckConf: HealthCheckConf{
  414. HealthCheckType: consts.HTTPProxy,
  415. HealthCheckTimeoutS: 3,
  416. HealthCheckMaxFailed: 3,
  417. HealthCheckIntervalS: 19,
  418. HealthCheckURL: "http://127.0.0.9:89/status",
  419. },
  420. },
  421. DomainConf: DomainConf{
  422. CustomDomains: []string{"web02.yourdomain.com"},
  423. SubDomain: "web01",
  424. },
  425. Locations: []string{"/", "/pic"},
  426. HTTPUser: "admin",
  427. HTTPPwd: "admin",
  428. HostHeaderRewrite: "example.com",
  429. Headers: map[string]string{
  430. "X-From-Where": "frp",
  431. },
  432. },
  433. testUser + ".web02": &HTTPSProxyConf{
  434. BaseProxyConf: BaseProxyConf{
  435. ProxyName: testUser + ".web02",
  436. ProxyType: consts.HTTPSProxy,
  437. UseCompression: true,
  438. UseEncryption: true,
  439. LocalSvrConf: LocalSvrConf{
  440. LocalIP: "127.0.0.9",
  441. LocalPort: 8009,
  442. },
  443. ProxyProtocolVersion: "v2",
  444. },
  445. DomainConf: DomainConf{
  446. CustomDomains: []string{"web02.yourdomain.com"},
  447. SubDomain: "web01",
  448. },
  449. },
  450. testUser + ".secret_tcp": &STCPProxyConf{
  451. BaseProxyConf: BaseProxyConf{
  452. ProxyName: testUser + ".secret_tcp",
  453. ProxyType: consts.STCPProxy,
  454. LocalSvrConf: LocalSvrConf{
  455. LocalIP: "127.0.0.1",
  456. LocalPort: 22,
  457. },
  458. },
  459. Role: "server",
  460. Sk: "abcdefg",
  461. },
  462. testUser + ".p2p_tcp": &XTCPProxyConf{
  463. BaseProxyConf: BaseProxyConf{
  464. ProxyName: testUser + ".p2p_tcp",
  465. ProxyType: consts.XTCPProxy,
  466. LocalSvrConf: LocalSvrConf{
  467. LocalIP: "127.0.0.1",
  468. LocalPort: 22,
  469. },
  470. },
  471. Role: "server",
  472. Sk: "abcdefg",
  473. },
  474. testUser + ".tcpmuxhttpconnect": &TCPMuxProxyConf{
  475. BaseProxyConf: BaseProxyConf{
  476. ProxyName: testUser + ".tcpmuxhttpconnect",
  477. ProxyType: consts.TCPMuxProxy,
  478. LocalSvrConf: LocalSvrConf{
  479. LocalIP: "127.0.0.1",
  480. LocalPort: 10701,
  481. },
  482. },
  483. DomainConf: DomainConf{
  484. CustomDomains: []string{"tunnel1"},
  485. SubDomain: "",
  486. },
  487. Multiplexer: "httpconnect",
  488. },
  489. testUser + ".plugin_unix_domain_socket": &TCPProxyConf{
  490. BaseProxyConf: BaseProxyConf{
  491. ProxyName: testUser + ".plugin_unix_domain_socket",
  492. ProxyType: consts.TCPProxy,
  493. LocalSvrConf: LocalSvrConf{
  494. LocalIP: "127.0.0.1",
  495. Plugin: "unix_domain_socket",
  496. PluginParams: map[string]string{
  497. "plugin_unix_path": "/var/run/docker.sock",
  498. },
  499. },
  500. },
  501. RemotePort: 6003,
  502. },
  503. testUser + ".plugin_http_proxy": &TCPProxyConf{
  504. BaseProxyConf: BaseProxyConf{
  505. ProxyName: testUser + ".plugin_http_proxy",
  506. ProxyType: consts.TCPProxy,
  507. LocalSvrConf: LocalSvrConf{
  508. LocalIP: "127.0.0.1",
  509. Plugin: "http_proxy",
  510. PluginParams: map[string]string{
  511. "plugin_http_user": "abc",
  512. "plugin_http_passwd": "abc",
  513. },
  514. },
  515. },
  516. RemotePort: 6004,
  517. },
  518. testUser + ".plugin_socks5": &TCPProxyConf{
  519. BaseProxyConf: BaseProxyConf{
  520. ProxyName: testUser + ".plugin_socks5",
  521. ProxyType: consts.TCPProxy,
  522. LocalSvrConf: LocalSvrConf{
  523. LocalIP: "127.0.0.1",
  524. Plugin: "socks5",
  525. PluginParams: map[string]string{
  526. "plugin_user": "abc",
  527. "plugin_passwd": "abc",
  528. },
  529. },
  530. },
  531. RemotePort: 6005,
  532. },
  533. testUser + ".plugin_static_file": &TCPProxyConf{
  534. BaseProxyConf: BaseProxyConf{
  535. ProxyName: testUser + ".plugin_static_file",
  536. ProxyType: consts.TCPProxy,
  537. LocalSvrConf: LocalSvrConf{
  538. LocalIP: "127.0.0.1",
  539. Plugin: "static_file",
  540. PluginParams: map[string]string{
  541. "plugin_local_path": "/var/www/blog",
  542. "plugin_strip_prefix": "static",
  543. "plugin_http_user": "abc",
  544. "plugin_http_passwd": "abc",
  545. },
  546. },
  547. },
  548. RemotePort: 6006,
  549. },
  550. testUser + ".plugin_https2http": &HTTPSProxyConf{
  551. BaseProxyConf: BaseProxyConf{
  552. ProxyName: testUser + ".plugin_https2http",
  553. ProxyType: consts.HTTPSProxy,
  554. LocalSvrConf: LocalSvrConf{
  555. LocalIP: "127.0.0.1",
  556. Plugin: "https2http",
  557. PluginParams: map[string]string{
  558. "plugin_local_addr": "127.0.0.1:80",
  559. "plugin_crt_path": "./server.crt",
  560. "plugin_key_path": "./server.key",
  561. "plugin_host_header_rewrite": "127.0.0.1",
  562. "plugin_header_X-From-Where": "frp",
  563. },
  564. },
  565. },
  566. DomainConf: DomainConf{
  567. CustomDomains: []string{"test.yourdomain.com"},
  568. },
  569. },
  570. testUser + ".plugin_http2https": &HTTPProxyConf{
  571. BaseProxyConf: BaseProxyConf{
  572. ProxyName: testUser + ".plugin_http2https",
  573. ProxyType: consts.HTTPProxy,
  574. LocalSvrConf: LocalSvrConf{
  575. LocalIP: "127.0.0.1",
  576. Plugin: "http2https",
  577. PluginParams: map[string]string{
  578. "plugin_local_addr": "127.0.0.1:443",
  579. "plugin_host_header_rewrite": "127.0.0.1",
  580. "plugin_header_X-From-Where": "frp",
  581. },
  582. },
  583. },
  584. DomainConf: DomainConf{
  585. CustomDomains: []string{"test.yourdomain.com"},
  586. },
  587. },
  588. }
  589. visitorExpected := map[string]VisitorConf{
  590. testUser + ".secret_tcp_visitor": &STCPVisitorConf{
  591. BaseVisitorConf: BaseVisitorConf{
  592. ProxyName: testUser + ".secret_tcp_visitor",
  593. ProxyType: consts.STCPProxy,
  594. Role: "visitor",
  595. Sk: "abcdefg",
  596. ServerName: testVisitorPrefix + "secret_tcp",
  597. BindAddr: "127.0.0.1",
  598. BindPort: 9000,
  599. },
  600. },
  601. testUser + ".p2p_tcp_visitor": &XTCPVisitorConf{
  602. BaseVisitorConf: BaseVisitorConf{
  603. ProxyName: testUser + ".p2p_tcp_visitor",
  604. ProxyType: consts.XTCPProxy,
  605. Role: "visitor",
  606. Sk: "abcdefg",
  607. ServerName: testProxyPrefix + "p2p_tcp",
  608. BindAddr: "127.0.0.1",
  609. BindPort: 9001,
  610. },
  611. },
  612. }
  613. proxyActual, visitorActual, err := LoadAllProxyConfsFromIni(testUser, testClientBytesWithFull, nil)
  614. assert.NoError(err)
  615. assert.Equal(proxyExpected, proxyActual)
  616. assert.Equal(visitorExpected, visitorActual)
  617. }