proxy_test.go 11 KB

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