mirror of
https://github.com/qaiu/netdisk-fast-download.git
synced 2026-12-04 10:24:00 +00:00
Compare commits
750
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
922e762273 | ||
|
|
4c48e992da | ||
|
|
76aaa7d214 | ||
|
|
3047ba97a2 | ||
|
|
c894e38ffa | ||
|
|
72893efd83 | ||
|
|
5cec707d76 | ||
|
|
793733853b | ||
|
|
b0471904fa | ||
|
|
53e921aa80 | ||
|
|
f08dd36635 | ||
|
|
792d7210a7 | ||
|
|
fcf8f3d71f | ||
|
|
4361d96c52 | ||
|
|
971cdcd067 | ||
|
|
4b9339072a | ||
|
|
6b6394e61f | ||
|
|
ba1c429508 | ||
|
|
69ef9c5bbb | ||
|
|
e91c08548a | ||
|
|
e794b34982 | ||
|
|
0a631e75ea | ||
|
|
72194dddc9 | ||
|
|
0d5014ad40 | ||
|
|
1d7836c1bd | ||
|
|
6ed8ae2e28 | ||
|
|
b5a0155dd0 | ||
|
|
8f372e5dd0 | ||
|
|
e9bfe14488 | ||
|
|
cbd10962c0 | ||
|
|
2bb66cfc82 | ||
|
|
fbfb760b3a | ||
|
|
ef03956944 | ||
|
|
542d246aab | ||
|
|
feff1b88c1 | ||
|
|
a7d41b0107 | ||
|
|
24c22fad7f | ||
|
|
a86b26d668 | ||
|
|
5781100905 | ||
|
|
04ba9a0e2c | ||
|
|
3825527519 | ||
|
|
531f431faa | ||
|
|
44b023d919 | ||
|
|
5681d3b805 | ||
|
|
8c07c6e657 | ||
|
|
25888661ad | ||
|
|
b6922b152f | ||
|
|
572c5a61fc | ||
|
|
dec6cf496f | ||
|
|
98a043daf9 | ||
|
|
12d0f6bfbb | ||
|
|
9d2fd09331 | ||
|
|
d692416a2e | ||
|
|
e9be9b3345 | ||
|
|
6acb774c87 | ||
|
|
511515cae4 | ||
|
|
1ab9df7826 | ||
|
|
ab8b687837 | ||
|
|
7616bf3144 | ||
|
|
01d7956de5 | ||
|
|
78e53c076e | ||
|
|
4d985cc96f | ||
|
|
13ccdb8adf | ||
|
|
ac1afcd35f | ||
|
|
0c678a4124 | ||
|
|
f77800602d | ||
|
|
79ab829952 | ||
|
|
2181ef38df | ||
|
|
e09c9f76a7 | ||
|
|
7adb938357 | ||
|
|
efb800d88e | ||
|
|
422d5b845b | ||
|
|
054e9cc1ec | ||
|
|
f3da45bb16 | ||
|
|
273dbae5e7 | ||
|
|
e103003b7a | ||
|
|
e853365fe4 | ||
|
|
b5f7d9694b | ||
|
|
f75d9f687b | ||
|
|
703bfa3061 | ||
|
|
6c709fde65 | ||
|
|
449fd741ac | ||
|
|
c18b8d80e1 | ||
|
|
06ba0aff52 | ||
|
|
74e401d99c | ||
|
|
4995bca901 | ||
|
|
7299fd8762 | ||
|
|
efbadde4ed | ||
|
|
d1fa787bef | ||
|
|
c282fcb109 | ||
|
|
4995173de2 | ||
|
|
1c1068beba | ||
|
|
4a5240fcbc | ||
|
|
0f34672e33 | ||
|
|
ecd365767a | ||
|
|
f358140974 | ||
|
|
541c21f963 | ||
|
|
bdd253383d | ||
|
|
0fb53d5159 | ||
|
|
b9ff408bdd | ||
|
|
aeb4394cf8 | ||
|
|
8d419d3265 | ||
|
|
b70e30796f | ||
|
|
d3ef521773 | ||
|
|
2969c92d3d | ||
|
|
2fde421169 | ||
|
|
3494271150 | ||
|
|
675d930f86 | ||
|
|
5e20ed5050 | ||
|
|
91f4fcaa76 | ||
|
|
0754fe20f1 | ||
|
|
034fd20d3b | ||
|
|
363c603bbb | ||
|
|
24bf98bca9 | ||
|
|
275f6b9e6d | ||
|
|
5db9e422f4 | ||
|
|
705b6ba4b7 | ||
|
|
0edc057347 | ||
|
|
6ae5d998e6 | ||
|
|
cbb442ceb6 | ||
|
|
0103841fb5 | ||
|
|
b6b7f0d8b7 | ||
|
|
46c6827eda | ||
|
|
1ef6e120a8 | ||
|
|
65d518373d | ||
|
|
bca4da4b6c | ||
|
|
0fd78defcb | ||
|
|
452fd0ea2c | ||
|
|
dd8f2efb37 | ||
|
|
0feb8e798a | ||
|
|
d55d8edd2f | ||
|
|
451496f102 | ||
|
|
6d6351bd58 | ||
|
|
1a5fc8d1ef | ||
|
|
a44f30f7e5 | ||
|
|
f9ebd34de3 | ||
|
|
b47db300a6 | ||
|
|
d19d8573f9 | ||
|
|
799e120069 | ||
|
|
13f83e8795 | ||
|
|
7b5900aae4 | ||
|
|
e36c0bbe45 | ||
|
|
af723aed3a | ||
|
|
0978186679 | ||
|
|
17460ff271 | ||
|
|
f81b3852ee | ||
|
|
37abebf8f8 | ||
|
|
79fab8c0d6 | ||
|
|
9b70fb2778 | ||
|
|
bd2868748f | ||
|
|
1f47bf13b5 | ||
|
|
7d8b33afe0 | ||
|
|
367f7c78a4 | ||
|
|
1bd23ec4ae | ||
|
|
3461532679 | ||
|
|
4cfcdfa1f8 | ||
|
|
ac2f526a1c | ||
|
|
cfb624e9e0 | ||
|
|
e1bf45b5c8 | ||
|
|
e2dc611aa4 | ||
|
|
77c953626f | ||
|
|
0e14c9a925 | ||
|
|
a4e8585e2c | ||
|
|
cf7d64916e | ||
|
|
4ac80bbfe8 | ||
|
|
cfe8352d45 | ||
|
|
4a0fe61d30 | ||
|
|
c62e109aff | ||
|
|
ff400d3be3 | ||
|
|
1d243b8f1b | ||
|
|
732a7f86fe | ||
|
|
3c428f6a6d | ||
|
|
bff17f2d4e | ||
|
|
df600eaada | ||
|
|
dd4027c931 | ||
|
|
d1569195e4 | ||
|
|
d6e88f0c53 | ||
|
|
2a9fa81e56 | ||
|
|
5eed1fdfa0 | ||
|
|
cf7fc4f502 | ||
|
|
0ea31d631a | ||
|
|
74840ab63f | ||
|
|
31f33339f1 | ||
|
|
c0a0d0dc47 | ||
|
|
0cd77ee9b9 | ||
|
|
741d7aa8ca | ||
|
|
d06a80dc73 | ||
|
|
86b9c43b8b | ||
|
|
206981d4b4 | ||
|
|
7d5831b5f4 | ||
|
|
7ca63985bd | ||
|
|
06416a4e5f | ||
|
|
88739e8d1a | ||
|
|
ffaba4f496 | ||
|
|
c4f94a2bc7 | ||
|
|
49a3918244 | ||
|
|
caddff567f | ||
|
|
aed9e9f10d | ||
|
|
6557b49383 | ||
|
|
082cc4c743 | ||
|
|
e261ebe698 | ||
|
|
5a0dc69186 | ||
|
|
29d8bf3ea4 | ||
|
|
07a330cfd4 | ||
|
|
aa30571709 | ||
|
|
2bb9912cf5 | ||
|
|
8f77d9fe98 | ||
|
|
9cb32b3e8f | ||
|
|
080206925f | ||
|
|
54d2a8189c | ||
|
|
377bc12cf9 | ||
|
|
da715c8a8f | ||
|
|
fa4028296f | ||
|
|
dc629a3126 | ||
|
|
746c7ad5b3 | ||
|
|
aef1b9ab11 | ||
|
|
79c9eb3dda | ||
|
|
5a08ed68c2 | ||
|
|
e5a623c5a8 | ||
|
|
4586138bf1 | ||
|
|
1dddec110e | ||
|
|
46e9999e4c | ||
|
|
a664ae3a56 | ||
|
|
9bcdcb2cb7 | ||
|
|
0b8592559a | ||
|
|
c0b18be5ab | ||
|
|
46b2eb1ccd | ||
|
|
d323376bed | ||
|
|
838c86ae15 | ||
|
|
42925c857c | ||
|
|
ba981d281f | ||
|
|
4159b884de | ||
|
|
36b38421e5 | ||
|
|
b77c8a80e9 | ||
|
|
886dcd039f | ||
|
|
d99885d396 | ||
|
|
942de9c430 | ||
|
|
ae3ff9ecbb | ||
|
|
bcc4315ea9 | ||
|
|
2f7304ab2d | ||
|
|
0df01ba3d5 | ||
|
|
710e454fd0 | ||
|
|
c46dfa00a0 | ||
|
|
9a3ea05023 | ||
|
|
66d7a62d3a | ||
|
|
f1b6cd3e18 | ||
|
|
189d1477a8 | ||
|
|
9c3945f45a | ||
|
|
77c7d6c5d6 | ||
|
|
ab3009e9cc | ||
|
|
1c2291f9cf | ||
|
|
efb135ee48 | ||
|
|
0699c4a127 | ||
|
|
33cef5f8e1 | ||
|
|
32d467b6d9 | ||
|
|
6dfa770137 | ||
|
|
0dfee8ab22 | ||
|
|
85fe910f25 | ||
|
|
6c60b0116f | ||
|
|
8dfcf510f6 | ||
|
|
be1ed3d46d | ||
|
|
1fca578c07 | ||
|
|
a83665ac44 | ||
|
|
21e8a370c3 | ||
|
|
3dd4dd139b | ||
|
|
afe2046bc8 | ||
|
|
6d24388690 | ||
|
|
0b024a849a | ||
|
|
8745dc3567 | ||
|
|
1f4c7019d4 | ||
|
|
255e7b2fb5 | ||
|
|
7419e536cf | ||
|
|
74df000287 | ||
|
|
2e0127d609 | ||
|
|
2b9168e8df | ||
|
|
7bb3cf7c51 | ||
|
|
2ce5058be0 | ||
|
|
a5fc41f152 | ||
|
|
3245a27156 | ||
|
|
ce0fbf65aa | ||
|
|
eb87c3d865 | ||
|
|
990a90e461 | ||
|
|
45cb255a4d | ||
|
|
5e8b8a3575 | ||
|
|
c317e53c24 | ||
|
|
29b59d8450 | ||
|
|
97b7e2f86e | ||
|
|
2f55294b58 | ||
|
|
2161190d9a | ||
|
|
aaae301cbc | ||
|
|
9ca6511235 | ||
|
|
8582290db3 | ||
|
|
5ff33d7c58 | ||
|
|
0cfb69a240 | ||
|
|
110a9beda4 | ||
|
|
fd6a3f5929 | ||
|
|
82ad6ec427 | ||
|
|
1bfc7c960d | ||
|
|
332f49f483 | ||
|
|
b967c7a1bb | ||
|
|
519dbe1f77 | ||
|
|
c64855d4ad | ||
|
|
d50d10ba89 | ||
|
|
e79478c421 | ||
|
|
c401a84eb8 | ||
|
|
a9978a6202 | ||
|
|
cc9d0a4b30 | ||
|
|
696ef832f8 | ||
|
|
442f9d1d2e | ||
|
|
a45a64380c | ||
|
|
df7442c3dd | ||
|
|
1a949725f3 | ||
|
|
7c14f3437b | ||
|
|
bc402da365 | ||
|
|
b95b474660 | ||
|
|
691a3770d9 | ||
|
|
49ec54a3b5 | ||
|
|
2fc15f437e | ||
|
|
190f6ca7ab | ||
|
|
c683fd27d4 | ||
|
|
d815cc1010 | ||
|
|
fd84ff1200 | ||
|
|
a420bad305 | ||
|
|
6ef6e47580 | ||
|
|
94f83ec296 | ||
|
|
702569c701 | ||
|
|
d4940ca9ee | ||
|
|
dbd1c138ca | ||
|
|
0b49c55cf3 | ||
|
|
b1ec3b2eea | ||
|
|
9ea89feee7 | ||
|
|
4a843194a3 | ||
|
|
03503115fd | ||
|
|
1870aef60e | ||
|
|
ed8fd66d1e | ||
|
|
c1c4c8cdc5 | ||
|
|
256ec3b152 | ||
|
|
da490e5bbd | ||
|
|
ba0ac86eea | ||
|
|
b5544c4131 | ||
|
|
d94ea6aaf3 | ||
|
|
742dda8677 | ||
|
|
76e0db0cfb | ||
|
|
6458a6e2c5 | ||
|
|
cbf2294a8e | ||
|
|
9d558bf4e2 | ||
|
|
fdf067c25e | ||
|
|
5f9da47513 | ||
|
|
b150641e3b | ||
|
|
6355c35452 | ||
|
|
81ffbbd6b1 | ||
|
|
07c650a474 | ||
|
|
04443bcb5e | ||
|
|
d06974d556 | ||
|
|
80fa51fd0a | ||
|
|
a170134456 | ||
|
|
ba76e0dc6c | ||
|
|
4a79542a46 | ||
|
|
999e260a60 | ||
|
|
ed40b254e4 | ||
|
|
8e9b9e6347 | ||
|
|
4fa2d88204 | ||
|
|
cb6d4811d2 | ||
|
|
c19601b209 | ||
|
|
6e6215ad7e | ||
|
|
3a25e5f2ae | ||
|
|
97ae1a5e92 | ||
|
|
7fc6367b9e | ||
|
|
a4a521a6f8 | ||
|
|
cb5c83be41 | ||
|
|
2056a91071 | ||
|
|
c760e47154 | ||
|
|
f6209a8959 | ||
|
|
3d30835ffa | ||
|
|
72ed0ea8f8 | ||
|
|
ea47bb39eb | ||
|
|
d698f82299 | ||
|
|
c45967e175 | ||
|
|
ba4666c32a | ||
|
|
1b357de2f3 | ||
|
|
d6d37e8204 | ||
|
|
cf76a5ddd6 | ||
|
|
cb9dbfcc69 | ||
|
|
4e27bf0dc4 | ||
|
|
42b366ed0f | ||
|
|
39c082e4ee | ||
|
|
4021c507b6 | ||
|
|
95193fc8f9 | ||
|
|
4d8e82080d | ||
|
|
4f3131979f | ||
|
|
fca608b44e | ||
|
|
ad9a8ab0b0 | ||
|
|
34cb89a6ea | ||
|
|
1f3161216a | ||
|
|
69d5f269bd | ||
|
|
e5f7b164a3 | ||
|
|
459c974cb8 | ||
|
|
7974c92382 | ||
|
|
2f22cb01eb | ||
|
|
b51add45f1 | ||
|
|
e17fb99de4 | ||
|
|
0f926a57ef | ||
|
|
4380bfe0d6 | ||
|
|
8127cd0758 | ||
|
|
71a220f42b | ||
|
|
d3b02676ec | ||
|
|
d8f0dc4f8e | ||
|
|
48aa5b6148 | ||
|
|
a989841a89 | ||
|
|
86783e8e46 | ||
|
|
66b9bcc53a | ||
|
|
ff08615d1e | ||
|
|
4a6c3a1f90 | ||
|
|
c79702eba8 | ||
|
|
41fc935c09 | ||
|
|
5fbbe5b240 | ||
|
|
9c121c03f2 | ||
|
|
b74c3f31c4 | ||
|
|
f23b97e22c | ||
|
|
0560989e77 | ||
|
|
f2c9c34324 | ||
|
|
a97268c702 | ||
|
|
2654b550fb | ||
|
|
12a5a17a30 | ||
|
|
e346812c0a | ||
|
|
6b2e391af9 | ||
|
|
199456cb11 | ||
|
|
636994387f | ||
|
|
90c79f7bac | ||
|
|
79601b36a5 | ||
|
|
96cef89f08 | ||
|
|
e057825b25 | ||
|
|
ebe848dfe8 | ||
|
|
e259a0989e | ||
|
|
f750aa68e8 | ||
|
|
49b8501e86 | ||
|
|
fc2e2a4697 | ||
|
|
b4b1d7f923 | ||
|
|
df646b8c43 | ||
|
|
8e790f6b22 | ||
|
|
2e76af980e | ||
|
|
80ccbe5b62 | ||
|
|
aa0cd68f7f | ||
|
|
51833148b1 | ||
|
|
0fa77ebf21 | ||
|
|
584c075930 | ||
|
|
9e7a3718a4 | ||
|
|
0e2ca2f1ca | ||
|
|
52e889333b | ||
|
|
4745440079 | ||
|
|
b5628eac17 | ||
|
|
d23b11577e | ||
|
|
f1dd9fc0ee | ||
|
|
0877fadcfb | ||
|
|
733059dc8e | ||
|
|
321380c2b9 | ||
|
|
deb121a51b | ||
|
|
b6aef7c239 | ||
|
|
b13a7a5ee1 | ||
|
|
fff6a00690 | ||
|
|
b4da3cee20 | ||
|
|
0a650996a1 | ||
|
|
37b91cd388 | ||
|
|
42b721eabf | ||
|
|
231d5c3fb9 | ||
|
|
064efdf3f3 | ||
|
|
7b364a0f90 | ||
|
|
c8a4ca7f16 | ||
|
|
97627b824c | ||
|
|
6dbdc9bd90 | ||
|
|
4166ea10af | ||
|
|
fa12ab2c51 | ||
|
|
4fc4ed8640 | ||
|
|
48172f2769 | ||
|
|
c7e6d68fbd | ||
|
|
e6672a51c5 | ||
|
|
abde7841ac | ||
|
|
8e661ed1c5 | ||
|
|
217cb3a776 | ||
|
|
b8c1bca900 | ||
|
|
5e09b8e92a | ||
|
|
c16bde6bb8 | ||
|
|
eb06eb9f3d | ||
|
|
0c49088098 | ||
|
|
b970241a64 | ||
|
|
6c5aafc11e | ||
|
|
ca0846f4a7 | ||
|
|
14f7fcc5ad | ||
|
|
23a18aba5c | ||
|
|
2d5a79bb16 | ||
|
|
51e1bbefbb | ||
|
|
6647fc5371 | ||
|
|
b67544f0cd | ||
|
|
ef5826a73b | ||
|
|
a48adbd0df | ||
|
|
5c60493a24 | ||
|
|
55e6227de0 | ||
|
|
24a7395004 | ||
|
|
b2a7187fc5 | ||
|
|
ace7cdc88e | ||
|
|
2e909b5868 | ||
|
|
de78bcbc98 | ||
|
|
c560f0e902 | ||
|
|
88860c9302 | ||
|
|
ef65d0e095 | ||
|
|
6438505f4a | ||
|
|
1be5030dd1 | ||
|
|
421b2f4a42 | ||
|
|
a66bf84381 | ||
|
|
0c4d366d6d | ||
|
|
a1d0a921fa | ||
|
|
2092230a61 | ||
|
|
6e5ae6eff3 | ||
|
|
4f8259d772 | ||
|
|
8b987d9824 | ||
|
|
e8ba451d18 | ||
|
|
77758db463 | ||
|
|
6c58598a8e | ||
|
|
3ac35230a3 | ||
|
|
ca91302d28 | ||
|
|
e07272a5dc | ||
|
|
461305e1df | ||
|
|
8e8ab10a0f | ||
|
|
e754326925 | ||
|
|
4c92994c6f | ||
|
|
66c57f47ac | ||
|
|
ec689eadd8 | ||
|
|
c1e15709a7 | ||
|
|
2848937ce7 | ||
|
|
42ff0c21b2 | ||
|
|
3ed7e547e6 | ||
|
|
fad8e688df | ||
|
|
b2f2dcac4c | ||
|
|
fcba78e977 | ||
|
|
77c9d777a1 | ||
|
|
4460659210 | ||
|
|
8631524107 | ||
|
|
0579588814 | ||
|
|
df2bfb6ac7 | ||
|
|
517b6f8910 | ||
|
|
94a46d2833 | ||
|
|
1631a0faa1 | ||
|
|
06d5943cb6 | ||
|
|
3095e13676 | ||
|
|
482cbce7e8 | ||
|
|
ef2fc3ab98 | ||
|
|
5b57b05eae | ||
|
|
093579c6f5 | ||
|
|
c2d4990d7f | ||
|
|
40e8380738 | ||
|
|
b716e1e861 | ||
|
|
8432d4952c | ||
|
|
dd8f085f63 | ||
|
|
161ff8d8a3 | ||
|
|
1390cd0104 | ||
|
|
7a02b1e97f | ||
|
|
036f107c90 | ||
|
|
5652383450 | ||
|
|
9a047a5da0 | ||
|
|
8975743a37 | ||
|
|
0e30eafe49 | ||
|
|
7facb62f21 | ||
|
|
30d43cb961 | ||
|
|
c505b17e35 | ||
|
|
080c4c753d | ||
|
|
ade0d34d91 | ||
|
|
56d082eb0b | ||
|
|
795c4529ba | ||
|
|
0f5cfe22ea | ||
|
|
925ad2c3a5 | ||
|
|
f3e96907fe | ||
|
|
75a1e58a7d | ||
|
|
379e889f71 | ||
|
|
40c06f397b | ||
|
|
9e9302436e | ||
|
|
6d816d4193 | ||
|
|
438eda9c08 | ||
|
|
ace39e4633 | ||
|
|
7712391f29 | ||
|
|
65f08dcb02 | ||
|
|
1d332aa6f4 | ||
|
|
ba81641517 | ||
|
|
fb30bdb879 | ||
|
|
fc451d3b41 | ||
|
|
ffee1f3462 | ||
|
|
f30027dd13 | ||
|
|
8b6aad17f4 | ||
|
|
b77930adfb | ||
|
|
aff8f88076 | ||
|
|
4e6582e24c | ||
|
|
fa9acaccfd | ||
|
|
0414f85f12 | ||
|
|
527dd0eeb4 | ||
|
|
74ed7475c9 | ||
|
|
54dc3dba96 | ||
|
|
9980159090 | ||
|
|
0b193ebb00 | ||
|
|
f5fc9843b2 | ||
|
|
df1f67dd26 | ||
|
|
b069a5f576 | ||
|
|
7686763a03 | ||
|
|
635a6eac37 | ||
|
|
877edc535f | ||
|
|
01d59e3c1e | ||
|
|
fece2799e3 | ||
|
|
de9756ee86 | ||
|
|
51f047a51b | ||
|
|
04b66e82b7 | ||
|
|
df89253647 | ||
|
|
45dbca794e | ||
|
|
857bf28f99 | ||
|
|
e07ce15228 | ||
|
|
0637bcfd8e | ||
|
|
23db0563ac | ||
|
|
ccba71aa4e | ||
|
|
fee4bf2ad6 | ||
|
|
5052fea9ef | ||
|
|
e85215fca1 | ||
|
|
e42fe45329 | ||
|
|
4240815bd1 | ||
|
|
6f0c5305e2 | ||
|
|
757005cad8 | ||
|
|
81651ad97c | ||
|
|
f3763b6058 | ||
|
|
82478dc485 | ||
|
|
703fd05d43 | ||
|
|
ff868b6e2a | ||
|
|
051a74b37b | ||
|
|
a0a1085623 | ||
|
|
2612d3919c | ||
|
|
6f123a236f | ||
|
|
71e57e6a08 | ||
|
|
7cb18d8186 | ||
|
|
cdbf670ece | ||
|
|
e0dafee617 | ||
|
|
c37bce1563 | ||
|
|
0b3c77d644 | ||
|
|
2cf85caf86 | ||
|
|
594010ba88 | ||
|
|
d91460d2e2 | ||
|
|
89713e6ac9 | ||
|
|
17c9b2538c | ||
|
|
d337b003cb | ||
|
|
8f1485656b | ||
|
|
f0c4ec3031 | ||
|
|
458be84aca | ||
|
|
c7716aad34 | ||
|
|
4a3e734408 | ||
|
|
54cc212753 | ||
|
|
f4ae1eaa51 | ||
|
|
d2537282c9 | ||
|
|
87527688c3 | ||
|
|
2be0b6505a | ||
|
|
672f100c7c | ||
|
|
5af402c0c5 | ||
|
|
693a4f0f63 | ||
|
|
f8d2426ff6 | ||
|
|
973a9bedcd | ||
|
|
a583733400 | ||
|
|
78eb51b3ca | ||
|
|
a2606be9d8 | ||
|
|
a4975c72ce | ||
|
|
58f96822a4 | ||
|
|
96b0d94986 | ||
|
|
70b38db8c5 | ||
|
|
b6a9c2d3a0 | ||
|
|
a01df6c7db | ||
|
|
4455bee570 | ||
|
|
cd0adef2ed | ||
|
|
4aa24a65fb | ||
|
|
760dca8772 | ||
|
|
8269673619 | ||
|
|
82ec586554 | ||
|
|
ca98cc8708 | ||
|
|
f07800985d | ||
|
|
b042df93b7 | ||
|
|
ecf4441946 | ||
|
|
39b2612840 | ||
|
|
218f486e6b | ||
|
|
cfcc25f175 | ||
|
|
155e88223c | ||
|
|
05039ece51 | ||
|
|
1c673f2b46 | ||
|
|
2232a70228 | ||
|
|
e661b1d817 | ||
|
|
5a6a65f580 | ||
|
|
5cdd3bcd30 | ||
|
|
1233a885b8 | ||
|
|
adf56cd768 | ||
|
|
cd4b208be9 | ||
|
|
502de1a5d0 | ||
|
|
4158f869a3 | ||
|
|
ff569d339c | ||
|
|
10eec323dd | ||
|
|
0a3db51c7d | ||
|
|
229aee0b30 | ||
|
|
44714aa981 | ||
|
|
2b6138a889 | ||
|
|
5e424f7bf4 | ||
|
|
294e47deed | ||
|
|
dc42547b73 | ||
|
|
7ef7f0706b | ||
|
|
a59b98a7c9 | ||
|
|
088fee9a4d | ||
|
|
d8666acfe8 | ||
|
|
209e9c2866 | ||
|
|
6c3195dea4 | ||
|
|
7d774a7433 | ||
|
|
f1ec4433cf | ||
|
|
1f825db261 | ||
|
|
1019f24f1d | ||
|
|
f5c5b99579 | ||
|
|
e002d19f1b | ||
|
|
0d5c9651f0 | ||
|
|
53fc13b95c | ||
|
|
694c3b0ddc | ||
|
|
9b3d4577cc | ||
|
|
77783915dd | ||
|
|
b67ac21a79 | ||
|
|
603afed2f2 | ||
|
|
c2a7c34496 | ||
|
|
edd40f48ba | ||
|
|
cca3d6b8b9 | ||
|
|
f004512903 | ||
|
|
6407bb6730 | ||
|
|
b914eeadec | ||
|
|
dcadc6783e | ||
|
|
bc9f43634f | ||
|
|
4778f0164c | ||
|
|
9904754a07 | ||
|
|
1b79077c9e | ||
|
|
c13afb05b3 | ||
|
|
03e320efb8 | ||
|
|
7846332476 | ||
|
|
2d5d3b86e0 | ||
|
|
7c9ba890af | ||
|
|
0d609daffa | ||
|
|
c12e56d402 | ||
|
|
c7b38c07d5 | ||
|
|
dc51066cea | ||
|
|
59d2fb3010 | ||
|
|
a0fe702c10 | ||
|
|
f886f7e366 | ||
|
|
1d475d88ed | ||
|
|
e64c901912 | ||
|
|
5fce02e623 | ||
|
|
13997bc543 | ||
|
|
3e05b0d6f9 | ||
|
|
966417f867 | ||
|
|
601a0d1b91 |
No files matched your search
-346
@@ -1,346 +0,0 @@
|
|||||||
# NetDisk Fast Download - Agent 规则文件
|
|
||||||
|
|
||||||
## 项目概述
|
|
||||||
网盘快速下载项目,支持多种网盘链接解析和下载加速。
|
|
||||||
|
|
||||||
## 技术栈
|
|
||||||
|
|
||||||
### 后端
|
|
||||||
- **Java 版本**: JDK 17
|
|
||||||
- **构建工具**: Maven 3.x
|
|
||||||
- **核心框架**: Vert.x 4.5.23
|
|
||||||
- **日志框架**: SLF4J 2.0.5 + Logback 1.5.19
|
|
||||||
- **工具库**:
|
|
||||||
- Lombok 1.18.38
|
|
||||||
- Apache Commons Lang3 3.18.0
|
|
||||||
- Apache Commons BeanUtils 2.0.0
|
|
||||||
- Jackson 2.14.2
|
|
||||||
- Reflections 0.10.2
|
|
||||||
|
|
||||||
### 前端
|
|
||||||
- Vue.js 框架
|
|
||||||
- Monaco Editor (代码编辑器)
|
|
||||||
|
|
||||||
### 测试
|
|
||||||
- JUnit 4.13.2
|
|
||||||
- **Maven 测试配置**: 默认跳过测试,使用 `-Dmaven.test.skip=false` 执行测试
|
|
||||||
|
|
||||||
## 项目模块结构
|
|
||||||
|
|
||||||
```
|
|
||||||
netdisk-fast-download/
|
|
||||||
├── core/ # 核心功能模块
|
|
||||||
├── core-database/ # 数据库模块
|
|
||||||
├── parser/ # 解析器模块(支持自定义解析器)
|
|
||||||
├── web-service/ # Web 服务模块
|
|
||||||
└── web-front/ # 前端模块
|
|
||||||
```
|
|
||||||
|
|
||||||
## 编码规范
|
|
||||||
|
|
||||||
### Java 代码规范
|
|
||||||
1. **使用 Lombok 注解简化代码**
|
|
||||||
- `@Data`, `@Getter`, `@Setter`, `@Builder` 等
|
|
||||||
- `@Slf4j` 用于日志
|
|
||||||
|
|
||||||
2. **异步编程**
|
|
||||||
- 使用 Vert.x 的 Future/Promise 模式
|
|
||||||
- 遵循响应式编程范式
|
|
||||||
- 避免阻塞操作
|
|
||||||
|
|
||||||
3. **日志规范**
|
|
||||||
- 使用 SLF4J + Logback
|
|
||||||
- 日志级别:ERROR(错误)、WARN(警告)、INFO(重要信息)、DEBUG(调试信息)
|
|
||||||
- 日志文件按日期分目录存储在 `logs/` 下
|
|
||||||
|
|
||||||
4. **包命名规范**
|
|
||||||
- 基础包名:`cn.qaiu`
|
|
||||||
- 子包按模块功能划分
|
|
||||||
|
|
||||||
### 测试规范
|
|
||||||
1. **默认跳过测试**: 打包时使用 `mvn clean package`
|
|
||||||
2. **执行测试**: 使用 `mvn test -Dmaven.test.skip=false`
|
|
||||||
3. 测试类放在 `src/test/java` 目录下
|
|
||||||
|
|
||||||
### Core 模块封装(禁止重复造轮子)
|
|
||||||
|
|
||||||
#### Web 路由封装
|
|
||||||
**核心类**: `cn.qaiu.vx.core.handlerfactory.RouterHandlerFactory`
|
|
||||||
|
|
||||||
使用注解方式定义路由,无需手动创建 Router:
|
|
||||||
```java
|
|
||||||
// ✅ 推荐:使用注解定义路由
|
|
||||||
@RouteHandler("/api") // 类级别路由前缀
|
|
||||||
@Slf4j
|
|
||||||
public class MyController {
|
|
||||||
|
|
||||||
@RouteMapping(value = "/users", method = RouteMethod.GET)
|
|
||||||
public Future<List<User>> getUsers() {
|
|
||||||
// 返回 Future,框架自动处理响应
|
|
||||||
return userService.findAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
@RouteMapping(value = "/user/:id", method = RouteMethod.GET)
|
|
||||||
public Future<User> getUserById(String id) {
|
|
||||||
// 路径参数自动注入
|
|
||||||
return userService.findById(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
@RouteMapping(value = "/user", method = RouteMethod.POST)
|
|
||||||
public Future<JsonResult<User>> createUser(HttpServerRequest request, String name, Integer age) {
|
|
||||||
// 查询参数自动注入
|
|
||||||
return userService.create(name, age)
|
|
||||||
.map(JsonResult::success);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ❌ 避免:手动创建路由
|
|
||||||
Router router = Router.router(vertx);
|
|
||||||
router.get("/api/users").handler(ctx -> {
|
|
||||||
// 不要这样写
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
**支持的注解:**
|
|
||||||
- `@RouteHandler(value="/path", order=0)` - 标记路由处理类
|
|
||||||
- `@RouteMapping(value="/path", method=RouteMethod.GET)` - 标记路由方法
|
|
||||||
- `@SockRouteMapper("/ws")` - WebSocket 路由
|
|
||||||
|
|
||||||
**自动参数注入:**
|
|
||||||
- `HttpServerRequest` - 请求对象
|
|
||||||
- `HttpServerResponse` - 响应对象
|
|
||||||
- `RoutingContext` - 路由上下文
|
|
||||||
- `String param` - 路径参数或查询参数(自动匹配名称)
|
|
||||||
- 自定义对象 - 自动从请求体反序列化
|
|
||||||
|
|
||||||
#### 响应处理工具
|
|
||||||
**工具类**: `cn.qaiu.vx.core.util.ResponseUtil`
|
|
||||||
|
|
||||||
```java
|
|
||||||
// ✅ 推荐:使用 ResponseUtil
|
|
||||||
ResponseUtil.redirect(response, "https://example.com");
|
|
||||||
ResponseUtil.fireJsonObjectResponse(ctx, jsonObject);
|
|
||||||
ResponseUtil.fireJsonResultResponse(ctx, JsonResult.success(data));
|
|
||||||
|
|
||||||
// ❌ 避免:手动设置响应头
|
|
||||||
response.putHeader("Content-Type", "application/json");
|
|
||||||
response.end(json);
|
|
||||||
```
|
|
||||||
|
|
||||||
#### 统一响应模型
|
|
||||||
**模型类**: `cn.qaiu.vx.core.model.JsonResult<T>`
|
|
||||||
|
|
||||||
```java
|
|
||||||
// ✅ 推荐:使用 JsonResult 统一响应格式
|
|
||||||
public Future<JsonResult<User>> getUser(String id) {
|
|
||||||
return userService.findById(id)
|
|
||||||
.map(JsonResult::success) // 成功响应
|
|
||||||
.otherwise(err -> JsonResult.error(err.getMessage())); // 错误响应
|
|
||||||
}
|
|
||||||
|
|
||||||
// 响应格式:
|
|
||||||
// {"code": 200, "msg": "success", "success": true, "data": {...}, "timestamp": 123456789}
|
|
||||||
```
|
|
||||||
|
|
||||||
#### 异步服务代理
|
|
||||||
**工具类**: `cn.qaiu.vx.core.util.AsyncServiceUtil`
|
|
||||||
|
|
||||||
```java
|
|
||||||
// ✅ 推荐:使用服务代理
|
|
||||||
private final UserService userService = AsyncServiceUtil.getAsyncServiceInstance(UserService.class);
|
|
||||||
|
|
||||||
// ❌ 避免:手动管理服务实例和 EventBus
|
|
||||||
```
|
|
||||||
|
|
||||||
### Core-Database 模块封装(禁止重复造轮子)
|
|
||||||
|
|
||||||
#### DDL 自动生成
|
|
||||||
**核心类**: `cn.qaiu.db.ddl.CreateTable`
|
|
||||||
|
|
||||||
使用注解定义实体,自动生成建表 SQL:
|
|
||||||
```java
|
|
||||||
// ✅ 推荐:使用注解定义实体
|
|
||||||
@Data
|
|
||||||
@Table("users") // 表名
|
|
||||||
public class User {
|
|
||||||
@Constraint(autoIncrement = true)
|
|
||||||
private Long id; // 自动识别为主键
|
|
||||||
|
|
||||||
@Constraint(notNull = true, uniqueKey = "uk_email")
|
|
||||||
@Length(varcharSize = 100)
|
|
||||||
private String email;
|
|
||||||
|
|
||||||
@Constraint(notNull = true)
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
@Constraint(defaultValue = "0", defaultValueIsFunction = false)
|
|
||||||
private Integer status;
|
|
||||||
|
|
||||||
@Constraint(defaultValue = "NOW()", defaultValueIsFunction = true)
|
|
||||||
private Date createdAt;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 自动建表
|
|
||||||
CreateTable.createTable(pool, JDBCType.MySQL);
|
|
||||||
|
|
||||||
// ❌ 避免:手写建表 SQL
|
|
||||||
pool.query("CREATE TABLE users (...)").execute();
|
|
||||||
```
|
|
||||||
|
|
||||||
**支持的注解:**
|
|
||||||
- `@Table("tableName")` - 指定表名和主键
|
|
||||||
- `@Constraint` - 字段约束
|
|
||||||
- `notNull` - 非空约束
|
|
||||||
- `uniqueKey` - 唯一键约束
|
|
||||||
- `defaultValue` - 默认值
|
|
||||||
- `autoIncrement` - 自增
|
|
||||||
- `@Length` - 字段长度
|
|
||||||
- `varcharSize` - VARCHAR 长度
|
|
||||||
- `decimalSize` - DECIMAL 精度
|
|
||||||
- `@TableGenIgnore` - 忽略字段(不生成列)
|
|
||||||
- `@Column(name="column_name")` - 自定义列名
|
|
||||||
|
|
||||||
#### 自动数据库创建
|
|
||||||
**工具类**: `cn.qaiu.db.ddl.CreateDatabase`
|
|
||||||
|
|
||||||
```java
|
|
||||||
// ✅ 推荐:自动创建数据库
|
|
||||||
JsonObject dbConfig = config.getJsonObject("database");
|
|
||||||
CreateDatabase.createDatabase(dbConfig);
|
|
||||||
|
|
||||||
// ❌ 避免:手动连接和执行 SQL
|
|
||||||
```
|
|
||||||
|
|
||||||
### Parser 模块特殊说明
|
|
||||||
1. 支持自定义解析器(Java、Python、JavaScript)
|
|
||||||
2. Python 解析器使用 GraalPy 实现
|
|
||||||
3. 支持 WebSocket 连接到外部 Python 环境
|
|
||||||
4. 包含安全测试和沙箱机制
|
|
||||||
|
|
||||||
## Maven 命令
|
|
||||||
|
|
||||||
### 常用命令
|
|
||||||
```bash
|
|
||||||
# 编译打包(跳过测试)
|
|
||||||
mvn clean package
|
|
||||||
|
|
||||||
# 安装到本地仓库(跳过测试)
|
|
||||||
mvn clean install
|
|
||||||
|
|
||||||
# 执行测试
|
|
||||||
mvn test -Dmaven.test.skip=false
|
|
||||||
|
|
||||||
# 编译并执行测试
|
|
||||||
mvn clean package -Dmaven.test.skip=false
|
|
||||||
|
|
||||||
# 只编译不打包
|
|
||||||
mvn clean compile
|
|
||||||
|
|
||||||
# 清理
|
|
||||||
mvn clean
|
|
||||||
```
|
|
||||||
|
|
||||||
### 模块化构建
|
|
||||||
```bash
|
|
||||||
# 只构建特定模块
|
|
||||||
mvn clean package -pl parser -am
|
|
||||||
|
|
||||||
# 构建多个模块
|
|
||||||
mvn clean package -pl core,parser -am
|
|
||||||
```
|
|
||||||
|
|
||||||
## 部署相关
|
|
||||||
|
|
||||||
### 目录结构
|
|
||||||
- `bin/`: 启动脚本和服务安装脚本
|
|
||||||
- `db/`: 数据库文件
|
|
||||||
- `logs/`: 日志文件(按日期分目录)
|
|
||||||
- `webroot/`: Web 静态资源根目录
|
|
||||||
|
|
||||||
### 脚本文件
|
|
||||||
- `run.sh` / `run.bat`: 启动脚本
|
|
||||||
- `stop.sh`: 停止脚本
|
|
||||||
- `service-install.sh`: Linux 服务安装
|
|
||||||
- `nfd-service-install.bat`: Windows 服务安装
|
|
||||||
|
|
||||||
## 开发注意事项
|
|
||||||
|
|
||||||
1. **字符编码**: 统一使用 UTF-8
|
|
||||||
2. **Java 版本**: 必须使用 JDK 17 或更高版本
|
|
||||||
3. **Vert.x 异步**: 避免在 Event Loop 线程中执行阻塞操作
|
|
||||||
4. **资源文件**:
|
|
||||||
- 静态资源放在 `webroot/` 目录
|
|
||||||
- 前端构建产物输出到 `web-front/public/`
|
|
||||||
5. **日志文件**: 不要提交 `logs/` 目录到版本控制
|
|
||||||
6. **测试**: 新增功能必须编写单元测试,使用 `-Dmaven.test.skip=false` 验证
|
|
||||||
|
|
||||||
## 代码审查要点
|
|
||||||
|
|
||||||
1. 是否正确处理异步操作
|
|
||||||
2. 是否有潜在的资源泄漏(连接、文件句柄等)
|
|
||||||
3. 异常处理是否完善
|
|
||||||
4. 日志记录是否合理
|
|
||||||
5. 是否遵循单一职责原则
|
|
||||||
6. 是否有适当的注释说明复杂逻辑
|
|
||||||
|
|
||||||
## 性能优化建议
|
|
||||||
|
|
||||||
1. 使用 Vert.x 的异步特性,避免阻塞
|
|
||||||
2. 合理使用缓存机制
|
|
||||||
3. 数据库查询优化
|
|
||||||
4. 静态资源压缩和缓存策略
|
|
||||||
5. 使用连接池管理数据库连接
|
|
||||||
|
|
||||||
## 安全注意事项
|
|
||||||
|
|
||||||
1. **Parser 模块**:
|
|
||||||
- 自定义解析器需要经过安全验证
|
|
||||||
- Python/JavaScript 代码执行需要沙箱隔离
|
|
||||||
- 参考 `parser/doc/SECURITY_TESTING_GUIDE.md`
|
|
||||||
|
|
||||||
2. **输入验证**:
|
|
||||||
- 所有外部输入必须验证和清理
|
|
||||||
- 防止注入攻击
|
|
||||||
|
|
||||||
3. **敏感信息**:
|
|
||||||
- 不要在日志中输出敏感信息
|
|
||||||
- 配置文件中的密钥要加密存储
|
|
||||||
|
|
||||||
## 文档参考
|
|
||||||
|
|
||||||
- Parser 模块文档: `parser/doc/`
|
|
||||||
- API 使用指南: `API_USAGE.md`
|
|
||||||
- 自定义解析器指南: `CUSTOM_PARSER_GUIDE.md`
|
|
||||||
- Python 解析器指南: `PYTHON_PARSER_GUIDE.md`
|
|
||||||
- JavaScript 解析器指南: `JAVASCRIPT_PARSER_GUIDE.md`
|
|
||||||
- 安全测试指南: `SECURITY_TESTING_GUIDE.md`
|
|
||||||
|
|
||||||
- 前端文档: `web-front/doc/`
|
|
||||||
- Monaco Editor 集成: `MONACO_EDITOR_NPM.md`
|
|
||||||
- Playground UI 升级: `PLAYGROUND_UI_UPGRADE.md`
|
|
||||||
|
|
||||||
## Git 提交规范
|
|
||||||
|
|
||||||
使用语义化提交信息:
|
|
||||||
- `feat`: 新功能
|
|
||||||
- `fix`: 修复 Bug
|
|
||||||
- `docs`: 文档更新
|
|
||||||
- `style`: 代码格式调整
|
|
||||||
- `refactor`: 重构
|
|
||||||
- `test`: 测试相关
|
|
||||||
- `chore`: 构建/工具链相关
|
|
||||||
|
|
||||||
示例:
|
|
||||||
```
|
|
||||||
feat(parser): 添加新的网盘解析器支持
|
|
||||||
fix(core): 修复下载链接过期问题
|
|
||||||
docs(readme): 更新安装说明
|
|
||||||
```
|
|
||||||
|
|
||||||
## AI 助手使用建议
|
|
||||||
|
|
||||||
1. 在修改代码前,先理解项目的模块结构和依赖关系
|
|
||||||
2. 生成的代码要符合项目现有的编码风格
|
|
||||||
3. 涉及异步操作时,优先使用 Vert.x 的 Future/Promise API
|
|
||||||
4. 修改配置文件时要考虑向后兼容性
|
|
||||||
5. 新增功能时同步更新相关文档
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
blank_issues_enabled: false
|
||||||
|
contact_links:
|
||||||
|
- name: 使用文档
|
||||||
|
url: https://nfd-parser.github.io/
|
||||||
|
about: 部署、接口和配置说明
|
||||||
|
- name: 已发布版本
|
||||||
|
url: https://github.com/qaiu/netdisk-fast-download/releases
|
||||||
|
about: 先看最新版本和更新说明,再反馈旧版本里已经修过的问题
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
name: 功能建议
|
||||||
|
description: 新功能或改进建议。不需要填写报错、日志或分享链接。
|
||||||
|
title: "[建议] "
|
||||||
|
labels:
|
||||||
|
- enhancement
|
||||||
|
body:
|
||||||
|
- type: markdown
|
||||||
|
attributes:
|
||||||
|
value: |
|
||||||
|
这里只讨论建议。解析失败或程序跑不起来,请分别使用对应模板。
|
||||||
|
|
||||||
|
如果要举例,只用可公开的测试分享,不要附带账号、令牌或私人文件。
|
||||||
|
- type: checkboxes
|
||||||
|
id: checks
|
||||||
|
attributes:
|
||||||
|
label: 提交前
|
||||||
|
options:
|
||||||
|
- label: 我查过现有 issue,没有找到相同建议
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: problem
|
||||||
|
attributes:
|
||||||
|
label: 想解决的问题
|
||||||
|
placeholder: 现在做什么不方便
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: proposal
|
||||||
|
attributes:
|
||||||
|
label: 建议怎么做
|
||||||
|
placeholder: 希望增加或改变什么
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: alternatives
|
||||||
|
attributes:
|
||||||
|
label: 还可以怎么做
|
||||||
|
description: 没有就留空。
|
||||||
|
validations:
|
||||||
|
required: false
|
||||||
@@ -0,0 +1,157 @@
|
|||||||
|
name: 解析失败
|
||||||
|
description: 某个分享链接解析失败、列目录失败,或直链无法下载
|
||||||
|
title: "[解析失败] "
|
||||||
|
labels:
|
||||||
|
- bug
|
||||||
|
body:
|
||||||
|
- type: markdown
|
||||||
|
attributes:
|
||||||
|
value: |
|
||||||
|
只提交可以公开的测试分享。私人文件、含个人信息的分享不要发到这里。
|
||||||
|
|
||||||
|
不要粘贴 Cookie、Authorization、refresh_token、access token、网盘账号密码、后台密码,或未打码的配置。提取码、访问密码和账号密码不是一回事:下面只接受分享本身的提取码或访问密码。
|
||||||
|
- type: checkboxes
|
||||||
|
id: checks
|
||||||
|
attributes:
|
||||||
|
label: 提交前
|
||||||
|
options:
|
||||||
|
- label: 我查过现有 issue,没有找到相同问题
|
||||||
|
required: true
|
||||||
|
- label: 我看过当前版本和更新说明,这不是已经修好的问题
|
||||||
|
required: true
|
||||||
|
- label: 分享可以公开,且没有附带令牌、Cookie、账号密码或未脱敏配置
|
||||||
|
required: true
|
||||||
|
- type: input
|
||||||
|
id: version
|
||||||
|
attributes:
|
||||||
|
label: 版本号
|
||||||
|
description: 页面上的版本,或发布包名里的版本,例如 0.4.15。
|
||||||
|
placeholder: "0.4.15"
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: dropdown
|
||||||
|
id: pan
|
||||||
|
attributes:
|
||||||
|
label: 网盘类型
|
||||||
|
options:
|
||||||
|
- 蓝奏云
|
||||||
|
- 蓝奏云优享
|
||||||
|
- 城通网盘
|
||||||
|
- 永硕 E 盘
|
||||||
|
- 123云盘
|
||||||
|
- 夸克网盘
|
||||||
|
- UC网盘
|
||||||
|
- 小飞机盘
|
||||||
|
- 其他(在现象里补充名称)
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: dropdown
|
||||||
|
id: deploy
|
||||||
|
attributes:
|
||||||
|
label: 部署方式
|
||||||
|
options:
|
||||||
|
- 本机发布包
|
||||||
|
- Docker
|
||||||
|
- 自行编译
|
||||||
|
- 公共站点
|
||||||
|
- 其他
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: dropdown
|
||||||
|
id: kind
|
||||||
|
attributes:
|
||||||
|
label: 分享内容
|
||||||
|
options:
|
||||||
|
- 单文件
|
||||||
|
- 目录
|
||||||
|
- 不确定
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: dropdown
|
||||||
|
id: access
|
||||||
|
attributes:
|
||||||
|
label: 分享是否带密码
|
||||||
|
description: 提取码是分享链接自带的短码。不要填写网盘登录密码。
|
||||||
|
options:
|
||||||
|
- 无密码
|
||||||
|
- 只有提取码
|
||||||
|
- 有访问密码
|
||||||
|
- 不确定
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: input
|
||||||
|
id: passcode
|
||||||
|
attributes:
|
||||||
|
label: 提取码或访问密码
|
||||||
|
description: 没有就留空。这里不是账号密码。
|
||||||
|
placeholder: "例如 abcd"
|
||||||
|
validations:
|
||||||
|
required: false
|
||||||
|
- type: input
|
||||||
|
id: share-url
|
||||||
|
attributes:
|
||||||
|
label: 可公开的完整分享链接
|
||||||
|
description: 用专门准备的测试分享。没有可公开链接时留空,并在下一项说明原因。
|
||||||
|
placeholder: "https://"
|
||||||
|
validations:
|
||||||
|
required: false
|
||||||
|
- type: textarea
|
||||||
|
id: no-share-reason
|
||||||
|
attributes:
|
||||||
|
label: 不能提供测试分享的原因
|
||||||
|
description: 已经填写链接就留空。
|
||||||
|
placeholder: 例如分享含私人文件,只能描述现象
|
||||||
|
validations:
|
||||||
|
required: false
|
||||||
|
- type: checkboxes
|
||||||
|
id: share-or-reason
|
||||||
|
attributes:
|
||||||
|
label: 链接
|
||||||
|
options:
|
||||||
|
- label: 我填写了可公开的完整分享链接,或已经说明不能提供的原因
|
||||||
|
required: true
|
||||||
|
- type: dropdown
|
||||||
|
id: official
|
||||||
|
attributes:
|
||||||
|
label: 官方网页是否可用
|
||||||
|
options:
|
||||||
|
- 可以打开,并且能下载
|
||||||
|
- 可以打开,但不能下载
|
||||||
|
- 打不开或提示失效
|
||||||
|
- 还没在官方网页试过
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: expected
|
||||||
|
attributes:
|
||||||
|
label: 预期结果
|
||||||
|
placeholder: 应解析出直链,或列出目录中的文件
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: actual
|
||||||
|
attributes:
|
||||||
|
label: 实际结果
|
||||||
|
description: 写页面或接口返回的原文。不要贴令牌。
|
||||||
|
placeholder: 例如提示分享不存在,或直链返回 403
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: steps
|
||||||
|
attributes:
|
||||||
|
label: 复现步骤
|
||||||
|
placeholder: |
|
||||||
|
1. 使用上述版本和部署方式
|
||||||
|
2. 解析这个分享
|
||||||
|
3. 看到上述结果
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: logs
|
||||||
|
attributes:
|
||||||
|
label: 脱敏后的报错日志
|
||||||
|
description: 没有日志就写「无」。删除 Cookie、Authorization、token、密码和未脱敏配置后再粘贴。
|
||||||
|
render: shell
|
||||||
|
placeholder: 无
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
name: 部署或运行问题
|
||||||
|
description: 程序无法启动、页面打不开、配置或运行报错。不是某一个分享解析失败。
|
||||||
|
title: "[运行问题] "
|
||||||
|
labels:
|
||||||
|
- bug
|
||||||
|
body:
|
||||||
|
- type: markdown
|
||||||
|
attributes:
|
||||||
|
value: |
|
||||||
|
单个分享解析失败请改用「解析失败」。
|
||||||
|
|
||||||
|
不要粘贴 Cookie、Authorization、refresh_token、access token、网盘或后台密码、个人信息,以及未打码的配置。
|
||||||
|
- type: checkboxes
|
||||||
|
id: checks
|
||||||
|
attributes:
|
||||||
|
label: 提交前
|
||||||
|
options:
|
||||||
|
- label: 我查过现有 issue,没有找到相同问题
|
||||||
|
required: true
|
||||||
|
- label: 我看过当前版本和更新说明
|
||||||
|
required: true
|
||||||
|
- label: 日志和配置已经去掉令牌、密码和个人信息
|
||||||
|
required: true
|
||||||
|
- type: input
|
||||||
|
id: version
|
||||||
|
attributes:
|
||||||
|
label: 版本号
|
||||||
|
description: 例如 0.4.15。不确定就写获取方式。
|
||||||
|
placeholder: "0.4.15"
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: dropdown
|
||||||
|
id: deploy
|
||||||
|
attributes:
|
||||||
|
label: 部署方式
|
||||||
|
options:
|
||||||
|
- 本机发布包(Linux)
|
||||||
|
- 本机发布包(Windows)
|
||||||
|
- Docker
|
||||||
|
- 自行编译
|
||||||
|
- 其他
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: input
|
||||||
|
id: environment
|
||||||
|
attributes:
|
||||||
|
label: 系统环境
|
||||||
|
description: 操作系统、架构,以及是否用了代理。没有就写「未记录」。
|
||||||
|
placeholder: "例如 Ubuntu 22.04 x86_64"
|
||||||
|
validations:
|
||||||
|
required: false
|
||||||
|
- type: textarea
|
||||||
|
id: expected
|
||||||
|
attributes:
|
||||||
|
label: 预期结果
|
||||||
|
placeholder: 服务应正常启动,页面可以打开
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: actual
|
||||||
|
attributes:
|
||||||
|
label: 实际结果
|
||||||
|
placeholder: 启动后立即退出,或页面无法访问
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: steps
|
||||||
|
attributes:
|
||||||
|
label: 复现步骤
|
||||||
|
placeholder: |
|
||||||
|
1. 下载并解压对应版本
|
||||||
|
2. 按文档启动
|
||||||
|
3. 看到上述结果
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
id: logs
|
||||||
|
attributes:
|
||||||
|
label: 脱敏后的报错日志
|
||||||
|
description: 没有日志就写「无」。不要包含令牌、密码或完整私有配置。
|
||||||
|
render: shell
|
||||||
|
placeholder: 无
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
@@ -1,495 +0,0 @@
|
|||||||
# GitHub Copilot Instructions - NetDisk Fast Download
|
|
||||||
|
|
||||||
## 项目简介
|
|
||||||
网盘快速下载项目,支持多种网盘链接解析和下载加速的 Java Web 应用。
|
|
||||||
|
|
||||||
## 技术栈要求
|
|
||||||
|
|
||||||
### 核心技术
|
|
||||||
- **Java**: JDK 17(必须)
|
|
||||||
- **框架**: Vert.x 4.5.23(异步响应式框架)
|
|
||||||
- **构建**: Maven 3.x
|
|
||||||
- **日志**: SLF4J 2.0.5 + Logback 1.5.19
|
|
||||||
- **前端**: Vue.js + Monaco Editor
|
|
||||||
|
|
||||||
### 重要依赖
|
|
||||||
- Lombok 1.18.38 - 简化 Java 代码
|
|
||||||
- Jackson 2.14.2 - JSON 处理
|
|
||||||
- Commons Lang3 3.18.0 - 工具类
|
|
||||||
- Reflections 0.10.2 - 反射工具
|
|
||||||
|
|
||||||
## 代码生成规范
|
|
||||||
|
|
||||||
### Java 代码风格
|
|
||||||
|
|
||||||
#### 1. 使用 Lombok 简化代码
|
|
||||||
```java
|
|
||||||
// ✅ 推荐:使用 Lombok 注解
|
|
||||||
@Data
|
|
||||||
@Builder
|
|
||||||
@Slf4j
|
|
||||||
public class Example {
|
|
||||||
private String name;
|
|
||||||
private int value;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ❌ 避免:手写 getter/setter
|
|
||||||
public class Example {
|
|
||||||
private String name;
|
|
||||||
public String getName() { return name; }
|
|
||||||
public void setName(String name) { this.name = name; }
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
#### 2. 异步编程模式(Vert.x)
|
|
||||||
```java
|
|
||||||
// ✅ 推荐:使用 Vert.x Future
|
|
||||||
public Future<String> fetchData() {
|
|
||||||
return vertx.createHttpClient()
|
|
||||||
.request(HttpMethod.GET, "http://example.com")
|
|
||||||
.compose(HttpClientRequest::send)
|
|
||||||
.compose(response -> response.body())
|
|
||||||
.map(Buffer::toString);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ❌ 避免:阻塞操作
|
|
||||||
public String fetchData() {
|
|
||||||
// 不要在 Event Loop 中执行阻塞代码
|
|
||||||
Thread.sleep(1000); // ❌
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
#### 3. 日志记录
|
|
||||||
```java
|
|
||||||
// ✅ 推荐:使用 @Slf4j + 参数化日志
|
|
||||||
@Slf4j
|
|
||||||
public class Service {
|
|
||||||
public void process(String id) {
|
|
||||||
log.info("Processing item: {}", id);
|
|
||||||
try {
|
|
||||||
// ...
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Failed to process item: {}", id, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ❌ 避免:字符串拼接
|
|
||||||
log.info("Processing item: " + id); // 性能差
|
|
||||||
System.out.println("Debug info"); // 不使用 System.out
|
|
||||||
```
|
|
||||||
|
|
||||||
#### 4. 异常处理
|
|
||||||
```java
|
|
||||||
// ✅ 推荐:完整的异常处理
|
|
||||||
public Future<Result> operation() {
|
|
||||||
return service.execute()
|
|
||||||
.recover(err -> {
|
|
||||||
log.error("Operation failed", err);
|
|
||||||
return Future.succeededFuture(Result.error(err.getMessage()));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// ❌ 避免:空的 catch 块或吞掉异常
|
|
||||||
try {
|
|
||||||
doSomething();
|
|
||||||
} catch (Exception e) {
|
|
||||||
// ❌ 空 catch
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 包和类命名
|
|
||||||
|
|
||||||
- 基础包名:`cn.qaiu`
|
|
||||||
- 模块包结构:
|
|
||||||
- `cn.qaiu.core.*` - 核心功能
|
|
||||||
- `cn.qaiu.parser.*` - 解析器相关
|
|
||||||
- `cn.qaiu.db.*` - 数据库相关
|
|
||||||
- `cn.qaiu.service.*` - 业务服务
|
|
||||||
- `cn.qaiu.web.*` - Web 相关
|
|
||||||
|
|
||||||
### 测试代码
|
|
||||||
|
|
||||||
```java
|
|
||||||
// ✅ 推荐:JUnit 4 测试
|
|
||||||
public class ServiceTest {
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() {
|
|
||||||
// 初始化
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testMethod() {
|
|
||||||
// Given
|
|
||||||
String input = "test";
|
|
||||||
|
|
||||||
// When
|
|
||||||
String result = service.process(input);
|
|
||||||
|
|
||||||
// Then
|
|
||||||
assertEquals("expected", result);
|
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
|
||||||
public void tearDown() {
|
|
||||||
// 清理
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## 特定模块指导
|
|
||||||
|
|
||||||
### Core 模块 - Web 路由封装(必须使用,禁止重复造轮子)
|
|
||||||
|
|
||||||
**核心思想:使用注解定义路由,框架自动处理请求和响应**
|
|
||||||
|
|
||||||
#### 1. 使用 @RouteHandler 和 @RouteMapping
|
|
||||||
```java
|
|
||||||
// ✅ 推荐:使用注解定义路由
|
|
||||||
@RouteHandler(value = "/api/v1", order = 10)
|
|
||||||
@Slf4j
|
|
||||||
public class UserController {
|
|
||||||
|
|
||||||
private final UserService userService = AsyncServiceUtil.getAsyncServiceInstance(UserService.class);
|
|
||||||
|
|
||||||
// GET /api/v1/users
|
|
||||||
@RouteMapping(value = "/users", method = RouteMethod.GET)
|
|
||||||
public Future<JsonResult<List<User>>> getUsers() {
|
|
||||||
return userService.findAll()
|
|
||||||
.map(JsonResult::success)
|
|
||||||
.otherwise(err -> JsonResult.error(err.getMessage()));
|
|
||||||
}
|
|
||||||
|
|
||||||
// GET /api/v1/user/:id (路径参数自动注入)
|
|
||||||
@RouteMapping(value = "/user/:id", method = RouteMethod.GET)
|
|
||||||
public Future<User> getUser(String id) {
|
|
||||||
// 返回值自动序列化为 JSON
|
|
||||||
return userService.findById(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
// POST /api/v1/user (查询参数自动注入)
|
|
||||||
@RouteMapping(value = "/user", method = RouteMethod.POST)
|
|
||||||
public Future<JsonResult<User>> createUser(HttpServerRequest request, String name, Integer age) {
|
|
||||||
return userService.create(name, age)
|
|
||||||
.map(JsonResult::success);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 重定向示例
|
|
||||||
@RouteMapping(value = "/redirect/:id", method = RouteMethod.GET)
|
|
||||||
public void redirect(HttpServerResponse response, String id) {
|
|
||||||
String targetUrl = "https://example.com/" + id;
|
|
||||||
ResponseUtil.redirect(response, targetUrl);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ❌ 避免:手动创建 Router 和 Handler
|
|
||||||
Router router = Router.router(vertx);
|
|
||||||
router.get("/api/users").handler(ctx -> {
|
|
||||||
// 不要这样写!使用注解方式
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
#### 2. 自动参数注入规则
|
|
||||||
- **路径参数**:`/user/:id` → `public Future<User> getUser(String id)`
|
|
||||||
- **查询参数**:`?name=xxx&age=18` → `public Future<User> create(String name, Integer age)`
|
|
||||||
- **Vert.x 对象**:自动注入 `HttpServerRequest`, `HttpServerResponse`, `RoutingContext`
|
|
||||||
- **请求体**:POST/PUT 的 JSON 自动反序列化为方法参数对象
|
|
||||||
|
|
||||||
#### 3. 响应处理
|
|
||||||
```java
|
|
||||||
// 方式1:返回 Future,框架自动处理
|
|
||||||
public Future<User> getUser(String id) {
|
|
||||||
return userService.findById(id); // 自动序列化为 JSON
|
|
||||||
}
|
|
||||||
|
|
||||||
// 方式2:返回 JsonResult 统一格式
|
|
||||||
public Future<JsonResult<User>> getUser(String id) {
|
|
||||||
return userService.findById(id).map(JsonResult::success);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 方式3:手动控制响应(仅在特殊情况使用)
|
|
||||||
public void customResponse(HttpServerResponse response) {
|
|
||||||
ResponseUtil.fireJsonObjectResponse(response, jsonObject);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
#### 4. WebSocket 路由
|
|
||||||
```java
|
|
||||||
@RouteHandler("/ws")
|
|
||||||
public class WebSocketHandler {
|
|
||||||
|
|
||||||
@SockRouteMapper("/chat")
|
|
||||||
public void handleChat(SockJSSocket socket) {
|
|
||||||
socket.handler(buffer -> {
|
|
||||||
log.info("Received: {}", buffer.toString());
|
|
||||||
socket.write(buffer); // Echo
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Core-Database 模块 - DDL 自动生成(必须使用,禁止重复造轮子)
|
|
||||||
|
|
||||||
**核心思想:使用注解定义实体,自动生成建表 SQL**
|
|
||||||
|
|
||||||
#### 1. 定义实体类
|
|
||||||
```java
|
|
||||||
// ✅ 推荐:使用注解定义实体
|
|
||||||
@Data
|
|
||||||
@Table(value = "t_user", keyFields = "id") // 表名和主键
|
|
||||||
public class User {
|
|
||||||
|
|
||||||
@Constraint(autoIncrement = true)
|
|
||||||
private Long id; // 主键自增
|
|
||||||
|
|
||||||
@Constraint(notNull = true, uniqueKey = "uk_email")
|
|
||||||
@Length(varcharSize = 100)
|
|
||||||
private String email; // 非空 + 唯一索引 + 长度100
|
|
||||||
|
|
||||||
@Constraint(notNull = true)
|
|
||||||
@Length(varcharSize = 50)
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
@Constraint(defaultValue = "0")
|
|
||||||
private Integer status; // 默认值 0
|
|
||||||
|
|
||||||
@Constraint(defaultValue = "NOW()", defaultValueIsFunction = true)
|
|
||||||
private Date createdAt; // 默认当前时间
|
|
||||||
|
|
||||||
@TableGenIgnore // 忽略此字段,不生成列
|
|
||||||
private transient String tempField;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 应用启动时自动建表
|
|
||||||
CreateTable.createTable(pool, JDBCType.MySQL);
|
|
||||||
|
|
||||||
// ❌ 避免:手写建表 SQL
|
|
||||||
String sql = "CREATE TABLE t_user (id BIGINT AUTO_INCREMENT PRIMARY KEY, ...)";
|
|
||||||
pool.query(sql).execute(); // 不要这样写!
|
|
||||||
```
|
|
||||||
|
|
||||||
#### 2. 支持的注解
|
|
||||||
|
|
||||||
**@Table** - 表定义
|
|
||||||
- `value` - 表名(默认类名转下划线)
|
|
||||||
- `keyFields` - 主键字段名(默认 "id")
|
|
||||||
|
|
||||||
**@Constraint** - 字段约束
|
|
||||||
- `notNull = true` - 非空约束
|
|
||||||
- `uniqueKey = "uk_name"` - 唯一索引(相同名称的字段组成联合唯一索引)
|
|
||||||
- `defaultValue = "value"` - 默认值
|
|
||||||
- `defaultValueIsFunction = true` - 默认值是函数(如 NOW())
|
|
||||||
- `autoIncrement = true` - 自增(仅用于主键)
|
|
||||||
|
|
||||||
**@Length** - 字段长度
|
|
||||||
- `varcharSize = 255` - VARCHAR 长度(默认 255)
|
|
||||||
- `decimalSize = {10, 2}` - DECIMAL 精度(默认 {22, 2})
|
|
||||||
|
|
||||||
**@Column** - 自定义列名
|
|
||||||
- `name = "column_name"` - 指定数据库列名
|
|
||||||
|
|
||||||
**@TableGenIgnore** - 忽略字段(不生成列)
|
|
||||||
|
|
||||||
#### 3. 自动创建数据库
|
|
||||||
```java
|
|
||||||
// ✅ 推荐:自动创建数据库
|
|
||||||
JsonObject dbConfig = new JsonObject()
|
|
||||||
.put("jdbcUrl", "jdbc:mysql://localhost:3306/mydb")
|
|
||||||
.put("username", "root")
|
|
||||||
.put("password", "password");
|
|
||||||
|
|
||||||
CreateDatabase.createDatabase(dbConfig);
|
|
||||||
|
|
||||||
// ❌ 避免:手动连接和执行 CREATE DATABASE
|
|
||||||
```
|
|
||||||
|
|
||||||
#### 4. 支持的数据库类型
|
|
||||||
- `JDBCType.MySQL` - MySQL
|
|
||||||
- `JDBCType.PostgreSQL` - PostgreSQL
|
|
||||||
- `JDBCType.H2DB` - H2 数据库
|
|
||||||
|
|
||||||
### Parser 模块
|
|
||||||
- 支持自定义解析器(Java/Python/JavaScript)
|
|
||||||
- Python 使用 GraalPy 执行
|
|
||||||
- 需要考虑安全性和沙箱隔离
|
|
||||||
- WebSocket 支持外部 Python 环境连接
|
|
||||||
|
|
||||||
```java
|
|
||||||
// Parser 接口实现示例
|
|
||||||
public class CustomParser implements IParser {
|
|
||||||
@Override
|
|
||||||
public Future<ParseResult> parse(String url, Map<String, String> params) {
|
|
||||||
return Future.future(promise -> {
|
|
||||||
// 异步解析逻辑
|
|
||||||
promise.complete(result);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Maven 配置注意事项
|
|
||||||
|
|
||||||
### 测试执行
|
|
||||||
```bash
|
|
||||||
# 默认打包跳过测试
|
|
||||||
mvn clean package
|
|
||||||
|
|
||||||
# 执行测试
|
|
||||||
mvn test -Dmaven.test.skip=false
|
|
||||||
mvn clean package -Dmaven.test.skip=false
|
|
||||||
```
|
|
||||||
|
|
||||||
### 模块化构建
|
|
||||||
```bash
|
|
||||||
# 构建特定模块
|
|
||||||
mvn clean package -pl parser -am
|
|
||||||
```
|
|
||||||
|
|
||||||
## 重要约定
|
|
||||||
|
|
||||||
### 1. 异步优先
|
|
||||||
- 所有 I/O 操作必须异步
|
|
||||||
- 使用 Vert.x Future/Promise API
|
|
||||||
- 避免阻塞 Event Loop
|
|
||||||
|
|
||||||
### 2. 资源管理
|
|
||||||
```java
|
|
||||||
// ✅ 推荐:使用 try-with-resources
|
|
||||||
try (InputStream is = new FileInputStream(file)) {
|
|
||||||
// 使用资源
|
|
||||||
}
|
|
||||||
|
|
||||||
// 或者确保在 finally 中关闭
|
|
||||||
HttpClient client = vertx.createHttpClient();
|
|
||||||
// 使用后必须关闭
|
|
||||||
client.close();
|
|
||||||
```
|
|
||||||
|
|
||||||
### 3. 配置外部化
|
|
||||||
- 配置文件优先使用 JSON 格式
|
|
||||||
- 敏感信息不要硬编码
|
|
||||||
- 支持环境变量覆盖
|
|
||||||
|
|
||||||
### 4. 错误处理
|
|
||||||
- 使用 Future 的 recover/otherwise
|
|
||||||
- 记录详细的错误日志
|
|
||||||
- 向用户返回友好的错误信息
|
|
||||||
|
|
||||||
## 性能考虑
|
|
||||||
|
|
||||||
1. **使用连接池**: 数据库连接、HTTP 客户端
|
|
||||||
2. **缓存策略**: 解析结果、静态资源
|
|
||||||
3. **批量操作**: 避免 N+1 查询问题
|
|
||||||
4. **异步非阻塞**: 充分利用 Vert.x 优势
|
|
||||||
|
|
||||||
## 安全要求
|
|
||||||
|
|
||||||
### Parser 模块安全
|
|
||||||
- 执行自定义代码必须沙箱隔离
|
|
||||||
- 限制资源访问(文件、网络)
|
|
||||||
- 设置执行超时
|
|
||||||
- 验证输入参数
|
|
||||||
|
|
||||||
```java
|
|
||||||
// ✅ 推荐:带安全检查的执行
|
|
||||||
public Future<Result> executeUserCode(String code) {
|
|
||||||
// 验证代码
|
|
||||||
if (!SecurityValidator.isValid(code)) {
|
|
||||||
return Future.failedFuture("Invalid code");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 在沙箱中执行
|
|
||||||
return sandboxExecutor.execute(code, TIMEOUT);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 输入验证
|
|
||||||
```java
|
|
||||||
// ✅ 推荐:验证所有外部输入
|
|
||||||
public Future<Result> parse(String url) {
|
|
||||||
if (StringUtils.isBlank(url) || !UrlValidator.isValid(url)) {
|
|
||||||
return Future.failedFuture("Invalid URL");
|
|
||||||
}
|
|
||||||
// 继续处理
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## 文档和注释
|
|
||||||
|
|
||||||
### JavaDoc 注释
|
|
||||||
```java
|
|
||||||
/**
|
|
||||||
* 解析网盘链接获取下载信息
|
|
||||||
*
|
|
||||||
* @param url 网盘分享链接
|
|
||||||
* @param params 额外参数(如密码)
|
|
||||||
* @return Future<ParseResult> 解析结果
|
|
||||||
*/
|
|
||||||
public Future<ParseResult> parse(String url, Map<String, String> params) {
|
|
||||||
// 实现
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### 复杂逻辑注释
|
|
||||||
```java
|
|
||||||
// 处理特殊情况:某些网盘需要二次验证
|
|
||||||
// 参考文档:docs/parser-flow.md
|
|
||||||
if (needsSecondaryVerification) {
|
|
||||||
// 实现二次验证逻辑
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## 常见模式
|
|
||||||
|
|
||||||
### 链式异步调用
|
|
||||||
```java
|
|
||||||
return fetchMetadata(url)
|
|
||||||
.compose(meta -> validateMetadata(meta))
|
|
||||||
.compose(meta -> fetchDownloadUrl(meta))
|
|
||||||
.compose(downloadUrl -> generateResult(downloadUrl))
|
|
||||||
.recover(this::handleError);
|
|
||||||
```
|
|
||||||
|
|
||||||
### 事件处理
|
|
||||||
```java
|
|
||||||
vertx.eventBus().<JsonObject>consumer("parser.request", msg -> {
|
|
||||||
JsonObject body = msg.body();
|
|
||||||
parse(body.getString("url"))
|
|
||||||
.onSuccess(result -> msg.reply(JsonObject.mapFrom(result)))
|
|
||||||
.onFailure(err -> msg.fail(500, err.getMessage()));
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
## 不应该做的事
|
|
||||||
|
|
||||||
1. ❌ 在 Event Loop 线程中执行阻塞操作
|
|
||||||
2. ❌ 使用 `System.out.println()` 而不是日志框架
|
|
||||||
3. ❌ 硬编码配置值(端口、路径、密钥等)
|
|
||||||
4. ❌ 忽略异常或使用空 catch 块
|
|
||||||
5. ❌ 返回 null,应该使用 Optional 或 Future.failedFuture()
|
|
||||||
6. ❌ 在生产代码中使用 `e.printStackTrace()`
|
|
||||||
7. ❌ 直接操作 Thread 而不使用 Vert.x 的 executeBlocking
|
|
||||||
8. ❌ 提交包含 `logs/` 目录的代码
|
|
||||||
|
|
||||||
## 代码审查清单
|
|
||||||
|
|
||||||
生成代码时请确保:
|
|
||||||
- [ ] 使用 Lombok 注解简化代码
|
|
||||||
- [ ] 异步操作使用 Vert.x Future
|
|
||||||
- [ ] 添加了 @Slf4j 和适当的日志
|
|
||||||
- [ ] 异常处理完整
|
|
||||||
- [ ] 输入参数已验证
|
|
||||||
- [ ] 资源正确释放
|
|
||||||
- [ ] 添加了必要的 JavaDoc
|
|
||||||
- [ ] 遵循项目包命名规范
|
|
||||||
- [ ] 没有阻塞操作在 Event Loop 中
|
|
||||||
- [ ] 测试用例覆盖主要场景
|
|
||||||
|
|
||||||
## 参考资源
|
|
||||||
|
|
||||||
- Vert.x 文档: https://vertx.io/docs/
|
|
||||||
- 项目 Parser 文档: `parser/doc/`
|
|
||||||
- 前端文档: `web-front/doc/`
|
|
||||||
- 安全测试指南: `parser/doc/SECURITY_TESTING_GUIDE.md`
|
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
## v0.4.15
|
||||||
|
|
||||||
|
- 浏览器与服务端以相同的 UTF-8 密钥字节处理临时认证,兼容 16、24 和 32 字节的密钥。
|
||||||
|
- 临时认证不再使用发行包里的公共密钥。未手动配置的服务实例每次启动生成随机密钥,只记录来源和指纹。保留旧发行包配置的实例也会自动迁移。
|
||||||
|
- 捐赠池默认关闭。显式启用后,受支持的网盘使用服务端选号,不再向前端提供捐赠账号。已保存的捐赠记录不会删除。
|
||||||
|
- 清理网页和文档中的公共密钥。需要网页个人认证、外部客户端或多实例共享密钥的自建站,须显式配置和安全分发 `server.authEncryptKey`。
|
||||||
|
|
||||||
|
### 升级提醒
|
||||||
|
|
||||||
|
旧公共密钥生成的 `auth` 参数不再兼容。升级后请核对部署配置,并视部署情况清理过往解析缓存和轮换捐赠凭据。
|
||||||
@@ -35,11 +35,11 @@ jobs:
|
|||||||
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
|
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
|
||||||
restore-keys: ${{ runner.os }}-m2
|
restore-keys: ${{ runner.os }}-m2
|
||||||
|
|
||||||
- name: 安装 GraalPy pip 包
|
- name: 编译项目
|
||||||
run: |
|
run: ./mvnw clean compile
|
||||||
cd parser
|
|
||||||
chmod +x setup-graalpy-packages.sh
|
|
||||||
./setup-graalpy-packages.sh
|
|
||||||
|
|
||||||
- name: 编译并打包项目
|
# - name: 运行测试
|
||||||
run: ./mvnw clean package -DskipTests
|
# run: ./mvnw test
|
||||||
|
|
||||||
|
- name: 打包项目
|
||||||
|
run: ./mvnw package -DskipTests
|
||||||
+246
-62
@@ -1,24 +1,17 @@
|
|||||||
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
|
name: Java CI(Maven 构建 + Docker 镜像 + 原生环境打包)
|
||||||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven
|
|
||||||
|
|
||||||
# This workflow uses actions that are not certified by GitHub.
|
|
||||||
# They are provided by a third-party and are governed by
|
|
||||||
# separate terms of service, privacy policy, and support
|
|
||||||
# documentation.
|
|
||||||
|
|
||||||
name: Java CI with Maven
|
|
||||||
|
|
||||||
# The API requires write permission on the repository to submit dependencies
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: write
|
contents: write
|
||||||
packages: write
|
packages: write
|
||||||
|
|
||||||
on:
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
push:
|
push:
|
||||||
tags:
|
tags:
|
||||||
- '*' # 只有推送tag时才会触发构建
|
- 'v*'
|
||||||
|
# '*' 不匹配含 / 的分支名(如 cursor/xxx),会让功能分支推送也发布 :latest 镜像
|
||||||
branches-ignore:
|
branches-ignore:
|
||||||
- '*' # 排除所有分支的提交
|
- '**'
|
||||||
paths-ignore:
|
paths-ignore:
|
||||||
- 'bin/**'
|
- 'bin/**'
|
||||||
- '.github/**'
|
- '.github/**'
|
||||||
@@ -32,93 +25,284 @@ on:
|
|||||||
- "main"
|
- "main"
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
|
# ================================================================
|
||||||
|
# 阶段一:构建前端 + Maven 打包(只执行一次,产物共享)
|
||||||
|
# ================================================================
|
||||||
build:
|
build:
|
||||||
|
name: 编译构建
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- name: 检出代码
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
- uses: actions/setup-node@v4
|
- name: 设置 Node.js 18
|
||||||
|
uses: actions/setup-node@v4
|
||||||
with:
|
with:
|
||||||
node-version: '18'
|
node-version: '18'
|
||||||
|
|
||||||
- name: Set up JDK 17
|
- name: 设置 JDK 17
|
||||||
uses: actions/setup-java@v3
|
uses: actions/setup-java@v3
|
||||||
with:
|
with:
|
||||||
java-version: '17'
|
java-version: '17'
|
||||||
distribution: 'temurin'
|
distribution: 'temurin'
|
||||||
cache: maven
|
cache: maven
|
||||||
|
|
||||||
- name: Build Frontend
|
- name: 构建前端
|
||||||
run: cd web-front && yarn install && yarn run build
|
run: cd web-front && yarn install && yarn run build
|
||||||
|
|
||||||
- name: Install GraalPy pip packages (for Python tags)
|
- name: Maven 编译打包
|
||||||
if: contains(github.ref, 'py')
|
|
||||||
run: |
|
|
||||||
cd parser
|
|
||||||
chmod +x setup-graalpy-packages.sh
|
|
||||||
./setup-graalpy-packages.sh
|
|
||||||
|
|
||||||
- name: Build with Maven
|
|
||||||
run: mvn -B package -DskipTests --file pom.xml
|
run: mvn -B package -DskipTests --file pom.xml
|
||||||
|
|
||||||
# Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
|
- name: Base64 兼容性回归测试
|
||||||
- name: Update dependency graph
|
if: github.event_name == 'pull_request'
|
||||||
|
run: >-
|
||||||
|
mvn -B test -DskipTests=false
|
||||||
|
-Dtest=Base64DecoderTest,HttpProxyBase64Test,Base64CompatibilityTest
|
||||||
|
-Dsurefire.failIfNoSpecifiedTests=false
|
||||||
|
-DargLine=-XX:-UseBASE64Intrinsics
|
||||||
|
--file pom.xml
|
||||||
|
|
||||||
|
- name: Base64 SIMD 兼容性回归测试
|
||||||
|
if: github.event_name == 'pull_request'
|
||||||
|
run: >-
|
||||||
|
mvn -B test -DskipTests=false
|
||||||
|
-Dtest=Base64DecoderTest,HttpProxyBase64Test,Base64CompatibilityTest
|
||||||
|
-Dsurefire.failIfNoSpecifiedTests=false
|
||||||
|
"-DargLine=--add-modules=jdk.incubator.vector -XX:-UseBASE64Intrinsics -Dnfd.test.requireVector=true"
|
||||||
|
--file pom.xml
|
||||||
|
|
||||||
|
- name: 更新依赖图谱
|
||||||
uses: advanced-security/maven-dependency-submission-action@v3
|
uses: advanced-security/maven-dependency-submission-action@v3
|
||||||
if: github.event_name != 'pull_request'
|
if: github.event_name != 'pull_request'
|
||||||
|
continue-on-error: true
|
||||||
with:
|
with:
|
||||||
ignore-maven-wrapper: true
|
ignore-maven-wrapper: true
|
||||||
|
|
||||||
# - uses: release-drafter/release-drafter@v5
|
- name: 分享应用打包目录(供原生包和 Docker 复用)
|
||||||
# env:
|
if: github.event_name != 'pull_request'
|
||||||
# GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
|
|
||||||
|
|
||||||
- name: Upload Artifact
|
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
|
name: app-package
|
||||||
|
path: web-service/target/package/
|
||||||
|
|
||||||
|
- name: 分享 bin-zip(供 Docker 复用)
|
||||||
|
if: github.event_name != 'pull_request'
|
||||||
|
uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: app-bin-zip
|
||||||
path: web-service/target/netdisk-fast-download-bin.zip
|
path: web-service/target/netdisk-fast-download-bin.zip
|
||||||
|
|
||||||
- name: Login to GitHub Container Registry
|
# ================================================================
|
||||||
|
# 阶段二-A:Docker 镜像构建(并行)
|
||||||
|
# ================================================================
|
||||||
|
docker:
|
||||||
|
name: Docker 镜像
|
||||||
|
needs: build
|
||||||
if: github.event_name != 'pull_request'
|
if: github.event_name != 'pull_request'
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: 检出代码
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: 下载 bin-zip 产物
|
||||||
|
uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
name: app-bin-zip
|
||||||
|
path: web-service/target/
|
||||||
|
|
||||||
|
- name: 登录 GitHub 容器仓库
|
||||||
uses: docker/login-action@v3
|
uses: docker/login-action@v3
|
||||||
with:
|
with:
|
||||||
registry: ghcr.io
|
registry: ghcr.io
|
||||||
username: ${{ github.actor }}
|
username: ${{ github.actor }}
|
||||||
password: ${{ secrets.GITHUB_TOKEN }}
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
- name: 设置 QEMU(多平台构建支持)
|
||||||
|
uses: docker/setup-qemu-action@v3
|
||||||
|
|
||||||
|
- name: 设置 Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
- name: Extract git tag
|
- name: 生成 Docker 标签
|
||||||
id: tag
|
id: docker_tag
|
||||||
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
GIT_TAG=$(git tag --points-at HEAD | head -n 1)
|
tag="$(printf '%s' "${GITHUB_REF_NAME}" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9_.-]+/-/g; s/^-+//; s/-+$//')"
|
||||||
echo "tag=$GIT_TAG" >> $GITHUB_OUTPUT
|
if [ -z "$tag" ]; then
|
||||||
# 检查是否为 Python 版本标签(以 py 结尾)
|
tag="snapshot"
|
||||||
if [[ "$GIT_TAG" == *py ]]; then
|
fi
|
||||||
echo "is_python=true" >> $GITHUB_OUTPUT
|
echo "tag=$tag" >> "$GITHUB_OUTPUT"
|
||||||
else
|
|
||||||
echo "is_python=false" >> $GITHUB_OUTPUT
|
- name: 构建并推送 Docker 镜像
|
||||||
|
uses: docker/build-push-action@v5
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
push: true
|
||||||
|
platforms: linux/amd64,linux/arm64,linux/arm/v7
|
||||||
|
tags: |
|
||||||
|
ghcr.io/${{ github.repository }}:${{ steps.docker_tag.outputs.tag }}
|
||||||
|
ghcr.io/${{ github.repository }}:latest
|
||||||
|
|
||||||
|
|
||||||
|
# ================================================================
|
||||||
|
# 阶段二-B:原生环境打包 Linux + Windows(并行)
|
||||||
|
# ================================================================
|
||||||
|
native-package:
|
||||||
|
name: 原生环境打包 → ${{ matrix.artifact-name }}
|
||||||
|
needs: build
|
||||||
|
if: github.event_name != 'pull_request'
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
include:
|
||||||
|
- os: ubuntu-latest
|
||||||
|
artifact-name: netdisk-fast-download-linux-amd64
|
||||||
|
- os: windows-latest
|
||||||
|
artifact-name: netdisk-fast-download-windows-amd64
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
defaults:
|
||||||
|
run:
|
||||||
|
shell: bash
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: 设置 JDK 17
|
||||||
|
uses: actions/setup-java@v3
|
||||||
|
with:
|
||||||
|
java-version: '17'
|
||||||
|
distribution: 'temurin'
|
||||||
|
|
||||||
|
- name: 下载 Maven 构建产物
|
||||||
|
uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
name: app-package
|
||||||
|
path: web-service/target/package
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# 选择 JDK 模块
|
||||||
|
# ============================================================
|
||||||
|
- name: 选择完整 JDK 模块集合
|
||||||
|
run: |
|
||||||
|
# 123 网盘/自定义 JS 解析器会通过 javax.script、Nashorn 和 SPI/反射加载模块,
|
||||||
|
# jdeps 无法稳定识别这些运行时依赖。这里优先保证原生包可运行,使用完整
|
||||||
|
# JDK 模块集合生成运行时,避免 Windows 精简 JRE 缺 java.scripting/jdk.dynalink 等模块。
|
||||||
|
printf 'JDK_MODULES=ALL-MODULE-PATH\n' >> $GITHUB_ENV
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# jlink 生成完整模块 JRE
|
||||||
|
# ============================================================
|
||||||
|
- name: 生成完整模块 JRE(jlink)
|
||||||
|
run: |
|
||||||
|
jlink \
|
||||||
|
--module-path "$JAVA_HOME/jmods" \
|
||||||
|
--add-modules "$JDK_MODULES" \
|
||||||
|
--output "native-package/netdisk-fast-download/jre" \
|
||||||
|
--strip-debug \
|
||||||
|
--compress=2 \
|
||||||
|
--no-header-files \
|
||||||
|
--no-man-pages
|
||||||
|
|
||||||
|
echo "JRE size:"
|
||||||
|
du -sh native-package/netdisk-fast-download/jre || true
|
||||||
|
|
||||||
|
# Windows: 确保 MSVC 运行时 DLL 到位
|
||||||
|
if [[ "$RUNNER_OS" == "Windows" ]]; then
|
||||||
|
JRE_BIN="native-package/netdisk-fast-download/jre/bin"
|
||||||
|
for dll in vcruntime140.dll msvcp140.dll vcruntime140_1.dll; do
|
||||||
|
if [ ! -f "$JRE_BIN/$dll" ] && [ -f "$JAVA_HOME/bin/$dll" ]; then
|
||||||
|
echo "jlink 未包含 $dll,从 JDK 补拷"
|
||||||
|
cp "$JAVA_HOME/bin/$dll" "$JRE_BIN/"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
echo "=== JRE bin 目录 DLL 清单 ==="
|
||||||
|
ls -la "$JRE_BIN"/*.dll 2>/dev/null || echo "(无 .dll 文件)"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
- name: Build and push Docker image (Standard)
|
echo "=== 关键 JDK 模块校验 ==="
|
||||||
if: github.event_name != 'pull_request' && steps.tag.outputs.is_python == 'false'
|
"native-package/netdisk-fast-download/jre/bin/java" --list-modules | grep -q '^java.scripting@'
|
||||||
uses: docker/build-push-action@v5
|
"native-package/netdisk-fast-download/jre/bin/java" --list-modules | grep -q '^jdk.dynalink@'
|
||||||
with:
|
"native-package/netdisk-fast-download/jre/bin/java" --list-modules | grep -q '^jdk.unsupported@'
|
||||||
context: .
|
|
||||||
push: true
|
|
||||||
platforms: linux/amd64,linux/arm64,linux/arm/v7
|
|
||||||
tags: |
|
|
||||||
ghcr.io/qaiu/netdisk-fast-download:${{ steps.tag.outputs.tag }}
|
|
||||||
ghcr.io/qaiu/netdisk-fast-download:latest
|
|
||||||
|
|
||||||
- name: Build and push Docker image (Python)
|
# ============================================================
|
||||||
if: github.event_name != 'pull_request' && steps.tag.outputs.is_python == 'true'
|
# 组装包目录
|
||||||
uses: docker/build-push-action@v5
|
# ============================================================
|
||||||
|
- name: 组装包目录
|
||||||
|
run: |
|
||||||
|
PKG="native-package/netdisk-fast-download"
|
||||||
|
SRC="web-service/target/package"
|
||||||
|
|
||||||
|
cp "$SRC/netdisk-fast-download.jar" "$PKG/"
|
||||||
|
cp -r "$SRC/lib" "$PKG/"
|
||||||
|
cp -r "$SRC/resources" "$PKG/"
|
||||||
|
cp -r "$SRC/webroot" "$PKG/"
|
||||||
|
|
||||||
|
mkdir -p "$PKG/db"
|
||||||
|
mkdir -p "$PKG/logs"
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# 生成启动脚本
|
||||||
|
# ============================================================
|
||||||
|
- name: 生成启动脚本(Linux)
|
||||||
|
run: |
|
||||||
|
PKG="native-package/netdisk-fast-download"
|
||||||
|
echo '#!/bin/bash' > "$PKG/run.sh"
|
||||||
|
echo 'DIR="$(cd "$(dirname "$0")" && pwd)"' >> "$PKG/run.sh"
|
||||||
|
echo 'cd "$DIR" || exit 1' >> "$PKG/run.sh"
|
||||||
|
echo 'exec "$DIR/jre/bin/java" -Xmx512M -Dfile.encoding=utf-8 -jar "$DIR/netdisk-fast-download.jar" "$@"' >> "$PKG/run.sh"
|
||||||
|
chmod +x "$PKG/run.sh"
|
||||||
|
|
||||||
|
- name: 生成启动脚本(Windows)
|
||||||
|
run: |
|
||||||
|
PKG="native-package/netdisk-fast-download"
|
||||||
|
echo '@echo off' > "$PKG/run.bat"
|
||||||
|
echo 'chcp 65001 > nul' >> "$PKG/run.bat"
|
||||||
|
echo 'pushd %~dp0' >> "$PKG/run.bat"
|
||||||
|
echo '"%~dp0jre\bin\java.exe" -Xmx512M -Dfile.encoding=utf-8 -jar "%~dp0netdisk-fast-download.jar" %*' >> "$PKG/run.bat"
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# 打包为 zip
|
||||||
|
# ============================================================
|
||||||
|
- name: 打包 ZIP(Linux)
|
||||||
|
if: runner.os == 'Linux'
|
||||||
|
run: |
|
||||||
|
cd native-package
|
||||||
|
zip -r "../${{ matrix.artifact-name }}.zip" netdisk-fast-download/
|
||||||
|
|
||||||
|
- name: 打包 ZIP(Windows)
|
||||||
|
if: runner.os == 'Windows'
|
||||||
|
shell: pwsh
|
||||||
|
run: |
|
||||||
|
Compress-Archive -Path native-package/netdisk-fast-download -DestinationPath "${{ matrix.artifact-name }}.zip"
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# 上传产物
|
||||||
|
# ============================================================
|
||||||
|
- name: 上传原生安装包
|
||||||
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
context: .
|
name: ${{ matrix.artifact-name }}
|
||||||
push: true
|
path: ${{ matrix.artifact-name }}.zip
|
||||||
platforms: linux/amd64,linux/arm64,linux/arm/v7
|
|
||||||
tags: |
|
# ================================================================
|
||||||
ghcr.io/qaiu/netdisk-fast-download:${{ steps.tag.outputs.tag }}
|
# 阶段三:创建 GitHub Release(只跑一次,避免矩阵任务重复拼接更新日志)
|
||||||
|
# ================================================================
|
||||||
|
publish-release:
|
||||||
|
name: 发布 GitHub Release
|
||||||
|
needs: native-package
|
||||||
|
if: github.event_name != 'pull_request' && github.ref_type == 'tag'
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: 下载原生安装包
|
||||||
|
uses: actions/download-artifact@v4
|
||||||
|
with:
|
||||||
|
pattern: netdisk-fast-download-*
|
||||||
|
merge-multiple: true
|
||||||
|
|
||||||
|
- name: 创建 Release
|
||||||
|
uses: softprops/action-gh-release@v2
|
||||||
|
with:
|
||||||
|
files: |
|
||||||
|
netdisk-fast-download-linux-amd64.zip
|
||||||
|
netdisk-fast-download-windows-amd64.zip
|
||||||
|
tag_name: ${{ github.ref_name }}
|
||||||
|
body_path: .github/release-notes.md
|
||||||
|
generate_release_notes: true
|
||||||
|
fail_on_unmatched_files: true
|
||||||
+12
-3
@@ -31,6 +31,7 @@ target/
|
|||||||
sdkTest.log
|
sdkTest.log
|
||||||
app.yml
|
app.yml
|
||||||
app-local.yml
|
app-local.yml
|
||||||
|
secret.yml
|
||||||
|
|
||||||
|
|
||||||
#some local files
|
#some local files
|
||||||
@@ -41,7 +42,9 @@ gradlew.bat
|
|||||||
unused.txt
|
unused.txt
|
||||||
/web-service/src/main/generated/
|
/web-service/src/main/generated/
|
||||||
/db
|
/db
|
||||||
|
/netdisk-fast-download/
|
||||||
/webroot/nfd-front/
|
/webroot/nfd-front/
|
||||||
|
/netdisk-fast-download/webroot/nfd-front/
|
||||||
package-lock.json
|
package-lock.json
|
||||||
|
|
||||||
# Maven generated files
|
# Maven generated files
|
||||||
@@ -81,6 +84,12 @@ yarn-error.log*
|
|||||||
*.ipr
|
*.ipr
|
||||||
*.iws
|
*.iws
|
||||||
|
|
||||||
# GraalPy pip packages (local installation)
|
# Build directories
|
||||||
parser/src/main/resources/graalpy-packages/
|
**/target/
|
||||||
**/graalpy-packages/
|
**/build/
|
||||||
|
**/classes/
|
||||||
|
**/out/
|
||||||
|
**/${project.build.directory}/
|
||||||
|
**/${project.basedir}/target/
|
||||||
|
**/${basedir}/target/
|
||||||
|
.spec-workflow/
|
||||||
Vendored
-59
@@ -1,77 +1,18 @@
|
|||||||
{
|
{
|
||||||
// 使用 IntelliSense 了解相关属性。
|
|
||||||
// 悬停以查看现有属性的描述。
|
|
||||||
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
|
|
||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
|
||||||
"type": "java",
|
|
||||||
"name": "PythonSecurityTestMain",
|
|
||||||
"request": "launch",
|
|
||||||
"mainClass": "cn.qaiu.parser.custompy.PythonSecurityTestMain",
|
|
||||||
"projectName": "parser"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"type": "java",
|
"type": "java",
|
||||||
"name": "Current File",
|
"name": "Current File",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"mainClass": "${file}"
|
"mainClass": "${file}"
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"type": "java",
|
|
||||||
"name": "StringCase",
|
|
||||||
"request": "launch",
|
|
||||||
"mainClass": "cn.qaiu.vx.core.util.StringCase",
|
|
||||||
"projectName": "core"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "java",
|
|
||||||
"name": "FCURLParser",
|
|
||||||
"request": "launch",
|
|
||||||
"mainClass": "cn.qaiu.parser.FCURLParser",
|
|
||||||
"projectName": "parser"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "java",
|
|
||||||
"name": "QkTool",
|
|
||||||
"request": "launch",
|
|
||||||
"mainClass": "cn.qaiu.parser.impl.QkTool",
|
|
||||||
"projectName": "parser"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "java",
|
|
||||||
"name": "WebClientExample",
|
|
||||||
"request": "launch",
|
|
||||||
"mainClass": "qaiu.web.test.WebClientExample",
|
|
||||||
"projectName": "parser"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"type": "java",
|
"type": "java",
|
||||||
"name": "AppMain",
|
"name": "AppMain",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"mainClass": "cn.qaiu.lz.AppMain",
|
"mainClass": "cn.qaiu.lz.AppMain",
|
||||||
"projectName": "web-service"
|
"projectName": "web-service"
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "java",
|
|
||||||
"name": "TestJs",
|
|
||||||
"request": "launch",
|
|
||||||
"mainClass": "cn.qaiu.web.test.TestJs",
|
|
||||||
"projectName": "web-service"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "java",
|
|
||||||
"name": "TestOS",
|
|
||||||
"request": "launch",
|
|
||||||
"mainClass": "cn.qaiu.web.test.TestOS",
|
|
||||||
"projectName": "web-service"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "java",
|
|
||||||
"name": "WebProxyExamples",
|
|
||||||
"request": "launch",
|
|
||||||
"mainClass": "cn.qaiu.web.test.WebProxyExamples",
|
|
||||||
"projectName": "web-service"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
Vendored
+2
-1
@@ -1,4 +1,5 @@
|
|||||||
{
|
{
|
||||||
"java.compile.nullAnalysis.mode": "automatic",
|
"java.compile.nullAnalysis.mode": "automatic",
|
||||||
"java.configuration.updateBuildConfiguration": "automatic"
|
"java.configuration.updateBuildConfiguration": "interactive",
|
||||||
|
"java.debug.settings.onBuildFailureProceed": true
|
||||||
}
|
}
|
||||||
+10
-4
@@ -1,4 +1,5 @@
|
|||||||
FROM eclipse-temurin:17-jre
|
ARG JAVA_RUNTIME_IMAGE=eclipse-temurin:17-jre
|
||||||
|
FROM ${JAVA_RUNTIME_IMAGE}
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
@@ -10,8 +11,13 @@ COPY ./web-service/target/netdisk-fast-download-bin.zip .
|
|||||||
RUN unzip netdisk-fast-download-bin.zip && \
|
RUN unzip netdisk-fast-download-bin.zip && \
|
||||||
mv netdisk-fast-download/* ./ && \
|
mv netdisk-fast-download/* ./ && \
|
||||||
rm netdisk-fast-download-bin.zip && \
|
rm netdisk-fast-download-bin.zip && \
|
||||||
chmod +x run.sh
|
chmod +x run.sh && \
|
||||||
|
mkdir -p db logs
|
||||||
|
|
||||||
EXPOSE 6400 6401
|
COPY ./docker-entrypoint.sh /docker-entrypoint.sh
|
||||||
|
RUN chmod +x /docker-entrypoint.sh
|
||||||
|
|
||||||
ENTRYPOINT ["sh", "run.sh"]
|
EXPOSE 6401
|
||||||
|
|
||||||
|
RUN addgroup --system appgroup && adduser --system --ingroup appgroup appuser
|
||||||
|
ENTRYPOINT ["/docker-entrypoint.sh"]
|
||||||
@@ -1,86 +1,76 @@
|
|||||||
<p align="center">
|
# 一款网盘分享链接云解析快速下载服务
|
||||||
<img src="https://github.com/user-attachments/assets/87401aae-b0b6-4ffb-bbeb-44756404d26f" alt="项目预览图" />
|
|
||||||
</p>
|
|
||||||
|
|
||||||
|
> [!TIP]
|
||||||
|
> 如果在线解析失败,可以试试 [NFD Client](https://github.com/qaiu/nfd-client-parser),在本机解析网盘分享链接。[下载安装包](https://github.com/qaiu/nfd-client-parser/releases/latest)
|
||||||
|
|
||||||
|
QQ交流群:1017480890
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://github.com/qaiu/netdisk-fast-download/actions/workflows/maven.yml"><img src="https://img.shields.io/github/actions/workflow/status/qaiu/netdisk-fast-download/maven.yml?branch=v0.1.9b8a&style=flat"></a>
|
<a href="https://github.com/qaiu/netdisk-fast-download/actions/workflows/maven.yml"><img src="https://img.shields.io/github/actions/workflow/status/qaiu/netdisk-fast-download/build.yml?branch=main&style=flat"></a>
|
||||||
<a href="https://www.oracle.com/cn/java/technologies/downloads"><img src="https://img.shields.io/badge/jdk-%3E%3D17-blue"></a>
|
<a href="https://www.oracle.com/cn/java/technologies/downloads"><img src="https://img.shields.io/badge/jdk-%3E%3D17-blue"></a>
|
||||||
<a href="https://vertx-china.github.io"><img src="https://img.shields.io/badge/vert.x-4.5.23-blue?style=flat"></a>
|
<a href="https://vertx-china.github.io"><img src="https://img.shields.io/badge/vert.x-4.5.27-blue?style=flat"></a>
|
||||||
<a href="https://raw.githubusercontent.com/qaiu/netdisk-fast-download/master/LICENSE"><img src="https://img.shields.io/github/license/qaiu/netdisk-fast-download?style=flat"></a>
|
<a href="https://raw.githubusercontent.com/qaiu/netdisk-fast-download/master/LICENSE"><img src="https://img.shields.io/github/license/qaiu/netdisk-fast-download?style=flat"></a>
|
||||||
<a href="https://github.com/qaiu/netdisk-fast-download/releases/"><img src="https://img.shields.io/github/v/release/qaiu/netdisk-fast-download?style=flat"></a>
|
<a href="https://github.com/qaiu/netdisk-fast-download/releases/"><img src="https://img.shields.io/github/v/release/qaiu/netdisk-fast-download?style=flat"></a>
|
||||||
|
<a href="https://github.com/QAIU/netdisk-fast-download">
|
||||||
|
<img src="https://img.shields.io/github/stars/QAIU/netdisk-fast-download?style=flat&logo=github" alt="GitHub Stars">
|
||||||
|
</a>
|
||||||
|
<a href="https://atomgit.com/QAIU/netdisk-fast-download"><img src="https://atomgit.com/QAIU/netdisk-fast-download/star/badge.svg" alt="AtomGit"></a>
|
||||||
|
<p align="center">
|
||||||
|
<a href="https://trendshift.io/repositories/12101" target="_blank"><img src="https://trendshift.io/api/badge/repositories/12101" alt="qaiu%2Fnetdisk-fast-download | Trendshift" style="width: 250px; height: 55px;" width="250" height="55"/></a>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
# netdisk-fast-download 网盘分享链接云解析服务
|
> 本平台所有音乐解析工具不再维护,请移步[Sonder播放器](https://sodecode.qaiu.top)
|
||||||
QQ群:1017480890
|
> 需要百度、迅雷的完整支持请使用[专业版](https://189.qaiu.top)。夸克网盘、UC网盘已包含在开源版。
|
||||||
|
|
||||||
> **📌 分支说明:**
|
## 介绍
|
||||||
> - 本分支 (`feature/graalpy-parser`) 为 **Python 解析器支持版本**,包含 GraalPy 环境
|
> netdisk-fast-download网盘直链解析可以把云盘分享链接转为直链,可广泛应用于各类下载站,资源站,个人博客,图床,APP下载更新,视频点播等领域。支持市面各大主流云盘的文件分享以及文件夹分享链接,已支持蓝奏云/蓝奏云优享/移动云云空间/小飞机盘/亿方云/123云盘/夸克网盘/UC网盘/永硕E盘/Cloudreve等,支持加密分享,以及部分网盘文件夹分享。
|
||||||
> - 如需标准版本(更小的镜像体积),请访问 [main 分支](https://github.com/qaiu/netdisk-fast-download/tree/main)
|
|
||||||
> - Docker 镜像:Python 版本使用 py 标签(如 `v0.1.9b19py`),标准版本使用 `latest` 标签
|
|
||||||
|
|
||||||
netdisk-fast-download网盘直链云解析(nfd云解析)能把网盘分享下载链接转化为直链,支持多款云盘,已支持蓝奏云/蓝奏云优享/奶牛快传/移动云云空间/小飞机盘/亿方云/123云盘/Cloudreve等,支持加密分享,以及部分网盘文件夹分享。
|
[官方文档](https://nfd-parser.github.io/)
|
||||||
|
[API接入](https://nfdparser.apifox.cn/)
|
||||||
|
[公益解析,lz站](https://lz.qaiu.top)
|
||||||
|
[公益解析,lz0站](https://lz0.qaiu.top)
|
||||||
|
[专业版](https://189.qaiu.top)
|
||||||
|
|
||||||
## 快速开始
|
## 快速开始
|
||||||
命令行下载分享文件:
|
|
||||||
```shell
|
|
||||||
curl -LOJ "https://lz.qaiu.top/parser?url=https://share.feijipan.com/s/nQOaNRPW&pwd=1234"
|
|
||||||
```
|
|
||||||
或者使用wget:
|
|
||||||
```shell
|
|
||||||
wget -O bilibili.mp4 "https://lz.qaiu.top/parser?url=https://share.feijipan.com/s/nQOaNRPW&pwd=1234"
|
|
||||||
```
|
|
||||||
或者使用浏览器[直接访问](https://nfd-parser.github.io/nfd-preview/preview.html?src=https%3A%2F%2Flz.qaiu.top%2Fparser%3Furl%3Dhttps%3A%2F%2Fshare.feijipan.com%2Fs%2FnQOaNRPW&name=bilibili.mp4&ext=mp4):
|
|
||||||
```
|
|
||||||
### 调用演示站下载:
|
|
||||||
https://lz.qaiu.top/parser?url=https://share.feijipan.com/s/nQOaNRPW&pwd=1234
|
|
||||||
### 调用演示站预览:
|
|
||||||
https://nfd-parser.github.io/nfd-preview/preview.html?src=https%3A%2F%2Flz.qaiu.top%2Fparser%3Furl%3Dhttps%3A%2F%2Fshare.feijipan.com%2Fs%2FnQOaNRPW&name=bilibili.mp4&ext=mp4
|
|
||||||
|
|
||||||
|
以带提取码的分享链接为例(提取码 `1234`),链接中含 `#`、`&`,作为 `url` 参数传入前必须做 URL 编码:
|
||||||
|
|
||||||
|
`https://www.ecpan.cn/web/#/yunpanProxy?path=%2F%23%2Fdrive%2Foutside&data=70017ece572737b12b30709b7f029308eJD0&isShare=1`
|
||||||
|
|
||||||
|
**命令行下载**
|
||||||
|
|
||||||
|
```shell
|
||||||
|
# curl
|
||||||
|
curl -LOJ "https://lz.qaiu.top/parser?url=https%3A%2F%2Fwww.ecpan.cn%2Fweb%2F%23%2FyunpanProxy%3Fpath%3D%252F%2523%252Fdrive%252Foutside%26data%3D70017ece572737b12b30709b7f029308eJD0%26isShare%3D1&pwd=1234"
|
||||||
|
|
||||||
|
# wget
|
||||||
|
wget -O v01 "https://lz.qaiu.top/parser?url=https%3A%2F%2Fwww.ecpan.cn%2Fweb%2F%23%2FyunpanProxy%3Fpath%3D%252F%2523%252Fdrive%252Foutside%26data%3D70017ece572737b12b30709b7f029308eJD0%26isShare%3D1&pwd=1234"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**短链方式**(key 取分享链接的 `data` 参数,提取码用 `@` 拼接)
|
||||||
|
|
||||||
|
| 用途 | 地址 |
|
||||||
|
| --- | --- |
|
||||||
|
| 下载 | `https://lz.qaiu.top/ec/70017ece572737b12b30709b7f029308eJD0@1234` |
|
||||||
|
| 在线预览 | [点击访问](https://nfd-parser.github.io/nfd-preview/preview.html?src=https%3A%2F%2Flz.qaiu.top%2Fec%2F70017ece572737b12b30709b7f029308eJD0%401234&name=v01&ext=mp4) |
|
||||||
|
|
||||||
|
> 无提取码时去掉 `&pwd=1234` / `@1234` 即可;预览页 `src` 需编码(`@` → `%40`),用短链可避免二次编码。
|
||||||
|
|
||||||
**解析器模块文档:** [parser/README.md](parser/README.md)
|
**解析器模块文档:** [parser/README.md](parser/README.md)
|
||||||
|
|
||||||
**JavaScript解析器文档:** [JavaScript解析器开发指南](parser/doc/JAVASCRIPT_PARSER_GUIDE.md) | [自定义解析器扩展指南](parser/doc/CUSTOM_PARSER_GUIDE.md) | [快速开始](parser/doc/CUSTOM_PARSER_QUICKSTART.md)
|
**JavaScript解析器文档:** [JavaScript解析器开发指南](parser/doc/JAVASCRIPT_PARSER_GUIDE.md) | [自定义解析器扩展指南](parser/doc/CUSTOM_PARSER_GUIDE.md) | [快速开始](parser/doc/CUSTOM_PARSER_QUICKSTART.md)
|
||||||
|
|
||||||
**Python解析器文档:** [Python解析器开发指南](parser/doc/PYTHON_PARSER_GUIDE.md) | [Playground测试报告](parser/doc/PYTHON_PLAYGROUND_TEST_REPORT.md) | [pylsp WebSocket集成](parser/doc/PYLSP_WEBSOCKET_GUIDE.md)
|
**Playground功能:** [JS解析器演练场密码保护说明](web-service/doc/PLAYGROUND_PASSWORD_PROTECTION.md)
|
||||||
|
|
||||||
## 演练场(Playground)
|
|
||||||
|
|
||||||
在线编写、测试和发布解析器脚本,支持 JavaScript 和 Python 两种语言。
|
**注意⚠️小飞机解析有IP限制,多数云服务商的大陆IP会被拦截(可以自行配置代理),和本程序无关**
|
||||||
|
**注意⚠️收到很多用户反馈,小飞机近期封号频繁,请尽可能选择其他网盘分享**
|
||||||
### 快速开始
|
**注意⚠️请不要过度依赖 lz.qaiu.top,建议本地搭建或者云服务器自行搭建。请求量过多的话服务器可能会被云盘厂商限制,遇到解析失败的分享链接不要着急提 issues,请先检查分享是否有效。**反馈时使用 [问题模板](https://github.com/qaiu/netdisk-fast-download/issues/new/choose),只提供可公开的测试分享,不要粘贴 Cookie、令牌或账号密码。
|
||||||
- **[演练场使用指南](web-service/doc/PLAYGROUND_GUIDE.md)** - 完整的使用教程和最佳实践
|
|
||||||
- **[5分钟快速上手](parser/doc/CUSTOM_PARSER_QUICKSTART.md)** - 快速集成指南
|
|
||||||
|
|
||||||
### 开发文档
|
|
||||||
- **JavaScript解析器**: [开发指南](parser/doc/JAVASCRIPT_PARSER_GUIDE.md) | [自定义扩展](parser/doc/CUSTOM_PARSER_GUIDE.md)
|
|
||||||
- **Python解析器**: [开发指南](parser/doc/PYTHON_PARSER_GUIDE.md) | [Python LSP连接](parser/doc/PYLSP_WEBSOCKET_GUIDE.md)
|
|
||||||
|
|
||||||
### 配置和安全
|
|
||||||
- **[密码保护配置](web-service/doc/PLAYGROUND_PASSWORD_PROTECTION.md)** - 访问控制和安全设置
|
|
||||||
- **[界面功能说明](web-front/doc/PLAYGROUND_UI_UPGRADE.md)** - IDE功能和快捷键
|
|
||||||
|
|
||||||
### 测试报告
|
|
||||||
- **[Python演练场测试报告](parser/doc/PYTHON_PLAYGROUND_TEST_REPORT.md)** - 功能验证和测试覆盖
|
|
||||||
|
|
||||||
### 在线体验
|
|
||||||
访问演练场页面:`http://your_host/playground`(需要密码或配置公开访问)
|
|
||||||
|
|
||||||
## 预览地址
|
|
||||||
[预览地址1](https://lz.qaiu.top)
|
|
||||||
[预览地址2](https://lz0.qaiu.top)
|
|
||||||
[移动/联通/天翼云盘大文件试用版](https://189.qaiu.top)
|
|
||||||
|
|
||||||
main分支依赖JDK17, 提供了JDK11分支[main-jdk11](https://github.com/qaiu/netdisk-fast-download/tree/main-jdk11)
|
|
||||||
**0.1.8及以上版本json接口格式有调整 参考json返回数据格式示例**
|
|
||||||
**小飞机解析有IP限制,多数云服务商的大陆IP会被拦截(可以自行配置代理),和本程序无关**
|
|
||||||
**注意: 请不要过度依赖lz.qaiu.top预览地址服务,建议本地搭建或者云服务器自行搭建。解析次数过多IP会被部分网盘厂商限制,不推荐做公共解析。**
|
|
||||||
|
|
||||||
## 网盘支持情况:
|
## 网盘支持情况:
|
||||||
> 20230905 奶牛云直链做了防盗链,需加入请求头:Referer: https://cowtransfer.com/
|
|
||||||
> 20230824 123云盘解析大文件(>100MB)失效,需要登录
|
> 20230824 123云盘解析大文件(>100MB)失效,需要登录
|
||||||
> 20230722 UC网盘解析失效,需要登录
|
> 20230722 UC网盘解析失效,需要登录
|
||||||
|
|
||||||
@@ -88,18 +78,19 @@ main分支依赖JDK17, 提供了JDK11分支[main-jdk11](https://github.com/qaiu/
|
|||||||
|
|
||||||
- [蓝奏云-lz](https://pc.woozooo.com/)
|
- [蓝奏云-lz](https://pc.woozooo.com/)
|
||||||
- [蓝奏云优享-iz](https://www.ilanzou.com/)
|
- [蓝奏云优享-iz](https://www.ilanzou.com/)
|
||||||
- [奶牛快传-cow](https://cowtransfer.com/)
|
|
||||||
- [移动云云空间-ec](https://www.ecpan.cn/web)
|
- [移动云云空间-ec](https://www.ecpan.cn/web)
|
||||||
- [小飞机网盘-fj](https://www.feijipan.com/)
|
- [小飞机网盘-fj](https://www.feijipan.com/)
|
||||||
- [亿方云-fc](https://www.fangcloud.com/)
|
- [亿方云-fc](https://www.fangcloud.com/)
|
||||||
- [123云盘-ye](https://www.123pan.com/)
|
- [123云盘-ye](https://www.123pan.com/) ⚠️仅建议本地部署使用。公共云解析没有共享 123 账号;下载返回 5112 / 请登录后下载时,在本机配置 `auths.ye.authorization`(Windows 可直接用 run.bat)
|
||||||
- ~[115网盘(失效)-p115](https://115.com/)~
|
- ~[115网盘(失效)-p115](https://115.com/)~
|
||||||
- ~[118网盘(已停服)-p118](https://www.118pan.com/)~
|
|
||||||
- [文叔叔-ws](https://www.wenshushu.cn/)
|
- [文叔叔-ws](https://www.wenshushu.cn/)
|
||||||
|
- [夸克网盘-qk](https://pan.quark.cn/)
|
||||||
|
- [UC网盘-uc](https://fast.uc.cn/)
|
||||||
- [联想乐云-le](https://lecloud.lenovo.com/)
|
- [联想乐云-le](https://lecloud.lenovo.com/)
|
||||||
- [QQ邮箱云盘-qqw](https://mail.qq.com/)
|
- [QQ邮箱云盘-qqw](https://mail.qq.com/)
|
||||||
- [QQ闪传-qqsc](https://nutty.qq.com/nutty/ssr/26797.html)
|
- [QQ闪传-qqsc](https://nutty.qq.com/nutty/ssr/26797.html)
|
||||||
- [城通网盘-ct](https://www.ctfile.com)
|
- [城通网盘-ct](https://www.ctfile.com)
|
||||||
|
- [永硕E盘-ys](https://www.ysepan.com/)(空间分享,如 `https://xxx.ysepan.com/`,密码为空间访问密码;多文件请用文件列表接口)
|
||||||
- [网易云音乐分享链接-mnes](https://music.163.com)
|
- [网易云音乐分享链接-mnes](https://music.163.com)
|
||||||
- [酷狗音乐分享链接-mkgs](https://www.kugou.com)
|
- [酷狗音乐分享链接-mkgs](https://www.kugou.com)
|
||||||
- [酷我音乐分享链接-mkws](https://kuwo.cn)
|
- [酷我音乐分享链接-mkws](https://kuwo.cn)
|
||||||
@@ -107,21 +98,26 @@ main分支依赖JDK17, 提供了JDK11分支[main-jdk11](https://github.com/qaiu/
|
|||||||
- [Cloudreve自建网盘-ce](https://github.com/cloudreve/Cloudreve)
|
- [Cloudreve自建网盘-ce](https://github.com/cloudreve/Cloudreve)
|
||||||
- ~[微雨云存储-pvvy](https://www.vyuyun.com/)~
|
- ~[微雨云存储-pvvy](https://www.vyuyun.com/)~
|
||||||
- [超星云盘(需要referer: https://pan-yz.chaoxing.com)-pcx](https://pan-yz.chaoxing.com)
|
- [超星云盘(需要referer: https://pan-yz.chaoxing.com)-pcx](https://pan-yz.chaoxing.com)
|
||||||
|
- [飞书云盘-fs](https://www.feishu.cn/)
|
||||||
- [WPS云文档-pwps](https://www.kdocs.cn/)
|
- [WPS云文档-pwps](https://www.kdocs.cn/)
|
||||||
- [汽水音乐-qishui_music](https://music.douyin.com/qishui/)
|
- [汽水音乐-qishui_music](https://music.douyin.com/qishui/)
|
||||||
- [咪咕音乐-migu](https://music.migu.cn/)
|
- [咪咕音乐-migu](https://music.migu.cn/)
|
||||||
- [一刻相册-baidu_photo](https://photo.baidu.com/)
|
|
||||||
- Google云盘-pgd
|
- Google云盘-pgd
|
||||||
- Onedrive-pod
|
- Onedrive-pod
|
||||||
- Dropbox-pdp
|
- Dropbox-pdp
|
||||||
- iCloud-pic
|
- iCloud-pic
|
||||||
### 仅专属版提供
|
### 专业版提供
|
||||||
|
- 百度网盘开放平台-bd
|
||||||
|
- 迅雷云盘开放平台-xl
|
||||||
|
- [光鸭云盘开放平台-gy](https://www.guangyapan.com)
|
||||||
- [移动云盘-p139](https://yun.139.com/)
|
- [移动云盘-p139](https://yun.139.com/)
|
||||||
- [联通云盘-pwo](https://pan.wo.cn/)
|
- [联通云盘-pwo](https://pan.wo.cn/)
|
||||||
- [天翼云盘-p189](https://cloud.189.cn/)
|
- [天翼云盘-p189](https://cloud.189.cn/)
|
||||||
|
|
||||||
## API接口
|
## API接口
|
||||||
|
|
||||||
|
[api接口文档](https://nfdparser.apifox.cn/)
|
||||||
|
|
||||||
### 服务端口
|
### 服务端口
|
||||||
- **6400**: API 服务端口(建议使用 Nginx 代理)
|
- **6400**: API 服务端口(建议使用 Nginx 代理)
|
||||||
- **6401**: 内置 Web 解析工具(个人使用可直接开放此端口)
|
- **6401**: 内置 Web 解析工具(个人使用可直接开放此端口)
|
||||||
@@ -151,6 +147,7 @@ GET /json/parser?url={分享链接}&pwd={密码}
|
|||||||
```
|
```
|
||||||
GET /json/{网盘标识}/{分享key}@{密码}
|
GET /json/{网盘标识}/{分享key}@{密码}
|
||||||
```
|
```
|
||||||
|
注意: 移动云云空间的 `分享key` 取分享链接中的 `data` 参数值
|
||||||
|
|
||||||
#### 3. 文件夹解析(v0.1.8fixed3+)
|
#### 3. 文件夹解析(v0.1.8fixed3+)
|
||||||
|
|
||||||
@@ -165,10 +162,97 @@ GET /json/getFileList?url={分享链接}&pwd={密码}
|
|||||||
- `{网盘标识}` 参考支持的网盘列表
|
- `{网盘标识}` 参考支持的网盘列表
|
||||||
- `your_host` 替换为您的域名或 IP
|
- `your_host` 替换为您的域名或 IP
|
||||||
|
|
||||||
### 特殊说明
|
### 认证参数(v0.2.1+)
|
||||||
|
部分网盘需要登录后的 Cookie 或 Authorization 才能下载。可通过 `auth` 参数传递认证信息:
|
||||||
|
|
||||||
- 移动云云空间的 `分享key` 取分享链接中的 `data` 参数值
|
**参数格式**:`auth` 参数值为 AES 加密后的 JSON 字符串,经过 Base64 编码和 URL 编码
|
||||||
- 移动云云空间、小飞机网盘的加密分享可忽略密码参数
|
|
||||||
|
**加密方式**:
|
||||||
|
- 算法:AES/ECB/PKCS5Padding
|
||||||
|
- 密钥:由自建服务显式配置并通过安全渠道交给可信客户端(无公共默认值)
|
||||||
|
- 流程:JSON → AES加密 → Base64 → URL编码
|
||||||
|
|
||||||
|
**JSON 结构**:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"authType": "cookie", // 认证类型: cookie/accesstoken/authorization/password/custom
|
||||||
|
"token": "your_cookie_here", // Cookie 或 Token 内容
|
||||||
|
"username": "", // 用户名(password 类型时使用)
|
||||||
|
"password": "", // 密码(password 类型时使用)
|
||||||
|
"ext1": "", // 扩展字段1(custom 类型时使用)
|
||||||
|
"ext2": "" // 扩展字段2(custom 类型时使用)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**网盘认证要求**:
|
||||||
|
| 网盘 | 认证要求 | 说明 |
|
||||||
|
|------|---------|------|
|
||||||
|
| 夸克网盘(QK) | 不超过 10MiB 可走原解析 | 不大于 `10 * 1024 * 1024` 字节(含等于该值)保持原来的分享解析。更大的文件仅在会员接口确认未过期 SVIP/VIP 时使用该账号直链;否则使用探测通过的 H5 文件地址并附上真实文件名。普通或过期 Cookie 不算会员 |
|
||||||
|
| UC网盘(UC) | **不能免登录** | 小文件仍要 Cookie。超过 10MiB 时同样只认未过期 SVIP/VIP 的账号直链;没有时才尝试 H5 文件地址。H5 没有可下载文件就返回明确错误,不会把 UC 当成免登录 |
|
||||||
|
| 小飞机网盘(FJ) | 可选 | 大文件(>100MB)需要认证 |
|
||||||
|
| 蓝奏优享(IZ) | 可选 | 大文件需要认证 |
|
||||||
|
| 123网盘(YE) | 下载通常需要 | 分享列表经常可以不登录读取。下载接口常见 5112 / 请登录后下载 / 您需要注册登录或付费后下载,请在**本机**配置 `auths.ye.authorization`(与 `token` 等价)或账号密码。公共云解析没有共享 123 账号。约 10MB 小文件有时可以直链,大文件和风控必须用你自己的账号。短链:`ye:分享key@提取码`;`#` 片段会去掉;`yun.123pan.com`、`www.123pan.com`、`*.share.123pan.cn` 同一类 |
|
||||||
|
|
||||||
|
**使用示例**:
|
||||||
|
```
|
||||||
|
GET /parser?url={分享链接}&pwd={密码}&auth={加密后的认证参数}
|
||||||
|
```
|
||||||
|
|
||||||
|
> 💡 提示:自建站的可信客户端可在网页中填写其显式配置的服务认证密钥;网页不会获取或保存服务端密钥。
|
||||||
|
> 不要在不受信任的站点输入服务密钥或网盘凭据。
|
||||||
|
|
||||||
|
#### 升级与捐赠开关
|
||||||
|
|
||||||
|
- 没有手动配置密钥(包括升级后仍保留旧发布包默认配置)的实例,在每次启动时自动生成进程随机密钥。启动日志只记录来源和指纹,不输出原文。重启后,之前用进程密钥生成的认证参数会失效。
|
||||||
|
- 要让可信浏览器、外部工具或多实例使用同一密钥,显式配置 `server.authEncryptKey`,通过安全渠道分发;不要使用旧发布包默认值。
|
||||||
|
- 捐赠默认关闭。仅设置 `server.enableDonatedAccounts: true` 且启用数据库后,才允许提交和服务端选用捐赠账号。关闭不删除已有记录。旧的浏览器取号接口已移除。仅 123 云盘、小飞机和蓝奏优享可使用服务端捐赠池;需要在下载响应携带账号请求头的网盘暂不开放。
|
||||||
|
|
||||||
|
#### 密钥作用说明
|
||||||
|
|
||||||
|
- `server.authEncryptKey`
|
||||||
|
- 作用:用于 `auth` 参数的 AES 加解密
|
||||||
|
- 要求:16、24 或 32 个 UTF-8 字节
|
||||||
|
|
||||||
|
- `server.donatedAccountFailureTokenSignKey`
|
||||||
|
- 作用:用于“捐赠账号失败计数 token”的 HMAC 签名/验签
|
||||||
|
- 目的:防止客户端伪造失败计数请求
|
||||||
|
- 建议:使用高强度随机字符串,且不要与 `authEncryptKey` 相同
|
||||||
|
|
||||||
|
#### `auth` 临时认证参数 与 `auths` 静态配置认证 的区别
|
||||||
|
|
||||||
|
本项目存在两种互相独立的认证配置方式,作用范围不同,不要混淆:
|
||||||
|
|
||||||
|
| 方式 | 配置位置 | 生效范围 | 适用场景 |
|
||||||
|
|------|---------|---------|---------|
|
||||||
|
| `auth` 临时认证参数 | 请求 URL 上的 `auth` 查询参数 | **仅当次请求**,优先级高于 app-dev.yml 中的静态配置 | 调用方按用户临时提供的账号/Cookie/token 解析,不同请求可携带不同认证信息 |
|
||||||
|
| `auths` 静态配置认证 | `app-dev.yml` 的 `auths.<网盘标识>` 节点 | **服务端长期生效**,所有未携带 `auth` 参数的请求都会复用 | 部署方自己长期配置一份账号,供所有请求默认使用 |
|
||||||
|
|
||||||
|
以 123网盘(`ye`)为例,`app-dev.yml` 中支持以下几种写法(三选一即可):
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
auths:
|
||||||
|
ye:
|
||||||
|
username: 你的123网盘账号
|
||||||
|
password: 你的123网盘密码
|
||||||
|
```
|
||||||
|
```yaml
|
||||||
|
auths:
|
||||||
|
ye:
|
||||||
|
token: 已登录后获取的 Authorization/AccessToken
|
||||||
|
```
|
||||||
|
```yaml
|
||||||
|
auths:
|
||||||
|
ye:
|
||||||
|
authorization: 已登录后获取的 Authorization/AccessToken # 与 token 等价,二选一
|
||||||
|
```
|
||||||
|
|
||||||
|
> ⚠️ 注意:YAML 中 key 后面不写值(如 `authorization:` 空着)等同于没配置,不会生效,必须填入真实的账号密码或 token 内容。
|
||||||
|
>
|
||||||
|
> 若 123 云盘账号密码登录触发验证码或风控,解析会直接失败并提示改用 Authorization,不会尝试验证码。请改填上面的 `authorization` / `token`(浏览器 localStorage 的 `authorToken`,或请求头 `authorization` / Cookie `sso-token` 里的 JWT)。
|
||||||
|
>
|
||||||
|
> 公共解析站(例如别人搭的在线解析)不能替你登录 123。看到「请登录后下载」「5112」「您需要注册登录或付费后下载」时,把服务跑在自己的机器上并填入自己的 Authorization。这是 123 的登录和风控要求。链接写法:`/d/ye/分享key@提取码`;浏览器地址栏里 `#` 后面的片段不参与解析;`yun.123pan.com` 与 `www.123pan.com`、`数字.share.123pan.cn` 是同一种分享。
|
||||||
|
|
||||||
|
如果只是临时调用一次,不想改动服务端配置,也可以用上面提到的 `auth` 参数临时传递(`authType` 可选 `password`/`accesstoken`/`authorization`),无需重启服务,仅本次请求生效。
|
||||||
|
|
||||||
### 示例
|
### 示例
|
||||||
|
|
||||||
@@ -245,9 +329,12 @@ json返回数据格式示例:
|
|||||||
"timestamp": 1736489219402
|
"timestamp": 1736489219402
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
#### 3. 文件夹解析(仅支持蓝奏云/蓝奏优享/小飞机网盘)
|
#### 3. 文件夹解析(支持蓝奏云/蓝奏优享/小飞机/永硕E盘等)
|
||||||
/v2/getFileList?url=分享链接&pwd=分享密码
|
/v2/getFileList?url=分享链接&pwd=分享密码
|
||||||
|
|
||||||
|
永硕E盘(`ys`)空间链接先返回目录列表,再带 `dirId` 获取目录内文件:
|
||||||
|
`/v2/getFileList?url=https://xxx.ysepan.com/&pwd=空间密码&dirId=目录编号`
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"code": 200,
|
"code": 200,
|
||||||
@@ -298,15 +385,21 @@ json返回数据格式示例:
|
|||||||
| 网盘名称 | 免登陆下载分享 | 加密分享 | 初始网盘空间 | 单文件大小限制 |
|
| 网盘名称 | 免登陆下载分享 | 加密分享 | 初始网盘空间 | 单文件大小限制 |
|
||||||
|-------------|---------|----------|-----------|-----------------|
|
|-------------|---------|----------|-----------|-----------------|
|
||||||
| 蓝奏云 | √ | √ | 不限空间 | 100M |
|
| 蓝奏云 | √ | √ | 不限空间 | 100M |
|
||||||
| 奶牛快传 | √ | X | 10G | 不限大小 |
|
|
||||||
| 移动云云空间(个人版) | √ | √(密码可忽略) | 5G(个人) | 不限大小 |
|
| 移动云云空间(个人版) | √ | √(密码可忽略) | 5G(个人) | 不限大小 |
|
||||||
| 小飞机网盘 | √ | √(密码可忽略) | 10G | 不限大小 |
|
| 小飞机网盘 | √ | √ | 10G | 不限大小 |
|
||||||
| 360亿方云 | √ | √(密码可忽略) | 100G(须实名) | 不限大小 |
|
| 360亿方云 | √ | √ | 100G(须实名) | 不限大小 |
|
||||||
| 123云盘 | √ | √ | 2T | 100G(>100M需要登录) |
|
| 123云盘 | 小文件有时可以 | √ | 2T | 大文件/风控需要本机登录 |
|
||||||
| 文叔叔 | √ | √ | 10G | 5GB |
|
| 文叔叔 | √ | √ | 10G | 5GB |
|
||||||
|
| 永硕E盘 | √ | √(空间密码) | 视套餐 | 视套餐 |
|
||||||
| WPS云文档 | √ | X | 5G(免费) | 10M(免费)/2G(会员) |
|
| WPS云文档 | √ | X | 5G(免费) | 10M(免费)/2G(会员) |
|
||||||
| 夸克网盘 | x | √ | 10G | 不限大小 |
|
| 夸克网盘 | ≤10MiB 原解析 | √ | 10G | 更大文件要有效 SVIP/VIP,或可用的 H5 文件地址 |
|
||||||
| UC网盘 | x | √ | 10G | 不限大小 |
|
| UC网盘 | x | √ | 10G | 不能免登录;大文件规则同夸克 |
|
||||||
|
| 飞书云盘 | √ | X | 15G | 不限大小 |
|
||||||
|
|
||||||
|
免登录边界(阈值由网盘决定,不是本项目写死的):
|
||||||
|
|
||||||
|
- **夸克 / UC**:单文件体积阈值是 `10 * 1024 * 1024` 字节(10MiB)。小于或等于该值保持原来的解析。严格大于该值时,先请求会员接口,只把 `member_type` 为 `SUPER_VIP`、`EXP_SVIP`、`Z_VIP`、`VIP`、`EXP_VIP` 且 `exp_at` 仍在未来的账号当作有效 SVIP/VIP,并走原来的账号直链。缺少档位、普通账号、已过期或只有 Cookie 都不会当成会员。没有有效会员时,才使用 H5 预览返回的 `https://dl-c-*.drive.quark.cn` 或 `https://dl-c-*.drive.uc.cn` 文件地址,并加上真实 `filename`(含中文)。预览网页、HTML、JSON、m3u8 不会当作下载成功。UC 不能免登录。目录列表不变。百度大约 100MB 以内有直链,属于专业版,这次没有改解析器。
|
||||||
|
- **123**:分享目录经常可以不登录看。下载则经常要登录。请本机配置 Authorization,不要依赖公共云解析的共享账号。约 10MB 的小文件有时能直链。
|
||||||
|
|
||||||
# 打包部署
|
# 打包部署
|
||||||
|
|
||||||
@@ -332,82 +425,7 @@ mvn package -DskipTests
|
|||||||
|
|
||||||
## Linux服务部署
|
## Linux服务部署
|
||||||
|
|
||||||
### Docker 部署
|
### Docker 部署(Main分支)
|
||||||
|
|
||||||
> **注意:** 本分支(feature/graalpy-parser)包含 Python 解析器支持(GraalPy),镜像体积较大。
|
|
||||||
> 如果不需要 Python 解析器功能,建议使用 main 分支的标准版本。
|
|
||||||
|
|
||||||
#### Python 版本特性
|
|
||||||
- ✅ 支持 Python 自定义解析器(基于 GraalPy)
|
|
||||||
- ✅ 内置 Python 语言服务器(pylsp)支持代码补全
|
|
||||||
- ✅ 支持外部 Python 环境通过 WebSocket 连接
|
|
||||||
- ⚠️ 镜像体积较大(约增加 200MB)
|
|
||||||
|
|
||||||
#### 海外服务器Docker部署(Python版)
|
|
||||||
|
|
||||||
> **注意:** 请根据 [Releases](https://github.com/qaiu/netdisk-fast-download/releases) 页面使用最新的 py 标签版本
|
|
||||||
|
|
||||||
```shell
|
|
||||||
# 创建目录
|
|
||||||
mkdir -p netdisk-fast-download
|
|
||||||
cd netdisk-fast-download
|
|
||||||
|
|
||||||
# 拉取镜像(使用 py 标签,请替换为最新版本号)
|
|
||||||
docker pull ghcr.io/qaiu/netdisk-fast-download:v0.1.9b19py
|
|
||||||
|
|
||||||
# 复制配置文件(或下载仓库web-service\src\main\resources)
|
|
||||||
docker create --name netdisk-fast-download ghcr.io/qaiu/netdisk-fast-download:v0.1.9b19py
|
|
||||||
docker cp netdisk-fast-download:/app/resources ./resources
|
|
||||||
docker rm netdisk-fast-download
|
|
||||||
|
|
||||||
# 启动容器
|
|
||||||
docker run -d -it --name netdisk-fast-download -p 6401:6401 --restart unless-stopped -e TZ=Asia/Shanghai -v ./resources:/app/resources -v ./db:/app/db -v ./logs:/app/logs ghcr.io/qaiu/netdisk-fast-download:v0.1.9b19py
|
|
||||||
|
|
||||||
# 反代6401端口
|
|
||||||
|
|
||||||
# 升级容器(请手动指定版本号)
|
|
||||||
# 注意:Python 版本不会更新 latest 标签,需要手动指定版本
|
|
||||||
docker pull ghcr.io/qaiu/netdisk-fast-download:v0.1.9b19py
|
|
||||||
docker stop netdisk-fast-download
|
|
||||||
docker rm netdisk-fast-download
|
|
||||||
docker run -d -it --name netdisk-fast-download -p 6401:6401 --restart unless-stopped -e TZ=Asia/Shanghai -v ./resources:/app/resources -v ./db:/app/db -v ./logs:/app/logs ghcr.io/qaiu/netdisk-fast-download:v0.1.9b19py
|
|
||||||
```
|
|
||||||
|
|
||||||
#### 国内Docker部署(Python版)
|
|
||||||
|
|
||||||
> **注意:** 请根据 [Releases](https://github.com/qaiu/netdisk-fast-download/releases) 页面使用最新的 py 标签版本
|
|
||||||
|
|
||||||
```shell
|
|
||||||
# 创建目录
|
|
||||||
mkdir -p netdisk-fast-download
|
|
||||||
cd netdisk-fast-download
|
|
||||||
|
|
||||||
# 拉取镜像(使用 py 标签,请替换为最新版本号)
|
|
||||||
docker pull ghcr.nju.edu.cn/qaiu/netdisk-fast-download:v0.1.9b19py
|
|
||||||
|
|
||||||
# 复制配置文件(或下载仓库web-service\src\main\resources)
|
|
||||||
docker create --name netdisk-fast-download ghcr.nju.edu.cn/qaiu/netdisk-fast-download:v0.1.9b19py
|
|
||||||
docker cp netdisk-fast-download:/app/resources ./resources
|
|
||||||
docker rm netdisk-fast-download
|
|
||||||
|
|
||||||
# 启动容器
|
|
||||||
docker run -d -it --name netdisk-fast-download -p 6401:6401 --restart unless-stopped -e TZ=Asia/Shanghai -v ./resources:/app/resources -v ./db:/app/db -v ./logs:/app/logs ghcr.nju.edu.cn/qaiu/netdisk-fast-download:v0.1.9b19py
|
|
||||||
|
|
||||||
# 反代6401端口
|
|
||||||
|
|
||||||
# 升级容器(请手动指定版本号)
|
|
||||||
# 注意:Python 版本不会更新 latest 标签,需要手动指定版本
|
|
||||||
docker pull ghcr.nju.edu.cn/qaiu/netdisk-fast-download:v0.1.9b19py
|
|
||||||
docker stop netdisk-fast-download
|
|
||||||
docker rm netdisk-fast-download
|
|
||||||
docker run -d -it --name netdisk-fast-download -p 6401:6401 --restart unless-stopped -e TZ=Asia/Shanghai -v ./resources:/app/resources -v ./db:/app/db -v ./logs:/app/logs ghcr.nju.edu.cn/qaiu/netdisk-fast-download:v0.1.9b19py
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
### Docker 部署(标准版 - Main分支)
|
|
||||||
|
|
||||||
如果不需要 Python 解析器功能,建议使用标准版本:
|
|
||||||
|
|
||||||
#### 海外服务器Docker部署
|
#### 海外服务器Docker部署
|
||||||
```shell
|
```shell
|
||||||
@@ -461,8 +479,8 @@ docker run --rm -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtow
|
|||||||
> 注意: netdisk-fast-download.service中的ExecStart的路径改为实际路径
|
> 注意: netdisk-fast-download.service中的ExecStart的路径改为实际路径
|
||||||
```shell
|
```shell
|
||||||
cd ~
|
cd ~
|
||||||
wget -O netdisk-fast-download.zip https://github.com/qaiu/netdisk-fast-download/releases/download/v0.1.9b7/netdisk-fast-download-bin.zip
|
wget -O netdisk-fast-download.zip https://github.com/qaiu/netdisk-fast-download/releases/download/v0.4.15/netdisk-fast-download-linux-amd64.zip
|
||||||
unzip netdisk-fast-download-bin.zip
|
unzip netdisk-fast-download.zip
|
||||||
cd netdisk-fast-download
|
cd netdisk-fast-download
|
||||||
bash service-install.sh
|
bash service-install.sh
|
||||||
```
|
```
|
||||||
@@ -518,7 +536,7 @@ nfd-proxy搭建http代理服务器
|
|||||||
参考https://github.com/nfd-parser/nfd-proxy
|
参考https://github.com/nfd-parser/nfd-proxy
|
||||||
|
|
||||||
### 认证信息配置说明
|
### 认证信息配置说明
|
||||||
部分网盘(如123)解析大文件时需要登录认证,可以在配置文件中添加认证信息。
|
夸克不超过 10MiB 可以不登录;更大的文件要本机未过期的 SVIP/VIP Cookie,或上游确实返回了可下载的 H5 文件地址。UC 不能免登录。123 的下载在公共解析站上基本都要你自己的 Authorization,云端没有共用账号。
|
||||||
|
|
||||||
修改配置文件:
|
修改配置文件:
|
||||||
app-dev.yml
|
app-dev.yml
|
||||||
@@ -526,31 +544,24 @@ app-dev.yml
|
|||||||
```yaml
|
```yaml
|
||||||
### 解析认证相关
|
### 解析认证相关
|
||||||
auths:
|
auths:
|
||||||
# 123:配置用户名密码
|
# 夸克 / UC 大于 10MiB(10 * 1024 * 1024 字节)时才会查询会员接口。
|
||||||
|
# 只有未过期的 SVIP/VIP 才使用该账号直链;不要把普通 Cookie 写成会员。
|
||||||
|
# qk:
|
||||||
|
# cookie: 浏览器里夸克网盘的 Cookie
|
||||||
|
# uc:
|
||||||
|
# cookie: 浏览器里 UC 网盘的 Cookie
|
||||||
|
# 123:优先填 authorization。账号密码若触发验证码会失败,本项目不处理验证码。
|
||||||
ye:
|
ye:
|
||||||
username: 你的用户名
|
username: 你的用户名
|
||||||
password: 你的密码
|
password: 你的密码
|
||||||
|
# authorization: 浏览器登录后的 authorToken / sso-token
|
||||||
|
# 城通免密和带密码分享都先匿名解析。仅当仍被 Turnstile 拦住时才需要登录令牌。
|
||||||
|
# 登录 https://pan.ctfile.com 后,从 Session Storage 复制 refresh_token。
|
||||||
|
# ct:
|
||||||
|
# refresh_token: 登录后的 refresh_token
|
||||||
|
# token: 可选,当前 access token
|
||||||
```
|
```
|
||||||
|
|
||||||
**注意:** 目前仅支持 123(ye)的认证配置。
|
|
||||||
|
|
||||||
## 开发计划
|
|
||||||
### v0.1.8~v0.1.9 ✓
|
|
||||||
- API添加文件信息(专属版/开源版)
|
|
||||||
- 目录解析(专属版/开源版)
|
|
||||||
- 文件预览功能(专属版/开源版)
|
|
||||||
- 文件夹预览功能(开源版)
|
|
||||||
- 友好的错误提示和一键反馈功能(开源版)
|
|
||||||
- 带cookie/token/username/pwd参数解析大文件(专属版)
|
|
||||||
### v0.2.x
|
|
||||||
- web后台管理--认证配置/分享链接管理(开源版/专属版)
|
|
||||||
- 123/小飞机/蓝奏优享等大文件解析(开源版)
|
|
||||||
- 直链分享(开源版/专属版)
|
|
||||||
- aria2/idm+/curl/wget链接生成(开源版/专属版)
|
|
||||||
- IP限流配置(开源版/专属版)
|
|
||||||
- refere防盗链,API鉴权防盗链(专属版)
|
|
||||||
- 123/小飞机/蓝奏优享/蓝奏文件夹解析API,天翼云盘/移动云盘文件夹解析API(专属版)
|
|
||||||
- 用户管理面板--营销推广系统(专属版)
|
|
||||||
|
|
||||||
**技术栈:**
|
**技术栈:**
|
||||||
Jdk17+Vert.x4
|
Jdk17+Vert.x4
|
||||||
@@ -559,36 +570,52 @@ Core模块集成Vert.x实现类似spring的注解式路由API
|
|||||||
|
|
||||||
## Star History
|
## Star History
|
||||||
|
|
||||||
[](https://star-history.com/#qaiu/netdisk-fast-download&Date)
|
<a href="https://www.star-history.com/?repos=qaiu%2Fnetdisk-fast-download&type=date&legend=bottom-right">
|
||||||
|
<picture>
|
||||||
## **免责声明**
|
<source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/chart?repos=qaiu/netdisk-fast-download&type=date&theme=dark&legend=bottom-right&sealed_token=dfQO_dJcTqcPkEnM7SfxRyHoFbV5Ah4LxoEhdlheMn4T2YLEV_WETxFZexeAbWN5OmNyYuycWan2d42PAFbw0CuU4oCTKgehfErFJ9eVl2CyVpP_4xrdQw" />
|
||||||
- 用户在使用本项目时,应自行承担风险,并确保其行为符合当地法律法规。开发者不对用户因使用本项目而导致的任何后果负责。
|
<source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/chart?repos=qaiu/netdisk-fast-download&type=date&legend=bottom-right&sealed_token=dfQO_dJcTqcPkEnM7SfxRyHoFbV5Ah4LxoEhdlheMn4T2YLEV_WETxFZexeAbWN5OmNyYuycWan2d42PAFbw0CuU4oCTKgehfErFJ9eVl2CyVpP_4xrdQw" />
|
||||||
|
<img alt="Star History Chart" src="https://api.star-history.com/chart?repos=qaiu/netdisk-fast-download&type=date&legend=bottom-right&sealed_token=dfQO_dJcTqcPkEnM7SfxRyHoFbV5Ah4LxoEhdlheMn4T2YLEV_WETxFZexeAbWN5OmNyYuycWan2d42PAFbw0CuU4oCTKgehfErFJ9eVl2CyVpP_4xrdQw" />
|
||||||
## 支持该项目
|
</picture>
|
||||||
开源不易,用爱发电,本项目长期维护如果觉得有帮助, 可以请作者喝杯咖啡, 感谢支持
|
|
||||||
|
|
||||||
本项目的服务器由林枫云提供赞助<br>
|
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
|
||||||
|
## **声明**
|
||||||
|
|
||||||
|
- 本项目**仅供个人学习与技术交流使用**,请勿用于商业用途或大规模滥用。
|
||||||
|
- 所用接口均来自各网盘**官方开放平台**、官方公开接口及**已知开源项目**,仅解析用户主动提供的分享链接,**不涉及破解或绕过版权保护**。
|
||||||
|
- 本项目**相当于自动化程序代替用户发起请求**,**不会提升或绕过任何会员权限**,也**不会突破网盘的限速策略**;我们**鼓励用户开通官方 VIP/SVIP**以获得更好的下载与加速体验。
|
||||||
|
- 使用者应遵守各网盘服务商的**用户协议与使用条款**,以及所在地区的**法律法规**,违反所产生的后果自行承担。
|
||||||
|
- **高频调用可能触发网盘风控**,导致**账号限流、封禁**或**来源 IP 被屏蔽**,请合理控制请求频率,避免高并发与批量请求。
|
||||||
|
- 网盘接口调整、风控策略变更等因素可能导致功能失效,本项目**不对可用性与稳定性作任何担保**。
|
||||||
|
- 开发者**不对**使用本项目产生的**任何直接或间接损失**(账号封禁、数据丢失、法律纠纷等)**承担责任**。
|
||||||
|
- 开发者保留**随时修改本免责声明**的权利,恕不另行通知。
|
||||||
|
- **下载、部署或使用本项目,即视为您已阅读并接受上述全部条款。**
|
||||||
|
|
||||||
|
|
||||||
|
## 赞助该项目
|
||||||
|
开源不易,用爱发电。本项目长期维护,如果觉得有帮助,欢迎赞助开发者,感谢支持。
|
||||||
|
|
||||||
|
<p align="center">
|
||||||
|
<a href="https://blog.qaiu.top/archives/da-shang-zhuan-yong"><img alt="微信" src="https://api.iconify.design/simple-icons/wechat.svg?color=%2307C160" width="44" height="44"></a>
|
||||||
|
|
||||||
|
<a href="https://blog.qaiu.top/archives/da-shang-zhuan-yong"><img alt="支付宝" src="https://api.iconify.design/simple-icons/alipay.svg?color=%231677FF" width="44" height="44"></a>
|
||||||
|
|
||||||
|
<a href="https://paypal.me/futianshun"><img alt="PayPal" src="https://api.iconify.design/simple-icons/paypal.svg?color=%2300457C" width="44" height="44"></a>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
本项目的服务器由林枫云提供赞助
|
||||||
|
|
||||||
<a href="https://www.dkdun.cn/aff/WDBRYKGH" target="_blank">
|
<a href="https://www.dkdun.cn/aff/WDBRYKGH" target="_blank">
|
||||||
<img src="https://www.dkdun.cn/themes/web/www/upload/local68c2dbb2ab148.png" width="200">
|
<img src="https://www.dkdun.cn/themes/web/www/upload/local68c2dbb2ab148.png" width="200">
|
||||||
</a>
|
</a>
|
||||||
</p>
|
|
||||||
|
|
||||||
|
|
||||||
### 关于赞助定制专属版
|
### 代理资源支持
|
||||||
1. 专属版提供对小飞机,蓝奏优享大文件解析的支持, 提供天翼云盘/移动云盘/联通云盘的解析支持。
|
|
||||||
2. 可提供托管服务:包含部署服务和云服务器环境。
|
|
||||||
3. 可提供功能定制开发。
|
|
||||||
您可能需要提供一定的资金赞助支持定制专属版, 请添加以下任意一个联系方式详谈赞助模式:
|
|
||||||
<p>qq: 197575894</p>
|
|
||||||
<p>wechat: imcoding_</p>
|
|
||||||
|
|
||||||
<!--
|
|
||||||

|
|
||||||
|
|
||||||
[手机端支付宝打赏跳转链接](https://qr.alipay.com/fkx01882dnoxxtjenhlxt53)
|
|
||||||
-->
|
|
||||||
|
|
||||||
|
|
||||||
|
<a href="https://proxy-seller.com/?partner=5799A39425654D33">
|
||||||
|
<img src="https://raw.githubusercontent.com/wujunwei928/parse-video-py/main/resources/proxy-seller-logo.png" alt="Proxy-Seller" width="120">
|
||||||
|
</a>
|
||||||
|
|
||||||
|
感谢 [Proxy-Seller](https://proxy-seller.com/?partner=5799A39425654D33) 提供代理 IP 支持,其香港 IP 可用于本项目。
|
||||||
|
|
||||||
|
[Proxy-Seller 优惠链接](https://proxy-seller.com/?partner=5799A39425654D33) · 专属折扣码:`BJSBDD_664959`
|
||||||
+107
@@ -0,0 +1,107 @@
|
|||||||
|
[English](#security-policy) | [简体中文](#安全政策)
|
||||||
|
|
||||||
|
# Security Policy
|
||||||
|
|
||||||
|
Thank you for helping keep netdisk-fast-download (NFD) and its users safe.
|
||||||
|
|
||||||
|
## Supported Versions
|
||||||
|
|
||||||
|
Security fixes are provided for the latest stable release. Older releases are not actively maintained and generally do not receive security backports. Users are strongly encouraged to upgrade to the latest stable release.
|
||||||
|
|
||||||
|
| Version | Supported |
|
||||||
|
| --- | --- |
|
||||||
|
| Latest stable release | ✅ |
|
||||||
|
| Older releases | ❌ |
|
||||||
|
|
||||||
|
## Reporting a Vulnerability
|
||||||
|
|
||||||
|
Please report suspected security vulnerabilities privately through [GitHub Private Vulnerability Reporting](https://github.com/qaiu/netdisk-fast-download/security/advisories/new). Do not disclose unpatched vulnerabilities in public Issues, Pull Requests, Discussions, or other public channels.
|
||||||
|
|
||||||
|
Please include the following information where possible:
|
||||||
|
|
||||||
|
- Affected version or commit
|
||||||
|
- Vulnerability description, affected component, and potential impact
|
||||||
|
- Reproduction steps or a minimal proof of concept
|
||||||
|
- Relevant configuration, logs, and deployment details, with sensitive information redacted
|
||||||
|
|
||||||
|
Do not include real credentials, authentication keys, API tokens, or personal data in your report. Test only systems you own or are authorized to test.
|
||||||
|
|
||||||
|
## Scope
|
||||||
|
|
||||||
|
This policy applies to security vulnerabilities in the official NFD project, including authentication, authorization, credential handling, and other security-sensitive functionality.
|
||||||
|
|
||||||
|
Ordinary bugs, upstream API changes, and compatibility issues without a security impact should be reported through [GitHub Issues](https://github.com/qaiu/netdisk-fast-download/issues). Issues caused solely by third-party services, unofficial modifications, or unsafe deployment configurations are generally outside the scope of this policy. However, vulnerabilities in NFD's default configuration or its handling of third-party services remain in scope.
|
||||||
|
|
||||||
|
## Security Response and Disclosure
|
||||||
|
|
||||||
|
Security reports are handled on a best-effort basis, taking into account severity, impact, and maintainer availability. We aim to assess reports, develop appropriate fixes or mitigations, and coordinate public disclosure with reporters where possible. No fixed response or remediation timeframe is guaranteed.
|
||||||
|
|
||||||
|
## Security Best Practices
|
||||||
|
|
||||||
|
- Keep NFD and its dependencies up to date.
|
||||||
|
- Use HTTPS and restrict access to sensitive administrative interfaces.
|
||||||
|
- Protect authentication keys, API tokens, and cloud-drive credentials; never expose them in public logs, frontend bundles, or diagnostic reports.
|
||||||
|
- Enable only the services and optional features you need.
|
||||||
|
- Revoke or rotate potentially compromised credentials promptly.
|
||||||
|
|
||||||
|
## Security Advisories
|
||||||
|
|
||||||
|
For published vulnerabilities, affected versions, fixes, and mitigation guidance, see [GitHub Security Advisories](https://github.com/qaiu/netdisk-fast-download/security/advisories) and [Release Notes](https://github.com/qaiu/netdisk-fast-download/releases).
|
||||||
|
|
||||||
|
## Acknowledgments
|
||||||
|
|
||||||
|
We appreciate everyone who responsibly reports vulnerabilities and helps improve the security of NFD.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
# 安全政策
|
||||||
|
|
||||||
|
感谢你帮助维护 netdisk-fast-download(NFD)及其用户的安全。
|
||||||
|
|
||||||
|
## 支持的版本
|
||||||
|
|
||||||
|
安全修复面向最新稳定版本。历史版本不再主动维护,原则上不提供安全补丁回移。强烈建议用户及时升级至最新稳定版本。
|
||||||
|
|
||||||
|
| 版本 | 是否支持 |
|
||||||
|
| --- | --- |
|
||||||
|
| 最新稳定版 | ✅ |
|
||||||
|
| 历史版本 | ❌ |
|
||||||
|
|
||||||
|
## 漏洞报告
|
||||||
|
|
||||||
|
请通过 [GitHub 私密漏洞报告](https://github.com/qaiu/netdisk-fast-download/security/advisories/new) 渠道提交疑似安全漏洞。请勿在公开 Issue、Pull Request、Discussion 或其他公开渠道披露尚未修复的漏洞。
|
||||||
|
|
||||||
|
请尽可能提供以下信息:
|
||||||
|
|
||||||
|
- 受影响的版本或提交
|
||||||
|
- 漏洞描述、受影响组件及潜在影响
|
||||||
|
- 复现步骤或最小概念验证
|
||||||
|
- 相关配置、日志及部署信息(请先脱敏)
|
||||||
|
|
||||||
|
请勿在报告中包含真实账号凭据、认证密钥、API 令牌或个人数据。请仅在自己拥有或已获得授权的环境中进行安全测试。
|
||||||
|
|
||||||
|
## 适用范围
|
||||||
|
|
||||||
|
本政策适用于 NFD 官方项目中的安全漏洞,包括身份认证、访问控制、凭据处理及其他涉及安全的功能。
|
||||||
|
|
||||||
|
不涉及安全影响的普通缺陷、上游接口变更及兼容性问题,请通过 [GitHub Issues](https://github.com/qaiu/netdisk-fast-download/issues) 反馈。仅由第三方服务、非官方修改或不安全部署配置引起的问题,原则上不属于本政策的适用范围。但 NFD 默认配置或第三方服务处理逻辑中存在的安全漏洞仍在适用范围内。
|
||||||
|
|
||||||
|
## 安全响应与披露
|
||||||
|
|
||||||
|
维护者将根据漏洞严重程度、影响范围及可用时间尽力处理安全报告,包括漏洞评估、制定修复或缓解措施,并尽可能与报告人协调公开披露。不承诺固定的响应或修复时限。
|
||||||
|
|
||||||
|
## 安全最佳实践
|
||||||
|
|
||||||
|
- 及时更新 NFD 及其依赖组件。
|
||||||
|
- 使用 HTTPS,并限制敏感管理接口的访问权限。
|
||||||
|
- 妥善保管认证密钥、API 令牌及网盘凭据,切勿通过公开日志、前端构建产物或诊断报告泄露。
|
||||||
|
- 仅启用实际需要的服务及可选功能。
|
||||||
|
- 及时撤销或轮换可能泄露的凭据。
|
||||||
|
|
||||||
|
## 安全公告
|
||||||
|
|
||||||
|
已公开的漏洞、影响版本、修复及缓解措施,请参阅 [GitHub 安全公告](https://github.com/qaiu/netdisk-fast-download/security/advisories) 和 [版本发布说明](https://github.com/qaiu/netdisk-fast-download/releases)。
|
||||||
|
|
||||||
|
## 致谢
|
||||||
|
|
||||||
|
感谢所有负责任地报告漏洞并帮助提升 NFD 安全性的研究人员与社区成员。
|
||||||
Regular → Executable
+5
-2
@@ -1,6 +1,9 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
# set -x
|
# set -x
|
||||||
LAUNCH_JAR="netdisk-fast-download.jar"
|
LAUNCH_JAR="netdisk-fast-download.jar"
|
||||||
nohup java -Xmx512M -jar "$LAUNCH_JAR" "$@" >startup.log 2>&1 &
|
VECTOR_MODULE_OPT=
|
||||||
tail -f startup.log
|
if java --list-modules 2>/dev/null | grep -q '^jdk.incubator.vector@'; then
|
||||||
|
VECTOR_MODULE_OPT=--add-modules=jdk.incubator.vector
|
||||||
|
fi
|
||||||
|
exec java ${VECTOR_MODULE_OPT} -Xmx${JVM_XMX:-512M} ${JVM_OPTS} -jar "$LAUNCH_JAR" "$@"
|
||||||
|
|
||||||
+1
-15
@@ -65,21 +65,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.postgresql</groupId>
|
<groupId>org.postgresql</groupId>
|
||||||
<artifactId>postgresql</artifactId>
|
<artifactId>postgresql</artifactId>
|
||||||
<version>42.7.3</version>
|
<version>42.7.13</version>
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- 测试依赖 -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>junit</groupId>
|
|
||||||
<artifactId>junit</artifactId>
|
|
||||||
<version>4.13.2</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.projectlombok</groupId>
|
|
||||||
<artifactId>lombok</artifactId>
|
|
||||||
<version>1.18.38</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ public class CreateDatabase {
|
|||||||
stmt.executeUpdate("CREATE DATABASE IF NOT EXISTS " + dbName + " CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
|
stmt.executeUpdate("CREATE DATABASE IF NOT EXISTS " + dbName + " CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
|
||||||
LOGGER.info(">>>>>>>>>>> 数据库'{}'创建成功 <<<<<<<<<<<<", dbName);
|
LOGGER.info(">>>>>>>>>>> 数据库'{}'创建成功 <<<<<<<<<<<<", dbName);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
LOGGER.error("创建数据库失败", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,35 +24,39 @@ import java.util.*;
|
|||||||
* @author <a href="https://qaiu.top">QAIU</a>
|
* @author <a href="https://qaiu.top">QAIU</a>
|
||||||
*/
|
*/
|
||||||
public class CreateTable {
|
public class CreateTable {
|
||||||
public static Map<Class<?>, String> javaProperty2SqlColumnMap = new HashMap<>() {{
|
public static final Map<Class<?>, String> javaProperty2SqlColumnMap;
|
||||||
|
static {
|
||||||
|
Map<Class<?>, String> map = new HashMap<>();
|
||||||
// Java类型到SQL类型的映射
|
// Java类型到SQL类型的映射
|
||||||
put(Integer.class, "INT");
|
map.put(Integer.class, "INT");
|
||||||
put(Short.class, "SMALLINT");
|
map.put(Short.class, "SMALLINT");
|
||||||
put(Byte.class, "TINYINT");
|
map.put(Byte.class, "TINYINT");
|
||||||
put(Long.class, "BIGINT");
|
map.put(Long.class, "BIGINT");
|
||||||
put(java.math.BigDecimal.class, "DECIMAL");
|
map.put(java.math.BigDecimal.class, "DECIMAL");
|
||||||
put(Double.class, "DOUBLE");
|
map.put(Double.class, "DOUBLE");
|
||||||
put(Float.class, "REAL");
|
map.put(Float.class, "REAL");
|
||||||
put(Boolean.class, "BOOLEAN");
|
map.put(Boolean.class, "BOOLEAN");
|
||||||
put(String.class, "VARCHAR");
|
map.put(String.class, "VARCHAR");
|
||||||
put(Date.class, "TIMESTAMP");
|
map.put(Date.class, "TIMESTAMP");
|
||||||
put(java.time.LocalDateTime.class, "TIMESTAMP");
|
map.put(java.time.LocalDateTime.class, "TIMESTAMP");
|
||||||
put(java.sql.Timestamp.class, "TIMESTAMP");
|
map.put(java.sql.Timestamp.class, "TIMESTAMP");
|
||||||
put(java.sql.Date.class, "DATE");
|
map.put(java.sql.Date.class, "DATE");
|
||||||
put(java.sql.Time.class, "TIME");
|
map.put(java.sql.Time.class, "TIME");
|
||||||
|
|
||||||
// 基本数据类型
|
// 基本数据类型
|
||||||
put(int.class, "INT");
|
map.put(int.class, "INT");
|
||||||
put(short.class, "SMALLINT");
|
map.put(short.class, "SMALLINT");
|
||||||
put(byte.class, "TINYINT");
|
map.put(byte.class, "TINYINT");
|
||||||
put(long.class, "BIGINT");
|
map.put(long.class, "BIGINT");
|
||||||
put(double.class, "DOUBLE");
|
map.put(double.class, "DOUBLE");
|
||||||
put(float.class, "REAL");
|
map.put(float.class, "REAL");
|
||||||
put(boolean.class, "BOOLEAN");
|
map.put(boolean.class, "BOOLEAN");
|
||||||
}};
|
|
||||||
|
javaProperty2SqlColumnMap = Collections.unmodifiableMap(map);
|
||||||
|
}
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(CreateTable.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(CreateTable.class);
|
||||||
public static String UNIQUE_PREFIX = "idx_";
|
public static final String UNIQUE_PREFIX = "idx_";
|
||||||
|
|
||||||
private static Case getCase(Class<?> clz) {
|
private static Case getCase(Class<?> clz) {
|
||||||
return switch (clz.getName()) {
|
return switch (clz.getName()) {
|
||||||
@@ -303,7 +307,7 @@ public class CreateTable {
|
|||||||
return promise.future();
|
return promise.future();
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Future<Object>> createFutures = new ArrayList<>();
|
List<Future<Object>> futures = new ArrayList<>();
|
||||||
|
|
||||||
for (Class<?> clazz : tableClasses) {
|
for (Class<?> clazz : tableClasses) {
|
||||||
List<String> sqlList = getCreateTableSQL(clazz, type);
|
List<String> sqlList = getCreateTableSQL(clazz, type);
|
||||||
@@ -312,41 +316,23 @@ public class CreateTable {
|
|||||||
for (String sql : sqlList) {
|
for (String sql : sqlList) {
|
||||||
try {
|
try {
|
||||||
pool.query(sql).execute().toCompletionStage().toCompletableFuture().join();
|
pool.query(sql).execute().toCompletionStage().toCompletableFuture().join();
|
||||||
createFutures.add(Future.succeededFuture());
|
futures.add(Future.succeededFuture());
|
||||||
LOGGER.debug("Executed SQL:\n{}", sql);
|
LOGGER.debug("Executed SQL:\n{}", sql);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
String message = e.getMessage();
|
String message = e.getMessage();
|
||||||
if (message != null && message.contains("Duplicate key name")) {
|
if (message != null && message.contains("Duplicate key name")) {
|
||||||
LOGGER.warn("Ignoring duplicate key error: {}", message);
|
LOGGER.warn("Ignoring duplicate key error: {}", message);
|
||||||
createFutures.add(Future.succeededFuture());
|
futures.add(Future.succeededFuture());
|
||||||
} else {
|
} else {
|
||||||
LOGGER.error("SQL Error: {}\nSQL: {}", message, sql);
|
LOGGER.error("SQL Error: {}\nSQL: {}", message, sql);
|
||||||
createFutures.add(Future.failedFuture(e));
|
futures.add(Future.failedFuture(e));
|
||||||
throw new RuntimeException(e); // Stop execution for other exceptions
|
throw new RuntimeException(e); // Stop execution for other exceptions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建表完成后,执行表结构迁移检查
|
Future.all(futures).onSuccess(r -> promise.complete()).onFailure(promise::fail);
|
||||||
Future.all(createFutures)
|
|
||||||
.compose(v -> {
|
|
||||||
LOGGER.info("开始检查表结构变更...");
|
|
||||||
List<Future<Void>> migrationFutures = new ArrayList<>();
|
|
||||||
for (Class<?> clazz : tableClasses) {
|
|
||||||
migrationFutures.add(SchemaMigration.migrateTable(pool, clazz, type));
|
|
||||||
}
|
|
||||||
return Future.all(migrationFutures).mapEmpty();
|
|
||||||
})
|
|
||||||
.onSuccess(v -> {
|
|
||||||
LOGGER.info("表结构检查和变更完成");
|
|
||||||
promise.complete();
|
|
||||||
})
|
|
||||||
.onFailure(err -> {
|
|
||||||
LOGGER.error("表结构变更失败", err);
|
|
||||||
promise.fail(err);
|
|
||||||
});
|
|
||||||
|
|
||||||
return promise.future();
|
return promise.future();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,44 +0,0 @@
|
|||||||
package cn.qaiu.db.ddl;
|
|
||||||
|
|
||||||
import java.lang.annotation.ElementType;
|
|
||||||
import java.lang.annotation.Retention;
|
|
||||||
import java.lang.annotation.RetentionPolicy;
|
|
||||||
import java.lang.annotation.Target;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 标识新增字段,用于数据库表结构迁移
|
|
||||||
* 只有带此注解的字段才会被 SchemaMigration 检查和添加
|
|
||||||
*
|
|
||||||
* <p>使用场景:</p>
|
|
||||||
* <ul>
|
|
||||||
* <li>在现有实体类中添加新字段时,使用此注解标记</li>
|
|
||||||
* <li>应用启动时会自动检测并添加到数据库表中</li>
|
|
||||||
* <li>添加成功后可以移除此注解,避免重复检查</li>
|
|
||||||
* </ul>
|
|
||||||
*
|
|
||||||
* <p>示例:</p>
|
|
||||||
* <pre>{@code
|
|
||||||
* @Data
|
|
||||||
* @Table("users")
|
|
||||||
* public class User {
|
|
||||||
* private Long id;
|
|
||||||
* private String name;
|
|
||||||
*
|
|
||||||
* @NewField // 标记为新增字段
|
|
||||||
* @Length(varcharSize = 32)
|
|
||||||
* @Constraint(defaultValue = "active")
|
|
||||||
* private String status;
|
|
||||||
* }
|
|
||||||
* }</pre>
|
|
||||||
*
|
|
||||||
* @author <a href="https://qaiu.top">QAIU</a>
|
|
||||||
*/
|
|
||||||
@Target(ElementType.FIELD)
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
public @interface NewField {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 字段描述(可选)
|
|
||||||
*/
|
|
||||||
String value() default "";
|
|
||||||
}
|
|
||||||
@@ -1,294 +0,0 @@
|
|||||||
package cn.qaiu.db.ddl;
|
|
||||||
|
|
||||||
import cn.qaiu.db.pool.JDBCType;
|
|
||||||
import io.vertx.codegen.format.Case;
|
|
||||||
import io.vertx.codegen.format.LowerCamelCase;
|
|
||||||
import io.vertx.codegen.format.SnakeCase;
|
|
||||||
import io.vertx.core.Future;
|
|
||||||
import io.vertx.core.Promise;
|
|
||||||
import io.vertx.sqlclient.Pool;
|
|
||||||
import io.vertx.sqlclient.templates.annotations.Column;
|
|
||||||
import io.vertx.sqlclient.templates.annotations.RowMapped;
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 数据库表结构变更处理器
|
|
||||||
* 用于在应用启动时自动检测并添加缺失的字段
|
|
||||||
*
|
|
||||||
* @author <a href="https://qaiu.top">QAIU</a>
|
|
||||||
*/
|
|
||||||
public class SchemaMigration {
|
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(SchemaMigration.class);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检查并迁移表结构
|
|
||||||
* 只处理带有 @NewField 注解的字段,避免检查所有字段导致的重复错误
|
|
||||||
*
|
|
||||||
* @param pool 数据库连接池
|
|
||||||
* @param clazz 实体类
|
|
||||||
* @param type 数据库类型
|
|
||||||
* @return Future
|
|
||||||
*/
|
|
||||||
public static Future<Void> migrateTable(Pool pool, Class<?> clazz, JDBCType type) {
|
|
||||||
Promise<Void> promise = Promise.promise();
|
|
||||||
|
|
||||||
try {
|
|
||||||
String tableName = getTableName(clazz);
|
|
||||||
|
|
||||||
// 获取带有 @NewField 注解的字段
|
|
||||||
List<Field> newFields = getNewFields(clazz);
|
|
||||||
|
|
||||||
if (newFields.isEmpty()) {
|
|
||||||
log.debug("表 '{}' 没有标记为 @NewField 的字段,跳过结构检查", tableName);
|
|
||||||
promise.complete();
|
|
||||||
return promise.future();
|
|
||||||
}
|
|
||||||
|
|
||||||
log.info("开始检查表 '{}' 的结构变更,新增字段数: {}", tableName, newFields.size());
|
|
||||||
|
|
||||||
// 获取表的所有字段
|
|
||||||
getTableColumns(pool, tableName, type)
|
|
||||||
.compose(existingColumns -> {
|
|
||||||
// 只添加带有 @NewField 注解且不存在的字段
|
|
||||||
return addNewFields(pool, clazz, tableName, newFields, existingColumns, type);
|
|
||||||
})
|
|
||||||
.onSuccess(v -> {
|
|
||||||
log.info("表 '{}' 结构变更完成", tableName);
|
|
||||||
promise.complete();
|
|
||||||
})
|
|
||||||
.onFailure(err -> {
|
|
||||||
log.error("表 '{}' 结构变更失败", tableName, err);
|
|
||||||
promise.fail(err);
|
|
||||||
});
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("检查表结构失败", e);
|
|
||||||
promise.fail(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return promise.future();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取带有 @NewField 注解的字段列表
|
|
||||||
*/
|
|
||||||
private static List<Field> getNewFields(Class<?> clazz) {
|
|
||||||
List<Field> newFields = new ArrayList<>();
|
|
||||||
for (Field field : clazz.getDeclaredFields()) {
|
|
||||||
if (field.isAnnotationPresent(NewField.class) && !isIgnoredField(field)) {
|
|
||||||
newFields.add(field);
|
|
||||||
String desc = field.getAnnotation(NewField.class).value();
|
|
||||||
if (StringUtils.isNotEmpty(desc)) {
|
|
||||||
log.debug("发现新字段: {} - {}", field.getName(), desc);
|
|
||||||
} else {
|
|
||||||
log.debug("发现新字段: {}", field.getName());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return newFields;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取表名
|
|
||||||
*/
|
|
||||||
private static String getTableName(Class<?> clazz) {
|
|
||||||
if (clazz.isAnnotationPresent(Table.class)) {
|
|
||||||
Table annotation = clazz.getAnnotation(Table.class);
|
|
||||||
if (StringUtils.isNotEmpty(annotation.value())) {
|
|
||||||
return annotation.value();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 默认使用类名转下划线命名
|
|
||||||
Case caseFormat = SnakeCase.INSTANCE;
|
|
||||||
if (clazz.isAnnotationPresent(RowMapped.class)) {
|
|
||||||
RowMapped annotation = clazz.getAnnotation(RowMapped.class);
|
|
||||||
caseFormat = getCase(annotation.formatter());
|
|
||||||
}
|
|
||||||
return LowerCamelCase.INSTANCE.to(caseFormat, clazz.getSimpleName());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取表的现有字段
|
|
||||||
*/
|
|
||||||
private static Future<Set<String>> getTableColumns(Pool pool, String tableName, JDBCType type) {
|
|
||||||
Promise<Set<String>> promise = Promise.promise();
|
|
||||||
|
|
||||||
String sql = switch (type) {
|
|
||||||
case MySQL -> String.format(
|
|
||||||
"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '%s'",
|
|
||||||
tableName
|
|
||||||
);
|
|
||||||
case H2DB -> String.format(
|
|
||||||
"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = SCHEMA() AND TABLE_NAME = '%s'",
|
|
||||||
tableName.toUpperCase()
|
|
||||||
);
|
|
||||||
case PostgreSQL -> String.format(
|
|
||||||
"SELECT column_name FROM information_schema.columns WHERE table_name = '%s'",
|
|
||||||
tableName.toLowerCase()
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
pool.query(sql).execute()
|
|
||||||
.onSuccess(rows -> {
|
|
||||||
Set<String> columns = new HashSet<>();
|
|
||||||
rows.forEach(row -> {
|
|
||||||
String columnName = row.getString(0);
|
|
||||||
if (columnName != null) {
|
|
||||||
columns.add(columnName.toLowerCase());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
log.debug("表 '{}' 现有字段: {}", tableName, columns);
|
|
||||||
promise.complete(columns);
|
|
||||||
})
|
|
||||||
.onFailure(err -> {
|
|
||||||
log.warn("获取表 '{}' 字段列表失败,可能表不存在: {}", tableName, err.getMessage());
|
|
||||||
promise.complete(new HashSet<>()); // 返回空集合,触发创建表逻辑
|
|
||||||
});
|
|
||||||
|
|
||||||
return promise.future();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 添加新字段(只处理带 @NewField 注解的字段)
|
|
||||||
*/
|
|
||||||
private static Future<Void> addNewFields(Pool pool, Class<?> clazz, String tableName,
|
|
||||||
List<Field> newFields, Set<String> existingColumns,
|
|
||||||
JDBCType type) {
|
|
||||||
List<Future<Void>> futures = new ArrayList<>();
|
|
||||||
|
|
||||||
Case caseFormat = SnakeCase.INSTANCE;
|
|
||||||
if (clazz.isAnnotationPresent(RowMapped.class)) {
|
|
||||||
RowMapped annotation = clazz.getAnnotation(RowMapped.class);
|
|
||||||
caseFormat = getCase(annotation.formatter());
|
|
||||||
}
|
|
||||||
|
|
||||||
String quotationMarks = type == JDBCType.MySQL ? "`" : "\"";
|
|
||||||
|
|
||||||
for (Field field : newFields) {
|
|
||||||
// 获取字段名
|
|
||||||
String columnName;
|
|
||||||
if (field.isAnnotationPresent(Column.class)) {
|
|
||||||
Column annotation = field.getAnnotation(Column.class);
|
|
||||||
columnName = StringUtils.isNotEmpty(annotation.name())
|
|
||||||
? annotation.name()
|
|
||||||
: LowerCamelCase.INSTANCE.to(caseFormat, field.getName());
|
|
||||||
} else {
|
|
||||||
columnName = LowerCamelCase.INSTANCE.to(caseFormat, field.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查字段是否已存在
|
|
||||||
if (existingColumns.contains(columnName.toLowerCase())) {
|
|
||||||
log.warn("字段 '{}' 已存在,请移除 @NewField 注解", columnName);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成 ALTER TABLE 语句
|
|
||||||
String sql = buildAlterTableSQL(tableName, field, columnName, quotationMarks, type);
|
|
||||||
|
|
||||||
log.info("添加字段: {}", sql);
|
|
||||||
|
|
||||||
Promise<Void> p = Promise.promise();
|
|
||||||
pool.query(sql).execute()
|
|
||||||
.onSuccess(v -> {
|
|
||||||
log.info("字段 '{}' 添加成功", columnName);
|
|
||||||
p.complete();
|
|
||||||
})
|
|
||||||
.onFailure(err -> {
|
|
||||||
String errorMsg = err.getMessage();
|
|
||||||
// 如果字段已存在,忽略错误(可能是并发执行或检测失败)
|
|
||||||
if (errorMsg != null && (errorMsg.contains("Duplicate column") ||
|
|
||||||
errorMsg.contains("already exists") ||
|
|
||||||
errorMsg.contains("duplicate key"))) {
|
|
||||||
log.warn("字段 '{}' 已存在,跳过添加", columnName);
|
|
||||||
p.complete();
|
|
||||||
} else {
|
|
||||||
log.error("字段 '{}' 添加失败", columnName, err);
|
|
||||||
p.fail(err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
futures.add(p.future());
|
|
||||||
}
|
|
||||||
|
|
||||||
return Future.all(futures).mapEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 构建 ALTER TABLE 添加字段的 SQL
|
|
||||||
*/
|
|
||||||
private static String buildAlterTableSQL(String tableName, Field field, String columnName,
|
|
||||||
String quotationMarks, JDBCType type) {
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
sb.append("ALTER TABLE ").append(quotationMarks).append(tableName).append(quotationMarks)
|
|
||||||
.append(" ADD COLUMN ").append(quotationMarks).append(columnName).append(quotationMarks);
|
|
||||||
|
|
||||||
// 获取字段类型
|
|
||||||
String sqlType = CreateTable.javaProperty2SqlColumnMap.get(field.getType());
|
|
||||||
if (sqlType == null) {
|
|
||||||
sqlType = "VARCHAR";
|
|
||||||
}
|
|
||||||
sb.append(" ").append(sqlType);
|
|
||||||
|
|
||||||
// 添加类型长度
|
|
||||||
int[] decimalSize = {22, 2};
|
|
||||||
int varcharSize = 255;
|
|
||||||
if (field.isAnnotationPresent(Length.class)) {
|
|
||||||
Length length = field.getAnnotation(Length.class);
|
|
||||||
decimalSize = length.decimalSize();
|
|
||||||
varcharSize = length.varcharSize();
|
|
||||||
}
|
|
||||||
|
|
||||||
if ("DECIMAL".equals(sqlType)) {
|
|
||||||
sb.append("(").append(decimalSize[0]).append(",").append(decimalSize[1]).append(")");
|
|
||||||
} else if ("VARCHAR".equals(sqlType)) {
|
|
||||||
sb.append("(").append(varcharSize).append(")");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 添加约束
|
|
||||||
if (field.isAnnotationPresent(Constraint.class)) {
|
|
||||||
Constraint constraint = field.getAnnotation(Constraint.class);
|
|
||||||
|
|
||||||
if (constraint.notNull()) {
|
|
||||||
sb.append(" NOT NULL");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (StringUtils.isNotEmpty(constraint.defaultValue())) {
|
|
||||||
String apostrophe = constraint.defaultValueIsFunction() ? "" : "'";
|
|
||||||
sb.append(" DEFAULT ").append(apostrophe).append(constraint.defaultValue()).append(apostrophe);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断是否忽略字段
|
|
||||||
*/
|
|
||||||
private static boolean isIgnoredField(Field field) {
|
|
||||||
int modifiers = field.getModifiers();
|
|
||||||
return java.lang.reflect.Modifier.isStatic(modifiers)
|
|
||||||
|| java.lang.reflect.Modifier.isTransient(modifiers)
|
|
||||||
|| field.isAnnotationPresent(TableGenIgnore.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取 Case 类型
|
|
||||||
*/
|
|
||||||
private static Case getCase(Class<?> clz) {
|
|
||||||
return switch (clz.getName()) {
|
|
||||||
case "io.vertx.codegen.format.CamelCase" -> io.vertx.codegen.format.CamelCase.INSTANCE;
|
|
||||||
case "io.vertx.codegen.format.SnakeCase" -> SnakeCase.INSTANCE;
|
|
||||||
case "io.vertx.codegen.format.LowerCamelCase" -> LowerCamelCase.INSTANCE;
|
|
||||||
default -> SnakeCase.INSTANCE;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -17,7 +17,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
*
|
*
|
||||||
* @author <a href="https://qaiu.top">QAIU</a>
|
* @author <a href="https://qaiu.top">QAIU</a>
|
||||||
*/
|
*/
|
||||||
public class JDBCPoolInit {
|
public class JDBCPoolInit implements AutoCloseable {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(JDBCPoolInit.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(JDBCPoolInit.class);
|
||||||
|
|
||||||
@@ -101,4 +101,16 @@ public class JDBCPoolInit {
|
|||||||
synchronized public JDBCPool getPool() {
|
synchronized public JDBCPool getPool() {
|
||||||
return pool;
|
return pool;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭连接池,释放数据库资源
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public synchronized void close() {
|
||||||
|
if (pool != null) {
|
||||||
|
pool.close();
|
||||||
|
LOGGER.info("数据库连接池已关闭: URL={}", url);
|
||||||
|
pool = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,265 +0,0 @@
|
|||||||
package cn.qaiu.db.ddl;
|
|
||||||
|
|
||||||
import cn.qaiu.db.pool.JDBCType;
|
|
||||||
import io.vertx.core.Future;
|
|
||||||
import io.vertx.core.Vertx;
|
|
||||||
import io.vertx.jdbcclient.JDBCPool;
|
|
||||||
import io.vertx.sqlclient.templates.annotations.Column;
|
|
||||||
import lombok.Data;
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import java.util.concurrent.CountDownLatch;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* SchemaMigration 单元测试
|
|
||||||
*/
|
|
||||||
public class SchemaMigrationTest {
|
|
||||||
|
|
||||||
private Vertx vertx;
|
|
||||||
private JDBCPool pool;
|
|
||||||
//
|
|
||||||
// @Before
|
|
||||||
// public void setUp() {
|
|
||||||
// vertx = Vertx.vertx();
|
|
||||||
//
|
|
||||||
// // 创建 H2 内存数据库连接池
|
|
||||||
// pool = JDBCPool.pool(vertx,
|
|
||||||
// "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1",
|
|
||||||
// "sa",
|
|
||||||
// ""
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
|
|
||||||
@After
|
|
||||||
public void tearDown() {
|
|
||||||
if (pool != null) {
|
|
||||||
pool.close();
|
|
||||||
}
|
|
||||||
if (vertx != null) {
|
|
||||||
vertx.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 测试添加新字段
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testAddNewField() throws Exception {
|
|
||||||
CountDownLatch latch = new CountDownLatch(1);
|
|
||||||
|
|
||||||
// 1. 先创建一个基础表
|
|
||||||
String createTableSQL = """
|
|
||||||
CREATE TABLE test_user (
|
|
||||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
|
||||||
name VARCHAR(50) NOT NULL
|
|
||||||
)
|
|
||||||
""";
|
|
||||||
|
|
||||||
pool.query(createTableSQL).execute()
|
|
||||||
.compose(v -> {
|
|
||||||
// 2. 使用 SchemaMigration 添加新字段
|
|
||||||
return SchemaMigration.migrateTable(pool, TestUserWithNewField.class, JDBCType.H2DB);
|
|
||||||
})
|
|
||||||
.compose(v -> {
|
|
||||||
// 3. 验证新字段是否添加成功
|
|
||||||
return pool.query("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS " +
|
|
||||||
"WHERE TABLE_NAME = 'TEST_USER' AND COLUMN_NAME = 'EMAIL'")
|
|
||||||
.execute();
|
|
||||||
})
|
|
||||||
.onSuccess(rows -> {
|
|
||||||
assertEquals("应该找到新添加的 email 字段", 1, rows.size());
|
|
||||||
latch.countDown();
|
|
||||||
})
|
|
||||||
.onFailure(err -> {
|
|
||||||
fail("测试失败: " + err.getMessage());
|
|
||||||
latch.countDown();
|
|
||||||
});
|
|
||||||
|
|
||||||
assertTrue("测试超时", latch.await(10, TimeUnit.SECONDS));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 测试不添加已存在的字段
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testSkipExistingField() throws Exception {
|
|
||||||
CountDownLatch latch = new CountDownLatch(1);
|
|
||||||
|
|
||||||
// 1. 创建包含 email 字段的表
|
|
||||||
String createTableSQL = """
|
|
||||||
CREATE TABLE test_user2 (
|
|
||||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
|
||||||
name VARCHAR(50) NOT NULL,
|
|
||||||
email VARCHAR(100)
|
|
||||||
)
|
|
||||||
""";
|
|
||||||
|
|
||||||
pool.query(createTableSQL).execute()
|
|
||||||
.compose(v -> {
|
|
||||||
// 2. 尝试再次添加 email 字段(应该跳过)
|
|
||||||
return SchemaMigration.migrateTable(pool, TestUserWithNewField2.class, JDBCType.H2DB);
|
|
||||||
})
|
|
||||||
.onSuccess(v -> {
|
|
||||||
// 3. 验证表结构正常,没有错误
|
|
||||||
latch.countDown();
|
|
||||||
})
|
|
||||||
.onFailure(err -> {
|
|
||||||
fail("测试失败: " + err.getMessage());
|
|
||||||
latch.countDown();
|
|
||||||
});
|
|
||||||
|
|
||||||
assertTrue("测试超时", latch.await(10, TimeUnit.SECONDS));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 测试没有 @NewField 注解时不执行迁移
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testNoNewFieldAnnotation() throws Exception {
|
|
||||||
CountDownLatch latch = new CountDownLatch(1);
|
|
||||||
|
|
||||||
// 1. 创建基础表
|
|
||||||
String createTableSQL = """
|
|
||||||
CREATE TABLE test_user3 (
|
|
||||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
|
||||||
name VARCHAR(50) NOT NULL
|
|
||||||
)
|
|
||||||
""";
|
|
||||||
|
|
||||||
pool.query(createTableSQL).execute()
|
|
||||||
.compose(v -> {
|
|
||||||
// 2. 使用没有 @NewField 注解的实体类
|
|
||||||
return SchemaMigration.migrateTable(pool, TestUserNoAnnotation.class, JDBCType.H2DB);
|
|
||||||
})
|
|
||||||
.compose(v -> {
|
|
||||||
// 3. 验证没有添加 email 字段
|
|
||||||
return pool.query("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS " +
|
|
||||||
"WHERE TABLE_NAME = 'TEST_USER3' AND COLUMN_NAME = 'EMAIL'")
|
|
||||||
.execute();
|
|
||||||
})
|
|
||||||
.onSuccess(rows -> {
|
|
||||||
assertEquals("不应该添加没有 @NewField 注解的字段", 0, rows.size());
|
|
||||||
latch.countDown();
|
|
||||||
})
|
|
||||||
.onFailure(err -> {
|
|
||||||
fail("测试失败: " + err.getMessage());
|
|
||||||
latch.countDown();
|
|
||||||
});
|
|
||||||
|
|
||||||
assertTrue("测试超时", latch.await(10, TimeUnit.SECONDS));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 测试多个新字段同时添加
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testMultipleNewFields() throws Exception {
|
|
||||||
CountDownLatch latch = new CountDownLatch(1);
|
|
||||||
|
|
||||||
// 1. 创建基础表
|
|
||||||
String createTableSQL = """
|
|
||||||
CREATE TABLE test_user4 (
|
|
||||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
|
||||||
name VARCHAR(50) NOT NULL
|
|
||||||
)
|
|
||||||
""";
|
|
||||||
|
|
||||||
pool.query(createTableSQL).execute()
|
|
||||||
.compose(v -> {
|
|
||||||
// 2. 添加多个新字段
|
|
||||||
return SchemaMigration.migrateTable(pool, TestUserMultipleNewFields.class, JDBCType.H2DB);
|
|
||||||
})
|
|
||||||
.compose(v -> {
|
|
||||||
// 3. 验证所有新字段都添加成功
|
|
||||||
return pool.query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS " +
|
|
||||||
"WHERE TABLE_NAME = 'TEST_USER4' AND COLUMN_NAME IN ('EMAIL', 'PHONE', 'ADDRESS')")
|
|
||||||
.execute();
|
|
||||||
})
|
|
||||||
.onSuccess(rows -> {
|
|
||||||
int count = rows.iterator().next().getInteger(0);
|
|
||||||
assertEquals("应该添加 3 个新字段", 3, count);
|
|
||||||
latch.countDown();
|
|
||||||
})
|
|
||||||
.onFailure(err -> {
|
|
||||||
fail("测试失败: " + err.getMessage());
|
|
||||||
latch.countDown();
|
|
||||||
});
|
|
||||||
|
|
||||||
assertTrue("测试超时", latch.await(10, TimeUnit.SECONDS));
|
|
||||||
}
|
|
||||||
|
|
||||||
// ========== 测试实体类 ==========
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@Table("test_user")
|
|
||||||
static class TestUserWithNewField {
|
|
||||||
@Constraint(autoIncrement = true)
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
@Length(varcharSize = 50)
|
|
||||||
@Constraint(notNull = true)
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
@NewField("用户邮箱")
|
|
||||||
@Length(varcharSize = 100)
|
|
||||||
private String email;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@Table("test_user2")
|
|
||||||
static class TestUserWithNewField2 {
|
|
||||||
@Constraint(autoIncrement = true)
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
@Length(varcharSize = 50)
|
|
||||||
@Constraint(notNull = true)
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
@NewField("用户邮箱")
|
|
||||||
@Length(varcharSize = 100)
|
|
||||||
private String email;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@Table("test_user3")
|
|
||||||
static class TestUserNoAnnotation {
|
|
||||||
@Constraint(autoIncrement = true)
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
@Length(varcharSize = 50)
|
|
||||||
@Constraint(notNull = true)
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
// 没有 @NewField 注解
|
|
||||||
@Length(varcharSize = 100)
|
|
||||||
private String email;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@Table("test_user4")
|
|
||||||
static class TestUserMultipleNewFields {
|
|
||||||
@Constraint(autoIncrement = true)
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
@Length(varcharSize = 50)
|
|
||||||
@Constraint(notNull = true)
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
@NewField("用户邮箱")
|
|
||||||
@Length(varcharSize = 100)
|
|
||||||
private String email;
|
|
||||||
|
|
||||||
@NewField("手机号")
|
|
||||||
@Length(varcharSize = 20)
|
|
||||||
private String phone;
|
|
||||||
|
|
||||||
@NewField("地址")
|
|
||||||
@Length(varcharSize = 255)
|
|
||||||
private String address;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -73,6 +73,12 @@
|
|||||||
<version>${jackson.version}</version>
|
<version>${jackson.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
@@ -1,73 +0,0 @@
|
|||||||
package cn.qaiu.vx.core.verticle.conf;
|
|
||||||
|
|
||||||
import io.vertx.core.json.JsonObject;
|
|
||||||
import io.vertx.core.json.JsonArray;
|
|
||||||
import io.vertx.core.json.impl.JsonUtil;
|
|
||||||
import java.time.Instant;
|
|
||||||
import java.time.format.DateTimeFormatter;
|
|
||||||
import java.util.Base64;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Converter and mapper for {@link cn.qaiu.vx.core.verticle.conf.HttpProxyConf}.
|
|
||||||
* NOTE: This class has been automatically generated from the {@link cn.qaiu.vx.core.verticle.conf.HttpProxyConf} original class using Vert.x codegen.
|
|
||||||
*/
|
|
||||||
public class HttpProxyConfConverter {
|
|
||||||
|
|
||||||
|
|
||||||
private static final Base64.Decoder BASE64_DECODER = JsonUtil.BASE64_DECODER;
|
|
||||||
private static final Base64.Encoder BASE64_ENCODER = JsonUtil.BASE64_ENCODER;
|
|
||||||
|
|
||||||
static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, HttpProxyConf obj) {
|
|
||||||
for (java.util.Map.Entry<String, Object> member : json) {
|
|
||||||
switch (member.getKey()) {
|
|
||||||
case "password":
|
|
||||||
if (member.getValue() instanceof String) {
|
|
||||||
obj.setPassword((String)member.getValue());
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case "port":
|
|
||||||
if (member.getValue() instanceof Number) {
|
|
||||||
obj.setPort(((Number)member.getValue()).intValue());
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case "preProxyOptions":
|
|
||||||
if (member.getValue() instanceof JsonObject) {
|
|
||||||
obj.setPreProxyOptions(new io.vertx.core.net.ProxyOptions((io.vertx.core.json.JsonObject)member.getValue()));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case "timeout":
|
|
||||||
if (member.getValue() instanceof Number) {
|
|
||||||
obj.setTimeout(((Number)member.getValue()).intValue());
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case "username":
|
|
||||||
if (member.getValue() instanceof String) {
|
|
||||||
obj.setUsername((String)member.getValue());
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void toJson(HttpProxyConf obj, JsonObject json) {
|
|
||||||
toJson(obj, json.getMap());
|
|
||||||
}
|
|
||||||
|
|
||||||
static void toJson(HttpProxyConf obj, java.util.Map<String, Object> json) {
|
|
||||||
if (obj.getPassword() != null) {
|
|
||||||
json.put("password", obj.getPassword());
|
|
||||||
}
|
|
||||||
if (obj.getPort() != null) {
|
|
||||||
json.put("port", obj.getPort());
|
|
||||||
}
|
|
||||||
if (obj.getPreProxyOptions() != null) {
|
|
||||||
json.put("preProxyOptions", obj.getPreProxyOptions().toJson());
|
|
||||||
}
|
|
||||||
if (obj.getTimeout() != null) {
|
|
||||||
json.put("timeout", obj.getTimeout());
|
|
||||||
}
|
|
||||||
if (obj.getUsername() != null) {
|
|
||||||
json.put("username", obj.getUsername());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -3,20 +3,26 @@ package cn.qaiu.vx.core;
|
|||||||
import cn.qaiu.vx.core.util.CommonUtil;
|
import cn.qaiu.vx.core.util.CommonUtil;
|
||||||
import cn.qaiu.vx.core.util.ConfigUtil;
|
import cn.qaiu.vx.core.util.ConfigUtil;
|
||||||
import cn.qaiu.vx.core.util.VertxHolder;
|
import cn.qaiu.vx.core.util.VertxHolder;
|
||||||
|
import cn.qaiu.vx.core.verticle.HttpProxyVerticle;
|
||||||
|
import cn.qaiu.vx.core.verticle.PostExecVerticle;
|
||||||
import cn.qaiu.vx.core.verticle.ReverseProxyVerticle;
|
import cn.qaiu.vx.core.verticle.ReverseProxyVerticle;
|
||||||
import cn.qaiu.vx.core.verticle.RouterVerticle;
|
import cn.qaiu.vx.core.verticle.RouterVerticle;
|
||||||
import cn.qaiu.vx.core.verticle.ServiceVerticle;
|
import cn.qaiu.vx.core.verticle.ServiceVerticle;
|
||||||
import io.vertx.core.*;
|
import io.vertx.core.*;
|
||||||
import io.vertx.core.dns.AddressResolverOptions;
|
import io.vertx.core.dns.AddressResolverOptions;
|
||||||
import io.vertx.core.impl.launcher.commands.VersionCommand;
|
|
||||||
import io.vertx.core.json.JsonObject;
|
import io.vertx.core.json.JsonObject;
|
||||||
import io.vertx.core.shareddata.LocalMap;
|
import io.vertx.core.shareddata.LocalMap;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.lang.management.ManagementFactory;
|
import java.lang.management.ManagementFactory;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.concurrent.locks.LockSupport;
|
import java.util.concurrent.locks.LockSupport;
|
||||||
|
|
||||||
import static cn.qaiu.vx.core.util.ConfigConstant.*;
|
import static cn.qaiu.vx.core.util.ConfigConstant.*;
|
||||||
@@ -41,11 +47,25 @@ public final class Deploy {
|
|||||||
private Handler<JsonObject> handle;
|
private Handler<JsonObject> handle;
|
||||||
|
|
||||||
private Thread mainThread;
|
private Thread mainThread;
|
||||||
|
private final List<Runnable> preShutdownTasks = new CopyOnWriteArrayList<>();
|
||||||
|
private final List<Runnable> postShutdownTasks = new CopyOnWriteArrayList<>();
|
||||||
|
|
||||||
public static Deploy instance() {
|
public static Deploy instance() {
|
||||||
return INSTANCE;
|
return INSTANCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addPreShutdownTask(Runnable task) {
|
||||||
|
if (task != null) {
|
||||||
|
preShutdownTasks.add(task);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addPostShutdownTask(Runnable task) {
|
||||||
|
if (task != null) {
|
||||||
|
postShutdownTasks.add(task);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param args 启动参数
|
* @param args 启动参数
|
||||||
@@ -54,15 +74,25 @@ public final class Deploy {
|
|||||||
public void start(String[] args, Handler<JsonObject> handle) {
|
public void start(String[] args, Handler<JsonObject> handle) {
|
||||||
this.mainThread = Thread.currentThread();
|
this.mainThread = Thread.currentThread();
|
||||||
this.handle = handle;
|
this.handle = handle;
|
||||||
|
|
||||||
if (args.length > 0 && args[0].startsWith("app-")) {
|
if (args.length > 0 && args[0].startsWith("app-")) {
|
||||||
// 启动参数dev或者prod
|
// 启动参数dev或者prod
|
||||||
path.append("-").append(args[0].replace("app-",""));
|
path.append("-").append(args[0].replace("app-",""));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取yml配置
|
// 读取yml配置,优先当前目录,其次 resources/ 子目录
|
||||||
|
String configFile = path + ".yml";
|
||||||
|
if (!Files.exists(Path.of(configFile)) && Files.exists(Path.of("resources", configFile))) {
|
||||||
|
path.insert(0, "resources/");
|
||||||
|
LOGGER.info("从 resources/ 目录加载配置: {}", path + ".yml");
|
||||||
|
}
|
||||||
ConfigUtil.readYamlConfig(path.toString(), tempVertx)
|
ConfigUtil.readYamlConfig(path.toString(), tempVertx)
|
||||||
.onSuccess(this::readConf)
|
.onSuccess(this::readConf)
|
||||||
.onFailure(Throwable::printStackTrace);
|
.onFailure(err -> {
|
||||||
|
LOGGER.error("读取配置文件失败: {}", err.getMessage(), err);
|
||||||
|
LockSupport.unpark(mainThread);
|
||||||
|
System.exit(-1);
|
||||||
|
});
|
||||||
LockSupport.park();
|
LockSupport.park();
|
||||||
deployVerticle();
|
deployVerticle();
|
||||||
}
|
}
|
||||||
@@ -104,7 +134,7 @@ public final class Deploy {
|
|||||||
|
|
||||||
System.out.printf(logoTemplate,
|
System.out.printf(logoTemplate,
|
||||||
CommonUtil.getAppVersion(),
|
CommonUtil.getAppVersion(),
|
||||||
VersionCommand.getVersion(),
|
"4x",
|
||||||
conf.getString("copyright"),
|
conf.getString("copyright"),
|
||||||
year
|
year
|
||||||
);
|
);
|
||||||
@@ -119,44 +149,112 @@ public final class Deploy {
|
|||||||
customConfig = globalConfig.getJsonObject(CUSTOM);
|
customConfig = globalConfig.getJsonObject(CUSTOM);
|
||||||
|
|
||||||
JsonObject vertxConfig = globalConfig.getJsonObject(VERTX);
|
JsonObject vertxConfig = globalConfig.getJsonObject(VERTX);
|
||||||
Integer vertxConfigELPS = vertxConfig.getInteger(EVENT_LOOP_POOL_SIZE);
|
JsonObject vertxOptionsConfig = vertxConfig.copy();
|
||||||
var vertxOptions = vertxConfigELPS == 0 ?
|
if (vertxOptionsConfig.getInteger(EVENT_LOOP_POOL_SIZE, 0) == 0) {
|
||||||
new VertxOptions() : new VertxOptions(vertxConfig);
|
vertxOptionsConfig.remove(EVENT_LOOP_POOL_SIZE);
|
||||||
|
}
|
||||||
|
if (vertxOptionsConfig.getInteger("workerPoolSize", 0) == 0) {
|
||||||
|
vertxOptionsConfig.remove("workerPoolSize");
|
||||||
|
}
|
||||||
|
Integer vertxConfigELPS = vertxConfig.getInteger(EVENT_LOOP_POOL_SIZE, 0);
|
||||||
|
var vertxOptions = vertxOptionsConfig.isEmpty() ?
|
||||||
|
new VertxOptions() : new VertxOptions(vertxOptionsConfig);
|
||||||
|
|
||||||
vertxOptions.setAddressResolverOptions(
|
// vertxOptions.setAddressResolverOptions(
|
||||||
new AddressResolverOptions().
|
// new AddressResolverOptions().
|
||||||
addServer("114.114.114.114").
|
// addServer("114.114.114.114").
|
||||||
addServer("114.114.115.115").
|
// addServer("114.114.115.115").
|
||||||
addServer("8.8.8.8").
|
// addServer("8.8.8.8").
|
||||||
addServer("8.8.4.4"));
|
// addServer("8.8.4.4"));
|
||||||
LOGGER.info("vertxConfigEventLoopPoolSize: {}, eventLoopPoolSize: {}, workerPoolSize: {}", vertxConfigELPS,
|
LOGGER.info("vertxConfigEventLoopPoolSize: {}, eventLoopPoolSize: {}, workerPoolSize: {}", vertxConfigELPS,
|
||||||
vertxOptions.getEventLoopPoolSize(),
|
vertxOptions.getEventLoopPoolSize(),
|
||||||
vertxOptions.getWorkerPoolSize());
|
vertxOptions.getWorkerPoolSize());
|
||||||
var vertx = Vertx.vertx(vertxOptions);
|
var vertx = Vertx.vertx(vertxOptions);
|
||||||
VertxHolder.init(vertx);
|
VertxHolder.init(vertx);
|
||||||
|
|
||||||
|
// 注册 ShutdownHook,确保进程退出时优雅关闭资源
|
||||||
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||||
|
LOGGER.info("JVM shutting down...");
|
||||||
|
runShutdownTasks("before Vert.x close", preShutdownTasks);
|
||||||
|
try {
|
||||||
|
LOGGER.info("Closing Vert.x...");
|
||||||
|
vertx.close().toCompletionStage().toCompletableFuture().get(10, java.util.concurrent.TimeUnit.SECONDS);
|
||||||
|
LOGGER.info("Vert.x closed successfully");
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.warn("Vert.x close error or timeout", e);
|
||||||
|
} finally {
|
||||||
|
runShutdownTasks("after Vert.x close", postShutdownTasks);
|
||||||
|
}
|
||||||
|
}));
|
||||||
//配置保存在共享数据中
|
//配置保存在共享数据中
|
||||||
var sharedData = vertx.sharedData();
|
var sharedData = vertx.sharedData();
|
||||||
LocalMap<String, Object> localMap = sharedData.getLocalMap(LOCAL);
|
LocalMap<String, Object> localMap = sharedData.getLocalMap(LOCAL);
|
||||||
localMap.put(GLOBAL_CONFIG, globalConfig);
|
localMap.put(GLOBAL_CONFIG, globalConfig);
|
||||||
localMap.put(CUSTOM_CONFIG, customConfig);
|
localMap.put(CUSTOM_CONFIG, customConfig);
|
||||||
localMap.put(SERVER, globalConfig.getJsonObject(SERVER));
|
localMap.put(SERVER, globalConfig.getJsonObject(SERVER));
|
||||||
var future0 = vertx.createSharedWorkerExecutor("other-handle")
|
WorkerExecutor otherHandleExecutor = vertx.createSharedWorkerExecutor("other-handle");
|
||||||
.executeBlocking(() -> {
|
var future0 = otherHandleExecutor.executeBlocking(() -> {
|
||||||
handle.handle(globalConfig);
|
handle.handle(globalConfig);
|
||||||
return "Other handle complete";
|
return "Other handle complete";
|
||||||
});
|
});
|
||||||
|
|
||||||
future0.onSuccess(res -> {
|
future0.onSuccess(res -> {
|
||||||
|
otherHandleExecutor.close();
|
||||||
LOGGER.info(res);
|
LOGGER.info(res);
|
||||||
// 部署 路由、异步service、反向代理 服务
|
// 部署 路由、异步service、反向代理 服务
|
||||||
var future1 = vertx.deployVerticle(RouterVerticle.class, getWorkDeploymentOptions("Router"));
|
var future1 = vertx.deployVerticle(RouterVerticle.class, getWorkDeploymentOptions("Router"));
|
||||||
var future2 = vertx.deployVerticle(ServiceVerticle.class, getWorkDeploymentOptions("Service"));
|
var future2 = vertx.deployVerticle(ServiceVerticle.class, getWorkDeploymentOptions("Service"));
|
||||||
var future3 = vertx.deployVerticle(ReverseProxyVerticle.class, getWorkDeploymentOptions("proxy"));
|
var future3 = vertx.deployVerticle(ReverseProxyVerticle.class, getWorkDeploymentOptions("proxy", 1));
|
||||||
|
|
||||||
|
|
||||||
|
JsonObject jsonObject = ((JsonObject) localMap.get(GLOBAL_CONFIG)).getJsonObject("proxy-server");
|
||||||
|
if (jsonObject != null) {
|
||||||
|
genPwd(jsonObject);
|
||||||
|
var future4 = vertx.deployVerticle(HttpProxyVerticle.class, getWorkDeploymentOptions("proxy", 1));
|
||||||
|
future4.onSuccess(LOGGER::info);
|
||||||
|
future4.onFailure(e -> LOGGER.error("Other handle error", e));
|
||||||
|
Future.all(future1, future2, future3, future4)
|
||||||
|
.onSuccess(this::deployWorkVerticalSuccess)
|
||||||
|
.onFailure(this::deployVerticalFailed);
|
||||||
|
} else {
|
||||||
Future.all(future1, future2, future3)
|
Future.all(future1, future2, future3)
|
||||||
.onSuccess(this::deployWorkVerticalSuccess)
|
.onSuccess(this::deployWorkVerticalSuccess)
|
||||||
.onFailure(this::deployVerticalFailed);
|
.onFailure(this::deployVerticalFailed);
|
||||||
}).onFailure(e -> LOGGER.error("Other handle error", e));
|
}
|
||||||
|
|
||||||
|
}).onFailure(e -> {
|
||||||
|
otherHandleExecutor.close();
|
||||||
|
LOGGER.error("Other handle error", e);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void genPwd(JsonObject jsonObject) {
|
||||||
|
if (jsonObject.getBoolean("randUserPwd")) {
|
||||||
|
var username = UUID.randomUUID().toString().replace("-", "").substring(0, 16);
|
||||||
|
var password = UUID.randomUUID().toString().replace("-", "").substring(0, 16);
|
||||||
|
jsonObject.put("username", username);
|
||||||
|
jsonObject.put("password", password);
|
||||||
|
}
|
||||||
|
LOGGER.info("=============server info=================");
|
||||||
|
LOGGER.info("\nport: {}\nusername: {}\npassword: {}",
|
||||||
|
jsonObject.getString("port"),
|
||||||
|
jsonObject.getString("username"),
|
||||||
|
jsonObject.getString("password"));
|
||||||
|
LOGGER.info("==============server info================");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void runShutdownTasks(String stage, List<Runnable> tasks) {
|
||||||
|
if (tasks.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
LOGGER.info("Running {} shutdown tasks: {}", stage, tasks.size());
|
||||||
|
for (Runnable task : tasks) {
|
||||||
|
try {
|
||||||
|
task.run();
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.warn("Shutdown task failed at stage {}", stage, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -178,6 +276,42 @@ public final class Deploy {
|
|||||||
var t1 = ((double) (System.currentTimeMillis() - startTime)) / 1000;
|
var t1 = ((double) (System.currentTimeMillis() - startTime)) / 1000;
|
||||||
var t2 = ((double) System.currentTimeMillis() - ManagementFactory.getRuntimeMXBean().getStartTime()) / 1000;
|
var t2 = ((double) System.currentTimeMillis() - ManagementFactory.getRuntimeMXBean().getStartTime()) / 1000;
|
||||||
LOGGER.info("web服务启动成功 -> 用时: {}s, jvm启动用时: {}s", t1, t2);
|
LOGGER.info("web服务启动成功 -> 用时: {}s, jvm启动用时: {}s", t1, t2);
|
||||||
|
|
||||||
|
// 检查是否处于安装引导模式(数据库未配置)
|
||||||
|
Object installMode = VertxHolder.getVertxInstance().sharedData()
|
||||||
|
.getLocalMap(LOCAL).get("installMode");
|
||||||
|
if (Boolean.TRUE.equals(installMode)) {
|
||||||
|
LOGGER.info("系统处于安装引导模式,等待用户完成数据库配置后再启动后置初始化...");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 正常模式:部署 PostExecVerticle 执行 AppRun 实现
|
||||||
|
deployPostExec();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 部署 PostExecVerticle(执行所有 AppRun 实现)
|
||||||
|
* 安装引导完成后也可手动调用此方法触发后置初始化
|
||||||
|
*/
|
||||||
|
public void deployPostExec() {
|
||||||
|
var vertx = VertxHolder.getVertxInstance();
|
||||||
|
var postExecFuture = vertx.deployVerticle(PostExecVerticle.class, getWorkDeploymentOptions("postExec", 2));
|
||||||
|
postExecFuture.onSuccess(id -> {
|
||||||
|
LOGGER.info("PostExecVerticle 部署成功,AppRun 实现执行完成");
|
||||||
|
}).onFailure(e -> {
|
||||||
|
LOGGER.error("PostExecVerticle 部署失败", e);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重新部署 ServiceVerticle,重新注册因 DB 未就绪而失败的服务到 EventBus
|
||||||
|
* 安装引导完成、DB 初始化后调用
|
||||||
|
*/
|
||||||
|
public void redeployServices() {
|
||||||
|
var vertx = VertxHolder.getVertxInstance();
|
||||||
|
vertx.deployVerticle(ServiceVerticle.class, getWorkDeploymentOptions("Service"))
|
||||||
|
.onSuccess(id -> LOGGER.info("ServiceVerticle 重新部署成功,DB 相关服务已注册"))
|
||||||
|
.onFailure(e -> LOGGER.error("ServiceVerticle 重新部署失败", e));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import java.lang.annotation.*;
|
|||||||
public @interface HandleSortFilter {
|
public @interface HandleSortFilter {
|
||||||
/**
|
/**
|
||||||
* 注册顺序,数字越大越先注册<br>
|
* 注册顺序,数字越大越先注册<br>
|
||||||
|
* 前置拦截器会先执行后注册即数字小的, 后置拦截器会先执行先注册的即数字大的<br>
|
||||||
* 值<0时会过滤掉该处理器
|
* 值<0时会过滤掉该处理器
|
||||||
*/
|
*/
|
||||||
int value() default 0;
|
int value() default 0;
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package cn.qaiu.vx.core.base;
|
||||||
|
|
||||||
|
import io.vertx.core.json.JsonObject;
|
||||||
|
|
||||||
|
public interface AppRun {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行方法
|
||||||
|
* @param config 启动配置文件
|
||||||
|
*/
|
||||||
|
void execute(JsonObject config);
|
||||||
|
}
|
||||||
@@ -1,14 +1,20 @@
|
|||||||
package cn.qaiu.vx.core.base;
|
package cn.qaiu.vx.core.base;
|
||||||
|
|
||||||
|
import cn.qaiu.vx.core.annotaions.HandleSortFilter;
|
||||||
import cn.qaiu.vx.core.interceptor.AfterInterceptor;
|
import cn.qaiu.vx.core.interceptor.AfterInterceptor;
|
||||||
import cn.qaiu.vx.core.model.JsonResult;
|
import cn.qaiu.vx.core.model.JsonResult;
|
||||||
import cn.qaiu.vx.core.util.CommonUtil;
|
|
||||||
import cn.qaiu.vx.core.util.ReflectionUtil;
|
import cn.qaiu.vx.core.util.ReflectionUtil;
|
||||||
import io.vertx.core.json.JsonObject;
|
import io.vertx.core.json.JsonObject;
|
||||||
import io.vertx.ext.web.RoutingContext;
|
import io.vertx.ext.web.RoutingContext;
|
||||||
import org.reflections.Reflections;
|
import org.reflections.Reflections;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static cn.qaiu.vx.core.util.ResponseUtil.*;
|
import static cn.qaiu.vx.core.util.ResponseUtil.*;
|
||||||
|
|
||||||
@@ -22,9 +28,10 @@ public interface BaseHttpApi {
|
|||||||
|
|
||||||
// 需要扫描注册的Router路径
|
// 需要扫描注册的Router路径
|
||||||
Reflections reflections = ReflectionUtil.getReflections();
|
Reflections reflections = ReflectionUtil.getReflections();
|
||||||
|
Logger LOGGER = LoggerFactory.getLogger(BaseHttpApi.class);
|
||||||
|
|
||||||
default void doFireJsonObjectResponse(RoutingContext ctx, JsonObject jsonObject) {
|
default void doFireJsonObjectResponse(RoutingContext ctx, JsonObject jsonObject) {
|
||||||
if (!ctx.response().ended()) {
|
if (!isResponseDone(ctx)) {
|
||||||
fireJsonObjectResponse(ctx, jsonObject);
|
fireJsonObjectResponse(ctx, jsonObject);
|
||||||
}
|
}
|
||||||
handleAfterInterceptor(ctx, jsonObject);
|
handleAfterInterceptor(ctx, jsonObject);
|
||||||
@@ -32,31 +39,93 @@ public interface BaseHttpApi {
|
|||||||
|
|
||||||
|
|
||||||
default <T> void doFireJsonResultResponse(RoutingContext ctx, JsonResult<T> jsonResult) {
|
default <T> void doFireJsonResultResponse(RoutingContext ctx, JsonResult<T> jsonResult) {
|
||||||
if (!ctx.response().ended()) {
|
if (!isResponseDone(ctx)) {
|
||||||
fireJsonResultResponse(ctx, jsonResult);
|
fireJsonResultResponse(ctx, jsonResult);
|
||||||
}
|
}
|
||||||
handleAfterInterceptor(ctx, jsonResult.toJsonObject());
|
handleAfterInterceptor(ctx, jsonResult.toJsonObject());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default void doFireJsonObjectResponse(RoutingContext ctx, JsonObject jsonObject, int statusCode) {
|
||||||
|
if (!isResponseDone(ctx)) {
|
||||||
|
fireJsonObjectResponse(ctx, jsonObject, statusCode);
|
||||||
|
}
|
||||||
|
handleAfterInterceptor(ctx, jsonObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
default <T> void doFireJsonResultResponse(RoutingContext ctx, JsonResult<T> jsonResult, int statusCode) {
|
||||||
|
if (!isResponseDone(ctx)) {
|
||||||
|
fireJsonResultResponse(ctx, jsonResult, statusCode);
|
||||||
|
}
|
||||||
|
handleAfterInterceptor(ctx, jsonResult.toJsonObject());
|
||||||
|
}
|
||||||
|
|
||||||
default Set<AfterInterceptor> getAfterInterceptor() {
|
default Set<AfterInterceptor> getAfterInterceptor() {
|
||||||
|
return AfterInterceptorHolder.INSTANCES;
|
||||||
|
}
|
||||||
|
|
||||||
|
class AfterInterceptorHolder {
|
||||||
|
private static final Set<AfterInterceptor> INSTANCES = loadAfterInterceptors();
|
||||||
|
|
||||||
|
private static Set<AfterInterceptor> loadAfterInterceptors() {
|
||||||
Set<Class<? extends AfterInterceptor>> afterInterceptorClassSet =
|
Set<Class<? extends AfterInterceptor>> afterInterceptorClassSet =
|
||||||
reflections.getSubTypesOf(AfterInterceptor.class);
|
reflections.getSubTypesOf(AfterInterceptor.class);
|
||||||
if (afterInterceptorClassSet == null) {
|
if (afterInterceptorClassSet == null || afterInterceptorClassSet.isEmpty()) {
|
||||||
|
return Collections.emptySet();
|
||||||
|
}
|
||||||
|
return afterInterceptorClassSet.stream()
|
||||||
|
.filter(AfterInterceptorHolder::isEnabled)
|
||||||
|
.sorted(AfterInterceptorHolder::compareOrder)
|
||||||
|
.map(AfterInterceptorHolder::newInterceptor)
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.collect(Collectors.collectingAndThen(
|
||||||
|
Collectors.toCollection(LinkedHashSet::new),
|
||||||
|
Collections::unmodifiableSet));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isEnabled(Class<? extends AfterInterceptor> clazz) {
|
||||||
|
HandleSortFilter sort = clazz.getAnnotation(HandleSortFilter.class);
|
||||||
|
return sort == null || sort.value() >= 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int compareOrder(Class<? extends AfterInterceptor> left, Class<? extends AfterInterceptor> right) {
|
||||||
|
return Integer.compare(order(left), order(right));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int order(Class<? extends AfterInterceptor> clazz) {
|
||||||
|
HandleSortFilter sort = clazz.getAnnotation(HandleSortFilter.class);
|
||||||
|
return sort == null ? 0 : sort.value();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static AfterInterceptor newInterceptor(Class<? extends AfterInterceptor> clazz) {
|
||||||
|
try {
|
||||||
|
return ReflectionUtil.newWithNoParam(clazz);
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.warn("AfterInterceptor 初始化失败,已跳过: {}", clazz.getName(), e);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return CommonUtil.sortClassSet(afterInterceptorClassSet);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
default void handleAfterInterceptor(RoutingContext ctx, JsonObject jsonObject) {
|
default void handleAfterInterceptor(RoutingContext ctx, JsonObject jsonObject) {
|
||||||
Set<AfterInterceptor> afterInterceptor = getAfterInterceptor();
|
if (ctx.response().closed()) {
|
||||||
if (afterInterceptor != null) {
|
return;
|
||||||
afterInterceptor.forEach(ai -> ai.handle(ctx, jsonObject));
|
|
||||||
}
|
}
|
||||||
if (!ctx.response().ended()) {
|
Set<AfterInterceptor> afterInterceptor = getAfterInterceptor();
|
||||||
|
afterInterceptor.forEach(ai -> {
|
||||||
|
try {
|
||||||
|
ai.handle(ctx, jsonObject);
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.warn("AfterInterceptor 执行失败: {}", ai.getClass().getName(), e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (!isResponseDone(ctx)) {
|
||||||
fireTextResponse(ctx, "handleAfterInterceptor: response not end");
|
fireTextResponse(ctx, "handleAfterInterceptor: response not end");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default boolean isResponseDone(RoutingContext ctx) {
|
||||||
|
return ctx.response().ended() || ctx.response().closed();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package cn.qaiu.vx.core.base;
|
||||||
|
|
||||||
|
import cn.qaiu.vx.core.annotaions.HandleSortFilter;
|
||||||
|
import io.vertx.core.json.JsonObject;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认的AppRun实现示例
|
||||||
|
* <br>Create date 2024-01-01 00:00:00
|
||||||
|
*
|
||||||
|
* @author <a href="https://qaiu.top">QAIU</a>
|
||||||
|
*/
|
||||||
|
@HandleSortFilter
|
||||||
|
public class DefaultAppRun implements AppRun {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultAppRun.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(JsonObject config) {
|
||||||
|
LOGGER.info("======> AppRun实现类开始执行,配置数: {}", config.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,8 +23,6 @@ import io.vertx.ext.web.RoutingContext;
|
|||||||
import io.vertx.ext.web.handler.*;
|
import io.vertx.ext.web.handler.*;
|
||||||
import io.vertx.ext.web.handler.sockjs.SockJSHandler;
|
import io.vertx.ext.web.handler.sockjs.SockJSHandler;
|
||||||
import io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions;
|
import io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions;
|
||||||
import io.vertx.ext.web.sstore.LocalSessionStore;
|
|
||||||
import io.vertx.ext.web.sstore.SessionStore;
|
|
||||||
import javassist.CtClass;
|
import javassist.CtClass;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.commons.lang3.tuple.Pair;
|
import org.apache.commons.lang3.tuple.Pair;
|
||||||
@@ -36,6 +34,7 @@ import java.lang.reflect.InvocationTargetException;
|
|||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@@ -69,120 +68,22 @@ public class RouterHandlerFactory implements BaseHttpApi {
|
|||||||
this.gatewayPrefix = gatewayPrefix;
|
this.gatewayPrefix = gatewayPrefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 在主路由上直接注册 WebSocket 路由
|
|
||||||
* 必须使用 order(-1000) 确保在所有拦截器之前执行
|
|
||||||
*/
|
|
||||||
private void registerWebSocketRoutes(Router mainRouter) {
|
|
||||||
try {
|
|
||||||
Set<Class<?>> handlers = reflections.getTypesAnnotatedWith(RouteHandler.class);
|
|
||||||
for (Class<?> handler : handlers) {
|
|
||||||
String root = getRootPath(handler);
|
|
||||||
Method[] methods = handler.getMethods();
|
|
||||||
|
|
||||||
for (Method method : methods) {
|
|
||||||
if (method.isAnnotationPresent(SockRouteMapper.class)) {
|
|
||||||
SockRouteMapper mapping = method.getAnnotation(SockRouteMapper.class);
|
|
||||||
String routeUrl = getRouteUrl(mapping.value());
|
|
||||||
String url = root.concat(routeUrl);
|
|
||||||
|
|
||||||
// 在这里创建实例,确保每个 handler 使用同一个实例
|
|
||||||
final Object instance = ReflectionUtil.newWithNoParam(handler);
|
|
||||||
final Method finalMethod = method;
|
|
||||||
|
|
||||||
LOGGER.info("========================================");
|
|
||||||
LOGGER.info("注册 WebSocket Handler (主路由,优先级最高):");
|
|
||||||
LOGGER.info(" 类: {}", handler.getName());
|
|
||||||
LOGGER.info(" 方法: {}", method.getName());
|
|
||||||
LOGGER.info(" 实例: {}", instance.getClass().getName());
|
|
||||||
LOGGER.info(" 完整路径: {}/*", url);
|
|
||||||
LOGGER.info("========================================");
|
|
||||||
|
|
||||||
SockJSHandlerOptions options = new SockJSHandlerOptions()
|
|
||||||
.setHeartbeatInterval(2000)
|
|
||||||
.setRegisterWriteHandler(true);
|
|
||||||
|
|
||||||
SockJSHandler sockJSHandler = SockJSHandler.create(VertxHolder.getVertxInstance(), options);
|
|
||||||
|
|
||||||
// SockJS 路径处理
|
|
||||||
String sockJsPath = url;
|
|
||||||
while (sockJsPath.endsWith("/") || sockJsPath.endsWith("*")) {
|
|
||||||
sockJsPath = sockJsPath.substring(0, sockJsPath.length() - 1);
|
|
||||||
}
|
|
||||||
final String finalSockJsPath = sockJsPath;
|
|
||||||
|
|
||||||
// ✅ socketHandler() 返回 Router,用于挂载
|
|
||||||
// 使用 final 变量确保闭包中引用正确
|
|
||||||
Router sockJsRouter = sockJSHandler.socketHandler(sock -> {
|
|
||||||
LOGGER.info("[WS] ==========================================");
|
|
||||||
LOGGER.info("[WS] SockJS socketHandler 回调被调用!");
|
|
||||||
LOGGER.info("[WS] Socket ID: {}", sock.writeHandlerID());
|
|
||||||
LOGGER.info("[WS] Remote Address: {}", sock.remoteAddress());
|
|
||||||
LOGGER.info("[WS] Local Address: {}", sock.localAddress());
|
|
||||||
LOGGER.info("[WS] 即将调用 method: {}.{}", instance.getClass().getSimpleName(), finalMethod.getName());
|
|
||||||
LOGGER.info("[WS] ==========================================");
|
|
||||||
try {
|
|
||||||
finalMethod.invoke(instance, sock);
|
|
||||||
LOGGER.info("[WS] Handler 调用成功");
|
|
||||||
} catch (Throwable e) {
|
|
||||||
LOGGER.error("[WS] WebSocket handler 调用失败", e);
|
|
||||||
if (e.getCause() != null) {
|
|
||||||
LOGGER.error("[WS] 原始异常", e.getCause());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// 添加调试 handler 来检查请求是否到达 SockJS 路径
|
|
||||||
// 注意:使用 "path*" 格式与 SockJS subRouter 保持一致
|
|
||||||
mainRouter.route(finalSockJsPath + "*").order(-1001).handler(ctx -> {
|
|
||||||
LOGGER.info("[WS-DEBUG] 请求到达 SockJS 路径: {}", ctx.request().path());
|
|
||||||
LOGGER.info("[WS-DEBUG] Method: {}, Upgrade: {}, Connection: {}",
|
|
||||||
ctx.request().method(),
|
|
||||||
ctx.request().headers().get("Upgrade"),
|
|
||||||
ctx.request().headers().get("Connection"));
|
|
||||||
ctx.next();
|
|
||||||
});
|
|
||||||
|
|
||||||
// 为 SockJS xhr/xhr_send 路径添加 BodyHandler
|
|
||||||
// 必须在 SockJS 路由之前,但 WebSocket 升级请求不需要
|
|
||||||
mainRouter.route(finalSockJsPath + "*").order(-1000).handler(BodyHandler.create());
|
|
||||||
|
|
||||||
// ✅ 挂载 SockJS 路由 - 注意:subRouter 需要使用 "path*" 格式而不是 "path/*"
|
|
||||||
mainRouter.route(finalSockJsPath + "*").order(-999).subRouter(sockJsRouter);
|
|
||||||
|
|
||||||
LOGGER.info("✅ WebSocket 路由注册完成: {} (order=-1000)", finalSockJsPath);
|
|
||||||
LOGGER.info(" SockJS 端点: {}/info, {}/websocket, {}/xhr", finalSockJsPath, finalSockJsPath, finalSockJsPath);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
LOGGER.error("注册 WebSocket 路由失败", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 开始扫描并注册handler
|
* 开始扫描并注册handler
|
||||||
*/
|
*/
|
||||||
public Router createRouter() {
|
public Router createRouter() {
|
||||||
// 主路由
|
// 主路由
|
||||||
Router mainRouter = Router.router(VertxHolder.getVertxInstance());
|
Router mainRouter = Router.router(VertxHolder.getVertxInstance());
|
||||||
|
|
||||||
// ⚠️ 重要:先注册 WebSocket 路由,必须在所有 handler 之前
|
|
||||||
// SockJSHandler 不能在 subRouter 中,必须直接挂载到主路由
|
|
||||||
// 注意:WebSocket 路由必须在 BodyHandler 之前注册,否则会干扰 WebSocket 升级
|
|
||||||
registerWebSocketRoutes(mainRouter);
|
|
||||||
|
|
||||||
mainRouter.route().handler(ctx -> {
|
mainRouter.route().handler(ctx -> {
|
||||||
String realPath = ctx.request().uri();
|
String realPath = ctx.request().uri();
|
||||||
|
|
||||||
if (realPath.startsWith(REROUTE_PATH_PREFIX)) {
|
if (realPath.startsWith(REROUTE_PATH_PREFIX)) {
|
||||||
// vertx web proxy暂不支持rewrite, 所以这里进行手动替换, 请求地址中的请求path前缀替换为originPath
|
// vertx web proxy暂不支持rewrite, 所以这里进行手动替换, 请求地址中的请求path前缀替换为originPath
|
||||||
String rePath = realPath.substring(REROUTE_PATH_PREFIX.length());
|
String rePath = realPath.replace(REROUTE_PATH_PREFIX, "");
|
||||||
ctx.reroute(rePath);
|
ctx.reroute(rePath);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOGGER.debug("The HTTP service request address information ===>path:{}, uri:{}, method:{}",
|
LOGGER.debug("New request:{}, {}, {}",
|
||||||
ctx.request().path(), ctx.request().absoluteURI(), ctx.request().method());
|
ctx.request().path(), ctx.request().absoluteURI(), ctx.request().method());
|
||||||
ctx.response().headers().add(ACCESS_CONTROL_ALLOW_ORIGIN, "*");
|
ctx.response().headers().add(ACCESS_CONTROL_ALLOW_ORIGIN, "*");
|
||||||
ctx.response().headers().add(DATE, LocalDateTime.now().format(ISO_LOCAL_DATE_TIME));
|
ctx.response().headers().add(DATE, LocalDateTime.now().format(ISO_LOCAL_DATE_TIME));
|
||||||
@@ -196,24 +97,13 @@ public class RouterHandlerFactory implements BaseHttpApi {
|
|||||||
mainRouter.route().handler(CorsHandler.create().addRelativeOrigin(".*").allowCredentials(true).allowedMethods(httpMethods));
|
mainRouter.route().handler(CorsHandler.create().addRelativeOrigin(".*").allowCredentials(true).allowedMethods(httpMethods));
|
||||||
|
|
||||||
// 配置文件上传路径
|
// 配置文件上传路径
|
||||||
// BodyHandler 用于处理 POST 请求体
|
mainRouter.route().handler(BodyHandler.create()
|
||||||
// SockJS 的 xhr/xhr_send 端点需要 BodyHandler,但 WebSocket 升级请求不需要
|
.setUploadsDirectory("uploads")
|
||||||
// 因此为 SockJS 路径单独配置 BodyHandler(排除 websocket 子路径)
|
.setBodyLimit(2L * 1024 * 1024));
|
||||||
mainRouter.route().handler(BodyHandler.create().setUploadsDirectory("uploads"));
|
|
||||||
|
|
||||||
// 配置Session管理 - 用于演练场登录状态持久化
|
// 拦截器
|
||||||
// 30天过期时间(毫秒)- 排除 WebSocket 路径
|
|
||||||
SessionStore sessionStore = LocalSessionStore.create(VertxHolder.getVertxInstance());
|
|
||||||
SessionHandler sessionHandler = SessionHandler.create(sessionStore)
|
|
||||||
.setSessionTimeout(30L * 24 * 60 * 60 * 1000) // 30天
|
|
||||||
.setSessionCookieName("SESSIONID") // Cookie名称
|
|
||||||
.setCookieHttpOnlyFlag(true) // 防止XSS攻击
|
|
||||||
.setCookieSecureFlag(false); // 非HTTPS环境设置为false
|
|
||||||
mainRouter.routeWithRegex("^(?!/v2/ws/).*").handler(sessionHandler);
|
|
||||||
|
|
||||||
// 拦截器 - 排除 WebSocket 路径
|
|
||||||
Set<Handler<RoutingContext>> interceptorSet = getInterceptorSet();
|
Set<Handler<RoutingContext>> interceptorSet = getInterceptorSet();
|
||||||
Route route0 = mainRouter.routeWithRegex("^(?!/v2/ws/).*");
|
Route route0 = mainRouter.route("/*");
|
||||||
interceptorSet.forEach(route0::handler);
|
interceptorSet.forEach(route0::handler);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -240,8 +130,9 @@ public class RouterHandlerFactory implements BaseHttpApi {
|
|||||||
// 错误请求处理
|
// 错误请求处理
|
||||||
mainRouter.errorHandler(405, ctx -> doFireJsonResultResponse(ctx, JsonResult
|
mainRouter.errorHandler(405, ctx -> doFireJsonResultResponse(ctx, JsonResult
|
||||||
.error("Method Not Allowed", 405)));
|
.error("Method Not Allowed", 405)));
|
||||||
mainRouter.errorHandler(404, ctx -> ctx.response().setStatusCode(404).setChunked(true)
|
mainRouter.errorHandler(404, ctx -> {
|
||||||
.end("Internal server error: 404 not found"));
|
ctx.response().setStatusCode(404).end("404 not found");
|
||||||
|
});
|
||||||
|
|
||||||
return mainRouter;
|
return mainRouter;
|
||||||
}
|
}
|
||||||
@@ -287,19 +178,38 @@ public class RouterHandlerFactory implements BaseHttpApi {
|
|||||||
route.handler(TimeoutHandler.create(SharedDataUtil.getCustomConfig().getInteger(ROUTE_TIME_OUT)));
|
route.handler(TimeoutHandler.create(SharedDataUtil.getCustomConfig().getInteger(ROUTE_TIME_OUT)));
|
||||||
route.handler(ResponseTimeHandler.create());
|
route.handler(ResponseTimeHandler.create());
|
||||||
route.handler(ctx -> handlerMethod(instance, method, ctx)).failureHandler(ctx -> {
|
route.handler(ctx -> handlerMethod(instance, method, ctx)).failureHandler(ctx -> {
|
||||||
if (ctx.response().ended()) return;
|
if (isResponseDone(ctx)) return;
|
||||||
// 超时处理器状态码503
|
// 超时处理器状态码503
|
||||||
if (ctx.statusCode() == 503 || ctx.failure() == null) {
|
if (ctx.statusCode() == 503 || ctx.failure() == null) {
|
||||||
doFireJsonResultResponse(ctx, JsonResult.error("未知异常, 请联系管理员", 500));
|
doFireJsonResultResponse(ctx, JsonResult.error("未知异常, 请联系管理员"), 503);
|
||||||
} else {
|
} else {
|
||||||
ctx.failure().printStackTrace();
|
LOGGER.error("路由处理失败", ctx.failure());
|
||||||
doFireJsonResultResponse(ctx, JsonResult.error(ctx.failure().getMessage(), 500));
|
String msg = ctx.failure() != null ? ctx.failure().getMessage() : "未知异常";
|
||||||
|
doFireJsonResultResponse(ctx, JsonResult.error(msg), 500);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else if (method.isAnnotationPresent(SockRouteMapper.class)) {
|
} else if (method.isAnnotationPresent(SockRouteMapper.class)) {
|
||||||
// WebSocket 路由已在 registerWebSocketRoutes() 中提前注册
|
// websocket 基于sockJs
|
||||||
// 跳过此处,避免重复注册
|
SockRouteMapper mapping = method.getAnnotation(SockRouteMapper.class);
|
||||||
continue;
|
String routeUrl = getRouteUrl(mapping.value());
|
||||||
|
String url = root.concat(routeUrl);
|
||||||
|
LOGGER.info("Register New Websocket Handler -> {}", url);
|
||||||
|
SockJSHandlerOptions options = new SockJSHandlerOptions()
|
||||||
|
.setHeartbeatInterval(2000)
|
||||||
|
.setRegisterWriteHandler(true);
|
||||||
|
|
||||||
|
SockJSHandler sockJSHandler = SockJSHandler.create(VertxHolder.getVertxInstance(), options);
|
||||||
|
Router route = sockJSHandler.socketHandler(sock -> {
|
||||||
|
try {
|
||||||
|
ReflectionUtil.invokeWithArguments(method, instance, sock);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
LOGGER.error("WebSocket处理异常", e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (url.endsWith("*")) {
|
||||||
|
throw new IllegalArgumentException("Don't include * when mounting a sub router");
|
||||||
|
}
|
||||||
|
router.route(url + "*").subRouter(route);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -329,7 +239,7 @@ public class RouterHandlerFactory implements BaseHttpApi {
|
|||||||
*/
|
*/
|
||||||
private Set<Handler<RoutingContext>> getInterceptorSet() {
|
private Set<Handler<RoutingContext>> getInterceptorSet() {
|
||||||
// 配置拦截
|
// 配置拦截
|
||||||
return getBeforeInterceptor().stream().map(BeforeInterceptor::doHandle).collect(Collectors.toSet());
|
return getBeforeInterceptor().stream().map(BeforeInterceptor::doHandle).collect(Collectors.toCollection(LinkedHashSet::new));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -398,32 +308,41 @@ public class RouterHandlerFactory implements BaseHttpApi {
|
|||||||
|
|
||||||
final MultiMap queryParams = ctx.queryParams();
|
final MultiMap queryParams = ctx.queryParams();
|
||||||
// 解析body-json参数
|
// 解析body-json参数
|
||||||
// 只处理POST/PUT/PATCH等有body的请求方法,避免GET请求读取body导致"Request has already been read"错误
|
if (HttpHeaderValues.APPLICATION_JSON.toString().equals(ctx.parsedHeaders().contentType().value())) {
|
||||||
String httpMethod = ctx.request().method().name();
|
|
||||||
if (("POST".equals(httpMethod) || "PUT".equals(httpMethod) || "PATCH".equals(httpMethod))
|
|
||||||
&& HttpHeaderValues.APPLICATION_JSON.toString().equals(ctx.parsedHeaders().contentType().value())
|
|
||||||
&& ctx.body() != null && ctx.body().asJsonObject() != null) {
|
|
||||||
JsonObject body = ctx.body().asJsonObject();
|
JsonObject body = ctx.body().asJsonObject();
|
||||||
if (body != null) {
|
if (body != null) {
|
||||||
methodParametersTemp.forEach((k, v) -> {
|
methodParametersTemp.forEach((k, v) -> {
|
||||||
|
String typeName = v.getRight().getName();
|
||||||
|
// 直接绑定 JsonObject 类型参数
|
||||||
|
if (JsonObject.class.getName().equals(typeName)) {
|
||||||
|
parameterValueList.put(k, body);
|
||||||
|
}
|
||||||
// 只解析已配置包名前缀的实体类
|
// 只解析已配置包名前缀的实体类
|
||||||
if (CommonUtil.matchRegList(entityPackagesReg.getList(), v.getRight().getName())) {
|
else if (CommonUtil.matchRegList(entityPackagesReg.getList(), typeName)) {
|
||||||
try {
|
try {
|
||||||
Class<?> aClass = Class.forName(v.getRight().getName());
|
Class<?> aClass = Class.forName(typeName);
|
||||||
JsonObject data = CommonUtil.getSubJsonForEntity(body, aClass);
|
JsonObject data = CommonUtil.getSubJsonForEntity(body, aClass);
|
||||||
if (!data.isEmpty()) {
|
if (!data.isEmpty()) {
|
||||||
Object entity = data.mapTo(aClass);
|
Object entity = data.mapTo(aClass);
|
||||||
parameterValueList.put(k, entity);
|
parameterValueList.put(k, entity);
|
||||||
}
|
}
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
e.printStackTrace();
|
LOGGER.error("实体类绑定异常: {}", typeName, e);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// body 可能是 JsonArray
|
||||||
|
JsonArray bodyArray = ctx.body().asJsonArray();
|
||||||
|
if (bodyArray != null) {
|
||||||
|
methodParametersTemp.forEach((k, v) -> {
|
||||||
|
if (JsonArray.class.getName().equals(v.getRight().getName())) {
|
||||||
|
parameterValueList.put(k, bodyArray);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else if (("POST".equals(httpMethod) || "PUT".equals(httpMethod) || "PATCH".equals(httpMethod))
|
}
|
||||||
&& ctx.body() != null) {
|
} else if (ctx.body() != null) {
|
||||||
queryParams.addAll(ParamUtil.paramsToMap(ctx.body().asString()));
|
queryParams.addAll(ParamUtil.paramsToMap(ctx.body().asString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -451,7 +370,21 @@ public class RouterHandlerFactory implements BaseHttpApi {
|
|||||||
Object entity = ParamUtil.multiMapToEntity(queryParams, aClass);
|
Object entity = ParamUtil.multiMapToEntity(queryParams, aClass);
|
||||||
parameterValueList.put(k, entity);
|
parameterValueList.put(k, entity);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
LOGGER.error("参数绑定异常: {}", v.getRight().getName(), e);
|
||||||
|
}
|
||||||
|
} else if (parameterValueList.get(k) == null
|
||||||
|
&& JsonObject.class.getName().equals(v.getRight().getName())) {
|
||||||
|
// 兜底: content-type 非 application/json 时尝试从 body 解析 JsonObject
|
||||||
|
if (ctx.body() != null) {
|
||||||
|
JsonObject jo = ctx.body().asJsonObject();
|
||||||
|
if (jo != null) parameterValueList.put(k, jo);
|
||||||
|
}
|
||||||
|
} else if (parameterValueList.get(k) == null
|
||||||
|
&& JsonArray.class.getName().equals(v.getRight().getName())) {
|
||||||
|
// 兜底: content-type 非 application/json 时尝试从 body 解析 JsonArray
|
||||||
|
if (ctx.body() != null) {
|
||||||
|
JsonArray ja = ctx.body().asJsonArray();
|
||||||
|
if (ja != null) parameterValueList.put(k, ja);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -462,40 +395,45 @@ public class RouterHandlerFactory implements BaseHttpApi {
|
|||||||
Object data = ReflectionUtil.invokeWithArguments(method, instance, parameterValueArray);
|
Object data = ReflectionUtil.invokeWithArguments(method, instance, parameterValueArray);
|
||||||
if (data != null) {
|
if (data != null) {
|
||||||
|
|
||||||
if (data instanceof JsonResult) {
|
if (data instanceof JsonResult jsonResult) {
|
||||||
doFireJsonResultResponse(ctx, (JsonResult<?>) data);
|
doFireJsonResultResponse(ctx, (JsonResult<?>) data, jsonResult.getCode());
|
||||||
}
|
} else if (data instanceof JsonObject) {
|
||||||
if (data instanceof JsonObject) {
|
|
||||||
doFireJsonObjectResponse(ctx, ((JsonObject) data));
|
doFireJsonObjectResponse(ctx, ((JsonObject) data));
|
||||||
} else if (data instanceof Future) { // 处理异步响应
|
} else if (data instanceof Future) { // 处理异步响应
|
||||||
((Future<?>) data).onSuccess(res -> {
|
Future<?> responseFuture = (Future<?>) data;
|
||||||
if (res instanceof JsonResult) {
|
AtomicReference<RoutingContext> ctxRef = new AtomicReference<>(ctx);
|
||||||
doFireJsonResultResponse(ctx, (JsonResult<?>) res);
|
ctx.addEndHandler(v -> ctxRef.set(null));
|
||||||
|
responseFuture.onComplete(ar -> {
|
||||||
|
RoutingContext responseCtx = ctxRef.getAndSet(null);
|
||||||
|
if (responseCtx == null || isResponseDone(responseCtx)) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if (res instanceof JsonObject) {
|
if (ar.succeeded()) {
|
||||||
doFireJsonObjectResponse(ctx, ((JsonObject) res));
|
Object res = ar.result();
|
||||||
|
if (res instanceof JsonResult jsonResult) {
|
||||||
|
doFireJsonResultResponse(responseCtx, jsonResult, jsonResult.getCode());
|
||||||
|
} else if (res instanceof JsonObject) {
|
||||||
|
doFireJsonObjectResponse(responseCtx, ((JsonObject) res));
|
||||||
} else if (res != null) {
|
} else if (res != null) {
|
||||||
doFireJsonResultResponse(ctx, JsonResult.data(res));
|
doFireJsonResultResponse(responseCtx, JsonResult.data(res));
|
||||||
} else {
|
} else {
|
||||||
handleAfterInterceptor(ctx, null);
|
doFireJsonResultResponse(responseCtx, JsonResult.data(null));
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
}).onFailure(e -> doFireJsonResultResponse(ctx, JsonResult.error(e.getMessage())));
|
Throwable e = ar.cause();
|
||||||
|
LOGGER.error("请求处理失败", e);
|
||||||
|
String msg = e != null && e.getMessage() != null ? e.getMessage() : "服务器内部错误";
|
||||||
|
doFireJsonResultResponse(responseCtx, JsonResult.error(msg), 500);
|
||||||
|
}
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
doFireJsonResultResponse(ctx, JsonResult.data(data));
|
doFireJsonResultResponse(ctx, JsonResult.data(data));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
e.printStackTrace();
|
LOGGER.error("请求处理异常", e);
|
||||||
String err = e.getMessage();
|
String msg = e.getMessage() != null ? e.getMessage() : "服务器内部错误";
|
||||||
if (e.getCause() != null) {
|
doFireJsonResultResponse(ctx, JsonResult.error(msg), 500);
|
||||||
if (e.getCause() instanceof InvocationTargetException) {
|
|
||||||
err = ((InvocationTargetException) e.getCause()).getTargetException().getMessage();
|
|
||||||
} else {
|
|
||||||
err = e.getCause().getMessage();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
doFireJsonResultResponse(ctx, JsonResult.error(err));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,10 +3,12 @@ package cn.qaiu.vx.core.interceptor;
|
|||||||
import io.vertx.core.Handler;
|
import io.vertx.core.Handler;
|
||||||
import io.vertx.ext.web.RoutingContext;
|
import io.vertx.ext.web.RoutingContext;
|
||||||
|
|
||||||
import static cn.qaiu.vx.core.util.ResponseUtil.sendError;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 前置拦截器接口
|
* 前置拦截器接口
|
||||||
|
* <p>
|
||||||
|
* 注意:Vert.x是异步非阻塞框架,不能在Event Loop中使用synchronized等阻塞操作!
|
||||||
|
* 所有操作都应该是非阻塞的,使用Vert.x的上下文数据存储机制保证线程安全。
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* @author <a href="https://qaiu.top">QAIU</a>
|
* @author <a href="https://qaiu.top">QAIU</a>
|
||||||
*/
|
*/
|
||||||
@@ -14,28 +16,25 @@ public interface BeforeInterceptor extends Handler<RoutingContext> {
|
|||||||
String IS_NEXT = "RoutingContextIsNext";
|
String IS_NEXT = "RoutingContextIsNext";
|
||||||
|
|
||||||
default Handler<RoutingContext> doHandle() {
|
default Handler<RoutingContext> doHandle() {
|
||||||
|
|
||||||
return ctx -> {
|
return ctx -> {
|
||||||
// 加同步锁
|
// 【优化】移除synchronized锁,Vert.x的RoutingContext本身就是线程安全的
|
||||||
synchronized (BeforeInterceptor.class) {
|
// 每个请求都有独立的RoutingContext,不需要额外加锁
|
||||||
ctx.put(IS_NEXT, false);
|
ctx.put(IS_NEXT, false);
|
||||||
BeforeInterceptor.this.handle(ctx);
|
handle(ctx); // 调用具体的处理逻辑
|
||||||
if (!(Boolean) ctx.get(IS_NEXT) && !ctx.response().ended()) {
|
// 确保如果没有调用doNext()并且响应未结束,则返回错误
|
||||||
sendError(ctx, 403);
|
// if (!(Boolean) ctx.get(IS_NEXT) && !ctx.response().ended()) {
|
||||||
}
|
// sendError(ctx, 403);
|
||||||
}
|
// }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
default void doNext(RoutingContext context) {
|
default void doNext(RoutingContext context) {
|
||||||
// 设置上下文状态为可以继续执行
|
// 【优化】移除synchronized锁
|
||||||
// 添加同步锁保障多线程下执行时序
|
// RoutingContext的put和next操作是线程安全的,不需要额外同步
|
||||||
synchronized (BeforeInterceptor.class) {
|
|
||||||
context.put(IS_NEXT, true);
|
context.put(IS_NEXT, true);
|
||||||
context.next();
|
context.next(); // 继续执行下一个处理器
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle(RoutingContext context);
|
void handle(RoutingContext context); // 实现具体的拦截处理逻辑
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,7 +30,7 @@ public class JsonResult<T> implements Serializable {
|
|||||||
|
|
||||||
private int code = SUCCESS_CODE;//状态码
|
private int code = SUCCESS_CODE;//状态码
|
||||||
|
|
||||||
private String msg = SUCCESS_MESSAGE; //消息
|
private String msg = SUCCESS_MESSAGE;//消息
|
||||||
|
|
||||||
private boolean success = true; //是否成功
|
private boolean success = true; //是否成功
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* ModuleGen cn.qaiu.vx.core
|
* ModuleGen cn.qaiu.vx.core
|
||||||
*/
|
*/
|
||||||
@ModuleGen(name = "vertx-http-proxy", groupPackage = "cn.qaiu.vx.core", useFutures = true)
|
@ModuleGen(name = "vertx-http-proxy", groupPackage = "cn.qaiu.vx.core")
|
||||||
package cn.qaiu.vx.core;
|
package cn.qaiu.vx.core;
|
||||||
|
|
||||||
import io.vertx.codegen.annotations.ModuleGen;
|
import io.vertx.codegen.annotations.ModuleGen;
|
||||||
@@ -5,7 +5,7 @@ import io.vertx.serviceproxy.ServiceProxyBuilder;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Xu Haidong
|
* @author Xu Haidong
|
||||||
* Create at 2018/8/15
|
* @date 2018/8/15
|
||||||
*/
|
*/
|
||||||
public final class AsyncServiceUtil {
|
public final class AsyncServiceUtil {
|
||||||
|
|
||||||
|
|||||||
@@ -13,9 +13,12 @@ import java.net.Socket;
|
|||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -28,6 +31,16 @@ public class CommonUtil {
|
|||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(CommonUtil.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(CommonUtil.class);
|
||||||
|
|
||||||
|
/** 正则表达式缓存,避免每次调用重新编译 */
|
||||||
|
private static final ConcurrentHashMap<String, Pattern> PATTERN_CACHE = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取预编译的 Pattern(带缓存)
|
||||||
|
*/
|
||||||
|
private static Pattern getCachedPattern(String regex) {
|
||||||
|
return PATTERN_CACHE.computeIfAbsent(regex, Pattern::compile);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 匹配正则list
|
* 匹配正则list
|
||||||
*
|
*
|
||||||
@@ -38,7 +51,7 @@ public class CommonUtil {
|
|||||||
public static boolean matchRegList(List<?> regList, String destStr) {
|
public static boolean matchRegList(List<?> regList, String destStr) {
|
||||||
// 判断是否忽略
|
// 判断是否忽略
|
||||||
for (Object ignores : regList) {
|
for (Object ignores : regList) {
|
||||||
if (destStr.matches(ignores.toString())) {
|
if (getCachedPattern(ignores.toString()).matcher(destStr).matches()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -117,7 +130,7 @@ public class CommonUtil {
|
|||||||
return set.stream().filter(c1 -> {
|
return set.stream().filter(c1 -> {
|
||||||
HandleSortFilter s1 = c1.getAnnotation(HandleSortFilter.class);
|
HandleSortFilter s1 = c1.getAnnotation(HandleSortFilter.class);
|
||||||
if (s1 != null) {
|
if (s1 != null) {
|
||||||
return s1.value() > 0;
|
return s1.value() >= 0;
|
||||||
} else {
|
} else {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -138,7 +151,7 @@ public class CommonUtil {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}).collect(Collectors.toSet());
|
}).collect(Collectors.toCollection(LinkedHashSet::new));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String appVersion;
|
private static String appVersion;
|
||||||
@@ -146,13 +159,20 @@ public class CommonUtil {
|
|||||||
public static String getAppVersion() {
|
public static String getAppVersion() {
|
||||||
if (null == appVersion) {
|
if (null == appVersion) {
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
try {
|
try (var is = CommonUtil.class.getClassLoader().getResourceAsStream("app.properties")) {
|
||||||
properties.load(CommonUtil.class.getClassLoader().getResourceAsStream("app.properties"));
|
if (is != null) {
|
||||||
if (!properties.isEmpty()) {
|
properties.load(is);
|
||||||
appVersion = properties.getProperty("app.version") + "build" + properties.getProperty("build");
|
String version = properties.getProperty("app.version");
|
||||||
|
String build = properties.getProperty("build");
|
||||||
|
if (version != null && !version.contains("${")) {
|
||||||
|
appVersion = version + "build" + (build == null || build.contains("${") ? "" : build);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
}
|
||||||
e.printStackTrace();
|
} catch (Exception e) {
|
||||||
|
LOGGER.error("读取app.properties失败", e);
|
||||||
|
}
|
||||||
|
if (appVersion == null) {
|
||||||
|
appVersion = "unknown";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return appVersion;
|
return appVersion;
|
||||||
|
|||||||
@@ -4,9 +4,15 @@ import io.vertx.config.ConfigRetriever;
|
|||||||
import io.vertx.config.ConfigRetrieverOptions;
|
import io.vertx.config.ConfigRetrieverOptions;
|
||||||
import io.vertx.config.ConfigStoreOptions;
|
import io.vertx.config.ConfigStoreOptions;
|
||||||
import io.vertx.core.Future;
|
import io.vertx.core.Future;
|
||||||
|
import io.vertx.core.Promise;
|
||||||
import io.vertx.core.Vertx;
|
import io.vertx.core.Vertx;
|
||||||
import io.vertx.core.json.JsonObject;
|
import io.vertx.core.json.JsonObject;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 异步读取配置工具类
|
* 异步读取配置工具类
|
||||||
* <br>Create date 2021/9/2 1:23
|
* <br>Create date 2021/9/2 1:23
|
||||||
@@ -24,7 +30,29 @@ public class ConfigUtil {
|
|||||||
* @return JsonObject的Future
|
* @return JsonObject的Future
|
||||||
*/
|
*/
|
||||||
public static Future<JsonObject> readConfig(String format, String path, Vertx vertx) {
|
public static Future<JsonObject> readConfig(String format, String path, Vertx vertx) {
|
||||||
// 读取yml配置
|
// 支持 classpath: 前缀从类路径读取,否则从文件系统读取
|
||||||
|
if (path != null && path.startsWith("classpath:")) {
|
||||||
|
String resource = path.substring("classpath:".length());
|
||||||
|
// 使用 executeBlocking(Callable) 直接返回 Future<JsonObject>
|
||||||
|
return vertx.executeBlocking(() -> {
|
||||||
|
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
|
||||||
|
if (is == null) {
|
||||||
|
throw new RuntimeException("classpath resource not found: " + resource);
|
||||||
|
}
|
||||||
|
try (InputStream in = is) {
|
||||||
|
byte[] bytes = in.readAllBytes();
|
||||||
|
String content = new String(bytes, StandardCharsets.UTF_8);
|
||||||
|
if ("json".equalsIgnoreCase(format)) {
|
||||||
|
return new JsonObject(content);
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("unsupported classpath format: " + format);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Promise<JsonObject> promise = Promise.promise();
|
||||||
|
|
||||||
ConfigStoreOptions store = new ConfigStoreOptions()
|
ConfigStoreOptions store = new ConfigStoreOptions()
|
||||||
.setType("file")
|
.setType("file")
|
||||||
.setFormat(format)
|
.setFormat(format)
|
||||||
@@ -33,8 +61,43 @@ public class ConfigUtil {
|
|||||||
ConfigRetriever retriever = ConfigRetriever
|
ConfigRetriever retriever = ConfigRetriever
|
||||||
.create(vertx, new ConfigRetrieverOptions().addStore(store));
|
.create(vertx, new ConfigRetrieverOptions().addStore(store));
|
||||||
|
|
||||||
return retriever.getConfig();
|
// 异步获取配置
|
||||||
|
// 成功直接完成 promise
|
||||||
|
retriever.getConfig()
|
||||||
|
.onSuccess(config -> {
|
||||||
|
promise.complete(config);
|
||||||
|
retriever.close();
|
||||||
|
})
|
||||||
|
.onFailure(err -> {
|
||||||
|
retriever.close();
|
||||||
|
// 读取失败时,尝试从 resources/ 子目录读取(兼容 Docker 卷挂载场景)
|
||||||
|
String resourcesPath = "resources/" + path;
|
||||||
|
if (!path.startsWith("resources/") && Files.exists(Path.of(resourcesPath))) {
|
||||||
|
ConfigStoreOptions fallbackStore = new ConfigStoreOptions()
|
||||||
|
.setType("file")
|
||||||
|
.setFormat(format)
|
||||||
|
.setConfig(new JsonObject().put("path", resourcesPath));
|
||||||
|
ConfigRetriever fallbackRetriever = ConfigRetriever
|
||||||
|
.create(vertx, new ConfigRetrieverOptions().addStore(fallbackStore));
|
||||||
|
fallbackRetriever.getConfig()
|
||||||
|
.onSuccess(config -> {
|
||||||
|
promise.complete(config);
|
||||||
|
fallbackRetriever.close();
|
||||||
|
})
|
||||||
|
.onFailure(e2 -> {
|
||||||
|
promise.fail(new RuntimeException(
|
||||||
|
"读取配置文件失败: " + path + " (也尝试了 " + resourcesPath + ")", e2));
|
||||||
|
fallbackRetriever.close();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
promise.fail(new RuntimeException(
|
||||||
|
"读取配置文件失败: " + path, err));
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return promise.future();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package cn.qaiu.vx.core.util;
|
||||||
|
|
||||||
|
import io.vertx.core.Future;
|
||||||
|
import io.vertx.core.Promise;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
|
public class FutureUtils {
|
||||||
|
|
||||||
|
/** 默认同步等待超时时间(秒) */
|
||||||
|
private static final long DEFAULT_TIMEOUT_SECONDS = 120;
|
||||||
|
|
||||||
|
public static <T> T getResult(Future<T> future) {
|
||||||
|
try {
|
||||||
|
return future.toCompletionStage().toCompletableFuture()
|
||||||
|
.get(DEFAULT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
throw new RuntimeException("线程被中断", e);
|
||||||
|
} catch (TimeoutException e) {
|
||||||
|
throw new RuntimeException("等待Future超时(" + DEFAULT_TIMEOUT_SECONDS + "秒)", e);
|
||||||
|
} catch (ExecutionException e) {
|
||||||
|
Throwable cause = e.getCause();
|
||||||
|
throw new RuntimeException(cause != null ? cause : e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T getResult(Promise<T> promise) {
|
||||||
|
try {
|
||||||
|
return promise.future().toCompletionStage().toCompletableFuture()
|
||||||
|
.get(DEFAULT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
throw new RuntimeException("线程被中断", e);
|
||||||
|
} catch (TimeoutException e) {
|
||||||
|
throw new RuntimeException("等待Promise超时(" + DEFAULT_TIMEOUT_SECONDS + "秒)", e);
|
||||||
|
} catch (ExecutionException e) {
|
||||||
|
Throwable cause = e.getCause();
|
||||||
|
throw new RuntimeException(cause != null ? cause : e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,7 +16,7 @@ import java.time.format.DateTimeFormatter;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author <a href="https://qaiu.top">QAIU</a>
|
* @author <a href="https://qaiu.top">QAIU</a>
|
||||||
* Create at 2023/10/14 9:07
|
* @date 2023/10/14 9:07
|
||||||
*/
|
*/
|
||||||
public class JacksonConfig {
|
public class JacksonConfig {
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package cn.qaiu.vx.core.util;
|
package cn.qaiu.vx.core.util;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* vertx 上下文外的本地容器 为不在vertx线程的方法传递数据
|
* vertx 上下文外的本地容器 为不在vertx线程的方法传递数据
|
||||||
@@ -10,11 +10,10 @@ import java.util.Map;
|
|||||||
* @author <a href="https://qaiu.top">QAIU</a>
|
* @author <a href="https://qaiu.top">QAIU</a>
|
||||||
*/
|
*/
|
||||||
public class LocalConstant {
|
public class LocalConstant {
|
||||||
private static final Map<String, Object> LOCAL_CONST = new HashMap<>();
|
private static final Map<String, Object> LOCAL_CONST = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
public static Map<String, Object> put(String k, Object v) {
|
public static Map<String, Object> put(String k, Object v) {
|
||||||
if (LOCAL_CONST.containsKey(k)) return LOCAL_CONST;
|
LOCAL_CONST.putIfAbsent(k, v);
|
||||||
LOCAL_CONST.put(k, v);
|
|
||||||
return LOCAL_CONST;
|
return LOCAL_CONST;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,16 +36,20 @@ public final class ParamUtil {
|
|||||||
|
|
||||||
public static MultiMap paramsToMap(String paramString) {
|
public static MultiMap paramsToMap(String paramString) {
|
||||||
MultiMap entries = MultiMap.caseInsensitiveMultiMap();
|
MultiMap entries = MultiMap.caseInsensitiveMultiMap();
|
||||||
if (paramString == null) return entries;
|
if (paramString == null || paramString.isEmpty()) return entries;
|
||||||
String[] params = paramString.split("&");
|
String[] params = paramString.split("&");
|
||||||
if (params.length == 0) return entries;
|
if (params.length == 0) return entries;
|
||||||
for (String param : params) {
|
for (String param : params) {
|
||||||
String[] kv = param.split("=");
|
if (param == null || param.isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String[] kv = param.split("=", 2);
|
||||||
if (kv.length == 2) {
|
if (kv.length == 2) {
|
||||||
entries.set(kv[0], kv[1]);
|
entries.set(kv[0], kv[1]);
|
||||||
} else {
|
} else if (kv.length == 1) {
|
||||||
entries.set(kv[0], "");
|
entries.set(kv[0], "");
|
||||||
}
|
}
|
||||||
|
// kv.length == 0 时(空字符串),跳过
|
||||||
}
|
}
|
||||||
return entries;
|
return entries;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,10 @@ import java.lang.reflect.Method;
|
|||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import static cn.qaiu.vx.core.util.ConfigConstant.BASE_LOCATIONS;
|
import static cn.qaiu.vx.core.util.ConfigConstant.BASE_LOCATIONS;
|
||||||
|
|
||||||
@@ -36,6 +40,16 @@ import static cn.qaiu.vx.core.util.ConfigConstant.BASE_LOCATIONS;
|
|||||||
*/
|
*/
|
||||||
public final class ReflectionUtil {
|
public final class ReflectionUtil {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(ReflectionUtil.class);
|
||||||
|
|
||||||
|
// 缓存Reflections实例,避免重复扫描(每次扫描约35K+值,耗时1-3秒,占用大量内存)
|
||||||
|
private static final Map<String, Reflections> REFLECTIONS_CACHE = new java.util.concurrent.ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
// 预编译的类型匹配正则,避免每次请求重新编译
|
||||||
|
private static final Pattern BASIC_TYPE_PATTERN = Pattern.compile(
|
||||||
|
"^java\\.lang\\.((Boolean)|(Character)|(Byte)|(Short)|(Integer)|(Long)|(Float)|(Double)|(String))$");
|
||||||
|
private static final Pattern BASIC_TYPE_ARRAY_PATTERN = Pattern.compile(
|
||||||
|
"^(boolean|char|byte|short|int|long|float|double|String)\\[]$");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 以默认配置的基础包路径获取反射器
|
* 以默认配置的基础包路径获取反射器
|
||||||
@@ -47,43 +61,41 @@ public final class ReflectionUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取反射器
|
* 获取反射器(带缓存)
|
||||||
*
|
*
|
||||||
* @param packageAddress Package address String
|
* @param packageAddress Package address String
|
||||||
* @return Reflections object
|
* @return Reflections object
|
||||||
*/
|
*/
|
||||||
public static Reflections getReflections(String packageAddress) {
|
public static Reflections getReflections(String packageAddress) {
|
||||||
|
return REFLECTIONS_CACHE.computeIfAbsent(packageAddress, key -> {
|
||||||
List<String> packageAddressList;
|
List<String> packageAddressList;
|
||||||
if (packageAddress.contains(",")) {
|
if (key.contains(",")) {
|
||||||
packageAddressList = Arrays.asList(packageAddress.split(","));
|
packageAddressList = Arrays.asList(key.split(","));
|
||||||
} else if (packageAddress.contains(";")) {
|
} else if (key.contains(";")) {
|
||||||
packageAddressList = Arrays.asList(packageAddress.split(";"));
|
packageAddressList = Arrays.asList(key.split(";"));
|
||||||
} else {
|
} else {
|
||||||
packageAddressList = Collections.singletonList(packageAddress);
|
packageAddressList = Collections.singletonList(key);
|
||||||
}
|
}
|
||||||
|
return createReflections(packageAddressList);
|
||||||
return getReflections(packageAddressList);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取反射器
|
* 获取反射器(带缓存)
|
||||||
*
|
*
|
||||||
* @param packageAddresses Package address List
|
* @param packageAddresses Package address List
|
||||||
* @return Reflections object
|
* @return Reflections object
|
||||||
*/
|
*/
|
||||||
public static Reflections getReflections(List<String> packageAddresses) {
|
public static Reflections getReflections(List<String> packageAddresses) {
|
||||||
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
|
String cacheKey = String.join(",", packageAddresses);
|
||||||
FilterBuilder filterBuilder = new FilterBuilder();
|
return REFLECTIONS_CACHE.computeIfAbsent(cacheKey, key -> createReflections(packageAddresses));
|
||||||
packageAddresses.forEach(str -> {
|
}
|
||||||
Collection<URL> urls = ClasspathHelper.forPackage(str.trim());
|
|
||||||
configurationBuilder.addUrls(urls);
|
|
||||||
filterBuilder.includePackage(str.trim());
|
|
||||||
});
|
|
||||||
|
|
||||||
// 采坑记录 2021-05-08
|
private static Reflections createReflections(List<String> packageAddresses) {
|
||||||
// 发现注解api层 没有继承父类时 这里反射一直有问题(Scanner SubTypesScanner was not configured)
|
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder()
|
||||||
// 因此这里需要手动配置各种Scanner扫描器 -- https://blog.csdn.net/qq_29499107/article/details/106889781
|
.addClassLoaders(Thread.currentThread().getContextClassLoader())
|
||||||
configurationBuilder.setScanners(
|
.forPackages(packageAddresses.toArray(new String[0]))
|
||||||
|
.setScanners(
|
||||||
Scanners.SubTypes.filterResultsBy(s -> true), //允许getAllTypes获取所有Object的子类, 不设置为false则 getAllTypes
|
Scanners.SubTypes.filterResultsBy(s -> true), //允许getAllTypes获取所有Object的子类, 不设置为false则 getAllTypes
|
||||||
// 会报错.默认为true.
|
// 会报错.默认为true.
|
||||||
new MethodParameterNamesScanner(), //设置方法参数名称 扫描器,否则调用getConstructorParamNames 会报错
|
new MethodParameterNamesScanner(), //设置方法参数名称 扫描器,否则调用getConstructorParamNames 会报错
|
||||||
@@ -91,8 +103,6 @@ public final class ReflectionUtil {
|
|||||||
new MemberUsageScanner(), //设置 member 扫描器,否则 getMethodUsage 会报错
|
new MemberUsageScanner(), //设置 member 扫描器,否则 getMethodUsage 会报错
|
||||||
Scanners.TypesAnnotated //设置类注解 扫描器 ,否则 getTypesAnnotatedWith 会报错
|
Scanners.TypesAnnotated //设置类注解 扫描器 ,否则 getTypesAnnotatedWith 会报错
|
||||||
);
|
);
|
||||||
|
|
||||||
configurationBuilder.filterInputsBy(filterBuilder);
|
|
||||||
return new Reflections(configurationBuilder);
|
return new Reflections(configurationBuilder);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +140,7 @@ public final class ReflectionUtil {
|
|||||||
parameterTypes[j - k]));
|
parameterTypes[j - k]));
|
||||||
}
|
}
|
||||||
} catch (NotFoundException e) {
|
} catch (NotFoundException e) {
|
||||||
e.printStackTrace();
|
LOGGER.error("获取方法参数失败", e);
|
||||||
}
|
}
|
||||||
return paramMap;
|
return paramMap;
|
||||||
}
|
}
|
||||||
@@ -185,7 +195,7 @@ public final class ReflectionUtil {
|
|||||||
try {
|
try {
|
||||||
return DateUtils.parseDate(value, fmt);
|
return DateUtils.parseDate(value, fmt);
|
||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
e.printStackTrace();
|
LOGGER.error("日期解析失败: {}", value, e);
|
||||||
throw new RuntimeException("无法将格式化日期");
|
throw new RuntimeException("无法将格式化日期");
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
@@ -217,7 +227,7 @@ public final class ReflectionUtil {
|
|||||||
}
|
}
|
||||||
return arr;
|
return arr;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
LOGGER.error("数组类型转换失败: {}", value, e);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -231,8 +241,7 @@ public final class ReflectionUtil {
|
|||||||
if (ctClass.isPrimitive() || "java.util.Date".equals(ctClass.getName())) {
|
if (ctClass.isPrimitive() || "java.util.Date".equals(ctClass.getName())) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return ctClass.getName().matches("^java\\.lang\\.((Boolean)|(Character)|(Byte)|(Short)|(Integer)|(Long)|" +
|
return BASIC_TYPE_PATTERN.matcher(ctClass.getName()).matches();
|
||||||
"(Float)|(Double)|(String))$");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -243,7 +252,7 @@ public final class ReflectionUtil {
|
|||||||
public static boolean isBasicTypeArray(CtClass ctClass) {
|
public static boolean isBasicTypeArray(CtClass ctClass) {
|
||||||
if (!ctClass.isArray()) {
|
if (!ctClass.isArray()) {
|
||||||
return false;
|
return false;
|
||||||
} else return (ctClass.getName().matches("^(boolen|char|byte|short|int|long|float|double|String)\\[]$"));
|
} else return BASIC_TYPE_ARRAY_PATTERN.matcher(ctClass.getName()).matches();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -12,24 +12,46 @@ import static io.vertx.core.http.HttpHeaders.CONTENT_TYPE;
|
|||||||
public class ResponseUtil {
|
public class ResponseUtil {
|
||||||
|
|
||||||
public static void redirect(HttpServerResponse response, String url) {
|
public static void redirect(HttpServerResponse response, String url) {
|
||||||
|
if (response.ended() || response.closed()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
response.putHeader(CONTENT_TYPE, "text/html; charset=utf-8")
|
response.putHeader(CONTENT_TYPE, "text/html; charset=utf-8")
|
||||||
|
.putHeader("Referrer-Policy", "no-referrer")
|
||||||
.putHeader(HttpHeaders.LOCATION, url).setStatusCode(302).end();
|
.putHeader(HttpHeaders.LOCATION, url).setStatusCode(302).end();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void redirect(HttpServerResponse response, String url, Promise<?> promise) {
|
public static void redirect(HttpServerResponse response, String url, Promise<?> promise) {
|
||||||
|
try {
|
||||||
redirect(response, url);
|
redirect(response, url);
|
||||||
promise.complete();
|
promise.tryComplete();
|
||||||
|
} catch (Throwable t) {
|
||||||
|
promise.tryFail(t);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void fireJsonObjectResponse(RoutingContext ctx, JsonObject jsonObject) {
|
public static void fireJsonObjectResponse(RoutingContext ctx, JsonObject jsonObject) {
|
||||||
ctx.response().putHeader(CONTENT_TYPE, "application/json; charset=utf-8")
|
fireJsonObjectResponse(ctx, jsonObject, 200);
|
||||||
.setStatusCode(200)
|
|
||||||
.end(jsonObject.encode());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void fireJsonObjectResponse(HttpServerResponse ctx, JsonObject jsonObject) {
|
public static void fireJsonObjectResponse(HttpServerResponse ctx, JsonObject jsonObject) {
|
||||||
|
fireJsonObjectResponse(ctx, jsonObject, 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void fireJsonObjectResponse(RoutingContext ctx, JsonObject jsonObject, int statusCode) {
|
||||||
|
if (ctx.response().ended() || ctx.response().closed()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ctx.response().putHeader(CONTENT_TYPE, "application/json; charset=utf-8")
|
||||||
|
.setStatusCode(statusCode)
|
||||||
|
.end(jsonObject.encode());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void fireJsonObjectResponse(HttpServerResponse ctx, JsonObject jsonObject, int statusCode) {
|
||||||
|
if (ctx.ended() || ctx.closed()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
ctx.putHeader(CONTENT_TYPE, "application/json; charset=utf-8")
|
ctx.putHeader(CONTENT_TYPE, "application/json; charset=utf-8")
|
||||||
.setStatusCode(200)
|
.setStatusCode(statusCode)
|
||||||
.end(jsonObject.encode());
|
.end(jsonObject.encode());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,15 +59,25 @@ public class ResponseUtil {
|
|||||||
fireJsonObjectResponse(ctx, jsonResult.toJsonObject());
|
fireJsonObjectResponse(ctx, jsonResult.toJsonObject());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <T> void fireJsonResultResponse(RoutingContext ctx, JsonResult<T> jsonResult, int statusCode) {
|
||||||
|
fireJsonObjectResponse(ctx, jsonResult.toJsonObject(), statusCode);
|
||||||
|
}
|
||||||
|
|
||||||
public static <T> void fireJsonResultResponse(HttpServerResponse ctx, JsonResult<T> jsonResult) {
|
public static <T> void fireJsonResultResponse(HttpServerResponse ctx, JsonResult<T> jsonResult) {
|
||||||
fireJsonObjectResponse(ctx, jsonResult.toJsonObject());
|
fireJsonObjectResponse(ctx, jsonResult.toJsonObject());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void fireTextResponse(RoutingContext ctx, String text) {
|
public static void fireTextResponse(RoutingContext ctx, String text) {
|
||||||
|
if (ctx.response().ended() || ctx.response().closed()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
ctx.response().putHeader(CONTENT_TYPE, "text/html; charset=utf-8").end(text);
|
ctx.response().putHeader(CONTENT_TYPE, "text/html; charset=utf-8").end(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void sendError(RoutingContext ctx, int statusCode) {
|
public static void sendError(RoutingContext ctx, int statusCode) {
|
||||||
|
if (ctx.response().ended() || ctx.response().closed()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
ctx.response().setStatusCode(statusCode).end();
|
ctx.response().setStatusCode(statusCode).end();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,7 +3,6 @@ package cn.qaiu.vx.core.util;
|
|||||||
import io.vertx.core.json.JsonArray;
|
import io.vertx.core.json.JsonArray;
|
||||||
import io.vertx.core.json.JsonObject;
|
import io.vertx.core.json.JsonObject;
|
||||||
import io.vertx.core.shareddata.LocalMap;
|
import io.vertx.core.shareddata.LocalMap;
|
||||||
import io.vertx.core.shareddata.SharedData;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* vertx 共享数据
|
* vertx 共享数据
|
||||||
@@ -13,10 +12,8 @@ import io.vertx.core.shareddata.SharedData;
|
|||||||
*/
|
*/
|
||||||
public class SharedDataUtil {
|
public class SharedDataUtil {
|
||||||
|
|
||||||
private static final SharedData sharedData = VertxHolder.getVertxInstance().sharedData();
|
public static io.vertx.core.shareddata.SharedData shareData() {
|
||||||
|
return VertxHolder.getVertxInstance().sharedData();
|
||||||
public static SharedData shareData() {
|
|
||||||
return sharedData;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static LocalMap<String, Object> getLocalMap(String key) {
|
public static LocalMap<String, Object> getLocalMap(String key) {
|
||||||
@@ -24,7 +21,7 @@ public class SharedDataUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static <T> LocalMap<String, T> getLocalMapWithCast(String key) {
|
public static <T> LocalMap<String, T> getLocalMapWithCast(String key) {
|
||||||
return sharedData.getLocalMap(key);
|
return shareData().getLocalMap(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JsonObject getJsonConfig(String key) {
|
public static JsonObject getJsonConfig(String key) {
|
||||||
|
|||||||
@@ -1,49 +1,110 @@
|
|||||||
package cn.qaiu.vx.core.verticle;
|
package cn.qaiu.vx.core.verticle;
|
||||||
|
|
||||||
import io.vertx.core.AbstractVerticle;
|
import io.vertx.core.AbstractVerticle;
|
||||||
import io.vertx.core.Vertx;
|
import io.vertx.core.Future;
|
||||||
import io.vertx.core.VertxOptions;
|
import io.vertx.core.Promise;
|
||||||
import io.vertx.core.dns.AddressResolverOptions;
|
|
||||||
import io.vertx.core.http.*;
|
import io.vertx.core.http.*;
|
||||||
|
import io.vertx.core.json.JsonObject;
|
||||||
import io.vertx.core.net.NetClient;
|
import io.vertx.core.net.NetClient;
|
||||||
import io.vertx.core.net.NetClientOptions;
|
import io.vertx.core.net.NetClientOptions;
|
||||||
import io.vertx.core.net.NetSocket;
|
import io.vertx.core.net.NetSocket;
|
||||||
import io.vertx.core.net.ProxyOptions;
|
import io.vertx.core.net.ProxyOptions;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
|
|
||||||
|
import static cn.qaiu.vx.core.util.ConfigConstant.GLOBAL_CONFIG;
|
||||||
|
import static cn.qaiu.vx.core.util.ConfigConstant.LOCAL;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class HttpProxyVerticle extends AbstractVerticle {
|
public class HttpProxyVerticle extends AbstractVerticle {
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(HttpProxyVerticle.class);
|
||||||
|
|
||||||
|
static byte[] decodeProxyAuthorization(String encoded) {
|
||||||
|
var source = new ByteArrayInputStream(encoded.getBytes(StandardCharsets.ISO_8859_1));
|
||||||
|
// The streaming decoder uses scalar Java code and does not call the bulk intrinsic.
|
||||||
|
try (var decoder = Base64.getDecoder().wrap(source)) {
|
||||||
|
byte[] decoded = decoder.readAllBytes();
|
||||||
|
if (source.available() != 0) {
|
||||||
|
throw new IllegalArgumentException("Unexpected data after Base64 padding");
|
||||||
|
}
|
||||||
|
return decoded;
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IllegalArgumentException("Invalid Base64 proxy credentials", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private HttpClient httpClient;
|
private HttpClient httpClient;
|
||||||
private NetClient netClient;
|
private NetClient netClient;
|
||||||
|
private HttpServer httpServer;
|
||||||
|
private volatile boolean stopping = false;
|
||||||
|
|
||||||
|
private JsonObject proxyPreConf;
|
||||||
|
private JsonObject proxyServerConf;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start() {
|
public void start(io.vertx.core.Promise<Void> startPromise) {
|
||||||
ProxyOptions proxyOptions = new ProxyOptions().setHost("127.0.0.1").setPort(7890);
|
stopping = false;
|
||||||
|
proxyServerConf = ((JsonObject)vertx.sharedData().getLocalMap(LOCAL).get(GLOBAL_CONFIG)).getJsonObject("proxy-server");
|
||||||
|
proxyPreConf = ((JsonObject)vertx.sharedData().getLocalMap(LOCAL).get(GLOBAL_CONFIG)).getJsonObject("proxy-pre");
|
||||||
|
Integer serverPort = proxyServerConf.getInteger("port");
|
||||||
|
|
||||||
|
ProxyOptions proxyOptions = null;
|
||||||
|
if (proxyPreConf != null && StringUtils.isNotBlank(proxyPreConf.getString("ip"))) {
|
||||||
|
proxyOptions = new ProxyOptions(proxyPreConf);
|
||||||
|
}
|
||||||
|
|
||||||
// 初始化 HTTP 客户端,用于向目标服务器发送 HTTP 请求
|
// 初始化 HTTP 客户端,用于向目标服务器发送 HTTP 请求
|
||||||
HttpClientOptions httpClientOptions = new HttpClientOptions();
|
HttpClientOptions httpClientOptions = new HttpClientOptions()
|
||||||
httpClient = vertx.createHttpClient(httpClientOptions.setProxyOptions(proxyOptions));
|
.setMaxPoolSize(64)
|
||||||
|
.setMaxWaitQueueSize(256)
|
||||||
|
.setConnectTimeout(15000)
|
||||||
|
.setIdleTimeout(60)
|
||||||
|
.setKeepAlive(true);
|
||||||
|
if (proxyOptions != null) {
|
||||||
|
httpClientOptions.setProxyOptions(proxyOptions);
|
||||||
|
}
|
||||||
|
httpClient = vertx.createHttpClient(httpClientOptions);
|
||||||
|
|
||||||
// 创建并启动 HTTP 代理服务器,监听指定端口
|
// 创建并启动 HTTP 代理服务器,监听指定端口
|
||||||
HttpServer server = vertx.createHttpServer(new HttpServerOptions().setClientAuth(ClientAuth.REQUIRED));
|
HttpServerOptions httpServerOptions = new HttpServerOptions();
|
||||||
server.requestHandler(this::handleClientRequest);
|
if (proxyServerConf.containsKey("username") &&
|
||||||
|
StringUtils.isNotBlank(proxyServerConf.getString("username"))) {
|
||||||
|
httpServerOptions.setClientAuth(ClientAuth.REQUIRED);
|
||||||
|
}
|
||||||
|
|
||||||
|
httpServer = vertx.createHttpServer(httpServerOptions);
|
||||||
|
httpServer.requestHandler(this::handleClientRequest);
|
||||||
|
|
||||||
// 初始化 NetClient,用于在 CONNECT 请求中建立 TCP 连接隧道
|
// 初始化 NetClient,用于在 CONNECT 请求中建立 TCP 连接隧道
|
||||||
netClient = vertx.createNetClient(new NetClientOptions()
|
NetClientOptions netClientOptions = new NetClientOptions();
|
||||||
.setProxyOptions(proxyOptions)
|
|
||||||
|
if (proxyOptions != null) {
|
||||||
|
netClientOptions.setProxyOptions(proxyOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
netClient = vertx.createNetClient(netClientOptions
|
||||||
.setConnectTimeout(15000)
|
.setConnectTimeout(15000)
|
||||||
.setTrustAll(true));
|
.setTrustAll(true));
|
||||||
|
|
||||||
// 启动 HTTP 代理服务器
|
// 启动 HTTP 代理服务器
|
||||||
server.listen(7891, ar -> {
|
httpServer.listen(serverPort)
|
||||||
if (ar.succeeded()) {
|
.onSuccess(res -> {
|
||||||
System.out.println("HTTP Proxy server started on port 7891");
|
LOGGER.info("HTTP Proxy server started on port {}", serverPort);
|
||||||
} else {
|
startPromise.complete();
|
||||||
System.err.println("Failed to start HTTP Proxy server: " + ar.cause());
|
})
|
||||||
}
|
.onFailure(err -> {
|
||||||
|
LOGGER.error("Failed to start HTTP Proxy server: " + err.getMessage(), err);
|
||||||
|
closeClients().onComplete(close -> startPromise.fail(err));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,7 +112,7 @@ public class HttpProxyVerticle extends AbstractVerticle {
|
|||||||
private void handleConnectRequest(HttpServerRequest clientRequest) {
|
private void handleConnectRequest(HttpServerRequest clientRequest) {
|
||||||
String[] uriParts = clientRequest.uri().split(":");
|
String[] uriParts = clientRequest.uri().split(":");
|
||||||
if (uriParts.length != 2) {
|
if (uriParts.length != 2) {
|
||||||
clientRequest.response().setStatusCode(400).end("Bad Request: Invalid URI format");
|
failClientResponse(clientRequest.response(), 400, "Bad Request: Invalid URI format");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,54 +122,84 @@ public class HttpProxyVerticle extends AbstractVerticle {
|
|||||||
try {
|
try {
|
||||||
targetPort = Integer.parseInt(uriParts[1]);
|
targetPort = Integer.parseInt(uriParts[1]);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
clientRequest.response().setStatusCode(400).end("Bad Request: Invalid port");
|
failClientResponse(clientRequest.response(), 400, "Bad Request: Invalid port");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
clientRequest.pause();
|
clientRequest.pause();
|
||||||
// 通过 NetClient 连接目标服务器并创建隧道
|
// 通过 NetClient 连接目标服务器并创建隧道
|
||||||
netClient.connect(targetPort, targetHost, connectionAttempt -> {
|
try {
|
||||||
if (connectionAttempt.succeeded()) {
|
netClient.connect(targetPort, targetHost)
|
||||||
NetSocket targetSocket = connectionAttempt.result();
|
.onSuccess(targetSocket -> {
|
||||||
|
// Upgrade client connection to NetSocket and implement bidirectional data flow
|
||||||
|
clientRequest.toNetSocket()
|
||||||
|
.onSuccess(clientSocket -> {
|
||||||
|
clientSocket.pipeTo(targetSocket)
|
||||||
|
.onFailure(err -> {
|
||||||
|
LOGGER.debug("CONNECT client -> target pipe closed", err);
|
||||||
|
closeTunnelSockets(clientSocket, targetSocket);
|
||||||
|
});
|
||||||
|
targetSocket.pipeTo(clientSocket)
|
||||||
|
.onFailure(err -> {
|
||||||
|
LOGGER.debug("CONNECT target -> client pipe closed", err);
|
||||||
|
closeTunnelSockets(clientSocket, targetSocket);
|
||||||
|
});
|
||||||
|
|
||||||
// 升级客户端连接到 NetSocket 并实现双向数据流
|
// Close the other socket when one side closes
|
||||||
clientRequest.toNetSocket().onComplete(clientSocketAttempt -> {
|
|
||||||
if (clientSocketAttempt.succeeded()) {
|
|
||||||
NetSocket clientSocket = clientSocketAttempt.result();
|
|
||||||
|
|
||||||
// 设置双向数据流转发
|
|
||||||
clientSocket.handler(targetSocket::write);
|
|
||||||
targetSocket.handler(clientSocket::write);
|
|
||||||
|
|
||||||
// 关闭其中一方时关闭另一方
|
|
||||||
clientSocket.closeHandler(v -> targetSocket.close());
|
clientSocket.closeHandler(v -> targetSocket.close());
|
||||||
targetSocket.closeHandler(v -> clientSocket.close());
|
targetSocket.closeHandler(v -> clientSocket.close());
|
||||||
} else {
|
})
|
||||||
System.err.println("Failed to upgrade client connection to socket: " + clientSocketAttempt.cause().getMessage());
|
.onFailure(clientSocketAttempt -> {
|
||||||
|
System.err.println("Failed to upgrade client connection to socket: " + clientSocketAttempt.getMessage());
|
||||||
targetSocket.close();
|
targetSocket.close();
|
||||||
clientRequest.response().setStatusCode(500).end("Internal Server Error");
|
failClientRequestAndClose(clientRequest, 500, "Internal Server Error");
|
||||||
}
|
|
||||||
});
|
});
|
||||||
} else {
|
})
|
||||||
System.err.println("Failed to connect to target: " + connectionAttempt.cause().getMessage());
|
.onFailure(connectionAttempt -> {
|
||||||
clientRequest.response().setStatusCode(502).end("Bad Gateway: Unable to connect to target");
|
LOGGER.warn("Failed to connect to target: {}", connectionAttempt.getMessage());
|
||||||
}
|
failClientRequestAndClose(clientRequest, 502, "Bad Gateway: Unable to connect to target");
|
||||||
});
|
});
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.warn("CONNECT 请求创建失败", e);
|
||||||
|
failClientRequestAndClose(clientRequest, 502, "Bad Gateway: Unable to connect to target");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理客户端的 HTTP 请求
|
// 处理客户端的 HTTP 请求
|
||||||
private void handleClientRequest(HttpServerRequest clientRequest) {
|
private void handleClientRequest(HttpServerRequest clientRequest) {
|
||||||
|
if (stopping) {
|
||||||
|
failClientResponse(clientRequest.response(), 503, "Service Unavailable");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 打印来源ip和访问目标URI
|
||||||
|
LOGGER.debug("source: {}, target: {}", clientRequest.remoteAddress().toString(), clientRequest.uri());
|
||||||
|
if (proxyServerConf.containsKey("username") &&
|
||||||
|
StringUtils.isNotBlank(proxyServerConf.getString("username"))) {
|
||||||
String s = clientRequest.headers().get("Proxy-Authorization");
|
String s = clientRequest.headers().get("Proxy-Authorization");
|
||||||
if (s == null) {
|
if (s == null) {
|
||||||
clientRequest.response().setStatusCode(403).end();
|
failClientResponse(clientRequest.response(), 403, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String[] split;
|
||||||
|
try {
|
||||||
|
split = new String(decodeProxyAuthorization(s.replace("Basic ", ""))).split(":");
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
LOGGER.warn("Proxy-Authorization header is not valid Base64");
|
||||||
|
failClientResponse(clientRequest.response(), 403, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (split.length <= 1) {
|
||||||
|
LOGGER.warn("Proxy-Authorization header format invalid: missing username:password separator");
|
||||||
|
failClientResponse(clientRequest.response(), 403, null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String username = proxyServerConf.getString("username");
|
||||||
|
String password = proxyServerConf.getString("password");
|
||||||
|
if (!split[0].equals(username) || !split[1].equals(password)) {
|
||||||
|
LOGGER.info("-----auth failed------\nusername: {}", split[0]);
|
||||||
|
failClientResponse(clientRequest.response(), 403, null);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String[] split = new String(Base64.getDecoder().decode(s.replace("Basic ", ""))).split(":");
|
|
||||||
if (split.length > 1) {
|
|
||||||
System.out.println(split[0]);
|
|
||||||
System.out.println(split[1]);
|
|
||||||
// TODO
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (clientRequest.method() == HttpMethod.CONNECT) {
|
if (clientRequest.method() == HttpMethod.CONNECT) {
|
||||||
// 处理 CONNECT 请求
|
// 处理 CONNECT 请求
|
||||||
@@ -124,61 +215,222 @@ public class HttpProxyVerticle extends AbstractVerticle {
|
|||||||
// 获取目标主机
|
// 获取目标主机
|
||||||
String hostHeader = clientRequest.getHeader("Host");
|
String hostHeader = clientRequest.getHeader("Host");
|
||||||
if (hostHeader == null) {
|
if (hostHeader == null) {
|
||||||
clientRequest.response().setStatusCode(400).end("Host header is missing");
|
failClientResponse(clientRequest.response(), 400, "Host header is missing");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String targetHost = hostHeader.split(":")[0];
|
HostAndPort target;
|
||||||
int targetPort = 80; // 默认为 HTTP 的端口
|
try {
|
||||||
clientRequest.pause(); // 暂停客户端请求的读取,避免数据丢失
|
target = parseHostHeader(hostHeader);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
failClientResponse(clientRequest.response(), 400, "Bad Request: Invalid Host header");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String targetHost = target.host();
|
||||||
|
int targetPort = extractPortFromUrl(clientRequest.uri(), target.port()); // 默认为 HTTP 的端口
|
||||||
|
if (targetPort <= 0) {
|
||||||
|
failClientResponse(clientRequest.response(), 400, "Bad Request: Invalid target port");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
clientRequest.pause(); // 暂停客户端请求的读取,等上游请求创建完成
|
||||||
|
|
||||||
|
try {
|
||||||
httpClient.request(clientRequest.method(), targetPort, targetHost, clientRequest.uri())
|
httpClient.request(clientRequest.method(), targetPort, targetHost, clientRequest.uri())
|
||||||
.onSuccess(request -> {
|
.onSuccess(request -> {
|
||||||
clientRequest.resume(); // 恢复客户端请求的读取
|
|
||||||
|
|
||||||
// 逐个设置请求头
|
// 逐个设置请求头
|
||||||
clientRequest.headers().forEach(header -> request.putHeader(header.getKey(), header.getValue()));
|
clientRequest.headers().forEach(header -> request.putHeader(header.getKey(), header.getValue()));
|
||||||
|
|
||||||
// 将客户端请求的 body 转发给目标服务器
|
request.response()
|
||||||
clientRequest.bodyHandler(body -> request.send(body, ar -> {
|
.onSuccess(response -> {
|
||||||
if (ar.succeeded()) {
|
HttpServerResponse clientResponse = clientRequest.response();
|
||||||
var response = ar.result();
|
if (clientResponse.ended() || clientResponse.closed()) {
|
||||||
clientRequest.response().setStatusCode(response.statusCode());
|
response.resume();
|
||||||
clientRequest.response().headers().setAll(response.headers());
|
return;
|
||||||
response.body().onSuccess(b-> clientRequest.response().end(b));
|
|
||||||
} else {
|
|
||||||
clientRequest.response().setStatusCode(502).end("Bad Gateway: Unable to reach target");
|
|
||||||
}
|
}
|
||||||
}));
|
clientResponse.setStatusCode(response.statusCode());
|
||||||
|
clientResponse.headers().setAll(response.headers());
|
||||||
|
response.pipeTo(clientResponse)
|
||||||
|
.onFailure(err -> {
|
||||||
|
LOGGER.error("HTTP代理响应转发失败", err);
|
||||||
|
try {
|
||||||
|
response.request().reset();
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.debug("HTTP代理上游响应已关闭", e);
|
||||||
|
}
|
||||||
|
failClientRequestAndClose(clientRequest, 502, "Bad Gateway: Unable to reach target");
|
||||||
|
});
|
||||||
})
|
})
|
||||||
.onFailure(err -> {
|
.onFailure(err -> {
|
||||||
err.printStackTrace();
|
LOGGER.error("HTTP代理响应失败", err);
|
||||||
clientRequest.response().setStatusCode(502).end("Bad Gateway: Request failed");
|
try {
|
||||||
|
request.reset();
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.debug("HTTP代理上游请求已关闭", e);
|
||||||
|
}
|
||||||
|
failClientRequestAndClose(clientRequest, 502, "Bad Gateway: Unable to reach target");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
clientRequest.pipeTo(request)
|
||||||
|
.onFailure(err -> {
|
||||||
|
LOGGER.error("HTTP代理请求转发失败", err);
|
||||||
|
try {
|
||||||
|
request.reset();
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.debug("HTTP代理上游请求已关闭", e);
|
||||||
|
}
|
||||||
|
failClientRequestAndClose(clientRequest, 502, "Bad Gateway: Unable to reach target");
|
||||||
|
});
|
||||||
|
clientRequest.resume();
|
||||||
|
})
|
||||||
|
.onFailure(err -> {
|
||||||
|
LOGGER.error("HTTP请求失败", err);
|
||||||
|
failClientRequestAndClose(clientRequest, 502, "Bad Gateway: Request failed");
|
||||||
|
});
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.error("HTTP请求创建失败", e);
|
||||||
|
failClientRequestAndClose(clientRequest, 502, "Bad Gateway: Request failed");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private void failClientResponse(HttpServerResponse response, String message) {
|
||||||
public void stop() {
|
failClientResponse(response, 502, message);
|
||||||
// 停止 HTTP 客户端以释放资源
|
}
|
||||||
if (httpClient != null) {
|
|
||||||
httpClient.close();
|
private void failClientResponse(HttpServerResponse response, int statusCode, String message) {
|
||||||
|
if (response.ended() || response.closed()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (!response.headWritten()) {
|
||||||
|
response.setStatusCode(statusCode);
|
||||||
|
if (message == null) {
|
||||||
|
response.end();
|
||||||
|
} else {
|
||||||
|
response.end(message);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
response.reset();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.debug("客户端响应已关闭,忽略代理错误响应", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void failClientRequestAndClose(HttpServerRequest request, int statusCode, String message) {
|
||||||
|
HttpServerResponse response = request.response();
|
||||||
|
if (response.ended() || response.closed()) {
|
||||||
|
closeClientConnection(request);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (!response.headWritten()) {
|
||||||
|
response.setStatusCode(statusCode);
|
||||||
|
Future<Void> endFuture = message == null ? response.end() : response.end(message);
|
||||||
|
endFuture.onComplete(v -> closeClientConnection(request));
|
||||||
|
} else {
|
||||||
|
response.reset();
|
||||||
|
closeClientConnection(request);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.debug("客户端响应已关闭,关闭代理连接", e);
|
||||||
|
closeClientConnection(request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void closeClientConnection(HttpServerRequest request) {
|
||||||
|
try {
|
||||||
|
request.connection().close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.debug("关闭客户端代理连接失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void closeTunnelSockets(NetSocket clientSocket, NetSocket targetSocket) {
|
||||||
|
try {
|
||||||
|
clientSocket.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.debug("关闭CONNECT客户端socket失败", e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
targetSocket.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.debug("关闭CONNECT目标socket失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO add Deploy
|
* 从 URL 中提取端口号
|
||||||
* @param args
|
*
|
||||||
|
* @param urlString URL 字符串
|
||||||
|
* @return 提取的端口号,如果没有指定端口,则返回默认端口
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static int extractPortFromUrl(String urlString) {
|
||||||
// 配置 DNS 解析器,使用多个 DNS 服务器来提升解析速度
|
return extractPortFromUrl(urlString, 80);
|
||||||
Vertx vertx = Vertx.vertx(new VertxOptions()
|
|
||||||
.setAddressResolverOptions(new AddressResolverOptions()
|
|
||||||
.addServer("114.114.114.114")
|
|
||||||
.addServer("114.114.115.115")
|
|
||||||
.addServer("8.8.8.8")
|
|
||||||
.addServer("8.8.4.4")));
|
|
||||||
|
|
||||||
// 部署 Verticle 并启动动态 HTTP 代理服务器
|
|
||||||
vertx.deployVerticle(new HttpProxyVerticle());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static int extractPortFromUrl(String urlString, int defaultPort) {
|
||||||
|
try {
|
||||||
|
URI uri = new URI(urlString);
|
||||||
|
int port = uri.getPort();
|
||||||
|
// 如果 URL 没有指定端口,使用默认端口
|
||||||
|
if (port == -1) {
|
||||||
|
if ("https".equalsIgnoreCase(uri.getScheme())) {
|
||||||
|
port = 443; // HTTPS 默认端口
|
||||||
|
} else {
|
||||||
|
port = defaultPort; // HTTP 默认端口
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return port;
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.error("提取端口失败: {}", urlString, e);
|
||||||
|
// 出现异常时返回 -1,表示提取失败
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private HostAndPort parseHostHeader(String hostHeader) {
|
||||||
|
if (hostHeader.startsWith("[")) {
|
||||||
|
int end = hostHeader.indexOf(']');
|
||||||
|
if (end > 0) {
|
||||||
|
String host = hostHeader.substring(1, end);
|
||||||
|
int port = 80;
|
||||||
|
if (hostHeader.length() > end + 2 && hostHeader.charAt(end + 1) == ':') {
|
||||||
|
port = Integer.parseInt(hostHeader.substring(end + 2));
|
||||||
|
}
|
||||||
|
return new HostAndPort(host, port);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int lastColon = hostHeader.lastIndexOf(':');
|
||||||
|
if (lastColon > 0 && hostHeader.indexOf(':') == lastColon) {
|
||||||
|
return new HostAndPort(hostHeader.substring(0, lastColon), Integer.parseInt(hostHeader.substring(lastColon + 1)));
|
||||||
|
}
|
||||||
|
return new HostAndPort(hostHeader, 80);
|
||||||
|
}
|
||||||
|
|
||||||
|
private record HostAndPort(String host, int port) {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stop(Promise<Void> stopPromise) {
|
||||||
|
stopping = true;
|
||||||
|
Future<Void> serverClose = httpServer == null ? Future.succeededFuture() : httpServer.close();
|
||||||
|
serverClose.onComplete(serverResult -> closeClients().onComplete(clientResult -> {
|
||||||
|
if (serverResult.failed()) {
|
||||||
|
stopPromise.fail(serverResult.cause());
|
||||||
|
} else if (clientResult.failed()) {
|
||||||
|
stopPromise.fail(clientResult.cause());
|
||||||
|
} else {
|
||||||
|
stopPromise.complete();
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Future<Void> closeClients() {
|
||||||
|
Future<Void> httpClientClose = httpClient == null ? Future.succeededFuture() : httpClient.close();
|
||||||
|
Future<Void> netClientClose = netClient == null ? Future.succeededFuture() : netClient.close();
|
||||||
|
return Future.all(httpClientClose, netClientClose).mapEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
package cn.qaiu.vx.core.verticle;
|
||||||
|
|
||||||
|
import cn.qaiu.vx.core.base.AppRun;
|
||||||
|
import cn.qaiu.vx.core.base.DefaultAppRun;
|
||||||
|
import cn.qaiu.vx.core.util.CommonUtil;
|
||||||
|
import cn.qaiu.vx.core.util.ReflectionUtil;
|
||||||
|
import cn.qaiu.vx.core.util.SharedDataUtil;
|
||||||
|
import io.vertx.core.AbstractVerticle;
|
||||||
|
import io.vertx.core.Promise;
|
||||||
|
import io.vertx.core.json.JsonObject;
|
||||||
|
import org.reflections.Reflections;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 后置执行Verticle - 在core启动后立即执行AppRun实现
|
||||||
|
* <br>Create date 2024-01-01 00:00:00
|
||||||
|
*
|
||||||
|
* @author <a href="https://qaiu.top">QAIU</a>
|
||||||
|
*/
|
||||||
|
public class PostExecVerticle extends AbstractVerticle {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(PostExecVerticle.class);
|
||||||
|
private static final Set<AppRun> appRunImplementations;
|
||||||
|
private static final AtomicBoolean lock = new AtomicBoolean(false);
|
||||||
|
|
||||||
|
static {
|
||||||
|
Reflections reflections = ReflectionUtil.getReflections();
|
||||||
|
Set<Class<? extends AppRun>> subTypesOf = reflections.getSubTypesOf(AppRun.class);
|
||||||
|
subTypesOf.add(DefaultAppRun.class);
|
||||||
|
appRunImplementations = CommonUtil.sortClassSet(subTypesOf);
|
||||||
|
if (appRunImplementations.isEmpty()) {
|
||||||
|
LOGGER.warn("未找到 AppRun 接口的实现类");
|
||||||
|
} else {
|
||||||
|
LOGGER.info("找到 {} 个 AppRun 接口的实现类", appRunImplementations.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void start(Promise<Void> startPromise) {
|
||||||
|
if (!lock.compareAndSet(false, true)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
LOGGER.info("PostExecVerticle 开始执行...");
|
||||||
|
|
||||||
|
if (appRunImplementations != null && !appRunImplementations.isEmpty()) {
|
||||||
|
appRunImplementations.forEach(appRun -> {
|
||||||
|
try {
|
||||||
|
LOGGER.info("执行 AppRun 实现: {}", appRun.getClass().getName());
|
||||||
|
JsonObject globalConfig = SharedDataUtil.getJsonConfig("globalConfig");
|
||||||
|
appRun.execute(globalConfig);
|
||||||
|
LOGGER.info("AppRun 实现 {} 执行完成", appRun.getClass().getName());
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.error("执行 AppRun 实现 {} 时发生错误",appRun.getClass().getName(), e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
LOGGER.info("未找到 AppRun 接口的实现类");
|
||||||
|
}
|
||||||
|
|
||||||
|
LOGGER.info("PostExecVerticle 执行完成");
|
||||||
|
startPromise.complete();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,18 +3,26 @@ package cn.qaiu.vx.core.verticle;
|
|||||||
import cn.qaiu.vx.core.util.*;
|
import cn.qaiu.vx.core.util.*;
|
||||||
import io.vertx.core.AbstractVerticle;
|
import io.vertx.core.AbstractVerticle;
|
||||||
import io.vertx.core.Future;
|
import io.vertx.core.Future;
|
||||||
|
import io.vertx.core.Handler;
|
||||||
import io.vertx.core.Promise;
|
import io.vertx.core.Promise;
|
||||||
import io.vertx.core.http.HttpClient;
|
import io.vertx.core.http.HttpClient;
|
||||||
|
import io.vertx.core.http.HttpClientOptions;
|
||||||
import io.vertx.core.http.HttpServer;
|
import io.vertx.core.http.HttpServer;
|
||||||
import io.vertx.core.http.HttpServerOptions;
|
import io.vertx.core.http.HttpServerOptions;
|
||||||
|
import io.vertx.core.http.HttpServerRequest;
|
||||||
|
import io.vertx.core.http.HttpServerResponse;
|
||||||
import io.vertx.core.json.JsonArray;
|
import io.vertx.core.json.JsonArray;
|
||||||
import io.vertx.core.json.JsonObject;
|
import io.vertx.core.json.JsonObject;
|
||||||
import io.vertx.core.net.PemKeyCertOptions;
|
import io.vertx.core.net.PemKeyCertOptions;
|
||||||
import io.vertx.ext.web.Route;
|
import io.vertx.ext.web.Route;
|
||||||
import io.vertx.ext.web.Router;
|
import io.vertx.ext.web.Router;
|
||||||
|
import io.vertx.ext.web.RoutingContext;
|
||||||
import io.vertx.ext.web.handler.StaticHandler;
|
import io.vertx.ext.web.handler.StaticHandler;
|
||||||
import io.vertx.ext.web.proxy.handler.ProxyHandler;
|
import io.vertx.ext.web.proxy.handler.ProxyHandler;
|
||||||
import io.vertx.httpproxy.HttpProxy;
|
import io.vertx.httpproxy.HttpProxy;
|
||||||
|
import io.vertx.httpproxy.ProxyContext;
|
||||||
|
import io.vertx.httpproxy.ProxyInterceptor;
|
||||||
|
import io.vertx.httpproxy.ProxyResponse;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -22,12 +30,18 @@ import org.slf4j.LoggerFactory;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>反向代理服务</p>
|
* <p>反向代理服务</p>
|
||||||
* <p>可以根据配置文件自动生成代理服务</p>
|
* <p>可以根据配置文件自动生成代理服务</p>
|
||||||
* <p>可以配置多个服务, 配置文件见示例</p>
|
* <p>可以配置多个服务, 配置文件见示例</p>
|
||||||
|
* <p>【优化】支持高并发场景,连接池复用,避免线程阻塞</p>
|
||||||
* <br>Create date 2021/9/2 0:41
|
* <br>Create date 2021/9/2 0:41
|
||||||
*
|
*
|
||||||
* @author <a href="https://qaiu.top">QAIU</a>
|
* @author <a href="https://qaiu.top">QAIU</a>
|
||||||
@@ -36,39 +50,201 @@ public class ReverseProxyVerticle extends AbstractVerticle {
|
|||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(ReverseProxyVerticle.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(ReverseProxyVerticle.class);
|
||||||
|
|
||||||
private static final String PATH_PROXY_CONFIG = SharedDataUtil
|
|
||||||
.getJsonConfig(ConfigConstant.GLOBAL_CONFIG)
|
|
||||||
.getString("proxyConf");
|
|
||||||
private static final Future<JsonObject> CONFIG = ConfigUtil.readYamlConfig(PATH_PROXY_CONFIG);
|
|
||||||
private static final String DEFAULT_PATH_404 = "webroot/err/page404.html";
|
private static final String DEFAULT_PATH_404 = "webroot/err/page404.html";
|
||||||
|
|
||||||
private static String serverName = "Vert.x-proxy-server"; //Server name in Http response header
|
private static String serverName = "Vert.x-proxy-server"; //Server name in Http response header
|
||||||
|
|
||||||
public static String REROUTE_PATH_PREFIX = "/__rrvpspp"; //re_route_vert_proxy_server_path_prefix 硬编码
|
public static String REROUTE_PATH_PREFIX = "/__rrvpspp"; //re_route_vert_proxy_server_path_prefix 硬编码
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 【优化】HttpClient连接池,按host:port缓存复用,避免每个请求都创建新连接
|
||||||
|
*/
|
||||||
|
private final Map<String, HttpClientEntry> httpClientPool = new ConcurrentHashMap<>();
|
||||||
|
private final List<HttpServer> httpServers = new ArrayList<>();
|
||||||
|
private volatile boolean stopping = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连接池条目。HttpProxy 会持有这里的 HttpClient 引用,不能在路由仍可用时关闭。
|
||||||
|
*/
|
||||||
|
private static class HttpClientEntry {
|
||||||
|
final HttpClient client;
|
||||||
|
|
||||||
|
HttpClientEntry(HttpClient client) {
|
||||||
|
this.client = client;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 【优化】高并发场景下的HttpClient配置
|
||||||
|
*/
|
||||||
|
private static final int MAX_POOL_SIZE = 32; // 最大连接池大小
|
||||||
|
private static final int MAX_WAIT_QUEUE_SIZE = 128; // 最大等待队列大小
|
||||||
|
private static final int CONNECT_TIMEOUT = 30000; // 连接超时30秒
|
||||||
|
private static final int IDLE_TIMEOUT = 60; // 空闲超时60秒
|
||||||
|
private static final boolean KEEP_ALIVE = true; // 启用Keep-Alive
|
||||||
|
private static final boolean PIPELINING = false; // 代理场景关闭管线化,避免慢响应堆积
|
||||||
@Override
|
@Override
|
||||||
public void start(Promise<Void> startPromise) {
|
public void start(Promise<Void> startPromise) {
|
||||||
CONFIG.onSuccess(this::handleProxyConfList);
|
stopping = false;
|
||||||
// createFileListener
|
String pathProxyConfig = SharedDataUtil
|
||||||
|
.getJsonConfig(ConfigConstant.GLOBAL_CONFIG)
|
||||||
|
.getString("proxyConf");
|
||||||
|
ConfigUtil.readYamlConfig(pathProxyConfig).onSuccess(config -> startProxyServers(config).onComplete(startPromise)).onFailure(e -> {
|
||||||
|
LOGGER.info("web代理配置已禁用,当前仅支持API调用");
|
||||||
startPromise.complete();
|
startPromise.complete();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 【优化】Verticle停止时清理HttpClient连接池
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void stop(Promise<Void> stopPromise) {
|
||||||
|
stopping = true;
|
||||||
|
LOGGER.info("Stopping ReverseProxyVerticle, closing {} servers and {} HttpClient connections...",
|
||||||
|
httpServers.size(), httpClientPool.size());
|
||||||
|
|
||||||
|
List<Future<Void>> serverCloseFutures = new ArrayList<>();
|
||||||
|
httpServers.forEach(server -> serverCloseFutures.add(server.close()));
|
||||||
|
Future<Void> serverCloseFuture = serverCloseFutures.isEmpty()
|
||||||
|
? Future.succeededFuture()
|
||||||
|
: Future.all(serverCloseFutures).mapEmpty();
|
||||||
|
|
||||||
|
serverCloseFuture.onComplete(serverClose -> {
|
||||||
|
List<Future<Void>> clientCloseFutures = new ArrayList<>();
|
||||||
|
closeHttpClients(clientCloseFutures);
|
||||||
|
Future<Void> clientCloseFuture = clientCloseFutures.isEmpty()
|
||||||
|
? Future.succeededFuture()
|
||||||
|
: Future.all(clientCloseFutures).mapEmpty();
|
||||||
|
|
||||||
|
clientCloseFuture.onComplete(clientClose -> {
|
||||||
|
if (serverClose.succeeded()) {
|
||||||
|
httpServers.clear();
|
||||||
|
}
|
||||||
|
if (clientClose.succeeded()) {
|
||||||
|
httpClientPool.clear();
|
||||||
|
}
|
||||||
|
if (serverClose.failed()) {
|
||||||
|
stopPromise.fail(serverClose.cause());
|
||||||
|
} else if (clientClose.failed()) {
|
||||||
|
stopPromise.fail(clientClose.cause());
|
||||||
|
} else {
|
||||||
|
stopPromise.complete();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void closeHttpClients(List<Future<Void>> closeFutures) {
|
||||||
|
httpClientPool.values().forEach(entry -> {
|
||||||
|
try {
|
||||||
|
closeFutures.add(entry.client.close());
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.warn("Error closing HttpClient: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 【优化】获取或创建HttpClient,实现连接池复用
|
||||||
|
* @param host 目标主机
|
||||||
|
* @param port 目标端口
|
||||||
|
* @return HttpClient实例
|
||||||
|
*/
|
||||||
|
private HttpClient getOrCreateHttpClient(String host, int port) {
|
||||||
|
String key = host + ":" + port;
|
||||||
|
HttpClientEntry entry = httpClientPool.computeIfAbsent(key, k -> {
|
||||||
|
LOGGER.info("Creating new HttpClient for {}", key);
|
||||||
|
HttpClientOptions options = new HttpClientOptions()
|
||||||
|
.setMaxPoolSize(MAX_POOL_SIZE) // 连接池大小
|
||||||
|
.setMaxWaitQueueSize(MAX_WAIT_QUEUE_SIZE) // 等待队列大小
|
||||||
|
.setConnectTimeout(CONNECT_TIMEOUT) // 连接超时
|
||||||
|
.setIdleTimeout(IDLE_TIMEOUT) // 空闲超时
|
||||||
|
.setKeepAlive(KEEP_ALIVE) // Keep-Alive
|
||||||
|
.setKeepAliveTimeout(120) // Keep-Alive超时120秒
|
||||||
|
.setPipelining(PIPELINING) // HTTP管线化
|
||||||
|
.setPipeliningLimit(10) // 管线化限制
|
||||||
|
.setDecompressionSupported(false) // 代理不解压,避免放大内存
|
||||||
|
.setTcpKeepAlive(true) // TCP Keep-Alive
|
||||||
|
.setTcpNoDelay(true) // 禁用Nagle算法,降低延迟
|
||||||
|
.setTcpFastOpen(true) // 启用TCP Fast Open
|
||||||
|
.setTcpQuickAck(true) // 启用TCP Quick ACK
|
||||||
|
.setReuseAddress(true) // 允许地址重用
|
||||||
|
.setReusePort(true); // 允许端口重用
|
||||||
|
return new HttpClientEntry(vertx.createHttpClient(options));
|
||||||
|
});
|
||||||
|
return entry.client;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 全局可信上游代理 IP 集合(如 nginx),仅这些 IP 的 X-Forwarded-For 会被信任
|
||||||
|
*/
|
||||||
|
private Set<String> globalTrustedProxies = new HashSet<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取主配置文件
|
* 获取主配置文件
|
||||||
*
|
*
|
||||||
* @param config proxy config
|
* @param config proxy config
|
||||||
*/
|
*/
|
||||||
private void handleProxyConfList(JsonObject config) {
|
private Future<Void> startProxyServers(JsonObject config) {
|
||||||
serverName = config.getString("server-name");
|
serverName = config.getString("server-name");
|
||||||
JsonArray proxyConfList = config.getJsonArray("proxy");
|
// 解析全局 trusted-proxies
|
||||||
if (proxyConfList != null) {
|
JsonArray trustedArr = config.getJsonArray("trusted-proxies");
|
||||||
proxyConfList.forEach(proxyConf -> {
|
if (trustedArr != null) {
|
||||||
if (proxyConf instanceof JsonObject) {
|
trustedArr.forEach(ip -> {
|
||||||
handleProxyConf((JsonObject) proxyConf);
|
if (ip instanceof String) {
|
||||||
|
globalTrustedProxies.add(((String) ip).trim());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
JsonArray proxyConfList = config.getJsonArray("proxy");
|
||||||
|
List<Future<Void>> listenFutures = new ArrayList<>();
|
||||||
|
if (proxyConfList != null) {
|
||||||
|
proxyConfList.forEach(proxyConf -> {
|
||||||
|
if (proxyConf instanceof JsonObject) {
|
||||||
|
listenFutures.add(handleProxyConf((JsonObject) proxyConf));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return listenFutures.isEmpty() ? Future.succeededFuture() : Future.all(listenFutures).mapEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析真实客户端 IP。
|
||||||
|
* 若直连来源在可信代理列表中,优先取 X-Real-IP,其次取 X-Forwarded-For 第一个值;
|
||||||
|
* 否则直接使用直连对端地址。
|
||||||
|
*/
|
||||||
|
private String resolveClientIp(HttpServerRequest request) {
|
||||||
|
String peerIp = request.remoteAddress().host();
|
||||||
|
if (globalTrustedProxies.contains(peerIp)) {
|
||||||
|
String realIp = request.getHeader("X-Real-IP");
|
||||||
|
if (StringUtils.isNotBlank(realIp)) {
|
||||||
|
return realIp.trim();
|
||||||
|
}
|
||||||
|
String xff = request.getHeader("X-Forwarded-For");
|
||||||
|
if (StringUtils.isNotBlank(xff)) {
|
||||||
|
return xff.split(",")[0].trim();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return peerIp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析 proxy-set-headers 中的 nginx 风格变量。
|
||||||
|
* 支持:$remote_addr、$proxy_add_x_forwarded_for、$scheme、$host;
|
||||||
|
* 其他值作为字面量直接使用。
|
||||||
|
*/
|
||||||
|
private String resolveHeaderVariable(String tpl, HttpServerRequest req, String clientIp) {
|
||||||
|
return switch (tpl) {
|
||||||
|
case "$remote_addr" -> clientIp;
|
||||||
|
case "$proxy_add_x_forwarded_for" -> {
|
||||||
|
String existing = req.getHeader("X-Forwarded-For");
|
||||||
|
yield StringUtils.isNotBlank(existing) ? existing + ", " + clientIp : clientIp;
|
||||||
|
}
|
||||||
|
case "$scheme" -> req.isSSL() ? "https" : "http";
|
||||||
|
case "$host" -> req.getHeader("Host");
|
||||||
|
default -> tpl;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -76,7 +252,7 @@ public class ReverseProxyVerticle extends AbstractVerticle {
|
|||||||
*
|
*
|
||||||
* @param proxyConf 代理配置
|
* @param proxyConf 代理配置
|
||||||
*/
|
*/
|
||||||
private void handleProxyConf(JsonObject proxyConf) {
|
private Future<Void> handleProxyConf(JsonObject proxyConf) {
|
||||||
// page404 path
|
// page404 path
|
||||||
if (proxyConf.containsKey(
|
if (proxyConf.containsKey(
|
||||||
|
|
||||||
@@ -97,18 +273,31 @@ public class ReverseProxyVerticle extends AbstractVerticle {
|
|||||||
proxyConf.put("page404", DEFAULT_PATH_404);
|
proxyConf.put("page404", DEFAULT_PATH_404);
|
||||||
}
|
}
|
||||||
|
|
||||||
final HttpClient httpClient = VertxHolder.getVertxInstance().createHttpClient();
|
|
||||||
Router proxyRouter = Router.router(vertx);
|
Router proxyRouter = Router.router(vertx);
|
||||||
|
|
||||||
// Add Server name header
|
// Add Server name header
|
||||||
proxyRouter.route().handler(ctx -> {
|
proxyRouter.route().handler(ctx -> {
|
||||||
|
if (stopping) {
|
||||||
|
sendProxyError(ctx, 503, "Service Unavailable");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String realPath = ctx.request().uri();
|
||||||
|
if (realPath.startsWith(REROUTE_PATH_PREFIX)) {
|
||||||
|
// vertx web proxy暂不支持rewrite, 所以这里进行手动替换, 请求地址中的请求path前缀替换为originPath
|
||||||
|
String rePath = realPath.replace(REROUTE_PATH_PREFIX, "");
|
||||||
|
ctx.reroute(rePath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ctx.response().ended() && !ctx.response().closed()) {
|
||||||
ctx.response().putHeader("Server", serverName);
|
ctx.response().putHeader("Server", serverName);
|
||||||
|
}
|
||||||
ctx.next();
|
ctx.next();
|
||||||
});
|
});
|
||||||
|
|
||||||
// http api proxy
|
// http api proxy
|
||||||
if (proxyConf.containsKey("location")) {
|
if (proxyConf.containsKey("location")) {
|
||||||
handleLocation(proxyConf.getJsonArray("location"), httpClient, proxyRouter);
|
handleLocation(proxyConf.getJsonArray("location"), proxyRouter);
|
||||||
}
|
}
|
||||||
|
|
||||||
// static server
|
// static server
|
||||||
@@ -117,20 +306,34 @@ public class ReverseProxyVerticle extends AbstractVerticle {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Send page404 page
|
// Send page404 page
|
||||||
proxyRouter.errorHandler(404, ctx -> ctx.response().sendFile(proxyConf.getString("page404")));
|
proxyRouter.errorHandler(404, ctx -> {
|
||||||
|
sendNotFoundPage(ctx, proxyConf.getString("page404"));
|
||||||
|
});
|
||||||
|
proxyRouter.errorHandler(500, this::handleProxyFailure);
|
||||||
|
|
||||||
HttpServer server = getHttpsServer(proxyConf);
|
HttpServer server = getHttpsServer(proxyConf);
|
||||||
server.requestHandler(proxyRouter);
|
server.requestHandler(proxyRouter);
|
||||||
|
|
||||||
Integer port = proxyConf.getInteger("listen");
|
Integer port = proxyConf.getInteger("listen");
|
||||||
LOGGER.info("proxy server start on {} port", port);
|
LOGGER.info("proxy server start on {} port", port);
|
||||||
server.listen(port);
|
return server.listen(port)
|
||||||
|
.onSuccess(s -> httpServers.add(s))
|
||||||
|
.onFailure(e -> LOGGER.error("proxy server start failed on {} port", port, e))
|
||||||
|
.mapEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
private HttpServer getHttpsServer(JsonObject proxyConf) {
|
private HttpServer getHttpsServer(JsonObject proxyConf) {
|
||||||
HttpServerOptions httpServerOptions = new HttpServerOptions()
|
HttpServerOptions httpServerOptions = new HttpServerOptions()
|
||||||
.setCompressionSupported(true);
|
// 【优化】高并发服务器配置
|
||||||
|
.setTcpKeepAlive(true) // TCP Keep-Alive
|
||||||
|
.setTcpNoDelay(true) // 禁用Nagle算法
|
||||||
|
.setCompressionSupported(true) // 启用压缩
|
||||||
|
.setAcceptBacklog(1024) // 限制积压队列,避免小容器内存膨胀
|
||||||
|
.setIdleTimeout(120) // 空闲超时120秒
|
||||||
|
.setTcpFastOpen(true) // 启用TCP Fast Open
|
||||||
|
.setTcpQuickAck(true) // 启用TCP Quick ACK
|
||||||
|
.setReuseAddress(true) // 允许地址重用
|
||||||
|
.setReusePort(true); // 允许端口重用
|
||||||
if (proxyConf.containsKey("ssl")) {
|
if (proxyConf.containsKey("ssl")) {
|
||||||
JsonObject sslConfig = proxyConf.getJsonObject("ssl");
|
JsonObject sslConfig = proxyConf.getJsonObject("ssl");
|
||||||
|
|
||||||
@@ -161,6 +364,67 @@ public class ReverseProxyVerticle extends AbstractVerticle {
|
|||||||
return vertx.createHttpServer(httpServerOptions);
|
return vertx.createHttpServer(httpServerOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void addProxyHandler(Route route, HttpProxy httpProxy) {
|
||||||
|
Handler<RoutingContext> proxyHandler = ProxyHandler.create(httpProxy);
|
||||||
|
route.handler(ctx -> {
|
||||||
|
try {
|
||||||
|
proxyHandler.handle(ctx);
|
||||||
|
} catch (Throwable t) {
|
||||||
|
LOGGER.error("反向代理处理异常", t);
|
||||||
|
ctx.fail(t);
|
||||||
|
}
|
||||||
|
}).failureHandler(this::handleProxyFailure);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleProxyFailure(RoutingContext ctx) {
|
||||||
|
Throwable failure = ctx.failure();
|
||||||
|
if (failure != null) {
|
||||||
|
LOGGER.error("反向代理路由失败", failure);
|
||||||
|
}
|
||||||
|
int statusCode = ctx.statusCode() > 0 ? ctx.statusCode() : 502;
|
||||||
|
if (statusCode < 400) {
|
||||||
|
statusCode = 502;
|
||||||
|
}
|
||||||
|
sendProxyError(ctx, statusCode, "Bad Gateway");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendNotFoundPage(RoutingContext ctx, String page404) {
|
||||||
|
HttpServerResponse response = ctx.response();
|
||||||
|
if (response.ended() || response.closed()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (response.headWritten()) {
|
||||||
|
response.reset();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
response.sendFile(page404)
|
||||||
|
.onFailure(e -> {
|
||||||
|
LOGGER.warn("发送代理 404 页面失败: {}", page404, e);
|
||||||
|
sendProxyError(ctx, 404, "404 not found");
|
||||||
|
});
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.warn("发送代理 404 页面异常: {}", page404, e);
|
||||||
|
sendProxyError(ctx, 404, "404 not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendProxyError(RoutingContext ctx, int statusCode, String message) {
|
||||||
|
HttpServerResponse response = ctx.response();
|
||||||
|
if (response.ended() || response.closed()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (!response.headWritten()) {
|
||||||
|
response.setStatusCode(statusCode).end(message);
|
||||||
|
} else {
|
||||||
|
response.reset();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.debug("代理响应已关闭,忽略错误响应", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理静态资源配置
|
* 处理静态资源配置
|
||||||
*
|
*
|
||||||
@@ -184,7 +448,6 @@ public class ReverseProxyVerticle extends AbstractVerticle {
|
|||||||
} else {
|
} else {
|
||||||
staticHandler = StaticHandler.create();
|
staticHandler = StaticHandler.create();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (staticConf.containsKey("directory-listing")) {
|
if (staticConf.containsKey("directory-listing")) {
|
||||||
staticHandler.setDirectoryListing(staticConf.getBoolean("directory-listing"));
|
staticHandler.setDirectoryListing(staticConf.getBoolean("directory-listing"));
|
||||||
} else if (staticConf.containsKey("index")) {
|
} else if (staticConf.containsKey("index")) {
|
||||||
@@ -197,10 +460,9 @@ public class ReverseProxyVerticle extends AbstractVerticle {
|
|||||||
* 处理Location配置 代理请求Location(和nginx类似?)
|
* 处理Location配置 代理请求Location(和nginx类似?)
|
||||||
*
|
*
|
||||||
* @param locationsConf location配置
|
* @param locationsConf location配置
|
||||||
* @param httpClient 客户端
|
|
||||||
* @param proxyRouter 代理路由
|
* @param proxyRouter 代理路由
|
||||||
*/
|
*/
|
||||||
private void handleLocation(JsonArray locationsConf, HttpClient httpClient, Router proxyRouter) {
|
private void handleLocation(JsonArray locationsConf, Router proxyRouter) {
|
||||||
|
|
||||||
locationsConf.stream().map(e -> (JsonObject) e).forEach(location -> {
|
locationsConf.stream().map(e -> (JsonObject) e).forEach(location -> {
|
||||||
// 代理规则
|
// 代理规则
|
||||||
@@ -211,14 +473,38 @@ public class ReverseProxyVerticle extends AbstractVerticle {
|
|||||||
String host = url.getHost();
|
String host = url.getHost();
|
||||||
int port = url.getPort();
|
int port = url.getPort();
|
||||||
if (port == -1) {
|
if (port == -1) {
|
||||||
port = 80;
|
port = 443;
|
||||||
}
|
}
|
||||||
String originPath = url.getPath();
|
String originPath = url.getPath();
|
||||||
LOGGER.info("path {}, originPath {}, to {}:{}", path, originPath, host, port);
|
LOGGER.info("path {}, originPath {}, to {}:{}", path, originPath, host, port);
|
||||||
|
|
||||||
// 注意这里不能origin多个代理地址, 一个实例只能代理一个origin
|
// 【优化】使用连接池获取HttpClient,避免每个location都创建新连接
|
||||||
|
final HttpClient httpClient = getOrCreateHttpClient(host, port);
|
||||||
final HttpProxy httpProxy = HttpProxy.reverseProxy(httpClient);
|
final HttpProxy httpProxy = HttpProxy.reverseProxy(httpClient);
|
||||||
httpProxy.origin(port, host);
|
httpProxy.origin(port, host);
|
||||||
|
|
||||||
|
// proxy-set-headers 支持(nginx 风格变量替换)
|
||||||
|
if (location.containsKey("proxy-set-headers")) {
|
||||||
|
final JsonObject headerConf = location.getJsonObject("proxy-set-headers");
|
||||||
|
httpProxy.addInterceptor(new ProxyInterceptor() {
|
||||||
|
@Override
|
||||||
|
public Future<ProxyResponse> handleProxyRequest(ProxyContext ctx) {
|
||||||
|
HttpServerRequest incoming = ctx.request().proxiedRequest();
|
||||||
|
String clientIp = resolveClientIp(incoming);
|
||||||
|
headerConf.forEach(entry -> {
|
||||||
|
Object val = entry.getValue();
|
||||||
|
if (val != null) {
|
||||||
|
String resolved = resolveHeaderVariable(val.toString(), incoming, clientIp);
|
||||||
|
if (resolved != null) {
|
||||||
|
ctx.request().putHeader(entry.getKey(), resolved);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return ProxyInterceptor.super.handleProxyRequest(ctx);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (StringUtils.isEmpty(path)) {
|
if (StringUtils.isEmpty(path)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -227,14 +513,14 @@ public class ReverseProxyVerticle extends AbstractVerticle {
|
|||||||
if (StringUtils.isEmpty(originPath) || path.equals(originPath)) {
|
if (StringUtils.isEmpty(originPath) || path.equals(originPath)) {
|
||||||
Route route = path.startsWith("~") ? proxyRouter.routeWithRegex(path.substring(1))
|
Route route = path.startsWith("~") ? proxyRouter.routeWithRegex(path.substring(1))
|
||||||
: proxyRouter.route(path);
|
: proxyRouter.route(path);
|
||||||
route.handler(ProxyHandler.create(httpProxy));
|
addProxyHandler(route, httpProxy);
|
||||||
} else {
|
} else {
|
||||||
// 配置 /api/, / => 请求 /api/test 代理后 /test
|
// 配置 /api/, / => 请求 /api/test 代理后 /test
|
||||||
// 配置 /api/, /xxx => 请求 /api/test 代理后 /xxx/test
|
// 配置 /api/, /xxx => 请求 /api/test 代理后 /xxx/test
|
||||||
final String path0 = path;
|
final String path0 = path;
|
||||||
final String originPath0 = REROUTE_PATH_PREFIX + originPath;
|
final String originPath0 = REROUTE_PATH_PREFIX + originPath;
|
||||||
|
|
||||||
proxyRouter.route(originPath0 + "*").handler(ProxyHandler.create(httpProxy));
|
addProxyHandler(proxyRouter.route(originPath0 + "*"), httpProxy);
|
||||||
proxyRouter.route(path0 + "*").handler(ctx -> {
|
proxyRouter.route(path0 + "*").handler(ctx -> {
|
||||||
String realPath = ctx.request().uri();
|
String realPath = ctx.request().uri();
|
||||||
if (realPath.startsWith(path0)) {
|
if (realPath.startsWith(path0)) {
|
||||||
@@ -245,6 +531,46 @@ public class ReverseProxyVerticle extends AbstractVerticle {
|
|||||||
ctx.next();
|
ctx.next();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
// 计算唯一后缀,避免多个 location 冲突
|
||||||
|
// String uniqueKey = (host + ":" + port + "|" + path).replaceAll("[^a-zA-Z0-9:_|/]", "");
|
||||||
|
// String uniqueSuffix = Integer.toHexString(uniqueKey.hashCode());
|
||||||
|
//
|
||||||
|
//// 规格化 originPath
|
||||||
|
// //String originPath = url.getPath(); // 原值
|
||||||
|
// if (StringUtils.isBlank(originPath)) originPath = "/";
|
||||||
|
//
|
||||||
|
//// 处理 index.html 的情况:用于首页兜底,其它子路径仍按目录穿透
|
||||||
|
// String indexFile;
|
||||||
|
// if (originPath.endsWith(".html")) {
|
||||||
|
// indexFile = originPath; // 例如 /index.html
|
||||||
|
// originPath = "/"; // 目录穿透基准改为根
|
||||||
|
// } else {
|
||||||
|
// indexFile = null;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//// 唯一内部挂载前缀
|
||||||
|
// final String originMount = REROUTE_PATH_PREFIX + uniqueSuffix + originPath;
|
||||||
|
//
|
||||||
|
//// 1) 目标挂载:所有被重写的请求最终到这里走 ProxyHandler
|
||||||
|
// proxyRouter.route(originMount + "*").handler(ProxyHandler.create(httpProxy));
|
||||||
|
//
|
||||||
|
//// 2) 从外部前缀 -> 内部挂载 的重写
|
||||||
|
// final String path0 = path;
|
||||||
|
// proxyRouter.route(path0 + "*").handler(ctx -> {
|
||||||
|
// String uri = ctx.request().uri();
|
||||||
|
// if (!uri.startsWith(path0)) { ctx.next(); return; }
|
||||||
|
//
|
||||||
|
// // 首页兜底:访问 /n2 或 /n2/ 时,重写到 index.html(如果配置了)
|
||||||
|
// if (indexFile != null && (uri.equals(path0) || uri.equals(path0.substring(0, path0.length()-1)))) {
|
||||||
|
// String rePath = originMount.endsWith("/") ? (originMount + indexFile.substring(1)) : (originMount + indexFile);
|
||||||
|
// ctx.reroute(rePath);
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // 一般穿透:/n2/xxx -> originMount + xxx
|
||||||
|
// String rePath = uri.replaceFirst("^" + path0, originMount);
|
||||||
|
// ctx.reroute(rePath);
|
||||||
|
// });
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (MalformedURLException e) {
|
} catch (MalformedURLException e) {
|
||||||
|
|||||||
@@ -22,22 +22,22 @@ public class RouterVerticle extends AbstractVerticle {
|
|||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(RouterVerticle.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(RouterVerticle.class);
|
||||||
|
|
||||||
private static final int port = SharedDataUtil.getValueForServerConfig("port");
|
|
||||||
private static final Router router = new RouterHandlerFactory(
|
|
||||||
SharedDataUtil.getJsonStringForServerConfig("contextPath")).createRouter();
|
|
||||||
|
|
||||||
private static final JsonObject globalConfig = SharedDataUtil.getJsonConfig("globalConfig");
|
|
||||||
|
|
||||||
private HttpServer server;
|
private HttpServer server;
|
||||||
|
private Router router;
|
||||||
|
private int port;
|
||||||
|
private JsonObject globalConfig;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
LOGGER.info(JacksonConfig.class.getSimpleName() + " >> ");
|
LOGGER.info(JacksonConfig.class.getSimpleName() + " >> ");
|
||||||
JacksonConfig.nothing();
|
JacksonConfig.nothing();
|
||||||
LOGGER.info("To start listening to port {} ......", port);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(Promise<Void> startPromise) {
|
public void start(Promise<Void> startPromise) {
|
||||||
|
port = SharedDataUtil.getValueForServerConfig("port");
|
||||||
|
globalConfig = SharedDataUtil.getJsonConfig("globalConfig");
|
||||||
|
LOGGER.info("To start listening to port {} ......", port);
|
||||||
|
|
||||||
// 端口是否占用
|
// 端口是否占用
|
||||||
if (CommonUtil.isPortUsing(port)) {
|
if (CommonUtil.isPortUsing(port)) {
|
||||||
throw new RuntimeException("Start fail: the '" + port + "' port is already in use...");
|
throw new RuntimeException("Start fail: the '" + port + "' port is already in use...");
|
||||||
@@ -49,9 +49,23 @@ public class RouterVerticle extends AbstractVerticle {
|
|||||||
options = new HttpServerOptions();
|
options = new HttpServerOptions();
|
||||||
}
|
}
|
||||||
options.setPort(port);
|
options.setPort(port);
|
||||||
|
|
||||||
|
// 【优化】高并发服务器配置
|
||||||
|
options.setTcpKeepAlive(true) // TCP Keep-Alive
|
||||||
|
.setTcpNoDelay(true) // 禁用Nagle算法,降低延迟
|
||||||
|
.setCompressionSupported(true) // 启用压缩
|
||||||
|
.setAcceptBacklog(50000) // 增加积压队列到50000,防止高并发时连接被拒绝
|
||||||
|
.setIdleTimeout(120) // 空闲超时120秒
|
||||||
|
.setTcpFastOpen(true) // 启用TCP Fast Open
|
||||||
|
.setTcpQuickAck(true) // 启用TCP Quick ACK
|
||||||
|
.setReuseAddress(true) // 允许地址重用
|
||||||
|
.setReusePort(true); // 允许端口重用
|
||||||
|
|
||||||
|
router = new RouterHandlerFactory(
|
||||||
|
SharedDataUtil.getJsonStringForServerConfig("contextPath")).createRouter();
|
||||||
server = vertx.createHttpServer(options);
|
server = vertx.createHttpServer(options);
|
||||||
|
|
||||||
server.requestHandler(router).webSocketHandler(s->{}).listen()
|
server.requestHandler(router).listen()
|
||||||
.onSuccess(s -> startPromise.complete())
|
.onSuccess(s -> startPromise.complete())
|
||||||
.onFailure(e -> startPromise.fail(e.getCause()));
|
.onFailure(e -> startPromise.fail(e.getCause()));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,11 +5,15 @@ import cn.qaiu.vx.core.base.BaseAsyncService;
|
|||||||
import cn.qaiu.vx.core.util.ReflectionUtil;
|
import cn.qaiu.vx.core.util.ReflectionUtil;
|
||||||
import io.vertx.core.AbstractVerticle;
|
import io.vertx.core.AbstractVerticle;
|
||||||
import io.vertx.core.Promise;
|
import io.vertx.core.Promise;
|
||||||
|
import io.vertx.core.eventbus.MessageConsumer;
|
||||||
|
import io.vertx.core.json.JsonObject;
|
||||||
import io.vertx.serviceproxy.ServiceBinder;
|
import io.vertx.serviceproxy.ServiceBinder;
|
||||||
import org.reflections.Reflections;
|
import org.reflections.Reflections;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
@@ -24,26 +28,48 @@ public class ServiceVerticle extends AbstractVerticle {
|
|||||||
Logger LOGGER = LoggerFactory.getLogger(ServiceVerticle.class);
|
Logger LOGGER = LoggerFactory.getLogger(ServiceVerticle.class);
|
||||||
private static final AtomicInteger ID = new AtomicInteger(1);
|
private static final AtomicInteger ID = new AtomicInteger(1);
|
||||||
private static final Set<Class<?>> handlers;
|
private static final Set<Class<?>> handlers;
|
||||||
|
private final List<MessageConsumer<JsonObject>> consumers = new ArrayList<>();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
Reflections reflections = ReflectionUtil.getReflections();
|
Reflections reflections = ReflectionUtil.getReflections();
|
||||||
handlers = reflections.getTypesAnnotatedWith(Service.class);
|
handlers = reflections.getTypesAnnotatedWith(Service.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(Promise<Void> startPromise) {
|
public void start(Promise<Void> startPromise) {
|
||||||
ServiceBinder binder = new ServiceBinder(vertx);
|
ServiceBinder binder = new ServiceBinder(vertx);
|
||||||
if (null != handlers && handlers.size() > 0) {
|
if (null != handlers && handlers.size() > 0) {
|
||||||
|
// handlers转为拼接类列表,xxx,yyy,zzz
|
||||||
|
StringBuilder serviceNames = new StringBuilder();
|
||||||
handlers.forEach(asyncService -> {
|
handlers.forEach(asyncService -> {
|
||||||
try {
|
try {
|
||||||
|
serviceNames.append(asyncService.getName()).append("|");
|
||||||
BaseAsyncService asInstance = (BaseAsyncService) ReflectionUtil.newWithNoParam(asyncService);
|
BaseAsyncService asInstance = (BaseAsyncService) ReflectionUtil.newWithNoParam(asyncService);
|
||||||
binder.setAddress(asInstance.getAddress()).register(asInstance.getAsyncInterfaceClass(), asInstance);
|
String address = asInstance.getAddress();
|
||||||
|
MessageConsumer<JsonObject> consumer = binder.setAddress(address)
|
||||||
|
.register(asInstance.getAsyncInterfaceClass(), asInstance);
|
||||||
|
consumers.add(consumer);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOGGER.error(e.getMessage());
|
LOGGER.error("Failed to register service: {}", asyncService.getName(), e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
LOGGER.info("registered async services -> id: {}", ID.getAndIncrement());
|
|
||||||
|
LOGGER.info("registered async services -> id: {}, name: {}", ID.getAndIncrement(), serviceNames.toString());
|
||||||
}
|
}
|
||||||
startPromise.complete();
|
startPromise.complete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stop(Promise<Void> stopPromise) {
|
||||||
|
int count = consumers.size();
|
||||||
|
consumers.forEach(consumer -> {
|
||||||
|
try {
|
||||||
|
consumer.unregister();
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOGGER.warn("Failed to unregister service consumer at address: {}", consumer.address(), e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
consumers.clear();
|
||||||
|
LOGGER.info("ServiceVerticle stopped, unregistered {} services", count);
|
||||||
|
stopPromise.complete();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -8,14 +8,14 @@ import io.vertx.core.net.ProxyOptions;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@DataObject
|
@DataObject
|
||||||
@JsonGen(publicConverter = false)
|
//@JsonGen(publicConverter = false)
|
||||||
public class HttpProxyConf {
|
public class HttpProxyConf {
|
||||||
|
|
||||||
public static final String DEFAULT_USERNAME = UUID.randomUUID().toString();
|
public static final String DEFAULT_USERNAME = UUID.randomUUID().toString();
|
||||||
|
|
||||||
public static final String DEFAULT_PASSWORD = UUID.randomUUID().toString();
|
public static final String DEFAULT_PASSWORD = UUID.randomUUID().toString();
|
||||||
|
|
||||||
public static final Integer DEFAULT_PORT = 6402;
|
public static final Integer DEFAULT_PORT = 6432;
|
||||||
|
|
||||||
public static final Integer DEFAULT_TIMEOUT = 15000;
|
public static final Integer DEFAULT_TIMEOUT = 15000;
|
||||||
|
|
||||||
@@ -32,7 +32,7 @@ public class HttpProxyConf {
|
|||||||
public HttpProxyConf() {
|
public HttpProxyConf() {
|
||||||
this.username = DEFAULT_USERNAME;
|
this.username = DEFAULT_USERNAME;
|
||||||
this.password = DEFAULT_PASSWORD;
|
this.password = DEFAULT_PASSWORD;
|
||||||
this.timeout = DEFAULT_PORT;
|
this.port = DEFAULT_PORT;
|
||||||
this.timeout = DEFAULT_TIMEOUT;
|
this.timeout = DEFAULT_TIMEOUT;
|
||||||
this.preProxyOptions = new ProxyOptions();
|
this.preProxyOptions = new ProxyOptions();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,134 @@
|
|||||||
|
package cn.qaiu.vx.core.test;
|
||||||
|
|
||||||
|
import io.vertx.core.json.JsonArray;
|
||||||
|
import io.vertx.core.json.JsonObject;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 单元测试:验证 RouterHandlerFactory 关于 JsonObject/JsonArray 参数绑定的核心分支逻辑是否正确
|
||||||
|
* (不启动整个 Vert.x 服务器,直接用 Vert.x JsonObject/JsonArray API 模拟验证关键逻辑)
|
||||||
|
*/
|
||||||
|
public class JsonBodyBindingLogicTest {
|
||||||
|
|
||||||
|
// === 模拟 handlerMethod 中的 JSON body 绑定逻辑 ===
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 模拟:content-type = application/json,body 是 JsonObject
|
||||||
|
* 期望:JsonObject 类型参数被正确绑定
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testJsonObjectBinding() {
|
||||||
|
String bodyStr = "{\"name\":\"test\",\"value\":123}";
|
||||||
|
|
||||||
|
// 模拟 ctx.body().asJsonObject()
|
||||||
|
JsonObject body = parseAsJsonObject(bodyStr);
|
||||||
|
Assert.assertNotNull("body 应能解析为 JsonObject", body);
|
||||||
|
|
||||||
|
// 模拟绑定逻辑中的类型判断
|
||||||
|
String targetType = JsonObject.class.getName();
|
||||||
|
boolean matched = JsonObject.class.getName().equals(targetType);
|
||||||
|
Assert.assertTrue("JsonObject 类型应命中绑定分支", matched);
|
||||||
|
|
||||||
|
// 模拟结果
|
||||||
|
Object bound = body; // parameterValueList.put(k, body)
|
||||||
|
Assert.assertNotNull("JsonObject 参数应被绑定(非null)", bound);
|
||||||
|
Assert.assertEquals("name字段应为test", "test", ((JsonObject) bound).getString("name"));
|
||||||
|
Assert.assertEquals("value字段应为123", 123, (int) ((JsonObject) bound).getInteger("value"));
|
||||||
|
|
||||||
|
System.out.println("[PASS] testJsonObjectBinding: JsonObject 绑定成功 -> " + bound);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 模拟:content-type = application/json,body 是 JsonArray
|
||||||
|
* 期望:JsonArray 类型参数被正确绑定
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testJsonArrayBinding() {
|
||||||
|
String bodyStr = "[1,2,3]";
|
||||||
|
|
||||||
|
// body 解析为 JsonObject 应返回 null
|
||||||
|
JsonObject bodyAsObj = parseAsJsonObject(bodyStr);
|
||||||
|
Assert.assertNull("JsonArray body 解析为 JsonObject 应为 null", bodyAsObj);
|
||||||
|
|
||||||
|
// 进入 else 分支,解析为 JsonArray
|
||||||
|
JsonArray bodyArr = parseAsJsonArray(bodyStr);
|
||||||
|
Assert.assertNotNull("body 应能解析为 JsonArray", bodyArr);
|
||||||
|
|
||||||
|
String targetType = JsonArray.class.getName();
|
||||||
|
boolean matched = JsonArray.class.getName().equals(targetType);
|
||||||
|
Assert.assertTrue("JsonArray 类型应命中绑定分支", matched);
|
||||||
|
|
||||||
|
Object bound = bodyArr;
|
||||||
|
Assert.assertNotNull("JsonArray 参数应被绑定(非null)", bound);
|
||||||
|
Assert.assertEquals("数组大小应为3", 3, ((JsonArray) bound).size());
|
||||||
|
|
||||||
|
System.out.println("[PASS] testJsonArrayBinding: JsonArray 绑定成功, size=" + ((JsonArray) bound).size());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证旧代码的 bug:条件 ctx.body().asJsonObject() != null 会把 JsonArray body 排除在外
|
||||||
|
* 新代码只判断 content-type,在 body==null 时才进 else 分支处理 JsonArray
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testOldConditionBug() {
|
||||||
|
String jsonArrayBody = "[1,2,3]";
|
||||||
|
|
||||||
|
// 旧代码条件:content-type==json && asJsonObject()!=null
|
||||||
|
// 对于 JsonArray body,asJsonObject() 返回 null,整个 if 跳过
|
||||||
|
JsonObject wrongParsed = parseAsJsonObject(jsonArrayBody);
|
||||||
|
boolean oldConditionPassed = wrongParsed != null; // 旧代码的第二个条件
|
||||||
|
Assert.assertFalse("旧代码 bug: JsonArray body 会导致 asJsonObject()==null,整个分支跳过", oldConditionPassed);
|
||||||
|
|
||||||
|
// 新代码:先进 if,body==null 再走 else 解析 JsonArray
|
||||||
|
boolean newConditionFirst = true; // content-type 匹配
|
||||||
|
JsonObject newBody = parseAsJsonObject(jsonArrayBody);
|
||||||
|
boolean newBodyIsNull = newBody == null; // null -> 进 else
|
||||||
|
Assert.assertTrue("新代码: body 解析为 null 时应走 else 分支解析 JsonArray", newBodyIsNull);
|
||||||
|
|
||||||
|
JsonArray newArr = parseAsJsonArray(jsonArrayBody);
|
||||||
|
Assert.assertNotNull("新代码: else 分支正确解析出 JsonArray", newArr);
|
||||||
|
|
||||||
|
System.out.println("[PASS] testOldConditionBug: 修复验证通过,新代码正确处理 JsonArray body");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证:JsonObject 参数旧代码没有绑定分支(只处理实体类)
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testOldMissingJsonObjectBranch() {
|
||||||
|
String bodyStr = "{\"key\":\"value\"}";
|
||||||
|
JsonObject body = parseAsJsonObject(bodyStr);
|
||||||
|
|
||||||
|
// 旧代码只调用 matchRegList(entityPackagesReg, typeName)
|
||||||
|
// 对于 io.vertx.core.json.JsonObject,该方法返回 false,不会被绑定
|
||||||
|
String typeName = JsonObject.class.getName(); // "io.vertx.core.json.JsonObject"
|
||||||
|
// entityPackagesReg 一般是 "cn.qaiu.*" 这类,不会匹配 io.vertx
|
||||||
|
boolean oldWouldBind = typeName.startsWith("cn.qaiu"); // 模拟旧代码逻辑
|
||||||
|
Assert.assertFalse("旧代码 bug: JsonObject 参数不会被绑定", oldWouldBind);
|
||||||
|
|
||||||
|
// 新代码:增加了 JsonObject 类型判断
|
||||||
|
boolean newWouldBind = JsonObject.class.getName().equals(typeName);
|
||||||
|
Assert.assertTrue("新代码: JsonObject 参数应能被绑定", newWouldBind);
|
||||||
|
|
||||||
|
System.out.println("[PASS] testOldMissingJsonObjectBranch: 修复验证通过");
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===== 辅助方法:模拟 Vert.x RequestBody 的 asJsonObject/asJsonArray 行为 =====
|
||||||
|
|
||||||
|
private JsonObject parseAsJsonObject(String str) {
|
||||||
|
try {
|
||||||
|
return new JsonObject(str);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private JsonArray parseAsJsonArray(String str) {
|
||||||
|
try {
|
||||||
|
return new JsonArray(str);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,125 @@
|
|||||||
|
package cn.qaiu.vx.core.test;
|
||||||
|
|
||||||
|
import cn.qaiu.vx.core.util.VertxHolder;
|
||||||
|
import io.vertx.core.Vertx;
|
||||||
|
import io.vertx.core.json.JsonArray;
|
||||||
|
import io.vertx.core.json.JsonObject;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.http.HttpClient;
|
||||||
|
import java.net.http.HttpRequest;
|
||||||
|
import java.net.http.HttpResponse;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 集成测试: 验证 RouterHandlerFactory 对 JsonObject/JsonArray 参数绑定逻辑是否正确
|
||||||
|
*
|
||||||
|
* 运行方式: mvn test-compile -pl core && java -cp "core/target/test-classes:core/target/classes:..." \
|
||||||
|
* cn.qaiu.vx.core.test.RouterHandlerBindingTest
|
||||||
|
*
|
||||||
|
* 或直接在 IDE 中运行 main 方法。
|
||||||
|
*/
|
||||||
|
public class RouterHandlerBindingTest {
|
||||||
|
|
||||||
|
static final int TEST_PORT = 18989;
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
System.out.println("=== RouterHandler JsonObject/JsonArray 绑定测试 ===\n");
|
||||||
|
|
||||||
|
// 1. 先初始化 Vert.x 与 VertxHolder ——必须在加载 RouterHandlerFactory 之前
|
||||||
|
Vertx vertx = Vertx.vertx();
|
||||||
|
VertxHolder.init(vertx);
|
||||||
|
|
||||||
|
// 2. 向 SharedData 注入最小化配置
|
||||||
|
// baseLocations 指向测试包,使 Reflections 只扫描 TestJsonHandler
|
||||||
|
vertx.sharedData().getLocalMap("local").put("customConfig", new JsonObject()
|
||||||
|
.put("baseLocations", "cn.qaiu.vx.core.test")
|
||||||
|
.put("routeTimeOut", 30000)
|
||||||
|
.put("entityPackagesReg", new JsonArray()));
|
||||||
|
// ReverseProxyVerticle.<clinit> 需要 globalConfig.proxyConf(非空字符串即可)
|
||||||
|
vertx.sharedData().getLocalMap("local").put("globalConfig", new JsonObject()
|
||||||
|
.put("proxyConf", "proxy.yml"));
|
||||||
|
|
||||||
|
// 3. 创建 Router(此时才触发 BaseHttpApi.reflections 静态字段初始化)
|
||||||
|
// 用反射延迟加载,确保上面的 SharedData 已就绪
|
||||||
|
cn.qaiu.vx.core.handlerfactory.RouterHandlerFactory factory =
|
||||||
|
new cn.qaiu.vx.core.handlerfactory.RouterHandlerFactory("api");
|
||||||
|
io.vertx.ext.web.Router router = factory.createRouter();
|
||||||
|
|
||||||
|
// 4. 启动 HTTP 服务器
|
||||||
|
CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
vertx.createHttpServer()
|
||||||
|
.requestHandler(router)
|
||||||
|
.listen(TEST_PORT, res -> {
|
||||||
|
if (res.succeeded()) {
|
||||||
|
System.out.println("✔ 测试服务器启动成功 port=" + TEST_PORT);
|
||||||
|
} else {
|
||||||
|
System.err.println("✘ 服务器启动失败: " + res.cause().getMessage());
|
||||||
|
}
|
||||||
|
latch.countDown();
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!latch.await(5, TimeUnit.SECONDS)) {
|
||||||
|
System.err.println("服务器启动超时");
|
||||||
|
vertx.close();
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
Thread.sleep(100); // 等 Vert.x 就绪
|
||||||
|
|
||||||
|
// 5. 执行测试
|
||||||
|
boolean allPassed = true;
|
||||||
|
allPassed &= testJsonObject();
|
||||||
|
allPassed &= testJsonArray();
|
||||||
|
|
||||||
|
// 6. 关闭
|
||||||
|
CountDownLatch closeLatch = new CountDownLatch(1);
|
||||||
|
vertx.close(v -> closeLatch.countDown());
|
||||||
|
closeLatch.await(3, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
System.out.println("\n" + (allPassed ? "✅ 全部测试通过!" : "❌ 存在测试失败!"));
|
||||||
|
System.exit(allPassed ? 0 : 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- 子测试 ----------
|
||||||
|
|
||||||
|
private static boolean testJsonObject() throws Exception {
|
||||||
|
String bodyStr = "{\"name\":\"test\",\"value\":123}";
|
||||||
|
String respBody = post("/api/test/json-object", bodyStr);
|
||||||
|
System.out.println("[JsonObject] 响应: " + respBody);
|
||||||
|
|
||||||
|
JsonObject result = new JsonObject(respBody);
|
||||||
|
JsonObject data = result.getJsonObject("data");
|
||||||
|
boolean bound = data != null && Boolean.TRUE.equals(data.getBoolean("bound"));
|
||||||
|
System.out.println("[JsonObject] " + (bound
|
||||||
|
? "PASS ✅ body 正确绑定为 JsonObject"
|
||||||
|
: "FAIL ❌ body 未绑定 (null)"));
|
||||||
|
return bound;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean testJsonArray() throws Exception {
|
||||||
|
String bodyStr = "[1,2,3]";
|
||||||
|
String respBody = post("/api/test/json-array", bodyStr);
|
||||||
|
System.out.println("[JsonArray] 响应: " + respBody);
|
||||||
|
|
||||||
|
JsonObject result = new JsonObject(respBody);
|
||||||
|
JsonObject data = result.getJsonObject("data");
|
||||||
|
boolean bound = data != null
|
||||||
|
&& Boolean.TRUE.equals(data.getBoolean("bound"))
|
||||||
|
&& Integer.valueOf(3).equals(data.getInteger("size"));
|
||||||
|
System.out.println("[JsonArray] " + (bound
|
||||||
|
? "PASS ✅ body 正确绑定为 JsonArray, size=3"
|
||||||
|
: "FAIL ❌ body 未绑定 或 size 不对"));
|
||||||
|
return bound;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String post(String path, String body) throws Exception {
|
||||||
|
HttpClient client = HttpClient.newHttpClient();
|
||||||
|
HttpRequest req = HttpRequest.newBuilder()
|
||||||
|
.uri(URI.create("http://localhost:" + TEST_PORT + path))
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.POST(HttpRequest.BodyPublishers.ofString(body))
|
||||||
|
.build();
|
||||||
|
return client.send(req, HttpResponse.BodyHandlers.ofString()).body();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package cn.qaiu.vx.core.test;
|
||||||
|
|
||||||
|
import cn.qaiu.vx.core.annotaions.RouteHandler;
|
||||||
|
import cn.qaiu.vx.core.annotaions.RouteMapping;
|
||||||
|
import cn.qaiu.vx.core.enums.MIMEType;
|
||||||
|
import cn.qaiu.vx.core.enums.RouteMethod;
|
||||||
|
import cn.qaiu.vx.core.model.JsonResult;
|
||||||
|
import io.vertx.core.Future;
|
||||||
|
import io.vertx.core.json.JsonArray;
|
||||||
|
import io.vertx.core.json.JsonObject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用于测试 RouterHandlerFactory 对 JsonObject/JsonArray 参数绑定的测试 Handler
|
||||||
|
*/
|
||||||
|
@RouteHandler("test")
|
||||||
|
public class TestJsonHandler {
|
||||||
|
|
||||||
|
/** POST /api/test/json-object Body: {"name":"test","value":123} */
|
||||||
|
@RouteMapping(value = "/json-object", method = RouteMethod.POST, requestMIMEType = MIMEType.APPLICATION_JSON)
|
||||||
|
public Future<JsonResult> testJsonObject(JsonObject body) {
|
||||||
|
// 只返回是否绑定成功及已知字段值,不嵌套原始 body 避免 toJsonObject() 循环
|
||||||
|
boolean bound = body != null;
|
||||||
|
String nameVal = bound ? body.getString("name", "") : "";
|
||||||
|
return Future.succeededFuture(JsonResult.data(new io.vertx.core.json.JsonObject()
|
||||||
|
.put("bound", bound)
|
||||||
|
.put("name", nameVal)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** POST /api/test/json-array Body: [1,2,3] */
|
||||||
|
@RouteMapping(value = "/json-array", method = RouteMethod.POST, requestMIMEType = MIMEType.APPLICATION_JSON)
|
||||||
|
public Future<JsonResult> testJsonArray(JsonArray body) {
|
||||||
|
return Future.succeededFuture(JsonResult.data(new io.vertx.core.json.JsonObject()
|
||||||
|
.put("bound", body != null)
|
||||||
|
.put("size", body != null ? body.size() : -1)));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package cn.qaiu.vx.core.verticle;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
public class HttpProxyBase64Test {
|
||||||
|
@Test
|
||||||
|
public void matchesJdkBasicDecoder() {
|
||||||
|
for (String value : new String[]{"", "Zg", "Zg==", "Zm8=", "dXNlcjpwYXNz", "+/8=", "ABCD",
|
||||||
|
"AB==", "Z", "Zg=", "Zm8==", "Zg==suffix", "Zg==\n", "-_8=", "AA\u2603", "===="}) {
|
||||||
|
byte[] expected;
|
||||||
|
try {
|
||||||
|
expected = Base64.getDecoder().decode(value);
|
||||||
|
} catch (IllegalArgumentException expectedFailure) {
|
||||||
|
try {
|
||||||
|
HttpProxyVerticle.decodeProxyAuthorization(value);
|
||||||
|
} catch (IllegalArgumentException actualFailure) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
fail("Accepted invalid proxy credentials: " + value);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
assertArrayEquals(expected, HttpProxyVerticle.decodeProxyAuthorization(value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -e
|
||||||
|
|
||||||
|
# Fix permissions on volume-mounted directories (runs as root)
|
||||||
|
chown -R appuser:appgroup /app/db /app/logs /app/resources 2>/dev/null || true
|
||||||
|
|
||||||
|
# Run Java directly - entrypoint is PID 1, exec makes Java PID 1
|
||||||
|
# Docker SIGTERM goes directly to Java, triggering ShutdownHook
|
||||||
|
DEFAULT_JVM_OPTS="-Xmx${JVM_XMX:-512M} -Xss${JVM_XSS:-512k} -XX:MaxDirectMemorySize=${JVM_MAX_DIRECT_MEMORY:-256M} -DNFD_LOG_LEVEL=${NFD_LOG_LEVEL:-info} -DNFD_PLAYGROUND_ENABLED=${NFD_PLAYGROUND_ENABLED:-false}"
|
||||||
|
if [ -n "${JVM_MAX_METASPACE:-}" ]; then
|
||||||
|
DEFAULT_JVM_OPTS="$DEFAULT_JVM_OPTS -XX:MaxMetaspaceSize=$JVM_MAX_METASPACE"
|
||||||
|
fi
|
||||||
|
# Keep this after JVM_OPTS to disable the intrinsic even with an older base image.
|
||||||
|
VECTOR_MODULE_OPT=
|
||||||
|
if java --list-modules 2>/dev/null | grep -q '^jdk.incubator.vector@'; then
|
||||||
|
VECTOR_MODULE_OPT=--add-modules=jdk.incubator.vector
|
||||||
|
fi
|
||||||
|
exec java ${VECTOR_MODULE_OPT} ${DEFAULT_JVM_OPTS} ${JVM_OPTS} -XX:-UseBASE64Intrinsics -Duser.timezone=${TZ:-Asia/Shanghai} -jar /app/netdisk-fast-download.jar
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"dependencies": {
|
||||||
|
"mvn": "^3.5.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
Whitespace-only changes.
+4
-5
@@ -4,26 +4,26 @@ NFD 解析器模块:聚合各类网盘/分享页解析,统一输出文件列
|
|||||||
|
|
||||||
- 语言:Java 17
|
- 语言:Java 17
|
||||||
- 构建:Maven
|
- 构建:Maven
|
||||||
- 模块版本:10.1.17
|
- 模块版本:10.2.7
|
||||||
|
|
||||||
## 依赖(Maven Central)
|
## 依赖(Maven Central)
|
||||||
```xml
|
```xml
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cn.qaiu</groupId>
|
<groupId>cn.qaiu</groupId>
|
||||||
<artifactId>parser</artifactId>
|
<artifactId>parser</artifactId>
|
||||||
<version>10.1.17</version>
|
<version>10.2.7</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
```
|
```
|
||||||
- Gradle Groovy DSL:
|
- Gradle Groovy DSL:
|
||||||
```groovy
|
```groovy
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'cn.qaiu:parser:10.1.17'
|
implementation 'cn.qaiu:parser:10.2.7'
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
- Gradle Kotlin DSL:
|
- Gradle Kotlin DSL:
|
||||||
```kotlin
|
```kotlin
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation("cn.qaiu:parser:10.1.17")
|
implementation("cn.qaiu:parser:10.2.7")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -96,7 +96,6 @@ String url = tool.parseSync();
|
|||||||
## 文档
|
## 文档
|
||||||
- parser/doc/README.md:解析约定、示例、IDEA `.http` 调试
|
- parser/doc/README.md:解析约定、示例、IDEA `.http` 调试
|
||||||
- **parser/doc/JAVASCRIPT_PARSER_GUIDE.md:JavaScript解析器开发完整指南** - 使用JavaScript编写自定义解析器
|
- **parser/doc/JAVASCRIPT_PARSER_GUIDE.md:JavaScript解析器开发完整指南** - 使用JavaScript编写自定义解析器
|
||||||
- **parser/doc/PYTHON_PARSER_GUIDE.md:Python解析器开发完整指南** - 使用Python(GraalPy)编写自定义解析器
|
|
||||||
- **parser/doc/CUSTOM_PARSER_GUIDE.md:自定义解析器扩展完整指南** - Java自定义解析器扩展
|
- **parser/doc/CUSTOM_PARSER_GUIDE.md:自定义解析器扩展完整指南** - Java自定义解析器扩展
|
||||||
- **parser/doc/CUSTOM_PARSER_QUICKSTART.md:自定义解析器快速开始** - 快速上手指南
|
- **parser/doc/CUSTOM_PARSER_QUICKSTART.md:自定义解析器快速开始** - 快速上手指南
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,33 @@
|
|||||||
|------|------|------|------|
|
|------|------|------|------|
|
||||||
| url | string | ✅ 是 | 分享链接(需URL编码) |
|
| url | string | ✅ 是 | 分享链接(需URL编码) |
|
||||||
| pwd | string | ❌ 否 | 分享密码 |
|
| pwd | string | ❌ 否 | 分享密码 |
|
||||||
|
| auth | string | ❌ 否 | 认证参数(AES加密后的JSON,用于需要登录的网盘) |
|
||||||
|
|
||||||
|
### 认证参数说明(v0.2.1+)
|
||||||
|
|
||||||
|
部分网盘需要登录后的 Cookie 或 Authorization 才能下载。夸克小文件可以不登录;UC 必须有 Cookie。`auth` 参数用于传递认证信息:
|
||||||
|
|
||||||
|
**加密方式**:
|
||||||
|
- 算法:AES/ECB/PKCS5Padding
|
||||||
|
- 密钥:由自建服务显式配置并通过安全渠道交给可信客户端(无公共默认值)
|
||||||
|
- 流程:JSON → AES加密 → Base64 → URL编码
|
||||||
|
|
||||||
|
**JSON 结构**:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"authType": "cookie", // 认证类型: cookie/accesstoken/authorization
|
||||||
|
"token": "your_cookie_here" // Cookie 或 Token 内容
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**网盘认证要求**:
|
||||||
|
| 网盘 | 认证要求 |
|
||||||
|
|------|---------|
|
||||||
|
| 夸克网盘(QK) | 小文件可免登录;超过访客大小限制(23018)需要 Cookie |
|
||||||
|
| UC网盘(UC) | **必须** |
|
||||||
|
| 123网盘(YE) | 下载通常需要本机 `auths.ye.authorization`;公共云解析没有共享账号 |
|
||||||
|
| 小飞机网盘(FJ) | 大文件需要 |
|
||||||
|
| 蓝奏优享(IZ) | 大文件需要 |
|
||||||
|
|
||||||
### 请求示例
|
### 请求示例
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
# 自定义解析器扩展指南
|
# 自定义解析器扩展指南
|
||||||
|
|
||||||
> 最后更新:2026-01-11
|
> 最后更新:2025-10-17
|
||||||
|
|
||||||
## 概述
|
## 概述
|
||||||
|
|
||||||
本模块支持用户自定义解析器扩展。用户在依赖本项目的 Maven 坐标后,可以实现自己的网盘解析器并注册到系统中使用。
|
本模块支持用户自定义解析器扩展。用户在依赖本项目的 Maven 坐标后,可以实现自己的网盘解析器并注册到系统中使用。
|
||||||
|
|
||||||
> **提示**:除了Java自定义解析器,本项目还支持使用脚本语言编写解析器,无需编译即可使用:
|
> **提示**:除了Java自定义解析器,本项目还支持使用JavaScript编写解析器,无需编译即可使用。
|
||||||
> - [JavaScript解析器开发指南](JAVASCRIPT_PARSER_GUIDE.md) - 使用JavaScript编写解析器
|
> 查看 [JavaScript解析器开发指南](JAVASCRIPT_PARSER_GUIDE.md) 了解更多。
|
||||||
> - [Python解析器开发指南](PYTHON_PARSER_GUIDE.md) - 使用Python编写解析器(基于GraalPy)
|
|
||||||
|
|
||||||
## 核心组件
|
## 核心组件
|
||||||
|
|
||||||
@@ -29,7 +28,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cn.qaiu</groupId>
|
<groupId>cn.qaiu</groupId>
|
||||||
<artifactId>parser</artifactId>
|
<artifactId>parser</artifactId>
|
||||||
<version>10.1.17</version>
|
<version>10.2.5</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cn.qaiu</groupId>
|
<groupId>cn.qaiu</groupId>
|
||||||
<artifactId>parser</artifactId>
|
<artifactId>parser</artifactId>
|
||||||
<version>10.1.17</version>
|
<version>10.2.5</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -4,19 +4,6 @@
|
|||||||
|
|
||||||
本指南介绍如何使用JavaScript编写自定义网盘解析器,支持通过JavaScript代码实现网盘解析逻辑,无需编写Java代码。
|
本指南介绍如何使用JavaScript编写自定义网盘解析器,支持通过JavaScript代码实现网盘解析逻辑,无需编写Java代码。
|
||||||
|
|
||||||
### 技术规格
|
|
||||||
|
|
||||||
- **JavaScript 引擎**: Nashorn (JDK 8-14 内置)
|
|
||||||
- **ECMAScript 版本**: ES5.1 (ECMA-262 5.1 Edition)
|
|
||||||
- **语法支持**: ES5 标准语法,不支持 ES6+ 特性(如箭头函数、async/await、模板字符串等)
|
|
||||||
- **运行模式**: 同步执行,所有操作都是阻塞式的
|
|
||||||
|
|
||||||
### 参考文档
|
|
||||||
|
|
||||||
- **ECMAScript 5.1 规范**: https://262.ecma-international.org/5.1/
|
|
||||||
- **MDN JavaScript 文档**: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript
|
|
||||||
- **Nashorn 用户指南**: https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/
|
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [快速开始](#快速开始)
|
- [快速开始](#快速开始)
|
||||||
@@ -253,13 +240,13 @@ var encoded = JsHttpClient.urlEncode("hello world"); // "hello%20world"
|
|||||||
var decoded = JsHttpClient.urlDecode("hello%20world"); // "hello world"
|
var decoded = JsHttpClient.urlDecode("hello%20world"); // "hello world"
|
||||||
|
|
||||||
// 发送简单表单数据
|
// 发送简单表单数据
|
||||||
var formResponse = http.sendForm({
|
var formResponse = http.sendForm("https://api.example.com/login", {
|
||||||
username: "user",
|
username: "user",
|
||||||
password: "pass"
|
password: "pass"
|
||||||
});
|
});
|
||||||
|
|
||||||
// 发送JSON数据
|
// 发送JSON数据
|
||||||
var jsonResponse = http.sendJson({
|
var jsonResponse = http.sendJson("https://api.example.com/submit", {
|
||||||
name: "test",
|
name: "test",
|
||||||
value: 123
|
value: 123
|
||||||
});
|
});
|
||||||
@@ -650,7 +637,7 @@ A: 使用 `shareLinkInfo.getSharePassword()` 方法。
|
|||||||
|
|
||||||
### Q: 如何处理需要登录的网盘?
|
### Q: 如何处理需要登录的网盘?
|
||||||
|
|
||||||
A: 使用 `http.putHeader()` 设置认证头,或使用 `http.sendForm()` 发送登录表单。
|
A: 使用 `http.putHeader()` 设置认证头,或使用 `http.sendForm(url, data)` 发送登录表单。
|
||||||
|
|
||||||
### Q: 如何解析复杂的HTML?
|
### Q: 如何解析复杂的HTML?
|
||||||
|
|
||||||
@@ -724,17 +711,9 @@ var response = http.get("https://api.example.com/data");
|
|||||||
|
|
||||||
## 相关文档
|
## 相关文档
|
||||||
|
|
||||||
### 项目文档
|
|
||||||
- [自定义解析器扩展指南](CUSTOM_PARSER_GUIDE.md) - Java自定义解析器扩展
|
- [自定义解析器扩展指南](CUSTOM_PARSER_GUIDE.md) - Java自定义解析器扩展
|
||||||
- [自定义解析器快速开始](CUSTOM_PARSER_QUICKSTART.md) - 快速上手指南
|
- [自定义解析器快速开始](CUSTOM_PARSER_QUICKSTART.md) - 快速上手指南
|
||||||
- [解析器开发文档](README.md) - 解析器开发约定和规范
|
- [解析器开发文档](README.md) - 解析器开发约定和规范
|
||||||
- [Python解析器开发指南](PYTHON_PARSER_GUIDE.md) - Python 版本解析器指南
|
|
||||||
|
|
||||||
### 外部资源
|
|
||||||
- **ECMAScript 5.1 规范**: https://262.ecma-international.org/5.1/
|
|
||||||
- **MDN JavaScript 参考**: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference
|
|
||||||
- **MDN JavaScript 指南**: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide
|
|
||||||
- **Nashorn 文档**: https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/
|
|
||||||
|
|
||||||
## 更新日志
|
## 更新日志
|
||||||
|
|
||||||
|
|||||||
@@ -1,215 +0,0 @@
|
|||||||
# Python Playground pylsp WebSocket 集成指南
|
|
||||||
|
|
||||||
## 概述
|
|
||||||
|
|
||||||
本文档说明了如何将 jedi 的 pylsp (python-lsp-server) 通过 WebSocket 集成到 Python Playground 中,实现实时代码检查、自动完成和悬停提示等功能。
|
|
||||||
|
|
||||||
## 架构
|
|
||||||
|
|
||||||
```
|
|
||||||
┌─────────────────────────────────────────────────────────────┐
|
|
||||||
│ 前端 (Vue + Monaco) │
|
|
||||||
│ ┌─────────────────────────────────────────────────────────┐│
|
|
||||||
│ │ PylspClient.js ││
|
|
||||||
│ │ - 通过 WebSocket 发送 LSP JSON-RPC 消息 ││
|
|
||||||
│ │ - 接收诊断信息并转换为 Monaco markers ││
|
|
||||||
│ └─────────────────────────────────────────────────────────┘│
|
|
||||||
└──────────────────────────┬──────────────────────────────────┘
|
|
||||||
│ WebSocket (SockJS)
|
|
||||||
▼
|
|
||||||
┌─────────────────────────────────────────────────────────────┐
|
|
||||||
│ 后端 (Vert.x + SockJS) │
|
|
||||||
│ ┌─────────────────────────────────────────────────────────┐│
|
|
||||||
│ │ PylspWebSocketHandler.java ││
|
|
||||||
│ │ - @SockRouteMapper("/pylsp/") ││
|
|
||||||
│ │ - 管理 pylsp 子进程 ││
|
|
||||||
│ │ - 转发 LSP 消息 ││
|
|
||||||
│ └─────────────────────────────────────────────────────────┘│
|
|
||||||
└──────────────────────────┬──────────────────────────────────┘
|
|
||||||
│ stdio (LSP协议)
|
|
||||||
▼
|
|
||||||
┌─────────────────────────────────────────────────────────────┐
|
|
||||||
│ pylsp (python-lsp-server) │
|
|
||||||
│ - jedi: 代码补全、定义跳转 │
|
|
||||||
│ - pyflakes: 语法错误检查 │
|
|
||||||
│ - pycodestyle: PEP8 风格检查 │
|
|
||||||
│ - mccabe: 复杂度检查 │
|
|
||||||
└─────────────────────────────────────────────────────────────┘
|
|
||||||
```
|
|
||||||
|
|
||||||
## 文件清单
|
|
||||||
|
|
||||||
### 后端 (Java)
|
|
||||||
|
|
||||||
1. **PylspWebSocketHandler.java**
|
|
||||||
- 路径: `web-service/src/main/java/cn/qaiu/lz/web/controller/PylspWebSocketHandler.java`
|
|
||||||
- 功能: WebSocket 端点,桥接前端与 pylsp 子进程
|
|
||||||
- 端点: `/ws/pylsp/*`
|
|
||||||
|
|
||||||
### 前端 (JavaScript/Vue)
|
|
||||||
|
|
||||||
1. **pylspClient.js**
|
|
||||||
- 路径: `web-front/src/utils/pylspClient.js`
|
|
||||||
- 功能: LSP WebSocket 客户端,封装 LSP 协议
|
|
||||||
|
|
||||||
### 测试
|
|
||||||
|
|
||||||
1. **RequestsIntegrationTest.java**
|
|
||||||
- 路径: `web-service/src/test/java/cn/qaiu/lz/web/playground/RequestsIntegrationTest.java`
|
|
||||||
- 功能: requests 库集成测试
|
|
||||||
|
|
||||||
2. **test_playground_api.py**
|
|
||||||
- 路径: `web-service/src/test/python/test_playground_api.py`
|
|
||||||
- 功能: API 接口的 pytest 测试脚本
|
|
||||||
|
|
||||||
## 使用方法
|
|
||||||
|
|
||||||
### 1. 安装 pylsp
|
|
||||||
|
|
||||||
```bash
|
|
||||||
pip install python-lsp-server[all]
|
|
||||||
```
|
|
||||||
|
|
||||||
或者只安装核心功能:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
pip install python-lsp-server jedi
|
|
||||||
```
|
|
||||||
|
|
||||||
### 2. 前端集成示例
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
import PylspClient from '@/utils/pylspClient';
|
|
||||||
|
|
||||||
// 创建客户端
|
|
||||||
const pylsp = new PylspClient({
|
|
||||||
onDiagnostics: (uri, markers) => {
|
|
||||||
// 设置 Monaco Editor markers
|
|
||||||
monaco.editor.setModelMarkers(model, 'pylsp', markers);
|
|
||||||
},
|
|
||||||
onConnected: () => {
|
|
||||||
console.log('pylsp 已连接');
|
|
||||||
},
|
|
||||||
onError: (error) => {
|
|
||||||
console.error('pylsp 错误:', error);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// 连接
|
|
||||||
await pylsp.connect();
|
|
||||||
|
|
||||||
// 打开文档
|
|
||||||
pylsp.openDocument(pythonCode);
|
|
||||||
|
|
||||||
// 更新文档(当代码改变时)
|
|
||||||
pylsp.updateDocument(newCode);
|
|
||||||
|
|
||||||
// 获取补全
|
|
||||||
const completions = await pylsp.getCompletions(line, column);
|
|
||||||
|
|
||||||
// 获取悬停信息
|
|
||||||
const hover = await pylsp.getHover(line, column);
|
|
||||||
|
|
||||||
// 断开连接
|
|
||||||
pylsp.disconnect();
|
|
||||||
```
|
|
||||||
|
|
||||||
### 3. 与 Monaco Editor 集成
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
// 监听代码变化
|
|
||||||
editor.onDidChangeModelContent((e) => {
|
|
||||||
const content = editor.getValue();
|
|
||||||
pylsp.updateDocument(content);
|
|
||||||
});
|
|
||||||
|
|
||||||
// 注册补全提供者
|
|
||||||
monaco.languages.registerCompletionItemProvider('python', {
|
|
||||||
provideCompletionItems: async (model, position) => {
|
|
||||||
const items = await pylsp.getCompletions(
|
|
||||||
position.lineNumber - 1,
|
|
||||||
position.column - 1
|
|
||||||
);
|
|
||||||
return { suggestions: items.map(convertToMonacoItem) };
|
|
||||||
}
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
## 已知限制
|
|
||||||
|
|
||||||
### GraalPy requests 库限制
|
|
||||||
|
|
||||||
由于 GraalPy 的 `unicodedata/LLVM` 限制,`requests` 库在后续创建的 Context 中无法正常导入(会抛出 `PolyglotException: null`)。
|
|
||||||
|
|
||||||
**错误链**:
|
|
||||||
```
|
|
||||||
requests → encodings.idna → stringprep → from unicodedata import ucd_3_2_0
|
|
||||||
```
|
|
||||||
|
|
||||||
**解决方案**:
|
|
||||||
1. 在代码顶层导入 requests(不要在函数内部导入)
|
|
||||||
2. 使用标准库的 `urllib.request` 作为替代
|
|
||||||
3. 首次执行时预热 requests 导入
|
|
||||||
|
|
||||||
### 测试注意事项
|
|
||||||
|
|
||||||
1. PyPlaygroundFullTest 中的测试2和测试5被标记为跳过(已知限制)
|
|
||||||
2. 测试13(前端模板代码)使用不依赖 requests 的版本
|
|
||||||
3. requests 功能在实际运行时通过首个 Context 可以正常使用
|
|
||||||
|
|
||||||
## 测试命令
|
|
||||||
|
|
||||||
### 运行 Java 单元测试
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# PyPlaygroundFullTest (13 个测试)
|
|
||||||
cd parser && mvn exec:java \
|
|
||||||
-Dexec.mainClass="cn.qaiu.parser.custompy.PyPlaygroundFullTest" \
|
|
||||||
-Dexec.classpathScope=test -q
|
|
||||||
|
|
||||||
# RequestsIntegrationTest
|
|
||||||
cd web-service && mvn exec:java \
|
|
||||||
-Dexec.mainClass="cn.qaiu.lz.web.playground.RequestsIntegrationTest" \
|
|
||||||
-Dexec.classpathScope=test -q
|
|
||||||
```
|
|
||||||
|
|
||||||
### 运行 Python API 测试
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# 需要后端服务运行
|
|
||||||
cd web-service/src/test/python
|
|
||||||
pip install pytest requests
|
|
||||||
pytest test_playground_api.py -v
|
|
||||||
```
|
|
||||||
|
|
||||||
## 配置
|
|
||||||
|
|
||||||
### 后端配置
|
|
||||||
|
|
||||||
`PylspWebSocketHandler.java` 中可以配置:
|
|
||||||
- pylsp 启动命令
|
|
||||||
- 心跳间隔
|
|
||||||
- 进程超时
|
|
||||||
|
|
||||||
### 前端配置
|
|
||||||
|
|
||||||
`pylspClient.js` 中可以配置:
|
|
||||||
- WebSocket URL
|
|
||||||
- 重连次数
|
|
||||||
- 重连延迟
|
|
||||||
- 请求超时
|
|
||||||
|
|
||||||
## 安全考虑
|
|
||||||
|
|
||||||
1. pylsp 进程在沙箱环境中运行
|
|
||||||
2. 每个 WebSocket 连接对应一个独立的 pylsp 进程
|
|
||||||
3. 连接关闭时自动清理进程
|
|
||||||
4. Playground 访问需要认证(如果配置了密码)
|
|
||||||
|
|
||||||
## 未来改进
|
|
||||||
|
|
||||||
1. 支持多文件项目分析
|
|
||||||
2. 添加 pyright 类型检查
|
|
||||||
3. 支持代码格式化(black/autopep8)
|
|
||||||
4. 添加重构功能
|
|
||||||
5. 支持虚拟环境选择
|
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -1,147 +0,0 @@
|
|||||||
# Python Playground 测试报告
|
|
||||||
|
|
||||||
## 测试概述
|
|
||||||
|
|
||||||
本文档总结了 Python Playground 功能的单元测试和接口测试结果。
|
|
||||||
|
|
||||||
## 测试文件
|
|
||||||
|
|
||||||
| 文件 | 位置 | 说明 |
|
|
||||||
|------|------|------|
|
|
||||||
| `PyPlaygroundFullTest.java` | parser/src/test/java/cn/qaiu/parser/custompy/ | 完整单元测试套件(13个测试) |
|
|
||||||
| `PyCodeSecurityCheckerTest.java` | parser/src/test/java/cn/qaiu/parser/custompy/ | 安全检查器测试(17个测试) |
|
|
||||||
| `PlaygroundApiTest.java` | parser/src/test/java/cn/qaiu/parser/custompy/ | API接口测试(需要后端运行) |
|
|
||||||
|
|
||||||
## 单元测试结果
|
|
||||||
|
|
||||||
### PyPlaygroundFullTest - 13/13 通过 ✅
|
|
||||||
|
|
||||||
| 测试 | 说明 | 结果 |
|
|
||||||
|------|------|------|
|
|
||||||
| 测试1 | 基础 Python 执行(1+2, 字符串操作) | ✅ 通过 |
|
|
||||||
| 测试2 | requests 库导入 | ⚠️ 跳过(已知限制,功能由测试13验证) |
|
|
||||||
| 测试3 | 标准库导入(json, re, base64, hashlib) | ✅ 通过 |
|
|
||||||
| 测试4 | 简单 parse 函数 | ✅ 通过 |
|
|
||||||
| 测试5 | 带 requests 的 parse 函数 | ⚠️ 跳过(已知限制,功能由测试13验证) |
|
|
||||||
| 测试6 | 带 share_link_info 的 parse 函数 | ✅ 通过 |
|
|
||||||
| 测试7 | PyPlaygroundExecutor 完整流程 | ✅ 通过 |
|
|
||||||
| 测试8 | 安全检查 - 拦截 subprocess | ✅ 通过 |
|
|
||||||
| 测试9 | 安全检查 - 拦截 socket | ✅ 通过 |
|
|
||||||
| 测试10 | 安全检查 - 拦截 os.system | ✅ 通过 |
|
|
||||||
| 测试11 | 安全检查 - 拦截 exec/eval | ✅ 通过 |
|
|
||||||
| 测试12 | 安全检查 - 允许安全代码 | ✅ 通过 |
|
|
||||||
| 测试13 | 前端模板代码执行(含 requests) | ✅ 通过 |
|
|
||||||
|
|
||||||
### PyCodeSecurityCheckerTest - 17/17 通过 ✅
|
|
||||||
|
|
||||||
所有安全检查器测试通过,验证了以下功能:
|
|
||||||
- 危险模块拦截:subprocess, socket, ctypes, multiprocessing
|
|
||||||
- 危险 os 方法拦截:system, popen, execv, fork, spawn, kill
|
|
||||||
- 危险内置函数拦截:exec, eval, compile, __import__
|
|
||||||
- 危险文件操作拦截:open with write mode
|
|
||||||
- 安全代码正确放行
|
|
||||||
|
|
||||||
## 已知限制
|
|
||||||
|
|
||||||
### GraalPy unicodedata/LLVM 限制
|
|
||||||
|
|
||||||
由于 GraalPy 的限制,`requests` 库只能在**第一个**创建的 Context 中成功导入。后续创建的 Context 导入 `requests` 会触发以下错误:
|
|
||||||
|
|
||||||
```
|
|
||||||
SystemError: GraalPy option 'NativeModules' is set to false, but the 'llvm' language,
|
|
||||||
which is required for this feature, is not available.
|
|
||||||
```
|
|
||||||
|
|
||||||
**原因**:`requests` 依赖的 `encodings.idna` 模块会导入 `unicodedata`,而该模块需要 LLVM 支持。
|
|
||||||
|
|
||||||
**影响**:
|
|
||||||
- 在单元测试中,多个测试用例无法同时测试 `requests` 导入
|
|
||||||
- 在实际运行中,只要使用 Context 池并确保 `requests` 在代码顶层导入,功能正常
|
|
||||||
|
|
||||||
**解决方案**:
|
|
||||||
- 确保 `import requests` 放在 Python 代码的顶层,而不是函数内部
|
|
||||||
- 前端模板已正确配置,实际使用不受影响
|
|
||||||
|
|
||||||
## 运行测试
|
|
||||||
|
|
||||||
### 运行单元测试
|
|
||||||
|
|
||||||
```bash
|
|
||||||
cd parser
|
|
||||||
mvn test-compile -q && mvn exec:java \
|
|
||||||
-Dexec.mainClass="cn.qaiu.parser.custompy.PyPlaygroundFullTest" \
|
|
||||||
-Dexec.classpathScope=test -q
|
|
||||||
```
|
|
||||||
|
|
||||||
### 运行安全检查器测试
|
|
||||||
|
|
||||||
```bash
|
|
||||||
cd parser
|
|
||||||
mvn test-compile -q && mvn exec:java \
|
|
||||||
-Dexec.mainClass="cn.qaiu.parser.custompy.PyCodeSecurityCheckerTest" \
|
|
||||||
-Dexec.classpathScope=test -q
|
|
||||||
```
|
|
||||||
|
|
||||||
### 运行 API 接口测试
|
|
||||||
|
|
||||||
**注意**:需要先启动后端服务
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# 启动后端服务
|
|
||||||
cd web-service && mvn exec:java -Dexec.mainClass=cn.qaiu.lz.AppMain
|
|
||||||
|
|
||||||
# 在另一个终端运行测试
|
|
||||||
cd parser
|
|
||||||
mvn test-compile -q && mvn exec:java \
|
|
||||||
-Dexec.mainClass="cn.qaiu.parser.custompy.PlaygroundApiTest" \
|
|
||||||
-Dexec.classpathScope=test -q
|
|
||||||
```
|
|
||||||
|
|
||||||
## API 接口测试内容
|
|
||||||
|
|
||||||
`PlaygroundApiTest` 测试以下接口:
|
|
||||||
|
|
||||||
1. **GET /v2/playground/status** - 获取演练场状态
|
|
||||||
2. **POST /v2/playground/test (JavaScript)** - JavaScript 代码执行
|
|
||||||
3. **POST /v2/playground/test (Python)** - Python 代码执行
|
|
||||||
4. **POST /v2/playground/test (安全检查)** - 验证危险代码被拦截
|
|
||||||
5. **POST /v2/playground/test (参数验证)** - 验证缺少参数时的错误处理
|
|
||||||
|
|
||||||
## 测试覆盖的核心组件
|
|
||||||
|
|
||||||
| 组件 | 说明 | 测试覆盖 |
|
|
||||||
|------|------|----------|
|
|
||||||
| `PyContextPool` | GraalPy Context 池管理 | ✅ 间接覆盖 |
|
|
||||||
| `PyPlaygroundExecutor` | Python 代码执行器 | ✅ 直接测试 |
|
|
||||||
| `PyCodeSecurityChecker` | 代码安全检查器 | ✅ 17个测试 |
|
|
||||||
| `PyPlaygroundLogger` | 日志记录器 | ✅ 间接覆盖 |
|
|
||||||
| `PyShareLinkInfoWrapper` | ShareLinkInfo 包装器 | ✅ 直接测试 |
|
|
||||||
| `PyHttpClient` | HTTP 客户端封装 | ⚠️ 部分覆盖 |
|
|
||||||
| `PyCryptoUtils` | 加密工具类 | ❌ 未直接测试 |
|
|
||||||
|
|
||||||
## 前端模板代码验证
|
|
||||||
|
|
||||||
测试13验证了前端 Python 模板代码的完整执行流程:
|
|
||||||
|
|
||||||
```python
|
|
||||||
import requests
|
|
||||||
import re
|
|
||||||
import json
|
|
||||||
|
|
||||||
def parse(share_link_info, http, logger):
|
|
||||||
share_url = share_link_info.get_share_url()
|
|
||||||
logger.info(f"开始解析: {share_url}")
|
|
||||||
# ... 解析逻辑
|
|
||||||
return "https://download.example.com/test.zip"
|
|
||||||
```
|
|
||||||
|
|
||||||
验证内容:
|
|
||||||
- ✅ `requests` 库导入
|
|
||||||
- ✅ `share_link_info.get_share_url()` 调用
|
|
||||||
- ✅ `logger.info()` 日志记录
|
|
||||||
- ✅ f-string 格式化
|
|
||||||
- ✅ 函数返回值处理
|
|
||||||
|
|
||||||
## 结论
|
|
||||||
|
|
||||||
Python Playground 功能已通过全面测试,核心功能正常工作。唯一的限制是 GraalPy 的 unicodedata/LLVM 问题,但在实际使用中不影响功能。建议在正式部署前进行完整的集成测试。
|
|
||||||
@@ -68,7 +68,7 @@ List<FileInfo> files = tool.parseFileListSync();
|
|||||||
```
|
```
|
||||||
|
|
||||||
要点:
|
要点:
|
||||||
- 必须先 WebClientVertxInit.init(Vertx);若未显式初始化,内部将懒加载 Vertx.vertx(),建议显式注入以统一生命周期。
|
- 必须先 WebClientVertxInit.init(Vertx);未初始化时会直接报错,避免解析器偷偷创建第二个 Vert.x 实例。
|
||||||
- 支持三种同步方法:
|
- 支持三种同步方法:
|
||||||
- `parseSync()`: 解析单个文件下载链接
|
- `parseSync()`: 解析单个文件下载链接
|
||||||
- `parseFileListSync()`: 解析文件列表
|
- `parseFileListSync()`: 解析文件列表
|
||||||
|
|||||||
@@ -261,39 +261,11 @@ var methods = systemClass.getDeclaredMethods();
|
|||||||
|
|
||||||
Nashorn引擎默认允许JavaScript访问所有Java类,这是一个严重的安全隐患。以下是建议的安全措施:
|
Nashorn引擎默认允许JavaScript访问所有Java类,这是一个严重的安全隐患。以下是建议的安全措施:
|
||||||
|
|
||||||
### 1. 使用ClassFilter限制类访问 🔒 必须
|
### 1. 演练场与已注册解析器的类访问限制
|
||||||
|
|
||||||
```java
|
两个入口都通过 `ParserScriptEngine.create()` 创建 Nashorn 引擎。引擎使用 `--no-java`,类过滤器拒绝按名称解析 Java 类。引擎在执行解析器代码前禁用包入口、脚本加载函数和脚本上下文全局变量。解析器仍可使用项目显式注入的分享信息、HTTP、Fetch 和日志接口。
|
||||||
import jdk.nashorn.api.scripting.ClassFilter;
|
|
||||||
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
|
|
||||||
|
|
||||||
public class SecurityClassFilter implements ClassFilter {
|
`ParserScriptEngineTest` 检查受限全局变量的属性,并通过两个执行入口验证普通解析器的行为。
|
||||||
@Override
|
|
||||||
public boolean exposeToScripts(String className) {
|
|
||||||
// 黑名单:禁止访问危险类
|
|
||||||
if (className.startsWith("java.lang.Runtime") ||
|
|
||||||
className.startsWith("java.lang.ProcessBuilder") ||
|
|
||||||
className.startsWith("java.io.File") ||
|
|
||||||
className.startsWith("java.nio.file") ||
|
|
||||||
className.startsWith("java.lang.System") ||
|
|
||||||
className.startsWith("java.lang.Class") ||
|
|
||||||
className.startsWith("java.lang.reflect") ||
|
|
||||||
className.startsWith("java.net.Socket") ||
|
|
||||||
className.startsWith("java.net.URL")) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 白名单:只允许特定的类
|
|
||||||
// return className.startsWith("允许的包名");
|
|
||||||
|
|
||||||
return false; // 默认拒绝所有
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 使用ClassFilter创建引擎
|
|
||||||
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
|
|
||||||
ScriptEngine engine = factory.getScriptEngine(new SecurityClassFilter());
|
|
||||||
```
|
|
||||||
|
|
||||||
### 2. 设置执行超时 ⏱️ 强烈推荐
|
### 2. 设置执行超时 ⏱️ 强烈推荐
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,345 @@
|
|||||||
|
# 认证参数传递指南 (Auth Parameter Guide)
|
||||||
|
|
||||||
|
## 概述
|
||||||
|
|
||||||
|
本文档描述了网盘解析接口中携带认证参数的方法。通过 `auth` 参数,可以在解析请求时传递临时认证信息(如 Cookie、Token、用户名密码等),使解析器能够访问需要登录或授权的网盘资源。
|
||||||
|
|
||||||
|
## 网盘认证要求
|
||||||
|
|
||||||
|
| 网盘 | 类型代码 | 认证要求 | 说明 |
|
||||||
|
|------|---------|---------|------|
|
||||||
|
| 夸克网盘 | QK | **必须** | 必须配置 Cookie 才能解析和下载 |
|
||||||
|
| UC网盘 | UC | **必须** | 必须配置 Cookie 才能解析和下载 |
|
||||||
|
| 小飞机网盘 | FJ | 可选 | 大文件(>100MB)需要配置认证信息 |
|
||||||
|
| 蓝奏优享 | IZ | 可选 | 大文件需要配置认证信息 |
|
||||||
|
| 其他网盘 | - | 不需要 | 无需认证即可解析 |
|
||||||
|
|
||||||
|
> 💡 **如何获取 Cookie**: 在浏览器中登录对应网盘,打开开发者工具(F12),切换到 Network 标签,刷新页面,在请求头中找到 Cookie 字段并复制完整内容。
|
||||||
|
|
||||||
|
## 认证参数格式
|
||||||
|
|
||||||
|
### 编码流程
|
||||||
|
|
||||||
|
```
|
||||||
|
JSON对象 → AES加密 → Base64编码 → URL编码
|
||||||
|
```
|
||||||
|
|
||||||
|
### 解码流程
|
||||||
|
|
||||||
|
```
|
||||||
|
URL解码 → Base64解码 → AES解密 → JSON对象
|
||||||
|
```
|
||||||
|
|
||||||
|
### 加密配置
|
||||||
|
|
||||||
|
- **加密算法**: AES/ECB/PKCS5Padding
|
||||||
|
- **密钥长度**: 16、24 或 32 个 UTF-8 字节
|
||||||
|
- **密钥**: 没有公共默认值。需要对接的可信客户端应使用自建服务显式配置的 `server.authEncryptKey`。
|
||||||
|
|
||||||
|
### 密钥作用说明(重要)
|
||||||
|
|
||||||
|
当前系统中涉及两类不同用途的密钥:
|
||||||
|
|
||||||
|
1. `server.authEncryptKey`
|
||||||
|
- 用途:加解密 `auth` 参数(前端/调用方传入的认证信息)
|
||||||
|
- 影响范围:`/parser`、`/json/parser`、`/v2/linkInfo` 等接口中的 `auth` 参数
|
||||||
|
- 注意:这是 **AES 对称加密密钥**,要求 16、24 或 32 字节
|
||||||
|
|
||||||
|
2. `server.donatedAccountFailureTokenSignKey`
|
||||||
|
- 用途:签名和验签“捐赠账号失败计数 token”(用于防伪造、失败计数)
|
||||||
|
- 影响范围:捐赠账号失败计数与自动失效逻辑
|
||||||
|
- 注意:这是 **HMAC 签名密钥**,与 `authEncryptKey` 已解耦,建议使用高强度随机字符串
|
||||||
|
|
||||||
|
> 建议:生产环境务必同时自定义这两个密钥,且不要设置为相同值。
|
||||||
|
|
||||||
|
## JSON 模型定义
|
||||||
|
|
||||||
|
### AuthParam 对象
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"authType": "string", // 认证类型(必填)
|
||||||
|
"username": "string", // 用户名
|
||||||
|
"password": "string", // 密码
|
||||||
|
"token": "string", // Token/AccessToken/Cookie值
|
||||||
|
"cookie": "string", // Cookie 字符串
|
||||||
|
"auth": "string", // Authorization 头内容
|
||||||
|
"ext1": "string", // 扩展字段1(格式: key:value)
|
||||||
|
"ext2": "string", // 扩展字段2(格式: key:value)
|
||||||
|
"ext3": "string", // 扩展字段3(格式: key:value)
|
||||||
|
"ext4": "string", // 扩展字段4(格式: key:value)
|
||||||
|
"ext5": "string" // 扩展字段5(格式: key:value)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 认证类型 (authType)
|
||||||
|
|
||||||
|
| authType | 说明 | 主要字段 |
|
||||||
|
|----------|------|---------|
|
||||||
|
| `accesstoken` | 使用 AccessToken 认证 | `token` |
|
||||||
|
| `cookie` | 使用 Cookie 认证 | `token` (存放 cookie 值) |
|
||||||
|
| `authorization` | 使用 Authorization 头认证 | `token` |
|
||||||
|
| `password` / `username_password` | 用户名密码认证 | `username`, `password` |
|
||||||
|
| `custom` | 自定义认证(使用扩展字段) | `token`, `ext1`-`ext5` |
|
||||||
|
|
||||||
|
### 示例 JSON
|
||||||
|
|
||||||
|
#### 1. Token 认证
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"authType": "accesstoken",
|
||||||
|
"token": "your_access_token_here"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 2. Cookie 认证
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"authType": "cookie",
|
||||||
|
"token": "session_id=abc123; user_token=xyz789"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 3. 用户名密码认证
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"authType": "password",
|
||||||
|
"username": "your_username",
|
||||||
|
"password": "your_password"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> 123云盘(`ye`)账号密码登录如果返回验证码或风控,解析会直接失败,并提示改用 Authorization,不会尝试验证码。请在浏览器登录后复制 `authorToken` / 请求头 `authorization` / Cookie `sso-token`,用 `authType=authorization` 或 `accesstoken` 传入,或写入 `app-dev.yml` 的 `auths.ye.authorization`。
|
||||||
|
|
||||||
|
#### 4. 自定义认证
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"authType": "custom",
|
||||||
|
"token": "main_token",
|
||||||
|
"ext1": "refresh_token:your_refresh_token",
|
||||||
|
"ext2": "device_id:device123"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 接口调用示例
|
||||||
|
|
||||||
|
### 基础接口
|
||||||
|
|
||||||
|
#### 1. 解析并重定向 (GET /parser)
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /parser?url={分享链接}&pwd={提取码}&auth={加密认证参数}
|
||||||
|
```
|
||||||
|
|
||||||
|
**参数说明:**
|
||||||
|
- `url`: 网盘分享链接(必填)
|
||||||
|
- `pwd`: 提取码(可选)
|
||||||
|
- `auth`: 加密后的认证参数(可选)
|
||||||
|
|
||||||
|
**响应:** 302 重定向到直链
|
||||||
|
|
||||||
|
#### 2. 解析返回 JSON (GET /json/parser)
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /json/parser?url={分享链接}&pwd={提取码}&auth={加密认证参数}
|
||||||
|
```
|
||||||
|
|
||||||
|
**响应示例:**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"shareKey": "lz:xxxx",
|
||||||
|
"directLink": "https://...",
|
||||||
|
"cacheHit": false,
|
||||||
|
"expires": "2026-02-05 12:00:00",
|
||||||
|
"expiration": 1738728000000
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 3. 获取链接信息 (GET /v2/linkInfo)
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /v2/linkInfo?url={分享链接}&pwd={提取码}&auth={加密认证参数}
|
||||||
|
```
|
||||||
|
|
||||||
|
**响应:** 返回下载链接、API 链接、预览链接等信息
|
||||||
|
|
||||||
|
## 各语言加密示例
|
||||||
|
|
||||||
|
### Java
|
||||||
|
|
||||||
|
```java
|
||||||
|
import cn.qaiu.lz.common.util.AuthParamCodec;
|
||||||
|
import cn.qaiu.lz.web.model.AuthParam;
|
||||||
|
|
||||||
|
// 方式1: 使用 AuthParam 对象
|
||||||
|
AuthParam authParam = AuthParam.builder()
|
||||||
|
.authType("accesstoken")
|
||||||
|
.token("your_token_here")
|
||||||
|
.build();
|
||||||
|
String encrypted = AuthParamCodec.encode(authParam);
|
||||||
|
|
||||||
|
// 方式2: 快速编码
|
||||||
|
String encrypted = AuthParamCodec.quickEncode("accesstoken", "your_token_here");
|
||||||
|
|
||||||
|
// 方式3: 用户名密码
|
||||||
|
String encrypted = AuthParamCodec.quickEncodePassword("username", "password");
|
||||||
|
|
||||||
|
// 解码
|
||||||
|
AuthParam decoded = AuthParamCodec.decode(encrypted);
|
||||||
|
```
|
||||||
|
|
||||||
|
### JavaScript (浏览器/Node.js)
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
// 使用 CryptoJS 库
|
||||||
|
const CryptoJS = require('crypto-js');
|
||||||
|
|
||||||
|
const AUTH_KEY = process.env.NFD_AUTH_ENCRYPT_KEY;
|
||||||
|
|
||||||
|
// 加密
|
||||||
|
function encodeAuthParam(authObj) {
|
||||||
|
const jsonStr = JSON.stringify(authObj);
|
||||||
|
const encrypted = CryptoJS.AES.encrypt(jsonStr, CryptoJS.enc.Utf8.parse(AUTH_KEY), {
|
||||||
|
mode: CryptoJS.mode.ECB,
|
||||||
|
padding: CryptoJS.pad.Pkcs7
|
||||||
|
});
|
||||||
|
const base64 = encrypted.toString();
|
||||||
|
return encodeURIComponent(base64);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解密
|
||||||
|
function decodeAuthParam(encryptedAuth) {
|
||||||
|
const base64 = decodeURIComponent(encryptedAuth);
|
||||||
|
const decrypted = CryptoJS.AES.decrypt(base64, CryptoJS.enc.Utf8.parse(AUTH_KEY), {
|
||||||
|
mode: CryptoJS.mode.ECB,
|
||||||
|
padding: CryptoJS.pad.Pkcs7
|
||||||
|
});
|
||||||
|
return JSON.parse(decrypted.toString(CryptoJS.enc.Utf8));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用示例
|
||||||
|
const auth = encodeAuthParam({
|
||||||
|
authType: 'accesstoken',
|
||||||
|
token: 'your_token_here'
|
||||||
|
});
|
||||||
|
const url = `http://127.0.0.1:6400/parser?url=${shareUrl}&auth=${auth}`;
|
||||||
|
```
|
||||||
|
|
||||||
|
### Python
|
||||||
|
|
||||||
|
```python
|
||||||
|
import json
|
||||||
|
import base64
|
||||||
|
from urllib.parse import quote, unquote
|
||||||
|
from Crypto.Cipher import AES
|
||||||
|
from Crypto.Util.Padding import pad, unpad
|
||||||
|
|
||||||
|
import os
|
||||||
|
AUTH_KEY = os.environ['NFD_AUTH_ENCRYPT_KEY'].encode('utf-8')
|
||||||
|
|
||||||
|
def encode_auth_param(auth_obj):
|
||||||
|
"""加密认证参数"""
|
||||||
|
json_str = json.dumps(auth_obj, ensure_ascii=False)
|
||||||
|
cipher = AES.new(AUTH_KEY, AES.MODE_ECB)
|
||||||
|
padded = pad(json_str.encode('utf-8'), AES.block_size)
|
||||||
|
encrypted = cipher.encrypt(padded)
|
||||||
|
base64_str = base64.b64encode(encrypted).decode('utf-8')
|
||||||
|
return quote(base64_str)
|
||||||
|
|
||||||
|
def decode_auth_param(encrypted_auth):
|
||||||
|
"""解密认证参数"""
|
||||||
|
base64_str = unquote(encrypted_auth)
|
||||||
|
encrypted = base64.b64decode(base64_str)
|
||||||
|
cipher = AES.new(AUTH_KEY, AES.MODE_ECB)
|
||||||
|
decrypted = unpad(cipher.decrypt(encrypted), AES.block_size)
|
||||||
|
return json.loads(decrypted.decode('utf-8'))
|
||||||
|
|
||||||
|
# 使用示例
|
||||||
|
auth = encode_auth_param({
|
||||||
|
'authType': 'accesstoken',
|
||||||
|
'token': 'your_token_here'
|
||||||
|
})
|
||||||
|
url = f'http://127.0.0.1:6400/parser?url={share_url}&auth={auth}'
|
||||||
|
```
|
||||||
|
|
||||||
|
### cURL 命令行
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 假设已加密的 auth 参数为 ENCRYPTED_AUTH
|
||||||
|
curl -L "http://127.0.0.1:6400/parser?url=https://www.lanzoux.com/xxxx&auth=ENCRYPTED_AUTH"
|
||||||
|
|
||||||
|
# 获取 JSON 响应
|
||||||
|
curl "http://127.0.0.1:6400/json/parser?url=https://www.lanzoux.com/xxxx&auth=ENCRYPTED_AUTH"
|
||||||
|
```
|
||||||
|
|
||||||
|
## 解析器使用认证信息
|
||||||
|
|
||||||
|
解析器可以从 `shareLinkInfo.otherParam.get("auths")` 获取 MultiMap 格式的认证信息:
|
||||||
|
|
||||||
|
```java
|
||||||
|
// 在解析器中获取认证信息
|
||||||
|
MultiMap auths = (MultiMap) shareLinkInfo.getOtherParam().get("auths");
|
||||||
|
|
||||||
|
if (auths != null) {
|
||||||
|
String authType = auths.get("authType");
|
||||||
|
String token = auths.get("token");
|
||||||
|
String username = auths.get("username");
|
||||||
|
String password = auths.get("password");
|
||||||
|
|
||||||
|
// 根据 authType 使用相应的认证方式
|
||||||
|
switch (authType) {
|
||||||
|
case "accesstoken":
|
||||||
|
// 使用 token 认证
|
||||||
|
break;
|
||||||
|
case "password":
|
||||||
|
// 使用用户名密码登录
|
||||||
|
break;
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 注意事项
|
||||||
|
|
||||||
|
1. **安全性**:
|
||||||
|
- 不要在日志中打印完整的认证参数
|
||||||
|
- 认证参数通过 HTTPS 传输更安全
|
||||||
|
- 密钥应妥善保管,建议在生产环境中更换默认密钥
|
||||||
|
|
||||||
|
2. **缓存策略**:
|
||||||
|
- 带有临时认证参数的请求目前不会被缓存
|
||||||
|
- 每次请求都会重新解析
|
||||||
|
|
||||||
|
3. **兼容性**:
|
||||||
|
- `auth` 参数与原有的 `pwd` 参数可以同时使用
|
||||||
|
- 不提供 `auth` 参数时,使用后台配置的认证信息
|
||||||
|
|
||||||
|
4. **扩展字段**:
|
||||||
|
- `ext1`-`ext5` 使用 `key:value` 格式
|
||||||
|
- 适用于需要传递多个自定义参数的场景
|
||||||
|
|
||||||
|
## 配置说明
|
||||||
|
|
||||||
|
在 `app-dev.yml` 中配置密钥:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
server:
|
||||||
|
# auth参数加密密钥(16、24 或 32 字节)
|
||||||
|
# authEncryptKey: "" # 仅在可信客户端或多实例需共享密钥时显式配置
|
||||||
|
|
||||||
|
# 捐赠账号失败计数token签名密钥(HMAC)
|
||||||
|
# 建议使用较长随机字符串,并与 authEncryptKey 不同
|
||||||
|
donatedAccountFailureTokenSignKey: 'your_random_hmac_sign_key'
|
||||||
|
```
|
||||||
|
|
||||||
|
### 密钥管理建议
|
||||||
|
|
||||||
|
- 不要在公开仓库提交生产密钥
|
||||||
|
- 建议通过环境变量或私有配置注入
|
||||||
|
- 调整 `authEncryptKey` 会影响 `auth` 参数兼容性
|
||||||
|
- 调整 `donatedAccountFailureTokenSignKey` 会使已签发的失败计数 token 失效(短期可接受)
|
||||||
|
|
||||||
|
## 更新日志
|
||||||
|
|
||||||
|
- **2026-02-05**: 初始版本,支持 accesstoken、cookie、password、custom 认证类型
|
||||||
|
- **2026-09-27**: 补充 123云盘账号密码登录触发验证码/风控时改用 Authorization 的说明
|
||||||
@@ -0,0 +1,120 @@
|
|||||||
|
# 认证参数传递指南 (简化版)
|
||||||
|
|
||||||
|
## JSON 对象模型
|
||||||
|
|
||||||
|
### AuthParam 对象
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"authType": "string", // 认证类型(必填)
|
||||||
|
"username": "string", // 用户名
|
||||||
|
"password": "string", // 密码
|
||||||
|
"token": "string", // Token/AccessToken/Cookie值
|
||||||
|
"cookie": "string", // Cookie 字符串
|
||||||
|
"auth": "string", // Authorization 头内容
|
||||||
|
"ext1": "string", // 扩展字段1(格式: key:value)
|
||||||
|
"ext2": "string", // 扩展字段2(格式: key:value)
|
||||||
|
"ext3": "string", // 扩展字段3(格式: key:value)
|
||||||
|
"ext4": "string", // 扩展字段4(格式: key:value)
|
||||||
|
"ext5": "string" // 扩展字段5(格式: key:value)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 认证类型
|
||||||
|
|
||||||
|
| authType | 说明 | 主要字段 |
|
||||||
|
|----------|------|---------|
|
||||||
|
| `accesstoken` | AccessToken 认证 | `token` |
|
||||||
|
| `cookie` | Cookie 认证 | `token` |
|
||||||
|
| `authorization` | Authorization 头认证 | `token` |
|
||||||
|
| `password` | 用户名密码认证 | `username`, `password` |
|
||||||
|
| `custom` | 自定义认证 | `token`, `ext1`-`ext5` |
|
||||||
|
|
||||||
|
## 示例
|
||||||
|
|
||||||
|
### Token 认证
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"authType": "accesstoken",
|
||||||
|
"token": "your_access_token_here"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Cookie 认证
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"authType": "cookie",
|
||||||
|
"token": "session_id=abc123; user_token=xyz789"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 用户名密码
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"authType": "password",
|
||||||
|
"username": "your_username",
|
||||||
|
"password": "your_password"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> 123云盘(`ye`)账号密码登录触发验证码/风控时,请改用 `authType=authorization` 或 `accesstoken`(`token` 填浏览器中的 Bearer token / JWT),或在 `app-dev.yml` 配置 `auths.ye.authorization`。解析不会尝试验证码。
|
||||||
|
|
||||||
|
### 自定义认证
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"authType": "custom",
|
||||||
|
"token": "main_token",
|
||||||
|
"ext1": "refresh_token:your_refresh_token",
|
||||||
|
"ext2": "device_id:device123"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 使用说明
|
||||||
|
|
||||||
|
1. **编码流程**: JSON对象 → AES加密 → Base64编码 → URL编码
|
||||||
|
2. **加密配置**: AES/ECB/PKCS5Padding, 密钥: 自建服务配置值(无公共默认值)
|
||||||
|
3. **接口调用**: `GET /parser?url={分享链接}&pwd={提取码}&auth={加密认证参数}`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 接口调用示例
|
||||||
|
|
||||||
|
### 基础接口
|
||||||
|
|
||||||
|
#### 1. 解析并重定向 (GET /parser)
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /parser?url={分享链接}&pwd={提取码}&auth={加密认证参数}
|
||||||
|
```
|
||||||
|
|
||||||
|
**参数说明:**
|
||||||
|
- `url`: 网盘分享链接(必填)
|
||||||
|
- `pwd`: 提取码(可选)
|
||||||
|
- `auth`: 加密后的认证参数(可选)
|
||||||
|
|
||||||
|
**响应:** 302 重定向到直链
|
||||||
|
|
||||||
|
#### 2. 解析返回 JSON (GET /json/parser)
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /json/parser?url={分享链接}&pwd={提取码}&auth={加密认证参数}
|
||||||
|
```
|
||||||
|
|
||||||
|
**响应示例:**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"shareKey": "lz:xxxx",
|
||||||
|
"directLink": "https://...",
|
||||||
|
"cacheHit": false,
|
||||||
|
"expires": "2026-02-05 12:00:00",
|
||||||
|
"expiration": 1738728000000
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 3. 获取链接信息 (GET /v2/linkInfo)
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /v2/linkInfo?url={分享链接}&pwd={提取码}&auth={加密认证参数}
|
||||||
|
```
|
||||||
|
|
||||||
|
**响应:** 返回下载链接、API 链接、预览链接等信息
|
||||||
@@ -17,7 +17,7 @@
|
|||||||
this.temporaryExecutor = WebClientVertxInit.get().createSharedWorkerExecutor(
|
this.temporaryExecutor = WebClientVertxInit.get().createSharedWorkerExecutor(
|
||||||
"playground-temp-" + System.currentTimeMillis(),
|
"playground-temp-" + System.currentTimeMillis(),
|
||||||
1, // 每个请求只需要1个线程
|
1, // 每个请求只需要1个线程
|
||||||
10000000000L // 设置非常长的超时,避免被vertx强制中断
|
10000000000L // 设置非常长的超时,避免触发Vert.x阻塞线程告警
|
||||||
);
|
);
|
||||||
|
|
||||||
// 执行完成或超时后关闭
|
// 执行完成或超时后关闭
|
||||||
|
|||||||
@@ -106,7 +106,7 @@ executionFuture.toCompletionStage()
|
|||||||
### 长期方案(需大量工作)
|
### 长期方案(需大量工作)
|
||||||
1. **迁移到GraalVM JavaScript引擎**
|
1. **迁移到GraalVM JavaScript引擎**
|
||||||
- 支持CPU时间限制
|
- 支持CPU时间限制
|
||||||
- 可以强制中断
|
- 相比Nashorn更容易实现受控取消
|
||||||
- 更好的性能
|
- 更好的性能
|
||||||
- 但需要额外依赖
|
- 但需要额外依赖
|
||||||
|
|
||||||
|
|||||||
+45
-53
@@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
<groupId>cn.qaiu</groupId>
|
<groupId>cn.qaiu</groupId>
|
||||||
<artifactId>parser</artifactId>
|
<artifactId>parser</artifactId>
|
||||||
<version>10.2.3</version>
|
<version>${parserVersion}</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>cn.qaiu:parser</name>
|
<name>cn.qaiu:parser</name>
|
||||||
@@ -35,9 +35,9 @@
|
|||||||
</developers>
|
</developers>
|
||||||
|
|
||||||
<scm>
|
<scm>
|
||||||
<connection>scm:git:https://github.com/qaiu/netdisk-fast-download.git</connection>
|
<connection>scm:git:https://github.com/${github.owner}/${github.repo}.git</connection>
|
||||||
<developerConnection>scm:git:ssh://[email protected]:qaiu/netdisk-fast-download.git</developerConnection>
|
<developerConnection>scm:git:ssh://[email protected]:${github.owner}/${github.repo}.git</developerConnection>
|
||||||
<url>https://github.com/qaiu/netdisk-fast-download</url>
|
<url>https://github.com/${github.owner}/${github.repo}</url>
|
||||||
</scm>
|
</scm>
|
||||||
|
|
||||||
<distributionManagement>
|
<distributionManagement>
|
||||||
@@ -52,23 +52,20 @@
|
|||||||
</distributionManagement>
|
</distributionManagement>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<revision>0.1.8</revision>
|
|
||||||
<java.version>17</java.version>
|
<java.version>17</java.version>
|
||||||
<maven.compiler.source>17</maven.compiler.source>
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
<maven.compiler.target>17</maven.compiler.target>
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
|
||||||
<!-- Versions -->
|
<!-- Versions -->
|
||||||
<vertx.version>4.5.23</vertx.version>
|
<vertx.version>4.5.27</vertx.version>
|
||||||
<org.reflections.version>0.10.2</org.reflections.version>
|
<org.reflections.version>0.10.2</org.reflections.version>
|
||||||
<lombok.version>1.18.38</lombok.version>
|
<lombok.version>1.18.38</lombok.version>
|
||||||
<slf4j.version>2.0.5</slf4j.version>
|
<slf4j.version>2.0.16</slf4j.version>
|
||||||
<commons-lang3.version>3.18.0</commons-lang3.version>
|
<commons-lang3.version>3.18.0</commons-lang3.version>
|
||||||
<jackson.version>2.14.2</jackson.version>
|
<jackson.version>2.18.9</jackson.version>
|
||||||
<logback.version>1.5.19</logback.version>
|
<logback.version>1.5.32</logback.version>
|
||||||
<junit.version>4.13.2</junit.version>
|
<junit.version>4.13.2</junit.version>
|
||||||
<!-- GraalPy -->
|
|
||||||
<graalpy.version>24.1.1</graalpy.version>
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@@ -107,32 +104,6 @@
|
|||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- GraalPy Python Runtime -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.graalvm.polyglot</groupId>
|
|
||||||
<artifactId>polyglot</artifactId>
|
|
||||||
<version>${graalpy.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.graalvm.polyglot</groupId>
|
|
||||||
<artifactId>python</artifactId>
|
|
||||||
<version>${graalpy.version}</version>
|
|
||||||
<type>pom</type>
|
|
||||||
</dependency>
|
|
||||||
<!-- GraalPy Python 包资源支持 -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.graalvm.python</groupId>
|
|
||||||
<artifactId>python-embedding</artifactId>
|
|
||||||
<version>${graalpy.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<!-- GraalPy LLVM 支持 - 允许多 Context 使用原生模块 (如 unicodedata) -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.graalvm.polyglot</groupId>
|
|
||||||
<artifactId>llvm-community</artifactId>
|
|
||||||
<version>${graalpy.version}</version>
|
|
||||||
<type>pom</type>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- Compression (Brotli) -->
|
<!-- Compression (Brotli) -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.brotli</groupId>
|
<groupId>org.brotli</groupId>
|
||||||
@@ -152,24 +123,37 @@
|
|||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
|
||||||
<!-- GraalPy Maven Plugin - 仅创建 Python Home,不使用 pip 安装 -->
|
<!-- 从 git remote origin 自动识别 GitHub 仓库地址 -->
|
||||||
<!-- pip 包手动安装到 src/main/resources/graalpy-packages/,可打包进 jar -->
|
|
||||||
<!-- 安装方法: ./setup-graalpy-packages.sh -->
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.graalvm.python</groupId>
|
<groupId>org.codehaus.gmavenplus</groupId>
|
||||||
<artifactId>graalpy-maven-plugin</artifactId>
|
<artifactId>gmavenplus-plugin</artifactId>
|
||||||
<version>${graalpy.version}</version>
|
<version>4.1.1</version>
|
||||||
<configuration>
|
<dependencies>
|
||||||
<!-- 不声明 packages,避免代理问题 -->
|
<dependency>
|
||||||
<!-- pip 包从 resources/graalpy-packages 加载 -->
|
<groupId>org.apache.groovy</groupId>
|
||||||
</configuration>
|
<artifactId>groovy</artifactId>
|
||||||
|
<version>4.0.24</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<id>prepare-python-resources</id>
|
<phase>initialize</phase>
|
||||||
<phase>generate-resources</phase>
|
<goals><goal>execute</goal></goals>
|
||||||
<goals>
|
<configuration>
|
||||||
<goal>process-graalpy-resources</goal>
|
<scripts>
|
||||||
</goals>
|
<script>
|
||||||
|
def url = 'git remote get-url origin'.execute().text.trim()
|
||||||
|
def m = (url =~ 'github\\.com[:/]([^/]+)/([^/.]+?)(?:\\.git)?$')
|
||||||
|
if (m.find()) {
|
||||||
|
project.properties.setProperty('github.owner', m.group(1))
|
||||||
|
project.properties.setProperty('github.repo', m.group(2))
|
||||||
|
} else {
|
||||||
|
project.properties.setProperty('github.owner', 'qaiu')
|
||||||
|
project.properties.setProperty('github.repo', 'netdisk-fast-download')
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</scripts>
|
||||||
|
</configuration>
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
@@ -181,6 +165,10 @@
|
|||||||
<version>3.13.0</version>
|
<version>3.13.0</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<release>${maven.compiler.source}</release>
|
<release>${maven.compiler.source}</release>
|
||||||
|
<compilerArgs>
|
||||||
|
<arg>--add-modules</arg>
|
||||||
|
<arg>jdk.incubator.vector</arg>
|
||||||
|
</compilerArgs>
|
||||||
<encoding>${project.build.sourceEncoding}</encoding>
|
<encoding>${project.build.sourceEncoding}</encoding>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
@@ -216,7 +204,11 @@
|
|||||||
<!-- 忽略 Javadoc 错误 -->
|
<!-- 忽略 Javadoc 错误 -->
|
||||||
<failOnError>false</failOnError>
|
<failOnError>false</failOnError>
|
||||||
<!-- 禁用 doclint(新版参数名改为 additionalOptions) -->
|
<!-- 禁用 doclint(新版参数名改为 additionalOptions) -->
|
||||||
<additionalOptions>-Xdoclint:none</additionalOptions>
|
<additionalOptions>
|
||||||
|
<additionalOption>-Xdoclint:none</additionalOption>
|
||||||
|
<additionalOption>--add-modules</additionalOption>
|
||||||
|
<additionalOption>jdk.incubator.vector</additionalOption>
|
||||||
|
</additionalOptions>
|
||||||
<!-- 如果项目源码中几乎没有 Javadoc,可设 true -->
|
<!-- 如果项目源码中几乎没有 Javadoc,可设 true -->
|
||||||
<quiet>true</quiet>
|
<quiet>true</quiet>
|
||||||
</configuration>
|
</configuration>
|
||||||
|
|||||||
@@ -1,127 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
# GraalPy pip 包安装脚本
|
|
||||||
# 将 pip 包安装到 src/main/resources/graalpy-packages/,可打包进 jar
|
|
||||||
# 不受 mvn clean 影响
|
|
||||||
#
|
|
||||||
# requests 是纯 Python 包,可以用系统 pip 安装
|
|
||||||
# GraalPy 运行时可以正常加载这些包
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
||||||
PARSER_DIR="$SCRIPT_DIR"
|
|
||||||
PACKAGES_DIR="$PARSER_DIR/src/main/resources/graalpy-packages"
|
|
||||||
|
|
||||||
echo "=== GraalPy pip 包安装脚本 ==="
|
|
||||||
echo ""
|
|
||||||
echo "目标目录: $PACKAGES_DIR"
|
|
||||||
echo ""
|
|
||||||
|
|
||||||
# 确保目标目录存在
|
|
||||||
mkdir -p "$PACKAGES_DIR"
|
|
||||||
|
|
||||||
# 定义要安装的包列表
|
|
||||||
# 1. requests 及其依赖 - HTTP 客户端
|
|
||||||
# 2. python-lsp-server 及其依赖 - Python LSP 服务器(用于代码智能提示)
|
|
||||||
PACKAGES=(
|
|
||||||
# requests 依赖
|
|
||||||
"requests"
|
|
||||||
"urllib3"
|
|
||||||
"charset_normalizer"
|
|
||||||
"idna"
|
|
||||||
"certifi"
|
|
||||||
|
|
||||||
# python-lsp-server (pylsp) 核心
|
|
||||||
"python-lsp-server"
|
|
||||||
"jedi"
|
|
||||||
"python-lsp-jsonrpc"
|
|
||||||
"pluggy"
|
|
||||||
|
|
||||||
# pylsp 可选功能
|
|
||||||
"pyflakes" # 代码检查
|
|
||||||
"pycodestyle" # PEP8 风格检查
|
|
||||||
"autopep8" # 自动格式化
|
|
||||||
"rope" # 重构支持
|
|
||||||
"yapf" # 代码格式化
|
|
||||||
)
|
|
||||||
|
|
||||||
echo "将安装以下包到 $PACKAGES_DIR :"
|
|
||||||
printf '%s\n' "${PACKAGES[@]}"
|
|
||||||
echo ""
|
|
||||||
|
|
||||||
# 使用系统 pip 安装包(纯 Python 包)
|
|
||||||
echo "开始安装..."
|
|
||||||
|
|
||||||
# 尝试不同的 pip 命令
|
|
||||||
if command -v pip3 &> /dev/null; then
|
|
||||||
PIP_CMD="pip3"
|
|
||||||
elif command -v pip &> /dev/null; then
|
|
||||||
PIP_CMD="pip"
|
|
||||||
elif command -v python3 &> /dev/null; then
|
|
||||||
PIP_CMD="python3 -m pip"
|
|
||||||
elif command -v python &> /dev/null; then
|
|
||||||
PIP_CMD="python -m pip"
|
|
||||||
else
|
|
||||||
echo "✗ 未找到 pip,请先安装 Python 和 pip"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "使用 pip 命令: $PIP_CMD"
|
|
||||||
echo ""
|
|
||||||
|
|
||||||
# 安装所有包
|
|
||||||
$PIP_CMD install --target="$PACKAGES_DIR" --upgrade "${PACKAGES[@]}" 2>&1
|
|
||||||
|
|
||||||
# 验证安装
|
|
||||||
echo ""
|
|
||||||
echo "验证安装..."
|
|
||||||
FAILED=0
|
|
||||||
|
|
||||||
if [ -d "$PACKAGES_DIR/requests" ]; then
|
|
||||||
echo "✓ requests 安装成功"
|
|
||||||
else
|
|
||||||
echo "✗ requests 安装失败"
|
|
||||||
FAILED=1
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -d "$PACKAGES_DIR/pylsp" ] || [ -d "$PACKAGES_DIR/python_lsp_server" ]; then
|
|
||||||
echo "✓ python-lsp-server 安装成功"
|
|
||||||
else
|
|
||||||
echo "✗ python-lsp-server 安装失败"
|
|
||||||
FAILED=1
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -d "$PACKAGES_DIR/jedi" ]; then
|
|
||||||
echo "✓ jedi 安装成功"
|
|
||||||
else
|
|
||||||
echo "✗ jedi 安装失败"
|
|
||||||
FAILED=1
|
|
||||||
fi
|
|
||||||
if [ -d "$PACKAGES_DIR/jedi" ]; then
|
|
||||||
echo "✓ jedi 安装成功"
|
|
||||||
else
|
|
||||||
echo "✗ jedi 安装失败"
|
|
||||||
FAILED=1
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ $FAILED -eq 1 ]; then
|
|
||||||
echo ""
|
|
||||||
echo "✗ 部分包安装失败,请检查错误信息"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# 列出已安装的包
|
|
||||||
echo ""
|
|
||||||
echo "已安装的主要包:"
|
|
||||||
ls -1 "$PACKAGES_DIR" | grep -E "^(requests|jedi|pylsp|python_lsp)" | sort | uniq
|
|
||||||
|
|
||||||
echo ""
|
|
||||||
echo "=== 安装完成 ==="
|
|
||||||
echo ""
|
|
||||||
echo "pip 包已安装到: $PACKAGES_DIR"
|
|
||||||
echo "此目录会被打包进 jar,不受 mvn clean 影响"
|
|
||||||
echo ""
|
|
||||||
echo "包含以下功能:"
|
|
||||||
echo " - requests: HTTP 客户端,用于网络请求"
|
|
||||||
echo " - python-lsp-server: Python 语言服务器,提供代码智能提示"
|
|
||||||
echo " - jedi: Python 自动完成和静态分析库"
|
|
||||||
@@ -7,12 +7,15 @@ import org.slf4j.LoggerFactory;
|
|||||||
import cn.qaiu.parser.custom.CustomParserRegistry;
|
import cn.qaiu.parser.custom.CustomParserRegistry;
|
||||||
|
|
||||||
public class WebClientVertxInit {
|
public class WebClientVertxInit {
|
||||||
private Vertx vertx = null;
|
private volatile Vertx vertx = null;
|
||||||
private static final WebClientVertxInit INSTANCE = new WebClientVertxInit();
|
private static final WebClientVertxInit INSTANCE = new WebClientVertxInit();
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(WebClientVertxInit.class);
|
private static final Logger log = LoggerFactory.getLogger(WebClientVertxInit.class);
|
||||||
|
|
||||||
public static void init(Vertx vx) {
|
public static synchronized void init(Vertx vx) {
|
||||||
|
if (vx == null) {
|
||||||
|
throw new IllegalArgumentException("Vertx instance must not be null");
|
||||||
|
}
|
||||||
INSTANCE.vertx = vx;
|
INSTANCE.vertx = vx;
|
||||||
|
|
||||||
// 自动加载JavaScript解析器脚本
|
// 自动加载JavaScript解析器脚本
|
||||||
@@ -23,17 +26,9 @@ public class WebClientVertxInit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Vertx get() {
|
public static synchronized Vertx get() {
|
||||||
if (INSTANCE.vertx == null) {
|
if (INSTANCE.vertx == null) {
|
||||||
log.info("getVertx: Vertx实例不存在, 创建Vertx实例.");
|
throw new IllegalStateException("Vertx实例未初始化,请先调用 WebClientVertxInit.init(vertx)");
|
||||||
INSTANCE.vertx = Vertx.vertx();
|
|
||||||
|
|
||||||
// 如果Vertx实例是新创建的,也尝试加载JavaScript脚本
|
|
||||||
try {
|
|
||||||
CustomParserRegistry.autoLoadJsScripts();
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.warn("自动加载JavaScript解析器脚本失败", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return INSTANCE.vertx;
|
return INSTANCE.vertx;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package cn.qaiu.entity;
|
package cn.qaiu.entity;
|
||||||
|
|
||||||
|
import cn.qaiu.util.QuarkAccountScope;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -85,8 +87,18 @@ public class ShareLinkInfo {
|
|||||||
public String getCacheKey() {
|
public String getCacheKey() {
|
||||||
// 将type和shareKey组合成一个字符串作为缓存key
|
// 将type和shareKey组合成一个字符串作为缓存key
|
||||||
String key = type + ":" + shareKey;
|
String key = type + ":" + shareKey;
|
||||||
if (type.equals("p115")) {
|
if (type != null && type.equals("p115")) {
|
||||||
key += ("_" + otherParam.get("UA").toString().hashCode());
|
Object ua = otherParam != null ? otherParam.get("UA") : null;
|
||||||
|
if (ua != null) {
|
||||||
|
key += ("_" + ua.toString().hashCode());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 带账号的夸克 / UC 直链按账号隔离,避免会员链和 Cookie 进入分享级缓存。
|
||||||
|
if (type != null && ("qk".equalsIgnoreCase(type) || "uc".equalsIgnoreCase(type))) {
|
||||||
|
String scope = QuarkAccountScope.suffix(otherParam);
|
||||||
|
if (scope != null) {
|
||||||
|
key += "#" + scope;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,145 @@
|
|||||||
|
package cn.qaiu.parser;
|
||||||
|
|
||||||
|
import cn.qaiu.entity.ShareLinkInfo;
|
||||||
|
import io.vertx.core.json.JsonObject;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 目录列表分页约定(解析器与 API 共用)。
|
||||||
|
* <p>
|
||||||
|
* 入参写在 {@link ShareLinkInfo#getOtherParam()}:
|
||||||
|
* <ul>
|
||||||
|
* <li>{@link #PAGE}:从 1 开始的页码;不传时解析器按原逻辑只取第一页</li>
|
||||||
|
* <li>{@link #CURSOR}:上一页返回的 {@code nextCursor},可选,只是加速,不可信</li>
|
||||||
|
* </ul>
|
||||||
|
* 解析器取完一页后写回 {@link #PAGE_INFO},见 {@link #pageInfo}。
|
||||||
|
* 没写 {@link #PAGE_INFO} 的解析器视为不分页,整份列表就是第 1 页。
|
||||||
|
*/
|
||||||
|
public final class FolderPaging {
|
||||||
|
|
||||||
|
public static final String PAGE = "page";
|
||||||
|
public static final String CURSOR = "pageCursor";
|
||||||
|
public static final String PAGE_INFO = "pageInfo";
|
||||||
|
|
||||||
|
/** 页码上限。蓝奏无游标时要从第 1 页顺序翻到目标页,必须有界。 */
|
||||||
|
public static final int MAX_PAGE = 500;
|
||||||
|
|
||||||
|
private static final Set<String> PAGED_TYPES = Set.of("lz", "fj", "iz");
|
||||||
|
|
||||||
|
private FolderPaging() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 该网盘的目录解析是否支持按页取。 */
|
||||||
|
public static boolean supports(String type) {
|
||||||
|
return type != null && PAGED_TYPES.contains(type.toLowerCase(Locale.ROOT));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析页码参数。空值返回 null(保持旧行为);非法值抛 IllegalArgumentException。
|
||||||
|
*/
|
||||||
|
public static Integer parsePage(String raw) {
|
||||||
|
if (raw == null || raw.isBlank()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
int page;
|
||||||
|
try {
|
||||||
|
page = Integer.parseInt(raw.trim());
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
throw new IllegalArgumentException("page 必须是正整数: " + raw);
|
||||||
|
}
|
||||||
|
if (page < 1 || page > MAX_PAGE) {
|
||||||
|
throw new IllegalArgumentException("page 超出范围 1-" + MAX_PAGE + ": " + raw);
|
||||||
|
}
|
||||||
|
return page;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 本次请求的页码;没传返回 null。 */
|
||||||
|
public static Integer requestedPage(ShareLinkInfo info) {
|
||||||
|
Object v = info.getOtherParam().get(PAGE);
|
||||||
|
if (v instanceof Number n) {
|
||||||
|
return n.intValue();
|
||||||
|
}
|
||||||
|
return v == null ? null : parsePage(v.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 本次请求的页码;没传按第 1 页。 */
|
||||||
|
public static int pageOrFirst(ShareLinkInfo info) {
|
||||||
|
Integer page = requestedPage(info);
|
||||||
|
return page == null ? 1 : page;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String cursor(ShareLinkInfo info) {
|
||||||
|
Object v = info.getOtherParam().get(CURSOR);
|
||||||
|
return v == null || v.toString().isBlank() ? null : v.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 写回分页结果。
|
||||||
|
*
|
||||||
|
* @param pageSize 上游每页条数(原始条数口径,含被过滤的占位项)
|
||||||
|
* @param hasMore 上游明确还有下一页
|
||||||
|
* @param total 上游给出的总条数,未知传 null
|
||||||
|
* @param totalPage 上游给出的总页数,未知传 null
|
||||||
|
* @param nextCursor 取下一页可直接复用的游标,没有传 null
|
||||||
|
*/
|
||||||
|
public static JsonObject pageInfo(ShareLinkInfo info, int page, int pageSize, boolean hasMore,
|
||||||
|
Integer total, Integer totalPage, String nextCursor) {
|
||||||
|
JsonObject json = new JsonObject()
|
||||||
|
.put("paged", true)
|
||||||
|
.put("page", page)
|
||||||
|
.put("pageSize", pageSize)
|
||||||
|
.put("hasMore", hasMore)
|
||||||
|
.put("nextPage", hasMore ? page + 1 : null)
|
||||||
|
.put("total", total)
|
||||||
|
.put("totalPage", totalPage)
|
||||||
|
.put("nextCursor", hasMore ? nextCursor : null);
|
||||||
|
info.getOtherParam().put(PAGE_INFO, json);
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上游给了总页数就以它为准;没给时整页满才认为可能还有。空页一律结束,避免无限翻页。
|
||||||
|
*/
|
||||||
|
public static boolean hasMore(int page, int rawCount, int pageSize, Integer totalPage) {
|
||||||
|
if (rawCount <= 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (totalPage != null && totalPage > 0) {
|
||||||
|
return page < totalPage;
|
||||||
|
}
|
||||||
|
return rawCount >= pageSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 小飞机 / 蓝奏优享 share/list 响应:offset 为页码,带 total / totalPage。 */
|
||||||
|
public static JsonObject offsetListPage(ShareLinkInfo info, JsonObject resp, int rawCount, int pageSize) {
|
||||||
|
int page = pageOrFirst(info);
|
||||||
|
Integer total = intOrNull(resp, "total");
|
||||||
|
Integer totalPage = intOrNull(resp, "totalPage");
|
||||||
|
return pageInfo(info, page, pageSize, hasMore(page, rawCount, pageSize, totalPage), total, totalPage, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Integer intOrNull(JsonObject json, String key) {
|
||||||
|
if (json == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Object v = json.getValue(key);
|
||||||
|
if (v instanceof Number n) {
|
||||||
|
return n.intValue();
|
||||||
|
}
|
||||||
|
if (v instanceof CharSequence s) {
|
||||||
|
try {
|
||||||
|
return Integer.parseInt(s.toString().trim());
|
||||||
|
} catch (NumberFormatException ignored) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static JsonObject pageInfo(ShareLinkInfo info) {
|
||||||
|
Object v = info.getOtherParam().get(PAGE_INFO);
|
||||||
|
return v instanceof JsonObject json ? json : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package cn.qaiu.parser;//package cn.qaiu.lz.common.parser;
|
package cn.qaiu.parser;//package cn.qaiu.lz.common.parser;
|
||||||
|
|
||||||
|
import cn.qaiu.WebClientVertxInit;
|
||||||
import cn.qaiu.entity.FileInfo;
|
import cn.qaiu.entity.FileInfo;
|
||||||
import cn.qaiu.entity.ShareLinkInfo;
|
import cn.qaiu.entity.ShareLinkInfo;
|
||||||
import cn.qaiu.parser.clientlink.ClientLinkGeneratorFactory;
|
import cn.qaiu.parser.clientlink.ClientLinkGeneratorFactory;
|
||||||
@@ -7,10 +8,26 @@ import cn.qaiu.parser.clientlink.ClientLinkType;
|
|||||||
import io.vertx.core.Future;
|
import io.vertx.core.Future;
|
||||||
import io.vertx.core.Promise;
|
import io.vertx.core.Promise;
|
||||||
|
|
||||||
|
import java.util.function.Supplier;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
public interface IPanTool {
|
public interface IPanTool extends AutoCloseable {
|
||||||
|
|
||||||
|
/** 同步等待超时时间(秒) */
|
||||||
|
long SYNC_TIMEOUT_SECONDS = 120;
|
||||||
|
|
||||||
|
ScheduledExecutorService CLOSE_AFTER_SCHEDULER = Executors.newSingleThreadScheduledExecutor(r -> {
|
||||||
|
Thread t = new Thread(r, "pan-tool-close-after");
|
||||||
|
t.setDaemon(true);
|
||||||
|
return t;
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 解析文件
|
* 解析文件
|
||||||
@@ -18,8 +35,72 @@ public interface IPanTool {
|
|||||||
*/
|
*/
|
||||||
Future<String> parse();
|
Future<String> parse();
|
||||||
|
|
||||||
|
static <T> Future<T> closeAfter(IPanTool tool, Supplier<Future<T>> action) {
|
||||||
|
Promise<T> promise = Promise.promise();
|
||||||
|
AtomicBoolean cleanupDone = new AtomicBoolean(false);
|
||||||
|
ScheduledFuture<?> cleanupTask = null;
|
||||||
|
try {
|
||||||
|
Future<T> future = action.get();
|
||||||
|
if (future == null) {
|
||||||
|
closeQuietly(tool);
|
||||||
|
return Future.failedFuture("解析器返回空 Future");
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanupTask = CLOSE_AFTER_SCHEDULER.schedule(() -> {
|
||||||
|
if (cleanupDone.compareAndSet(false, true)) {
|
||||||
|
closeQuietly(tool);
|
||||||
|
failOnVertxContext(promise, "解析超时(" + SYNC_TIMEOUT_SECONDS + "秒)");
|
||||||
|
}
|
||||||
|
}, SYNC_TIMEOUT_SECONDS, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
ScheduledFuture<?> scheduledCleanupTask = cleanupTask;
|
||||||
|
future.onComplete(ar -> {
|
||||||
|
scheduledCleanupTask.cancel(false);
|
||||||
|
if (!cleanupDone.compareAndSet(false, true)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
closeQuietly(tool);
|
||||||
|
if (ar.succeeded()) {
|
||||||
|
promise.tryComplete(ar.result());
|
||||||
|
} else {
|
||||||
|
promise.tryFail(ar.cause());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return promise.future();
|
||||||
|
} catch (Throwable t) {
|
||||||
|
if (cleanupTask != null) {
|
||||||
|
cleanupTask.cancel(false);
|
||||||
|
}
|
||||||
|
closeQuietly(tool);
|
||||||
|
return Future.failedFuture(t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> void failOnVertxContext(Promise<T> promise, String message) {
|
||||||
|
try {
|
||||||
|
WebClientVertxInit.get().runOnContext(ignored -> promise.tryFail(message));
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
promise.tryFail(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void closeQuietly(IPanTool tool) {
|
||||||
|
if (tool == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
tool.close();
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
// ignore cleanup failures
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void shutdownCloseAfterScheduler() {
|
||||||
|
CLOSE_AFTER_SCHEDULER.shutdownNow();
|
||||||
|
}
|
||||||
|
|
||||||
default String parseSync() {
|
default String parseSync() {
|
||||||
return parse().toCompletionStage().toCompletableFuture().join();
|
return timedJoin(parse());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -33,7 +114,7 @@ public interface IPanTool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
default List<FileInfo> parseFileListSync() {
|
default List<FileInfo> parseFileListSync() {
|
||||||
return parseFileList().toCompletionStage().toCompletableFuture().join();
|
return timedJoin(parseFileList());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -47,7 +128,7 @@ public interface IPanTool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
default String parseByIdSync() {
|
default String parseByIdSync() {
|
||||||
return parseById().toCompletionStage().toCompletableFuture().join();
|
return timedJoin(parseById());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -126,7 +207,7 @@ public interface IPanTool {
|
|||||||
* @return Map<ClientLinkType, String> 客户端下载链接集合
|
* @return Map<ClientLinkType, String> 客户端下载链接集合
|
||||||
*/
|
*/
|
||||||
default Map<ClientLinkType, String> parseWithClientLinksSync() {
|
default Map<ClientLinkType, String> parseWithClientLinksSync() {
|
||||||
return parseWithClientLinks().toCompletionStage().toCompletableFuture().join();
|
return timedJoin(parseWithClientLinks());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -137,4 +218,26 @@ public interface IPanTool {
|
|||||||
default ShareLinkInfo getShareLinkInfo() {
|
default ShareLinkInfo getShareLinkInfo() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
default void close() {
|
||||||
|
// default no-op
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 带超时的同步等待工具方法,替代无超时的 join()
|
||||||
|
*/
|
||||||
|
private static <T> T timedJoin(Future<T> future) {
|
||||||
|
try {
|
||||||
|
return future.toCompletionStage().toCompletableFuture()
|
||||||
|
.get(SYNC_TIMEOUT_SECONDS, TimeUnit.SECONDS);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
throw new RuntimeException("线程被中断", e);
|
||||||
|
} catch (TimeoutException e) {
|
||||||
|
throw new RuntimeException("同步等待超时(" + SYNC_TIMEOUT_SECONDS + "秒)", e);
|
||||||
|
} catch (java.util.concurrent.ExecutionException e) {
|
||||||
|
throw new RuntimeException(e.getCause() != null ? e.getCause() : e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -20,6 +20,9 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -34,30 +37,67 @@ import java.util.zip.GZIPInputStream;
|
|||||||
* <p>{网盘标识}Tool, 网盘标识不超过5个字符, 可以取网盘名称首字母缩写或拼音首字母, <br>
|
* <p>{网盘标识}Tool, 网盘标识不超过5个字符, 可以取网盘名称首字母缩写或拼音首字母, <br>
|
||||||
* 音乐类型的解析以M开头, 例如网易云音乐Mne</p>
|
* 音乐类型的解析以M开头, 例如网易云音乐Mne</p>
|
||||||
*/
|
*/
|
||||||
public abstract class PanBase implements IPanTool {
|
public abstract class PanBase implements IPanTool, Closeable {
|
||||||
protected Logger log = LoggerFactory.getLogger(this.getClass());
|
protected Logger log = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
protected Promise<String> promise = Promise.promise();
|
protected Promise<String> promise = Promise.promise();
|
||||||
|
|
||||||
/**
|
private static final int MAX_COMPRESSED_RESPONSE_BYTES = 8 * 1024 * 1024;
|
||||||
* Http client
|
private static final int MAX_DECOMPRESSED_RESPONSE_CHARS = 16 * 1024 * 1024;
|
||||||
*/
|
private static final int MAX_ERROR_BODY_CHARS = 4096;
|
||||||
protected WebClient client = WebClient.create(WebClientVertxInit.get(),
|
|
||||||
new WebClientOptions());
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Http client session (会话管理, 带cookie请求)
|
* 共享的 WebClient 配置(设置超时避免连接无限期占用)
|
||||||
|
*/
|
||||||
|
private static final WebClientOptions SHARED_OPTIONS = new WebClientOptions()
|
||||||
|
.setConnectTimeout(10000) // 连接超时 10 秒
|
||||||
|
.setIdleTimeout(30) // 空闲超时 30 秒
|
||||||
|
.setIdleTimeoutUnit(java.util.concurrent.TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
private static final Object SHARED_CLIENT_LOCK = new Object();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 共享的 WebClient 实例(线程安全,避免每请求创建导致资源泄漏)
|
||||||
|
*/
|
||||||
|
private static volatile WebClient sharedClient;
|
||||||
|
private static volatile WebClient sharedClientNoRedirects;
|
||||||
|
private static volatile WebClient sharedClientDisableUA;
|
||||||
|
private static volatile boolean sharedClientsShutdown = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Http client (默认使用共享实例,代理模式下使用独立实例)
|
||||||
|
*/
|
||||||
|
protected WebClient client = sharedClient();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Http client session (会话管理, 带cookie请求, 每实例独立)
|
||||||
*/
|
*/
|
||||||
protected WebClientSession clientSession = WebClientSession.create(client);
|
protected WebClientSession clientSession = WebClientSession.create(client);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Http client 不自动跳转
|
* Http client 不自动跳转
|
||||||
*/
|
*/
|
||||||
protected WebClient clientNoRedirects = WebClient.create(WebClientVertxInit.get(),
|
protected WebClient clientNoRedirects = sharedClientNoRedirects();
|
||||||
new WebClientOptions().setFollowRedirects(false));
|
|
||||||
|
/**
|
||||||
|
* Http client disable UserAgent
|
||||||
|
*/
|
||||||
|
protected WebClient clientDisableUA = sharedClientDisableUA();
|
||||||
|
|
||||||
protected ShareLinkInfo shareLinkInfo;
|
protected ShareLinkInfo shareLinkInfo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 标记是否为代理模式(代理模式创建的 WebClient 需要手动关闭)
|
||||||
|
*/
|
||||||
|
private boolean isProxyMode = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 代理模式下创建的独立 WebClient 实例(需要在 close 时释放)
|
||||||
|
*/
|
||||||
|
private WebClient proxyClient = null;
|
||||||
|
private WebClient proxyClientNoRedirects = null;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 子类重写此构造方法不需要添加额外逻辑
|
* 子类重写此构造方法不需要添加额外逻辑
|
||||||
* 如:
|
* 如:
|
||||||
@@ -72,6 +112,7 @@ public abstract class PanBase implements IPanTool {
|
|||||||
public PanBase(ShareLinkInfo shareLinkInfo) {
|
public PanBase(ShareLinkInfo shareLinkInfo) {
|
||||||
this.shareLinkInfo = shareLinkInfo;
|
this.shareLinkInfo = shareLinkInfo;
|
||||||
if (shareLinkInfo.getOtherParam().containsKey("proxy")) {
|
if (shareLinkInfo.getOtherParam().containsKey("proxy")) {
|
||||||
|
this.isProxyMode = true;
|
||||||
JsonObject proxy = (JsonObject) shareLinkInfo.getOtherParam().get("proxy");
|
JsonObject proxy = (JsonObject) shareLinkInfo.getOtherParam().get("proxy");
|
||||||
ProxyOptions proxyOptions = new ProxyOptions()
|
ProxyOptions proxyOptions = new ProxyOptions()
|
||||||
.setType(ProxyType.valueOf(proxy.getString("type").toUpperCase()))
|
.setType(ProxyType.valueOf(proxy.getString("type").toUpperCase()))
|
||||||
@@ -83,22 +124,156 @@ public abstract class PanBase implements IPanTool {
|
|||||||
if (StringUtils.isNotEmpty(proxy.getString("password"))) {
|
if (StringUtils.isNotEmpty(proxy.getString("password"))) {
|
||||||
proxyOptions.setPassword(proxy.getString("password"));
|
proxyOptions.setPassword(proxy.getString("password"));
|
||||||
}
|
}
|
||||||
this.client = WebClient.create(WebClientVertxInit.get(),
|
// 代理模式下创建独立的 WebClient 实例(应用超时配置)
|
||||||
new WebClientOptions()
|
this.proxyClient = WebClient.create(WebClientVertxInit.get(),
|
||||||
|
new WebClientOptions(SHARED_OPTIONS)
|
||||||
|
.setUserAgentEnabled(false)
|
||||||
|
.setProxyOptions(proxyOptions));
|
||||||
|
this.proxyClientNoRedirects = WebClient.create(WebClientVertxInit.get(),
|
||||||
|
new WebClientOptions(SHARED_OPTIONS).setFollowRedirects(false)
|
||||||
.setUserAgentEnabled(false)
|
.setUserAgentEnabled(false)
|
||||||
.setProxyOptions(proxyOptions));
|
.setProxyOptions(proxyOptions));
|
||||||
|
|
||||||
|
this.client = proxyClient;
|
||||||
this.clientSession = WebClientSession.create(client);
|
this.clientSession = WebClientSession.create(client);
|
||||||
this.clientNoRedirects = WebClient.create(WebClientVertxInit.get(),
|
this.clientNoRedirects = proxyClientNoRedirects;
|
||||||
new WebClientOptions().setFollowRedirects(false)
|
|
||||||
.setUserAgentEnabled(false)
|
|
||||||
.setProxyOptions(proxyOptions));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected PanBase() {
|
protected PanBase() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static WebClient sharedClient() {
|
||||||
|
synchronized (SHARED_CLIENT_LOCK) {
|
||||||
|
if (sharedClientsShutdown) {
|
||||||
|
throw new IllegalStateException("共享 WebClient 已关闭");
|
||||||
|
}
|
||||||
|
if (sharedClient == null) {
|
||||||
|
sharedClient = WebClient.create(WebClientVertxInit.get(), new WebClientOptions(SHARED_OPTIONS));
|
||||||
|
}
|
||||||
|
return sharedClient;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static WebClient sharedClientNoRedirects() {
|
||||||
|
synchronized (SHARED_CLIENT_LOCK) {
|
||||||
|
if (sharedClientsShutdown) {
|
||||||
|
throw new IllegalStateException("共享 WebClient 已关闭");
|
||||||
|
}
|
||||||
|
if (sharedClientNoRedirects == null) {
|
||||||
|
sharedClientNoRedirects = WebClient.create(WebClientVertxInit.get(),
|
||||||
|
new WebClientOptions(SHARED_OPTIONS).setFollowRedirects(false));
|
||||||
|
}
|
||||||
|
return sharedClientNoRedirects;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static WebClient sharedClientDisableUA() {
|
||||||
|
synchronized (SHARED_CLIENT_LOCK) {
|
||||||
|
if (sharedClientsShutdown) {
|
||||||
|
throw new IllegalStateException("共享 WebClient 已关闭");
|
||||||
|
}
|
||||||
|
if (sharedClientDisableUA == null) {
|
||||||
|
sharedClientDisableUA = WebClient.create(WebClientVertxInit.get(),
|
||||||
|
new WebClientOptions(SHARED_OPTIONS).setUserAgentEnabled(false));
|
||||||
|
}
|
||||||
|
return sharedClientDisableUA;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void shutdownSharedClients() {
|
||||||
|
synchronized (SHARED_CLIENT_LOCK) {
|
||||||
|
sharedClientsShutdown = true;
|
||||||
|
closeSharedClient(sharedClient, "shared WebClient");
|
||||||
|
closeSharedClient(sharedClientNoRedirects, "shared WebClientNoRedirects");
|
||||||
|
closeSharedClient(sharedClientDisableUA, "shared WebClientDisableUA");
|
||||||
|
sharedClient = null;
|
||||||
|
sharedClientNoRedirects = null;
|
||||||
|
sharedClientDisableUA = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void closeSharedClient(WebClient client, String name) {
|
||||||
|
if (client == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
client.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
LoggerFactory.getLogger(PanBase.class).warn("关闭 {} 失败: {}", name, e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SSRF 防护: 校验通用自定义域名解析器 (CE/Ce4/Kd/Other 等) 即将请求的目标主机,
|
||||||
|
* 拒绝解析到回环/内网/链路本地/组播等非公网地址的域名, 阻止攻击者通过可控 DNS
|
||||||
|
* 记录 (或直接填写内网域名) 让服务端向内网/云元数据接口发起请求。
|
||||||
|
* <p>
|
||||||
|
* 必须在子类 parse() 中构造出 baseUrl/发起任何 clientSession 请求之前调用。
|
||||||
|
*
|
||||||
|
* @param url 从 shareLinkInfo.getShareUrl() 解析出的 URL
|
||||||
|
* @throws IOException 当主机无法解析或解析结果落入禁止的地址段时抛出
|
||||||
|
*/
|
||||||
|
protected static void assertPublicHost(URL url) throws IOException {
|
||||||
|
String host = url.getHost();
|
||||||
|
InetAddress[] addresses;
|
||||||
|
try {
|
||||||
|
addresses = InetAddress.getAllByName(host);
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
throw new IOException("无法解析目标主机: " + host, e);
|
||||||
|
}
|
||||||
|
if (addresses.length == 0) {
|
||||||
|
throw new IOException("无法解析目标主机: " + host);
|
||||||
|
}
|
||||||
|
for (InetAddress addr : addresses) {
|
||||||
|
if (isDisallowedAddress(addr)) {
|
||||||
|
throw new IOException("目标地址不允许访问(内网/回环/链路本地/组播): "
|
||||||
|
+ host + " -> " + addr.getHostAddress());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断地址是否落入禁止访问的范围: 0.0.0.0/8, 10/8, 127/8, 169.254/16(含云元数据
|
||||||
|
* 169.254.169.254), 172.16/12, 192.168/16, 100.64/10(CGNAT), 224/4~255/4(组播/保留),
|
||||||
|
* 以及对应的 IPv6 回环/链路本地/唯一本地地址(fc00::/7)/组播地址; IPv4-映射的 IPv6
|
||||||
|
* 地址(::ffff:a.b.c.d) 会先还原为 IPv4 再判断,避免绕过。
|
||||||
|
*/
|
||||||
|
private static boolean isDisallowedAddress(InetAddress addr) {
|
||||||
|
byte[] bytes = addr.getAddress();
|
||||||
|
if (bytes.length == 16 && isIPv4Mapped(bytes)) {
|
||||||
|
byte[] v4 = new byte[4];
|
||||||
|
System.arraycopy(bytes, 12, v4, 0, 4);
|
||||||
|
bytes = v4;
|
||||||
|
}
|
||||||
|
if (bytes.length == 4) {
|
||||||
|
int b0 = bytes[0] & 0xFF;
|
||||||
|
int b1 = bytes[1] & 0xFF;
|
||||||
|
if (b0 == 0) return true; // 0.0.0.0/8
|
||||||
|
if (b0 == 10) return true; // 10.0.0.0/8
|
||||||
|
if (b0 == 127) return true; // 127.0.0.0/8 loopback
|
||||||
|
if (b0 == 169 && b1 == 254) return true; // 169.254.0.0/16 (含云元数据 169.254.169.254)
|
||||||
|
if (b0 == 172 && b1 >= 16 && b1 <= 31) return true; // 172.16.0.0/12
|
||||||
|
if (b0 == 192 && b1 == 168) return true; // 192.168.0.0/16
|
||||||
|
if (b0 == 100 && b1 >= 64 && b1 <= 127) return true;// 100.64.0.0/10 CGNAT
|
||||||
|
return b0 >= 224; // 224.0.0.0/4 组播 + 240.0.0.0/4 保留
|
||||||
|
}
|
||||||
|
// IPv6
|
||||||
|
if (addr.isAnyLocalAddress() || addr.isLoopbackAddress()
|
||||||
|
|| addr.isLinkLocalAddress() || addr.isSiteLocalAddress()
|
||||||
|
|| addr.isMulticastAddress()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return bytes.length == 16 && (bytes[0] & 0xFE) == 0xFC; // fc00::/7 unique local
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isIPv4Mapped(byte[] b) {
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
if (b[i] != 0) return false;
|
||||||
|
}
|
||||||
|
return (b[10] & 0xFF) == 0xFF && (b[11] & 0xFF) == 0xFF;
|
||||||
|
}
|
||||||
|
|
||||||
protected String baseMsg() {
|
protected String baseMsg() {
|
||||||
if (shareLinkInfo.getShareUrl() != null) {
|
if (shareLinkInfo.getShareUrl() != null) {
|
||||||
return shareLinkInfo.getPanName() + "-" + shareLinkInfo.getType() + ": url=" + shareLinkInfo.getShareUrl();
|
return shareLinkInfo.getPanName() + "-" + shareLinkInfo.getType() + ": url=" + shareLinkInfo.getShareUrl();
|
||||||
@@ -123,16 +298,19 @@ public abstract class PanBase implements IPanTool {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String s = String.format(errorMsg.replaceAll("\\{}", "%s"), args);
|
String s = String.format(errorMsg.replaceAll("\\{}", "%s"), args);
|
||||||
log.error("解析异常: " + s, t.fillInStackTrace());
|
// 只记录异常消息和类型,不调用 fillInStackTrace 避免产生巨大栈信息
|
||||||
promise.fail(baseMsg() + ": 解析异常: " + s + " -> " + t);
|
log.error("解析异常: {} - {}: {}", s, t.getClass().getSimpleName(), t.getMessage());
|
||||||
|
// 只传递异常消息,不传递完整异常对象,减少内存占用
|
||||||
|
String failMsg = baseMsg() + ": 解析异常: " + s + " -> " + t.getClass().getSimpleName() + ": " + t.getMessage();
|
||||||
|
promise.fail(failMsg);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("ErrorMsg format fail. The parameter has been discarded", e);
|
log.error("ErrorMsg format fail. The parameter has been discarded", e);
|
||||||
log.error("解析异常: " + errorMsg, t.fillInStackTrace());
|
log.error("解析异常: {} - {}: {}", errorMsg, t.getClass().getSimpleName(), t.getMessage());
|
||||||
if (promise.future().isComplete()) {
|
if (promise.future().isComplete()) {
|
||||||
log.warn("ErrorMsg format. Promise 已经完成, 无法再次失败: {}", errorMsg);
|
log.warn("ErrorMsg format. Promise 已经完成, 无法再次失败: {}", errorMsg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
promise.fail(baseMsg() + ": 解析异常: " + errorMsg + " -> " + t);
|
promise.fail(baseMsg() + ": 解析异常: " + errorMsg + " -> " + t.getClass().getSimpleName() + ": " + t.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,7 +350,7 @@ public abstract class PanBase implements IPanTool {
|
|||||||
* @return Handler
|
* @return Handler
|
||||||
*/
|
*/
|
||||||
protected Handler<Throwable> handleFail(String errorMsg) {
|
protected Handler<Throwable> handleFail(String errorMsg) {
|
||||||
return t -> fail(baseMsg() + " - 请求异常 {}: -> {}", errorMsg, t.fillInStackTrace());
|
return t -> fail(baseMsg() + " - 请求异常 {}: -> {}", errorMsg, t.getClass().getSimpleName() + ": " + t.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Handler<Throwable> handleFail() {
|
protected Handler<Throwable> handleFail() {
|
||||||
@@ -190,29 +368,27 @@ public abstract class PanBase implements IPanTool {
|
|||||||
// 检查响应头中的Content-Encoding是否为gzip
|
// 检查响应头中的Content-Encoding是否为gzip
|
||||||
String contentEncoding = res.getHeader("Content-Encoding");
|
String contentEncoding = res.getHeader("Content-Encoding");
|
||||||
try {
|
try {
|
||||||
if ("gzip".equalsIgnoreCase(contentEncoding)) {
|
if ("gzip".equalsIgnoreCase(contentEncoding) && res.body() instanceof Buffer gzipBody
|
||||||
// 如果是gzip压缩的响应体,解压
|
&& gzipBody.length() >= 2
|
||||||
return new JsonObject(decompressGzip((Buffer) res.body()));
|
&& (gzipBody.getByte(0) & 0xff) == 0x1f
|
||||||
|
&& (gzipBody.getByte(1) & 0xff) == 0x8b) {
|
||||||
|
// 如果是gzip压缩的响应体,解压(只解压一次,缓存结果)
|
||||||
|
String decompressed = decompressGzip(gzipBody);
|
||||||
|
return new JsonObject(decompressed);
|
||||||
} else {
|
} else {
|
||||||
return res.bodyAsJsonObject();
|
return res.bodyAsJsonObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
if ("gzip".equalsIgnoreCase(contentEncoding)) {
|
if ("gzip".equalsIgnoreCase(contentEncoding)) {
|
||||||
// 如果是gzip压缩的响应体,解压
|
// gzip解压失败,记录错误
|
||||||
try {
|
log.error("响应gzip解压或JSON解析失败: {}", e.getMessage());
|
||||||
log.error(decompressGzip((Buffer) res.body()));
|
fail("响应gzip解压或JSON解析失败: {}", e.getMessage());
|
||||||
fail(decompressGzip((Buffer) res.body()));
|
|
||||||
//throw new RuntimeException("响应不是JSON格式");
|
|
||||||
} catch (IOException ex) {
|
|
||||||
log.error("响应gzip解压失败");
|
|
||||||
fail("响应gzip解压失败: {}", ex.getMessage());
|
|
||||||
//throw new RuntimeException("响应gzip解压失败", ex);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
log.error("解析失败: json格式异常: {}", res.bodyAsString());
|
// 上游响应体可能来自内网探测目标,仅写日志,避免经 HTTP 500 回传给调用方
|
||||||
fail("解析失败: json格式异常: {}", res.bodyAsString());
|
String bodyPreview = responseBodyPreview(res);
|
||||||
//throw new RuntimeException("解析失败: json格式异常");
|
log.error("解析失败: json格式异常: {}", bodyPreview);
|
||||||
|
fail("解析失败: json格式异常");
|
||||||
}
|
}
|
||||||
return JsonObject.of();
|
return JsonObject.of();
|
||||||
}
|
}
|
||||||
@@ -242,7 +418,18 @@ public abstract class PanBase implements IPanTool {
|
|||||||
protected void completeWithMeta(String url, Map<String, String> headers) {
|
protected void completeWithMeta(String url, Map<String, String> headers) {
|
||||||
shareLinkInfo.getOtherParam().put("downloadUrl", url);
|
shareLinkInfo.getOtherParam().put("downloadUrl", url);
|
||||||
if (headers != null && !headers.isEmpty()) {
|
if (headers != null && !headers.isEmpty()) {
|
||||||
shareLinkInfo.getOtherParam().put("downloadHeaders", headers);
|
// 过滤 null/空值,避免 cookie:null 覆盖入口参数或污染 curl 命令
|
||||||
|
Map<String, String> clean = new HashMap<>();
|
||||||
|
headers.forEach((k, v) -> {
|
||||||
|
if (k != null && v != null && !v.isBlank()) {
|
||||||
|
clean.put(k, v);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (!clean.isEmpty()) {
|
||||||
|
shareLinkInfo.getOtherParam().put("downloadHeaders", clean);
|
||||||
|
// UC/夸克等需带 cookie 的直链,标记前端走下载器
|
||||||
|
shareLinkInfo.getOtherParam().put("needDownloader", true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
promise.complete(url);
|
promise.complete(url);
|
||||||
}
|
}
|
||||||
@@ -275,11 +462,15 @@ public abstract class PanBase implements IPanTool {
|
|||||||
if (iterator.hasNext()) {
|
if (iterator.hasNext()) {
|
||||||
PanDomainTemplate next = iterator.next();
|
PanDomainTemplate next = iterator.next();
|
||||||
log.debug("规则不匹配, 处理解析器转发: {} -> {}", shareLinkInfo.getPanName(), next.getDisplayName());
|
log.debug("规则不匹配, 处理解析器转发: {} -> {}", shareLinkInfo.getPanName(), next.getDisplayName());
|
||||||
ParserCreate.fromType(next.name())
|
try {
|
||||||
|
IPanTool nextTool = ParserCreate.fromType(next.name())
|
||||||
.fromAnyShareUrl(shareLinkInfo.getShareUrl())
|
.fromAnyShareUrl(shareLinkInfo.getShareUrl())
|
||||||
.createTool()
|
.createTool();
|
||||||
.parse()
|
IPanTool.closeAfter(nextTool, nextTool::parse)
|
||||||
.onComplete(promise);
|
.onComplete(promise);
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail(e, "转发到下一个解析器失败: {}", next.getDisplayName());
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
fail("error: 没有下一个解析处理器");
|
fail("error: 没有下一个解析处理器");
|
||||||
}
|
}
|
||||||
@@ -295,6 +486,12 @@ public abstract class PanBase implements IPanTool {
|
|||||||
* @throws IOException IOException
|
* @throws IOException IOException
|
||||||
*/
|
*/
|
||||||
private String decompressGzip(Buffer compressedData) throws IOException {
|
private String decompressGzip(Buffer compressedData) throws IOException {
|
||||||
|
if (compressedData == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
if (compressedData.length() > MAX_COMPRESSED_RESPONSE_BYTES) {
|
||||||
|
throw new IOException("gzip响应体过大: " + compressedData.length() + " bytes");
|
||||||
|
}
|
||||||
try (ByteArrayInputStream bais = new ByteArrayInputStream(compressedData.getBytes());
|
try (ByteArrayInputStream bais = new ByteArrayInputStream(compressedData.getBytes());
|
||||||
GZIPInputStream gzis = new GZIPInputStream(bais);
|
GZIPInputStream gzis = new GZIPInputStream(bais);
|
||||||
InputStreamReader isr = new InputStreamReader(gzis, StandardCharsets.UTF_8);
|
InputStreamReader isr = new InputStreamReader(gzis, StandardCharsets.UTF_8);
|
||||||
@@ -303,18 +500,97 @@ public abstract class PanBase implements IPanTool {
|
|||||||
char[] buffer = new char[4096];
|
char[] buffer = new char[4096];
|
||||||
int n;
|
int n;
|
||||||
while ((n = isr.read(buffer)) != -1) {
|
while ((n = isr.read(buffer)) != -1) {
|
||||||
writer.write(buffer, 0, n);
|
writeLimited(writer, buffer, n);
|
||||||
}
|
}
|
||||||
return writer.toString();
|
return writer.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void writeLimited(StringWriter writer, char[] buffer, int len) throws IOException {
|
||||||
|
if (writer.getBuffer().length() + len > MAX_DECOMPRESSED_RESPONSE_CHARS) {
|
||||||
|
throw new IOException("gzip解压后响应体过大");
|
||||||
|
}
|
||||||
|
writer.write(buffer, 0, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String responseBodyPreview(HttpResponse<?> res) {
|
||||||
|
if (res == null || res.body() == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (res.body() instanceof Buffer body) {
|
||||||
|
int length = Math.min(body.length(), MAX_ERROR_BODY_CHARS);
|
||||||
|
String preview = new String(body.getBytes(0, length), StandardCharsets.UTF_8);
|
||||||
|
return body.length() > length ? preview + "...(truncated " + body.length() + " bytes)" : preview;
|
||||||
|
}
|
||||||
|
String text = res.bodyAsString();
|
||||||
|
if (text == null || text.length() <= MAX_ERROR_BODY_CHARS) {
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
return text.substring(0, MAX_ERROR_BODY_CHARS) + "...(truncated " + text.length() + " chars)";
|
||||||
|
} catch (Exception e) {
|
||||||
|
return "<body preview failed: " + e.getMessage() + ">";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected String getDomainName(){
|
protected String getDomainName(){
|
||||||
return shareLinkInfo.getOtherParam().getOrDefault("domainName", "").toString();
|
return shareLinkInfo.getOtherParam().getOrDefault("domainName", "").toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将入口请求中的加密 auth 透传到子目录/下载链接,避免进入子目录后丢失认证。
|
||||||
|
* otherParam 中的 key 为 {@code _authQuery}(由 web 层写入)。
|
||||||
|
*/
|
||||||
|
protected String appendAuthQuery(String url) {
|
||||||
|
if (StringUtils.isBlank(url) || shareLinkInfo == null || shareLinkInfo.getOtherParam() == null) {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
Object authObj = shareLinkInfo.getOtherParam().get("_authQuery");
|
||||||
|
if (authObj == null) {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
String auth = authObj.toString();
|
||||||
|
if (StringUtils.isBlank(auth)) {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
// 已带 auth 则不再追加
|
||||||
|
if (url.contains("auth=")) {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
String encoded = java.net.URLEncoder.encode(auth, StandardCharsets.UTF_8);
|
||||||
|
return url + (url.contains("?") ? "&" : "?") + "auth=" + encoded;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return url + (url.contains("?") ? "&" : "?") + "auth=" + auth;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ShareLinkInfo getShareLinkInfo() {
|
public ShareLinkInfo getShareLinkInfo() {
|
||||||
return shareLinkInfo;
|
return shareLinkInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭代理模式下创建的 WebClient 资源
|
||||||
|
* 非代理模式使用共享实例,不需要关闭
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
if (isProxyMode) {
|
||||||
|
try {
|
||||||
|
if (proxyClient != null) {
|
||||||
|
proxyClient.close();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("关闭代理 WebClient 失败: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (proxyClientNoRedirects != null) {
|
||||||
|
proxyClientNoRedirects.close();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("关闭代理 WebClientNoRedirects 失败: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -68,8 +68,8 @@ public enum PanDomainTemplate {
|
|||||||
t-is.cn
|
t-is.cn
|
||||||
*/
|
*/
|
||||||
LZ("蓝奏云",
|
LZ("蓝奏云",
|
||||||
compile("https://(?:[a-zA-Z\\d-]+\\.)?(" +
|
compile("https://(?:[a-zA-Z\\d-]+\\.)?(?:" +
|
||||||
"lanzoul|" +
|
"(?:lanzoul|" +
|
||||||
"lanzouh|" +
|
"lanzouh|" +
|
||||||
"lanosso|" +
|
"lanosso|" +
|
||||||
"lanpv|" +
|
"lanpv|" +
|
||||||
@@ -102,9 +102,10 @@ public enum PanDomainTemplate {
|
|||||||
"lanzouc|" +
|
"lanzouc|" +
|
||||||
"lanzoui|" +
|
"lanzoui|" +
|
||||||
"lanzoug|" +
|
"lanzoug|" +
|
||||||
"lanzoum" +
|
"lanzoum)\\.com" +
|
||||||
")\\.com/(.+/)?(?<KEY>.+)"),
|
"|t-is\\.cn" +
|
||||||
"https://lanzoux.com/{shareKey}",
|
")/(?<KEY>.+)"),
|
||||||
|
"https://w1.lanzn.com/{shareKey}",
|
||||||
LzTool.class),
|
LzTool.class),
|
||||||
|
|
||||||
// https://www.feijix.com/s/
|
// https://www.feijix.com/s/
|
||||||
@@ -114,15 +115,15 @@ public enum PanDomainTemplate {
|
|||||||
"https://www.feijix.com/s/{shareKey}",
|
"https://www.feijix.com/s/{shareKey}",
|
||||||
FjTool.class),
|
FjTool.class),
|
||||||
|
|
||||||
// https://lecloud.lenovo.com/share/
|
// https://lecloud.lenovo.com/share/ https://lecloud.lenovo.com/mshare/
|
||||||
LE("联想乐云",
|
LE("联想乐云",
|
||||||
compile("https://lecloud?\\.lenovo\\.com/share/(?<KEY>.+)"),
|
compile("https://lecloud\\.lenovo\\.com/m?share/(?<KEY>.+)"),
|
||||||
"https://lecloud.lenovo.com/share/{shareKey}",
|
"https://lecloud.lenovo.com/share/{shareKey}",
|
||||||
LeTool.class),
|
LeTool.class),
|
||||||
|
|
||||||
// https://v2.fangcloud.com/s/
|
// https://v2.fangcloud.com/s/ https://v2.fangcloud.cn/h5/share/ (移动端H5落地页)
|
||||||
FC("亿方云",
|
FC("亿方云",
|
||||||
compile("https://v2\\.fangcloud\\.(com|cn)/(s|sharing)/(?<KEY>.+)"),
|
compile("https://v2\\.fangcloud\\.(com|cn)/(?:h5/)?(s|share|sharing)/(?<KEY>.+)"),
|
||||||
"https://v2.fangcloud.com/s/{shareKey}",
|
"https://v2.fangcloud.com/s/{shareKey}",
|
||||||
"https://www.fangcloud.com/",
|
"https://www.fangcloud.com/",
|
||||||
FcTool.class),
|
FcTool.class),
|
||||||
@@ -143,9 +144,41 @@ public enum PanDomainTemplate {
|
|||||||
compile("https://qfile\\.qq\\.com/q/(?<KEY>.+)"),
|
compile("https://qfile\\.qq\\.com/q/(?<KEY>.+)"),
|
||||||
"https://qfile.qq.com/q/{shareKey}",
|
"https://qfile.qq.com/q/{shareKey}",
|
||||||
QQscTool.class),
|
QQscTool.class),
|
||||||
// https://f.ws59.cn/f/或者https://www.wenshushu.cn/f/
|
// https://f.ws59.cn/f/ 或者 https://www.wenshushu.cn/f/ 等多个镜像域名
|
||||||
|
/*
|
||||||
|
f.wsNN.cn (如 f.ws59.cn, f.ws28.cn 等)
|
||||||
|
www.wenshushu.cn
|
||||||
|
新增域名:
|
||||||
|
www.wenxiaozhan.net
|
||||||
|
www.wenxiaozhan.cn
|
||||||
|
www.wss.show
|
||||||
|
www.ws28.cn
|
||||||
|
www.wss.email
|
||||||
|
www.wss1.cn
|
||||||
|
www.ws59.cn
|
||||||
|
www.wss.cc
|
||||||
|
www.wss.pet
|
||||||
|
www.wss.ink
|
||||||
|
www.wenxiaozhan.com
|
||||||
|
www.wenshushu.com
|
||||||
|
www.wss.zone
|
||||||
|
*/
|
||||||
WS("文叔叔",
|
WS("文叔叔",
|
||||||
compile("https://(f\\.ws(\\d{2})\\.cn|www\\.wenshushu\\.cn)/f/(?<KEY>.+)"),
|
compile("https://(f\\.ws(\\d{2})\\.cn|" +
|
||||||
|
"www\\.wenxiaozhan\\.net|" +
|
||||||
|
"www\\.wenxiaozhan\\.cn|" +
|
||||||
|
"www\\.wss\\.show|" +
|
||||||
|
"www\\.ws28\\.cn|" +
|
||||||
|
"www\\.wss\\.email|" +
|
||||||
|
"www\\.wss1\\.cn|" +
|
||||||
|
"www\\.ws59\\.cn|" +
|
||||||
|
"www\\.wss\\.cc|" +
|
||||||
|
"www\\.wss\\.pet|" +
|
||||||
|
"www\\.wss\\.ink|" +
|
||||||
|
"www\\.wenxiaozhan\\.com|" +
|
||||||
|
"www\\.wenshushu\\.com|" +
|
||||||
|
"www\\.wss\\.zone|" +
|
||||||
|
"www\\.wenshushu\\.cn)/f/(?<KEY>.+)"),
|
||||||
"https://www.wenshushu.cn/f/{shareKey}",
|
"https://www.wenshushu.cn/f/{shareKey}",
|
||||||
WsTool.class),
|
WsTool.class),
|
||||||
// https://www.123pan.com/s/
|
// https://www.123pan.com/s/
|
||||||
@@ -175,7 +208,7 @@ public enum PanDomainTemplate {
|
|||||||
123795.com
|
123795.com
|
||||||
*/
|
*/
|
||||||
YE("123网盘",
|
YE("123网盘",
|
||||||
compile("https://www\\.(" +
|
compile("https://(?:[a-zA-Z\\d-]+\\.)*(" +
|
||||||
"123254\\.com|" +
|
"123254\\.com|" +
|
||||||
"123957\\.com|" +
|
"123957\\.com|" +
|
||||||
"123295\\.com|" +
|
"123295\\.com|" +
|
||||||
@@ -199,7 +232,7 @@ public enum PanDomainTemplate {
|
|||||||
"123635\\.com|" +
|
"123635\\.com|" +
|
||||||
"123242\\.com|" +
|
"123242\\.com|" +
|
||||||
"123795\\.com" +
|
"123795\\.com" +
|
||||||
")/s/(?<KEY>.+)(.html)?"),
|
")/(?:(?:s|123pan)/|(?:[^/?#]+/)+)?(?<KEY>[a-zA-Z0-9]+-[a-zA-Z0-9]+|[a-zA-Z0-9_-]+)(?:\\.html)?(?:[?#].*)?"),
|
||||||
"https://www.123pan.com/s/{shareKey}",
|
"https://www.123pan.com/s/{shareKey}",
|
||||||
Ye2Tool.class),
|
Ye2Tool.class),
|
||||||
// https://www.ecpan.cn/web/#/yunpanProxy?path=%2F%23%2Fdrive%2Foutside&data={code}&isShare=1
|
// https://www.ecpan.cn/web/#/yunpanProxy?path=%2F%23%2Fdrive%2Foutside&data={code}&isShare=1
|
||||||
@@ -210,19 +243,20 @@ public enum PanDomainTemplate {
|
|||||||
EcTool.class),
|
EcTool.class),
|
||||||
// https://cowtransfer.com/s/
|
// https://cowtransfer.com/s/
|
||||||
COW("奶牛快传",
|
COW("奶牛快传",
|
||||||
compile("https://(.*)cowtransfer\\.com/s/(?<KEY>.+)"),
|
compile("https://(?:[a-zA-Z\\d-]+\\.)?cowtransfer\\.com/s/(?<KEY>.+)"),
|
||||||
"https://cowtransfer.com/s/{shareKey}",
|
"https://cowtransfer.com/s/{shareKey}",
|
||||||
CowTool.class),
|
CowTool.class),
|
||||||
CT("城通网盘",
|
CT("城通网盘",
|
||||||
compile("https://(?:[a-zA-Z\\d-]+\\.)?(ctfile|545c|u062|ghpym|474b)\\.com/f(ile)?/" +
|
compile("https?://(?:[a-zA-Z\\d-]+\\.)?(ctfile|545c|u062|ghpym|474b)\\.com/f(ile)?/" +
|
||||||
"(?<KEY>[0-9a-zA-Z_-]+)(\\?p=(?<PWD>\\w+))?"),
|
"(?<KEY>[0-9a-zA-Z_-]+)/?(?:\\?(?:(?:[^#&]*&)*p=(?<PWD>\\w+)(?:&[^#]*)?|[^#]*))?"),
|
||||||
"https://474b.com/file/{shareKey}",
|
"https://ctfile.com/file/{shareKey}",
|
||||||
|
CtTool.class),
|
||||||
|
// https://url94.ctfile.com/d/64115194-164803691-48508c?p=7609&d=164803691&fk=decb36
|
||||||
|
CTD("城通网盘-目录",
|
||||||
|
compile("https?://(?:[a-zA-Z\\d-]+\\.)?(ctfile|545c|u062|ghpym|474b)\\.com/d/" +
|
||||||
|
"(?<KEY>[0-9a-zA-Z_-]+)/?(?:\\?(?:(?:[^#&]*&)*p=(?<PWD>\\w+)(?:&[^#]*)?|[^#]*))?"),
|
||||||
|
"https://ctfile.com/d/{shareKey}",
|
||||||
CtTool.class),
|
CtTool.class),
|
||||||
// https://xxx.118pan.com/bxxx
|
|
||||||
P118("118网盘",
|
|
||||||
compile("https://(?:[a-zA-Z\\d-]+\\.)?118pan\\.com/b(?<KEY>.+)"),
|
|
||||||
"https://qaiu.118pan.com/b{shareKey}",
|
|
||||||
P118Tool.class),
|
|
||||||
// https://www.vyuyun.com/s/QMa6ie?password=I4KG7H
|
// https://www.vyuyun.com/s/QMa6ie?password=I4KG7H
|
||||||
// https://www.vyuyun.com/s/QMa6ie/file?password=I4KG7H
|
// https://www.vyuyun.com/s/QMa6ie/file?password=I4KG7H
|
||||||
PVYY("微雨云存储",
|
PVYY("微雨云存储",
|
||||||
@@ -238,7 +272,7 @@ public enum PanDomainTemplate {
|
|||||||
PodTool.class),
|
PodTool.class),
|
||||||
// 404网盘 https://drive.google.com/file/d/xxx/view?usp=sharing
|
// 404网盘 https://drive.google.com/file/d/xxx/view?usp=sharing
|
||||||
PGD("GoogleDrive",
|
PGD("GoogleDrive",
|
||||||
compile("https://drive\\.google\\.com/file/d/(?<KEY>.+)/view(\\?usp=(sharing|drive_link))?"),
|
compile("https://(?:[a-zA-Z\\d-]+\\.)?drive\\.google\\.com/file/d/(?<KEY>.+)/view(\\?usp=(sharing|drive_link))?"),
|
||||||
"https://drive.google.com/file/d/{shareKey}/view?usp=sharing",
|
"https://drive.google.com/file/d/{shareKey}/view?usp=sharing",
|
||||||
PgdTool.class),
|
PgdTool.class),
|
||||||
// iCloud https://www.icloud.com.cn/iclouddrive/xxx#fonts
|
// iCloud https://www.icloud.com.cn/iclouddrive/xxx#fonts
|
||||||
@@ -248,11 +282,11 @@ public enum PanDomainTemplate {
|
|||||||
PicTool.class),
|
PicTool.class),
|
||||||
// https://www.dropbox.com/scl/fi/cwnbms1yn8u6rcatzyta7/emqx-5.0.26-el7-amd64.tar.gz?rlkey=3uoi4bxz5mv93jmlaws0nlol1&e=8&st=fe0lclc2&dl=0
|
// https://www.dropbox.com/scl/fi/cwnbms1yn8u6rcatzyta7/emqx-5.0.26-el7-amd64.tar.gz?rlkey=3uoi4bxz5mv93jmlaws0nlol1&e=8&st=fe0lclc2&dl=0
|
||||||
PDB("dropbox",
|
PDB("dropbox",
|
||||||
compile("https://www.dropbox.com/scl/fi/(?<KEY>\\w+)/.+?rlkey=(?<PWD>\\w+).*"),
|
compile("https://www\\.dropbox\\.com/scl/fi/(?<KEY>\\w+)/.+?rlkey=(?<PWD>\\w+).*"),
|
||||||
"https://www.dropbox.com/scl/fi/{shareKey}/?rlkey={pwd}&dl=0",
|
"https://www.dropbox.com/scl/fi/{shareKey}/?rlkey={pwd}&dl=0",
|
||||||
PdbTool.class),
|
PdbTool.class),
|
||||||
P115("115网盘",
|
P115("115网盘",
|
||||||
compile("https://(115|anxia).com/s/(?<KEY>\\w+)(\\?password=(?<PWD>\\w+))?([&#].*)?"),
|
compile("https://(115|anxia)\\.com/s/(?<KEY>\\w+)(\\?password=(?<PWD>\\w+))?([&#].*)?"),
|
||||||
"https://115.com/s/{shareKey}?password={pwd}",
|
"https://115.com/s/{shareKey}?password={pwd}",
|
||||||
P115Tool.class),
|
P115Tool.class),
|
||||||
// 链接:https://www.yunpan.com/surl_yD7wz4VgU9v(提取码:fc70)
|
// 链接:https://www.yunpan.com/surl_yD7wz4VgU9v(提取码:fc70)
|
||||||
@@ -263,7 +297,7 @@ public enum PanDomainTemplate {
|
|||||||
|
|
||||||
// https://pan-yz.cldisk.com/external/m/file/953658049102462976
|
// https://pan-yz.cldisk.com/external/m/file/953658049102462976
|
||||||
Pcx("超星云盘(需要referer头)",
|
Pcx("超星云盘(需要referer头)",
|
||||||
compile("https://pan-yz\\.cldisk\\.com/external/m/file/(?<KEY>\\w+)"),
|
compile("https://pan-yz\\.(chaoxing\\.com|cldisk\\.com)/external/m/file/(?<KEY>\\w+)(\\?.*)?"),
|
||||||
"https://pan-yz.cldisk.com/external/m/file/{shareKey}",
|
"https://pan-yz.cldisk.com/external/m/file/{shareKey}",
|
||||||
PcxTool.class),
|
PcxTool.class),
|
||||||
// WPS:分享格式:https://www.kdocs.cn/l/ck0azivLlDi3 ;API格式:https://www.kdocs.cn/api/office/file/{shareKey}/download
|
// WPS:分享格式:https://www.kdocs.cn/l/ck0azivLlDi3 ;API格式:https://www.kdocs.cn/api/office/file/{shareKey}/download
|
||||||
@@ -272,6 +306,27 @@ public enum PanDomainTemplate {
|
|||||||
compile("https://(?:[a-zA-Z\\d-]+\\.)?kdocs\\.cn/l/(?<KEY>.+)"),
|
compile("https://(?:[a-zA-Z\\d-]+\\.)?kdocs\\.cn/l/(?<KEY>.+)"),
|
||||||
"https://www.kdocs.cn/l/{shareKey}",
|
"https://www.kdocs.cn/l/{shareKey}",
|
||||||
PwpsTool.class),
|
PwpsTool.class),
|
||||||
|
|
||||||
|
// https://fast.uc.cn/s/33197dd53ace4
|
||||||
|
// https://drive.uc.cn/s/e623b6da278e4?public=1#/list/share
|
||||||
|
UC("UC网盘",
|
||||||
|
compile("https://(fast|drive)\\.uc\\.cn/s/(?<KEY>\\w+)(\\?public=\\d+)?([&#].*)?"),
|
||||||
|
"https://drive.uc.cn/s/{shareKey}",
|
||||||
|
UcTool.class),
|
||||||
|
// https://pan.quark.cn/s/6a325cdaec58
|
||||||
|
QK("夸克网盘",
|
||||||
|
compile("https://pan\\.quark\\.cn/s/(?<KEY>\\w+)([&#].*)?"),
|
||||||
|
"https://pan.quark.cn/s/{shareKey}",
|
||||||
|
QkTool.class),
|
||||||
|
|
||||||
|
// https://xxx.feishu.cn/file/VnCxbt35KoowKoxldO3c3C7VnMc
|
||||||
|
// https://xxx.feishu.cn/drive/folder/RQSKf8EQ4l7dMedqzHucpMbancg
|
||||||
|
FS("飞书云盘",
|
||||||
|
compile("https://[^.]+\\.feishu\\.cn/(?:file|drive/folder)/(?<KEY>[A-Za-z0-9_-]+)(\\?.*)?"),
|
||||||
|
"https://feishu.cn/file/{shareKey}",
|
||||||
|
"https://www.feishu.cn/",
|
||||||
|
FsTool.class),
|
||||||
|
|
||||||
// =====================音乐类解析 分享链接标志->MxxS (单歌曲/普通音质)==========================
|
// =====================音乐类解析 分享链接标志->MxxS (单歌曲/普通音质)==========================
|
||||||
// http://163cn.tv/xxx
|
// http://163cn.tv/xxx
|
||||||
MNES("网易云音乐分享",
|
MNES("网易云音乐分享",
|
||||||
@@ -280,7 +335,7 @@ public enum PanDomainTemplate {
|
|||||||
MnesTool.class),
|
MnesTool.class),
|
||||||
// https://music.163.com/#/song?id=xxx
|
// https://music.163.com/#/song?id=xxx
|
||||||
MNE("网易云音乐歌曲详情",
|
MNE("网易云音乐歌曲详情",
|
||||||
compile("https://(y.)?music\\.163\\.com/(#|m/)?song\\?id=(?<KEY>.+)(&.*)?"),
|
compile("https://(y\\.)?music\\.163\\.com/(?:#/|m/)?song\\?id=(?<KEY>.+)(&.*)?"),
|
||||||
"https://music.163.com/#/song?id={shareKey}",
|
"https://music.163.com/#/song?id={shareKey}",
|
||||||
MnesTool.MneTool.class),
|
MnesTool.MneTool.class),
|
||||||
// https://c6.y.qq.com/base/fcgi-bin/u?__=xxx
|
// https://c6.y.qq.com/base/fcgi-bin/u?__=xxx
|
||||||
@@ -301,7 +356,7 @@ public enum PanDomainTemplate {
|
|||||||
MkgsTool.class),
|
MkgsTool.class),
|
||||||
// https://www.kugou.com/share/2bi8Fe9CSV3.html?id=2bi8Fe9CSV3#6ed9gna4"
|
// https://www.kugou.com/share/2bi8Fe9CSV3.html?id=2bi8Fe9CSV3#6ed9gna4"
|
||||||
MKGS2("酷狗音乐分享2",
|
MKGS2("酷狗音乐分享2",
|
||||||
compile("https://(?:[a-zA-Z\\d-]+\\.)?kugou\\.com/share/(?<KEY>.+).html.*"),
|
compile("https://(?:[a-zA-Z\\d-]+\\.)?kugou\\.com/share/(?<KEY>.+)\\.html.*"),
|
||||||
"https://www.kugou.com/share/{shareKey}.html",
|
"https://www.kugou.com/share/{shareKey}.html",
|
||||||
MkgsTool.Mkgs2Tool.class),
|
MkgsTool.Mkgs2Tool.class),
|
||||||
// https://www.kugou.com/mixsong/2bi8Fe9CSV3
|
// https://www.kugou.com/mixsong/2bi8Fe9CSV3
|
||||||
@@ -322,6 +377,16 @@ public enum PanDomainTemplate {
|
|||||||
MmgTool.class),
|
MmgTool.class),
|
||||||
// =====================私有盘解析==========================
|
// =====================私有盘解析==========================
|
||||||
|
|
||||||
|
// 永硕E盘空间分享:https://qaiu.ysepan.com/ (空间名即 shareKey,密码为空间访问密码)
|
||||||
|
// 主域名 ysepan.com / ys168.com;备用 cccpan.com / ysupan.com / uupan.net / ysok.net
|
||||||
|
YS("永硕E盘",
|
||||||
|
compile("https?://(?!(?:www|zy|ht|api|c\\d+|ys-[a-zA-Z0-9]+)\\.)(?<KEY>[a-zA-Z\\d-]+)\\."
|
||||||
|
+ "(?:ysepan\\.com|ys168\\.com|cccpan\\.com|ysupan\\.com|uupan\\.net|ysok\\.net)"
|
||||||
|
+ "/?(?:\\?.*)?"),
|
||||||
|
"https://{shareKey}.ysepan.com/",
|
||||||
|
"https://www.ysepan.com/",
|
||||||
|
YsTool.class),
|
||||||
|
|
||||||
// Cloudreve自定义域名解析, 解析器CeTool兜底策略, 即任意域名如果匹配不到对应的规则, 则由CeTool统一处理,
|
// Cloudreve自定义域名解析, 解析器CeTool兜底策略, 即任意域名如果匹配不到对应的规则, 则由CeTool统一处理,
|
||||||
// 如果不属于Cloudreve盘 则调用下一个自定义域名解析器, 若都处理不了则抛出异常, 这种匹配模式类似责任链
|
// 如果不属于Cloudreve盘 则调用下一个自定义域名解析器, 若都处理不了则抛出异常, 这种匹配模式类似责任链
|
||||||
// http(s)://pan.huang1111.cn/s/xxx
|
// http(s)://pan.huang1111.cn/s/xxx
|
||||||
|
|||||||
@@ -4,12 +4,13 @@ import cn.qaiu.entity.ShareLinkInfo;
|
|||||||
import cn.qaiu.parser.custom.CustomParserConfig;
|
import cn.qaiu.parser.custom.CustomParserConfig;
|
||||||
import cn.qaiu.parser.custom.CustomParserRegistry;
|
import cn.qaiu.parser.custom.CustomParserRegistry;
|
||||||
import cn.qaiu.parser.customjs.JsParserExecutor;
|
import cn.qaiu.parser.customjs.JsParserExecutor;
|
||||||
import cn.qaiu.parser.custompy.PyParserExecutor;
|
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.EnumSet;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
|
|
||||||
import static cn.qaiu.parser.PanDomainTemplate.KEY;
|
import static cn.qaiu.parser.PanDomainTemplate.KEY;
|
||||||
@@ -24,6 +25,9 @@ import static cn.qaiu.parser.PanDomainTemplate.PWD;
|
|||||||
* Create at 2024/9/15 14:10
|
* Create at 2024/9/15 14:10
|
||||||
*/
|
*/
|
||||||
public class ParserCreate {
|
public class ParserCreate {
|
||||||
|
private static final Set<PanDomainTemplate> GENERIC_BUILT_IN_PARSERS =
|
||||||
|
EnumSet.of(PanDomainTemplate.CE, PanDomainTemplate.KD, PanDomainTemplate.OTHER);
|
||||||
|
|
||||||
private final PanDomainTemplate panDomainTemplate;
|
private final PanDomainTemplate panDomainTemplate;
|
||||||
private final ShareLinkInfo shareLinkInfo;
|
private final ShareLinkInfo shareLinkInfo;
|
||||||
|
|
||||||
@@ -82,7 +86,7 @@ public class ParserCreate {
|
|||||||
if (shareKey != null) {
|
if (shareKey != null) {
|
||||||
shareLinkInfo.setShareKey(shareKey);
|
shareLinkInfo.setShareKey(shareKey);
|
||||||
}
|
}
|
||||||
} catch (Exception ignored) {}
|
} catch (IllegalStateException | IllegalArgumentException ignored) {}
|
||||||
|
|
||||||
// 提取密码
|
// 提取密码
|
||||||
try {
|
try {
|
||||||
@@ -90,7 +94,7 @@ public class ParserCreate {
|
|||||||
if (StringUtils.isNotEmpty(pwd)) {
|
if (StringUtils.isNotEmpty(pwd)) {
|
||||||
shareLinkInfo.setSharePassword(pwd);
|
shareLinkInfo.setSharePassword(pwd);
|
||||||
}
|
}
|
||||||
} catch (Exception ignored) {}
|
} catch (IllegalStateException | IllegalArgumentException ignored) {}
|
||||||
|
|
||||||
// 设置标准URL
|
// 设置标准URL
|
||||||
if (customParserConfig.getStandardUrlTemplate() != null) {
|
if (customParserConfig.getStandardUrlTemplate() != null) {
|
||||||
@@ -133,12 +137,12 @@ public class ParserCreate {
|
|||||||
if (StringUtils.isNotEmpty(pwd)) {
|
if (StringUtils.isNotEmpty(pwd)) {
|
||||||
shareLinkInfo.setSharePassword(pwd);
|
shareLinkInfo.setSharePassword(pwd);
|
||||||
}
|
}
|
||||||
standardUrl = standardUrl.replace("{pwd}", pwd);
|
standardUrl = standardUrl.replace("{pwd}", StringUtils.defaultString(pwd));
|
||||||
} catch (Exception ignored) {}
|
} catch (IllegalStateException | IllegalArgumentException ignored) {}
|
||||||
|
|
||||||
shareLinkInfo.setShareUrl(shareUrl);
|
shareLinkInfo.setShareUrl(shareUrl);
|
||||||
shareLinkInfo.setShareKey(shareKey);
|
shareLinkInfo.setShareKey(shareKey);
|
||||||
if (!(panDomainTemplate.ordinal() >= PanDomainTemplate.CE.ordinal())) {
|
if (!isGenericBuiltInParser(panDomainTemplate)) {
|
||||||
shareLinkInfo.setStandardUrl(standardUrl);
|
shareLinkInfo.setStandardUrl(standardUrl);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
@@ -156,9 +160,6 @@ public class ParserCreate {
|
|||||||
// 检查是否为JavaScript解析器
|
// 检查是否为JavaScript解析器
|
||||||
if (customParserConfig.isJsParser()) {
|
if (customParserConfig.isJsParser()) {
|
||||||
return new JsParserExecutor(shareLinkInfo, customParserConfig);
|
return new JsParserExecutor(shareLinkInfo, customParserConfig);
|
||||||
} else if (customParserConfig.isPyParser()) {
|
|
||||||
// Python解析器
|
|
||||||
return new PyParserExecutor(shareLinkInfo, customParserConfig);
|
|
||||||
} else {
|
} else {
|
||||||
// Java实现的解析器
|
// Java实现的解析器
|
||||||
try {
|
try {
|
||||||
@@ -201,7 +202,7 @@ public class ParserCreate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 内置解析器处理
|
// 内置解析器处理
|
||||||
if (panDomainTemplate.ordinal() >= PanDomainTemplate.CE.ordinal()) {
|
if (isGenericBuiltInParser(panDomainTemplate)) {
|
||||||
// 处理Cloudreve(ce)类: pan.huang1111.cn_s_wDz5TK _ -> /
|
// 处理Cloudreve(ce)类: pan.huang1111.cn_s_wDz5TK _ -> /
|
||||||
String[] s = shareKey.split("_");
|
String[] s = shareKey.split("_");
|
||||||
String standardUrl = "https://" + String.join("/", s);
|
String standardUrl = "https://" + String.join("/", s);
|
||||||
@@ -251,9 +252,19 @@ public class ParserCreate {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据分享链接获取PanDomainTemplate实例(优先匹配自定义解析器)
|
// 根据分享链接获取PanDomainTemplate实例
|
||||||
public synchronized static ParserCreate fromShareUrl(String shareUrl) {
|
public synchronized static ParserCreate fromShareUrl(String shareUrl) {
|
||||||
// 优先查找支持正则匹配的自定义解析器
|
if (StringUtils.isBlank(shareUrl)) {
|
||||||
|
throw new IllegalArgumentException("shareUrl不能为空");
|
||||||
|
}
|
||||||
|
shareUrl = shareUrl.trim();
|
||||||
|
|
||||||
|
ParserCreate builtInParser = fromBuiltInShareUrl(shareUrl, false);
|
||||||
|
if (builtInParser != null) {
|
||||||
|
return builtInParser;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 明确内置解析器未命中时,再查找支持正则匹配的自定义解析器
|
||||||
for (CustomParserConfig customConfig : CustomParserRegistry.getAll().values()) {
|
for (CustomParserConfig customConfig : CustomParserRegistry.getAll().values()) {
|
||||||
if (customConfig.supportsFromShareUrl()) {
|
if (customConfig.supportsFromShareUrl()) {
|
||||||
Matcher matcher = customConfig.getMatchPattern().matcher(shareUrl);
|
Matcher matcher = customConfig.getMatchPattern().matcher(shareUrl);
|
||||||
@@ -270,14 +281,14 @@ public class ParserCreate {
|
|||||||
if (shareKey != null) {
|
if (shareKey != null) {
|
||||||
shareLinkInfo.setShareKey(shareKey);
|
shareLinkInfo.setShareKey(shareKey);
|
||||||
}
|
}
|
||||||
} catch (Exception ignored) {}
|
} catch (IllegalStateException | IllegalArgumentException ignored) {}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String password = matcher.group("PWD");
|
String password = matcher.group("PWD");
|
||||||
if (password != null) {
|
if (password != null) {
|
||||||
shareLinkInfo.setSharePassword(password);
|
shareLinkInfo.setSharePassword(password);
|
||||||
}
|
}
|
||||||
} catch (Exception ignored) {}
|
} catch (IllegalStateException | IllegalArgumentException ignored) {}
|
||||||
|
|
||||||
// 设置标准URL(如果有模板)
|
// 设置标准URL(如果有模板)
|
||||||
if (customConfig.getStandardUrlTemplate() != null) {
|
if (customConfig.getStandardUrlTemplate() != null) {
|
||||||
@@ -300,21 +311,34 @@ public class ParserCreate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查找内置解析器
|
// 最后再走 Cloudreve/可道云/其他网盘这类泛化兜底,避免抢走自定义解析器
|
||||||
|
builtInParser = fromBuiltInShareUrl(shareUrl, true);
|
||||||
|
if (builtInParser != null) {
|
||||||
|
return builtInParser;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new IllegalArgumentException("Unsupported share URL");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ParserCreate fromBuiltInShareUrl(String shareUrl, boolean genericOnly) {
|
||||||
for (PanDomainTemplate panDomainTemplate : PanDomainTemplate.values()) {
|
for (PanDomainTemplate panDomainTemplate : PanDomainTemplate.values()) {
|
||||||
|
boolean genericParser = isGenericBuiltInParser(panDomainTemplate);
|
||||||
|
if (genericOnly != genericParser) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (panDomainTemplate.getPattern().matcher(shareUrl).matches()) {
|
if (panDomainTemplate.getPattern().matcher(shareUrl).matches()) {
|
||||||
ShareLinkInfo shareLinkInfo = ShareLinkInfo.newBuilder()
|
ShareLinkInfo shareLinkInfo = ShareLinkInfo.newBuilder()
|
||||||
.type(panDomainTemplate.name().toLowerCase())
|
.type(panDomainTemplate.name().toLowerCase())
|
||||||
.panName(panDomainTemplate.getDisplayName())
|
.panName(panDomainTemplate.getDisplayName())
|
||||||
.shareUrl(shareUrl).build();
|
.shareUrl(shareUrl).build();
|
||||||
if (panDomainTemplate.ordinal() >= PanDomainTemplate.CE.ordinal()) {
|
if (isGenericBuiltInParser(panDomainTemplate)) {
|
||||||
shareLinkInfo.setStandardUrl(shareUrl);
|
shareLinkInfo.setStandardUrl(shareUrl);
|
||||||
}
|
}
|
||||||
ParserCreate parserCreate = new ParserCreate(panDomainTemplate, shareLinkInfo);
|
ParserCreate parserCreate = new ParserCreate(panDomainTemplate, shareLinkInfo);
|
||||||
return parserCreate.normalizeShareLink();
|
return parserCreate.normalizeShareLink();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new IllegalArgumentException("Unsupported share URL");
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据type获取枚举实例(优先查找自定义解析器)
|
// 根据type获取枚举实例(优先查找自定义解析器)
|
||||||
@@ -357,7 +381,7 @@ public class ParserCreate {
|
|||||||
// 自定义解析器处理
|
// 自定义解析器处理
|
||||||
if (isCustomParser) {
|
if (isCustomParser) {
|
||||||
path = this.shareLinkInfo.getType() + "/" + this.shareLinkInfo.getShareKey();
|
path = this.shareLinkInfo.getType() + "/" + this.shareLinkInfo.getShareKey();
|
||||||
} else if (panDomainTemplate.ordinal() >= PanDomainTemplate.CE.ordinal()) {
|
} else if (isGenericBuiltInParser(panDomainTemplate)) {
|
||||||
// 处理Cloudreve(ce)类: pan.huang1111.cn_s_wDz5TK _ -> /
|
// 处理Cloudreve(ce)类: pan.huang1111.cn_s_wDz5TK _ -> /
|
||||||
path = this.shareLinkInfo.getType() + "/"
|
path = this.shareLinkInfo.getType() + "/"
|
||||||
+ this.shareLinkInfo.getShareUrl()
|
+ this.shareLinkInfo.getShareUrl()
|
||||||
@@ -386,6 +410,10 @@ public class ParserCreate {
|
|||||||
return customParserConfig;
|
return customParserConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean isGenericBuiltInParser(PanDomainTemplate panDomainTemplate) {
|
||||||
|
return GENERIC_BUILT_IN_PARSERS.contains(panDomainTemplate);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取内置解析器模板(仅当isCustomParser为false时有效)
|
* 获取内置解析器模板(仅当isCustomParser为false时有效)
|
||||||
* @return 内置解析器模板,如果是自定义解析器则返回null
|
* @return 内置解析器模板,如果是自定义解析器则返回null
|
||||||
|
|||||||
@@ -0,0 +1,270 @@
|
|||||||
|
package cn.qaiu.parser;
|
||||||
|
|
||||||
|
import io.vertx.core.json.JsonArray;
|
||||||
|
import io.vertx.core.json.JsonObject;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 账号密码登录撞上验证码/风控时的用户提示。
|
||||||
|
* 只识别失败形态并引导改用 Authorization,不实现验证码求解、扫码或重试。
|
||||||
|
*/
|
||||||
|
public final class PasswordLoginRisk {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 123 云盘(ye)账号密码登录被验证码/风控拦住时返回给调用方的固定说明。
|
||||||
|
* 上游 code/message 会另附在这句话后面,便于和“密码错误”区分。
|
||||||
|
*/
|
||||||
|
public static final String AUTHORIZATION_HINT =
|
||||||
|
"账号密码登录触发了验证码/风控,已停止用户名密码登录(不会尝试验证码)。"
|
||||||
|
+ "请改为配置 Authorization(浏览器登录后的 Bearer token / JWT,"
|
||||||
|
+ "例如 localStorage 的 authorToken、请求头 authorization 或 Cookie 的 sso-token)。"
|
||||||
|
+ "静态配置写在 app-dev.yml 的 auths.ye.authorization(与 auths.ye.token 等价);"
|
||||||
|
+ "临时请求使用 authType=authorization 或 authType=accesstoken,token 填该令牌(不要重复带 Bearer 前缀)。"
|
||||||
|
+ "说明见 parser/doc/auth-param/。";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上游文案命中这些片段时视为验证码/风控,而不是单纯的密码错误。
|
||||||
|
* 依据公开客户端(AList/OpenList)实际展示过的 123 登录失败文案,以及登录页上的滑块/人机验证提示。
|
||||||
|
*/
|
||||||
|
private static final String[] RISK_KEYWORDS = {
|
||||||
|
"验证码",
|
||||||
|
"风控",
|
||||||
|
"安全风险",
|
||||||
|
"安全验证",
|
||||||
|
"安全校验",
|
||||||
|
"滑块",
|
||||||
|
"滑动验证",
|
||||||
|
"拼图验证",
|
||||||
|
"短信验证",
|
||||||
|
"微信登录",
|
||||||
|
"微信进行登录",
|
||||||
|
"境外登录",
|
||||||
|
"需要验证",
|
||||||
|
"请验证",
|
||||||
|
"请你验证",
|
||||||
|
"进行验证",
|
||||||
|
"完成验证",
|
||||||
|
"二次验证",
|
||||||
|
"人机验证",
|
||||||
|
"操作频繁",
|
||||||
|
"请求过于频繁",
|
||||||
|
"请求频繁",
|
||||||
|
"captcha",
|
||||||
|
"recaptcha",
|
||||||
|
"risk control"
|
||||||
|
};
|
||||||
|
|
||||||
|
private PasswordLoginRisk() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isLoginSuccess(JsonObject json) {
|
||||||
|
if (json == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Integer code = readCode(json);
|
||||||
|
return code != null && code == 200 && StringUtils.isNotBlank(readToken(json));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean indicatesCaptchaOrRisk(JsonObject json) {
|
||||||
|
if (json == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (hasTruthyRiskField(json) || hasTruthyRiskField(dataObject(json))) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return textIndicatesCaptchaOrRisk(collectMessageText(json));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean textIndicatesCaptchaOrRisk(String text) {
|
||||||
|
if (StringUtils.isBlank(text)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
String lower = text.toLowerCase(Locale.ROOT);
|
||||||
|
for (String keyword : RISK_KEYWORDS) {
|
||||||
|
if (lower.contains(keyword.toLowerCase(Locale.ROOT))) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 非成功登录响应的用户可见说明。验证码/风控走 Authorization 引导,其余保持“登录失败: 上游文案”。
|
||||||
|
*/
|
||||||
|
public static String describeLoginFailure(JsonObject json) {
|
||||||
|
if (json == null) {
|
||||||
|
return "登录响应格式异常";
|
||||||
|
}
|
||||||
|
if (indicatesCaptchaOrRisk(json)) {
|
||||||
|
return AUTHORIZATION_HINT + " 上游返回:" + upstreamSummary(json);
|
||||||
|
}
|
||||||
|
Integer code = readCode(json);
|
||||||
|
if (code != null && code == 200) {
|
||||||
|
return "未获取到token";
|
||||||
|
}
|
||||||
|
String message = firstMessage(json);
|
||||||
|
if (StringUtils.isBlank(message)) {
|
||||||
|
return "登录失败";
|
||||||
|
}
|
||||||
|
return "登录失败: " + message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String nonJsonLoginFailure(String body) {
|
||||||
|
if (textIndicatesCaptchaOrRisk(body)) {
|
||||||
|
return AUTHORIZATION_HINT + " 上游返回:" + truncate(body, 300);
|
||||||
|
}
|
||||||
|
return "登录响应格式异常: " + body;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String collectMessageText(JsonObject json) {
|
||||||
|
StringBuilder text = new StringBuilder();
|
||||||
|
appendMessage(text, json);
|
||||||
|
JsonObject data = dataObject(json);
|
||||||
|
if (data != null) {
|
||||||
|
appendMessage(text, data);
|
||||||
|
}
|
||||||
|
return text.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void appendMessage(StringBuilder text, JsonObject json) {
|
||||||
|
append(text, json.getString("message"));
|
||||||
|
append(text, json.getString("msg"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void append(StringBuilder text, String value) {
|
||||||
|
if (StringUtils.isNotBlank(value)) {
|
||||||
|
if (text.length() > 0) {
|
||||||
|
text.append(' ');
|
||||||
|
}
|
||||||
|
text.append(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean hasTruthyRiskField(JsonObject obj) {
|
||||||
|
if (obj == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (String key : obj.fieldNames()) {
|
||||||
|
if (!isRiskFieldName(key)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (isTruthyRiskValue(obj.getValue(key))) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isRiskFieldName(String key) {
|
||||||
|
String lower = key.toLowerCase(Locale.ROOT);
|
||||||
|
return lower.contains("captcha")
|
||||||
|
|| lower.contains("vcode")
|
||||||
|
|| lower.contains("risk")
|
||||||
|
|| "needverify".equals(lower)
|
||||||
|
|| "need_verify".equals(lower)
|
||||||
|
|| "slideverify".equals(lower)
|
||||||
|
|| "verifycode".equals(lower)
|
||||||
|
|| "verify_code".equals(lower);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isTruthyRiskValue(Object value) {
|
||||||
|
if (value == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (value instanceof Boolean) {
|
||||||
|
return (Boolean) value;
|
||||||
|
}
|
||||||
|
if (value instanceof Number) {
|
||||||
|
return ((Number) value).doubleValue() != 0d;
|
||||||
|
}
|
||||||
|
if (value instanceof CharSequence) {
|
||||||
|
String text = value.toString().trim();
|
||||||
|
if (text.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
String lower = text.toLowerCase(Locale.ROOT);
|
||||||
|
return !"false".equals(lower) && !"0".equals(lower) && !"null".equals(lower) && !"none".equals(lower);
|
||||||
|
}
|
||||||
|
if (value instanceof JsonObject) {
|
||||||
|
return !((JsonObject) value).isEmpty();
|
||||||
|
}
|
||||||
|
if (value instanceof JsonArray) {
|
||||||
|
return !((JsonArray) value).isEmpty();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String upstreamSummary(JsonObject json) {
|
||||||
|
StringBuilder summary = new StringBuilder();
|
||||||
|
Integer code = readCode(json);
|
||||||
|
if (code != null) {
|
||||||
|
summary.append("code=").append(code);
|
||||||
|
}
|
||||||
|
String message = firstMessage(json);
|
||||||
|
if (StringUtils.isNotBlank(message)) {
|
||||||
|
if (summary.length() > 0) {
|
||||||
|
summary.append(", ");
|
||||||
|
}
|
||||||
|
summary.append("message=").append(message);
|
||||||
|
}
|
||||||
|
if (summary.length() == 0) {
|
||||||
|
summary.append(json.encode());
|
||||||
|
}
|
||||||
|
return truncate(summary.toString(), 300);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String firstMessage(JsonObject json) {
|
||||||
|
String message = json.getString("message");
|
||||||
|
if (StringUtils.isBlank(message)) {
|
||||||
|
message = json.getString("msg");
|
||||||
|
}
|
||||||
|
if (StringUtils.isBlank(message)) {
|
||||||
|
JsonObject data = dataObject(json);
|
||||||
|
if (data != null) {
|
||||||
|
message = data.getString("message");
|
||||||
|
if (StringUtils.isBlank(message)) {
|
||||||
|
message = data.getString("msg");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
static String readToken(JsonObject json) {
|
||||||
|
JsonObject data = dataObject(json);
|
||||||
|
if (data == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Object token = data.getValue("token");
|
||||||
|
return token == null ? null : token.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static JsonObject dataObject(JsonObject json) {
|
||||||
|
Object data = json.getValue("data");
|
||||||
|
return data instanceof JsonObject ? (JsonObject) data : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Integer readCode(JsonObject json) {
|
||||||
|
Object code = json.getValue("code");
|
||||||
|
if (code instanceof Number) {
|
||||||
|
return ((Number) code).intValue();
|
||||||
|
}
|
||||||
|
if (code instanceof String && StringUtils.isNumeric(((String) code).trim())) {
|
||||||
|
return Integer.parseInt(((String) code).trim());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String truncate(String value, int max) {
|
||||||
|
if (value == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
String trimmed = value.trim();
|
||||||
|
if (trimmed.length() <= max) {
|
||||||
|
return trimmed;
|
||||||
|
}
|
||||||
|
return trimmed.substring(0, max) + "...";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,149 @@
|
|||||||
|
package cn.qaiu.parser;
|
||||||
|
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析器 Token/Cookie 缓存 — 支持多账号隔离。
|
||||||
|
* <p>
|
||||||
|
* 以 (diskType + "#" + accountKey) 作为缓存 key,不同账号的 token 互不覆盖。
|
||||||
|
* accountKey 优先使用 _configId,其次使用 username、cookie 前16位等可区分标识。
|
||||||
|
* </p>
|
||||||
|
*/
|
||||||
|
public final class TokenCache {
|
||||||
|
|
||||||
|
private TokenCache() {}
|
||||||
|
|
||||||
|
/** token 缓存 */
|
||||||
|
private static final ConcurrentHashMap<String, String> tokenMap = new ConcurrentHashMap<>();
|
||||||
|
/** 过期时间缓存(毫秒时间戳) */
|
||||||
|
private static final ConcurrentHashMap<String, Long> expireMap = new ConcurrentHashMap<>();
|
||||||
|
/** 同一 key 下的额外字符串缓存(如 userId) */
|
||||||
|
private static final ConcurrentHashMap<String, String> extraMap = new ConcurrentHashMap<>();
|
||||||
|
/** 布尔标记缓存(如 authFlag) */
|
||||||
|
private static final ConcurrentHashMap<String, Boolean> flagMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
// ============ key 构造 ============
|
||||||
|
|
||||||
|
public static String key(String diskType, String accountKey) {
|
||||||
|
return diskType + "#" + (accountKey == null ? "_default" : accountKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============ token ============
|
||||||
|
|
||||||
|
public static String getToken(String cacheKey) {
|
||||||
|
return tokenMap.get(cacheKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void putToken(String cacheKey, String token) {
|
||||||
|
if (token == null) {
|
||||||
|
tokenMap.remove(cacheKey);
|
||||||
|
} else {
|
||||||
|
tokenMap.put(cacheKey, token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============ expire ============
|
||||||
|
|
||||||
|
public static long getExpire(String cacheKey) {
|
||||||
|
return expireMap.getOrDefault(cacheKey, 0L);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void putExpire(String cacheKey, long expireMs) {
|
||||||
|
expireMap.put(cacheKey, expireMs);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isExpired(String cacheKey) {
|
||||||
|
long exp = getExpire(cacheKey);
|
||||||
|
return exp <= 0 || System.currentTimeMillis() > exp;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============ extra (userId 等) ============
|
||||||
|
|
||||||
|
public static String getExtra(String cacheKey) {
|
||||||
|
return extraMap.get(cacheKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void putExtra(String cacheKey, String value) {
|
||||||
|
if (value == null) {
|
||||||
|
extraMap.remove(cacheKey);
|
||||||
|
} else {
|
||||||
|
extraMap.put(cacheKey, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============ flag (authFlag 等) ============
|
||||||
|
|
||||||
|
public static boolean getFlag(String cacheKey, boolean defaultValue) {
|
||||||
|
return flagMap.getOrDefault(cacheKey, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void putFlag(String cacheKey, boolean value) {
|
||||||
|
flagMap.put(cacheKey, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============ 清除 ============
|
||||||
|
|
||||||
|
public static void remove(String cacheKey) {
|
||||||
|
tokenMap.remove(cacheKey);
|
||||||
|
expireMap.remove(cacheKey);
|
||||||
|
extraMap.remove(cacheKey);
|
||||||
|
flagMap.remove(cacheKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清除指定网盘类型的所有缓存(精准清除,不影响其他网盘类型)
|
||||||
|
*/
|
||||||
|
public static void removeByDiskType(String diskType) {
|
||||||
|
String prefix = diskType + "#";
|
||||||
|
tokenMap.keySet().removeIf(k -> k.startsWith(prefix));
|
||||||
|
expireMap.keySet().removeIf(k -> k.startsWith(prefix));
|
||||||
|
extraMap.keySet().removeIf(k -> k.startsWith(prefix));
|
||||||
|
flagMap.keySet().removeIf(k -> k.startsWith(prefix));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void clear() {
|
||||||
|
tokenMap.clear();
|
||||||
|
expireMap.clear();
|
||||||
|
extraMap.clear();
|
||||||
|
flagMap.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============ Token 持久化队列 ============
|
||||||
|
|
||||||
|
/** 待持久化的 cachedToken 数据 (cacheKey -> [token, expireMs]) */
|
||||||
|
private static final ConcurrentHashMap<String, String[]> persistQueue = new ConcurrentHashMap<>();
|
||||||
|
/** 待回写的凭据更新 (cacheKey -> newCredential),如 PaliTool refresh_token 轮换 */
|
||||||
|
private static final ConcurrentHashMap<String, String> credentialUpdateQueue = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析器登录成功后,将 token 加入持久化队列(下次 recordConfigUsage 回写 DB)
|
||||||
|
*/
|
||||||
|
public static void queueCachedTokenPersist(String cacheKey, String token, long expireMs) {
|
||||||
|
if (cacheKey != null && token != null) {
|
||||||
|
persistQueue.put(cacheKey, new String[]{token, String.valueOf(expireMs)});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 凭据本身被替换(如 PaliTool refresh_token 轮换),加入回写队列
|
||||||
|
*/
|
||||||
|
public static void queueCredentialUpdate(String cacheKey, String newCredential) {
|
||||||
|
if (cacheKey != null && newCredential != null) {
|
||||||
|
credentialUpdateQueue.put(cacheKey, newCredential);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消费持久化队列:返回 [token, expireMs] 并移除;无数据返回 null
|
||||||
|
*/
|
||||||
|
public static String[] pollCachedTokenPersist(String cacheKey) {
|
||||||
|
return cacheKey == null ? null : persistQueue.remove(cacheKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消费凭据更新队列:返回新凭据并移除;无数据返回 null
|
||||||
|
*/
|
||||||
|
public static String pollCredentialUpdate(String cacheKey) {
|
||||||
|
return cacheKey == null ? null : credentialUpdateQueue.remove(cacheKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,13 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 客户端下载链接生成器工厂类
|
* 客户端下载链接生成器工厂类
|
||||||
|
* <p>
|
||||||
|
* 支持的客户端类型:
|
||||||
|
* <ul>
|
||||||
|
* <li>CURL - cURL 命令,支持 Cookie</li>
|
||||||
|
* <li>ARIA2 - Aria2 命令,支持 Cookie</li>
|
||||||
|
* <li>THUNDER - 迅雷协议,不支持 Cookie</li>
|
||||||
|
* </ul>
|
||||||
*
|
*
|
||||||
* @author <a href="https://qaiu.top">QAIU</a>
|
* @author <a href="https://qaiu.top">QAIU</a>
|
||||||
* Create at 2025/01/21
|
* Create at 2025/01/21
|
||||||
@@ -25,16 +32,10 @@ public class ClientLinkGeneratorFactory {
|
|||||||
// 静态初始化块,注册默认的生成器
|
// 静态初始化块,注册默认的生成器
|
||||||
static {
|
static {
|
||||||
try {
|
try {
|
||||||
// 注册默认生成器 - 按指定顺序注册
|
// 注册默认生成器 - 只保留3种(按需求)
|
||||||
register(new Aria2LinkGenerator());
|
register(new CurlLinkGenerator()); // cURL 命令,支持 Cookie
|
||||||
register(new MotrixLinkGenerator());
|
register(new Aria2LinkGenerator()); // Aria2 命令,支持 Cookie
|
||||||
register(new BitCometLinkGenerator());
|
register(new ThunderLinkGenerator()); // 迅雷协议,不支持 Cookie
|
||||||
register(new ThunderLinkGenerator());
|
|
||||||
register(new WgetLinkGenerator());
|
|
||||||
register(new CurlLinkGenerator());
|
|
||||||
register(new IdmLinkGenerator());
|
|
||||||
register(new FdmLinkGenerator());
|
|
||||||
register(new PowerShellLinkGenerator());
|
|
||||||
|
|
||||||
log.info("客户端链接生成器工厂初始化完成,已注册 {} 个生成器", generators.size());
|
log.info("客户端链接生成器工厂初始化完成,已注册 {} 个生成器", generators.size());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
@@ -2,27 +2,32 @@ package cn.qaiu.parser.clientlink;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 客户端下载工具类型枚举
|
* 客户端下载工具类型枚举
|
||||||
|
* <p>
|
||||||
|
* 支持的客户端类型:
|
||||||
|
* <ul>
|
||||||
|
* <li>CURL - cURL 命令行工具,支持 Cookie</li>
|
||||||
|
* <li>ARIA2 - 多线程下载器,支持 Cookie</li>
|
||||||
|
* <li>THUNDER - 迅雷下载器,不支持 Cookie(使用迅雷协议)</li>
|
||||||
|
* </ul>
|
||||||
*
|
*
|
||||||
* @author <a href="https://qaiu.top">QAIU</a>
|
* @author <a href="https://qaiu.top">QAIU</a>
|
||||||
* Create at 2025/01/21
|
* Create at 2025/01/21
|
||||||
*/
|
*/
|
||||||
public enum ClientLinkType {
|
public enum ClientLinkType {
|
||||||
ARIA2("aria2", "Aria2"),
|
CURL("curl", "cURL 命令", true, "命令行下载工具,支持Cookie"),
|
||||||
MOTRIX("motrix", "Motrix"),
|
ARIA2("aria2", "Aria2", true, "多线程下载器,支持Cookie"),
|
||||||
BITCOMET("bitcomet", "比特彗星"),
|
THUNDER("thunder", "迅雷", false, "迅雷下载器,不支持Cookie");
|
||||||
THUNDER("thunder", "迅雷"),
|
|
||||||
WGET("wget", "wget 命令"),
|
|
||||||
CURL("curl", "cURL 命令"),
|
|
||||||
IDM("idm", "IDM"),
|
|
||||||
FDM("fdm", "Free Download Manager"),
|
|
||||||
POWERSHELL("powershell", "PowerShell");
|
|
||||||
|
|
||||||
private final String code;
|
private final String code;
|
||||||
private final String displayName;
|
private final String displayName;
|
||||||
|
private final boolean supportsCookie;
|
||||||
|
private final String description;
|
||||||
|
|
||||||
ClientLinkType(String code, String displayName) {
|
ClientLinkType(String code, String displayName, boolean supportsCookie, String description) {
|
||||||
this.code = code;
|
this.code = code;
|
||||||
this.displayName = displayName;
|
this.displayName = displayName;
|
||||||
|
this.supportsCookie = supportsCookie;
|
||||||
|
this.description = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCode() {
|
public String getCode() {
|
||||||
@@ -33,6 +38,14 @@ public enum ClientLinkType {
|
|||||||
return displayName;
|
return displayName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isSupportsCookie() {
|
||||||
|
return supportsCookie;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return displayName;
|
return displayName;
|
||||||
|
|||||||
@@ -7,6 +7,13 @@ import java.util.Map;
|
|||||||
/**
|
/**
|
||||||
* 客户端下载链接生成工具类
|
* 客户端下载链接生成工具类
|
||||||
* 提供便捷的静态方法来生成各种客户端下载链接
|
* 提供便捷的静态方法来生成各种客户端下载链接
|
||||||
|
* <p>
|
||||||
|
* 支持的客户端类型:
|
||||||
|
* <ul>
|
||||||
|
* <li>CURL - cURL 命令,支持 Cookie</li>
|
||||||
|
* <li>ARIA2 - Aria2 命令,支持 Cookie</li>
|
||||||
|
* <li>THUNDER - 迅雷协议,不支持 Cookie</li>
|
||||||
|
* </ul>
|
||||||
*
|
*
|
||||||
* @author <a href="https://qaiu.top">QAIU</a>
|
* @author <a href="https://qaiu.top">QAIU</a>
|
||||||
* Create at 2025/01/21
|
* Create at 2025/01/21
|
||||||
@@ -35,7 +42,7 @@ public class ClientLinkUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成 curl 命令
|
* 生成 curl 命令(支持 Cookie)
|
||||||
*
|
*
|
||||||
* @param info ShareLinkInfo 对象
|
* @param info ShareLinkInfo 对象
|
||||||
* @return curl 命令字符串
|
* @return curl 命令字符串
|
||||||
@@ -45,17 +52,7 @@ public class ClientLinkUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成 wget 命令
|
* 生成 aria2 命令(支持 Cookie)
|
||||||
*
|
|
||||||
* @param info ShareLinkInfo 对象
|
|
||||||
* @return wget 命令字符串
|
|
||||||
*/
|
|
||||||
public static String generateWgetCommand(ShareLinkInfo info) {
|
|
||||||
return generateClientLink(info, ClientLinkType.WGET);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成 aria2 命令
|
|
||||||
*
|
*
|
||||||
* @param info ShareLinkInfo 对象
|
* @param info ShareLinkInfo 对象
|
||||||
* @return aria2 命令字符串
|
* @return aria2 命令字符串
|
||||||
@@ -65,7 +62,7 @@ public class ClientLinkUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成迅雷链接
|
* 生成迅雷链接(不支持 Cookie)
|
||||||
*
|
*
|
||||||
* @param info ShareLinkInfo 对象
|
* @param info ShareLinkInfo 对象
|
||||||
* @return 迅雷协议链接
|
* @return 迅雷协议链接
|
||||||
@@ -74,56 +71,6 @@ public class ClientLinkUtils {
|
|||||||
return generateClientLink(info, ClientLinkType.THUNDER);
|
return generateClientLink(info, ClientLinkType.THUNDER);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成 IDM 链接
|
|
||||||
*
|
|
||||||
* @param info ShareLinkInfo 对象
|
|
||||||
* @return IDM 协议链接
|
|
||||||
*/
|
|
||||||
public static String generateIdmLink(ShareLinkInfo info) {
|
|
||||||
return generateClientLink(info, ClientLinkType.IDM);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成比特彗星链接
|
|
||||||
*
|
|
||||||
* @param info ShareLinkInfo 对象
|
|
||||||
* @return 比特彗星协议链接
|
|
||||||
*/
|
|
||||||
public static String generateBitCometLink(ShareLinkInfo info) {
|
|
||||||
return generateClientLink(info, ClientLinkType.BITCOMET);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成 Motrix 导入格式
|
|
||||||
*
|
|
||||||
* @param info ShareLinkInfo 对象
|
|
||||||
* @return Motrix JSON 格式字符串
|
|
||||||
*/
|
|
||||||
public static String generateMotrixFormat(ShareLinkInfo info) {
|
|
||||||
return generateClientLink(info, ClientLinkType.MOTRIX);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成 FDM 导入格式
|
|
||||||
*
|
|
||||||
* @param info ShareLinkInfo 对象
|
|
||||||
* @return FDM 格式字符串
|
|
||||||
*/
|
|
||||||
public static String generateFdmFormat(ShareLinkInfo info) {
|
|
||||||
return generateClientLink(info, ClientLinkType.FDM);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 生成 PowerShell 命令
|
|
||||||
*
|
|
||||||
* @param info ShareLinkInfo 对象
|
|
||||||
* @return PowerShell 命令字符串
|
|
||||||
*/
|
|
||||||
public static String generatePowerShellCommand(ShareLinkInfo info) {
|
|
||||||
return generateClientLink(info, ClientLinkType.POWERSHELL);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检查 ShareLinkInfo 是否包含有效的下载元数据
|
* 检查 ShareLinkInfo 是否包含有效的下载元数据
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -41,6 +41,8 @@ public class Aria2LinkGenerator implements ClientLinkGenerator {
|
|||||||
parts.add("--continue"); // 支持断点续传
|
parts.add("--continue"); // 支持断点续传
|
||||||
parts.add("--max-tries=3"); // 最大重试次数
|
parts.add("--max-tries=3"); // 最大重试次数
|
||||||
parts.add("--retry-wait=5"); // 重试等待时间
|
parts.add("--retry-wait=5"); // 重试等待时间
|
||||||
|
parts.add("-s 8"); // 分成8片段下载
|
||||||
|
parts.add("-x 8"); // 每个服务器使用8个连接
|
||||||
|
|
||||||
// 添加URL
|
// 添加URL
|
||||||
parts.add("\"" + meta.getUrl() + "\"");
|
parts.add("\"" + meta.getUrl() + "\"");
|
||||||
|
|||||||
@@ -1,69 +0,0 @@
|
|||||||
package cn.qaiu.parser.clientlink.impl;
|
|
||||||
|
|
||||||
import cn.qaiu.parser.clientlink.ClientLinkGenerator;
|
|
||||||
import cn.qaiu.parser.clientlink.ClientLinkType;
|
|
||||||
import cn.qaiu.parser.clientlink.DownloadLinkMeta;
|
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.Base64;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 比特彗星协议链接生成器
|
|
||||||
*
|
|
||||||
* @author <a href="https://qaiu.top">QAIU</a>
|
|
||||||
* Create at 2025/01/21
|
|
||||||
*/
|
|
||||||
public class BitCometLinkGenerator implements ClientLinkGenerator {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String generate(DownloadLinkMeta meta) {
|
|
||||||
if (!supports(meta)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
// 比特彗星支持 HTTP 下载,格式类似 IDM
|
|
||||||
String encodedUrl = Base64.getEncoder().encodeToString(
|
|
||||||
meta.getUrl().getBytes(StandardCharsets.UTF_8)
|
|
||||||
);
|
|
||||||
|
|
||||||
StringBuilder link = new StringBuilder("bitcomet:///?url=").append(encodedUrl);
|
|
||||||
|
|
||||||
// 添加请求头
|
|
||||||
if (meta.getHeaders() != null && !meta.getHeaders().isEmpty()) {
|
|
||||||
StringBuilder headerStr = new StringBuilder();
|
|
||||||
for (Map.Entry<String, String> entry : meta.getHeaders().entrySet()) {
|
|
||||||
if (headerStr.length() > 0) {
|
|
||||||
headerStr.append("\\r\\n");
|
|
||||||
}
|
|
||||||
headerStr.append(entry.getKey()).append(": ").append(entry.getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
String encodedHeaders = Base64.getEncoder().encodeToString(
|
|
||||||
headerStr.toString().getBytes(StandardCharsets.UTF_8)
|
|
||||||
);
|
|
||||||
link.append("&header=").append(encodedHeaders);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 添加文件名
|
|
||||||
if (meta.getFileName() != null && !meta.getFileName().trim().isEmpty()) {
|
|
||||||
String encodedFileName = Base64.getEncoder().encodeToString(
|
|
||||||
meta.getFileName().getBytes(StandardCharsets.UTF_8)
|
|
||||||
);
|
|
||||||
link.append("&filename=").append(encodedFileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
return link.toString();
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
// 如果编码失败,返回简单的URL
|
|
||||||
return "bitcomet:///?url=" + meta.getUrl();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ClientLinkType getType() {
|
|
||||||
return ClientLinkType.BITCOMET;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
package cn.qaiu.parser.clientlink.impl;
|
|
||||||
|
|
||||||
import cn.qaiu.parser.clientlink.ClientLinkGenerator;
|
|
||||||
import cn.qaiu.parser.clientlink.ClientLinkType;
|
|
||||||
import cn.qaiu.parser.clientlink.DownloadLinkMeta;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Free Download Manager 导入格式生成器
|
|
||||||
*
|
|
||||||
* @author <a href="https://qaiu.top">QAIU</a>
|
|
||||||
* Create at 2025/01/21
|
|
||||||
*/
|
|
||||||
public class FdmLinkGenerator implements ClientLinkGenerator {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String generate(DownloadLinkMeta meta) {
|
|
||||||
if (!supports(meta)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// FDM 支持简单的文本格式导入
|
|
||||||
StringBuilder result = new StringBuilder();
|
|
||||||
result.append("URL=").append(meta.getUrl()).append("\n");
|
|
||||||
|
|
||||||
// 添加文件名
|
|
||||||
if (meta.getFileName() != null && !meta.getFileName().trim().isEmpty()) {
|
|
||||||
result.append("Filename=").append(meta.getFileName()).append("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 添加请求头
|
|
||||||
if (meta.getHeaders() != null && !meta.getHeaders().isEmpty()) {
|
|
||||||
result.append("Headers=");
|
|
||||||
boolean first = true;
|
|
||||||
for (Map.Entry<String, String> entry : meta.getHeaders().entrySet()) {
|
|
||||||
if (!first) {
|
|
||||||
result.append("; ");
|
|
||||||
}
|
|
||||||
result.append(entry.getKey()).append(": ").append(entry.getValue());
|
|
||||||
first = false;
|
|
||||||
}
|
|
||||||
result.append("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
result.append("Referer=").append(meta.getReferer() != null ? meta.getReferer() : "").append("\n");
|
|
||||||
result.append("User-Agent=").append(meta.getUserAgent() != null ? meta.getUserAgent() : "").append("\n");
|
|
||||||
|
|
||||||
return result.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ClientLinkType getType() {
|
|
||||||
return ClientLinkType.FDM;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
package cn.qaiu.parser.clientlink.impl;
|
|
||||||
|
|
||||||
import cn.qaiu.parser.clientlink.ClientLinkGenerator;
|
|
||||||
import cn.qaiu.parser.clientlink.ClientLinkType;
|
|
||||||
import cn.qaiu.parser.clientlink.DownloadLinkMeta;
|
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.Base64;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* IDM 协议链接生成器
|
|
||||||
*
|
|
||||||
* @author <a href="https://qaiu.top">QAIU</a>
|
|
||||||
* Create at 2025/01/21
|
|
||||||
*/
|
|
||||||
public class IdmLinkGenerator implements ClientLinkGenerator {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String generate(DownloadLinkMeta meta) {
|
|
||||||
if (!supports(meta)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
// 对URL进行Base64编码
|
|
||||||
String encodedUrl = Base64.getEncoder().encodeToString(
|
|
||||||
meta.getUrl().getBytes(StandardCharsets.UTF_8)
|
|
||||||
);
|
|
||||||
|
|
||||||
StringBuilder link = new StringBuilder("idm:///?url=").append(encodedUrl);
|
|
||||||
|
|
||||||
// 添加请求头
|
|
||||||
if (meta.getHeaders() != null && !meta.getHeaders().isEmpty()) {
|
|
||||||
StringBuilder headerStr = new StringBuilder();
|
|
||||||
for (Map.Entry<String, String> entry : meta.getHeaders().entrySet()) {
|
|
||||||
if (headerStr.length() > 0) {
|
|
||||||
headerStr.append("\\r\\n");
|
|
||||||
}
|
|
||||||
headerStr.append(entry.getKey()).append(": ").append(entry.getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
String encodedHeaders = Base64.getEncoder().encodeToString(
|
|
||||||
headerStr.toString().getBytes(StandardCharsets.UTF_8)
|
|
||||||
);
|
|
||||||
link.append("&header=").append(encodedHeaders);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 添加文件名
|
|
||||||
if (meta.getFileName() != null && !meta.getFileName().trim().isEmpty()) {
|
|
||||||
String encodedFileName = Base64.getEncoder().encodeToString(
|
|
||||||
meta.getFileName().getBytes(StandardCharsets.UTF_8)
|
|
||||||
);
|
|
||||||
link.append("&filename=").append(encodedFileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
return link.toString();
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
// 如果编码失败,返回简单的URL
|
|
||||||
return "idm:///?url=" + meta.getUrl();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ClientLinkType getType() {
|
|
||||||
return ClientLinkType.IDM;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
package cn.qaiu.parser.clientlink.impl;
|
|
||||||
|
|
||||||
import cn.qaiu.parser.clientlink.ClientLinkGenerator;
|
|
||||||
import cn.qaiu.parser.clientlink.ClientLinkType;
|
|
||||||
import cn.qaiu.parser.clientlink.DownloadLinkMeta;
|
|
||||||
import io.vertx.core.json.JsonObject;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Motrix 导入格式生成器
|
|
||||||
*
|
|
||||||
* @author <a href="https://qaiu.top">QAIU</a>
|
|
||||||
* Create at 2025/01/21
|
|
||||||
*/
|
|
||||||
public class MotrixLinkGenerator implements ClientLinkGenerator {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String generate(DownloadLinkMeta meta) {
|
|
||||||
if (!supports(meta)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 使用 Vert.x JsonObject 构建 JSON
|
|
||||||
JsonObject taskJson = new JsonObject();
|
|
||||||
taskJson.put("url", meta.getUrl());
|
|
||||||
|
|
||||||
// 添加文件名
|
|
||||||
if (meta.getFileName() != null && !meta.getFileName().trim().isEmpty()) {
|
|
||||||
taskJson.put("filename", meta.getFileName());
|
|
||||||
}
|
|
||||||
|
|
||||||
// 添加请求头
|
|
||||||
if (meta.getHeaders() != null && !meta.getHeaders().isEmpty()) {
|
|
||||||
JsonObject headersJson = new JsonObject();
|
|
||||||
for (Map.Entry<String, String> entry : meta.getHeaders().entrySet()) {
|
|
||||||
headersJson.put(entry.getKey(), entry.getValue());
|
|
||||||
}
|
|
||||||
taskJson.put("headers", headersJson);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 设置输出文件名
|
|
||||||
String outputFile = meta.getFileName() != null ? meta.getFileName() : "";
|
|
||||||
taskJson.put("out", outputFile);
|
|
||||||
|
|
||||||
return taskJson.encodePrettily();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ClientLinkType getType() {
|
|
||||||
return ClientLinkType.MOTRIX;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,98 +0,0 @@
|
|||||||
package cn.qaiu.parser.clientlink.impl;
|
|
||||||
|
|
||||||
import cn.qaiu.parser.clientlink.ClientLinkGenerator;
|
|
||||||
import cn.qaiu.parser.clientlink.ClientLinkType;
|
|
||||||
import cn.qaiu.parser.clientlink.DownloadLinkMeta;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* PowerShell 命令生成器
|
|
||||||
*
|
|
||||||
* @author <a href="https://qaiu.top">QAIU</a>
|
|
||||||
* Create at 2025/01/21
|
|
||||||
*/
|
|
||||||
public class PowerShellLinkGenerator implements ClientLinkGenerator {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String generate(DownloadLinkMeta meta) {
|
|
||||||
if (!supports(meta)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
List<String> lines = new ArrayList<>();
|
|
||||||
|
|
||||||
// 创建 WebRequestSession
|
|
||||||
lines.add("$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession");
|
|
||||||
|
|
||||||
// 设置 User-Agent(如果存在)
|
|
||||||
String userAgent = meta.getUserAgent();
|
|
||||||
if (userAgent == null && meta.getHeaders() != null) {
|
|
||||||
userAgent = meta.getHeaders().get("User-Agent");
|
|
||||||
}
|
|
||||||
if (userAgent != null && !userAgent.trim().isEmpty()) {
|
|
||||||
lines.add("$session.UserAgent = \"" + escapePowerShellString(userAgent) + "\"");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 构建 Invoke-WebRequest 命令
|
|
||||||
List<String> invokeParams = new ArrayList<>();
|
|
||||||
invokeParams.add("Invoke-WebRequest");
|
|
||||||
invokeParams.add("-UseBasicParsing");
|
|
||||||
invokeParams.add("-Uri \"" + escapePowerShellString(meta.getUrl()) + "\"");
|
|
||||||
|
|
||||||
// 添加 WebSession
|
|
||||||
invokeParams.add("-WebSession $session");
|
|
||||||
|
|
||||||
// 添加请求头
|
|
||||||
if (meta.getHeaders() != null && !meta.getHeaders().isEmpty()) {
|
|
||||||
List<String> headerLines = new ArrayList<>();
|
|
||||||
headerLines.add("-Headers @{");
|
|
||||||
|
|
||||||
boolean first = true;
|
|
||||||
for (Map.Entry<String, String> entry : meta.getHeaders().entrySet()) {
|
|
||||||
if (!first) {
|
|
||||||
headerLines.add("");
|
|
||||||
}
|
|
||||||
headerLines.add(" \"" + escapePowerShellString(entry.getKey()) + "\"=\"" +
|
|
||||||
escapePowerShellString(entry.getValue()) + "\"");
|
|
||||||
first = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
headerLines.add("}");
|
|
||||||
|
|
||||||
// 将头部参数添加到主命令中
|
|
||||||
invokeParams.add(String.join("`\n", headerLines));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 设置输出文件(如果指定了文件名)
|
|
||||||
if (meta.getFileName() != null && !meta.getFileName().trim().isEmpty()) {
|
|
||||||
invokeParams.add("-OutFile \"" + escapePowerShellString(meta.getFileName()) + "\"");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 将所有参数连接起来
|
|
||||||
String invokeCommand = String.join(" `\n", invokeParams);
|
|
||||||
lines.add(invokeCommand);
|
|
||||||
|
|
||||||
return String.join("\n", lines);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 转义 PowerShell 字符串中的特殊字符
|
|
||||||
*/
|
|
||||||
private String escapePowerShellString(String str) {
|
|
||||||
if (str == null) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
return str.replace("`", "``")
|
|
||||||
.replace("\"", "`\"")
|
|
||||||
.replace("$", "`$");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ClientLinkType getType() {
|
|
||||||
return ClientLinkType.POWERSHELL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
package cn.qaiu.parser.clientlink.impl;
|
package cn.qaiu.parser.clientlink.impl;
|
||||||
|
|
||||||
|
import cn.qaiu.util.Base64Encoder;
|
||||||
import cn.qaiu.parser.clientlink.ClientLinkGenerator;
|
import cn.qaiu.parser.clientlink.ClientLinkGenerator;
|
||||||
import cn.qaiu.parser.clientlink.ClientLinkType;
|
import cn.qaiu.parser.clientlink.ClientLinkType;
|
||||||
import cn.qaiu.parser.clientlink.DownloadLinkMeta;
|
import cn.qaiu.parser.clientlink.DownloadLinkMeta;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Base64;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 迅雷协议链接生成器
|
* 迅雷协议链接生成器
|
||||||
@@ -27,7 +27,7 @@ public class ThunderLinkGenerator implements ClientLinkGenerator {
|
|||||||
String thunderUrl = "AA" + originalUrl + "ZZ";
|
String thunderUrl = "AA" + originalUrl + "ZZ";
|
||||||
|
|
||||||
// Base64编码
|
// Base64编码
|
||||||
String encodedUrl = Base64.getEncoder().encodeToString(
|
String encodedUrl = Base64Encoder.encode(
|
||||||
thunderUrl.getBytes(StandardCharsets.UTF_8)
|
thunderUrl.getBytes(StandardCharsets.UTF_8)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -1,51 +0,0 @@
|
|||||||
package cn.qaiu.parser.clientlink.impl;
|
|
||||||
|
|
||||||
import cn.qaiu.parser.clientlink.ClientLinkGenerator;
|
|
||||||
import cn.qaiu.parser.clientlink.ClientLinkType;
|
|
||||||
import cn.qaiu.parser.clientlink.DownloadLinkMeta;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* wget 命令生成器
|
|
||||||
*
|
|
||||||
* @author <a href="https://qaiu.top">QAIU</a>
|
|
||||||
* Create at 2025/01/21
|
|
||||||
*/
|
|
||||||
public class WgetLinkGenerator implements ClientLinkGenerator {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String generate(DownloadLinkMeta meta) {
|
|
||||||
if (!supports(meta)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
List<String> parts = new ArrayList<>();
|
|
||||||
parts.add("wget");
|
|
||||||
|
|
||||||
// 添加请求头
|
|
||||||
if (meta.getHeaders() != null && !meta.getHeaders().isEmpty()) {
|
|
||||||
for (Map.Entry<String, String> entry : meta.getHeaders().entrySet()) {
|
|
||||||
parts.add("--header=\"" + entry.getKey() + ": " + entry.getValue() + "\"");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 设置输出文件名
|
|
||||||
if (meta.getFileName() != null && !meta.getFileName().trim().isEmpty()) {
|
|
||||||
parts.add("-O");
|
|
||||||
parts.add("\"" + meta.getFileName() + "\"");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 添加URL
|
|
||||||
parts.add("\"" + meta.getUrl() + "\"");
|
|
||||||
|
|
||||||
return String.join(" \\\n ", parts);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ClientLinkType getType() {
|
|
||||||
return ClientLinkType.WGET;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -53,26 +53,11 @@ public class CustomParserConfig {
|
|||||||
*/
|
*/
|
||||||
private final String jsCode;
|
private final String jsCode;
|
||||||
|
|
||||||
/**
|
|
||||||
* Python代码(用于Python解析器)
|
|
||||||
*/
|
|
||||||
private final String pyCode;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否为JavaScript解析器
|
* 是否为JavaScript解析器
|
||||||
*/
|
*/
|
||||||
private final boolean isJsParser;
|
private final boolean isJsParser;
|
||||||
|
|
||||||
/**
|
|
||||||
* 是否为Python解析器
|
|
||||||
*/
|
|
||||||
private final boolean isPyParser;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 脚本语言类型:javascript, python
|
|
||||||
*/
|
|
||||||
private final String language;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 元数据信息(从脚本注释中解析)
|
* 元数据信息(从脚本注释中解析)
|
||||||
*/
|
*/
|
||||||
@@ -86,10 +71,7 @@ public class CustomParserConfig {
|
|||||||
this.panDomain = builder.panDomain;
|
this.panDomain = builder.panDomain;
|
||||||
this.matchPattern = builder.matchPattern;
|
this.matchPattern = builder.matchPattern;
|
||||||
this.jsCode = builder.jsCode;
|
this.jsCode = builder.jsCode;
|
||||||
this.pyCode = builder.pyCode;
|
|
||||||
this.isJsParser = builder.isJsParser;
|
this.isJsParser = builder.isJsParser;
|
||||||
this.isPyParser = builder.isPyParser;
|
|
||||||
this.language = builder.language;
|
|
||||||
this.metadata = builder.metadata;
|
this.metadata = builder.metadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,22 +103,10 @@ public class CustomParserConfig {
|
|||||||
return jsCode;
|
return jsCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPyCode() {
|
|
||||||
return pyCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isJsParser() {
|
public boolean isJsParser() {
|
||||||
return isJsParser;
|
return isJsParser;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isPyParser() {
|
|
||||||
return isPyParser;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getLanguage() {
|
|
||||||
return language;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<String, String> getMetadata() {
|
public Map<String, String> getMetadata() {
|
||||||
return metadata;
|
return metadata;
|
||||||
}
|
}
|
||||||
@@ -164,10 +134,7 @@ public class CustomParserConfig {
|
|||||||
private String panDomain;
|
private String panDomain;
|
||||||
private Pattern matchPattern;
|
private Pattern matchPattern;
|
||||||
private String jsCode;
|
private String jsCode;
|
||||||
private String pyCode;
|
|
||||||
private boolean isJsParser;
|
private boolean isJsParser;
|
||||||
private boolean isPyParser;
|
|
||||||
private String language;
|
|
||||||
private Map<String, String> metadata;
|
private Map<String, String> metadata;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -244,45 +211,12 @@ public class CustomParserConfig {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置Python代码(用于Python解析器)
|
|
||||||
* @param pyCode Python代码
|
|
||||||
*/
|
|
||||||
public Builder pyCode(String pyCode) {
|
|
||||||
this.pyCode = pyCode;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置是否为JavaScript解析器
|
* 设置是否为JavaScript解析器
|
||||||
* @param isJsParser 是否为JavaScript解析器
|
* @param isJsParser 是否为JavaScript解析器
|
||||||
*/
|
*/
|
||||||
public Builder isJsParser(boolean isJsParser) {
|
public Builder isJsParser(boolean isJsParser) {
|
||||||
this.isJsParser = isJsParser;
|
this.isJsParser = isJsParser;
|
||||||
if (isJsParser) {
|
|
||||||
this.language = "javascript";
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置是否为Python解析器
|
|
||||||
* @param isPyParser 是否为Python解析器
|
|
||||||
*/
|
|
||||||
public Builder isPyParser(boolean isPyParser) {
|
|
||||||
this.isPyParser = isPyParser;
|
|
||||||
if (isPyParser) {
|
|
||||||
this.language = "python";
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 设置脚本语言类型
|
|
||||||
* @param language 语言类型:javascript, python
|
|
||||||
*/
|
|
||||||
public Builder language(String language) {
|
|
||||||
this.language = language;
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -312,11 +246,6 @@ public class CustomParserConfig {
|
|||||||
if (jsCode == null || jsCode.trim().isEmpty()) {
|
if (jsCode == null || jsCode.trim().isEmpty()) {
|
||||||
throw new IllegalArgumentException("JavaScript解析器的jsCode不能为空");
|
throw new IllegalArgumentException("JavaScript解析器的jsCode不能为空");
|
||||||
}
|
}
|
||||||
} else if (isPyParser) {
|
|
||||||
// 如果是Python解析器,验证pyCode
|
|
||||||
if (pyCode == null || pyCode.trim().isEmpty()) {
|
|
||||||
throw new IllegalArgumentException("Python解析器的pyCode不能为空");
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// 如果是Java解析器,验证toolClass
|
// 如果是Java解析器,验证toolClass
|
||||||
if (toolClass == null) {
|
if (toolClass == null) {
|
||||||
@@ -359,10 +288,7 @@ public class CustomParserConfig {
|
|||||||
", panDomain='" + panDomain + '\'' +
|
", panDomain='" + panDomain + '\'' +
|
||||||
", matchPattern=" + (matchPattern != null ? matchPattern.pattern() : "null") +
|
", matchPattern=" + (matchPattern != null ? matchPattern.pattern() : "null") +
|
||||||
", jsCode=" + (jsCode != null ? "[JavaScript代码]" : "null") +
|
", jsCode=" + (jsCode != null ? "[JavaScript代码]" : "null") +
|
||||||
", pyCode=" + (pyCode != null ? "[Python代码]" : "null") +
|
|
||||||
", isJsParser=" + isJsParser +
|
", isJsParser=" + isJsParser +
|
||||||
", isPyParser=" + isPyParser +
|
|
||||||
", language='" + language + '\'' +
|
|
||||||
", metadata=" + metadata +
|
", metadata=" + metadata +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,6 @@ import org.slf4j.LoggerFactory;
|
|||||||
import cn.qaiu.parser.PanDomainTemplate;
|
import cn.qaiu.parser.PanDomainTemplate;
|
||||||
import cn.qaiu.parser.customjs.JsScriptLoader;
|
import cn.qaiu.parser.customjs.JsScriptLoader;
|
||||||
import cn.qaiu.parser.customjs.JsScriptMetadataParser;
|
import cn.qaiu.parser.customjs.JsScriptMetadataParser;
|
||||||
import cn.qaiu.parser.custompy.PyScriptLoader;
|
|
||||||
import cn.qaiu.parser.custompy.PyScriptMetadataParser;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -23,6 +21,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
public class CustomParserRegistry {
|
public class CustomParserRegistry {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(CustomParserRegistry.class);
|
private static final Logger log = LoggerFactory.getLogger(CustomParserRegistry.class);
|
||||||
|
private static final int MAX_CUSTOM_PARSERS = Integer.getInteger("parser.custom.maxRegistrySize", 256);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 存储自定义解析器配置的Map,key为类型标识,value为配置对象
|
* 存储自定义解析器配置的Map,key为类型标识,value为配置对象
|
||||||
@@ -35,7 +34,7 @@ public class CustomParserRegistry {
|
|||||||
* @param config 解析器配置
|
* @param config 解析器配置
|
||||||
* @throws IllegalArgumentException 如果type已存在或与内置解析器冲突
|
* @throws IllegalArgumentException 如果type已存在或与内置解析器冲突
|
||||||
*/
|
*/
|
||||||
public static void register(CustomParserConfig config) {
|
public static synchronized void register(CustomParserConfig config) {
|
||||||
if (config == null) {
|
if (config == null) {
|
||||||
throw new IllegalArgumentException("config不能为空");
|
throw new IllegalArgumentException("config不能为空");
|
||||||
}
|
}
|
||||||
@@ -61,6 +60,11 @@ public class CustomParserRegistry {
|
|||||||
"类型标识 '" + type + "' 已被注册,请先注销或使用其他标识"
|
"类型标识 '" + type + "' 已被注册,请先注销或使用其他标识"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
if (CUSTOM_PARSERS.size() >= MAX_CUSTOM_PARSERS) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"自定义解析器数量已达到上限(" + MAX_CUSTOM_PARSERS + "个),请先注销不需要的解析器"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
CUSTOM_PARSERS.put(type, config);
|
CUSTOM_PARSERS.put(type, config);
|
||||||
log.info("注册自定义解析器成功: {} ({})", config.getDisplayName(), type);
|
log.info("注册自定义解析器成功: {} ({})", config.getDisplayName(), type);
|
||||||
@@ -84,24 +88,6 @@ public class CustomParserRegistry {
|
|||||||
register(config);
|
register(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 注册Python解析器
|
|
||||||
*
|
|
||||||
* @param config Python解析器配置
|
|
||||||
* @throws IllegalArgumentException 如果type已存在或与内置解析器冲突
|
|
||||||
*/
|
|
||||||
public static void registerPy(CustomParserConfig config) {
|
|
||||||
if (config == null) {
|
|
||||||
throw new IllegalArgumentException("config不能为空");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!config.isPyParser()) {
|
|
||||||
throw new IllegalArgumentException("config必须是Python解析器配置");
|
|
||||||
}
|
|
||||||
|
|
||||||
register(config);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从JavaScript代码字符串注册解析器
|
* 从JavaScript代码字符串注册解析器
|
||||||
*
|
*
|
||||||
@@ -159,63 +145,6 @@ public class CustomParserRegistry {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 从Python代码字符串注册解析器
|
|
||||||
*
|
|
||||||
* @param pyCode Python代码
|
|
||||||
* @throws IllegalArgumentException 如果解析失败
|
|
||||||
*/
|
|
||||||
public static void registerPyFromCode(String pyCode) {
|
|
||||||
if (pyCode == null || pyCode.trim().isEmpty()) {
|
|
||||||
throw new IllegalArgumentException("Python代码不能为空");
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
CustomParserConfig config = PyScriptMetadataParser.parseScript(pyCode);
|
|
||||||
registerPy(config);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new IllegalArgumentException("解析Python代码失败: " + e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 从文件注册Python解析器
|
|
||||||
*
|
|
||||||
* @param filePath 文件路径
|
|
||||||
* @throws IllegalArgumentException 如果文件不存在或解析失败
|
|
||||||
*/
|
|
||||||
public static void registerPyFromFile(String filePath) {
|
|
||||||
if (filePath == null || filePath.trim().isEmpty()) {
|
|
||||||
throw new IllegalArgumentException("文件路径不能为空");
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
CustomParserConfig config = PyScriptLoader.loadFromFile(filePath);
|
|
||||||
registerPy(config);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new IllegalArgumentException("从文件加载Python解析器失败: " + e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 从资源文件注册Python解析器
|
|
||||||
*
|
|
||||||
* @param resourcePath 资源路径
|
|
||||||
* @throws IllegalArgumentException 如果资源不存在或解析失败
|
|
||||||
*/
|
|
||||||
public static void registerPyFromResource(String resourcePath) {
|
|
||||||
if (resourcePath == null || resourcePath.trim().isEmpty()) {
|
|
||||||
throw new IllegalArgumentException("资源路径不能为空");
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
CustomParserConfig config = PyScriptLoader.loadFromResource(resourcePath);
|
|
||||||
registerPy(config);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new IllegalArgumentException("从资源加载Python解析器失败: " + e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自动加载所有JavaScript脚本
|
* 自动加载所有JavaScript脚本
|
||||||
*/
|
*/
|
||||||
@@ -242,47 +171,13 @@ public class CustomParserRegistry {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 自动加载所有Python脚本
|
|
||||||
*/
|
|
||||||
public static void autoLoadPyScripts() {
|
|
||||||
try {
|
|
||||||
List<CustomParserConfig> configs = PyScriptLoader.loadAllScripts();
|
|
||||||
int successCount = 0;
|
|
||||||
int failCount = 0;
|
|
||||||
|
|
||||||
for (CustomParserConfig config : configs) {
|
|
||||||
try {
|
|
||||||
registerPy(config);
|
|
||||||
successCount++;
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("加载Python脚本失败: {}", config.getType(), e);
|
|
||||||
failCount++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
log.info("自动加载Python脚本完成: 成功 {} 个,失败 {} 个", successCount, failCount);
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("自动加载Python脚本时发生异常", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 自动加载所有脚本(JavaScript和Python)
|
|
||||||
*/
|
|
||||||
public static void autoLoadAllScripts() {
|
|
||||||
autoLoadJsScripts();
|
|
||||||
autoLoadPyScripts();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注销自定义解析器
|
* 注销自定义解析器
|
||||||
*
|
*
|
||||||
* @param type 解析器类型标识
|
* @param type 解析器类型标识
|
||||||
* @return 是否注销成功
|
* @return 是否注销成功
|
||||||
*/
|
*/
|
||||||
public static boolean unregister(String type) {
|
public static synchronized boolean unregister(String type) {
|
||||||
if (type == null || type.trim().isEmpty()) {
|
if (type == null || type.trim().isEmpty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -324,7 +219,7 @@ public class CustomParserRegistry {
|
|||||||
/**
|
/**
|
||||||
* 清空所有自定义解析器
|
* 清空所有自定义解析器
|
||||||
*/
|
*/
|
||||||
public static void clear() {
|
public static synchronized void clear() {
|
||||||
CUSTOM_PARSERS.clear();
|
CUSTOM_PARSERS.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,19 +2,21 @@ package cn.qaiu.parser.customjs;
|
|||||||
|
|
||||||
import cn.qaiu.WebClientVertxInit;
|
import cn.qaiu.WebClientVertxInit;
|
||||||
import cn.qaiu.util.HttpResponseHelper;
|
import cn.qaiu.util.HttpResponseHelper;
|
||||||
import io.vertx.core.Future;
|
|
||||||
import io.vertx.core.MultiMap;
|
import io.vertx.core.MultiMap;
|
||||||
import io.vertx.core.Promise;
|
import io.vertx.core.Promise;
|
||||||
import io.vertx.core.buffer.Buffer;
|
import io.vertx.core.buffer.Buffer;
|
||||||
|
import io.vertx.core.http.HttpClient;
|
||||||
|
import io.vertx.core.http.HttpClientOptions;
|
||||||
|
import io.vertx.core.http.HttpClientRequest;
|
||||||
|
import io.vertx.core.http.HttpClientResponse;
|
||||||
|
import io.vertx.core.http.HttpHeaders;
|
||||||
|
import io.vertx.core.http.HttpMethod;
|
||||||
|
import io.vertx.core.http.RequestOptions;
|
||||||
|
import io.vertx.core.json.Json;
|
||||||
import io.vertx.core.json.JsonObject;
|
import io.vertx.core.json.JsonObject;
|
||||||
import io.vertx.core.net.ProxyOptions;
|
import io.vertx.core.net.ProxyOptions;
|
||||||
import io.vertx.core.net.ProxyType;
|
import io.vertx.core.net.ProxyType;
|
||||||
import io.vertx.ext.web.client.HttpRequest;
|
|
||||||
import io.vertx.ext.web.client.HttpResponse;
|
import io.vertx.ext.web.client.HttpResponse;
|
||||||
import io.vertx.ext.web.client.WebClient;
|
|
||||||
import io.vertx.ext.web.client.WebClientOptions;
|
|
||||||
import io.vertx.ext.web.client.WebClientSession;
|
|
||||||
import io.vertx.ext.web.multipart.MultipartForm;
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -27,8 +29,13 @@ import java.net.UnknownHostException;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -41,9 +48,58 @@ import java.util.regex.Pattern;
|
|||||||
public class JsHttpClient {
|
public class JsHttpClient {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(JsHttpClient.class);
|
private static final Logger log = LoggerFactory.getLogger(JsHttpClient.class);
|
||||||
|
private static final int MAX_RESPONSE_BODY_BYTES = 8 * 1024 * 1024;
|
||||||
|
private static final int MAX_REQUEST_BODY_BYTES = 8 * 1024 * 1024;
|
||||||
|
private static final int MAX_HEADER_COUNT = 64;
|
||||||
|
private static final int MAX_HEADER_VALUE_LENGTH = 4096;
|
||||||
|
private static final int MAX_TIMEOUT_SECONDS = 120;
|
||||||
|
private static final int MAX_REDIRECTS = 5;
|
||||||
|
private static final String DEFAULT_ACCEPT_ENCODING = "gzip, deflate, br";
|
||||||
|
|
||||||
private final WebClient client;
|
private static final Object SHARED_CLIENT_LOCK = new Object();
|
||||||
private final WebClientSession clientSession;
|
// 共享 HttpClient 实例(非代理模式),懒加载避免类初始化阶段抢跑 Vert.x。
|
||||||
|
private static volatile HttpClient sharedClient;
|
||||||
|
private static volatile boolean sharedClientShutdown = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭共享 HttpClient(应用关闭时调用)
|
||||||
|
*/
|
||||||
|
public static void shutdownSharedClient() {
|
||||||
|
synchronized (SHARED_CLIENT_LOCK) {
|
||||||
|
sharedClientShutdown = true;
|
||||||
|
if (sharedClient != null) {
|
||||||
|
sharedClient.close();
|
||||||
|
sharedClient = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static HttpClient sharedClient() {
|
||||||
|
synchronized (SHARED_CLIENT_LOCK) {
|
||||||
|
ensureSharedClientAvailable();
|
||||||
|
if (sharedClient == null) {
|
||||||
|
sharedClient = WebClientVertxInit.get().createHttpClient(
|
||||||
|
new HttpClientOptions()
|
||||||
|
.setConnectTimeout(10000)
|
||||||
|
.setIdleTimeout(30)
|
||||||
|
.setIdleTimeoutUnit(TimeUnit.SECONDS)
|
||||||
|
.setMaxPoolSize(64));
|
||||||
|
}
|
||||||
|
return sharedClient;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ensureSharedClientAvailable() {
|
||||||
|
if (sharedClientShutdown) {
|
||||||
|
throw new IllegalStateException("共享 JavaScript HttpClient 已关闭");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private final HttpClient client;
|
||||||
|
private final boolean ownClient; // 标记是否为自建 client(需要 close)
|
||||||
|
private final AtomicBoolean closed = new AtomicBoolean(false);
|
||||||
|
private final Object requestLock = new Object();
|
||||||
|
private final Set<HttpClientRequest> activeRequests = ConcurrentHashMap.newKeySet();
|
||||||
private MultiMap headers;
|
private MultiMap headers;
|
||||||
private int timeoutSeconds = 30; // 默认超时时间30秒
|
private int timeoutSeconds = 30; // 默认超时时间30秒
|
||||||
|
|
||||||
@@ -61,11 +117,12 @@ public class JsHttpClient {
|
|||||||
};
|
};
|
||||||
|
|
||||||
public JsHttpClient() {
|
public JsHttpClient() {
|
||||||
this.client = WebClient.create(WebClientVertxInit.get(), new WebClientOptions());;
|
ensureSharedClientAvailable();
|
||||||
this.clientSession = WebClientSession.create(client);
|
this.client = sharedClient();
|
||||||
|
this.ownClient = false;
|
||||||
this.headers = MultiMap.caseInsensitiveMultiMap();
|
this.headers = MultiMap.caseInsensitiveMultiMap();
|
||||||
// 设置默认的Accept-Encoding头以支持压缩响应
|
// 设置默认的Accept-Encoding头以支持压缩响应
|
||||||
this.headers.set("Accept-Encoding", "gzip, deflate, br, zstd");
|
this.headers.set("Accept-Encoding", DEFAULT_ACCEPT_ENCODING);
|
||||||
// 设置默认的User-Agent头
|
// 设置默认的User-Agent头
|
||||||
this.headers.set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0");
|
this.headers.set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0");
|
||||||
// 设置默认的Accept-Language头
|
// 设置默认的Accept-Language头
|
||||||
@@ -77,6 +134,7 @@ public class JsHttpClient {
|
|||||||
* @param proxyConfig 代理配置JsonObject,包含type、host、port、username、password
|
* @param proxyConfig 代理配置JsonObject,包含type、host、port、username、password
|
||||||
*/
|
*/
|
||||||
public JsHttpClient(JsonObject proxyConfig) {
|
public JsHttpClient(JsonObject proxyConfig) {
|
||||||
|
ensureSharedClientAvailable();
|
||||||
if (proxyConfig != null && proxyConfig.containsKey("type")) {
|
if (proxyConfig != null && proxyConfig.containsKey("type")) {
|
||||||
ProxyOptions proxyOptions = new ProxyOptions()
|
ProxyOptions proxyOptions = new ProxyOptions()
|
||||||
.setType(ProxyType.valueOf(proxyConfig.getString("type").toUpperCase()))
|
.setType(ProxyType.valueOf(proxyConfig.getString("type").toUpperCase()))
|
||||||
@@ -90,18 +148,21 @@ public class JsHttpClient {
|
|||||||
proxyOptions.setPassword(proxyConfig.getString("password"));
|
proxyOptions.setPassword(proxyConfig.getString("password"));
|
||||||
}
|
}
|
||||||
|
|
||||||
this.client = WebClient.create(WebClientVertxInit.get(),
|
this.client = WebClientVertxInit.get().createHttpClient(
|
||||||
new WebClientOptions()
|
new HttpClientOptions()
|
||||||
.setUserAgentEnabled(false)
|
.setConnectTimeout(10000)
|
||||||
|
.setIdleTimeout(30)
|
||||||
|
.setIdleTimeoutUnit(TimeUnit.SECONDS)
|
||||||
|
.setMaxPoolSize(16)
|
||||||
.setProxyOptions(proxyOptions));
|
.setProxyOptions(proxyOptions));
|
||||||
this.clientSession = WebClientSession.create(client);
|
this.ownClient = true;
|
||||||
} else {
|
} else {
|
||||||
this.client = WebClient.create(WebClientVertxInit.get());
|
this.client = sharedClient();
|
||||||
this.clientSession = WebClientSession.create(client);
|
this.ownClient = false;
|
||||||
}
|
}
|
||||||
this.headers = MultiMap.caseInsensitiveMultiMap();
|
this.headers = MultiMap.caseInsensitiveMultiMap();
|
||||||
// 设置默认的Accept-Encoding头以支持压缩响应
|
// 设置默认的Accept-Encoding头以支持压缩响应
|
||||||
this.headers.set("Accept-Encoding", "gzip, deflate, br, zstd");
|
this.headers.set("Accept-Encoding", DEFAULT_ACCEPT_ENCODING);
|
||||||
// 设置默认的User-Agent头
|
// 设置默认的User-Agent头
|
||||||
this.headers.set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0");
|
this.headers.set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0");
|
||||||
// 设置默认的Accept-Language头
|
// 设置默认的Accept-Language头
|
||||||
@@ -183,13 +244,7 @@ public class JsHttpClient {
|
|||||||
*/
|
*/
|
||||||
public JsHttpResponse get(String url) {
|
public JsHttpResponse get(String url) {
|
||||||
validateUrlSecurity(url);
|
validateUrlSecurity(url);
|
||||||
return executeRequest(() -> {
|
return executeRequest(HttpMethod.GET, url, null, false);
|
||||||
HttpRequest<Buffer> request = client.getAbs(url);
|
|
||||||
if (!headers.isEmpty()) {
|
|
||||||
request.putHeaders(headers);
|
|
||||||
}
|
|
||||||
return request.send();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -198,16 +253,25 @@ public class JsHttpClient {
|
|||||||
* @return HTTP响应
|
* @return HTTP响应
|
||||||
*/
|
*/
|
||||||
public JsHttpResponse getWithRedirect(String url) {
|
public JsHttpResponse getWithRedirect(String url) {
|
||||||
validateUrlSecurity(url);
|
String currentUrl = url;
|
||||||
return executeRequest(() -> {
|
for (int redirectCount = 0; redirectCount <= MAX_REDIRECTS; redirectCount++) {
|
||||||
HttpRequest<Buffer> request = client.getAbs(url);
|
validateUrlSecurity(currentUrl);
|
||||||
if (!headers.isEmpty()) {
|
JsHttpResponse response = executeRequest(HttpMethod.GET, currentUrl, null, false);
|
||||||
request.putHeaders(headers);
|
if (!isRedirectStatus(response.statusCode())) {
|
||||||
|
return response;
|
||||||
}
|
}
|
||||||
// 设置跟随重定向
|
|
||||||
request.followRedirects(true);
|
if (redirectCount == MAX_REDIRECTS) {
|
||||||
return request.send();
|
throw new RuntimeException("重定向次数超过限制: " + MAX_REDIRECTS);
|
||||||
});
|
}
|
||||||
|
|
||||||
|
String location = response.header(HttpHeaders.LOCATION.toString());
|
||||||
|
if (StringUtils.isBlank(location)) {
|
||||||
|
throw new RuntimeException("重定向响应缺少Location头");
|
||||||
|
}
|
||||||
|
currentUrl = resolveRedirectUrl(currentUrl, location);
|
||||||
|
}
|
||||||
|
throw new RuntimeException("重定向处理失败");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -217,15 +281,7 @@ public class JsHttpClient {
|
|||||||
*/
|
*/
|
||||||
public JsHttpResponse getNoRedirect(String url) {
|
public JsHttpResponse getNoRedirect(String url) {
|
||||||
validateUrlSecurity(url);
|
validateUrlSecurity(url);
|
||||||
return executeRequest(() -> {
|
return executeRequest(HttpMethod.GET, url, null, false);
|
||||||
HttpRequest<Buffer> request = client.getAbs(url);
|
|
||||||
if (!headers.isEmpty()) {
|
|
||||||
request.putHeaders(headers);
|
|
||||||
}
|
|
||||||
// 设置不跟随重定向
|
|
||||||
request.followRedirects(false);
|
|
||||||
return request.send();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -236,26 +292,7 @@ public class JsHttpClient {
|
|||||||
*/
|
*/
|
||||||
public JsHttpResponse post(String url, Object data) {
|
public JsHttpResponse post(String url, Object data) {
|
||||||
validateUrlSecurity(url);
|
validateUrlSecurity(url);
|
||||||
return executeRequest(() -> {
|
return executeRequest(HttpMethod.POST, url, bodyFromData(data), false);
|
||||||
HttpRequest<Buffer> request = client.postAbs(url);
|
|
||||||
if (!headers.isEmpty()) {
|
|
||||||
request.putHeaders(headers);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data != null) {
|
|
||||||
if (data instanceof String) {
|
|
||||||
return request.sendBuffer(Buffer.buffer((String) data));
|
|
||||||
} else if (data instanceof Map) {
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
Map<String, String> mapData = (Map<String, String>) data;
|
|
||||||
return request.sendForm(MultiMap.caseInsensitiveMultiMap().addAll(mapData));
|
|
||||||
} else {
|
|
||||||
return request.sendJson(data);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return request.send();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -266,26 +303,7 @@ public class JsHttpClient {
|
|||||||
*/
|
*/
|
||||||
public JsHttpResponse put(String url, Object data) {
|
public JsHttpResponse put(String url, Object data) {
|
||||||
validateUrlSecurity(url);
|
validateUrlSecurity(url);
|
||||||
return executeRequest(() -> {
|
return executeRequest(HttpMethod.PUT, url, bodyFromData(data), false);
|
||||||
HttpRequest<Buffer> request = client.putAbs(url);
|
|
||||||
if (!headers.isEmpty()) {
|
|
||||||
request.putHeaders(headers);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data != null) {
|
|
||||||
if (data instanceof String) {
|
|
||||||
return request.sendBuffer(Buffer.buffer((String) data));
|
|
||||||
} else if (data instanceof Map) {
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
Map<String, String> mapData = (Map<String, String>) data;
|
|
||||||
return request.sendForm(MultiMap.caseInsensitiveMultiMap().addAll(mapData));
|
|
||||||
} else {
|
|
||||||
return request.sendJson(data);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return request.send();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -294,13 +312,8 @@ public class JsHttpClient {
|
|||||||
* @return HTTP响应
|
* @return HTTP响应
|
||||||
*/
|
*/
|
||||||
public JsHttpResponse delete(String url) {
|
public JsHttpResponse delete(String url) {
|
||||||
return executeRequest(() -> {
|
validateUrlSecurity(url);
|
||||||
HttpRequest<Buffer> request = client.deleteAbs(url);
|
return executeRequest(HttpMethod.DELETE, url, null, false);
|
||||||
if (!headers.isEmpty()) {
|
|
||||||
request.putHeaders(headers);
|
|
||||||
}
|
|
||||||
return request.send();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -310,26 +323,8 @@ public class JsHttpClient {
|
|||||||
* @return HTTP响应
|
* @return HTTP响应
|
||||||
*/
|
*/
|
||||||
public JsHttpResponse patch(String url, Object data) {
|
public JsHttpResponse patch(String url, Object data) {
|
||||||
return executeRequest(() -> {
|
validateUrlSecurity(url);
|
||||||
HttpRequest<Buffer> request = client.patchAbs(url);
|
return executeRequest(HttpMethod.PATCH, url, bodyFromData(data), false);
|
||||||
if (!headers.isEmpty()) {
|
|
||||||
request.putHeaders(headers);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data != null) {
|
|
||||||
if (data instanceof String) {
|
|
||||||
return request.sendBuffer(Buffer.buffer((String) data));
|
|
||||||
} else if (data instanceof Map) {
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
Map<String, String> mapData = (Map<String, String>) data;
|
|
||||||
return request.sendForm(MultiMap.caseInsensitiveMultiMap().addAll(mapData));
|
|
||||||
} else {
|
|
||||||
return request.sendJson(data);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return request.send();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -340,6 +335,12 @@ public class JsHttpClient {
|
|||||||
*/
|
*/
|
||||||
public JsHttpClient putHeader(String name, String value) {
|
public JsHttpClient putHeader(String name, String value) {
|
||||||
if (name != null && value != null) {
|
if (name != null && value != null) {
|
||||||
|
if (headers.size() >= MAX_HEADER_COUNT && !headers.contains(name)) {
|
||||||
|
throw new IllegalArgumentException("请求头数量超过限制");
|
||||||
|
}
|
||||||
|
if (value.length() > MAX_HEADER_VALUE_LENGTH) {
|
||||||
|
throw new IllegalArgumentException("请求头过长: " + name);
|
||||||
|
}
|
||||||
headers.set(name, value);
|
headers.set(name, value);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
@@ -353,9 +354,7 @@ public class JsHttpClient {
|
|||||||
public JsHttpClient putHeaders(Map<String, String> headersMap) {
|
public JsHttpClient putHeaders(Map<String, String> headersMap) {
|
||||||
if (headersMap != null) {
|
if (headersMap != null) {
|
||||||
for (Map.Entry<String, String> entry : headersMap.entrySet()) {
|
for (Map.Entry<String, String> entry : headersMap.entrySet()) {
|
||||||
if (entry.getKey() != null && entry.getValue() != null) {
|
putHeader(entry.getKey(), entry.getValue());
|
||||||
headers.set(entry.getKey(), entry.getValue());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
@@ -380,7 +379,7 @@ public class JsHttpClient {
|
|||||||
public JsHttpClient clearHeaders() {
|
public JsHttpClient clearHeaders() {
|
||||||
headers.clear();
|
headers.clear();
|
||||||
// 重新设置默认头
|
// 重新设置默认头
|
||||||
headers.set("Accept-Encoding", "gzip, deflate, br, zstd");
|
headers.set("Accept-Encoding", DEFAULT_ACCEPT_ENCODING);
|
||||||
headers.set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0");
|
headers.set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0");
|
||||||
headers.set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6");
|
headers.set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6");
|
||||||
return this;
|
return this;
|
||||||
@@ -405,7 +404,7 @@ public class JsHttpClient {
|
|||||||
*/
|
*/
|
||||||
public JsHttpClient setTimeout(int seconds) {
|
public JsHttpClient setTimeout(int seconds) {
|
||||||
if (seconds > 0) {
|
if (seconds > 0) {
|
||||||
this.timeoutSeconds = seconds;
|
this.timeoutSeconds = Math.min(seconds, MAX_TIMEOUT_SECONDS);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -450,19 +449,12 @@ public class JsHttpClient {
|
|||||||
* @return HTTP响应
|
* @return HTTP响应
|
||||||
*/
|
*/
|
||||||
public JsHttpResponse sendForm(Map<String, String> data) {
|
public JsHttpResponse sendForm(Map<String, String> data) {
|
||||||
return executeRequest(() -> {
|
throw new IllegalArgumentException("sendForm(data) 缺少请求URL,请使用 post(url, data)");
|
||||||
HttpRequest<Buffer> request = client.postAbs("");
|
|
||||||
if (!headers.isEmpty()) {
|
|
||||||
request.putHeaders(headers);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MultiMap formData = MultiMap.caseInsensitiveMultiMap();
|
public JsHttpResponse sendForm(String url, Map<String, String> data) {
|
||||||
if (data != null) {
|
validateUrlSecurity(url);
|
||||||
formData.addAll(data);
|
return executeRequest(HttpMethod.POST, url, formBody(data), false);
|
||||||
}
|
|
||||||
|
|
||||||
return request.sendForm(formData);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -474,34 +466,8 @@ public class JsHttpClient {
|
|||||||
* @return HTTP响应
|
* @return HTTP响应
|
||||||
*/
|
*/
|
||||||
public JsHttpResponse sendMultipartForm(String url, Map<String, Object> data) {
|
public JsHttpResponse sendMultipartForm(String url, Map<String, Object> data) {
|
||||||
return executeRequest(() -> {
|
validateUrlSecurity(url);
|
||||||
HttpRequest<Buffer> request = client.postAbs(url);
|
return executeRequest(HttpMethod.POST, url, multipartBody(data), false);
|
||||||
if (!headers.isEmpty()) {
|
|
||||||
request.putHeaders(headers);
|
|
||||||
}
|
|
||||||
|
|
||||||
MultipartForm form = MultipartForm.create();
|
|
||||||
|
|
||||||
if (data != null) {
|
|
||||||
for (Map.Entry<String, Object> entry : data.entrySet()) {
|
|
||||||
String key = entry.getKey();
|
|
||||||
Object value = entry.getValue();
|
|
||||||
|
|
||||||
if (value instanceof String) {
|
|
||||||
form.attribute(key, (String) value);
|
|
||||||
} else if (value instanceof byte[]) {
|
|
||||||
form.binaryFileUpload(key, key, Buffer.buffer((byte[]) value), "application/octet-stream");
|
|
||||||
} else if (value instanceof Buffer) {
|
|
||||||
form.binaryFileUpload(key, key, (Buffer) value, "application/octet-stream");
|
|
||||||
} else if (value != null) {
|
|
||||||
// 其他类型转换为字符串
|
|
||||||
form.attribute(key, value.toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return request.sendMultipartForm(form);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -510,44 +476,103 @@ public class JsHttpClient {
|
|||||||
* @return HTTP响应
|
* @return HTTP响应
|
||||||
*/
|
*/
|
||||||
public JsHttpResponse sendJson(Object data) {
|
public JsHttpResponse sendJson(Object data) {
|
||||||
return executeRequest(() -> {
|
throw new IllegalArgumentException("sendJson(data) 缺少请求URL,请使用 post(url, data)");
|
||||||
HttpRequest<Buffer> request = client.postAbs("");
|
|
||||||
if (!headers.isEmpty()) {
|
|
||||||
request.putHeaders(headers);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return request.sendJson(data);
|
public JsHttpResponse sendJson(String url, Object data) {
|
||||||
});
|
validateUrlSecurity(url);
|
||||||
|
return executeRequest(HttpMethod.POST, url, jsonBody(data), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行HTTP请求(同步)
|
* 执行HTTP请求(同步)
|
||||||
*/
|
*/
|
||||||
private JsHttpResponse executeRequest(RequestExecutor executor) {
|
private JsHttpResponse executeRequest(HttpMethod method, String url, RequestBody requestBody, boolean followRedirects) {
|
||||||
try {
|
if (closed.get()) {
|
||||||
Promise<HttpResponse<Buffer>> promise = Promise.promise();
|
throw new IllegalStateException("HTTP客户端已关闭");
|
||||||
Future<HttpResponse<Buffer>> future = executor.execute();
|
|
||||||
|
|
||||||
future.onComplete(result -> {
|
|
||||||
if (result.succeeded()) {
|
|
||||||
promise.complete(result.result());
|
|
||||||
} else {
|
|
||||||
promise.fail(result.cause());
|
|
||||||
}
|
}
|
||||||
}).onFailure(Throwable::printStackTrace);
|
AtomicReference<HttpClientRequest> requestRef = new AtomicReference<>();
|
||||||
|
AtomicBoolean abandoned = new AtomicBoolean(false);
|
||||||
|
try {
|
||||||
|
Promise<JsHttpResponse> promise = Promise.promise();
|
||||||
|
|
||||||
// 等待响应完成(使用配置的超时时间)
|
RequestOptions options = new RequestOptions()
|
||||||
HttpResponse<Buffer> response = promise.future().toCompletionStage()
|
.setMethod(method)
|
||||||
|
.setAbsoluteURI(url)
|
||||||
|
.setFollowRedirects(followRedirects)
|
||||||
|
.setTimeout(TimeUnit.SECONDS.toMillis(timeoutSeconds))
|
||||||
|
.setHeaders(MultiMap.caseInsensitiveMultiMap().setAll(headers));
|
||||||
|
|
||||||
|
client.request(options).onComplete(ar -> {
|
||||||
|
if (ar.failed()) {
|
||||||
|
promise.tryFail(ar.cause());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
HttpClientRequest request = ar.result();
|
||||||
|
synchronized (requestLock) {
|
||||||
|
if (closed.get() || abandoned.get()) {
|
||||||
|
request.reset();
|
||||||
|
promise.tryFail("HTTP客户端已关闭");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
activeRequests.add(request);
|
||||||
|
requestRef.set(request);
|
||||||
|
request.exceptionHandler(e -> {
|
||||||
|
finishRequest(request);
|
||||||
|
promise.tryFail(e);
|
||||||
|
});
|
||||||
|
request.response().onComplete(responseAr -> {
|
||||||
|
if (responseAr.succeeded()) {
|
||||||
|
collectResponse(request, responseAr.result(), promise);
|
||||||
|
} else {
|
||||||
|
finishRequest(request);
|
||||||
|
promise.tryFail(responseAr.cause());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (closed.get() || abandoned.get()) {
|
||||||
|
request.reset();
|
||||||
|
finishRequest(request);
|
||||||
|
promise.tryFail("HTTP客户端已关闭");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (requestBody == null || requestBody.body() == null) {
|
||||||
|
request.end().onFailure(e -> {
|
||||||
|
finishRequest(request);
|
||||||
|
promise.tryFail(e);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
request.headers().set(HttpHeaders.CONTENT_LENGTH, String.valueOf(requestBody.body().length()));
|
||||||
|
if (StringUtils.isNotEmpty(requestBody.contentType())) {
|
||||||
|
request.headers().set(HttpHeaders.CONTENT_TYPE, requestBody.contentType());
|
||||||
|
}
|
||||||
|
request.end(requestBody.body()).onFailure(e -> {
|
||||||
|
finishRequest(request);
|
||||||
|
promise.tryFail(e);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return promise.future().toCompletionStage()
|
||||||
.toCompletableFuture()
|
.toCompletableFuture()
|
||||||
.get(timeoutSeconds, TimeUnit.SECONDS);
|
.get(timeoutSeconds, TimeUnit.SECONDS);
|
||||||
|
|
||||||
return new JsHttpResponse(response);
|
|
||||||
|
|
||||||
} catch (TimeoutException e) {
|
} catch (TimeoutException e) {
|
||||||
|
// RequestOptions timeout 通常会先触发;这里再兜底,避免等待线程返回后请求还在后台下载。
|
||||||
String errorMsg = "HTTP请求超时(" + timeoutSeconds + "秒)";
|
String errorMsg = "HTTP请求超时(" + timeoutSeconds + "秒)";
|
||||||
|
abandoned.set(true);
|
||||||
|
synchronized (requestLock) {
|
||||||
|
abortRequest(requestRef);
|
||||||
|
}
|
||||||
log.error(errorMsg, e);
|
log.error(errorMsg, e);
|
||||||
throw new RuntimeException(errorMsg, e);
|
throw new RuntimeException(errorMsg, e);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
abandoned.set(true);
|
||||||
|
synchronized (requestLock) {
|
||||||
|
abortRequest(requestRef);
|
||||||
|
}
|
||||||
String errorMsg = e.getMessage();
|
String errorMsg = e.getMessage();
|
||||||
if (errorMsg == null || errorMsg.trim().isEmpty()) {
|
if (errorMsg == null || errorMsg.trim().isEmpty()) {
|
||||||
errorMsg = e.getClass().getSimpleName();
|
errorMsg = e.getClass().getSimpleName();
|
||||||
@@ -560,12 +585,195 @@ public class JsHttpClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private static boolean isRedirectStatus(int statusCode) {
|
||||||
* 请求执行器接口
|
return statusCode == 301 || statusCode == 302 || statusCode == 303
|
||||||
*/
|
|| statusCode == 307 || statusCode == 308;
|
||||||
@FunctionalInterface
|
}
|
||||||
private interface RequestExecutor {
|
|
||||||
Future<HttpResponse<Buffer>> execute();
|
private String resolveRedirectUrl(String currentUrl, String location) {
|
||||||
|
try {
|
||||||
|
URI redirectUri = new URI(currentUrl).resolve(location.trim());
|
||||||
|
String scheme = redirectUri.getScheme();
|
||||||
|
if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
|
||||||
|
throw new SecurityException("🔒 安全拦截: 重定向协议不被允许");
|
||||||
|
}
|
||||||
|
String redirectUrl = redirectUri.toString();
|
||||||
|
validateUrlSecurity(redirectUrl);
|
||||||
|
return redirectUrl;
|
||||||
|
} catch (SecurityException e) {
|
||||||
|
throw e;
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("解析重定向地址失败: " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void collectResponse(HttpClientRequest request, HttpClientResponse response, Promise<JsHttpResponse> promise) {
|
||||||
|
Buffer body = Buffer.buffer();
|
||||||
|
AtomicBoolean done = new AtomicBoolean(false);
|
||||||
|
|
||||||
|
String contentLengthHeader = response.getHeader(HttpHeaders.CONTENT_LENGTH.toString());
|
||||||
|
if (StringUtils.isNumeric(contentLengthHeader)) {
|
||||||
|
long contentLength = Long.parseLong(contentLengthHeader);
|
||||||
|
if (contentLength > MAX_RESPONSE_BODY_BYTES) {
|
||||||
|
done.set(true);
|
||||||
|
request.reset();
|
||||||
|
finishRequest(request);
|
||||||
|
promise.tryFail("响应体过大: " + contentLength + " bytes");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
response.exceptionHandler(e -> {
|
||||||
|
if (done.compareAndSet(false, true)) {
|
||||||
|
finishRequest(request);
|
||||||
|
promise.tryFail(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
response.handler(chunk -> {
|
||||||
|
if (done.get()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (body.length() + chunk.length() > MAX_RESPONSE_BODY_BYTES) {
|
||||||
|
if (done.compareAndSet(false, true)) {
|
||||||
|
request.reset();
|
||||||
|
finishRequest(request);
|
||||||
|
promise.tryFail("响应体过大: " + (body.length() + chunk.length()) + " bytes");
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
body.appendBuffer(chunk);
|
||||||
|
});
|
||||||
|
response.endHandler(v -> {
|
||||||
|
if (done.compareAndSet(false, true)) {
|
||||||
|
finishRequest(request);
|
||||||
|
promise.tryComplete(new JsHttpResponse(
|
||||||
|
response.statusCode(),
|
||||||
|
MultiMap.caseInsensitiveMultiMap().setAll(response.headers()),
|
||||||
|
body,
|
||||||
|
response.statusMessage(),
|
||||||
|
null
|
||||||
|
));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
response.resume();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void finishRequest(HttpClientRequest request) {
|
||||||
|
if (request != null) {
|
||||||
|
activeRequests.remove(request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void abortRequest(AtomicReference<HttpClientRequest> requestRef) {
|
||||||
|
HttpClientRequest request = requestRef.get();
|
||||||
|
if (request != null) {
|
||||||
|
try {
|
||||||
|
request.reset();
|
||||||
|
} finally {
|
||||||
|
finishRequest(request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private RequestBody bodyFromData(Object data) {
|
||||||
|
if (data == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (data instanceof String str) {
|
||||||
|
return plainTextBody(str);
|
||||||
|
}
|
||||||
|
if (data instanceof Buffer buffer) {
|
||||||
|
return limitedBody(buffer, null);
|
||||||
|
}
|
||||||
|
if (data instanceof byte[] bytes) {
|
||||||
|
return limitedBody(Buffer.buffer(bytes), null);
|
||||||
|
}
|
||||||
|
if (data instanceof Map<?, ?> map) {
|
||||||
|
Map<String, String> formMap = new HashMap<>();
|
||||||
|
map.forEach((key, value) -> {
|
||||||
|
if (key != null && value != null) {
|
||||||
|
formMap.put(String.valueOf(key), String.valueOf(value));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return formBody(formMap);
|
||||||
|
}
|
||||||
|
return jsonBody(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
private RequestBody plainTextBody(String data) {
|
||||||
|
return limitedBody(Buffer.buffer(data, StandardCharsets.UTF_8.name()), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private RequestBody jsonBody(Object data) {
|
||||||
|
Buffer body = data == null ? Buffer.buffer() : Buffer.buffer(Json.encode(data), StandardCharsets.UTF_8.name());
|
||||||
|
return limitedBody(body, "application/json; charset=utf-8");
|
||||||
|
}
|
||||||
|
|
||||||
|
private RequestBody formBody(Map<String, String> data) {
|
||||||
|
StringBuilder encoded = new StringBuilder();
|
||||||
|
if (data != null) {
|
||||||
|
for (Map.Entry<String, String> entry : data.entrySet()) {
|
||||||
|
if (encoded.length() > 0) {
|
||||||
|
encoded.append('&');
|
||||||
|
}
|
||||||
|
encoded.append(urlEncode(entry.getKey()));
|
||||||
|
encoded.append('=');
|
||||||
|
encoded.append(urlEncode(entry.getValue()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return limitedBody(Buffer.buffer(encoded.toString(), StandardCharsets.UTF_8.name()),
|
||||||
|
"application/x-www-form-urlencoded; charset=utf-8");
|
||||||
|
}
|
||||||
|
|
||||||
|
private RequestBody multipartBody(Map<String, Object> data) {
|
||||||
|
String boundary = "----NetdiskJsHttpClientBoundary" + UUID.randomUUID().toString().replace("-", "");
|
||||||
|
Buffer body = Buffer.buffer();
|
||||||
|
if (data != null) {
|
||||||
|
for (Map.Entry<String, Object> entry : data.entrySet()) {
|
||||||
|
String key = entry.getKey();
|
||||||
|
Object value = entry.getValue();
|
||||||
|
if (key == null || value == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
appendAscii(body, "--" + boundary + "\r\n");
|
||||||
|
if (value instanceof byte[] bytes) {
|
||||||
|
appendAscii(body, "Content-Disposition: form-data; name=\"" + escapeMultipart(key)
|
||||||
|
+ "\"; filename=\"" + escapeMultipart(key) + "\"\r\n");
|
||||||
|
appendAscii(body, "Content-Type: application/octet-stream\r\n\r\n");
|
||||||
|
body.appendBytes(bytes);
|
||||||
|
appendAscii(body, "\r\n");
|
||||||
|
} else if (value instanceof Buffer buffer) {
|
||||||
|
appendAscii(body, "Content-Disposition: form-data; name=\"" + escapeMultipart(key)
|
||||||
|
+ "\"; filename=\"" + escapeMultipart(key) + "\"\r\n");
|
||||||
|
appendAscii(body, "Content-Type: application/octet-stream\r\n\r\n");
|
||||||
|
body.appendBuffer(buffer);
|
||||||
|
appendAscii(body, "\r\n");
|
||||||
|
} else {
|
||||||
|
appendAscii(body, "Content-Disposition: form-data; name=\"" + escapeMultipart(key) + "\"\r\n\r\n");
|
||||||
|
body.appendString(String.valueOf(value), StandardCharsets.UTF_8.name());
|
||||||
|
appendAscii(body, "\r\n");
|
||||||
|
}
|
||||||
|
ensureRequestBodyLimit(body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
appendAscii(body, "--" + boundary + "--\r\n");
|
||||||
|
return limitedBody(body, "multipart/form-data; boundary=" + boundary);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void appendAscii(Buffer body, String value) {
|
||||||
|
body.appendString(value, StandardCharsets.US_ASCII.name());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String escapeMultipart(String value) {
|
||||||
|
return value.replace("\\", "\\\\").replace("\"", "\\\"");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static RequestBody limitedBody(Buffer body, String contentType) {
|
||||||
|
ensureRequestBodyLimit(body);
|
||||||
|
return new RequestBody(body, contentType);
|
||||||
|
}
|
||||||
|
|
||||||
|
private record RequestBody(Buffer body, String contentType) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -573,10 +781,29 @@ public class JsHttpClient {
|
|||||||
*/
|
*/
|
||||||
public static class JsHttpResponse {
|
public static class JsHttpResponse {
|
||||||
|
|
||||||
private final HttpResponse<Buffer> response;
|
private final int statusCode;
|
||||||
|
private final MultiMap headers;
|
||||||
|
private final Buffer body;
|
||||||
|
private final String statusMessage;
|
||||||
|
private final HttpResponse<Buffer> originalResponse;
|
||||||
|
|
||||||
public JsHttpResponse(HttpResponse<Buffer> response) {
|
public JsHttpResponse(HttpResponse<Buffer> response) {
|
||||||
this.response = response;
|
this(
|
||||||
|
response.statusCode(),
|
||||||
|
MultiMap.caseInsensitiveMultiMap().setAll(response.headers()),
|
||||||
|
response.body(),
|
||||||
|
response.statusMessage(),
|
||||||
|
response
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsHttpResponse(int statusCode, MultiMap headers, Buffer body, String statusMessage,
|
||||||
|
HttpResponse<Buffer> originalResponse) {
|
||||||
|
this.statusCode = statusCode;
|
||||||
|
this.headers = headers == null ? MultiMap.caseInsensitiveMultiMap() : headers;
|
||||||
|
this.body = body == null ? Buffer.buffer() : body;
|
||||||
|
this.statusMessage = statusMessage;
|
||||||
|
this.originalResponse = originalResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -584,7 +811,7 @@ public class JsHttpClient {
|
|||||||
* @return 响应体字符串
|
* @return 响应体字符串
|
||||||
*/
|
*/
|
||||||
public String body() {
|
public String body() {
|
||||||
return HttpResponseHelper.asText(response);
|
return HttpResponseHelper.asText(body, header(HttpHeaders.CONTENT_ENCODING.toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -593,7 +820,7 @@ public class JsHttpClient {
|
|||||||
*/
|
*/
|
||||||
public Object json() {
|
public Object json() {
|
||||||
try {
|
try {
|
||||||
JsonObject jsonObject = HttpResponseHelper.asJson(response);
|
JsonObject jsonObject = HttpResponseHelper.asJson(body, header(HttpHeaders.CONTENT_ENCODING.toString()));
|
||||||
if (jsonObject == null || jsonObject.isEmpty()) {
|
if (jsonObject == null || jsonObject.isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -611,7 +838,7 @@ public class JsHttpClient {
|
|||||||
* @return 状态码
|
* @return 状态码
|
||||||
*/
|
*/
|
||||||
public int statusCode() {
|
public int statusCode() {
|
||||||
return response.statusCode();
|
return statusCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -620,7 +847,7 @@ public class JsHttpClient {
|
|||||||
* @return 头值
|
* @return 头值
|
||||||
*/
|
*/
|
||||||
public String header(String name) {
|
public String header(String name) {
|
||||||
return response.getHeader(name);
|
return headers.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -628,10 +855,9 @@ public class JsHttpClient {
|
|||||||
* @return 响应头Map
|
* @return 响应头Map
|
||||||
*/
|
*/
|
||||||
public Map<String, String> headers() {
|
public Map<String, String> headers() {
|
||||||
MultiMap responseHeaders = response.headers();
|
|
||||||
Map<String, String> result = new HashMap<>();
|
Map<String, String> result = new HashMap<>();
|
||||||
for (String name : responseHeaders.names()) {
|
for (String name : headers.names()) {
|
||||||
result.put(name, responseHeaders.get(name));
|
result.put(name, headers.get(name));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -649,8 +875,14 @@ public class JsHttpClient {
|
|||||||
* 获取原始响应对象
|
* 获取原始响应对象
|
||||||
* @return HttpResponse对象
|
* @return HttpResponse对象
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public HttpResponse<Buffer> getOriginalResponse() {
|
public HttpResponse<Buffer> getOriginalResponse() {
|
||||||
return response;
|
if (originalResponse == null) {
|
||||||
|
throw new UnsupportedOperationException(
|
||||||
|
"流式HTTP客户端不再保留原始Vert.x HttpResponse,请使用statusCode/header/headers/body/bodyBytes方法"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return originalResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -658,11 +890,8 @@ public class JsHttpClient {
|
|||||||
* @return 响应体字节数组
|
* @return 响应体字节数组
|
||||||
*/
|
*/
|
||||||
public byte[] bodyBytes() {
|
public byte[] bodyBytes() {
|
||||||
Buffer buffer = response.body();
|
ensureResponseBodyLimit(body);
|
||||||
if (buffer == null) {
|
return body.getBytes();
|
||||||
return new byte[0];
|
|
||||||
}
|
|
||||||
return buffer.getBytes();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -670,11 +899,46 @@ public class JsHttpClient {
|
|||||||
* @return 响应体大小(字节)
|
* @return 响应体大小(字节)
|
||||||
*/
|
*/
|
||||||
public long bodySize() {
|
public long bodySize() {
|
||||||
Buffer buffer = response.body();
|
return body.length();
|
||||||
if (buffer == null) {
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
return buffer.length();
|
|
||||||
|
public String statusMessage() {
|
||||||
|
return statusMessage;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭 HttpClient 释放连接池资源
|
||||||
|
* 仅关闭自建的 client(代理模式),共享实例不关闭
|
||||||
|
*/
|
||||||
|
public void close() {
|
||||||
|
if (!closed.compareAndSet(false, true)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
synchronized (requestLock) {
|
||||||
|
for (HttpClientRequest request : activeRequests) {
|
||||||
|
try {
|
||||||
|
request.reset();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.debug("重置 JavaScript HTTP 请求失败: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
activeRequests.clear();
|
||||||
|
}
|
||||||
|
if (ownClient && client != null) {
|
||||||
|
client.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ensureResponseBodyLimit(Buffer buffer) {
|
||||||
|
if (buffer != null && buffer.length() > MAX_RESPONSE_BODY_BYTES) {
|
||||||
|
throw new IllegalArgumentException("响应体过大: " + buffer.length() + " bytes");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ensureRequestBodyLimit(Buffer buffer) {
|
||||||
|
if (buffer != null && buffer.length() > MAX_REQUEST_BODY_BYTES) {
|
||||||
|
throw new IllegalArgumentException("请求体过大: " + buffer.length() + " bytes");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,9 +6,9 @@ import cn.qaiu.entity.ShareLinkInfo;
|
|||||||
import cn.qaiu.parser.IPanTool;
|
import cn.qaiu.parser.IPanTool;
|
||||||
import cn.qaiu.parser.custom.CustomParserConfig;
|
import cn.qaiu.parser.custom.CustomParserConfig;
|
||||||
import io.vertx.core.Future;
|
import io.vertx.core.Future;
|
||||||
|
import io.vertx.core.Promise;
|
||||||
import io.vertx.core.WorkerExecutor;
|
import io.vertx.core.WorkerExecutor;
|
||||||
import io.vertx.core.json.JsonObject;
|
import io.vertx.core.json.JsonObject;
|
||||||
import org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory;
|
|
||||||
import org.openjdk.nashorn.api.scripting.ScriptObjectMirror;
|
import org.openjdk.nashorn.api.scripting.ScriptObjectMirror;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -20,6 +20,13 @@ import java.io.InputStreamReader;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
|
import java.util.concurrent.Semaphore;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -29,21 +36,46 @@ import java.util.stream.Collectors;
|
|||||||
* @author <a href="https://qaiu.top">QAIU</a>
|
* @author <a href="https://qaiu.top">QAIU</a>
|
||||||
* Create at 2025/10/17
|
* Create at 2025/10/17
|
||||||
*/
|
*/
|
||||||
public class JsParserExecutor implements IPanTool {
|
public class JsParserExecutor implements IPanTool, AutoCloseable {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(JsParserExecutor.class);
|
private static final Logger log = LoggerFactory.getLogger(JsParserExecutor.class);
|
||||||
|
|
||||||
private static final WorkerExecutor EXECUTOR = WebClientVertxInit.get().createSharedWorkerExecutor("parser-executor", 32);
|
private static volatile WorkerExecutor EXECUTOR;
|
||||||
|
private static final Object EXECUTOR_LOCK = new Object();
|
||||||
|
private static volatile boolean executorShutdown = false;
|
||||||
|
|
||||||
private static String FETCH_RUNTIME_JS = null;
|
/** 安全网调度器:当 onComplete 未触发时,延迟强制释放资源 */
|
||||||
|
private static final ScheduledExecutorService CLEANUP_SCHEDULER =
|
||||||
|
Executors.newSingleThreadScheduledExecutor(r -> {
|
||||||
|
Thread t = new Thread(r, "js-parser-cleanup-safety");
|
||||||
|
t.setDaemon(true);
|
||||||
|
return t;
|
||||||
|
});
|
||||||
|
private static final long EXECUTION_TIMEOUT_SECONDS = 30;
|
||||||
|
private static final int MAX_RESULT_STRING_LENGTH = 1024 * 1024;
|
||||||
|
private static final int MAX_FILE_LIST_SIZE = 1000;
|
||||||
|
private static final int MAX_FILE_FIELD_LENGTH = 4096;
|
||||||
|
private static final int MAX_CONCURRENT_EXECUTIONS =
|
||||||
|
Math.max(1, Integer.getInteger("parser.custom.js.maxConcurrentExecutions", 32));
|
||||||
|
private static final Semaphore EXECUTION_PERMITS = new Semaphore(MAX_CONCURRENT_EXECUTIONS);
|
||||||
|
|
||||||
|
private static volatile String FETCH_RUNTIME_JS = null;
|
||||||
|
|
||||||
private final CustomParserConfig config;
|
private final CustomParserConfig config;
|
||||||
private final ShareLinkInfo shareLinkInfo;
|
private final ShareLinkInfo shareLinkInfo;
|
||||||
private final ScriptEngine engine;
|
private volatile ScriptEngine engine;
|
||||||
|
private final Object engineLock = new Object();
|
||||||
private final JsHttpClient httpClient;
|
private final JsHttpClient httpClient;
|
||||||
private final JsLogger jsLogger;
|
private final JsLogger jsLogger;
|
||||||
private final JsShareLinkInfoWrapper shareLinkInfoWrapper;
|
private final JsShareLinkInfoWrapper shareLinkInfoWrapper;
|
||||||
private final JsFetchBridge fetchBridge;
|
private final JsFetchBridge fetchBridge;
|
||||||
|
/** 标记是否已释放,防止重复关闭 */
|
||||||
|
private final AtomicBoolean closed = new AtomicBoolean(false);
|
||||||
|
private final Object lifecycleLock = new Object();
|
||||||
|
private volatile boolean running = false;
|
||||||
|
private volatile boolean closeRequested = false;
|
||||||
|
/** 安全网定时任务句柄,正常完成时取消 */
|
||||||
|
private volatile ScheduledFuture<?> safetyCleanupFuture = null;
|
||||||
|
|
||||||
public JsParserExecutor(ShareLinkInfo shareLinkInfo, CustomParserConfig config) {
|
public JsParserExecutor(ShareLinkInfo shareLinkInfo, CustomParserConfig config) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
@@ -59,7 +91,6 @@ public class JsParserExecutor implements IPanTool {
|
|||||||
this.jsLogger = new JsLogger("JsParser-" + config.getType());
|
this.jsLogger = new JsLogger("JsParser-" + config.getType());
|
||||||
this.shareLinkInfoWrapper = new JsShareLinkInfoWrapper(shareLinkInfo);
|
this.shareLinkInfoWrapper = new JsShareLinkInfoWrapper(shareLinkInfo);
|
||||||
this.fetchBridge = new JsFetchBridge(httpClient);
|
this.fetchBridge = new JsFetchBridge(httpClient);
|
||||||
this.engine = initEngine();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -102,15 +133,8 @@ public class JsParserExecutor implements IPanTool {
|
|||||||
*/
|
*/
|
||||||
private ScriptEngine initEngine() {
|
private ScriptEngine initEngine() {
|
||||||
try {
|
try {
|
||||||
// 使用安全的ClassFilter创建Nashorn引擎
|
ScriptEngine engine = ParserScriptEngine.create();
|
||||||
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
|
this.engine = engine;
|
||||||
|
|
||||||
// 正确的方法签名: getScriptEngine(String[] args, ClassLoader appLoader, ClassFilter classFilter)
|
|
||||||
ScriptEngine engine = factory.getScriptEngine(new String[0], null, new SecurityClassFilter());
|
|
||||||
|
|
||||||
if (engine == null) {
|
|
||||||
throw new RuntimeException("无法创建JavaScript引擎,请确保Nashorn可用");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 注入Java对象到JavaScript环境
|
// 注入Java对象到JavaScript环境
|
||||||
engine.put("http", httpClient);
|
engine.put("http", httpClient);
|
||||||
@@ -118,14 +142,6 @@ public class JsParserExecutor implements IPanTool {
|
|||||||
engine.put("shareLinkInfo", shareLinkInfoWrapper);
|
engine.put("shareLinkInfo", shareLinkInfoWrapper);
|
||||||
engine.put("JavaFetch", fetchBridge);
|
engine.put("JavaFetch", fetchBridge);
|
||||||
|
|
||||||
// 禁用Java对象访问
|
|
||||||
engine.eval("var Java = undefined;");
|
|
||||||
engine.eval("var JavaImporter = undefined;");
|
|
||||||
engine.eval("var Packages = undefined;");
|
|
||||||
engine.eval("var javax = undefined;");
|
|
||||||
engine.eval("var org = undefined;");
|
|
||||||
engine.eval("var com = undefined;");
|
|
||||||
|
|
||||||
// 加载fetch运行时(Promise和fetch API polyfill)
|
// 加载fetch运行时(Promise和fetch API polyfill)
|
||||||
String fetchRuntime = loadFetchRuntime();
|
String fetchRuntime = loadFetchRuntime();
|
||||||
if (!fetchRuntime.isEmpty()) {
|
if (!fetchRuntime.isEmpty()) {
|
||||||
@@ -146,12 +162,190 @@ public class JsParserExecutor implements IPanTool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ScriptEngine engine() {
|
||||||
|
ScriptEngine current = engine;
|
||||||
|
if (current != null) {
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
synchronized (engineLock) {
|
||||||
|
if (closed.get()) {
|
||||||
|
throw new IllegalStateException("JavaScript解析器已关闭");
|
||||||
|
}
|
||||||
|
if (engine == null) {
|
||||||
|
engine = initEngine();
|
||||||
|
}
|
||||||
|
return engine;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void beginExecution() {
|
||||||
|
synchronized (lifecycleLock) {
|
||||||
|
if (closed.get() || closeRequested) {
|
||||||
|
throw new IllegalStateException("JavaScript解析器已关闭");
|
||||||
|
}
|
||||||
|
if (running) {
|
||||||
|
throw new IllegalStateException("JavaScript解析器已在运行");
|
||||||
|
}
|
||||||
|
running = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void finishExecution() {
|
||||||
|
synchronized (lifecycleLock) {
|
||||||
|
running = false;
|
||||||
|
if (closeRequested) {
|
||||||
|
doClose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 释放资源(ScriptEngine 和 HttpClient),避免内存泄漏
|
||||||
|
* 幂等:可安全多次调用
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
synchronized (lifecycleLock) {
|
||||||
|
closeRequested = true;
|
||||||
|
cancelSafetyCleanup();
|
||||||
|
if (running || closed.get()) {
|
||||||
|
closeExternalResources();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
doClose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void doClose() {
|
||||||
|
if (!closed.compareAndSet(false, true)) return;
|
||||||
|
closeRequested = false;
|
||||||
|
closeExternalResources();
|
||||||
|
cleanupEngine();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void closeExternalResources() {
|
||||||
|
if (httpClient != null) {
|
||||||
|
httpClient.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void cleanupEngine() {
|
||||||
|
// 清除 ScriptEngine 持有的所有引用和内部状态,帮助 GC 回收
|
||||||
|
if (engine != null) {
|
||||||
|
try {
|
||||||
|
engine.put("http", null);
|
||||||
|
engine.put("logger", null);
|
||||||
|
engine.put("shareLinkInfo", null);
|
||||||
|
engine.put("JavaFetch", null);
|
||||||
|
// 彻底清除 ENGINE_SCOPE bindings,释放 JS AST、编译函数、闭包等运行时状态
|
||||||
|
var bindings = engine.getBindings(javax.script.ScriptContext.ENGINE_SCOPE);
|
||||||
|
if (bindings != null) {
|
||||||
|
bindings.clear();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("清理 ScriptEngine bindings 失败: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void cancelSafetyCleanup() {
|
||||||
|
// 取消安全网定时任务(如果正常完成则无需再触发)
|
||||||
|
if (safetyCleanupFuture != null) {
|
||||||
|
safetyCleanupFuture.cancel(false);
|
||||||
|
safetyCleanupFuture = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭全局 WorkerExecutor 和清理调度器(应在应用关闭时调用)
|
||||||
|
*/
|
||||||
|
public static void shutdownExecutor() {
|
||||||
|
synchronized (EXECUTOR_LOCK) {
|
||||||
|
executorShutdown = true;
|
||||||
|
if (EXECUTOR != null) {
|
||||||
|
EXECUTOR.close();
|
||||||
|
EXECUTOR = null;
|
||||||
|
log.info("JsParserExecutor WorkerExecutor 已关闭");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CLEANUP_SCHEDULER.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取或创建 WorkerExecutor(懒加载)
|
||||||
|
*/
|
||||||
|
private static WorkerExecutor getExecutor() {
|
||||||
|
synchronized (EXECUTOR_LOCK) {
|
||||||
|
if (executorShutdown) {
|
||||||
|
throw new IllegalStateException("JavaScript解析器 WorkerExecutor 已关闭");
|
||||||
|
}
|
||||||
|
if (EXECUTOR == null) {
|
||||||
|
EXECUTOR = WebClientVertxInit.get().createSharedWorkerExecutor("parser-executor", 32);
|
||||||
|
}
|
||||||
|
return EXECUTOR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T> Future<T> executeBlockingWithPermit(String operation, Callable<T> blockingCode) {
|
||||||
|
if (!EXECUTION_PERMITS.tryAcquire()) {
|
||||||
|
String message = "JavaScript " + operation + " 执行并发已满,请稍后重试";
|
||||||
|
jsLogger.error(message);
|
||||||
|
close();
|
||||||
|
return Future.failedFuture(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return getExecutor().executeBlocking(() -> {
|
||||||
|
boolean executionStarted = false;
|
||||||
|
try {
|
||||||
|
beginExecution();
|
||||||
|
executionStarted = true;
|
||||||
|
return blockingCode.call();
|
||||||
|
} finally {
|
||||||
|
if (executionStarted) {
|
||||||
|
finishExecution();
|
||||||
|
}
|
||||||
|
EXECUTION_PERMITS.release();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (Throwable e) {
|
||||||
|
EXECUTION_PERMITS.release();
|
||||||
|
close();
|
||||||
|
return Future.failedFuture(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T> Future<T> withTimeout(Future<T> executionFuture, String operation) {
|
||||||
|
Promise<T> promise = Promise.promise();
|
||||||
|
try {
|
||||||
|
safetyCleanupFuture = CLEANUP_SCHEDULER.schedule(() -> {
|
||||||
|
if (promise.tryFail("JavaScript " + operation + " 执行超时(" + EXECUTION_TIMEOUT_SECONDS + "秒)")) {
|
||||||
|
jsLogger.error("{} 执行超时,已停止外部HTTP资源;ScriptEngine将在执行线程退出后清理", operation);
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
}, EXECUTION_TIMEOUT_SECONDS, TimeUnit.SECONDS);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("安全网调度失败: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
executionFuture.onComplete(ar -> {
|
||||||
|
cancelSafetyCleanup();
|
||||||
|
if (ar.succeeded()) {
|
||||||
|
promise.tryComplete(ar.result());
|
||||||
|
} else {
|
||||||
|
promise.tryFail(ar.cause());
|
||||||
|
}
|
||||||
|
close();
|
||||||
|
});
|
||||||
|
return promise.future();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Future<String> parse() {
|
public Future<String> parse() {
|
||||||
jsLogger.info("开始执行JavaScript解析器: {}", config.getType());
|
jsLogger.info("开始执行JavaScript解析器: {}", config.getType());
|
||||||
|
|
||||||
// 使用executeBlocking在工作线程上执行,避免阻塞EventLoop线程
|
// 使用executeBlocking在工作线程上执行,避免阻塞EventLoop线程
|
||||||
return EXECUTOR.executeBlocking(() -> {
|
Future<String> executionFuture = executeBlockingWithPermit("parse", () -> {
|
||||||
|
ScriptEngine engine = engine();
|
||||||
// 直接调用全局parse函数
|
// 直接调用全局parse函数
|
||||||
Object parseFunction = engine.get("parse");
|
Object parseFunction = engine.get("parse");
|
||||||
if (parseFunction == null) {
|
if (parseFunction == null) {
|
||||||
@@ -163,8 +357,9 @@ public class JsParserExecutor implements IPanTool {
|
|||||||
Object result = parseMirror.call(null, shareLinkInfoWrapper, httpClient, jsLogger);
|
Object result = parseMirror.call(null, shareLinkInfoWrapper, httpClient, jsLogger);
|
||||||
|
|
||||||
if (result instanceof String) {
|
if (result instanceof String) {
|
||||||
jsLogger.info("解析成功: {}", result);
|
String resultText = limitResultString((String) result, "parse");
|
||||||
return (String) result;
|
jsLogger.info("解析成功,结果长度: {}", resultText.length());
|
||||||
|
return resultText;
|
||||||
} else {
|
} else {
|
||||||
jsLogger.error("parse方法返回值类型错误,期望String,实际: {}",
|
jsLogger.error("parse方法返回值类型错误,期望String,实际: {}",
|
||||||
result != null ? result.getClass().getSimpleName() : "null");
|
result != null ? result.getClass().getSimpleName() : "null");
|
||||||
@@ -174,6 +369,7 @@ public class JsParserExecutor implements IPanTool {
|
|||||||
throw new RuntimeException("parse函数类型错误");
|
throw new RuntimeException("parse函数类型错误");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
return withTimeout(executionFuture, "parse");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -181,7 +377,8 @@ public class JsParserExecutor implements IPanTool {
|
|||||||
jsLogger.info("开始执行JavaScript文件列表解析: {}", config.getType());
|
jsLogger.info("开始执行JavaScript文件列表解析: {}", config.getType());
|
||||||
|
|
||||||
// 使用executeBlocking在工作线程上执行,避免阻塞EventLoop线程
|
// 使用executeBlocking在工作线程上执行,避免阻塞EventLoop线程
|
||||||
return EXECUTOR.executeBlocking(() -> {
|
Future<List<FileInfo>> executionFuture = executeBlockingWithPermit("parseFileList", () -> {
|
||||||
|
ScriptEngine engine = engine();
|
||||||
// 直接调用全局parseFileList函数
|
// 直接调用全局parseFileList函数
|
||||||
Object parseFileListFunction = engine.get("parseFileList");
|
Object parseFileListFunction = engine.get("parseFileList");
|
||||||
if (parseFileListFunction == null) {
|
if (parseFileListFunction == null) {
|
||||||
@@ -207,6 +404,7 @@ public class JsParserExecutor implements IPanTool {
|
|||||||
throw new RuntimeException("parseFileList函数类型错误");
|
throw new RuntimeException("parseFileList函数类型错误");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
return withTimeout(executionFuture, "parseFileList");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -214,7 +412,8 @@ public class JsParserExecutor implements IPanTool {
|
|||||||
jsLogger.info("开始执行JavaScript按ID解析: {}", config.getType());
|
jsLogger.info("开始执行JavaScript按ID解析: {}", config.getType());
|
||||||
|
|
||||||
// 使用executeBlocking在工作线程上执行,避免阻塞EventLoop线程
|
// 使用executeBlocking在工作线程上执行,避免阻塞EventLoop线程
|
||||||
return EXECUTOR.executeBlocking(() -> {
|
Future<String> executionFuture = executeBlockingWithPermit("parseById", () -> {
|
||||||
|
ScriptEngine engine = engine();
|
||||||
// 直接调用全局parseById函数
|
// 直接调用全局parseById函数
|
||||||
Object parseByIdFunction = engine.get("parseById");
|
Object parseByIdFunction = engine.get("parseById");
|
||||||
if (parseByIdFunction == null) {
|
if (parseByIdFunction == null) {
|
||||||
@@ -227,8 +426,9 @@ public class JsParserExecutor implements IPanTool {
|
|||||||
Object result = parseByIdMirror.call(null, shareLinkInfoWrapper, httpClient, jsLogger);
|
Object result = parseByIdMirror.call(null, shareLinkInfoWrapper, httpClient, jsLogger);
|
||||||
|
|
||||||
if (result instanceof String) {
|
if (result instanceof String) {
|
||||||
jsLogger.info("按ID解析成功: {}", result);
|
String resultText = limitResultString((String) result, "parseById");
|
||||||
return (String) result;
|
jsLogger.info("按ID解析成功,结果长度: {}", resultText.length());
|
||||||
|
return resultText;
|
||||||
} else {
|
} else {
|
||||||
jsLogger.error("parseById方法返回值类型错误,期望String,实际: {}",
|
jsLogger.error("parseById方法返回值类型错误,期望String,实际: {}",
|
||||||
result != null ? result.getClass().getSimpleName() : "null");
|
result != null ? result.getClass().getSimpleName() : "null");
|
||||||
@@ -238,6 +438,7 @@ public class JsParserExecutor implements IPanTool {
|
|||||||
throw new RuntimeException("parseById函数类型错误");
|
throw new RuntimeException("parseById函数类型错误");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
return withTimeout(executionFuture, "parseById");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -247,6 +448,9 @@ public class JsParserExecutor implements IPanTool {
|
|||||||
List<FileInfo> fileList = new ArrayList<>();
|
List<FileInfo> fileList = new ArrayList<>();
|
||||||
|
|
||||||
if (resultMirror.isArray()) {
|
if (resultMirror.isArray()) {
|
||||||
|
if (resultMirror.size() > MAX_FILE_LIST_SIZE) {
|
||||||
|
throw new RuntimeException("文件列表数量超过限制: " + resultMirror.size());
|
||||||
|
}
|
||||||
for (int i = 0; i < resultMirror.size(); i++) {
|
for (int i = 0; i < resultMirror.size(); i++) {
|
||||||
Object item = resultMirror.get(String.valueOf(i));
|
Object item = resultMirror.get(String.valueOf(i));
|
||||||
if (item instanceof ScriptObjectMirror) {
|
if (item instanceof ScriptObjectMirror) {
|
||||||
@@ -270,13 +474,13 @@ public class JsParserExecutor implements IPanTool {
|
|||||||
|
|
||||||
// 设置基本字段
|
// 设置基本字段
|
||||||
if (itemMirror.hasMember("fileName")) {
|
if (itemMirror.hasMember("fileName")) {
|
||||||
fileInfo.setFileName(itemMirror.getMember("fileName").toString());
|
fileInfo.setFileName(limitField(itemMirror.getMember("fileName")));
|
||||||
}
|
}
|
||||||
if (itemMirror.hasMember("fileId")) {
|
if (itemMirror.hasMember("fileId")) {
|
||||||
fileInfo.setFileId(itemMirror.getMember("fileId").toString());
|
fileInfo.setFileId(limitField(itemMirror.getMember("fileId")));
|
||||||
}
|
}
|
||||||
if (itemMirror.hasMember("fileType")) {
|
if (itemMirror.hasMember("fileType")) {
|
||||||
fileInfo.setFileType(itemMirror.getMember("fileType").toString());
|
fileInfo.setFileType(limitField(itemMirror.getMember("fileType")));
|
||||||
}
|
}
|
||||||
if (itemMirror.hasMember("size")) {
|
if (itemMirror.hasMember("size")) {
|
||||||
Object size = itemMirror.getMember("size");
|
Object size = itemMirror.getMember("size");
|
||||||
@@ -285,16 +489,16 @@ public class JsParserExecutor implements IPanTool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (itemMirror.hasMember("sizeStr")) {
|
if (itemMirror.hasMember("sizeStr")) {
|
||||||
fileInfo.setSizeStr(itemMirror.getMember("sizeStr").toString());
|
fileInfo.setSizeStr(limitField(itemMirror.getMember("sizeStr")));
|
||||||
}
|
}
|
||||||
if (itemMirror.hasMember("createTime")) {
|
if (itemMirror.hasMember("createTime")) {
|
||||||
fileInfo.setCreateTime(itemMirror.getMember("createTime").toString());
|
fileInfo.setCreateTime(limitField(itemMirror.getMember("createTime")));
|
||||||
}
|
}
|
||||||
if (itemMirror.hasMember("updateTime")) {
|
if (itemMirror.hasMember("updateTime")) {
|
||||||
fileInfo.setUpdateTime(itemMirror.getMember("updateTime").toString());
|
fileInfo.setUpdateTime(limitField(itemMirror.getMember("updateTime")));
|
||||||
}
|
}
|
||||||
if (itemMirror.hasMember("createBy")) {
|
if (itemMirror.hasMember("createBy")) {
|
||||||
fileInfo.setCreateBy(itemMirror.getMember("createBy").toString());
|
fileInfo.setCreateBy(limitField(itemMirror.getMember("createBy")));
|
||||||
}
|
}
|
||||||
if (itemMirror.hasMember("downloadCount")) {
|
if (itemMirror.hasMember("downloadCount")) {
|
||||||
Object downloadCount = itemMirror.getMember("downloadCount");
|
Object downloadCount = itemMirror.getMember("downloadCount");
|
||||||
@@ -303,16 +507,16 @@ public class JsParserExecutor implements IPanTool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (itemMirror.hasMember("fileIcon")) {
|
if (itemMirror.hasMember("fileIcon")) {
|
||||||
fileInfo.setFileIcon(itemMirror.getMember("fileIcon").toString());
|
fileInfo.setFileIcon(limitField(itemMirror.getMember("fileIcon")));
|
||||||
}
|
}
|
||||||
if (itemMirror.hasMember("panType")) {
|
if (itemMirror.hasMember("panType")) {
|
||||||
fileInfo.setPanType(itemMirror.getMember("panType").toString());
|
fileInfo.setPanType(limitField(itemMirror.getMember("panType")));
|
||||||
}
|
}
|
||||||
if (itemMirror.hasMember("parserUrl")) {
|
if (itemMirror.hasMember("parserUrl")) {
|
||||||
fileInfo.setParserUrl(itemMirror.getMember("parserUrl").toString());
|
fileInfo.setParserUrl(limitField(itemMirror.getMember("parserUrl")));
|
||||||
}
|
}
|
||||||
if (itemMirror.hasMember("previewUrl")) {
|
if (itemMirror.hasMember("previewUrl")) {
|
||||||
fileInfo.setPreviewUrl(itemMirror.getMember("previewUrl").toString());
|
fileInfo.setPreviewUrl(limitField(itemMirror.getMember("previewUrl")));
|
||||||
}
|
}
|
||||||
|
|
||||||
return fileInfo;
|
return fileInfo;
|
||||||
@@ -322,4 +526,22 @@ public class JsParserExecutor implements IPanTool {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String limitResultString(String value, String operation) {
|
||||||
|
if (value.length() > MAX_RESULT_STRING_LENGTH) {
|
||||||
|
throw new RuntimeException(operation + " 返回结果过大: " + value.length() + " 字符");
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String limitField(Object value) {
|
||||||
|
if (value == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String text = value.toString();
|
||||||
|
if (text.length() > MAX_FILE_FIELD_LENGTH) {
|
||||||
|
throw new RuntimeException("文件字段过长: " + text.length() + " 字符");
|
||||||
|
}
|
||||||
|
return text;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,6 @@ import cn.qaiu.entity.ShareLinkInfo;
|
|||||||
import io.vertx.core.Future;
|
import io.vertx.core.Future;
|
||||||
import io.vertx.core.Promise;
|
import io.vertx.core.Promise;
|
||||||
import io.vertx.core.json.JsonObject;
|
import io.vertx.core.json.JsonObject;
|
||||||
import org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory;
|
|
||||||
import org.openjdk.nashorn.api.scripting.ScriptObjectMirror;
|
import org.openjdk.nashorn.api.scripting.ScriptObjectMirror;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -21,20 +20,37 @@ import java.util.concurrent.*;
|
|||||||
*
|
*
|
||||||
* @author <a href="https://qaiu.top">QAIU</a>
|
* @author <a href="https://qaiu.top">QAIU</a>
|
||||||
*/
|
*/
|
||||||
public class JsPlaygroundExecutor {
|
public class JsPlaygroundExecutor implements AutoCloseable {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(JsPlaygroundExecutor.class);
|
private static final Logger log = LoggerFactory.getLogger(JsPlaygroundExecutor.class);
|
||||||
|
|
||||||
// JavaScript执行超时时间(秒)
|
// JavaScript执行超时时间(秒)
|
||||||
private static final long EXECUTION_TIMEOUT_SECONDS = 30;
|
private static final long EXECUTION_TIMEOUT_SECONDS = 30;
|
||||||
|
private static final int MAX_RESULT_STRING_LENGTH = 1024 * 1024;
|
||||||
|
private static final int MAX_FILE_LIST_SIZE = 1000;
|
||||||
|
private static final int MAX_FILE_FIELD_LENGTH = 4096;
|
||||||
|
private static final int TIMEOUT_LOG_RETAIN = 50;
|
||||||
|
|
||||||
// 使用独立的线程池,不受Vert.x的BlockedThreadChecker监控
|
// 使用有界线程池,防止线程无限增长导致内存溢出
|
||||||
private static final ExecutorService INDEPENDENT_EXECUTOR = Executors.newCachedThreadPool(r -> {
|
private static final int POOL_MAX_THREADS = 16;
|
||||||
|
private static final int POOL_QUEUE_CAPACITY = 256;
|
||||||
|
private static final ExecutorService INDEPENDENT_EXECUTOR = new ThreadPoolExecutor(
|
||||||
|
4, POOL_MAX_THREADS, 60L, TimeUnit.SECONDS,
|
||||||
|
new ArrayBlockingQueue<>(POOL_QUEUE_CAPACITY),
|
||||||
|
r -> {
|
||||||
Thread thread = new Thread(r);
|
Thread thread = new Thread(r);
|
||||||
thread.setName("playground-independent-" + System.currentTimeMillis());
|
thread.setName("playground-independent-" + thread.getId());
|
||||||
thread.setDaemon(true); // 设置为守护线程,服务关闭时自动清理
|
thread.setDaemon(true);
|
||||||
return thread;
|
return thread;
|
||||||
});
|
},
|
||||||
|
(r, executor) -> {
|
||||||
|
// 拒绝策略:记录日志并抛出异常,避免阻塞 Vert.x EventLoop
|
||||||
|
log.warn("演练场线程池已满,拒绝任务。活跃线程: {}, 队列大小: {}",
|
||||||
|
((ThreadPoolExecutor) executor).getActiveCount(),
|
||||||
|
((ThreadPoolExecutor) executor).getQueue().size());
|
||||||
|
throw new java.util.concurrent.RejectedExecutionException("演练场线程池已满,请稍后重试");
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
// 超时调度线程池,用于处理超时中断
|
// 超时调度线程池,用于处理超时中断
|
||||||
private static final ScheduledExecutorService TIMEOUT_SCHEDULER = Executors.newScheduledThreadPool(2, r -> {
|
private static final ScheduledExecutorService TIMEOUT_SCHEDULER = Executors.newScheduledThreadPool(2, r -> {
|
||||||
@@ -44,13 +60,28 @@ public class JsPlaygroundExecutor {
|
|||||||
return thread;
|
return thread;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭静态线程池(应在应用关闭时调用)
|
||||||
|
*/
|
||||||
|
public static void shutdownPools() {
|
||||||
|
INDEPENDENT_EXECUTOR.shutdown();
|
||||||
|
TIMEOUT_SCHEDULER.shutdown();
|
||||||
|
log.info("JsPlaygroundExecutor 线程池已关闭");
|
||||||
|
}
|
||||||
|
|
||||||
private final ShareLinkInfo shareLinkInfo;
|
private final ShareLinkInfo shareLinkInfo;
|
||||||
private final String jsCode;
|
private final String jsCode;
|
||||||
private final ScriptEngine engine;
|
private volatile ScriptEngine engine;
|
||||||
|
private final Object engineLock = new Object();
|
||||||
private final JsHttpClient httpClient;
|
private final JsHttpClient httpClient;
|
||||||
private final JsPlaygroundLogger playgroundLogger;
|
private final JsPlaygroundLogger playgroundLogger;
|
||||||
private final JsShareLinkInfoWrapper shareLinkInfoWrapper;
|
private final JsShareLinkInfoWrapper shareLinkInfoWrapper;
|
||||||
private final JsFetchBridge fetchBridge;
|
private final JsFetchBridge fetchBridge;
|
||||||
|
/** 标记是否已释放,防止重复关闭 */
|
||||||
|
private volatile boolean closed = false;
|
||||||
|
private final Object lifecycleLock = new Object();
|
||||||
|
private volatile boolean running = false;
|
||||||
|
private volatile boolean closeRequested = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建演练场执行器
|
* 创建演练场执行器
|
||||||
@@ -72,7 +103,6 @@ public class JsPlaygroundExecutor {
|
|||||||
this.playgroundLogger = new JsPlaygroundLogger();
|
this.playgroundLogger = new JsPlaygroundLogger();
|
||||||
this.shareLinkInfoWrapper = new JsShareLinkInfoWrapper(shareLinkInfo);
|
this.shareLinkInfoWrapper = new JsShareLinkInfoWrapper(shareLinkInfo);
|
||||||
this.fetchBridge = new JsFetchBridge(httpClient);
|
this.fetchBridge = new JsFetchBridge(httpClient);
|
||||||
this.engine = initEngine();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -80,15 +110,8 @@ public class JsPlaygroundExecutor {
|
|||||||
*/
|
*/
|
||||||
private ScriptEngine initEngine() {
|
private ScriptEngine initEngine() {
|
||||||
try {
|
try {
|
||||||
// 使用安全的ClassFilter创建Nashorn引擎
|
ScriptEngine engine = ParserScriptEngine.create();
|
||||||
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
|
this.engine = engine;
|
||||||
|
|
||||||
// 正确的方法签名: getScriptEngine(String[] args, ClassLoader appLoader, ClassFilter classFilter)
|
|
||||||
ScriptEngine engine = factory.getScriptEngine(new String[0], null, new SecurityClassFilter());
|
|
||||||
|
|
||||||
if (engine == null) {
|
|
||||||
throw new RuntimeException("无法创建JavaScript引擎,请确保Nashorn可用");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 注入Java对象到JavaScript环境
|
// 注入Java对象到JavaScript环境
|
||||||
engine.put("http", httpClient);
|
engine.put("http", httpClient);
|
||||||
@@ -96,14 +119,6 @@ public class JsPlaygroundExecutor {
|
|||||||
engine.put("shareLinkInfo", shareLinkInfoWrapper);
|
engine.put("shareLinkInfo", shareLinkInfoWrapper);
|
||||||
engine.put("JavaFetch", fetchBridge);
|
engine.put("JavaFetch", fetchBridge);
|
||||||
|
|
||||||
// 禁用Java对象访问
|
|
||||||
engine.eval("var Java = undefined;");
|
|
||||||
engine.eval("var JavaImporter = undefined;");
|
|
||||||
engine.eval("var Packages = undefined;");
|
|
||||||
engine.eval("var javax = undefined;");
|
|
||||||
engine.eval("var org = undefined;");
|
|
||||||
engine.eval("var com = undefined;");
|
|
||||||
|
|
||||||
// 加载fetch运行时(Promise和fetch API polyfill)
|
// 加载fetch运行时(Promise和fetch API polyfill)
|
||||||
String fetchRuntime = JsParserExecutor.loadFetchRuntime();
|
String fetchRuntime = JsParserExecutor.loadFetchRuntime();
|
||||||
if (!fetchRuntime.isEmpty()) {
|
if (!fetchRuntime.isEmpty()) {
|
||||||
@@ -134,8 +149,13 @@ public class JsPlaygroundExecutor {
|
|||||||
public Future<String> executeParseAsync() {
|
public Future<String> executeParseAsync() {
|
||||||
Promise<String> promise = Promise.promise();
|
Promise<String> promise = Promise.promise();
|
||||||
|
|
||||||
|
final CompletableFuture<String> executionFuture;
|
||||||
|
try {
|
||||||
// 使用独立的ExecutorService执行,避免Vert.x的BlockedThreadChecker输出警告
|
// 使用独立的ExecutorService执行,避免Vert.x的BlockedThreadChecker输出警告
|
||||||
CompletableFuture<String> executionFuture = CompletableFuture.supplyAsync(() -> {
|
executionFuture = CompletableFuture.supplyAsync(() -> {
|
||||||
|
beginExecution();
|
||||||
|
try {
|
||||||
|
ScriptEngine engine = engine();
|
||||||
playgroundLogger.infoJava("开始执行parse方法");
|
playgroundLogger.infoJava("开始执行parse方法");
|
||||||
try {
|
try {
|
||||||
Object parseFunction = engine.get("parse");
|
Object parseFunction = engine.get("parse");
|
||||||
@@ -151,8 +171,9 @@ public class JsPlaygroundExecutor {
|
|||||||
log.debug("[JsPlaygroundExecutor] parse函数执行完成,当前日志数量: {}", playgroundLogger.size());
|
log.debug("[JsPlaygroundExecutor] parse函数执行完成,当前日志数量: {}", playgroundLogger.size());
|
||||||
|
|
||||||
if (result instanceof String) {
|
if (result instanceof String) {
|
||||||
playgroundLogger.infoJava("解析成功,返回结果: " + result);
|
String resultText = limitResultString((String) result, "parse");
|
||||||
return (String) result;
|
playgroundLogger.infoJava("解析成功,返回结果长度: " + resultText.length());
|
||||||
|
return resultText;
|
||||||
} else {
|
} else {
|
||||||
String errorMsg = "parse方法返回值类型错误,期望String,实际: " +
|
String errorMsg = "parse方法返回值类型错误,期望String,实际: " +
|
||||||
(result != null ? result.getClass().getSimpleName() : "null");
|
(result != null ? result.getClass().getSimpleName() : "null");
|
||||||
@@ -167,16 +188,18 @@ public class JsPlaygroundExecutor {
|
|||||||
playgroundLogger.errorJava("执行parse方法失败: " + e.getMessage(), e);
|
playgroundLogger.errorJava("执行parse方法失败: " + e.getMessage(), e);
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}, INDEPENDENT_EXECUTOR);
|
} finally {
|
||||||
|
finishExecution();
|
||||||
// 创建超时任务,强制取消执行
|
|
||||||
ScheduledFuture<?> timeoutTask = TIMEOUT_SCHEDULER.schedule(() -> {
|
|
||||||
if (!executionFuture.isDone()) {
|
|
||||||
executionFuture.cancel(true); // 强制中断执行线程
|
|
||||||
playgroundLogger.errorJava("执行超时,已强制中断");
|
|
||||||
log.warn("JavaScript执行超时,已强制取消");
|
|
||||||
}
|
}
|
||||||
}, EXECUTION_TIMEOUT_SECONDS, TimeUnit.SECONDS);
|
}, INDEPENDENT_EXECUTOR);
|
||||||
|
} catch (java.util.concurrent.RejectedExecutionException e) {
|
||||||
|
log.warn("演练场线程池已满,任务被拒绝");
|
||||||
|
close(); // 释放已创建的 ScriptEngine 和 HttpClient 资源
|
||||||
|
promise.fail(new RuntimeException("演练场线程池已满,请稍后重试", e));
|
||||||
|
return promise.future();
|
||||||
|
}
|
||||||
|
|
||||||
|
ScheduledFuture<?> timeoutTask = scheduleTimeout(executionFuture, "parse");
|
||||||
|
|
||||||
// 处理执行结果
|
// 处理执行结果
|
||||||
executionFuture.whenComplete((result, error) -> {
|
executionFuture.whenComplete((result, error) -> {
|
||||||
@@ -185,7 +208,7 @@ public class JsPlaygroundExecutor {
|
|||||||
|
|
||||||
if (error != null) {
|
if (error != null) {
|
||||||
if (error instanceof CancellationException) {
|
if (error instanceof CancellationException) {
|
||||||
String timeoutMsg = "JavaScript执行超时(超过" + EXECUTION_TIMEOUT_SECONDS + "秒),已强制中断";
|
String timeoutMsg = "JavaScript执行超时(超过" + EXECUTION_TIMEOUT_SECONDS + "秒),已返回超时并停止外部HTTP资源;ScriptEngine将在执行线程退出后清理";
|
||||||
playgroundLogger.errorJava(timeoutMsg);
|
playgroundLogger.errorJava(timeoutMsg);
|
||||||
log.error(timeoutMsg);
|
log.error(timeoutMsg);
|
||||||
promise.fail(new RuntimeException(timeoutMsg));
|
promise.fail(new RuntimeException(timeoutMsg));
|
||||||
@@ -210,8 +233,13 @@ public class JsPlaygroundExecutor {
|
|||||||
public Future<List<FileInfo>> executeParseFileListAsync() {
|
public Future<List<FileInfo>> executeParseFileListAsync() {
|
||||||
Promise<List<FileInfo>> promise = Promise.promise();
|
Promise<List<FileInfo>> promise = Promise.promise();
|
||||||
|
|
||||||
|
final CompletableFuture<List<FileInfo>> executionFuture;
|
||||||
|
try {
|
||||||
// 使用独立的ExecutorService执行,避免Vert.x的BlockedThreadChecker输出警告
|
// 使用独立的ExecutorService执行,避免Vert.x的BlockedThreadChecker输出警告
|
||||||
CompletableFuture<List<FileInfo>> executionFuture = CompletableFuture.supplyAsync(() -> {
|
executionFuture = CompletableFuture.supplyAsync(() -> {
|
||||||
|
beginExecution();
|
||||||
|
try {
|
||||||
|
ScriptEngine engine = engine();
|
||||||
playgroundLogger.infoJava("开始执行parseFileList方法");
|
playgroundLogger.infoJava("开始执行parseFileList方法");
|
||||||
try {
|
try {
|
||||||
Object parseFileListFunction = engine.get("parseFileList");
|
Object parseFileListFunction = engine.get("parseFileList");
|
||||||
@@ -242,16 +270,18 @@ public class JsPlaygroundExecutor {
|
|||||||
playgroundLogger.errorJava("执行parseFileList方法失败: " + e.getMessage(), e);
|
playgroundLogger.errorJava("执行parseFileList方法失败: " + e.getMessage(), e);
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}, INDEPENDENT_EXECUTOR);
|
} finally {
|
||||||
|
finishExecution();
|
||||||
// 创建超时任务,强制取消执行
|
|
||||||
ScheduledFuture<?> timeoutTask = TIMEOUT_SCHEDULER.schedule(() -> {
|
|
||||||
if (!executionFuture.isDone()) {
|
|
||||||
executionFuture.cancel(true); // 强制中断执行线程
|
|
||||||
playgroundLogger.errorJava("执行超时,已强制中断");
|
|
||||||
log.warn("JavaScript执行超时,已强制取消");
|
|
||||||
}
|
}
|
||||||
}, EXECUTION_TIMEOUT_SECONDS, TimeUnit.SECONDS);
|
}, INDEPENDENT_EXECUTOR);
|
||||||
|
} catch (java.util.concurrent.RejectedExecutionException e) {
|
||||||
|
log.warn("演练场线程池已满,任务被拒绝");
|
||||||
|
close(); // 释放已创建的 ScriptEngine 和 HttpClient 资源
|
||||||
|
promise.fail(new RuntimeException("演练场线程池已满,请稍后重试", e));
|
||||||
|
return promise.future();
|
||||||
|
}
|
||||||
|
|
||||||
|
ScheduledFuture<?> timeoutTask = scheduleTimeout(executionFuture, "parseFileList");
|
||||||
|
|
||||||
// 处理执行结果
|
// 处理执行结果
|
||||||
executionFuture.whenComplete((result, error) -> {
|
executionFuture.whenComplete((result, error) -> {
|
||||||
@@ -260,7 +290,7 @@ public class JsPlaygroundExecutor {
|
|||||||
|
|
||||||
if (error != null) {
|
if (error != null) {
|
||||||
if (error instanceof CancellationException) {
|
if (error instanceof CancellationException) {
|
||||||
String timeoutMsg = "JavaScript执行超时(超过" + EXECUTION_TIMEOUT_SECONDS + "秒),已强制中断";
|
String timeoutMsg = "JavaScript执行超时(超过" + EXECUTION_TIMEOUT_SECONDS + "秒),已返回超时并停止外部HTTP资源;ScriptEngine将在执行线程退出后清理";
|
||||||
playgroundLogger.errorJava(timeoutMsg);
|
playgroundLogger.errorJava(timeoutMsg);
|
||||||
log.error(timeoutMsg);
|
log.error(timeoutMsg);
|
||||||
promise.fail(new RuntimeException(timeoutMsg));
|
promise.fail(new RuntimeException(timeoutMsg));
|
||||||
@@ -285,8 +315,13 @@ public class JsPlaygroundExecutor {
|
|||||||
public Future<String> executeParseByIdAsync() {
|
public Future<String> executeParseByIdAsync() {
|
||||||
Promise<String> promise = Promise.promise();
|
Promise<String> promise = Promise.promise();
|
||||||
|
|
||||||
|
final CompletableFuture<String> executionFuture;
|
||||||
|
try {
|
||||||
// 使用独立的ExecutorService执行,避免Vert.x的BlockedThreadChecker输出警告
|
// 使用独立的ExecutorService执行,避免Vert.x的BlockedThreadChecker输出警告
|
||||||
CompletableFuture<String> executionFuture = CompletableFuture.supplyAsync(() -> {
|
executionFuture = CompletableFuture.supplyAsync(() -> {
|
||||||
|
beginExecution();
|
||||||
|
try {
|
||||||
|
ScriptEngine engine = engine();
|
||||||
playgroundLogger.infoJava("开始执行parseById方法");
|
playgroundLogger.infoJava("开始执行parseById方法");
|
||||||
try {
|
try {
|
||||||
Object parseByIdFunction = engine.get("parseById");
|
Object parseByIdFunction = engine.get("parseById");
|
||||||
@@ -300,8 +335,9 @@ public class JsPlaygroundExecutor {
|
|||||||
Object result = parseByIdMirror.call(null, shareLinkInfoWrapper, httpClient, playgroundLogger);
|
Object result = parseByIdMirror.call(null, shareLinkInfoWrapper, httpClient, playgroundLogger);
|
||||||
|
|
||||||
if (result instanceof String) {
|
if (result instanceof String) {
|
||||||
playgroundLogger.infoJava("按ID解析成功: " + result);
|
String resultText = limitResultString((String) result, "parseById");
|
||||||
return (String) result;
|
playgroundLogger.infoJava("按ID解析成功,返回结果长度: " + resultText.length());
|
||||||
|
return resultText;
|
||||||
} else {
|
} else {
|
||||||
String errorMsg = "parseById方法返回值类型错误,期望String,实际: " +
|
String errorMsg = "parseById方法返回值类型错误,期望String,实际: " +
|
||||||
(result != null ? result.getClass().getSimpleName() : "null");
|
(result != null ? result.getClass().getSimpleName() : "null");
|
||||||
@@ -316,16 +352,18 @@ public class JsPlaygroundExecutor {
|
|||||||
playgroundLogger.errorJava("执行parseById方法失败: " + e.getMessage(), e);
|
playgroundLogger.errorJava("执行parseById方法失败: " + e.getMessage(), e);
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}, INDEPENDENT_EXECUTOR);
|
} finally {
|
||||||
|
finishExecution();
|
||||||
// 创建超时任务,强制取消执行
|
|
||||||
ScheduledFuture<?> timeoutTask = TIMEOUT_SCHEDULER.schedule(() -> {
|
|
||||||
if (!executionFuture.isDone()) {
|
|
||||||
executionFuture.cancel(true); // 强制中断执行线程
|
|
||||||
playgroundLogger.errorJava("执行超时,已强制中断");
|
|
||||||
log.warn("JavaScript执行超时,已强制取消");
|
|
||||||
}
|
}
|
||||||
}, EXECUTION_TIMEOUT_SECONDS, TimeUnit.SECONDS);
|
}, INDEPENDENT_EXECUTOR);
|
||||||
|
} catch (java.util.concurrent.RejectedExecutionException e) {
|
||||||
|
log.warn("演练场线程池已满,任务被拒绝");
|
||||||
|
close(); // 释放已创建的 ScriptEngine 和 HttpClient 资源
|
||||||
|
promise.fail(new RuntimeException("演练场线程池已满,请稍后重试", e));
|
||||||
|
return promise.future();
|
||||||
|
}
|
||||||
|
|
||||||
|
ScheduledFuture<?> timeoutTask = scheduleTimeout(executionFuture, "parseById");
|
||||||
|
|
||||||
// 处理执行结果
|
// 处理执行结果
|
||||||
executionFuture.whenComplete((result, error) -> {
|
executionFuture.whenComplete((result, error) -> {
|
||||||
@@ -334,7 +372,7 @@ public class JsPlaygroundExecutor {
|
|||||||
|
|
||||||
if (error != null) {
|
if (error != null) {
|
||||||
if (error instanceof CancellationException) {
|
if (error instanceof CancellationException) {
|
||||||
String timeoutMsg = "JavaScript执行超时(超过" + EXECUTION_TIMEOUT_SECONDS + "秒),已强制中断";
|
String timeoutMsg = "JavaScript执行超时(超过" + EXECUTION_TIMEOUT_SECONDS + "秒),已返回超时并停止外部HTTP资源;ScriptEngine将在执行线程退出后清理";
|
||||||
playgroundLogger.errorJava(timeoutMsg);
|
playgroundLogger.errorJava(timeoutMsg);
|
||||||
log.error(timeoutMsg);
|
log.error(timeoutMsg);
|
||||||
promise.fail(new RuntimeException(timeoutMsg));
|
promise.fail(new RuntimeException(timeoutMsg));
|
||||||
@@ -355,7 +393,7 @@ public class JsPlaygroundExecutor {
|
|||||||
*/
|
*/
|
||||||
public List<JsPlaygroundLogger.LogEntry> getLogs() {
|
public List<JsPlaygroundLogger.LogEntry> getLogs() {
|
||||||
List<JsPlaygroundLogger.LogEntry> logs = playgroundLogger.getLogs();
|
List<JsPlaygroundLogger.LogEntry> logs = playgroundLogger.getLogs();
|
||||||
System.out.println("[JsPlaygroundExecutor] 获取日志,数量: " + logs.size());
|
log.debug("获取日志,数量: {}", logs.size());
|
||||||
return logs;
|
return logs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -373,6 +411,9 @@ public class JsPlaygroundExecutor {
|
|||||||
List<FileInfo> fileList = new ArrayList<>();
|
List<FileInfo> fileList = new ArrayList<>();
|
||||||
|
|
||||||
if (resultMirror.isArray()) {
|
if (resultMirror.isArray()) {
|
||||||
|
if (resultMirror.size() > MAX_FILE_LIST_SIZE) {
|
||||||
|
throw new RuntimeException("文件列表数量超过限制: " + resultMirror.size());
|
||||||
|
}
|
||||||
for (int i = 0; i < resultMirror.size(); i++) {
|
for (int i = 0; i < resultMirror.size(); i++) {
|
||||||
Object item = resultMirror.get(String.valueOf(i));
|
Object item = resultMirror.get(String.valueOf(i));
|
||||||
if (item instanceof ScriptObjectMirror) {
|
if (item instanceof ScriptObjectMirror) {
|
||||||
@@ -396,13 +437,13 @@ public class JsPlaygroundExecutor {
|
|||||||
|
|
||||||
// 设置基本字段
|
// 设置基本字段
|
||||||
if (itemMirror.hasMember("fileName")) {
|
if (itemMirror.hasMember("fileName")) {
|
||||||
fileInfo.setFileName(itemMirror.getMember("fileName").toString());
|
fileInfo.setFileName(limitField(itemMirror.getMember("fileName")));
|
||||||
}
|
}
|
||||||
if (itemMirror.hasMember("fileId")) {
|
if (itemMirror.hasMember("fileId")) {
|
||||||
fileInfo.setFileId(itemMirror.getMember("fileId").toString());
|
fileInfo.setFileId(limitField(itemMirror.getMember("fileId")));
|
||||||
}
|
}
|
||||||
if (itemMirror.hasMember("fileType")) {
|
if (itemMirror.hasMember("fileType")) {
|
||||||
fileInfo.setFileType(itemMirror.getMember("fileType").toString());
|
fileInfo.setFileType(limitField(itemMirror.getMember("fileType")));
|
||||||
}
|
}
|
||||||
if (itemMirror.hasMember("size")) {
|
if (itemMirror.hasMember("size")) {
|
||||||
Object size = itemMirror.getMember("size");
|
Object size = itemMirror.getMember("size");
|
||||||
@@ -411,16 +452,16 @@ public class JsPlaygroundExecutor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (itemMirror.hasMember("sizeStr")) {
|
if (itemMirror.hasMember("sizeStr")) {
|
||||||
fileInfo.setSizeStr(itemMirror.getMember("sizeStr").toString());
|
fileInfo.setSizeStr(limitField(itemMirror.getMember("sizeStr")));
|
||||||
}
|
}
|
||||||
if (itemMirror.hasMember("createTime")) {
|
if (itemMirror.hasMember("createTime")) {
|
||||||
fileInfo.setCreateTime(itemMirror.getMember("createTime").toString());
|
fileInfo.setCreateTime(limitField(itemMirror.getMember("createTime")));
|
||||||
}
|
}
|
||||||
if (itemMirror.hasMember("updateTime")) {
|
if (itemMirror.hasMember("updateTime")) {
|
||||||
fileInfo.setUpdateTime(itemMirror.getMember("updateTime").toString());
|
fileInfo.setUpdateTime(limitField(itemMirror.getMember("updateTime")));
|
||||||
}
|
}
|
||||||
if (itemMirror.hasMember("createBy")) {
|
if (itemMirror.hasMember("createBy")) {
|
||||||
fileInfo.setCreateBy(itemMirror.getMember("createBy").toString());
|
fileInfo.setCreateBy(limitField(itemMirror.getMember("createBy")));
|
||||||
}
|
}
|
||||||
if (itemMirror.hasMember("downloadCount")) {
|
if (itemMirror.hasMember("downloadCount")) {
|
||||||
Object downloadCount = itemMirror.getMember("downloadCount");
|
Object downloadCount = itemMirror.getMember("downloadCount");
|
||||||
@@ -429,16 +470,16 @@ public class JsPlaygroundExecutor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (itemMirror.hasMember("fileIcon")) {
|
if (itemMirror.hasMember("fileIcon")) {
|
||||||
fileInfo.setFileIcon(itemMirror.getMember("fileIcon").toString());
|
fileInfo.setFileIcon(limitField(itemMirror.getMember("fileIcon")));
|
||||||
}
|
}
|
||||||
if (itemMirror.hasMember("panType")) {
|
if (itemMirror.hasMember("panType")) {
|
||||||
fileInfo.setPanType(itemMirror.getMember("panType").toString());
|
fileInfo.setPanType(limitField(itemMirror.getMember("panType")));
|
||||||
}
|
}
|
||||||
if (itemMirror.hasMember("parserUrl")) {
|
if (itemMirror.hasMember("parserUrl")) {
|
||||||
fileInfo.setParserUrl(itemMirror.getMember("parserUrl").toString());
|
fileInfo.setParserUrl(limitField(itemMirror.getMember("parserUrl")));
|
||||||
}
|
}
|
||||||
if (itemMirror.hasMember("previewUrl")) {
|
if (itemMirror.hasMember("previewUrl")) {
|
||||||
fileInfo.setPreviewUrl(itemMirror.getMember("previewUrl").toString());
|
fileInfo.setPreviewUrl(limitField(itemMirror.getMember("previewUrl")));
|
||||||
}
|
}
|
||||||
|
|
||||||
return fileInfo;
|
return fileInfo;
|
||||||
@@ -448,5 +489,135 @@ public class JsPlaygroundExecutor {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String limitResultString(String value, String operation) {
|
||||||
|
if (value.length() > MAX_RESULT_STRING_LENGTH) {
|
||||||
|
throw new RuntimeException(operation + " 返回结果过大: " + value.length() + " 字符");
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String limitField(Object value) {
|
||||||
|
if (value == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String text = value.toString();
|
||||||
|
if (text.length() > MAX_FILE_FIELD_LENGTH) {
|
||||||
|
throw new RuntimeException("文件字段过长: " + text.length() + " 字符");
|
||||||
|
}
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void beginExecution() {
|
||||||
|
synchronized (lifecycleLock) {
|
||||||
|
if (closed) {
|
||||||
|
throw new CancellationException("演练场执行器已关闭");
|
||||||
|
}
|
||||||
|
if (running) {
|
||||||
|
throw new IllegalStateException("演练场执行器已在运行");
|
||||||
|
}
|
||||||
|
running = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ScriptEngine engine() {
|
||||||
|
ScriptEngine current = engine;
|
||||||
|
if (current != null) {
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
synchronized (engineLock) {
|
||||||
|
if (closed) {
|
||||||
|
throw new CancellationException("演练场执行器已关闭");
|
||||||
|
}
|
||||||
|
if (engine == null) {
|
||||||
|
engine = initEngine();
|
||||||
|
}
|
||||||
|
return engine;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void finishExecution() {
|
||||||
|
synchronized (lifecycleLock) {
|
||||||
|
running = false;
|
||||||
|
if (closeRequested) {
|
||||||
|
doClose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ScheduledFuture<?> scheduleTimeout(CompletableFuture<?> executionFuture, String operation) {
|
||||||
|
// cancel(true) 只能请求中断,Nashorn 死循环不保证立即停止。
|
||||||
|
return TIMEOUT_SCHEDULER.schedule(() -> {
|
||||||
|
if (!executionFuture.isDone()) {
|
||||||
|
executionFuture.cancel(true);
|
||||||
|
playgroundLogger.errorJava(operation + " 执行超时,已请求取消并停止外部HTTP资源");
|
||||||
|
forceCloseAfterTimeout();
|
||||||
|
log.warn("JavaScript {} 执行超时,已请求取消;Nashorn长循环可能继续占用线程,ScriptEngine将在执行线程退出后清理", operation);
|
||||||
|
}
|
||||||
|
}, EXECUTION_TIMEOUT_SECONDS, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void forceCloseAfterTimeout() {
|
||||||
|
synchronized (lifecycleLock) {
|
||||||
|
closeRequested = true;
|
||||||
|
if (running || closed) {
|
||||||
|
closeExternalResources();
|
||||||
|
} else {
|
||||||
|
doClose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
playgroundLogger.trimToLast(TIMEOUT_LOG_RETAIN);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 释放资源(HttpClient 和 ScriptEngine),避免内存泄漏
|
||||||
|
* 幂等:可安全多次调用
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
synchronized (lifecycleLock) {
|
||||||
|
closeRequested = true;
|
||||||
|
if (running || closed) {
|
||||||
|
closeExternalResources();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
doClose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void doClose() {
|
||||||
|
if (closed) return;
|
||||||
|
closed = true;
|
||||||
|
closeRequested = false;
|
||||||
|
closeExternalResources();
|
||||||
|
cleanupEngine();
|
||||||
|
log.debug("JsPlaygroundExecutor 资源已释放");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void closeExternalResources() {
|
||||||
|
if (httpClient != null) {
|
||||||
|
httpClient.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void cleanupEngine() {
|
||||||
|
// 清除 ScriptEngine 的所有 bindings,释放 JS 运行时引用
|
||||||
|
if (engine != null) {
|
||||||
|
try {
|
||||||
|
// 清除注入的 Java 对象引用
|
||||||
|
engine.put("http", null);
|
||||||
|
engine.put("logger", null);
|
||||||
|
engine.put("shareLinkInfo", null);
|
||||||
|
engine.put("JavaFetch", null);
|
||||||
|
// 清除所有 ENGINE_SCOPE bindings,包括 eval 加载的 JS 函数
|
||||||
|
var bindings = engine.getBindings(javax.script.ScriptContext.ENGINE_SCOPE);
|
||||||
|
if (bindings != null) {
|
||||||
|
bindings.clear();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("清理 ScriptEngine bindings 失败: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4,6 +4,9 @@ import java.util.ArrayList;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 演练场日志收集器
|
* 演练场日志收集器
|
||||||
* 收集JavaScript执行过程中的日志信息
|
* 收集JavaScript执行过程中的日志信息
|
||||||
@@ -13,7 +16,11 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class JsPlaygroundLogger {
|
public class JsPlaygroundLogger {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(JsPlaygroundLogger.class);
|
||||||
|
|
||||||
// 使用线程安全的列表
|
// 使用线程安全的列表
|
||||||
|
private static final int MAX_LOG_SIZE = 1000;
|
||||||
|
private static final int MAX_LOG_MESSAGE_LENGTH = 4096;
|
||||||
private final List<LogEntry> logs = Collections.synchronizedList(new ArrayList<>());
|
private final List<LogEntry> logs = Collections.synchronizedList(new ArrayList<>());
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -56,7 +63,23 @@ public class JsPlaygroundLogger {
|
|||||||
if (obj == null) {
|
if (obj == null) {
|
||||||
return "null";
|
return "null";
|
||||||
}
|
}
|
||||||
return obj.toString();
|
String msg = obj.toString();
|
||||||
|
if (msg.length() <= MAX_LOG_MESSAGE_LENGTH) {
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
return msg.substring(0, MAX_LOG_MESSAGE_LENGTH) + "...[truncated]";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加日志条目,超过最大容量时移除最早的条目
|
||||||
|
*/
|
||||||
|
private void addLog(LogEntry entry) {
|
||||||
|
synchronized (logs) {
|
||||||
|
if (logs.size() >= MAX_LOG_SIZE) {
|
||||||
|
logs.remove(0);
|
||||||
|
}
|
||||||
|
logs.add(entry);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -67,8 +90,8 @@ public class JsPlaygroundLogger {
|
|||||||
*/
|
*/
|
||||||
private void log(String level, Object message, String source) {
|
private void log(String level, Object message, String source) {
|
||||||
String msg = toString(message);
|
String msg = toString(message);
|
||||||
logs.add(new LogEntry(level, msg, source));
|
addLog(new LogEntry(level, msg, source));
|
||||||
System.out.println("[" + source + "PlaygroundLogger] " + level + ": " + msg);
|
log.debug("[{}PlaygroundLogger] {}: {}", source, level, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -109,10 +132,10 @@ public class JsPlaygroundLogger {
|
|||||||
public void error(Object message, Throwable throwable) {
|
public void error(Object message, Throwable throwable) {
|
||||||
String msg = toString(message);
|
String msg = toString(message);
|
||||||
if (throwable != null) {
|
if (throwable != null) {
|
||||||
msg = msg + ": " + throwable.getMessage();
|
msg = toString(msg + ": " + throwable.getMessage());
|
||||||
}
|
}
|
||||||
logs.add(new LogEntry("ERROR", msg, "JS"));
|
addLog(new LogEntry("ERROR", msg, "JS"));
|
||||||
System.out.println("[JSPlaygroundLogger] ERROR: " + msg);
|
log.debug("[JSPlaygroundLogger] ERROR: {}", msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===== 以下是供Java层调用的内部方法 =====
|
// ===== 以下是供Java层调用的内部方法 =====
|
||||||
@@ -149,12 +172,12 @@ public class JsPlaygroundLogger {
|
|||||||
* 错误日志(带异常,供Java层调用)
|
* 错误日志(带异常,供Java层调用)
|
||||||
*/
|
*/
|
||||||
public void errorJava(String message, Throwable throwable) {
|
public void errorJava(String message, Throwable throwable) {
|
||||||
String msg = message;
|
String msg = toString(message);
|
||||||
if (throwable != null) {
|
if (throwable != null) {
|
||||||
msg = msg + ": " + throwable.getMessage();
|
msg = toString(msg + ": " + throwable.getMessage());
|
||||||
}
|
}
|
||||||
logs.add(new LogEntry("ERROR", msg, "JAVA"));
|
addLog(new LogEntry("ERROR", msg, "JAVA"));
|
||||||
System.out.println("[JAVAPlaygroundLogger] ERROR: " + msg);
|
log.debug("[JAVAPlaygroundLogger] ERROR: {}", msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -179,4 +202,17 @@ public class JsPlaygroundLogger {
|
|||||||
public void clear() {
|
public void clear() {
|
||||||
logs.clear();
|
logs.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void trimToLast(int maxEntries) {
|
||||||
|
if (maxEntries < 0) {
|
||||||
|
throw new IllegalArgumentException("maxEntries不能小于0");
|
||||||
|
}
|
||||||
|
synchronized (logs) {
|
||||||
|
int removeCount = logs.size() - maxEntries;
|
||||||
|
if (removeCount <= 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
logs.subList(0, removeCount).clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -31,6 +31,8 @@ public class JsScriptLoader {
|
|||||||
|
|
||||||
private static final String RESOURCE_PATH = "custom-parsers";
|
private static final String RESOURCE_PATH = "custom-parsers";
|
||||||
private static final String EXTERNAL_PATH = "./custom-parsers";
|
private static final String EXTERNAL_PATH = "./custom-parsers";
|
||||||
|
private static final long MAX_SCRIPT_SIZE_BYTES = 128 * 1024;
|
||||||
|
private static final int MAX_EXTERNAL_SCRIPT_COUNT = 100;
|
||||||
|
|
||||||
// 系统属性配置的外部目录路径
|
// 系统属性配置的外部目录路径
|
||||||
private static final String EXTERNAL_PATH_PROPERTY = "parser.custom-parsers.path";
|
private static final String EXTERNAL_PATH_PROPERTY = "parser.custom-parsers.path";
|
||||||
@@ -83,13 +85,15 @@ public class JsScriptLoader {
|
|||||||
.getResourceAsStream(resourceFile);
|
.getResourceAsStream(resourceFile);
|
||||||
|
|
||||||
if (inputStream != null) {
|
if (inputStream != null) {
|
||||||
String jsCode = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
|
try (inputStream) {
|
||||||
|
String jsCode = readResourceScript(inputStream, resourceFile);
|
||||||
CustomParserConfig config = JsScriptMetadataParser.parseScript(jsCode);
|
CustomParserConfig config = JsScriptMetadataParser.parseScript(jsCode);
|
||||||
configs.add(config);
|
configs.add(config);
|
||||||
|
|
||||||
String fileName = resourceFile.substring(resourceFile.lastIndexOf('/') + 1);
|
String fileName = resourceFile.substring(resourceFile.lastIndexOf('/') + 1);
|
||||||
log.debug("从资源目录加载脚本: {}", fileName);
|
log.debug("从资源目录加载脚本: {}", fileName);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("加载资源脚本失败: {}", resourceFile, e);
|
log.warn("加载资源脚本失败: {}", resourceFile, e);
|
||||||
}
|
}
|
||||||
@@ -139,8 +143,8 @@ public class JsScriptLoader {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
String jarPath = jarUrl.getPath().substring(5, jarUrl.getPath().indexOf("!"));
|
String jarPath = jarUrl.getPath().substring(5, jarUrl.getPath().indexOf("!"));
|
||||||
JarFile jarFile = new JarFile(jarPath);
|
|
||||||
|
|
||||||
|
try (JarFile jarFile = new JarFile(jarPath)) {
|
||||||
Enumeration<JarEntry> entries = jarFile.entries();
|
Enumeration<JarEntry> entries = jarFile.entries();
|
||||||
while (entries.hasMoreElements()) {
|
while (entries.hasMoreElements()) {
|
||||||
JarEntry entry = entries.nextElement();
|
JarEntry entry = entries.nextElement();
|
||||||
@@ -152,8 +156,7 @@ public class JsScriptLoader {
|
|||||||
resourceFiles.add(entryName);
|
resourceFiles.add(entryName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
jarFile.close();
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.debug("解析JAR包资源文件失败", e);
|
log.debug("解析JAR包资源文件失败", e);
|
||||||
}
|
}
|
||||||
@@ -208,8 +211,10 @@ public class JsScriptLoader {
|
|||||||
paths.filter(Files::isRegularFile)
|
paths.filter(Files::isRegularFile)
|
||||||
.filter(path -> path.toString().endsWith(".js"))
|
.filter(path -> path.toString().endsWith(".js"))
|
||||||
.filter(path -> !isExcludedFile(path.getFileName().toString()))
|
.filter(path -> !isExcludedFile(path.getFileName().toString()))
|
||||||
|
.limit(MAX_EXTERNAL_SCRIPT_COUNT)
|
||||||
.forEach(path -> {
|
.forEach(path -> {
|
||||||
try {
|
try {
|
||||||
|
ensureScriptSize(path);
|
||||||
String jsCode = Files.readString(path, StandardCharsets.UTF_8);
|
String jsCode = Files.readString(path, StandardCharsets.UTF_8);
|
||||||
CustomParserConfig config = JsScriptMetadataParser.parseScript(jsCode);
|
CustomParserConfig config = JsScriptMetadataParser.parseScript(jsCode);
|
||||||
configs.add(config);
|
configs.add(config);
|
||||||
@@ -263,6 +268,7 @@ public class JsScriptLoader {
|
|||||||
throw new IllegalArgumentException("文件不存在: " + filePath);
|
throw new IllegalArgumentException("文件不存在: " + filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ensureScriptSize(path);
|
||||||
String jsCode = Files.readString(path, StandardCharsets.UTF_8);
|
String jsCode = Files.readString(path, StandardCharsets.UTF_8);
|
||||||
return JsScriptMetadataParser.parseScript(jsCode);
|
return JsScriptMetadataParser.parseScript(jsCode);
|
||||||
|
|
||||||
@@ -285,8 +291,10 @@ public class JsScriptLoader {
|
|||||||
throw new IllegalArgumentException("资源文件不存在: " + resourcePath);
|
throw new IllegalArgumentException("资源文件不存在: " + resourcePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
String jsCode = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
|
try (inputStream) {
|
||||||
|
String jsCode = readResourceScript(inputStream, resourcePath);
|
||||||
return JsScriptMetadataParser.parseScript(jsCode);
|
return JsScriptMetadataParser.parseScript(jsCode);
|
||||||
|
}
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException("读取资源文件失败: " + resourcePath, e);
|
throw new RuntimeException("读取资源文件失败: " + resourcePath, e);
|
||||||
@@ -347,4 +355,19 @@ public class JsScriptLoader {
|
|||||||
fileName.contains(".test.") ||
|
fileName.contains(".test.") ||
|
||||||
fileName.contains(".spec.");
|
fileName.contains(".spec.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void ensureScriptSize(Path path) throws IOException {
|
||||||
|
long size = Files.size(path);
|
||||||
|
if (size > MAX_SCRIPT_SIZE_BYTES) {
|
||||||
|
throw new IllegalArgumentException("JavaScript脚本超过128KB限制: " + path.getFileName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String readResourceScript(InputStream inputStream, String name) throws IOException {
|
||||||
|
byte[] bytes = inputStream.readNBytes((int) MAX_SCRIPT_SIZE_BYTES + 1);
|
||||||
|
if (bytes.length > MAX_SCRIPT_SIZE_BYTES) {
|
||||||
|
throw new IllegalArgumentException("JavaScript资源脚本超过128KB限制: " + name);
|
||||||
|
}
|
||||||
|
return new String(bytes, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loaded 100 of 326 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user