client_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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/stretchr/testify/assert"
  18. "github.com/fatedier/frp/pkg/auth"
  19. "github.com/fatedier/frp/pkg/consts"
  20. )
  21. const (
  22. testUser = "test"
  23. )
  24. var testClientBytesWithFull = []byte(`
  25. # [common] is integral section
  26. [common]
  27. server_addr = 0.0.0.9
  28. server_port = 7009
  29. http_proxy = http://user:passwd@192.168.1.128:8080
  30. log_file = ./frpc.log9
  31. log_way = file
  32. log_level = info9
  33. log_max_days = 39
  34. disable_log_color = false
  35. authenticate_heartbeats = false
  36. authenticate_new_work_conns = false
  37. token = 12345678
  38. oidc_client_id = client-id
  39. oidc_client_secret = client-secret
  40. oidc_audience = audience
  41. oidc_token_endpoint_url = endpoint_url
  42. admin_addr = 127.0.0.9
  43. admin_port = 7409
  44. admin_user = admin9
  45. admin_pwd = admin9
  46. assets_dir = ./static9
  47. pool_count = 59
  48. tcp_mux
  49. user = your_name
  50. login_fail_exit
  51. protocol = tcp
  52. tls_enable = true
  53. tls_cert_file = client.crt
  54. tls_key_file = client.key
  55. tls_trusted_ca_file = ca.crt
  56. tls_server_name = example.com
  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. func Test_LoadClientCommonConf(t *testing.T) {
  216. assert := assert.New(t)
  217. expected := ClientCommonConf{
  218. ClientConfig: auth.ClientConfig{
  219. BaseConfig: auth.BaseConfig{
  220. AuthenticationMethod: "token",
  221. AuthenticateHeartBeats: false,
  222. AuthenticateNewWorkConns: false,
  223. },
  224. TokenConfig: auth.TokenConfig{
  225. Token: "12345678",
  226. },
  227. OidcClientConfig: auth.OidcClientConfig{
  228. OidcClientID: "client-id",
  229. OidcClientSecret: "client-secret",
  230. OidcAudience: "audience",
  231. OidcTokenEndpointURL: "endpoint_url",
  232. },
  233. },
  234. ServerAddr: "0.0.0.9",
  235. ServerPort: 7009,
  236. DialServerTimeout: 10,
  237. DialServerKeepAlive: 7200,
  238. HTTPProxy: "http://user:passwd@192.168.1.128:8080",
  239. LogFile: "./frpc.log9",
  240. LogWay: "file",
  241. LogLevel: "info9",
  242. LogMaxDays: 39,
  243. DisableLogColor: false,
  244. AdminAddr: "127.0.0.9",
  245. AdminPort: 7409,
  246. AdminUser: "admin9",
  247. AdminPwd: "admin9",
  248. AssetsDir: "./static9",
  249. PoolCount: 59,
  250. TCPMux: true,
  251. TCPMuxKeepaliveInterval: 60,
  252. User: "your_name",
  253. LoginFailExit: true,
  254. Protocol: "tcp",
  255. QUICKeepalivePeriod: 10,
  256. QUICMaxIdleTimeout: 30,
  257. QUICMaxIncomingStreams: 100000,
  258. TLSEnable: true,
  259. TLSCertFile: "client.crt",
  260. TLSKeyFile: "client.key",
  261. TLSTrustedCaFile: "ca.crt",
  262. TLSServerName: "example.com",
  263. DNSServer: "8.8.8.9",
  264. Start: []string{"ssh", "dns"},
  265. HeartbeatInterval: 39,
  266. HeartbeatTimeout: 99,
  267. Metas: map[string]string{
  268. "var1": "123",
  269. "var2": "234",
  270. },
  271. UDPPacketSize: 1509,
  272. IncludeConfigFiles: []string{},
  273. }
  274. common, err := UnmarshalClientConfFromIni(testClientBytesWithFull)
  275. assert.NoError(err)
  276. assert.EqualValues(expected, common)
  277. }
  278. func Test_LoadClientBasicConf(t *testing.T) {
  279. assert := assert.New(t)
  280. proxyExpected := map[string]ProxyConf{
  281. testUser + ".ssh": &TCPProxyConf{
  282. BaseProxyConf: BaseProxyConf{
  283. ProxyName: testUser + ".ssh",
  284. ProxyType: consts.TCPProxy,
  285. UseCompression: true,
  286. UseEncryption: true,
  287. Group: "test_group",
  288. GroupKey: "123456",
  289. BandwidthLimit: MustBandwidthQuantity("19MB"),
  290. Metas: map[string]string{
  291. "var1": "123",
  292. "var2": "234",
  293. },
  294. LocalSvrConf: LocalSvrConf{
  295. LocalIP: "127.0.0.9",
  296. LocalPort: 29,
  297. },
  298. HealthCheckConf: HealthCheckConf{
  299. HealthCheckType: consts.TCPProxy,
  300. HealthCheckTimeoutS: 3,
  301. HealthCheckMaxFailed: 3,
  302. HealthCheckIntervalS: 19,
  303. HealthCheckAddr: "127.0.0.9:29",
  304. },
  305. },
  306. RemotePort: 6009,
  307. },
  308. testUser + ".ssh_random": &TCPProxyConf{
  309. BaseProxyConf: BaseProxyConf{
  310. ProxyName: testUser + ".ssh_random",
  311. ProxyType: consts.TCPProxy,
  312. LocalSvrConf: LocalSvrConf{
  313. LocalIP: "127.0.0.9",
  314. LocalPort: 29,
  315. },
  316. },
  317. RemotePort: 9,
  318. },
  319. testUser + ".tcp_port_0": &TCPProxyConf{
  320. BaseProxyConf: BaseProxyConf{
  321. ProxyName: testUser + ".tcp_port_0",
  322. ProxyType: consts.TCPProxy,
  323. LocalSvrConf: LocalSvrConf{
  324. LocalIP: "127.0.0.9",
  325. LocalPort: 6010,
  326. },
  327. },
  328. RemotePort: 6010,
  329. },
  330. testUser + ".tcp_port_1": &TCPProxyConf{
  331. BaseProxyConf: BaseProxyConf{
  332. ProxyName: testUser + ".tcp_port_1",
  333. ProxyType: consts.TCPProxy,
  334. LocalSvrConf: LocalSvrConf{
  335. LocalIP: "127.0.0.9",
  336. LocalPort: 6011,
  337. },
  338. },
  339. RemotePort: 6011,
  340. },
  341. testUser + ".tcp_port_2": &TCPProxyConf{
  342. BaseProxyConf: BaseProxyConf{
  343. ProxyName: testUser + ".tcp_port_2",
  344. ProxyType: consts.TCPProxy,
  345. LocalSvrConf: LocalSvrConf{
  346. LocalIP: "127.0.0.9",
  347. LocalPort: 6019,
  348. },
  349. },
  350. RemotePort: 6019,
  351. },
  352. testUser + ".dns": &UDPProxyConf{
  353. BaseProxyConf: BaseProxyConf{
  354. ProxyName: testUser + ".dns",
  355. ProxyType: consts.UDPProxy,
  356. UseEncryption: true,
  357. UseCompression: true,
  358. LocalSvrConf: LocalSvrConf{
  359. LocalIP: "114.114.114.114",
  360. LocalPort: 59,
  361. },
  362. },
  363. RemotePort: 6009,
  364. },
  365. testUser + ".udp_port_0": &UDPProxyConf{
  366. BaseProxyConf: BaseProxyConf{
  367. ProxyName: testUser + ".udp_port_0",
  368. ProxyType: consts.UDPProxy,
  369. UseEncryption: true,
  370. UseCompression: true,
  371. LocalSvrConf: LocalSvrConf{
  372. LocalIP: "114.114.114.114",
  373. LocalPort: 6000,
  374. },
  375. },
  376. RemotePort: 6000,
  377. },
  378. testUser + ".udp_port_1": &UDPProxyConf{
  379. BaseProxyConf: BaseProxyConf{
  380. ProxyName: testUser + ".udp_port_1",
  381. ProxyType: consts.UDPProxy,
  382. UseEncryption: true,
  383. UseCompression: true,
  384. LocalSvrConf: LocalSvrConf{
  385. LocalIP: "114.114.114.114",
  386. LocalPort: 6010,
  387. },
  388. },
  389. RemotePort: 6010,
  390. },
  391. testUser + ".udp_port_2": &UDPProxyConf{
  392. BaseProxyConf: BaseProxyConf{
  393. ProxyName: testUser + ".udp_port_2",
  394. ProxyType: consts.UDPProxy,
  395. UseEncryption: true,
  396. UseCompression: true,
  397. LocalSvrConf: LocalSvrConf{
  398. LocalIP: "114.114.114.114",
  399. LocalPort: 6011,
  400. },
  401. },
  402. RemotePort: 6011,
  403. },
  404. testUser + ".web01": &HTTPProxyConf{
  405. BaseProxyConf: BaseProxyConf{
  406. ProxyName: testUser + ".web01",
  407. ProxyType: consts.HTTPProxy,
  408. UseCompression: true,
  409. UseEncryption: true,
  410. LocalSvrConf: LocalSvrConf{
  411. LocalIP: "127.0.0.9",
  412. LocalPort: 89,
  413. },
  414. HealthCheckConf: HealthCheckConf{
  415. HealthCheckType: consts.HTTPProxy,
  416. HealthCheckTimeoutS: 3,
  417. HealthCheckMaxFailed: 3,
  418. HealthCheckIntervalS: 19,
  419. HealthCheckURL: "http://127.0.0.9:89/status",
  420. },
  421. },
  422. DomainConf: DomainConf{
  423. CustomDomains: []string{"web02.yourdomain.com"},
  424. SubDomain: "web01",
  425. },
  426. Locations: []string{"/", "/pic"},
  427. HTTPUser: "admin",
  428. HTTPPwd: "admin",
  429. HostHeaderRewrite: "example.com",
  430. Headers: map[string]string{
  431. "X-From-Where": "frp",
  432. },
  433. },
  434. testUser + ".web02": &HTTPSProxyConf{
  435. BaseProxyConf: BaseProxyConf{
  436. ProxyName: testUser + ".web02",
  437. ProxyType: consts.HTTPSProxy,
  438. UseCompression: true,
  439. UseEncryption: true,
  440. LocalSvrConf: LocalSvrConf{
  441. LocalIP: "127.0.0.9",
  442. LocalPort: 8009,
  443. },
  444. ProxyProtocolVersion: "v2",
  445. },
  446. DomainConf: DomainConf{
  447. CustomDomains: []string{"web02.yourdomain.com"},
  448. SubDomain: "web01",
  449. },
  450. },
  451. testUser + ".secret_tcp": &STCPProxyConf{
  452. BaseProxyConf: BaseProxyConf{
  453. ProxyName: testUser + ".secret_tcp",
  454. ProxyType: consts.STCPProxy,
  455. LocalSvrConf: LocalSvrConf{
  456. LocalIP: "127.0.0.1",
  457. LocalPort: 22,
  458. },
  459. },
  460. Role: "server",
  461. Sk: "abcdefg",
  462. },
  463. testUser + ".p2p_tcp": &XTCPProxyConf{
  464. BaseProxyConf: BaseProxyConf{
  465. ProxyName: testUser + ".p2p_tcp",
  466. ProxyType: consts.XTCPProxy,
  467. LocalSvrConf: LocalSvrConf{
  468. LocalIP: "127.0.0.1",
  469. LocalPort: 22,
  470. },
  471. },
  472. Role: "server",
  473. Sk: "abcdefg",
  474. },
  475. testUser + ".tcpmuxhttpconnect": &TCPMuxProxyConf{
  476. BaseProxyConf: BaseProxyConf{
  477. ProxyName: testUser + ".tcpmuxhttpconnect",
  478. ProxyType: consts.TCPMuxProxy,
  479. LocalSvrConf: LocalSvrConf{
  480. LocalIP: "127.0.0.1",
  481. LocalPort: 10701,
  482. },
  483. },
  484. DomainConf: DomainConf{
  485. CustomDomains: []string{"tunnel1"},
  486. SubDomain: "",
  487. },
  488. Multiplexer: "httpconnect",
  489. },
  490. testUser + ".plugin_unix_domain_socket": &TCPProxyConf{
  491. BaseProxyConf: BaseProxyConf{
  492. ProxyName: testUser + ".plugin_unix_domain_socket",
  493. ProxyType: consts.TCPProxy,
  494. LocalSvrConf: LocalSvrConf{
  495. LocalIP: "127.0.0.1",
  496. Plugin: "unix_domain_socket",
  497. PluginParams: map[string]string{
  498. "plugin_unix_path": "/var/run/docker.sock",
  499. },
  500. },
  501. },
  502. RemotePort: 6003,
  503. },
  504. testUser + ".plugin_http_proxy": &TCPProxyConf{
  505. BaseProxyConf: BaseProxyConf{
  506. ProxyName: testUser + ".plugin_http_proxy",
  507. ProxyType: consts.TCPProxy,
  508. LocalSvrConf: LocalSvrConf{
  509. LocalIP: "127.0.0.1",
  510. Plugin: "http_proxy",
  511. PluginParams: map[string]string{
  512. "plugin_http_user": "abc",
  513. "plugin_http_passwd": "abc",
  514. },
  515. },
  516. },
  517. RemotePort: 6004,
  518. },
  519. testUser + ".plugin_socks5": &TCPProxyConf{
  520. BaseProxyConf: BaseProxyConf{
  521. ProxyName: testUser + ".plugin_socks5",
  522. ProxyType: consts.TCPProxy,
  523. LocalSvrConf: LocalSvrConf{
  524. LocalIP: "127.0.0.1",
  525. Plugin: "socks5",
  526. PluginParams: map[string]string{
  527. "plugin_user": "abc",
  528. "plugin_passwd": "abc",
  529. },
  530. },
  531. },
  532. RemotePort: 6005,
  533. },
  534. testUser + ".plugin_static_file": &TCPProxyConf{
  535. BaseProxyConf: BaseProxyConf{
  536. ProxyName: testUser + ".plugin_static_file",
  537. ProxyType: consts.TCPProxy,
  538. LocalSvrConf: LocalSvrConf{
  539. LocalIP: "127.0.0.1",
  540. Plugin: "static_file",
  541. PluginParams: map[string]string{
  542. "plugin_local_path": "/var/www/blog",
  543. "plugin_strip_prefix": "static",
  544. "plugin_http_user": "abc",
  545. "plugin_http_passwd": "abc",
  546. },
  547. },
  548. },
  549. RemotePort: 6006,
  550. },
  551. testUser + ".plugin_https2http": &HTTPSProxyConf{
  552. BaseProxyConf: BaseProxyConf{
  553. ProxyName: testUser + ".plugin_https2http",
  554. ProxyType: consts.HTTPSProxy,
  555. LocalSvrConf: LocalSvrConf{
  556. LocalIP: "127.0.0.1",
  557. Plugin: "https2http",
  558. PluginParams: map[string]string{
  559. "plugin_local_addr": "127.0.0.1:80",
  560. "plugin_crt_path": "./server.crt",
  561. "plugin_key_path": "./server.key",
  562. "plugin_host_header_rewrite": "127.0.0.1",
  563. "plugin_header_X-From-Where": "frp",
  564. },
  565. },
  566. },
  567. DomainConf: DomainConf{
  568. CustomDomains: []string{"test.yourdomain.com"},
  569. },
  570. },
  571. testUser + ".plugin_http2https": &HTTPProxyConf{
  572. BaseProxyConf: BaseProxyConf{
  573. ProxyName: testUser + ".plugin_http2https",
  574. ProxyType: consts.HTTPProxy,
  575. LocalSvrConf: LocalSvrConf{
  576. LocalIP: "127.0.0.1",
  577. Plugin: "http2https",
  578. PluginParams: map[string]string{
  579. "plugin_local_addr": "127.0.0.1:443",
  580. "plugin_host_header_rewrite": "127.0.0.1",
  581. "plugin_header_X-From-Where": "frp",
  582. },
  583. },
  584. },
  585. DomainConf: DomainConf{
  586. CustomDomains: []string{"test.yourdomain.com"},
  587. },
  588. },
  589. }
  590. visitorExpected := map[string]VisitorConf{
  591. testUser + ".secret_tcp_visitor": &STCPVisitorConf{
  592. BaseVisitorConf: BaseVisitorConf{
  593. ProxyName: testUser + ".secret_tcp_visitor",
  594. ProxyType: consts.STCPProxy,
  595. Role: "visitor",
  596. Sk: "abcdefg",
  597. ServerName: testVisitorPrefix + "secret_tcp",
  598. BindAddr: "127.0.0.1",
  599. BindPort: 9000,
  600. },
  601. },
  602. testUser + ".p2p_tcp_visitor": &XTCPVisitorConf{
  603. BaseVisitorConf: BaseVisitorConf{
  604. ProxyName: testUser + ".p2p_tcp_visitor",
  605. ProxyType: consts.XTCPProxy,
  606. Role: "visitor",
  607. Sk: "abcdefg",
  608. ServerName: testProxyPrefix + "p2p_tcp",
  609. BindAddr: "127.0.0.1",
  610. BindPort: 9001,
  611. },
  612. },
  613. }
  614. proxyActual, visitorActual, err := LoadAllProxyConfsFromIni(testUser, testClientBytesWithFull, nil)
  615. assert.NoError(err)
  616. assert.Equal(proxyExpected, proxyActual)
  617. assert.Equal(visitorExpected, visitorActual)
  618. }