1
0

proxy_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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(t *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. Role: "server",
  249. Sk: "abcdefg",
  250. },
  251. },
  252. {
  253. sname: "p2p_tcp",
  254. source: []byte(`
  255. [p2p_tcp]
  256. type = xtcp
  257. sk = abcdefg
  258. local_ip = 127.0.0.1
  259. local_port = 22
  260. use_encryption = false
  261. use_compression = false
  262. `),
  263. expected: &XTCPProxyConf{
  264. BaseProxyConf: BaseProxyConf{
  265. ProxyName: testProxyPrefix + "p2p_tcp",
  266. ProxyType: consts.XTCPProxy,
  267. LocalSvrConf: LocalSvrConf{
  268. LocalIP: "127.0.0.1",
  269. LocalPort: 22,
  270. },
  271. BandwidthLimitMode: BandwidthLimitModeClient,
  272. },
  273. Role: "server",
  274. Sk: "abcdefg",
  275. },
  276. },
  277. {
  278. sname: "tcpmuxhttpconnect",
  279. source: []byte(`
  280. [tcpmuxhttpconnect]
  281. type = tcpmux
  282. multiplexer = httpconnect
  283. local_ip = 127.0.0.1
  284. local_port = 10701
  285. custom_domains = tunnel1
  286. `),
  287. expected: &TCPMuxProxyConf{
  288. BaseProxyConf: BaseProxyConf{
  289. ProxyName: testProxyPrefix + "tcpmuxhttpconnect",
  290. ProxyType: consts.TCPMuxProxy,
  291. LocalSvrConf: LocalSvrConf{
  292. LocalIP: "127.0.0.1",
  293. LocalPort: 10701,
  294. },
  295. BandwidthLimitMode: BandwidthLimitModeClient,
  296. },
  297. DomainConf: DomainConf{
  298. CustomDomains: []string{"tunnel1"},
  299. SubDomain: "",
  300. },
  301. Multiplexer: "httpconnect",
  302. },
  303. },
  304. }
  305. for _, c := range testcases {
  306. f, err := ini.LoadSources(testLoadOptions, c.source)
  307. assert.NoError(err)
  308. proxyType := f.Section(c.sname).Key("type").String()
  309. assert.NotEmpty(proxyType)
  310. actual := DefaultProxyConf(proxyType)
  311. assert.NotNil(actual)
  312. err = actual.UnmarshalFromIni(testProxyPrefix, c.sname, f.Section(c.sname))
  313. assert.NoError(err)
  314. assert.Equal(c.expected, actual)
  315. }
  316. }
  317. func Test_RangeProxy_UnmarshalFromIni(t *testing.T) {
  318. assert := assert.New(t)
  319. testcases := []struct {
  320. sname string
  321. source []byte
  322. expected map[string]ProxyConf
  323. }{
  324. {
  325. sname: "range:tcp_port",
  326. source: []byte(`
  327. [range:tcp_port]
  328. type = tcp
  329. local_ip = 127.0.0.9
  330. local_port = 6010-6011,6019
  331. remote_port = 6010-6011,6019
  332. use_encryption = false
  333. use_compression = false
  334. `),
  335. expected: map[string]ProxyConf{
  336. "tcp_port_0": &TCPProxyConf{
  337. BaseProxyConf: BaseProxyConf{
  338. ProxyName: testProxyPrefix + "tcp_port_0",
  339. ProxyType: consts.TCPProxy,
  340. LocalSvrConf: LocalSvrConf{
  341. LocalIP: "127.0.0.9",
  342. LocalPort: 6010,
  343. },
  344. BandwidthLimitMode: BandwidthLimitModeClient,
  345. },
  346. RemotePort: 6010,
  347. },
  348. "tcp_port_1": &TCPProxyConf{
  349. BaseProxyConf: BaseProxyConf{
  350. ProxyName: testProxyPrefix + "tcp_port_1",
  351. ProxyType: consts.TCPProxy,
  352. LocalSvrConf: LocalSvrConf{
  353. LocalIP: "127.0.0.9",
  354. LocalPort: 6011,
  355. },
  356. BandwidthLimitMode: BandwidthLimitModeClient,
  357. },
  358. RemotePort: 6011,
  359. },
  360. "tcp_port_2": &TCPProxyConf{
  361. BaseProxyConf: BaseProxyConf{
  362. ProxyName: testProxyPrefix + "tcp_port_2",
  363. ProxyType: consts.TCPProxy,
  364. LocalSvrConf: LocalSvrConf{
  365. LocalIP: "127.0.0.9",
  366. LocalPort: 6019,
  367. },
  368. BandwidthLimitMode: BandwidthLimitModeClient,
  369. },
  370. RemotePort: 6019,
  371. },
  372. },
  373. },
  374. {
  375. sname: "range:udp_port",
  376. source: []byte(`
  377. [range:udp_port]
  378. type = udp
  379. local_ip = 114.114.114.114
  380. local_port = 6000,6010-6011
  381. remote_port = 6000,6010-6011
  382. use_encryption
  383. use_compression
  384. `),
  385. expected: map[string]ProxyConf{
  386. "udp_port_0": &UDPProxyConf{
  387. BaseProxyConf: BaseProxyConf{
  388. ProxyName: testProxyPrefix + "udp_port_0",
  389. ProxyType: consts.UDPProxy,
  390. UseEncryption: true,
  391. UseCompression: true,
  392. LocalSvrConf: LocalSvrConf{
  393. LocalIP: "114.114.114.114",
  394. LocalPort: 6000,
  395. },
  396. BandwidthLimitMode: BandwidthLimitModeClient,
  397. },
  398. RemotePort: 6000,
  399. },
  400. "udp_port_1": &UDPProxyConf{
  401. BaseProxyConf: BaseProxyConf{
  402. ProxyName: testProxyPrefix + "udp_port_1",
  403. ProxyType: consts.UDPProxy,
  404. UseEncryption: true,
  405. UseCompression: true,
  406. LocalSvrConf: LocalSvrConf{
  407. LocalIP: "114.114.114.114",
  408. LocalPort: 6010,
  409. },
  410. BandwidthLimitMode: BandwidthLimitModeClient,
  411. },
  412. RemotePort: 6010,
  413. },
  414. "udp_port_2": &UDPProxyConf{
  415. BaseProxyConf: BaseProxyConf{
  416. ProxyName: testProxyPrefix + "udp_port_2",
  417. ProxyType: consts.UDPProxy,
  418. UseEncryption: true,
  419. UseCompression: true,
  420. LocalSvrConf: LocalSvrConf{
  421. LocalIP: "114.114.114.114",
  422. LocalPort: 6011,
  423. },
  424. BandwidthLimitMode: BandwidthLimitModeClient,
  425. },
  426. RemotePort: 6011,
  427. },
  428. },
  429. },
  430. }
  431. for _, c := range testcases {
  432. f, err := ini.LoadSources(testLoadOptions, c.source)
  433. assert.NoError(err)
  434. actual := make(map[string]ProxyConf)
  435. s := f.Section(c.sname)
  436. err = renderRangeProxyTemplates(f, s)
  437. assert.NoError(err)
  438. f.DeleteSection(ini.DefaultSection)
  439. f.DeleteSection(c.sname)
  440. for _, section := range f.Sections() {
  441. proxyType := section.Key("type").String()
  442. newsname := section.Name()
  443. tmp := DefaultProxyConf(proxyType)
  444. err = tmp.UnmarshalFromIni(testProxyPrefix, newsname, section)
  445. assert.NoError(err)
  446. actual[newsname] = tmp
  447. }
  448. assert.Equal(c.expected, actual)
  449. }
  450. }