proxy_parser_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. package upstream
  2. import (
  3. "testing"
  4. )
  5. func TestParseProxyTargetsFromRawContent(t *testing.T) {
  6. config := `map $http_upgrade $connection_upgrade {
  7. default upgrade;
  8. '' close;
  9. }
  10. upstream api-1 {
  11. server 127.0.0.1:9000;
  12. server 127.0.0.1:443;
  13. }
  14. upstream api-2 {
  15. server 127.0.0.1:9003;
  16. server 127.0.0.1:9005;
  17. }
  18. server {
  19. listen 80;
  20. listen [::]:80;
  21. server_name test.jackyu.cn;
  22. location / {
  23. # First attempt to serve request as file, then
  24. # as directory, then fall back to displaying a 404.
  25. index index.html;
  26. try_files $uri $uri/ /index.html;
  27. }
  28. location /admin {
  29. index admin.html;
  30. try_files $uri $uri/ /admin.html;
  31. }
  32. location /user {
  33. index user.html;
  34. try_files $uri $uri/ /user.html;
  35. }
  36. location /api/ {
  37. proxy_http_version 1.1;
  38. proxy_set_header Upgrade $http_upgrade;
  39. proxy_set_header Connection $connection_upgrade;
  40. proxy_pass http://api-1/;
  41. proxy_redirect off;
  42. proxy_set_header Host $host;
  43. proxy_set_header X-Real-IP $remote_addr;
  44. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  45. proxy_set_header X-Forwarded-Proto $scheme;
  46. client_max_body_size 1000m;
  47. }
  48. }
  49. server {
  50. listen 443 ssl;
  51. listen [::]:443 ssl;
  52. server_name test.jackyu.cn;
  53. ssl_certificate /etc/nginx/ssl/test.jackyu.cn_P256/fullchain.cer;
  54. ssl_certificate_key /etc/nginx/ssl/test.jackyu.cn_P256/private.key;
  55. root /var/www/ibeta/html;
  56. index index.html;
  57. http2 on;
  58. access_log /var/log/nginx/test.jackyu.cn.log main;
  59. location / {
  60. # First attempt to serve request as file, then
  61. # as directory, then fall back to displaying a 404.
  62. index index.html;
  63. try_files $uri $uri/ /index.html;
  64. }
  65. location /admin {
  66. index admin.html;
  67. try_files $uri $uri/ /admin.html;
  68. }
  69. location /user {
  70. index user.html;
  71. try_files $uri $uri/ /user.html;
  72. }
  73. location /api/ {
  74. proxy_http_version 1.1;
  75. proxy_set_header Upgrade $http_upgrade;
  76. proxy_set_header Connection $connection_upgrade;
  77. proxy_pass http://api-1/;
  78. proxy_redirect off;
  79. proxy_set_header Host $host;
  80. proxy_set_header X-Real-IP $remote_addr;
  81. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  82. proxy_set_header X-Forwarded-Proto $scheme;
  83. client_max_body_size 100m;
  84. }
  85. }`
  86. targets := ParseProxyTargetsFromRawContent(config)
  87. // Expected targets: 4 upstream servers (2 from api-1, 2 from api-2)
  88. // proxy_pass http://api-1/ should be ignored since it references an upstream
  89. expectedTargets := []ProxyTarget{
  90. {Host: "127.0.0.1", Port: "9000", Type: "upstream"},
  91. {Host: "127.0.0.1", Port: "443", Type: "upstream"},
  92. {Host: "127.0.0.1", Port: "9003", Type: "upstream"},
  93. {Host: "127.0.0.1", Port: "9005", Type: "upstream"},
  94. }
  95. if len(targets) != len(expectedTargets) {
  96. t.Errorf("Expected %d targets, got %d", len(expectedTargets), len(targets))
  97. for i, target := range targets {
  98. t.Logf("Target %d: %+v", i, target)
  99. }
  100. return
  101. }
  102. // Create a map for easier comparison
  103. targetMap := make(map[string]ProxyTarget)
  104. for _, target := range targets {
  105. key := target.Host + ":" + target.Port + ":" + target.Type
  106. targetMap[key] = target
  107. }
  108. for _, expected := range expectedTargets {
  109. key := expected.Host + ":" + expected.Port + ":" + expected.Type
  110. if _, found := targetMap[key]; !found {
  111. t.Errorf("Expected target not found: %+v", expected)
  112. }
  113. }
  114. }
  115. func TestIsUpstreamReference(t *testing.T) {
  116. upstreamNames := map[string]bool{
  117. "api-1": true,
  118. "api-2": true,
  119. "backend": true,
  120. "myUpStr": true,
  121. }
  122. tests := []struct {
  123. proxyPass string
  124. expected bool
  125. }{
  126. {"http://api-1/", true},
  127. {"http://api-1", true},
  128. {"https://api-2/path", true},
  129. {"http://backend", true},
  130. {"http://127.0.0.1:8080", false},
  131. {"https://example.com", false},
  132. {"http://unknown-upstream", false},
  133. // Test cases for nginx variables
  134. {"https://myUpStr$request_uri", true},
  135. {"http://api-1$request_uri", true},
  136. {"https://backend$server_name", true},
  137. {"http://unknown-upstream$request_uri", false},
  138. {"https://example.com$request_uri", false},
  139. // Test cases for URLs with variables and paths
  140. {"https://myUpStr/api$request_uri", true},
  141. {"http://api-1:8080$request_uri", true},
  142. }
  143. for _, test := range tests {
  144. result := isUpstreamReference(test.proxyPass, upstreamNames)
  145. if result != test.expected {
  146. t.Errorf("isUpstreamReference(%q) = %v, expected %v", test.proxyPass, result, test.expected)
  147. }
  148. }
  149. }
  150. func TestParseProxyTargetsWithDirectProxyPass(t *testing.T) {
  151. config := `upstream api-1 {
  152. server 127.0.0.1:9000;
  153. server 127.0.0.1:443;
  154. }
  155. server {
  156. listen 80;
  157. server_name test.jackyu.cn;
  158. location /api/ {
  159. proxy_pass http://api-1/;
  160. }
  161. location /external/ {
  162. proxy_pass http://external.example.com:8080/;
  163. }
  164. location /another/ {
  165. proxy_pass https://another.example.com/;
  166. }
  167. }`
  168. targets := ParseProxyTargetsFromRawContent(config)
  169. // Expected targets:
  170. // - 2 upstream servers from api-1
  171. // - 2 direct proxy_pass targets (external.example.com:8080, another.example.com:443)
  172. // - proxy_pass http://api-1/ should be ignored since it references an upstream
  173. expectedTargets := []ProxyTarget{
  174. {Host: "127.0.0.1", Port: "9000", Type: "upstream"},
  175. {Host: "127.0.0.1", Port: "443", Type: "upstream"},
  176. {Host: "external.example.com", Port: "8080", Type: "proxy_pass"},
  177. {Host: "another.example.com", Port: "443", Type: "proxy_pass"},
  178. }
  179. if len(targets) != len(expectedTargets) {
  180. t.Errorf("Expected %d targets, got %d", len(expectedTargets), len(targets))
  181. for i, target := range targets {
  182. t.Logf("Target %d: %+v", i, target)
  183. }
  184. return
  185. }
  186. // Create a map for easier comparison
  187. targetMap := make(map[string]ProxyTarget)
  188. for _, target := range targets {
  189. key := target.Host + ":" + target.Port + ":" + target.Type
  190. targetMap[key] = target
  191. }
  192. for _, expected := range expectedTargets {
  193. key := expected.Host + ":" + expected.Port + ":" + expected.Type
  194. if _, found := targetMap[key]; !found {
  195. t.Errorf("Expected target not found: %+v", expected)
  196. }
  197. }
  198. }
  199. func TestParseProxyTargetsFromStreamConfig(t *testing.T) {
  200. config := `upstream backend {
  201. server 127.0.0.1:9000;
  202. server 127.0.0.1:9001;
  203. }
  204. server {
  205. listen 12345;
  206. proxy_pass backend;
  207. }
  208. server {
  209. listen 12346;
  210. proxy_pass 192.168.1.100:8080;
  211. }
  212. server {
  213. listen 12347;
  214. proxy_pass example.com:3306;
  215. }`
  216. targets := ParseProxyTargetsFromRawContent(config)
  217. // Expected targets:
  218. // - 2 upstream servers from backend
  219. // - 2 direct proxy_pass targets (192.168.1.100:8080, example.com:3306)
  220. // - proxy_pass backend should be ignored since it references an upstream
  221. expectedTargets := []ProxyTarget{
  222. {Host: "127.0.0.1", Port: "9000", Type: "upstream"},
  223. {Host: "127.0.0.1", Port: "9001", Type: "upstream"},
  224. {Host: "192.168.1.100", Port: "8080", Type: "proxy_pass"},
  225. {Host: "example.com", Port: "3306", Type: "proxy_pass"},
  226. }
  227. if len(targets) != len(expectedTargets) {
  228. t.Errorf("Expected %d targets, got %d", len(expectedTargets), len(targets))
  229. for i, target := range targets {
  230. t.Logf("Target %d: %+v", i, target)
  231. }
  232. return
  233. }
  234. // Create a map for easier comparison
  235. targetMap := make(map[string]ProxyTarget)
  236. for _, target := range targets {
  237. key := target.Host + ":" + target.Port + ":" + target.Type
  238. targetMap[key] = target
  239. }
  240. for _, expected := range expectedTargets {
  241. key := expected.Host + ":" + expected.Port + ":" + expected.Type
  242. if _, found := targetMap[key]; !found {
  243. t.Errorf("Expected target not found: %+v", expected)
  244. }
  245. }
  246. }
  247. func TestParseProxyTargetsFromMixedConfig(t *testing.T) {
  248. config := `upstream web_backend {
  249. server web1.example.com:80;
  250. server web2.example.com:80;
  251. }
  252. upstream stream_backend {
  253. server stream1.example.com:12345;
  254. server stream2.example.com:12345;
  255. }
  256. # HTTP server block
  257. server {
  258. listen 80;
  259. server_name example.com;
  260. location / {
  261. proxy_pass http://web_backend/;
  262. }
  263. location /api {
  264. proxy_pass http://api.example.com:8080/;
  265. }
  266. }
  267. # Stream server blocks
  268. server {
  269. listen 12345;
  270. proxy_pass stream_backend;
  271. }
  272. server {
  273. listen 3306;
  274. proxy_pass mysql.example.com:3306;
  275. }`
  276. targets := ParseProxyTargetsFromRawContent(config)
  277. // Expected targets:
  278. // - 2 upstream servers from web_backend
  279. // - 2 upstream servers from stream_backend
  280. // - 1 direct HTTP proxy_pass (api.example.com:8080)
  281. // - 1 direct stream proxy_pass (mysql.example.com:3306)
  282. // - proxy_pass http://web_backend/ and proxy_pass stream_backend should be ignored
  283. expectedTargets := []ProxyTarget{
  284. {Host: "web1.example.com", Port: "80", Type: "upstream"},
  285. {Host: "web2.example.com", Port: "80", Type: "upstream"},
  286. {Host: "stream1.example.com", Port: "12345", Type: "upstream"},
  287. {Host: "stream2.example.com", Port: "12345", Type: "upstream"},
  288. {Host: "api.example.com", Port: "8080", Type: "proxy_pass"},
  289. {Host: "mysql.example.com", Port: "3306", Type: "proxy_pass"},
  290. }
  291. if len(targets) != len(expectedTargets) {
  292. t.Errorf("Expected %d targets, got %d", len(expectedTargets), len(targets))
  293. for i, target := range targets {
  294. t.Logf("Target %d: %+v", i, target)
  295. }
  296. return
  297. }
  298. // Create a map for easier comparison
  299. targetMap := make(map[string]ProxyTarget)
  300. for _, target := range targets {
  301. key := target.Host + ":" + target.Port + ":" + target.Type
  302. targetMap[key] = target
  303. }
  304. for _, expected := range expectedTargets {
  305. key := expected.Host + ":" + expected.Port + ":" + expected.Type
  306. if _, found := targetMap[key]; !found {
  307. t.Errorf("Expected target not found: %+v", expected)
  308. }
  309. }
  310. }
  311. func TestParseProxyTargetsFromUserConfig(t *testing.T) {
  312. config := `upstream my-tcp {
  313. server 127.0.0.1:9000;
  314. }
  315. server {
  316. listen 1234-1236;
  317. resolver 8.8.8.8 valid=1s;
  318. proxy_pass example.com:$server_port;
  319. }`
  320. targets := ParseProxyTargetsFromRawContent(config)
  321. // Print actual results for debugging
  322. t.Logf("Found %d targets:", len(targets))
  323. for i, target := range targets {
  324. t.Logf("Target %d: Host=%s, Port=%s, Type=%s", i+1, target.Host, target.Port, target.Type)
  325. }
  326. // Expected targets:
  327. // - 1 upstream server from my-tcp
  328. // - 1 proxy_pass target (example.com with variable port should still be parsed)
  329. expectedTargets := []ProxyTarget{
  330. {Host: "127.0.0.1", Port: "9000", Type: "upstream"},
  331. {Host: "example.com", Port: "$server_port", Type: "proxy_pass"},
  332. }
  333. if len(targets) != len(expectedTargets) {
  334. t.Errorf("Expected %d targets, got %d", len(expectedTargets), len(targets))
  335. return
  336. }
  337. // Create a map for easier comparison
  338. targetMap := make(map[string]ProxyTarget)
  339. for _, target := range targets {
  340. key := target.Host + ":" + target.Port + ":" + target.Type
  341. targetMap[key] = target
  342. }
  343. for _, expected := range expectedTargets {
  344. key := expected.Host + ":" + expected.Port + ":" + expected.Type
  345. if _, found := targetMap[key]; !found {
  346. t.Errorf("Expected target not found: %+v", expected)
  347. }
  348. }
  349. }
  350. func TestParseProxyTargetsWithNginxVariables(t *testing.T) {
  351. config := `map $http_upgrade $connection_upgrade {
  352. default upgrade;
  353. '' close;
  354. }
  355. upstream myUpStr {
  356. keepalive 32;
  357. keepalive_timeout 600s;
  358. server 192.168.1.100:8080;
  359. }
  360. server {
  361. listen 80;
  362. listen [::]:80;
  363. server_name my.domain.tld;
  364. return 307 https://$server_name$request_uri;
  365. }
  366. server {
  367. listen 443 ssl http2;
  368. listen [::]:443 ssl http2;
  369. server_name my.domain.tld;
  370. ssl_certificate /path/to/cert;
  371. ssl_certificate_key /path/to/key;
  372. location / {
  373. proxy_http_version 1.1;
  374. proxy_set_header Upgrade $http_upgrade;
  375. proxy_set_header Connection $connection_upgrade;
  376. client_max_body_size 1000m;
  377. proxy_redirect off;
  378. add_header X-Served-By $host;
  379. proxy_set_header Host $host;
  380. proxy_set_header X-Forwarded-Scheme $scheme;
  381. proxy_set_header X-Forwarded-Proto $scheme;
  382. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  383. proxy_set_header X-Real-IP $remote_addr;
  384. proxy_set_header X-Forwarded-Host $host:$server_port;
  385. proxy_set_header X-Forwarded-Server $host;
  386. proxy_pass https://myUpStr$request_uri;
  387. }
  388. }`
  389. targets := ParseProxyTargetsFromRawContent(config)
  390. // Expected targets:
  391. // - 1 upstream server from myUpStr
  392. // - proxy_pass https://myUpStr$request_uri should be ignored since it references an upstream
  393. expectedTargets := []ProxyTarget{
  394. {Host: "192.168.1.100", Port: "8080", Type: "upstream"},
  395. }
  396. if len(targets) != len(expectedTargets) {
  397. t.Errorf("Expected %d targets, got %d", len(expectedTargets), len(targets))
  398. for i, target := range targets {
  399. t.Logf("Target %d: %+v", i, target)
  400. }
  401. return
  402. }
  403. // Create a map for easier comparison
  404. targetMap := make(map[string]ProxyTarget)
  405. for _, target := range targets {
  406. key := target.Host + ":" + target.Port + ":" + target.Type
  407. targetMap[key] = target
  408. }
  409. for _, expected := range expectedTargets {
  410. key := expected.Host + ":" + expected.Port + ":" + expected.Type
  411. if _, found := targetMap[key]; !found {
  412. t.Errorf("Expected target not found: %+v", expected)
  413. }
  414. }
  415. }
  416. func TestParseProxyTargetsWithComplexNginxVariables(t *testing.T) {
  417. config := `upstream backend_api {
  418. server api1.example.com:8080;
  419. server api2.example.com:8080;
  420. }
  421. upstream backend_ws {
  422. server ws1.example.com:9000;
  423. server ws2.example.com:9000;
  424. }
  425. server {
  426. listen 80;
  427. server_name example.com;
  428. location /api/ {
  429. proxy_pass http://backend_api$request_uri;
  430. }
  431. location /ws/ {
  432. proxy_pass http://backend_ws/websocket$request_uri;
  433. }
  434. location /external/ {
  435. proxy_pass https://external.example.com:8443$request_uri;
  436. }
  437. location /static/ {
  438. proxy_pass http://static.example.com$uri;
  439. }
  440. }`
  441. targets := ParseProxyTargetsFromRawContent(config)
  442. // Expected targets:
  443. // - 2 upstream servers from backend_api
  444. // - 2 upstream servers from backend_ws
  445. // - 1 direct proxy_pass (external.example.com:8443)
  446. // - 1 direct proxy_pass (static.example.com:80)
  447. // - proxy_pass with upstream references should be ignored
  448. expectedTargets := []ProxyTarget{
  449. {Host: "api1.example.com", Port: "8080", Type: "upstream"},
  450. {Host: "api2.example.com", Port: "8080", Type: "upstream"},
  451. {Host: "ws1.example.com", Port: "9000", Type: "upstream"},
  452. {Host: "ws2.example.com", Port: "9000", Type: "upstream"},
  453. {Host: "external.example.com", Port: "8443", Type: "proxy_pass"},
  454. {Host: "static.example.com", Port: "80", Type: "proxy_pass"},
  455. }
  456. if len(targets) != len(expectedTargets) {
  457. t.Errorf("Expected %d targets, got %d", len(expectedTargets), len(targets))
  458. for i, target := range targets {
  459. t.Logf("Target %d: %+v", i, target)
  460. }
  461. return
  462. }
  463. // Create a map for easier comparison
  464. targetMap := make(map[string]ProxyTarget)
  465. for _, target := range targets {
  466. key := target.Host + ":" + target.Port + ":" + target.Type
  467. targetMap[key] = target
  468. }
  469. for _, expected := range expectedTargets {
  470. key := expected.Host + ":" + expected.Port + ":" + expected.Type
  471. if _, found := targetMap[key]; !found {
  472. t.Errorf("Expected target not found: %+v", expected)
  473. }
  474. }
  475. }