client_test.go 16 KB

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