proxy_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. "gopkg.in/ini.v1"
  19. "github.com/fatedier/frp/pkg/consts"
  20. )
  21. var (
  22. testLoadOptions = ini.LoadOptions{
  23. Insensitive: false,
  24. InsensitiveSections: false,
  25. InsensitiveKeys: false,
  26. IgnoreInlineComment: true,
  27. AllowBooleanKeys: true,
  28. }
  29. testProxyPrefix = "test."
  30. )
  31. func Test_Proxy_Interface(_ *testing.T) {
  32. for name := range proxyConfTypeMap {
  33. NewConfByType(name)
  34. }
  35. }
  36. func Test_Proxy_UnmarshalFromIni(t *testing.T) {
  37. assert := assert.New(t)
  38. testcases := []struct {
  39. sname string
  40. source []byte
  41. expected ProxyConf
  42. }{
  43. {
  44. sname: "ssh",
  45. source: []byte(`
  46. [ssh]
  47. # tcp | udp | http | https | stcp | xtcp, default is tcp
  48. type = tcp
  49. local_ip = 127.0.0.9
  50. local_port = 29
  51. bandwidth_limit = 19MB
  52. bandwidth_limit_mode = server
  53. use_encryption
  54. use_compression
  55. remote_port = 6009
  56. group = test_group
  57. group_key = 123456
  58. health_check_type = tcp
  59. health_check_timeout_s = 3
  60. health_check_max_failed = 3
  61. health_check_interval_s = 19
  62. meta_var1 = 123
  63. meta_var2 = 234`),
  64. expected: &TCPProxyConf{
  65. BaseProxyConf: BaseProxyConf{
  66. ProxyName: testProxyPrefix + "ssh",
  67. ProxyType: consts.TCPProxy,
  68. UseCompression: true,
  69. UseEncryption: true,
  70. Group: "test_group",
  71. GroupKey: "123456",
  72. BandwidthLimit: MustBandwidthQuantity("19MB"),
  73. BandwidthLimitMode: BandwidthLimitModeServer,
  74. Metas: map[string]string{
  75. "var1": "123",
  76. "var2": "234",
  77. },
  78. LocalSvrConf: LocalSvrConf{
  79. LocalIP: "127.0.0.9",
  80. LocalPort: 29,
  81. },
  82. HealthCheckConf: HealthCheckConf{
  83. HealthCheckType: consts.TCPProxy,
  84. HealthCheckTimeoutS: 3,
  85. HealthCheckMaxFailed: 3,
  86. HealthCheckIntervalS: 19,
  87. HealthCheckAddr: "127.0.0.9:29",
  88. },
  89. },
  90. RemotePort: 6009,
  91. },
  92. },
  93. {
  94. sname: "ssh_random",
  95. source: []byte(`
  96. [ssh_random]
  97. type = tcp
  98. local_ip = 127.0.0.9
  99. local_port = 29
  100. remote_port = 9
  101. `),
  102. expected: &TCPProxyConf{
  103. BaseProxyConf: BaseProxyConf{
  104. ProxyName: testProxyPrefix + "ssh_random",
  105. ProxyType: consts.TCPProxy,
  106. LocalSvrConf: LocalSvrConf{
  107. LocalIP: "127.0.0.9",
  108. LocalPort: 29,
  109. },
  110. BandwidthLimitMode: BandwidthLimitModeClient,
  111. },
  112. RemotePort: 9,
  113. },
  114. },
  115. {
  116. sname: "dns",
  117. source: []byte(`
  118. [dns]
  119. type = udp
  120. local_ip = 114.114.114.114
  121. local_port = 59
  122. remote_port = 6009
  123. use_encryption
  124. use_compression
  125. `),
  126. expected: &UDPProxyConf{
  127. BaseProxyConf: BaseProxyConf{
  128. ProxyName: testProxyPrefix + "dns",
  129. ProxyType: consts.UDPProxy,
  130. UseEncryption: true,
  131. UseCompression: true,
  132. LocalSvrConf: LocalSvrConf{
  133. LocalIP: "114.114.114.114",
  134. LocalPort: 59,
  135. },
  136. BandwidthLimitMode: BandwidthLimitModeClient,
  137. },
  138. RemotePort: 6009,
  139. },
  140. },
  141. {
  142. sname: "web01",
  143. source: []byte(`
  144. [web01]
  145. type = http
  146. local_ip = 127.0.0.9
  147. local_port = 89
  148. use_encryption
  149. use_compression
  150. http_user = admin
  151. http_pwd = admin
  152. subdomain = web01
  153. custom_domains = web02.yourdomain.com
  154. locations = /,/pic
  155. host_header_rewrite = example.com
  156. header_X-From-Where = frp
  157. health_check_type = http
  158. health_check_url = /status
  159. health_check_interval_s = 19
  160. health_check_max_failed = 3
  161. health_check_timeout_s = 3
  162. `),
  163. expected: &HTTPProxyConf{
  164. BaseProxyConf: BaseProxyConf{
  165. ProxyName: testProxyPrefix + "web01",
  166. ProxyType: consts.HTTPProxy,
  167. UseCompression: true,
  168. UseEncryption: true,
  169. LocalSvrConf: LocalSvrConf{
  170. LocalIP: "127.0.0.9",
  171. LocalPort: 89,
  172. },
  173. HealthCheckConf: HealthCheckConf{
  174. HealthCheckType: consts.HTTPProxy,
  175. HealthCheckTimeoutS: 3,
  176. HealthCheckMaxFailed: 3,
  177. HealthCheckIntervalS: 19,
  178. HealthCheckURL: "http://127.0.0.9:89/status",
  179. },
  180. BandwidthLimitMode: BandwidthLimitModeClient,
  181. },
  182. DomainConf: DomainConf{
  183. CustomDomains: []string{"web02.yourdomain.com"},
  184. SubDomain: "web01",
  185. },
  186. Locations: []string{"/", "/pic"},
  187. HTTPUser: "admin",
  188. HTTPPwd: "admin",
  189. HostHeaderRewrite: "example.com",
  190. Headers: map[string]string{
  191. "X-From-Where": "frp",
  192. },
  193. },
  194. },
  195. {
  196. sname: "web02",
  197. source: []byte(`
  198. [web02]
  199. type = https
  200. local_ip = 127.0.0.9
  201. local_port = 8009
  202. use_encryption
  203. use_compression
  204. subdomain = web01
  205. custom_domains = web02.yourdomain.com
  206. proxy_protocol_version = v2
  207. `),
  208. expected: &HTTPSProxyConf{
  209. BaseProxyConf: BaseProxyConf{
  210. ProxyName: testProxyPrefix + "web02",
  211. ProxyType: consts.HTTPSProxy,
  212. UseCompression: true,
  213. UseEncryption: true,
  214. LocalSvrConf: LocalSvrConf{
  215. LocalIP: "127.0.0.9",
  216. LocalPort: 8009,
  217. },
  218. ProxyProtocolVersion: "v2",
  219. BandwidthLimitMode: BandwidthLimitModeClient,
  220. },
  221. DomainConf: DomainConf{
  222. CustomDomains: []string{"web02.yourdomain.com"},
  223. SubDomain: "web01",
  224. },
  225. },
  226. },
  227. {
  228. sname: "secret_tcp",
  229. source: []byte(`
  230. [secret_tcp]
  231. type = stcp
  232. sk = abcdefg
  233. local_ip = 127.0.0.1
  234. local_port = 22
  235. use_encryption = false
  236. use_compression = false
  237. `),
  238. expected: &STCPProxyConf{
  239. BaseProxyConf: BaseProxyConf{
  240. ProxyName: testProxyPrefix + "secret_tcp",
  241. ProxyType: consts.STCPProxy,
  242. LocalSvrConf: LocalSvrConf{
  243. LocalIP: "127.0.0.1",
  244. LocalPort: 22,
  245. },
  246. BandwidthLimitMode: BandwidthLimitModeClient,
  247. },
  248. RoleServerCommonConf: RoleServerCommonConf{
  249. Role: "server",
  250. Sk: "abcdefg",
  251. },
  252. },
  253. },
  254. {
  255. sname: "p2p_tcp",
  256. source: []byte(`
  257. [p2p_tcp]
  258. type = xtcp
  259. sk = abcdefg
  260. local_ip = 127.0.0.1
  261. local_port = 22
  262. use_encryption = false
  263. use_compression = false
  264. `),
  265. expected: &XTCPProxyConf{
  266. BaseProxyConf: BaseProxyConf{
  267. ProxyName: testProxyPrefix + "p2p_tcp",
  268. ProxyType: consts.XTCPProxy,
  269. LocalSvrConf: LocalSvrConf{
  270. LocalIP: "127.0.0.1",
  271. LocalPort: 22,
  272. },
  273. BandwidthLimitMode: BandwidthLimitModeClient,
  274. },
  275. RoleServerCommonConf: RoleServerCommonConf{
  276. Role: "server",
  277. Sk: "abcdefg",
  278. },
  279. },
  280. },
  281. {
  282. sname: "tcpmuxhttpconnect",
  283. source: []byte(`
  284. [tcpmuxhttpconnect]
  285. type = tcpmux
  286. multiplexer = httpconnect
  287. local_ip = 127.0.0.1
  288. local_port = 10701
  289. custom_domains = tunnel1
  290. `),
  291. expected: &TCPMuxProxyConf{
  292. BaseProxyConf: BaseProxyConf{
  293. ProxyName: testProxyPrefix + "tcpmuxhttpconnect",
  294. ProxyType: consts.TCPMuxProxy,
  295. LocalSvrConf: LocalSvrConf{
  296. LocalIP: "127.0.0.1",
  297. LocalPort: 10701,
  298. },
  299. BandwidthLimitMode: BandwidthLimitModeClient,
  300. },
  301. DomainConf: DomainConf{
  302. CustomDomains: []string{"tunnel1"},
  303. SubDomain: "",
  304. },
  305. Multiplexer: "httpconnect",
  306. },
  307. },
  308. }
  309. for _, c := range testcases {
  310. f, err := ini.LoadSources(testLoadOptions, c.source)
  311. assert.NoError(err)
  312. proxyType := f.Section(c.sname).Key("type").String()
  313. assert.NotEmpty(proxyType)
  314. actual := DefaultProxyConf(proxyType)
  315. assert.NotNil(actual)
  316. err = actual.UnmarshalFromIni(testProxyPrefix, c.sname, f.Section(c.sname))
  317. assert.NoError(err)
  318. assert.Equal(c.expected, actual)
  319. }
  320. }
  321. func Test_RangeProxy_UnmarshalFromIni(t *testing.T) {
  322. assert := assert.New(t)
  323. testcases := []struct {
  324. sname string
  325. source []byte
  326. expected map[string]ProxyConf
  327. }{
  328. {
  329. sname: "range:tcp_port",
  330. source: []byte(`
  331. [range:tcp_port]
  332. type = tcp
  333. local_ip = 127.0.0.9
  334. local_port = 6010-6011,6019
  335. remote_port = 6010-6011,6019
  336. use_encryption = false
  337. use_compression = false
  338. `),
  339. expected: map[string]ProxyConf{
  340. "tcp_port_0": &TCPProxyConf{
  341. BaseProxyConf: BaseProxyConf{
  342. ProxyName: testProxyPrefix + "tcp_port_0",
  343. ProxyType: consts.TCPProxy,
  344. LocalSvrConf: LocalSvrConf{
  345. LocalIP: "127.0.0.9",
  346. LocalPort: 6010,
  347. },
  348. BandwidthLimitMode: BandwidthLimitModeClient,
  349. },
  350. RemotePort: 6010,
  351. },
  352. "tcp_port_1": &TCPProxyConf{
  353. BaseProxyConf: BaseProxyConf{
  354. ProxyName: testProxyPrefix + "tcp_port_1",
  355. ProxyType: consts.TCPProxy,
  356. LocalSvrConf: LocalSvrConf{
  357. LocalIP: "127.0.0.9",
  358. LocalPort: 6011,
  359. },
  360. BandwidthLimitMode: BandwidthLimitModeClient,
  361. },
  362. RemotePort: 6011,
  363. },
  364. "tcp_port_2": &TCPProxyConf{
  365. BaseProxyConf: BaseProxyConf{
  366. ProxyName: testProxyPrefix + "tcp_port_2",
  367. ProxyType: consts.TCPProxy,
  368. LocalSvrConf: LocalSvrConf{
  369. LocalIP: "127.0.0.9",
  370. LocalPort: 6019,
  371. },
  372. BandwidthLimitMode: BandwidthLimitModeClient,
  373. },
  374. RemotePort: 6019,
  375. },
  376. },
  377. },
  378. {
  379. sname: "range:udp_port",
  380. source: []byte(`
  381. [range:udp_port]
  382. type = udp
  383. local_ip = 114.114.114.114
  384. local_port = 6000,6010-6011
  385. remote_port = 6000,6010-6011
  386. use_encryption
  387. use_compression
  388. `),
  389. expected: map[string]ProxyConf{
  390. "udp_port_0": &UDPProxyConf{
  391. BaseProxyConf: BaseProxyConf{
  392. ProxyName: testProxyPrefix + "udp_port_0",
  393. ProxyType: consts.UDPProxy,
  394. UseEncryption: true,
  395. UseCompression: true,
  396. LocalSvrConf: LocalSvrConf{
  397. LocalIP: "114.114.114.114",
  398. LocalPort: 6000,
  399. },
  400. BandwidthLimitMode: BandwidthLimitModeClient,
  401. },
  402. RemotePort: 6000,
  403. },
  404. "udp_port_1": &UDPProxyConf{
  405. BaseProxyConf: BaseProxyConf{
  406. ProxyName: testProxyPrefix + "udp_port_1",
  407. ProxyType: consts.UDPProxy,
  408. UseEncryption: true,
  409. UseCompression: true,
  410. LocalSvrConf: LocalSvrConf{
  411. LocalIP: "114.114.114.114",
  412. LocalPort: 6010,
  413. },
  414. BandwidthLimitMode: BandwidthLimitModeClient,
  415. },
  416. RemotePort: 6010,
  417. },
  418. "udp_port_2": &UDPProxyConf{
  419. BaseProxyConf: BaseProxyConf{
  420. ProxyName: testProxyPrefix + "udp_port_2",
  421. ProxyType: consts.UDPProxy,
  422. UseEncryption: true,
  423. UseCompression: true,
  424. LocalSvrConf: LocalSvrConf{
  425. LocalIP: "114.114.114.114",
  426. LocalPort: 6011,
  427. },
  428. BandwidthLimitMode: BandwidthLimitModeClient,
  429. },
  430. RemotePort: 6011,
  431. },
  432. },
  433. },
  434. }
  435. for _, c := range testcases {
  436. f, err := ini.LoadSources(testLoadOptions, c.source)
  437. assert.NoError(err)
  438. actual := make(map[string]ProxyConf)
  439. s := f.Section(c.sname)
  440. err = renderRangeProxyTemplates(f, s)
  441. assert.NoError(err)
  442. f.DeleteSection(ini.DefaultSection)
  443. f.DeleteSection(c.sname)
  444. for _, section := range f.Sections() {
  445. proxyType := section.Key("type").String()
  446. newsname := section.Name()
  447. tmp := DefaultProxyConf(proxyType)
  448. err = tmp.UnmarshalFromIni(testProxyPrefix, newsname, section)
  449. assert.NoError(err)
  450. actual[newsname] = tmp
  451. }
  452. assert.Equal(c.expected, actual)
  453. }
  454. }