mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-02-28 01:29:53 +00:00
Compare commits
520 Commits
v1.19.5
...
Prerelease
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3035ae89e3 | ||
|
|
c251e411e5 | ||
|
|
f6722ab79b | ||
|
|
4ca515896b | ||
|
|
836c972c54 | ||
|
|
43509da1a9 | ||
|
|
3752cb044f | ||
|
|
30391b40c4 | ||
|
|
05fbf552ec | ||
|
|
e4143cf1ad | ||
|
|
5eaf5d16ce | ||
|
|
9dee264f13 | ||
|
|
50480406cf | ||
|
|
6eb27ac3dc | ||
|
|
a949ad883c | ||
|
|
20bf57c117 | ||
|
|
60a9312057 | ||
|
|
9fda032a28 | ||
|
|
c3399fd346 | ||
|
|
445083b624 | ||
|
|
97f25250a6 | ||
|
|
8b0bcb6740 | ||
|
|
022f677385 | ||
|
|
32799662ad | ||
|
|
86257fc83c | ||
|
|
f2222b5e02 | ||
|
|
3ac62152cb | ||
|
|
5516ca18fd | ||
|
|
3bca69c745 | ||
|
|
558b3840ea | ||
|
|
f94da9f2b3 | ||
|
|
2cfc4ba044 | ||
|
|
034f1d1e9b | ||
|
|
dede56fe4b | ||
|
|
5fda87d50e | ||
|
|
5e1b133e4e | ||
|
|
27a3ca6afc | ||
|
|
7573affdd4 | ||
|
|
17fbaf9100 | ||
|
|
710772f993 | ||
|
|
d36b024b10 | ||
|
|
f52c9356c2 | ||
|
|
e45c896185 | ||
|
|
d18a14afeb | ||
|
|
6aaabc97ca | ||
|
|
85c024a4a6 | ||
|
|
c33c90d7af | ||
|
|
65c3d3e4e2 | ||
|
|
98b3060558 | ||
|
|
b90100645e | ||
|
|
46ee1649c0 | ||
|
|
e3b8fc2b77 | ||
|
|
707fe8b207 | ||
|
|
1e1434d1de | ||
|
|
26052ba5e5 | ||
|
|
75a0cd5aff | ||
|
|
e03ba23f65 | ||
|
|
0c995a2479 | ||
|
|
3c526ae06e | ||
|
|
0b009b514c | ||
|
|
18d139a15d | ||
|
|
5f250413fe | ||
|
|
993595df73 | ||
|
|
828fd30dc3 | ||
|
|
11000dccd7 | ||
|
|
0818aa54aa | ||
|
|
edbfebeacd | ||
|
|
06f5fbac06 | ||
|
|
f38fc2020f | ||
|
|
97bce45eba | ||
|
|
bc28cd486a | ||
|
|
cdabd1e8b1 | ||
|
|
c5b0f00bb2 | ||
|
|
c128d23dec | ||
|
|
ee37a353d0 | ||
|
|
0cf37de1a8 | ||
|
|
ae6069c178 | ||
|
|
c8e33a4347 | ||
|
|
19a6b5d6f7 | ||
|
|
efb800866e | ||
|
|
94c8d60f72 | ||
|
|
0f2baca2de | ||
|
|
b18a33552c | ||
|
|
487de9b548 | ||
|
|
1a6230ec03 | ||
|
|
e6bf56b9af | ||
|
|
0ad9ac325a | ||
|
|
d6b1263236 | ||
|
|
4d7670339b | ||
|
|
0cffc8d76d | ||
|
|
1f8bee9710 | ||
|
|
eb30d3f331 | ||
|
|
10f4bebdfa | ||
|
|
06387d5045 | ||
|
|
c393e917eb | ||
|
|
4f0a6fa117 | ||
|
|
4f9bfd216f | ||
|
|
498f81aad3 | ||
|
|
9168bee6b7 | ||
|
|
e6c0e3b19c | ||
|
|
287f9e5185 | ||
|
|
c456370f4f | ||
|
|
10ef29f5cd | ||
|
|
85ba7f6a0a | ||
|
|
7daf37bc15 | ||
|
|
64015b7634 | ||
|
|
5585304d68 | ||
|
|
911211578c | ||
|
|
abb55199f2 | ||
|
|
87c3f700e5 | ||
|
|
4a723e8d3f | ||
|
|
93cf46e430 | ||
|
|
35a1130c92 | ||
|
|
1ebcb25e4a | ||
|
|
cbcacdbb8c | ||
|
|
17966b5418 | ||
|
|
bc8f0dcf77 | ||
|
|
827cd616e8 | ||
|
|
e1384e86ab | ||
|
|
b92b38701c | ||
|
|
1cab34d257 | ||
|
|
a06097c2c4 | ||
|
|
bc9db11cb4 | ||
|
|
69e301820c | ||
|
|
e7a04e0762 | ||
|
|
7e8c2876fb | ||
|
|
936ebc7718 | ||
|
|
b753a57e6a | ||
|
|
dd99bfc892 | ||
|
|
2a1b3b2aed | ||
|
|
2211789a7c | ||
|
|
e652e277a7 | ||
|
|
40863d248d | ||
|
|
17b8eb8772 | ||
|
|
6b40072bc5 | ||
|
|
f44aa22d50 | ||
|
|
c33d9ad857 | ||
|
|
25041b599e | ||
|
|
6539b509cb | ||
|
|
d2007fdc22 | ||
|
|
b5fa3ee99a | ||
|
|
91f5593f4e | ||
|
|
90470ac304 | ||
|
|
b509affe5b | ||
|
|
32ce513977 | ||
|
|
30891f8781 | ||
|
|
e4cdb9b600 | ||
|
|
d33dbbe2f9 | ||
|
|
d8dcaa7500 | ||
|
|
9df8392c65 | ||
|
|
fdb7cb1f58 | ||
|
|
7cd58fbdf6 | ||
|
|
bc719eb96d | ||
|
|
ac90543548 | ||
|
|
9a5e506f66 | ||
|
|
a001b1b110 | ||
|
|
d1f89fa05e | ||
|
|
e2796e2d5c | ||
|
|
93de49d20c | ||
|
|
4d3167ff2f | ||
|
|
5998956a72 | ||
|
|
6cf1743961 | ||
|
|
8b6ba22b90 | ||
|
|
7571c87afb | ||
|
|
d4d2c062a3 | ||
|
|
438d4138d6 | ||
|
|
140d892ccf | ||
|
|
5aa140c493 | ||
|
|
c107c6a824 | ||
|
|
f6e494e73f | ||
|
|
0b3159bf9b | ||
|
|
45fd628788 | ||
|
|
2f545ef634 | ||
|
|
054e63cb3f | ||
|
|
d48bcf1e1e | ||
|
|
0df2f79ece | ||
|
|
644c04fdc9 | ||
|
|
926aaec717 | ||
|
|
a4b76809ac | ||
|
|
ff76576cbe | ||
|
|
1d5890abc1 | ||
|
|
a3c023ae3e | ||
|
|
8b32c4371e | ||
|
|
5a285acd32 | ||
|
|
c2209d68f7 | ||
|
|
fd39c2a7fc | ||
|
|
421dc79aea | ||
|
|
27b47f976c | ||
|
|
c25a38898f | ||
|
|
f3edbc2b45 | ||
|
|
6fb1f796a5 | ||
|
|
99e68e9983 | ||
|
|
6bffbdd9d3 | ||
|
|
cfdaebe952 | ||
|
|
c8af92a01f | ||
|
|
ff62386f6b | ||
|
|
85c56e7446 | ||
|
|
9ed9c3d1c3 | ||
|
|
dcfe664a7d | ||
|
|
fb1ae21fb7 | ||
|
|
90f47a6d0c | ||
|
|
f2bf4a077e | ||
|
|
8701639347 | ||
|
|
5bc0ac7281 | ||
|
|
c5fe3670ef | ||
|
|
de2ff37f4f | ||
|
|
da69b192f2 | ||
|
|
c13549f564 | ||
|
|
ce168c0e67 | ||
|
|
d225625378 | ||
|
|
40e0813869 | ||
|
|
94b591ed44 | ||
|
|
f7bd8b83e5 | ||
|
|
f45c6f5e91 | ||
|
|
3b15cbd9eb | ||
|
|
6f4da5f1fb | ||
|
|
fdc46f0471 | ||
|
|
57b527d54a | ||
|
|
29eaa4d699 | ||
|
|
92ecdfcc00 | ||
|
|
0992ee8adf | ||
|
|
0c25831726 | ||
|
|
9cc208bd47 | ||
|
|
b57f3052a8 | ||
|
|
3a1caf1316 | ||
|
|
1b1f95aa9c | ||
|
|
8a9300d44e | ||
|
|
abe6c3bb35 | ||
|
|
e28c8e6a51 | ||
|
|
74a86f147b | ||
|
|
7917f24f42 | ||
|
|
0dc5e3051d | ||
|
|
40b2cde2b2 | ||
|
|
6786705212 | ||
|
|
00638f30a7 | ||
|
|
2222d0e3fa | ||
|
|
f3ebd5c489 | ||
|
|
74e64d31e3 | ||
|
|
0c556bcaf3 | ||
|
|
7e71d21ab4 | ||
|
|
30bead4e2e | ||
|
|
16836ea465 | ||
|
|
d6f1af5372 | ||
|
|
26335f8ecc | ||
|
|
c4449a9f62 | ||
|
|
5e17d6fe01 | ||
|
|
8cdfd87d1e | ||
|
|
f02766a765 | ||
|
|
cea29e2615 | ||
|
|
a0f1ac4ef5 | ||
|
|
57e14e5b62 | ||
|
|
08fc100c85 | ||
|
|
571be856ea | ||
|
|
ad69ee84a9 | ||
|
|
dd7b3c28ad | ||
|
|
1b99759eaf | ||
|
|
a8f7e25851 | ||
|
|
909729ca8f | ||
|
|
6c527f8d20 | ||
|
|
7061c5a8ea | ||
|
|
23448ec119 | ||
|
|
318b3524c3 | ||
|
|
1d09ed82f1 | ||
|
|
b27325eed0 | ||
|
|
0d3d31dc5f | ||
|
|
108bf645fa | ||
|
|
50e1afd963 | ||
|
|
0336d64e52 | ||
|
|
02d954bfa8 | ||
|
|
9a124a390f | ||
|
|
fed4b369a3 | ||
|
|
f8ee5c1e15 | ||
|
|
8eba1c8afd | ||
|
|
65d3920f02 | ||
|
|
7e9e12cc4c | ||
|
|
80a90f05f3 | ||
|
|
3b63fef2eb | ||
|
|
29872007b3 | ||
|
|
33cde6592e | ||
|
|
c98f5f44b7 | ||
|
|
d094075c88 | ||
|
|
4188277b61 | ||
|
|
545d9b844d | ||
|
|
472cefb6d7 | ||
|
|
ccff0035cb | ||
|
|
455f2136f1 | ||
|
|
63781b3a6a | ||
|
|
c6e596f33f | ||
|
|
45cb45accb | ||
|
|
5c73025b53 | ||
|
|
cdd02a90c3 | ||
|
|
0ced98da4d | ||
|
|
84086a6e6c | ||
|
|
443200a51e | ||
|
|
aca0d97beb | ||
|
|
2605bf78f9 | ||
|
|
d2395fb43a | ||
|
|
e3d9a8e2fd | ||
|
|
1ae050ca3b | ||
|
|
7f38763e22 | ||
|
|
2a8831b0d0 | ||
|
|
cdf5e0c73e | ||
|
|
48f3ea8bc9 | ||
|
|
375e160368 | ||
|
|
b31664beeb | ||
|
|
7960bcae15 | ||
|
|
664ddb8d55 | ||
|
|
e4dfe09744 | ||
|
|
b56068ee1c | ||
|
|
99e888c829 | ||
|
|
873d0deeaa | ||
|
|
7e0a77c99c | ||
|
|
5f09db2655 | ||
|
|
10174d281c | ||
|
|
12c30acdda | ||
|
|
2790481709 | ||
|
|
182f60d424 | ||
|
|
930c70f065 | ||
|
|
fc61715e4e | ||
|
|
438be2d379 | ||
|
|
4e20ed65f2 | ||
|
|
ce760fcf19 | ||
|
|
0f76fdf4c5 | ||
|
|
03f4513f61 | ||
|
|
26f603057f | ||
|
|
b481eca4a4 | ||
|
|
eb028b65fc | ||
|
|
48c1b1cdb2 | ||
|
|
76e40baebc | ||
|
|
946b4025df | ||
|
|
089766b285 | ||
|
|
b643388539 | ||
|
|
0836ec6ee3 | ||
|
|
eeb2ad8dae | ||
|
|
71290b057f | ||
|
|
41b321dfe1 | ||
|
|
a18e99f966 | ||
|
|
f90d0b954c | ||
|
|
0408da2aee | ||
|
|
335d54e488 | ||
|
|
d11f9c895c | ||
|
|
e54ca7ceca | ||
|
|
ce82d49c25 | ||
|
|
8e6be1992b | ||
|
|
0e9102daae | ||
|
|
46dccf26d1 | ||
|
|
854c6a13c3 | ||
|
|
b4c3bbf660 | ||
|
|
6c726d6436 | ||
|
|
a0bdb861a9 | ||
|
|
eca5a27774 | ||
|
|
16d95df100 | ||
|
|
9b90719ddd | ||
|
|
7392529677 | ||
|
|
dc52c38179 | ||
|
|
d7999a32d3 | ||
|
|
b41ea05481 | ||
|
|
e6fe895190 | ||
|
|
adf553a958 | ||
|
|
2a915a5c94 | ||
|
|
1b0c72bfab | ||
|
|
e89af723cd | ||
|
|
e8fddd85af | ||
|
|
f04af734e3 | ||
|
|
00035302a1 | ||
|
|
578e659bb9 | ||
|
|
0f1baeb935 | ||
|
|
16ff9e815b | ||
|
|
5f1f296213 | ||
|
|
66fd5c9f0c | ||
|
|
c3a3009a8c | ||
|
|
01cd7e2c0e | ||
|
|
deec7aafe5 | ||
|
|
a9b7e705f0 | ||
|
|
fb043df1b6 | ||
|
|
748b5df902 | ||
|
|
8cbae59d55 | ||
|
|
a37440c81b | ||
|
|
dbb002a5ba | ||
|
|
1a84153213 | ||
|
|
dfe6e0509b | ||
|
|
b6dde7ded7 | ||
|
|
9f1da11792 | ||
|
|
63ad95e10f | ||
|
|
b06ec5bef8 | ||
|
|
d4fbffd8e8 | ||
|
|
305020175d | ||
|
|
79decdc253 | ||
|
|
407c13b8a4 | ||
|
|
d84b182be3 | ||
|
|
8f18d3f6db | ||
|
|
b9260e06b8 | ||
|
|
6337151207 | ||
|
|
aa555ced5f | ||
|
|
349b773b40 | ||
|
|
300eb8b12a | ||
|
|
2b84dd3618 | ||
|
|
6a620ba287 | ||
|
|
56c3462b76 | ||
|
|
6f4fe71e41 | ||
|
|
ba3e7187a6 | ||
|
|
0d92b6724b | ||
|
|
241ae92bce | ||
|
|
91985c1ef8 | ||
|
|
6a9d428991 | ||
|
|
765cbbcc01 | ||
|
|
5b975275f5 | ||
|
|
166392fe17 | ||
|
|
5c6aa433ca | ||
|
|
2c55dc2557 | ||
|
|
56c0b088e8 | ||
|
|
5344e869a8 | ||
|
|
6cfaf15cbf | ||
|
|
31f0060b30 | ||
|
|
c60750d549 | ||
|
|
ebf5918e94 | ||
|
|
93ca18517c | ||
|
|
32d447ce99 | ||
|
|
617fef84ae | ||
|
|
d19199322d | ||
|
|
87795e3a07 | ||
|
|
85bb40aaf8 | ||
|
|
082bcec281 | ||
|
|
9283cb0f5f | ||
|
|
ae7967f662 | ||
|
|
01f8f2db2f | ||
|
|
255ff5e977 | ||
|
|
939e4109d7 | ||
|
|
40587b62b8 | ||
|
|
85e6d25de5 | ||
|
|
29a37f4f4b | ||
|
|
2f9a3b3469 | ||
|
|
40ea0ba098 | ||
|
|
8d7f947a80 | ||
|
|
71a8705636 | ||
|
|
c0f452b540 | ||
|
|
6c9abe16cc | ||
|
|
213d80c1e2 | ||
|
|
1db89da122 | ||
|
|
689c58f661 | ||
|
|
33590c4066 | ||
|
|
60ae9dce56 | ||
|
|
4741ac6702 | ||
|
|
ef3d7e4dd7 | ||
|
|
a1c7881229 | ||
|
|
12e3952b74 | ||
|
|
88419cbd12 | ||
|
|
4ed830330e | ||
|
|
3ed6ff9402 | ||
|
|
34de62d21d | ||
|
|
d2e255f257 | ||
|
|
a0c46bb4b7 | ||
|
|
9e3bf14b1a | ||
|
|
28c387a9b6 | ||
|
|
15eda703b4 | ||
|
|
b1d12a15db | ||
|
|
5a21bf3642 | ||
|
|
199fb8fd5d | ||
|
|
fd959feff2 | ||
|
|
d5a03901d2 | ||
|
|
257fead538 | ||
|
|
c489c5260b | ||
|
|
8f92b1de13 | ||
|
|
9f7a2a36c1 | ||
|
|
a93479124c | ||
|
|
ed42c4feb8 | ||
|
|
608ddb1b44 | ||
|
|
d036d98128 | ||
|
|
d900c71214 | ||
|
|
1672750c47 | ||
|
|
41b57afb3f | ||
|
|
188372cb04 | ||
|
|
a1350d4985 | ||
|
|
dc958e6a39 | ||
|
|
8a5f3b8909 | ||
|
|
c6d7ef8cb8 | ||
|
|
bb8c47d83d | ||
|
|
5cf0f18c29 | ||
|
|
83213d493e | ||
|
|
90ed01ed53 | ||
|
|
f91a586da8 | ||
|
|
266fb03838 | ||
|
|
76e9607fd7 | ||
|
|
23e2d3a132 | ||
|
|
6e35cf9399 | ||
|
|
2116640886 | ||
|
|
a4fcd3af07 | ||
|
|
d22a893060 | ||
|
|
00cceba890 | ||
|
|
2b4726b9ad | ||
|
|
26e6d83f8b | ||
|
|
50d7834e09 | ||
|
|
86c127db8b | ||
|
|
febb6021aa | ||
|
|
9e57b298bf | ||
|
|
791ea5e568 | ||
|
|
7e7016b567 | ||
|
|
b4fe669848 | ||
|
|
cad26ac6a8 | ||
|
|
f328203bc1 | ||
|
|
5c40a6340c | ||
|
|
61d6a9abd6 | ||
|
|
a013ac32a3 | ||
|
|
ee5d77cfd1 | ||
|
|
936df90ace | ||
|
|
f774276896 | ||
|
|
aa51b9faba | ||
|
|
d55b047125 | ||
|
|
efc7abc6e0 | ||
|
|
c2301f66a4 | ||
|
|
468cfc3cc4 | ||
|
|
5dce957755 | ||
|
|
4ecb49b3b9 | ||
|
|
7de4af28d2 | ||
|
|
48d8efb3e9 | ||
|
|
e6e7aa5ae2 | ||
|
|
99aa1b0de1 | ||
|
|
52ad793d11 | ||
|
|
2fb9331211 | ||
|
|
793ce45db0 |
182
.github/patch/go1.21.patch
vendored
Normal file
182
.github/patch/go1.21.patch
vendored
Normal file
@@ -0,0 +1,182 @@
|
|||||||
|
Subject: [PATCH] Revert "[release-branch.go1.21] crypto/rand,runtime: switch RtlGenRandom for ProcessPrng"
|
||||||
|
---
|
||||||
|
Index: src/crypto/rand/rand.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/crypto/rand/rand.go b/src/crypto/rand/rand.go
|
||||||
|
--- a/src/crypto/rand/rand.go (revision 8bba868de983dd7bf55fcd121495ba8d6e2734e7)
|
||||||
|
+++ b/src/crypto/rand/rand.go (revision 7e6c963d81e14ee394402671d4044b2940c8d2c1)
|
||||||
|
@@ -15,7 +15,7 @@
|
||||||
|
// available, /dev/urandom otherwise.
|
||||||
|
// On OpenBSD and macOS, Reader uses getentropy(2).
|
||||||
|
// On other Unix-like systems, Reader reads from /dev/urandom.
|
||||||
|
-// On Windows systems, Reader uses the ProcessPrng API.
|
||||||
|
+// On Windows systems, Reader uses the RtlGenRandom API.
|
||||||
|
// On JS/Wasm, Reader uses the Web Crypto API.
|
||||||
|
// On WASIP1/Wasm, Reader uses random_get from wasi_snapshot_preview1.
|
||||||
|
var Reader io.Reader
|
||||||
|
Index: src/crypto/rand/rand_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/crypto/rand/rand_windows.go b/src/crypto/rand/rand_windows.go
|
||||||
|
--- a/src/crypto/rand/rand_windows.go (revision 8bba868de983dd7bf55fcd121495ba8d6e2734e7)
|
||||||
|
+++ b/src/crypto/rand/rand_windows.go (revision 7e6c963d81e14ee394402671d4044b2940c8d2c1)
|
||||||
|
@@ -15,8 +15,11 @@
|
||||||
|
|
||||||
|
type rngReader struct{}
|
||||||
|
|
||||||
|
-func (r *rngReader) Read(b []byte) (int, error) {
|
||||||
|
- if err := windows.ProcessPrng(b); err != nil {
|
||||||
|
+func (r *rngReader) Read(b []byte) (n int, err error) {
|
||||||
|
+ // RtlGenRandom only returns 1<<32-1 bytes at a time. We only read at
|
||||||
|
+ // most 1<<31-1 bytes at a time so that this works the same on 32-bit
|
||||||
|
+ // and 64-bit systems.
|
||||||
|
+ if err := batched(windows.RtlGenRandom, 1<<31-1)(b); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return len(b), nil
|
||||||
|
Index: src/internal/syscall/windows/syscall_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/internal/syscall/windows/syscall_windows.go b/src/internal/syscall/windows/syscall_windows.go
|
||||||
|
--- a/src/internal/syscall/windows/syscall_windows.go (revision 8bba868de983dd7bf55fcd121495ba8d6e2734e7)
|
||||||
|
+++ b/src/internal/syscall/windows/syscall_windows.go (revision 7e6c963d81e14ee394402671d4044b2940c8d2c1)
|
||||||
|
@@ -384,7 +384,7 @@
|
||||||
|
//sys DestroyEnvironmentBlock(block *uint16) (err error) = userenv.DestroyEnvironmentBlock
|
||||||
|
//sys CreateEvent(eventAttrs *SecurityAttributes, manualReset uint32, initialState uint32, name *uint16) (handle syscall.Handle, err error) = kernel32.CreateEventW
|
||||||
|
|
||||||
|
-//sys ProcessPrng(buf []byte) (err error) = bcryptprimitives.ProcessPrng
|
||||||
|
+//sys RtlGenRandom(buf []byte) (err error) = advapi32.SystemFunction036
|
||||||
|
|
||||||
|
//sys RtlLookupFunctionEntry(pc uintptr, baseAddress *uintptr, table *byte) (ret uintptr) = kernel32.RtlLookupFunctionEntry
|
||||||
|
//sys RtlVirtualUnwind(handlerType uint32, baseAddress uintptr, pc uintptr, entry uintptr, ctxt uintptr, data *uintptr, frame *uintptr, ctxptrs *byte) (ret uintptr) = kernel32.RtlVirtualUnwind
|
||||||
|
Index: src/internal/syscall/windows/zsyscall_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/internal/syscall/windows/zsyscall_windows.go b/src/internal/syscall/windows/zsyscall_windows.go
|
||||||
|
--- a/src/internal/syscall/windows/zsyscall_windows.go (revision 8bba868de983dd7bf55fcd121495ba8d6e2734e7)
|
||||||
|
+++ b/src/internal/syscall/windows/zsyscall_windows.go (revision 7e6c963d81e14ee394402671d4044b2940c8d2c1)
|
||||||
|
@@ -37,14 +37,13 @@
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
- modadvapi32 = syscall.NewLazyDLL(sysdll.Add("advapi32.dll"))
|
||||||
|
- modbcryptprimitives = syscall.NewLazyDLL(sysdll.Add("bcryptprimitives.dll"))
|
||||||
|
- modiphlpapi = syscall.NewLazyDLL(sysdll.Add("iphlpapi.dll"))
|
||||||
|
- modkernel32 = syscall.NewLazyDLL(sysdll.Add("kernel32.dll"))
|
||||||
|
- modnetapi32 = syscall.NewLazyDLL(sysdll.Add("netapi32.dll"))
|
||||||
|
- modpsapi = syscall.NewLazyDLL(sysdll.Add("psapi.dll"))
|
||||||
|
- moduserenv = syscall.NewLazyDLL(sysdll.Add("userenv.dll"))
|
||||||
|
- modws2_32 = syscall.NewLazyDLL(sysdll.Add("ws2_32.dll"))
|
||||||
|
+ modadvapi32 = syscall.NewLazyDLL(sysdll.Add("advapi32.dll"))
|
||||||
|
+ modiphlpapi = syscall.NewLazyDLL(sysdll.Add("iphlpapi.dll"))
|
||||||
|
+ modkernel32 = syscall.NewLazyDLL(sysdll.Add("kernel32.dll"))
|
||||||
|
+ modnetapi32 = syscall.NewLazyDLL(sysdll.Add("netapi32.dll"))
|
||||||
|
+ modpsapi = syscall.NewLazyDLL(sysdll.Add("psapi.dll"))
|
||||||
|
+ moduserenv = syscall.NewLazyDLL(sysdll.Add("userenv.dll"))
|
||||||
|
+ modws2_32 = syscall.NewLazyDLL(sysdll.Add("ws2_32.dll"))
|
||||||
|
|
||||||
|
procAdjustTokenPrivileges = modadvapi32.NewProc("AdjustTokenPrivileges")
|
||||||
|
procDuplicateTokenEx = modadvapi32.NewProc("DuplicateTokenEx")
|
||||||
|
@@ -53,7 +52,7 @@
|
||||||
|
procOpenThreadToken = modadvapi32.NewProc("OpenThreadToken")
|
||||||
|
procRevertToSelf = modadvapi32.NewProc("RevertToSelf")
|
||||||
|
procSetTokenInformation = modadvapi32.NewProc("SetTokenInformation")
|
||||||
|
- procProcessPrng = modbcryptprimitives.NewProc("ProcessPrng")
|
||||||
|
+ procSystemFunction036 = modadvapi32.NewProc("SystemFunction036")
|
||||||
|
procGetAdaptersAddresses = modiphlpapi.NewProc("GetAdaptersAddresses")
|
||||||
|
procCreateEventW = modkernel32.NewProc("CreateEventW")
|
||||||
|
procGetACP = modkernel32.NewProc("GetACP")
|
||||||
|
@@ -149,12 +148,12 @@
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
-func ProcessPrng(buf []byte) (err error) {
|
||||||
|
+func RtlGenRandom(buf []byte) (err error) {
|
||||||
|
var _p0 *byte
|
||||||
|
if len(buf) > 0 {
|
||||||
|
_p0 = &buf[0]
|
||||||
|
}
|
||||||
|
- r1, _, e1 := syscall.Syscall(procProcessPrng.Addr(), 2, uintptr(unsafe.Pointer(_p0)), uintptr(len(buf)), 0)
|
||||||
|
+ r1, _, e1 := syscall.Syscall(procSystemFunction036.Addr(), 2, uintptr(unsafe.Pointer(_p0)), uintptr(len(buf)), 0)
|
||||||
|
if r1 == 0 {
|
||||||
|
err = errnoErr(e1)
|
||||||
|
}
|
||||||
|
Index: src/runtime/os_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/runtime/os_windows.go b/src/runtime/os_windows.go
|
||||||
|
--- a/src/runtime/os_windows.go (revision 8bba868de983dd7bf55fcd121495ba8d6e2734e7)
|
||||||
|
+++ b/src/runtime/os_windows.go (revision 7e6c963d81e14ee394402671d4044b2940c8d2c1)
|
||||||
|
@@ -127,8 +127,15 @@
|
||||||
|
_AddVectoredContinueHandler,
|
||||||
|
_ stdFunction
|
||||||
|
|
||||||
|
- // Use ProcessPrng to generate cryptographically random data.
|
||||||
|
- _ProcessPrng stdFunction
|
||||||
|
+ // Use RtlGenRandom to generate cryptographically random data.
|
||||||
|
+ // This approach has been recommended by Microsoft (see issue
|
||||||
|
+ // 15589 for details).
|
||||||
|
+ // The RtlGenRandom is not listed in advapi32.dll, instead
|
||||||
|
+ // RtlGenRandom function can be found by searching for SystemFunction036.
|
||||||
|
+ // Also some versions of Mingw cannot link to SystemFunction036
|
||||||
|
+ // when building executable as Cgo. So load SystemFunction036
|
||||||
|
+ // manually during runtime startup.
|
||||||
|
+ _RtlGenRandom stdFunction
|
||||||
|
|
||||||
|
// Load ntdll.dll manually during startup, otherwise Mingw
|
||||||
|
// links wrong printf function to cgo executable (see issue
|
||||||
|
@@ -145,12 +152,12 @@
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
- bcryptprimitivesdll = [...]uint16{'b', 'c', 'r', 'y', 'p', 't', 'p', 'r', 'i', 'm', 'i', 't', 'i', 'v', 'e', 's', '.', 'd', 'l', 'l', 0}
|
||||||
|
- kernel32dll = [...]uint16{'k', 'e', 'r', 'n', 'e', 'l', '3', '2', '.', 'd', 'l', 'l', 0}
|
||||||
|
- ntdlldll = [...]uint16{'n', 't', 'd', 'l', 'l', '.', 'd', 'l', 'l', 0}
|
||||||
|
- powrprofdll = [...]uint16{'p', 'o', 'w', 'r', 'p', 'r', 'o', 'f', '.', 'd', 'l', 'l', 0}
|
||||||
|
- winmmdll = [...]uint16{'w', 'i', 'n', 'm', 'm', '.', 'd', 'l', 'l', 0}
|
||||||
|
- ws2_32dll = [...]uint16{'w', 's', '2', '_', '3', '2', '.', 'd', 'l', 'l', 0}
|
||||||
|
+ advapi32dll = [...]uint16{'a', 'd', 'v', 'a', 'p', 'i', '3', '2', '.', 'd', 'l', 'l', 0}
|
||||||
|
+ kernel32dll = [...]uint16{'k', 'e', 'r', 'n', 'e', 'l', '3', '2', '.', 'd', 'l', 'l', 0}
|
||||||
|
+ ntdlldll = [...]uint16{'n', 't', 'd', 'l', 'l', '.', 'd', 'l', 'l', 0}
|
||||||
|
+ powrprofdll = [...]uint16{'p', 'o', 'w', 'r', 'p', 'r', 'o', 'f', '.', 'd', 'l', 'l', 0}
|
||||||
|
+ winmmdll = [...]uint16{'w', 'i', 'n', 'm', 'm', '.', 'd', 'l', 'l', 0}
|
||||||
|
+ ws2_32dll = [...]uint16{'w', 's', '2', '_', '3', '2', '.', 'd', 'l', 'l', 0}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Function to be called by windows CreateThread
|
||||||
|
@@ -249,11 +256,11 @@
|
||||||
|
}
|
||||||
|
_AddVectoredContinueHandler = windowsFindfunc(k32, []byte("AddVectoredContinueHandler\000"))
|
||||||
|
|
||||||
|
- bcryptPrimitives := windowsLoadSystemLib(bcryptprimitivesdll[:])
|
||||||
|
- if bcryptPrimitives == 0 {
|
||||||
|
- throw("bcryptprimitives.dll not found")
|
||||||
|
+ a32 := windowsLoadSystemLib(advapi32dll[:])
|
||||||
|
+ if a32 == 0 {
|
||||||
|
+ throw("advapi32.dll not found")
|
||||||
|
}
|
||||||
|
- _ProcessPrng = windowsFindfunc(bcryptPrimitives, []byte("ProcessPrng\000"))
|
||||||
|
+ _RtlGenRandom = windowsFindfunc(a32, []byte("SystemFunction036\000"))
|
||||||
|
|
||||||
|
n32 := windowsLoadSystemLib(ntdlldll[:])
|
||||||
|
if n32 == 0 {
|
||||||
|
@@ -610,7 +617,7 @@
|
||||||
|
//go:nosplit
|
||||||
|
func getRandomData(r []byte) {
|
||||||
|
n := 0
|
||||||
|
- if stdcall2(_ProcessPrng, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 {
|
||||||
|
+ if stdcall2(_RtlGenRandom, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 {
|
||||||
|
n = len(r)
|
||||||
|
}
|
||||||
|
extendRandom(r, n)
|
||||||
645
.github/patch/go1.22.patch
vendored
Normal file
645
.github/patch/go1.22.patch
vendored
Normal file
@@ -0,0 +1,645 @@
|
|||||||
|
Subject: [PATCH] Revert "runtime: always use LoadLibraryEx to load system libraries"
|
||||||
|
Revert "syscall: remove Windows 7 console handle workaround"
|
||||||
|
Revert "net: remove sysSocket fallback for Windows 7"
|
||||||
|
Revert "crypto/rand,runtime: switch RtlGenRandom for ProcessPrng"
|
||||||
|
---
|
||||||
|
Index: src/crypto/rand/rand.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/crypto/rand/rand.go b/src/crypto/rand/rand.go
|
||||||
|
--- a/src/crypto/rand/rand.go (revision cb4eee693c382bea4222f20837e26501d40ed892)
|
||||||
|
+++ b/src/crypto/rand/rand.go (revision 9779155f18b6556a034f7bb79fb7fb2aad1e26a9)
|
||||||
|
@@ -15,7 +15,7 @@
|
||||||
|
// available, /dev/urandom otherwise.
|
||||||
|
// On OpenBSD and macOS, Reader uses getentropy(2).
|
||||||
|
// On other Unix-like systems, Reader reads from /dev/urandom.
|
||||||
|
-// On Windows systems, Reader uses the ProcessPrng API.
|
||||||
|
+// On Windows systems, Reader uses the RtlGenRandom API.
|
||||||
|
// On JS/Wasm, Reader uses the Web Crypto API.
|
||||||
|
// On WASIP1/Wasm, Reader uses random_get from wasi_snapshot_preview1.
|
||||||
|
var Reader io.Reader
|
||||||
|
Index: src/crypto/rand/rand_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/crypto/rand/rand_windows.go b/src/crypto/rand/rand_windows.go
|
||||||
|
--- a/src/crypto/rand/rand_windows.go (revision cb4eee693c382bea4222f20837e26501d40ed892)
|
||||||
|
+++ b/src/crypto/rand/rand_windows.go (revision 9779155f18b6556a034f7bb79fb7fb2aad1e26a9)
|
||||||
|
@@ -15,8 +15,11 @@
|
||||||
|
|
||||||
|
type rngReader struct{}
|
||||||
|
|
||||||
|
-func (r *rngReader) Read(b []byte) (int, error) {
|
||||||
|
- if err := windows.ProcessPrng(b); err != nil {
|
||||||
|
+func (r *rngReader) Read(b []byte) (n int, err error) {
|
||||||
|
+ // RtlGenRandom only returns 1<<32-1 bytes at a time. We only read at
|
||||||
|
+ // most 1<<31-1 bytes at a time so that this works the same on 32-bit
|
||||||
|
+ // and 64-bit systems.
|
||||||
|
+ if err := batched(windows.RtlGenRandom, 1<<31-1)(b); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return len(b), nil
|
||||||
|
Index: src/internal/syscall/windows/syscall_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/internal/syscall/windows/syscall_windows.go b/src/internal/syscall/windows/syscall_windows.go
|
||||||
|
--- a/src/internal/syscall/windows/syscall_windows.go (revision cb4eee693c382bea4222f20837e26501d40ed892)
|
||||||
|
+++ b/src/internal/syscall/windows/syscall_windows.go (revision 9779155f18b6556a034f7bb79fb7fb2aad1e26a9)
|
||||||
|
@@ -384,7 +384,7 @@
|
||||||
|
//sys DestroyEnvironmentBlock(block *uint16) (err error) = userenv.DestroyEnvironmentBlock
|
||||||
|
//sys CreateEvent(eventAttrs *SecurityAttributes, manualReset uint32, initialState uint32, name *uint16) (handle syscall.Handle, err error) = kernel32.CreateEventW
|
||||||
|
|
||||||
|
-//sys ProcessPrng(buf []byte) (err error) = bcryptprimitives.ProcessPrng
|
||||||
|
+//sys RtlGenRandom(buf []byte) (err error) = advapi32.SystemFunction036
|
||||||
|
|
||||||
|
type FILE_ID_BOTH_DIR_INFO struct {
|
||||||
|
NextEntryOffset uint32
|
||||||
|
Index: src/internal/syscall/windows/zsyscall_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/internal/syscall/windows/zsyscall_windows.go b/src/internal/syscall/windows/zsyscall_windows.go
|
||||||
|
--- a/src/internal/syscall/windows/zsyscall_windows.go (revision cb4eee693c382bea4222f20837e26501d40ed892)
|
||||||
|
+++ b/src/internal/syscall/windows/zsyscall_windows.go (revision 9779155f18b6556a034f7bb79fb7fb2aad1e26a9)
|
||||||
|
@@ -37,14 +37,13 @@
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
- modadvapi32 = syscall.NewLazyDLL(sysdll.Add("advapi32.dll"))
|
||||||
|
- modbcryptprimitives = syscall.NewLazyDLL(sysdll.Add("bcryptprimitives.dll"))
|
||||||
|
- modiphlpapi = syscall.NewLazyDLL(sysdll.Add("iphlpapi.dll"))
|
||||||
|
- modkernel32 = syscall.NewLazyDLL(sysdll.Add("kernel32.dll"))
|
||||||
|
- modnetapi32 = syscall.NewLazyDLL(sysdll.Add("netapi32.dll"))
|
||||||
|
- modpsapi = syscall.NewLazyDLL(sysdll.Add("psapi.dll"))
|
||||||
|
- moduserenv = syscall.NewLazyDLL(sysdll.Add("userenv.dll"))
|
||||||
|
- modws2_32 = syscall.NewLazyDLL(sysdll.Add("ws2_32.dll"))
|
||||||
|
+ modadvapi32 = syscall.NewLazyDLL(sysdll.Add("advapi32.dll"))
|
||||||
|
+ modiphlpapi = syscall.NewLazyDLL(sysdll.Add("iphlpapi.dll"))
|
||||||
|
+ modkernel32 = syscall.NewLazyDLL(sysdll.Add("kernel32.dll"))
|
||||||
|
+ modnetapi32 = syscall.NewLazyDLL(sysdll.Add("netapi32.dll"))
|
||||||
|
+ modpsapi = syscall.NewLazyDLL(sysdll.Add("psapi.dll"))
|
||||||
|
+ moduserenv = syscall.NewLazyDLL(sysdll.Add("userenv.dll"))
|
||||||
|
+ modws2_32 = syscall.NewLazyDLL(sysdll.Add("ws2_32.dll"))
|
||||||
|
|
||||||
|
procAdjustTokenPrivileges = modadvapi32.NewProc("AdjustTokenPrivileges")
|
||||||
|
procDuplicateTokenEx = modadvapi32.NewProc("DuplicateTokenEx")
|
||||||
|
@@ -56,7 +55,7 @@
|
||||||
|
procQueryServiceStatus = modadvapi32.NewProc("QueryServiceStatus")
|
||||||
|
procRevertToSelf = modadvapi32.NewProc("RevertToSelf")
|
||||||
|
procSetTokenInformation = modadvapi32.NewProc("SetTokenInformation")
|
||||||
|
- procProcessPrng = modbcryptprimitives.NewProc("ProcessPrng")
|
||||||
|
+ procSystemFunction036 = modadvapi32.NewProc("SystemFunction036")
|
||||||
|
procGetAdaptersAddresses = modiphlpapi.NewProc("GetAdaptersAddresses")
|
||||||
|
procCreateEventW = modkernel32.NewProc("CreateEventW")
|
||||||
|
procGetACP = modkernel32.NewProc("GetACP")
|
||||||
|
@@ -180,12 +179,12 @@
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
-func ProcessPrng(buf []byte) (err error) {
|
||||||
|
+func RtlGenRandom(buf []byte) (err error) {
|
||||||
|
var _p0 *byte
|
||||||
|
if len(buf) > 0 {
|
||||||
|
_p0 = &buf[0]
|
||||||
|
}
|
||||||
|
- r1, _, e1 := syscall.Syscall(procProcessPrng.Addr(), 2, uintptr(unsafe.Pointer(_p0)), uintptr(len(buf)), 0)
|
||||||
|
+ r1, _, e1 := syscall.Syscall(procSystemFunction036.Addr(), 2, uintptr(unsafe.Pointer(_p0)), uintptr(len(buf)), 0)
|
||||||
|
if r1 == 0 {
|
||||||
|
err = errnoErr(e1)
|
||||||
|
}
|
||||||
|
Index: src/runtime/os_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/runtime/os_windows.go b/src/runtime/os_windows.go
|
||||||
|
--- a/src/runtime/os_windows.go (revision cb4eee693c382bea4222f20837e26501d40ed892)
|
||||||
|
+++ b/src/runtime/os_windows.go (revision 83ff9782e024cb328b690cbf0da4e7848a327f4f)
|
||||||
|
@@ -40,8 +40,8 @@
|
||||||
|
//go:cgo_import_dynamic runtime._GetSystemInfo GetSystemInfo%1 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._GetThreadContext GetThreadContext%2 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._SetThreadContext SetThreadContext%2 "kernel32.dll"
|
||||||
|
-//go:cgo_import_dynamic runtime._LoadLibraryExW LoadLibraryExW%3 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._LoadLibraryW LoadLibraryW%1 "kernel32.dll"
|
||||||
|
+//go:cgo_import_dynamic runtime._LoadLibraryA LoadLibraryA%1 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._PostQueuedCompletionStatus PostQueuedCompletionStatus%4 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._QueryPerformanceCounter QueryPerformanceCounter%1 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._RaiseFailFastException RaiseFailFastException%3 "kernel32.dll"
|
||||||
|
@@ -74,7 +74,6 @@
|
||||||
|
// Following syscalls are available on every Windows PC.
|
||||||
|
// All these variables are set by the Windows executable
|
||||||
|
// loader before the Go program starts.
|
||||||
|
- _AddVectoredContinueHandler,
|
||||||
|
_AddVectoredExceptionHandler,
|
||||||
|
_CloseHandle,
|
||||||
|
_CreateEventA,
|
||||||
|
@@ -98,8 +97,8 @@
|
||||||
|
_GetSystemInfo,
|
||||||
|
_GetThreadContext,
|
||||||
|
_SetThreadContext,
|
||||||
|
- _LoadLibraryExW,
|
||||||
|
_LoadLibraryW,
|
||||||
|
+ _LoadLibraryA,
|
||||||
|
_PostQueuedCompletionStatus,
|
||||||
|
_QueryPerformanceCounter,
|
||||||
|
_RaiseFailFastException,
|
||||||
|
@@ -127,8 +126,23 @@
|
||||||
|
_WriteFile,
|
||||||
|
_ stdFunction
|
||||||
|
|
||||||
|
- // Use ProcessPrng to generate cryptographically random data.
|
||||||
|
- _ProcessPrng stdFunction
|
||||||
|
+ // Following syscalls are only available on some Windows PCs.
|
||||||
|
+ // We will load syscalls, if available, before using them.
|
||||||
|
+ _AddDllDirectory,
|
||||||
|
+ _AddVectoredContinueHandler,
|
||||||
|
+ _LoadLibraryExA,
|
||||||
|
+ _LoadLibraryExW,
|
||||||
|
+ _ stdFunction
|
||||||
|
+
|
||||||
|
+ // Use RtlGenRandom to generate cryptographically random data.
|
||||||
|
+ // This approach has been recommended by Microsoft (see issue
|
||||||
|
+ // 15589 for details).
|
||||||
|
+ // The RtlGenRandom is not listed in advapi32.dll, instead
|
||||||
|
+ // RtlGenRandom function can be found by searching for SystemFunction036.
|
||||||
|
+ // Also some versions of Mingw cannot link to SystemFunction036
|
||||||
|
+ // when building executable as Cgo. So load SystemFunction036
|
||||||
|
+ // manually during runtime startup.
|
||||||
|
+ _RtlGenRandom stdFunction
|
||||||
|
|
||||||
|
// Load ntdll.dll manually during startup, otherwise Mingw
|
||||||
|
// links wrong printf function to cgo executable (see issue
|
||||||
|
@@ -143,14 +157,6 @@
|
||||||
|
_ stdFunction
|
||||||
|
)
|
||||||
|
|
||||||
|
-var (
|
||||||
|
- bcryptprimitivesdll = [...]uint16{'b', 'c', 'r', 'y', 'p', 't', 'p', 'r', 'i', 'm', 'i', 't', 'i', 'v', 'e', 's', '.', 'd', 'l', 'l', 0}
|
||||||
|
- ntdlldll = [...]uint16{'n', 't', 'd', 'l', 'l', '.', 'd', 'l', 'l', 0}
|
||||||
|
- powrprofdll = [...]uint16{'p', 'o', 'w', 'r', 'p', 'r', 'o', 'f', '.', 'd', 'l', 'l', 0}
|
||||||
|
- winmmdll = [...]uint16{'w', 'i', 'n', 'm', 'm', '.', 'd', 'l', 'l', 0}
|
||||||
|
- ws2_32dll = [...]uint16{'w', 's', '2', '_', '3', '2', '.', 'd', 'l', 'l', 0}
|
||||||
|
-)
|
||||||
|
-
|
||||||
|
// Function to be called by windows CreateThread
|
||||||
|
// to start new os thread.
|
||||||
|
func tstart_stdcall(newm *m)
|
||||||
|
@@ -239,25 +245,51 @@
|
||||||
|
return unsafe.String(&sysDirectory[0], sysDirectoryLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
-func windowsLoadSystemLib(name []uint16) uintptr {
|
||||||
|
- return stdcall3(_LoadLibraryExW, uintptr(unsafe.Pointer(&name[0])), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32)
|
||||||
|
+//go:linkname syscall_getSystemDirectory syscall.getSystemDirectory
|
||||||
|
+func syscall_getSystemDirectory() string {
|
||||||
|
+ return unsafe.String(&sysDirectory[0], sysDirectoryLen)
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+func windowsLoadSystemLib(name []byte) uintptr {
|
||||||
|
+ if useLoadLibraryEx {
|
||||||
|
+ return stdcall3(_LoadLibraryExA, uintptr(unsafe.Pointer(&name[0])), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32)
|
||||||
|
+ } else {
|
||||||
|
+ absName := append(sysDirectory[:sysDirectoryLen], name...)
|
||||||
|
+ return stdcall1(_LoadLibraryA, uintptr(unsafe.Pointer(&absName[0])))
|
||||||
|
+ }
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadOptionalSyscalls() {
|
||||||
|
- bcryptPrimitives := windowsLoadSystemLib(bcryptprimitivesdll[:])
|
||||||
|
- if bcryptPrimitives == 0 {
|
||||||
|
- throw("bcryptprimitives.dll not found")
|
||||||
|
+ var kernel32dll = []byte("kernel32.dll\000")
|
||||||
|
+ k32 := stdcall1(_LoadLibraryA, uintptr(unsafe.Pointer(&kernel32dll[0])))
|
||||||
|
+ if k32 == 0 {
|
||||||
|
+ throw("kernel32.dll not found")
|
||||||
|
}
|
||||||
|
- _ProcessPrng = windowsFindfunc(bcryptPrimitives, []byte("ProcessPrng\000"))
|
||||||
|
+ _AddDllDirectory = windowsFindfunc(k32, []byte("AddDllDirectory\000"))
|
||||||
|
+ _AddVectoredContinueHandler = windowsFindfunc(k32, []byte("AddVectoredContinueHandler\000"))
|
||||||
|
+ _LoadLibraryExA = windowsFindfunc(k32, []byte("LoadLibraryExA\000"))
|
||||||
|
+ _LoadLibraryExW = windowsFindfunc(k32, []byte("LoadLibraryExW\000"))
|
||||||
|
+ useLoadLibraryEx = (_LoadLibraryExW != nil && _LoadLibraryExA != nil && _AddDllDirectory != nil)
|
||||||
|
+
|
||||||
|
+ initSysDirectory()
|
||||||
|
|
||||||
|
- n32 := windowsLoadSystemLib(ntdlldll[:])
|
||||||
|
+ var advapi32dll = []byte("advapi32.dll\000")
|
||||||
|
+ a32 := windowsLoadSystemLib(advapi32dll)
|
||||||
|
+ if a32 == 0 {
|
||||||
|
+ throw("advapi32.dll not found")
|
||||||
|
+ }
|
||||||
|
+ _RtlGenRandom = windowsFindfunc(a32, []byte("SystemFunction036\000"))
|
||||||
|
+
|
||||||
|
+ var ntdll = []byte("ntdll.dll\000")
|
||||||
|
+ n32 := windowsLoadSystemLib(ntdll)
|
||||||
|
if n32 == 0 {
|
||||||
|
throw("ntdll.dll not found")
|
||||||
|
}
|
||||||
|
_RtlGetCurrentPeb = windowsFindfunc(n32, []byte("RtlGetCurrentPeb\000"))
|
||||||
|
_RtlGetNtVersionNumbers = windowsFindfunc(n32, []byte("RtlGetNtVersionNumbers\000"))
|
||||||
|
|
||||||
|
- m32 := windowsLoadSystemLib(winmmdll[:])
|
||||||
|
+ var winmmdll = []byte("winmm.dll\000")
|
||||||
|
+ m32 := windowsLoadSystemLib(winmmdll)
|
||||||
|
if m32 == 0 {
|
||||||
|
throw("winmm.dll not found")
|
||||||
|
}
|
||||||
|
@@ -267,7 +299,8 @@
|
||||||
|
throw("timeBegin/EndPeriod not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
- ws232 := windowsLoadSystemLib(ws2_32dll[:])
|
||||||
|
+ var ws232dll = []byte("ws2_32.dll\000")
|
||||||
|
+ ws232 := windowsLoadSystemLib(ws232dll)
|
||||||
|
if ws232 == 0 {
|
||||||
|
throw("ws2_32.dll not found")
|
||||||
|
}
|
||||||
|
@@ -286,7 +319,7 @@
|
||||||
|
context uintptr
|
||||||
|
}
|
||||||
|
|
||||||
|
- powrprof := windowsLoadSystemLib(powrprofdll[:])
|
||||||
|
+ powrprof := windowsLoadSystemLib([]byte("powrprof.dll\000"))
|
||||||
|
if powrprof == 0 {
|
||||||
|
return // Running on Windows 7, where we don't need it anyway.
|
||||||
|
}
|
||||||
|
@@ -360,6 +393,22 @@
|
||||||
|
// in sys_windows_386.s and sys_windows_amd64.s:
|
||||||
|
func getlasterror() uint32
|
||||||
|
|
||||||
|
+// When loading DLLs, we prefer to use LoadLibraryEx with
|
||||||
|
+// LOAD_LIBRARY_SEARCH_* flags, if available. LoadLibraryEx is not
|
||||||
|
+// available on old Windows, though, and the LOAD_LIBRARY_SEARCH_*
|
||||||
|
+// flags are not available on some versions of Windows without a
|
||||||
|
+// security patch.
|
||||||
|
+//
|
||||||
|
+// https://msdn.microsoft.com/en-us/library/ms684179(v=vs.85).aspx says:
|
||||||
|
+// "Windows 7, Windows Server 2008 R2, Windows Vista, and Windows
|
||||||
|
+// Server 2008: The LOAD_LIBRARY_SEARCH_* flags are available on
|
||||||
|
+// systems that have KB2533623 installed. To determine whether the
|
||||||
|
+// flags are available, use GetProcAddress to get the address of the
|
||||||
|
+// AddDllDirectory, RemoveDllDirectory, or SetDefaultDllDirectories
|
||||||
|
+// function. If GetProcAddress succeeds, the LOAD_LIBRARY_SEARCH_*
|
||||||
|
+// flags can be used with LoadLibraryEx."
|
||||||
|
+var useLoadLibraryEx bool
|
||||||
|
+
|
||||||
|
var timeBeginPeriodRetValue uint32
|
||||||
|
|
||||||
|
// osRelaxMinNS indicates that sysmon shouldn't osRelax if the next
|
||||||
|
@@ -507,7 +556,6 @@
|
||||||
|
initHighResTimer()
|
||||||
|
timeBeginPeriodRetValue = osRelax(false)
|
||||||
|
|
||||||
|
- initSysDirectory()
|
||||||
|
initLongPathSupport()
|
||||||
|
|
||||||
|
ncpu = getproccount()
|
||||||
|
@@ -524,7 +572,7 @@
|
||||||
|
//go:nosplit
|
||||||
|
func readRandom(r []byte) int {
|
||||||
|
n := 0
|
||||||
|
- if stdcall2(_ProcessPrng, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 {
|
||||||
|
+ if stdcall2(_RtlGenRandom, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 {
|
||||||
|
n = len(r)
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
Index: src/net/hook_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/hook_windows.go b/src/net/hook_windows.go
|
||||||
|
--- a/src/net/hook_windows.go (revision 9779155f18b6556a034f7bb79fb7fb2aad1e26a9)
|
||||||
|
+++ b/src/net/hook_windows.go (revision ef0606261340e608017860b423ffae5c1ce78239)
|
||||||
|
@@ -13,6 +13,7 @@
|
||||||
|
hostsFilePath = windows.GetSystemDirectory() + "/Drivers/etc/hosts"
|
||||||
|
|
||||||
|
// Placeholders for socket system calls.
|
||||||
|
+ socketFunc func(int, int, int) (syscall.Handle, error) = syscall.Socket
|
||||||
|
wsaSocketFunc func(int32, int32, int32, *syscall.WSAProtocolInfo, uint32, uint32) (syscall.Handle, error) = windows.WSASocket
|
||||||
|
connectFunc func(syscall.Handle, syscall.Sockaddr) error = syscall.Connect
|
||||||
|
listenFunc func(syscall.Handle, int) error = syscall.Listen
|
||||||
|
Index: src/net/internal/socktest/main_test.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/internal/socktest/main_test.go b/src/net/internal/socktest/main_test.go
|
||||||
|
--- a/src/net/internal/socktest/main_test.go (revision 9779155f18b6556a034f7bb79fb7fb2aad1e26a9)
|
||||||
|
+++ b/src/net/internal/socktest/main_test.go (revision ef0606261340e608017860b423ffae5c1ce78239)
|
||||||
|
@@ -2,7 +2,7 @@
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
-//go:build !js && !plan9 && !wasip1 && !windows
|
||||||
|
+//go:build !js && !plan9 && !wasip1
|
||||||
|
|
||||||
|
package socktest_test
|
||||||
|
|
||||||
|
Index: src/net/internal/socktest/main_windows_test.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/internal/socktest/main_windows_test.go b/src/net/internal/socktest/main_windows_test.go
|
||||||
|
new file mode 100644
|
||||||
|
--- /dev/null (revision ef0606261340e608017860b423ffae5c1ce78239)
|
||||||
|
+++ b/src/net/internal/socktest/main_windows_test.go (revision ef0606261340e608017860b423ffae5c1ce78239)
|
||||||
|
@@ -0,0 +1,22 @@
|
||||||
|
+// Copyright 2015 The Go Authors. All rights reserved.
|
||||||
|
+// Use of this source code is governed by a BSD-style
|
||||||
|
+// license that can be found in the LICENSE file.
|
||||||
|
+
|
||||||
|
+package socktest_test
|
||||||
|
+
|
||||||
|
+import "syscall"
|
||||||
|
+
|
||||||
|
+var (
|
||||||
|
+ socketFunc func(int, int, int) (syscall.Handle, error)
|
||||||
|
+ closeFunc func(syscall.Handle) error
|
||||||
|
+)
|
||||||
|
+
|
||||||
|
+func installTestHooks() {
|
||||||
|
+ socketFunc = sw.Socket
|
||||||
|
+ closeFunc = sw.Closesocket
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+func uninstallTestHooks() {
|
||||||
|
+ socketFunc = syscall.Socket
|
||||||
|
+ closeFunc = syscall.Closesocket
|
||||||
|
+}
|
||||||
|
Index: src/net/internal/socktest/sys_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/internal/socktest/sys_windows.go b/src/net/internal/socktest/sys_windows.go
|
||||||
|
--- a/src/net/internal/socktest/sys_windows.go (revision 9779155f18b6556a034f7bb79fb7fb2aad1e26a9)
|
||||||
|
+++ b/src/net/internal/socktest/sys_windows.go (revision ef0606261340e608017860b423ffae5c1ce78239)
|
||||||
|
@@ -9,6 +9,38 @@
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
+// Socket wraps [syscall.Socket].
|
||||||
|
+func (sw *Switch) Socket(family, sotype, proto int) (s syscall.Handle, err error) {
|
||||||
|
+ sw.once.Do(sw.init)
|
||||||
|
+
|
||||||
|
+ so := &Status{Cookie: cookie(family, sotype, proto)}
|
||||||
|
+ sw.fmu.RLock()
|
||||||
|
+ f, _ := sw.fltab[FilterSocket]
|
||||||
|
+ sw.fmu.RUnlock()
|
||||||
|
+
|
||||||
|
+ af, err := f.apply(so)
|
||||||
|
+ if err != nil {
|
||||||
|
+ return syscall.InvalidHandle, err
|
||||||
|
+ }
|
||||||
|
+ s, so.Err = syscall.Socket(family, sotype, proto)
|
||||||
|
+ if err = af.apply(so); err != nil {
|
||||||
|
+ if so.Err == nil {
|
||||||
|
+ syscall.Closesocket(s)
|
||||||
|
+ }
|
||||||
|
+ return syscall.InvalidHandle, err
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
+ sw.smu.Lock()
|
||||||
|
+ defer sw.smu.Unlock()
|
||||||
|
+ if so.Err != nil {
|
||||||
|
+ sw.stats.getLocked(so.Cookie).OpenFailed++
|
||||||
|
+ return syscall.InvalidHandle, so.Err
|
||||||
|
+ }
|
||||||
|
+ nso := sw.addLocked(s, family, sotype, proto)
|
||||||
|
+ sw.stats.getLocked(nso.Cookie).Opened++
|
||||||
|
+ return s, nil
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
// WSASocket wraps [syscall.WSASocket].
|
||||||
|
func (sw *Switch) WSASocket(family, sotype, proto int32, protinfo *syscall.WSAProtocolInfo, group uint32, flags uint32) (s syscall.Handle, err error) {
|
||||||
|
sw.once.Do(sw.init)
|
||||||
|
Index: src/net/main_windows_test.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/main_windows_test.go b/src/net/main_windows_test.go
|
||||||
|
--- a/src/net/main_windows_test.go (revision 9779155f18b6556a034f7bb79fb7fb2aad1e26a9)
|
||||||
|
+++ b/src/net/main_windows_test.go (revision ef0606261340e608017860b423ffae5c1ce78239)
|
||||||
|
@@ -8,6 +8,7 @@
|
||||||
|
|
||||||
|
var (
|
||||||
|
// Placeholders for saving original socket system calls.
|
||||||
|
+ origSocket = socketFunc
|
||||||
|
origWSASocket = wsaSocketFunc
|
||||||
|
origClosesocket = poll.CloseFunc
|
||||||
|
origConnect = connectFunc
|
||||||
|
@@ -17,6 +18,7 @@
|
||||||
|
)
|
||||||
|
|
||||||
|
func installTestHooks() {
|
||||||
|
+ socketFunc = sw.Socket
|
||||||
|
wsaSocketFunc = sw.WSASocket
|
||||||
|
poll.CloseFunc = sw.Closesocket
|
||||||
|
connectFunc = sw.Connect
|
||||||
|
@@ -26,6 +28,7 @@
|
||||||
|
}
|
||||||
|
|
||||||
|
func uninstallTestHooks() {
|
||||||
|
+ socketFunc = origSocket
|
||||||
|
wsaSocketFunc = origWSASocket
|
||||||
|
poll.CloseFunc = origClosesocket
|
||||||
|
connectFunc = origConnect
|
||||||
|
Index: src/net/sock_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/sock_windows.go b/src/net/sock_windows.go
|
||||||
|
--- a/src/net/sock_windows.go (revision 9779155f18b6556a034f7bb79fb7fb2aad1e26a9)
|
||||||
|
+++ b/src/net/sock_windows.go (revision ef0606261340e608017860b423ffae5c1ce78239)
|
||||||
|
@@ -20,6 +20,21 @@
|
||||||
|
func sysSocket(family, sotype, proto int) (syscall.Handle, error) {
|
||||||
|
s, err := wsaSocketFunc(int32(family), int32(sotype), int32(proto),
|
||||||
|
nil, 0, windows.WSA_FLAG_OVERLAPPED|windows.WSA_FLAG_NO_HANDLE_INHERIT)
|
||||||
|
+ if err == nil {
|
||||||
|
+ return s, nil
|
||||||
|
+ }
|
||||||
|
+ // WSA_FLAG_NO_HANDLE_INHERIT flag is not supported on some
|
||||||
|
+ // old versions of Windows, see
|
||||||
|
+ // https://msdn.microsoft.com/en-us/library/windows/desktop/ms742212(v=vs.85).aspx
|
||||||
|
+ // for details. Just use syscall.Socket, if windows.WSASocket failed.
|
||||||
|
+
|
||||||
|
+ // See ../syscall/exec_unix.go for description of ForkLock.
|
||||||
|
+ syscall.ForkLock.RLock()
|
||||||
|
+ s, err = socketFunc(family, sotype, proto)
|
||||||
|
+ if err == nil {
|
||||||
|
+ syscall.CloseOnExec(s)
|
||||||
|
+ }
|
||||||
|
+ syscall.ForkLock.RUnlock()
|
||||||
|
if err != nil {
|
||||||
|
return syscall.InvalidHandle, os.NewSyscallError("socket", err)
|
||||||
|
}
|
||||||
|
Index: src/syscall/exec_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/syscall/exec_windows.go b/src/syscall/exec_windows.go
|
||||||
|
--- a/src/syscall/exec_windows.go (revision 9779155f18b6556a034f7bb79fb7fb2aad1e26a9)
|
||||||
|
+++ b/src/syscall/exec_windows.go (revision 7f83badcb925a7e743188041cb6e561fc9b5b642)
|
||||||
|
@@ -14,7 +14,6 @@
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
-// ForkLock is not used on Windows.
|
||||||
|
var ForkLock sync.RWMutex
|
||||||
|
|
||||||
|
// EscapeArg rewrites command line argument s as prescribed
|
||||||
|
@@ -317,6 +316,17 @@
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
+ var maj, min, build uint32
|
||||||
|
+ rtlGetNtVersionNumbers(&maj, &min, &build)
|
||||||
|
+ isWin7 := maj < 6 || (maj == 6 && min <= 1)
|
||||||
|
+ // NT kernel handles are divisible by 4, with the bottom 3 bits left as
|
||||||
|
+ // a tag. The fully set tag correlates with the types of handles we're
|
||||||
|
+ // concerned about here. Except, the kernel will interpret some
|
||||||
|
+ // special handle values, like -1, -2, and so forth, so kernelbase.dll
|
||||||
|
+ // checks to see that those bottom three bits are checked, but that top
|
||||||
|
+ // bit is not checked.
|
||||||
|
+ isLegacyWin7ConsoleHandle := func(handle Handle) bool { return isWin7 && handle&0x10000003 == 3 }
|
||||||
|
+
|
||||||
|
p, _ := GetCurrentProcess()
|
||||||
|
parentProcess := p
|
||||||
|
if sys.ParentProcess != 0 {
|
||||||
|
@@ -325,7 +335,15 @@
|
||||||
|
fd := make([]Handle, len(attr.Files))
|
||||||
|
for i := range attr.Files {
|
||||||
|
if attr.Files[i] > 0 {
|
||||||
|
- err := DuplicateHandle(p, Handle(attr.Files[i]), parentProcess, &fd[i], 0, true, DUPLICATE_SAME_ACCESS)
|
||||||
|
+ destinationProcessHandle := parentProcess
|
||||||
|
+
|
||||||
|
+ // On Windows 7, console handles aren't real handles, and can only be duplicated
|
||||||
|
+ // into the current process, not a parent one, which amounts to the same thing.
|
||||||
|
+ if parentProcess != p && isLegacyWin7ConsoleHandle(Handle(attr.Files[i])) {
|
||||||
|
+ destinationProcessHandle = p
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
+ err := DuplicateHandle(p, Handle(attr.Files[i]), destinationProcessHandle, &fd[i], 0, true, DUPLICATE_SAME_ACCESS)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
@@ -356,6 +374,14 @@
|
||||||
|
|
||||||
|
fd = append(fd, sys.AdditionalInheritedHandles...)
|
||||||
|
|
||||||
|
+ // On Windows 7, console handles aren't real handles, so don't pass them
|
||||||
|
+ // through to PROC_THREAD_ATTRIBUTE_HANDLE_LIST.
|
||||||
|
+ for i := range fd {
|
||||||
|
+ if isLegacyWin7ConsoleHandle(fd[i]) {
|
||||||
|
+ fd[i] = 0
|
||||||
|
+ }
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
// The presence of a NULL handle in the list is enough to cause PROC_THREAD_ATTRIBUTE_HANDLE_LIST
|
||||||
|
// to treat the entire list as empty, so remove NULL handles.
|
||||||
|
j := 0
|
||||||
|
Index: src/runtime/syscall_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/runtime/syscall_windows.go b/src/runtime/syscall_windows.go
|
||||||
|
--- a/src/runtime/syscall_windows.go (revision 7f83badcb925a7e743188041cb6e561fc9b5b642)
|
||||||
|
+++ b/src/runtime/syscall_windows.go (revision 83ff9782e024cb328b690cbf0da4e7848a327f4f)
|
||||||
|
@@ -413,23 +413,36 @@
|
||||||
|
|
||||||
|
const _LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800
|
||||||
|
|
||||||
|
+// When available, this function will use LoadLibraryEx with the filename
|
||||||
|
+// parameter and the important SEARCH_SYSTEM32 argument. But on systems that
|
||||||
|
+// do not have that option, absoluteFilepath should contain a fallback
|
||||||
|
+// to the full path inside of system32 for use with vanilla LoadLibrary.
|
||||||
|
+//
|
||||||
|
//go:linkname syscall_loadsystemlibrary syscall.loadsystemlibrary
|
||||||
|
//go:nosplit
|
||||||
|
//go:cgo_unsafe_args
|
||||||
|
-func syscall_loadsystemlibrary(filename *uint16) (handle, err uintptr) {
|
||||||
|
+func syscall_loadsystemlibrary(filename *uint16, absoluteFilepath *uint16) (handle, err uintptr) {
|
||||||
|
lockOSThread()
|
||||||
|
c := &getg().m.syscall
|
||||||
|
- c.fn = getLoadLibraryEx()
|
||||||
|
- c.n = 3
|
||||||
|
- args := struct {
|
||||||
|
- lpFileName *uint16
|
||||||
|
- hFile uintptr // always 0
|
||||||
|
- flags uint32
|
||||||
|
- }{filename, 0, _LOAD_LIBRARY_SEARCH_SYSTEM32}
|
||||||
|
- c.args = uintptr(noescape(unsafe.Pointer(&args)))
|
||||||
|
+
|
||||||
|
+ if useLoadLibraryEx {
|
||||||
|
+ c.fn = getLoadLibraryEx()
|
||||||
|
+ c.n = 3
|
||||||
|
+ args := struct {
|
||||||
|
+ lpFileName *uint16
|
||||||
|
+ hFile uintptr // always 0
|
||||||
|
+ flags uint32
|
||||||
|
+ }{filename, 0, _LOAD_LIBRARY_SEARCH_SYSTEM32}
|
||||||
|
+ c.args = uintptr(noescape(unsafe.Pointer(&args)))
|
||||||
|
+ } else {
|
||||||
|
+ c.fn = getLoadLibrary()
|
||||||
|
+ c.n = 1
|
||||||
|
+ c.args = uintptr(noescape(unsafe.Pointer(&absoluteFilepath)))
|
||||||
|
+ }
|
||||||
|
|
||||||
|
cgocall(asmstdcallAddr, unsafe.Pointer(c))
|
||||||
|
KeepAlive(filename)
|
||||||
|
+ KeepAlive(absoluteFilepath)
|
||||||
|
handle = c.r1
|
||||||
|
if handle == 0 {
|
||||||
|
err = c.err
|
||||||
|
Index: src/syscall/dll_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/syscall/dll_windows.go b/src/syscall/dll_windows.go
|
||||||
|
--- a/src/syscall/dll_windows.go (revision 7f83badcb925a7e743188041cb6e561fc9b5b642)
|
||||||
|
+++ b/src/syscall/dll_windows.go (revision 83ff9782e024cb328b690cbf0da4e7848a327f4f)
|
||||||
|
@@ -44,7 +44,7 @@
|
||||||
|
|
||||||
|
func SyscallN(trap uintptr, args ...uintptr) (r1, r2 uintptr, err Errno)
|
||||||
|
func loadlibrary(filename *uint16) (handle uintptr, err Errno)
|
||||||
|
-func loadsystemlibrary(filename *uint16) (handle uintptr, err Errno)
|
||||||
|
+func loadsystemlibrary(filename *uint16, absoluteFilepath *uint16) (handle uintptr, err Errno)
|
||||||
|
func getprocaddress(handle uintptr, procname *uint8) (proc uintptr, err Errno)
|
||||||
|
|
||||||
|
// A DLL implements access to a single DLL.
|
||||||
|
@@ -53,6 +53,9 @@
|
||||||
|
Handle Handle
|
||||||
|
}
|
||||||
|
|
||||||
|
+//go:linkname getSystemDirectory
|
||||||
|
+func getSystemDirectory() string // Implemented in runtime package.
|
||||||
|
+
|
||||||
|
// LoadDLL loads the named DLL file into memory.
|
||||||
|
//
|
||||||
|
// If name is not an absolute path and is not a known system DLL used by
|
||||||
|
@@ -69,7 +72,11 @@
|
||||||
|
var h uintptr
|
||||||
|
var e Errno
|
||||||
|
if sysdll.IsSystemDLL[name] {
|
||||||
|
- h, e = loadsystemlibrary(namep)
|
||||||
|
+ absoluteFilepathp, err := UTF16PtrFromString(getSystemDirectory() + name)
|
||||||
|
+ if err != nil {
|
||||||
|
+ return nil, err
|
||||||
|
+ }
|
||||||
|
+ h, e = loadsystemlibrary(namep, absoluteFilepathp)
|
||||||
|
} else {
|
||||||
|
h, e = loadlibrary(namep)
|
||||||
|
}
|
||||||
643
.github/patch/go1.23.patch
vendored
Normal file
643
.github/patch/go1.23.patch
vendored
Normal file
@@ -0,0 +1,643 @@
|
|||||||
|
Subject: [PATCH] Revert "runtime: always use LoadLibraryEx to load system libraries"
|
||||||
|
Revert "syscall: remove Windows 7 console handle workaround"
|
||||||
|
Revert "net: remove sysSocket fallback for Windows 7"
|
||||||
|
Revert "crypto/rand,runtime: switch RtlGenRandom for ProcessPrng"
|
||||||
|
---
|
||||||
|
Index: src/crypto/rand/rand.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/crypto/rand/rand.go b/src/crypto/rand/rand.go
|
||||||
|
--- a/src/crypto/rand/rand.go (revision 6885bad7dd86880be6929c02085e5c7a67ff2887)
|
||||||
|
+++ b/src/crypto/rand/rand.go (revision 9ac42137ef6730e8b7daca016ece831297a1d75b)
|
||||||
|
@@ -16,7 +16,7 @@
|
||||||
|
// - On macOS and iOS, Reader uses arc4random_buf(3).
|
||||||
|
// - On OpenBSD and NetBSD, Reader uses getentropy(2).
|
||||||
|
// - On other Unix-like systems, Reader reads from /dev/urandom.
|
||||||
|
-// - On Windows, Reader uses the ProcessPrng API.
|
||||||
|
+// - On Windows systems, Reader uses the RtlGenRandom API.
|
||||||
|
// - On js/wasm, Reader uses the Web Crypto API.
|
||||||
|
// - On wasip1/wasm, Reader uses random_get from wasi_snapshot_preview1.
|
||||||
|
var Reader io.Reader
|
||||||
|
Index: src/crypto/rand/rand_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/crypto/rand/rand_windows.go b/src/crypto/rand/rand_windows.go
|
||||||
|
--- a/src/crypto/rand/rand_windows.go (revision 6885bad7dd86880be6929c02085e5c7a67ff2887)
|
||||||
|
+++ b/src/crypto/rand/rand_windows.go (revision 9ac42137ef6730e8b7daca016ece831297a1d75b)
|
||||||
|
@@ -15,8 +15,11 @@
|
||||||
|
|
||||||
|
type rngReader struct{}
|
||||||
|
|
||||||
|
-func (r *rngReader) Read(b []byte) (int, error) {
|
||||||
|
- if err := windows.ProcessPrng(b); err != nil {
|
||||||
|
+func (r *rngReader) Read(b []byte) (n int, err error) {
|
||||||
|
+ // RtlGenRandom only returns 1<<32-1 bytes at a time. We only read at
|
||||||
|
+ // most 1<<31-1 bytes at a time so that this works the same on 32-bit
|
||||||
|
+ // and 64-bit systems.
|
||||||
|
+ if err := batched(windows.RtlGenRandom, 1<<31-1)(b); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return len(b), nil
|
||||||
|
Index: src/internal/syscall/windows/syscall_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/internal/syscall/windows/syscall_windows.go b/src/internal/syscall/windows/syscall_windows.go
|
||||||
|
--- a/src/internal/syscall/windows/syscall_windows.go (revision 6885bad7dd86880be6929c02085e5c7a67ff2887)
|
||||||
|
+++ b/src/internal/syscall/windows/syscall_windows.go (revision 9ac42137ef6730e8b7daca016ece831297a1d75b)
|
||||||
|
@@ -414,7 +414,7 @@
|
||||||
|
//sys DestroyEnvironmentBlock(block *uint16) (err error) = userenv.DestroyEnvironmentBlock
|
||||||
|
//sys CreateEvent(eventAttrs *SecurityAttributes, manualReset uint32, initialState uint32, name *uint16) (handle syscall.Handle, err error) = kernel32.CreateEventW
|
||||||
|
|
||||||
|
-//sys ProcessPrng(buf []byte) (err error) = bcryptprimitives.ProcessPrng
|
||||||
|
+//sys RtlGenRandom(buf []byte) (err error) = advapi32.SystemFunction036
|
||||||
|
|
||||||
|
type FILE_ID_BOTH_DIR_INFO struct {
|
||||||
|
NextEntryOffset uint32
|
||||||
|
Index: src/internal/syscall/windows/zsyscall_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/internal/syscall/windows/zsyscall_windows.go b/src/internal/syscall/windows/zsyscall_windows.go
|
||||||
|
--- a/src/internal/syscall/windows/zsyscall_windows.go (revision 6885bad7dd86880be6929c02085e5c7a67ff2887)
|
||||||
|
+++ b/src/internal/syscall/windows/zsyscall_windows.go (revision 9ac42137ef6730e8b7daca016ece831297a1d75b)
|
||||||
|
@@ -38,7 +38,6 @@
|
||||||
|
|
||||||
|
var (
|
||||||
|
modadvapi32 = syscall.NewLazyDLL(sysdll.Add("advapi32.dll"))
|
||||||
|
- modbcryptprimitives = syscall.NewLazyDLL(sysdll.Add("bcryptprimitives.dll"))
|
||||||
|
modiphlpapi = syscall.NewLazyDLL(sysdll.Add("iphlpapi.dll"))
|
||||||
|
modkernel32 = syscall.NewLazyDLL(sysdll.Add("kernel32.dll"))
|
||||||
|
modnetapi32 = syscall.NewLazyDLL(sysdll.Add("netapi32.dll"))
|
||||||
|
@@ -57,7 +56,7 @@
|
||||||
|
procQueryServiceStatus = modadvapi32.NewProc("QueryServiceStatus")
|
||||||
|
procRevertToSelf = modadvapi32.NewProc("RevertToSelf")
|
||||||
|
procSetTokenInformation = modadvapi32.NewProc("SetTokenInformation")
|
||||||
|
- procProcessPrng = modbcryptprimitives.NewProc("ProcessPrng")
|
||||||
|
+ procSystemFunction036 = modadvapi32.NewProc("SystemFunction036")
|
||||||
|
procGetAdaptersAddresses = modiphlpapi.NewProc("GetAdaptersAddresses")
|
||||||
|
procCreateEventW = modkernel32.NewProc("CreateEventW")
|
||||||
|
procGetACP = modkernel32.NewProc("GetACP")
|
||||||
|
@@ -183,12 +182,12 @@
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
-func ProcessPrng(buf []byte) (err error) {
|
||||||
|
+func RtlGenRandom(buf []byte) (err error) {
|
||||||
|
var _p0 *byte
|
||||||
|
if len(buf) > 0 {
|
||||||
|
_p0 = &buf[0]
|
||||||
|
}
|
||||||
|
- r1, _, e1 := syscall.Syscall(procProcessPrng.Addr(), 2, uintptr(unsafe.Pointer(_p0)), uintptr(len(buf)), 0)
|
||||||
|
+ r1, _, e1 := syscall.Syscall(procSystemFunction036.Addr(), 2, uintptr(unsafe.Pointer(_p0)), uintptr(len(buf)), 0)
|
||||||
|
if r1 == 0 {
|
||||||
|
err = errnoErr(e1)
|
||||||
|
}
|
||||||
|
Index: src/runtime/os_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/runtime/os_windows.go b/src/runtime/os_windows.go
|
||||||
|
--- a/src/runtime/os_windows.go (revision 6885bad7dd86880be6929c02085e5c7a67ff2887)
|
||||||
|
+++ b/src/runtime/os_windows.go (revision 69e2eed6dd0f6d815ebf15797761c13f31213dd6)
|
||||||
|
@@ -39,8 +39,8 @@
|
||||||
|
//go:cgo_import_dynamic runtime._GetSystemInfo GetSystemInfo%1 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._GetThreadContext GetThreadContext%2 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._SetThreadContext SetThreadContext%2 "kernel32.dll"
|
||||||
|
-//go:cgo_import_dynamic runtime._LoadLibraryExW LoadLibraryExW%3 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._LoadLibraryW LoadLibraryW%1 "kernel32.dll"
|
||||||
|
+//go:cgo_import_dynamic runtime._LoadLibraryA LoadLibraryA%1 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._PostQueuedCompletionStatus PostQueuedCompletionStatus%4 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._QueryPerformanceCounter QueryPerformanceCounter%1 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._QueryPerformanceFrequency QueryPerformanceFrequency%1 "kernel32.dll"
|
||||||
|
@@ -74,7 +74,6 @@
|
||||||
|
// Following syscalls are available on every Windows PC.
|
||||||
|
// All these variables are set by the Windows executable
|
||||||
|
// loader before the Go program starts.
|
||||||
|
- _AddVectoredContinueHandler,
|
||||||
|
_AddVectoredExceptionHandler,
|
||||||
|
_CloseHandle,
|
||||||
|
_CreateEventA,
|
||||||
|
@@ -97,8 +96,8 @@
|
||||||
|
_GetSystemInfo,
|
||||||
|
_GetThreadContext,
|
||||||
|
_SetThreadContext,
|
||||||
|
- _LoadLibraryExW,
|
||||||
|
_LoadLibraryW,
|
||||||
|
+ _LoadLibraryA,
|
||||||
|
_PostQueuedCompletionStatus,
|
||||||
|
_QueryPerformanceCounter,
|
||||||
|
_QueryPerformanceFrequency,
|
||||||
|
@@ -127,8 +126,23 @@
|
||||||
|
_WriteFile,
|
||||||
|
_ stdFunction
|
||||||
|
|
||||||
|
- // Use ProcessPrng to generate cryptographically random data.
|
||||||
|
- _ProcessPrng stdFunction
|
||||||
|
+ // Following syscalls are only available on some Windows PCs.
|
||||||
|
+ // We will load syscalls, if available, before using them.
|
||||||
|
+ _AddDllDirectory,
|
||||||
|
+ _AddVectoredContinueHandler,
|
||||||
|
+ _LoadLibraryExA,
|
||||||
|
+ _LoadLibraryExW,
|
||||||
|
+ _ stdFunction
|
||||||
|
+
|
||||||
|
+ // Use RtlGenRandom to generate cryptographically random data.
|
||||||
|
+ // This approach has been recommended by Microsoft (see issue
|
||||||
|
+ // 15589 for details).
|
||||||
|
+ // The RtlGenRandom is not listed in advapi32.dll, instead
|
||||||
|
+ // RtlGenRandom function can be found by searching for SystemFunction036.
|
||||||
|
+ // Also some versions of Mingw cannot link to SystemFunction036
|
||||||
|
+ // when building executable as Cgo. So load SystemFunction036
|
||||||
|
+ // manually during runtime startup.
|
||||||
|
+ _RtlGenRandom stdFunction
|
||||||
|
|
||||||
|
// Load ntdll.dll manually during startup, otherwise Mingw
|
||||||
|
// links wrong printf function to cgo executable (see issue
|
||||||
|
@@ -145,13 +159,6 @@
|
||||||
|
_ stdFunction
|
||||||
|
)
|
||||||
|
|
||||||
|
-var (
|
||||||
|
- bcryptprimitivesdll = [...]uint16{'b', 'c', 'r', 'y', 'p', 't', 'p', 'r', 'i', 'm', 'i', 't', 'i', 'v', 'e', 's', '.', 'd', 'l', 'l', 0}
|
||||||
|
- ntdlldll = [...]uint16{'n', 't', 'd', 'l', 'l', '.', 'd', 'l', 'l', 0}
|
||||||
|
- powrprofdll = [...]uint16{'p', 'o', 'w', 'r', 'p', 'r', 'o', 'f', '.', 'd', 'l', 'l', 0}
|
||||||
|
- winmmdll = [...]uint16{'w', 'i', 'n', 'm', 'm', '.', 'd', 'l', 'l', 0}
|
||||||
|
-)
|
||||||
|
-
|
||||||
|
// Function to be called by windows CreateThread
|
||||||
|
// to start new os thread.
|
||||||
|
func tstart_stdcall(newm *m)
|
||||||
|
@@ -244,8 +251,18 @@
|
||||||
|
return unsafe.String(&sysDirectory[0], sysDirectoryLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
-func windowsLoadSystemLib(name []uint16) uintptr {
|
||||||
|
- return stdcall3(_LoadLibraryExW, uintptr(unsafe.Pointer(&name[0])), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32)
|
||||||
|
+//go:linkname syscall_getSystemDirectory syscall.getSystemDirectory
|
||||||
|
+func syscall_getSystemDirectory() string {
|
||||||
|
+ return unsafe.String(&sysDirectory[0], sysDirectoryLen)
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+func windowsLoadSystemLib(name []byte) uintptr {
|
||||||
|
+ if useLoadLibraryEx {
|
||||||
|
+ return stdcall3(_LoadLibraryExA, uintptr(unsafe.Pointer(&name[0])), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32)
|
||||||
|
+ } else {
|
||||||
|
+ absName := append(sysDirectory[:sysDirectoryLen], name...)
|
||||||
|
+ return stdcall1(_LoadLibraryA, uintptr(unsafe.Pointer(&absName[0])))
|
||||||
|
+ }
|
||||||
|
}
|
||||||
|
|
||||||
|
//go:linkname windows_QueryPerformanceCounter internal/syscall/windows.QueryPerformanceCounter
|
||||||
|
@@ -263,13 +280,28 @@
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadOptionalSyscalls() {
|
||||||
|
- bcryptPrimitives := windowsLoadSystemLib(bcryptprimitivesdll[:])
|
||||||
|
- if bcryptPrimitives == 0 {
|
||||||
|
- throw("bcryptprimitives.dll not found")
|
||||||
|
+ var kernel32dll = []byte("kernel32.dll\000")
|
||||||
|
+ k32 := stdcall1(_LoadLibraryA, uintptr(unsafe.Pointer(&kernel32dll[0])))
|
||||||
|
+ if k32 == 0 {
|
||||||
|
+ throw("kernel32.dll not found")
|
||||||
|
}
|
||||||
|
- _ProcessPrng = windowsFindfunc(bcryptPrimitives, []byte("ProcessPrng\000"))
|
||||||
|
+ _AddDllDirectory = windowsFindfunc(k32, []byte("AddDllDirectory\000"))
|
||||||
|
+ _AddVectoredContinueHandler = windowsFindfunc(k32, []byte("AddVectoredContinueHandler\000"))
|
||||||
|
+ _LoadLibraryExA = windowsFindfunc(k32, []byte("LoadLibraryExA\000"))
|
||||||
|
+ _LoadLibraryExW = windowsFindfunc(k32, []byte("LoadLibraryExW\000"))
|
||||||
|
+ useLoadLibraryEx = (_LoadLibraryExW != nil && _LoadLibraryExA != nil && _AddDllDirectory != nil)
|
||||||
|
+
|
||||||
|
+ initSysDirectory()
|
||||||
|
|
||||||
|
- n32 := windowsLoadSystemLib(ntdlldll[:])
|
||||||
|
+ var advapi32dll = []byte("advapi32.dll\000")
|
||||||
|
+ a32 := windowsLoadSystemLib(advapi32dll)
|
||||||
|
+ if a32 == 0 {
|
||||||
|
+ throw("advapi32.dll not found")
|
||||||
|
+ }
|
||||||
|
+ _RtlGenRandom = windowsFindfunc(a32, []byte("SystemFunction036\000"))
|
||||||
|
+
|
||||||
|
+ var ntdll = []byte("ntdll.dll\000")
|
||||||
|
+ n32 := windowsLoadSystemLib(ntdll)
|
||||||
|
if n32 == 0 {
|
||||||
|
throw("ntdll.dll not found")
|
||||||
|
}
|
||||||
|
@@ -298,7 +330,7 @@
|
||||||
|
context uintptr
|
||||||
|
}
|
||||||
|
|
||||||
|
- powrprof := windowsLoadSystemLib(powrprofdll[:])
|
||||||
|
+ powrprof := windowsLoadSystemLib([]byte("powrprof.dll\000"))
|
||||||
|
if powrprof == 0 {
|
||||||
|
return // Running on Windows 7, where we don't need it anyway.
|
||||||
|
}
|
||||||
|
@@ -357,6 +389,22 @@
|
||||||
|
// in sys_windows_386.s and sys_windows_amd64.s:
|
||||||
|
func getlasterror() uint32
|
||||||
|
|
||||||
|
+// When loading DLLs, we prefer to use LoadLibraryEx with
|
||||||
|
+// LOAD_LIBRARY_SEARCH_* flags, if available. LoadLibraryEx is not
|
||||||
|
+// available on old Windows, though, and the LOAD_LIBRARY_SEARCH_*
|
||||||
|
+// flags are not available on some versions of Windows without a
|
||||||
|
+// security patch.
|
||||||
|
+//
|
||||||
|
+// https://msdn.microsoft.com/en-us/library/ms684179(v=vs.85).aspx says:
|
||||||
|
+// "Windows 7, Windows Server 2008 R2, Windows Vista, and Windows
|
||||||
|
+// Server 2008: The LOAD_LIBRARY_SEARCH_* flags are available on
|
||||||
|
+// systems that have KB2533623 installed. To determine whether the
|
||||||
|
+// flags are available, use GetProcAddress to get the address of the
|
||||||
|
+// AddDllDirectory, RemoveDllDirectory, or SetDefaultDllDirectories
|
||||||
|
+// function. If GetProcAddress succeeds, the LOAD_LIBRARY_SEARCH_*
|
||||||
|
+// flags can be used with LoadLibraryEx."
|
||||||
|
+var useLoadLibraryEx bool
|
||||||
|
+
|
||||||
|
var timeBeginPeriodRetValue uint32
|
||||||
|
|
||||||
|
// osRelaxMinNS indicates that sysmon shouldn't osRelax if the next
|
||||||
|
@@ -430,7 +478,8 @@
|
||||||
|
// Only load winmm.dll if we need it.
|
||||||
|
// This avoids a dependency on winmm.dll for Go programs
|
||||||
|
// that run on new Windows versions.
|
||||||
|
- m32 := windowsLoadSystemLib(winmmdll[:])
|
||||||
|
+ var winmmdll = []byte("winmm.dll\000")
|
||||||
|
+ m32 := windowsLoadSystemLib(winmmdll)
|
||||||
|
if m32 == 0 {
|
||||||
|
print("runtime: LoadLibraryExW failed; errno=", getlasterror(), "\n")
|
||||||
|
throw("winmm.dll not found")
|
||||||
|
@@ -471,6 +520,28 @@
|
||||||
|
canUseLongPaths = true
|
||||||
|
}
|
||||||
|
|
||||||
|
+var osVersionInfo struct {
|
||||||
|
+ majorVersion uint32
|
||||||
|
+ minorVersion uint32
|
||||||
|
+ buildNumber uint32
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+func initOsVersionInfo() {
|
||||||
|
+ info := _OSVERSIONINFOW{}
|
||||||
|
+ info.osVersionInfoSize = uint32(unsafe.Sizeof(info))
|
||||||
|
+ stdcall1(_RtlGetVersion, uintptr(unsafe.Pointer(&info)))
|
||||||
|
+ osVersionInfo.majorVersion = info.majorVersion
|
||||||
|
+ osVersionInfo.minorVersion = info.minorVersion
|
||||||
|
+ osVersionInfo.buildNumber = info.buildNumber
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+//go:linkname rtlGetNtVersionNumbers syscall.rtlGetNtVersionNumbers
|
||||||
|
+func rtlGetNtVersionNumbers(majorVersion *uint32, minorVersion *uint32, buildNumber *uint32) {
|
||||||
|
+ *majorVersion = osVersionInfo.majorVersion
|
||||||
|
+ *minorVersion = osVersionInfo.minorVersion
|
||||||
|
+ *buildNumber = osVersionInfo.buildNumber
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
func osinit() {
|
||||||
|
asmstdcallAddr = unsafe.Pointer(abi.FuncPCABI0(asmstdcall))
|
||||||
|
|
||||||
|
@@ -483,8 +554,8 @@
|
||||||
|
initHighResTimer()
|
||||||
|
timeBeginPeriodRetValue = osRelax(false)
|
||||||
|
|
||||||
|
- initSysDirectory()
|
||||||
|
initLongPathSupport()
|
||||||
|
+ initOsVersionInfo()
|
||||||
|
|
||||||
|
ncpu = getproccount()
|
||||||
|
|
||||||
|
@@ -500,7 +571,7 @@
|
||||||
|
//go:nosplit
|
||||||
|
func readRandom(r []byte) int {
|
||||||
|
n := 0
|
||||||
|
- if stdcall2(_ProcessPrng, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 {
|
||||||
|
+ if stdcall2(_RtlGenRandom, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 {
|
||||||
|
n = len(r)
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
Index: src/net/hook_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/hook_windows.go b/src/net/hook_windows.go
|
||||||
|
--- a/src/net/hook_windows.go (revision 9ac42137ef6730e8b7daca016ece831297a1d75b)
|
||||||
|
+++ b/src/net/hook_windows.go (revision 21290de8a4c91408de7c2b5b68757b1e90af49dd)
|
||||||
|
@@ -13,6 +13,7 @@
|
||||||
|
hostsFilePath = windows.GetSystemDirectory() + "/Drivers/etc/hosts"
|
||||||
|
|
||||||
|
// Placeholders for socket system calls.
|
||||||
|
+ socketFunc func(int, int, int) (syscall.Handle, error) = syscall.Socket
|
||||||
|
wsaSocketFunc func(int32, int32, int32, *syscall.WSAProtocolInfo, uint32, uint32) (syscall.Handle, error) = windows.WSASocket
|
||||||
|
connectFunc func(syscall.Handle, syscall.Sockaddr) error = syscall.Connect
|
||||||
|
listenFunc func(syscall.Handle, int) error = syscall.Listen
|
||||||
|
Index: src/net/internal/socktest/main_test.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/internal/socktest/main_test.go b/src/net/internal/socktest/main_test.go
|
||||||
|
--- a/src/net/internal/socktest/main_test.go (revision 9ac42137ef6730e8b7daca016ece831297a1d75b)
|
||||||
|
+++ b/src/net/internal/socktest/main_test.go (revision 21290de8a4c91408de7c2b5b68757b1e90af49dd)
|
||||||
|
@@ -2,7 +2,7 @@
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
-//go:build !js && !plan9 && !wasip1 && !windows
|
||||||
|
+//go:build !js && !plan9 && !wasip1
|
||||||
|
|
||||||
|
package socktest_test
|
||||||
|
|
||||||
|
Index: src/net/internal/socktest/main_windows_test.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/internal/socktest/main_windows_test.go b/src/net/internal/socktest/main_windows_test.go
|
||||||
|
new file mode 100644
|
||||||
|
--- /dev/null (revision 21290de8a4c91408de7c2b5b68757b1e90af49dd)
|
||||||
|
+++ b/src/net/internal/socktest/main_windows_test.go (revision 21290de8a4c91408de7c2b5b68757b1e90af49dd)
|
||||||
|
@@ -0,0 +1,22 @@
|
||||||
|
+// Copyright 2015 The Go Authors. All rights reserved.
|
||||||
|
+// Use of this source code is governed by a BSD-style
|
||||||
|
+// license that can be found in the LICENSE file.
|
||||||
|
+
|
||||||
|
+package socktest_test
|
||||||
|
+
|
||||||
|
+import "syscall"
|
||||||
|
+
|
||||||
|
+var (
|
||||||
|
+ socketFunc func(int, int, int) (syscall.Handle, error)
|
||||||
|
+ closeFunc func(syscall.Handle) error
|
||||||
|
+)
|
||||||
|
+
|
||||||
|
+func installTestHooks() {
|
||||||
|
+ socketFunc = sw.Socket
|
||||||
|
+ closeFunc = sw.Closesocket
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+func uninstallTestHooks() {
|
||||||
|
+ socketFunc = syscall.Socket
|
||||||
|
+ closeFunc = syscall.Closesocket
|
||||||
|
+}
|
||||||
|
Index: src/net/internal/socktest/sys_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/internal/socktest/sys_windows.go b/src/net/internal/socktest/sys_windows.go
|
||||||
|
--- a/src/net/internal/socktest/sys_windows.go (revision 9ac42137ef6730e8b7daca016ece831297a1d75b)
|
||||||
|
+++ b/src/net/internal/socktest/sys_windows.go (revision 21290de8a4c91408de7c2b5b68757b1e90af49dd)
|
||||||
|
@@ -9,6 +9,38 @@
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
+// Socket wraps [syscall.Socket].
|
||||||
|
+func (sw *Switch) Socket(family, sotype, proto int) (s syscall.Handle, err error) {
|
||||||
|
+ sw.once.Do(sw.init)
|
||||||
|
+
|
||||||
|
+ so := &Status{Cookie: cookie(family, sotype, proto)}
|
||||||
|
+ sw.fmu.RLock()
|
||||||
|
+ f, _ := sw.fltab[FilterSocket]
|
||||||
|
+ sw.fmu.RUnlock()
|
||||||
|
+
|
||||||
|
+ af, err := f.apply(so)
|
||||||
|
+ if err != nil {
|
||||||
|
+ return syscall.InvalidHandle, err
|
||||||
|
+ }
|
||||||
|
+ s, so.Err = syscall.Socket(family, sotype, proto)
|
||||||
|
+ if err = af.apply(so); err != nil {
|
||||||
|
+ if so.Err == nil {
|
||||||
|
+ syscall.Closesocket(s)
|
||||||
|
+ }
|
||||||
|
+ return syscall.InvalidHandle, err
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
+ sw.smu.Lock()
|
||||||
|
+ defer sw.smu.Unlock()
|
||||||
|
+ if so.Err != nil {
|
||||||
|
+ sw.stats.getLocked(so.Cookie).OpenFailed++
|
||||||
|
+ return syscall.InvalidHandle, so.Err
|
||||||
|
+ }
|
||||||
|
+ nso := sw.addLocked(s, family, sotype, proto)
|
||||||
|
+ sw.stats.getLocked(nso.Cookie).Opened++
|
||||||
|
+ return s, nil
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
// WSASocket wraps [syscall.WSASocket].
|
||||||
|
func (sw *Switch) WSASocket(family, sotype, proto int32, protinfo *syscall.WSAProtocolInfo, group uint32, flags uint32) (s syscall.Handle, err error) {
|
||||||
|
sw.once.Do(sw.init)
|
||||||
|
Index: src/net/main_windows_test.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/main_windows_test.go b/src/net/main_windows_test.go
|
||||||
|
--- a/src/net/main_windows_test.go (revision 9ac42137ef6730e8b7daca016ece831297a1d75b)
|
||||||
|
+++ b/src/net/main_windows_test.go (revision 21290de8a4c91408de7c2b5b68757b1e90af49dd)
|
||||||
|
@@ -8,6 +8,7 @@
|
||||||
|
|
||||||
|
var (
|
||||||
|
// Placeholders for saving original socket system calls.
|
||||||
|
+ origSocket = socketFunc
|
||||||
|
origWSASocket = wsaSocketFunc
|
||||||
|
origClosesocket = poll.CloseFunc
|
||||||
|
origConnect = connectFunc
|
||||||
|
@@ -17,6 +18,7 @@
|
||||||
|
)
|
||||||
|
|
||||||
|
func installTestHooks() {
|
||||||
|
+ socketFunc = sw.Socket
|
||||||
|
wsaSocketFunc = sw.WSASocket
|
||||||
|
poll.CloseFunc = sw.Closesocket
|
||||||
|
connectFunc = sw.Connect
|
||||||
|
@@ -26,6 +28,7 @@
|
||||||
|
}
|
||||||
|
|
||||||
|
func uninstallTestHooks() {
|
||||||
|
+ socketFunc = origSocket
|
||||||
|
wsaSocketFunc = origWSASocket
|
||||||
|
poll.CloseFunc = origClosesocket
|
||||||
|
connectFunc = origConnect
|
||||||
|
Index: src/net/sock_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/sock_windows.go b/src/net/sock_windows.go
|
||||||
|
--- a/src/net/sock_windows.go (revision 9ac42137ef6730e8b7daca016ece831297a1d75b)
|
||||||
|
+++ b/src/net/sock_windows.go (revision 21290de8a4c91408de7c2b5b68757b1e90af49dd)
|
||||||
|
@@ -20,6 +20,21 @@
|
||||||
|
func sysSocket(family, sotype, proto int) (syscall.Handle, error) {
|
||||||
|
s, err := wsaSocketFunc(int32(family), int32(sotype), int32(proto),
|
||||||
|
nil, 0, windows.WSA_FLAG_OVERLAPPED|windows.WSA_FLAG_NO_HANDLE_INHERIT)
|
||||||
|
+ if err == nil {
|
||||||
|
+ return s, nil
|
||||||
|
+ }
|
||||||
|
+ // WSA_FLAG_NO_HANDLE_INHERIT flag is not supported on some
|
||||||
|
+ // old versions of Windows, see
|
||||||
|
+ // https://msdn.microsoft.com/en-us/library/windows/desktop/ms742212(v=vs.85).aspx
|
||||||
|
+ // for details. Just use syscall.Socket, if windows.WSASocket failed.
|
||||||
|
+
|
||||||
|
+ // See ../syscall/exec_unix.go for description of ForkLock.
|
||||||
|
+ syscall.ForkLock.RLock()
|
||||||
|
+ s, err = socketFunc(family, sotype, proto)
|
||||||
|
+ if err == nil {
|
||||||
|
+ syscall.CloseOnExec(s)
|
||||||
|
+ }
|
||||||
|
+ syscall.ForkLock.RUnlock()
|
||||||
|
if err != nil {
|
||||||
|
return syscall.InvalidHandle, os.NewSyscallError("socket", err)
|
||||||
|
}
|
||||||
|
Index: src/syscall/exec_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/syscall/exec_windows.go b/src/syscall/exec_windows.go
|
||||||
|
--- a/src/syscall/exec_windows.go (revision 9ac42137ef6730e8b7daca016ece831297a1d75b)
|
||||||
|
+++ b/src/syscall/exec_windows.go (revision 6a31d3fa8e47ddabc10bd97bff10d9a85f4cfb76)
|
||||||
|
@@ -14,7 +14,6 @@
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
-// ForkLock is not used on Windows.
|
||||||
|
var ForkLock sync.RWMutex
|
||||||
|
|
||||||
|
// EscapeArg rewrites command line argument s as prescribed
|
||||||
|
@@ -254,6 +253,9 @@
|
||||||
|
var zeroProcAttr ProcAttr
|
||||||
|
var zeroSysProcAttr SysProcAttr
|
||||||
|
|
||||||
|
+//go:linkname rtlGetNtVersionNumbers
|
||||||
|
+func rtlGetNtVersionNumbers(majorVersion *uint32, minorVersion *uint32, buildNumber *uint32)
|
||||||
|
+
|
||||||
|
func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle uintptr, err error) {
|
||||||
|
if len(argv0) == 0 {
|
||||||
|
return 0, 0, EWINDOWS
|
||||||
|
@@ -317,6 +319,17 @@
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
+ var maj, min, build uint32
|
||||||
|
+ rtlGetNtVersionNumbers(&maj, &min, &build)
|
||||||
|
+ isWin7 := maj < 6 || (maj == 6 && min <= 1)
|
||||||
|
+ // NT kernel handles are divisible by 4, with the bottom 3 bits left as
|
||||||
|
+ // a tag. The fully set tag correlates with the types of handles we're
|
||||||
|
+ // concerned about here. Except, the kernel will interpret some
|
||||||
|
+ // special handle values, like -1, -2, and so forth, so kernelbase.dll
|
||||||
|
+ // checks to see that those bottom three bits are checked, but that top
|
||||||
|
+ // bit is not checked.
|
||||||
|
+ isLegacyWin7ConsoleHandle := func(handle Handle) bool { return isWin7 && handle&0x10000003 == 3 }
|
||||||
|
+
|
||||||
|
p, _ := GetCurrentProcess()
|
||||||
|
parentProcess := p
|
||||||
|
if sys.ParentProcess != 0 {
|
||||||
|
@@ -325,7 +338,15 @@
|
||||||
|
fd := make([]Handle, len(attr.Files))
|
||||||
|
for i := range attr.Files {
|
||||||
|
if attr.Files[i] > 0 {
|
||||||
|
- err := DuplicateHandle(p, Handle(attr.Files[i]), parentProcess, &fd[i], 0, true, DUPLICATE_SAME_ACCESS)
|
||||||
|
+ destinationProcessHandle := parentProcess
|
||||||
|
+
|
||||||
|
+ // On Windows 7, console handles aren't real handles, and can only be duplicated
|
||||||
|
+ // into the current process, not a parent one, which amounts to the same thing.
|
||||||
|
+ if parentProcess != p && isLegacyWin7ConsoleHandle(Handle(attr.Files[i])) {
|
||||||
|
+ destinationProcessHandle = p
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
+ err := DuplicateHandle(p, Handle(attr.Files[i]), destinationProcessHandle, &fd[i], 0, true, DUPLICATE_SAME_ACCESS)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
@@ -356,6 +377,14 @@
|
||||||
|
|
||||||
|
fd = append(fd, sys.AdditionalInheritedHandles...)
|
||||||
|
|
||||||
|
+ // On Windows 7, console handles aren't real handles, so don't pass them
|
||||||
|
+ // through to PROC_THREAD_ATTRIBUTE_HANDLE_LIST.
|
||||||
|
+ for i := range fd {
|
||||||
|
+ if isLegacyWin7ConsoleHandle(fd[i]) {
|
||||||
|
+ fd[i] = 0
|
||||||
|
+ }
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
// The presence of a NULL handle in the list is enough to cause PROC_THREAD_ATTRIBUTE_HANDLE_LIST
|
||||||
|
// to treat the entire list as empty, so remove NULL handles.
|
||||||
|
j := 0
|
||||||
|
Index: src/runtime/syscall_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/runtime/syscall_windows.go b/src/runtime/syscall_windows.go
|
||||||
|
--- a/src/runtime/syscall_windows.go (revision 6a31d3fa8e47ddabc10bd97bff10d9a85f4cfb76)
|
||||||
|
+++ b/src/runtime/syscall_windows.go (revision 69e2eed6dd0f6d815ebf15797761c13f31213dd6)
|
||||||
|
@@ -413,10 +413,20 @@
|
||||||
|
|
||||||
|
const _LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800
|
||||||
|
|
||||||
|
+// When available, this function will use LoadLibraryEx with the filename
|
||||||
|
+// parameter and the important SEARCH_SYSTEM32 argument. But on systems that
|
||||||
|
+// do not have that option, absoluteFilepath should contain a fallback
|
||||||
|
+// to the full path inside of system32 for use with vanilla LoadLibrary.
|
||||||
|
+//
|
||||||
|
//go:linkname syscall_loadsystemlibrary syscall.loadsystemlibrary
|
||||||
|
-func syscall_loadsystemlibrary(filename *uint16) (handle, err uintptr) {
|
||||||
|
- handle, _, err = syscall_SyscallN(uintptr(unsafe.Pointer(_LoadLibraryExW)), uintptr(unsafe.Pointer(filename)), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32)
|
||||||
|
+func syscall_loadsystemlibrary(filename *uint16, absoluteFilepath *uint16) (handle, err uintptr) {
|
||||||
|
+ if useLoadLibraryEx {
|
||||||
|
+ handle, _, err = syscall_SyscallN(uintptr(unsafe.Pointer(_LoadLibraryExW)), uintptr(unsafe.Pointer(filename)), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32)
|
||||||
|
+ } else {
|
||||||
|
+ handle, _, err = syscall_SyscallN(uintptr(unsafe.Pointer(_LoadLibraryW)), uintptr(unsafe.Pointer(absoluteFilepath)))
|
||||||
|
+ }
|
||||||
|
KeepAlive(filename)
|
||||||
|
+ KeepAlive(absoluteFilepath)
|
||||||
|
if handle != 0 {
|
||||||
|
err = 0
|
||||||
|
}
|
||||||
|
Index: src/syscall/dll_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/syscall/dll_windows.go b/src/syscall/dll_windows.go
|
||||||
|
--- a/src/syscall/dll_windows.go (revision 6a31d3fa8e47ddabc10bd97bff10d9a85f4cfb76)
|
||||||
|
+++ b/src/syscall/dll_windows.go (revision 69e2eed6dd0f6d815ebf15797761c13f31213dd6)
|
||||||
|
@@ -44,7 +44,7 @@
|
||||||
|
|
||||||
|
func SyscallN(trap uintptr, args ...uintptr) (r1, r2 uintptr, err Errno)
|
||||||
|
func loadlibrary(filename *uint16) (handle uintptr, err Errno)
|
||||||
|
-func loadsystemlibrary(filename *uint16) (handle uintptr, err Errno)
|
||||||
|
+func loadsystemlibrary(filename *uint16, absoluteFilepath *uint16) (handle uintptr, err Errno)
|
||||||
|
func getprocaddress(handle uintptr, procname *uint8) (proc uintptr, err Errno)
|
||||||
|
|
||||||
|
// A DLL implements access to a single DLL.
|
||||||
|
@@ -53,6 +53,9 @@
|
||||||
|
Handle Handle
|
||||||
|
}
|
||||||
|
|
||||||
|
+//go:linkname getSystemDirectory
|
||||||
|
+func getSystemDirectory() string // Implemented in runtime package.
|
||||||
|
+
|
||||||
|
// LoadDLL loads the named DLL file into memory.
|
||||||
|
//
|
||||||
|
// If name is not an absolute path and is not a known system DLL used by
|
||||||
|
@@ -69,7 +72,11 @@
|
||||||
|
var h uintptr
|
||||||
|
var e Errno
|
||||||
|
if sysdll.IsSystemDLL[name] {
|
||||||
|
- h, e = loadsystemlibrary(namep)
|
||||||
|
+ absoluteFilepathp, err := UTF16PtrFromString(getSystemDirectory() + name)
|
||||||
|
+ if err != nil {
|
||||||
|
+ return nil, err
|
||||||
|
+ }
|
||||||
|
+ h, e = loadsystemlibrary(namep, absoluteFilepathp)
|
||||||
|
} else {
|
||||||
|
h, e = loadlibrary(namep)
|
||||||
|
}
|
||||||
657
.github/patch/go1.24.patch
vendored
Normal file
657
.github/patch/go1.24.patch
vendored
Normal file
@@ -0,0 +1,657 @@
|
|||||||
|
Subject: [PATCH] Revert "runtime: always use LoadLibraryEx to load system libraries"
|
||||||
|
Revert "syscall: remove Windows 7 console handle workaround"
|
||||||
|
Revert "net: remove sysSocket fallback for Windows 7"
|
||||||
|
Revert "crypto/rand,runtime: switch RtlGenRandom for ProcessPrng"
|
||||||
|
---
|
||||||
|
Index: src/crypto/internal/sysrand/rand_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/crypto/internal/sysrand/rand_windows.go b/src/crypto/internal/sysrand/rand_windows.go
|
||||||
|
--- a/src/crypto/internal/sysrand/rand_windows.go (revision 3901409b5d0fb7c85a3e6730a59943cc93b2835c)
|
||||||
|
+++ b/src/crypto/internal/sysrand/rand_windows.go (revision 2a406dc9f1ea7323d6ca9fccb2fe9ddebb6b1cc8)
|
||||||
|
@@ -7,5 +7,26 @@
|
||||||
|
import "internal/syscall/windows"
|
||||||
|
|
||||||
|
func read(b []byte) error {
|
||||||
|
- return windows.ProcessPrng(b)
|
||||||
|
+ // RtlGenRandom only returns 1<<32-1 bytes at a time. We only read at
|
||||||
|
+ // most 1<<31-1 bytes at a time so that this works the same on 32-bit
|
||||||
|
+ // and 64-bit systems.
|
||||||
|
+ return batched(windows.RtlGenRandom, 1<<31-1)(b)
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+// batched returns a function that calls f to populate a []byte by chunking it
|
||||||
|
+// into subslices of, at most, readMax bytes.
|
||||||
|
+func batched(f func([]byte) error, readMax int) func([]byte) error {
|
||||||
|
+ return func(out []byte) error {
|
||||||
|
+ for len(out) > 0 {
|
||||||
|
+ read := len(out)
|
||||||
|
+ if read > readMax {
|
||||||
|
+ read = readMax
|
||||||
|
+ }
|
||||||
|
+ if err := f(out[:read]); err != nil {
|
||||||
|
+ return err
|
||||||
|
+ }
|
||||||
|
+ out = out[read:]
|
||||||
|
+ }
|
||||||
|
+ return nil
|
||||||
|
+ }
|
||||||
|
}
|
||||||
|
Index: src/crypto/rand/rand.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/crypto/rand/rand.go b/src/crypto/rand/rand.go
|
||||||
|
--- a/src/crypto/rand/rand.go (revision 3901409b5d0fb7c85a3e6730a59943cc93b2835c)
|
||||||
|
+++ b/src/crypto/rand/rand.go (revision 2a406dc9f1ea7323d6ca9fccb2fe9ddebb6b1cc8)
|
||||||
|
@@ -22,7 +22,7 @@
|
||||||
|
// - On legacy Linux (< 3.17), Reader opens /dev/urandom on first use.
|
||||||
|
// - On macOS, iOS, and OpenBSD Reader, uses arc4random_buf(3).
|
||||||
|
// - On NetBSD, Reader uses the kern.arandom sysctl.
|
||||||
|
-// - On Windows, Reader uses the ProcessPrng API.
|
||||||
|
+// - On Windows systems, Reader uses the RtlGenRandom API.
|
||||||
|
// - On js/wasm, Reader uses the Web Crypto API.
|
||||||
|
// - On wasip1/wasm, Reader uses random_get.
|
||||||
|
//
|
||||||
|
Index: src/internal/syscall/windows/syscall_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/internal/syscall/windows/syscall_windows.go b/src/internal/syscall/windows/syscall_windows.go
|
||||||
|
--- a/src/internal/syscall/windows/syscall_windows.go (revision 3901409b5d0fb7c85a3e6730a59943cc93b2835c)
|
||||||
|
+++ b/src/internal/syscall/windows/syscall_windows.go (revision 2a406dc9f1ea7323d6ca9fccb2fe9ddebb6b1cc8)
|
||||||
|
@@ -416,7 +416,7 @@
|
||||||
|
//sys DestroyEnvironmentBlock(block *uint16) (err error) = userenv.DestroyEnvironmentBlock
|
||||||
|
//sys CreateEvent(eventAttrs *SecurityAttributes, manualReset uint32, initialState uint32, name *uint16) (handle syscall.Handle, err error) = kernel32.CreateEventW
|
||||||
|
|
||||||
|
-//sys ProcessPrng(buf []byte) (err error) = bcryptprimitives.ProcessPrng
|
||||||
|
+//sys RtlGenRandom(buf []byte) (err error) = advapi32.SystemFunction036
|
||||||
|
|
||||||
|
type FILE_ID_BOTH_DIR_INFO struct {
|
||||||
|
NextEntryOffset uint32
|
||||||
|
Index: src/internal/syscall/windows/zsyscall_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/internal/syscall/windows/zsyscall_windows.go b/src/internal/syscall/windows/zsyscall_windows.go
|
||||||
|
--- a/src/internal/syscall/windows/zsyscall_windows.go (revision 3901409b5d0fb7c85a3e6730a59943cc93b2835c)
|
||||||
|
+++ b/src/internal/syscall/windows/zsyscall_windows.go (revision 2a406dc9f1ea7323d6ca9fccb2fe9ddebb6b1cc8)
|
||||||
|
@@ -38,7 +38,6 @@
|
||||||
|
|
||||||
|
var (
|
||||||
|
modadvapi32 = syscall.NewLazyDLL(sysdll.Add("advapi32.dll"))
|
||||||
|
- modbcryptprimitives = syscall.NewLazyDLL(sysdll.Add("bcryptprimitives.dll"))
|
||||||
|
modiphlpapi = syscall.NewLazyDLL(sysdll.Add("iphlpapi.dll"))
|
||||||
|
modkernel32 = syscall.NewLazyDLL(sysdll.Add("kernel32.dll"))
|
||||||
|
modnetapi32 = syscall.NewLazyDLL(sysdll.Add("netapi32.dll"))
|
||||||
|
@@ -63,7 +62,7 @@
|
||||||
|
procQueryServiceStatus = modadvapi32.NewProc("QueryServiceStatus")
|
||||||
|
procRevertToSelf = modadvapi32.NewProc("RevertToSelf")
|
||||||
|
procSetTokenInformation = modadvapi32.NewProc("SetTokenInformation")
|
||||||
|
- procProcessPrng = modbcryptprimitives.NewProc("ProcessPrng")
|
||||||
|
+ procSystemFunction036 = modadvapi32.NewProc("SystemFunction036")
|
||||||
|
procGetAdaptersAddresses = modiphlpapi.NewProc("GetAdaptersAddresses")
|
||||||
|
procCreateEventW = modkernel32.NewProc("CreateEventW")
|
||||||
|
procGetACP = modkernel32.NewProc("GetACP")
|
||||||
|
@@ -236,12 +235,12 @@
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
-func ProcessPrng(buf []byte) (err error) {
|
||||||
|
+func RtlGenRandom(buf []byte) (err error) {
|
||||||
|
var _p0 *byte
|
||||||
|
if len(buf) > 0 {
|
||||||
|
_p0 = &buf[0]
|
||||||
|
}
|
||||||
|
- r1, _, e1 := syscall.Syscall(procProcessPrng.Addr(), 2, uintptr(unsafe.Pointer(_p0)), uintptr(len(buf)), 0)
|
||||||
|
+ r1, _, e1 := syscall.Syscall(procSystemFunction036.Addr(), 2, uintptr(unsafe.Pointer(_p0)), uintptr(len(buf)), 0)
|
||||||
|
if r1 == 0 {
|
||||||
|
err = errnoErr(e1)
|
||||||
|
}
|
||||||
|
Index: src/runtime/os_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/runtime/os_windows.go b/src/runtime/os_windows.go
|
||||||
|
--- a/src/runtime/os_windows.go (revision 3901409b5d0fb7c85a3e6730a59943cc93b2835c)
|
||||||
|
+++ b/src/runtime/os_windows.go (revision ac3e93c061779dfefc0dd13a5b6e6f764a25621e)
|
||||||
|
@@ -40,8 +40,8 @@
|
||||||
|
//go:cgo_import_dynamic runtime._GetSystemInfo GetSystemInfo%1 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._GetThreadContext GetThreadContext%2 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._SetThreadContext SetThreadContext%2 "kernel32.dll"
|
||||||
|
-//go:cgo_import_dynamic runtime._LoadLibraryExW LoadLibraryExW%3 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._LoadLibraryW LoadLibraryW%1 "kernel32.dll"
|
||||||
|
+//go:cgo_import_dynamic runtime._LoadLibraryA LoadLibraryA%1 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._PostQueuedCompletionStatus PostQueuedCompletionStatus%4 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._QueryPerformanceCounter QueryPerformanceCounter%1 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._QueryPerformanceFrequency QueryPerformanceFrequency%1 "kernel32.dll"
|
||||||
|
@@ -75,7 +75,6 @@
|
||||||
|
// Following syscalls are available on every Windows PC.
|
||||||
|
// All these variables are set by the Windows executable
|
||||||
|
// loader before the Go program starts.
|
||||||
|
- _AddVectoredContinueHandler,
|
||||||
|
_AddVectoredExceptionHandler,
|
||||||
|
_CloseHandle,
|
||||||
|
_CreateEventA,
|
||||||
|
@@ -98,8 +97,8 @@
|
||||||
|
_GetSystemInfo,
|
||||||
|
_GetThreadContext,
|
||||||
|
_SetThreadContext,
|
||||||
|
- _LoadLibraryExW,
|
||||||
|
_LoadLibraryW,
|
||||||
|
+ _LoadLibraryA,
|
||||||
|
_PostQueuedCompletionStatus,
|
||||||
|
_QueryPerformanceCounter,
|
||||||
|
_QueryPerformanceFrequency,
|
||||||
|
@@ -128,8 +127,23 @@
|
||||||
|
_WriteFile,
|
||||||
|
_ stdFunction
|
||||||
|
|
||||||
|
- // Use ProcessPrng to generate cryptographically random data.
|
||||||
|
- _ProcessPrng stdFunction
|
||||||
|
+ // Following syscalls are only available on some Windows PCs.
|
||||||
|
+ // We will load syscalls, if available, before using them.
|
||||||
|
+ _AddDllDirectory,
|
||||||
|
+ _AddVectoredContinueHandler,
|
||||||
|
+ _LoadLibraryExA,
|
||||||
|
+ _LoadLibraryExW,
|
||||||
|
+ _ stdFunction
|
||||||
|
+
|
||||||
|
+ // Use RtlGenRandom to generate cryptographically random data.
|
||||||
|
+ // This approach has been recommended by Microsoft (see issue
|
||||||
|
+ // 15589 for details).
|
||||||
|
+ // The RtlGenRandom is not listed in advapi32.dll, instead
|
||||||
|
+ // RtlGenRandom function can be found by searching for SystemFunction036.
|
||||||
|
+ // Also some versions of Mingw cannot link to SystemFunction036
|
||||||
|
+ // when building executable as Cgo. So load SystemFunction036
|
||||||
|
+ // manually during runtime startup.
|
||||||
|
+ _RtlGenRandom stdFunction
|
||||||
|
|
||||||
|
// Load ntdll.dll manually during startup, otherwise Mingw
|
||||||
|
// links wrong printf function to cgo executable (see issue
|
||||||
|
@@ -146,13 +160,6 @@
|
||||||
|
_ stdFunction
|
||||||
|
)
|
||||||
|
|
||||||
|
-var (
|
||||||
|
- bcryptprimitivesdll = [...]uint16{'b', 'c', 'r', 'y', 'p', 't', 'p', 'r', 'i', 'm', 'i', 't', 'i', 'v', 'e', 's', '.', 'd', 'l', 'l', 0}
|
||||||
|
- ntdlldll = [...]uint16{'n', 't', 'd', 'l', 'l', '.', 'd', 'l', 'l', 0}
|
||||||
|
- powrprofdll = [...]uint16{'p', 'o', 'w', 'r', 'p', 'r', 'o', 'f', '.', 'd', 'l', 'l', 0}
|
||||||
|
- winmmdll = [...]uint16{'w', 'i', 'n', 'm', 'm', '.', 'd', 'l', 'l', 0}
|
||||||
|
-)
|
||||||
|
-
|
||||||
|
// Function to be called by windows CreateThread
|
||||||
|
// to start new os thread.
|
||||||
|
func tstart_stdcall(newm *m)
|
||||||
|
@@ -245,8 +252,18 @@
|
||||||
|
return unsafe.String(&sysDirectory[0], sysDirectoryLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
-func windowsLoadSystemLib(name []uint16) uintptr {
|
||||||
|
- return stdcall3(_LoadLibraryExW, uintptr(unsafe.Pointer(&name[0])), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32)
|
||||||
|
+//go:linkname syscall_getSystemDirectory syscall.getSystemDirectory
|
||||||
|
+func syscall_getSystemDirectory() string {
|
||||||
|
+ return unsafe.String(&sysDirectory[0], sysDirectoryLen)
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+func windowsLoadSystemLib(name []byte) uintptr {
|
||||||
|
+ if useLoadLibraryEx {
|
||||||
|
+ return stdcall3(_LoadLibraryExA, uintptr(unsafe.Pointer(&name[0])), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32)
|
||||||
|
+ } else {
|
||||||
|
+ absName := append(sysDirectory[:sysDirectoryLen], name...)
|
||||||
|
+ return stdcall1(_LoadLibraryA, uintptr(unsafe.Pointer(&absName[0])))
|
||||||
|
+ }
|
||||||
|
}
|
||||||
|
|
||||||
|
//go:linkname windows_QueryPerformanceCounter internal/syscall/windows.QueryPerformanceCounter
|
||||||
|
@@ -264,13 +281,28 @@
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadOptionalSyscalls() {
|
||||||
|
- bcryptPrimitives := windowsLoadSystemLib(bcryptprimitivesdll[:])
|
||||||
|
- if bcryptPrimitives == 0 {
|
||||||
|
- throw("bcryptprimitives.dll not found")
|
||||||
|
+ var kernel32dll = []byte("kernel32.dll\000")
|
||||||
|
+ k32 := stdcall1(_LoadLibraryA, uintptr(unsafe.Pointer(&kernel32dll[0])))
|
||||||
|
+ if k32 == 0 {
|
||||||
|
+ throw("kernel32.dll not found")
|
||||||
|
}
|
||||||
|
- _ProcessPrng = windowsFindfunc(bcryptPrimitives, []byte("ProcessPrng\000"))
|
||||||
|
+ _AddDllDirectory = windowsFindfunc(k32, []byte("AddDllDirectory\000"))
|
||||||
|
+ _AddVectoredContinueHandler = windowsFindfunc(k32, []byte("AddVectoredContinueHandler\000"))
|
||||||
|
+ _LoadLibraryExA = windowsFindfunc(k32, []byte("LoadLibraryExA\000"))
|
||||||
|
+ _LoadLibraryExW = windowsFindfunc(k32, []byte("LoadLibraryExW\000"))
|
||||||
|
+ useLoadLibraryEx = (_LoadLibraryExW != nil && _LoadLibraryExA != nil && _AddDllDirectory != nil)
|
||||||
|
+
|
||||||
|
+ initSysDirectory()
|
||||||
|
|
||||||
|
- n32 := windowsLoadSystemLib(ntdlldll[:])
|
||||||
|
+ var advapi32dll = []byte("advapi32.dll\000")
|
||||||
|
+ a32 := windowsLoadSystemLib(advapi32dll)
|
||||||
|
+ if a32 == 0 {
|
||||||
|
+ throw("advapi32.dll not found")
|
||||||
|
+ }
|
||||||
|
+ _RtlGenRandom = windowsFindfunc(a32, []byte("SystemFunction036\000"))
|
||||||
|
+
|
||||||
|
+ var ntdll = []byte("ntdll.dll\000")
|
||||||
|
+ n32 := windowsLoadSystemLib(ntdll)
|
||||||
|
if n32 == 0 {
|
||||||
|
throw("ntdll.dll not found")
|
||||||
|
}
|
||||||
|
@@ -299,7 +331,7 @@
|
||||||
|
context uintptr
|
||||||
|
}
|
||||||
|
|
||||||
|
- powrprof := windowsLoadSystemLib(powrprofdll[:])
|
||||||
|
+ powrprof := windowsLoadSystemLib([]byte("powrprof.dll\000"))
|
||||||
|
if powrprof == 0 {
|
||||||
|
return // Running on Windows 7, where we don't need it anyway.
|
||||||
|
}
|
||||||
|
@@ -358,6 +390,22 @@
|
||||||
|
// in sys_windows_386.s and sys_windows_amd64.s:
|
||||||
|
func getlasterror() uint32
|
||||||
|
|
||||||
|
+// When loading DLLs, we prefer to use LoadLibraryEx with
|
||||||
|
+// LOAD_LIBRARY_SEARCH_* flags, if available. LoadLibraryEx is not
|
||||||
|
+// available on old Windows, though, and the LOAD_LIBRARY_SEARCH_*
|
||||||
|
+// flags are not available on some versions of Windows without a
|
||||||
|
+// security patch.
|
||||||
|
+//
|
||||||
|
+// https://msdn.microsoft.com/en-us/library/ms684179(v=vs.85).aspx says:
|
||||||
|
+// "Windows 7, Windows Server 2008 R2, Windows Vista, and Windows
|
||||||
|
+// Server 2008: The LOAD_LIBRARY_SEARCH_* flags are available on
|
||||||
|
+// systems that have KB2533623 installed. To determine whether the
|
||||||
|
+// flags are available, use GetProcAddress to get the address of the
|
||||||
|
+// AddDllDirectory, RemoveDllDirectory, or SetDefaultDllDirectories
|
||||||
|
+// function. If GetProcAddress succeeds, the LOAD_LIBRARY_SEARCH_*
|
||||||
|
+// flags can be used with LoadLibraryEx."
|
||||||
|
+var useLoadLibraryEx bool
|
||||||
|
+
|
||||||
|
var timeBeginPeriodRetValue uint32
|
||||||
|
|
||||||
|
// osRelaxMinNS indicates that sysmon shouldn't osRelax if the next
|
||||||
|
@@ -431,7 +479,8 @@
|
||||||
|
// Only load winmm.dll if we need it.
|
||||||
|
// This avoids a dependency on winmm.dll for Go programs
|
||||||
|
// that run on new Windows versions.
|
||||||
|
- m32 := windowsLoadSystemLib(winmmdll[:])
|
||||||
|
+ var winmmdll = []byte("winmm.dll\000")
|
||||||
|
+ m32 := windowsLoadSystemLib(winmmdll)
|
||||||
|
if m32 == 0 {
|
||||||
|
print("runtime: LoadLibraryExW failed; errno=", getlasterror(), "\n")
|
||||||
|
throw("winmm.dll not found")
|
||||||
|
@@ -472,6 +521,28 @@
|
||||||
|
canUseLongPaths = true
|
||||||
|
}
|
||||||
|
|
||||||
|
+var osVersionInfo struct {
|
||||||
|
+ majorVersion uint32
|
||||||
|
+ minorVersion uint32
|
||||||
|
+ buildNumber uint32
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+func initOsVersionInfo() {
|
||||||
|
+ info := _OSVERSIONINFOW{}
|
||||||
|
+ info.osVersionInfoSize = uint32(unsafe.Sizeof(info))
|
||||||
|
+ stdcall1(_RtlGetVersion, uintptr(unsafe.Pointer(&info)))
|
||||||
|
+ osVersionInfo.majorVersion = info.majorVersion
|
||||||
|
+ osVersionInfo.minorVersion = info.minorVersion
|
||||||
|
+ osVersionInfo.buildNumber = info.buildNumber
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+//go:linkname rtlGetNtVersionNumbers syscall.rtlGetNtVersionNumbers
|
||||||
|
+func rtlGetNtVersionNumbers(majorVersion *uint32, minorVersion *uint32, buildNumber *uint32) {
|
||||||
|
+ *majorVersion = osVersionInfo.majorVersion
|
||||||
|
+ *minorVersion = osVersionInfo.minorVersion
|
||||||
|
+ *buildNumber = osVersionInfo.buildNumber
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
func osinit() {
|
||||||
|
asmstdcallAddr = unsafe.Pointer(abi.FuncPCABI0(asmstdcall))
|
||||||
|
|
||||||
|
@@ -484,8 +555,8 @@
|
||||||
|
initHighResTimer()
|
||||||
|
timeBeginPeriodRetValue = osRelax(false)
|
||||||
|
|
||||||
|
- initSysDirectory()
|
||||||
|
initLongPathSupport()
|
||||||
|
+ initOsVersionInfo()
|
||||||
|
|
||||||
|
ncpu = getproccount()
|
||||||
|
|
||||||
|
@@ -501,7 +572,7 @@
|
||||||
|
//go:nosplit
|
||||||
|
func readRandom(r []byte) int {
|
||||||
|
n := 0
|
||||||
|
- if stdcall2(_ProcessPrng, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 {
|
||||||
|
+ if stdcall2(_RtlGenRandom, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 {
|
||||||
|
n = len(r)
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
Index: src/net/hook_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/hook_windows.go b/src/net/hook_windows.go
|
||||||
|
--- a/src/net/hook_windows.go (revision 2a406dc9f1ea7323d6ca9fccb2fe9ddebb6b1cc8)
|
||||||
|
+++ b/src/net/hook_windows.go (revision 7b1fd7d39c6be0185fbe1d929578ab372ac5c632)
|
||||||
|
@@ -13,6 +13,7 @@
|
||||||
|
hostsFilePath = windows.GetSystemDirectory() + "/Drivers/etc/hosts"
|
||||||
|
|
||||||
|
// Placeholders for socket system calls.
|
||||||
|
+ socketFunc func(int, int, int) (syscall.Handle, error) = syscall.Socket
|
||||||
|
wsaSocketFunc func(int32, int32, int32, *syscall.WSAProtocolInfo, uint32, uint32) (syscall.Handle, error) = windows.WSASocket
|
||||||
|
connectFunc func(syscall.Handle, syscall.Sockaddr) error = syscall.Connect
|
||||||
|
listenFunc func(syscall.Handle, int) error = syscall.Listen
|
||||||
|
Index: src/net/internal/socktest/main_test.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/internal/socktest/main_test.go b/src/net/internal/socktest/main_test.go
|
||||||
|
--- a/src/net/internal/socktest/main_test.go (revision 2a406dc9f1ea7323d6ca9fccb2fe9ddebb6b1cc8)
|
||||||
|
+++ b/src/net/internal/socktest/main_test.go (revision 7b1fd7d39c6be0185fbe1d929578ab372ac5c632)
|
||||||
|
@@ -2,7 +2,7 @@
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
-//go:build !js && !plan9 && !wasip1 && !windows
|
||||||
|
+//go:build !js && !plan9 && !wasip1
|
||||||
|
|
||||||
|
package socktest_test
|
||||||
|
|
||||||
|
Index: src/net/internal/socktest/main_windows_test.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/internal/socktest/main_windows_test.go b/src/net/internal/socktest/main_windows_test.go
|
||||||
|
new file mode 100644
|
||||||
|
--- /dev/null (revision 7b1fd7d39c6be0185fbe1d929578ab372ac5c632)
|
||||||
|
+++ b/src/net/internal/socktest/main_windows_test.go (revision 7b1fd7d39c6be0185fbe1d929578ab372ac5c632)
|
||||||
|
@@ -0,0 +1,22 @@
|
||||||
|
+// Copyright 2015 The Go Authors. All rights reserved.
|
||||||
|
+// Use of this source code is governed by a BSD-style
|
||||||
|
+// license that can be found in the LICENSE file.
|
||||||
|
+
|
||||||
|
+package socktest_test
|
||||||
|
+
|
||||||
|
+import "syscall"
|
||||||
|
+
|
||||||
|
+var (
|
||||||
|
+ socketFunc func(int, int, int) (syscall.Handle, error)
|
||||||
|
+ closeFunc func(syscall.Handle) error
|
||||||
|
+)
|
||||||
|
+
|
||||||
|
+func installTestHooks() {
|
||||||
|
+ socketFunc = sw.Socket
|
||||||
|
+ closeFunc = sw.Closesocket
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+func uninstallTestHooks() {
|
||||||
|
+ socketFunc = syscall.Socket
|
||||||
|
+ closeFunc = syscall.Closesocket
|
||||||
|
+}
|
||||||
|
Index: src/net/internal/socktest/sys_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/internal/socktest/sys_windows.go b/src/net/internal/socktest/sys_windows.go
|
||||||
|
--- a/src/net/internal/socktest/sys_windows.go (revision 2a406dc9f1ea7323d6ca9fccb2fe9ddebb6b1cc8)
|
||||||
|
+++ b/src/net/internal/socktest/sys_windows.go (revision 7b1fd7d39c6be0185fbe1d929578ab372ac5c632)
|
||||||
|
@@ -9,6 +9,38 @@
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
+// Socket wraps [syscall.Socket].
|
||||||
|
+func (sw *Switch) Socket(family, sotype, proto int) (s syscall.Handle, err error) {
|
||||||
|
+ sw.once.Do(sw.init)
|
||||||
|
+
|
||||||
|
+ so := &Status{Cookie: cookie(family, sotype, proto)}
|
||||||
|
+ sw.fmu.RLock()
|
||||||
|
+ f, _ := sw.fltab[FilterSocket]
|
||||||
|
+ sw.fmu.RUnlock()
|
||||||
|
+
|
||||||
|
+ af, err := f.apply(so)
|
||||||
|
+ if err != nil {
|
||||||
|
+ return syscall.InvalidHandle, err
|
||||||
|
+ }
|
||||||
|
+ s, so.Err = syscall.Socket(family, sotype, proto)
|
||||||
|
+ if err = af.apply(so); err != nil {
|
||||||
|
+ if so.Err == nil {
|
||||||
|
+ syscall.Closesocket(s)
|
||||||
|
+ }
|
||||||
|
+ return syscall.InvalidHandle, err
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
+ sw.smu.Lock()
|
||||||
|
+ defer sw.smu.Unlock()
|
||||||
|
+ if so.Err != nil {
|
||||||
|
+ sw.stats.getLocked(so.Cookie).OpenFailed++
|
||||||
|
+ return syscall.InvalidHandle, so.Err
|
||||||
|
+ }
|
||||||
|
+ nso := sw.addLocked(s, family, sotype, proto)
|
||||||
|
+ sw.stats.getLocked(nso.Cookie).Opened++
|
||||||
|
+ return s, nil
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
// WSASocket wraps [syscall.WSASocket].
|
||||||
|
func (sw *Switch) WSASocket(family, sotype, proto int32, protinfo *syscall.WSAProtocolInfo, group uint32, flags uint32) (s syscall.Handle, err error) {
|
||||||
|
sw.once.Do(sw.init)
|
||||||
|
Index: src/net/main_windows_test.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/main_windows_test.go b/src/net/main_windows_test.go
|
||||||
|
--- a/src/net/main_windows_test.go (revision 2a406dc9f1ea7323d6ca9fccb2fe9ddebb6b1cc8)
|
||||||
|
+++ b/src/net/main_windows_test.go (revision 7b1fd7d39c6be0185fbe1d929578ab372ac5c632)
|
||||||
|
@@ -8,6 +8,7 @@
|
||||||
|
|
||||||
|
var (
|
||||||
|
// Placeholders for saving original socket system calls.
|
||||||
|
+ origSocket = socketFunc
|
||||||
|
origWSASocket = wsaSocketFunc
|
||||||
|
origClosesocket = poll.CloseFunc
|
||||||
|
origConnect = connectFunc
|
||||||
|
@@ -17,6 +18,7 @@
|
||||||
|
)
|
||||||
|
|
||||||
|
func installTestHooks() {
|
||||||
|
+ socketFunc = sw.Socket
|
||||||
|
wsaSocketFunc = sw.WSASocket
|
||||||
|
poll.CloseFunc = sw.Closesocket
|
||||||
|
connectFunc = sw.Connect
|
||||||
|
@@ -26,6 +28,7 @@
|
||||||
|
}
|
||||||
|
|
||||||
|
func uninstallTestHooks() {
|
||||||
|
+ socketFunc = origSocket
|
||||||
|
wsaSocketFunc = origWSASocket
|
||||||
|
poll.CloseFunc = origClosesocket
|
||||||
|
connectFunc = origConnect
|
||||||
|
Index: src/net/sock_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/sock_windows.go b/src/net/sock_windows.go
|
||||||
|
--- a/src/net/sock_windows.go (revision 2a406dc9f1ea7323d6ca9fccb2fe9ddebb6b1cc8)
|
||||||
|
+++ b/src/net/sock_windows.go (revision 7b1fd7d39c6be0185fbe1d929578ab372ac5c632)
|
||||||
|
@@ -20,6 +20,21 @@
|
||||||
|
func sysSocket(family, sotype, proto int) (syscall.Handle, error) {
|
||||||
|
s, err := wsaSocketFunc(int32(family), int32(sotype), int32(proto),
|
||||||
|
nil, 0, windows.WSA_FLAG_OVERLAPPED|windows.WSA_FLAG_NO_HANDLE_INHERIT)
|
||||||
|
+ if err == nil {
|
||||||
|
+ return s, nil
|
||||||
|
+ }
|
||||||
|
+ // WSA_FLAG_NO_HANDLE_INHERIT flag is not supported on some
|
||||||
|
+ // old versions of Windows, see
|
||||||
|
+ // https://msdn.microsoft.com/en-us/library/windows/desktop/ms742212(v=vs.85).aspx
|
||||||
|
+ // for details. Just use syscall.Socket, if windows.WSASocket failed.
|
||||||
|
+
|
||||||
|
+ // See ../syscall/exec_unix.go for description of ForkLock.
|
||||||
|
+ syscall.ForkLock.RLock()
|
||||||
|
+ s, err = socketFunc(family, sotype, proto)
|
||||||
|
+ if err == nil {
|
||||||
|
+ syscall.CloseOnExec(s)
|
||||||
|
+ }
|
||||||
|
+ syscall.ForkLock.RUnlock()
|
||||||
|
if err != nil {
|
||||||
|
return syscall.InvalidHandle, os.NewSyscallError("socket", err)
|
||||||
|
}
|
||||||
|
Index: src/syscall/exec_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/syscall/exec_windows.go b/src/syscall/exec_windows.go
|
||||||
|
--- a/src/syscall/exec_windows.go (revision 2a406dc9f1ea7323d6ca9fccb2fe9ddebb6b1cc8)
|
||||||
|
+++ b/src/syscall/exec_windows.go (revision 979d6d8bab3823ff572ace26767fd2ce3cf351ae)
|
||||||
|
@@ -14,7 +14,6 @@
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
-// ForkLock is not used on Windows.
|
||||||
|
var ForkLock sync.RWMutex
|
||||||
|
|
||||||
|
// EscapeArg rewrites command line argument s as prescribed
|
||||||
|
@@ -254,6 +253,9 @@
|
||||||
|
var zeroProcAttr ProcAttr
|
||||||
|
var zeroSysProcAttr SysProcAttr
|
||||||
|
|
||||||
|
+//go:linkname rtlGetNtVersionNumbers
|
||||||
|
+func rtlGetNtVersionNumbers(majorVersion *uint32, minorVersion *uint32, buildNumber *uint32)
|
||||||
|
+
|
||||||
|
func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle uintptr, err error) {
|
||||||
|
if len(argv0) == 0 {
|
||||||
|
return 0, 0, EWINDOWS
|
||||||
|
@@ -317,6 +319,17 @@
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
+ var maj, min, build uint32
|
||||||
|
+ rtlGetNtVersionNumbers(&maj, &min, &build)
|
||||||
|
+ isWin7 := maj < 6 || (maj == 6 && min <= 1)
|
||||||
|
+ // NT kernel handles are divisible by 4, with the bottom 3 bits left as
|
||||||
|
+ // a tag. The fully set tag correlates with the types of handles we're
|
||||||
|
+ // concerned about here. Except, the kernel will interpret some
|
||||||
|
+ // special handle values, like -1, -2, and so forth, so kernelbase.dll
|
||||||
|
+ // checks to see that those bottom three bits are checked, but that top
|
||||||
|
+ // bit is not checked.
|
||||||
|
+ isLegacyWin7ConsoleHandle := func(handle Handle) bool { return isWin7 && handle&0x10000003 == 3 }
|
||||||
|
+
|
||||||
|
p, _ := GetCurrentProcess()
|
||||||
|
parentProcess := p
|
||||||
|
if sys.ParentProcess != 0 {
|
||||||
|
@@ -325,7 +338,15 @@
|
||||||
|
fd := make([]Handle, len(attr.Files))
|
||||||
|
for i := range attr.Files {
|
||||||
|
if attr.Files[i] > 0 {
|
||||||
|
- err := DuplicateHandle(p, Handle(attr.Files[i]), parentProcess, &fd[i], 0, true, DUPLICATE_SAME_ACCESS)
|
||||||
|
+ destinationProcessHandle := parentProcess
|
||||||
|
+
|
||||||
|
+ // On Windows 7, console handles aren't real handles, and can only be duplicated
|
||||||
|
+ // into the current process, not a parent one, which amounts to the same thing.
|
||||||
|
+ if parentProcess != p && isLegacyWin7ConsoleHandle(Handle(attr.Files[i])) {
|
||||||
|
+ destinationProcessHandle = p
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
+ err := DuplicateHandle(p, Handle(attr.Files[i]), destinationProcessHandle, &fd[i], 0, true, DUPLICATE_SAME_ACCESS)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
@@ -356,6 +377,14 @@
|
||||||
|
|
||||||
|
fd = append(fd, sys.AdditionalInheritedHandles...)
|
||||||
|
|
||||||
|
+ // On Windows 7, console handles aren't real handles, so don't pass them
|
||||||
|
+ // through to PROC_THREAD_ATTRIBUTE_HANDLE_LIST.
|
||||||
|
+ for i := range fd {
|
||||||
|
+ if isLegacyWin7ConsoleHandle(fd[i]) {
|
||||||
|
+ fd[i] = 0
|
||||||
|
+ }
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
// The presence of a NULL handle in the list is enough to cause PROC_THREAD_ATTRIBUTE_HANDLE_LIST
|
||||||
|
// to treat the entire list as empty, so remove NULL handles.
|
||||||
|
j := 0
|
||||||
|
Index: src/runtime/syscall_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/runtime/syscall_windows.go b/src/runtime/syscall_windows.go
|
||||||
|
--- a/src/runtime/syscall_windows.go (revision 979d6d8bab3823ff572ace26767fd2ce3cf351ae)
|
||||||
|
+++ b/src/runtime/syscall_windows.go (revision ac3e93c061779dfefc0dd13a5b6e6f764a25621e)
|
||||||
|
@@ -413,10 +413,20 @@
|
||||||
|
|
||||||
|
const _LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800
|
||||||
|
|
||||||
|
+// When available, this function will use LoadLibraryEx with the filename
|
||||||
|
+// parameter and the important SEARCH_SYSTEM32 argument. But on systems that
|
||||||
|
+// do not have that option, absoluteFilepath should contain a fallback
|
||||||
|
+// to the full path inside of system32 for use with vanilla LoadLibrary.
|
||||||
|
+//
|
||||||
|
//go:linkname syscall_loadsystemlibrary syscall.loadsystemlibrary
|
||||||
|
-func syscall_loadsystemlibrary(filename *uint16) (handle, err uintptr) {
|
||||||
|
- handle, _, err = syscall_SyscallN(uintptr(unsafe.Pointer(_LoadLibraryExW)), uintptr(unsafe.Pointer(filename)), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32)
|
||||||
|
+func syscall_loadsystemlibrary(filename *uint16, absoluteFilepath *uint16) (handle, err uintptr) {
|
||||||
|
+ if useLoadLibraryEx {
|
||||||
|
+ handle, _, err = syscall_SyscallN(uintptr(unsafe.Pointer(_LoadLibraryExW)), uintptr(unsafe.Pointer(filename)), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32)
|
||||||
|
+ } else {
|
||||||
|
+ handle, _, err = syscall_SyscallN(uintptr(unsafe.Pointer(_LoadLibraryW)), uintptr(unsafe.Pointer(absoluteFilepath)))
|
||||||
|
+ }
|
||||||
|
KeepAlive(filename)
|
||||||
|
+ KeepAlive(absoluteFilepath)
|
||||||
|
if handle != 0 {
|
||||||
|
err = 0
|
||||||
|
}
|
||||||
|
Index: src/syscall/dll_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/syscall/dll_windows.go b/src/syscall/dll_windows.go
|
||||||
|
--- a/src/syscall/dll_windows.go (revision 979d6d8bab3823ff572ace26767fd2ce3cf351ae)
|
||||||
|
+++ b/src/syscall/dll_windows.go (revision ac3e93c061779dfefc0dd13a5b6e6f764a25621e)
|
||||||
|
@@ -45,7 +45,7 @@
|
||||||
|
//go:noescape
|
||||||
|
func SyscallN(trap uintptr, args ...uintptr) (r1, r2 uintptr, err Errno)
|
||||||
|
func loadlibrary(filename *uint16) (handle uintptr, err Errno)
|
||||||
|
-func loadsystemlibrary(filename *uint16) (handle uintptr, err Errno)
|
||||||
|
+func loadsystemlibrary(filename *uint16, absoluteFilepath *uint16) (handle uintptr, err Errno)
|
||||||
|
func getprocaddress(handle uintptr, procname *uint8) (proc uintptr, err Errno)
|
||||||
|
|
||||||
|
// A DLL implements access to a single DLL.
|
||||||
|
@@ -54,6 +54,9 @@
|
||||||
|
Handle Handle
|
||||||
|
}
|
||||||
|
|
||||||
|
+//go:linkname getSystemDirectory
|
||||||
|
+func getSystemDirectory() string // Implemented in runtime package.
|
||||||
|
+
|
||||||
|
// LoadDLL loads the named DLL file into memory.
|
||||||
|
//
|
||||||
|
// If name is not an absolute path and is not a known system DLL used by
|
||||||
|
@@ -70,7 +73,11 @@
|
||||||
|
var h uintptr
|
||||||
|
var e Errno
|
||||||
|
if sysdll.IsSystemDLL[name] {
|
||||||
|
- h, e = loadsystemlibrary(namep)
|
||||||
|
+ absoluteFilepathp, err := UTF16PtrFromString(getSystemDirectory() + name)
|
||||||
|
+ if err != nil {
|
||||||
|
+ return nil, err
|
||||||
|
+ }
|
||||||
|
+ h, e = loadsystemlibrary(namep, absoluteFilepathp)
|
||||||
|
} else {
|
||||||
|
h, e = loadlibrary(namep)
|
||||||
|
}
|
||||||
884
.github/patch/go1.25.patch
vendored
Normal file
884
.github/patch/go1.25.patch
vendored
Normal file
@@ -0,0 +1,884 @@
|
|||||||
|
Subject: [PATCH] Revert "os: remove 5ms sleep on Windows in (*Process).Wait"
|
||||||
|
Fix os.RemoveAll not working on Windows7
|
||||||
|
Revert "runtime: always use LoadLibraryEx to load system libraries"
|
||||||
|
Revert "syscall: remove Windows 7 console handle workaround"
|
||||||
|
Revert "net: remove sysSocket fallback for Windows 7"
|
||||||
|
Revert "crypto/rand,runtime: switch RtlGenRandom for ProcessPrng"
|
||||||
|
---
|
||||||
|
Index: src/crypto/internal/sysrand/rand_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/crypto/internal/sysrand/rand_windows.go b/src/crypto/internal/sysrand/rand_windows.go
|
||||||
|
--- a/src/crypto/internal/sysrand/rand_windows.go (revision 6e676ab2b809d46623acb5988248d95d1eb7939c)
|
||||||
|
+++ b/src/crypto/internal/sysrand/rand_windows.go (revision 8cb5472d94c34b88733a81091bd328e70ee565a4)
|
||||||
|
@@ -7,5 +7,26 @@
|
||||||
|
import "internal/syscall/windows"
|
||||||
|
|
||||||
|
func read(b []byte) error {
|
||||||
|
- return windows.ProcessPrng(b)
|
||||||
|
+ // RtlGenRandom only returns 1<<32-1 bytes at a time. We only read at
|
||||||
|
+ // most 1<<31-1 bytes at a time so that this works the same on 32-bit
|
||||||
|
+ // and 64-bit systems.
|
||||||
|
+ return batched(windows.RtlGenRandom, 1<<31-1)(b)
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+// batched returns a function that calls f to populate a []byte by chunking it
|
||||||
|
+// into subslices of, at most, readMax bytes.
|
||||||
|
+func batched(f func([]byte) error, readMax int) func([]byte) error {
|
||||||
|
+ return func(out []byte) error {
|
||||||
|
+ for len(out) > 0 {
|
||||||
|
+ read := len(out)
|
||||||
|
+ if read > readMax {
|
||||||
|
+ read = readMax
|
||||||
|
+ }
|
||||||
|
+ if err := f(out[:read]); err != nil {
|
||||||
|
+ return err
|
||||||
|
+ }
|
||||||
|
+ out = out[read:]
|
||||||
|
+ }
|
||||||
|
+ return nil
|
||||||
|
+ }
|
||||||
|
}
|
||||||
|
Index: src/crypto/rand/rand.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/crypto/rand/rand.go b/src/crypto/rand/rand.go
|
||||||
|
--- a/src/crypto/rand/rand.go (revision 6e676ab2b809d46623acb5988248d95d1eb7939c)
|
||||||
|
+++ b/src/crypto/rand/rand.go (revision 8cb5472d94c34b88733a81091bd328e70ee565a4)
|
||||||
|
@@ -22,7 +22,7 @@
|
||||||
|
// - On legacy Linux (< 3.17), Reader opens /dev/urandom on first use.
|
||||||
|
// - On macOS, iOS, and OpenBSD Reader, uses arc4random_buf(3).
|
||||||
|
// - On NetBSD, Reader uses the kern.arandom sysctl.
|
||||||
|
-// - On Windows, Reader uses the ProcessPrng API.
|
||||||
|
+// - On Windows systems, Reader uses the RtlGenRandom API.
|
||||||
|
// - On js/wasm, Reader uses the Web Crypto API.
|
||||||
|
// - On wasip1/wasm, Reader uses random_get.
|
||||||
|
//
|
||||||
|
Index: src/internal/syscall/windows/syscall_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/internal/syscall/windows/syscall_windows.go b/src/internal/syscall/windows/syscall_windows.go
|
||||||
|
--- a/src/internal/syscall/windows/syscall_windows.go (revision 6e676ab2b809d46623acb5988248d95d1eb7939c)
|
||||||
|
+++ b/src/internal/syscall/windows/syscall_windows.go (revision 8cb5472d94c34b88733a81091bd328e70ee565a4)
|
||||||
|
@@ -419,7 +419,7 @@
|
||||||
|
//sys DestroyEnvironmentBlock(block *uint16) (err error) = userenv.DestroyEnvironmentBlock
|
||||||
|
//sys CreateEvent(eventAttrs *SecurityAttributes, manualReset uint32, initialState uint32, name *uint16) (handle syscall.Handle, err error) = kernel32.CreateEventW
|
||||||
|
|
||||||
|
-//sys ProcessPrng(buf []byte) (err error) = bcryptprimitives.ProcessPrng
|
||||||
|
+//sys RtlGenRandom(buf []byte) (err error) = advapi32.SystemFunction036
|
||||||
|
|
||||||
|
type FILE_ID_BOTH_DIR_INFO struct {
|
||||||
|
NextEntryOffset uint32
|
||||||
|
Index: src/internal/syscall/windows/zsyscall_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/internal/syscall/windows/zsyscall_windows.go b/src/internal/syscall/windows/zsyscall_windows.go
|
||||||
|
--- a/src/internal/syscall/windows/zsyscall_windows.go (revision 6e676ab2b809d46623acb5988248d95d1eb7939c)
|
||||||
|
+++ b/src/internal/syscall/windows/zsyscall_windows.go (revision 8cb5472d94c34b88733a81091bd328e70ee565a4)
|
||||||
|
@@ -38,7 +38,6 @@
|
||||||
|
|
||||||
|
var (
|
||||||
|
modadvapi32 = syscall.NewLazyDLL(sysdll.Add("advapi32.dll"))
|
||||||
|
- modbcryptprimitives = syscall.NewLazyDLL(sysdll.Add("bcryptprimitives.dll"))
|
||||||
|
modiphlpapi = syscall.NewLazyDLL(sysdll.Add("iphlpapi.dll"))
|
||||||
|
modkernel32 = syscall.NewLazyDLL(sysdll.Add("kernel32.dll"))
|
||||||
|
modnetapi32 = syscall.NewLazyDLL(sysdll.Add("netapi32.dll"))
|
||||||
|
@@ -63,7 +62,7 @@
|
||||||
|
procQueryServiceStatus = modadvapi32.NewProc("QueryServiceStatus")
|
||||||
|
procRevertToSelf = modadvapi32.NewProc("RevertToSelf")
|
||||||
|
procSetTokenInformation = modadvapi32.NewProc("SetTokenInformation")
|
||||||
|
- procProcessPrng = modbcryptprimitives.NewProc("ProcessPrng")
|
||||||
|
+ procSystemFunction036 = modadvapi32.NewProc("SystemFunction036")
|
||||||
|
procGetAdaptersAddresses = modiphlpapi.NewProc("GetAdaptersAddresses")
|
||||||
|
procCreateEventW = modkernel32.NewProc("CreateEventW")
|
||||||
|
procCreateIoCompletionPort = modkernel32.NewProc("CreateIoCompletionPort")
|
||||||
|
@@ -242,12 +241,12 @@
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
-func ProcessPrng(buf []byte) (err error) {
|
||||||
|
+func RtlGenRandom(buf []byte) (err error) {
|
||||||
|
var _p0 *byte
|
||||||
|
if len(buf) > 0 {
|
||||||
|
_p0 = &buf[0]
|
||||||
|
}
|
||||||
|
- r1, _, e1 := syscall.Syscall(procProcessPrng.Addr(), 2, uintptr(unsafe.Pointer(_p0)), uintptr(len(buf)), 0)
|
||||||
|
+ r1, _, e1 := syscall.Syscall(procSystemFunction036.Addr(), 2, uintptr(unsafe.Pointer(_p0)), uintptr(len(buf)), 0)
|
||||||
|
if r1 == 0 {
|
||||||
|
err = errnoErr(e1)
|
||||||
|
}
|
||||||
|
Index: src/runtime/os_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/runtime/os_windows.go b/src/runtime/os_windows.go
|
||||||
|
--- a/src/runtime/os_windows.go (revision 6e676ab2b809d46623acb5988248d95d1eb7939c)
|
||||||
|
+++ b/src/runtime/os_windows.go (revision f56f1e23507e646c85243a71bde7b9629b2f970c)
|
||||||
|
@@ -39,8 +39,8 @@
|
||||||
|
//go:cgo_import_dynamic runtime._GetSystemInfo GetSystemInfo%1 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._GetThreadContext GetThreadContext%2 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._SetThreadContext SetThreadContext%2 "kernel32.dll"
|
||||||
|
-//go:cgo_import_dynamic runtime._LoadLibraryExW LoadLibraryExW%3 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._LoadLibraryW LoadLibraryW%1 "kernel32.dll"
|
||||||
|
+//go:cgo_import_dynamic runtime._LoadLibraryA LoadLibraryA%1 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._PostQueuedCompletionStatus PostQueuedCompletionStatus%4 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._QueryPerformanceCounter QueryPerformanceCounter%1 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._QueryPerformanceFrequency QueryPerformanceFrequency%1 "kernel32.dll"
|
||||||
|
@@ -74,7 +74,6 @@
|
||||||
|
// Following syscalls are available on every Windows PC.
|
||||||
|
// All these variables are set by the Windows executable
|
||||||
|
// loader before the Go program starts.
|
||||||
|
- _AddVectoredContinueHandler,
|
||||||
|
_AddVectoredExceptionHandler,
|
||||||
|
_CloseHandle,
|
||||||
|
_CreateEventA,
|
||||||
|
@@ -97,8 +96,8 @@
|
||||||
|
_GetSystemInfo,
|
||||||
|
_GetThreadContext,
|
||||||
|
_SetThreadContext,
|
||||||
|
- _LoadLibraryExW,
|
||||||
|
_LoadLibraryW,
|
||||||
|
+ _LoadLibraryA,
|
||||||
|
_PostQueuedCompletionStatus,
|
||||||
|
_QueryPerformanceCounter,
|
||||||
|
_QueryPerformanceFrequency,
|
||||||
|
@@ -127,8 +126,23 @@
|
||||||
|
_WriteFile,
|
||||||
|
_ stdFunction
|
||||||
|
|
||||||
|
- // Use ProcessPrng to generate cryptographically random data.
|
||||||
|
- _ProcessPrng stdFunction
|
||||||
|
+ // Following syscalls are only available on some Windows PCs.
|
||||||
|
+ // We will load syscalls, if available, before using them.
|
||||||
|
+ _AddDllDirectory,
|
||||||
|
+ _AddVectoredContinueHandler,
|
||||||
|
+ _LoadLibraryExA,
|
||||||
|
+ _LoadLibraryExW,
|
||||||
|
+ _ stdFunction
|
||||||
|
+
|
||||||
|
+ // Use RtlGenRandom to generate cryptographically random data.
|
||||||
|
+ // This approach has been recommended by Microsoft (see issue
|
||||||
|
+ // 15589 for details).
|
||||||
|
+ // The RtlGenRandom is not listed in advapi32.dll, instead
|
||||||
|
+ // RtlGenRandom function can be found by searching for SystemFunction036.
|
||||||
|
+ // Also some versions of Mingw cannot link to SystemFunction036
|
||||||
|
+ // when building executable as Cgo. So load SystemFunction036
|
||||||
|
+ // manually during runtime startup.
|
||||||
|
+ _RtlGenRandom stdFunction
|
||||||
|
|
||||||
|
// Load ntdll.dll manually during startup, otherwise Mingw
|
||||||
|
// links wrong printf function to cgo executable (see issue
|
||||||
|
@@ -145,13 +159,6 @@
|
||||||
|
_ stdFunction
|
||||||
|
)
|
||||||
|
|
||||||
|
-var (
|
||||||
|
- bcryptprimitivesdll = [...]uint16{'b', 'c', 'r', 'y', 'p', 't', 'p', 'r', 'i', 'm', 'i', 't', 'i', 'v', 'e', 's', '.', 'd', 'l', 'l', 0}
|
||||||
|
- ntdlldll = [...]uint16{'n', 't', 'd', 'l', 'l', '.', 'd', 'l', 'l', 0}
|
||||||
|
- powrprofdll = [...]uint16{'p', 'o', 'w', 'r', 'p', 'r', 'o', 'f', '.', 'd', 'l', 'l', 0}
|
||||||
|
- winmmdll = [...]uint16{'w', 'i', 'n', 'm', 'm', '.', 'd', 'l', 'l', 0}
|
||||||
|
-)
|
||||||
|
-
|
||||||
|
// Function to be called by windows CreateThread
|
||||||
|
// to start new os thread.
|
||||||
|
func tstart_stdcall(newm *m)
|
||||||
|
@@ -244,8 +251,18 @@
|
||||||
|
return unsafe.String(&sysDirectory[0], sysDirectoryLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
-func windowsLoadSystemLib(name []uint16) uintptr {
|
||||||
|
- return stdcall3(_LoadLibraryExW, uintptr(unsafe.Pointer(&name[0])), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32)
|
||||||
|
+//go:linkname syscall_getSystemDirectory syscall.getSystemDirectory
|
||||||
|
+func syscall_getSystemDirectory() string {
|
||||||
|
+ return unsafe.String(&sysDirectory[0], sysDirectoryLen)
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+func windowsLoadSystemLib(name []byte) uintptr {
|
||||||
|
+ if useLoadLibraryEx {
|
||||||
|
+ return stdcall3(_LoadLibraryExA, uintptr(unsafe.Pointer(&name[0])), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32)
|
||||||
|
+ } else {
|
||||||
|
+ absName := append(sysDirectory[:sysDirectoryLen], name...)
|
||||||
|
+ return stdcall1(_LoadLibraryA, uintptr(unsafe.Pointer(&absName[0])))
|
||||||
|
+ }
|
||||||
|
}
|
||||||
|
|
||||||
|
//go:linkname windows_QueryPerformanceCounter internal/syscall/windows.QueryPerformanceCounter
|
||||||
|
@@ -263,13 +280,28 @@
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadOptionalSyscalls() {
|
||||||
|
- bcryptPrimitives := windowsLoadSystemLib(bcryptprimitivesdll[:])
|
||||||
|
- if bcryptPrimitives == 0 {
|
||||||
|
- throw("bcryptprimitives.dll not found")
|
||||||
|
+ var kernel32dll = []byte("kernel32.dll\000")
|
||||||
|
+ k32 := stdcall1(_LoadLibraryA, uintptr(unsafe.Pointer(&kernel32dll[0])))
|
||||||
|
+ if k32 == 0 {
|
||||||
|
+ throw("kernel32.dll not found")
|
||||||
|
}
|
||||||
|
- _ProcessPrng = windowsFindfunc(bcryptPrimitives, []byte("ProcessPrng\000"))
|
||||||
|
+ _AddDllDirectory = windowsFindfunc(k32, []byte("AddDllDirectory\000"))
|
||||||
|
+ _AddVectoredContinueHandler = windowsFindfunc(k32, []byte("AddVectoredContinueHandler\000"))
|
||||||
|
+ _LoadLibraryExA = windowsFindfunc(k32, []byte("LoadLibraryExA\000"))
|
||||||
|
+ _LoadLibraryExW = windowsFindfunc(k32, []byte("LoadLibraryExW\000"))
|
||||||
|
+ useLoadLibraryEx = (_LoadLibraryExW != nil && _LoadLibraryExA != nil && _AddDllDirectory != nil)
|
||||||
|
+
|
||||||
|
+ initSysDirectory()
|
||||||
|
|
||||||
|
- n32 := windowsLoadSystemLib(ntdlldll[:])
|
||||||
|
+ var advapi32dll = []byte("advapi32.dll\000")
|
||||||
|
+ a32 := windowsLoadSystemLib(advapi32dll)
|
||||||
|
+ if a32 == 0 {
|
||||||
|
+ throw("advapi32.dll not found")
|
||||||
|
+ }
|
||||||
|
+ _RtlGenRandom = windowsFindfunc(a32, []byte("SystemFunction036\000"))
|
||||||
|
+
|
||||||
|
+ var ntdll = []byte("ntdll.dll\000")
|
||||||
|
+ n32 := windowsLoadSystemLib(ntdll)
|
||||||
|
if n32 == 0 {
|
||||||
|
throw("ntdll.dll not found")
|
||||||
|
}
|
||||||
|
@@ -298,7 +330,7 @@
|
||||||
|
context uintptr
|
||||||
|
}
|
||||||
|
|
||||||
|
- powrprof := windowsLoadSystemLib(powrprofdll[:])
|
||||||
|
+ powrprof := windowsLoadSystemLib([]byte("powrprof.dll\000"))
|
||||||
|
if powrprof == 0 {
|
||||||
|
return // Running on Windows 7, where we don't need it anyway.
|
||||||
|
}
|
||||||
|
@@ -357,6 +389,22 @@
|
||||||
|
// in sys_windows_386.s and sys_windows_amd64.s:
|
||||||
|
func getlasterror() uint32
|
||||||
|
|
||||||
|
+// When loading DLLs, we prefer to use LoadLibraryEx with
|
||||||
|
+// LOAD_LIBRARY_SEARCH_* flags, if available. LoadLibraryEx is not
|
||||||
|
+// available on old Windows, though, and the LOAD_LIBRARY_SEARCH_*
|
||||||
|
+// flags are not available on some versions of Windows without a
|
||||||
|
+// security patch.
|
||||||
|
+//
|
||||||
|
+// https://msdn.microsoft.com/en-us/library/ms684179(v=vs.85).aspx says:
|
||||||
|
+// "Windows 7, Windows Server 2008 R2, Windows Vista, and Windows
|
||||||
|
+// Server 2008: The LOAD_LIBRARY_SEARCH_* flags are available on
|
||||||
|
+// systems that have KB2533623 installed. To determine whether the
|
||||||
|
+// flags are available, use GetProcAddress to get the address of the
|
||||||
|
+// AddDllDirectory, RemoveDllDirectory, or SetDefaultDllDirectories
|
||||||
|
+// function. If GetProcAddress succeeds, the LOAD_LIBRARY_SEARCH_*
|
||||||
|
+// flags can be used with LoadLibraryEx."
|
||||||
|
+var useLoadLibraryEx bool
|
||||||
|
+
|
||||||
|
var timeBeginPeriodRetValue uint32
|
||||||
|
|
||||||
|
// osRelaxMinNS indicates that sysmon shouldn't osRelax if the next
|
||||||
|
@@ -430,7 +478,8 @@
|
||||||
|
// Only load winmm.dll if we need it.
|
||||||
|
// This avoids a dependency on winmm.dll for Go programs
|
||||||
|
// that run on new Windows versions.
|
||||||
|
- m32 := windowsLoadSystemLib(winmmdll[:])
|
||||||
|
+ var winmmdll = []byte("winmm.dll\000")
|
||||||
|
+ m32 := windowsLoadSystemLib(winmmdll)
|
||||||
|
if m32 == 0 {
|
||||||
|
print("runtime: LoadLibraryExW failed; errno=", getlasterror(), "\n")
|
||||||
|
throw("winmm.dll not found")
|
||||||
|
@@ -471,6 +520,28 @@
|
||||||
|
canUseLongPaths = true
|
||||||
|
}
|
||||||
|
|
||||||
|
+var osVersionInfo struct {
|
||||||
|
+ majorVersion uint32
|
||||||
|
+ minorVersion uint32
|
||||||
|
+ buildNumber uint32
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+func initOsVersionInfo() {
|
||||||
|
+ info := _OSVERSIONINFOW{}
|
||||||
|
+ info.osVersionInfoSize = uint32(unsafe.Sizeof(info))
|
||||||
|
+ stdcall1(_RtlGetVersion, uintptr(unsafe.Pointer(&info)))
|
||||||
|
+ osVersionInfo.majorVersion = info.majorVersion
|
||||||
|
+ osVersionInfo.minorVersion = info.minorVersion
|
||||||
|
+ osVersionInfo.buildNumber = info.buildNumber
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+//go:linkname rtlGetNtVersionNumbers syscall.rtlGetNtVersionNumbers
|
||||||
|
+func rtlGetNtVersionNumbers(majorVersion *uint32, minorVersion *uint32, buildNumber *uint32) {
|
||||||
|
+ *majorVersion = osVersionInfo.majorVersion
|
||||||
|
+ *minorVersion = osVersionInfo.minorVersion
|
||||||
|
+ *buildNumber = osVersionInfo.buildNumber
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
func osinit() {
|
||||||
|
asmstdcallAddr = unsafe.Pointer(abi.FuncPCABI0(asmstdcall))
|
||||||
|
|
||||||
|
@@ -483,8 +554,8 @@
|
||||||
|
initHighResTimer()
|
||||||
|
timeBeginPeriodRetValue = osRelax(false)
|
||||||
|
|
||||||
|
- initSysDirectory()
|
||||||
|
initLongPathSupport()
|
||||||
|
+ initOsVersionInfo()
|
||||||
|
|
||||||
|
numCPUStartup = getCPUCount()
|
||||||
|
|
||||||
|
@@ -500,7 +571,7 @@
|
||||||
|
//go:nosplit
|
||||||
|
func readRandom(r []byte) int {
|
||||||
|
n := 0
|
||||||
|
- if stdcall2(_ProcessPrng, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 {
|
||||||
|
+ if stdcall2(_RtlGenRandom, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 {
|
||||||
|
n = len(r)
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
Index: src/net/hook_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/hook_windows.go b/src/net/hook_windows.go
|
||||||
|
--- a/src/net/hook_windows.go (revision 8cb5472d94c34b88733a81091bd328e70ee565a4)
|
||||||
|
+++ b/src/net/hook_windows.go (revision 6788c4c6f9fafb56729bad6b660f7ee2272d699f)
|
||||||
|
@@ -13,6 +13,7 @@
|
||||||
|
hostsFilePath = windows.GetSystemDirectory() + "/Drivers/etc/hosts"
|
||||||
|
|
||||||
|
// Placeholders for socket system calls.
|
||||||
|
+ socketFunc func(int, int, int) (syscall.Handle, error) = syscall.Socket
|
||||||
|
wsaSocketFunc func(int32, int32, int32, *syscall.WSAProtocolInfo, uint32, uint32) (syscall.Handle, error) = windows.WSASocket
|
||||||
|
connectFunc func(syscall.Handle, syscall.Sockaddr) error = syscall.Connect
|
||||||
|
listenFunc func(syscall.Handle, int) error = syscall.Listen
|
||||||
|
Index: src/net/internal/socktest/main_test.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/internal/socktest/main_test.go b/src/net/internal/socktest/main_test.go
|
||||||
|
--- a/src/net/internal/socktest/main_test.go (revision 8cb5472d94c34b88733a81091bd328e70ee565a4)
|
||||||
|
+++ b/src/net/internal/socktest/main_test.go (revision 6788c4c6f9fafb56729bad6b660f7ee2272d699f)
|
||||||
|
@@ -2,7 +2,7 @@
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
-//go:build !js && !plan9 && !wasip1 && !windows
|
||||||
|
+//go:build !js && !plan9 && !wasip1
|
||||||
|
|
||||||
|
package socktest_test
|
||||||
|
|
||||||
|
Index: src/net/internal/socktest/main_windows_test.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/internal/socktest/main_windows_test.go b/src/net/internal/socktest/main_windows_test.go
|
||||||
|
new file mode 100644
|
||||||
|
--- /dev/null (revision 6788c4c6f9fafb56729bad6b660f7ee2272d699f)
|
||||||
|
+++ b/src/net/internal/socktest/main_windows_test.go (revision 6788c4c6f9fafb56729bad6b660f7ee2272d699f)
|
||||||
|
@@ -0,0 +1,22 @@
|
||||||
|
+// Copyright 2015 The Go Authors. All rights reserved.
|
||||||
|
+// Use of this source code is governed by a BSD-style
|
||||||
|
+// license that can be found in the LICENSE file.
|
||||||
|
+
|
||||||
|
+package socktest_test
|
||||||
|
+
|
||||||
|
+import "syscall"
|
||||||
|
+
|
||||||
|
+var (
|
||||||
|
+ socketFunc func(int, int, int) (syscall.Handle, error)
|
||||||
|
+ closeFunc func(syscall.Handle) error
|
||||||
|
+)
|
||||||
|
+
|
||||||
|
+func installTestHooks() {
|
||||||
|
+ socketFunc = sw.Socket
|
||||||
|
+ closeFunc = sw.Closesocket
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+func uninstallTestHooks() {
|
||||||
|
+ socketFunc = syscall.Socket
|
||||||
|
+ closeFunc = syscall.Closesocket
|
||||||
|
+}
|
||||||
|
Index: src/net/internal/socktest/sys_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/internal/socktest/sys_windows.go b/src/net/internal/socktest/sys_windows.go
|
||||||
|
--- a/src/net/internal/socktest/sys_windows.go (revision 8cb5472d94c34b88733a81091bd328e70ee565a4)
|
||||||
|
+++ b/src/net/internal/socktest/sys_windows.go (revision 6788c4c6f9fafb56729bad6b660f7ee2272d699f)
|
||||||
|
@@ -9,6 +9,38 @@
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
+// Socket wraps [syscall.Socket].
|
||||||
|
+func (sw *Switch) Socket(family, sotype, proto int) (s syscall.Handle, err error) {
|
||||||
|
+ sw.once.Do(sw.init)
|
||||||
|
+
|
||||||
|
+ so := &Status{Cookie: cookie(family, sotype, proto)}
|
||||||
|
+ sw.fmu.RLock()
|
||||||
|
+ f, _ := sw.fltab[FilterSocket]
|
||||||
|
+ sw.fmu.RUnlock()
|
||||||
|
+
|
||||||
|
+ af, err := f.apply(so)
|
||||||
|
+ if err != nil {
|
||||||
|
+ return syscall.InvalidHandle, err
|
||||||
|
+ }
|
||||||
|
+ s, so.Err = syscall.Socket(family, sotype, proto)
|
||||||
|
+ if err = af.apply(so); err != nil {
|
||||||
|
+ if so.Err == nil {
|
||||||
|
+ syscall.Closesocket(s)
|
||||||
|
+ }
|
||||||
|
+ return syscall.InvalidHandle, err
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
+ sw.smu.Lock()
|
||||||
|
+ defer sw.smu.Unlock()
|
||||||
|
+ if so.Err != nil {
|
||||||
|
+ sw.stats.getLocked(so.Cookie).OpenFailed++
|
||||||
|
+ return syscall.InvalidHandle, so.Err
|
||||||
|
+ }
|
||||||
|
+ nso := sw.addLocked(s, family, sotype, proto)
|
||||||
|
+ sw.stats.getLocked(nso.Cookie).Opened++
|
||||||
|
+ return s, nil
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
// WSASocket wraps [syscall.WSASocket].
|
||||||
|
func (sw *Switch) WSASocket(family, sotype, proto int32, protinfo *syscall.WSAProtocolInfo, group uint32, flags uint32) (s syscall.Handle, err error) {
|
||||||
|
sw.once.Do(sw.init)
|
||||||
|
Index: src/net/main_windows_test.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/main_windows_test.go b/src/net/main_windows_test.go
|
||||||
|
--- a/src/net/main_windows_test.go (revision 8cb5472d94c34b88733a81091bd328e70ee565a4)
|
||||||
|
+++ b/src/net/main_windows_test.go (revision 6788c4c6f9fafb56729bad6b660f7ee2272d699f)
|
||||||
|
@@ -12,6 +12,7 @@
|
||||||
|
|
||||||
|
var (
|
||||||
|
// Placeholders for saving original socket system calls.
|
||||||
|
+ origSocket = socketFunc
|
||||||
|
origWSASocket = wsaSocketFunc
|
||||||
|
origClosesocket = poll.CloseFunc
|
||||||
|
origConnect = connectFunc
|
||||||
|
@@ -21,6 +22,7 @@
|
||||||
|
)
|
||||||
|
|
||||||
|
func installTestHooks() {
|
||||||
|
+ socketFunc = sw.Socket
|
||||||
|
wsaSocketFunc = sw.WSASocket
|
||||||
|
poll.CloseFunc = sw.Closesocket
|
||||||
|
connectFunc = sw.Connect
|
||||||
|
@@ -30,6 +32,7 @@
|
||||||
|
}
|
||||||
|
|
||||||
|
func uninstallTestHooks() {
|
||||||
|
+ socketFunc = origSocket
|
||||||
|
wsaSocketFunc = origWSASocket
|
||||||
|
poll.CloseFunc = origClosesocket
|
||||||
|
connectFunc = origConnect
|
||||||
|
Index: src/net/sock_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/sock_windows.go b/src/net/sock_windows.go
|
||||||
|
--- a/src/net/sock_windows.go (revision 8cb5472d94c34b88733a81091bd328e70ee565a4)
|
||||||
|
+++ b/src/net/sock_windows.go (revision 6788c4c6f9fafb56729bad6b660f7ee2272d699f)
|
||||||
|
@@ -20,6 +20,21 @@
|
||||||
|
func sysSocket(family, sotype, proto int) (syscall.Handle, error) {
|
||||||
|
s, err := wsaSocketFunc(int32(family), int32(sotype), int32(proto),
|
||||||
|
nil, 0, windows.WSA_FLAG_OVERLAPPED|windows.WSA_FLAG_NO_HANDLE_INHERIT)
|
||||||
|
+ if err == nil {
|
||||||
|
+ return s, nil
|
||||||
|
+ }
|
||||||
|
+ // WSA_FLAG_NO_HANDLE_INHERIT flag is not supported on some
|
||||||
|
+ // old versions of Windows, see
|
||||||
|
+ // https://msdn.microsoft.com/en-us/library/windows/desktop/ms742212(v=vs.85).aspx
|
||||||
|
+ // for details. Just use syscall.Socket, if windows.WSASocket failed.
|
||||||
|
+
|
||||||
|
+ // See ../syscall/exec_unix.go for description of ForkLock.
|
||||||
|
+ syscall.ForkLock.RLock()
|
||||||
|
+ s, err = socketFunc(family, sotype, proto)
|
||||||
|
+ if err == nil {
|
||||||
|
+ syscall.CloseOnExec(s)
|
||||||
|
+ }
|
||||||
|
+ syscall.ForkLock.RUnlock()
|
||||||
|
if err != nil {
|
||||||
|
return syscall.InvalidHandle, os.NewSyscallError("socket", err)
|
||||||
|
}
|
||||||
|
Index: src/syscall/exec_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/syscall/exec_windows.go b/src/syscall/exec_windows.go
|
||||||
|
--- a/src/syscall/exec_windows.go (revision 8cb5472d94c34b88733a81091bd328e70ee565a4)
|
||||||
|
+++ b/src/syscall/exec_windows.go (revision a5b2168bb836ed9d6601c626f95e56c07923f906)
|
||||||
|
@@ -14,7 +14,6 @@
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
-// ForkLock is not used on Windows.
|
||||||
|
var ForkLock sync.RWMutex
|
||||||
|
|
||||||
|
// EscapeArg rewrites command line argument s as prescribed
|
||||||
|
@@ -254,6 +253,9 @@
|
||||||
|
var zeroProcAttr ProcAttr
|
||||||
|
var zeroSysProcAttr SysProcAttr
|
||||||
|
|
||||||
|
+//go:linkname rtlGetNtVersionNumbers
|
||||||
|
+func rtlGetNtVersionNumbers(majorVersion *uint32, minorVersion *uint32, buildNumber *uint32)
|
||||||
|
+
|
||||||
|
func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle uintptr, err error) {
|
||||||
|
if len(argv0) == 0 {
|
||||||
|
return 0, 0, EWINDOWS
|
||||||
|
@@ -317,6 +319,17 @@
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
+ var maj, min, build uint32
|
||||||
|
+ rtlGetNtVersionNumbers(&maj, &min, &build)
|
||||||
|
+ isWin7 := maj < 6 || (maj == 6 && min <= 1)
|
||||||
|
+ // NT kernel handles are divisible by 4, with the bottom 3 bits left as
|
||||||
|
+ // a tag. The fully set tag correlates with the types of handles we're
|
||||||
|
+ // concerned about here. Except, the kernel will interpret some
|
||||||
|
+ // special handle values, like -1, -2, and so forth, so kernelbase.dll
|
||||||
|
+ // checks to see that those bottom three bits are checked, but that top
|
||||||
|
+ // bit is not checked.
|
||||||
|
+ isLegacyWin7ConsoleHandle := func(handle Handle) bool { return isWin7 && handle&0x10000003 == 3 }
|
||||||
|
+
|
||||||
|
p, _ := GetCurrentProcess()
|
||||||
|
parentProcess := p
|
||||||
|
if sys.ParentProcess != 0 {
|
||||||
|
@@ -325,7 +338,15 @@
|
||||||
|
fd := make([]Handle, len(attr.Files))
|
||||||
|
for i := range attr.Files {
|
||||||
|
if attr.Files[i] > 0 {
|
||||||
|
- err := DuplicateHandle(p, Handle(attr.Files[i]), parentProcess, &fd[i], 0, true, DUPLICATE_SAME_ACCESS)
|
||||||
|
+ destinationProcessHandle := parentProcess
|
||||||
|
+
|
||||||
|
+ // On Windows 7, console handles aren't real handles, and can only be duplicated
|
||||||
|
+ // into the current process, not a parent one, which amounts to the same thing.
|
||||||
|
+ if parentProcess != p && isLegacyWin7ConsoleHandle(Handle(attr.Files[i])) {
|
||||||
|
+ destinationProcessHandle = p
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
+ err := DuplicateHandle(p, Handle(attr.Files[i]), destinationProcessHandle, &fd[i], 0, true, DUPLICATE_SAME_ACCESS)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
@@ -356,6 +377,14 @@
|
||||||
|
|
||||||
|
fd = append(fd, sys.AdditionalInheritedHandles...)
|
||||||
|
|
||||||
|
+ // On Windows 7, console handles aren't real handles, so don't pass them
|
||||||
|
+ // through to PROC_THREAD_ATTRIBUTE_HANDLE_LIST.
|
||||||
|
+ for i := range fd {
|
||||||
|
+ if isLegacyWin7ConsoleHandle(fd[i]) {
|
||||||
|
+ fd[i] = 0
|
||||||
|
+ }
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
// The presence of a NULL handle in the list is enough to cause PROC_THREAD_ATTRIBUTE_HANDLE_LIST
|
||||||
|
// to treat the entire list as empty, so remove NULL handles.
|
||||||
|
j := 0
|
||||||
|
Index: src/runtime/syscall_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/runtime/syscall_windows.go b/src/runtime/syscall_windows.go
|
||||||
|
--- a/src/runtime/syscall_windows.go (revision a5b2168bb836ed9d6601c626f95e56c07923f906)
|
||||||
|
+++ b/src/runtime/syscall_windows.go (revision f56f1e23507e646c85243a71bde7b9629b2f970c)
|
||||||
|
@@ -413,10 +413,20 @@
|
||||||
|
|
||||||
|
const _LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800
|
||||||
|
|
||||||
|
+// When available, this function will use LoadLibraryEx with the filename
|
||||||
|
+// parameter and the important SEARCH_SYSTEM32 argument. But on systems that
|
||||||
|
+// do not have that option, absoluteFilepath should contain a fallback
|
||||||
|
+// to the full path inside of system32 for use with vanilla LoadLibrary.
|
||||||
|
+//
|
||||||
|
//go:linkname syscall_loadsystemlibrary syscall.loadsystemlibrary
|
||||||
|
-func syscall_loadsystemlibrary(filename *uint16) (handle, err uintptr) {
|
||||||
|
- handle, _, err = syscall_SyscallN(uintptr(unsafe.Pointer(_LoadLibraryExW)), uintptr(unsafe.Pointer(filename)), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32)
|
||||||
|
+func syscall_loadsystemlibrary(filename *uint16, absoluteFilepath *uint16) (handle, err uintptr) {
|
||||||
|
+ if useLoadLibraryEx {
|
||||||
|
+ handle, _, err = syscall_SyscallN(uintptr(unsafe.Pointer(_LoadLibraryExW)), uintptr(unsafe.Pointer(filename)), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32)
|
||||||
|
+ } else {
|
||||||
|
+ handle, _, err = syscall_SyscallN(uintptr(unsafe.Pointer(_LoadLibraryW)), uintptr(unsafe.Pointer(absoluteFilepath)))
|
||||||
|
+ }
|
||||||
|
KeepAlive(filename)
|
||||||
|
+ KeepAlive(absoluteFilepath)
|
||||||
|
if handle != 0 {
|
||||||
|
err = 0
|
||||||
|
}
|
||||||
|
Index: src/syscall/dll_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/syscall/dll_windows.go b/src/syscall/dll_windows.go
|
||||||
|
--- a/src/syscall/dll_windows.go (revision a5b2168bb836ed9d6601c626f95e56c07923f906)
|
||||||
|
+++ b/src/syscall/dll_windows.go (revision f56f1e23507e646c85243a71bde7b9629b2f970c)
|
||||||
|
@@ -45,7 +45,7 @@
|
||||||
|
//go:noescape
|
||||||
|
func SyscallN(trap uintptr, args ...uintptr) (r1, r2 uintptr, err Errno)
|
||||||
|
func loadlibrary(filename *uint16) (handle uintptr, err Errno)
|
||||||
|
-func loadsystemlibrary(filename *uint16) (handle uintptr, err Errno)
|
||||||
|
+func loadsystemlibrary(filename *uint16, absoluteFilepath *uint16) (handle uintptr, err Errno)
|
||||||
|
func getprocaddress(handle uintptr, procname *uint8) (proc uintptr, err Errno)
|
||||||
|
|
||||||
|
// A DLL implements access to a single DLL.
|
||||||
|
@@ -54,6 +54,9 @@
|
||||||
|
Handle Handle
|
||||||
|
}
|
||||||
|
|
||||||
|
+//go:linkname getSystemDirectory
|
||||||
|
+func getSystemDirectory() string // Implemented in runtime package.
|
||||||
|
+
|
||||||
|
// LoadDLL loads the named DLL file into memory.
|
||||||
|
//
|
||||||
|
// If name is not an absolute path and is not a known system DLL used by
|
||||||
|
@@ -70,7 +73,11 @@
|
||||||
|
var h uintptr
|
||||||
|
var e Errno
|
||||||
|
if sysdll.IsSystemDLL[name] {
|
||||||
|
- h, e = loadsystemlibrary(namep)
|
||||||
|
+ absoluteFilepathp, err := UTF16PtrFromString(getSystemDirectory() + name)
|
||||||
|
+ if err != nil {
|
||||||
|
+ return nil, err
|
||||||
|
+ }
|
||||||
|
+ h, e = loadsystemlibrary(namep, absoluteFilepathp)
|
||||||
|
} else {
|
||||||
|
h, e = loadlibrary(namep)
|
||||||
|
}
|
||||||
|
Index: src/os/removeall_at.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/os/removeall_at.go b/src/os/removeall_at.go
|
||||||
|
--- a/src/os/removeall_at.go (revision f56f1e23507e646c85243a71bde7b9629b2f970c)
|
||||||
|
+++ b/src/os/removeall_at.go (revision 0a52622d2331ff975fb0442617ec19bc352bb2ed)
|
||||||
|
@@ -2,7 +2,7 @@
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
-//go:build unix || wasip1 || windows
|
||||||
|
+//go:build unix || wasip1
|
||||||
|
|
||||||
|
package os
|
||||||
|
|
||||||
|
@@ -175,3 +175,25 @@
|
||||||
|
}
|
||||||
|
return newDirFile(fd, name)
|
||||||
|
}
|
||||||
|
+
|
||||||
|
+func rootRemoveAll(r *Root, name string) error {
|
||||||
|
+ // Consistency with os.RemoveAll: Strip trailing /s from the name,
|
||||||
|
+ // so RemoveAll("not_a_directory/") succeeds.
|
||||||
|
+ for len(name) > 0 && IsPathSeparator(name[len(name)-1]) {
|
||||||
|
+ name = name[:len(name)-1]
|
||||||
|
+ }
|
||||||
|
+ if endsWithDot(name) {
|
||||||
|
+ // Consistency with os.RemoveAll: Return EINVAL when trying to remove .
|
||||||
|
+ return &PathError{Op: "RemoveAll", Path: name, Err: syscall.EINVAL}
|
||||||
|
+ }
|
||||||
|
+ _, err := doInRoot(r, name, nil, func(parent sysfdType, name string) (struct{}, error) {
|
||||||
|
+ return struct{}{}, removeAllFrom(parent, name)
|
||||||
|
+ })
|
||||||
|
+ if IsNotExist(err) {
|
||||||
|
+ return nil
|
||||||
|
+ }
|
||||||
|
+ if err != nil {
|
||||||
|
+ return &PathError{Op: "RemoveAll", Path: name, Err: underlyingError(err)}
|
||||||
|
+ }
|
||||||
|
+ return err
|
||||||
|
+}
|
||||||
|
Index: src/os/removeall_noat.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/os/removeall_noat.go b/src/os/removeall_noat.go
|
||||||
|
--- a/src/os/removeall_noat.go (revision f56f1e23507e646c85243a71bde7b9629b2f970c)
|
||||||
|
+++ b/src/os/removeall_noat.go (revision 0a52622d2331ff975fb0442617ec19bc352bb2ed)
|
||||||
|
@@ -2,7 +2,7 @@
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
-//go:build (js && wasm) || plan9
|
||||||
|
+//go:build (js && wasm) || plan9 || windows
|
||||||
|
|
||||||
|
package os
|
||||||
|
|
||||||
|
@@ -140,3 +140,22 @@
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
+
|
||||||
|
+func rootRemoveAll(r *Root, name string) error {
|
||||||
|
+ if endsWithDot(name) {
|
||||||
|
+ // Consistency with os.RemoveAll: Return EINVAL when trying to remove .
|
||||||
|
+ return &PathError{Op: "RemoveAll", Path: name, Err: syscall.EINVAL}
|
||||||
|
+ }
|
||||||
|
+ if err := checkPathEscapesLstat(r, name); err != nil {
|
||||||
|
+ if err == syscall.ENOTDIR {
|
||||||
|
+ // Some intermediate path component is not a directory.
|
||||||
|
+ // RemoveAll treats this as success (since the target doesn't exist).
|
||||||
|
+ return nil
|
||||||
|
+ }
|
||||||
|
+ return &PathError{Op: "RemoveAll", Path: name, Err: err}
|
||||||
|
+ }
|
||||||
|
+ if err := RemoveAll(joinPath(r.root.name, name)); err != nil {
|
||||||
|
+ return &PathError{Op: "RemoveAll", Path: name, Err: underlyingError(err)}
|
||||||
|
+ }
|
||||||
|
+ return nil
|
||||||
|
+}
|
||||||
|
Index: src/os/root_noopenat.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/os/root_noopenat.go b/src/os/root_noopenat.go
|
||||||
|
--- a/src/os/root_noopenat.go (revision f56f1e23507e646c85243a71bde7b9629b2f970c)
|
||||||
|
+++ b/src/os/root_noopenat.go (revision 0a52622d2331ff975fb0442617ec19bc352bb2ed)
|
||||||
|
@@ -11,7 +11,6 @@
|
||||||
|
"internal/filepathlite"
|
||||||
|
"internal/stringslite"
|
||||||
|
"sync/atomic"
|
||||||
|
- "syscall"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
@@ -185,25 +184,6 @@
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
-
|
||||||
|
-func rootRemoveAll(r *Root, name string) error {
|
||||||
|
- if endsWithDot(name) {
|
||||||
|
- // Consistency with os.RemoveAll: Return EINVAL when trying to remove .
|
||||||
|
- return &PathError{Op: "RemoveAll", Path: name, Err: syscall.EINVAL}
|
||||||
|
- }
|
||||||
|
- if err := checkPathEscapesLstat(r, name); err != nil {
|
||||||
|
- if err == syscall.ENOTDIR {
|
||||||
|
- // Some intermediate path component is not a directory.
|
||||||
|
- // RemoveAll treats this as success (since the target doesn't exist).
|
||||||
|
- return nil
|
||||||
|
- }
|
||||||
|
- return &PathError{Op: "RemoveAll", Path: name, Err: err}
|
||||||
|
- }
|
||||||
|
- if err := RemoveAll(joinPath(r.root.name, name)); err != nil {
|
||||||
|
- return &PathError{Op: "RemoveAll", Path: name, Err: underlyingError(err)}
|
||||||
|
- }
|
||||||
|
- return nil
|
||||||
|
-}
|
||||||
|
|
||||||
|
func rootReadlink(r *Root, name string) (string, error) {
|
||||||
|
if err := checkPathEscapesLstat(r, name); err != nil {
|
||||||
|
Index: src/os/root_openat.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/os/root_openat.go b/src/os/root_openat.go
|
||||||
|
--- a/src/os/root_openat.go (revision f56f1e23507e646c85243a71bde7b9629b2f970c)
|
||||||
|
+++ b/src/os/root_openat.go (revision 0a52622d2331ff975fb0442617ec19bc352bb2ed)
|
||||||
|
@@ -194,28 +194,6 @@
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
-func rootRemoveAll(r *Root, name string) error {
|
||||||
|
- // Consistency with os.RemoveAll: Strip trailing /s from the name,
|
||||||
|
- // so RemoveAll("not_a_directory/") succeeds.
|
||||||
|
- for len(name) > 0 && IsPathSeparator(name[len(name)-1]) {
|
||||||
|
- name = name[:len(name)-1]
|
||||||
|
- }
|
||||||
|
- if endsWithDot(name) {
|
||||||
|
- // Consistency with os.RemoveAll: Return EINVAL when trying to remove .
|
||||||
|
- return &PathError{Op: "RemoveAll", Path: name, Err: syscall.EINVAL}
|
||||||
|
- }
|
||||||
|
- _, err := doInRoot(r, name, nil, func(parent sysfdType, name string) (struct{}, error) {
|
||||||
|
- return struct{}{}, removeAllFrom(parent, name)
|
||||||
|
- })
|
||||||
|
- if IsNotExist(err) {
|
||||||
|
- return nil
|
||||||
|
- }
|
||||||
|
- if err != nil {
|
||||||
|
- return &PathError{Op: "RemoveAll", Path: name, Err: underlyingError(err)}
|
||||||
|
- }
|
||||||
|
- return err
|
||||||
|
-}
|
||||||
|
-
|
||||||
|
func rootRename(r *Root, oldname, newname string) error {
|
||||||
|
_, err := doInRoot(r, oldname, nil, func(oldparent sysfdType, oldname string) (struct{}, error) {
|
||||||
|
_, err := doInRoot(r, newname, nil, func(newparent sysfdType, newname string) (struct{}, error) {
|
||||||
|
Index: src/os/root_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/os/root_windows.go b/src/os/root_windows.go
|
||||||
|
--- a/src/os/root_windows.go (revision f56f1e23507e646c85243a71bde7b9629b2f970c)
|
||||||
|
+++ b/src/os/root_windows.go (revision 0a52622d2331ff975fb0442617ec19bc352bb2ed)
|
||||||
|
@@ -402,3 +402,14 @@
|
||||||
|
}
|
||||||
|
return fi.Mode(), nil
|
||||||
|
}
|
||||||
|
+
|
||||||
|
+func checkPathEscapes(r *Root, name string) error {
|
||||||
|
+ if !filepathlite.IsLocal(name) {
|
||||||
|
+ return errPathEscapes
|
||||||
|
+ }
|
||||||
|
+ return nil
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+func checkPathEscapesLstat(r *Root, name string) error {
|
||||||
|
+ return checkPathEscapes(r, name)
|
||||||
|
+}
|
||||||
|
Index: src/os/exec_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/os/exec_windows.go b/src/os/exec_windows.go
|
||||||
|
--- a/src/os/exec_windows.go (revision 0a52622d2331ff975fb0442617ec19bc352bb2ed)
|
||||||
|
+++ b/src/os/exec_windows.go (revision fb3d09a67fe97008ad76fea97ae88170072cbdbb)
|
||||||
|
@@ -10,6 +10,7 @@
|
||||||
|
"runtime"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
+ _ "unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Note that Process.handle is never nil because Windows always requires
|
||||||
|
@@ -49,9 +50,23 @@
|
||||||
|
// than statusDone.
|
||||||
|
p.doRelease(statusReleased)
|
||||||
|
|
||||||
|
+ var maj, min, build uint32
|
||||||
|
+ rtlGetNtVersionNumbers(&maj, &min, &build)
|
||||||
|
+ if maj < 10 {
|
||||||
|
+ // NOTE(brainman): It seems that sometimes process is not dead
|
||||||
|
+ // when WaitForSingleObject returns. But we do not know any
|
||||||
|
+ // other way to wait for it. Sleeping for a while seems to do
|
||||||
|
+ // the trick sometimes.
|
||||||
|
+ // See https://golang.org/issue/25965 for details.
|
||||||
|
+ time.Sleep(5 * time.Millisecond)
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
return &ProcessState{p.Pid, syscall.WaitStatus{ExitCode: ec}, &u}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
+//go:linkname rtlGetNtVersionNumbers syscall.rtlGetNtVersionNumbers
|
||||||
|
+func rtlGetNtVersionNumbers(majorVersion *uint32, minorVersion *uint32, buildNumber *uint32)
|
||||||
|
+
|
||||||
|
func (p *Process) signal(sig Signal) error {
|
||||||
|
handle, status := p.handleTransientAcquire()
|
||||||
|
switch status {
|
||||||
883
.github/patch/go1.26.patch
vendored
Normal file
883
.github/patch/go1.26.patch
vendored
Normal file
@@ -0,0 +1,883 @@
|
|||||||
|
Subject: [PATCH] Revert "os: remove 5ms sleep on Windows in (*Process).Wait"
|
||||||
|
Fix os.RemoveAll not working on Windows7
|
||||||
|
Revert "runtime: always use LoadLibraryEx to load system libraries"
|
||||||
|
Revert "syscall: remove Windows 7 console handle workaround"
|
||||||
|
Revert "net: remove sysSocket fallback for Windows 7"
|
||||||
|
Revert "crypto/rand,runtime: switch RtlGenRandom for ProcessPrng"
|
||||||
|
---
|
||||||
|
Index: src/crypto/internal/sysrand/rand_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/crypto/internal/sysrand/rand_windows.go b/src/crypto/internal/sysrand/rand_windows.go
|
||||||
|
--- a/src/crypto/internal/sysrand/rand_windows.go (revision c599a8f2385849a225d02843b3c6389dbfc5aa69)
|
||||||
|
+++ b/src/crypto/internal/sysrand/rand_windows.go (revision b0d48afabb9fd14976c27221cb525c5d2ebbfe79)
|
||||||
|
@@ -7,5 +7,26 @@
|
||||||
|
import "internal/syscall/windows"
|
||||||
|
|
||||||
|
func read(b []byte) error {
|
||||||
|
- return windows.ProcessPrng(b)
|
||||||
|
+ // RtlGenRandom only returns 1<<32-1 bytes at a time. We only read at
|
||||||
|
+ // most 1<<31-1 bytes at a time so that this works the same on 32-bit
|
||||||
|
+ // and 64-bit systems.
|
||||||
|
+ return batched(windows.RtlGenRandom, 1<<31-1)(b)
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+// batched returns a function that calls f to populate a []byte by chunking it
|
||||||
|
+// into subslices of, at most, readMax bytes.
|
||||||
|
+func batched(f func([]byte) error, readMax int) func([]byte) error {
|
||||||
|
+ return func(out []byte) error {
|
||||||
|
+ for len(out) > 0 {
|
||||||
|
+ read := len(out)
|
||||||
|
+ if read > readMax {
|
||||||
|
+ read = readMax
|
||||||
|
+ }
|
||||||
|
+ if err := f(out[:read]); err != nil {
|
||||||
|
+ return err
|
||||||
|
+ }
|
||||||
|
+ out = out[read:]
|
||||||
|
+ }
|
||||||
|
+ return nil
|
||||||
|
+ }
|
||||||
|
}
|
||||||
|
Index: src/crypto/rand/rand.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/crypto/rand/rand.go b/src/crypto/rand/rand.go
|
||||||
|
--- a/src/crypto/rand/rand.go (revision c599a8f2385849a225d02843b3c6389dbfc5aa69)
|
||||||
|
+++ b/src/crypto/rand/rand.go (revision b0d48afabb9fd14976c27221cb525c5d2ebbfe79)
|
||||||
|
@@ -25,7 +25,7 @@
|
||||||
|
// - On legacy Linux (< 3.17), Reader opens /dev/urandom on first use.
|
||||||
|
// - On macOS, iOS, and OpenBSD Reader, uses arc4random_buf(3).
|
||||||
|
// - On NetBSD, Reader uses the kern.arandom sysctl.
|
||||||
|
-// - On Windows, Reader uses the ProcessPrng API.
|
||||||
|
+// - On Windows systems, Reader uses the RtlGenRandom API.
|
||||||
|
// - On js/wasm, Reader uses the Web Crypto API.
|
||||||
|
// - On wasip1/wasm, Reader uses random_get.
|
||||||
|
//
|
||||||
|
Index: src/internal/syscall/windows/syscall_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/internal/syscall/windows/syscall_windows.go b/src/internal/syscall/windows/syscall_windows.go
|
||||||
|
--- a/src/internal/syscall/windows/syscall_windows.go (revision c599a8f2385849a225d02843b3c6389dbfc5aa69)
|
||||||
|
+++ b/src/internal/syscall/windows/syscall_windows.go (revision b0d48afabb9fd14976c27221cb525c5d2ebbfe79)
|
||||||
|
@@ -421,7 +421,7 @@
|
||||||
|
//sys DestroyEnvironmentBlock(block *uint16) (err error) = userenv.DestroyEnvironmentBlock
|
||||||
|
//sys CreateEvent(eventAttrs *SecurityAttributes, manualReset uint32, initialState uint32, name *uint16) (handle syscall.Handle, err error) = kernel32.CreateEventW
|
||||||
|
|
||||||
|
-//sys ProcessPrng(buf []byte) (err error) = bcryptprimitives.ProcessPrng
|
||||||
|
+//sys RtlGenRandom(buf []byte) (err error) = advapi32.SystemFunction036
|
||||||
|
|
||||||
|
type FILE_ID_BOTH_DIR_INFO struct {
|
||||||
|
NextEntryOffset uint32
|
||||||
|
Index: src/internal/syscall/windows/zsyscall_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/internal/syscall/windows/zsyscall_windows.go b/src/internal/syscall/windows/zsyscall_windows.go
|
||||||
|
--- a/src/internal/syscall/windows/zsyscall_windows.go (revision c599a8f2385849a225d02843b3c6389dbfc5aa69)
|
||||||
|
+++ b/src/internal/syscall/windows/zsyscall_windows.go (revision b0d48afabb9fd14976c27221cb525c5d2ebbfe79)
|
||||||
|
@@ -38,7 +38,6 @@
|
||||||
|
|
||||||
|
var (
|
||||||
|
modadvapi32 = syscall.NewLazyDLL(sysdll.Add("advapi32.dll"))
|
||||||
|
- modbcryptprimitives = syscall.NewLazyDLL(sysdll.Add("bcryptprimitives.dll"))
|
||||||
|
modiphlpapi = syscall.NewLazyDLL(sysdll.Add("iphlpapi.dll"))
|
||||||
|
modkernel32 = syscall.NewLazyDLL(sysdll.Add("kernel32.dll"))
|
||||||
|
modnetapi32 = syscall.NewLazyDLL(sysdll.Add("netapi32.dll"))
|
||||||
|
@@ -63,7 +62,7 @@
|
||||||
|
procQueryServiceStatus = modadvapi32.NewProc("QueryServiceStatus")
|
||||||
|
procRevertToSelf = modadvapi32.NewProc("RevertToSelf")
|
||||||
|
procSetTokenInformation = modadvapi32.NewProc("SetTokenInformation")
|
||||||
|
- procProcessPrng = modbcryptprimitives.NewProc("ProcessPrng")
|
||||||
|
+ procSystemFunction036 = modadvapi32.NewProc("SystemFunction036")
|
||||||
|
procGetAdaptersAddresses = modiphlpapi.NewProc("GetAdaptersAddresses")
|
||||||
|
procCreateEventW = modkernel32.NewProc("CreateEventW")
|
||||||
|
procCreateIoCompletionPort = modkernel32.NewProc("CreateIoCompletionPort")
|
||||||
|
@@ -244,12 +243,12 @@
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
-func ProcessPrng(buf []byte) (err error) {
|
||||||
|
+func RtlGenRandom(buf []byte) (err error) {
|
||||||
|
var _p0 *byte
|
||||||
|
if len(buf) > 0 {
|
||||||
|
_p0 = &buf[0]
|
||||||
|
}
|
||||||
|
- r1, _, e1 := syscall.SyscallN(procProcessPrng.Addr(), uintptr(unsafe.Pointer(_p0)), uintptr(len(buf)))
|
||||||
|
+ r1, _, e1 := syscall.SyscallN(procSystemFunction036.Addr(), uintptr(unsafe.Pointer(_p0)), uintptr(len(buf)), 0)
|
||||||
|
if r1 == 0 {
|
||||||
|
err = errnoErr(e1)
|
||||||
|
}
|
||||||
|
Index: src/runtime/os_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/runtime/os_windows.go b/src/runtime/os_windows.go
|
||||||
|
--- a/src/runtime/os_windows.go (revision c599a8f2385849a225d02843b3c6389dbfc5aa69)
|
||||||
|
+++ b/src/runtime/os_windows.go (revision ea2726a6fa25fbfa1092e696e522eafca544d24c)
|
||||||
|
@@ -40,7 +40,8 @@
|
||||||
|
//go:cgo_import_dynamic runtime._GetSystemInfo GetSystemInfo%1 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._GetThreadContext GetThreadContext%2 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._SetThreadContext SetThreadContext%2 "kernel32.dll"
|
||||||
|
-//go:cgo_import_dynamic runtime._LoadLibraryExW LoadLibraryExW%3 "kernel32.dll"
|
||||||
|
+//go:cgo_import_dynamic runtime._LoadLibraryW LoadLibraryW%1 "kernel32.dll"
|
||||||
|
+//go:cgo_import_dynamic runtime._LoadLibraryA LoadLibraryA%1 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._PostQueuedCompletionStatus PostQueuedCompletionStatus%4 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._QueryPerformanceCounter QueryPerformanceCounter%1 "kernel32.dll"
|
||||||
|
//go:cgo_import_dynamic runtime._QueryPerformanceFrequency QueryPerformanceFrequency%1 "kernel32.dll"
|
||||||
|
@@ -74,7 +75,6 @@
|
||||||
|
// Following syscalls are available on every Windows PC.
|
||||||
|
// All these variables are set by the Windows executable
|
||||||
|
// loader before the Go program starts.
|
||||||
|
- _AddVectoredContinueHandler,
|
||||||
|
_AddVectoredExceptionHandler,
|
||||||
|
_CloseHandle,
|
||||||
|
_CreateEventA,
|
||||||
|
@@ -97,7 +97,8 @@
|
||||||
|
_GetSystemInfo,
|
||||||
|
_GetThreadContext,
|
||||||
|
_SetThreadContext,
|
||||||
|
- _LoadLibraryExW,
|
||||||
|
+ _LoadLibraryW,
|
||||||
|
+ _LoadLibraryA,
|
||||||
|
_PostQueuedCompletionStatus,
|
||||||
|
_QueryPerformanceCounter,
|
||||||
|
_QueryPerformanceFrequency,
|
||||||
|
@@ -126,8 +127,23 @@
|
||||||
|
_WriteFile,
|
||||||
|
_ stdFunction
|
||||||
|
|
||||||
|
- // Use ProcessPrng to generate cryptographically random data.
|
||||||
|
- _ProcessPrng stdFunction
|
||||||
|
+ // Following syscalls are only available on some Windows PCs.
|
||||||
|
+ // We will load syscalls, if available, before using them.
|
||||||
|
+ _AddDllDirectory,
|
||||||
|
+ _AddVectoredContinueHandler,
|
||||||
|
+ _LoadLibraryExA,
|
||||||
|
+ _LoadLibraryExW,
|
||||||
|
+ _ stdFunction
|
||||||
|
+
|
||||||
|
+ // Use RtlGenRandom to generate cryptographically random data.
|
||||||
|
+ // This approach has been recommended by Microsoft (see issue
|
||||||
|
+ // 15589 for details).
|
||||||
|
+ // The RtlGenRandom is not listed in advapi32.dll, instead
|
||||||
|
+ // RtlGenRandom function can be found by searching for SystemFunction036.
|
||||||
|
+ // Also some versions of Mingw cannot link to SystemFunction036
|
||||||
|
+ // when building executable as Cgo. So load SystemFunction036
|
||||||
|
+ // manually during runtime startup.
|
||||||
|
+ _RtlGenRandom stdFunction
|
||||||
|
|
||||||
|
// Load ntdll.dll manually during startup, otherwise Mingw
|
||||||
|
// links wrong printf function to cgo executable (see issue
|
||||||
|
@@ -144,13 +160,6 @@
|
||||||
|
_ stdFunction
|
||||||
|
)
|
||||||
|
|
||||||
|
-var (
|
||||||
|
- bcryptprimitivesdll = [...]uint16{'b', 'c', 'r', 'y', 'p', 't', 'p', 'r', 'i', 'm', 'i', 't', 'i', 'v', 'e', 's', '.', 'd', 'l', 'l', 0}
|
||||||
|
- ntdlldll = [...]uint16{'n', 't', 'd', 'l', 'l', '.', 'd', 'l', 'l', 0}
|
||||||
|
- powrprofdll = [...]uint16{'p', 'o', 'w', 'r', 'p', 'r', 'o', 'f', '.', 'd', 'l', 'l', 0}
|
||||||
|
- winmmdll = [...]uint16{'w', 'i', 'n', 'm', 'm', '.', 'd', 'l', 'l', 0}
|
||||||
|
-)
|
||||||
|
-
|
||||||
|
// Function to be called by windows CreateThread
|
||||||
|
// to start new os thread.
|
||||||
|
func tstart_stdcall(newm *m)
|
||||||
|
@@ -242,9 +251,40 @@
|
||||||
|
return unsafe.String(&sysDirectory[0], sysDirectoryLen)
|
||||||
|
}
|
||||||
|
|
||||||
|
-func windowsLoadSystemLib(name []uint16) uintptr {
|
||||||
|
- const _LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800
|
||||||
|
- return stdcall(_LoadLibraryExW, uintptr(unsafe.Pointer(&name[0])), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32)
|
||||||
|
+//go:linkname syscall_getSystemDirectory syscall.getSystemDirectory
|
||||||
|
+func syscall_getSystemDirectory() string {
|
||||||
|
+ return unsafe.String(&sysDirectory[0], sysDirectoryLen)
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+func windowsLoadSystemLib(name []byte) uintptr {
|
||||||
|
+ if useLoadLibraryEx {
|
||||||
|
+ return stdcall(_LoadLibraryExA, uintptr(unsafe.Pointer(&name[0])), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32)
|
||||||
|
+ } else {
|
||||||
|
+ absName := append(sysDirectory[:sysDirectoryLen], name...)
|
||||||
|
+ return stdcall(_LoadLibraryA, uintptr(unsafe.Pointer(&absName[0])))
|
||||||
|
+ }
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+const _LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800
|
||||||
|
+
|
||||||
|
+// When available, this function will use LoadLibraryEx with the filename
|
||||||
|
+// parameter and the important SEARCH_SYSTEM32 argument. But on systems that
|
||||||
|
+// do not have that option, absoluteFilepath should contain a fallback
|
||||||
|
+// to the full path inside of system32 for use with vanilla LoadLibrary.
|
||||||
|
+//
|
||||||
|
+//go:linkname syscall_loadsystemlibrary syscall.loadsystemlibrary
|
||||||
|
+func syscall_loadsystemlibrary(filename *uint16, absoluteFilepath *uint16) (handle, err uintptr) {
|
||||||
|
+ if useLoadLibraryEx {
|
||||||
|
+ handle, _, err = syscall_syscalln(uintptr(unsafe.Pointer(_LoadLibraryExW)), 3, uintptr(unsafe.Pointer(filename)), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32)
|
||||||
|
+ } else {
|
||||||
|
+ handle, _, err = syscall_syscalln(uintptr(unsafe.Pointer(_LoadLibraryW)), 1, uintptr(unsafe.Pointer(absoluteFilepath)))
|
||||||
|
+ }
|
||||||
|
+ KeepAlive(filename)
|
||||||
|
+ KeepAlive(absoluteFilepath)
|
||||||
|
+ if handle != 0 {
|
||||||
|
+ err = 0
|
||||||
|
+ }
|
||||||
|
+ return
|
||||||
|
}
|
||||||
|
|
||||||
|
//go:linkname windows_QueryPerformanceCounter internal/syscall/windows.QueryPerformanceCounter
|
||||||
|
@@ -262,13 +302,28 @@
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadOptionalSyscalls() {
|
||||||
|
- bcryptPrimitives := windowsLoadSystemLib(bcryptprimitivesdll[:])
|
||||||
|
- if bcryptPrimitives == 0 {
|
||||||
|
- throw("bcryptprimitives.dll not found")
|
||||||
|
+ var kernel32dll = []byte("kernel32.dll\000")
|
||||||
|
+ k32 := stdcall(_LoadLibraryA, uintptr(unsafe.Pointer(&kernel32dll[0])))
|
||||||
|
+ if k32 == 0 {
|
||||||
|
+ throw("kernel32.dll not found")
|
||||||
|
}
|
||||||
|
- _ProcessPrng = windowsFindfunc(bcryptPrimitives, []byte("ProcessPrng\000"))
|
||||||
|
+ _AddDllDirectory = windowsFindfunc(k32, []byte("AddDllDirectory\000"))
|
||||||
|
+ _AddVectoredContinueHandler = windowsFindfunc(k32, []byte("AddVectoredContinueHandler\000"))
|
||||||
|
+ _LoadLibraryExA = windowsFindfunc(k32, []byte("LoadLibraryExA\000"))
|
||||||
|
+ _LoadLibraryExW = windowsFindfunc(k32, []byte("LoadLibraryExW\000"))
|
||||||
|
+ useLoadLibraryEx = (_LoadLibraryExW != nil && _LoadLibraryExA != nil && _AddDllDirectory != nil)
|
||||||
|
+
|
||||||
|
+ initSysDirectory()
|
||||||
|
|
||||||
|
- n32 := windowsLoadSystemLib(ntdlldll[:])
|
||||||
|
+ var advapi32dll = []byte("advapi32.dll\000")
|
||||||
|
+ a32 := windowsLoadSystemLib(advapi32dll)
|
||||||
|
+ if a32 == 0 {
|
||||||
|
+ throw("advapi32.dll not found")
|
||||||
|
+ }
|
||||||
|
+ _RtlGenRandom = windowsFindfunc(a32, []byte("SystemFunction036\000"))
|
||||||
|
+
|
||||||
|
+ var ntdll = []byte("ntdll.dll\000")
|
||||||
|
+ n32 := windowsLoadSystemLib(ntdll)
|
||||||
|
if n32 == 0 {
|
||||||
|
throw("ntdll.dll not found")
|
||||||
|
}
|
||||||
|
@@ -297,7 +352,7 @@
|
||||||
|
context uintptr
|
||||||
|
}
|
||||||
|
|
||||||
|
- powrprof := windowsLoadSystemLib(powrprofdll[:])
|
||||||
|
+ powrprof := windowsLoadSystemLib([]byte("powrprof.dll\000"))
|
||||||
|
if powrprof == 0 {
|
||||||
|
return // Running on Windows 7, where we don't need it anyway.
|
||||||
|
}
|
||||||
|
@@ -351,6 +406,22 @@
|
||||||
|
// in sys_windows_386.s and sys_windows_amd64.s:
|
||||||
|
func getlasterror() uint32
|
||||||
|
|
||||||
|
+// When loading DLLs, we prefer to use LoadLibraryEx with
|
||||||
|
+// LOAD_LIBRARY_SEARCH_* flags, if available. LoadLibraryEx is not
|
||||||
|
+// available on old Windows, though, and the LOAD_LIBRARY_SEARCH_*
|
||||||
|
+// flags are not available on some versions of Windows without a
|
||||||
|
+// security patch.
|
||||||
|
+//
|
||||||
|
+// https://msdn.microsoft.com/en-us/library/ms684179(v=vs.85).aspx says:
|
||||||
|
+// "Windows 7, Windows Server 2008 R2, Windows Vista, and Windows
|
||||||
|
+// Server 2008: The LOAD_LIBRARY_SEARCH_* flags are available on
|
||||||
|
+// systems that have KB2533623 installed. To determine whether the
|
||||||
|
+// flags are available, use GetProcAddress to get the address of the
|
||||||
|
+// AddDllDirectory, RemoveDllDirectory, or SetDefaultDllDirectories
|
||||||
|
+// function. If GetProcAddress succeeds, the LOAD_LIBRARY_SEARCH_*
|
||||||
|
+// flags can be used with LoadLibraryEx."
|
||||||
|
+var useLoadLibraryEx bool
|
||||||
|
+
|
||||||
|
var timeBeginPeriodRetValue uint32
|
||||||
|
|
||||||
|
// osRelaxMinNS indicates that sysmon shouldn't osRelax if the next
|
||||||
|
@@ -417,7 +488,8 @@
|
||||||
|
// Only load winmm.dll if we need it.
|
||||||
|
// This avoids a dependency on winmm.dll for Go programs
|
||||||
|
// that run on new Windows versions.
|
||||||
|
- m32 := windowsLoadSystemLib(winmmdll[:])
|
||||||
|
+ var winmmdll = []byte("winmm.dll\000")
|
||||||
|
+ m32 := windowsLoadSystemLib(winmmdll)
|
||||||
|
if m32 == 0 {
|
||||||
|
print("runtime: LoadLibraryExW failed; errno=", getlasterror(), "\n")
|
||||||
|
throw("winmm.dll not found")
|
||||||
|
@@ -458,6 +530,28 @@
|
||||||
|
canUseLongPaths = true
|
||||||
|
}
|
||||||
|
|
||||||
|
+var osVersionInfo struct {
|
||||||
|
+ majorVersion uint32
|
||||||
|
+ minorVersion uint32
|
||||||
|
+ buildNumber uint32
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+func initOsVersionInfo() {
|
||||||
|
+ info := windows.OSVERSIONINFOW{}
|
||||||
|
+ info.OSVersionInfoSize = uint32(unsafe.Sizeof(info))
|
||||||
|
+ stdcall(_RtlGetVersion, uintptr(unsafe.Pointer(&info)))
|
||||||
|
+ osVersionInfo.majorVersion = info.MajorVersion
|
||||||
|
+ osVersionInfo.minorVersion = info.MinorVersion
|
||||||
|
+ osVersionInfo.buildNumber = info.BuildNumber
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+//go:linkname rtlGetNtVersionNumbers syscall.rtlGetNtVersionNumbers
|
||||||
|
+func rtlGetNtVersionNumbers(majorVersion *uint32, minorVersion *uint32, buildNumber *uint32) {
|
||||||
|
+ *majorVersion = osVersionInfo.majorVersion
|
||||||
|
+ *minorVersion = osVersionInfo.minorVersion
|
||||||
|
+ *buildNumber = osVersionInfo.buildNumber
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
func osinit() {
|
||||||
|
asmstdcallAddr = unsafe.Pointer(windows.AsmStdCallAddr())
|
||||||
|
|
||||||
|
@@ -470,8 +564,8 @@
|
||||||
|
initHighResTimer()
|
||||||
|
timeBeginPeriodRetValue = osRelax(false)
|
||||||
|
|
||||||
|
- initSysDirectory()
|
||||||
|
initLongPathSupport()
|
||||||
|
+ initOsVersionInfo()
|
||||||
|
|
||||||
|
numCPUStartup = getCPUCount()
|
||||||
|
|
||||||
|
@@ -487,7 +581,7 @@
|
||||||
|
//go:nosplit
|
||||||
|
func readRandom(r []byte) int {
|
||||||
|
n := 0
|
||||||
|
- if stdcall(_ProcessPrng, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 {
|
||||||
|
+ if stdcall(_RtlGenRandom, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 {
|
||||||
|
n = len(r)
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
Index: src/net/hook_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/hook_windows.go b/src/net/hook_windows.go
|
||||||
|
--- a/src/net/hook_windows.go (revision b0d48afabb9fd14976c27221cb525c5d2ebbfe79)
|
||||||
|
+++ b/src/net/hook_windows.go (revision 44e76f7cf1bc6e04b5da724e0b2e48f393713506)
|
||||||
|
@@ -13,6 +13,7 @@
|
||||||
|
hostsFilePath = windows.GetSystemDirectory() + "/Drivers/etc/hosts"
|
||||||
|
|
||||||
|
// Placeholders for socket system calls.
|
||||||
|
+ socketFunc func(int, int, int) (syscall.Handle, error) = syscall.Socket
|
||||||
|
wsaSocketFunc func(int32, int32, int32, *syscall.WSAProtocolInfo, uint32, uint32) (syscall.Handle, error) = windows.WSASocket
|
||||||
|
connectFunc func(syscall.Handle, syscall.Sockaddr) error = syscall.Connect
|
||||||
|
listenFunc func(syscall.Handle, int) error = syscall.Listen
|
||||||
|
Index: src/net/internal/socktest/main_test.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/internal/socktest/main_test.go b/src/net/internal/socktest/main_test.go
|
||||||
|
--- a/src/net/internal/socktest/main_test.go (revision b0d48afabb9fd14976c27221cb525c5d2ebbfe79)
|
||||||
|
+++ b/src/net/internal/socktest/main_test.go (revision 44e76f7cf1bc6e04b5da724e0b2e48f393713506)
|
||||||
|
@@ -2,7 +2,7 @@
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
-//go:build !js && !plan9 && !wasip1 && !windows
|
||||||
|
+//go:build !js && !plan9 && !wasip1
|
||||||
|
|
||||||
|
package socktest_test
|
||||||
|
|
||||||
|
Index: src/net/internal/socktest/main_windows_test.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/internal/socktest/main_windows_test.go b/src/net/internal/socktest/main_windows_test.go
|
||||||
|
new file mode 100644
|
||||||
|
--- /dev/null (revision 44e76f7cf1bc6e04b5da724e0b2e48f393713506)
|
||||||
|
+++ b/src/net/internal/socktest/main_windows_test.go (revision 44e76f7cf1bc6e04b5da724e0b2e48f393713506)
|
||||||
|
@@ -0,0 +1,22 @@
|
||||||
|
+// Copyright 2015 The Go Authors. All rights reserved.
|
||||||
|
+// Use of this source code is governed by a BSD-style
|
||||||
|
+// license that can be found in the LICENSE file.
|
||||||
|
+
|
||||||
|
+package socktest_test
|
||||||
|
+
|
||||||
|
+import "syscall"
|
||||||
|
+
|
||||||
|
+var (
|
||||||
|
+ socketFunc func(int, int, int) (syscall.Handle, error)
|
||||||
|
+ closeFunc func(syscall.Handle) error
|
||||||
|
+)
|
||||||
|
+
|
||||||
|
+func installTestHooks() {
|
||||||
|
+ socketFunc = sw.Socket
|
||||||
|
+ closeFunc = sw.Closesocket
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+func uninstallTestHooks() {
|
||||||
|
+ socketFunc = syscall.Socket
|
||||||
|
+ closeFunc = syscall.Closesocket
|
||||||
|
+}
|
||||||
|
Index: src/net/internal/socktest/sys_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/internal/socktest/sys_windows.go b/src/net/internal/socktest/sys_windows.go
|
||||||
|
--- a/src/net/internal/socktest/sys_windows.go (revision b0d48afabb9fd14976c27221cb525c5d2ebbfe79)
|
||||||
|
+++ b/src/net/internal/socktest/sys_windows.go (revision 44e76f7cf1bc6e04b5da724e0b2e48f393713506)
|
||||||
|
@@ -9,6 +9,38 @@
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
+// Socket wraps [syscall.Socket].
|
||||||
|
+func (sw *Switch) Socket(family, sotype, proto int) (s syscall.Handle, err error) {
|
||||||
|
+ sw.once.Do(sw.init)
|
||||||
|
+
|
||||||
|
+ so := &Status{Cookie: cookie(family, sotype, proto)}
|
||||||
|
+ sw.fmu.RLock()
|
||||||
|
+ f, _ := sw.fltab[FilterSocket]
|
||||||
|
+ sw.fmu.RUnlock()
|
||||||
|
+
|
||||||
|
+ af, err := f.apply(so)
|
||||||
|
+ if err != nil {
|
||||||
|
+ return syscall.InvalidHandle, err
|
||||||
|
+ }
|
||||||
|
+ s, so.Err = syscall.Socket(family, sotype, proto)
|
||||||
|
+ if err = af.apply(so); err != nil {
|
||||||
|
+ if so.Err == nil {
|
||||||
|
+ syscall.Closesocket(s)
|
||||||
|
+ }
|
||||||
|
+ return syscall.InvalidHandle, err
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
+ sw.smu.Lock()
|
||||||
|
+ defer sw.smu.Unlock()
|
||||||
|
+ if so.Err != nil {
|
||||||
|
+ sw.stats.getLocked(so.Cookie).OpenFailed++
|
||||||
|
+ return syscall.InvalidHandle, so.Err
|
||||||
|
+ }
|
||||||
|
+ nso := sw.addLocked(s, family, sotype, proto)
|
||||||
|
+ sw.stats.getLocked(nso.Cookie).Opened++
|
||||||
|
+ return s, nil
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
// WSASocket wraps [syscall.WSASocket].
|
||||||
|
func (sw *Switch) WSASocket(family, sotype, proto int32, protinfo *syscall.WSAProtocolInfo, group uint32, flags uint32) (s syscall.Handle, err error) {
|
||||||
|
sw.once.Do(sw.init)
|
||||||
|
Index: src/net/main_windows_test.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/main_windows_test.go b/src/net/main_windows_test.go
|
||||||
|
--- a/src/net/main_windows_test.go (revision b0d48afabb9fd14976c27221cb525c5d2ebbfe79)
|
||||||
|
+++ b/src/net/main_windows_test.go (revision 44e76f7cf1bc6e04b5da724e0b2e48f393713506)
|
||||||
|
@@ -12,6 +12,7 @@
|
||||||
|
|
||||||
|
var (
|
||||||
|
// Placeholders for saving original socket system calls.
|
||||||
|
+ origSocket = socketFunc
|
||||||
|
origWSASocket = wsaSocketFunc
|
||||||
|
origClosesocket = poll.CloseFunc
|
||||||
|
origConnect = connectFunc
|
||||||
|
@@ -21,6 +22,7 @@
|
||||||
|
)
|
||||||
|
|
||||||
|
func installTestHooks() {
|
||||||
|
+ socketFunc = sw.Socket
|
||||||
|
wsaSocketFunc = sw.WSASocket
|
||||||
|
poll.CloseFunc = sw.Closesocket
|
||||||
|
connectFunc = sw.Connect
|
||||||
|
@@ -30,6 +32,7 @@
|
||||||
|
}
|
||||||
|
|
||||||
|
func uninstallTestHooks() {
|
||||||
|
+ socketFunc = origSocket
|
||||||
|
wsaSocketFunc = origWSASocket
|
||||||
|
poll.CloseFunc = origClosesocket
|
||||||
|
connectFunc = origConnect
|
||||||
|
Index: src/net/sock_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/net/sock_windows.go b/src/net/sock_windows.go
|
||||||
|
--- a/src/net/sock_windows.go (revision b0d48afabb9fd14976c27221cb525c5d2ebbfe79)
|
||||||
|
+++ b/src/net/sock_windows.go (revision 44e76f7cf1bc6e04b5da724e0b2e48f393713506)
|
||||||
|
@@ -20,6 +20,21 @@
|
||||||
|
func sysSocket(family, sotype, proto int) (syscall.Handle, error) {
|
||||||
|
s, err := wsaSocketFunc(int32(family), int32(sotype), int32(proto),
|
||||||
|
nil, 0, windows.WSA_FLAG_OVERLAPPED|windows.WSA_FLAG_NO_HANDLE_INHERIT)
|
||||||
|
+ if err == nil {
|
||||||
|
+ return s, nil
|
||||||
|
+ }
|
||||||
|
+ // WSA_FLAG_NO_HANDLE_INHERIT flag is not supported on some
|
||||||
|
+ // old versions of Windows, see
|
||||||
|
+ // https://msdn.microsoft.com/en-us/library/windows/desktop/ms742212(v=vs.85).aspx
|
||||||
|
+ // for details. Just use syscall.Socket, if windows.WSASocket failed.
|
||||||
|
+
|
||||||
|
+ // See ../syscall/exec_unix.go for description of ForkLock.
|
||||||
|
+ syscall.ForkLock.RLock()
|
||||||
|
+ s, err = socketFunc(family, sotype, proto)
|
||||||
|
+ if err == nil {
|
||||||
|
+ syscall.CloseOnExec(s)
|
||||||
|
+ }
|
||||||
|
+ syscall.ForkLock.RUnlock()
|
||||||
|
if err != nil {
|
||||||
|
return syscall.InvalidHandle, os.NewSyscallError("socket", err)
|
||||||
|
}
|
||||||
|
Index: src/syscall/exec_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/syscall/exec_windows.go b/src/syscall/exec_windows.go
|
||||||
|
--- a/src/syscall/exec_windows.go (revision b0d48afabb9fd14976c27221cb525c5d2ebbfe79)
|
||||||
|
+++ b/src/syscall/exec_windows.go (revision b4aece36e51ecce81c3ee9fe03e31db552e90018)
|
||||||
|
@@ -15,7 +15,6 @@
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
-// ForkLock is not used on Windows.
|
||||||
|
var ForkLock sync.RWMutex
|
||||||
|
|
||||||
|
// EscapeArg rewrites command line argument s as prescribed
|
||||||
|
@@ -304,6 +303,9 @@
|
||||||
|
var zeroProcAttr ProcAttr
|
||||||
|
var zeroSysProcAttr SysProcAttr
|
||||||
|
|
||||||
|
+//go:linkname rtlGetNtVersionNumbers
|
||||||
|
+func rtlGetNtVersionNumbers(majorVersion *uint32, minorVersion *uint32, buildNumber *uint32)
|
||||||
|
+
|
||||||
|
func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle uintptr, err error) {
|
||||||
|
if len(argv0) == 0 {
|
||||||
|
return 0, 0, EWINDOWS
|
||||||
|
@@ -367,6 +369,17 @@
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
+ var maj, min, build uint32
|
||||||
|
+ rtlGetNtVersionNumbers(&maj, &min, &build)
|
||||||
|
+ isWin7 := maj < 6 || (maj == 6 && min <= 1)
|
||||||
|
+ // NT kernel handles are divisible by 4, with the bottom 3 bits left as
|
||||||
|
+ // a tag. The fully set tag correlates with the types of handles we're
|
||||||
|
+ // concerned about here. Except, the kernel will interpret some
|
||||||
|
+ // special handle values, like -1, -2, and so forth, so kernelbase.dll
|
||||||
|
+ // checks to see that those bottom three bits are checked, but that top
|
||||||
|
+ // bit is not checked.
|
||||||
|
+ isLegacyWin7ConsoleHandle := func(handle Handle) bool { return isWin7 && handle&0x10000003 == 3 }
|
||||||
|
+
|
||||||
|
p, _ := GetCurrentProcess()
|
||||||
|
parentProcess := p
|
||||||
|
if sys.ParentProcess != 0 {
|
||||||
|
@@ -375,7 +388,15 @@
|
||||||
|
fd := make([]Handle, len(attr.Files))
|
||||||
|
for i := range attr.Files {
|
||||||
|
if attr.Files[i] > 0 {
|
||||||
|
- err := DuplicateHandle(p, Handle(attr.Files[i]), parentProcess, &fd[i], 0, true, DUPLICATE_SAME_ACCESS)
|
||||||
|
+ destinationProcessHandle := parentProcess
|
||||||
|
+
|
||||||
|
+ // On Windows 7, console handles aren't real handles, and can only be duplicated
|
||||||
|
+ // into the current process, not a parent one, which amounts to the same thing.
|
||||||
|
+ if parentProcess != p && isLegacyWin7ConsoleHandle(Handle(attr.Files[i])) {
|
||||||
|
+ destinationProcessHandle = p
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
+ err := DuplicateHandle(p, Handle(attr.Files[i]), destinationProcessHandle, &fd[i], 0, true, DUPLICATE_SAME_ACCESS)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
@@ -406,6 +427,14 @@
|
||||||
|
|
||||||
|
fd = append(fd, sys.AdditionalInheritedHandles...)
|
||||||
|
|
||||||
|
+ // On Windows 7, console handles aren't real handles, so don't pass them
|
||||||
|
+ // through to PROC_THREAD_ATTRIBUTE_HANDLE_LIST.
|
||||||
|
+ for i := range fd {
|
||||||
|
+ if isLegacyWin7ConsoleHandle(fd[i]) {
|
||||||
|
+ fd[i] = 0
|
||||||
|
+ }
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
// The presence of a NULL handle in the list is enough to cause PROC_THREAD_ATTRIBUTE_HANDLE_LIST
|
||||||
|
// to treat the entire list as empty, so remove NULL handles.
|
||||||
|
j := 0
|
||||||
|
Index: src/syscall/dll_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/syscall/dll_windows.go b/src/syscall/dll_windows.go
|
||||||
|
--- a/src/syscall/dll_windows.go (revision b4aece36e51ecce81c3ee9fe03e31db552e90018)
|
||||||
|
+++ b/src/syscall/dll_windows.go (revision ea2726a6fa25fbfa1092e696e522eafca544d24c)
|
||||||
|
@@ -119,14 +119,7 @@
|
||||||
|
}
|
||||||
|
|
||||||
|
//go:linkname loadsystemlibrary
|
||||||
|
-func loadsystemlibrary(filename *uint16) (uintptr, Errno) {
|
||||||
|
- const _LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800
|
||||||
|
- handle, _, err := SyscallN(uintptr(__LoadLibraryExW), uintptr(unsafe.Pointer(filename)), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32)
|
||||||
|
- if handle != 0 {
|
||||||
|
- err = 0
|
||||||
|
- }
|
||||||
|
- return handle, err
|
||||||
|
-}
|
||||||
|
+func loadsystemlibrary(filename *uint16, absoluteFilepath *uint16) (handle uintptr, err Errno)
|
||||||
|
|
||||||
|
//go:linkname getprocaddress
|
||||||
|
func getprocaddress(handle uintptr, procname *uint8) (uintptr, Errno) {
|
||||||
|
@@ -143,6 +136,9 @@
|
||||||
|
Handle Handle
|
||||||
|
}
|
||||||
|
|
||||||
|
+//go:linkname getSystemDirectory
|
||||||
|
+func getSystemDirectory() string // Implemented in runtime package.
|
||||||
|
+
|
||||||
|
// LoadDLL loads the named DLL file into memory.
|
||||||
|
//
|
||||||
|
// If name is not an absolute path and is not a known system DLL used by
|
||||||
|
@@ -159,7 +155,11 @@
|
||||||
|
var h uintptr
|
||||||
|
var e Errno
|
||||||
|
if sysdll.IsSystemDLL[name] {
|
||||||
|
- h, e = loadsystemlibrary(namep)
|
||||||
|
+ absoluteFilepathp, err := UTF16PtrFromString(getSystemDirectory() + name)
|
||||||
|
+ if err != nil {
|
||||||
|
+ return nil, err
|
||||||
|
+ }
|
||||||
|
+ h, e = loadsystemlibrary(namep, absoluteFilepathp)
|
||||||
|
} else {
|
||||||
|
h, e = loadlibrary(namep)
|
||||||
|
}
|
||||||
|
Index: src/os/removeall_at.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/os/removeall_at.go b/src/os/removeall_at.go
|
||||||
|
--- a/src/os/removeall_at.go (revision ea2726a6fa25fbfa1092e696e522eafca544d24c)
|
||||||
|
+++ b/src/os/removeall_at.go (revision d47e0d22130d597dcf9daa6b41fd9501274f0cb2)
|
||||||
|
@@ -2,7 +2,7 @@
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
-//go:build unix || wasip1 || windows
|
||||||
|
+//go:build unix || wasip1
|
||||||
|
|
||||||
|
package os
|
||||||
|
|
||||||
|
@@ -175,3 +175,25 @@
|
||||||
|
}
|
||||||
|
return newDirFile(fd, name)
|
||||||
|
}
|
||||||
|
+
|
||||||
|
+func rootRemoveAll(r *Root, name string) error {
|
||||||
|
+ // Consistency with os.RemoveAll: Strip trailing /s from the name,
|
||||||
|
+ // so RemoveAll("not_a_directory/") succeeds.
|
||||||
|
+ for len(name) > 0 && IsPathSeparator(name[len(name)-1]) {
|
||||||
|
+ name = name[:len(name)-1]
|
||||||
|
+ }
|
||||||
|
+ if endsWithDot(name) {
|
||||||
|
+ // Consistency with os.RemoveAll: Return EINVAL when trying to remove .
|
||||||
|
+ return &PathError{Op: "RemoveAll", Path: name, Err: syscall.EINVAL}
|
||||||
|
+ }
|
||||||
|
+ _, err := doInRoot(r, name, nil, func(parent sysfdType, name string) (struct{}, error) {
|
||||||
|
+ return struct{}{}, removeAllFrom(parent, name)
|
||||||
|
+ })
|
||||||
|
+ if IsNotExist(err) {
|
||||||
|
+ return nil
|
||||||
|
+ }
|
||||||
|
+ if err != nil {
|
||||||
|
+ return &PathError{Op: "RemoveAll", Path: name, Err: underlyingError(err)}
|
||||||
|
+ }
|
||||||
|
+ return err
|
||||||
|
+}
|
||||||
|
Index: src/os/removeall_noat.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/os/removeall_noat.go b/src/os/removeall_noat.go
|
||||||
|
--- a/src/os/removeall_noat.go (revision ea2726a6fa25fbfa1092e696e522eafca544d24c)
|
||||||
|
+++ b/src/os/removeall_noat.go (revision d47e0d22130d597dcf9daa6b41fd9501274f0cb2)
|
||||||
|
@@ -2,7 +2,7 @@
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
-//go:build (js && wasm) || plan9
|
||||||
|
+//go:build (js && wasm) || plan9 || windows
|
||||||
|
|
||||||
|
package os
|
||||||
|
|
||||||
|
@@ -140,3 +140,22 @@
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
+
|
||||||
|
+func rootRemoveAll(r *Root, name string) error {
|
||||||
|
+ if endsWithDot(name) {
|
||||||
|
+ // Consistency with os.RemoveAll: Return EINVAL when trying to remove .
|
||||||
|
+ return &PathError{Op: "RemoveAll", Path: name, Err: syscall.EINVAL}
|
||||||
|
+ }
|
||||||
|
+ if err := checkPathEscapesLstat(r, name); err != nil {
|
||||||
|
+ if err == syscall.ENOTDIR {
|
||||||
|
+ // Some intermediate path component is not a directory.
|
||||||
|
+ // RemoveAll treats this as success (since the target doesn't exist).
|
||||||
|
+ return nil
|
||||||
|
+ }
|
||||||
|
+ return &PathError{Op: "RemoveAll", Path: name, Err: err}
|
||||||
|
+ }
|
||||||
|
+ if err := RemoveAll(joinPath(r.root.name, name)); err != nil {
|
||||||
|
+ return &PathError{Op: "RemoveAll", Path: name, Err: underlyingError(err)}
|
||||||
|
+ }
|
||||||
|
+ return nil
|
||||||
|
+}
|
||||||
|
Index: src/os/root_noopenat.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/os/root_noopenat.go b/src/os/root_noopenat.go
|
||||||
|
--- a/src/os/root_noopenat.go (revision ea2726a6fa25fbfa1092e696e522eafca544d24c)
|
||||||
|
+++ b/src/os/root_noopenat.go (revision d47e0d22130d597dcf9daa6b41fd9501274f0cb2)
|
||||||
|
@@ -11,7 +11,6 @@
|
||||||
|
"internal/filepathlite"
|
||||||
|
"internal/stringslite"
|
||||||
|
"sync/atomic"
|
||||||
|
- "syscall"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
@@ -185,25 +184,6 @@
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
-
|
||||||
|
-func rootRemoveAll(r *Root, name string) error {
|
||||||
|
- if endsWithDot(name) {
|
||||||
|
- // Consistency with os.RemoveAll: Return EINVAL when trying to remove .
|
||||||
|
- return &PathError{Op: "RemoveAll", Path: name, Err: syscall.EINVAL}
|
||||||
|
- }
|
||||||
|
- if err := checkPathEscapesLstat(r, name); err != nil {
|
||||||
|
- if err == syscall.ENOTDIR {
|
||||||
|
- // Some intermediate path component is not a directory.
|
||||||
|
- // RemoveAll treats this as success (since the target doesn't exist).
|
||||||
|
- return nil
|
||||||
|
- }
|
||||||
|
- return &PathError{Op: "RemoveAll", Path: name, Err: err}
|
||||||
|
- }
|
||||||
|
- if err := RemoveAll(joinPath(r.root.name, name)); err != nil {
|
||||||
|
- return &PathError{Op: "RemoveAll", Path: name, Err: underlyingError(err)}
|
||||||
|
- }
|
||||||
|
- return nil
|
||||||
|
-}
|
||||||
|
|
||||||
|
func rootReadlink(r *Root, name string) (string, error) {
|
||||||
|
if err := checkPathEscapesLstat(r, name); err != nil {
|
||||||
|
Index: src/os/root_openat.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/os/root_openat.go b/src/os/root_openat.go
|
||||||
|
--- a/src/os/root_openat.go (revision ea2726a6fa25fbfa1092e696e522eafca544d24c)
|
||||||
|
+++ b/src/os/root_openat.go (revision d47e0d22130d597dcf9daa6b41fd9501274f0cb2)
|
||||||
|
@@ -196,28 +196,6 @@
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
-func rootRemoveAll(r *Root, name string) error {
|
||||||
|
- // Consistency with os.RemoveAll: Strip trailing /s from the name,
|
||||||
|
- // so RemoveAll("not_a_directory/") succeeds.
|
||||||
|
- for len(name) > 0 && IsPathSeparator(name[len(name)-1]) {
|
||||||
|
- name = name[:len(name)-1]
|
||||||
|
- }
|
||||||
|
- if endsWithDot(name) {
|
||||||
|
- // Consistency with os.RemoveAll: Return EINVAL when trying to remove .
|
||||||
|
- return &PathError{Op: "RemoveAll", Path: name, Err: syscall.EINVAL}
|
||||||
|
- }
|
||||||
|
- _, err := doInRoot(r, name, nil, func(parent sysfdType, name string) (struct{}, error) {
|
||||||
|
- return struct{}{}, removeAllFrom(parent, name)
|
||||||
|
- })
|
||||||
|
- if IsNotExist(err) {
|
||||||
|
- return nil
|
||||||
|
- }
|
||||||
|
- if err != nil {
|
||||||
|
- return &PathError{Op: "RemoveAll", Path: name, Err: underlyingError(err)}
|
||||||
|
- }
|
||||||
|
- return err
|
||||||
|
-}
|
||||||
|
-
|
||||||
|
func rootRename(r *Root, oldname, newname string) error {
|
||||||
|
_, err := doInRoot(r, oldname, nil, func(oldparent sysfdType, oldname string) (struct{}, error) {
|
||||||
|
_, err := doInRoot(r, newname, nil, func(newparent sysfdType, newname string) (struct{}, error) {
|
||||||
|
Index: src/os/root_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/os/root_windows.go b/src/os/root_windows.go
|
||||||
|
--- a/src/os/root_windows.go (revision ea2726a6fa25fbfa1092e696e522eafca544d24c)
|
||||||
|
+++ b/src/os/root_windows.go (revision d47e0d22130d597dcf9daa6b41fd9501274f0cb2)
|
||||||
|
@@ -402,3 +402,14 @@
|
||||||
|
}
|
||||||
|
return fi.Mode(), nil
|
||||||
|
}
|
||||||
|
+
|
||||||
|
+func checkPathEscapes(r *Root, name string) error {
|
||||||
|
+ if !filepathlite.IsLocal(name) {
|
||||||
|
+ return errPathEscapes
|
||||||
|
+ }
|
||||||
|
+ return nil
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+func checkPathEscapesLstat(r *Root, name string) error {
|
||||||
|
+ return checkPathEscapes(r, name)
|
||||||
|
+}
|
||||||
|
Index: src/os/exec_windows.go
|
||||||
|
IDEA additional info:
|
||||||
|
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
|
||||||
|
<+>UTF-8
|
||||||
|
===================================================================
|
||||||
|
diff --git a/src/os/exec_windows.go b/src/os/exec_windows.go
|
||||||
|
--- a/src/os/exec_windows.go (revision d47e0d22130d597dcf9daa6b41fd9501274f0cb2)
|
||||||
|
+++ b/src/os/exec_windows.go (revision 00e8daec9a4d88f44a8dc55d3bdb71878e525b41)
|
||||||
|
@@ -10,6 +10,7 @@
|
||||||
|
"runtime"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
+ _ "unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Note that Process.handle is never nil because Windows always requires
|
||||||
|
@@ -49,9 +50,23 @@
|
||||||
|
// than statusDone.
|
||||||
|
p.doRelease(statusReleased)
|
||||||
|
|
||||||
|
+ var maj, min, build uint32
|
||||||
|
+ rtlGetNtVersionNumbers(&maj, &min, &build)
|
||||||
|
+ if maj < 10 {
|
||||||
|
+ // NOTE(brainman): It seems that sometimes process is not dead
|
||||||
|
+ // when WaitForSingleObject returns. But we do not know any
|
||||||
|
+ // other way to wait for it. Sleeping for a while seems to do
|
||||||
|
+ // the trick sometimes.
|
||||||
|
+ // See https://golang.org/issue/25965 for details.
|
||||||
|
+ time.Sleep(5 * time.Millisecond)
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
return &ProcessState{p.Pid, syscall.WaitStatus{ExitCode: ec}, &u}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
+//go:linkname rtlGetNtVersionNumbers syscall.rtlGetNtVersionNumbers
|
||||||
|
+func rtlGetNtVersionNumbers(majorVersion *uint32, minorVersion *uint32, buildNumber *uint32)
|
||||||
|
+
|
||||||
|
func (p *Process) signal(sig Signal) error {
|
||||||
|
handle, status := p.handleTransientAcquire()
|
||||||
|
switch status {
|
||||||
18
.github/release/.fpm_systemd
vendored
Normal file
18
.github/release/.fpm_systemd
vendored
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
-s dir
|
||||||
|
--name mihomo
|
||||||
|
--category net
|
||||||
|
--license GPL-3.0-or-later
|
||||||
|
--description "The universal proxy platform."
|
||||||
|
--url "https://wiki.metacubex.one/"
|
||||||
|
--maintainer "MetaCubeX <none@example.com>"
|
||||||
|
--deb-field "Bug: https://github.com/MetaCubeX/mihomo/issues"
|
||||||
|
--no-deb-generate-changes
|
||||||
|
--config-files /etc/mihomo/config.yaml
|
||||||
|
|
||||||
|
.github/release/config.yaml=/etc/mihomo/config.yaml
|
||||||
|
|
||||||
|
.github/release/mihomo.service=/usr/lib/systemd/system/mihomo.service
|
||||||
|
.github/release/mihomo@.service=/usr/lib/systemd/system/mihomo@.service
|
||||||
|
|
||||||
|
|
||||||
|
LICENSE=/usr/share/licenses/mihomo/LICENSE
|
||||||
15
.github/release/config.yaml
vendored
Normal file
15
.github/release/config.yaml
vendored
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
mixed-port: 7890
|
||||||
|
|
||||||
|
dns:
|
||||||
|
enable: true
|
||||||
|
ipv6: true
|
||||||
|
enhanced-mode: fake-ip
|
||||||
|
fake-ip-filter:
|
||||||
|
- "*"
|
||||||
|
- "+.lan"
|
||||||
|
- "+.local"
|
||||||
|
nameserver:
|
||||||
|
- system
|
||||||
|
|
||||||
|
rules:
|
||||||
|
- MATCH,DIRECT
|
||||||
306
.github/workflows/build.yml
vendored
306
.github/workflows/build.yml
vendored
@@ -29,36 +29,52 @@ jobs:
|
|||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
jobs:
|
jobs:
|
||||||
- { goos: darwin, goarch: arm64, output: arm64 }
|
- { goos: darwin, goarch: amd64, goamd64: v1, output: amd64-compatible } # old style file name will be removed in next released
|
||||||
- { goos: darwin, goarch: amd64, goamd64: v1, output: amd64-compatible }
|
|
||||||
- { goos: darwin, goarch: amd64, goamd64: v3, output: amd64 }
|
- { goos: darwin, goarch: amd64, goamd64: v3, output: amd64 }
|
||||||
|
- { goos: darwin, goarch: amd64, goamd64: v1, output: amd64-v1 }
|
||||||
|
- { goos: darwin, goarch: amd64, goamd64: v2, output: amd64-v2 }
|
||||||
|
- { goos: darwin, goarch: amd64, goamd64: v3, output: amd64-v3 }
|
||||||
|
- { goos: darwin, goarch: arm64, output: arm64 }
|
||||||
|
|
||||||
- { goos: linux, goarch: '386', output: '386' }
|
- { goos: linux, goarch: '386', go386: sse2, output: '386', debian: i386, rpm: i386}
|
||||||
- { goos: linux, goarch: amd64, goamd64: v1, output: amd64-compatible, test: test }
|
- { goos: linux, goarch: '386', go386: softfloat, output: '386-softfloat' }
|
||||||
- { goos: linux, goarch: amd64, goamd64: v3, output: amd64 }
|
- { goos: linux, goarch: amd64, goamd64: v1, output: amd64-compatible} # old style file name will be removed in next released
|
||||||
- { goos: linux, goarch: arm64, output: arm64 }
|
- { goos: linux, goarch: amd64, goamd64: v3, output: amd64, debian: amd64, rpm: x86_64, pacman: x86_64}
|
||||||
|
- { goos: linux, goarch: amd64, goamd64: v1, output: amd64-v1, debian: amd64, rpm: x86_64, pacman: x86_64, test: test }
|
||||||
|
- { goos: linux, goarch: amd64, goamd64: v2, output: amd64-v2, debian: amd64, rpm: x86_64, pacman: x86_64}
|
||||||
|
- { goos: linux, goarch: amd64, goamd64: v3, output: amd64-v3, debian: amd64, rpm: x86_64, pacman: x86_64}
|
||||||
|
- { goos: linux, goarch: arm64, output: arm64, debian: arm64, rpm: aarch64, pacman: aarch64}
|
||||||
- { goos: linux, goarch: arm, goarm: '5', output: armv5 }
|
- { goos: linux, goarch: arm, goarm: '5', output: armv5 }
|
||||||
- { goos: linux, goarch: arm, goarm: '6', output: armv6 }
|
- { goos: linux, goarch: arm, goarm: '6', output: armv6, debian: armel, rpm: armv6hl}
|
||||||
- { goos: linux, goarch: arm, goarm: '7', output: armv7 }
|
- { goos: linux, goarch: arm, goarm: '7', output: armv7, debian: armhf, rpm: armv7hl, pacman: armv7hl}
|
||||||
- { goos: linux, goarch: mips, gomips: hardfloat, output: mips-hardfloat }
|
- { goos: linux, goarch: mips, gomips: hardfloat, output: mips-hardfloat }
|
||||||
- { goos: linux, goarch: mips, gomips: softfloat, output: mips-softfloat }
|
- { goos: linux, goarch: mips, gomips: softfloat, output: mips-softfloat }
|
||||||
- { goos: linux, goarch: mipsle, gomips: hardfloat, output: mipsle-hardfloat }
|
- { goos: linux, goarch: mipsle, gomips: hardfloat, output: mipsle-hardfloat }
|
||||||
- { goos: linux, goarch: mipsle, gomips: softfloat, output: mipsle-softfloat }
|
- { goos: linux, goarch: mipsle, gomips: softfloat, output: mipsle-softfloat }
|
||||||
- { goos: linux, goarch: mips64, output: mips64 }
|
- { goos: linux, goarch: mips64, output: mips64 }
|
||||||
- { goos: linux, goarch: mips64le, output: mips64le }
|
- { goos: linux, goarch: mips64le, output: mips64le, debian: mips64el, rpm: mips64el }
|
||||||
- { goos: linux, goarch: loong64, output: loong64-abi1, abi: '1' }
|
- { goos: linux, goarch: loong64, output: loong64-abi1, abi: '1', debian: loongarch64, rpm: loongarch64 }
|
||||||
- { goos: linux, goarch: loong64, output: loong64-abi2, abi: '2' }
|
- { goos: linux, goarch: loong64, output: loong64-abi2, abi: '2', debian: loong64, rpm: loong64 }
|
||||||
- { goos: linux, goarch: riscv64, output: riscv64 }
|
- { goos: linux, goarch: riscv64, output: riscv64, debian: riscv64, rpm: riscv64 }
|
||||||
- { goos: linux, goarch: s390x, output: s390x }
|
- { goos: linux, goarch: s390x, output: s390x, debian: s390x, rpm: s390x }
|
||||||
|
- { goos: linux, goarch: ppc64le, output: ppc64le, debian: ppc64el, rpm: ppc64le }
|
||||||
|
|
||||||
|
# Go 1.26 with special patch can work on Windows 7
|
||||||
|
# https://github.com/MetaCubeX/go/commits/release-branch.go1.26/
|
||||||
- { goos: windows, goarch: '386', output: '386' }
|
- { goos: windows, goarch: '386', output: '386' }
|
||||||
- { goos: windows, goarch: amd64, goamd64: v1, output: amd64-compatible }
|
- { goos: windows, goarch: amd64, goamd64: v1, output: amd64-compatible } # old style file name will be removed in next released
|
||||||
- { goos: windows, goarch: amd64, goamd64: v3, output: amd64 }
|
- { goos: windows, goarch: amd64, goamd64: v3, output: amd64 }
|
||||||
|
- { goos: windows, goarch: amd64, goamd64: v1, output: amd64-v1 }
|
||||||
|
- { goos: windows, goarch: amd64, goamd64: v2, output: amd64-v2 }
|
||||||
|
- { goos: windows, goarch: amd64, goamd64: v3, output: amd64-v3 }
|
||||||
- { goos: windows, goarch: arm64, output: arm64 }
|
- { goos: windows, goarch: arm64, output: arm64 }
|
||||||
|
|
||||||
- { goos: freebsd, goarch: '386', output: '386' }
|
- { goos: freebsd, goarch: '386', output: '386' }
|
||||||
- { goos: freebsd, goarch: amd64, goamd64: v1, output: amd64-compatible }
|
- { goos: freebsd, goarch: amd64, goamd64: v1, output: amd64-compatible } # old style file name will be removed in next released
|
||||||
- { goos: freebsd, goarch: amd64, goamd64: v3, output: amd64 }
|
- { goos: freebsd, goarch: amd64, goamd64: v3, output: amd64 }
|
||||||
|
- { goos: freebsd, goarch: amd64, goamd64: v1, output: amd64-v1 }
|
||||||
|
- { goos: freebsd, goarch: amd64, goamd64: v2, output: amd64-v2 }
|
||||||
|
- { goos: freebsd, goarch: amd64, goamd64: v3, output: amd64-v3 }
|
||||||
- { goos: freebsd, goarch: arm64, output: arm64 }
|
- { goos: freebsd, goarch: arm64, output: arm64 }
|
||||||
|
|
||||||
- { goos: android, goarch: '386', ndk: i686-linux-android34, output: '386' }
|
- { goos: android, goarch: '386', ndk: i686-linux-android34, output: '386' }
|
||||||
@@ -66,73 +82,136 @@ jobs:
|
|||||||
- { goos: android, goarch: arm, ndk: armv7a-linux-androideabi34, output: armv7 }
|
- { goos: android, goarch: arm, ndk: armv7a-linux-androideabi34, output: armv7 }
|
||||||
- { goos: android, goarch: arm64, ndk: aarch64-linux-android34, output: arm64-v8 }
|
- { goos: android, goarch: arm64, ndk: aarch64-linux-android34, output: arm64-v8 }
|
||||||
|
|
||||||
|
# Go 1.25 with special patch can work on Windows 7
|
||||||
|
# https://github.com/MetaCubeX/go/commits/release-branch.go1.25/
|
||||||
|
- { goos: windows, goarch: '386', output: '386-go125', goversion: '1.25' }
|
||||||
|
- { goos: windows, goarch: amd64, goamd64: v1, output: amd64-v1-go125, goversion: '1.25' }
|
||||||
|
- { goos: windows, goarch: amd64, goamd64: v2, output: amd64-v2-go125, goversion: '1.25' }
|
||||||
|
- { goos: windows, goarch: amd64, goamd64: v3, output: amd64-v3-go125, goversion: '1.25' }
|
||||||
|
|
||||||
|
# Go 1.24 with special patch can work on Windows 7
|
||||||
|
# https://github.com/MetaCubeX/go/commits/release-branch.go1.24/
|
||||||
|
- { goos: windows, goarch: '386', output: '386-go124', goversion: '1.24' }
|
||||||
|
- { goos: windows, goarch: amd64, goamd64: v1, output: amd64-v1-go124, goversion: '1.24' }
|
||||||
|
- { goos: windows, goarch: amd64, goamd64: v2, output: amd64-v2-go124, goversion: '1.24' }
|
||||||
|
- { goos: windows, goarch: amd64, goamd64: v3, output: amd64-v3-go124, goversion: '1.24' }
|
||||||
|
|
||||||
# Go 1.23 with special patch can work on Windows 7
|
# Go 1.23 with special patch can work on Windows 7
|
||||||
# https://github.com/MetaCubeX/go/commits/release-branch.go1.23/
|
# https://github.com/MetaCubeX/go/commits/release-branch.go1.23/
|
||||||
- { goos: windows, goarch: '386', output: '386-go123', goversion: '1.23' }
|
- { goos: windows, goarch: '386', output: '386-go123', goversion: '1.23' }
|
||||||
- { goos: windows, goarch: amd64, goamd64: v1, output: amd64-compatible-go123, goversion: '1.23' }
|
- { goos: windows, goarch: amd64, goamd64: v1, output: amd64-v1-go123, goversion: '1.23' }
|
||||||
- { goos: windows, goarch: amd64, goamd64: v3, output: amd64-go123, goversion: '1.23' }
|
- { goos: windows, goarch: amd64, goamd64: v2, output: amd64-v2-go123, goversion: '1.23' }
|
||||||
|
- { goos: windows, goarch: amd64, goamd64: v3, output: amd64-v3-go123, goversion: '1.23' }
|
||||||
|
|
||||||
# Go 1.22 with special patch can work on Windows 7
|
# Go 1.22 with special patch can work on Windows 7
|
||||||
# https://github.com/MetaCubeX/go/commits/release-branch.go1.22/
|
# https://github.com/MetaCubeX/go/commits/release-branch.go1.22/
|
||||||
- { goos: windows, goarch: '386', output: '386-go122', goversion: '1.22' }
|
- { goos: windows, goarch: '386', output: '386-go122', goversion: '1.22' }
|
||||||
- { goos: windows, goarch: amd64, goamd64: v1, output: amd64-compatible-go122, goversion: '1.22' }
|
- { goos: windows, goarch: amd64, goamd64: v1, output: amd64-v1-go122, goversion: '1.22' }
|
||||||
- { goos: windows, goarch: amd64, goamd64: v3, output: amd64-go122, goversion: '1.22' }
|
- { goos: windows, goarch: amd64, goamd64: v2, output: amd64-v2-go122, goversion: '1.22' }
|
||||||
|
- { goos: windows, goarch: amd64, goamd64: v3, output: amd64-v3-go122, goversion: '1.22' }
|
||||||
|
|
||||||
# Go 1.21 can revert commit `9e4385` to work on Windows 7
|
# Go 1.21 can revert commit `9e4385` to work on Windows 7
|
||||||
# https://github.com/golang/go/issues/64622#issuecomment-1847475161
|
# https://github.com/golang/go/issues/64622#issuecomment-1847475161
|
||||||
# (OR we can just use golang1.21.4 which unneeded any patch)
|
# (OR we can just use golang1.21.4 which unneeded any patch)
|
||||||
- { goos: windows, goarch: '386', output: '386-go121', goversion: '1.21' }
|
- { goos: windows, goarch: '386', output: '386-go121', goversion: '1.21' }
|
||||||
- { goos: windows, goarch: amd64, goamd64: v1, output: amd64-compatible-go121, goversion: '1.21' }
|
- { goos: windows, goarch: amd64, goamd64: v1, output: amd64-v1-go121, goversion: '1.21' }
|
||||||
- { goos: windows, goarch: amd64, goamd64: v3, output: amd64-go121, goversion: '1.21' }
|
- { goos: windows, goarch: amd64, goamd64: v2, output: amd64-v2-go121, goversion: '1.21' }
|
||||||
|
- { goos: windows, goarch: amd64, goamd64: v3, output: amd64-v3-go121, goversion: '1.21' }
|
||||||
|
|
||||||
# Go 1.20 is the last release that will run on any release of Windows 7, 8, Server 2008 and Server 2012. Go 1.21 will require at least Windows 10 or Server 2016.
|
# Go 1.20 is the last release that will run on any release of Windows 7, 8, Server 2008 and Server 2012. Go 1.21 will require at least Windows 10 or Server 2016.
|
||||||
- { goos: windows, goarch: '386', output: '386-go120', goversion: '1.20' }
|
- { goos: windows, goarch: '386', output: '386-go120', goversion: '1.20' }
|
||||||
- { goos: windows, goarch: amd64, goamd64: v1, output: amd64-compatible-go120, goversion: '1.20' }
|
- { goos: windows, goarch: amd64, goamd64: v1, output: amd64-v1-go120, goversion: '1.20' }
|
||||||
- { goos: windows, goarch: amd64, goamd64: v3, output: amd64-go120, goversion: '1.20' }
|
- { goos: windows, goarch: amd64, goamd64: v2, output: amd64-v2-go120, goversion: '1.20' }
|
||||||
|
- { goos: windows, goarch: amd64, goamd64: v3, output: amd64-v3-go120, goversion: '1.20' }
|
||||||
|
|
||||||
|
# Go 1.24 is the last release that will run on macOS 11 Big Sur. Go 1.25 will require macOS 12 Monterey or later.
|
||||||
|
- { goos: darwin, goarch: arm64, output: arm64-go124, goversion: '1.24' }
|
||||||
|
- { goos: darwin, goarch: amd64, goamd64: v1, output: amd64-v1-go124, goversion: '1.24' }
|
||||||
|
- { goos: darwin, goarch: amd64, goamd64: v2, output: amd64-v2-go124, goversion: '1.24' }
|
||||||
|
- { goos: darwin, goarch: amd64, goamd64: v3, output: amd64-v3-go124, goversion: '1.24' }
|
||||||
|
|
||||||
# Go 1.22 is the last release that will run on macOS 10.15 Catalina. Go 1.23 will require macOS 11 Big Sur or later.
|
# Go 1.22 is the last release that will run on macOS 10.15 Catalina. Go 1.23 will require macOS 11 Big Sur or later.
|
||||||
- { goos: darwin, goarch: arm64, output: arm64-go122, goversion: '1.22' }
|
- { goos: darwin, goarch: arm64, output: arm64-go122, goversion: '1.22' }
|
||||||
- { goos: darwin, goarch: amd64, goamd64: v1, output: amd64-compatible-go122, goversion: '1.22' }
|
- { goos: darwin, goarch: amd64, goamd64: v1, output: amd64-v1-go122, goversion: '1.22' }
|
||||||
- { goos: darwin, goarch: amd64, goamd64: v3, output: amd64-go122, goversion: '1.22' }
|
- { goos: darwin, goarch: amd64, goamd64: v2, output: amd64-v2-go122, goversion: '1.22' }
|
||||||
|
- { goos: darwin, goarch: amd64, goamd64: v3, output: amd64-v3-go122, goversion: '1.22' }
|
||||||
|
|
||||||
# Go 1.20 is the last release that will run on macOS 10.13 High Sierra or 10.14 Mojave. Go 1.21 will require macOS 10.15 Catalina or later.
|
# Go 1.20 is the last release that will run on macOS 10.13 High Sierra or 10.14 Mojave. Go 1.21 will require macOS 10.15 Catalina or later.
|
||||||
- { goos: darwin, goarch: arm64, output: arm64-go120, goversion: '1.20' }
|
- { goos: darwin, goarch: arm64, output: arm64-go120, goversion: '1.20' }
|
||||||
- { goos: darwin, goarch: amd64, goamd64: v1, output: amd64-compatible-go120, goversion: '1.20' }
|
- { goos: darwin, goarch: amd64, goamd64: v1, output: amd64-v1-go120, goversion: '1.20' }
|
||||||
- { goos: darwin, goarch: amd64, goamd64: v3, output: amd64-go120, goversion: '1.20' }
|
- { goos: darwin, goarch: amd64, goamd64: v2, output: amd64-v2-go120, goversion: '1.20' }
|
||||||
|
- { goos: darwin, goarch: amd64, goamd64: v3, output: amd64-v3-go120, goversion: '1.20' }
|
||||||
|
|
||||||
# Go 1.23 is the last release that requires Linux kernel version 2.6.32 or later. Go 1.24 will require Linux kernel version 3.2 or later.
|
# Go 1.23 is the last release that requires Linux kernel version 2.6.32 or later. Go 1.24 will require Linux kernel version 3.2 or later.
|
||||||
- { goos: linux, goarch: '386', output: '386-go123', goversion: '1.23' }
|
- { goos: linux, goarch: '386', output: '386-go123', goversion: '1.23' }
|
||||||
- { goos: linux, goarch: amd64, goamd64: v1, output: amd64-compatible-go123, goversion: '1.23', test: test }
|
- { goos: linux, goarch: amd64, goamd64: v1, output: amd64-v1-go123, goversion: '1.23', test: test }
|
||||||
- { goos: linux, goarch: amd64, goamd64: v3, output: amd64-go123, goversion: '1.23' }
|
- { goos: linux, goarch: amd64, goamd64: v2, output: amd64-v2-go123, goversion: '1.23' }
|
||||||
|
- { goos: linux, goarch: amd64, goamd64: v3, output: amd64-v3-go123, goversion: '1.23' }
|
||||||
|
|
||||||
# only for test
|
# only for test
|
||||||
- { goos: linux, goarch: '386', output: '386-go120', goversion: '1.20' }
|
- { goos: linux, goarch: '386', output: '386-go120', goversion: '1.20' }
|
||||||
- { goos: linux, goarch: amd64, goamd64: v1, output: amd64-compatible-go120, goversion: '1.20', test: test }
|
- { goos: linux, goarch: amd64, goamd64: v1, output: amd64-v1-go120, goversion: '1.20', test: test }
|
||||||
- { goos: linux, goarch: amd64, goamd64: v3, output: amd64-go120, goversion: '1.20' }
|
- { goos: linux, goarch: amd64, goamd64: v2, output: amd64-v2-go120, goversion: '1.20' }
|
||||||
|
- { goos: linux, goarch: amd64, goamd64: v3, output: amd64-v3-go120, goversion: '1.20' }
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v5
|
||||||
|
|
||||||
- name: Set up Go
|
- name: Set up Go
|
||||||
if: ${{ matrix.jobs.goversion == '' && matrix.jobs.abi != '1' }}
|
if: ${{ matrix.jobs.goversion == '' && matrix.jobs.abi != '1' }}
|
||||||
uses: actions/setup-go@v5
|
uses: actions/setup-go@v6
|
||||||
with:
|
with:
|
||||||
go-version: '1.24'
|
go-version: '1.26'
|
||||||
|
check-latest: true # Always check for the latest patch release
|
||||||
|
|
||||||
- name: Set up Go
|
- name: Set up Go
|
||||||
if: ${{ matrix.jobs.goversion != '' && matrix.jobs.abi != '1' }}
|
if: ${{ matrix.jobs.goversion != '' && matrix.jobs.abi != '1' }}
|
||||||
uses: actions/setup-go@v5
|
uses: actions/setup-go@v6
|
||||||
with:
|
with:
|
||||||
go-version: ${{ matrix.jobs.goversion }}
|
go-version: ${{ matrix.jobs.goversion }}
|
||||||
|
check-latest: true # Always check for the latest patch release
|
||||||
|
|
||||||
- name: Set up Go1.23 loongarch abi1
|
- name: Set up Go1.25 loongarch abi1
|
||||||
if: ${{ matrix.jobs.goarch == 'loong64' && matrix.jobs.abi == '1' }}
|
if: ${{ matrix.jobs.goarch == 'loong64' && matrix.jobs.abi == '1' }}
|
||||||
run: |
|
run: |
|
||||||
wget -q https://github.com/MetaCubeX/loongarch64-golang/releases/download/1.23.0/go1.23.0.linux-amd64-abi1.tar.gz
|
wget -q https://github.com/MetaCubeX/loongarch64-golang/releases/download/1.25.5/go1.25.5.linux-amd64-abi1.tar.gz
|
||||||
sudo tar zxf go1.23.0.linux-amd64-abi1.tar.gz -C /usr/local
|
sudo tar zxf go1.25.5.linux-amd64-abi1.tar.gz -C /usr/local
|
||||||
echo "/usr/local/go/bin" >> $GITHUB_PATH
|
echo "/usr/local/go/bin" >> $GITHUB_PATH
|
||||||
|
|
||||||
# modify from https://github.com/restic/restic/issues/4636#issuecomment-1896455557
|
# this patch file only works on golang1.26.x
|
||||||
|
# that means after golang1.27 release it must be changed
|
||||||
|
# see: https://github.com/MetaCubeX/go/commits/release-branch.go1.26/
|
||||||
|
# revert:
|
||||||
|
# 693def151adff1af707d82d28f55dba81ceb08e1: "crypto/rand,runtime: switch RtlGenRandom for ProcessPrng"
|
||||||
|
# 7c1157f9544922e96945196b47b95664b1e39108: "net: remove sysSocket fallback for Windows 7"
|
||||||
|
# 48042aa09c2f878c4faa576948b07fe625c4707a: "syscall: remove Windows 7 console handle workaround"
|
||||||
|
# a17d959debdb04cd550016a3501dd09d50cd62e7: "runtime: always use LoadLibraryEx to load system libraries"
|
||||||
|
# f0894a00f4b756d4b9b4078af2e686b359493583: "os: remove 5ms sleep on Windows in (*Process).Wait"
|
||||||
|
# sepical fix:
|
||||||
|
# - os.RemoveAll not working on Windows7
|
||||||
|
- name: Revert Golang1.26 commit for Windows7/8
|
||||||
|
if: ${{ matrix.jobs.goos == 'windows' && matrix.jobs.goversion == '' }}
|
||||||
|
run: |
|
||||||
|
cd $(go env GOROOT)
|
||||||
|
patch --verbose -p 1 < $GITHUB_WORKSPACE/.github/patch/go1.26.patch
|
||||||
|
|
||||||
|
# this patch file only works on golang1.25.x
|
||||||
|
# that means after golang1.26 release it must be changed
|
||||||
|
# see: https://github.com/MetaCubeX/go/commits/release-branch.go1.25/
|
||||||
|
# revert:
|
||||||
|
# 693def151adff1af707d82d28f55dba81ceb08e1: "crypto/rand,runtime: switch RtlGenRandom for ProcessPrng"
|
||||||
|
# 7c1157f9544922e96945196b47b95664b1e39108: "net: remove sysSocket fallback for Windows 7"
|
||||||
|
# 48042aa09c2f878c4faa576948b07fe625c4707a: "syscall: remove Windows 7 console handle workaround"
|
||||||
|
# a17d959debdb04cd550016a3501dd09d50cd62e7: "runtime: always use LoadLibraryEx to load system libraries"
|
||||||
|
# f0894a00f4b756d4b9b4078af2e686b359493583: "os: remove 5ms sleep on Windows in (*Process).Wait"
|
||||||
|
# sepical fix:
|
||||||
|
# - os.RemoveAll not working on Windows7
|
||||||
|
- name: Revert Golang1.25 commit for Windows7/8
|
||||||
|
if: ${{ matrix.jobs.goos == 'windows' && matrix.jobs.goversion == '1.25' }}
|
||||||
|
run: |
|
||||||
|
cd $(go env GOROOT)
|
||||||
|
patch --verbose -p 1 < $GITHUB_WORKSPACE/.github/patch/go1.25.patch
|
||||||
|
|
||||||
# this patch file only works on golang1.24.x
|
# this patch file only works on golang1.24.x
|
||||||
# that means after golang1.25 release it must be changed
|
# that means after golang1.25 release it must be changed
|
||||||
# see: https://github.com/MetaCubeX/go/commits/release-branch.go1.24/
|
# see: https://github.com/MetaCubeX/go/commits/release-branch.go1.24/
|
||||||
@@ -142,15 +221,11 @@ jobs:
|
|||||||
# 48042aa09c2f878c4faa576948b07fe625c4707a: "syscall: remove Windows 7 console handle workaround"
|
# 48042aa09c2f878c4faa576948b07fe625c4707a: "syscall: remove Windows 7 console handle workaround"
|
||||||
# a17d959debdb04cd550016a3501dd09d50cd62e7: "runtime: always use LoadLibraryEx to load system libraries"
|
# a17d959debdb04cd550016a3501dd09d50cd62e7: "runtime: always use LoadLibraryEx to load system libraries"
|
||||||
- name: Revert Golang1.24 commit for Windows7/8
|
- name: Revert Golang1.24 commit for Windows7/8
|
||||||
if: ${{ matrix.jobs.goos == 'windows' && matrix.jobs.goversion == '' }}
|
if: ${{ matrix.jobs.goos == 'windows' && matrix.jobs.goversion == '1.24' }}
|
||||||
run: |
|
run: |
|
||||||
cd $(go env GOROOT)
|
cd $(go env GOROOT)
|
||||||
curl https://github.com/MetaCubeX/go/commit/2a406dc9f1ea7323d6ca9fccb2fe9ddebb6b1cc8.diff | patch --verbose -p 1
|
patch --verbose -p 1 < $GITHUB_WORKSPACE/.github/patch/go1.24.patch
|
||||||
curl https://github.com/MetaCubeX/go/commit/7b1fd7d39c6be0185fbe1d929578ab372ac5c632.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/979d6d8bab3823ff572ace26767fd2ce3cf351ae.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/ac3e93c061779dfefc0dd13a5b6e6f764a25621e.diff | patch --verbose -p 1
|
|
||||||
|
|
||||||
# modify from https://github.com/restic/restic/issues/4636#issuecomment-1896455557
|
|
||||||
# this patch file only works on golang1.23.x
|
# this patch file only works on golang1.23.x
|
||||||
# that means after golang1.24 release it must be changed
|
# that means after golang1.24 release it must be changed
|
||||||
# see: https://github.com/MetaCubeX/go/commits/release-branch.go1.23/
|
# see: https://github.com/MetaCubeX/go/commits/release-branch.go1.23/
|
||||||
@@ -163,12 +238,8 @@ jobs:
|
|||||||
if: ${{ matrix.jobs.goos == 'windows' && matrix.jobs.goversion == '1.23' }}
|
if: ${{ matrix.jobs.goos == 'windows' && matrix.jobs.goversion == '1.23' }}
|
||||||
run: |
|
run: |
|
||||||
cd $(go env GOROOT)
|
cd $(go env GOROOT)
|
||||||
curl https://github.com/MetaCubeX/go/commit/9ac42137ef6730e8b7daca016ece831297a1d75b.diff | patch --verbose -p 1
|
patch --verbose -p 1 < $GITHUB_WORKSPACE/.github/patch/go1.23.patch
|
||||||
curl https://github.com/MetaCubeX/go/commit/21290de8a4c91408de7c2b5b68757b1e90af49dd.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/6a31d3fa8e47ddabc10bd97bff10d9a85f4cfb76.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/69e2eed6dd0f6d815ebf15797761c13f31213dd6.diff | patch --verbose -p 1
|
|
||||||
|
|
||||||
# modify from https://github.com/restic/restic/issues/4636#issuecomment-1896455557
|
|
||||||
# this patch file only works on golang1.22.x
|
# this patch file only works on golang1.22.x
|
||||||
# that means after golang1.23 release it must be changed
|
# that means after golang1.23 release it must be changed
|
||||||
# see: https://github.com/MetaCubeX/go/commits/release-branch.go1.22/
|
# see: https://github.com/MetaCubeX/go/commits/release-branch.go1.22/
|
||||||
@@ -181,30 +252,27 @@ jobs:
|
|||||||
if: ${{ matrix.jobs.goos == 'windows' && matrix.jobs.goversion == '1.22' }}
|
if: ${{ matrix.jobs.goos == 'windows' && matrix.jobs.goversion == '1.22' }}
|
||||||
run: |
|
run: |
|
||||||
cd $(go env GOROOT)
|
cd $(go env GOROOT)
|
||||||
curl https://github.com/MetaCubeX/go/commit/9779155f18b6556a034f7bb79fb7fb2aad1e26a9.diff | patch --verbose -p 1
|
patch --verbose -p 1 < $GITHUB_WORKSPACE/.github/patch/go1.22.patch
|
||||||
curl https://github.com/MetaCubeX/go/commit/ef0606261340e608017860b423ffae5c1ce78239.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/7f83badcb925a7e743188041cb6e561fc9b5b642.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/83ff9782e024cb328b690cbf0da4e7848a327f4f.diff | patch --verbose -p 1
|
|
||||||
|
|
||||||
# modify from https://github.com/restic/restic/issues/4636#issuecomment-1896455557
|
# modify from https://github.com/restic/restic/issues/4636#issuecomment-1896455557
|
||||||
- name: Revert Golang1.21 commit for Windows7/8
|
- name: Revert Golang1.21 commit for Windows7/8
|
||||||
if: ${{ matrix.jobs.goos == 'windows' && matrix.jobs.goversion == '1.21' }}
|
if: ${{ matrix.jobs.goos == 'windows' && matrix.jobs.goversion == '1.21' }}
|
||||||
run: |
|
run: |
|
||||||
cd $(go env GOROOT)
|
cd $(go env GOROOT)
|
||||||
curl https://github.com/golang/go/commit/9e43850a3298a9b8b1162ba0033d4c53f8637571.diff | patch --verbose -R -p 1
|
patch --verbose -p 1 < $GITHUB_WORKSPACE/.github/patch/go1.21.patch
|
||||||
|
|
||||||
- name: Set variables
|
- name: Set variables
|
||||||
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.version != '' }}
|
|
||||||
run: echo "VERSION=${{ github.event.inputs.version }}" >> $GITHUB_ENV
|
|
||||||
shell: bash
|
|
||||||
|
|
||||||
- name: Set variables
|
|
||||||
if: ${{ github.event_name != 'workflow_dispatch' && github.ref_name == 'Alpha' }}
|
|
||||||
run: echo "VERSION=alpha-$(git rev-parse --short HEAD)" >> $GITHUB_ENV
|
|
||||||
shell: bash
|
|
||||||
|
|
||||||
- name: Set Time Variable
|
|
||||||
run: |
|
run: |
|
||||||
|
VERSION="${GITHUB_REF_NAME,,}-$(git rev-parse --short HEAD)"
|
||||||
|
VERSION="${VERSION//\//-}"
|
||||||
|
PackageVersion="$(curl -s "https://api.github.com/repos/MetaCubeX/mihomo/releases/latest" | jq -r '.tag_name' | sed 's/v//g' | awk -F '.' '{$NF = $NF + 1; print}' OFS='.').${VERSION/-/.}"
|
||||||
|
if [ -n "${{ github.event.inputs.version }}" ]; then
|
||||||
|
VERSION=${{ github.event.inputs.version }}
|
||||||
|
PackageVersion="${VERSION#v}"
|
||||||
|
fi
|
||||||
|
echo "VERSION=${VERSION}" >> $GITHUB_ENV
|
||||||
|
echo "PackageVersion=${PackageVersion}" >> $GITHUB_ENV
|
||||||
|
|
||||||
echo "BUILDTIME=$(date)" >> $GITHUB_ENV
|
echo "BUILDTIME=$(date)" >> $GITHUB_ENV
|
||||||
echo "CGO_ENABLED=0" >> $GITHUB_ENV
|
echo "CGO_ENABLED=0" >> $GITHUB_ENV
|
||||||
echo "BUILDTAG=-extldflags --static" >> $GITHUB_ENV
|
echo "BUILDTAG=-extldflags --static" >> $GITHUB_ENV
|
||||||
@@ -215,7 +283,7 @@ jobs:
|
|||||||
uses: nttld/setup-ndk@v1
|
uses: nttld/setup-ndk@v1
|
||||||
id: setup-ndk
|
id: setup-ndk
|
||||||
with:
|
with:
|
||||||
ndk-version: r28-beta1
|
ndk-version: r29-beta1
|
||||||
|
|
||||||
- name: Set NDK path
|
- name: Set NDK path
|
||||||
if: ${{ matrix.jobs.goos == 'android' }}
|
if: ${{ matrix.jobs.goos == 'android' }}
|
||||||
@@ -233,7 +301,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Update CA
|
- name: Update CA
|
||||||
run: |
|
run: |
|
||||||
sudo apt-get install ca-certificates
|
sudo apt-get update && sudo apt-get install ca-certificates
|
||||||
sudo update-ca-certificates
|
sudo update-ca-certificates
|
||||||
cp -f /etc/ssl/certs/ca-certificates.crt component/ca/ca-certificates.crt
|
cp -f /etc/ssl/certs/ca-certificates.crt component/ca/ca-certificates.crt
|
||||||
|
|
||||||
@@ -242,6 +310,7 @@ jobs:
|
|||||||
GOOS: ${{matrix.jobs.goos}}
|
GOOS: ${{matrix.jobs.goos}}
|
||||||
GOARCH: ${{matrix.jobs.goarch}}
|
GOARCH: ${{matrix.jobs.goarch}}
|
||||||
GOAMD64: ${{matrix.jobs.goamd64}}
|
GOAMD64: ${{matrix.jobs.goamd64}}
|
||||||
|
GO386: ${{matrix.jobs.go386}}
|
||||||
GOARM: ${{matrix.jobs.goarm}}
|
GOARM: ${{matrix.jobs.goarm}}
|
||||||
GOMIPS: ${{matrix.jobs.gomips}}
|
GOMIPS: ${{matrix.jobs.gomips}}
|
||||||
run: |
|
run: |
|
||||||
@@ -256,79 +325,51 @@ jobs:
|
|||||||
rm mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}
|
rm mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}
|
||||||
fi
|
fi
|
||||||
|
|
||||||
- name: Create DEB package
|
- name: Package DEB
|
||||||
if: ${{ matrix.jobs.goos == 'linux' && !contains(matrix.jobs.goarch, 'mips') }}
|
if: matrix.jobs.debian != ''
|
||||||
run: |
|
run: |
|
||||||
sudo apt-get install dpkg
|
set -xeuo pipefail
|
||||||
if [ "${{matrix.jobs.abi}}" = "1" ]; then
|
sudo gem install fpm
|
||||||
ARCH=loongarch64
|
cp .github/release/.fpm_systemd .fpm
|
||||||
elif [ "${{matrix.jobs.goarm}}" = "7" ]; then
|
|
||||||
ARCH=armhf
|
|
||||||
elif [ "${{matrix.jobs.goarch}}" = "arm" ]; then
|
|
||||||
ARCH=armel
|
|
||||||
else
|
|
||||||
ARCH=${{matrix.jobs.goarch}}
|
|
||||||
fi
|
|
||||||
PackageVersion=$(curl -s "https://api.github.com/repos/MetaCubeX/mihomo/releases/latest" | grep -o '"tag_name": "[^"]*' | grep -o '[^"]*$' | sed 's/v//g' )
|
|
||||||
if [ $(git branch | awk -F ' ' '{print $2}') = "Alpha" ]; then
|
|
||||||
PackageVersion="$(echo "${PackageVersion}" | awk -F '.' '{$NF = $NF + 1; print}' OFS='.')-${VERSION}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
mkdir -p mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}-${VERSION}/DEBIAN
|
fpm -t deb \
|
||||||
mkdir -p mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}-${VERSION}/usr/bin
|
-v "${PackageVersion}" \
|
||||||
mkdir -p mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}-${VERSION}/usr/share/licenses/mihomo
|
-p "mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}-${VERSION}.deb" \
|
||||||
mkdir -p mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}-${VERSION}/etc/mihomo
|
--architecture ${{ matrix.jobs.debian }} \
|
||||||
mkdir -p mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}-${VERSION}/lib/systemd/system
|
mihomo=/usr/bin/mihomo
|
||||||
|
|
||||||
cp mihomo mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}-${VERSION}/usr/bin/
|
- name: Package RPM
|
||||||
cp LICENSE mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}-${VERSION}/usr/share/licenses/mihomo/
|
if: matrix.jobs.rpm != ''
|
||||||
cp .github/{mihomo.service,mihomo@.service} mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}-${VERSION}/lib/systemd/system/
|
|
||||||
|
|
||||||
cat > mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}-${VERSION}/etc/mihomo/config.yaml <<EOF
|
|
||||||
mixed-port: 7890
|
|
||||||
EOF
|
|
||||||
|
|
||||||
cat > mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}-${VERSION}/DEBIAN/conffiles <<EOF
|
|
||||||
/etc/mihomo/config.yaml
|
|
||||||
EOF
|
|
||||||
|
|
||||||
cat > mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}-${VERSION}/DEBIAN/control <<EOF
|
|
||||||
Package: mihomo
|
|
||||||
Version: ${PackageVersion}
|
|
||||||
Section:
|
|
||||||
Priority: extra
|
|
||||||
Architecture: ${ARCH}
|
|
||||||
Maintainer: MetaCubeX <none@example.com>
|
|
||||||
Homepage: https://wiki.metacubex.one/
|
|
||||||
Description: The universal proxy platform.
|
|
||||||
EOF
|
|
||||||
|
|
||||||
dpkg-deb -Z gzip --build mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}-${VERSION}
|
|
||||||
|
|
||||||
- name: Convert DEB to RPM
|
|
||||||
if: ${{ matrix.jobs.goos == 'linux' && !contains(matrix.jobs.goarch, 'mips') }}
|
|
||||||
run: |
|
run: |
|
||||||
sudo apt-get install -y alien
|
set -xeuo pipefail
|
||||||
alien --to-rpm --scripts mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}-${VERSION}.deb
|
sudo gem install fpm
|
||||||
mv mihomo*.rpm mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}-${VERSION}.rpm
|
cp .github/release/.fpm_systemd .fpm
|
||||||
|
|
||||||
# - name: Convert DEB to PKG
|
fpm -t rpm \
|
||||||
# if: ${{ matrix.jobs.goos == 'linux' && !contains(matrix.jobs.goarch, 'mips') && !contains(matrix.jobs.goarch, 'loong64') }}
|
-v "${PackageVersion}" \
|
||||||
# run: |
|
-p "mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}-${VERSION}.rpm" \
|
||||||
# docker pull archlinux
|
--architecture ${{ matrix.jobs.rpm }} \
|
||||||
# docker run --rm -v ./:/mnt archlinux bash -c "
|
mihomo=/usr/bin/mihomo
|
||||||
# pacman -Syu pkgfile base-devel --noconfirm
|
|
||||||
# curl -L https://github.com/helixarch/debtap/raw/master/debtap > /usr/bin/debtap
|
- name: Package Pacman
|
||||||
# chmod 755 /usr/bin/debtap
|
if: matrix.jobs.pacman != ''
|
||||||
# debtap -u
|
run: |
|
||||||
# debtap -Q /mnt/mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}-${VERSION}.deb
|
set -xeuo pipefail
|
||||||
# "
|
sudo gem install fpm
|
||||||
# mv mihomo*.pkg.tar.zst mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}-${VERSION}.pkg.tar.zst
|
sudo apt-get update && sudo apt-get install -y libarchive-tools
|
||||||
|
cp .github/release/.fpm_systemd .fpm
|
||||||
|
|
||||||
|
fpm -t pacman \
|
||||||
|
-v "${PackageVersion}" \
|
||||||
|
-p "mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}-${VERSION}.pkg.tar.zst" \
|
||||||
|
--architecture ${{ matrix.jobs.pacman }} \
|
||||||
|
mihomo=/usr/bin/mihomo
|
||||||
|
|
||||||
- name: Save version
|
- name: Save version
|
||||||
run: |
|
run: |
|
||||||
echo ${VERSION} > version.txt
|
echo ${VERSION} > version.txt
|
||||||
shell: bash
|
shell: bash
|
||||||
|
|
||||||
- name: Archive production artifacts
|
- name: Archive production artifacts
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
@@ -337,6 +378,7 @@ jobs:
|
|||||||
mihomo*.gz
|
mihomo*.gz
|
||||||
mihomo*.deb
|
mihomo*.deb
|
||||||
mihomo*.rpm
|
mihomo*.rpm
|
||||||
|
mihomo*.pkg.tar.zst
|
||||||
mihomo*.zip
|
mihomo*.zip
|
||||||
version.txt
|
version.txt
|
||||||
checksums.txt
|
checksums.txt
|
||||||
@@ -406,7 +448,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v5
|
||||||
with:
|
with:
|
||||||
ref: Meta
|
ref: Meta
|
||||||
fetch-depth: '0'
|
fetch-depth: '0'
|
||||||
@@ -465,7 +507,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v5
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
|
|||||||
71
.github/workflows/test.yml
vendored
71
.github/workflows/test.yml
vendored
@@ -22,8 +22,10 @@ jobs:
|
|||||||
- 'windows-latest' # amd64 windows
|
- 'windows-latest' # amd64 windows
|
||||||
- 'macos-latest' # arm64 macos
|
- 'macos-latest' # arm64 macos
|
||||||
- 'ubuntu-24.04-arm' # arm64 linux
|
- 'ubuntu-24.04-arm' # arm64 linux
|
||||||
- 'macos-13' # amd64 macos
|
- 'macos-15-intel' # amd64 macos
|
||||||
go-version:
|
go-version:
|
||||||
|
- '1.26'
|
||||||
|
- '1.25'
|
||||||
- '1.24'
|
- '1.24'
|
||||||
- '1.23'
|
- '1.23'
|
||||||
- '1.22'
|
- '1.22'
|
||||||
@@ -40,73 +42,24 @@ jobs:
|
|||||||
# Fix mingw trying to be smart and converting paths https://github.com/moby/moby/issues/24029#issuecomment-250412919
|
# Fix mingw trying to be smart and converting paths https://github.com/moby/moby/issues/24029#issuecomment-250412919
|
||||||
MSYS_NO_PATHCONV: true
|
MSYS_NO_PATHCONV: true
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v5
|
||||||
|
|
||||||
- name: Setup Go
|
- name: Setup Go
|
||||||
uses: actions/setup-go@v5
|
uses: actions/setup-go@v6
|
||||||
with:
|
with:
|
||||||
go-version: ${{ matrix.go-version }}
|
go-version: ${{ matrix.go-version }}
|
||||||
|
check-latest: true # Always check for the latest patch release
|
||||||
|
|
||||||
# modify from https://github.com/restic/restic/issues/4636#issuecomment-1896455557
|
- name: Revert Golang commit for Windows7/8
|
||||||
# this patch file only works on golang1.24.x
|
if: ${{ runner.os == 'Windows' && matrix.go-version != '1.20' }}
|
||||||
# that means after golang1.25 release it must be changed
|
|
||||||
# see: https://github.com/MetaCubeX/go/commits/release-branch.go1.24/
|
|
||||||
# revert:
|
|
||||||
# 693def151adff1af707d82d28f55dba81ceb08e1: "crypto/rand,runtime: switch RtlGenRandom for ProcessPrng"
|
|
||||||
# 7c1157f9544922e96945196b47b95664b1e39108: "net: remove sysSocket fallback for Windows 7"
|
|
||||||
# 48042aa09c2f878c4faa576948b07fe625c4707a: "syscall: remove Windows 7 console handle workaround"
|
|
||||||
# a17d959debdb04cd550016a3501dd09d50cd62e7: "runtime: always use LoadLibraryEx to load system libraries"
|
|
||||||
- name: Revert Golang1.24 commit for Windows7/8
|
|
||||||
if: ${{ runner.os == 'Windows' && matrix.go-version == '1.24' }}
|
|
||||||
run: |
|
run: |
|
||||||
cd $(go env GOROOT)
|
cd $(go env GOROOT)
|
||||||
curl https://github.com/MetaCubeX/go/commit/2a406dc9f1ea7323d6ca9fccb2fe9ddebb6b1cc8.diff | patch --verbose -p 1
|
patch --verbose -p 1 < $GITHUB_WORKSPACE/.github/patch/go${{matrix.go-version}}.patch
|
||||||
curl https://github.com/MetaCubeX/go/commit/7b1fd7d39c6be0185fbe1d929578ab372ac5c632.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/979d6d8bab3823ff572ace26767fd2ce3cf351ae.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/ac3e93c061779dfefc0dd13a5b6e6f764a25621e.diff | patch --verbose -p 1
|
|
||||||
|
|
||||||
# modify from https://github.com/restic/restic/issues/4636#issuecomment-1896455557
|
- name: Remove inbound test for macOS
|
||||||
# this patch file only works on golang1.23.x
|
if: ${{ runner.os == 'macOS' }}
|
||||||
# that means after golang1.24 release it must be changed
|
|
||||||
# see: https://github.com/MetaCubeX/go/commits/release-branch.go1.23/
|
|
||||||
# revert:
|
|
||||||
# 693def151adff1af707d82d28f55dba81ceb08e1: "crypto/rand,runtime: switch RtlGenRandom for ProcessPrng"
|
|
||||||
# 7c1157f9544922e96945196b47b95664b1e39108: "net: remove sysSocket fallback for Windows 7"
|
|
||||||
# 48042aa09c2f878c4faa576948b07fe625c4707a: "syscall: remove Windows 7 console handle workaround"
|
|
||||||
# a17d959debdb04cd550016a3501dd09d50cd62e7: "runtime: always use LoadLibraryEx to load system libraries"
|
|
||||||
- name: Revert Golang1.23 commit for Windows7/8
|
|
||||||
if: ${{ runner.os == 'Windows' && matrix.go-version == '1.23' }}
|
|
||||||
run: |
|
run: |
|
||||||
cd $(go env GOROOT)
|
rm -rf listener/inbound/*_test.go
|
||||||
curl https://github.com/MetaCubeX/go/commit/9ac42137ef6730e8b7daca016ece831297a1d75b.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/21290de8a4c91408de7c2b5b68757b1e90af49dd.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/6a31d3fa8e47ddabc10bd97bff10d9a85f4cfb76.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/69e2eed6dd0f6d815ebf15797761c13f31213dd6.diff | patch --verbose -p 1
|
|
||||||
|
|
||||||
# modify from https://github.com/restic/restic/issues/4636#issuecomment-1896455557
|
|
||||||
# this patch file only works on golang1.22.x
|
|
||||||
# that means after golang1.23 release it must be changed
|
|
||||||
# see: https://github.com/MetaCubeX/go/commits/release-branch.go1.22/
|
|
||||||
# revert:
|
|
||||||
# 693def151adff1af707d82d28f55dba81ceb08e1: "crypto/rand,runtime: switch RtlGenRandom for ProcessPrng"
|
|
||||||
# 7c1157f9544922e96945196b47b95664b1e39108: "net: remove sysSocket fallback for Windows 7"
|
|
||||||
# 48042aa09c2f878c4faa576948b07fe625c4707a: "syscall: remove Windows 7 console handle workaround"
|
|
||||||
# a17d959debdb04cd550016a3501dd09d50cd62e7: "runtime: always use LoadLibraryEx to load system libraries"
|
|
||||||
- name: Revert Golang1.22 commit for Windows7/8
|
|
||||||
if: ${{ runner.os == 'Windows' && matrix.go-version == '1.22' }}
|
|
||||||
run: |
|
|
||||||
cd $(go env GOROOT)
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/9779155f18b6556a034f7bb79fb7fb2aad1e26a9.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/ef0606261340e608017860b423ffae5c1ce78239.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/7f83badcb925a7e743188041cb6e561fc9b5b642.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/83ff9782e024cb328b690cbf0da4e7848a327f4f.diff | patch --verbose -p 1
|
|
||||||
|
|
||||||
# modify from https://github.com/restic/restic/issues/4636#issuecomment-1896455557
|
|
||||||
- name: Revert Golang1.21 commit for Windows7/8
|
|
||||||
if: ${{ runner.os == 'Windows' && matrix.go-version == '1.21' }}
|
|
||||||
run: |
|
|
||||||
cd $(go env GOROOT)
|
|
||||||
curl https://github.com/golang/go/commit/9e43850a3298a9b8b1162ba0033d4c53f8637571.diff | patch --verbose -R -p 1
|
|
||||||
|
|
||||||
- name: Test
|
- name: Test
|
||||||
run: go test ./... -v -count=1
|
run: go test ./... -v -count=1
|
||||||
|
|||||||
3
.github/workflows/trigger-cmfa-update.yml
vendored
3
.github/workflows/trigger-cmfa-update.yml
vendored
@@ -10,9 +10,6 @@ on:
|
|||||||
- Alpha
|
- Alpha
|
||||||
tags:
|
tags:
|
||||||
- "v*"
|
- "v*"
|
||||||
pull_request_target:
|
|
||||||
branches:
|
|
||||||
- Alpha
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
# Send "core-updated" to MetaCubeX/ClashMetaForAndroid to trigger update-dependencies
|
# Send "core-updated" to MetaCubeX/ClashMetaForAndroid to trigger update-dependencies
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ RUN echo "I'm building for $TARGETPLATFORM"
|
|||||||
|
|
||||||
RUN apk add --no-cache gzip && \
|
RUN apk add --no-cache gzip && \
|
||||||
mkdir /mihomo-config && \
|
mkdir /mihomo-config && \
|
||||||
wget -O /mihomo-config/geoip.metadb https://fastly.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geoip.metadb && \
|
wget -O /mihomo-config/geoip.metadb https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geoip.metadb && \
|
||||||
wget -O /mihomo-config/geosite.dat https://fastly.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geosite.dat && \
|
wget -O /mihomo-config/geosite.dat https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geosite.dat && \
|
||||||
wget -O /mihomo-config/geoip.dat https://fastly.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geoip.dat
|
wget -O /mihomo-config/geoip.dat https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geoip.dat
|
||||||
|
|
||||||
COPY docker/file-name.sh /mihomo/file-name.sh
|
COPY docker/file-name.sh /mihomo/file-name.sh
|
||||||
WORKDIR /mihomo
|
WORKDIR /mihomo
|
||||||
|
|||||||
57
Makefile
57
Makefile
@@ -17,11 +17,19 @@ GOBUILD=CGO_ENABLED=0 go build -tags with_gvisor -trimpath -ldflags '-X "github.
|
|||||||
-w -s -buildid='
|
-w -s -buildid='
|
||||||
|
|
||||||
PLATFORM_LIST = \
|
PLATFORM_LIST = \
|
||||||
|
darwin-386 \
|
||||||
darwin-amd64-compatible \
|
darwin-amd64-compatible \
|
||||||
darwin-amd64 \
|
darwin-amd64 \
|
||||||
|
darwin-amd64-v1 \
|
||||||
|
darwin-amd64-v2 \
|
||||||
|
darwin-amd64-v3 \
|
||||||
darwin-arm64 \
|
darwin-arm64 \
|
||||||
|
linux-386 \
|
||||||
linux-amd64-compatible \
|
linux-amd64-compatible \
|
||||||
linux-amd64 \
|
linux-amd64 \
|
||||||
|
linux-amd64-v1 \
|
||||||
|
linux-amd64-v2 \
|
||||||
|
linux-amd64-v3 \
|
||||||
linux-armv5 \
|
linux-armv5 \
|
||||||
linux-armv6 \
|
linux-armv6 \
|
||||||
linux-armv7 \
|
linux-armv7 \
|
||||||
@@ -43,37 +51,61 @@ WINDOWS_ARCH_LIST = \
|
|||||||
windows-386 \
|
windows-386 \
|
||||||
windows-amd64-compatible \
|
windows-amd64-compatible \
|
||||||
windows-amd64 \
|
windows-amd64 \
|
||||||
|
windows-amd64-v1 \
|
||||||
|
windows-amd64-v2 \
|
||||||
|
windows-amd64-v3 \
|
||||||
windows-arm64 \
|
windows-arm64 \
|
||||||
windows-arm32v7
|
windows-arm32v7
|
||||||
|
|
||||||
all:linux-amd64 linux-arm64\
|
all:linux-amd64-v3 linux-arm64\
|
||||||
darwin-amd64 darwin-arm64\
|
darwin-amd64-v3 darwin-arm64\
|
||||||
windows-amd64 windows-arm64\
|
windows-amd64-v3 windows-arm64\
|
||||||
|
|
||||||
|
|
||||||
darwin-all: darwin-amd64 darwin-arm64
|
darwin-all: darwin-amd64-v3 darwin-arm64
|
||||||
|
|
||||||
docker:
|
docker:
|
||||||
GOAMD64=v1 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
GOAMD64=v1 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||||
|
|
||||||
darwin-amd64:
|
darwin-386:
|
||||||
GOARCH=amd64 GOOS=darwin GOAMD64=v3 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
GOARCH=386 GOOS=darwin $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||||
|
|
||||||
darwin-amd64-compatible:
|
darwin-amd64-compatible:
|
||||||
GOARCH=amd64 GOOS=darwin GOAMD64=v1 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
GOARCH=amd64 GOOS=darwin GOAMD64=v1 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||||
|
|
||||||
|
darwin-amd64:
|
||||||
|
GOARCH=amd64 GOOS=darwin GOAMD64=v3 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||||
|
|
||||||
|
darwin-amd64-v1:
|
||||||
|
GOARCH=amd64 GOOS=darwin GOAMD64=v1 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||||
|
|
||||||
|
darwin-amd64-v2:
|
||||||
|
GOARCH=amd64 GOOS=darwin GOAMD64=v2 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||||
|
|
||||||
|
darwin-amd64-v3:
|
||||||
|
GOARCH=amd64 GOOS=darwin GOAMD64=v3 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||||
|
|
||||||
darwin-arm64:
|
darwin-arm64:
|
||||||
GOARCH=arm64 GOOS=darwin $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
GOARCH=arm64 GOOS=darwin $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||||
|
|
||||||
linux-386:
|
linux-386:
|
||||||
GOARCH=386 GOOS=linux $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
GOARCH=386 GOOS=linux $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||||
|
|
||||||
|
linux-amd64-compatible:
|
||||||
|
GOARCH=amd64 GOOS=linux GOAMD64=v1 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||||
|
|
||||||
linux-amd64:
|
linux-amd64:
|
||||||
GOARCH=amd64 GOOS=linux GOAMD64=v3 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
GOARCH=amd64 GOOS=linux GOAMD64=v3 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||||
|
|
||||||
linux-amd64-compatible:
|
linux-amd64-v1:
|
||||||
GOARCH=amd64 GOOS=linux GOAMD64=v1 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
GOARCH=amd64 GOOS=linux GOAMD64=v1 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||||
|
|
||||||
|
linux-amd64-v2:
|
||||||
|
GOARCH=amd64 GOOS=linux GOAMD64=v2 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||||
|
|
||||||
|
linux-amd64-v3:
|
||||||
|
GOARCH=amd64 GOOS=linux GOAMD64=v3 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||||
|
|
||||||
linux-arm64:
|
linux-arm64:
|
||||||
GOARCH=arm64 GOOS=linux $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
GOARCH=arm64 GOOS=linux $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||||
|
|
||||||
@@ -125,12 +157,21 @@ freebsd-arm64:
|
|||||||
windows-386:
|
windows-386:
|
||||||
GOARCH=386 GOOS=windows $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe
|
GOARCH=386 GOOS=windows $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe
|
||||||
|
|
||||||
|
windows-amd64-compatible:
|
||||||
|
GOARCH=amd64 GOOS=windows GOAMD64=v1 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe
|
||||||
|
|
||||||
windows-amd64:
|
windows-amd64:
|
||||||
GOARCH=amd64 GOOS=windows GOAMD64=v3 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe
|
GOARCH=amd64 GOOS=windows GOAMD64=v3 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe
|
||||||
|
|
||||||
windows-amd64-compatible:
|
windows-amd64-v1:
|
||||||
GOARCH=amd64 GOOS=windows GOAMD64=v1 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe
|
GOARCH=amd64 GOOS=windows GOAMD64=v1 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe
|
||||||
|
|
||||||
|
windows-amd64-v2:
|
||||||
|
GOARCH=amd64 GOOS=windows GOAMD64=v2 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe
|
||||||
|
|
||||||
|
windows-amd64-v3:
|
||||||
|
GOARCH=amd64 GOOS=windows GOAMD64=v3 $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe
|
||||||
|
|
||||||
windows-arm64:
|
windows-arm64:
|
||||||
GOARCH=arm64 GOOS=windows $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe
|
GOARCH=arm64 GOOS=windows $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe
|
||||||
|
|
||||||
|
|||||||
@@ -2,25 +2,22 @@ package adapter
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
|
||||||
"net/netip"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/atomic"
|
"github.com/metacubex/mihomo/common/atomic"
|
||||||
"github.com/metacubex/mihomo/common/queue"
|
"github.com/metacubex/mihomo/common/queue"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
|
"github.com/metacubex/mihomo/common/xsync"
|
||||||
"github.com/metacubex/mihomo/component/ca"
|
"github.com/metacubex/mihomo/component/ca"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/log"
|
"github.com/metacubex/mihomo/log"
|
||||||
"github.com/puzpuzpuz/xsync/v3"
|
|
||||||
|
"github.com/metacubex/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
var UnifiedDelay = atomic.NewBool(false)
|
var UnifiedDelay = atomic.NewBool(false)
|
||||||
@@ -38,7 +35,7 @@ type Proxy struct {
|
|||||||
C.ProxyAdapter
|
C.ProxyAdapter
|
||||||
alive atomic.Bool
|
alive atomic.Bool
|
||||||
history *queue.Queue[C.DelayHistory]
|
history *queue.Queue[C.DelayHistory]
|
||||||
extra *xsync.MapOf[string, *internalProxyState]
|
extra xsync.Map[string, *internalProxyState]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adapter implements C.Proxy
|
// Adapter implements C.Proxy
|
||||||
@@ -55,29 +52,15 @@ func (p *Proxy) AliveForTestUrl(url string) bool {
|
|||||||
return p.alive.Load()
|
return p.alive.Load()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dial implements C.Proxy
|
|
||||||
func (p *Proxy) Dial(metadata *C.Metadata) (C.Conn, error) {
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTCPTimeout)
|
|
||||||
defer cancel()
|
|
||||||
return p.DialContext(ctx, metadata)
|
|
||||||
}
|
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (p *Proxy) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
func (p *Proxy) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
||||||
conn, err := p.ProxyAdapter.DialContext(ctx, metadata, opts...)
|
conn, err := p.ProxyAdapter.DialContext(ctx, metadata)
|
||||||
return conn, err
|
return conn, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialUDP implements C.ProxyAdapter
|
|
||||||
func (p *Proxy) DialUDP(metadata *C.Metadata) (C.PacketConn, error) {
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), C.DefaultUDPTimeout)
|
|
||||||
defer cancel()
|
|
||||||
return p.ListenPacketContext(ctx, metadata)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (p *Proxy) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
func (p *Proxy) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
||||||
pc, err := p.ProxyAdapter.ListenPacketContext(ctx, metadata, opts...)
|
pc, err := p.ProxyAdapter.ListenPacketContext(ctx, metadata)
|
||||||
return pc, err
|
return pc, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,8 +154,9 @@ func (p *Proxy) MarshalJSON() ([]byte, error) {
|
|||||||
mapping["mptcp"] = proxyInfo.MPTCP
|
mapping["mptcp"] = proxyInfo.MPTCP
|
||||||
mapping["smux"] = proxyInfo.SMUX
|
mapping["smux"] = proxyInfo.SMUX
|
||||||
mapping["interface"] = proxyInfo.Interface
|
mapping["interface"] = proxyInfo.Interface
|
||||||
mapping["dialer-proxy"] = proxyInfo.DialerProxy
|
|
||||||
mapping["routing-mark"] = proxyInfo.RoutingMark
|
mapping["routing-mark"] = proxyInfo.RoutingMark
|
||||||
|
mapping["provider-name"] = proxyInfo.ProviderName
|
||||||
|
mapping["dialer-proxy"] = proxyInfo.DialerProxy
|
||||||
|
|
||||||
return json.Marshal(mapping)
|
return json.Marshal(mapping)
|
||||||
}
|
}
|
||||||
@@ -195,14 +179,12 @@ func (p *Proxy) URLTest(ctx context.Context, url string, expectedStatus utils.In
|
|||||||
p.history.Pop()
|
p.history.Pop()
|
||||||
}
|
}
|
||||||
|
|
||||||
state, ok := p.extra.Load(url)
|
state, _ := p.extra.LoadOrStoreFn(url, func() *internalProxyState {
|
||||||
if !ok {
|
return &internalProxyState{
|
||||||
state = &internalProxyState{
|
|
||||||
history: queue.New[C.DelayHistory](defaultHistoriesNum),
|
history: queue.New[C.DelayHistory](defaultHistoriesNum),
|
||||||
alive: atomic.NewBool(true),
|
alive: atomic.NewBool(true),
|
||||||
}
|
}
|
||||||
p.extra.Store(url, state)
|
})
|
||||||
}
|
|
||||||
|
|
||||||
if !satisfied {
|
if !satisfied {
|
||||||
record.Delay = 0
|
record.Delay = 0
|
||||||
@@ -239,6 +221,11 @@ func (p *Proxy) URLTest(ctx context.Context, url string, expectedStatus utils.In
|
|||||||
}
|
}
|
||||||
req = req.WithContext(ctx)
|
req = req.WithContext(ctx)
|
||||||
|
|
||||||
|
tlsConfig, err := ca.GetTLSConfig(ca.Option{})
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
transport := &http.Transport{
|
transport := &http.Transport{
|
||||||
DialContext: func(context.Context, string, string) (net.Conn, error) {
|
DialContext: func(context.Context, string, string) (net.Conn, error) {
|
||||||
return instance, nil
|
return instance, nil
|
||||||
@@ -248,7 +235,7 @@ func (p *Proxy) URLTest(ctx context.Context, url string, expectedStatus utils.In
|
|||||||
IdleConnTimeout: 90 * time.Second,
|
IdleConnTimeout: 90 * time.Second,
|
||||||
TLSHandshakeTimeout: 10 * time.Second,
|
TLSHandshakeTimeout: 10 * time.Second,
|
||||||
ExpectContinueTimeout: 1 * time.Second,
|
ExpectContinueTimeout: 1 * time.Second,
|
||||||
TLSClientConfig: ca.GetGlobalTLSConfig(&tls.Config{}),
|
TLSClientConfig: tlsConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
client := http.Client{
|
client := http.Client{
|
||||||
@@ -296,7 +283,7 @@ func NewProxy(adapter C.ProxyAdapter) *Proxy {
|
|||||||
ProxyAdapter: adapter,
|
ProxyAdapter: adapter,
|
||||||
history: queue.New[C.DelayHistory](defaultHistoriesNum),
|
history: queue.New[C.DelayHistory](defaultHistoriesNum),
|
||||||
alive: atomic.NewBool(true),
|
alive: atomic.NewBool(true),
|
||||||
extra: xsync.NewMapOf[string, *internalProxyState]()}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func urlToMetadata(rawURL string) (addr C.Metadata, err error) {
|
func urlToMetadata(rawURL string) (addr C.Metadata, err error) {
|
||||||
@@ -317,15 +304,7 @@ func urlToMetadata(rawURL string) (addr C.Metadata, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
uintPort, err := strconv.ParseUint(port, 10, 16)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
addr = C.Metadata{
|
err = addr.SetRemoteAddress(net.JoinHostPort(u.Hostname(), port))
|
||||||
Host: u.Hostname(),
|
|
||||||
DstIP: netip.Addr{},
|
|
||||||
DstPort: uint16(uintPort),
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,10 @@ package inbound
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
|
||||||
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
|
|
||||||
|
"github.com/metacubex/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewHTTPS receive CONNECT request and return ConnContext
|
// NewHTTPS receive CONNECT request and return ConnContext
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/component/keepalive"
|
"github.com/metacubex/mihomo/component/keepalive"
|
||||||
|
"github.com/metacubex/mihomo/component/mptcp"
|
||||||
|
|
||||||
"github.com/metacubex/tfo-go"
|
"github.com/metacubex/tfo-go"
|
||||||
)
|
)
|
||||||
@@ -34,13 +35,13 @@ func Tfo() bool {
|
|||||||
func SetMPTCP(open bool) {
|
func SetMPTCP(open bool) {
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
defer mutex.Unlock()
|
defer mutex.Unlock()
|
||||||
setMultiPathTCP(&lc.ListenConfig, open)
|
mptcp.SetNetListenConfig(&lc.ListenConfig, open)
|
||||||
}
|
}
|
||||||
|
|
||||||
func MPTCP() bool {
|
func MPTCP() bool {
|
||||||
mutex.RLock()
|
mutex.RLock()
|
||||||
defer mutex.RUnlock()
|
defer mutex.RUnlock()
|
||||||
return getMultiPathTCP(&lc.ListenConfig)
|
return mptcp.GetNetListenConfig(&lc.ListenConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
func preResolve(network, address string) (string, error) {
|
func preResolve(network, address string) (string, error) {
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
//go:build !go1.21
|
|
||||||
|
|
||||||
package inbound
|
|
||||||
|
|
||||||
import "net"
|
|
||||||
|
|
||||||
const multipathTCPAvailable = false
|
|
||||||
|
|
||||||
func setMultiPathTCP(listenConfig *net.ListenConfig, open bool) {
|
|
||||||
}
|
|
||||||
|
|
||||||
func getMultiPathTCP(listenConfig *net.ListenConfig) bool {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
//go:build go1.21
|
|
||||||
|
|
||||||
package inbound
|
|
||||||
|
|
||||||
import "net"
|
|
||||||
|
|
||||||
const multipathTCPAvailable = true
|
|
||||||
|
|
||||||
func setMultiPathTCP(listenConfig *net.ListenConfig, open bool) {
|
|
||||||
listenConfig.SetMultipathTCP(open)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getMultiPathTCP(listenConfig *net.ListenConfig) bool {
|
|
||||||
return listenConfig.MultipathTCP()
|
|
||||||
}
|
|
||||||
@@ -2,13 +2,13 @@ package inbound
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/transport/socks5"
|
"github.com/metacubex/mihomo/transport/socks5"
|
||||||
|
|
||||||
|
"github.com/metacubex/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func parseSocksAddr(target socks5.Addr) *C.Metadata {
|
func parseSocksAddr(target socks5.Addr) *C.Metadata {
|
||||||
@@ -41,23 +41,8 @@ func parseHTTPAddr(request *http.Request) *C.Metadata {
|
|||||||
// trim FQDN (#737)
|
// trim FQDN (#737)
|
||||||
host = strings.TrimRight(host, ".")
|
host = strings.TrimRight(host, ".")
|
||||||
|
|
||||||
var uint16Port uint16
|
metadata := &C.Metadata{}
|
||||||
if port, err := strconv.ParseUint(port, 10, 16); err == nil {
|
_ = metadata.SetRemoteAddress(net.JoinHostPort(host, port))
|
||||||
uint16Port = uint16(port)
|
|
||||||
}
|
|
||||||
|
|
||||||
metadata := &C.Metadata{
|
|
||||||
NetWork: C.TCP,
|
|
||||||
Host: host,
|
|
||||||
DstIP: netip.Addr{},
|
|
||||||
DstPort: uint16Port,
|
|
||||||
}
|
|
||||||
|
|
||||||
ip, err := netip.ParseAddr(host)
|
|
||||||
if err == nil {
|
|
||||||
metadata.DstIP = ip
|
|
||||||
}
|
|
||||||
|
|
||||||
return metadata
|
return metadata
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,50 +2,47 @@ package outbound
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
CN "github.com/metacubex/mihomo/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
"github.com/metacubex/mihomo/component/proxydialer"
|
||||||
"github.com/metacubex/mihomo/component/resolver"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/transport/anytls"
|
"github.com/metacubex/mihomo/transport/anytls"
|
||||||
"github.com/metacubex/mihomo/transport/vmess"
|
"github.com/metacubex/mihomo/transport/vmess"
|
||||||
|
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/metacubex/sing/common/metadata"
|
||||||
"github.com/sagernet/sing/common/uot"
|
"github.com/metacubex/sing/common/uot"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AnyTLS struct {
|
type AnyTLS struct {
|
||||||
*Base
|
*Base
|
||||||
client *anytls.Client
|
client *anytls.Client
|
||||||
dialer proxydialer.SingDialer
|
|
||||||
option *AnyTLSOption
|
option *AnyTLSOption
|
||||||
}
|
}
|
||||||
|
|
||||||
type AnyTLSOption struct {
|
type AnyTLSOption struct {
|
||||||
BasicOption
|
BasicOption
|
||||||
Name string `proxy:"name"`
|
Name string `proxy:"name"`
|
||||||
Server string `proxy:"server"`
|
Server string `proxy:"server"`
|
||||||
Port int `proxy:"port"`
|
Port int `proxy:"port"`
|
||||||
Password string `proxy:"password"`
|
Password string `proxy:"password"`
|
||||||
ALPN []string `proxy:"alpn,omitempty"`
|
ALPN []string `proxy:"alpn,omitempty"`
|
||||||
SNI string `proxy:"sni,omitempty"`
|
SNI string `proxy:"sni,omitempty"`
|
||||||
ClientFingerprint string `proxy:"client-fingerprint,omitempty"`
|
ECHOpts ECHOptions `proxy:"ech-opts,omitempty"`
|
||||||
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
ClientFingerprint string `proxy:"client-fingerprint,omitempty"`
|
||||||
Fingerprint string `proxy:"fingerprint,omitempty"`
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
||||||
UDP bool `proxy:"udp,omitempty"`
|
Fingerprint string `proxy:"fingerprint,omitempty"`
|
||||||
IdleSessionCheckInterval int `proxy:"idle-session-check-interval,omitempty"`
|
Certificate string `proxy:"certificate,omitempty"`
|
||||||
IdleSessionTimeout int `proxy:"idle-session-timeout,omitempty"`
|
PrivateKey string `proxy:"private-key,omitempty"`
|
||||||
MinIdleSession int `proxy:"min-idle-session,omitempty"`
|
UDP bool `proxy:"udp,omitempty"`
|
||||||
|
IdleSessionCheckInterval int `proxy:"idle-session-check-interval,omitempty"`
|
||||||
|
IdleSessionTimeout int `proxy:"idle-session-timeout,omitempty"`
|
||||||
|
MinIdleSession int `proxy:"min-idle-session,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *AnyTLS) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
func (t *AnyTLS) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
options := t.Base.DialOptions(opts...)
|
|
||||||
t.dialer.SetDialer(dialer.NewDialer(options...))
|
|
||||||
c, err := t.client.CreateProxy(ctx, M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort))
|
c, err := t.client.CreateProxy(ctx, M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -53,25 +50,20 @@ func (t *AnyTLS) DialContext(ctx context.Context, metadata *C.Metadata, opts ...
|
|||||||
return NewConn(c, t), nil
|
return NewConn(c, t), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *AnyTLS) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
func (t *AnyTLS) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
|
if err = t.ResolveUDP(ctx, metadata); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
// create tcp
|
// create tcp
|
||||||
options := t.Base.DialOptions(opts...)
|
|
||||||
t.dialer.SetDialer(dialer.NewDialer(options...))
|
|
||||||
c, err := t.client.CreateProxy(ctx, uot.RequestDestination(2))
|
c, err := t.client.CreateProxy(ctx, uot.RequestDestination(2))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// create uot on tcp
|
// create uot on tcp
|
||||||
if !metadata.Resolved() {
|
|
||||||
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("can't resolve ip")
|
|
||||||
}
|
|
||||||
metadata.DstIP = ip
|
|
||||||
}
|
|
||||||
destination := M.SocksaddrFromNet(metadata.UDPAddr())
|
destination := M.SocksaddrFromNet(metadata.UDPAddr())
|
||||||
return newPacketConn(CN.NewThreadSafePacketConn(uot.NewLazyConn(c, uot.Request{Destination: destination})), t), nil
|
return newPacketConn(N.NewThreadSafePacketConn(uot.NewLazyConn(c, uot.Request{Destination: destination})), t), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SupportUOT implements C.ProxyAdapter
|
// SupportUOT implements C.ProxyAdapter
|
||||||
@@ -93,8 +85,23 @@ func (t *AnyTLS) Close() error {
|
|||||||
|
|
||||||
func NewAnyTLS(option AnyTLSOption) (*AnyTLS, error) {
|
func NewAnyTLS(option AnyTLSOption) (*AnyTLS, error) {
|
||||||
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
||||||
|
outbound := &AnyTLS{
|
||||||
singDialer := proxydialer.NewByNameSingDialer(option.DialerProxy, dialer.NewDialer())
|
Base: &Base{
|
||||||
|
name: option.Name,
|
||||||
|
addr: addr,
|
||||||
|
tp: C.AnyTLS,
|
||||||
|
pdName: option.ProviderName,
|
||||||
|
udp: option.UDP,
|
||||||
|
tfo: option.TFO,
|
||||||
|
mpTcp: option.MPTCP,
|
||||||
|
iface: option.Interface,
|
||||||
|
rmark: option.RoutingMark,
|
||||||
|
prefer: option.IPVersion,
|
||||||
|
},
|
||||||
|
option: &option,
|
||||||
|
}
|
||||||
|
outbound.dialer = option.NewDialer(outbound.DialOptions())
|
||||||
|
singDialer := proxydialer.NewSingDialer(outbound.dialer)
|
||||||
|
|
||||||
tOption := anytls.ClientConfig{
|
tOption := anytls.ClientConfig{
|
||||||
Password: option.Password,
|
Password: option.Password,
|
||||||
@@ -104,34 +111,27 @@ func NewAnyTLS(option AnyTLSOption) (*AnyTLS, error) {
|
|||||||
IdleSessionTimeout: time.Duration(option.IdleSessionTimeout) * time.Second,
|
IdleSessionTimeout: time.Duration(option.IdleSessionTimeout) * time.Second,
|
||||||
MinIdleSession: option.MinIdleSession,
|
MinIdleSession: option.MinIdleSession,
|
||||||
}
|
}
|
||||||
|
echConfig, err := option.ECHOpts.Parse()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
tlsConfig := &vmess.TLSConfig{
|
tlsConfig := &vmess.TLSConfig{
|
||||||
Host: option.SNI,
|
Host: option.SNI,
|
||||||
SkipCertVerify: option.SkipCertVerify,
|
SkipCertVerify: option.SkipCertVerify,
|
||||||
NextProtos: option.ALPN,
|
NextProtos: option.ALPN,
|
||||||
FingerPrint: option.Fingerprint,
|
FingerPrint: option.Fingerprint,
|
||||||
|
Certificate: option.Certificate,
|
||||||
|
PrivateKey: option.PrivateKey,
|
||||||
ClientFingerprint: option.ClientFingerprint,
|
ClientFingerprint: option.ClientFingerprint,
|
||||||
|
ECH: echConfig,
|
||||||
}
|
}
|
||||||
if tlsConfig.Host == "" {
|
if tlsConfig.Host == "" {
|
||||||
tlsConfig.Host = option.Server
|
tlsConfig.Host = option.Server
|
||||||
}
|
}
|
||||||
tOption.TLSConfig = tlsConfig
|
tOption.TLSConfig = tlsConfig
|
||||||
|
|
||||||
outbound := &AnyTLS{
|
client := anytls.NewClient(context.TODO(), tOption)
|
||||||
Base: &Base{
|
outbound.client = client
|
||||||
name: option.Name,
|
|
||||||
addr: addr,
|
|
||||||
tp: C.AnyTLS,
|
|
||||||
udp: option.UDP,
|
|
||||||
tfo: option.TFO,
|
|
||||||
mpTcp: option.MPTCP,
|
|
||||||
iface: option.Interface,
|
|
||||||
rmark: option.RoutingMark,
|
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
|
||||||
},
|
|
||||||
client: anytls.NewClient(context.TODO(), tOption),
|
|
||||||
option: &option,
|
|
||||||
dialer: singDialer,
|
|
||||||
}
|
|
||||||
|
|
||||||
return outbound, nil
|
return outbound, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,36 +3,41 @@ package outbound
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
|
"github.com/metacubex/mihomo/component/proxydialer"
|
||||||
|
"github.com/metacubex/mihomo/component/resolver"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/log"
|
"github.com/metacubex/mihomo/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ProxyAdapter interface {
|
type ProxyAdapter interface {
|
||||||
C.ProxyAdapter
|
C.ProxyAdapter
|
||||||
DialOptions(opts ...dialer.Option) []dialer.Option
|
DialOptions() []dialer.Option
|
||||||
|
ResolveUDP(ctx context.Context, metadata *C.Metadata) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type Base struct {
|
type Base struct {
|
||||||
name string
|
name string
|
||||||
addr string
|
addr string
|
||||||
iface string
|
|
||||||
tp C.AdapterType
|
tp C.AdapterType
|
||||||
|
pdName string
|
||||||
udp bool
|
udp bool
|
||||||
xudp bool
|
xudp bool
|
||||||
tfo bool
|
tfo bool
|
||||||
mpTcp bool
|
mpTcp bool
|
||||||
|
iface string
|
||||||
rmark int
|
rmark int
|
||||||
id string
|
|
||||||
prefer C.DNSPrefer
|
prefer C.DNSPrefer
|
||||||
|
dialer C.Dialer
|
||||||
|
id string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name implements C.ProxyAdapter
|
// Name implements C.ProxyAdapter
|
||||||
@@ -54,35 +59,15 @@ func (b *Base) Type() C.AdapterType {
|
|||||||
return b.tp
|
return b.tp
|
||||||
}
|
}
|
||||||
|
|
||||||
// StreamConnContext implements C.ProxyAdapter
|
func (b *Base) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
||||||
func (b *Base) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
|
||||||
return c, C.ErrNotSupport
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Base) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
|
||||||
return nil, C.ErrNotSupport
|
|
||||||
}
|
|
||||||
|
|
||||||
// DialContextWithDialer implements C.ProxyAdapter
|
|
||||||
func (b *Base) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
|
||||||
return nil, C.ErrNotSupport
|
return nil, C.ErrNotSupport
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (b *Base) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
func (b *Base) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
||||||
return nil, C.ErrNotSupport
|
return nil, C.ErrNotSupport
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketWithDialer implements C.ProxyAdapter
|
|
||||||
func (b *Base) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
|
||||||
return nil, C.ErrNotSupport
|
|
||||||
}
|
|
||||||
|
|
||||||
// SupportWithDialer implements C.ProxyAdapter
|
|
||||||
func (b *Base) SupportWithDialer() C.NetWork {
|
|
||||||
return C.InvalidNet
|
|
||||||
}
|
|
||||||
|
|
||||||
// SupportUOT implements C.ProxyAdapter
|
// SupportUOT implements C.ProxyAdapter
|
||||||
func (b *Base) SupportUOT() bool {
|
func (b *Base) SupportUOT() bool {
|
||||||
return false
|
return false
|
||||||
@@ -101,6 +86,7 @@ func (b *Base) ProxyInfo() (info C.ProxyInfo) {
|
|||||||
info.SMUX = false
|
info.SMUX = false
|
||||||
info.Interface = b.iface
|
info.Interface = b.iface
|
||||||
info.RoutingMark = b.rmark
|
info.RoutingMark = b.rmark
|
||||||
|
info.ProviderName = b.pdName
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,7 +114,7 @@ func (b *Base) Unwrap(metadata *C.Metadata, touch bool) C.Proxy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialOptions return []dialer.Option from struct
|
// DialOptions return []dialer.Option from struct
|
||||||
func (b *Base) DialOptions(opts ...dialer.Option) []dialer.Option {
|
func (b *Base) DialOptions() (opts []dialer.Option) {
|
||||||
if b.iface != "" {
|
if b.iface != "" {
|
||||||
opts = append(opts, dialer.WithInterface(b.iface))
|
opts = append(opts, dialer.WithInterface(b.iface))
|
||||||
}
|
}
|
||||||
@@ -160,17 +146,46 @@ func (b *Base) DialOptions(opts ...dialer.Option) []dialer.Option {
|
|||||||
return opts
|
return opts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Base) ResolveUDP(ctx context.Context, metadata *C.Metadata) error {
|
||||||
|
if !metadata.Resolved() {
|
||||||
|
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("can't resolve ip: %w", err)
|
||||||
|
}
|
||||||
|
metadata.DstIP = ip
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Base) Close() error {
|
func (b *Base) Close() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type BasicOption struct {
|
type BasicOption struct {
|
||||||
TFO bool `proxy:"tfo,omitempty"`
|
TFO bool `proxy:"tfo,omitempty"`
|
||||||
MPTCP bool `proxy:"mptcp,omitempty"`
|
MPTCP bool `proxy:"mptcp,omitempty"`
|
||||||
Interface string `proxy:"interface-name,omitempty" group:"interface-name,omitempty"`
|
Interface string `proxy:"interface-name,omitempty"`
|
||||||
RoutingMark int `proxy:"routing-mark,omitempty" group:"routing-mark,omitempty"`
|
RoutingMark int `proxy:"routing-mark,omitempty"`
|
||||||
IPVersion string `proxy:"ip-version,omitempty"`
|
IPVersion C.DNSPrefer `proxy:"ip-version,omitempty"`
|
||||||
DialerProxy string `proxy:"dialer-proxy,omitempty"` // don't apply this option into groups, but can set a group name in a proxy
|
DialerProxy string `proxy:"dialer-proxy,omitempty"` // don't apply this option into groups, but can set a group name in a proxy
|
||||||
|
|
||||||
|
//
|
||||||
|
// The following parameters are used internally, assign value by the structure decoder are disallowed
|
||||||
|
//
|
||||||
|
DialerForAPI C.Dialer `proxy:"-"` // the dialer used for API usage has higher priority than all the above configurations.
|
||||||
|
ProviderName string `proxy:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *BasicOption) NewDialer(opts []dialer.Option) C.Dialer {
|
||||||
|
cDialer := b.DialerForAPI
|
||||||
|
if cDialer == nil {
|
||||||
|
if b.DialerProxy != "" {
|
||||||
|
cDialer = proxydialer.NewByName(b.DialerProxy)
|
||||||
|
} else {
|
||||||
|
cDialer = dialer.NewDialer(opts...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cDialer
|
||||||
}
|
}
|
||||||
|
|
||||||
type BaseOption struct {
|
type BaseOption struct {
|
||||||
@@ -203,12 +218,22 @@ func NewBase(opt BaseOption) *Base {
|
|||||||
|
|
||||||
type conn struct {
|
type conn struct {
|
||||||
N.ExtendedConn
|
N.ExtendedConn
|
||||||
chain C.Chain
|
chain C.Chain
|
||||||
actualRemoteDestination string
|
pdChain C.Chain
|
||||||
|
adapterAddr string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *conn) RemoteDestination() string {
|
func (c *conn) RemoteDestination() string {
|
||||||
return c.actualRemoteDestination
|
if remoteAddr := c.RemoteAddr(); remoteAddr != nil {
|
||||||
|
m := C.Metadata{}
|
||||||
|
if err := m.SetRemoteAddr(remoteAddr); err == nil {
|
||||||
|
if m.Valid() {
|
||||||
|
return m.String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
host, _, _ := net.SplitHostPort(c.adapterAddr)
|
||||||
|
return host
|
||||||
}
|
}
|
||||||
|
|
||||||
// Chains implements C.Connection
|
// Chains implements C.Connection
|
||||||
@@ -216,9 +241,15 @@ func (c *conn) Chains() C.Chain {
|
|||||||
return c.chain
|
return c.chain
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ProviderChains implements C.Connection
|
||||||
|
func (c *conn) ProviderChains() C.Chain {
|
||||||
|
return c.pdChain
|
||||||
|
}
|
||||||
|
|
||||||
// AppendToChains implements C.Connection
|
// AppendToChains implements C.Connection
|
||||||
func (c *conn) AppendToChains(a C.ProxyAdapter) {
|
func (c *conn) AppendToChains(a C.ProxyAdapter) {
|
||||||
c.chain = append(c.chain, a.Name())
|
c.chain = append(c.chain, a.Name())
|
||||||
|
c.pdChain = append(c.pdChain, a.ProxyInfo().ProviderName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *conn) Upstream() any {
|
func (c *conn) Upstream() any {
|
||||||
@@ -241,19 +272,28 @@ func NewConn(c net.Conn, a C.ProxyAdapter) C.Conn {
|
|||||||
if _, ok := c.(syscall.Conn); !ok { // exclusion system conn like *net.TCPConn
|
if _, ok := c.(syscall.Conn); !ok { // exclusion system conn like *net.TCPConn
|
||||||
c = N.NewDeadlineConn(c) // most conn from outbound can't handle readDeadline correctly
|
c = N.NewDeadlineConn(c) // most conn from outbound can't handle readDeadline correctly
|
||||||
}
|
}
|
||||||
return &conn{N.NewExtendedConn(c), []string{a.Name()}, parseRemoteDestination(a.Addr())}
|
cc := &conn{N.NewExtendedConn(c), nil, nil, a.Addr()}
|
||||||
|
cc.AppendToChains(a)
|
||||||
|
return cc
|
||||||
}
|
}
|
||||||
|
|
||||||
type packetConn struct {
|
type packetConn struct {
|
||||||
N.EnhancePacketConn
|
N.EnhancePacketConn
|
||||||
chain C.Chain
|
chain C.Chain
|
||||||
adapterName string
|
pdChain C.Chain
|
||||||
connID string
|
adapterName string
|
||||||
actualRemoteDestination string
|
connID string
|
||||||
|
adapterAddr string
|
||||||
|
resolveUDP func(ctx context.Context, metadata *C.Metadata) error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *packetConn) ResolveUDP(ctx context.Context, metadata *C.Metadata) error {
|
||||||
|
return c.resolveUDP(ctx, metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *packetConn) RemoteDestination() string {
|
func (c *packetConn) RemoteDestination() string {
|
||||||
return c.actualRemoteDestination
|
host, _, _ := net.SplitHostPort(c.adapterAddr)
|
||||||
|
return host
|
||||||
}
|
}
|
||||||
|
|
||||||
// Chains implements C.Connection
|
// Chains implements C.Connection
|
||||||
@@ -261,9 +301,15 @@ func (c *packetConn) Chains() C.Chain {
|
|||||||
return c.chain
|
return c.chain
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ProviderChains implements C.Connection
|
||||||
|
func (c *packetConn) ProviderChains() C.Chain {
|
||||||
|
return c.pdChain
|
||||||
|
}
|
||||||
|
|
||||||
// AppendToChains implements C.Connection
|
// AppendToChains implements C.Connection
|
||||||
func (c *packetConn) AppendToChains(a C.ProxyAdapter) {
|
func (c *packetConn) AppendToChains(a C.ProxyAdapter) {
|
||||||
c.chain = append(c.chain, a.Name())
|
c.chain = append(c.chain, a.Name())
|
||||||
|
c.pdChain = append(c.pdChain, a.ProxyInfo().ProviderName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *packetConn) LocalAddr() net.Addr {
|
func (c *packetConn) LocalAddr() net.Addr {
|
||||||
@@ -287,24 +333,14 @@ func (c *packetConn) AddRef(ref any) {
|
|||||||
c.EnhancePacketConn = N.NewRefPacketConn(c.EnhancePacketConn, ref) // add ref for autoCloseProxyAdapter
|
c.EnhancePacketConn = N.NewRefPacketConn(c.EnhancePacketConn, ref) // add ref for autoCloseProxyAdapter
|
||||||
}
|
}
|
||||||
|
|
||||||
func newPacketConn(pc net.PacketConn, a C.ProxyAdapter) C.PacketConn {
|
func newPacketConn(pc net.PacketConn, a ProxyAdapter) C.PacketConn {
|
||||||
epc := N.NewEnhancePacketConn(pc)
|
epc := N.NewEnhancePacketConn(pc)
|
||||||
if _, ok := pc.(syscall.Conn); !ok { // exclusion system conn like *net.UDPConn
|
if _, ok := pc.(syscall.Conn); !ok { // exclusion system conn like *net.UDPConn
|
||||||
epc = N.NewDeadlineEnhancePacketConn(epc) // most conn from outbound can't handle readDeadline correctly
|
epc = N.NewDeadlineEnhancePacketConn(epc) // most conn from outbound can't handle readDeadline correctly
|
||||||
}
|
}
|
||||||
return &packetConn{epc, []string{a.Name()}, a.Name(), utils.NewUUIDV4().String(), parseRemoteDestination(a.Addr())}
|
cpc := &packetConn{epc, nil, nil, a.Name(), utils.NewUUIDV4().String(), a.Addr(), a.ResolveUDP}
|
||||||
}
|
cpc.AppendToChains(a)
|
||||||
|
return cpc
|
||||||
func parseRemoteDestination(addr string) string {
|
|
||||||
if dst, _, err := net.SplitHostPort(addr); err == nil {
|
|
||||||
return dst
|
|
||||||
} else {
|
|
||||||
if addrError, ok := err.(*net.AddrError); ok && strings.Contains(addrError.Err, "missing port") {
|
|
||||||
return dst
|
|
||||||
} else {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type AddRef interface {
|
type AddRef interface {
|
||||||
@@ -317,8 +353,8 @@ type autoCloseProxyAdapter struct {
|
|||||||
closeErr error
|
closeErr error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *autoCloseProxyAdapter) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
func (p *autoCloseProxyAdapter) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
c, err := p.ProxyAdapter.DialContext(ctx, metadata, opts...)
|
c, err := p.ProxyAdapter.DialContext(ctx, metadata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -328,30 +364,8 @@ func (p *autoCloseProxyAdapter) DialContext(ctx context.Context, metadata *C.Met
|
|||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *autoCloseProxyAdapter) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
func (p *autoCloseProxyAdapter) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
c, err := p.ProxyAdapter.DialContextWithDialer(ctx, dialer, metadata)
|
pc, err := p.ProxyAdapter.ListenPacketContext(ctx, metadata)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if c, ok := c.(AddRef); ok {
|
|
||||||
c.AddRef(p)
|
|
||||||
}
|
|
||||||
return c, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *autoCloseProxyAdapter) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
|
||||||
pc, err := p.ProxyAdapter.ListenPacketContext(ctx, metadata, opts...)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if pc, ok := pc.(AddRef); ok {
|
|
||||||
pc.AddRef(p)
|
|
||||||
}
|
|
||||||
return pc, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *autoCloseProxyAdapter) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
|
||||||
pc, err := p.ProxyAdapter.ListenPacketWithDialer(ctx, dialer, metadata)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,8 @@ package outbound
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"fmt"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
"github.com/metacubex/mihomo/component/loopback"
|
"github.com/metacubex/mihomo/component/loopback"
|
||||||
"github.com/metacubex/mihomo/component/resolver"
|
"github.com/metacubex/mihomo/component/resolver"
|
||||||
@@ -20,12 +21,13 @@ type DirectOption struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (d *Direct) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
func (d *Direct) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
||||||
if err := d.loopBack.CheckConn(metadata); err != nil {
|
if err := d.loopBack.CheckConn(metadata); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
opts := d.DialOptions()
|
||||||
opts = append(opts, dialer.WithResolver(resolver.DirectHostResolver))
|
opts = append(opts, dialer.WithResolver(resolver.DirectHostResolver))
|
||||||
c, err := dialer.DialContext(ctx, "tcp", metadata.RemoteAddress(), d.Base.DialOptions(opts...)...)
|
c, err := dialer.DialContext(ctx, "tcp", metadata.RemoteAddress(), opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -33,25 +35,31 @@ func (d *Direct) DialContext(ctx context.Context, metadata *C.Metadata, opts ...
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (d *Direct) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
func (d *Direct) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
||||||
if err := d.loopBack.CheckPacketConn(metadata); err != nil {
|
if err := d.loopBack.CheckPacketConn(metadata); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// net.UDPConn.WriteTo only working with *net.UDPAddr, so we need a net.UDPAddr
|
if err := d.ResolveUDP(ctx, metadata); err != nil {
|
||||||
if !metadata.Resolved() {
|
return nil, err
|
||||||
ip, err := resolver.ResolveIPWithResolver(ctx, metadata.Host, resolver.DirectHostResolver)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("can't resolve ip")
|
|
||||||
}
|
|
||||||
metadata.DstIP = ip
|
|
||||||
}
|
}
|
||||||
pc, err := dialer.NewDialer(d.Base.DialOptions(opts...)...).ListenPacket(ctx, "udp", "", metadata.AddrPort())
|
pc, err := dialer.NewDialer(d.DialOptions()...).ListenPacket(ctx, "udp", "", metadata.AddrPort())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return d.loopBack.NewPacketConn(newPacketConn(pc, d)), nil
|
return d.loopBack.NewPacketConn(newPacketConn(pc, d)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Direct) ResolveUDP(ctx context.Context, metadata *C.Metadata) error {
|
||||||
|
if (!metadata.Resolved() || resolver.DirectHostResolver != resolver.DefaultResolver) && metadata.Host != "" {
|
||||||
|
ip, err := resolver.ResolveIPWithResolver(ctx, metadata.Host, resolver.DirectHostResolver)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("can't resolve ip: %w", err)
|
||||||
|
}
|
||||||
|
metadata.DstIP = ip
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (d *Direct) IsL3Protocol(metadata *C.Metadata) bool {
|
func (d *Direct) IsL3Protocol(metadata *C.Metadata) bool {
|
||||||
return true // tell DNSDialer don't send domain to DialContext, avoid lookback to DefaultResolver
|
return true // tell DNSDialer don't send domain to DialContext, avoid lookback to DefaultResolver
|
||||||
}
|
}
|
||||||
@@ -61,12 +69,13 @@ func NewDirectWithOption(option DirectOption) *Direct {
|
|||||||
Base: &Base{
|
Base: &Base{
|
||||||
name: option.Name,
|
name: option.Name,
|
||||||
tp: C.Direct,
|
tp: C.Direct,
|
||||||
|
pdName: option.ProviderName,
|
||||||
udp: true,
|
udp: true,
|
||||||
tfo: option.TFO,
|
tfo: option.TFO,
|
||||||
mpTcp: option.MPTCP,
|
mpTcp: option.MPTCP,
|
||||||
iface: option.Interface,
|
iface: option.Interface,
|
||||||
rmark: option.RoutingMark,
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: option.IPVersion,
|
||||||
},
|
},
|
||||||
loopBack: loopback.NewDetector(),
|
loopBack: loopback.NewDetector(),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,11 +3,11 @@ package outbound
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
|
"net/netip"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/metacubex/mihomo/common/pool"
|
"github.com/metacubex/mihomo/common/pool"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
|
||||||
"github.com/metacubex/mihomo/component/resolver"
|
"github.com/metacubex/mihomo/component/resolver"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/log"
|
"github.com/metacubex/mihomo/log"
|
||||||
@@ -23,15 +23,18 @@ type DnsOption struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (d *Dns) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
func (d *Dns) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
||||||
left, right := N.Pipe()
|
left, right := N.Pipe()
|
||||||
go resolver.RelayDnsConn(context.Background(), right, 0)
|
go resolver.RelayDnsConn(context.Background(), right, 0)
|
||||||
return NewConn(left, d), nil
|
return NewConn(left, d), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (d *Dns) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
func (d *Dns) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
||||||
log.Debugln("[DNS] hijack udp:%s from %s", metadata.RemoteAddress(), metadata.SourceAddrPort())
|
log.Debugln("[DNS] hijack udp:%s from %s", metadata.RemoteAddress(), metadata.SourceAddrPort())
|
||||||
|
if err := d.ResolveUDP(ctx, metadata); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
@@ -42,6 +45,13 @@ func (d *Dns) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opt
|
|||||||
}, d), nil
|
}, d), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Dns) ResolveUDP(ctx context.Context, metadata *C.Metadata) error {
|
||||||
|
if !metadata.Resolved() {
|
||||||
|
metadata.DstIP = netip.AddrFrom4([4]byte{127, 0, 0, 2})
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
type dnsPacket struct {
|
type dnsPacket struct {
|
||||||
data []byte
|
data []byte
|
||||||
put func()
|
put func()
|
||||||
@@ -148,12 +158,13 @@ func NewDnsWithOption(option DnsOption) *Dns {
|
|||||||
Base: &Base{
|
Base: &Base{
|
||||||
name: option.Name,
|
name: option.Name,
|
||||||
tp: C.Dns,
|
tp: C.Dns,
|
||||||
|
pdName: option.ProviderName,
|
||||||
udp: true,
|
udp: true,
|
||||||
tfo: option.TFO,
|
tfo: option.TFO,
|
||||||
mpTcp: option.MPTCP,
|
mpTcp: option.MPTCP,
|
||||||
iface: option.Interface,
|
iface: option.Interface,
|
||||||
rmark: option.RoutingMark,
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: option.IPVersion,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
41
adapter/outbound/ech.go
Normal file
41
adapter/outbound/ech.go
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package outbound
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/base64"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/metacubex/mihomo/component/ech"
|
||||||
|
"github.com/metacubex/mihomo/component/resolver"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ECHOptions struct {
|
||||||
|
Enable bool `proxy:"enable,omitempty" obfs:"enable,omitempty"`
|
||||||
|
Config string `proxy:"config,omitempty" obfs:"config,omitempty"`
|
||||||
|
|
||||||
|
QueryServerName string `proxy:"query-server-name,omitempty" obfs:"query-server-name,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o ECHOptions) Parse() (*ech.Config, error) {
|
||||||
|
if !o.Enable {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
echConfig := &ech.Config{}
|
||||||
|
if o.Config != "" {
|
||||||
|
list, err := base64.StdEncoding.DecodeString(o.Config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("base64 decode ech config string failed: %v", err)
|
||||||
|
}
|
||||||
|
echConfig.GetEncryptedClientHelloConfigList = func(ctx context.Context, serverName string) ([]byte, error) {
|
||||||
|
return list, nil
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
echConfig.GetEncryptedClientHelloConfigList = func(ctx context.Context, serverName string) ([]byte, error) {
|
||||||
|
if o.QueryServerName != "" { // overrides the domain name used for ECH HTTPS record queries
|
||||||
|
serverName = o.QueryServerName
|
||||||
|
}
|
||||||
|
return resolver.ResolveECHWithResolver(ctx, serverName, resolver.ProxyServerHostResolver)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return echConfig, nil
|
||||||
|
}
|
||||||
@@ -3,19 +3,18 @@ package outbound
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/metacubex/mihomo/component/ca"
|
"github.com/metacubex/mihomo/component/ca"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
|
|
||||||
|
"github.com/metacubex/http"
|
||||||
|
"github.com/metacubex/tls"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Http struct {
|
type Http struct {
|
||||||
@@ -37,6 +36,8 @@ type HttpOption struct {
|
|||||||
SNI string `proxy:"sni,omitempty"`
|
SNI string `proxy:"sni,omitempty"`
|
||||||
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
||||||
Fingerprint string `proxy:"fingerprint,omitempty"`
|
Fingerprint string `proxy:"fingerprint,omitempty"`
|
||||||
|
Certificate string `proxy:"certificate,omitempty"`
|
||||||
|
PrivateKey string `proxy:"private-key,omitempty"`
|
||||||
Headers map[string]string `proxy:"headers,omitempty"`
|
Headers map[string]string `proxy:"headers,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,19 +59,8 @@ func (h *Http) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Me
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (h *Http) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
func (h *Http) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
return h.DialContextWithDialer(ctx, dialer.NewDialer(h.Base.DialOptions(opts...)...), metadata)
|
c, err := h.dialer.DialContext(ctx, "tcp", h.addr)
|
||||||
}
|
|
||||||
|
|
||||||
// DialContextWithDialer implements C.ProxyAdapter
|
|
||||||
func (h *Http) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
|
||||||
if len(h.option.DialerProxy) > 0 {
|
|
||||||
dialer, err = proxydialer.NewByName(h.option.DialerProxy, dialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
c, err := dialer.DialContext(ctx, "tcp", h.addr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error: %w", h.addr, err)
|
return nil, fmt.Errorf("%s connect error: %w", h.addr, err)
|
||||||
}
|
}
|
||||||
@@ -87,11 +77,6 @@ func (h *Http) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metad
|
|||||||
return NewConn(c, h), nil
|
return NewConn(c, h), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SupportWithDialer implements C.ProxyAdapter
|
|
||||||
func (h *Http) SupportWithDialer() C.NetWork {
|
|
||||||
return C.TCP
|
|
||||||
}
|
|
||||||
|
|
||||||
// ProxyInfo implements C.ProxyAdapter
|
// ProxyInfo implements C.ProxyAdapter
|
||||||
func (h *Http) ProxyInfo() C.ProxyInfo {
|
func (h *Http) ProxyInfo() C.ProxyInfo {
|
||||||
info := h.Base.ProxyInfo()
|
info := h.Base.ProxyInfo()
|
||||||
@@ -167,29 +152,37 @@ func NewHttp(option HttpOption) (*Http, error) {
|
|||||||
sni = option.SNI
|
sni = option.SNI
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
tlsConfig, err = ca.GetSpecifiedFingerprintTLSConfig(&tls.Config{
|
tlsConfig, err = ca.GetTLSConfig(ca.Option{
|
||||||
InsecureSkipVerify: option.SkipCertVerify,
|
TLSConfig: &tls.Config{
|
||||||
ServerName: sni,
|
InsecureSkipVerify: option.SkipCertVerify,
|
||||||
}, option.Fingerprint)
|
ServerName: sni,
|
||||||
|
},
|
||||||
|
Fingerprint: option.Fingerprint,
|
||||||
|
Certificate: option.Certificate,
|
||||||
|
PrivateKey: option.PrivateKey,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Http{
|
outbound := &Http{
|
||||||
Base: &Base{
|
Base: &Base{
|
||||||
name: option.Name,
|
name: option.Name,
|
||||||
addr: net.JoinHostPort(option.Server, strconv.Itoa(option.Port)),
|
addr: net.JoinHostPort(option.Server, strconv.Itoa(option.Port)),
|
||||||
tp: C.Http,
|
tp: C.Http,
|
||||||
|
pdName: option.ProviderName,
|
||||||
tfo: option.TFO,
|
tfo: option.TFO,
|
||||||
mpTcp: option.MPTCP,
|
mpTcp: option.MPTCP,
|
||||||
iface: option.Interface,
|
iface: option.Interface,
|
||||||
rmark: option.RoutingMark,
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: option.IPVersion,
|
||||||
},
|
},
|
||||||
user: option.UserName,
|
user: option.UserName,
|
||||||
pass: option.Password,
|
pass: option.Password,
|
||||||
tlsConfig: tlsConfig,
|
tlsConfig: tlsConfig,
|
||||||
option: &option,
|
option: &option,
|
||||||
}, nil
|
}
|
||||||
|
outbound.dialer = option.NewDialer(outbound.DialOptions())
|
||||||
|
return outbound, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package outbound
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
@@ -10,13 +9,9 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/quic-go"
|
|
||||||
"github.com/metacubex/quic-go/congestion"
|
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/component/ca"
|
"github.com/metacubex/mihomo/component/ca"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
"github.com/metacubex/mihomo/component/ech"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/log"
|
"github.com/metacubex/mihomo/log"
|
||||||
hyCongestion "github.com/metacubex/mihomo/transport/hysteria/congestion"
|
hyCongestion "github.com/metacubex/mihomo/transport/hysteria/congestion"
|
||||||
@@ -25,6 +20,12 @@ import (
|
|||||||
"github.com/metacubex/mihomo/transport/hysteria/pmtud_fix"
|
"github.com/metacubex/mihomo/transport/hysteria/pmtud_fix"
|
||||||
"github.com/metacubex/mihomo/transport/hysteria/transport"
|
"github.com/metacubex/mihomo/transport/hysteria/transport"
|
||||||
"github.com/metacubex/mihomo/transport/hysteria/utils"
|
"github.com/metacubex/mihomo/transport/hysteria/utils"
|
||||||
|
|
||||||
|
"github.com/metacubex/tls"
|
||||||
|
|
||||||
|
"github.com/metacubex/quic-go"
|
||||||
|
"github.com/metacubex/quic-go/congestion"
|
||||||
|
M "github.com/metacubex/sing/common/metadata"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -43,10 +44,13 @@ type Hysteria struct {
|
|||||||
|
|
||||||
option *HysteriaOption
|
option *HysteriaOption
|
||||||
client *core.Client
|
client *core.Client
|
||||||
|
|
||||||
|
tlsConfig *tls.Config
|
||||||
|
echConfig *ech.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hysteria) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
func (h *Hysteria) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
||||||
tcpConn, err := h.client.DialTCP(metadata.String(), metadata.DstPort, h.genHdc(ctx, opts...))
|
tcpConn, err := h.client.DialTCP(metadata.String(), metadata.DstPort, h.genHdc(ctx))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -54,31 +58,34 @@ func (h *Hysteria) DialContext(ctx context.Context, metadata *C.Metadata, opts .
|
|||||||
return NewConn(tcpConn, h), nil
|
return NewConn(tcpConn, h), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hysteria) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
func (h *Hysteria) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
||||||
udpConn, err := h.client.DialUDP(h.genHdc(ctx, opts...))
|
if err := h.ResolveUDP(ctx, metadata); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
udpConn, err := h.client.DialUDP(h.genHdc(ctx))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return newPacketConn(&hyPacketConn{udpConn}, h), nil
|
return newPacketConn(&hyPacketConn{udpConn}, h), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hysteria) genHdc(ctx context.Context, opts ...dialer.Option) utils.PacketDialer {
|
func (h *Hysteria) genHdc(ctx context.Context) utils.PacketDialer {
|
||||||
return &hyDialerWithContext{
|
return &hyDialerWithContext{
|
||||||
ctx: context.Background(),
|
ctx: context.Background(),
|
||||||
hyDialer: func(network string, rAddr net.Addr) (net.PacketConn, error) {
|
hyDialer: func(network string, rAddr net.Addr) (net.PacketConn, error) {
|
||||||
var err error
|
|
||||||
var cDialer C.Dialer = dialer.NewDialer(h.Base.DialOptions(opts...)...)
|
|
||||||
if len(h.option.DialerProxy) > 0 {
|
|
||||||
cDialer, err = proxydialer.NewByName(h.option.DialerProxy, cDialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
rAddrPort, _ := netip.ParseAddrPort(rAddr.String())
|
rAddrPort, _ := netip.ParseAddrPort(rAddr.String())
|
||||||
return cDialer.ListenPacket(ctx, network, "", rAddrPort)
|
return h.dialer.ListenPacket(ctx, network, "", rAddrPort)
|
||||||
},
|
},
|
||||||
remoteAddr: func(addr string) (net.Addr, error) {
|
remoteAddr: func(addr string) (net.Addr, error) {
|
||||||
return resolveUDPAddr(ctx, "udp", addr, h.prefer)
|
udpAddr, err := resolveUDPAddr(ctx, "udp", addr, h.prefer)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = h.echConfig.ClientHandle(ctx, h.tlsConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return udpAddr, nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -92,30 +99,31 @@ func (h *Hysteria) ProxyInfo() C.ProxyInfo {
|
|||||||
|
|
||||||
type HysteriaOption struct {
|
type HysteriaOption struct {
|
||||||
BasicOption
|
BasicOption
|
||||||
Name string `proxy:"name"`
|
Name string `proxy:"name"`
|
||||||
Server string `proxy:"server"`
|
Server string `proxy:"server"`
|
||||||
Port int `proxy:"port,omitempty"`
|
Port int `proxy:"port,omitempty"`
|
||||||
Ports string `proxy:"ports,omitempty"`
|
Ports string `proxy:"ports,omitempty"`
|
||||||
Protocol string `proxy:"protocol,omitempty"`
|
Protocol string `proxy:"protocol,omitempty"`
|
||||||
ObfsProtocol string `proxy:"obfs-protocol,omitempty"` // compatible with Stash
|
ObfsProtocol string `proxy:"obfs-protocol,omitempty"` // compatible with Stash
|
||||||
Up string `proxy:"up"`
|
Up string `proxy:"up"`
|
||||||
UpSpeed int `proxy:"up-speed,omitempty"` // compatible with Stash
|
UpSpeed int `proxy:"up-speed,omitempty"` // compatible with Stash
|
||||||
Down string `proxy:"down"`
|
Down string `proxy:"down"`
|
||||||
DownSpeed int `proxy:"down-speed,omitempty"` // compatible with Stash
|
DownSpeed int `proxy:"down-speed,omitempty"` // compatible with Stash
|
||||||
Auth string `proxy:"auth,omitempty"`
|
Auth string `proxy:"auth,omitempty"`
|
||||||
AuthString string `proxy:"auth-str,omitempty"`
|
AuthString string `proxy:"auth-str,omitempty"`
|
||||||
Obfs string `proxy:"obfs,omitempty"`
|
Obfs string `proxy:"obfs,omitempty"`
|
||||||
SNI string `proxy:"sni,omitempty"`
|
SNI string `proxy:"sni,omitempty"`
|
||||||
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
ECHOpts ECHOptions `proxy:"ech-opts,omitempty"`
|
||||||
Fingerprint string `proxy:"fingerprint,omitempty"`
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
||||||
ALPN []string `proxy:"alpn,omitempty"`
|
Fingerprint string `proxy:"fingerprint,omitempty"`
|
||||||
CustomCA string `proxy:"ca,omitempty"`
|
Certificate string `proxy:"certificate,omitempty"`
|
||||||
CustomCAString string `proxy:"ca-str,omitempty"`
|
PrivateKey string `proxy:"private-key,omitempty"`
|
||||||
ReceiveWindowConn int `proxy:"recv-window-conn,omitempty"`
|
ALPN []string `proxy:"alpn,omitempty"`
|
||||||
ReceiveWindow int `proxy:"recv-window,omitempty"`
|
ReceiveWindowConn int `proxy:"recv-window-conn,omitempty"`
|
||||||
DisableMTUDiscovery bool `proxy:"disable-mtu-discovery,omitempty"`
|
ReceiveWindow int `proxy:"recv-window,omitempty"`
|
||||||
FastOpen bool `proxy:"fast-open,omitempty"`
|
DisableMTUDiscovery bool `proxy:"disable-mtu-discovery,omitempty"`
|
||||||
HopInterval int `proxy:"hop-interval,omitempty"`
|
FastOpen bool `proxy:"fast-open,omitempty"`
|
||||||
|
HopInterval int `proxy:"hop-interval,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *HysteriaOption) Speed() (uint64, uint64, error) {
|
func (c *HysteriaOption) Speed() (uint64, uint64, error) {
|
||||||
@@ -143,23 +151,32 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
|
|||||||
serverName = option.SNI
|
serverName = option.SNI
|
||||||
}
|
}
|
||||||
|
|
||||||
tlsConfig := &tls.Config{
|
tlsConfig, err := ca.GetTLSConfig(ca.Option{
|
||||||
ServerName: serverName,
|
TLSConfig: &tls.Config{
|
||||||
InsecureSkipVerify: option.SkipCertVerify,
|
ServerName: serverName,
|
||||||
MinVersion: tls.VersionTLS13,
|
InsecureSkipVerify: option.SkipCertVerify,
|
||||||
}
|
MinVersion: tls.VersionTLS13,
|
||||||
|
},
|
||||||
var err error
|
Fingerprint: option.Fingerprint,
|
||||||
tlsConfig, err = ca.GetTLSConfig(tlsConfig, option.Fingerprint, option.CustomCA, option.CustomCAString)
|
Certificate: option.Certificate,
|
||||||
|
PrivateKey: option.PrivateKey,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(option.ALPN) > 0 {
|
if option.ALPN != nil { // structure's Decode will ensure value not nil when input has value even it was set an empty array
|
||||||
tlsConfig.NextProtos = option.ALPN
|
tlsConfig.NextProtos = option.ALPN
|
||||||
} else {
|
} else {
|
||||||
tlsConfig.NextProtos = []string{DefaultALPN}
|
tlsConfig.NextProtos = []string{DefaultALPN}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
echConfig, err := option.ECHOpts.Parse()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
tlsClientConfig := tlsConfig
|
||||||
|
|
||||||
quicConfig := &quic.Config{
|
quicConfig := &quic.Config{
|
||||||
InitialStreamReceiveWindow: uint64(option.ReceiveWindowConn),
|
InitialStreamReceiveWindow: uint64(option.ReceiveWindowConn),
|
||||||
MaxStreamReceiveWindow: uint64(option.ReceiveWindowConn),
|
MaxStreamReceiveWindow: uint64(option.ReceiveWindowConn),
|
||||||
@@ -214,7 +231,7 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
|
|||||||
down = uint64(option.DownSpeed * mbpsToBps)
|
down = uint64(option.DownSpeed * mbpsToBps)
|
||||||
}
|
}
|
||||||
client, err := core.NewClient(
|
client, err := core.NewClient(
|
||||||
addr, ports, option.Protocol, auth, tlsConfig, quicConfig, clientTransport, up, down, func(refBPS uint64) congestion.CongestionControl {
|
addr, ports, option.Protocol, auth, tlsClientConfig, quicConfig, clientTransport, up, down, func(refBPS uint64) congestion.CongestionControl {
|
||||||
return hyCongestion.NewBrutalSender(congestion.ByteCount(refBPS))
|
return hyCongestion.NewBrutalSender(congestion.ByteCount(refBPS))
|
||||||
}, obfuscator, hopInterval, option.FastOpen,
|
}, obfuscator, hopInterval, option.FastOpen,
|
||||||
)
|
)
|
||||||
@@ -226,15 +243,19 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
|
|||||||
name: option.Name,
|
name: option.Name,
|
||||||
addr: addr,
|
addr: addr,
|
||||||
tp: C.Hysteria,
|
tp: C.Hysteria,
|
||||||
|
pdName: option.ProviderName,
|
||||||
udp: true,
|
udp: true,
|
||||||
tfo: option.FastOpen,
|
tfo: option.FastOpen,
|
||||||
iface: option.Interface,
|
iface: option.Interface,
|
||||||
rmark: option.RoutingMark,
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: option.IPVersion,
|
||||||
},
|
},
|
||||||
option: &option,
|
option: &option,
|
||||||
client: client,
|
client: client,
|
||||||
|
tlsConfig: tlsClientConfig,
|
||||||
|
echConfig: echConfig,
|
||||||
}
|
}
|
||||||
|
outbound.dialer = option.NewDialer(outbound.DialOptions())
|
||||||
|
|
||||||
return outbound, nil
|
return outbound, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,27 +2,24 @@ package outbound
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
CN "github.com/metacubex/mihomo/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
"github.com/metacubex/mihomo/component/ca"
|
"github.com/metacubex/mihomo/component/ca"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
"github.com/metacubex/mihomo/component/proxydialer"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/log"
|
"github.com/metacubex/mihomo/log"
|
||||||
tuicCommon "github.com/metacubex/mihomo/transport/tuic/common"
|
tuicCommon "github.com/metacubex/mihomo/transport/tuic/common"
|
||||||
|
|
||||||
"github.com/metacubex/sing-quic/hysteria2"
|
|
||||||
|
|
||||||
"github.com/metacubex/quic-go"
|
"github.com/metacubex/quic-go"
|
||||||
"github.com/metacubex/randv2"
|
"github.com/metacubex/sing-quic/hysteria2"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/metacubex/sing/common/metadata"
|
||||||
|
"github.com/metacubex/tls"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -37,29 +34,29 @@ type Hysteria2 struct {
|
|||||||
|
|
||||||
option *Hysteria2Option
|
option *Hysteria2Option
|
||||||
client *hysteria2.Client
|
client *hysteria2.Client
|
||||||
dialer proxydialer.SingDialer
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Hysteria2Option struct {
|
type Hysteria2Option struct {
|
||||||
BasicOption
|
BasicOption
|
||||||
Name string `proxy:"name"`
|
Name string `proxy:"name"`
|
||||||
Server string `proxy:"server"`
|
Server string `proxy:"server"`
|
||||||
Port int `proxy:"port,omitempty"`
|
Port int `proxy:"port,omitempty"`
|
||||||
Ports string `proxy:"ports,omitempty"`
|
Ports string `proxy:"ports,omitempty"`
|
||||||
HopInterval int `proxy:"hop-interval,omitempty"`
|
HopInterval int `proxy:"hop-interval,omitempty"`
|
||||||
Up string `proxy:"up,omitempty"`
|
Up string `proxy:"up,omitempty"`
|
||||||
Down string `proxy:"down,omitempty"`
|
Down string `proxy:"down,omitempty"`
|
||||||
Password string `proxy:"password,omitempty"`
|
Password string `proxy:"password,omitempty"`
|
||||||
Obfs string `proxy:"obfs,omitempty"`
|
Obfs string `proxy:"obfs,omitempty"`
|
||||||
ObfsPassword string `proxy:"obfs-password,omitempty"`
|
ObfsPassword string `proxy:"obfs-password,omitempty"`
|
||||||
SNI string `proxy:"sni,omitempty"`
|
SNI string `proxy:"sni,omitempty"`
|
||||||
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
ECHOpts ECHOptions `proxy:"ech-opts,omitempty"`
|
||||||
Fingerprint string `proxy:"fingerprint,omitempty"`
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
||||||
ALPN []string `proxy:"alpn,omitempty"`
|
Fingerprint string `proxy:"fingerprint,omitempty"`
|
||||||
CustomCA string `proxy:"ca,omitempty"`
|
Certificate string `proxy:"certificate,omitempty"`
|
||||||
CustomCAString string `proxy:"ca-str,omitempty"`
|
PrivateKey string `proxy:"private-key,omitempty"`
|
||||||
CWND int `proxy:"cwnd,omitempty"`
|
ALPN []string `proxy:"alpn,omitempty"`
|
||||||
UdpMTU int `proxy:"udp-mtu,omitempty"`
|
CWND int `proxy:"cwnd,omitempty"`
|
||||||
|
UdpMTU int `proxy:"udp-mtu,omitempty"`
|
||||||
|
|
||||||
// quic-go special config
|
// quic-go special config
|
||||||
InitialStreamReceiveWindow uint64 `proxy:"initial-stream-receive-window,omitempty"`
|
InitialStreamReceiveWindow uint64 `proxy:"initial-stream-receive-window,omitempty"`
|
||||||
@@ -68,9 +65,7 @@ type Hysteria2Option struct {
|
|||||||
MaxConnectionReceiveWindow uint64 `proxy:"max-connection-receive-window,omitempty"`
|
MaxConnectionReceiveWindow uint64 `proxy:"max-connection-receive-window,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hysteria2) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
func (h *Hysteria2) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
options := h.Base.DialOptions(opts...)
|
|
||||||
h.dialer.SetDialer(dialer.NewDialer(options...))
|
|
||||||
c, err := h.client.DialConn(ctx, M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort))
|
c, err := h.client.DialConn(ctx, M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -78,9 +73,10 @@ func (h *Hysteria2) DialContext(ctx context.Context, metadata *C.Metadata, opts
|
|||||||
return NewConn(c, h), nil
|
return NewConn(c, h), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hysteria2) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
func (h *Hysteria2) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
options := h.Base.DialOptions(opts...)
|
if err = h.ResolveUDP(ctx, metadata); err != nil {
|
||||||
h.dialer.SetDialer(dialer.NewDialer(options...))
|
return nil, err
|
||||||
|
}
|
||||||
pc, err := h.client.ListenPacket(ctx)
|
pc, err := h.client.ListenPacket(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -88,7 +84,7 @@ func (h *Hysteria2) ListenPacketContext(ctx context.Context, metadata *C.Metadat
|
|||||||
if pc == nil {
|
if pc == nil {
|
||||||
return nil, errors.New("packetConn is nil")
|
return nil, errors.New("packetConn is nil")
|
||||||
}
|
}
|
||||||
return newPacketConn(CN.NewThreadSafePacketConn(pc), h), nil
|
return newPacketConn(N.NewThreadSafePacketConn(pc), h), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close implements C.ProxyAdapter
|
// Close implements C.ProxyAdapter
|
||||||
@@ -108,6 +104,22 @@ func (h *Hysteria2) ProxyInfo() C.ProxyInfo {
|
|||||||
|
|
||||||
func NewHysteria2(option Hysteria2Option) (*Hysteria2, error) {
|
func NewHysteria2(option Hysteria2Option) (*Hysteria2, error) {
|
||||||
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
||||||
|
outbound := &Hysteria2{
|
||||||
|
Base: &Base{
|
||||||
|
name: option.Name,
|
||||||
|
addr: addr,
|
||||||
|
tp: C.Hysteria2,
|
||||||
|
pdName: option.ProviderName,
|
||||||
|
udp: true,
|
||||||
|
iface: option.Interface,
|
||||||
|
rmark: option.RoutingMark,
|
||||||
|
prefer: option.IPVersion,
|
||||||
|
},
|
||||||
|
option: &option,
|
||||||
|
}
|
||||||
|
outbound.dialer = option.NewDialer(outbound.DialOptions())
|
||||||
|
singDialer := proxydialer.NewSingDialer(outbound.dialer)
|
||||||
|
|
||||||
var salamanderPassword string
|
var salamanderPassword string
|
||||||
if len(option.Obfs) > 0 {
|
if len(option.Obfs) > 0 {
|
||||||
if option.ObfsPassword == "" {
|
if option.ObfsPassword == "" {
|
||||||
@@ -126,22 +138,30 @@ func NewHysteria2(option Hysteria2Option) (*Hysteria2, error) {
|
|||||||
serverName = option.SNI
|
serverName = option.SNI
|
||||||
}
|
}
|
||||||
|
|
||||||
tlsConfig := &tls.Config{
|
tlsConfig, err := ca.GetTLSConfig(ca.Option{
|
||||||
ServerName: serverName,
|
TLSConfig: &tls.Config{
|
||||||
InsecureSkipVerify: option.SkipCertVerify,
|
ServerName: serverName,
|
||||||
MinVersion: tls.VersionTLS13,
|
InsecureSkipVerify: option.SkipCertVerify,
|
||||||
}
|
MinVersion: tls.VersionTLS13,
|
||||||
|
},
|
||||||
var err error
|
Fingerprint: option.Fingerprint,
|
||||||
tlsConfig, err = ca.GetTLSConfig(tlsConfig, option.Fingerprint, option.CustomCA, option.CustomCAString)
|
Certificate: option.Certificate,
|
||||||
|
PrivateKey: option.PrivateKey,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(option.ALPN) > 0 {
|
if option.ALPN != nil { // structure's Decode will ensure value not nil when input has value even it was set an empty array
|
||||||
tlsConfig.NextProtos = option.ALPN
|
tlsConfig.NextProtos = option.ALPN
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tlsClientConfig := tlsConfig
|
||||||
|
echConfig, err := option.ECHOpts.Parse()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
if option.UdpMTU == 0 {
|
if option.UdpMTU == 0 {
|
||||||
// "1200" from quic-go's MaxDatagramSize
|
// "1200" from quic-go's MaxDatagramSize
|
||||||
// "-3" from quic-go's DatagramFrame.MaxDataLen
|
// "-3" from quic-go's DatagramFrame.MaxDataLen
|
||||||
@@ -155,8 +175,6 @@ func NewHysteria2(option Hysteria2Option) (*Hysteria2, error) {
|
|||||||
MaxConnectionReceiveWindow: option.MaxConnectionReceiveWindow,
|
MaxConnectionReceiveWindow: option.MaxConnectionReceiveWindow,
|
||||||
}
|
}
|
||||||
|
|
||||||
singDialer := proxydialer.NewByNameSingDialer(option.DialerProxy, dialer.NewDialer())
|
|
||||||
|
|
||||||
clientOptions := hysteria2.ClientOptions{
|
clientOptions := hysteria2.ClientOptions{
|
||||||
Context: context.TODO(),
|
Context: context.TODO(),
|
||||||
Dialer: singDialer,
|
Dialer: singDialer,
|
||||||
@@ -165,41 +183,46 @@ func NewHysteria2(option Hysteria2Option) (*Hysteria2, error) {
|
|||||||
ReceiveBPS: StringToBps(option.Down),
|
ReceiveBPS: StringToBps(option.Down),
|
||||||
SalamanderPassword: salamanderPassword,
|
SalamanderPassword: salamanderPassword,
|
||||||
Password: option.Password,
|
Password: option.Password,
|
||||||
TLSConfig: tlsConfig,
|
TLSConfig: tlsClientConfig,
|
||||||
QUICConfig: quicConfig,
|
QUICConfig: quicConfig,
|
||||||
UDPDisabled: false,
|
UDPDisabled: false,
|
||||||
CWND: option.CWND,
|
CWND: option.CWND,
|
||||||
UdpMTU: option.UdpMTU,
|
UdpMTU: option.UdpMTU,
|
||||||
ServerAddress: func(ctx context.Context) (*net.UDPAddr, error) {
|
ServerAddress: func(ctx context.Context) (*net.UDPAddr, error) {
|
||||||
return resolveUDPAddr(ctx, "udp", addr, C.NewDNSPrefer(option.IPVersion))
|
udpAddr, err := resolveUDPAddr(ctx, "udp", addr, option.IPVersion)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = echConfig.ClientHandle(ctx, tlsClientConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return udpAddr, nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var ranges utils.IntRanges[uint16]
|
var ranges utils.IntRanges[uint16]
|
||||||
var serverAddress []string
|
var serverPorts []uint16
|
||||||
if option.Ports != "" {
|
if option.Ports != "" {
|
||||||
ranges, err = utils.NewUnsignedRanges[uint16](option.Ports)
|
ranges, err = utils.NewUnsignedRanges[uint16](option.Ports)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
ranges.Range(func(port uint16) bool {
|
ranges.Range(func(port uint16) bool {
|
||||||
serverAddress = append(serverAddress, net.JoinHostPort(option.Server, strconv.Itoa(int(port))))
|
serverPorts = append(serverPorts, port)
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
if len(serverAddress) > 0 {
|
if len(serverPorts) > 0 {
|
||||||
clientOptions.ServerAddress = func(ctx context.Context) (*net.UDPAddr, error) {
|
|
||||||
return resolveUDPAddr(ctx, "udp", serverAddress[randv2.IntN(len(serverAddress))], C.NewDNSPrefer(option.IPVersion))
|
|
||||||
}
|
|
||||||
|
|
||||||
if option.HopInterval == 0 {
|
if option.HopInterval == 0 {
|
||||||
option.HopInterval = defaultHopInterval
|
option.HopInterval = defaultHopInterval
|
||||||
} else if option.HopInterval < minHopInterval {
|
} else if option.HopInterval < minHopInterval {
|
||||||
option.HopInterval = minHopInterval
|
option.HopInterval = minHopInterval
|
||||||
}
|
}
|
||||||
clientOptions.HopInterval = time.Duration(option.HopInterval) * time.Second
|
clientOptions.HopInterval = time.Duration(option.HopInterval) * time.Second
|
||||||
|
clientOptions.ServerPorts = serverPorts
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if option.Port == 0 && len(serverAddress) == 0 {
|
if option.Port == 0 && len(serverPorts) == 0 {
|
||||||
return nil, errors.New("invalid port")
|
return nil, errors.New("invalid port")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -207,21 +230,7 @@ func NewHysteria2(option Hysteria2Option) (*Hysteria2, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
outbound.client = client
|
||||||
outbound := &Hysteria2{
|
|
||||||
Base: &Base{
|
|
||||||
name: option.Name,
|
|
||||||
addr: addr,
|
|
||||||
tp: C.Hysteria2,
|
|
||||||
udp: true,
|
|
||||||
iface: option.Interface,
|
|
||||||
rmark: option.RoutingMark,
|
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
|
||||||
},
|
|
||||||
option: &option,
|
|
||||||
client: client,
|
|
||||||
dialer: singDialer,
|
|
||||||
}
|
|
||||||
|
|
||||||
return outbound, nil
|
return outbound, nil
|
||||||
}
|
}
|
||||||
|
|||||||
397
adapter/outbound/masque.go
Normal file
397
adapter/outbound/masque.go
Normal file
@@ -0,0 +1,397 @@
|
|||||||
|
package outbound
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/ecdsa"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/netip"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/metacubex/mihomo/common/atomic"
|
||||||
|
"github.com/metacubex/mihomo/common/contextutils"
|
||||||
|
"github.com/metacubex/mihomo/common/pool"
|
||||||
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
|
"github.com/metacubex/mihomo/component/resolver"
|
||||||
|
C "github.com/metacubex/mihomo/constant"
|
||||||
|
"github.com/metacubex/mihomo/dns"
|
||||||
|
"github.com/metacubex/mihomo/log"
|
||||||
|
"github.com/metacubex/mihomo/transport/masque"
|
||||||
|
"github.com/metacubex/mihomo/transport/tuic/common"
|
||||||
|
|
||||||
|
connectip "github.com/metacubex/connect-ip-go"
|
||||||
|
"github.com/metacubex/quic-go"
|
||||||
|
wireguard "github.com/metacubex/sing-wireguard"
|
||||||
|
M "github.com/metacubex/sing/common/metadata"
|
||||||
|
"github.com/metacubex/tls"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Masque struct {
|
||||||
|
*Base
|
||||||
|
tlsConfig *tls.Config
|
||||||
|
quicConfig *quic.Config
|
||||||
|
tunDevice wireguard.Device
|
||||||
|
resolver resolver.Resolver
|
||||||
|
uri string
|
||||||
|
|
||||||
|
runCtx context.Context
|
||||||
|
runCancel context.CancelFunc
|
||||||
|
runMutex sync.Mutex
|
||||||
|
running atomic.Bool
|
||||||
|
runDevice atomic.Bool
|
||||||
|
|
||||||
|
option MasqueOption
|
||||||
|
}
|
||||||
|
|
||||||
|
type MasqueOption struct {
|
||||||
|
BasicOption
|
||||||
|
Name string `proxy:"name"`
|
||||||
|
Server string `proxy:"server"`
|
||||||
|
Port int `proxy:"port"`
|
||||||
|
PrivateKey string `proxy:"private-key"`
|
||||||
|
PublicKey string `proxy:"public-key"`
|
||||||
|
Ip string `proxy:"ip,omitempty"`
|
||||||
|
Ipv6 string `proxy:"ipv6,omitempty"`
|
||||||
|
URI string `proxy:"uri,omitempty"`
|
||||||
|
SNI string `proxy:"sni,omitempty"`
|
||||||
|
MTU int `proxy:"mtu,omitempty"`
|
||||||
|
UDP bool `proxy:"udp,omitempty"`
|
||||||
|
|
||||||
|
CongestionController string `proxy:"congestion-controller,omitempty"`
|
||||||
|
CWND int `proxy:"cwnd,omitempty"`
|
||||||
|
|
||||||
|
RemoteDnsResolve bool `proxy:"remote-dns-resolve,omitempty"`
|
||||||
|
Dns []string `proxy:"dns,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (option MasqueOption) Prefixes() ([]netip.Prefix, error) {
|
||||||
|
localPrefixes := make([]netip.Prefix, 0, 2)
|
||||||
|
if len(option.Ip) > 0 {
|
||||||
|
if !strings.Contains(option.Ip, "/") {
|
||||||
|
option.Ip = option.Ip + "/32"
|
||||||
|
}
|
||||||
|
if prefix, err := netip.ParsePrefix(option.Ip); err == nil {
|
||||||
|
localPrefixes = append(localPrefixes, prefix)
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("ip address parse error: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(option.Ipv6) > 0 {
|
||||||
|
if !strings.Contains(option.Ipv6, "/") {
|
||||||
|
option.Ipv6 = option.Ipv6 + "/128"
|
||||||
|
}
|
||||||
|
if prefix, err := netip.ParsePrefix(option.Ipv6); err == nil {
|
||||||
|
localPrefixes = append(localPrefixes, prefix)
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("ipv6 address parse error: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(localPrefixes) == 0 {
|
||||||
|
return nil, errors.New("missing local address")
|
||||||
|
}
|
||||||
|
return localPrefixes, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMasque(option MasqueOption) (*Masque, error) {
|
||||||
|
outbound := &Masque{
|
||||||
|
Base: &Base{
|
||||||
|
name: option.Name,
|
||||||
|
addr: net.JoinHostPort(option.Server, strconv.Itoa(option.Port)),
|
||||||
|
tp: C.Masque,
|
||||||
|
pdName: option.ProviderName,
|
||||||
|
udp: option.UDP,
|
||||||
|
iface: option.Interface,
|
||||||
|
rmark: option.RoutingMark,
|
||||||
|
prefer: option.IPVersion,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
outbound.dialer = option.NewDialer(outbound.DialOptions())
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
outbound.runCtx = ctx
|
||||||
|
outbound.runCancel = cancel
|
||||||
|
|
||||||
|
privKeyB64, err := base64.StdEncoding.DecodeString(option.PrivateKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to decode private key: %v", err)
|
||||||
|
}
|
||||||
|
privKey, err := x509.ParseECPrivateKey(privKeyB64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse private key: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
endpointPubKeyB64, err := base64.StdEncoding.DecodeString(option.PublicKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to decode public key: %v", err)
|
||||||
|
}
|
||||||
|
pubKey, err := x509.ParsePKIXPublicKey(endpointPubKeyB64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse public key: %v", err)
|
||||||
|
}
|
||||||
|
ecPubKey, ok := pubKey.(*ecdsa.PublicKey)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("failed to assert public key as ECDSA")
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := option.URI
|
||||||
|
if uri == "" {
|
||||||
|
uri = masque.ConnectURI
|
||||||
|
}
|
||||||
|
outbound.uri = uri
|
||||||
|
|
||||||
|
sni := option.SNI
|
||||||
|
if sni == "" {
|
||||||
|
sni = masque.ConnectSNI
|
||||||
|
}
|
||||||
|
|
||||||
|
tlsConfig, err := masque.PrepareTlsConfig(privKey, ecPubKey, sni)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to prepare TLS config: %v\n", err)
|
||||||
|
}
|
||||||
|
outbound.tlsConfig = tlsConfig
|
||||||
|
|
||||||
|
outbound.quicConfig = &quic.Config{
|
||||||
|
EnableDatagrams: true,
|
||||||
|
InitialPacketSize: 1242,
|
||||||
|
KeepAlivePeriod: 30 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
prefixes, err := option.Prefixes()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
outbound.option = option
|
||||||
|
|
||||||
|
mtu := option.MTU
|
||||||
|
if mtu == 0 {
|
||||||
|
mtu = 1280
|
||||||
|
}
|
||||||
|
if len(prefixes) == 0 {
|
||||||
|
return nil, errors.New("missing local address")
|
||||||
|
}
|
||||||
|
outbound.tunDevice, err = wireguard.NewStackDevice(prefixes, uint32(mtu))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("create device: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var has6 bool
|
||||||
|
for _, address := range prefixes {
|
||||||
|
if !address.Addr().Unmap().Is4() {
|
||||||
|
has6 = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if option.RemoteDnsResolve && len(option.Dns) > 0 {
|
||||||
|
nss, err := dns.ParseNameServer(option.Dns)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for i := range nss {
|
||||||
|
nss[i].ProxyAdapter = outbound
|
||||||
|
}
|
||||||
|
outbound.resolver = dns.NewResolver(dns.Config{
|
||||||
|
Main: nss,
|
||||||
|
IPv6: has6,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return outbound, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Masque) run(ctx context.Context) error {
|
||||||
|
if w.running.Load() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
w.runMutex.Lock()
|
||||||
|
defer w.runMutex.Unlock()
|
||||||
|
// double-check like sync.Once
|
||||||
|
if w.running.Load() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if w.runCtx.Err() != nil {
|
||||||
|
return w.runCtx.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
if !w.runDevice.Load() {
|
||||||
|
err := w.tunDevice.Start()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
w.runDevice.Store(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
udpAddr, err := resolveUDPAddr(ctx, "udp", w.addr, w.prefer)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
pc, err := w.dialer.ListenPacket(ctx, "udp", "", udpAddr.AddrPort())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
quicConn, err := quic.Dial(ctx, pc, udpAddr, w.tlsConfig, w.quicConfig)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
common.SetCongestionController(quicConn, w.option.CongestionController, w.option.CWND)
|
||||||
|
|
||||||
|
tr, ipConn, err := masque.ConnectTunnel(ctx, quicConn, w.uri)
|
||||||
|
if err != nil {
|
||||||
|
_ = pc.Close()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
w.running.Store(true)
|
||||||
|
|
||||||
|
runCtx, runCancel := context.WithCancel(w.runCtx)
|
||||||
|
contextutils.AfterFunc(runCtx, func() {
|
||||||
|
w.running.Store(false)
|
||||||
|
_ = ipConn.Close()
|
||||||
|
_ = tr.Close()
|
||||||
|
_ = pc.Close()
|
||||||
|
})
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer runCancel()
|
||||||
|
buf := pool.Get(pool.UDPBufferSize)
|
||||||
|
defer pool.Put(buf)
|
||||||
|
bufs := [][]byte{buf}
|
||||||
|
sizes := []int{0}
|
||||||
|
for runCtx.Err() == nil {
|
||||||
|
_, err := w.tunDevice.Read(bufs, sizes, 0)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorln("[Masque](%s) error reading from TUN device: %v", w.name, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
icmp, err := ipConn.WritePacket(buf[:sizes[0]])
|
||||||
|
if err != nil {
|
||||||
|
if errors.As(err, new(*connectip.CloseError)) {
|
||||||
|
log.Errorln("[Masque](%s) connection closed while writing to IP connection: %v", w.name, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Warnln("[Masque](%s) error writing to IP connection: %v, continuing...", w.name, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(icmp) > 0 {
|
||||||
|
if _, err := w.tunDevice.Write([][]byte{icmp}, 0); err != nil {
|
||||||
|
log.Warnln("[Masque](%s) error writing ICMP to TUN device: %v, continuing...", w.name, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer runCancel()
|
||||||
|
buf := pool.Get(pool.UDPBufferSize)
|
||||||
|
defer pool.Put(buf)
|
||||||
|
for runCtx.Err() == nil {
|
||||||
|
n, err := ipConn.ReadPacket(buf)
|
||||||
|
if err != nil {
|
||||||
|
if errors.As(err, new(*connectip.CloseError)) {
|
||||||
|
log.Errorln("[Masque](%s) connection closed while writing to IP connection: %v", w.name, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Warnln("[Masque](%s) error reading from IP connection: %v, continuing...", w.name, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, err := w.tunDevice.Write([][]byte{buf[:n]}, 0); err != nil {
|
||||||
|
log.Errorln("[Masque](%s) error writing to TUN device: %v", w.name, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close implements C.ProxyAdapter
|
||||||
|
func (w *Masque) Close() error {
|
||||||
|
w.runCancel()
|
||||||
|
if w.tunDevice != nil {
|
||||||
|
w.tunDevice.Close()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Masque) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
|
var conn net.Conn
|
||||||
|
if err = w.run(ctx); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !metadata.Resolved() || w.resolver != nil {
|
||||||
|
r := resolver.DefaultResolver
|
||||||
|
if w.resolver != nil {
|
||||||
|
r = w.resolver
|
||||||
|
}
|
||||||
|
options := w.DialOptions()
|
||||||
|
options = append(options, dialer.WithResolver(r))
|
||||||
|
options = append(options, dialer.WithNetDialer(wgNetDialer{tunDevice: w.tunDevice}))
|
||||||
|
conn, err = dialer.NewDialer(options...).DialContext(ctx, "tcp", metadata.RemoteAddress())
|
||||||
|
} else {
|
||||||
|
conn, err = w.tunDevice.DialContext(ctx, "tcp", M.SocksaddrFrom(metadata.DstIP, metadata.DstPort).Unwrap())
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if conn == nil {
|
||||||
|
return nil, errors.New("conn is nil")
|
||||||
|
}
|
||||||
|
return NewConn(conn, w), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Masque) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
|
var pc net.PacketConn
|
||||||
|
if err = w.run(ctx); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err = w.ResolveUDP(ctx, metadata); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
pc, err = w.tunDevice.ListenPacket(ctx, M.SocksaddrFrom(metadata.DstIP, metadata.DstPort).Unwrap())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if pc == nil {
|
||||||
|
return nil, errors.New("packetConn is nil")
|
||||||
|
}
|
||||||
|
return newPacketConn(pc, w), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Masque) ResolveUDP(ctx context.Context, metadata *C.Metadata) error {
|
||||||
|
if (!metadata.Resolved() || w.resolver != nil) && metadata.Host != "" {
|
||||||
|
r := resolver.DefaultResolver
|
||||||
|
if w.resolver != nil {
|
||||||
|
r = w.resolver
|
||||||
|
}
|
||||||
|
ip, err := resolver.ResolveIPWithResolver(ctx, metadata.Host, r)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("can't resolve ip: %w", err)
|
||||||
|
}
|
||||||
|
metadata.DstIP = ip
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ProxyInfo implements C.ProxyAdapter
|
||||||
|
func (w *Masque) ProxyInfo() C.ProxyInfo {
|
||||||
|
info := w.Base.ProxyInfo()
|
||||||
|
info.DialerProxy = w.option.DialerProxy
|
||||||
|
return info
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsL3Protocol implements C.ProxyAdapter
|
||||||
|
func (w *Masque) IsL3Protocol(metadata *C.Metadata) bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
@@ -4,17 +4,18 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"net/netip"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
CN "github.com/metacubex/mihomo/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"github.com/metacubex/mihomo/component/resolver"
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
|
|
||||||
mieruclient "github.com/enfein/mieru/v3/apis/client"
|
mieruclient "github.com/enfein/mieru/v3/apis/client"
|
||||||
mierucommon "github.com/enfein/mieru/v3/apis/common"
|
mierucommon "github.com/enfein/mieru/v3/apis/common"
|
||||||
mierumodel "github.com/enfein/mieru/v3/apis/model"
|
mierumodel "github.com/enfein/mieru/v3/apis/model"
|
||||||
|
mierutp "github.com/enfein/mieru/v3/apis/trafficpattern"
|
||||||
mierupb "github.com/enfein/mieru/v3/pkg/appctl/appctlpb"
|
mierupb "github.com/enfein/mieru/v3/pkg/appctl/appctlpb"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
)
|
)
|
||||||
@@ -28,20 +29,61 @@ type Mieru struct {
|
|||||||
|
|
||||||
type MieruOption struct {
|
type MieruOption struct {
|
||||||
BasicOption
|
BasicOption
|
||||||
Name string `proxy:"name"`
|
Name string `proxy:"name"`
|
||||||
Server string `proxy:"server"`
|
Server string `proxy:"server"`
|
||||||
Port int `proxy:"port,omitempty"`
|
Port int `proxy:"port,omitempty"`
|
||||||
PortRange string `proxy:"port-range,omitempty"`
|
PortRange string `proxy:"port-range,omitempty"`
|
||||||
Transport string `proxy:"transport"`
|
Transport string `proxy:"transport"`
|
||||||
UDP bool `proxy:"udp,omitempty"`
|
UDP bool `proxy:"udp,omitempty"`
|
||||||
UserName string `proxy:"username"`
|
UserName string `proxy:"username"`
|
||||||
Password string `proxy:"password"`
|
Password string `proxy:"password"`
|
||||||
Multiplexing string `proxy:"multiplexing,omitempty"`
|
Multiplexing string `proxy:"multiplexing,omitempty"`
|
||||||
|
HandshakeMode string `proxy:"handshake-mode,omitempty"`
|
||||||
|
TrafficPattern string `proxy:"traffic-pattern,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type mieruPacketDialer struct {
|
||||||
|
C.Dialer
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ mierucommon.PacketDialer = (*mieruPacketDialer)(nil)
|
||||||
|
|
||||||
|
func (pd mieruPacketDialer) ListenPacket(ctx context.Context, network, laddr, raddr string) (net.PacketConn, error) {
|
||||||
|
rAddrPort, err := netip.ParseAddrPort(raddr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid address %s: %w", raddr, err)
|
||||||
|
}
|
||||||
|
return pd.Dialer.ListenPacket(ctx, network, laddr, rAddrPort)
|
||||||
|
}
|
||||||
|
|
||||||
|
type mieruDNSResolver struct {
|
||||||
|
prefer C.DNSPrefer
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ mierucommon.DNSResolver = (*mieruDNSResolver)(nil)
|
||||||
|
|
||||||
|
func (dr mieruDNSResolver) LookupIP(ctx context.Context, network, host string) (_ []net.IP, err error) {
|
||||||
|
var ip netip.Addr
|
||||||
|
switch dr.prefer {
|
||||||
|
case C.IPv4Only:
|
||||||
|
ip, err = resolver.ResolveIPv4WithResolver(ctx, host, resolver.ProxyServerHostResolver)
|
||||||
|
case C.IPv6Only:
|
||||||
|
ip, err = resolver.ResolveIPv6WithResolver(ctx, host, resolver.ProxyServerHostResolver)
|
||||||
|
case C.IPv6Prefer:
|
||||||
|
ip, err = resolver.ResolveIPPrefer6WithResolver(ctx, host, resolver.ProxyServerHostResolver)
|
||||||
|
default:
|
||||||
|
ip, err = resolver.ResolveIPWithResolver(ctx, host, resolver.ProxyServerHostResolver)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("can't resolve ip: %w", err)
|
||||||
|
}
|
||||||
|
// TODO: handle IP4P (due to interface limitations, it's currently impossible to modify the port here)
|
||||||
|
return []net.IP{ip.AsSlice()}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (m *Mieru) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
func (m *Mieru) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
||||||
if err := m.ensureClientIsRunning(opts...); err != nil {
|
if err := m.ensureClientIsRunning(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
addr := metadataToMieruNetAddrSpec(metadata)
|
addr := metadataToMieruNetAddrSpec(metadata)
|
||||||
@@ -53,15 +95,18 @@ func (m *Mieru) DialContext(ctx context.Context, metadata *C.Metadata, opts ...d
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (m *Mieru) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
func (m *Mieru) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
if err := m.ensureClientIsRunning(opts...); err != nil {
|
if err = m.ResolveUDP(ctx, metadata); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := m.ensureClientIsRunning(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
c, err := m.client.DialContext(ctx, metadata.UDPAddr())
|
c, err := m.client.DialContext(ctx, metadata.UDPAddr())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("dial to %s failed: %w", metadata.UDPAddr(), err)
|
return nil, fmt.Errorf("dial to %s failed: %w", metadata.UDPAddr(), err)
|
||||||
}
|
}
|
||||||
return newPacketConn(CN.NewThreadSafePacketConn(mierucommon.NewUDPAssociateWrapper(mierucommon.NewPacketOverStreamTunnel(c))), m), nil
|
return newPacketConn(N.NewThreadSafePacketConn(mierucommon.NewUDPAssociateWrapper(mierucommon.NewPacketOverStreamTunnel(c))), m), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SupportUOT implements C.ProxyAdapter
|
// SupportUOT implements C.ProxyAdapter
|
||||||
@@ -76,7 +121,7 @@ func (m *Mieru) ProxyInfo() C.ProxyInfo {
|
|||||||
return info
|
return info
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Mieru) ensureClientIsRunning(opts ...dialer.Option) error {
|
func (m *Mieru) ensureClientIsRunning() error {
|
||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
defer m.mu.Unlock()
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
@@ -85,19 +130,13 @@ func (m *Mieru) ensureClientIsRunning(opts ...dialer.Option) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create a dialer and add it to the client config, before starting the client.
|
// Create a dialer and add it to the client config, before starting the client.
|
||||||
var dialer C.Dialer = dialer.NewDialer(m.Base.DialOptions(opts...)...)
|
|
||||||
var err error
|
|
||||||
if len(m.option.DialerProxy) > 0 {
|
|
||||||
dialer, err = proxydialer.NewByName(m.option.DialerProxy, dialer)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
config, err := m.client.Load()
|
config, err := m.client.Load()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
config.Dialer = dialer
|
config.Dialer = m.dialer
|
||||||
|
config.PacketDialer = mieruPacketDialer{Dialer: m.dialer}
|
||||||
|
config.Resolver = mieruDNSResolver{prefer: m.prefer}
|
||||||
if err := m.client.Store(config); err != nil {
|
if err := m.client.Store(config); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -130,16 +169,18 @@ func NewMieru(option MieruOption) (*Mieru, error) {
|
|||||||
Base: &Base{
|
Base: &Base{
|
||||||
name: option.Name,
|
name: option.Name,
|
||||||
addr: addr,
|
addr: addr,
|
||||||
iface: option.Interface,
|
|
||||||
tp: C.Mieru,
|
tp: C.Mieru,
|
||||||
|
pdName: option.ProviderName,
|
||||||
udp: option.UDP,
|
udp: option.UDP,
|
||||||
xudp: false,
|
xudp: false,
|
||||||
|
iface: option.Interface,
|
||||||
rmark: option.RoutingMark,
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: option.IPVersion,
|
||||||
},
|
},
|
||||||
option: &option,
|
option: &option,
|
||||||
client: c,
|
client: c,
|
||||||
}
|
}
|
||||||
|
outbound.dialer = option.NewDialer(outbound.DialOptions())
|
||||||
return outbound, nil
|
return outbound, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,23 +195,21 @@ func (m *Mieru) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func metadataToMieruNetAddrSpec(metadata *C.Metadata) mierumodel.NetAddrSpec {
|
func metadataToMieruNetAddrSpec(metadata *C.Metadata) mierumodel.NetAddrSpec {
|
||||||
|
spec := mierumodel.NetAddrSpec{
|
||||||
|
Net: metadata.NetWork.String(),
|
||||||
|
}
|
||||||
if metadata.Host != "" {
|
if metadata.Host != "" {
|
||||||
return mierumodel.NetAddrSpec{
|
spec.AddrSpec = mierumodel.AddrSpec{
|
||||||
AddrSpec: mierumodel.AddrSpec{
|
FQDN: metadata.Host,
|
||||||
FQDN: metadata.Host,
|
Port: int(metadata.DstPort),
|
||||||
Port: int(metadata.DstPort),
|
|
||||||
},
|
|
||||||
Net: "tcp",
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return mierumodel.NetAddrSpec{
|
spec.AddrSpec = mierumodel.AddrSpec{
|
||||||
AddrSpec: mierumodel.AddrSpec{
|
IP: metadata.DstIP.AsSlice(),
|
||||||
IP: metadata.DstIP.AsSlice(),
|
Port: int(metadata.DstPort),
|
||||||
Port: int(metadata.DstPort),
|
|
||||||
},
|
|
||||||
Net: "tcp",
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return spec
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildMieruClientConfig(option MieruOption) (*mieruclient.ClientConfig, error) {
|
func buildMieruClientConfig(option MieruOption) (*mieruclient.ClientConfig, error) {
|
||||||
@@ -178,7 +217,13 @@ func buildMieruClientConfig(option MieruOption) (*mieruclient.ClientConfig, erro
|
|||||||
return nil, fmt.Errorf("failed to validate mieru option: %w", err)
|
return nil, fmt.Errorf("failed to validate mieru option: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
transportProtocol := mierupb.TransportProtocol_TCP.Enum()
|
var transportProtocol = mierupb.TransportProtocol_UNKNOWN_TRANSPORT_PROTOCOL.Enum()
|
||||||
|
switch option.Transport {
|
||||||
|
case "TCP":
|
||||||
|
transportProtocol = mierupb.TransportProtocol_TCP.Enum()
|
||||||
|
case "UDP":
|
||||||
|
transportProtocol = mierupb.TransportProtocol_UDP.Enum()
|
||||||
|
}
|
||||||
var server *mierupb.ServerEndpoint
|
var server *mierupb.ServerEndpoint
|
||||||
if net.ParseIP(option.Server) != nil {
|
if net.ParseIP(option.Server) != nil {
|
||||||
// server is an IP address
|
// server is an IP address
|
||||||
@@ -236,12 +281,22 @@ func buildMieruClientConfig(option MieruOption) (*mieruclient.ClientConfig, erro
|
|||||||
},
|
},
|
||||||
Servers: []*mierupb.ServerEndpoint{server},
|
Servers: []*mierupb.ServerEndpoint{server},
|
||||||
},
|
},
|
||||||
|
DNSConfig: &mierucommon.ClientDNSConfig{
|
||||||
|
BypassDialerDNS: true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
if multiplexing, ok := mierupb.MultiplexingLevel_value[option.Multiplexing]; ok {
|
if multiplexing, ok := mierupb.MultiplexingLevel_value[option.Multiplexing]; ok {
|
||||||
config.Profile.Multiplexing = &mierupb.MultiplexingConfig{
|
config.Profile.Multiplexing = &mierupb.MultiplexingConfig{
|
||||||
Level: mierupb.MultiplexingLevel(multiplexing).Enum(),
|
Level: mierupb.MultiplexingLevel(multiplexing).Enum(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if handshakeMode, ok := mierupb.HandshakeMode_value[option.HandshakeMode]; ok {
|
||||||
|
config.Profile.HandshakeMode = (*mierupb.HandshakeMode)(&handshakeMode)
|
||||||
|
}
|
||||||
|
if option.TrafficPattern != "" {
|
||||||
|
trafficPattern, _ := mierutp.Decode(option.TrafficPattern)
|
||||||
|
config.Profile.TrafficPattern = trafficPattern
|
||||||
|
}
|
||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -277,8 +332,8 @@ func validateMieruOption(option MieruOption) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if option.Transport != "TCP" {
|
if option.Transport != "TCP" && option.Transport != "UDP" {
|
||||||
return fmt.Errorf("transport must be TCP")
|
return fmt.Errorf("transport must be TCP or UDP")
|
||||||
}
|
}
|
||||||
if option.UserName == "" {
|
if option.UserName == "" {
|
||||||
return fmt.Errorf("username is empty")
|
return fmt.Errorf("username is empty")
|
||||||
@@ -291,6 +346,20 @@ func validateMieruOption(option MieruOption) error {
|
|||||||
return fmt.Errorf("invalid multiplexing level: %s", option.Multiplexing)
|
return fmt.Errorf("invalid multiplexing level: %s", option.Multiplexing)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if option.HandshakeMode != "" {
|
||||||
|
if _, ok := mierupb.HandshakeMode_value[option.HandshakeMode]; !ok {
|
||||||
|
return fmt.Errorf("invalid handshake mode: %s", option.HandshakeMode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if option.TrafficPattern != "" {
|
||||||
|
trafficPattern, err := mierutp.Decode(option.TrafficPattern)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to decode traffic pattern %q: %w", option.TrafficPattern, err)
|
||||||
|
}
|
||||||
|
if err := mierutp.Validate(trafficPattern); err != nil {
|
||||||
|
return fmt.Errorf("invalid traffic pattern %q: %w", option.TrafficPattern, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,12 +31,13 @@ func TestNewMieru(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
option: MieruOption{
|
option: MieruOption{
|
||||||
Name: "test",
|
Name: "test",
|
||||||
Server: "example.com",
|
Server: "example.com",
|
||||||
Port: 10003,
|
Port: 10003,
|
||||||
Transport: "TCP",
|
Transport: "UDP",
|
||||||
UserName: "test",
|
UserName: "test",
|
||||||
Password: "test",
|
Password: "test",
|
||||||
|
TrafficPattern: "GgQIARAK",
|
||||||
},
|
},
|
||||||
wantBaseAddr: "example.com:10003",
|
wantBaseAddr: "example.com:10003",
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -12,12 +12,15 @@ import (
|
|||||||
|
|
||||||
type RealityOptions struct {
|
type RealityOptions struct {
|
||||||
PublicKey string `proxy:"public-key"`
|
PublicKey string `proxy:"public-key"`
|
||||||
ShortID string `proxy:"short-id"`
|
ShortID string `proxy:"short-id,omitempty"`
|
||||||
|
|
||||||
|
SupportX25519MLKEM768 bool `proxy:"support-x25519mlkem768,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o RealityOptions) Parse() (*tlsC.RealityConfig, error) {
|
func (o RealityOptions) Parse() (*tlsC.RealityConfig, error) {
|
||||||
if o.PublicKey != "" {
|
if o.PublicKey != "" {
|
||||||
config := new(tlsC.RealityConfig)
|
config := new(tlsC.RealityConfig)
|
||||||
|
config.SupportX25519MLKEM768 = o.SupportX25519MLKEM768
|
||||||
|
|
||||||
const x25519ScalarSize = 32
|
const x25519ScalarSize = 32
|
||||||
publicKey, err := base64.RawURLEncoding.DecodeString(o.PublicKey)
|
publicKey, err := base64.RawURLEncoding.DecodeString(o.PublicKey)
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
"net/netip"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/buf"
|
"github.com/metacubex/mihomo/common/buf"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,11 +17,12 @@ type Reject struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type RejectOption struct {
|
type RejectOption struct {
|
||||||
|
BasicOption
|
||||||
Name string `proxy:"name"`
|
Name string `proxy:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (r *Reject) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
func (r *Reject) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
||||||
if r.drop {
|
if r.drop {
|
||||||
return NewConn(dropConn{}, r), nil
|
return NewConn(dropConn{}, r), nil
|
||||||
}
|
}
|
||||||
@@ -29,10 +30,20 @@ func (r *Reject) DialContext(ctx context.Context, metadata *C.Metadata, opts ...
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (r *Reject) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
func (r *Reject) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
||||||
|
if err := r.ResolveUDP(ctx, metadata); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return newPacketConn(&nopPacketConn{}, r), nil
|
return newPacketConn(&nopPacketConn{}, r), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Reject) ResolveUDP(ctx context.Context, metadata *C.Metadata) error {
|
||||||
|
if !metadata.Resolved() {
|
||||||
|
metadata.DstIP = netip.IPv4Unspecified()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func NewRejectWithOption(option RejectOption) *Reject {
|
func NewRejectWithOption(option RejectOption) *Reject {
|
||||||
return &Reject{
|
return &Reject{
|
||||||
Base: &Base{
|
Base: &Base{
|
||||||
|
|||||||
@@ -2,27 +2,25 @@ package outbound
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/metacubex/mihomo/common/structure"
|
"github.com/metacubex/mihomo/common/structure"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
|
||||||
"github.com/metacubex/mihomo/component/resolver"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
|
"github.com/metacubex/mihomo/ntp"
|
||||||
gost "github.com/metacubex/mihomo/transport/gost-plugin"
|
gost "github.com/metacubex/mihomo/transport/gost-plugin"
|
||||||
|
"github.com/metacubex/mihomo/transport/kcptun"
|
||||||
"github.com/metacubex/mihomo/transport/restls"
|
"github.com/metacubex/mihomo/transport/restls"
|
||||||
obfs "github.com/metacubex/mihomo/transport/simple-obfs"
|
obfs "github.com/metacubex/mihomo/transport/simple-obfs"
|
||||||
shadowtls "github.com/metacubex/mihomo/transport/sing-shadowtls"
|
shadowtls "github.com/metacubex/mihomo/transport/sing-shadowtls"
|
||||||
v2rayObfs "github.com/metacubex/mihomo/transport/v2ray-plugin"
|
v2rayObfs "github.com/metacubex/mihomo/transport/v2ray-plugin"
|
||||||
|
|
||||||
shadowsocks "github.com/metacubex/sing-shadowsocks2"
|
shadowsocks "github.com/metacubex/sing-shadowsocks2"
|
||||||
"github.com/sagernet/sing/common/bufio"
|
"github.com/metacubex/sing/common/bufio"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/metacubex/sing/common/metadata"
|
||||||
"github.com/sagernet/sing/common/uot"
|
"github.com/metacubex/sing/common/uot"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ShadowSocks struct {
|
type ShadowSocks struct {
|
||||||
@@ -37,6 +35,7 @@ type ShadowSocks struct {
|
|||||||
gostOption *gost.Option
|
gostOption *gost.Option
|
||||||
shadowTLSOption *shadowtls.ShadowTLSOption
|
shadowTLSOption *shadowtls.ShadowTLSOption
|
||||||
restlsConfig *restls.Config
|
restlsConfig *restls.Config
|
||||||
|
kcptunClient *kcptun.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
type ShadowSocksOption struct {
|
type ShadowSocksOption struct {
|
||||||
@@ -64,7 +63,10 @@ type v2rayObfsOption struct {
|
|||||||
Host string `obfs:"host,omitempty"`
|
Host string `obfs:"host,omitempty"`
|
||||||
Path string `obfs:"path,omitempty"`
|
Path string `obfs:"path,omitempty"`
|
||||||
TLS bool `obfs:"tls,omitempty"`
|
TLS bool `obfs:"tls,omitempty"`
|
||||||
|
ECHOpts ECHOptions `obfs:"ech-opts,omitempty"`
|
||||||
Fingerprint string `obfs:"fingerprint,omitempty"`
|
Fingerprint string `obfs:"fingerprint,omitempty"`
|
||||||
|
Certificate string `obfs:"certificate,omitempty"`
|
||||||
|
PrivateKey string `obfs:"private-key,omitempty"`
|
||||||
Headers map[string]string `obfs:"headers,omitempty"`
|
Headers map[string]string `obfs:"headers,omitempty"`
|
||||||
SkipCertVerify bool `obfs:"skip-cert-verify,omitempty"`
|
SkipCertVerify bool `obfs:"skip-cert-verify,omitempty"`
|
||||||
Mux bool `obfs:"mux,omitempty"`
|
Mux bool `obfs:"mux,omitempty"`
|
||||||
@@ -77,18 +79,24 @@ type gostObfsOption struct {
|
|||||||
Host string `obfs:"host,omitempty"`
|
Host string `obfs:"host,omitempty"`
|
||||||
Path string `obfs:"path,omitempty"`
|
Path string `obfs:"path,omitempty"`
|
||||||
TLS bool `obfs:"tls,omitempty"`
|
TLS bool `obfs:"tls,omitempty"`
|
||||||
|
ECHOpts ECHOptions `obfs:"ech-opts,omitempty"`
|
||||||
Fingerprint string `obfs:"fingerprint,omitempty"`
|
Fingerprint string `obfs:"fingerprint,omitempty"`
|
||||||
|
Certificate string `obfs:"certificate,omitempty"`
|
||||||
|
PrivateKey string `obfs:"private-key,omitempty"`
|
||||||
Headers map[string]string `obfs:"headers,omitempty"`
|
Headers map[string]string `obfs:"headers,omitempty"`
|
||||||
SkipCertVerify bool `obfs:"skip-cert-verify,omitempty"`
|
SkipCertVerify bool `obfs:"skip-cert-verify,omitempty"`
|
||||||
Mux bool `obfs:"mux,omitempty"`
|
Mux bool `obfs:"mux,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type shadowTLSOption struct {
|
type shadowTLSOption struct {
|
||||||
Password string `obfs:"password"`
|
Password string `obfs:"password,omitempty"`
|
||||||
Host string `obfs:"host"`
|
Host string `obfs:"host"`
|
||||||
Fingerprint string `obfs:"fingerprint,omitempty"`
|
Fingerprint string `obfs:"fingerprint,omitempty"`
|
||||||
SkipCertVerify bool `obfs:"skip-cert-verify,omitempty"`
|
Certificate string `obfs:"certificate,omitempty"`
|
||||||
Version int `obfs:"version,omitempty"`
|
PrivateKey string `obfs:"private-key,omitempty"`
|
||||||
|
SkipCertVerify bool `obfs:"skip-cert-verify,omitempty"`
|
||||||
|
Version int `obfs:"version,omitempty"`
|
||||||
|
ALPN []string `obfs:"alpn,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type restlsOption struct {
|
type restlsOption struct {
|
||||||
@@ -98,6 +106,34 @@ type restlsOption struct {
|
|||||||
RestlsScript string `obfs:"restls-script,omitempty"`
|
RestlsScript string `obfs:"restls-script,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type kcpTunOption struct {
|
||||||
|
Key string `obfs:"key,omitempty"`
|
||||||
|
Crypt string `obfs:"crypt,omitempty"`
|
||||||
|
Mode string `obfs:"mode,omitempty"`
|
||||||
|
Conn int `obfs:"conn,omitempty"`
|
||||||
|
AutoExpire int `obfs:"autoexpire,omitempty"`
|
||||||
|
ScavengeTTL int `obfs:"scavengettl,omitempty"`
|
||||||
|
MTU int `obfs:"mtu,omitempty"`
|
||||||
|
RateLimit int `obfs:"ratelimit,omitempty"`
|
||||||
|
SndWnd int `obfs:"sndwnd,omitempty"`
|
||||||
|
RcvWnd int `obfs:"rcvwnd,omitempty"`
|
||||||
|
DataShard int `obfs:"datashard,omitempty"`
|
||||||
|
ParityShard int `obfs:"parityshard,omitempty"`
|
||||||
|
DSCP int `obfs:"dscp,omitempty"`
|
||||||
|
NoComp bool `obfs:"nocomp,omitempty"`
|
||||||
|
AckNodelay bool `obfs:"acknodelay,omitempty"`
|
||||||
|
NoDelay int `obfs:"nodelay,omitempty"`
|
||||||
|
Interval int `obfs:"interval,omitempty"`
|
||||||
|
Resend int `obfs:"resend,omitempty"`
|
||||||
|
NoCongestion int `obfs:"nc,omitempty"`
|
||||||
|
SockBuf int `obfs:"sockbuf,omitempty"`
|
||||||
|
SmuxVer int `obfs:"smuxver,omitempty"`
|
||||||
|
SmuxBuf int `obfs:"smuxbuf,omitempty"`
|
||||||
|
FrameSize int `obfs:"framesize,omitempty"`
|
||||||
|
StreamBuf int `obfs:"streambuf,omitempty"`
|
||||||
|
KeepAlive int `obfs:"keepalive,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
// StreamConnContext implements C.ProxyAdapter
|
// StreamConnContext implements C.ProxyAdapter
|
||||||
func (ss *ShadowSocks) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ net.Conn, err error) {
|
func (ss *ShadowSocks) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ net.Conn, err error) {
|
||||||
useEarly := false
|
useEarly := false
|
||||||
@@ -154,19 +190,28 @@ func (ss *ShadowSocks) StreamConnContext(ctx context.Context, c net.Conn, metada
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (ss *ShadowSocks) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
func (ss *ShadowSocks) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
return ss.DialContextWithDialer(ctx, dialer.NewDialer(ss.Base.DialOptions(opts...)...), metadata)
|
var c net.Conn
|
||||||
}
|
if ss.kcptunClient != nil {
|
||||||
|
c, err = ss.kcptunClient.OpenStream(ctx, func(ctx context.Context) (net.PacketConn, net.Addr, error) {
|
||||||
|
if err = ss.ResolveUDP(ctx, metadata); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
addr, err := resolveUDPAddr(ctx, "udp", ss.addr, ss.prefer)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
// DialContextWithDialer implements C.ProxyAdapter
|
pc, err := ss.dialer.ListenPacket(ctx, "udp", "", addr.AddrPort())
|
||||||
func (ss *ShadowSocks) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
if err != nil {
|
||||||
if len(ss.option.DialerProxy) > 0 {
|
return nil, nil, err
|
||||||
dialer, err = proxydialer.NewByName(ss.option.DialerProxy, dialer)
|
}
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return pc, addr, nil
|
||||||
}
|
})
|
||||||
|
} else {
|
||||||
|
c, err = ss.dialer.DialContext(ctx, "tcp", ss.addr)
|
||||||
}
|
}
|
||||||
c, err := dialer.DialContext(ctx, "tcp", ss.addr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error: %w", ss.addr, err)
|
return nil, fmt.Errorf("%s connect error: %w", ss.addr, err)
|
||||||
}
|
}
|
||||||
@@ -180,31 +225,23 @@ func (ss *ShadowSocks) DialContextWithDialer(ctx context.Context, dialer C.Diale
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (ss *ShadowSocks) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
func (ss *ShadowSocks) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
||||||
return ss.ListenPacketWithDialer(ctx, dialer.NewDialer(ss.Base.DialOptions(opts...)...), metadata)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListenPacketWithDialer implements C.ProxyAdapter
|
|
||||||
func (ss *ShadowSocks) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
|
||||||
if ss.option.UDPOverTCP {
|
if ss.option.UDPOverTCP {
|
||||||
tcpConn, err := ss.DialContextWithDialer(ctx, dialer, metadata)
|
tcpConn, err := ss.DialContext(ctx, metadata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return ss.ListenPacketOnStreamConn(ctx, tcpConn, metadata)
|
return ss.ListenPacketOnStreamConn(ctx, tcpConn, metadata)
|
||||||
}
|
}
|
||||||
if len(ss.option.DialerProxy) > 0 {
|
if err := ss.ResolveUDP(ctx, metadata); err != nil {
|
||||||
dialer, err = proxydialer.NewByName(ss.option.DialerProxy, dialer)
|
return nil, err
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
addr, err := resolveUDPAddr(ctx, "udp", ss.addr, ss.prefer)
|
addr, err := resolveUDPAddr(ctx, "udp", ss.addr, ss.prefer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
pc, err := dialer.ListenPacket(ctx, "udp", "", addr.AddrPort())
|
pc, err := ss.dialer.ListenPacket(ctx, "udp", "", addr.AddrPort())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -212,11 +249,6 @@ func (ss *ShadowSocks) ListenPacketWithDialer(ctx context.Context, dialer C.Dial
|
|||||||
return newPacketConn(pc, ss), nil
|
return newPacketConn(pc, ss), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SupportWithDialer implements C.ProxyAdapter
|
|
||||||
func (ss *ShadowSocks) SupportWithDialer() C.NetWork {
|
|
||||||
return C.ALLNet
|
|
||||||
}
|
|
||||||
|
|
||||||
// ProxyInfo implements C.ProxyAdapter
|
// ProxyInfo implements C.ProxyAdapter
|
||||||
func (ss *ShadowSocks) ProxyInfo() C.ProxyInfo {
|
func (ss *ShadowSocks) ProxyInfo() C.ProxyInfo {
|
||||||
info := ss.Base.ProxyInfo()
|
info := ss.Base.ProxyInfo()
|
||||||
@@ -227,15 +259,9 @@ func (ss *ShadowSocks) ProxyInfo() C.ProxyInfo {
|
|||||||
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
||||||
func (ss *ShadowSocks) ListenPacketOnStreamConn(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
func (ss *ShadowSocks) ListenPacketOnStreamConn(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
if ss.option.UDPOverTCP {
|
if ss.option.UDPOverTCP {
|
||||||
// ss uot use stream-oriented udp with a special address, so we need a net.UDPAddr
|
if err = ss.ResolveUDP(ctx, metadata); err != nil {
|
||||||
if !metadata.Resolved() {
|
return nil, err
|
||||||
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("can't resolve ip")
|
|
||||||
}
|
|
||||||
metadata.DstIP = ip
|
|
||||||
}
|
}
|
||||||
|
|
||||||
destination := M.SocksaddrFromNet(metadata.UDPAddr())
|
destination := M.SocksaddrFromNet(metadata.UDPAddr())
|
||||||
if ss.option.UDPOverTCPVersion == uot.LegacyVersion {
|
if ss.option.UDPOverTCPVersion == uot.LegacyVersion {
|
||||||
return newPacketConn(N.NewThreadSafePacketConn(uot.NewConn(c, uot.Request{Destination: destination})), ss), nil
|
return newPacketConn(N.NewThreadSafePacketConn(uot.NewConn(c, uot.Request{Destination: destination})), ss), nil
|
||||||
@@ -251,10 +277,18 @@ func (ss *ShadowSocks) SupportUOT() bool {
|
|||||||
return ss.option.UDPOverTCP
|
return ss.option.UDPOverTCP
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ss *ShadowSocks) Close() error {
|
||||||
|
if ss.kcptunClient != nil {
|
||||||
|
return ss.kcptunClient.Close()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
||||||
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
||||||
method, err := shadowsocks.CreateMethod(context.Background(), option.Cipher, shadowsocks.MethodOptions{
|
method, err := shadowsocks.CreateMethod(option.Cipher, shadowsocks.MethodOptions{
|
||||||
Password: option.Password,
|
Password: option.Password,
|
||||||
|
TimeFunc: ntp.Now,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("ss %s cipher: %s initialize error: %w", addr, option.Cipher, err)
|
return nil, fmt.Errorf("ss %s cipher: %s initialize error: %w", addr, option.Cipher, err)
|
||||||
@@ -265,6 +299,7 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
|||||||
var obfsOption *simpleObfsOption
|
var obfsOption *simpleObfsOption
|
||||||
var shadowTLSOpt *shadowtls.ShadowTLSOption
|
var shadowTLSOpt *shadowtls.ShadowTLSOption
|
||||||
var restlsConfig *restls.Config
|
var restlsConfig *restls.Config
|
||||||
|
var kcptunClient *kcptun.Client
|
||||||
obfsMode := ""
|
obfsMode := ""
|
||||||
|
|
||||||
decoder := structure.NewDecoder(structure.Option{TagName: "obfs", WeaklyTypedInput: true})
|
decoder := structure.NewDecoder(structure.Option{TagName: "obfs", WeaklyTypedInput: true})
|
||||||
@@ -302,6 +337,14 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
|||||||
v2rayOption.TLS = true
|
v2rayOption.TLS = true
|
||||||
v2rayOption.SkipCertVerify = opts.SkipCertVerify
|
v2rayOption.SkipCertVerify = opts.SkipCertVerify
|
||||||
v2rayOption.Fingerprint = opts.Fingerprint
|
v2rayOption.Fingerprint = opts.Fingerprint
|
||||||
|
v2rayOption.Certificate = opts.Certificate
|
||||||
|
v2rayOption.PrivateKey = opts.PrivateKey
|
||||||
|
|
||||||
|
echConfig, err := opts.ECHOpts.Parse()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("ss %s initialize v2ray-plugin error: %w", addr, err)
|
||||||
|
}
|
||||||
|
v2rayOption.ECHConfig = echConfig
|
||||||
}
|
}
|
||||||
} else if option.Plugin == "gost-plugin" {
|
} else if option.Plugin == "gost-plugin" {
|
||||||
opts := gostObfsOption{Host: "bing.com", Mux: true}
|
opts := gostObfsOption{Host: "bing.com", Mux: true}
|
||||||
@@ -324,6 +367,14 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
|||||||
gostOption.TLS = true
|
gostOption.TLS = true
|
||||||
gostOption.SkipCertVerify = opts.SkipCertVerify
|
gostOption.SkipCertVerify = opts.SkipCertVerify
|
||||||
gostOption.Fingerprint = opts.Fingerprint
|
gostOption.Fingerprint = opts.Fingerprint
|
||||||
|
gostOption.Certificate = opts.Certificate
|
||||||
|
gostOption.PrivateKey = opts.PrivateKey
|
||||||
|
|
||||||
|
echConfig, err := opts.ECHOpts.Parse()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("ss %s initialize gost-plugin error: %w", addr, err)
|
||||||
|
}
|
||||||
|
gostOption.ECHConfig = echConfig
|
||||||
}
|
}
|
||||||
} else if option.Plugin == shadowtls.Mode {
|
} else if option.Plugin == shadowtls.Mode {
|
||||||
obfsMode = shadowtls.Mode
|
obfsMode = shadowtls.Mode
|
||||||
@@ -338,10 +389,18 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
|||||||
Password: opt.Password,
|
Password: opt.Password,
|
||||||
Host: opt.Host,
|
Host: opt.Host,
|
||||||
Fingerprint: opt.Fingerprint,
|
Fingerprint: opt.Fingerprint,
|
||||||
|
Certificate: opt.Certificate,
|
||||||
|
PrivateKey: opt.PrivateKey,
|
||||||
ClientFingerprint: option.ClientFingerprint,
|
ClientFingerprint: option.ClientFingerprint,
|
||||||
SkipCertVerify: opt.SkipCertVerify,
|
SkipCertVerify: opt.SkipCertVerify,
|
||||||
Version: opt.Version,
|
Version: opt.Version,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if opt.ALPN != nil { // structure's Decode will ensure value not nil when input has value even it was set an empty array
|
||||||
|
shadowTLSOpt.ALPN = opt.ALPN
|
||||||
|
} else {
|
||||||
|
shadowTLSOpt.ALPN = shadowtls.DefaultALPN
|
||||||
|
}
|
||||||
} else if option.Plugin == restls.Mode {
|
} else if option.Plugin == restls.Mode {
|
||||||
obfsMode = restls.Mode
|
obfsMode = restls.Mode
|
||||||
restlsOpt := &restlsOption{}
|
restlsOpt := &restlsOption{}
|
||||||
@@ -354,6 +413,41 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
|||||||
return nil, fmt.Errorf("ss %s initialize restls-plugin error: %w", addr, err)
|
return nil, fmt.Errorf("ss %s initialize restls-plugin error: %w", addr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else if option.Plugin == kcptun.Mode {
|
||||||
|
obfsMode = kcptun.Mode
|
||||||
|
kcptunOpt := &kcpTunOption{}
|
||||||
|
if err := decoder.Decode(option.PluginOpts, kcptunOpt); err != nil {
|
||||||
|
return nil, fmt.Errorf("ss %s initialize kcptun-plugin error: %w", addr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
kcptunClient = kcptun.NewClient(kcptun.Config{
|
||||||
|
Key: kcptunOpt.Key,
|
||||||
|
Crypt: kcptunOpt.Crypt,
|
||||||
|
Mode: kcptunOpt.Mode,
|
||||||
|
Conn: kcptunOpt.Conn,
|
||||||
|
AutoExpire: kcptunOpt.AutoExpire,
|
||||||
|
ScavengeTTL: kcptunOpt.ScavengeTTL,
|
||||||
|
MTU: kcptunOpt.MTU,
|
||||||
|
RateLimit: kcptunOpt.RateLimit,
|
||||||
|
SndWnd: kcptunOpt.SndWnd,
|
||||||
|
RcvWnd: kcptunOpt.RcvWnd,
|
||||||
|
DataShard: kcptunOpt.DataShard,
|
||||||
|
ParityShard: kcptunOpt.ParityShard,
|
||||||
|
DSCP: kcptunOpt.DSCP,
|
||||||
|
NoComp: kcptunOpt.NoComp,
|
||||||
|
AckNodelay: kcptunOpt.AckNodelay,
|
||||||
|
NoDelay: kcptunOpt.NoDelay,
|
||||||
|
Interval: kcptunOpt.Interval,
|
||||||
|
Resend: kcptunOpt.Resend,
|
||||||
|
NoCongestion: kcptunOpt.NoCongestion,
|
||||||
|
SockBuf: kcptunOpt.SockBuf,
|
||||||
|
SmuxVer: kcptunOpt.SmuxVer,
|
||||||
|
SmuxBuf: kcptunOpt.SmuxBuf,
|
||||||
|
FrameSize: kcptunOpt.FrameSize,
|
||||||
|
StreamBuf: kcptunOpt.StreamBuf,
|
||||||
|
KeepAlive: kcptunOpt.KeepAlive,
|
||||||
|
})
|
||||||
|
option.UDPOverTCP = true // must open uot
|
||||||
}
|
}
|
||||||
switch option.UDPOverTCPVersion {
|
switch option.UDPOverTCPVersion {
|
||||||
case uot.Version, uot.LegacyVersion:
|
case uot.Version, uot.LegacyVersion:
|
||||||
@@ -363,17 +457,18 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
|||||||
return nil, fmt.Errorf("ss %s unknown udp over tcp protocol version: %d", addr, option.UDPOverTCPVersion)
|
return nil, fmt.Errorf("ss %s unknown udp over tcp protocol version: %d", addr, option.UDPOverTCPVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ShadowSocks{
|
outbound := &ShadowSocks{
|
||||||
Base: &Base{
|
Base: &Base{
|
||||||
name: option.Name,
|
name: option.Name,
|
||||||
addr: addr,
|
addr: addr,
|
||||||
tp: C.Shadowsocks,
|
tp: C.Shadowsocks,
|
||||||
|
pdName: option.ProviderName,
|
||||||
udp: option.UDP,
|
udp: option.UDP,
|
||||||
tfo: option.TFO,
|
tfo: option.TFO,
|
||||||
mpTcp: option.MPTCP,
|
mpTcp: option.MPTCP,
|
||||||
iface: option.Interface,
|
iface: option.Interface,
|
||||||
rmark: option.RoutingMark,
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: option.IPVersion,
|
||||||
},
|
},
|
||||||
method: method,
|
method: method,
|
||||||
|
|
||||||
@@ -384,5 +479,8 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
|||||||
obfsOption: obfsOption,
|
obfsOption: obfsOption,
|
||||||
shadowTLSOption: shadowTLSOpt,
|
shadowTLSOption: shadowTLSOpt,
|
||||||
restlsConfig: restlsConfig,
|
restlsConfig: restlsConfig,
|
||||||
}, nil
|
kcptunClient: kcptunClient,
|
||||||
|
}
|
||||||
|
outbound.dialer = option.NewDialer(outbound.DialOptions())
|
||||||
|
return outbound, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/transport/shadowsocks/core"
|
"github.com/metacubex/mihomo/transport/shadowsocks/core"
|
||||||
"github.com/metacubex/mihomo/transport/shadowsocks/shadowaead"
|
"github.com/metacubex/mihomo/transport/shadowsocks/shadowaead"
|
||||||
@@ -67,19 +65,8 @@ func (ssr *ShadowSocksR) StreamConnContext(ctx context.Context, c net.Conn, meta
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (ssr *ShadowSocksR) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
func (ssr *ShadowSocksR) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
return ssr.DialContextWithDialer(ctx, dialer.NewDialer(ssr.Base.DialOptions(opts...)...), metadata)
|
c, err := ssr.dialer.DialContext(ctx, "tcp", ssr.addr)
|
||||||
}
|
|
||||||
|
|
||||||
// DialContextWithDialer implements C.ProxyAdapter
|
|
||||||
func (ssr *ShadowSocksR) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
|
||||||
if len(ssr.option.DialerProxy) > 0 {
|
|
||||||
dialer, err = proxydialer.NewByName(ssr.option.DialerProxy, dialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
c, err := dialer.DialContext(ctx, "tcp", ssr.addr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error: %w", ssr.addr, err)
|
return nil, fmt.Errorf("%s connect error: %w", ssr.addr, err)
|
||||||
}
|
}
|
||||||
@@ -93,24 +80,16 @@ func (ssr *ShadowSocksR) DialContextWithDialer(ctx context.Context, dialer C.Dia
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (ssr *ShadowSocksR) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
func (ssr *ShadowSocksR) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
||||||
return ssr.ListenPacketWithDialer(ctx, dialer.NewDialer(ssr.Base.DialOptions(opts...)...), metadata)
|
if err := ssr.ResolveUDP(ctx, metadata); err != nil {
|
||||||
}
|
return nil, err
|
||||||
|
|
||||||
// ListenPacketWithDialer implements C.ProxyAdapter
|
|
||||||
func (ssr *ShadowSocksR) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
|
||||||
if len(ssr.option.DialerProxy) > 0 {
|
|
||||||
dialer, err = proxydialer.NewByName(ssr.option.DialerProxy, dialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
addr, err := resolveUDPAddr(ctx, "udp", ssr.addr, ssr.prefer)
|
addr, err := resolveUDPAddr(ctx, "udp", ssr.addr, ssr.prefer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
pc, err := dialer.ListenPacket(ctx, "udp", "", addr.AddrPort())
|
pc, err := ssr.dialer.ListenPacket(ctx, "udp", "", addr.AddrPort())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -120,11 +99,6 @@ func (ssr *ShadowSocksR) ListenPacketWithDialer(ctx context.Context, dialer C.Di
|
|||||||
return newPacketConn(&ssrPacketConn{EnhancePacketConn: epc, rAddr: addr}, ssr), nil
|
return newPacketConn(&ssrPacketConn{EnhancePacketConn: epc, rAddr: addr}, ssr), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SupportWithDialer implements C.ProxyAdapter
|
|
||||||
func (ssr *ShadowSocksR) SupportWithDialer() C.NetWork {
|
|
||||||
return C.ALLNet
|
|
||||||
}
|
|
||||||
|
|
||||||
// ProxyInfo implements C.ProxyAdapter
|
// ProxyInfo implements C.ProxyAdapter
|
||||||
func (ssr *ShadowSocksR) ProxyInfo() C.ProxyInfo {
|
func (ssr *ShadowSocksR) ProxyInfo() C.ProxyInfo {
|
||||||
info := ssr.Base.ProxyInfo()
|
info := ssr.Base.ProxyInfo()
|
||||||
@@ -183,23 +157,26 @@ func NewShadowSocksR(option ShadowSocksROption) (*ShadowSocksR, error) {
|
|||||||
return nil, fmt.Errorf("ssr %s initialize protocol error: %w", addr, err)
|
return nil, fmt.Errorf("ssr %s initialize protocol error: %w", addr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ShadowSocksR{
|
outbound := &ShadowSocksR{
|
||||||
Base: &Base{
|
Base: &Base{
|
||||||
name: option.Name,
|
name: option.Name,
|
||||||
addr: addr,
|
addr: addr,
|
||||||
tp: C.ShadowsocksR,
|
tp: C.ShadowsocksR,
|
||||||
|
pdName: option.ProviderName,
|
||||||
udp: option.UDP,
|
udp: option.UDP,
|
||||||
tfo: option.TFO,
|
tfo: option.TFO,
|
||||||
mpTcp: option.MPTCP,
|
mpTcp: option.MPTCP,
|
||||||
iface: option.Interface,
|
iface: option.Interface,
|
||||||
rmark: option.RoutingMark,
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: option.IPVersion,
|
||||||
},
|
},
|
||||||
option: &option,
|
option: &option,
|
||||||
cipher: coreCiph,
|
cipher: coreCiph,
|
||||||
obfs: obfs,
|
obfs: obfs,
|
||||||
protocol: protocol,
|
protocol: protocol,
|
||||||
}, nil
|
}
|
||||||
|
outbound.dialer = option.NewDialer(outbound.DialOptions())
|
||||||
|
return outbound, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type ssrPacketConn struct {
|
type ssrPacketConn struct {
|
||||||
@@ -249,13 +226,14 @@ func (spc *ssrPacketConn) WaitReadFrom() (data []byte, put func(), addr net.Addr
|
|||||||
return nil, nil, nil, errors.New("parse addr error")
|
return nil, nil, nil, errors.New("parse addr error")
|
||||||
}
|
}
|
||||||
|
|
||||||
addr = _addr.UDPAddr()
|
udpAddr := _addr.UDPAddr()
|
||||||
if addr == nil {
|
if udpAddr == nil {
|
||||||
if put != nil {
|
if put != nil {
|
||||||
put()
|
put()
|
||||||
}
|
}
|
||||||
return nil, nil, nil, errors.New("parse addr error")
|
return nil, nil, nil, errors.New("parse addr error")
|
||||||
}
|
}
|
||||||
|
addr = udpAddr
|
||||||
|
|
||||||
data = data[len(_addr):]
|
data = data[len(_addr):]
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -2,24 +2,20 @@ package outbound
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
|
|
||||||
CN "github.com/metacubex/mihomo/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
"github.com/metacubex/mihomo/component/proxydialer"
|
||||||
"github.com/metacubex/mihomo/component/resolver"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/log"
|
"github.com/metacubex/mihomo/log"
|
||||||
|
|
||||||
mux "github.com/sagernet/sing-mux"
|
mux "github.com/metacubex/sing-mux"
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
E "github.com/metacubex/sing/common/exceptions"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/metacubex/sing/common/metadata"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SingMux struct {
|
type SingMux struct {
|
||||||
ProxyAdapter
|
ProxyAdapter
|
||||||
client *mux.Client
|
client *mux.Client
|
||||||
dialer proxydialer.SingDialer
|
|
||||||
onlyTcp bool
|
onlyTcp bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,9 +37,7 @@ type BrutalOption struct {
|
|||||||
Down string `proxy:"down,omitempty"`
|
Down string `proxy:"down,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SingMux) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
func (s *SingMux) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
options := s.ProxyAdapter.DialOptions(opts...)
|
|
||||||
s.dialer.SetDialer(dialer.NewDialer(options...))
|
|
||||||
c, err := s.client.DialContext(ctx, "tcp", M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort))
|
c, err := s.client.DialContext(ctx, "tcp", M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -51,22 +45,13 @@ func (s *SingMux) DialContext(ctx context.Context, metadata *C.Metadata, opts ..
|
|||||||
return NewConn(c, s), err
|
return NewConn(c, s), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SingMux) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
func (s *SingMux) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
if s.onlyTcp {
|
if s.onlyTcp {
|
||||||
return s.ProxyAdapter.ListenPacketContext(ctx, metadata, opts...)
|
return s.ProxyAdapter.ListenPacketContext(ctx, metadata)
|
||||||
}
|
}
|
||||||
options := s.ProxyAdapter.DialOptions(opts...)
|
if err = s.ProxyAdapter.ResolveUDP(ctx, metadata); err != nil {
|
||||||
s.dialer.SetDialer(dialer.NewDialer(options...))
|
return nil, err
|
||||||
|
|
||||||
// sing-mux use stream-oriented udp with a special address, so we need a net.UDPAddr
|
|
||||||
if !metadata.Resolved() {
|
|
||||||
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("can't resolve ip")
|
|
||||||
}
|
|
||||||
metadata.DstIP = ip
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pc, err := s.client.ListenPacket(ctx, M.SocksaddrFromNet(metadata.UDPAddr()))
|
pc, err := s.client.ListenPacket(ctx, M.SocksaddrFromNet(metadata.UDPAddr()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -74,7 +59,7 @@ func (s *SingMux) ListenPacketContext(ctx context.Context, metadata *C.Metadata,
|
|||||||
if pc == nil {
|
if pc == nil {
|
||||||
return nil, E.New("packetConn is nil")
|
return nil, E.New("packetConn is nil")
|
||||||
}
|
}
|
||||||
return newPacketConn(CN.NewThreadSafePacketConn(pc), s), nil
|
return newPacketConn(N.NewThreadSafePacketConn(pc), s), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SingMux) SupportUDP() bool {
|
func (s *SingMux) SupportUDP() bool {
|
||||||
@@ -109,7 +94,7 @@ func NewSingMux(option SingMuxOption, proxy ProxyAdapter) (ProxyAdapter, error)
|
|||||||
// TODO
|
// TODO
|
||||||
// "TCP Brutal is only supported on Linux-based systems"
|
// "TCP Brutal is only supported on Linux-based systems"
|
||||||
|
|
||||||
singDialer := proxydialer.NewSingDialer(proxy, dialer.NewDialer(), option.Statistic)
|
singDialer := proxydialer.NewSingDialer(proxydialer.New(proxy, option.Statistic))
|
||||||
client, err := mux.NewClient(mux.Options{
|
client, err := mux.NewClient(mux.Options{
|
||||||
Dialer: singDialer,
|
Dialer: singDialer,
|
||||||
Logger: log.SingLogger,
|
Logger: log.SingLogger,
|
||||||
@@ -130,7 +115,6 @@ func NewSingMux(option SingMuxOption, proxy ProxyAdapter) (ProxyAdapter, error)
|
|||||||
outbound := &SingMux{
|
outbound := &SingMux{
|
||||||
ProxyAdapter: proxy,
|
ProxyAdapter: proxy,
|
||||||
client: client,
|
client: client,
|
||||||
dialer: singDialer,
|
|
||||||
onlyTcp: option.OnlyTcp,
|
onlyTcp: option.OnlyTcp,
|
||||||
}
|
}
|
||||||
return outbound, nil
|
return outbound, nil
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ import (
|
|||||||
|
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/metacubex/mihomo/common/structure"
|
"github.com/metacubex/mihomo/common/structure"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
obfs "github.com/metacubex/mihomo/transport/simple-obfs"
|
obfs "github.com/metacubex/mihomo/transport/simple-obfs"
|
||||||
"github.com/metacubex/mihomo/transport/snell"
|
"github.com/metacubex/mihomo/transport/snell"
|
||||||
@@ -75,8 +73,8 @@ func (s *Snell) writeHeaderContext(ctx context.Context, c net.Conn, metadata *C.
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (s *Snell) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
func (s *Snell) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
if s.version == snell.Version2 && dialer.IsZeroOptions(opts) {
|
if s.version == snell.Version2 {
|
||||||
c, err := s.pool.Get()
|
c, err := s.pool.Get()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -89,18 +87,7 @@ func (s *Snell) DialContext(ctx context.Context, metadata *C.Metadata, opts ...d
|
|||||||
return NewConn(c, s), err
|
return NewConn(c, s), err
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.DialContextWithDialer(ctx, dialer.NewDialer(s.Base.DialOptions(opts...)...), metadata)
|
c, err := s.dialer.DialContext(ctx, "tcp", s.addr)
|
||||||
}
|
|
||||||
|
|
||||||
// DialContextWithDialer implements C.ProxyAdapter
|
|
||||||
func (s *Snell) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
|
||||||
if len(s.option.DialerProxy) > 0 {
|
|
||||||
dialer, err = proxydialer.NewByName(s.option.DialerProxy, dialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
c, err := dialer.DialContext(ctx, "tcp", s.addr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error: %w", s.addr, err)
|
return nil, fmt.Errorf("%s connect error: %w", s.addr, err)
|
||||||
}
|
}
|
||||||
@@ -114,20 +101,12 @@ func (s *Snell) DialContextWithDialer(ctx context.Context, dialer C.Dialer, meta
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (s *Snell) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
func (s *Snell) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
||||||
return s.ListenPacketWithDialer(ctx, dialer.NewDialer(s.Base.DialOptions(opts...)...), metadata)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListenPacketWithDialer implements C.ProxyAdapter
|
|
||||||
func (s *Snell) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (C.PacketConn, error) {
|
|
||||||
var err error
|
var err error
|
||||||
if len(s.option.DialerProxy) > 0 {
|
if err = s.ResolveUDP(ctx, metadata); err != nil {
|
||||||
dialer, err = proxydialer.NewByName(s.option.DialerProxy, dialer)
|
return nil, err
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
c, err := dialer.DialContext(ctx, "tcp", s.addr)
|
c, err := s.dialer.DialContext(ctx, "tcp", s.addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -138,11 +117,6 @@ func (s *Snell) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, met
|
|||||||
return newPacketConn(pc, s), nil
|
return newPacketConn(pc, s), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SupportWithDialer implements C.ProxyAdapter
|
|
||||||
func (s *Snell) SupportWithDialer() C.NetWork {
|
|
||||||
return C.ALLNet
|
|
||||||
}
|
|
||||||
|
|
||||||
// SupportUOT implements C.ProxyAdapter
|
// SupportUOT implements C.ProxyAdapter
|
||||||
func (s *Snell) SupportUOT() bool {
|
func (s *Snell) SupportUOT() bool {
|
||||||
return true
|
return true
|
||||||
@@ -191,30 +165,24 @@ func NewSnell(option SnellOption) (*Snell, error) {
|
|||||||
name: option.Name,
|
name: option.Name,
|
||||||
addr: addr,
|
addr: addr,
|
||||||
tp: C.Snell,
|
tp: C.Snell,
|
||||||
|
pdName: option.ProviderName,
|
||||||
udp: option.UDP,
|
udp: option.UDP,
|
||||||
tfo: option.TFO,
|
tfo: option.TFO,
|
||||||
mpTcp: option.MPTCP,
|
mpTcp: option.MPTCP,
|
||||||
iface: option.Interface,
|
iface: option.Interface,
|
||||||
rmark: option.RoutingMark,
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: option.IPVersion,
|
||||||
},
|
},
|
||||||
option: &option,
|
option: &option,
|
||||||
psk: psk,
|
psk: psk,
|
||||||
obfsOption: obfsOption,
|
obfsOption: obfsOption,
|
||||||
version: option.Version,
|
version: option.Version,
|
||||||
}
|
}
|
||||||
|
s.dialer = option.NewDialer(s.DialOptions())
|
||||||
|
|
||||||
if option.Version == snell.Version2 {
|
if option.Version == snell.Version2 {
|
||||||
s.pool = snell.NewPool(func(ctx context.Context) (*snell.Snell, error) {
|
s.pool = snell.NewPool(func(ctx context.Context) (*snell.Snell, error) {
|
||||||
var err error
|
c, err := s.dialer.DialContext(ctx, "tcp", addr)
|
||||||
var cDialer C.Dialer = dialer.NewDialer(s.Base.DialOptions()...)
|
|
||||||
if len(s.option.DialerProxy) > 0 {
|
|
||||||
cDialer, err = proxydialer.NewByName(s.option.DialerProxy, cDialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
c, err := cDialer.DialContext(ctx, "tcp", addr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package outbound
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -12,10 +11,10 @@ import (
|
|||||||
|
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/metacubex/mihomo/component/ca"
|
"github.com/metacubex/mihomo/component/ca"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/transport/socks5"
|
"github.com/metacubex/mihomo/transport/socks5"
|
||||||
|
|
||||||
|
"github.com/metacubex/tls"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Socks5 struct {
|
type Socks5 struct {
|
||||||
@@ -39,6 +38,8 @@ type Socks5Option struct {
|
|||||||
UDP bool `proxy:"udp,omitempty"`
|
UDP bool `proxy:"udp,omitempty"`
|
||||||
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
||||||
Fingerprint string `proxy:"fingerprint,omitempty"`
|
Fingerprint string `proxy:"fingerprint,omitempty"`
|
||||||
|
Certificate string `proxy:"certificate,omitempty"`
|
||||||
|
PrivateKey string `proxy:"private-key,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// StreamConnContext implements C.ProxyAdapter
|
// StreamConnContext implements C.ProxyAdapter
|
||||||
@@ -66,19 +67,8 @@ func (ss *Socks5) StreamConnContext(ctx context.Context, c net.Conn, metadata *C
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (ss *Socks5) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
func (ss *Socks5) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
return ss.DialContextWithDialer(ctx, dialer.NewDialer(ss.Base.DialOptions(opts...)...), metadata)
|
c, err := ss.dialer.DialContext(ctx, "tcp", ss.addr)
|
||||||
}
|
|
||||||
|
|
||||||
// DialContextWithDialer implements C.ProxyAdapter
|
|
||||||
func (ss *Socks5) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
|
||||||
if len(ss.option.DialerProxy) > 0 {
|
|
||||||
dialer, err = proxydialer.NewByName(ss.option.DialerProxy, dialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
c, err := dialer.DialContext(ctx, "tcp", ss.addr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error: %w", ss.addr, err)
|
return nil, fmt.Errorf("%s connect error: %w", ss.addr, err)
|
||||||
}
|
}
|
||||||
@@ -95,21 +85,12 @@ func (ss *Socks5) DialContextWithDialer(ctx context.Context, dialer C.Dialer, me
|
|||||||
return NewConn(c, ss), nil
|
return NewConn(c, ss), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SupportWithDialer implements C.ProxyAdapter
|
|
||||||
func (ss *Socks5) SupportWithDialer() C.NetWork {
|
|
||||||
return C.TCP
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (ss *Socks5) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
func (ss *Socks5) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
var cDialer C.Dialer = dialer.NewDialer(ss.Base.DialOptions(opts...)...)
|
if err = ss.ResolveUDP(ctx, metadata); err != nil {
|
||||||
if len(ss.option.DialerProxy) > 0 {
|
return nil, err
|
||||||
cDialer, err = proxydialer.NewByName(ss.option.DialerProxy, cDialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
c, err := cDialer.DialContext(ctx, "tcp", ss.addr)
|
c, err := ss.dialer.DialContext(ctx, "tcp", ss.addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("%s connect error: %w", ss.addr, err)
|
err = fmt.Errorf("%s connect error: %w", ss.addr, err)
|
||||||
return
|
return
|
||||||
@@ -156,7 +137,7 @@ func (ss *Socks5) ListenPacketContext(ctx context.Context, metadata *C.Metadata,
|
|||||||
bindUDPAddr.IP = serverAddr.IP
|
bindUDPAddr.IP = serverAddr.IP
|
||||||
}
|
}
|
||||||
|
|
||||||
pc, err := cDialer.ListenPacket(ctx, "udp", "", bindUDPAddr.AddrPort())
|
pc, err := ss.dialer.ListenPacket(ctx, "udp", "", bindUDPAddr.AddrPort())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -190,29 +171,33 @@ func (ss *Socks5) clientHandshakeContext(ctx context.Context, c net.Conn, addr s
|
|||||||
func NewSocks5(option Socks5Option) (*Socks5, error) {
|
func NewSocks5(option Socks5Option) (*Socks5, error) {
|
||||||
var tlsConfig *tls.Config
|
var tlsConfig *tls.Config
|
||||||
if option.TLS {
|
if option.TLS {
|
||||||
tlsConfig = &tls.Config{
|
|
||||||
InsecureSkipVerify: option.SkipCertVerify,
|
|
||||||
ServerName: option.Server,
|
|
||||||
}
|
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
tlsConfig, err = ca.GetSpecifiedFingerprintTLSConfig(tlsConfig, option.Fingerprint)
|
tlsConfig, err = ca.GetTLSConfig(ca.Option{
|
||||||
|
TLSConfig: &tls.Config{
|
||||||
|
InsecureSkipVerify: option.SkipCertVerify,
|
||||||
|
ServerName: option.Server,
|
||||||
|
},
|
||||||
|
Fingerprint: option.Fingerprint,
|
||||||
|
Certificate: option.Certificate,
|
||||||
|
PrivateKey: option.PrivateKey,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Socks5{
|
outbound := &Socks5{
|
||||||
Base: &Base{
|
Base: &Base{
|
||||||
name: option.Name,
|
name: option.Name,
|
||||||
addr: net.JoinHostPort(option.Server, strconv.Itoa(option.Port)),
|
addr: net.JoinHostPort(option.Server, strconv.Itoa(option.Port)),
|
||||||
tp: C.Socks5,
|
tp: C.Socks5,
|
||||||
|
pdName: option.ProviderName,
|
||||||
udp: option.UDP,
|
udp: option.UDP,
|
||||||
tfo: option.TFO,
|
tfo: option.TFO,
|
||||||
mpTcp: option.MPTCP,
|
mpTcp: option.MPTCP,
|
||||||
iface: option.Interface,
|
iface: option.Interface,
|
||||||
rmark: option.RoutingMark,
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: option.IPVersion,
|
||||||
},
|
},
|
||||||
option: &option,
|
option: &option,
|
||||||
user: option.UserName,
|
user: option.UserName,
|
||||||
@@ -220,7 +205,9 @@ func NewSocks5(option Socks5Option) (*Socks5, error) {
|
|||||||
tls: option.TLS,
|
tls: option.TLS,
|
||||||
skipCertVerify: option.SkipCertVerify,
|
skipCertVerify: option.SkipCertVerify,
|
||||||
tlsConfig: tlsConfig,
|
tlsConfig: tlsConfig,
|
||||||
}, nil
|
}
|
||||||
|
outbound.dialer = option.NewDialer(outbound.DialOptions())
|
||||||
|
return outbound, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type socksPacketConn struct {
|
type socksPacketConn struct {
|
||||||
|
|||||||
@@ -12,8 +12,6 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
|
|
||||||
"github.com/metacubex/randv2"
|
"github.com/metacubex/randv2"
|
||||||
@@ -43,15 +41,8 @@ type SshOption struct {
|
|||||||
HostKeyAlgorithms []string `proxy:"host-key-algorithms,omitempty"`
|
HostKeyAlgorithms []string `proxy:"host-key-algorithms,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Ssh) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
func (s *Ssh) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
var cDialer C.Dialer = dialer.NewDialer(s.Base.DialOptions(opts...)...)
|
client, err := s.connect(ctx, s.addr)
|
||||||
if len(s.option.DialerProxy) > 0 {
|
|
||||||
cDialer, err = proxydialer.NewByName(s.option.DialerProxy, cDialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
client, err := s.connect(ctx, cDialer, s.addr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -63,13 +54,13 @@ func (s *Ssh) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dia
|
|||||||
return NewConn(c, s), nil
|
return NewConn(c, s), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Ssh) connect(ctx context.Context, cDialer C.Dialer, addr string) (client *ssh.Client, err error) {
|
func (s *Ssh) connect(ctx context.Context, addr string) (client *ssh.Client, err error) {
|
||||||
s.cMutex.Lock()
|
s.cMutex.Lock()
|
||||||
defer s.cMutex.Unlock()
|
defer s.cMutex.Unlock()
|
||||||
if s.client != nil {
|
if s.client != nil {
|
||||||
return s.client, nil
|
return s.client, nil
|
||||||
}
|
}
|
||||||
c, err := cDialer.DialContext(ctx, "tcp", addr)
|
c, err := s.dialer.DialContext(ctx, "tcp", addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -136,7 +127,11 @@ func NewSsh(option SshOption) (*Ssh, error) {
|
|||||||
if strings.Contains(option.PrivateKey, "PRIVATE KEY") {
|
if strings.Contains(option.PrivateKey, "PRIVATE KEY") {
|
||||||
b = []byte(option.PrivateKey)
|
b = []byte(option.PrivateKey)
|
||||||
} else {
|
} else {
|
||||||
b, err = os.ReadFile(C.Path.Resolve(option.PrivateKey))
|
path := C.Path.Resolve(option.PrivateKey)
|
||||||
|
if !C.Path.IsSafePath(path) {
|
||||||
|
return nil, C.Path.ErrNotSafePath(path)
|
||||||
|
}
|
||||||
|
b, err = os.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -191,14 +186,15 @@ func NewSsh(option SshOption) (*Ssh, error) {
|
|||||||
name: option.Name,
|
name: option.Name,
|
||||||
addr: addr,
|
addr: addr,
|
||||||
tp: C.Ssh,
|
tp: C.Ssh,
|
||||||
|
pdName: option.ProviderName,
|
||||||
udp: false,
|
udp: false,
|
||||||
iface: option.Interface,
|
iface: option.Interface,
|
||||||
rmark: option.RoutingMark,
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: option.IPVersion,
|
||||||
},
|
},
|
||||||
option: &option,
|
option: &option,
|
||||||
config: &config,
|
config: &config,
|
||||||
}
|
}
|
||||||
|
outbound.dialer = option.NewDialer(outbound.DialOptions())
|
||||||
return outbound, nil
|
return outbound, nil
|
||||||
}
|
}
|
||||||
|
|||||||
405
adapter/outbound/sudoku.go
Normal file
405
adapter/outbound/sudoku.go
Normal file
@@ -0,0 +1,405 @@
|
|||||||
|
package outbound
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
|
C "github.com/metacubex/mihomo/constant"
|
||||||
|
"github.com/metacubex/mihomo/transport/sudoku"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Sudoku struct {
|
||||||
|
*Base
|
||||||
|
option *SudokuOption
|
||||||
|
baseConf sudoku.ProtocolConfig
|
||||||
|
|
||||||
|
httpMaskMu sync.Mutex
|
||||||
|
httpMaskClient *sudoku.HTTPMaskTunnelClient
|
||||||
|
|
||||||
|
muxMu sync.Mutex
|
||||||
|
muxClient *sudoku.MultiplexClient
|
||||||
|
}
|
||||||
|
|
||||||
|
type SudokuOption struct {
|
||||||
|
BasicOption
|
||||||
|
Name string `proxy:"name"`
|
||||||
|
Server string `proxy:"server"`
|
||||||
|
Port int `proxy:"port"`
|
||||||
|
Key string `proxy:"key"`
|
||||||
|
AEADMethod string `proxy:"aead-method,omitempty"`
|
||||||
|
PaddingMin *int `proxy:"padding-min,omitempty"`
|
||||||
|
PaddingMax *int `proxy:"padding-max,omitempty"`
|
||||||
|
TableType string `proxy:"table-type,omitempty"` // "prefer_ascii" or "prefer_entropy"
|
||||||
|
EnablePureDownlink *bool `proxy:"enable-pure-downlink,omitempty"`
|
||||||
|
HTTPMask bool `proxy:"http-mask,omitempty"`
|
||||||
|
HTTPMaskMode string `proxy:"http-mask-mode,omitempty"` // "legacy" (default), "stream", "poll", "auto"
|
||||||
|
HTTPMaskTLS bool `proxy:"http-mask-tls,omitempty"` // only for http-mask-mode stream/poll/auto
|
||||||
|
HTTPMaskHost string `proxy:"http-mask-host,omitempty"` // optional Host/SNI override (domain or domain:port)
|
||||||
|
PathRoot string `proxy:"path-root,omitempty"` // optional first-level path prefix for HTTP tunnel endpoints
|
||||||
|
HTTPMaskMultiplex string `proxy:"http-mask-multiplex,omitempty"` // "off" (default), "auto" (reuse h1/h2), "on" (single tunnel, multi-target)
|
||||||
|
CustomTable string `proxy:"custom-table,omitempty"` // optional custom byte layout, e.g. xpxvvpvv
|
||||||
|
CustomTables []string `proxy:"custom-tables,omitempty"` // optional table rotation patterns, overrides custom-table when non-empty
|
||||||
|
}
|
||||||
|
|
||||||
|
// DialContext implements C.ProxyAdapter
|
||||||
|
func (s *Sudoku) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
|
cfg, err := s.buildConfig(metadata)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
muxMode := normalizeHTTPMaskMultiplex(cfg.HTTPMaskMultiplex)
|
||||||
|
if muxMode == "on" && !cfg.DisableHTTPMask && httpTunnelModeEnabled(cfg.HTTPMaskMode) {
|
||||||
|
stream, muxErr := s.dialMultiplex(ctx, cfg.TargetAddress)
|
||||||
|
if muxErr == nil {
|
||||||
|
return NewConn(stream, s), nil
|
||||||
|
}
|
||||||
|
return nil, muxErr
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err := s.dialAndHandshake(ctx, cfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer func() { safeConnClose(c, err) }()
|
||||||
|
|
||||||
|
addrBuf, err := sudoku.EncodeAddress(cfg.TargetAddress)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("encode target address failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err = c.Write(addrBuf); err != nil {
|
||||||
|
return nil, fmt.Errorf("send target address failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return NewConn(c, s), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
|
func (s *Sudoku) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
||||||
|
if err := s.ResolveUDP(ctx, metadata); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg, err := s.buildConfig(metadata)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err := s.dialAndHandshake(ctx, cfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = sudoku.WritePreface(c); err != nil {
|
||||||
|
_ = c.Close()
|
||||||
|
return nil, fmt.Errorf("send uot preface failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return newPacketConn(N.NewThreadSafePacketConn(sudoku.NewUoTPacketConn(c)), s), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SupportUOT implements C.ProxyAdapter
|
||||||
|
func (s *Sudoku) SupportUOT() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// ProxyInfo implements C.ProxyAdapter
|
||||||
|
func (s *Sudoku) ProxyInfo() C.ProxyInfo {
|
||||||
|
info := s.Base.ProxyInfo()
|
||||||
|
info.DialerProxy = s.option.DialerProxy
|
||||||
|
return info
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Sudoku) buildConfig(metadata *C.Metadata) (*sudoku.ProtocolConfig, error) {
|
||||||
|
if metadata == nil || metadata.DstPort == 0 || !metadata.Valid() {
|
||||||
|
return nil, fmt.Errorf("invalid metadata for sudoku outbound")
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg := s.baseConf
|
||||||
|
cfg.TargetAddress = metadata.RemoteAddress()
|
||||||
|
|
||||||
|
if err := cfg.ValidateClient(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &cfg, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSudoku(option SudokuOption) (*Sudoku, error) {
|
||||||
|
if option.Server == "" {
|
||||||
|
return nil, fmt.Errorf("server is required")
|
||||||
|
}
|
||||||
|
if option.Port <= 0 || option.Port > 65535 {
|
||||||
|
return nil, fmt.Errorf("invalid port: %d", option.Port)
|
||||||
|
}
|
||||||
|
if option.Key == "" {
|
||||||
|
return nil, fmt.Errorf("key is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
tableType := strings.ToLower(option.TableType)
|
||||||
|
if tableType == "" {
|
||||||
|
tableType = "prefer_ascii"
|
||||||
|
}
|
||||||
|
if tableType != "prefer_ascii" && tableType != "prefer_entropy" {
|
||||||
|
return nil, fmt.Errorf("table-type must be prefer_ascii or prefer_entropy")
|
||||||
|
}
|
||||||
|
|
||||||
|
defaultConf := sudoku.DefaultConfig()
|
||||||
|
paddingMin := defaultConf.PaddingMin
|
||||||
|
paddingMax := defaultConf.PaddingMax
|
||||||
|
if option.PaddingMin != nil {
|
||||||
|
paddingMin = *option.PaddingMin
|
||||||
|
}
|
||||||
|
if option.PaddingMax != nil {
|
||||||
|
paddingMax = *option.PaddingMax
|
||||||
|
}
|
||||||
|
if option.PaddingMin == nil && option.PaddingMax != nil && paddingMax < paddingMin {
|
||||||
|
paddingMin = paddingMax
|
||||||
|
}
|
||||||
|
if option.PaddingMax == nil && option.PaddingMin != nil && paddingMax < paddingMin {
|
||||||
|
paddingMax = paddingMin
|
||||||
|
}
|
||||||
|
enablePureDownlink := defaultConf.EnablePureDownlink
|
||||||
|
if option.EnablePureDownlink != nil {
|
||||||
|
enablePureDownlink = *option.EnablePureDownlink
|
||||||
|
}
|
||||||
|
|
||||||
|
baseConf := sudoku.ProtocolConfig{
|
||||||
|
ServerAddress: net.JoinHostPort(option.Server, strconv.Itoa(option.Port)),
|
||||||
|
Key: option.Key,
|
||||||
|
AEADMethod: defaultConf.AEADMethod,
|
||||||
|
PaddingMin: paddingMin,
|
||||||
|
PaddingMax: paddingMax,
|
||||||
|
EnablePureDownlink: enablePureDownlink,
|
||||||
|
HandshakeTimeoutSeconds: defaultConf.HandshakeTimeoutSeconds,
|
||||||
|
DisableHTTPMask: !option.HTTPMask,
|
||||||
|
HTTPMaskMode: defaultConf.HTTPMaskMode,
|
||||||
|
HTTPMaskTLSEnabled: option.HTTPMaskTLS,
|
||||||
|
HTTPMaskHost: option.HTTPMaskHost,
|
||||||
|
HTTPMaskPathRoot: strings.TrimSpace(option.PathRoot),
|
||||||
|
HTTPMaskMultiplex: defaultConf.HTTPMaskMultiplex,
|
||||||
|
}
|
||||||
|
if option.HTTPMaskMode != "" {
|
||||||
|
baseConf.HTTPMaskMode = option.HTTPMaskMode
|
||||||
|
}
|
||||||
|
if option.HTTPMaskMultiplex != "" {
|
||||||
|
baseConf.HTTPMaskMultiplex = option.HTTPMaskMultiplex
|
||||||
|
}
|
||||||
|
tables, err := sudoku.NewTablesWithCustomPatterns(sudoku.ClientAEADSeed(option.Key), tableType, option.CustomTable, option.CustomTables)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("build table(s) failed: %w", err)
|
||||||
|
}
|
||||||
|
if len(tables) == 1 {
|
||||||
|
baseConf.Table = tables[0]
|
||||||
|
} else {
|
||||||
|
baseConf.Tables = tables
|
||||||
|
}
|
||||||
|
if option.AEADMethod != "" {
|
||||||
|
baseConf.AEADMethod = option.AEADMethod
|
||||||
|
}
|
||||||
|
|
||||||
|
outbound := &Sudoku{
|
||||||
|
Base: &Base{
|
||||||
|
name: option.Name,
|
||||||
|
addr: baseConf.ServerAddress,
|
||||||
|
tp: C.Sudoku,
|
||||||
|
pdName: option.ProviderName,
|
||||||
|
udp: true,
|
||||||
|
tfo: option.TFO,
|
||||||
|
mpTcp: option.MPTCP,
|
||||||
|
iface: option.Interface,
|
||||||
|
rmark: option.RoutingMark,
|
||||||
|
prefer: option.IPVersion,
|
||||||
|
},
|
||||||
|
option: &option,
|
||||||
|
baseConf: baseConf,
|
||||||
|
}
|
||||||
|
outbound.dialer = option.NewDialer(outbound.DialOptions())
|
||||||
|
return outbound, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Sudoku) Close() error {
|
||||||
|
s.resetMuxClient()
|
||||||
|
s.resetHTTPMaskClient()
|
||||||
|
return s.Base.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeHTTPMaskMultiplex(mode string) string {
|
||||||
|
switch strings.ToLower(strings.TrimSpace(mode)) {
|
||||||
|
case "", "off":
|
||||||
|
return "off"
|
||||||
|
case "auto":
|
||||||
|
return "auto"
|
||||||
|
case "on":
|
||||||
|
return "on"
|
||||||
|
default:
|
||||||
|
return "off"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func httpTunnelModeEnabled(mode string) bool {
|
||||||
|
switch strings.ToLower(strings.TrimSpace(mode)) {
|
||||||
|
case "stream", "poll", "auto":
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Sudoku) dialAndHandshake(ctx context.Context, cfg *sudoku.ProtocolConfig) (_ net.Conn, err error) {
|
||||||
|
if cfg == nil {
|
||||||
|
return nil, fmt.Errorf("config is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
handshakeCfg := *cfg
|
||||||
|
if !handshakeCfg.DisableHTTPMask && httpTunnelModeEnabled(handshakeCfg.HTTPMaskMode) {
|
||||||
|
handshakeCfg.DisableHTTPMask = true
|
||||||
|
}
|
||||||
|
|
||||||
|
upgrade := func(raw net.Conn) (net.Conn, error) {
|
||||||
|
return sudoku.ClientHandshake(raw, &handshakeCfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
c net.Conn
|
||||||
|
handshakeDone bool
|
||||||
|
)
|
||||||
|
if !cfg.DisableHTTPMask && httpTunnelModeEnabled(cfg.HTTPMaskMode) {
|
||||||
|
muxMode := normalizeHTTPMaskMultiplex(cfg.HTTPMaskMultiplex)
|
||||||
|
switch muxMode {
|
||||||
|
case "auto", "on":
|
||||||
|
client, errX := s.getOrCreateHTTPMaskClient(cfg)
|
||||||
|
if errX != nil {
|
||||||
|
return nil, errX
|
||||||
|
}
|
||||||
|
c, err = client.Dial(ctx, upgrade)
|
||||||
|
default:
|
||||||
|
c, err = sudoku.DialHTTPMaskTunnel(ctx, cfg.ServerAddress, cfg, s.dialer.DialContext, upgrade)
|
||||||
|
}
|
||||||
|
if err == nil && c != nil {
|
||||||
|
handshakeDone = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if c == nil && err == nil {
|
||||||
|
c, err = s.dialer.DialContext(ctx, "tcp", s.addr)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("%s connect error: %w", s.addr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() { safeConnClose(c, err) }()
|
||||||
|
|
||||||
|
if ctx.Done() != nil {
|
||||||
|
done := N.SetupContextForConn(ctx, c)
|
||||||
|
defer done(&err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !handshakeDone {
|
||||||
|
c, err = sudoku.ClientHandshake(c, &handshakeCfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Sudoku) dialMultiplex(ctx context.Context, targetAddress string) (net.Conn, error) {
|
||||||
|
for attempt := 0; attempt < 2; attempt++ {
|
||||||
|
client, err := s.getOrCreateMuxClient(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
stream, err := client.Dial(ctx, targetAddress)
|
||||||
|
if err != nil {
|
||||||
|
s.resetMuxClient()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
return stream, nil
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("multiplex open stream failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Sudoku) getOrCreateMuxClient(ctx context.Context) (*sudoku.MultiplexClient, error) {
|
||||||
|
if s == nil {
|
||||||
|
return nil, fmt.Errorf("nil adapter")
|
||||||
|
}
|
||||||
|
|
||||||
|
s.muxMu.Lock()
|
||||||
|
if s.muxClient != nil && !s.muxClient.IsClosed() {
|
||||||
|
client := s.muxClient
|
||||||
|
s.muxMu.Unlock()
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
s.muxMu.Unlock()
|
||||||
|
|
||||||
|
s.muxMu.Lock()
|
||||||
|
defer s.muxMu.Unlock()
|
||||||
|
|
||||||
|
if s.muxClient != nil && !s.muxClient.IsClosed() {
|
||||||
|
return s.muxClient, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
baseCfg := s.baseConf
|
||||||
|
baseConn, err := s.dialAndHandshake(ctx, &baseCfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := sudoku.StartMultiplexClient(baseConn)
|
||||||
|
if err != nil {
|
||||||
|
_ = baseConn.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
s.muxClient = client
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Sudoku) resetMuxClient() {
|
||||||
|
s.muxMu.Lock()
|
||||||
|
defer s.muxMu.Unlock()
|
||||||
|
if s.muxClient != nil {
|
||||||
|
_ = s.muxClient.Close()
|
||||||
|
s.muxClient = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Sudoku) getOrCreateHTTPMaskClient(cfg *sudoku.ProtocolConfig) (*sudoku.HTTPMaskTunnelClient, error) {
|
||||||
|
if s == nil {
|
||||||
|
return nil, fmt.Errorf("nil adapter")
|
||||||
|
}
|
||||||
|
if cfg == nil {
|
||||||
|
return nil, fmt.Errorf("config is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
s.httpMaskMu.Lock()
|
||||||
|
defer s.httpMaskMu.Unlock()
|
||||||
|
|
||||||
|
if s.httpMaskClient != nil {
|
||||||
|
return s.httpMaskClient, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err := sudoku.NewHTTPMaskTunnelClient(cfg.ServerAddress, cfg, s.dialer.DialContext)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
s.httpMaskClient = c
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Sudoku) resetHTTPMaskClient() {
|
||||||
|
s.httpMaskMu.Lock()
|
||||||
|
defer s.httpMaskMu.Unlock()
|
||||||
|
if s.httpMaskClient != nil {
|
||||||
|
s.httpMaskClient.CloseIdleConnections()
|
||||||
|
s.httpMaskClient = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,23 +2,23 @@ package outbound
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/metacubex/mihomo/component/ca"
|
"github.com/metacubex/mihomo/component/ca"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"github.com/metacubex/mihomo/component/ech"
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
|
||||||
tlsC "github.com/metacubex/mihomo/component/tls"
|
tlsC "github.com/metacubex/mihomo/component/tls"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/transport/gun"
|
"github.com/metacubex/mihomo/transport/gun"
|
||||||
"github.com/metacubex/mihomo/transport/shadowsocks/core"
|
"github.com/metacubex/mihomo/transport/shadowsocks/core"
|
||||||
"github.com/metacubex/mihomo/transport/trojan"
|
"github.com/metacubex/mihomo/transport/trojan"
|
||||||
"github.com/metacubex/mihomo/transport/vmess"
|
"github.com/metacubex/mihomo/transport/vmess"
|
||||||
|
|
||||||
|
"github.com/metacubex/http"
|
||||||
|
"github.com/metacubex/tls"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Trojan struct {
|
type Trojan struct {
|
||||||
@@ -27,11 +27,11 @@ type Trojan struct {
|
|||||||
hexPassword [trojan.KeyLength]byte
|
hexPassword [trojan.KeyLength]byte
|
||||||
|
|
||||||
// for gun mux
|
// for gun mux
|
||||||
gunTLSConfig *tls.Config
|
|
||||||
gunConfig *gun.Config
|
gunConfig *gun.Config
|
||||||
transport *gun.TransportWrap
|
gunTransport *gun.TransportWrap
|
||||||
|
|
||||||
realityConfig *tlsC.RealityConfig
|
realityConfig *tlsC.RealityConfig
|
||||||
|
echConfig *ech.Config
|
||||||
|
|
||||||
ssCipher core.Cipher
|
ssCipher core.Cipher
|
||||||
}
|
}
|
||||||
@@ -46,8 +46,11 @@ type TrojanOption struct {
|
|||||||
SNI string `proxy:"sni,omitempty"`
|
SNI string `proxy:"sni,omitempty"`
|
||||||
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
||||||
Fingerprint string `proxy:"fingerprint,omitempty"`
|
Fingerprint string `proxy:"fingerprint,omitempty"`
|
||||||
|
Certificate string `proxy:"certificate,omitempty"`
|
||||||
|
PrivateKey string `proxy:"private-key,omitempty"`
|
||||||
UDP bool `proxy:"udp,omitempty"`
|
UDP bool `proxy:"udp,omitempty"`
|
||||||
Network string `proxy:"network,omitempty"`
|
Network string `proxy:"network,omitempty"`
|
||||||
|
ECHOpts ECHOptions `proxy:"ech-opts,omitempty"`
|
||||||
RealityOpts RealityOptions `proxy:"reality-opts,omitempty"`
|
RealityOpts RealityOptions `proxy:"reality-opts,omitempty"`
|
||||||
GrpcOpts GrpcOptions `proxy:"grpc-opts,omitempty"`
|
GrpcOpts GrpcOptions `proxy:"grpc-opts,omitempty"`
|
||||||
WSOpts WSOptions `proxy:"ws-opts,omitempty"`
|
WSOpts WSOptions `proxy:"ws-opts,omitempty"`
|
||||||
@@ -62,7 +65,6 @@ type TrojanSSOption struct {
|
|||||||
Password string `proxy:"password,omitempty"`
|
Password string `proxy:"password,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// StreamConnContext implements C.ProxyAdapter
|
|
||||||
func (t *Trojan) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ net.Conn, err error) {
|
func (t *Trojan) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ net.Conn, err error) {
|
||||||
switch t.option.Network {
|
switch t.option.Network {
|
||||||
case "ws":
|
case "ws":
|
||||||
@@ -77,6 +79,7 @@ func (t *Trojan) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.
|
|||||||
V2rayHttpUpgrade: t.option.WSOpts.V2rayHttpUpgrade,
|
V2rayHttpUpgrade: t.option.WSOpts.V2rayHttpUpgrade,
|
||||||
V2rayHttpUpgradeFastOpen: t.option.WSOpts.V2rayHttpUpgradeFastOpen,
|
V2rayHttpUpgradeFastOpen: t.option.WSOpts.V2rayHttpUpgradeFastOpen,
|
||||||
ClientFingerprint: t.option.ClientFingerprint,
|
ClientFingerprint: t.option.ClientFingerprint,
|
||||||
|
ECHConfig: t.echConfig,
|
||||||
Headers: http.Header{},
|
Headers: http.Header{},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,39 +94,45 @@ func (t *Trojan) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.
|
|||||||
}
|
}
|
||||||
|
|
||||||
alpn := trojan.DefaultWebsocketALPN
|
alpn := trojan.DefaultWebsocketALPN
|
||||||
if len(t.option.ALPN) != 0 {
|
if t.option.ALPN != nil { // structure's Decode will ensure value not nil when input has value even it was set an empty array
|
||||||
alpn = t.option.ALPN
|
alpn = t.option.ALPN
|
||||||
}
|
}
|
||||||
|
|
||||||
wsOpts.TLS = true
|
wsOpts.TLS = true
|
||||||
tlsConfig := &tls.Config{
|
wsOpts.TLSConfig, err = ca.GetTLSConfig(ca.Option{
|
||||||
NextProtos: alpn,
|
TLSConfig: &tls.Config{
|
||||||
MinVersion: tls.VersionTLS12,
|
NextProtos: alpn,
|
||||||
InsecureSkipVerify: t.option.SkipCertVerify,
|
MinVersion: tls.VersionTLS12,
|
||||||
ServerName: t.option.SNI,
|
InsecureSkipVerify: t.option.SkipCertVerify,
|
||||||
}
|
ServerName: t.option.SNI,
|
||||||
|
},
|
||||||
wsOpts.TLSConfig, err = ca.GetSpecifiedFingerprintTLSConfig(tlsConfig, t.option.Fingerprint)
|
Fingerprint: t.option.Fingerprint,
|
||||||
|
Certificate: t.option.Certificate,
|
||||||
|
PrivateKey: t.option.PrivateKey,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err = vmess.StreamWebsocketConn(ctx, c, wsOpts)
|
c, err = vmess.StreamWebsocketConn(ctx, c, wsOpts)
|
||||||
case "grpc":
|
case "grpc":
|
||||||
c, err = gun.StreamGunWithConn(c, t.gunTLSConfig, t.gunConfig, t.realityConfig)
|
break // already handle in gun transport
|
||||||
default:
|
default:
|
||||||
// default tcp network
|
// default tcp network
|
||||||
// handle TLS
|
// handle TLS
|
||||||
alpn := trojan.DefaultALPN
|
alpn := trojan.DefaultALPN
|
||||||
if len(t.option.ALPN) != 0 {
|
if t.option.ALPN != nil { // structure's Decode will ensure value not nil when input has value even it was set an empty array
|
||||||
alpn = t.option.ALPN
|
alpn = t.option.ALPN
|
||||||
}
|
}
|
||||||
c, err = vmess.StreamTLSConn(ctx, c, &vmess.TLSConfig{
|
c, err = vmess.StreamTLSConn(ctx, c, &vmess.TLSConfig{
|
||||||
Host: t.option.SNI,
|
Host: t.option.SNI,
|
||||||
SkipCertVerify: t.option.SkipCertVerify,
|
SkipCertVerify: t.option.SkipCertVerify,
|
||||||
FingerPrint: t.option.Fingerprint,
|
FingerPrint: t.option.Fingerprint,
|
||||||
|
Certificate: t.option.Certificate,
|
||||||
|
PrivateKey: t.option.PrivateKey,
|
||||||
ClientFingerprint: t.option.ClientFingerprint,
|
ClientFingerprint: t.option.ClientFingerprint,
|
||||||
NextProtos: alpn,
|
NextProtos: alpn,
|
||||||
|
ECH: t.echConfig,
|
||||||
Reality: t.realityConfig,
|
Reality: t.realityConfig,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -165,41 +174,17 @@ func (t *Trojan) writeHeaderContext(ctx context.Context, c net.Conn, metadata *C
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (t *Trojan) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
func (t *Trojan) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
var c net.Conn
|
var c net.Conn
|
||||||
// gun transport
|
// gun transport
|
||||||
if t.transport != nil && dialer.IsZeroOptions(opts) {
|
if t.gunTransport != nil {
|
||||||
c, err = gun.StreamGunWithTransport(t.transport, t.gunConfig)
|
c, err = gun.StreamGunWithTransport(t.gunTransport, t.gunConfig)
|
||||||
if err != nil {
|
} else {
|
||||||
return nil, err
|
c, err = t.dialer.DialContext(ctx, "tcp", t.addr)
|
||||||
}
|
|
||||||
defer func(c net.Conn) {
|
|
||||||
safeConnClose(c, err)
|
|
||||||
}(c)
|
|
||||||
|
|
||||||
c, err = t.streamConnContext(ctx, c, metadata)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return NewConn(c, t), nil
|
|
||||||
}
|
}
|
||||||
return t.DialContextWithDialer(ctx, dialer.NewDialer(t.Base.DialOptions(opts...)...), metadata)
|
|
||||||
}
|
|
||||||
|
|
||||||
// DialContextWithDialer implements C.ProxyAdapter
|
|
||||||
func (t *Trojan) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
|
||||||
if len(t.option.DialerProxy) > 0 {
|
|
||||||
dialer, err = proxydialer.NewByName(t.option.DialerProxy, dialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
c, err := dialer.DialContext(ctx, "tcp", t.addr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error: %w", t.addr, err)
|
return nil, fmt.Errorf("%s connect error: %w", t.addr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func(c net.Conn) {
|
defer func(c net.Conn) {
|
||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
}(c)
|
}(c)
|
||||||
@@ -213,45 +198,25 @@ func (t *Trojan) DialContextWithDialer(ctx context.Context, dialer C.Dialer, met
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (t *Trojan) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
func (t *Trojan) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
|
if err = t.ResolveUDP(ctx, metadata); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
var c net.Conn
|
var c net.Conn
|
||||||
|
|
||||||
// grpc transport
|
// grpc transport
|
||||||
if t.transport != nil && dialer.IsZeroOptions(opts) {
|
if t.gunTransport != nil {
|
||||||
c, err = gun.StreamGunWithTransport(t.transport, t.gunConfig)
|
c, err = gun.StreamGunWithTransport(t.gunTransport, t.gunConfig)
|
||||||
if err != nil {
|
} else {
|
||||||
return nil, fmt.Errorf("%s connect error: %w", t.addr, err)
|
c, err = t.dialer.DialContext(ctx, "tcp", t.addr)
|
||||||
}
|
|
||||||
defer func(c net.Conn) {
|
|
||||||
safeConnClose(c, err)
|
|
||||||
}(c)
|
|
||||||
|
|
||||||
c, err = t.streamConnContext(ctx, c, metadata)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
pc := trojan.NewPacketConn(c)
|
|
||||||
return newPacketConn(pc, t), err
|
|
||||||
}
|
}
|
||||||
return t.ListenPacketWithDialer(ctx, dialer.NewDialer(t.Base.DialOptions(opts...)...), metadata)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListenPacketWithDialer implements C.ProxyAdapter
|
|
||||||
func (t *Trojan) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
|
||||||
if len(t.option.DialerProxy) > 0 {
|
|
||||||
dialer, err = proxydialer.NewByName(t.option.DialerProxy, dialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
c, err := dialer.DialContext(ctx, "tcp", t.addr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error: %w", t.addr, err)
|
return nil, fmt.Errorf("%s connect error: %w", t.addr, err)
|
||||||
}
|
}
|
||||||
defer func(c net.Conn) {
|
defer func(c net.Conn) {
|
||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
}(c)
|
}(c)
|
||||||
|
|
||||||
c, err = t.StreamConnContext(ctx, c, metadata)
|
c, err = t.StreamConnContext(ctx, c, metadata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -261,17 +226,6 @@ func (t *Trojan) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, me
|
|||||||
return newPacketConn(pc, t), err
|
return newPacketConn(pc, t), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// SupportWithDialer implements C.ProxyAdapter
|
|
||||||
func (t *Trojan) SupportWithDialer() C.NetWork {
|
|
||||||
return C.ALLNet
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
|
||||||
func (t *Trojan) ListenPacketOnStreamConn(c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
|
||||||
pc := trojan.NewPacketConn(c)
|
|
||||||
return newPacketConn(pc, t), err
|
|
||||||
}
|
|
||||||
|
|
||||||
// SupportUOT implements C.ProxyAdapter
|
// SupportUOT implements C.ProxyAdapter
|
||||||
func (t *Trojan) SupportUOT() bool {
|
func (t *Trojan) SupportUOT() bool {
|
||||||
return true
|
return true
|
||||||
@@ -286,8 +240,8 @@ func (t *Trojan) ProxyInfo() C.ProxyInfo {
|
|||||||
|
|
||||||
// Close implements C.ProxyAdapter
|
// Close implements C.ProxyAdapter
|
||||||
func (t *Trojan) Close() error {
|
func (t *Trojan) Close() error {
|
||||||
if t.transport != nil {
|
if t.gunTransport != nil {
|
||||||
return t.transport.Close()
|
return t.gunTransport.Close()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -295,21 +249,27 @@ func (t *Trojan) Close() error {
|
|||||||
func NewTrojan(option TrojanOption) (*Trojan, error) {
|
func NewTrojan(option TrojanOption) (*Trojan, error) {
|
||||||
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
||||||
|
|
||||||
|
if option.SNI == "" {
|
||||||
|
option.SNI = option.Server
|
||||||
|
}
|
||||||
|
|
||||||
t := &Trojan{
|
t := &Trojan{
|
||||||
Base: &Base{
|
Base: &Base{
|
||||||
name: option.Name,
|
name: option.Name,
|
||||||
addr: addr,
|
addr: addr,
|
||||||
tp: C.Trojan,
|
tp: C.Trojan,
|
||||||
|
pdName: option.ProviderName,
|
||||||
udp: option.UDP,
|
udp: option.UDP,
|
||||||
tfo: option.TFO,
|
tfo: option.TFO,
|
||||||
mpTcp: option.MPTCP,
|
mpTcp: option.MPTCP,
|
||||||
iface: option.Interface,
|
iface: option.Interface,
|
||||||
rmark: option.RoutingMark,
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: option.IPVersion,
|
||||||
},
|
},
|
||||||
option: &option,
|
option: &option,
|
||||||
hexPassword: trojan.Key(option.Password),
|
hexPassword: trojan.Key(option.Password),
|
||||||
}
|
}
|
||||||
|
t.dialer = option.NewDialer(t.DialOptions())
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
t.realityConfig, err = option.RealityOpts.Parse()
|
t.realityConfig, err = option.RealityOpts.Parse()
|
||||||
@@ -317,6 +277,11 @@ func NewTrojan(option TrojanOption) (*Trojan, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t.echConfig, err = option.ECHOpts.Parse()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
if option.SSOpts.Enabled {
|
if option.SSOpts.Enabled {
|
||||||
if option.SSOpts.Password == "" {
|
if option.SSOpts.Password == "" {
|
||||||
return nil, errors.New("empty password")
|
return nil, errors.New("empty password")
|
||||||
@@ -333,41 +298,31 @@ func NewTrojan(option TrojanOption) (*Trojan, error) {
|
|||||||
|
|
||||||
if option.Network == "grpc" {
|
if option.Network == "grpc" {
|
||||||
dialFn := func(ctx context.Context, network, addr string) (net.Conn, error) {
|
dialFn := func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
var err error
|
c, err := t.dialer.DialContext(ctx, "tcp", t.addr)
|
||||||
var cDialer C.Dialer = dialer.NewDialer(t.Base.DialOptions()...)
|
|
||||||
if len(t.option.DialerProxy) > 0 {
|
|
||||||
cDialer, err = proxydialer.NewByName(t.option.DialerProxy, cDialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
c, err := cDialer.DialContext(ctx, "tcp", t.addr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error: %s", t.addr, err.Error())
|
return nil, fmt.Errorf("%s connect error: %s", t.addr, err.Error())
|
||||||
}
|
}
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
tlsConfig := &tls.Config{
|
tlsConfig := &vmess.TLSConfig{
|
||||||
NextProtos: option.ALPN,
|
|
||||||
MinVersion: tls.VersionTLS12,
|
|
||||||
InsecureSkipVerify: option.SkipCertVerify,
|
|
||||||
ServerName: option.SNI,
|
|
||||||
}
|
|
||||||
|
|
||||||
var err error
|
|
||||||
tlsConfig, err = ca.GetSpecifiedFingerprintTLSConfig(tlsConfig, option.Fingerprint)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
t.transport = gun.NewHTTP2Client(dialFn, tlsConfig, option.ClientFingerprint, t.realityConfig)
|
|
||||||
|
|
||||||
t.gunTLSConfig = tlsConfig
|
|
||||||
t.gunConfig = &gun.Config{
|
|
||||||
ServiceName: option.GrpcOpts.GrpcServiceName,
|
|
||||||
Host: option.SNI,
|
Host: option.SNI,
|
||||||
|
SkipCertVerify: option.SkipCertVerify,
|
||||||
|
FingerPrint: option.Fingerprint,
|
||||||
|
Certificate: option.Certificate,
|
||||||
|
PrivateKey: option.PrivateKey,
|
||||||
ClientFingerprint: option.ClientFingerprint,
|
ClientFingerprint: option.ClientFingerprint,
|
||||||
|
NextProtos: []string{"h2"},
|
||||||
|
ECH: t.echConfig,
|
||||||
|
Reality: t.realityConfig,
|
||||||
|
}
|
||||||
|
|
||||||
|
t.gunTransport = gun.NewHTTP2Client(dialFn, tlsConfig)
|
||||||
|
|
||||||
|
t.gunConfig = &gun.Config{
|
||||||
|
ServiceName: option.GrpcOpts.GrpcServiceName,
|
||||||
|
UserAgent: option.GrpcOpts.GrpcUserAgent,
|
||||||
|
Host: option.SNI,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
144
adapter/outbound/trusttunnel.go
Normal file
144
adapter/outbound/trusttunnel.go
Normal file
@@ -0,0 +1,144 @@
|
|||||||
|
package outbound
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net"
|
||||||
|
"net/netip"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
|
C "github.com/metacubex/mihomo/constant"
|
||||||
|
"github.com/metacubex/mihomo/transport/trusttunnel"
|
||||||
|
"github.com/metacubex/mihomo/transport/vmess"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TrustTunnel struct {
|
||||||
|
*Base
|
||||||
|
client *trusttunnel.Client
|
||||||
|
option *TrustTunnelOption
|
||||||
|
}
|
||||||
|
|
||||||
|
type TrustTunnelOption struct {
|
||||||
|
BasicOption
|
||||||
|
Name string `proxy:"name"`
|
||||||
|
Server string `proxy:"server"`
|
||||||
|
Port int `proxy:"port"`
|
||||||
|
UserName string `proxy:"username,omitempty"`
|
||||||
|
Password string `proxy:"password,omitempty"`
|
||||||
|
ALPN []string `proxy:"alpn,omitempty"`
|
||||||
|
SNI string `proxy:"sni,omitempty"`
|
||||||
|
ECHOpts ECHOptions `proxy:"ech-opts,omitempty"`
|
||||||
|
ClientFingerprint string `proxy:"client-fingerprint,omitempty"`
|
||||||
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
||||||
|
Fingerprint string `proxy:"fingerprint,omitempty"`
|
||||||
|
Certificate string `proxy:"certificate,omitempty"`
|
||||||
|
PrivateKey string `proxy:"private-key,omitempty"`
|
||||||
|
UDP bool `proxy:"udp,omitempty"`
|
||||||
|
HealthCheck bool `proxy:"health-check,omitempty"`
|
||||||
|
|
||||||
|
Quic bool `proxy:"quic,omitempty"`
|
||||||
|
CongestionController string `proxy:"congestion-controller,omitempty"`
|
||||||
|
CWND int `proxy:"cwnd,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TrustTunnel) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
|
c, err := t.client.Dial(ctx, metadata.RemoteAddress())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return NewConn(c, t), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TrustTunnel) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
|
if err = t.ResolveUDP(ctx, metadata); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
pc, err := t.client.ListenPacket(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return newPacketConn(N.NewThreadSafePacketConn(pc), t), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SupportUOT implements C.ProxyAdapter
|
||||||
|
func (t *TrustTunnel) SupportUOT() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// ProxyInfo implements C.ProxyAdapter
|
||||||
|
func (t *TrustTunnel) ProxyInfo() C.ProxyInfo {
|
||||||
|
info := t.Base.ProxyInfo()
|
||||||
|
info.DialerProxy = t.option.DialerProxy
|
||||||
|
return info
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close implements C.ProxyAdapter
|
||||||
|
func (t *TrustTunnel) Close() error {
|
||||||
|
return t.client.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTrustTunnel(option TrustTunnelOption) (*TrustTunnel, error) {
|
||||||
|
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
||||||
|
outbound := &TrustTunnel{
|
||||||
|
Base: &Base{
|
||||||
|
name: option.Name,
|
||||||
|
addr: addr,
|
||||||
|
tp: C.TrustTunnel,
|
||||||
|
pdName: option.ProviderName,
|
||||||
|
udp: option.UDP,
|
||||||
|
tfo: option.TFO,
|
||||||
|
mpTcp: option.MPTCP,
|
||||||
|
iface: option.Interface,
|
||||||
|
rmark: option.RoutingMark,
|
||||||
|
prefer: option.IPVersion,
|
||||||
|
},
|
||||||
|
option: &option,
|
||||||
|
}
|
||||||
|
outbound.dialer = option.NewDialer(outbound.DialOptions())
|
||||||
|
|
||||||
|
tOption := trusttunnel.ClientOptions{
|
||||||
|
Dialer: outbound.dialer,
|
||||||
|
ResolvUDP: func(ctx context.Context, server string) (netip.AddrPort, error) {
|
||||||
|
udpAddr, err := resolveUDPAddr(ctx, "udp", server, option.IPVersion)
|
||||||
|
if err != nil {
|
||||||
|
return netip.AddrPort{}, err
|
||||||
|
}
|
||||||
|
return udpAddr.AddrPort(), nil
|
||||||
|
},
|
||||||
|
Server: addr,
|
||||||
|
Username: option.UserName,
|
||||||
|
Password: option.Password,
|
||||||
|
QUIC: option.Quic,
|
||||||
|
QUICCongestionControl: option.CongestionController,
|
||||||
|
QUICCwnd: option.CWND,
|
||||||
|
HealthCheck: option.HealthCheck,
|
||||||
|
}
|
||||||
|
echConfig, err := option.ECHOpts.Parse()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
tlsConfig := &vmess.TLSConfig{
|
||||||
|
Host: option.SNI,
|
||||||
|
SkipCertVerify: option.SkipCertVerify,
|
||||||
|
NextProtos: option.ALPN,
|
||||||
|
FingerPrint: option.Fingerprint,
|
||||||
|
Certificate: option.Certificate,
|
||||||
|
PrivateKey: option.PrivateKey,
|
||||||
|
ClientFingerprint: option.ClientFingerprint,
|
||||||
|
ECH: echConfig,
|
||||||
|
}
|
||||||
|
if tlsConfig.Host == "" {
|
||||||
|
tlsConfig.Host = option.Server
|
||||||
|
}
|
||||||
|
tOption.TLSConfig = tlsConfig
|
||||||
|
|
||||||
|
client, err := trusttunnel.NewClient(context.TODO(), tOption)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
outbound.client = client
|
||||||
|
|
||||||
|
return outbound, nil
|
||||||
|
}
|
||||||
@@ -2,8 +2,6 @@ package outbound
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"net"
|
"net"
|
||||||
@@ -11,22 +9,24 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/component/ca"
|
"github.com/metacubex/mihomo/component/ca"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"github.com/metacubex/mihomo/component/ech"
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
|
||||||
"github.com/metacubex/mihomo/component/resolver"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/transport/tuic"
|
"github.com/metacubex/mihomo/transport/tuic"
|
||||||
|
|
||||||
"github.com/gofrs/uuid/v5"
|
"github.com/gofrs/uuid/v5"
|
||||||
"github.com/metacubex/quic-go"
|
"github.com/metacubex/quic-go"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/metacubex/sing/common/metadata"
|
||||||
"github.com/sagernet/sing/common/uot"
|
"github.com/metacubex/sing/common/uot"
|
||||||
|
"github.com/metacubex/tls"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Tuic struct {
|
type Tuic struct {
|
||||||
*Base
|
*Base
|
||||||
option *TuicOption
|
option *TuicOption
|
||||||
client *tuic.PoolClient
|
client *tuic.PoolClient
|
||||||
|
|
||||||
|
tlsConfig *tls.Config
|
||||||
|
echConfig *ech.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
type TuicOption struct {
|
type TuicOption struct {
|
||||||
@@ -47,31 +47,27 @@ type TuicOption struct {
|
|||||||
DisableSni bool `proxy:"disable-sni,omitempty"`
|
DisableSni bool `proxy:"disable-sni,omitempty"`
|
||||||
MaxUdpRelayPacketSize int `proxy:"max-udp-relay-packet-size,omitempty"`
|
MaxUdpRelayPacketSize int `proxy:"max-udp-relay-packet-size,omitempty"`
|
||||||
|
|
||||||
FastOpen bool `proxy:"fast-open,omitempty"`
|
FastOpen bool `proxy:"fast-open,omitempty"`
|
||||||
MaxOpenStreams int `proxy:"max-open-streams,omitempty"`
|
MaxOpenStreams int `proxy:"max-open-streams,omitempty"`
|
||||||
CWND int `proxy:"cwnd,omitempty"`
|
CWND int `proxy:"cwnd,omitempty"`
|
||||||
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
||||||
Fingerprint string `proxy:"fingerprint,omitempty"`
|
Fingerprint string `proxy:"fingerprint,omitempty"`
|
||||||
CustomCA string `proxy:"ca,omitempty"`
|
Certificate string `proxy:"certificate,omitempty"`
|
||||||
CustomCAString string `proxy:"ca-str,omitempty"`
|
PrivateKey string `proxy:"private-key,omitempty"`
|
||||||
ReceiveWindowConn int `proxy:"recv-window-conn,omitempty"`
|
ReceiveWindowConn int `proxy:"recv-window-conn,omitempty"`
|
||||||
ReceiveWindow int `proxy:"recv-window,omitempty"`
|
ReceiveWindow int `proxy:"recv-window,omitempty"`
|
||||||
DisableMTUDiscovery bool `proxy:"disable-mtu-discovery,omitempty"`
|
DisableMTUDiscovery bool `proxy:"disable-mtu-discovery,omitempty"`
|
||||||
MaxDatagramFrameSize int `proxy:"max-datagram-frame-size,omitempty"`
|
MaxDatagramFrameSize int `proxy:"max-datagram-frame-size,omitempty"`
|
||||||
SNI string `proxy:"sni,omitempty"`
|
SNI string `proxy:"sni,omitempty"`
|
||||||
|
ECHOpts ECHOptions `proxy:"ech-opts,omitempty"`
|
||||||
|
|
||||||
UDPOverStream bool `proxy:"udp-over-stream,omitempty"`
|
UDPOverStream bool `proxy:"udp-over-stream,omitempty"`
|
||||||
UDPOverStreamVersion int `proxy:"udp-over-stream-version,omitempty"`
|
UDPOverStreamVersion int `proxy:"udp-over-stream-version,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (t *Tuic) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
func (t *Tuic) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
||||||
return t.DialContextWithDialer(ctx, dialer.NewDialer(t.Base.DialOptions(opts...)...), metadata)
|
conn, err := t.client.DialContext(ctx, metadata)
|
||||||
}
|
|
||||||
|
|
||||||
// DialContextWithDialer implements C.ProxyAdapter
|
|
||||||
func (t *Tuic) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (C.Conn, error) {
|
|
||||||
conn, err := t.client.DialContextWithDialer(ctx, metadata, dialer, t.dialWithDialer)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -79,30 +75,22 @@ func (t *Tuic) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metad
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (t *Tuic) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
func (t *Tuic) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
return t.ListenPacketWithDialer(ctx, dialer.NewDialer(t.Base.DialOptions(opts...)...), metadata)
|
if err = t.ResolveUDP(ctx, metadata); err != nil {
|
||||||
}
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
// ListenPacketWithDialer implements C.ProxyAdapter
|
|
||||||
func (t *Tuic) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
|
||||||
if t.option.UDPOverStream {
|
if t.option.UDPOverStream {
|
||||||
uotDestination := uot.RequestDestination(uint8(t.option.UDPOverStreamVersion))
|
uotDestination := uot.RequestDestination(uint8(t.option.UDPOverStreamVersion))
|
||||||
uotMetadata := *metadata
|
uotMetadata := *metadata
|
||||||
uotMetadata.Host = uotDestination.Fqdn
|
uotMetadata.Host = uotDestination.Fqdn
|
||||||
uotMetadata.DstPort = uotDestination.Port
|
uotMetadata.DstPort = uotDestination.Port
|
||||||
c, err := t.DialContextWithDialer(ctx, dialer, &uotMetadata)
|
c, err := t.DialContext(ctx, &uotMetadata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// tuic uos use stream-oriented udp with a special address, so we need a net.UDPAddr
|
// tuic uos use stream-oriented udp with a special address, so we need a net.UDPAddr
|
||||||
if !metadata.Resolved() {
|
|
||||||
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("can't resolve ip")
|
|
||||||
}
|
|
||||||
metadata.DstIP = ip
|
|
||||||
}
|
|
||||||
|
|
||||||
destination := M.SocksaddrFromNet(metadata.UDPAddr())
|
destination := M.SocksaddrFromNet(metadata.UDPAddr())
|
||||||
if t.option.UDPOverStreamVersion == uot.LegacyVersion {
|
if t.option.UDPOverStreamVersion == uot.LegacyVersion {
|
||||||
@@ -111,32 +99,25 @@ func (t *Tuic) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, meta
|
|||||||
return newPacketConn(uot.NewLazyConn(c, uot.Request{Destination: destination}), t), nil
|
return newPacketConn(uot.NewLazyConn(c, uot.Request{Destination: destination}), t), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pc, err := t.client.ListenPacketWithDialer(ctx, metadata, dialer, t.dialWithDialer)
|
pc, err := t.client.ListenPacket(ctx, metadata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return newPacketConn(pc, t), nil
|
return newPacketConn(pc, t), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SupportWithDialer implements C.ProxyAdapter
|
func (t *Tuic) dial(ctx context.Context) (transport *quic.Transport, addr net.Addr, err error) {
|
||||||
func (t *Tuic) SupportWithDialer() C.NetWork {
|
|
||||||
return C.ALLNet
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *Tuic) dialWithDialer(ctx context.Context, dialer C.Dialer) (transport *quic.Transport, addr net.Addr, err error) {
|
|
||||||
if len(t.option.DialerProxy) > 0 {
|
|
||||||
dialer, err = proxydialer.NewByName(t.option.DialerProxy, dialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
udpAddr, err := resolveUDPAddr(ctx, "udp", t.addr, t.prefer)
|
udpAddr, err := resolveUDPAddr(ctx, "udp", t.addr, t.prefer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
err = t.echConfig.ClientHandle(ctx, t.tlsConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
addr = udpAddr
|
addr = udpAddr
|
||||||
var pc net.PacketConn
|
var pc net.PacketConn
|
||||||
pc, err = dialer.ListenPacket(ctx, "udp", "", udpAddr.AddrPort())
|
pc, err = t.dialer.ListenPacket(ctx, "udp", "", udpAddr.AddrPort())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@@ -156,17 +137,20 @@ func (t *Tuic) ProxyInfo() C.ProxyInfo {
|
|||||||
func NewTuic(option TuicOption) (*Tuic, error) {
|
func NewTuic(option TuicOption) (*Tuic, error) {
|
||||||
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
||||||
serverName := option.Server
|
serverName := option.Server
|
||||||
tlsConfig := &tls.Config{
|
|
||||||
ServerName: serverName,
|
|
||||||
InsecureSkipVerify: option.SkipCertVerify,
|
|
||||||
MinVersion: tls.VersionTLS13,
|
|
||||||
}
|
|
||||||
if option.SNI != "" {
|
if option.SNI != "" {
|
||||||
tlsConfig.ServerName = option.SNI
|
serverName = option.SNI
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
tlsConfig, err := ca.GetTLSConfig(ca.Option{
|
||||||
tlsConfig, err = ca.GetTLSConfig(tlsConfig, option.Fingerprint, option.CustomCA, option.CustomCAString)
|
TLSConfig: &tls.Config{
|
||||||
|
ServerName: serverName,
|
||||||
|
InsecureSkipVerify: option.SkipCertVerify,
|
||||||
|
MinVersion: tls.VersionTLS13,
|
||||||
|
},
|
||||||
|
Fingerprint: option.Fingerprint,
|
||||||
|
Certificate: option.Certificate,
|
||||||
|
PrivateKey: option.PrivateKey,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -248,6 +232,12 @@ func NewTuic(option TuicOption) (*Tuic, error) {
|
|||||||
tlsConfig.InsecureSkipVerify = true // tls: either ServerName or InsecureSkipVerify must be specified in the tls.Config
|
tlsConfig.InsecureSkipVerify = true // tls: either ServerName or InsecureSkipVerify must be specified in the tls.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tlsClientConfig := tlsConfig
|
||||||
|
echConfig, err := option.ECHOpts.Parse()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
switch option.UDPOverStreamVersion {
|
switch option.UDPOverStreamVersion {
|
||||||
case uot.Version, uot.LegacyVersion:
|
case uot.Version, uot.LegacyVersion:
|
||||||
case 0:
|
case 0:
|
||||||
@@ -261,14 +251,18 @@ func NewTuic(option TuicOption) (*Tuic, error) {
|
|||||||
name: option.Name,
|
name: option.Name,
|
||||||
addr: addr,
|
addr: addr,
|
||||||
tp: C.Tuic,
|
tp: C.Tuic,
|
||||||
|
pdName: option.ProviderName,
|
||||||
udp: true,
|
udp: true,
|
||||||
tfo: option.FastOpen,
|
tfo: option.FastOpen,
|
||||||
iface: option.Interface,
|
iface: option.Interface,
|
||||||
rmark: option.RoutingMark,
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: option.IPVersion,
|
||||||
},
|
},
|
||||||
option: &option,
|
option: &option,
|
||||||
|
tlsConfig: tlsClientConfig,
|
||||||
|
echConfig: echConfig,
|
||||||
}
|
}
|
||||||
|
t.dialer = option.NewDialer(t.DialOptions())
|
||||||
|
|
||||||
clientMaxOpenStreams := int64(option.MaxOpenStreams)
|
clientMaxOpenStreams := int64(option.MaxOpenStreams)
|
||||||
|
|
||||||
@@ -284,7 +278,7 @@ func NewTuic(option TuicOption) (*Tuic, error) {
|
|||||||
if len(option.Token) > 0 {
|
if len(option.Token) > 0 {
|
||||||
tkn := tuic.GenTKN(option.Token)
|
tkn := tuic.GenTKN(option.Token)
|
||||||
clientOption := &tuic.ClientOptionV4{
|
clientOption := &tuic.ClientOptionV4{
|
||||||
TlsConfig: tlsConfig,
|
TlsConfig: tlsClientConfig,
|
||||||
QuicConfig: quicConfig,
|
QuicConfig: quicConfig,
|
||||||
Token: tkn,
|
Token: tkn,
|
||||||
UdpRelayMode: udpRelayMode,
|
UdpRelayMode: udpRelayMode,
|
||||||
@@ -297,14 +291,14 @@ func NewTuic(option TuicOption) (*Tuic, error) {
|
|||||||
CWND: option.CWND,
|
CWND: option.CWND,
|
||||||
}
|
}
|
||||||
|
|
||||||
t.client = tuic.NewPoolClientV4(clientOption)
|
t.client = tuic.NewPoolClientV4(clientOption, t.dial)
|
||||||
} else {
|
} else {
|
||||||
maxUdpRelayPacketSize := option.MaxUdpRelayPacketSize
|
maxUdpRelayPacketSize := option.MaxUdpRelayPacketSize
|
||||||
if maxUdpRelayPacketSize > tuic.MaxFragSizeV5 {
|
if maxUdpRelayPacketSize > tuic.MaxFragSizeV5 {
|
||||||
maxUdpRelayPacketSize = tuic.MaxFragSizeV5
|
maxUdpRelayPacketSize = tuic.MaxFragSizeV5
|
||||||
}
|
}
|
||||||
clientOption := &tuic.ClientOptionV5{
|
clientOption := &tuic.ClientOptionV5{
|
||||||
TlsConfig: tlsConfig,
|
TlsConfig: tlsClientConfig,
|
||||||
QuicConfig: quicConfig,
|
QuicConfig: quicConfig,
|
||||||
Uuid: uuid.FromStringOrNil(option.UUID),
|
Uuid: uuid.FromStringOrNil(option.UUID),
|
||||||
Password: option.Password,
|
Password: option.Password,
|
||||||
@@ -316,7 +310,7 @@ func NewTuic(option TuicOption) (*Tuic, error) {
|
|||||||
CWND: option.CWND,
|
CWND: option.CWND,
|
||||||
}
|
}
|
||||||
|
|
||||||
t.client = tuic.NewPoolClientV5(clientOption)
|
t.client = tuic.NewPoolClientV5(clientOption, t.dial)
|
||||||
}
|
}
|
||||||
|
|
||||||
return t, nil
|
return t, nil
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ func resolveUDPAddr(ctx context.Context, network, address string, prefer C.DNSPr
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var ip netip.Addr
|
var ip netip.Addr
|
||||||
switch prefer {
|
switch prefer {
|
||||||
case C.IPv4Only:
|
case C.IPv4Only:
|
||||||
@@ -56,7 +57,17 @@ func resolveUDPAddr(ctx context.Context, network, address string, prefer C.DNSPr
|
|||||||
}
|
}
|
||||||
|
|
||||||
ip, port = resolver.LookupIP4P(ip, port)
|
ip, port = resolver.LookupIP4P(ip, port)
|
||||||
return net.ResolveUDPAddr(network, net.JoinHostPort(ip.String(), port))
|
|
||||||
|
var uint16Port uint16
|
||||||
|
if port, err := strconv.ParseUint(port, 10, 16); err == nil {
|
||||||
|
uint16Port = uint16(port)
|
||||||
|
} else {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// our resolver always unmap before return, so unneeded unmap at here
|
||||||
|
// which is different with net.ResolveUDPAddr maybe return 4in6 address
|
||||||
|
// 4in6 addresses can cause some strange effects on sing-based code
|
||||||
|
return net.UDPAddrFromAddrPort(netip.AddrPortFrom(ip, uint16Port)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func safeConnClose(c net.Conn, err error) {
|
func safeConnClose(c net.Conn, err error) {
|
||||||
|
|||||||
@@ -2,37 +2,27 @@ package outbound
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
|
||||||
"encoding/binary"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/convert"
|
"github.com/metacubex/mihomo/common/convert"
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
"github.com/metacubex/mihomo/component/ca"
|
"github.com/metacubex/mihomo/component/ca"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"github.com/metacubex/mihomo/component/ech"
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
|
||||||
"github.com/metacubex/mihomo/component/resolver"
|
|
||||||
tlsC "github.com/metacubex/mihomo/component/tls"
|
tlsC "github.com/metacubex/mihomo/component/tls"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/transport/gun"
|
"github.com/metacubex/mihomo/transport/gun"
|
||||||
"github.com/metacubex/mihomo/transport/vless"
|
"github.com/metacubex/mihomo/transport/vless"
|
||||||
|
"github.com/metacubex/mihomo/transport/vless/encryption"
|
||||||
"github.com/metacubex/mihomo/transport/vmess"
|
"github.com/metacubex/mihomo/transport/vmess"
|
||||||
|
|
||||||
|
"github.com/metacubex/http"
|
||||||
vmessSing "github.com/metacubex/sing-vmess"
|
vmessSing "github.com/metacubex/sing-vmess"
|
||||||
"github.com/metacubex/sing-vmess/packetaddr"
|
"github.com/metacubex/sing-vmess/packetaddr"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/metacubex/sing/common/metadata"
|
||||||
)
|
"github.com/metacubex/tls"
|
||||||
|
|
||||||
const (
|
|
||||||
// max packet length
|
|
||||||
maxLength = 1024 << 3
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Vless struct {
|
type Vless struct {
|
||||||
@@ -40,12 +30,14 @@ type Vless struct {
|
|||||||
client *vless.Client
|
client *vless.Client
|
||||||
option *VlessOption
|
option *VlessOption
|
||||||
|
|
||||||
|
encryption *encryption.ClientInstance
|
||||||
|
|
||||||
// for gun mux
|
// for gun mux
|
||||||
gunTLSConfig *tls.Config
|
|
||||||
gunConfig *gun.Config
|
gunConfig *gun.Config
|
||||||
transport *gun.TransportWrap
|
gunTransport *gun.TransportWrap
|
||||||
|
|
||||||
realityConfig *tlsC.RealityConfig
|
realityConfig *tlsC.RealityConfig
|
||||||
|
echConfig *ech.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
type VlessOption struct {
|
type VlessOption struct {
|
||||||
@@ -61,16 +53,19 @@ type VlessOption struct {
|
|||||||
PacketAddr bool `proxy:"packet-addr,omitempty"`
|
PacketAddr bool `proxy:"packet-addr,omitempty"`
|
||||||
XUDP bool `proxy:"xudp,omitempty"`
|
XUDP bool `proxy:"xudp,omitempty"`
|
||||||
PacketEncoding string `proxy:"packet-encoding,omitempty"`
|
PacketEncoding string `proxy:"packet-encoding,omitempty"`
|
||||||
|
Encryption string `proxy:"encryption,omitempty"`
|
||||||
Network string `proxy:"network,omitempty"`
|
Network string `proxy:"network,omitempty"`
|
||||||
|
ECHOpts ECHOptions `proxy:"ech-opts,omitempty"`
|
||||||
RealityOpts RealityOptions `proxy:"reality-opts,omitempty"`
|
RealityOpts RealityOptions `proxy:"reality-opts,omitempty"`
|
||||||
HTTPOpts HTTPOptions `proxy:"http-opts,omitempty"`
|
HTTPOpts HTTPOptions `proxy:"http-opts,omitempty"`
|
||||||
HTTP2Opts HTTP2Options `proxy:"h2-opts,omitempty"`
|
HTTP2Opts HTTP2Options `proxy:"h2-opts,omitempty"`
|
||||||
GrpcOpts GrpcOptions `proxy:"grpc-opts,omitempty"`
|
GrpcOpts GrpcOptions `proxy:"grpc-opts,omitempty"`
|
||||||
WSOpts WSOptions `proxy:"ws-opts,omitempty"`
|
WSOpts WSOptions `proxy:"ws-opts,omitempty"`
|
||||||
WSPath string `proxy:"ws-path,omitempty"`
|
|
||||||
WSHeaders map[string]string `proxy:"ws-headers,omitempty"`
|
WSHeaders map[string]string `proxy:"ws-headers,omitempty"`
|
||||||
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
||||||
Fingerprint string `proxy:"fingerprint,omitempty"`
|
Fingerprint string `proxy:"fingerprint,omitempty"`
|
||||||
|
Certificate string `proxy:"certificate,omitempty"`
|
||||||
|
PrivateKey string `proxy:"private-key,omitempty"`
|
||||||
ServerName string `proxy:"servername,omitempty"`
|
ServerName string `proxy:"servername,omitempty"`
|
||||||
ClientFingerprint string `proxy:"client-fingerprint,omitempty"`
|
ClientFingerprint string `proxy:"client-fingerprint,omitempty"`
|
||||||
}
|
}
|
||||||
@@ -88,6 +83,7 @@ func (v *Vless) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
V2rayHttpUpgrade: v.option.WSOpts.V2rayHttpUpgrade,
|
V2rayHttpUpgrade: v.option.WSOpts.V2rayHttpUpgrade,
|
||||||
V2rayHttpUpgradeFastOpen: v.option.WSOpts.V2rayHttpUpgradeFastOpen,
|
V2rayHttpUpgradeFastOpen: v.option.WSOpts.V2rayHttpUpgradeFastOpen,
|
||||||
ClientFingerprint: v.option.ClientFingerprint,
|
ClientFingerprint: v.option.ClientFingerprint,
|
||||||
|
ECHConfig: v.echConfig,
|
||||||
Headers: http.Header{},
|
Headers: http.Header{},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,14 +94,17 @@ func (v *Vless) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
}
|
}
|
||||||
if v.option.TLS {
|
if v.option.TLS {
|
||||||
wsOpts.TLS = true
|
wsOpts.TLS = true
|
||||||
tlsConfig := &tls.Config{
|
wsOpts.TLSConfig, err = ca.GetTLSConfig(ca.Option{
|
||||||
MinVersion: tls.VersionTLS12,
|
TLSConfig: &tls.Config{
|
||||||
ServerName: host,
|
MinVersion: tls.VersionTLS12,
|
||||||
InsecureSkipVerify: v.option.SkipCertVerify,
|
ServerName: host,
|
||||||
NextProtos: []string{"http/1.1"},
|
InsecureSkipVerify: v.option.SkipCertVerify,
|
||||||
}
|
NextProtos: []string{"http/1.1"},
|
||||||
|
},
|
||||||
wsOpts.TLSConfig, err = ca.GetSpecifiedFingerprintTLSConfig(tlsConfig, v.option.Fingerprint)
|
Fingerprint: v.option.Fingerprint,
|
||||||
|
Certificate: v.option.Certificate,
|
||||||
|
PrivateKey: v.option.PrivateKey,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -151,7 +150,7 @@ func (v *Vless) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
|
|
||||||
c, err = vmess.StreamH2Conn(ctx, c, h2Opts)
|
c, err = vmess.StreamH2Conn(ctx, c, h2Opts)
|
||||||
case "grpc":
|
case "grpc":
|
||||||
c, err = gun.StreamGunWithConn(c, v.gunTLSConfig, v.gunConfig, v.realityConfig)
|
break // already handle in gun transport
|
||||||
default:
|
default:
|
||||||
// default tcp network
|
// default tcp network
|
||||||
// handle TLS
|
// handle TLS
|
||||||
@@ -170,6 +169,12 @@ func (v *Vless) streamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
done := N.SetupContextForConn(ctx, c)
|
done := N.SetupContextForConn(ctx, c)
|
||||||
defer done(&err)
|
defer done(&err)
|
||||||
}
|
}
|
||||||
|
if v.encryption != nil {
|
||||||
|
c, err = v.encryption.Handshake(c)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
if metadata.NetWork == C.UDP {
|
if metadata.NetWork == C.UDP {
|
||||||
if v.option.PacketAddr {
|
if v.option.PacketAddr {
|
||||||
metadata = &C.Metadata{
|
metadata = &C.Metadata{
|
||||||
@@ -185,9 +190,6 @@ func (v *Vless) streamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
conn, err = v.client.StreamConn(c, parseVlessAddr(metadata, v.option.XUDP))
|
conn, err = v.client.StreamConn(c, parseVlessAddr(metadata, v.option.XUDP))
|
||||||
if v.option.PacketAddr {
|
|
||||||
conn = packetaddr.NewBindConn(conn)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
conn, err = v.client.StreamConn(c, parseVlessAddr(metadata, false))
|
conn, err = v.client.StreamConn(c, parseVlessAddr(metadata, false))
|
||||||
}
|
}
|
||||||
@@ -205,7 +207,10 @@ func (v *Vless) streamTLSConn(ctx context.Context, conn net.Conn, isH2 bool) (ne
|
|||||||
Host: host,
|
Host: host,
|
||||||
SkipCertVerify: v.option.SkipCertVerify,
|
SkipCertVerify: v.option.SkipCertVerify,
|
||||||
FingerPrint: v.option.Fingerprint,
|
FingerPrint: v.option.Fingerprint,
|
||||||
|
Certificate: v.option.Certificate,
|
||||||
|
PrivateKey: v.option.PrivateKey,
|
||||||
ClientFingerprint: v.option.ClientFingerprint,
|
ClientFingerprint: v.option.ClientFingerprint,
|
||||||
|
ECH: v.echConfig,
|
||||||
Reality: v.realityConfig,
|
Reality: v.realityConfig,
|
||||||
NextProtos: v.option.ALPN,
|
NextProtos: v.option.ALPN,
|
||||||
}
|
}
|
||||||
@@ -225,37 +230,14 @@ func (v *Vless) streamTLSConn(ctx context.Context, conn net.Conn, isH2 bool) (ne
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (v *Vless) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
func (v *Vless) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
var c net.Conn
|
var c net.Conn
|
||||||
// gun transport
|
// gun transport
|
||||||
if v.transport != nil && dialer.IsZeroOptions(opts) {
|
if v.gunTransport != nil {
|
||||||
c, err = gun.StreamGunWithTransport(v.transport, v.gunConfig)
|
c, err = gun.StreamGunWithTransport(v.gunTransport, v.gunConfig)
|
||||||
if err != nil {
|
} else {
|
||||||
return nil, err
|
c, err = v.dialer.DialContext(ctx, "tcp", v.addr)
|
||||||
}
|
|
||||||
defer func(c net.Conn) {
|
|
||||||
safeConnClose(c, err)
|
|
||||||
}(c)
|
|
||||||
|
|
||||||
c, err = v.streamConnContext(ctx, c, metadata)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return NewConn(c, v), nil
|
|
||||||
}
|
}
|
||||||
return v.DialContextWithDialer(ctx, dialer.NewDialer(v.Base.DialOptions(opts...)...), metadata)
|
|
||||||
}
|
|
||||||
|
|
||||||
// DialContextWithDialer implements C.ProxyAdapter
|
|
||||||
func (v *Vless) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
|
||||||
if len(v.option.DialerProxy) > 0 {
|
|
||||||
dialer, err = proxydialer.NewByName(v.option.DialerProxy, dialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
c, err := dialer.DialContext(ctx, "tcp", v.addr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
||||||
}
|
}
|
||||||
@@ -271,55 +253,17 @@ func (v *Vless) DialContextWithDialer(ctx context.Context, dialer C.Dialer, meta
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (v *Vless) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
func (v *Vless) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
// vless use stream-oriented udp with a special address, so we need a net.UDPAddr
|
if err = v.ResolveUDP(ctx, metadata); err != nil {
|
||||||
if !metadata.Resolved() {
|
return nil, err
|
||||||
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("can't resolve ip")
|
|
||||||
}
|
|
||||||
metadata.DstIP = ip
|
|
||||||
}
|
}
|
||||||
var c net.Conn
|
var c net.Conn
|
||||||
// gun transport
|
// gun transport
|
||||||
if v.transport != nil && dialer.IsZeroOptions(opts) {
|
if v.gunTransport != nil {
|
||||||
c, err = gun.StreamGunWithTransport(v.transport, v.gunConfig)
|
c, err = gun.StreamGunWithTransport(v.gunTransport, v.gunConfig)
|
||||||
if err != nil {
|
} else {
|
||||||
return nil, err
|
c, err = v.dialer.DialContext(ctx, "tcp", v.addr)
|
||||||
}
|
|
||||||
defer func(c net.Conn) {
|
|
||||||
safeConnClose(c, err)
|
|
||||||
}(c)
|
|
||||||
|
|
||||||
c, err = v.streamConnContext(ctx, c, metadata)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("new vless client error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return v.ListenPacketOnStreamConn(ctx, c, metadata)
|
|
||||||
}
|
}
|
||||||
return v.ListenPacketWithDialer(ctx, dialer.NewDialer(v.Base.DialOptions(opts...)...), metadata)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListenPacketWithDialer implements C.ProxyAdapter
|
|
||||||
func (v *Vless) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
|
||||||
if len(v.option.DialerProxy) > 0 {
|
|
||||||
dialer, err = proxydialer.NewByName(v.option.DialerProxy, dialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// vless use stream-oriented udp with a special address, so we need a net.UDPAddr
|
|
||||||
if !metadata.Resolved() {
|
|
||||||
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("can't resolve ip")
|
|
||||||
}
|
|
||||||
metadata.DstIP = ip
|
|
||||||
}
|
|
||||||
|
|
||||||
c, err := dialer.DialContext(ctx, "tcp", v.addr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
||||||
}
|
}
|
||||||
@@ -335,20 +279,10 @@ func (v *Vless) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, met
|
|||||||
return v.ListenPacketOnStreamConn(ctx, c, metadata)
|
return v.ListenPacketOnStreamConn(ctx, c, metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SupportWithDialer implements C.ProxyAdapter
|
|
||||||
func (v *Vless) SupportWithDialer() C.NetWork {
|
|
||||||
return C.ALLNet
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
||||||
func (v *Vless) ListenPacketOnStreamConn(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
func (v *Vless) ListenPacketOnStreamConn(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
// vless use stream-oriented udp with a special address, so we need a net.UDPAddr
|
if err = v.ResolveUDP(ctx, metadata); err != nil {
|
||||||
if !metadata.Resolved() {
|
return nil, err
|
||||||
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("can't resolve ip")
|
|
||||||
}
|
|
||||||
metadata.DstIP = ip
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.option.XUDP {
|
if v.option.XUDP {
|
||||||
@@ -363,12 +297,11 @@ func (v *Vless) ListenPacketOnStreamConn(ctx context.Context, c net.Conn, metada
|
|||||||
), v), nil
|
), v), nil
|
||||||
} else if v.option.PacketAddr {
|
} else if v.option.PacketAddr {
|
||||||
return newPacketConn(N.NewThreadSafePacketConn(
|
return newPacketConn(N.NewThreadSafePacketConn(
|
||||||
packetaddr.NewConn(&vlessPacketConn{
|
packetaddr.NewConn(v.client.PacketConn(c, metadata.UDPAddr()),
|
||||||
Conn: c, rAddr: metadata.UDPAddr(),
|
M.SocksaddrFromNet(metadata.UDPAddr())),
|
||||||
}, M.SocksaddrFromNet(metadata.UDPAddr())),
|
|
||||||
), v), nil
|
), v), nil
|
||||||
}
|
}
|
||||||
return newPacketConn(N.NewThreadSafePacketConn(&vlessPacketConn{Conn: c, rAddr: metadata.UDPAddr()}), v), nil
|
return newPacketConn(N.NewThreadSafePacketConn(v.client.PacketConn(c, metadata.UDPAddr())), v), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SupportUOT implements C.ProxyAdapter
|
// SupportUOT implements C.ProxyAdapter
|
||||||
@@ -385,8 +318,8 @@ func (v *Vless) ProxyInfo() C.ProxyInfo {
|
|||||||
|
|
||||||
// Close implements C.ProxyAdapter
|
// Close implements C.ProxyAdapter
|
||||||
func (v *Vless) Close() error {
|
func (v *Vless) Close() error {
|
||||||
if v.transport != nil {
|
if v.gunTransport != nil {
|
||||||
return v.transport.Close()
|
return v.gunTransport.Close()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -419,101 +352,9 @@ func parseVlessAddr(metadata *C.Metadata, xudp bool) *vless.DstAddr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type vlessPacketConn struct {
|
|
||||||
net.Conn
|
|
||||||
rAddr net.Addr
|
|
||||||
remain int
|
|
||||||
mux sync.Mutex
|
|
||||||
cache [2]byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *vlessPacketConn) writePacket(payload []byte) (int, error) {
|
|
||||||
binary.BigEndian.PutUint16(c.cache[:], uint16(len(payload)))
|
|
||||||
|
|
||||||
if _, err := c.Conn.Write(c.cache[:]); err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.Conn.Write(payload)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *vlessPacketConn) WriteTo(b []byte, addr net.Addr) (int, error) {
|
|
||||||
total := len(b)
|
|
||||||
if total == 0 {
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if total <= maxLength {
|
|
||||||
return c.writePacket(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
offset := 0
|
|
||||||
|
|
||||||
for offset < total {
|
|
||||||
cursor := offset + maxLength
|
|
||||||
if cursor > total {
|
|
||||||
cursor = total
|
|
||||||
}
|
|
||||||
|
|
||||||
n, err := c.writePacket(b[offset:cursor])
|
|
||||||
if err != nil {
|
|
||||||
return offset + n, err
|
|
||||||
}
|
|
||||||
|
|
||||||
offset = cursor
|
|
||||||
if offset == total {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return total, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *vlessPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
|
||||||
c.mux.Lock()
|
|
||||||
defer c.mux.Unlock()
|
|
||||||
|
|
||||||
if c.remain > 0 {
|
|
||||||
length := len(b)
|
|
||||||
if c.remain < length {
|
|
||||||
length = c.remain
|
|
||||||
}
|
|
||||||
|
|
||||||
n, err := c.Conn.Read(b[:length])
|
|
||||||
if err != nil {
|
|
||||||
return 0, c.rAddr, err
|
|
||||||
}
|
|
||||||
|
|
||||||
c.remain -= n
|
|
||||||
return n, c.rAddr, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := c.Conn.Read(b[:2]); err != nil {
|
|
||||||
return 0, c.rAddr, err
|
|
||||||
}
|
|
||||||
|
|
||||||
total := int(binary.BigEndian.Uint16(b[:2]))
|
|
||||||
if total == 0 {
|
|
||||||
return 0, c.rAddr, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
length := len(b)
|
|
||||||
if length > total {
|
|
||||||
length = total
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := io.ReadFull(c.Conn, b[:length]); err != nil {
|
|
||||||
return 0, c.rAddr, errors.New("read packet error")
|
|
||||||
}
|
|
||||||
|
|
||||||
c.remain = total - length
|
|
||||||
|
|
||||||
return length, c.rAddr, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewVless(option VlessOption) (*Vless, error) {
|
func NewVless(option VlessOption) (*Vless, error) {
|
||||||
var addons *vless.Addons
|
var addons *vless.Addons
|
||||||
if option.Network != "ws" && len(option.Flow) >= 16 {
|
if len(option.Flow) >= 16 {
|
||||||
option.Flow = option.Flow[:16]
|
option.Flow = option.Flow[:16]
|
||||||
if option.Flow != vless.XRV {
|
if option.Flow != vless.XRV {
|
||||||
return nil, fmt.Errorf("unsupported xtls flow type: %s", option.Flow)
|
return nil, fmt.Errorf("unsupported xtls flow type: %s", option.Flow)
|
||||||
@@ -546,23 +387,35 @@ func NewVless(option VlessOption) (*Vless, error) {
|
|||||||
name: option.Name,
|
name: option.Name,
|
||||||
addr: net.JoinHostPort(option.Server, strconv.Itoa(option.Port)),
|
addr: net.JoinHostPort(option.Server, strconv.Itoa(option.Port)),
|
||||||
tp: C.Vless,
|
tp: C.Vless,
|
||||||
|
pdName: option.ProviderName,
|
||||||
udp: option.UDP,
|
udp: option.UDP,
|
||||||
xudp: option.XUDP,
|
xudp: option.XUDP,
|
||||||
tfo: option.TFO,
|
tfo: option.TFO,
|
||||||
mpTcp: option.MPTCP,
|
mpTcp: option.MPTCP,
|
||||||
iface: option.Interface,
|
iface: option.Interface,
|
||||||
rmark: option.RoutingMark,
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: option.IPVersion,
|
||||||
},
|
},
|
||||||
client: client,
|
client: client,
|
||||||
option: &option,
|
option: &option,
|
||||||
}
|
}
|
||||||
|
v.dialer = option.NewDialer(v.DialOptions())
|
||||||
|
|
||||||
|
v.encryption, err = encryption.NewClient(option.Encryption)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
v.realityConfig, err = v.option.RealityOpts.Parse()
|
v.realityConfig, err = v.option.RealityOpts.Parse()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
v.echConfig, err = v.option.ECHOpts.Parse()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
switch option.Network {
|
switch option.Network {
|
||||||
case "h2":
|
case "h2":
|
||||||
if len(option.HTTP2Opts.Host) == 0 {
|
if len(option.HTTP2Opts.Host) == 0 {
|
||||||
@@ -570,15 +423,7 @@ func NewVless(option VlessOption) (*Vless, error) {
|
|||||||
}
|
}
|
||||||
case "grpc":
|
case "grpc":
|
||||||
dialFn := func(ctx context.Context, network, addr string) (net.Conn, error) {
|
dialFn := func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
var err error
|
c, err := v.dialer.DialContext(ctx, "tcp", v.addr)
|
||||||
var cDialer C.Dialer = dialer.NewDialer(v.Base.DialOptions()...)
|
|
||||||
if len(v.option.DialerProxy) > 0 {
|
|
||||||
cDialer, err = proxydialer.NewByName(v.option.DialerProxy, cDialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
c, err := cDialer.DialContext(ctx, "tcp", v.addr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
||||||
}
|
}
|
||||||
@@ -586,32 +431,35 @@ func NewVless(option VlessOption) (*Vless, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
gunConfig := &gun.Config{
|
gunConfig := &gun.Config{
|
||||||
ServiceName: v.option.GrpcOpts.GrpcServiceName,
|
ServiceName: option.GrpcOpts.GrpcServiceName,
|
||||||
Host: v.option.ServerName,
|
UserAgent: option.GrpcOpts.GrpcUserAgent,
|
||||||
ClientFingerprint: v.option.ClientFingerprint,
|
Host: option.ServerName,
|
||||||
}
|
}
|
||||||
if option.ServerName == "" {
|
if option.ServerName == "" {
|
||||||
gunConfig.Host = v.addr
|
gunConfig.Host = v.addr
|
||||||
}
|
}
|
||||||
var tlsConfig *tls.Config
|
var tlsConfig *vmess.TLSConfig
|
||||||
if option.TLS {
|
if option.TLS {
|
||||||
tlsConfig, err = ca.GetSpecifiedFingerprintTLSConfig(&tls.Config{
|
tlsConfig = &vmess.TLSConfig{
|
||||||
InsecureSkipVerify: v.option.SkipCertVerify,
|
Host: option.ServerName,
|
||||||
ServerName: v.option.ServerName,
|
SkipCertVerify: option.SkipCertVerify,
|
||||||
}, v.option.Fingerprint)
|
FingerPrint: option.Fingerprint,
|
||||||
if err != nil {
|
Certificate: option.Certificate,
|
||||||
return nil, err
|
PrivateKey: option.PrivateKey,
|
||||||
|
ClientFingerprint: option.ClientFingerprint,
|
||||||
|
NextProtos: []string{"h2"},
|
||||||
|
ECH: v.echConfig,
|
||||||
|
Reality: v.realityConfig,
|
||||||
}
|
}
|
||||||
if option.ServerName == "" {
|
if option.ServerName == "" {
|
||||||
host, _, _ := net.SplitHostPort(v.addr)
|
host, _, _ := net.SplitHostPort(v.addr)
|
||||||
tlsConfig.ServerName = host
|
tlsConfig.Host = host
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
v.gunTLSConfig = tlsConfig
|
|
||||||
v.gunConfig = gunConfig
|
v.gunConfig = gunConfig
|
||||||
|
|
||||||
v.transport = gun.NewHTTP2Client(dialFn, tlsConfig, v.option.ClientFingerprint, v.realityConfig)
|
v.gunTransport = gun.NewHTTP2Client(dialFn, tlsConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
return v, nil
|
return v, nil
|
||||||
|
|||||||
@@ -2,11 +2,9 @@ package outbound
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -14,18 +12,18 @@ import (
|
|||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
"github.com/metacubex/mihomo/component/ca"
|
"github.com/metacubex/mihomo/component/ca"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"github.com/metacubex/mihomo/component/ech"
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
|
||||||
"github.com/metacubex/mihomo/component/resolver"
|
|
||||||
tlsC "github.com/metacubex/mihomo/component/tls"
|
tlsC "github.com/metacubex/mihomo/component/tls"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/ntp"
|
"github.com/metacubex/mihomo/ntp"
|
||||||
"github.com/metacubex/mihomo/transport/gun"
|
"github.com/metacubex/mihomo/transport/gun"
|
||||||
mihomoVMess "github.com/metacubex/mihomo/transport/vmess"
|
mihomoVMess "github.com/metacubex/mihomo/transport/vmess"
|
||||||
|
|
||||||
|
"github.com/metacubex/http"
|
||||||
vmess "github.com/metacubex/sing-vmess"
|
vmess "github.com/metacubex/sing-vmess"
|
||||||
"github.com/metacubex/sing-vmess/packetaddr"
|
"github.com/metacubex/sing-vmess/packetaddr"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/metacubex/sing/common/metadata"
|
||||||
|
"github.com/metacubex/tls"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrUDPRemoteAddrMismatch = errors.New("udp packet dropped due to mismatched remote address")
|
var ErrUDPRemoteAddrMismatch = errors.New("udp packet dropped due to mismatched remote address")
|
||||||
@@ -36,11 +34,11 @@ type Vmess struct {
|
|||||||
option *VmessOption
|
option *VmessOption
|
||||||
|
|
||||||
// for gun mux
|
// for gun mux
|
||||||
gunTLSConfig *tls.Config
|
|
||||||
gunConfig *gun.Config
|
gunConfig *gun.Config
|
||||||
transport *gun.TransportWrap
|
gunTransport *gun.TransportWrap
|
||||||
|
|
||||||
realityConfig *tlsC.RealityConfig
|
realityConfig *tlsC.RealityConfig
|
||||||
|
echConfig *ech.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
type VmessOption struct {
|
type VmessOption struct {
|
||||||
@@ -57,7 +55,10 @@ type VmessOption struct {
|
|||||||
ALPN []string `proxy:"alpn,omitempty"`
|
ALPN []string `proxy:"alpn,omitempty"`
|
||||||
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
||||||
Fingerprint string `proxy:"fingerprint,omitempty"`
|
Fingerprint string `proxy:"fingerprint,omitempty"`
|
||||||
|
Certificate string `proxy:"certificate,omitempty"`
|
||||||
|
PrivateKey string `proxy:"private-key,omitempty"`
|
||||||
ServerName string `proxy:"servername,omitempty"`
|
ServerName string `proxy:"servername,omitempty"`
|
||||||
|
ECHOpts ECHOptions `proxy:"ech-opts,omitempty"`
|
||||||
RealityOpts RealityOptions `proxy:"reality-opts,omitempty"`
|
RealityOpts RealityOptions `proxy:"reality-opts,omitempty"`
|
||||||
HTTPOpts HTTPOptions `proxy:"http-opts,omitempty"`
|
HTTPOpts HTTPOptions `proxy:"http-opts,omitempty"`
|
||||||
HTTP2Opts HTTP2Options `proxy:"h2-opts,omitempty"`
|
HTTP2Opts HTTP2Options `proxy:"h2-opts,omitempty"`
|
||||||
@@ -84,6 +85,7 @@ type HTTP2Options struct {
|
|||||||
|
|
||||||
type GrpcOptions struct {
|
type GrpcOptions struct {
|
||||||
GrpcServiceName string `proxy:"grpc-service-name,omitempty"`
|
GrpcServiceName string `proxy:"grpc-service-name,omitempty"`
|
||||||
|
GrpcUserAgent string `proxy:"grpc-user-agent,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type WSOptions struct {
|
type WSOptions struct {
|
||||||
@@ -95,7 +97,6 @@ type WSOptions struct {
|
|||||||
V2rayHttpUpgradeFastOpen bool `proxy:"v2ray-http-upgrade-fast-open,omitempty"`
|
V2rayHttpUpgradeFastOpen bool `proxy:"v2ray-http-upgrade-fast-open,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// StreamConnContext implements C.ProxyAdapter
|
|
||||||
func (v *Vmess) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ net.Conn, err error) {
|
func (v *Vmess) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ net.Conn, err error) {
|
||||||
switch v.option.Network {
|
switch v.option.Network {
|
||||||
case "ws":
|
case "ws":
|
||||||
@@ -109,6 +110,7 @@ func (v *Vmess) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
V2rayHttpUpgrade: v.option.WSOpts.V2rayHttpUpgrade,
|
V2rayHttpUpgrade: v.option.WSOpts.V2rayHttpUpgrade,
|
||||||
V2rayHttpUpgradeFastOpen: v.option.WSOpts.V2rayHttpUpgradeFastOpen,
|
V2rayHttpUpgradeFastOpen: v.option.WSOpts.V2rayHttpUpgradeFastOpen,
|
||||||
ClientFingerprint: v.option.ClientFingerprint,
|
ClientFingerprint: v.option.ClientFingerprint,
|
||||||
|
ECHConfig: v.echConfig,
|
||||||
Headers: http.Header{},
|
Headers: http.Header{},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,13 +122,16 @@ func (v *Vmess) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
|
|
||||||
if v.option.TLS {
|
if v.option.TLS {
|
||||||
wsOpts.TLS = true
|
wsOpts.TLS = true
|
||||||
tlsConfig := &tls.Config{
|
wsOpts.TLSConfig, err = ca.GetTLSConfig(ca.Option{
|
||||||
ServerName: host,
|
TLSConfig: &tls.Config{
|
||||||
InsecureSkipVerify: v.option.SkipCertVerify,
|
ServerName: host,
|
||||||
NextProtos: []string{"http/1.1"},
|
InsecureSkipVerify: v.option.SkipCertVerify,
|
||||||
}
|
NextProtos: []string{"http/1.1"},
|
||||||
|
},
|
||||||
wsOpts.TLSConfig, err = ca.GetSpecifiedFingerprintTLSConfig(tlsConfig, v.option.Fingerprint)
|
Fingerprint: v.option.Fingerprint,
|
||||||
|
Certificate: v.option.Certificate,
|
||||||
|
PrivateKey: v.option.PrivateKey,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -146,6 +151,7 @@ func (v *Vmess) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
Host: host,
|
Host: host,
|
||||||
SkipCertVerify: v.option.SkipCertVerify,
|
SkipCertVerify: v.option.SkipCertVerify,
|
||||||
ClientFingerprint: v.option.ClientFingerprint,
|
ClientFingerprint: v.option.ClientFingerprint,
|
||||||
|
ECH: v.echConfig,
|
||||||
Reality: v.realityConfig,
|
Reality: v.realityConfig,
|
||||||
NextProtos: v.option.ALPN,
|
NextProtos: v.option.ALPN,
|
||||||
}
|
}
|
||||||
@@ -174,6 +180,8 @@ func (v *Vmess) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
Host: host,
|
Host: host,
|
||||||
SkipCertVerify: v.option.SkipCertVerify,
|
SkipCertVerify: v.option.SkipCertVerify,
|
||||||
FingerPrint: v.option.Fingerprint,
|
FingerPrint: v.option.Fingerprint,
|
||||||
|
Certificate: v.option.Certificate,
|
||||||
|
PrivateKey: v.option.PrivateKey,
|
||||||
NextProtos: []string{"h2"},
|
NextProtos: []string{"h2"},
|
||||||
ClientFingerprint: v.option.ClientFingerprint,
|
ClientFingerprint: v.option.ClientFingerprint,
|
||||||
Reality: v.realityConfig,
|
Reality: v.realityConfig,
|
||||||
@@ -195,7 +203,7 @@ func (v *Vmess) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
|
|
||||||
c, err = mihomoVMess.StreamH2Conn(ctx, c, h2Opts)
|
c, err = mihomoVMess.StreamH2Conn(ctx, c, h2Opts)
|
||||||
case "grpc":
|
case "grpc":
|
||||||
c, err = gun.StreamGunWithConn(c, v.gunTLSConfig, v.gunConfig, v.realityConfig)
|
break // already handle in gun transport
|
||||||
default:
|
default:
|
||||||
// handle TLS
|
// handle TLS
|
||||||
if v.option.TLS {
|
if v.option.TLS {
|
||||||
@@ -204,7 +212,10 @@ func (v *Vmess) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
Host: host,
|
Host: host,
|
||||||
SkipCertVerify: v.option.SkipCertVerify,
|
SkipCertVerify: v.option.SkipCertVerify,
|
||||||
FingerPrint: v.option.Fingerprint,
|
FingerPrint: v.option.Fingerprint,
|
||||||
|
Certificate: v.option.Certificate,
|
||||||
|
PrivateKey: v.option.PrivateKey,
|
||||||
ClientFingerprint: v.option.ClientFingerprint,
|
ClientFingerprint: v.option.ClientFingerprint,
|
||||||
|
ECH: v.echConfig,
|
||||||
Reality: v.realityConfig,
|
Reality: v.realityConfig,
|
||||||
NextProtos: v.option.ALPN,
|
NextProtos: v.option.ALPN,
|
||||||
}
|
}
|
||||||
@@ -280,37 +291,14 @@ func (v *Vmess) streamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (v *Vmess) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
func (v *Vmess) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
var c net.Conn
|
var c net.Conn
|
||||||
// gun transport
|
// gun transport
|
||||||
if v.transport != nil && dialer.IsZeroOptions(opts) {
|
if v.gunTransport != nil {
|
||||||
c, err = gun.StreamGunWithTransport(v.transport, v.gunConfig)
|
c, err = gun.StreamGunWithTransport(v.gunTransport, v.gunConfig)
|
||||||
if err != nil {
|
} else {
|
||||||
return nil, err
|
c, err = v.dialer.DialContext(ctx, "tcp", v.addr)
|
||||||
}
|
|
||||||
defer func(c net.Conn) {
|
|
||||||
safeConnClose(c, err)
|
|
||||||
}(c)
|
|
||||||
|
|
||||||
c, err = v.streamConnContext(ctx, c, metadata)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return NewConn(c, v), nil
|
|
||||||
}
|
}
|
||||||
return v.DialContextWithDialer(ctx, dialer.NewDialer(v.Base.DialOptions(opts...)...), metadata)
|
|
||||||
}
|
|
||||||
|
|
||||||
// DialContextWithDialer implements C.ProxyAdapter
|
|
||||||
func (v *Vmess) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
|
||||||
if len(v.option.DialerProxy) > 0 {
|
|
||||||
dialer, err = proxydialer.NewByName(v.option.DialerProxy, dialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
c, err := dialer.DialContext(ctx, "tcp", v.addr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
||||||
}
|
}
|
||||||
@@ -323,54 +311,17 @@ func (v *Vmess) DialContextWithDialer(ctx context.Context, dialer C.Dialer, meta
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (v *Vmess) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
func (v *Vmess) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
// vmess use stream-oriented udp with a special address, so we need a net.UDPAddr
|
if err = v.ResolveUDP(ctx, metadata); err != nil {
|
||||||
if !metadata.Resolved() {
|
return nil, err
|
||||||
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("can't resolve ip")
|
|
||||||
}
|
|
||||||
metadata.DstIP = ip
|
|
||||||
}
|
}
|
||||||
var c net.Conn
|
var c net.Conn
|
||||||
// gun transport
|
// gun transport
|
||||||
if v.transport != nil && dialer.IsZeroOptions(opts) {
|
if v.gunTransport != nil {
|
||||||
c, err = gun.StreamGunWithTransport(v.transport, v.gunConfig)
|
c, err = gun.StreamGunWithTransport(v.gunTransport, v.gunConfig)
|
||||||
if err != nil {
|
} else {
|
||||||
return nil, err
|
c, err = v.dialer.DialContext(ctx, "tcp", v.addr)
|
||||||
}
|
|
||||||
defer func(c net.Conn) {
|
|
||||||
safeConnClose(c, err)
|
|
||||||
}(c)
|
|
||||||
|
|
||||||
c, err = v.streamConnContext(ctx, c, metadata)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("new vmess client error: %v", err)
|
|
||||||
}
|
|
||||||
return v.ListenPacketOnStreamConn(ctx, c, metadata)
|
|
||||||
}
|
}
|
||||||
return v.ListenPacketWithDialer(ctx, dialer.NewDialer(v.Base.DialOptions(opts...)...), metadata)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListenPacketWithDialer implements C.ProxyAdapter
|
|
||||||
func (v *Vmess) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
|
||||||
if len(v.option.DialerProxy) > 0 {
|
|
||||||
dialer, err = proxydialer.NewByName(v.option.DialerProxy, dialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// vmess use stream-oriented udp with a special address, so we need a net.UDPAddr
|
|
||||||
if !metadata.Resolved() {
|
|
||||||
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("can't resolve ip")
|
|
||||||
}
|
|
||||||
metadata.DstIP = ip
|
|
||||||
}
|
|
||||||
|
|
||||||
c, err := dialer.DialContext(ctx, "tcp", v.addr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
||||||
}
|
}
|
||||||
@@ -385,11 +336,6 @@ func (v *Vmess) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, met
|
|||||||
return v.ListenPacketOnStreamConn(ctx, c, metadata)
|
return v.ListenPacketOnStreamConn(ctx, c, metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SupportWithDialer implements C.ProxyAdapter
|
|
||||||
func (v *Vmess) SupportWithDialer() C.NetWork {
|
|
||||||
return C.ALLNet
|
|
||||||
}
|
|
||||||
|
|
||||||
// ProxyInfo implements C.ProxyAdapter
|
// ProxyInfo implements C.ProxyAdapter
|
||||||
func (v *Vmess) ProxyInfo() C.ProxyInfo {
|
func (v *Vmess) ProxyInfo() C.ProxyInfo {
|
||||||
info := v.Base.ProxyInfo()
|
info := v.Base.ProxyInfo()
|
||||||
@@ -399,21 +345,16 @@ func (v *Vmess) ProxyInfo() C.ProxyInfo {
|
|||||||
|
|
||||||
// Close implements C.ProxyAdapter
|
// Close implements C.ProxyAdapter
|
||||||
func (v *Vmess) Close() error {
|
func (v *Vmess) Close() error {
|
||||||
if v.transport != nil {
|
if v.gunTransport != nil {
|
||||||
return v.transport.Close()
|
return v.gunTransport.Close()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
||||||
func (v *Vmess) ListenPacketOnStreamConn(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
func (v *Vmess) ListenPacketOnStreamConn(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
// vmess use stream-oriented udp with a special address, so we need a net.UDPAddr
|
if err = v.ResolveUDP(ctx, metadata); err != nil {
|
||||||
if !metadata.Resolved() {
|
return nil, err
|
||||||
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("can't resolve ip")
|
|
||||||
}
|
|
||||||
metadata.DstIP = ip
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if pc, ok := c.(net.PacketConn); ok {
|
if pc, ok := c.(net.PacketConn); ok {
|
||||||
@@ -457,23 +398,30 @@ func NewVmess(option VmessOption) (*Vmess, error) {
|
|||||||
name: option.Name,
|
name: option.Name,
|
||||||
addr: net.JoinHostPort(option.Server, strconv.Itoa(option.Port)),
|
addr: net.JoinHostPort(option.Server, strconv.Itoa(option.Port)),
|
||||||
tp: C.Vmess,
|
tp: C.Vmess,
|
||||||
|
pdName: option.ProviderName,
|
||||||
udp: option.UDP,
|
udp: option.UDP,
|
||||||
xudp: option.XUDP,
|
xudp: option.XUDP,
|
||||||
tfo: option.TFO,
|
tfo: option.TFO,
|
||||||
mpTcp: option.MPTCP,
|
mpTcp: option.MPTCP,
|
||||||
iface: option.Interface,
|
iface: option.Interface,
|
||||||
rmark: option.RoutingMark,
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: option.IPVersion,
|
||||||
},
|
},
|
||||||
client: client,
|
client: client,
|
||||||
option: &option,
|
option: &option,
|
||||||
}
|
}
|
||||||
|
v.dialer = option.NewDialer(v.DialOptions())
|
||||||
|
|
||||||
v.realityConfig, err = v.option.RealityOpts.Parse()
|
v.realityConfig, err = v.option.RealityOpts.Parse()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
v.echConfig, err = v.option.ECHOpts.Parse()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
switch option.Network {
|
switch option.Network {
|
||||||
case "h2":
|
case "h2":
|
||||||
if len(option.HTTP2Opts.Host) == 0 {
|
if len(option.HTTP2Opts.Host) == 0 {
|
||||||
@@ -481,15 +429,7 @@ func NewVmess(option VmessOption) (*Vmess, error) {
|
|||||||
}
|
}
|
||||||
case "grpc":
|
case "grpc":
|
||||||
dialFn := func(ctx context.Context, network, addr string) (net.Conn, error) {
|
dialFn := func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
var err error
|
c, err := v.dialer.DialContext(ctx, "tcp", v.addr)
|
||||||
var cDialer C.Dialer = dialer.NewDialer(v.Base.DialOptions()...)
|
|
||||||
if len(v.option.DialerProxy) > 0 {
|
|
||||||
cDialer, err = proxydialer.NewByName(v.option.DialerProxy, cDialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
c, err := cDialer.DialContext(ctx, "tcp", v.addr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
||||||
}
|
}
|
||||||
@@ -497,32 +437,35 @@ func NewVmess(option VmessOption) (*Vmess, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
gunConfig := &gun.Config{
|
gunConfig := &gun.Config{
|
||||||
ServiceName: v.option.GrpcOpts.GrpcServiceName,
|
ServiceName: option.GrpcOpts.GrpcServiceName,
|
||||||
Host: v.option.ServerName,
|
UserAgent: option.GrpcOpts.GrpcUserAgent,
|
||||||
ClientFingerprint: v.option.ClientFingerprint,
|
Host: option.ServerName,
|
||||||
}
|
}
|
||||||
if option.ServerName == "" {
|
if option.ServerName == "" {
|
||||||
gunConfig.Host = v.addr
|
gunConfig.Host = v.addr
|
||||||
}
|
}
|
||||||
var tlsConfig *tls.Config
|
var tlsConfig *mihomoVMess.TLSConfig
|
||||||
if option.TLS {
|
if option.TLS {
|
||||||
tlsConfig, err = ca.GetSpecifiedFingerprintTLSConfig(&tls.Config{
|
tlsConfig = &mihomoVMess.TLSConfig{
|
||||||
InsecureSkipVerify: v.option.SkipCertVerify,
|
Host: option.ServerName,
|
||||||
ServerName: v.option.ServerName,
|
SkipCertVerify: option.SkipCertVerify,
|
||||||
}, v.option.Fingerprint)
|
FingerPrint: option.Fingerprint,
|
||||||
if err != nil {
|
Certificate: option.Certificate,
|
||||||
return nil, err
|
PrivateKey: option.PrivateKey,
|
||||||
|
ClientFingerprint: option.ClientFingerprint,
|
||||||
|
NextProtos: []string{"h2"},
|
||||||
|
ECH: v.echConfig,
|
||||||
|
Reality: v.realityConfig,
|
||||||
}
|
}
|
||||||
if option.ServerName == "" {
|
if option.ServerName == "" {
|
||||||
host, _, _ := net.SplitHostPort(v.addr)
|
host, _, _ := net.SplitHostPort(v.addr)
|
||||||
tlsConfig.ServerName = host
|
tlsConfig.Host = host
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
v.gunTLSConfig = tlsConfig
|
|
||||||
v.gunConfig = gunConfig
|
v.gunConfig = gunConfig
|
||||||
|
|
||||||
v.transport = gun.NewHTTP2Client(dialFn, tlsConfig, v.option.ClientFingerprint, v.realityConfig)
|
v.gunTransport = gun.NewHTTP2Client(dialFn, tlsConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
return v, nil
|
return v, nil
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
@@ -26,9 +25,9 @@ import (
|
|||||||
wireguard "github.com/metacubex/sing-wireguard"
|
wireguard "github.com/metacubex/sing-wireguard"
|
||||||
"github.com/metacubex/wireguard-go/device"
|
"github.com/metacubex/wireguard-go/device"
|
||||||
|
|
||||||
"github.com/sagernet/sing/common/debug"
|
"github.com/metacubex/sing/common/debug"
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
E "github.com/metacubex/sing/common/exceptions"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/metacubex/sing/common/metadata"
|
||||||
)
|
)
|
||||||
|
|
||||||
type wireguardGoDevice interface {
|
type wireguardGoDevice interface {
|
||||||
@@ -41,7 +40,6 @@ type WireGuard struct {
|
|||||||
bind *wireguard.ClientBind
|
bind *wireguard.ClientBind
|
||||||
device wireguardGoDevice
|
device wireguardGoDevice
|
||||||
tunDevice wireguard.Device
|
tunDevice wireguard.Device
|
||||||
dialer proxydialer.SingDialer
|
|
||||||
resolver resolver.Resolver
|
resolver resolver.Resolver
|
||||||
|
|
||||||
initOk atomic.Bool
|
initOk atomic.Bool
|
||||||
@@ -79,8 +77,8 @@ type WireGuardOption struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type WireGuardPeerOption struct {
|
type WireGuardPeerOption struct {
|
||||||
Server string `proxy:"server"`
|
Server string `proxy:"server,omitempty"`
|
||||||
Port int `proxy:"port"`
|
Port int `proxy:"port,omitempty"`
|
||||||
PublicKey string `proxy:"public-key,omitempty"`
|
PublicKey string `proxy:"public-key,omitempty"`
|
||||||
PreSharedKey string `proxy:"pre-shared-key,omitempty"`
|
PreSharedKey string `proxy:"pre-shared-key,omitempty"`
|
||||||
Reserved []uint8 `proxy:"reserved,omitempty"`
|
Reserved []uint8 `proxy:"reserved,omitempty"`
|
||||||
@@ -88,15 +86,26 @@ type WireGuardPeerOption struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type AmneziaWGOption struct {
|
type AmneziaWGOption struct {
|
||||||
JC int `proxy:"jc"`
|
JC int `proxy:"jc,omitempty"`
|
||||||
JMin int `proxy:"jmin"`
|
JMin int `proxy:"jmin,omitempty"`
|
||||||
JMax int `proxy:"jmax"`
|
JMax int `proxy:"jmax,omitempty"`
|
||||||
S1 int `proxy:"s1"`
|
S1 int `proxy:"s1,omitempty"`
|
||||||
S2 int `proxy:"s2"`
|
S2 int `proxy:"s2,omitempty"`
|
||||||
H1 uint32 `proxy:"h1"`
|
S3 int `proxy:"s3,omitempty"` // AmneziaWG v1.5 and v2
|
||||||
H2 uint32 `proxy:"h2"`
|
S4 int `proxy:"s4,omitempty"` // AmneziaWG v1.5 and v2
|
||||||
H3 uint32 `proxy:"h3"`
|
H1 string `proxy:"h1,omitempty"` // In AmneziaWG v1.x, it was uint32, but our WeaklyTypedInput can handle this situation
|
||||||
H4 uint32 `proxy:"h4"`
|
H2 string `proxy:"h2,omitempty"` // In AmneziaWG v1.x, it was uint32, but our WeaklyTypedInput can handle this situation
|
||||||
|
H3 string `proxy:"h3,omitempty"` // In AmneziaWG v1.x, it was uint32, but our WeaklyTypedInput can handle this situation
|
||||||
|
H4 string `proxy:"h4,omitempty"` // In AmneziaWG v1.x, it was uint32, but our WeaklyTypedInput can handle this situation
|
||||||
|
I1 string `proxy:"i1,omitempty"` // AmneziaWG v1.5 and v2
|
||||||
|
I2 string `proxy:"i2,omitempty"` // AmneziaWG v1.5 and v2
|
||||||
|
I3 string `proxy:"i3,omitempty"` // AmneziaWG v1.5 and v2
|
||||||
|
I4 string `proxy:"i4,omitempty"` // AmneziaWG v1.5 and v2
|
||||||
|
I5 string `proxy:"i5,omitempty"` // AmneziaWG v1.5 and v2
|
||||||
|
J1 string `proxy:"j1,omitempty"` // AmneziaWG v1.5 only (removed in v2)
|
||||||
|
J2 string `proxy:"j2,omitempty"` // AmneziaWG v1.5 only (removed in v2)
|
||||||
|
J3 string `proxy:"j3,omitempty"` // AmneziaWG v1.5 only (removed in v2)
|
||||||
|
Itime int64 `proxy:"itime,omitempty"` // AmneziaWG v1.5 only (removed in v2)
|
||||||
}
|
}
|
||||||
|
|
||||||
type wgSingErrorHandler struct {
|
type wgSingErrorHandler struct {
|
||||||
@@ -161,13 +170,15 @@ func NewWireGuard(option WireGuardOption) (*WireGuard, error) {
|
|||||||
name: option.Name,
|
name: option.Name,
|
||||||
addr: net.JoinHostPort(option.Server, strconv.Itoa(option.Port)),
|
addr: net.JoinHostPort(option.Server, strconv.Itoa(option.Port)),
|
||||||
tp: C.WireGuard,
|
tp: C.WireGuard,
|
||||||
|
pdName: option.ProviderName,
|
||||||
udp: option.UDP,
|
udp: option.UDP,
|
||||||
iface: option.Interface,
|
iface: option.Interface,
|
||||||
rmark: option.RoutingMark,
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: option.IPVersion,
|
||||||
},
|
},
|
||||||
dialer: proxydialer.NewSlowDownSingDialer(proxydialer.NewByNameSingDialer(option.DialerProxy, dialer.NewDialer()), slowdown.New()),
|
|
||||||
}
|
}
|
||||||
|
outbound.dialer = option.NewDialer(outbound.DialOptions())
|
||||||
|
singDialer := proxydialer.NewSingDialer(proxydialer.NewSlowDownDialer(outbound.dialer, slowdown.New()))
|
||||||
|
|
||||||
var reserved [3]uint8
|
var reserved [3]uint8
|
||||||
if len(option.Reserved) > 0 {
|
if len(option.Reserved) > 0 {
|
||||||
@@ -185,7 +196,7 @@ func NewWireGuard(option WireGuardOption) (*WireGuard, error) {
|
|||||||
outbound.connectAddr = option.Addr()
|
outbound.connectAddr = option.Addr()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
outbound.bind = wireguard.NewClientBind(context.Background(), wgSingErrorHandler{outbound.Name()}, outbound.dialer, isConnect, outbound.connectAddr.AddrPort(), reserved)
|
outbound.bind = wireguard.NewClientBind(context.Background(), wgSingErrorHandler{outbound.Name()}, singDialer, isConnect, outbound.connectAddr.AddrPort(), reserved)
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
outbound.localPrefixes, err = option.Prefixes()
|
outbound.localPrefixes, err = option.Prefixes()
|
||||||
@@ -386,15 +397,66 @@ func (w *WireGuard) genIpcConf(ctx context.Context, updateOnly bool) (string, er
|
|||||||
if !updateOnly {
|
if !updateOnly {
|
||||||
ipcConf += "private_key=" + w.option.PrivateKey + "\n"
|
ipcConf += "private_key=" + w.option.PrivateKey + "\n"
|
||||||
if w.option.AmneziaWGOption != nil {
|
if w.option.AmneziaWGOption != nil {
|
||||||
ipcConf += "jc=" + strconv.Itoa(w.option.AmneziaWGOption.JC) + "\n"
|
if w.option.AmneziaWGOption.JC != 0 {
|
||||||
ipcConf += "jmin=" + strconv.Itoa(w.option.AmneziaWGOption.JMin) + "\n"
|
ipcConf += "jc=" + strconv.Itoa(w.option.AmneziaWGOption.JC) + "\n"
|
||||||
ipcConf += "jmax=" + strconv.Itoa(w.option.AmneziaWGOption.JMax) + "\n"
|
}
|
||||||
ipcConf += "s1=" + strconv.Itoa(w.option.AmneziaWGOption.S1) + "\n"
|
if w.option.AmneziaWGOption.JMin != 0 {
|
||||||
ipcConf += "s2=" + strconv.Itoa(w.option.AmneziaWGOption.S2) + "\n"
|
ipcConf += "jmin=" + strconv.Itoa(w.option.AmneziaWGOption.JMin) + "\n"
|
||||||
ipcConf += "h1=" + strconv.FormatUint(uint64(w.option.AmneziaWGOption.H1), 10) + "\n"
|
}
|
||||||
ipcConf += "h2=" + strconv.FormatUint(uint64(w.option.AmneziaWGOption.H2), 10) + "\n"
|
if w.option.AmneziaWGOption.JMax != 0 {
|
||||||
ipcConf += "h3=" + strconv.FormatUint(uint64(w.option.AmneziaWGOption.H3), 10) + "\n"
|
ipcConf += "jmax=" + strconv.Itoa(w.option.AmneziaWGOption.JMax) + "\n"
|
||||||
ipcConf += "h4=" + strconv.FormatUint(uint64(w.option.AmneziaWGOption.H4), 10) + "\n"
|
}
|
||||||
|
if w.option.AmneziaWGOption.S1 != 0 {
|
||||||
|
ipcConf += "s1=" + strconv.Itoa(w.option.AmneziaWGOption.S1) + "\n"
|
||||||
|
}
|
||||||
|
if w.option.AmneziaWGOption.S2 != 0 {
|
||||||
|
ipcConf += "s2=" + strconv.Itoa(w.option.AmneziaWGOption.S2) + "\n"
|
||||||
|
}
|
||||||
|
if w.option.AmneziaWGOption.S3 != 0 {
|
||||||
|
ipcConf += "s3=" + strconv.Itoa(w.option.AmneziaWGOption.S3) + "\n"
|
||||||
|
}
|
||||||
|
if w.option.AmneziaWGOption.S4 != 0 {
|
||||||
|
ipcConf += "s4=" + strconv.Itoa(w.option.AmneziaWGOption.S4) + "\n"
|
||||||
|
}
|
||||||
|
if w.option.AmneziaWGOption.H1 != "" {
|
||||||
|
ipcConf += "h1=" + w.option.AmneziaWGOption.H1 + "\n"
|
||||||
|
}
|
||||||
|
if w.option.AmneziaWGOption.H2 != "" {
|
||||||
|
ipcConf += "h2=" + w.option.AmneziaWGOption.H2 + "\n"
|
||||||
|
}
|
||||||
|
if w.option.AmneziaWGOption.H3 != "" {
|
||||||
|
ipcConf += "h3=" + w.option.AmneziaWGOption.H3 + "\n"
|
||||||
|
}
|
||||||
|
if w.option.AmneziaWGOption.H4 != "" {
|
||||||
|
ipcConf += "h4=" + w.option.AmneziaWGOption.H4 + "\n"
|
||||||
|
}
|
||||||
|
if w.option.AmneziaWGOption.I1 != "" {
|
||||||
|
ipcConf += "i1=" + w.option.AmneziaWGOption.I1 + "\n"
|
||||||
|
}
|
||||||
|
if w.option.AmneziaWGOption.I2 != "" {
|
||||||
|
ipcConf += "i2=" + w.option.AmneziaWGOption.I2 + "\n"
|
||||||
|
}
|
||||||
|
if w.option.AmneziaWGOption.I3 != "" {
|
||||||
|
ipcConf += "i3=" + w.option.AmneziaWGOption.I3 + "\n"
|
||||||
|
}
|
||||||
|
if w.option.AmneziaWGOption.I4 != "" {
|
||||||
|
ipcConf += "i4=" + w.option.AmneziaWGOption.I4 + "\n"
|
||||||
|
}
|
||||||
|
if w.option.AmneziaWGOption.I5 != "" {
|
||||||
|
ipcConf += "i5=" + w.option.AmneziaWGOption.I5 + "\n"
|
||||||
|
}
|
||||||
|
if w.option.AmneziaWGOption.J1 != "" {
|
||||||
|
ipcConf += "j1=" + w.option.AmneziaWGOption.J1 + "\n"
|
||||||
|
}
|
||||||
|
if w.option.AmneziaWGOption.J2 != "" {
|
||||||
|
ipcConf += "j2=" + w.option.AmneziaWGOption.J2 + "\n"
|
||||||
|
}
|
||||||
|
if w.option.AmneziaWGOption.J3 != "" {
|
||||||
|
ipcConf += "j3=" + w.option.AmneziaWGOption.J3 + "\n"
|
||||||
|
}
|
||||||
|
if w.option.AmneziaWGOption.Itime != 0 {
|
||||||
|
ipcConf += "itime=" + strconv.FormatInt(int64(w.option.AmneziaWGOption.Itime), 10) + "\n"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(w.option.Peers) > 0 {
|
if len(w.option.Peers) > 0 {
|
||||||
@@ -488,9 +550,7 @@ func (w *WireGuard) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WireGuard) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
func (w *WireGuard) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
options := w.Base.DialOptions(opts...)
|
|
||||||
w.dialer.SetDialer(dialer.NewDialer(options...))
|
|
||||||
var conn net.Conn
|
var conn net.Conn
|
||||||
if err = w.init(ctx); err != nil {
|
if err = w.init(ctx); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -500,6 +560,7 @@ func (w *WireGuard) DialContext(ctx context.Context, metadata *C.Metadata, opts
|
|||||||
if w.resolver != nil {
|
if w.resolver != nil {
|
||||||
r = w.resolver
|
r = w.resolver
|
||||||
}
|
}
|
||||||
|
options := w.DialOptions()
|
||||||
options = append(options, dialer.WithResolver(r))
|
options = append(options, dialer.WithResolver(r))
|
||||||
options = append(options, dialer.WithNetDialer(wgNetDialer{tunDevice: w.tunDevice}))
|
options = append(options, dialer.WithNetDialer(wgNetDialer{tunDevice: w.tunDevice}))
|
||||||
conn, err = dialer.NewDialer(options...).DialContext(ctx, "tcp", metadata.RemoteAddress())
|
conn, err = dialer.NewDialer(options...).DialContext(ctx, "tcp", metadata.RemoteAddress())
|
||||||
@@ -515,23 +576,13 @@ func (w *WireGuard) DialContext(ctx context.Context, metadata *C.Metadata, opts
|
|||||||
return NewConn(conn, w), nil
|
return NewConn(conn, w), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WireGuard) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
func (w *WireGuard) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
options := w.Base.DialOptions(opts...)
|
|
||||||
w.dialer.SetDialer(dialer.NewDialer(options...))
|
|
||||||
var pc net.PacketConn
|
var pc net.PacketConn
|
||||||
if err = w.init(ctx); err != nil {
|
if err = w.init(ctx); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if (!metadata.Resolved() || w.resolver != nil) && metadata.Host != "" {
|
if err = w.ResolveUDP(ctx, metadata); err != nil {
|
||||||
r := resolver.DefaultResolver
|
return nil, err
|
||||||
if w.resolver != nil {
|
|
||||||
r = w.resolver
|
|
||||||
}
|
|
||||||
ip, err := resolver.ResolveIPWithResolver(ctx, metadata.Host, r)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("can't resolve ip")
|
|
||||||
}
|
|
||||||
metadata.DstIP = ip
|
|
||||||
}
|
}
|
||||||
pc, err = w.tunDevice.ListenPacket(ctx, M.SocksaddrFrom(metadata.DstIP, metadata.DstPort).Unwrap())
|
pc, err = w.tunDevice.ListenPacket(ctx, M.SocksaddrFrom(metadata.DstIP, metadata.DstPort).Unwrap())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -543,6 +594,28 @@ func (w *WireGuard) ListenPacketContext(ctx context.Context, metadata *C.Metadat
|
|||||||
return newPacketConn(pc, w), nil
|
return newPacketConn(pc, w), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *WireGuard) ResolveUDP(ctx context.Context, metadata *C.Metadata) error {
|
||||||
|
if (!metadata.Resolved() || w.resolver != nil) && metadata.Host != "" {
|
||||||
|
r := resolver.DefaultResolver
|
||||||
|
if w.resolver != nil {
|
||||||
|
r = w.resolver
|
||||||
|
}
|
||||||
|
ip, err := resolver.ResolveIPWithResolver(ctx, metadata.Host, r)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("can't resolve ip: %w", err)
|
||||||
|
}
|
||||||
|
metadata.DstIP = ip
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ProxyInfo implements C.ProxyAdapter
|
||||||
|
func (w *WireGuard) ProxyInfo() C.ProxyInfo {
|
||||||
|
info := w.Base.ProxyInfo()
|
||||||
|
info.DialerProxy = w.option.DialerProxy
|
||||||
|
return info
|
||||||
|
}
|
||||||
|
|
||||||
// IsL3Protocol implements C.ProxyAdapter
|
// IsL3Protocol implements C.ProxyAdapter
|
||||||
func (w *WireGuard) IsL3Protocol(metadata *C.Metadata) bool {
|
func (w *WireGuard) IsL3Protocol(metadata *C.Metadata) bool {
|
||||||
return true
|
return true
|
||||||
|
|||||||
@@ -6,13 +6,11 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/adapter/outbound"
|
|
||||||
"github.com/metacubex/mihomo/common/callback"
|
"github.com/metacubex/mihomo/common/callback"
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/constant/provider"
|
P "github.com/metacubex/mihomo/constant/provider"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Fallback struct {
|
type Fallback struct {
|
||||||
@@ -31,9 +29,9 @@ func (f *Fallback) Now() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (f *Fallback) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
func (f *Fallback) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
||||||
proxy := f.findAliveProxy(true)
|
proxy := f.findAliveProxy(true)
|
||||||
c, err := proxy.DialContext(ctx, metadata, f.Base.DialOptions(opts...)...)
|
c, err := proxy.DialContext(ctx, metadata)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
c.AppendToChains(f)
|
c.AppendToChains(f)
|
||||||
} else {
|
} else {
|
||||||
@@ -54,9 +52,9 @@ func (f *Fallback) DialContext(ctx context.Context, metadata *C.Metadata, opts .
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (f *Fallback) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
func (f *Fallback) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
||||||
proxy := f.findAliveProxy(true)
|
proxy := f.findAliveProxy(true)
|
||||||
pc, err := proxy.ListenPacketContext(ctx, metadata, f.Base.DialOptions(opts...)...)
|
pc, err := proxy.ListenPacketContext(ctx, metadata)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
pc.AppendToChains(f)
|
pc.AppendToChains(f)
|
||||||
}
|
}
|
||||||
@@ -152,21 +150,25 @@ func (f *Fallback) ForceSet(name string) {
|
|||||||
f.selected = name
|
f.selected = name
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFallback(option *GroupCommonOption, providers []provider.ProxyProvider) *Fallback {
|
func (f *Fallback) Providers() []P.ProxyProvider {
|
||||||
|
return f.providers
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Fallback) Proxies() []C.Proxy {
|
||||||
|
return f.GetProxies(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewFallback(option *GroupCommonOption, providers []P.ProxyProvider) *Fallback {
|
||||||
return &Fallback{
|
return &Fallback{
|
||||||
GroupBase: NewGroupBase(GroupBaseOption{
|
GroupBase: NewGroupBase(GroupBaseOption{
|
||||||
outbound.BaseOption{
|
Name: option.Name,
|
||||||
Name: option.Name,
|
Type: C.Fallback,
|
||||||
Type: C.Fallback,
|
Filter: option.Filter,
|
||||||
Interface: option.Interface,
|
ExcludeFilter: option.ExcludeFilter,
|
||||||
RoutingMark: option.RoutingMark,
|
ExcludeType: option.ExcludeType,
|
||||||
},
|
TestTimeout: option.TestTimeout,
|
||||||
option.Filter,
|
MaxFailedTimes: option.MaxFailedTimes,
|
||||||
option.ExcludeFilter,
|
Providers: providers,
|
||||||
option.ExcludeType,
|
|
||||||
option.TestTimeout,
|
|
||||||
option.MaxFailedTimes,
|
|
||||||
providers,
|
|
||||||
}),
|
}),
|
||||||
disableUDP: option.DisableUDP,
|
disableUDP: option.DisableUDP,
|
||||||
testUrl: option.URL,
|
testUrl: option.URL,
|
||||||
|
|||||||
@@ -12,8 +12,7 @@ import (
|
|||||||
"github.com/metacubex/mihomo/common/atomic"
|
"github.com/metacubex/mihomo/common/atomic"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/constant/provider"
|
P "github.com/metacubex/mihomo/constant/provider"
|
||||||
types "github.com/metacubex/mihomo/constant/provider"
|
|
||||||
"github.com/metacubex/mihomo/log"
|
"github.com/metacubex/mihomo/log"
|
||||||
"github.com/metacubex/mihomo/tunnel"
|
"github.com/metacubex/mihomo/tunnel"
|
||||||
|
|
||||||
@@ -26,7 +25,7 @@ type GroupBase struct {
|
|||||||
filterRegs []*regexp2.Regexp
|
filterRegs []*regexp2.Regexp
|
||||||
excludeFilterRegs []*regexp2.Regexp
|
excludeFilterRegs []*regexp2.Regexp
|
||||||
excludeTypeArray []string
|
excludeTypeArray []string
|
||||||
providers []provider.ProxyProvider
|
providers []P.ProxyProvider
|
||||||
failedTestMux sync.Mutex
|
failedTestMux sync.Mutex
|
||||||
failedTimes int
|
failedTimes int
|
||||||
failedTime time.Time
|
failedTime time.Time
|
||||||
@@ -41,53 +40,47 @@ type GroupBase struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type GroupBaseOption struct {
|
type GroupBaseOption struct {
|
||||||
outbound.BaseOption
|
Name string
|
||||||
filter string
|
Type C.AdapterType
|
||||||
excludeFilter string
|
Filter string
|
||||||
excludeType string
|
ExcludeFilter string
|
||||||
|
ExcludeType string
|
||||||
TestTimeout int
|
TestTimeout int
|
||||||
maxFailedTimes int
|
MaxFailedTimes int
|
||||||
providers []provider.ProxyProvider
|
Providers []P.ProxyProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGroupBase(opt GroupBaseOption) *GroupBase {
|
func NewGroupBase(opt GroupBaseOption) *GroupBase {
|
||||||
if opt.RoutingMark != 0 {
|
|
||||||
log.Warnln("The group [%s] with routing-mark configuration is deprecated, please set it directly on the proxy instead", opt.Name)
|
|
||||||
}
|
|
||||||
if opt.Interface != "" {
|
|
||||||
log.Warnln("The group [%s] with interface-name configuration is deprecated, please set it directly on the proxy instead", opt.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
var excludeTypeArray []string
|
var excludeTypeArray []string
|
||||||
if opt.excludeType != "" {
|
if opt.ExcludeType != "" {
|
||||||
excludeTypeArray = strings.Split(opt.excludeType, "|")
|
excludeTypeArray = strings.Split(opt.ExcludeType, "|")
|
||||||
}
|
}
|
||||||
|
|
||||||
var excludeFilterRegs []*regexp2.Regexp
|
var excludeFilterRegs []*regexp2.Regexp
|
||||||
if opt.excludeFilter != "" {
|
if opt.ExcludeFilter != "" {
|
||||||
for _, excludeFilter := range strings.Split(opt.excludeFilter, "`") {
|
for _, excludeFilter := range strings.Split(opt.ExcludeFilter, "`") {
|
||||||
excludeFilterReg := regexp2.MustCompile(excludeFilter, regexp2.None)
|
excludeFilterReg := regexp2.MustCompile(excludeFilter, regexp2.None)
|
||||||
excludeFilterRegs = append(excludeFilterRegs, excludeFilterReg)
|
excludeFilterRegs = append(excludeFilterRegs, excludeFilterReg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var filterRegs []*regexp2.Regexp
|
var filterRegs []*regexp2.Regexp
|
||||||
if opt.filter != "" {
|
if opt.Filter != "" {
|
||||||
for _, filter := range strings.Split(opt.filter, "`") {
|
for _, filter := range strings.Split(opt.Filter, "`") {
|
||||||
filterReg := regexp2.MustCompile(filter, regexp2.None)
|
filterReg := regexp2.MustCompile(filter, regexp2.None)
|
||||||
filterRegs = append(filterRegs, filterReg)
|
filterRegs = append(filterRegs, filterReg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
gb := &GroupBase{
|
gb := &GroupBase{
|
||||||
Base: outbound.NewBase(opt.BaseOption),
|
Base: outbound.NewBase(outbound.BaseOption{Name: opt.Name, Type: opt.Type}),
|
||||||
filterRegs: filterRegs,
|
filterRegs: filterRegs,
|
||||||
excludeFilterRegs: excludeFilterRegs,
|
excludeFilterRegs: excludeFilterRegs,
|
||||||
excludeTypeArray: excludeTypeArray,
|
excludeTypeArray: excludeTypeArray,
|
||||||
providers: opt.providers,
|
providers: opt.Providers,
|
||||||
failedTesting: atomic.NewBool(false),
|
failedTesting: atomic.NewBool(false),
|
||||||
TestTimeout: opt.TestTimeout,
|
TestTimeout: opt.TestTimeout,
|
||||||
maxFailedTimes: opt.maxFailedTimes,
|
maxFailedTimes: opt.MaxFailedTimes,
|
||||||
}
|
}
|
||||||
|
|
||||||
if gb.TestTimeout == 0 {
|
if gb.TestTimeout == 0 {
|
||||||
@@ -131,7 +124,7 @@ func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for _, pd := range gb.providers {
|
for _, pd := range gb.providers {
|
||||||
if pd.VehicleType() == types.Compatible { // compatible provider unneeded filter
|
if pd.VehicleType() == P.Compatible { // compatible provider unneeded filter
|
||||||
proxies = append(proxies, pd.Proxies()...)
|
proxies = append(proxies, pd.Proxies()...)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -279,7 +272,7 @@ func (gb *GroupBase) onDialFailed(adapterType C.AdapterType, err error, fn func(
|
|||||||
|
|
||||||
log.Debugln("ProxyGroup: %s failed count: %d", gb.Name(), gb.failedTimes)
|
log.Debugln("ProxyGroup: %s failed count: %d", gb.Name(), gb.failedTimes)
|
||||||
if gb.failedTimes >= gb.maxFailedTimes {
|
if gb.failedTimes >= gb.maxFailedTimes {
|
||||||
log.Warnln("because %s failed multiple times, active health check", gb.Name())
|
log.Warnln("because %s failed multiple times, activate health check", gb.Name())
|
||||||
fn()
|
fn()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,14 +9,12 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/adapter/outbound"
|
|
||||||
"github.com/metacubex/mihomo/common/callback"
|
"github.com/metacubex/mihomo/common/callback"
|
||||||
"github.com/metacubex/mihomo/common/lru"
|
"github.com/metacubex/mihomo/common/lru"
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/constant/provider"
|
P "github.com/metacubex/mihomo/constant/provider"
|
||||||
|
|
||||||
"golang.org/x/net/publicsuffix"
|
"golang.org/x/net/publicsuffix"
|
||||||
)
|
)
|
||||||
@@ -88,9 +86,9 @@ func jumpHash(key uint64, buckets int32) int32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (lb *LoadBalance) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (c C.Conn, err error) {
|
func (lb *LoadBalance) DialContext(ctx context.Context, metadata *C.Metadata) (c C.Conn, err error) {
|
||||||
proxy := lb.Unwrap(metadata, true)
|
proxy := lb.Unwrap(metadata, true)
|
||||||
c, err = proxy.DialContext(ctx, metadata, lb.Base.DialOptions(opts...)...)
|
c, err = proxy.DialContext(ctx, metadata)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
c.AppendToChains(lb)
|
c.AppendToChains(lb)
|
||||||
@@ -112,7 +110,7 @@ func (lb *LoadBalance) DialContext(ctx context.Context, metadata *C.Metadata, op
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (lb *LoadBalance) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (pc C.PacketConn, err error) {
|
func (lb *LoadBalance) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (pc C.PacketConn, err error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
pc.AppendToChains(lb)
|
pc.AppendToChains(lb)
|
||||||
@@ -120,7 +118,7 @@ func (lb *LoadBalance) ListenPacketContext(ctx context.Context, metadata *C.Meta
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
proxy := lb.Unwrap(metadata, true)
|
proxy := lb.Unwrap(metadata, true)
|
||||||
return proxy.ListenPacketContext(ctx, metadata, lb.Base.DialOptions(opts...)...)
|
return proxy.ListenPacketContext(ctx, metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SupportUDP implements C.ProxyAdapter
|
// SupportUDP implements C.ProxyAdapter
|
||||||
@@ -196,7 +194,7 @@ func strategyStickySessions(url string) strategyFn {
|
|||||||
key := utils.MapHash(getKeyWithSrcAndDst(metadata))
|
key := utils.MapHash(getKeyWithSrcAndDst(metadata))
|
||||||
length := len(proxies)
|
length := len(proxies)
|
||||||
idx, has := lruCache.Get(key)
|
idx, has := lruCache.Get(key)
|
||||||
if !has {
|
if !has || idx >= length {
|
||||||
idx = int(jumpHash(key+uint64(time.Now().UnixNano()), int32(length)))
|
idx = int(jumpHash(key+uint64(time.Now().UnixNano()), int32(length)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,7 +239,19 @@ func (lb *LoadBalance) MarshalJSON() ([]byte, error) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLoadBalance(option *GroupCommonOption, providers []provider.ProxyProvider, strategy string) (lb *LoadBalance, err error) {
|
func (lb *LoadBalance) Providers() []P.ProxyProvider {
|
||||||
|
return lb.providers
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lb *LoadBalance) Proxies() []C.Proxy {
|
||||||
|
return lb.GetProxies(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lb *LoadBalance) Now() string {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewLoadBalance(option *GroupCommonOption, providers []P.ProxyProvider, strategy string) (lb *LoadBalance, err error) {
|
||||||
var strategyFn strategyFn
|
var strategyFn strategyFn
|
||||||
switch strategy {
|
switch strategy {
|
||||||
case "consistent-hashing":
|
case "consistent-hashing":
|
||||||
@@ -255,18 +265,14 @@ func NewLoadBalance(option *GroupCommonOption, providers []provider.ProxyProvide
|
|||||||
}
|
}
|
||||||
return &LoadBalance{
|
return &LoadBalance{
|
||||||
GroupBase: NewGroupBase(GroupBaseOption{
|
GroupBase: NewGroupBase(GroupBaseOption{
|
||||||
outbound.BaseOption{
|
Name: option.Name,
|
||||||
Name: option.Name,
|
Type: C.LoadBalance,
|
||||||
Type: C.LoadBalance,
|
Filter: option.Filter,
|
||||||
Interface: option.Interface,
|
ExcludeFilter: option.ExcludeFilter,
|
||||||
RoutingMark: option.RoutingMark,
|
ExcludeType: option.ExcludeType,
|
||||||
},
|
TestTimeout: option.TestTimeout,
|
||||||
option.Filter,
|
MaxFailedTimes: option.MaxFailedTimes,
|
||||||
option.ExcludeFilter,
|
Providers: providers,
|
||||||
option.ExcludeType,
|
|
||||||
option.TestTimeout,
|
|
||||||
option.MaxFailedTimes,
|
|
||||||
providers,
|
|
||||||
}),
|
}),
|
||||||
strategyFn: strategyFn,
|
strategyFn: strategyFn,
|
||||||
disableUDP: option.DisableUDP,
|
disableUDP: option.DisableUDP,
|
||||||
|
|||||||
@@ -7,12 +7,12 @@ import (
|
|||||||
|
|
||||||
"github.com/dlclark/regexp2"
|
"github.com/dlclark/regexp2"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/adapter/outbound"
|
|
||||||
"github.com/metacubex/mihomo/adapter/provider"
|
"github.com/metacubex/mihomo/adapter/provider"
|
||||||
"github.com/metacubex/mihomo/common/structure"
|
"github.com/metacubex/mihomo/common/structure"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
types "github.com/metacubex/mihomo/constant/provider"
|
P "github.com/metacubex/mihomo/constant/provider"
|
||||||
|
"github.com/metacubex/mihomo/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -23,7 +23,6 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type GroupCommonOption struct {
|
type GroupCommonOption struct {
|
||||||
outbound.BasicOption
|
|
||||||
Name string `group:"name"`
|
Name string `group:"name"`
|
||||||
Type string `group:"type"`
|
Type string `group:"type"`
|
||||||
Proxies []string `group:"proxies,omitempty"`
|
Proxies []string `group:"proxies,omitempty"`
|
||||||
@@ -45,7 +44,7 @@ type GroupCommonOption struct {
|
|||||||
Icon string `group:"icon,omitempty"`
|
Icon string `group:"icon,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, providersMap map[string]types.ProxyProvider, AllProxies []string, AllProviders []string) (C.ProxyAdapter, error) {
|
func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, providersMap map[string]P.ProxyProvider, AllProxies []string, AllProviders []string) (C.ProxyAdapter, error) {
|
||||||
decoder := structure.NewDecoder(structure.Option{TagName: "group", WeaklyTypedInput: true})
|
decoder := structure.NewDecoder(structure.Option{TagName: "group", WeaklyTypedInput: true})
|
||||||
|
|
||||||
groupOption := &GroupCommonOption{
|
groupOption := &GroupCommonOption{
|
||||||
@@ -59,9 +58,19 @@ func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, provide
|
|||||||
return nil, errFormat
|
return nil, errFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if _, ok := config["routing-mark"]; ok {
|
||||||
|
log.Errorln("The group [%s] with routing-mark configuration was removed, please set it directly on the proxy instead", groupOption.Name)
|
||||||
|
}
|
||||||
|
if _, ok := config["interface-name"]; ok {
|
||||||
|
log.Errorln("The group [%s] with interface-name configuration was removed, please set it directly on the proxy instead", groupOption.Name)
|
||||||
|
}
|
||||||
|
if _, ok := config["dialer-proxy"]; ok {
|
||||||
|
log.Errorln("The group [%s] with dialer-proxy configuration is not allowed, please set it directly on the proxy instead", groupOption.Name)
|
||||||
|
}
|
||||||
|
|
||||||
groupName := groupOption.Name
|
groupName := groupOption.Name
|
||||||
|
|
||||||
providers := []types.ProxyProvider{}
|
providers := []P.ProxyProvider{}
|
||||||
|
|
||||||
if groupOption.IncludeAll {
|
if groupOption.IncludeAll {
|
||||||
groupOption.IncludeAllProviders = true
|
groupOption.IncludeAllProviders = true
|
||||||
@@ -159,7 +168,7 @@ func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, provide
|
|||||||
return nil, fmt.Errorf("%s: %w", groupName, err)
|
return nil, fmt.Errorf("%s: %w", groupName, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
providers = append([]types.ProxyProvider{pd}, providers...)
|
providers = append([]P.ProxyProvider{pd}, providers...)
|
||||||
providersMap[groupName] = pd
|
providersMap[groupName] = pd
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -176,7 +185,7 @@ func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, provide
|
|||||||
strategy := parseStrategy(config)
|
strategy := parseStrategy(config)
|
||||||
return NewLoadBalance(groupOption, providers, strategy)
|
return NewLoadBalance(groupOption, providers, strategy)
|
||||||
case "relay":
|
case "relay":
|
||||||
group = NewRelay(groupOption, providers)
|
return nil, fmt.Errorf("%w: The group [%s] with relay type was removed, please using dialer-proxy instead", errType, groupName)
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("%w: %s", errType, groupOption.Type)
|
return nil, fmt.Errorf("%w: %s", errType, groupOption.Type)
|
||||||
}
|
}
|
||||||
@@ -196,15 +205,15 @@ func getProxies(mapping map[string]C.Proxy, list []string) ([]C.Proxy, error) {
|
|||||||
return ps, nil
|
return ps, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getProviders(mapping map[string]types.ProxyProvider, list []string) ([]types.ProxyProvider, error) {
|
func getProviders(mapping map[string]P.ProxyProvider, list []string) ([]P.ProxyProvider, error) {
|
||||||
var ps []types.ProxyProvider
|
var ps []P.ProxyProvider
|
||||||
for _, name := range list {
|
for _, name := range list {
|
||||||
p, ok := mapping[name]
|
p, ok := mapping[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("'%s' not found", name)
|
return nil, fmt.Errorf("'%s' not found", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.VehicleType() == types.Compatible {
|
if p.VehicleType() == P.Compatible {
|
||||||
return nil, fmt.Errorf("proxy group %s can't contains in `use`", name)
|
return nil, fmt.Errorf("proxy group %s can't contains in `use`", name)
|
||||||
}
|
}
|
||||||
ps = append(ps, p)
|
ps = append(ps, p)
|
||||||
@@ -212,7 +221,7 @@ func getProviders(mapping map[string]types.ProxyProvider, list []string) ([]type
|
|||||||
return ps, nil
|
return ps, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func addTestUrlToProviders(providers []types.ProxyProvider, url string, expectedStatus utils.IntRanges[uint16], filter string, interval uint) {
|
func addTestUrlToProviders(providers []P.ProxyProvider, url string, expectedStatus utils.IntRanges[uint16], filter string, interval uint) {
|
||||||
if len(providers) == 0 || len(url) == 0 {
|
if len(providers) == 0 || len(url) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,64 +0,0 @@
|
|||||||
//go:build android && cmfa
|
|
||||||
|
|
||||||
package outboundgroup
|
|
||||||
|
|
||||||
import (
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
|
||||||
"github.com/metacubex/mihomo/constant/provider"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ProxyGroup interface {
|
|
||||||
C.ProxyAdapter
|
|
||||||
|
|
||||||
Providers() []provider.ProxyProvider
|
|
||||||
Proxies() []C.Proxy
|
|
||||||
Now() string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Fallback) Providers() []provider.ProxyProvider {
|
|
||||||
return f.providers
|
|
||||||
}
|
|
||||||
|
|
||||||
func (lb *LoadBalance) Providers() []provider.ProxyProvider {
|
|
||||||
return lb.providers
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Fallback) Proxies() []C.Proxy {
|
|
||||||
return f.GetProxies(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (lb *LoadBalance) Proxies() []C.Proxy {
|
|
||||||
return lb.GetProxies(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (lb *LoadBalance) Now() string {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Relay) Providers() []provider.ProxyProvider {
|
|
||||||
return r.providers
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Relay) Proxies() []C.Proxy {
|
|
||||||
return r.GetProxies(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Relay) Now() string {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Selector) Providers() []provider.ProxyProvider {
|
|
||||||
return s.providers
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Selector) Proxies() []C.Proxy {
|
|
||||||
return s.GetProxies(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *URLTest) Providers() []provider.ProxyProvider {
|
|
||||||
return u.providers
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *URLTest) Proxies() []C.Proxy {
|
|
||||||
return u.GetProxies(false)
|
|
||||||
}
|
|
||||||
@@ -1,172 +0,0 @@
|
|||||||
package outboundgroup
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/adapter/outbound"
|
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
|
||||||
"github.com/metacubex/mihomo/constant/provider"
|
|
||||||
"github.com/metacubex/mihomo/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Relay struct {
|
|
||||||
*GroupBase
|
|
||||||
Hidden bool
|
|
||||||
Icon string
|
|
||||||
}
|
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
|
||||||
func (r *Relay) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
|
||||||
proxies, chainProxies := r.proxies(metadata, true)
|
|
||||||
|
|
||||||
switch len(proxies) {
|
|
||||||
case 0:
|
|
||||||
return outbound.NewDirect().DialContext(ctx, metadata, r.Base.DialOptions(opts...)...)
|
|
||||||
case 1:
|
|
||||||
return proxies[0].DialContext(ctx, metadata, r.Base.DialOptions(opts...)...)
|
|
||||||
}
|
|
||||||
var d C.Dialer
|
|
||||||
d = dialer.NewDialer(r.Base.DialOptions(opts...)...)
|
|
||||||
for _, proxy := range proxies[:len(proxies)-1] {
|
|
||||||
d = proxydialer.New(proxy, d, false)
|
|
||||||
}
|
|
||||||
last := proxies[len(proxies)-1]
|
|
||||||
conn, err := last.DialContextWithDialer(ctx, d, metadata)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := len(chainProxies) - 2; i >= 0; i-- {
|
|
||||||
conn.AppendToChains(chainProxies[i])
|
|
||||||
}
|
|
||||||
|
|
||||||
conn.AppendToChains(r)
|
|
||||||
|
|
||||||
return conn, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
|
||||||
func (r *Relay) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
|
||||||
proxies, chainProxies := r.proxies(metadata, true)
|
|
||||||
|
|
||||||
switch len(proxies) {
|
|
||||||
case 0:
|
|
||||||
return outbound.NewDirect().ListenPacketContext(ctx, metadata, r.Base.DialOptions(opts...)...)
|
|
||||||
case 1:
|
|
||||||
return proxies[0].ListenPacketContext(ctx, metadata, r.Base.DialOptions(opts...)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
var d C.Dialer
|
|
||||||
d = dialer.NewDialer(r.Base.DialOptions(opts...)...)
|
|
||||||
for _, proxy := range proxies[:len(proxies)-1] {
|
|
||||||
d = proxydialer.New(proxy, d, false)
|
|
||||||
}
|
|
||||||
last := proxies[len(proxies)-1]
|
|
||||||
pc, err := last.ListenPacketWithDialer(ctx, d, metadata)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := len(chainProxies) - 2; i >= 0; i-- {
|
|
||||||
pc.AppendToChains(chainProxies[i])
|
|
||||||
}
|
|
||||||
|
|
||||||
pc.AppendToChains(r)
|
|
||||||
|
|
||||||
return pc, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SupportUDP implements C.ProxyAdapter
|
|
||||||
func (r *Relay) SupportUDP() bool {
|
|
||||||
proxies, _ := r.proxies(nil, false)
|
|
||||||
if len(proxies) == 0 { // C.Direct
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
for i := len(proxies) - 1; i >= 0; i-- {
|
|
||||||
proxy := proxies[i]
|
|
||||||
if !proxy.SupportUDP() {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if proxy.SupportUOT() {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
switch proxy.SupportWithDialer() {
|
|
||||||
case C.ALLNet:
|
|
||||||
case C.UDP:
|
|
||||||
default: // C.TCP and C.InvalidNet
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// MarshalJSON implements C.ProxyAdapter
|
|
||||||
func (r *Relay) MarshalJSON() ([]byte, error) {
|
|
||||||
all := []string{}
|
|
||||||
for _, proxy := range r.GetProxies(false) {
|
|
||||||
all = append(all, proxy.Name())
|
|
||||||
}
|
|
||||||
return json.Marshal(map[string]any{
|
|
||||||
"type": r.Type().String(),
|
|
||||||
"all": all,
|
|
||||||
"hidden": r.Hidden,
|
|
||||||
"icon": r.Icon,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Relay) proxies(metadata *C.Metadata, touch bool) ([]C.Proxy, []C.Proxy) {
|
|
||||||
rawProxies := r.GetProxies(touch)
|
|
||||||
|
|
||||||
var proxies []C.Proxy
|
|
||||||
var chainProxies []C.Proxy
|
|
||||||
var targetProxies []C.Proxy
|
|
||||||
|
|
||||||
for n, proxy := range rawProxies {
|
|
||||||
proxies = append(proxies, proxy)
|
|
||||||
chainProxies = append(chainProxies, proxy)
|
|
||||||
subproxy := proxy.Unwrap(metadata, touch)
|
|
||||||
for subproxy != nil {
|
|
||||||
chainProxies = append(chainProxies, subproxy)
|
|
||||||
proxies[n] = subproxy
|
|
||||||
subproxy = subproxy.Unwrap(metadata, touch)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, proxy := range proxies {
|
|
||||||
if proxy.Type() != C.Direct && proxy.Type() != C.Compatible {
|
|
||||||
targetProxies = append(targetProxies, proxy)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return targetProxies, chainProxies
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Relay) Addr() string {
|
|
||||||
proxies, _ := r.proxies(nil, false)
|
|
||||||
return proxies[len(proxies)-1].Addr()
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewRelay(option *GroupCommonOption, providers []provider.ProxyProvider) *Relay {
|
|
||||||
log.Warnln("The group [%s] with relay type is deprecated, please using dialer-proxy instead", option.Name)
|
|
||||||
return &Relay{
|
|
||||||
GroupBase: NewGroupBase(GroupBaseOption{
|
|
||||||
outbound.BaseOption{
|
|
||||||
Name: option.Name,
|
|
||||||
Type: C.Relay,
|
|
||||||
Interface: option.Interface,
|
|
||||||
RoutingMark: option.RoutingMark,
|
|
||||||
},
|
|
||||||
"",
|
|
||||||
"",
|
|
||||||
"",
|
|
||||||
5000,
|
|
||||||
5,
|
|
||||||
providers,
|
|
||||||
}),
|
|
||||||
Hidden: option.Hidden,
|
|
||||||
Icon: option.Icon,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -5,23 +5,22 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/adapter/outbound"
|
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/constant/provider"
|
P "github.com/metacubex/mihomo/constant/provider"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Selector struct {
|
type Selector struct {
|
||||||
*GroupBase
|
*GroupBase
|
||||||
disableUDP bool
|
disableUDP bool
|
||||||
selected string
|
selected string
|
||||||
|
testUrl string
|
||||||
Hidden bool
|
Hidden bool
|
||||||
Icon string
|
Icon string
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (s *Selector) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
func (s *Selector) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
||||||
c, err := s.selectedProxy(true).DialContext(ctx, metadata, s.Base.DialOptions(opts...)...)
|
c, err := s.selectedProxy(true).DialContext(ctx, metadata)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
c.AppendToChains(s)
|
c.AppendToChains(s)
|
||||||
}
|
}
|
||||||
@@ -29,8 +28,8 @@ func (s *Selector) DialContext(ctx context.Context, metadata *C.Metadata, opts .
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (s *Selector) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
func (s *Selector) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
||||||
pc, err := s.selectedProxy(true).ListenPacketContext(ctx, metadata, s.Base.DialOptions(opts...)...)
|
pc, err := s.selectedProxy(true).ListenPacketContext(ctx, metadata)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
pc.AppendToChains(s)
|
pc.AppendToChains(s)
|
||||||
}
|
}
|
||||||
@@ -57,13 +56,20 @@ func (s *Selector) MarshalJSON() ([]byte, error) {
|
|||||||
for _, proxy := range s.GetProxies(false) {
|
for _, proxy := range s.GetProxies(false) {
|
||||||
all = append(all, proxy.Name())
|
all = append(all, proxy.Name())
|
||||||
}
|
}
|
||||||
|
// When testurl is the default value
|
||||||
|
// do not append a value to ensure that the web dashboard follows the settings of the dashboard
|
||||||
|
var url string
|
||||||
|
if s.testUrl != C.DefaultTestURL {
|
||||||
|
url = s.testUrl
|
||||||
|
}
|
||||||
|
|
||||||
return json.Marshal(map[string]any{
|
return json.Marshal(map[string]any{
|
||||||
"type": s.Type().String(),
|
"type": s.Type().String(),
|
||||||
"now": s.Now(),
|
"now": s.Now(),
|
||||||
"all": all,
|
"all": all,
|
||||||
"hidden": s.Hidden,
|
"testUrl": url,
|
||||||
"icon": s.Icon,
|
"hidden": s.Hidden,
|
||||||
|
"icon": s.Icon,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,24 +108,29 @@ func (s *Selector) selectedProxy(touch bool) C.Proxy {
|
|||||||
return proxies[0]
|
return proxies[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSelector(option *GroupCommonOption, providers []provider.ProxyProvider) *Selector {
|
func (s *Selector) Providers() []P.ProxyProvider {
|
||||||
|
return s.providers
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Selector) Proxies() []C.Proxy {
|
||||||
|
return s.GetProxies(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSelector(option *GroupCommonOption, providers []P.ProxyProvider) *Selector {
|
||||||
return &Selector{
|
return &Selector{
|
||||||
GroupBase: NewGroupBase(GroupBaseOption{
|
GroupBase: NewGroupBase(GroupBaseOption{
|
||||||
outbound.BaseOption{
|
Name: option.Name,
|
||||||
Name: option.Name,
|
Type: C.Selector,
|
||||||
Type: C.Selector,
|
Filter: option.Filter,
|
||||||
Interface: option.Interface,
|
ExcludeFilter: option.ExcludeFilter,
|
||||||
RoutingMark: option.RoutingMark,
|
ExcludeType: option.ExcludeType,
|
||||||
},
|
TestTimeout: option.TestTimeout,
|
||||||
option.Filter,
|
MaxFailedTimes: option.MaxFailedTimes,
|
||||||
option.ExcludeFilter,
|
Providers: providers,
|
||||||
option.ExcludeType,
|
|
||||||
option.TestTimeout,
|
|
||||||
option.MaxFailedTimes,
|
|
||||||
providers,
|
|
||||||
}),
|
}),
|
||||||
selected: "COMPATIBLE",
|
selected: "COMPATIBLE",
|
||||||
disableUDP: option.DisableUDP,
|
disableUDP: option.DisableUDP,
|
||||||
|
testUrl: option.URL,
|
||||||
Hidden: option.Hidden,
|
Hidden: option.Hidden,
|
||||||
Icon: option.Icon,
|
Icon: option.Icon,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,14 +6,12 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/adapter/outbound"
|
|
||||||
"github.com/metacubex/mihomo/common/callback"
|
"github.com/metacubex/mihomo/common/callback"
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/metacubex/mihomo/common/singledo"
|
"github.com/metacubex/mihomo/common/singledo"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/constant/provider"
|
P "github.com/metacubex/mihomo/constant/provider"
|
||||||
)
|
)
|
||||||
|
|
||||||
type urlTestOption func(*URLTest)
|
type urlTestOption func(*URLTest)
|
||||||
@@ -62,9 +60,9 @@ func (u *URLTest) ForceSet(name string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (u *URLTest) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (c C.Conn, err error) {
|
func (u *URLTest) DialContext(ctx context.Context, metadata *C.Metadata) (c C.Conn, err error) {
|
||||||
proxy := u.fast(true)
|
proxy := u.fast(true)
|
||||||
c, err = proxy.DialContext(ctx, metadata, u.Base.DialOptions(opts...)...)
|
c, err = proxy.DialContext(ctx, metadata)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
c.AppendToChains(u)
|
c.AppendToChains(u)
|
||||||
} else {
|
} else {
|
||||||
@@ -85,9 +83,9 @@ func (u *URLTest) DialContext(ctx context.Context, metadata *C.Metadata, opts ..
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (u *URLTest) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
func (u *URLTest) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
||||||
proxy := u.fast(true)
|
proxy := u.fast(true)
|
||||||
pc, err := proxy.ListenPacketContext(ctx, metadata, u.Base.DialOptions(opts...)...)
|
pc, err := proxy.ListenPacketContext(ctx, metadata)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
pc.AppendToChains(u)
|
pc.AppendToChains(u)
|
||||||
} else {
|
} else {
|
||||||
@@ -187,6 +185,14 @@ func (u *URLTest) MarshalJSON() ([]byte, error) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *URLTest) Providers() []P.ProxyProvider {
|
||||||
|
return u.providers
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *URLTest) Proxies() []C.Proxy {
|
||||||
|
return u.GetProxies(false)
|
||||||
|
}
|
||||||
|
|
||||||
func (u *URLTest) URLTest(ctx context.Context, url string, expectedStatus utils.IntRanges[uint16]) (map[string]uint16, error) {
|
func (u *URLTest) URLTest(ctx context.Context, url string, expectedStatus utils.IntRanges[uint16]) (map[string]uint16, error) {
|
||||||
return u.GroupBase.URLTest(ctx, u.testUrl, expectedStatus)
|
return u.GroupBase.URLTest(ctx, u.testUrl, expectedStatus)
|
||||||
}
|
}
|
||||||
@@ -204,22 +210,17 @@ func parseURLTestOption(config map[string]any) []urlTestOption {
|
|||||||
return opts
|
return opts
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewURLTest(option *GroupCommonOption, providers []provider.ProxyProvider, options ...urlTestOption) *URLTest {
|
func NewURLTest(option *GroupCommonOption, providers []P.ProxyProvider, options ...urlTestOption) *URLTest {
|
||||||
urlTest := &URLTest{
|
urlTest := &URLTest{
|
||||||
GroupBase: NewGroupBase(GroupBaseOption{
|
GroupBase: NewGroupBase(GroupBaseOption{
|
||||||
outbound.BaseOption{
|
Name: option.Name,
|
||||||
Name: option.Name,
|
Type: C.URLTest,
|
||||||
Type: C.URLTest,
|
Filter: option.Filter,
|
||||||
Interface: option.Interface,
|
ExcludeFilter: option.ExcludeFilter,
|
||||||
RoutingMark: option.RoutingMark,
|
ExcludeType: option.ExcludeType,
|
||||||
},
|
TestTimeout: option.TestTimeout,
|
||||||
|
MaxFailedTimes: option.MaxFailedTimes,
|
||||||
option.Filter,
|
Providers: providers,
|
||||||
option.ExcludeFilter,
|
|
||||||
option.ExcludeType,
|
|
||||||
option.TestTimeout,
|
|
||||||
option.MaxFailedTimes,
|
|
||||||
providers,
|
|
||||||
}),
|
}),
|
||||||
fastSingle: singledo.NewSingle[C.Proxy](time.Second * 10),
|
fastSingle: singledo.NewSingle[C.Proxy](time.Second * 10),
|
||||||
disableUDP: option.DisableUDP,
|
disableUDP: option.DisableUDP,
|
||||||
|
|||||||
@@ -1,5 +1,29 @@
|
|||||||
package outboundgroup
|
package outboundgroup
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
|
C "github.com/metacubex/mihomo/constant"
|
||||||
|
P "github.com/metacubex/mihomo/constant/provider"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ProxyGroup interface {
|
||||||
|
C.ProxyAdapter
|
||||||
|
|
||||||
|
Providers() []P.ProxyProvider
|
||||||
|
Proxies() []C.Proxy
|
||||||
|
Now() string
|
||||||
|
Touch()
|
||||||
|
|
||||||
|
URLTest(ctx context.Context, url string, expectedStatus utils.IntRanges[uint16]) (mp map[string]uint16, err error)
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ ProxyGroup = (*Fallback)(nil)
|
||||||
|
var _ ProxyGroup = (*LoadBalance)(nil)
|
||||||
|
var _ ProxyGroup = (*URLTest)(nil)
|
||||||
|
var _ ProxyGroup = (*Selector)(nil)
|
||||||
|
|
||||||
type SelectAble interface {
|
type SelectAble interface {
|
||||||
Set(string) error
|
Set(string) error
|
||||||
ForceSet(name string)
|
ForceSet(name string)
|
||||||
|
|||||||
@@ -8,150 +8,171 @@ import (
|
|||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ParseProxy(mapping map[string]any) (C.Proxy, error) {
|
func ParseProxy(mapping map[string]any, options ...ProxyOption) (C.Proxy, error) {
|
||||||
decoder := structure.NewDecoder(structure.Option{TagName: "proxy", WeaklyTypedInput: true, KeyReplacer: structure.DefaultKeyReplacer})
|
decoder := structure.NewDecoder(structure.Option{TagName: "proxy", WeaklyTypedInput: true, KeyReplacer: structure.DefaultKeyReplacer})
|
||||||
proxyType, existType := mapping["type"].(string)
|
proxyType, existType := mapping["type"].(string)
|
||||||
if !existType {
|
if !existType {
|
||||||
return nil, fmt.Errorf("missing type")
|
return nil, fmt.Errorf("missing type")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
opt := applyProxyOptions(options...)
|
||||||
|
basicOption := outbound.BasicOption{
|
||||||
|
DialerForAPI: opt.DialerForAPI,
|
||||||
|
ProviderName: opt.ProviderName,
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
proxy outbound.ProxyAdapter
|
proxy outbound.ProxyAdapter
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
switch proxyType {
|
switch proxyType {
|
||||||
case "ss":
|
case "ss":
|
||||||
ssOption := &outbound.ShadowSocksOption{}
|
ssOption := &outbound.ShadowSocksOption{BasicOption: basicOption}
|
||||||
err = decoder.Decode(mapping, ssOption)
|
err = decoder.Decode(mapping, ssOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
proxy, err = outbound.NewShadowSocks(*ssOption)
|
proxy, err = outbound.NewShadowSocks(*ssOption)
|
||||||
case "ssr":
|
case "ssr":
|
||||||
ssrOption := &outbound.ShadowSocksROption{}
|
ssrOption := &outbound.ShadowSocksROption{BasicOption: basicOption}
|
||||||
err = decoder.Decode(mapping, ssrOption)
|
err = decoder.Decode(mapping, ssrOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
proxy, err = outbound.NewShadowSocksR(*ssrOption)
|
proxy, err = outbound.NewShadowSocksR(*ssrOption)
|
||||||
case "socks5":
|
case "socks5":
|
||||||
socksOption := &outbound.Socks5Option{}
|
socksOption := &outbound.Socks5Option{BasicOption: basicOption}
|
||||||
err = decoder.Decode(mapping, socksOption)
|
err = decoder.Decode(mapping, socksOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
proxy, err = outbound.NewSocks5(*socksOption)
|
proxy, err = outbound.NewSocks5(*socksOption)
|
||||||
case "http":
|
case "http":
|
||||||
httpOption := &outbound.HttpOption{}
|
httpOption := &outbound.HttpOption{BasicOption: basicOption}
|
||||||
err = decoder.Decode(mapping, httpOption)
|
err = decoder.Decode(mapping, httpOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
proxy, err = outbound.NewHttp(*httpOption)
|
proxy, err = outbound.NewHttp(*httpOption)
|
||||||
case "vmess":
|
case "vmess":
|
||||||
vmessOption := &outbound.VmessOption{
|
vmessOption := &outbound.VmessOption{BasicOption: basicOption}
|
||||||
HTTPOpts: outbound.HTTPOptions{
|
|
||||||
Method: "GET",
|
|
||||||
Path: []string{"/"},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
err = decoder.Decode(mapping, vmessOption)
|
err = decoder.Decode(mapping, vmessOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
proxy, err = outbound.NewVmess(*vmessOption)
|
proxy, err = outbound.NewVmess(*vmessOption)
|
||||||
case "vless":
|
case "vless":
|
||||||
vlessOption := &outbound.VlessOption{}
|
vlessOption := &outbound.VlessOption{BasicOption: basicOption}
|
||||||
err = decoder.Decode(mapping, vlessOption)
|
err = decoder.Decode(mapping, vlessOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
proxy, err = outbound.NewVless(*vlessOption)
|
proxy, err = outbound.NewVless(*vlessOption)
|
||||||
case "snell":
|
case "snell":
|
||||||
snellOption := &outbound.SnellOption{}
|
snellOption := &outbound.SnellOption{BasicOption: basicOption}
|
||||||
err = decoder.Decode(mapping, snellOption)
|
err = decoder.Decode(mapping, snellOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
proxy, err = outbound.NewSnell(*snellOption)
|
proxy, err = outbound.NewSnell(*snellOption)
|
||||||
case "trojan":
|
case "trojan":
|
||||||
trojanOption := &outbound.TrojanOption{}
|
trojanOption := &outbound.TrojanOption{BasicOption: basicOption}
|
||||||
err = decoder.Decode(mapping, trojanOption)
|
err = decoder.Decode(mapping, trojanOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
proxy, err = outbound.NewTrojan(*trojanOption)
|
proxy, err = outbound.NewTrojan(*trojanOption)
|
||||||
case "hysteria":
|
case "hysteria":
|
||||||
hyOption := &outbound.HysteriaOption{}
|
hyOption := &outbound.HysteriaOption{BasicOption: basicOption}
|
||||||
err = decoder.Decode(mapping, hyOption)
|
err = decoder.Decode(mapping, hyOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
proxy, err = outbound.NewHysteria(*hyOption)
|
proxy, err = outbound.NewHysteria(*hyOption)
|
||||||
case "hysteria2":
|
case "hysteria2":
|
||||||
hyOption := &outbound.Hysteria2Option{}
|
hyOption := &outbound.Hysteria2Option{BasicOption: basicOption}
|
||||||
err = decoder.Decode(mapping, hyOption)
|
err = decoder.Decode(mapping, hyOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
proxy, err = outbound.NewHysteria2(*hyOption)
|
proxy, err = outbound.NewHysteria2(*hyOption)
|
||||||
case "wireguard":
|
case "wireguard":
|
||||||
wgOption := &outbound.WireGuardOption{}
|
wgOption := &outbound.WireGuardOption{BasicOption: basicOption}
|
||||||
err = decoder.Decode(mapping, wgOption)
|
err = decoder.Decode(mapping, wgOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
proxy, err = outbound.NewWireGuard(*wgOption)
|
proxy, err = outbound.NewWireGuard(*wgOption)
|
||||||
case "tuic":
|
case "tuic":
|
||||||
tuicOption := &outbound.TuicOption{}
|
tuicOption := &outbound.TuicOption{BasicOption: basicOption}
|
||||||
err = decoder.Decode(mapping, tuicOption)
|
err = decoder.Decode(mapping, tuicOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
proxy, err = outbound.NewTuic(*tuicOption)
|
proxy, err = outbound.NewTuic(*tuicOption)
|
||||||
case "direct":
|
case "direct":
|
||||||
directOption := &outbound.DirectOption{}
|
directOption := &outbound.DirectOption{BasicOption: basicOption}
|
||||||
err = decoder.Decode(mapping, directOption)
|
err = decoder.Decode(mapping, directOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
proxy = outbound.NewDirectWithOption(*directOption)
|
proxy = outbound.NewDirectWithOption(*directOption)
|
||||||
case "dns":
|
case "dns":
|
||||||
dnsOptions := &outbound.DnsOption{}
|
dnsOptions := &outbound.DnsOption{BasicOption: basicOption}
|
||||||
err = decoder.Decode(mapping, dnsOptions)
|
err = decoder.Decode(mapping, dnsOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
proxy = outbound.NewDnsWithOption(*dnsOptions)
|
proxy = outbound.NewDnsWithOption(*dnsOptions)
|
||||||
case "reject":
|
case "reject":
|
||||||
rejectOption := &outbound.RejectOption{}
|
rejectOption := &outbound.RejectOption{BasicOption: basicOption}
|
||||||
err = decoder.Decode(mapping, rejectOption)
|
err = decoder.Decode(mapping, rejectOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
proxy = outbound.NewRejectWithOption(*rejectOption)
|
proxy = outbound.NewRejectWithOption(*rejectOption)
|
||||||
case "ssh":
|
case "ssh":
|
||||||
sshOption := &outbound.SshOption{}
|
sshOption := &outbound.SshOption{BasicOption: basicOption}
|
||||||
err = decoder.Decode(mapping, sshOption)
|
err = decoder.Decode(mapping, sshOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
proxy, err = outbound.NewSsh(*sshOption)
|
proxy, err = outbound.NewSsh(*sshOption)
|
||||||
case "mieru":
|
case "mieru":
|
||||||
mieruOption := &outbound.MieruOption{}
|
mieruOption := &outbound.MieruOption{BasicOption: basicOption}
|
||||||
err = decoder.Decode(mapping, mieruOption)
|
err = decoder.Decode(mapping, mieruOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
proxy, err = outbound.NewMieru(*mieruOption)
|
proxy, err = outbound.NewMieru(*mieruOption)
|
||||||
case "anytls":
|
case "anytls":
|
||||||
anytlsOption := &outbound.AnyTLSOption{}
|
anytlsOption := &outbound.AnyTLSOption{BasicOption: basicOption}
|
||||||
err = decoder.Decode(mapping, anytlsOption)
|
err = decoder.Decode(mapping, anytlsOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
proxy, err = outbound.NewAnyTLS(*anytlsOption)
|
proxy, err = outbound.NewAnyTLS(*anytlsOption)
|
||||||
|
case "sudoku":
|
||||||
|
sudokuOption := &outbound.SudokuOption{BasicOption: basicOption}
|
||||||
|
err = decoder.Decode(mapping, sudokuOption)
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
proxy, err = outbound.NewSudoku(*sudokuOption)
|
||||||
|
case "masque":
|
||||||
|
masqueOption := &outbound.MasqueOption{BasicOption: basicOption}
|
||||||
|
err = decoder.Decode(mapping, masqueOption)
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
proxy, err = outbound.NewMasque(*masqueOption)
|
||||||
|
case "trusttunnel":
|
||||||
|
trustTunnelOption := &outbound.TrustTunnelOption{BasicOption: basicOption}
|
||||||
|
err = decoder.Decode(mapping, trustTunnelOption)
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
proxy, err = outbound.NewTrustTunnel(*trustTunnelOption)
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unsupport proxy type: %s", proxyType)
|
return nil, fmt.Errorf("unsupport proxy type: %s", proxyType)
|
||||||
}
|
}
|
||||||
@@ -177,3 +198,30 @@ func ParseProxy(mapping map[string]any) (C.Proxy, error) {
|
|||||||
proxy = outbound.NewAutoCloseProxyAdapter(proxy)
|
proxy = outbound.NewAutoCloseProxyAdapter(proxy)
|
||||||
return NewProxy(proxy), nil
|
return NewProxy(proxy), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type proxyOption struct {
|
||||||
|
DialerForAPI C.Dialer
|
||||||
|
ProviderName string
|
||||||
|
}
|
||||||
|
|
||||||
|
func applyProxyOptions(options ...ProxyOption) proxyOption {
|
||||||
|
opt := proxyOption{}
|
||||||
|
for _, o := range options {
|
||||||
|
o(&opt)
|
||||||
|
}
|
||||||
|
return opt
|
||||||
|
}
|
||||||
|
|
||||||
|
type ProxyOption func(opt *proxyOption)
|
||||||
|
|
||||||
|
func WithDialerForAPI(dialer C.Dialer) ProxyOption {
|
||||||
|
return func(opt *proxyOption) {
|
||||||
|
opt.DialerForAPI = dialer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithProviderName(name string) ProxyOption {
|
||||||
|
return func(opt *proxyOption) {
|
||||||
|
opt.ProviderName = name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,13 +7,13 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/atomic"
|
"github.com/metacubex/mihomo/common/atomic"
|
||||||
"github.com/metacubex/mihomo/common/batch"
|
|
||||||
"github.com/metacubex/mihomo/common/singledo"
|
"github.com/metacubex/mihomo/common/singledo"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/log"
|
"github.com/metacubex/mihomo/log"
|
||||||
|
|
||||||
"github.com/dlclark/regexp2"
|
"github.com/dlclark/regexp2"
|
||||||
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HealthCheckOption struct {
|
type HealthCheckOption struct {
|
||||||
@@ -32,7 +32,6 @@ type HealthCheck struct {
|
|||||||
url string
|
url string
|
||||||
extra map[string]*extraOption
|
extra map[string]*extraOption
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
started atomic.Bool
|
|
||||||
proxies []C.Proxy
|
proxies []C.Proxy
|
||||||
interval time.Duration
|
interval time.Duration
|
||||||
lazy bool
|
lazy bool
|
||||||
@@ -43,13 +42,8 @@ type HealthCheck struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (hc *HealthCheck) process() {
|
func (hc *HealthCheck) process() {
|
||||||
if hc.started.Load() {
|
|
||||||
log.Warnln("Skip start health check timer due to it's started")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ticker := time.NewTicker(hc.interval)
|
ticker := time.NewTicker(hc.interval)
|
||||||
hc.start()
|
go hc.check()
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
@@ -62,13 +56,12 @@ func (hc *HealthCheck) process() {
|
|||||||
}
|
}
|
||||||
case <-hc.ctx.Done():
|
case <-hc.ctx.Done():
|
||||||
ticker.Stop()
|
ticker.Stop()
|
||||||
hc.stop()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hc *HealthCheck) setProxy(proxies []C.Proxy) {
|
func (hc *HealthCheck) setProxies(proxies []C.Proxy) {
|
||||||
hc.proxies = proxies
|
hc.proxies = proxies
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,10 +98,6 @@ func (hc *HealthCheck) registerHealthCheckTask(url string, expectedStatus utils.
|
|||||||
option := &extraOption{filters: map[string]struct{}{}, expectedStatus: expectedStatus}
|
option := &extraOption{filters: map[string]struct{}{}, expectedStatus: expectedStatus}
|
||||||
splitAndAddFiltersToExtra(filter, option)
|
splitAndAddFiltersToExtra(filter, option)
|
||||||
hc.extra[url] = option
|
hc.extra[url] = option
|
||||||
|
|
||||||
if hc.auto() && !hc.started.Load() {
|
|
||||||
go hc.process()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func splitAndAddFiltersToExtra(filter string, option *extraOption) {
|
func splitAndAddFiltersToExtra(filter string, option *extraOption) {
|
||||||
@@ -131,14 +120,6 @@ func (hc *HealthCheck) touch() {
|
|||||||
hc.lastTouch.Store(time.Now())
|
hc.lastTouch.Store(time.Now())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hc *HealthCheck) start() {
|
|
||||||
hc.started.Store(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (hc *HealthCheck) stop() {
|
|
||||||
hc.started.Store(false)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (hc *HealthCheck) check() {
|
func (hc *HealthCheck) check() {
|
||||||
if len(hc.proxies) == 0 {
|
if len(hc.proxies) == 0 {
|
||||||
return
|
return
|
||||||
@@ -147,7 +128,8 @@ func (hc *HealthCheck) check() {
|
|||||||
_, _, _ = hc.singleDo.Do(func() (struct{}, error) {
|
_, _, _ = hc.singleDo.Do(func() (struct{}, error) {
|
||||||
id := utils.NewUUIDV4().String()
|
id := utils.NewUUIDV4().String()
|
||||||
log.Debugln("Start New Health Checking {%s}", id)
|
log.Debugln("Start New Health Checking {%s}", id)
|
||||||
b, _ := batch.New[bool](hc.ctx, batch.WithConcurrencyNum[bool](10))
|
b := new(errgroup.Group)
|
||||||
|
b.SetLimit(10)
|
||||||
|
|
||||||
// execute default health check
|
// execute default health check
|
||||||
option := &extraOption{filters: nil, expectedStatus: hc.expectedStatus}
|
option := &extraOption{filters: nil, expectedStatus: hc.expectedStatus}
|
||||||
@@ -159,13 +141,13 @@ func (hc *HealthCheck) check() {
|
|||||||
hc.execute(b, url, id, option)
|
hc.execute(b, url, id, option)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
b.Wait()
|
_ = b.Wait()
|
||||||
log.Debugln("Finish A Health Checking {%s}", id)
|
log.Debugln("Finish A Health Checking {%s}", id)
|
||||||
return struct{}{}, nil
|
return struct{}{}, nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hc *HealthCheck) execute(b *batch.Batch[bool], url, uid string, option *extraOption) {
|
func (hc *HealthCheck) execute(b *errgroup.Group, url, uid string, option *extraOption) {
|
||||||
url = strings.TrimSpace(url)
|
url = strings.TrimSpace(url)
|
||||||
if len(url) == 0 {
|
if len(url) == 0 {
|
||||||
log.Debugln("Health Check has been skipped due to testUrl is empty, {%s}", uid)
|
log.Debugln("Health Check has been skipped due to testUrl is empty, {%s}", uid)
|
||||||
@@ -195,13 +177,13 @@ func (hc *HealthCheck) execute(b *batch.Batch[bool], url, uid string, option *ex
|
|||||||
}
|
}
|
||||||
|
|
||||||
p := proxy
|
p := proxy
|
||||||
b.Go(p.Name(), func() (bool, error) {
|
b.Go(func() error {
|
||||||
ctx, cancel := context.WithTimeout(hc.ctx, hc.timeout)
|
ctx, cancel := context.WithTimeout(hc.ctx, hc.timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
log.Debugln("Health Checking, proxy: %s, url: %s, id: {%s}", p.Name(), url, uid)
|
log.Debugln("Health Checking, proxy: %s, url: %s, id: {%s}", p.Name(), url, uid)
|
||||||
_, _ = p.URLTest(ctx, url, expectedStatus)
|
_, _ = p.URLTest(ctx, url, expectedStatus)
|
||||||
log.Debugln("Health Checked, proxy: %s, url: %s, alive: %t, delay: %d ms uid: {%s}", p.Name(), url, p.AliveForTestUrl(url), p.LastDelayForTestUrl(url), uid)
|
log.Debugln("Health Checked, proxy: %s, url: %s, alive: %t, delay: %d ms uid: {%s}", p.Name(), url, p.AliveForTestUrl(url), p.LastDelayForTestUrl(url), uid)
|
||||||
return false, nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
88
adapter/provider/override.go
Normal file
88
adapter/provider/override.go
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/dlclark/regexp2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type overrideSchema struct {
|
||||||
|
TFO *bool `provider:"tfo,omitempty"`
|
||||||
|
MPTcp *bool `provider:"mptcp,omitempty"`
|
||||||
|
UDP *bool `provider:"udp,omitempty"`
|
||||||
|
UDPOverTCP *bool `provider:"udp-over-tcp,omitempty"`
|
||||||
|
Up *string `provider:"up,omitempty"`
|
||||||
|
Down *string `provider:"down,omitempty"`
|
||||||
|
DialerProxy *string `provider:"dialer-proxy,omitempty"`
|
||||||
|
SkipCertVerify *bool `provider:"skip-cert-verify,omitempty"`
|
||||||
|
Interface *string `provider:"interface-name,omitempty"`
|
||||||
|
RoutingMark *int `provider:"routing-mark,omitempty"`
|
||||||
|
IPVersion *string `provider:"ip-version,omitempty"`
|
||||||
|
|
||||||
|
AdditionalPrefix *string `provider:"additional-prefix,omitempty"`
|
||||||
|
AdditionalSuffix *string `provider:"additional-suffix,omitempty"`
|
||||||
|
ProxyName []overrideProxyNameSchema `provider:"proxy-name,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type overrideProxyNameSchema struct {
|
||||||
|
// matching expression for regex replacement
|
||||||
|
Pattern *regexp2.Regexp `provider:"pattern"`
|
||||||
|
// the new content after regex matching
|
||||||
|
Target string `provider:"target"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ encoding.TextUnmarshaler = (*regexp2.Regexp)(nil) // ensure *regexp2.Regexp can decode direct by structure package
|
||||||
|
|
||||||
|
func (o *overrideSchema) Apply(mapping map[string]any) error {
|
||||||
|
if o.TFO != nil {
|
||||||
|
mapping["tfo"] = *o.TFO
|
||||||
|
}
|
||||||
|
if o.MPTcp != nil {
|
||||||
|
mapping["mptcp"] = *o.MPTcp
|
||||||
|
}
|
||||||
|
if o.UDP != nil {
|
||||||
|
mapping["udp"] = *o.UDP
|
||||||
|
}
|
||||||
|
if o.UDPOverTCP != nil {
|
||||||
|
mapping["udp-over-tcp"] = *o.UDPOverTCP
|
||||||
|
}
|
||||||
|
if o.Up != nil {
|
||||||
|
mapping["up"] = *o.Up
|
||||||
|
}
|
||||||
|
if o.Down != nil {
|
||||||
|
mapping["down"] = *o.Down
|
||||||
|
}
|
||||||
|
if o.DialerProxy != nil {
|
||||||
|
mapping["dialer-proxy"] = *o.DialerProxy
|
||||||
|
}
|
||||||
|
if o.SkipCertVerify != nil {
|
||||||
|
mapping["skip-cert-verify"] = *o.SkipCertVerify
|
||||||
|
}
|
||||||
|
if o.Interface != nil {
|
||||||
|
mapping["interface-name"] = *o.Interface
|
||||||
|
}
|
||||||
|
if o.RoutingMark != nil {
|
||||||
|
mapping["routing-mark"] = *o.RoutingMark
|
||||||
|
}
|
||||||
|
if o.IPVersion != nil {
|
||||||
|
mapping["ip-version"] = *o.IPVersion
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, expr := range o.ProxyName {
|
||||||
|
name := mapping["name"].(string)
|
||||||
|
newName, err := expr.Pattern.Replace(name, expr.Target, 0, -1)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("proxy name replace error: %w", err)
|
||||||
|
}
|
||||||
|
mapping["name"] = newName
|
||||||
|
}
|
||||||
|
if o.AdditionalPrefix != nil {
|
||||||
|
mapping["name"] = fmt.Sprintf("%s%s", *o.AdditionalPrefix, mapping["name"])
|
||||||
|
}
|
||||||
|
if o.AdditionalSuffix != nil {
|
||||||
|
mapping["name"] = fmt.Sprintf("%s%s", mapping["name"], *o.AdditionalSuffix)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package provider
|
package provider
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
@@ -10,14 +9,11 @@ import (
|
|||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
"github.com/metacubex/mihomo/component/resource"
|
"github.com/metacubex/mihomo/component/resource"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
types "github.com/metacubex/mihomo/constant/provider"
|
P "github.com/metacubex/mihomo/constant/provider"
|
||||||
|
|
||||||
"github.com/dlclark/regexp2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
errVehicleType = errors.New("unsupport vehicle type")
|
errVehicleType = errors.New("unsupport vehicle type")
|
||||||
errSubPath = errors.New("path is not subpath of home directory")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type healthCheckSchema struct {
|
type healthCheckSchema struct {
|
||||||
@@ -29,33 +25,6 @@ type healthCheckSchema struct {
|
|||||||
ExpectedStatus string `provider:"expected-status,omitempty"`
|
ExpectedStatus string `provider:"expected-status,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type OverrideProxyNameSchema struct {
|
|
||||||
// matching expression for regex replacement
|
|
||||||
Pattern *regexp2.Regexp `provider:"pattern"`
|
|
||||||
// the new content after regex matching
|
|
||||||
Target string `provider:"target"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ encoding.TextUnmarshaler = (*regexp2.Regexp)(nil) // ensure *regexp2.Regexp can decode direct by structure package
|
|
||||||
|
|
||||||
type OverrideSchema struct {
|
|
||||||
TFO *bool `provider:"tfo,omitempty"`
|
|
||||||
MPTcp *bool `provider:"mptcp,omitempty"`
|
|
||||||
UDP *bool `provider:"udp,omitempty"`
|
|
||||||
UDPOverTCP *bool `provider:"udp-over-tcp,omitempty"`
|
|
||||||
Up *string `provider:"up,omitempty"`
|
|
||||||
Down *string `provider:"down,omitempty"`
|
|
||||||
DialerProxy *string `provider:"dialer-proxy,omitempty"`
|
|
||||||
SkipCertVerify *bool `provider:"skip-cert-verify,omitempty"`
|
|
||||||
Interface *string `provider:"interface-name,omitempty"`
|
|
||||||
RoutingMark *int `provider:"routing-mark,omitempty"`
|
|
||||||
IPVersion *string `provider:"ip-version,omitempty"`
|
|
||||||
AdditionalPrefix *string `provider:"additional-prefix,omitempty"`
|
|
||||||
AdditionalSuffix *string `provider:"additional-suffix,omitempty"`
|
|
||||||
|
|
||||||
ProxyName []OverrideProxyNameSchema `provider:"proxy-name,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type proxyProviderSchema struct {
|
type proxyProviderSchema struct {
|
||||||
Type string `provider:"type"`
|
Type string `provider:"type"`
|
||||||
Path string `provider:"path,omitempty"`
|
Path string `provider:"path,omitempty"`
|
||||||
@@ -70,11 +39,11 @@ type proxyProviderSchema struct {
|
|||||||
Payload []map[string]any `provider:"payload,omitempty"`
|
Payload []map[string]any `provider:"payload,omitempty"`
|
||||||
|
|
||||||
HealthCheck healthCheckSchema `provider:"health-check,omitempty"`
|
HealthCheck healthCheckSchema `provider:"health-check,omitempty"`
|
||||||
Override OverrideSchema `provider:"override,omitempty"`
|
Override overrideSchema `provider:"override,omitempty"`
|
||||||
Header map[string][]string `provider:"header,omitempty"`
|
Header map[string][]string `provider:"header,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvider, error) {
|
func ParseProxyProvider(name string, mapping map[string]any) (P.ProxyProvider, error) {
|
||||||
decoder := structure.NewDecoder(structure.Option{TagName: "provider", WeaklyTypedInput: true})
|
decoder := structure.NewDecoder(structure.Option{TagName: "provider", WeaklyTypedInput: true})
|
||||||
|
|
||||||
schema := &proxyProviderSchema{
|
schema := &proxyProviderSchema{
|
||||||
@@ -100,22 +69,25 @@ func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvide
|
|||||||
}
|
}
|
||||||
hc := NewHealthCheck([]C.Proxy{}, schema.HealthCheck.URL, uint(schema.HealthCheck.TestTimeout), hcInterval, schema.HealthCheck.Lazy, expectedStatus)
|
hc := NewHealthCheck([]C.Proxy{}, schema.HealthCheck.URL, uint(schema.HealthCheck.TestTimeout), hcInterval, schema.HealthCheck.Lazy, expectedStatus)
|
||||||
|
|
||||||
parser, err := NewProxiesParser(schema.Filter, schema.ExcludeFilter, schema.ExcludeType, schema.DialerProxy, schema.Override)
|
parser, err := NewProxiesParser(name, schema.Filter, schema.ExcludeFilter, schema.ExcludeType, schema.DialerProxy, schema.Override)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var vehicle types.Vehicle
|
var vehicle P.Vehicle
|
||||||
switch schema.Type {
|
switch schema.Type {
|
||||||
case "file":
|
case "file":
|
||||||
path := C.Path.Resolve(schema.Path)
|
path := C.Path.Resolve(schema.Path)
|
||||||
|
if !C.Path.IsSafePath(path) {
|
||||||
|
return nil, C.Path.ErrNotSafePath(path)
|
||||||
|
}
|
||||||
vehicle = resource.NewFileVehicle(path)
|
vehicle = resource.NewFileVehicle(path)
|
||||||
case "http":
|
case "http":
|
||||||
path := C.Path.GetPathByHash("proxies", schema.URL)
|
path := C.Path.GetPathByHash("proxies", schema.URL)
|
||||||
if schema.Path != "" {
|
if schema.Path != "" {
|
||||||
path = C.Path.Resolve(schema.Path)
|
path = C.Path.Resolve(schema.Path)
|
||||||
if !C.Path.IsSafePath(path) {
|
if !C.Path.IsSafePath(path) {
|
||||||
return nil, fmt.Errorf("%w: %s", errSubPath, path)
|
return nil, C.Path.ErrNotSafePath(path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
vehicle = resource.NewHTTPVehicle(schema.URL, path, schema.Proxy, schema.Header, resource.DefaultHttpTimeout, schema.SizeLimit)
|
vehicle = resource.NewHTTPVehicle(schema.URL, path, schema.Proxy, schema.Header, resource.DefaultHttpTimeout, schema.SizeLimit)
|
||||||
@@ -127,5 +99,5 @@ func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvide
|
|||||||
|
|
||||||
interval := time.Duration(uint(schema.Interval)) * time.Second
|
interval := time.Duration(uint(schema.Interval)) * time.Second
|
||||||
|
|
||||||
return NewProxySetProvider(name, interval, parser, vehicle, hc)
|
return NewProxySetProvider(name, interval, schema.Payload, parser, vehicle, hc)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,23 +4,23 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"reflect"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/adapter"
|
"github.com/metacubex/mihomo/adapter"
|
||||||
"github.com/metacubex/mihomo/common/convert"
|
"github.com/metacubex/mihomo/common/convert"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
|
"github.com/metacubex/mihomo/common/yaml"
|
||||||
"github.com/metacubex/mihomo/component/profile/cachefile"
|
"github.com/metacubex/mihomo/component/profile/cachefile"
|
||||||
"github.com/metacubex/mihomo/component/resource"
|
"github.com/metacubex/mihomo/component/resource"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
types "github.com/metacubex/mihomo/constant/provider"
|
P "github.com/metacubex/mihomo/constant/provider"
|
||||||
"github.com/metacubex/mihomo/tunnel/statistic"
|
"github.com/metacubex/mihomo/tunnel/statistic"
|
||||||
|
|
||||||
"github.com/dlclark/regexp2"
|
"github.com/dlclark/regexp2"
|
||||||
"gopkg.in/yaml.v3"
|
"github.com/metacubex/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -43,6 +43,7 @@ type providerForApi struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type baseProvider struct {
|
type baseProvider struct {
|
||||||
|
mutex sync.RWMutex
|
||||||
name string
|
name string
|
||||||
proxies []C.Proxy
|
proxies []C.Proxy
|
||||||
healthCheck *HealthCheck
|
healthCheck *HealthCheck
|
||||||
@@ -54,22 +55,35 @@ func (bp *baseProvider) Name() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (bp *baseProvider) Version() uint32 {
|
func (bp *baseProvider) Version() uint32 {
|
||||||
|
bp.mutex.RLock()
|
||||||
|
defer bp.mutex.RUnlock()
|
||||||
return bp.version
|
return bp.version
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (bp *baseProvider) Initial() error {
|
||||||
|
if bp.healthCheck.auto() {
|
||||||
|
go bp.healthCheck.process()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (bp *baseProvider) HealthCheck() {
|
func (bp *baseProvider) HealthCheck() {
|
||||||
bp.healthCheck.check()
|
bp.healthCheck.check()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bp *baseProvider) Type() types.ProviderType {
|
func (bp *baseProvider) Type() P.ProviderType {
|
||||||
return types.Proxy
|
return P.Proxy
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bp *baseProvider) Proxies() []C.Proxy {
|
func (bp *baseProvider) Proxies() []C.Proxy {
|
||||||
|
bp.mutex.RLock()
|
||||||
|
defer bp.mutex.RUnlock()
|
||||||
return bp.proxies
|
return bp.proxies
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bp *baseProvider) Count() int {
|
func (bp *baseProvider) Count() int {
|
||||||
|
bp.mutex.RLock()
|
||||||
|
defer bp.mutex.RUnlock()
|
||||||
return len(bp.proxies)
|
return len(bp.proxies)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,9 +100,11 @@ func (bp *baseProvider) RegisterHealthCheckTask(url string, expectedStatus utils
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (bp *baseProvider) setProxies(proxies []C.Proxy) {
|
func (bp *baseProvider) setProxies(proxies []C.Proxy) {
|
||||||
|
bp.mutex.Lock()
|
||||||
|
defer bp.mutex.Unlock()
|
||||||
bp.proxies = proxies
|
bp.proxies = proxies
|
||||||
bp.version += 1
|
bp.version += 1
|
||||||
bp.healthCheck.setProxy(proxies)
|
bp.healthCheck.setProxies(proxies)
|
||||||
if bp.healthCheck.auto() {
|
if bp.healthCheck.auto() {
|
||||||
go bp.healthCheck.check()
|
go bp.healthCheck.check()
|
||||||
}
|
}
|
||||||
@@ -133,6 +149,9 @@ func (pp *proxySetProvider) Update() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (pp *proxySetProvider) Initial() error {
|
func (pp *proxySetProvider) Initial() error {
|
||||||
|
if err := pp.baseProvider.Initial(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
_, err := pp.Fetcher.Initial()
|
_, err := pp.Fetcher.Initial()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -146,7 +165,7 @@ func (pp *proxySetProvider) Initial() error {
|
|||||||
|
|
||||||
func (pp *proxySetProvider) closeAllConnections() {
|
func (pp *proxySetProvider) closeAllConnections() {
|
||||||
statistic.DefaultManager.Range(func(c statistic.Tracker) bool {
|
statistic.DefaultManager.Range(func(c statistic.Tracker) bool {
|
||||||
for _, chain := range c.Chains() {
|
for _, chain := range c.ProviderChains() {
|
||||||
if chain == pp.Name() {
|
if chain == pp.Name() {
|
||||||
_ = c.Close()
|
_ = c.Close()
|
||||||
break
|
break
|
||||||
@@ -161,11 +180,7 @@ func (pp *proxySetProvider) Close() error {
|
|||||||
return pp.Fetcher.Close()
|
return pp.Fetcher.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProxySetProvider(name string, interval time.Duration, parser resource.Parser[[]C.Proxy], vehicle types.Vehicle, hc *HealthCheck) (*ProxySetProvider, error) {
|
func NewProxySetProvider(name string, interval time.Duration, payload []map[string]any, parser resource.Parser[[]C.Proxy], vehicle P.Vehicle, hc *HealthCheck) (*ProxySetProvider, error) {
|
||||||
if hc.auto() {
|
|
||||||
go hc.process()
|
|
||||||
}
|
|
||||||
|
|
||||||
pd := &proxySetProvider{
|
pd := &proxySetProvider{
|
||||||
baseProvider: baseProvider{
|
baseProvider: baseProvider{
|
||||||
name: name,
|
name: name,
|
||||||
@@ -174,6 +189,21 @@ func NewProxySetProvider(name string, interval time.Duration, parser resource.Pa
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(payload) > 0 { // using as fallback proxies
|
||||||
|
ps := ProxySchema{Proxies: payload}
|
||||||
|
buf, err := yaml.Marshal(ps)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
proxies, err := parser(buf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
pd.proxies = proxies
|
||||||
|
// direct call setProxies on hc to avoid starting a health check process immediately, it should be done by Initial()
|
||||||
|
hc.setProxies(proxies)
|
||||||
|
}
|
||||||
|
|
||||||
fetcher := resource.NewFetcher[[]C.Proxy](name, interval, vehicle, parser, pd.setProxies)
|
fetcher := resource.NewFetcher[[]C.Proxy](name, interval, vehicle, parser, pd.setProxies)
|
||||||
pd.Fetcher = fetcher
|
pd.Fetcher = fetcher
|
||||||
if httpVehicle, ok := vehicle.(*resource.HTTPVehicle); ok {
|
if httpVehicle, ok := vehicle.(*resource.HTTPVehicle); ok {
|
||||||
@@ -217,12 +247,8 @@ func (ip *inlineProvider) MarshalJSON() ([]byte, error) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ip *inlineProvider) VehicleType() types.VehicleType {
|
func (ip *inlineProvider) VehicleType() P.VehicleType {
|
||||||
return types.Inline
|
return P.Inline
|
||||||
}
|
|
||||||
|
|
||||||
func (ip *inlineProvider) Initial() error {
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ip *inlineProvider) Update() error {
|
func (ip *inlineProvider) Update() error {
|
||||||
@@ -232,10 +258,6 @@ func (ip *inlineProvider) Update() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewInlineProvider(name string, payload []map[string]any, parser resource.Parser[[]C.Proxy], hc *HealthCheck) (*InlineProvider, error) {
|
func NewInlineProvider(name string, payload []map[string]any, parser resource.Parser[[]C.Proxy], hc *HealthCheck) (*InlineProvider, error) {
|
||||||
if hc.auto() {
|
|
||||||
go hc.process()
|
|
||||||
}
|
|
||||||
|
|
||||||
ps := ProxySchema{Proxies: payload}
|
ps := ProxySchema{Proxies: payload}
|
||||||
buf, err := yaml.Marshal(ps)
|
buf, err := yaml.Marshal(ps)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -245,6 +267,8 @@ func NewInlineProvider(name string, payload []map[string]any, parser resource.Pa
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
// direct call setProxies on hc to avoid starting a health check process immediately, it should be done by Initial()
|
||||||
|
hc.setProxies(proxies)
|
||||||
|
|
||||||
ip := &inlineProvider{
|
ip := &inlineProvider{
|
||||||
baseProvider: baseProvider{
|
baseProvider: baseProvider{
|
||||||
@@ -288,15 +312,8 @@ func (cp *compatibleProvider) Update() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cp *compatibleProvider) Initial() error {
|
func (cp *compatibleProvider) VehicleType() P.VehicleType {
|
||||||
if cp.healthCheck.interval != 0 && cp.healthCheck.url != "" {
|
return P.Compatible
|
||||||
cp.HealthCheck()
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cp *compatibleProvider) VehicleType() types.VehicleType {
|
|
||||||
return types.Compatible
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCompatibleProvider(name string, proxies []C.Proxy, hc *HealthCheck) (*CompatibleProvider, error) {
|
func NewCompatibleProvider(name string, proxies []C.Proxy, hc *HealthCheck) (*CompatibleProvider, error) {
|
||||||
@@ -304,10 +321,6 @@ func NewCompatibleProvider(name string, proxies []C.Proxy, hc *HealthCheck) (*Co
|
|||||||
return nil, errors.New("provider need one proxy at least")
|
return nil, errors.New("provider need one proxy at least")
|
||||||
}
|
}
|
||||||
|
|
||||||
if hc.auto() {
|
|
||||||
go hc.process()
|
|
||||||
}
|
|
||||||
|
|
||||||
pd := &compatibleProvider{
|
pd := &compatibleProvider{
|
||||||
baseProvider: baseProvider{
|
baseProvider: baseProvider{
|
||||||
name: name,
|
name: name,
|
||||||
@@ -326,16 +339,23 @@ func (cp *CompatibleProvider) Close() error {
|
|||||||
return cp.compatibleProvider.Close()
|
return cp.compatibleProvider.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProxiesParser(filter string, excludeFilter string, excludeType string, dialerProxy string, override OverrideSchema) (resource.Parser[[]C.Proxy], error) {
|
func NewProxiesParser(pdName string, filter string, excludeFilter string, excludeType string, dialerProxy string, override overrideSchema) (resource.Parser[[]C.Proxy], error) {
|
||||||
excludeFilterReg, err := regexp2.Compile(excludeFilter, regexp2.None)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("invalid excludeFilter regex: %w", err)
|
|
||||||
}
|
|
||||||
var excludeTypeArray []string
|
var excludeTypeArray []string
|
||||||
if excludeType != "" {
|
if excludeType != "" {
|
||||||
excludeTypeArray = strings.Split(excludeType, "|")
|
excludeTypeArray = strings.Split(excludeType, "|")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var excludeFilterRegs []*regexp2.Regexp
|
||||||
|
if excludeFilter != "" {
|
||||||
|
for _, excludeFilter := range strings.Split(excludeFilter, "`") {
|
||||||
|
excludeFilterReg, err := regexp2.Compile(excludeFilter, regexp2.None)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid excludeFilter regex: %w", err)
|
||||||
|
}
|
||||||
|
excludeFilterRegs = append(excludeFilterRegs, excludeFilterReg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var filterRegs []*regexp2.Regexp
|
var filterRegs []*regexp2.Regexp
|
||||||
for _, filter := range strings.Split(filter, "`") {
|
for _, filter := range strings.Split(filter, "`") {
|
||||||
filterReg, err := regexp2.Compile(filter, regexp2.None)
|
filterReg, err := regexp2.Compile(filter, regexp2.None)
|
||||||
@@ -363,8 +383,9 @@ func NewProxiesParser(filter string, excludeFilter string, excludeType string, d
|
|||||||
proxies := []C.Proxy{}
|
proxies := []C.Proxy{}
|
||||||
proxiesSet := map[string]struct{}{}
|
proxiesSet := map[string]struct{}{}
|
||||||
for _, filterReg := range filterRegs {
|
for _, filterReg := range filterRegs {
|
||||||
|
LOOP1:
|
||||||
for idx, mapping := range schema.Proxies {
|
for idx, mapping := range schema.Proxies {
|
||||||
if nil != excludeTypeArray && len(excludeTypeArray) > 0 {
|
if len(excludeTypeArray) > 0 {
|
||||||
mType, ok := mapping["type"]
|
mType, ok := mapping["type"]
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
@@ -373,18 +394,11 @@ func NewProxiesParser(filter string, excludeFilter string, excludeType string, d
|
|||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
flag := false
|
for _, excludeType := range excludeTypeArray {
|
||||||
for i := range excludeTypeArray {
|
if strings.EqualFold(pType, excludeType) {
|
||||||
if strings.EqualFold(pType, excludeTypeArray[i]) {
|
continue LOOP1
|
||||||
flag = true
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
if flag {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
mName, ok := mapping["name"]
|
mName, ok := mapping["name"]
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -394,9 +408,11 @@ func NewProxiesParser(filter string, excludeFilter string, excludeType string, d
|
|||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if len(excludeFilter) > 0 {
|
if len(excludeFilterRegs) > 0 {
|
||||||
if mat, _ := excludeFilterReg.MatchString(name); mat {
|
for _, excludeFilterReg := range excludeFilterRegs {
|
||||||
continue
|
if mat, _ := excludeFilterReg.MatchString(name); mat {
|
||||||
|
continue LOOP1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(filter) > 0 {
|
if len(filter) > 0 {
|
||||||
@@ -412,36 +428,12 @@ func NewProxiesParser(filter string, excludeFilter string, excludeType string, d
|
|||||||
mapping["dialer-proxy"] = dialerProxy
|
mapping["dialer-proxy"] = dialerProxy
|
||||||
}
|
}
|
||||||
|
|
||||||
val := reflect.ValueOf(override)
|
err := override.Apply(mapping)
|
||||||
for i := 0; i < val.NumField(); i++ {
|
if err != nil {
|
||||||
field := val.Field(i)
|
return nil, fmt.Errorf("proxy %d override error: %w", idx, err)
|
||||||
if field.IsNil() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
fieldName := strings.Split(val.Type().Field(i).Tag.Get("provider"), ",")[0]
|
|
||||||
switch fieldName {
|
|
||||||
case "additional-prefix":
|
|
||||||
name := mapping["name"].(string)
|
|
||||||
mapping["name"] = *field.Interface().(*string) + name
|
|
||||||
case "additional-suffix":
|
|
||||||
name := mapping["name"].(string)
|
|
||||||
mapping["name"] = name + *field.Interface().(*string)
|
|
||||||
case "proxy-name":
|
|
||||||
// Iterate through all naming replacement rules and perform the replacements.
|
|
||||||
for _, expr := range override.ProxyName {
|
|
||||||
name := mapping["name"].(string)
|
|
||||||
newName, err := expr.Pattern.Replace(name, expr.Target, 0, -1)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("proxy name replace error: %w", err)
|
|
||||||
}
|
|
||||||
mapping["name"] = newName
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
mapping[fieldName] = field.Elem().Interface()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
proxy, err := adapter.ParseProxy(mapping)
|
proxy, err := adapter.ParseProxy(mapping, adapter.WithProviderName(pdName))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("proxy %d error: %w", idx, err)
|
return nil, fmt.Errorf("proxy %d error: %w", idx, err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
// Copyright 2014 The Go Authors. All rights reserved.
|
// Copyright 2014 The Go Authors. All rights reserved.
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
//go:build android && cgo
|
||||||
|
// +build android,cgo
|
||||||
|
|
||||||
// kanged from https://github.com/golang/mobile/blob/c713f31d574bb632a93f169b2cc99c9e753fef0e/app/android.go#L89
|
// kanged from https://github.com/golang/mobile/blob/c713f31d574bb632a93f169b2cc99c9e753fef0e/app/android.go#L89
|
||||||
|
|
||||||
|
|||||||
63
common/atomic/enum.go
Normal file
63
common/atomic/enum.go
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
package atomic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"sync/atomic"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Int32Enum[T ~int32] struct {
|
||||||
|
value atomic.Int32
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Int32Enum[T]) MarshalJSON() ([]byte, error) {
|
||||||
|
return json.Marshal(i.Load())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Int32Enum[T]) UnmarshalJSON(b []byte) error {
|
||||||
|
var v T
|
||||||
|
if err := json.Unmarshal(b, &v); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
i.Store(v)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Int32Enum[T]) MarshalYAML() (any, error) {
|
||||||
|
return i.Load(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Int32Enum[T]) UnmarshalYAML(unmarshal func(any) error) error {
|
||||||
|
var v T
|
||||||
|
if err := unmarshal(&v); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
i.Store(v)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Int32Enum[T]) String() string {
|
||||||
|
return fmt.Sprint(i.Load())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Int32Enum[T]) Store(v T) {
|
||||||
|
i.value.Store(int32(v))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Int32Enum[T]) Load() T {
|
||||||
|
return T(i.value.Load())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Int32Enum[T]) Swap(new T) T {
|
||||||
|
return T(i.value.Swap(int32(new)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Int32Enum[T]) CompareAndSwap(old, new T) bool {
|
||||||
|
return i.value.CompareAndSwap(int32(old), int32(new))
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewInt32Enum[T ~int32](v T) *Int32Enum[T] {
|
||||||
|
a := &Int32Enum[T]{}
|
||||||
|
a.Store(v)
|
||||||
|
return a
|
||||||
|
}
|
||||||
@@ -29,6 +29,19 @@ func (i *Bool) UnmarshalJSON(b []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *Bool) MarshalYAML() (any, error) {
|
||||||
|
return i.Load(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Bool) UnmarshalYAML(unmarshal func(any) error) error {
|
||||||
|
var v bool
|
||||||
|
if err := unmarshal(&v); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
i.Store(v)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (i *Bool) String() string {
|
func (i *Bool) String() string {
|
||||||
v := i.Load()
|
v := i.Load()
|
||||||
return strconv.FormatBool(v)
|
return strconv.FormatBool(v)
|
||||||
@@ -58,6 +71,19 @@ func (p *Pointer[T]) UnmarshalJSON(b []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Pointer[T]) MarshalYAML() (any, error) {
|
||||||
|
return p.Load(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Pointer[T]) UnmarshalYAML(unmarshal func(any) error) error {
|
||||||
|
var v *T
|
||||||
|
if err := unmarshal(&v); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
p.Store(v)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Pointer[T]) String() string {
|
func (p *Pointer[T]) String() string {
|
||||||
return fmt.Sprint(p.Load())
|
return fmt.Sprint(p.Load())
|
||||||
}
|
}
|
||||||
@@ -84,6 +110,19 @@ func (i *Int32) UnmarshalJSON(b []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *Int32) MarshalYAML() (any, error) {
|
||||||
|
return i.Load(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Int32) UnmarshalYAML(unmarshal func(any) error) error {
|
||||||
|
var v int32
|
||||||
|
if err := unmarshal(&v); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
i.Store(v)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (i *Int32) String() string {
|
func (i *Int32) String() string {
|
||||||
v := i.Load()
|
v := i.Load()
|
||||||
return strconv.FormatInt(int64(v), 10)
|
return strconv.FormatInt(int64(v), 10)
|
||||||
@@ -111,6 +150,19 @@ func (i *Int64) UnmarshalJSON(b []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *Int64) MarshalYAML() (any, error) {
|
||||||
|
return i.Load(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Int64) UnmarshalYAML(unmarshal func(any) error) error {
|
||||||
|
var v int64
|
||||||
|
if err := unmarshal(&v); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
i.Store(v)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (i *Int64) String() string {
|
func (i *Int64) String() string {
|
||||||
v := i.Load()
|
v := i.Load()
|
||||||
return strconv.FormatInt(int64(v), 10)
|
return strconv.FormatInt(int64(v), 10)
|
||||||
@@ -138,6 +190,19 @@ func (i *Uint32) UnmarshalJSON(b []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *Uint32) MarshalYAML() (any, error) {
|
||||||
|
return i.Load(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Uint32) UnmarshalYAML(unmarshal func(any) error) error {
|
||||||
|
var v uint32
|
||||||
|
if err := unmarshal(&v); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
i.Store(v)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (i *Uint32) String() string {
|
func (i *Uint32) String() string {
|
||||||
v := i.Load()
|
v := i.Load()
|
||||||
return strconv.FormatUint(uint64(v), 10)
|
return strconv.FormatUint(uint64(v), 10)
|
||||||
@@ -165,6 +230,19 @@ func (i *Uint64) UnmarshalJSON(b []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *Uint64) MarshalYAML() (any, error) {
|
||||||
|
return i.Load(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Uint64) UnmarshalYAML(unmarshal func(any) error) error {
|
||||||
|
var v uint64
|
||||||
|
if err := unmarshal(&v); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
i.Store(v)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (i *Uint64) String() string {
|
func (i *Uint64) String() string {
|
||||||
v := i.Load()
|
v := i.Load()
|
||||||
return strconv.FormatUint(uint64(v), 10)
|
return strconv.FormatUint(uint64(v), 10)
|
||||||
@@ -192,6 +270,19 @@ func (i *Uintptr) UnmarshalJSON(b []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *Uintptr) MarshalYAML() (any, error) {
|
||||||
|
return i.Load(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Uintptr) UnmarshalYAML(unmarshal func(any) error) error {
|
||||||
|
var v uintptr
|
||||||
|
if err := unmarshal(&v); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
i.Store(v)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (i *Uintptr) String() string {
|
func (i *Uintptr) String() string {
|
||||||
v := i.Load()
|
v := i.Load()
|
||||||
return strconv.FormatUint(uint64(v), 10)
|
return strconv.FormatUint(uint64(v), 10)
|
||||||
|
|||||||
@@ -5,49 +5,50 @@ import (
|
|||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
func DefaultValue[T any]() T {
|
|
||||||
var defaultValue T
|
|
||||||
return defaultValue
|
|
||||||
}
|
|
||||||
|
|
||||||
type TypedValue[T any] struct {
|
type TypedValue[T any] struct {
|
||||||
_ noCopy
|
value atomic.Pointer[T]
|
||||||
value atomic.Value
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// tValue is a struct with determined type to resolve atomic.Value usages with interface types
|
func (t *TypedValue[T]) Load() (v T) {
|
||||||
// https://github.com/golang/go/issues/22550
|
v, _ = t.LoadOk()
|
||||||
//
|
return
|
||||||
// The intention to have an atomic value store for errors. However, running this code panics:
|
|
||||||
// panic: sync/atomic: store of inconsistently typed value into Value
|
|
||||||
// This is because atomic.Value requires that the underlying concrete type be the same (which is a reasonable expectation for its implementation).
|
|
||||||
// When going through the atomic.Value.Store method call, the fact that both these are of the error interface is lost.
|
|
||||||
type tValue[T any] struct {
|
|
||||||
value T
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TypedValue[T]) Load() T {
|
func (t *TypedValue[T]) LoadOk() (v T, ok bool) {
|
||||||
value := t.value.Load()
|
value := t.value.Load()
|
||||||
if value == nil {
|
if value == nil {
|
||||||
return DefaultValue[T]()
|
return
|
||||||
}
|
}
|
||||||
return value.(tValue[T]).value
|
return *value, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TypedValue[T]) Store(value T) {
|
func (t *TypedValue[T]) Store(value T) {
|
||||||
t.value.Store(tValue[T]{value})
|
t.value.Store(&value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TypedValue[T]) Swap(new T) T {
|
func (t *TypedValue[T]) Swap(new T) (v T) {
|
||||||
old := t.value.Swap(tValue[T]{new})
|
old := t.value.Swap(&new)
|
||||||
if old == nil {
|
if old == nil {
|
||||||
return DefaultValue[T]()
|
return
|
||||||
}
|
}
|
||||||
return old.(tValue[T]).value
|
return *old
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TypedValue[T]) CompareAndSwap(old, new T) bool {
|
func (t *TypedValue[T]) CompareAndSwap(old, new T) bool {
|
||||||
return t.value.CompareAndSwap(tValue[T]{old}, tValue[T]{new})
|
for {
|
||||||
|
currentP := t.value.Load()
|
||||||
|
var currentValue T
|
||||||
|
if currentP != nil {
|
||||||
|
currentValue = *currentP
|
||||||
|
}
|
||||||
|
// Compare old and current via runtime equality check.
|
||||||
|
if any(currentValue) != any(old) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if t.value.CompareAndSwap(currentP, &new) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TypedValue[T]) MarshalJSON() ([]byte, error) {
|
func (t *TypedValue[T]) MarshalJSON() ([]byte, error) {
|
||||||
@@ -63,13 +64,20 @@ func (t *TypedValue[T]) UnmarshalJSON(b []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *TypedValue[T]) MarshalYAML() (any, error) {
|
||||||
|
return t.Load(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TypedValue[T]) UnmarshalYAML(unmarshal func(any) error) error {
|
||||||
|
var v T
|
||||||
|
if err := unmarshal(&v); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
t.Store(v)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func NewTypedValue[T any](t T) (v TypedValue[T]) {
|
func NewTypedValue[T any](t T) (v TypedValue[T]) {
|
||||||
v.Store(t)
|
v.Store(t)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type noCopy struct{}
|
|
||||||
|
|
||||||
// Lock is a no-op used by -copylocks checker from `go vet`.
|
|
||||||
func (*noCopy) Lock() {}
|
|
||||||
func (*noCopy) Unlock() {}
|
|
||||||
|
|||||||
169
common/atomic/value_test.go
Normal file
169
common/atomic/value_test.go
Normal file
@@ -0,0 +1,169 @@
|
|||||||
|
package atomic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTypedValue(t *testing.T) {
|
||||||
|
{
|
||||||
|
var v TypedValue[int]
|
||||||
|
got, gotOk := v.LoadOk()
|
||||||
|
if got != 0 || gotOk {
|
||||||
|
t.Fatalf("LoadOk = (%v, %v), want (0, false)", got, gotOk)
|
||||||
|
}
|
||||||
|
v.Store(1)
|
||||||
|
got, gotOk = v.LoadOk()
|
||||||
|
if got != 1 || !gotOk {
|
||||||
|
t.Fatalf("LoadOk = (%v, %v), want (1, true)", got, gotOk)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
var v TypedValue[error]
|
||||||
|
got, gotOk := v.LoadOk()
|
||||||
|
if got != nil || gotOk {
|
||||||
|
t.Fatalf("LoadOk = (%v, %v), want (nil, false)", got, gotOk)
|
||||||
|
}
|
||||||
|
v.Store(io.EOF)
|
||||||
|
got, gotOk = v.LoadOk()
|
||||||
|
if got != io.EOF || !gotOk {
|
||||||
|
t.Fatalf("LoadOk = (%v, %v), want (EOF, true)", got, gotOk)
|
||||||
|
}
|
||||||
|
err := &os.PathError{}
|
||||||
|
v.Store(err)
|
||||||
|
got, gotOk = v.LoadOk()
|
||||||
|
if got != err || !gotOk {
|
||||||
|
t.Fatalf("LoadOk = (%v, %v), want (%v, true)", got, gotOk, err)
|
||||||
|
}
|
||||||
|
v.Store(nil)
|
||||||
|
got, gotOk = v.LoadOk()
|
||||||
|
if got != nil || !gotOk {
|
||||||
|
t.Fatalf("LoadOk = (%v, %v), want (nil, true)", got, gotOk)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
e1, e2, e3 := io.EOF, &os.PathError{}, &os.PathError{}
|
||||||
|
var v TypedValue[error]
|
||||||
|
if v.CompareAndSwap(e1, e2) != false {
|
||||||
|
t.Fatalf("CompareAndSwap = true, want false")
|
||||||
|
}
|
||||||
|
if value := v.Load(); value != nil {
|
||||||
|
t.Fatalf("Load = (%v), want (%v)", value, nil)
|
||||||
|
}
|
||||||
|
if v.CompareAndSwap(nil, e1) != true {
|
||||||
|
t.Fatalf("CompareAndSwap = false, want true")
|
||||||
|
}
|
||||||
|
if value := v.Load(); value != e1 {
|
||||||
|
t.Fatalf("Load = (%v), want (%v)", value, e1)
|
||||||
|
}
|
||||||
|
if v.CompareAndSwap(e2, e3) != false {
|
||||||
|
t.Fatalf("CompareAndSwap = true, want false")
|
||||||
|
}
|
||||||
|
if value := v.Load(); value != e1 {
|
||||||
|
t.Fatalf("Load = (%v), want (%v)", value, e1)
|
||||||
|
}
|
||||||
|
if v.CompareAndSwap(e1, e2) != true {
|
||||||
|
t.Fatalf("CompareAndSwap = false, want true")
|
||||||
|
}
|
||||||
|
if value := v.Load(); value != e2 {
|
||||||
|
t.Fatalf("Load = (%v), want (%v)", value, e2)
|
||||||
|
}
|
||||||
|
if v.CompareAndSwap(e3, e2) != false {
|
||||||
|
t.Fatalf("CompareAndSwap = true, want false")
|
||||||
|
}
|
||||||
|
if value := v.Load(); value != e2 {
|
||||||
|
t.Fatalf("Load = (%v), want (%v)", value, e2)
|
||||||
|
}
|
||||||
|
if v.CompareAndSwap(nil, e3) != false {
|
||||||
|
t.Fatalf("CompareAndSwap = true, want false")
|
||||||
|
}
|
||||||
|
if value := v.Load(); value != e2 {
|
||||||
|
t.Fatalf("Load = (%v), want (%v)", value, e2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
c1, c2, c3 := make(chan struct{}), make(chan struct{}), make(chan struct{})
|
||||||
|
var v TypedValue[chan struct{}]
|
||||||
|
if v.CompareAndSwap(c1, c2) != false {
|
||||||
|
t.Fatalf("CompareAndSwap = true, want false")
|
||||||
|
}
|
||||||
|
if value := v.Load(); value != nil {
|
||||||
|
t.Fatalf("Load = (%v), want (%v)", value, nil)
|
||||||
|
}
|
||||||
|
if v.CompareAndSwap(nil, c1) != true {
|
||||||
|
t.Fatalf("CompareAndSwap = false, want true")
|
||||||
|
}
|
||||||
|
if value := v.Load(); value != c1 {
|
||||||
|
t.Fatalf("Load = (%v), want (%v)", value, c1)
|
||||||
|
}
|
||||||
|
if v.CompareAndSwap(c2, c3) != false {
|
||||||
|
t.Fatalf("CompareAndSwap = true, want false")
|
||||||
|
}
|
||||||
|
if value := v.Load(); value != c1 {
|
||||||
|
t.Fatalf("Load = (%v), want (%v)", value, c1)
|
||||||
|
}
|
||||||
|
if v.CompareAndSwap(c1, c2) != true {
|
||||||
|
t.Fatalf("CompareAndSwap = false, want true")
|
||||||
|
}
|
||||||
|
if value := v.Load(); value != c2 {
|
||||||
|
t.Fatalf("Load = (%v), want (%v)", value, c2)
|
||||||
|
}
|
||||||
|
if v.CompareAndSwap(c3, c2) != false {
|
||||||
|
t.Fatalf("CompareAndSwap = true, want false")
|
||||||
|
}
|
||||||
|
if value := v.Load(); value != c2 {
|
||||||
|
t.Fatalf("Load = (%v), want (%v)", value, c2)
|
||||||
|
}
|
||||||
|
if v.CompareAndSwap(nil, c3) != false {
|
||||||
|
t.Fatalf("CompareAndSwap = true, want false")
|
||||||
|
}
|
||||||
|
if value := v.Load(); value != c2 {
|
||||||
|
t.Fatalf("Load = (%v), want (%v)", value, c2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
c1, c2, c3 := &io.LimitedReader{}, &io.SectionReader{}, &io.SectionReader{}
|
||||||
|
var v TypedValue[io.Reader]
|
||||||
|
if v.CompareAndSwap(c1, c2) != false {
|
||||||
|
t.Fatalf("CompareAndSwap = true, want false")
|
||||||
|
}
|
||||||
|
if value := v.Load(); value != nil {
|
||||||
|
t.Fatalf("Load = (%v), want (%v)", value, nil)
|
||||||
|
}
|
||||||
|
if v.CompareAndSwap(nil, c1) != true {
|
||||||
|
t.Fatalf("CompareAndSwap = false, want true")
|
||||||
|
}
|
||||||
|
if value := v.Load(); value != c1 {
|
||||||
|
t.Fatalf("Load = (%v), want (%v)", value, c1)
|
||||||
|
}
|
||||||
|
if v.CompareAndSwap(c2, c3) != false {
|
||||||
|
t.Fatalf("CompareAndSwap = true, want false")
|
||||||
|
}
|
||||||
|
if value := v.Load(); value != c1 {
|
||||||
|
t.Fatalf("Load = (%v), want (%v)", value, c1)
|
||||||
|
}
|
||||||
|
if v.CompareAndSwap(c1, c2) != true {
|
||||||
|
t.Fatalf("CompareAndSwap = false, want true")
|
||||||
|
}
|
||||||
|
if value := v.Load(); value != c2 {
|
||||||
|
t.Fatalf("Load = (%v), want (%v)", value, c2)
|
||||||
|
}
|
||||||
|
if v.CompareAndSwap(c3, c2) != false {
|
||||||
|
t.Fatalf("CompareAndSwap = true, want false")
|
||||||
|
}
|
||||||
|
if value := v.Load(); value != c2 {
|
||||||
|
t.Fatalf("Load = (%v), want (%v)", value, c2)
|
||||||
|
}
|
||||||
|
if v.CompareAndSwap(nil, c3) != false {
|
||||||
|
t.Fatalf("CompareAndSwap = true, want false")
|
||||||
|
}
|
||||||
|
if value := v.Load(); value != c2 {
|
||||||
|
t.Fatalf("Load = (%v), want (%v)", value, c2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,21 +1,59 @@
|
|||||||
package buf
|
package buf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/sagernet/sing/common"
|
"github.com/metacubex/sing/common/buf"
|
||||||
"github.com/sagernet/sing/common/buf"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const BufferSize = buf.BufferSize
|
const BufferSize = buf.BufferSize
|
||||||
|
|
||||||
type Buffer = buf.Buffer
|
type Buffer = buf.Buffer
|
||||||
|
|
||||||
var New = buf.New
|
func New() *Buffer {
|
||||||
var NewPacket = buf.NewPacket
|
return buf.New()
|
||||||
var NewSize = buf.NewSize
|
}
|
||||||
var With = buf.With
|
|
||||||
var As = buf.As
|
|
||||||
|
|
||||||
var (
|
func NewPacket() *Buffer {
|
||||||
Must = common.Must
|
return buf.NewPacket()
|
||||||
Error = common.Error
|
}
|
||||||
)
|
|
||||||
|
func NewSize(size int) *Buffer {
|
||||||
|
return buf.NewSize(size)
|
||||||
|
}
|
||||||
|
|
||||||
|
func With(data []byte) *Buffer {
|
||||||
|
return buf.With(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func As(data []byte) *Buffer {
|
||||||
|
return buf.As(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReleaseMulti(buffers []*Buffer) {
|
||||||
|
buf.ReleaseMulti(buffers)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Error(_ any, err error) error {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func Must(errs ...error) {
|
||||||
|
for _, err := range errs {
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Must1[T any](result T, err error) T {
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func Must2[T any, T2 any](result T, result2 T2, err error) (T, T2) {
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return result, result2
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package convert
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -43,3 +44,22 @@ func decodeUrlSafe(data string) string {
|
|||||||
}
|
}
|
||||||
return string(dcBuf)
|
return string(dcBuf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TryDecodeBase64(s string) (decoded []byte, err error) {
|
||||||
|
if len(s)%4 == 0 {
|
||||||
|
if decoded, err = base64.StdEncoding.DecodeString(s); err == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if decoded, err = base64.URLEncoding.DecodeString(s); err == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if decoded, err = base64.RawStdEncoding.DecodeString(s); err == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if decoded, err = base64.RawURLEncoding.DecodeString(s); err == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("invalid base64-encoded string")
|
||||||
|
}
|
||||||
|
|||||||
@@ -201,6 +201,10 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|||||||
trojan["client-fingerprint"] = fingerprint
|
trojan["client-fingerprint"] = fingerprint
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if pcs := query.Get("pcs"); pcs != "" {
|
||||||
|
trojan["fingerprint"] = pcs
|
||||||
|
}
|
||||||
|
|
||||||
proxies = append(proxies, trojan)
|
proxies = append(proxies, trojan)
|
||||||
|
|
||||||
case "vless":
|
case "vless":
|
||||||
@@ -208,6 +212,9 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if decodedHost, err := tryDecodeBase64([]byte(urlVLess.Host)); err == nil {
|
||||||
|
urlVLess.Host = string(decodedHost)
|
||||||
|
}
|
||||||
query := urlVLess.Query()
|
query := urlVLess.Query()
|
||||||
vless := make(map[string]any, 20)
|
vless := make(map[string]any, 20)
|
||||||
err = handleVShareLink(names, urlVLess, scheme, vless)
|
err = handleVShareLink(names, urlVLess, scheme, vless)
|
||||||
@@ -218,6 +225,9 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|||||||
if flow := query.Get("flow"); flow != "" {
|
if flow := query.Get("flow"); flow != "" {
|
||||||
vless["flow"] = strings.ToLower(flow)
|
vless["flow"] = strings.ToLower(flow)
|
||||||
}
|
}
|
||||||
|
if encryption := query.Get("encryption"); encryption != "" {
|
||||||
|
vless["encryption"] = encryption
|
||||||
|
}
|
||||||
proxies = append(proxies, vless)
|
proxies = append(proxies, vless)
|
||||||
|
|
||||||
case "vmess":
|
case "vmess":
|
||||||
@@ -456,12 +466,12 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|||||||
proxies = append(proxies, ss)
|
proxies = append(proxies, ss)
|
||||||
|
|
||||||
case "ssr":
|
case "ssr":
|
||||||
dcBuf, err := encRaw.DecodeString(body)
|
dcBuf, err := TryDecodeBase64(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// ssr://host:port:protocol:method:obfs:urlsafebase64pass/?obfsparam=urlsafebase64&protoparam=&remarks=urlsafebase64&group=urlsafebase64&udpport=0&uot=1
|
// ssr://host:port:protocol:method:obfs:urlsafebase64pass/?obfsparam=urlsafebase64param&protoparam=urlsafebase64param&remarks=urlsafebase64remarks&group=urlsafebase64group&udpport=0&uot=1
|
||||||
|
|
||||||
before, after, ok := strings.Cut(string(dcBuf), "/?")
|
before, after, ok := strings.Cut(string(dcBuf), "/?")
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -490,7 +500,7 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|||||||
name := uniqueName(names, remarks)
|
name := uniqueName(names, remarks)
|
||||||
|
|
||||||
obfsParam := decodeUrlSafe(query.Get("obfsparam"))
|
obfsParam := decodeUrlSafe(query.Get("obfsparam"))
|
||||||
protocolParam := query.Get("protoparam")
|
protocolParam := decodeUrlSafe(query.Get("protoparam"))
|
||||||
|
|
||||||
ssr := make(map[string]any, 20)
|
ssr := make(map[string]any, 20)
|
||||||
|
|
||||||
@@ -513,6 +523,101 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
proxies = append(proxies, ssr)
|
proxies = append(proxies, ssr)
|
||||||
|
|
||||||
|
case "socks", "socks5", "socks5h", "http", "https":
|
||||||
|
link, err := url.Parse(line)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
server := link.Hostname()
|
||||||
|
if server == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
portStr := link.Port()
|
||||||
|
if portStr == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
remarks := link.Fragment
|
||||||
|
if remarks == "" {
|
||||||
|
remarks = fmt.Sprintf("%s:%s", server, portStr)
|
||||||
|
}
|
||||||
|
name := uniqueName(names, remarks)
|
||||||
|
encodeStr := link.User.String()
|
||||||
|
var username, password string
|
||||||
|
if encodeStr != "" {
|
||||||
|
decodeStr := string(DecodeBase64([]byte(encodeStr)))
|
||||||
|
splitStr := strings.Split(decodeStr, ":")
|
||||||
|
|
||||||
|
// todo: should use url.QueryUnescape ?
|
||||||
|
username = splitStr[0]
|
||||||
|
if len(splitStr) == 2 {
|
||||||
|
password = splitStr[1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
socks := make(map[string]any, 10)
|
||||||
|
socks["name"] = name
|
||||||
|
socks["type"] = func() string {
|
||||||
|
switch scheme {
|
||||||
|
case "socks", "socks5", "socks5h":
|
||||||
|
return "socks5"
|
||||||
|
case "http", "https":
|
||||||
|
return "http"
|
||||||
|
}
|
||||||
|
return scheme
|
||||||
|
}()
|
||||||
|
socks["server"] = server
|
||||||
|
socks["port"] = portStr
|
||||||
|
socks["username"] = username
|
||||||
|
socks["password"] = password
|
||||||
|
socks["skip-cert-verify"] = true
|
||||||
|
if scheme == "https" {
|
||||||
|
socks["tls"] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
proxies = append(proxies, socks)
|
||||||
|
|
||||||
|
case "anytls":
|
||||||
|
// https://github.com/anytls/anytls-go/blob/main/docs/uri_scheme.md
|
||||||
|
link, err := url.Parse(line)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
username := link.User.Username()
|
||||||
|
password, exist := link.User.Password()
|
||||||
|
if !exist {
|
||||||
|
password = username
|
||||||
|
}
|
||||||
|
query := link.Query()
|
||||||
|
server := link.Hostname()
|
||||||
|
if server == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
portStr := link.Port()
|
||||||
|
if portStr == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
insecure, sni := query.Get("insecure"), query.Get("sni")
|
||||||
|
insecureBool := insecure == "1"
|
||||||
|
fingerprint := query.Get("hpkp")
|
||||||
|
|
||||||
|
remarks := link.Fragment
|
||||||
|
if remarks == "" {
|
||||||
|
remarks = fmt.Sprintf("%s:%s", server, portStr)
|
||||||
|
}
|
||||||
|
name := uniqueName(names, remarks)
|
||||||
|
anytls := make(map[string]any, 10)
|
||||||
|
anytls["name"] = name
|
||||||
|
anytls["type"] = "anytls"
|
||||||
|
anytls["server"] = server
|
||||||
|
anytls["port"] = portStr
|
||||||
|
anytls["username"] = username
|
||||||
|
anytls["password"] = password
|
||||||
|
anytls["sni"] = sni
|
||||||
|
anytls["fingerprint"] = fingerprint
|
||||||
|
anytls["skip-cert-verify"] = insecureBool
|
||||||
|
anytls["udp"] = true
|
||||||
|
|
||||||
|
proxies = append(proxies, anytls)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,12 +2,12 @@ package convert
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"net/http"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
|
|
||||||
|
"github.com/metacubex/http"
|
||||||
"github.com/metacubex/randv2"
|
"github.com/metacubex/randv2"
|
||||||
"github.com/metacubex/sing-shadowsocks/shadowimpl"
|
"github.com/metacubex/sing-shadowsocks/shadowimpl"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -35,6 +35,9 @@ func handleVShareLink(names map[string]int, url *url.URL, scheme string, proxy m
|
|||||||
if alpn := query.Get("alpn"); alpn != "" {
|
if alpn := query.Get("alpn"); alpn != "" {
|
||||||
proxy["alpn"] = strings.Split(alpn, ",")
|
proxy["alpn"] = strings.Split(alpn, ",")
|
||||||
}
|
}
|
||||||
|
if pcs := query.Get("pcs"); pcs != "" {
|
||||||
|
proxy["fingerprint"] = pcs
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if sni := query.Get("sni"); sni != "" {
|
if sni := query.Get("sni"); sni != "" {
|
||||||
proxy["servername"] = sni
|
proxy["servername"] = sni
|
||||||
|
|||||||
674
common/deque/deque.go
Normal file
674
common/deque/deque.go
Normal file
@@ -0,0 +1,674 @@
|
|||||||
|
package deque
|
||||||
|
|
||||||
|
// copy and modified from https://github.com/gammazero/deque/blob/v1.2.0/deque.go
|
||||||
|
// which is licensed under MIT.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// minCapacity is the smallest capacity that deque may have. Must be power of 2
|
||||||
|
// for bitwise modulus: x % n == x & (n - 1).
|
||||||
|
const minCapacity = 8
|
||||||
|
|
||||||
|
// Deque represents a single instance of the deque data structure. A Deque
|
||||||
|
// instance contains items of the type specified by the type argument.
|
||||||
|
//
|
||||||
|
// For example, to create a Deque that contains strings do one of the
|
||||||
|
// following:
|
||||||
|
//
|
||||||
|
// var stringDeque deque.Deque[string]
|
||||||
|
// stringDeque := new(deque.Deque[string])
|
||||||
|
// stringDeque := &deque.Deque[string]{}
|
||||||
|
//
|
||||||
|
// To create a Deque that will never resize to have space for less than 64
|
||||||
|
// items, specify a base capacity:
|
||||||
|
//
|
||||||
|
// var d deque.Deque[int]
|
||||||
|
// d.SetBaseCap(64)
|
||||||
|
//
|
||||||
|
// To ensure the Deque can store 1000 items without needing to resize while
|
||||||
|
// items are added:
|
||||||
|
//
|
||||||
|
// d.Grow(1000)
|
||||||
|
//
|
||||||
|
// Any values supplied to [SetBaseCap] and [Grow] are rounded up to the nearest
|
||||||
|
// power of 2, since the Deque grows by powers of 2.
|
||||||
|
type Deque[T any] struct {
|
||||||
|
buf []T
|
||||||
|
head int
|
||||||
|
tail int
|
||||||
|
count int
|
||||||
|
minCap int
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cap returns the current capacity of the Deque. If q is nil, q.Cap() is zero.
|
||||||
|
func (q *Deque[T]) Cap() int {
|
||||||
|
if q == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return len(q.buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Len returns the number of elements currently stored in the queue. If q is
|
||||||
|
// nil, q.Len() returns zero.
|
||||||
|
func (q *Deque[T]) Len() int {
|
||||||
|
if q == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return q.count
|
||||||
|
}
|
||||||
|
|
||||||
|
// PushBack appends an element to the back of the queue. Implements FIFO when
|
||||||
|
// elements are removed with [PopFront], and LIFO when elements are removed with
|
||||||
|
// [PopBack].
|
||||||
|
func (q *Deque[T]) PushBack(elem T) {
|
||||||
|
q.growIfFull()
|
||||||
|
|
||||||
|
q.buf[q.tail] = elem
|
||||||
|
// Calculate new tail position.
|
||||||
|
q.tail = q.next(q.tail)
|
||||||
|
q.count++
|
||||||
|
}
|
||||||
|
|
||||||
|
// PushFront prepends an element to the front of the queue.
|
||||||
|
func (q *Deque[T]) PushFront(elem T) {
|
||||||
|
q.growIfFull()
|
||||||
|
|
||||||
|
// Calculate new head position.
|
||||||
|
q.head = q.prev(q.head)
|
||||||
|
q.buf[q.head] = elem
|
||||||
|
q.count++
|
||||||
|
}
|
||||||
|
|
||||||
|
// PopFront removes and returns the element from the front of the queue.
|
||||||
|
// Implements FIFO when used with [PushBack]. If the queue is empty, the call
|
||||||
|
// panics.
|
||||||
|
func (q *Deque[T]) PopFront() T {
|
||||||
|
if q.count <= 0 {
|
||||||
|
panic("deque: PopFront() called on empty queue")
|
||||||
|
}
|
||||||
|
ret := q.buf[q.head]
|
||||||
|
var zero T
|
||||||
|
q.buf[q.head] = zero
|
||||||
|
// Calculate new head position.
|
||||||
|
q.head = q.next(q.head)
|
||||||
|
q.count--
|
||||||
|
|
||||||
|
q.shrinkIfExcess()
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
// IterPopFront returns an iterator that iteratively removes items from the
|
||||||
|
// front of the deque. This is more efficient than removing items one at a time
|
||||||
|
// because it avoids intermediate resizing. If a resize is necessary, only one
|
||||||
|
// is done when iteration ends.
|
||||||
|
func (q *Deque[T]) IterPopFront() func(yield func(T) bool) {
|
||||||
|
return func(yield func(T) bool) {
|
||||||
|
if q.Len() == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var zero T
|
||||||
|
for q.count != 0 {
|
||||||
|
ret := q.buf[q.head]
|
||||||
|
q.buf[q.head] = zero
|
||||||
|
q.head = q.next(q.head)
|
||||||
|
q.count--
|
||||||
|
if !yield(ret) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
q.shrinkToFit()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// PopBack removes and returns the element from the back of the queue.
|
||||||
|
// Implements LIFO when used with [PushBack]. If the queue is empty, the call
|
||||||
|
// panics.
|
||||||
|
func (q *Deque[T]) PopBack() T {
|
||||||
|
if q.count <= 0 {
|
||||||
|
panic("deque: PopBack() called on empty queue")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate new tail position
|
||||||
|
q.tail = q.prev(q.tail)
|
||||||
|
|
||||||
|
// Remove value at tail.
|
||||||
|
ret := q.buf[q.tail]
|
||||||
|
var zero T
|
||||||
|
q.buf[q.tail] = zero
|
||||||
|
q.count--
|
||||||
|
|
||||||
|
q.shrinkIfExcess()
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
// IterPopBack returns an iterator that iteratively removes items from the back
|
||||||
|
// of the deque. This is more efficient than removing items one at a time
|
||||||
|
// because it avoids intermediate resizing. If a resize is necessary, only one
|
||||||
|
// is done when iteration ends.
|
||||||
|
func (q *Deque[T]) IterPopBack() func(yield func(T) bool) {
|
||||||
|
return func(yield func(T) bool) {
|
||||||
|
if q.Len() == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var zero T
|
||||||
|
for q.count != 0 {
|
||||||
|
q.tail = q.prev(q.tail)
|
||||||
|
ret := q.buf[q.tail]
|
||||||
|
q.buf[q.tail] = zero
|
||||||
|
q.count--
|
||||||
|
if !yield(ret) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
q.shrinkToFit()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Front returns the element at the front of the queue. This is the element
|
||||||
|
// that would be returned by [PopFront]. This call panics if the queue is
|
||||||
|
// empty.
|
||||||
|
func (q *Deque[T]) Front() T {
|
||||||
|
if q.count <= 0 {
|
||||||
|
panic("deque: Front() called when empty")
|
||||||
|
}
|
||||||
|
return q.buf[q.head]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Back returns the element at the back of the queue. This is the element that
|
||||||
|
// would be returned by [PopBack]. This call panics if the queue is empty.
|
||||||
|
func (q *Deque[T]) Back() T {
|
||||||
|
if q.count <= 0 {
|
||||||
|
panic("deque: Back() called when empty")
|
||||||
|
}
|
||||||
|
return q.buf[q.prev(q.tail)]
|
||||||
|
}
|
||||||
|
|
||||||
|
// At returns the element at index i in the queue without removing the element
|
||||||
|
// from the queue. This method accepts only non-negative index values. At(0)
|
||||||
|
// refers to the first element and is the same as [Front]. At(Len()-1) refers
|
||||||
|
// to the last element and is the same as [Back]. If the index is invalid, the
|
||||||
|
// call panics.
|
||||||
|
//
|
||||||
|
// The purpose of At is to allow Deque to serve as a more general purpose
|
||||||
|
// circular buffer, where items are only added to and removed from the ends of
|
||||||
|
// the deque, but may be read from any place within the deque. Consider the
|
||||||
|
// case of a fixed-size circular log buffer: A new entry is pushed onto one end
|
||||||
|
// and when full the oldest is popped from the other end. All the log entries
|
||||||
|
// in the buffer must be readable without altering the buffer contents.
|
||||||
|
func (q *Deque[T]) At(i int) T {
|
||||||
|
q.checkRange(i)
|
||||||
|
// bitwise modulus
|
||||||
|
return q.buf[(q.head+i)&(len(q.buf)-1)]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set assigns the item to index i in the queue. Set indexes the deque the same
|
||||||
|
// as [At] but perform the opposite operation. If the index is invalid, the call
|
||||||
|
// panics.
|
||||||
|
func (q *Deque[T]) Set(i int, item T) {
|
||||||
|
q.checkRange(i)
|
||||||
|
// bitwise modulus
|
||||||
|
q.buf[(q.head+i)&(len(q.buf)-1)] = item
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iter returns a go iterator to range over all items in the Deque, yielding
|
||||||
|
// each item from front (index 0) to back (index Len()-1). Modification of
|
||||||
|
// Deque during iteration panics.
|
||||||
|
func (q *Deque[T]) Iter() func(yield func(T) bool) {
|
||||||
|
return func(yield func(T) bool) {
|
||||||
|
origHead := q.head
|
||||||
|
origTail := q.tail
|
||||||
|
head := origHead
|
||||||
|
for i := -0; i < q.Len(); i++ {
|
||||||
|
if q.head != origHead || q.tail != origTail {
|
||||||
|
panic("deque: modified during iteration")
|
||||||
|
}
|
||||||
|
if !yield(q.buf[head]) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
head = q.next(head)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// RIter returns a reverse go iterator to range over all items in the Deque,
|
||||||
|
// yielding each item from back (index Len()-1) to front (index 0).
|
||||||
|
// Modification of Deque during iteration panics.
|
||||||
|
func (q *Deque[T]) RIter() func(yield func(T) bool) {
|
||||||
|
return func(yield func(T) bool) {
|
||||||
|
origHead := q.head
|
||||||
|
origTail := q.tail
|
||||||
|
tail := origTail
|
||||||
|
for i := -0; i < q.Len(); i++ {
|
||||||
|
if q.head != origHead || q.tail != origTail {
|
||||||
|
panic("deque: modified during iteration")
|
||||||
|
}
|
||||||
|
tail = q.prev(tail)
|
||||||
|
if !yield(q.buf[tail]) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear removes all elements from the queue, but retains the current capacity.
|
||||||
|
// This is useful when repeatedly reusing the queue at high frequency to avoid
|
||||||
|
// GC during reuse. The queue will not be resized smaller as long as items are
|
||||||
|
// only added. Only when items are removed is the queue subject to getting
|
||||||
|
// resized smaller.
|
||||||
|
func (q *Deque[T]) Clear() {
|
||||||
|
if q.Len() == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
head, tail := q.head, q.tail
|
||||||
|
q.count = 0
|
||||||
|
q.head = 0
|
||||||
|
q.tail = 0
|
||||||
|
|
||||||
|
if head >= tail {
|
||||||
|
// [DEF....ABC]
|
||||||
|
clearSlice(q.buf[head:])
|
||||||
|
head = 0
|
||||||
|
}
|
||||||
|
clearSlice(q.buf[head:tail])
|
||||||
|
}
|
||||||
|
|
||||||
|
func clearSlice[S ~[]E, E any](s S) {
|
||||||
|
var zero E
|
||||||
|
for i := range s {
|
||||||
|
s[i] = zero
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Grow grows deque's capacity, if necessary, to guarantee space for another n
|
||||||
|
// items. After Grow(n), at least n items can be written to the deque without
|
||||||
|
// another allocation. If n is negative, Grow panics.
|
||||||
|
func (q *Deque[T]) Grow(n int) {
|
||||||
|
if n < 0 {
|
||||||
|
panic("deque.Grow: negative count")
|
||||||
|
}
|
||||||
|
c := q.Cap()
|
||||||
|
l := q.Len()
|
||||||
|
// If already big enough.
|
||||||
|
if n <= c-l {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if c == 0 {
|
||||||
|
c = minCapacity
|
||||||
|
}
|
||||||
|
|
||||||
|
newLen := l + n
|
||||||
|
for c < newLen {
|
||||||
|
c <<= 1
|
||||||
|
}
|
||||||
|
if l == 0 {
|
||||||
|
q.buf = make([]T, c)
|
||||||
|
q.head = 0
|
||||||
|
q.tail = 0
|
||||||
|
} else {
|
||||||
|
q.resize(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy copies the contents of the given src Deque into this Deque.
|
||||||
|
//
|
||||||
|
// n := b.Copy(a)
|
||||||
|
//
|
||||||
|
// is an efficient shortcut for
|
||||||
|
//
|
||||||
|
// b.Clear()
|
||||||
|
// n := a.Len()
|
||||||
|
// b.Grow(n)
|
||||||
|
// for i := 0; i < n; i++ {
|
||||||
|
// b.PushBack(a.At(i))
|
||||||
|
// }
|
||||||
|
func (q *Deque[T]) Copy(src Deque[T]) int {
|
||||||
|
q.Clear()
|
||||||
|
q.Grow(src.Len())
|
||||||
|
n := src.CopyOutSlice(q.buf)
|
||||||
|
q.count = n
|
||||||
|
q.tail = n
|
||||||
|
q.head = 0
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
// AppendToSlice appends from the Deque to the given slice. If the slice has
|
||||||
|
// insufficient capacity to store all elements in Deque, then allocate a new
|
||||||
|
// slice. Returns the resulting slice.
|
||||||
|
//
|
||||||
|
// out = q.AppendToSlice(out)
|
||||||
|
//
|
||||||
|
// is an efficient shortcut for
|
||||||
|
//
|
||||||
|
// for i := 0; i < q.Len(); i++ {
|
||||||
|
// x = append(out, q.At(i))
|
||||||
|
// }
|
||||||
|
func (q *Deque[T]) AppendToSlice(out []T) []T {
|
||||||
|
if q.count == 0 {
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
head, tail := q.head, q.tail
|
||||||
|
|
||||||
|
if head >= tail {
|
||||||
|
// [DEF....ABC]
|
||||||
|
out = append(out, q.buf[head:]...)
|
||||||
|
head = 0
|
||||||
|
}
|
||||||
|
return append(out, q.buf[head:tail]...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CopyInSlice replaces the contents of Deque with all the elements from the
|
||||||
|
// given slice, in. If len(in) is zero, then this is equivalent to calling
|
||||||
|
// [Clear].
|
||||||
|
//
|
||||||
|
// q.CopyInSlice(in)
|
||||||
|
//
|
||||||
|
// is an efficient shortcut for
|
||||||
|
//
|
||||||
|
// q.Clear()
|
||||||
|
// for i := range in {
|
||||||
|
// q.PushBack(in[i])
|
||||||
|
// }
|
||||||
|
func (q *Deque[T]) CopyInSlice(in []T) {
|
||||||
|
// Allocate new buffer if more space needed.
|
||||||
|
if len(q.buf) < len(in) {
|
||||||
|
newCap := len(q.buf)
|
||||||
|
if newCap == 0 {
|
||||||
|
newCap = minCapacity
|
||||||
|
q.minCap = minCapacity
|
||||||
|
}
|
||||||
|
for newCap < len(in) {
|
||||||
|
newCap <<= 1
|
||||||
|
}
|
||||||
|
q.buf = make([]T, newCap)
|
||||||
|
} else if len(q.buf) > len(in) {
|
||||||
|
q.Clear()
|
||||||
|
}
|
||||||
|
n := copy(q.buf, in)
|
||||||
|
q.count = n
|
||||||
|
q.tail = n
|
||||||
|
q.head = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// CopyOutSlice copies elements from the Deque into the given slice, up to the
|
||||||
|
// size of the buffer. Returns the number of elements copied, which will be the
|
||||||
|
// minimum of q.Len() and len(out).
|
||||||
|
//
|
||||||
|
// n := q.CopyOutSlice(out)
|
||||||
|
//
|
||||||
|
// is an efficient shortcut for
|
||||||
|
//
|
||||||
|
// n := min(len(out), q.Len())
|
||||||
|
// for i := 0; i < n; i++ {
|
||||||
|
// out[i] = q.At(i)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// This function is preferable to one that returns a copy of the internal
|
||||||
|
// buffer because this allows reuse of memory receiving data, for repeated copy
|
||||||
|
// operations.
|
||||||
|
func (q *Deque[T]) CopyOutSlice(out []T) int {
|
||||||
|
if q.count == 0 || len(out) == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
head, tail := q.head, q.tail
|
||||||
|
var n int
|
||||||
|
|
||||||
|
if head >= tail {
|
||||||
|
// [DEF....ABC]
|
||||||
|
n = copy(out, q.buf[head:])
|
||||||
|
out = out[n:]
|
||||||
|
if len(out) == 0 {
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
head = 0
|
||||||
|
}
|
||||||
|
n += copy(out, q.buf[head:tail])
|
||||||
|
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rotate rotates the deque n steps front-to-back. If n is negative, rotates
|
||||||
|
// back-to-front. Having Deque provide Rotate avoids resizing that could happen
|
||||||
|
// if implementing rotation using only Pop and Push methods. If q.Len() is one
|
||||||
|
// or less, or q is nil, then Rotate does nothing.
|
||||||
|
func (q *Deque[T]) Rotate(n int) {
|
||||||
|
if q.Len() <= 1 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Rotating a multiple of q.count is same as no rotation.
|
||||||
|
n %= q.count
|
||||||
|
if n == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
modBits := len(q.buf) - 1
|
||||||
|
// If no empty space in buffer, only move head and tail indexes.
|
||||||
|
if q.head == q.tail {
|
||||||
|
// Calculate new head and tail using bitwise modulus.
|
||||||
|
q.head = (q.head + n) & modBits
|
||||||
|
q.tail = q.head
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var zero T
|
||||||
|
|
||||||
|
if n < 0 {
|
||||||
|
// Rotate back to front.
|
||||||
|
for ; n < 0; n++ {
|
||||||
|
// Calculate new head and tail using bitwise modulus.
|
||||||
|
q.head = (q.head - 1) & modBits
|
||||||
|
q.tail = (q.tail - 1) & modBits
|
||||||
|
// Put tail value at head and remove value at tail.
|
||||||
|
q.buf[q.head] = q.buf[q.tail]
|
||||||
|
q.buf[q.tail] = zero
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rotate front to back.
|
||||||
|
for ; n > 0; n-- {
|
||||||
|
// Put head value at tail and remove value at head.
|
||||||
|
q.buf[q.tail] = q.buf[q.head]
|
||||||
|
q.buf[q.head] = zero
|
||||||
|
// Calculate new head and tail using bitwise modulus.
|
||||||
|
q.head = (q.head + 1) & modBits
|
||||||
|
q.tail = (q.tail + 1) & modBits
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Index returns the index into the Deque of the first item satisfying f(item),
|
||||||
|
// or -1 if none do. If q is nil, then -1 is always returned. Search is linear
|
||||||
|
// starting with index 0.
|
||||||
|
func (q *Deque[T]) Index(f func(T) bool) int {
|
||||||
|
if q.Len() > 0 {
|
||||||
|
modBits := len(q.buf) - 1
|
||||||
|
for i := 0; i < q.count; i++ {
|
||||||
|
if f(q.buf[(q.head+i)&modBits]) {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
// RIndex is the same as Index, but searches from Back to Front. The index
|
||||||
|
// returned is from Front to Back, where index 0 is the index of the item
|
||||||
|
// returned by [Front].
|
||||||
|
func (q *Deque[T]) RIndex(f func(T) bool) int {
|
||||||
|
if q.Len() > 0 {
|
||||||
|
modBits := len(q.buf) - 1
|
||||||
|
for i := q.count - 1; i >= 0; i-- {
|
||||||
|
if f(q.buf[(q.head+i)&modBits]) {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert is used to insert an element into the middle of the queue, before the
|
||||||
|
// element at the specified index. Insert(0,e) is the same as PushFront(e) and
|
||||||
|
// Insert(Len(),e) is the same as PushBack(e). Out of range indexes result in
|
||||||
|
// pushing the item onto the front of back of the deque.
|
||||||
|
//
|
||||||
|
// Important: Deque is optimized for O(1) operations at the ends of the queue,
|
||||||
|
// not for operations in the the middle. Complexity of this function is
|
||||||
|
// constant plus linear in the lesser of the distances between the index and
|
||||||
|
// either of the ends of the queue.
|
||||||
|
func (q *Deque[T]) Insert(at int, item T) {
|
||||||
|
if at <= 0 {
|
||||||
|
q.PushFront(item)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if at >= q.Len() {
|
||||||
|
q.PushBack(item)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if at*2 < q.count {
|
||||||
|
q.PushFront(item)
|
||||||
|
front := q.head
|
||||||
|
for i := 0; i < at; i++ {
|
||||||
|
next := q.next(front)
|
||||||
|
q.buf[front], q.buf[next] = q.buf[next], q.buf[front]
|
||||||
|
front = next
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
swaps := q.count - at
|
||||||
|
q.PushBack(item)
|
||||||
|
back := q.prev(q.tail)
|
||||||
|
for i := 0; i < swaps; i++ {
|
||||||
|
prev := q.prev(back)
|
||||||
|
q.buf[back], q.buf[prev] = q.buf[prev], q.buf[back]
|
||||||
|
back = prev
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove removes and returns an element from the middle of the queue, at the
|
||||||
|
// specified index. Remove(0) is the same as [PopFront] and Remove(Len()-1) is
|
||||||
|
// the same as [PopBack]. Accepts only non-negative index values, and panics if
|
||||||
|
// index is out of range.
|
||||||
|
//
|
||||||
|
// Important: Deque is optimized for O(1) operations at the ends of the queue,
|
||||||
|
// not for operations in the the middle. Complexity of this function is
|
||||||
|
// constant plus linear in the lesser of the distances between the index and
|
||||||
|
// either of the ends of the queue.
|
||||||
|
func (q *Deque[T]) Remove(at int) T {
|
||||||
|
q.checkRange(at)
|
||||||
|
rm := (q.head + at) & (len(q.buf) - 1)
|
||||||
|
if at*2 < q.count {
|
||||||
|
for i := 0; i < at; i++ {
|
||||||
|
prev := q.prev(rm)
|
||||||
|
q.buf[prev], q.buf[rm] = q.buf[rm], q.buf[prev]
|
||||||
|
rm = prev
|
||||||
|
}
|
||||||
|
return q.PopFront()
|
||||||
|
}
|
||||||
|
swaps := q.count - at - 1
|
||||||
|
for i := 0; i < swaps; i++ {
|
||||||
|
next := q.next(rm)
|
||||||
|
q.buf[rm], q.buf[next] = q.buf[next], q.buf[rm]
|
||||||
|
rm = next
|
||||||
|
}
|
||||||
|
return q.PopBack()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetBaseCap sets a base capacity so that at least the specified number of
|
||||||
|
// items can always be stored without resizing.
|
||||||
|
func (q *Deque[T]) SetBaseCap(baseCap int) {
|
||||||
|
minCap := minCapacity
|
||||||
|
for minCap < baseCap {
|
||||||
|
minCap <<= 1
|
||||||
|
}
|
||||||
|
q.minCap = minCap
|
||||||
|
}
|
||||||
|
|
||||||
|
// Swap exchanges the two values at idxA and idxB. It panics if either index is
|
||||||
|
// out of range.
|
||||||
|
func (q *Deque[T]) Swap(idxA, idxB int) {
|
||||||
|
q.checkRange(idxA)
|
||||||
|
q.checkRange(idxB)
|
||||||
|
if idxA == idxB {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
realA := (q.head + idxA) & (len(q.buf) - 1)
|
||||||
|
realB := (q.head + idxB) & (len(q.buf) - 1)
|
||||||
|
q.buf[realA], q.buf[realB] = q.buf[realB], q.buf[realA]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Deque[T]) checkRange(i int) {
|
||||||
|
if i < 0 || i >= q.count {
|
||||||
|
panic(fmt.Sprintf("deque: index out of range %d with length %d", i, q.Len()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// prev returns the previous buffer position wrapping around buffer.
|
||||||
|
func (q *Deque[T]) prev(i int) int {
|
||||||
|
return (i - 1) & (len(q.buf) - 1) // bitwise modulus
|
||||||
|
}
|
||||||
|
|
||||||
|
// next returns the next buffer position wrapping around buffer.
|
||||||
|
func (q *Deque[T]) next(i int) int {
|
||||||
|
return (i + 1) & (len(q.buf) - 1) // bitwise modulus
|
||||||
|
}
|
||||||
|
|
||||||
|
// growIfFull resizes up if the buffer is full.
|
||||||
|
func (q *Deque[T]) growIfFull() {
|
||||||
|
if q.count != len(q.buf) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(q.buf) == 0 {
|
||||||
|
if q.minCap == 0 {
|
||||||
|
q.minCap = minCapacity
|
||||||
|
}
|
||||||
|
q.buf = make([]T, q.minCap)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
q.resize(q.count << 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// shrinkIfExcess resize down if the buffer 1/4 full.
|
||||||
|
func (q *Deque[T]) shrinkIfExcess() {
|
||||||
|
if len(q.buf) > q.minCap && (q.count<<2) == len(q.buf) {
|
||||||
|
q.resize(q.count << 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Deque[T]) shrinkToFit() {
|
||||||
|
if len(q.buf) > q.minCap && (q.count<<2) <= len(q.buf) {
|
||||||
|
if q.count == 0 {
|
||||||
|
q.head = 0
|
||||||
|
q.tail = 0
|
||||||
|
q.buf = make([]T, q.minCap)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c := q.minCap
|
||||||
|
for c < q.count {
|
||||||
|
c <<= 1
|
||||||
|
}
|
||||||
|
q.resize(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// resize resizes the deque to fit exactly twice its current contents. This is
|
||||||
|
// used to grow the queue when it is full, and also to shrink it when it is
|
||||||
|
// only a quarter full.
|
||||||
|
func (q *Deque[T]) resize(newSize int) {
|
||||||
|
newBuf := make([]T, newSize)
|
||||||
|
if q.tail > q.head {
|
||||||
|
copy(newBuf, q.buf[q.head:q.tail])
|
||||||
|
} else {
|
||||||
|
n := copy(newBuf, q.buf[q.head:])
|
||||||
|
copy(newBuf[n:], q.buf[:q.tail])
|
||||||
|
}
|
||||||
|
|
||||||
|
q.head = 0
|
||||||
|
q.tail = q.count
|
||||||
|
q.buf = newBuf
|
||||||
|
}
|
||||||
19
common/maphash/common.go
Normal file
19
common/maphash/common.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package maphash
|
||||||
|
|
||||||
|
import "hash/maphash"
|
||||||
|
|
||||||
|
type Seed = maphash.Seed
|
||||||
|
|
||||||
|
func MakeSeed() Seed {
|
||||||
|
return maphash.MakeSeed()
|
||||||
|
}
|
||||||
|
|
||||||
|
type Hash = maphash.Hash
|
||||||
|
|
||||||
|
func Bytes(seed Seed, b []byte) uint64 {
|
||||||
|
return maphash.Bytes(seed, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func String(seed Seed, s string) uint64 {
|
||||||
|
return maphash.String(seed, s)
|
||||||
|
}
|
||||||
142
common/maphash/comparable_go120.go
Normal file
142
common/maphash/comparable_go120.go
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
//go:build !go1.24
|
||||||
|
|
||||||
|
package maphash
|
||||||
|
|
||||||
|
import "unsafe"
|
||||||
|
|
||||||
|
func Comparable[T comparable](s Seed, v T) uint64 {
|
||||||
|
return comparableHash(*(*seedTyp)(unsafe.Pointer(&s)), v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func comparableHash[T comparable](seed seedTyp, v T) uint64 {
|
||||||
|
s := seed.s
|
||||||
|
var m map[T]struct{}
|
||||||
|
mTyp := iTypeOf(m)
|
||||||
|
var hasher func(unsafe.Pointer, uintptr) uintptr
|
||||||
|
hasher = (*iMapType)(unsafe.Pointer(mTyp)).Hasher
|
||||||
|
|
||||||
|
p := escape(unsafe.Pointer(&v))
|
||||||
|
|
||||||
|
if ptrSize == 8 {
|
||||||
|
return uint64(hasher(p, uintptr(s)))
|
||||||
|
}
|
||||||
|
lo := hasher(p, uintptr(s))
|
||||||
|
hi := hasher(p, uintptr(s>>32))
|
||||||
|
return uint64(hi)<<32 | uint64(lo)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteComparable adds x to the data hashed by h.
|
||||||
|
func WriteComparable[T comparable](h *Hash, x T) {
|
||||||
|
// writeComparable (not in purego mode) directly operates on h.state
|
||||||
|
// without using h.buf. Mix in the buffer length so it won't
|
||||||
|
// commute with a buffered write, which either changes h.n or changes
|
||||||
|
// h.state.
|
||||||
|
hash := (*hashTyp)(unsafe.Pointer(h))
|
||||||
|
if hash.n != 0 {
|
||||||
|
hash.state.s = comparableHash(hash.state, hash.n)
|
||||||
|
}
|
||||||
|
hash.state.s = comparableHash(hash.state, x)
|
||||||
|
}
|
||||||
|
|
||||||
|
// go/src/hash/maphash/maphash.go
|
||||||
|
type hashTyp struct {
|
||||||
|
_ [0]func() // not comparable
|
||||||
|
seed seedTyp // initial seed used for this hash
|
||||||
|
state seedTyp // current hash of all flushed bytes
|
||||||
|
buf [128]byte // unflushed byte buffer
|
||||||
|
n int // number of unflushed bytes
|
||||||
|
}
|
||||||
|
|
||||||
|
type seedTyp struct {
|
||||||
|
s uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
type iTFlag uint8
|
||||||
|
type iKind uint8
|
||||||
|
type iNameOff int32
|
||||||
|
|
||||||
|
// TypeOff is the offset to a type from moduledata.types. See resolveTypeOff in runtime.
|
||||||
|
type iTypeOff int32
|
||||||
|
|
||||||
|
type iType struct {
|
||||||
|
Size_ uintptr
|
||||||
|
PtrBytes uintptr // number of (prefix) bytes in the type that can contain pointers
|
||||||
|
Hash uint32 // hash of type; avoids computation in hash tables
|
||||||
|
TFlag iTFlag // extra type information flags
|
||||||
|
Align_ uint8 // alignment of variable with this type
|
||||||
|
FieldAlign_ uint8 // alignment of struct field with this type
|
||||||
|
Kind_ iKind // enumeration for C
|
||||||
|
// function for comparing objects of this type
|
||||||
|
// (ptr to object A, ptr to object B) -> ==?
|
||||||
|
Equal func(unsafe.Pointer, unsafe.Pointer) bool
|
||||||
|
// GCData stores the GC type data for the garbage collector.
|
||||||
|
// Normally, GCData points to a bitmask that describes the
|
||||||
|
// ptr/nonptr fields of the type. The bitmask will have at
|
||||||
|
// least PtrBytes/ptrSize bits.
|
||||||
|
// If the TFlagGCMaskOnDemand bit is set, GCData is instead a
|
||||||
|
// **byte and the pointer to the bitmask is one dereference away.
|
||||||
|
// The runtime will build the bitmask if needed.
|
||||||
|
// (See runtime/type.go:getGCMask.)
|
||||||
|
// Note: multiple types may have the same value of GCData,
|
||||||
|
// including when TFlagGCMaskOnDemand is set. The types will, of course,
|
||||||
|
// have the same pointer layout (but not necessarily the same size).
|
||||||
|
GCData *byte
|
||||||
|
Str iNameOff // string form
|
||||||
|
PtrToThis iTypeOff // type for pointer to this type, may be zero
|
||||||
|
}
|
||||||
|
|
||||||
|
type iMapType struct {
|
||||||
|
iType
|
||||||
|
Key *iType
|
||||||
|
Elem *iType
|
||||||
|
Group *iType // internal type representing a slot group
|
||||||
|
// function for hashing keys (ptr to key, seed) -> hash
|
||||||
|
Hasher func(unsafe.Pointer, uintptr) uintptr
|
||||||
|
}
|
||||||
|
|
||||||
|
func iTypeOf(a any) *iType {
|
||||||
|
eface := *(*iEmptyInterface)(unsafe.Pointer(&a))
|
||||||
|
// Types are either static (for compiler-created types) or
|
||||||
|
// heap-allocated but always reachable (for reflection-created
|
||||||
|
// types, held in the central map). So there is no need to
|
||||||
|
// escape types. noescape here help avoid unnecessary escape
|
||||||
|
// of v.
|
||||||
|
return (*iType)(noescape(unsafe.Pointer(eface.Type)))
|
||||||
|
}
|
||||||
|
|
||||||
|
type iEmptyInterface struct {
|
||||||
|
Type *iType
|
||||||
|
Data unsafe.Pointer
|
||||||
|
}
|
||||||
|
|
||||||
|
// noescape hides a pointer from escape analysis. noescape is
|
||||||
|
// the identity function but escape analysis doesn't think the
|
||||||
|
// output depends on the input. noescape is inlined and currently
|
||||||
|
// compiles down to zero instructions.
|
||||||
|
// USE CAREFULLY!
|
||||||
|
//
|
||||||
|
// nolint:all
|
||||||
|
//
|
||||||
|
//go:nosplit
|
||||||
|
//goland:noinspection ALL
|
||||||
|
func noescape(p unsafe.Pointer) unsafe.Pointer {
|
||||||
|
x := uintptr(p)
|
||||||
|
return unsafe.Pointer(x ^ 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
var alwaysFalse bool
|
||||||
|
var escapeSink any
|
||||||
|
|
||||||
|
// escape forces any pointers in x to escape to the heap.
|
||||||
|
func escape[T any](x T) T {
|
||||||
|
if alwaysFalse {
|
||||||
|
escapeSink = x
|
||||||
|
}
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
|
||||||
|
// ptrSize is the size of a pointer in bytes - unsafe.Sizeof(uintptr(0)) but as an ideal constant.
|
||||||
|
// It is also the size of the machine's native word size (that is, 4 on 32-bit systems, 8 on 64-bit).
|
||||||
|
const ptrSize = 4 << (^uintptr(0) >> 63)
|
||||||
|
|
||||||
|
const testComparableAllocations = false
|
||||||
15
common/maphash/comparable_go124.go
Normal file
15
common/maphash/comparable_go124.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
//go:build go1.24
|
||||||
|
|
||||||
|
package maphash
|
||||||
|
|
||||||
|
import "hash/maphash"
|
||||||
|
|
||||||
|
func Comparable[T comparable](seed Seed, v T) uint64 {
|
||||||
|
return maphash.Comparable(seed, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func WriteComparable[T comparable](h *Hash, x T) {
|
||||||
|
maphash.WriteComparable(h, x)
|
||||||
|
}
|
||||||
|
|
||||||
|
const testComparableAllocations = true
|
||||||
534
common/maphash/maphash_test.go
Normal file
534
common/maphash/maphash_test.go
Normal file
@@ -0,0 +1,534 @@
|
|||||||
|
// Copyright 2019 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package maphash
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"hash"
|
||||||
|
"math"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
rand "github.com/metacubex/randv2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestUnseededHash(t *testing.T) {
|
||||||
|
m := map[uint64]struct{}{}
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
h := new(Hash)
|
||||||
|
m[h.Sum64()] = struct{}{}
|
||||||
|
}
|
||||||
|
if len(m) < 900 {
|
||||||
|
t.Errorf("empty hash not sufficiently random: got %d, want 1000", len(m))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSeededHash(t *testing.T) {
|
||||||
|
s := MakeSeed()
|
||||||
|
m := map[uint64]struct{}{}
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
h := new(Hash)
|
||||||
|
h.SetSeed(s)
|
||||||
|
m[h.Sum64()] = struct{}{}
|
||||||
|
}
|
||||||
|
if len(m) != 1 {
|
||||||
|
t.Errorf("seeded hash is random: got %d, want 1", len(m))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHashGrouping(t *testing.T) {
|
||||||
|
b := bytes.Repeat([]byte("foo"), 100)
|
||||||
|
hh := make([]*Hash, 7)
|
||||||
|
for i := range hh {
|
||||||
|
hh[i] = new(Hash)
|
||||||
|
}
|
||||||
|
for _, h := range hh[1:] {
|
||||||
|
h.SetSeed(hh[0].Seed())
|
||||||
|
}
|
||||||
|
hh[0].Write(b)
|
||||||
|
hh[1].WriteString(string(b))
|
||||||
|
|
||||||
|
writeByte := func(h *Hash, b byte) {
|
||||||
|
err := h.WriteByte(b)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("WriteByte: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writeSingleByte := func(h *Hash, b byte) {
|
||||||
|
_, err := h.Write([]byte{b})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Write single byte: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writeStringSingleByte := func(h *Hash, b byte) {
|
||||||
|
_, err := h.WriteString(string([]byte{b}))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("WriteString single byte: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, x := range b {
|
||||||
|
writeByte(hh[2], x)
|
||||||
|
writeSingleByte(hh[3], x)
|
||||||
|
if i == 0 {
|
||||||
|
writeByte(hh[4], x)
|
||||||
|
} else {
|
||||||
|
writeSingleByte(hh[4], x)
|
||||||
|
}
|
||||||
|
writeStringSingleByte(hh[5], x)
|
||||||
|
if i == 0 {
|
||||||
|
writeByte(hh[6], x)
|
||||||
|
} else {
|
||||||
|
writeStringSingleByte(hh[6], x)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sum := hh[0].Sum64()
|
||||||
|
for i, h := range hh {
|
||||||
|
if sum != h.Sum64() {
|
||||||
|
t.Errorf("hash %d not identical to a single Write", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if sum1 := Bytes(hh[0].Seed(), b); sum1 != hh[0].Sum64() {
|
||||||
|
t.Errorf("hash using Bytes not identical to a single Write")
|
||||||
|
}
|
||||||
|
|
||||||
|
if sum1 := String(hh[0].Seed(), string(b)); sum1 != hh[0].Sum64() {
|
||||||
|
t.Errorf("hash using String not identical to a single Write")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHashBytesVsString(t *testing.T) {
|
||||||
|
s := "foo"
|
||||||
|
b := []byte(s)
|
||||||
|
h1 := new(Hash)
|
||||||
|
h2 := new(Hash)
|
||||||
|
h2.SetSeed(h1.Seed())
|
||||||
|
n1, err1 := h1.WriteString(s)
|
||||||
|
if n1 != len(s) || err1 != nil {
|
||||||
|
t.Fatalf("WriteString(s) = %d, %v, want %d, nil", n1, err1, len(s))
|
||||||
|
}
|
||||||
|
n2, err2 := h2.Write(b)
|
||||||
|
if n2 != len(b) || err2 != nil {
|
||||||
|
t.Fatalf("Write(b) = %d, %v, want %d, nil", n2, err2, len(b))
|
||||||
|
}
|
||||||
|
if h1.Sum64() != h2.Sum64() {
|
||||||
|
t.Errorf("hash of string and bytes not identical")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHashHighBytes(t *testing.T) {
|
||||||
|
// See issue 34925.
|
||||||
|
const N = 10
|
||||||
|
m := map[uint64]struct{}{}
|
||||||
|
for i := 0; i < N; i++ {
|
||||||
|
h := new(Hash)
|
||||||
|
h.WriteString("foo")
|
||||||
|
m[h.Sum64()>>32] = struct{}{}
|
||||||
|
}
|
||||||
|
if len(m) < N/2 {
|
||||||
|
t.Errorf("from %d seeds, wanted at least %d different hashes; got %d", N, N/2, len(m))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRepeat(t *testing.T) {
|
||||||
|
h1 := new(Hash)
|
||||||
|
h1.WriteString("testing")
|
||||||
|
sum1 := h1.Sum64()
|
||||||
|
|
||||||
|
h1.Reset()
|
||||||
|
h1.WriteString("testing")
|
||||||
|
sum2 := h1.Sum64()
|
||||||
|
|
||||||
|
if sum1 != sum2 {
|
||||||
|
t.Errorf("different sum after resetting: %#x != %#x", sum1, sum2)
|
||||||
|
}
|
||||||
|
|
||||||
|
h2 := new(Hash)
|
||||||
|
h2.SetSeed(h1.Seed())
|
||||||
|
h2.WriteString("testing")
|
||||||
|
sum3 := h2.Sum64()
|
||||||
|
|
||||||
|
if sum1 != sum3 {
|
||||||
|
t.Errorf("different sum on the same seed: %#x != %#x", sum1, sum3)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSeedFromSum64(t *testing.T) {
|
||||||
|
h1 := new(Hash)
|
||||||
|
h1.WriteString("foo")
|
||||||
|
x := h1.Sum64() // seed generated here
|
||||||
|
h2 := new(Hash)
|
||||||
|
h2.SetSeed(h1.Seed())
|
||||||
|
h2.WriteString("foo")
|
||||||
|
y := h2.Sum64()
|
||||||
|
if x != y {
|
||||||
|
t.Errorf("hashes don't match: want %x, got %x", x, y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSeedFromSeed(t *testing.T) {
|
||||||
|
h1 := new(Hash)
|
||||||
|
h1.WriteString("foo")
|
||||||
|
_ = h1.Seed() // seed generated here
|
||||||
|
x := h1.Sum64()
|
||||||
|
h2 := new(Hash)
|
||||||
|
h2.SetSeed(h1.Seed())
|
||||||
|
h2.WriteString("foo")
|
||||||
|
y := h2.Sum64()
|
||||||
|
if x != y {
|
||||||
|
t.Errorf("hashes don't match: want %x, got %x", x, y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSeedFromFlush(t *testing.T) {
|
||||||
|
b := make([]byte, 65)
|
||||||
|
h1 := new(Hash)
|
||||||
|
h1.Write(b) // seed generated here
|
||||||
|
x := h1.Sum64()
|
||||||
|
h2 := new(Hash)
|
||||||
|
h2.SetSeed(h1.Seed())
|
||||||
|
h2.Write(b)
|
||||||
|
y := h2.Sum64()
|
||||||
|
if x != y {
|
||||||
|
t.Errorf("hashes don't match: want %x, got %x", x, y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSeedFromReset(t *testing.T) {
|
||||||
|
h1 := new(Hash)
|
||||||
|
h1.WriteString("foo")
|
||||||
|
h1.Reset() // seed generated here
|
||||||
|
h1.WriteString("foo")
|
||||||
|
x := h1.Sum64()
|
||||||
|
h2 := new(Hash)
|
||||||
|
h2.SetSeed(h1.Seed())
|
||||||
|
h2.WriteString("foo")
|
||||||
|
y := h2.Sum64()
|
||||||
|
if x != y {
|
||||||
|
t.Errorf("hashes don't match: want %x, got %x", x, y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func negativeZero[T float32 | float64]() T {
|
||||||
|
var f T
|
||||||
|
f = -f
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestComparable(t *testing.T) {
|
||||||
|
testComparable(t, int64(2))
|
||||||
|
testComparable(t, uint64(8))
|
||||||
|
testComparable(t, uintptr(12))
|
||||||
|
testComparable(t, any("s"))
|
||||||
|
testComparable(t, "s")
|
||||||
|
testComparable(t, true)
|
||||||
|
testComparable(t, new(float64))
|
||||||
|
testComparable(t, float64(9))
|
||||||
|
testComparable(t, complex128(9i+1))
|
||||||
|
testComparable(t, struct{}{})
|
||||||
|
testComparable(t, struct {
|
||||||
|
i int
|
||||||
|
u uint
|
||||||
|
b bool
|
||||||
|
f float64
|
||||||
|
p *int
|
||||||
|
a any
|
||||||
|
}{i: 9, u: 1, b: true, f: 9.9, p: new(int), a: 1})
|
||||||
|
type S struct {
|
||||||
|
s string
|
||||||
|
}
|
||||||
|
s1 := S{s: heapStr(t)}
|
||||||
|
s2 := S{s: heapStr(t)}
|
||||||
|
if unsafe.StringData(s1.s) == unsafe.StringData(s2.s) {
|
||||||
|
t.Fatalf("unexpected two heapStr ptr equal")
|
||||||
|
}
|
||||||
|
if s1.s != s2.s {
|
||||||
|
t.Fatalf("unexpected two heapStr value not equal")
|
||||||
|
}
|
||||||
|
testComparable(t, s1, s2)
|
||||||
|
testComparable(t, s1.s, s2.s)
|
||||||
|
testComparable(t, float32(0), negativeZero[float32]())
|
||||||
|
testComparable(t, float64(0), negativeZero[float64]())
|
||||||
|
testComparableNoEqual(t, math.NaN(), math.NaN())
|
||||||
|
testComparableNoEqual(t, [2]string{"a", ""}, [2]string{"", "a"})
|
||||||
|
testComparableNoEqual(t, struct{ a, b string }{"foo", ""}, struct{ a, b string }{"", "foo"})
|
||||||
|
testComparableNoEqual(t, struct{ a, b any }{int(0), struct{}{}}, struct{ a, b any }{struct{}{}, int(0)})
|
||||||
|
}
|
||||||
|
|
||||||
|
func testComparableNoEqual[T comparable](t *testing.T, v1, v2 T) {
|
||||||
|
seed := MakeSeed()
|
||||||
|
if Comparable(seed, v1) == Comparable(seed, v2) {
|
||||||
|
t.Fatalf("Comparable(seed, %v) == Comparable(seed, %v)", v1, v2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var heapStrValue = []byte("aTestString")
|
||||||
|
|
||||||
|
func heapStr(t *testing.T) string {
|
||||||
|
return string(heapStrValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testComparable[T comparable](t *testing.T, v T, v2 ...T) {
|
||||||
|
t.Run(TypeFor[T]().String(), func(t *testing.T) {
|
||||||
|
var a, b T = v, v
|
||||||
|
if len(v2) != 0 {
|
||||||
|
b = v2[0]
|
||||||
|
}
|
||||||
|
var pa *T = &a
|
||||||
|
seed := MakeSeed()
|
||||||
|
if Comparable(seed, a) != Comparable(seed, b) {
|
||||||
|
t.Fatalf("Comparable(seed, %v) != Comparable(seed, %v)", a, b)
|
||||||
|
}
|
||||||
|
old := Comparable(seed, pa)
|
||||||
|
stackGrow(8192)
|
||||||
|
new := Comparable(seed, pa)
|
||||||
|
if old != new {
|
||||||
|
t.Fatal("Comparable(seed, ptr) != Comparable(seed, ptr)")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
var use byte
|
||||||
|
|
||||||
|
//go:noinline
|
||||||
|
func stackGrow(dep int) {
|
||||||
|
if dep == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var local [1024]byte
|
||||||
|
// make sure local is allocated on the stack.
|
||||||
|
local[rand.Uint64()%1024] = byte(rand.Uint64())
|
||||||
|
use = local[rand.Uint64()%1024]
|
||||||
|
stackGrow(dep - 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteComparable(t *testing.T) {
|
||||||
|
testWriteComparable(t, int64(2))
|
||||||
|
testWriteComparable(t, uint64(8))
|
||||||
|
testWriteComparable(t, uintptr(12))
|
||||||
|
testWriteComparable(t, any("s"))
|
||||||
|
testWriteComparable(t, "s")
|
||||||
|
testComparable(t, true)
|
||||||
|
testWriteComparable(t, new(float64))
|
||||||
|
testWriteComparable(t, float64(9))
|
||||||
|
testWriteComparable(t, complex128(9i+1))
|
||||||
|
testWriteComparable(t, struct{}{})
|
||||||
|
testWriteComparable(t, struct {
|
||||||
|
i int
|
||||||
|
u uint
|
||||||
|
b bool
|
||||||
|
f float64
|
||||||
|
p *int
|
||||||
|
a any
|
||||||
|
}{i: 9, u: 1, b: true, f: 9.9, p: new(int), a: 1})
|
||||||
|
type S struct {
|
||||||
|
s string
|
||||||
|
}
|
||||||
|
s1 := S{s: heapStr(t)}
|
||||||
|
s2 := S{s: heapStr(t)}
|
||||||
|
if unsafe.StringData(s1.s) == unsafe.StringData(s2.s) {
|
||||||
|
t.Fatalf("unexpected two heapStr ptr equal")
|
||||||
|
}
|
||||||
|
if s1.s != s2.s {
|
||||||
|
t.Fatalf("unexpected two heapStr value not equal")
|
||||||
|
}
|
||||||
|
testWriteComparable(t, s1, s2)
|
||||||
|
testWriteComparable(t, s1.s, s2.s)
|
||||||
|
testWriteComparable(t, float32(0), negativeZero[float32]())
|
||||||
|
testWriteComparable(t, float64(0), negativeZero[float64]())
|
||||||
|
testWriteComparableNoEqual(t, math.NaN(), math.NaN())
|
||||||
|
testWriteComparableNoEqual(t, [2]string{"a", ""}, [2]string{"", "a"})
|
||||||
|
testWriteComparableNoEqual(t, struct{ a, b string }{"foo", ""}, struct{ a, b string }{"", "foo"})
|
||||||
|
testWriteComparableNoEqual(t, struct{ a, b any }{int(0), struct{}{}}, struct{ a, b any }{struct{}{}, int(0)})
|
||||||
|
}
|
||||||
|
|
||||||
|
func testWriteComparableNoEqual[T comparable](t *testing.T, v1, v2 T) {
|
||||||
|
seed := MakeSeed()
|
||||||
|
h1 := Hash{}
|
||||||
|
h2 := Hash{}
|
||||||
|
*(*Seed)(unsafe.Pointer(&h1)), *(*Seed)(unsafe.Pointer(&h2)) = seed, seed
|
||||||
|
WriteComparable(&h1, v1)
|
||||||
|
WriteComparable(&h2, v2)
|
||||||
|
if h1.Sum64() == h2.Sum64() {
|
||||||
|
t.Fatalf("WriteComparable(seed, %v) == WriteComparable(seed, %v)", v1, v2)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func testWriteComparable[T comparable](t *testing.T, v T, v2 ...T) {
|
||||||
|
t.Run(TypeFor[T]().String(), func(t *testing.T) {
|
||||||
|
var a, b T = v, v
|
||||||
|
if len(v2) != 0 {
|
||||||
|
b = v2[0]
|
||||||
|
}
|
||||||
|
var pa *T = &a
|
||||||
|
h1 := Hash{}
|
||||||
|
h2 := Hash{}
|
||||||
|
*(*Seed)(unsafe.Pointer(&h1)) = MakeSeed()
|
||||||
|
h2 = h1
|
||||||
|
WriteComparable(&h1, a)
|
||||||
|
WriteComparable(&h2, b)
|
||||||
|
if h1.Sum64() != h2.Sum64() {
|
||||||
|
t.Fatalf("WriteComparable(h, %v) != WriteComparable(h, %v)", a, b)
|
||||||
|
}
|
||||||
|
WriteComparable(&h1, pa)
|
||||||
|
old := h1.Sum64()
|
||||||
|
stackGrow(8192)
|
||||||
|
WriteComparable(&h2, pa)
|
||||||
|
new := h2.Sum64()
|
||||||
|
if old != new {
|
||||||
|
t.Fatal("WriteComparable(seed, ptr) != WriteComparable(seed, ptr)")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestComparableShouldPanic(t *testing.T) {
|
||||||
|
s := []byte("s")
|
||||||
|
a := any(s)
|
||||||
|
defer func() {
|
||||||
|
e := recover()
|
||||||
|
err, ok := e.(error)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("Comaparable(any([]byte)) should panic")
|
||||||
|
}
|
||||||
|
want := "hash of unhashable type []uint8"
|
||||||
|
if s := err.Error(); !strings.Contains(s, want) {
|
||||||
|
t.Fatalf("want %s, got %s", want, s)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
Comparable(MakeSeed(), a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteComparableNoncommute(t *testing.T) {
|
||||||
|
seed := MakeSeed()
|
||||||
|
var h1, h2 Hash
|
||||||
|
h1.SetSeed(seed)
|
||||||
|
h2.SetSeed(seed)
|
||||||
|
|
||||||
|
h1.WriteString("abc")
|
||||||
|
WriteComparable(&h1, 123)
|
||||||
|
WriteComparable(&h2, 123)
|
||||||
|
h2.WriteString("abc")
|
||||||
|
|
||||||
|
if h1.Sum64() == h2.Sum64() {
|
||||||
|
t.Errorf("WriteComparable and WriteString unexpectedly commute")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestComparableAllocations(t *testing.T) {
|
||||||
|
if !testComparableAllocations {
|
||||||
|
t.Skip("test broken in old golang version")
|
||||||
|
}
|
||||||
|
seed := MakeSeed()
|
||||||
|
x := heapStr(t)
|
||||||
|
allocs := testing.AllocsPerRun(10, func() {
|
||||||
|
s := "s" + x
|
||||||
|
Comparable(seed, s)
|
||||||
|
})
|
||||||
|
if allocs > 0 {
|
||||||
|
t.Errorf("got %v allocs, want 0", allocs)
|
||||||
|
}
|
||||||
|
|
||||||
|
type S struct {
|
||||||
|
a int
|
||||||
|
b string
|
||||||
|
}
|
||||||
|
allocs = testing.AllocsPerRun(10, func() {
|
||||||
|
s := S{123, "s" + x}
|
||||||
|
Comparable(seed, s)
|
||||||
|
})
|
||||||
|
if allocs > 0 {
|
||||||
|
t.Errorf("got %v allocs, want 0", allocs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure a Hash implements the hash.Hash and hash.Hash64 interfaces.
|
||||||
|
var _ hash.Hash = &Hash{}
|
||||||
|
var _ hash.Hash64 = &Hash{}
|
||||||
|
|
||||||
|
func benchmarkSize(b *testing.B, size int) {
|
||||||
|
h := &Hash{}
|
||||||
|
buf := make([]byte, size)
|
||||||
|
s := string(buf)
|
||||||
|
|
||||||
|
b.Run("Write", func(b *testing.B) {
|
||||||
|
b.SetBytes(int64(size))
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
h.Reset()
|
||||||
|
h.Write(buf)
|
||||||
|
h.Sum64()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
b.Run("Bytes", func(b *testing.B) {
|
||||||
|
b.SetBytes(int64(size))
|
||||||
|
seed := h.Seed()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
Bytes(seed, buf)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
b.Run("String", func(b *testing.B) {
|
||||||
|
b.SetBytes(int64(size))
|
||||||
|
seed := h.Seed()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
String(seed, s)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkHash(b *testing.B) {
|
||||||
|
sizes := []int{4, 8, 16, 32, 64, 256, 320, 1024, 4096, 16384}
|
||||||
|
for _, size := range sizes {
|
||||||
|
b.Run(fmt.Sprint("n=", size), func(b *testing.B) {
|
||||||
|
benchmarkSize(b, size)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchmarkComparable[T comparable](b *testing.B, v T) {
|
||||||
|
b.Run(TypeFor[T]().String(), func(b *testing.B) {
|
||||||
|
seed := MakeSeed()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
Comparable(seed, v)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkComparable(b *testing.B) {
|
||||||
|
type testStruct struct {
|
||||||
|
i int
|
||||||
|
u uint
|
||||||
|
b bool
|
||||||
|
f float64
|
||||||
|
p *int
|
||||||
|
a any
|
||||||
|
}
|
||||||
|
benchmarkComparable(b, int64(2))
|
||||||
|
benchmarkComparable(b, uint64(8))
|
||||||
|
benchmarkComparable(b, uintptr(12))
|
||||||
|
benchmarkComparable(b, any("s"))
|
||||||
|
benchmarkComparable(b, "s")
|
||||||
|
benchmarkComparable(b, true)
|
||||||
|
benchmarkComparable(b, new(float64))
|
||||||
|
benchmarkComparable(b, float64(9))
|
||||||
|
benchmarkComparable(b, complex128(9i+1))
|
||||||
|
benchmarkComparable(b, struct{}{})
|
||||||
|
benchmarkComparable(b, testStruct{i: 9, u: 1, b: true, f: 9.9, p: new(int), a: 1})
|
||||||
|
}
|
||||||
|
|
||||||
|
// TypeFor returns the [Type] that represents the type argument T.
|
||||||
|
func TypeFor[T any]() reflect.Type {
|
||||||
|
var v T
|
||||||
|
if t := reflect.TypeOf(v); t != nil {
|
||||||
|
return t // optimize for T being a non-interface kind
|
||||||
|
}
|
||||||
|
return reflect.TypeOf((*T)(nil)).Elem() // only for an interface kind
|
||||||
|
}
|
||||||
@@ -7,9 +7,9 @@ import (
|
|||||||
|
|
||||||
"github.com/metacubex/mihomo/common/atomic"
|
"github.com/metacubex/mihomo/common/atomic"
|
||||||
|
|
||||||
"github.com/sagernet/sing/common/buf"
|
"github.com/metacubex/sing/common/buf"
|
||||||
"github.com/sagernet/sing/common/bufio"
|
"github.com/metacubex/sing/common/bufio"
|
||||||
"github.com/sagernet/sing/common/network"
|
"github.com/metacubex/sing/common/network"
|
||||||
)
|
)
|
||||||
|
|
||||||
type connReadResult struct {
|
type connReadResult struct {
|
||||||
@@ -20,7 +20,7 @@ type connReadResult struct {
|
|||||||
type Conn struct {
|
type Conn struct {
|
||||||
network.ExtendedConn
|
network.ExtendedConn
|
||||||
deadline atomic.TypedValue[time.Time]
|
deadline atomic.TypedValue[time.Time]
|
||||||
pipeDeadline pipeDeadline
|
pipeDeadline PipeDeadline
|
||||||
disablePipe atomic.Bool
|
disablePipe atomic.Bool
|
||||||
inRead atomic.Bool
|
inRead atomic.Bool
|
||||||
resultCh chan *connReadResult
|
resultCh chan *connReadResult
|
||||||
@@ -34,7 +34,7 @@ func IsConn(conn any) bool {
|
|||||||
func NewConn(conn net.Conn) *Conn {
|
func NewConn(conn net.Conn) *Conn {
|
||||||
c := &Conn{
|
c := &Conn{
|
||||||
ExtendedConn: bufio.NewExtendedConn(conn),
|
ExtendedConn: bufio.NewExtendedConn(conn),
|
||||||
pipeDeadline: makePipeDeadline(),
|
pipeDeadline: MakePipeDeadline(),
|
||||||
resultCh: make(chan *connReadResult, 1),
|
resultCh: make(chan *connReadResult, 1),
|
||||||
}
|
}
|
||||||
c.resultCh <- nil
|
c.resultCh <- nil
|
||||||
@@ -58,7 +58,7 @@ func (c *Conn) Read(p []byte) (n int, err error) {
|
|||||||
c.resultCh <- nil
|
c.resultCh <- nil
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
case <-c.pipeDeadline.wait():
|
case <-c.pipeDeadline.Wait():
|
||||||
return 0, os.ErrDeadlineExceeded
|
return 0, os.ErrDeadlineExceeded
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,7 +104,7 @@ func (c *Conn) ReadBuffer(buffer *buf.Buffer) (err error) {
|
|||||||
c.resultCh <- nil
|
c.resultCh <- nil
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
case <-c.pipeDeadline.wait():
|
case <-c.pipeDeadline.Wait():
|
||||||
return os.ErrDeadlineExceeded
|
return os.ErrDeadlineExceeded
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +130,7 @@ func (c *Conn) SetReadDeadline(t time.Time) error {
|
|||||||
return c.ExtendedConn.SetReadDeadline(t)
|
return c.ExtendedConn.SetReadDeadline(t)
|
||||||
}
|
}
|
||||||
c.deadline.Store(t)
|
c.deadline.Store(t)
|
||||||
c.pipeDeadline.set(t)
|
c.pipeDeadline.Set(t)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,6 +149,10 @@ func (c *Conn) ReaderReplaceable() bool {
|
|||||||
return c.disablePipe.Load() || c.deadline.Load().IsZero()
|
return c.disablePipe.Load() || c.deadline.Load().IsZero()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Conn) WriterReplaceable() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Conn) Upstream() any {
|
func (c *Conn) Upstream() any {
|
||||||
return c.ExtendedConn
|
return c.ExtendedConn
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ type readResult struct {
|
|||||||
type NetPacketConn struct {
|
type NetPacketConn struct {
|
||||||
net.PacketConn
|
net.PacketConn
|
||||||
deadline atomic.TypedValue[time.Time]
|
deadline atomic.TypedValue[time.Time]
|
||||||
pipeDeadline pipeDeadline
|
pipeDeadline PipeDeadline
|
||||||
disablePipe atomic.Bool
|
disablePipe atomic.Bool
|
||||||
inRead atomic.Bool
|
inRead atomic.Bool
|
||||||
resultCh chan any
|
resultCh chan any
|
||||||
@@ -28,7 +28,7 @@ type NetPacketConn struct {
|
|||||||
func NewNetPacketConn(pc net.PacketConn) net.PacketConn {
|
func NewNetPacketConn(pc net.PacketConn) net.PacketConn {
|
||||||
npc := &NetPacketConn{
|
npc := &NetPacketConn{
|
||||||
PacketConn: pc,
|
PacketConn: pc,
|
||||||
pipeDeadline: makePipeDeadline(),
|
pipeDeadline: MakePipeDeadline(),
|
||||||
resultCh: make(chan any, 1),
|
resultCh: make(chan any, 1),
|
||||||
}
|
}
|
||||||
npc.resultCh <- nil
|
npc.resultCh <- nil
|
||||||
@@ -83,7 +83,7 @@ FOR:
|
|||||||
c.resultCh <- nil
|
c.resultCh <- nil
|
||||||
break FOR
|
break FOR
|
||||||
}
|
}
|
||||||
case <-c.pipeDeadline.wait():
|
case <-c.pipeDeadline.Wait():
|
||||||
return 0, nil, os.ErrDeadlineExceeded
|
return 0, nil, os.ErrDeadlineExceeded
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -122,7 +122,7 @@ func (c *NetPacketConn) SetReadDeadline(t time.Time) error {
|
|||||||
return c.PacketConn.SetReadDeadline(t)
|
return c.PacketConn.SetReadDeadline(t)
|
||||||
}
|
}
|
||||||
c.deadline.Store(t)
|
c.deadline.Store(t)
|
||||||
c.pipeDeadline.set(t)
|
c.pipeDeadline.Set(t)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ FOR:
|
|||||||
c.netPacketConn.resultCh <- nil
|
c.netPacketConn.resultCh <- nil
|
||||||
break FOR
|
break FOR
|
||||||
}
|
}
|
||||||
case <-c.netPacketConn.pipeDeadline.wait():
|
case <-c.netPacketConn.pipeDeadline.Wait():
|
||||||
return nil, nil, nil, os.ErrDeadlineExceeded
|
return nil, nil, nil, os.ErrDeadlineExceeded
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,10 +6,10 @@ import (
|
|||||||
|
|
||||||
"github.com/metacubex/mihomo/common/net/packet"
|
"github.com/metacubex/mihomo/common/net/packet"
|
||||||
|
|
||||||
"github.com/sagernet/sing/common/buf"
|
"github.com/metacubex/sing/common/buf"
|
||||||
"github.com/sagernet/sing/common/bufio"
|
"github.com/metacubex/sing/common/bufio"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/metacubex/sing/common/metadata"
|
||||||
N "github.com/sagernet/sing/common/network"
|
N "github.com/metacubex/sing/common/network"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SingPacketConn struct {
|
type SingPacketConn struct {
|
||||||
@@ -69,7 +69,7 @@ FOR:
|
|||||||
c.netPacketConn.resultCh <- nil
|
c.netPacketConn.resultCh <- nil
|
||||||
break FOR
|
break FOR
|
||||||
}
|
}
|
||||||
case <-c.netPacketConn.pipeDeadline.wait():
|
case <-c.netPacketConn.pipeDeadline.Wait():
|
||||||
return M.Socksaddr{}, os.ErrDeadlineExceeded
|
return M.Socksaddr{}, os.ErrDeadlineExceeded
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -146,7 +146,7 @@ FOR:
|
|||||||
c.netPacketConn.resultCh <- nil
|
c.netPacketConn.resultCh <- nil
|
||||||
break FOR
|
break FOR
|
||||||
}
|
}
|
||||||
case <-c.netPacketConn.pipeDeadline.wait():
|
case <-c.netPacketConn.pipeDeadline.Wait():
|
||||||
return nil, M.Socksaddr{}, os.ErrDeadlineExceeded
|
return nil, M.Socksaddr{}, os.ErrDeadlineExceeded
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,24 +9,24 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// pipeDeadline is an abstraction for handling timeouts.
|
// PipeDeadline is an abstraction for handling timeouts.
|
||||||
type pipeDeadline struct {
|
type PipeDeadline struct {
|
||||||
mu sync.Mutex // Guards timer and cancel
|
mu sync.Mutex // Guards timer and cancel
|
||||||
timer *time.Timer
|
timer *time.Timer
|
||||||
cancel chan struct{} // Must be non-nil
|
cancel chan struct{} // Must be non-nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func makePipeDeadline() pipeDeadline {
|
func MakePipeDeadline() PipeDeadline {
|
||||||
return pipeDeadline{cancel: make(chan struct{})}
|
return PipeDeadline{cancel: make(chan struct{})}
|
||||||
}
|
}
|
||||||
|
|
||||||
// set sets the point in time when the deadline will time out.
|
// Set sets the point in time when the deadline will time out.
|
||||||
// A timeout event is signaled by closing the channel returned by waiter.
|
// A timeout event is signaled by closing the channel returned by waiter.
|
||||||
// Once a timeout has occurred, the deadline can be refreshed by specifying a
|
// Once a timeout has occurred, the deadline can be refreshed by specifying a
|
||||||
// t value in the future.
|
// t value in the future.
|
||||||
//
|
//
|
||||||
// A zero value for t prevents timeout.
|
// A zero value for t prevents timeout.
|
||||||
func (d *pipeDeadline) set(t time.Time) {
|
func (d *PipeDeadline) Set(t time.Time) {
|
||||||
d.mu.Lock()
|
d.mu.Lock()
|
||||||
defer d.mu.Unlock()
|
defer d.mu.Unlock()
|
||||||
|
|
||||||
@@ -61,8 +61,8 @@ func (d *pipeDeadline) set(t time.Time) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// wait returns a channel that is closed when the deadline is exceeded.
|
// Wait returns a channel that is closed when the deadline is exceeded.
|
||||||
func (d *pipeDeadline) wait() chan struct{} {
|
func (d *PipeDeadline) Wait() chan struct{} {
|
||||||
d.mu.Lock()
|
d.mu.Lock()
|
||||||
defer d.mu.Unlock()
|
defer d.mu.Unlock()
|
||||||
return d.cancel
|
return d.cancel
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/sagernet/sing/common/buf"
|
"github.com/metacubex/sing/common/buf"
|
||||||
N "github.com/sagernet/sing/common/network"
|
N "github.com/metacubex/sing/common/network"
|
||||||
)
|
)
|
||||||
|
|
||||||
type pipeAddr struct{}
|
type pipeAddr struct{}
|
||||||
@@ -33,8 +33,8 @@ type pipe struct {
|
|||||||
localDone chan struct{}
|
localDone chan struct{}
|
||||||
remoteDone <-chan struct{}
|
remoteDone <-chan struct{}
|
||||||
|
|
||||||
readDeadline pipeDeadline
|
readDeadline PipeDeadline
|
||||||
writeDeadline pipeDeadline
|
writeDeadline PipeDeadline
|
||||||
|
|
||||||
readWaitOptions N.ReadWaitOptions
|
readWaitOptions N.ReadWaitOptions
|
||||||
}
|
}
|
||||||
@@ -56,15 +56,15 @@ func Pipe() (net.Conn, net.Conn) {
|
|||||||
rdRx: cb1, rdTx: cn1,
|
rdRx: cb1, rdTx: cn1,
|
||||||
wrTx: cb2, wrRx: cn2,
|
wrTx: cb2, wrRx: cn2,
|
||||||
localDone: done1, remoteDone: done2,
|
localDone: done1, remoteDone: done2,
|
||||||
readDeadline: makePipeDeadline(),
|
readDeadline: MakePipeDeadline(),
|
||||||
writeDeadline: makePipeDeadline(),
|
writeDeadline: MakePipeDeadline(),
|
||||||
}
|
}
|
||||||
p2 := &pipe{
|
p2 := &pipe{
|
||||||
rdRx: cb2, rdTx: cn2,
|
rdRx: cb2, rdTx: cn2,
|
||||||
wrTx: cb1, wrRx: cn1,
|
wrTx: cb1, wrRx: cn1,
|
||||||
localDone: done2, remoteDone: done1,
|
localDone: done2, remoteDone: done1,
|
||||||
readDeadline: makePipeDeadline(),
|
readDeadline: MakePipeDeadline(),
|
||||||
writeDeadline: makePipeDeadline(),
|
writeDeadline: MakePipeDeadline(),
|
||||||
}
|
}
|
||||||
return p1, p2
|
return p1, p2
|
||||||
}
|
}
|
||||||
@@ -86,7 +86,7 @@ func (p *pipe) read(b []byte) (n int, err error) {
|
|||||||
return 0, io.ErrClosedPipe
|
return 0, io.ErrClosedPipe
|
||||||
case isClosedChan(p.remoteDone):
|
case isClosedChan(p.remoteDone):
|
||||||
return 0, io.EOF
|
return 0, io.EOF
|
||||||
case isClosedChan(p.readDeadline.wait()):
|
case isClosedChan(p.readDeadline.Wait()):
|
||||||
return 0, os.ErrDeadlineExceeded
|
return 0, os.ErrDeadlineExceeded
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,7 +99,7 @@ func (p *pipe) read(b []byte) (n int, err error) {
|
|||||||
return 0, io.ErrClosedPipe
|
return 0, io.ErrClosedPipe
|
||||||
case <-p.remoteDone:
|
case <-p.remoteDone:
|
||||||
return 0, io.EOF
|
return 0, io.EOF
|
||||||
case <-p.readDeadline.wait():
|
case <-p.readDeadline.Wait():
|
||||||
return 0, os.ErrDeadlineExceeded
|
return 0, os.ErrDeadlineExceeded
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -118,7 +118,7 @@ func (p *pipe) write(b []byte) (n int, err error) {
|
|||||||
return 0, io.ErrClosedPipe
|
return 0, io.ErrClosedPipe
|
||||||
case isClosedChan(p.remoteDone):
|
case isClosedChan(p.remoteDone):
|
||||||
return 0, io.ErrClosedPipe
|
return 0, io.ErrClosedPipe
|
||||||
case isClosedChan(p.writeDeadline.wait()):
|
case isClosedChan(p.writeDeadline.Wait()):
|
||||||
return 0, os.ErrDeadlineExceeded
|
return 0, os.ErrDeadlineExceeded
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,7 +134,7 @@ func (p *pipe) write(b []byte) (n int, err error) {
|
|||||||
return n, io.ErrClosedPipe
|
return n, io.ErrClosedPipe
|
||||||
case <-p.remoteDone:
|
case <-p.remoteDone:
|
||||||
return n, io.ErrClosedPipe
|
return n, io.ErrClosedPipe
|
||||||
case <-p.writeDeadline.wait():
|
case <-p.writeDeadline.Wait():
|
||||||
return n, os.ErrDeadlineExceeded
|
return n, os.ErrDeadlineExceeded
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -145,8 +145,8 @@ func (p *pipe) SetDeadline(t time.Time) error {
|
|||||||
if isClosedChan(p.localDone) || isClosedChan(p.remoteDone) {
|
if isClosedChan(p.localDone) || isClosedChan(p.remoteDone) {
|
||||||
return io.ErrClosedPipe
|
return io.ErrClosedPipe
|
||||||
}
|
}
|
||||||
p.readDeadline.set(t)
|
p.readDeadline.Set(t)
|
||||||
p.writeDeadline.set(t)
|
p.writeDeadline.Set(t)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,7 +154,7 @@ func (p *pipe) SetReadDeadline(t time.Time) error {
|
|||||||
if isClosedChan(p.localDone) || isClosedChan(p.remoteDone) {
|
if isClosedChan(p.localDone) || isClosedChan(p.remoteDone) {
|
||||||
return io.ErrClosedPipe
|
return io.ErrClosedPipe
|
||||||
}
|
}
|
||||||
p.readDeadline.set(t)
|
p.readDeadline.Set(t)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,7 +162,7 @@ func (p *pipe) SetWriteDeadline(t time.Time) error {
|
|||||||
if isClosedChan(p.localDone) || isClosedChan(p.remoteDone) {
|
if isClosedChan(p.localDone) || isClosedChan(p.remoteDone) {
|
||||||
return io.ErrClosedPipe
|
return io.ErrClosedPipe
|
||||||
}
|
}
|
||||||
p.writeDeadline.set(t)
|
p.writeDeadline.Set(t)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,7 +192,7 @@ func (p *pipe) waitReadBuffer() (buffer *buf.Buffer, err error) {
|
|||||||
return nil, io.ErrClosedPipe
|
return nil, io.ErrClosedPipe
|
||||||
case isClosedChan(p.remoteDone):
|
case isClosedChan(p.remoteDone):
|
||||||
return nil, io.EOF
|
return nil, io.EOF
|
||||||
case isClosedChan(p.readDeadline.wait()):
|
case isClosedChan(p.readDeadline.Wait()):
|
||||||
return nil, os.ErrDeadlineExceeded
|
return nil, os.ErrDeadlineExceeded
|
||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
@@ -211,7 +211,7 @@ func (p *pipe) waitReadBuffer() (buffer *buf.Buffer, err error) {
|
|||||||
return nil, io.ErrClosedPipe
|
return nil, io.ErrClosedPipe
|
||||||
case <-p.remoteDone:
|
case <-p.remoteDone:
|
||||||
return nil, io.EOF
|
return nil, io.EOF
|
||||||
case <-p.readDeadline.wait():
|
case <-p.readDeadline.Wait():
|
||||||
return nil, os.ErrDeadlineExceeded
|
return nil, os.ErrDeadlineExceeded
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
90
common/net/listener.go
Normal file
90
common/net/listener.go
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
package net
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
type handleContextListener struct {
|
||||||
|
net.Listener
|
||||||
|
ctx context.Context
|
||||||
|
cancel context.CancelFunc
|
||||||
|
conns chan net.Conn
|
||||||
|
err error
|
||||||
|
once sync.Once
|
||||||
|
handle func(context.Context, net.Conn) (net.Conn, error)
|
||||||
|
panicLog func(any)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *handleContextListener) init() {
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
c, err := l.Listener.Accept()
|
||||||
|
if err != nil {
|
||||||
|
l.err = err
|
||||||
|
close(l.conns)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
if l.panicLog != nil {
|
||||||
|
l.panicLog(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
if conn, err := l.handle(l.ctx, c); err == nil {
|
||||||
|
l.conns <- conn
|
||||||
|
} else {
|
||||||
|
// handle failed, close the underlying connection.
|
||||||
|
_ = c.Close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *handleContextListener) Accept() (net.Conn, error) {
|
||||||
|
l.once.Do(l.init)
|
||||||
|
if c, ok := <-l.conns; ok {
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
return nil, l.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *handleContextListener) Close() error {
|
||||||
|
l.cancel()
|
||||||
|
l.once.Do(func() { // l.init has not been called yet, so close related resources directly.
|
||||||
|
l.err = net.ErrClosed
|
||||||
|
close(l.conns)
|
||||||
|
})
|
||||||
|
defer func() {
|
||||||
|
// at here, listener has been closed, so we should close all connections in the channel
|
||||||
|
for c := range l.conns {
|
||||||
|
go func(c net.Conn) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
if l.panicLog != nil {
|
||||||
|
l.panicLog(r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
_ = c.Close()
|
||||||
|
}(c)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return l.Listener.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHandleContextListener(ctx context.Context, l net.Listener, handle func(context.Context, net.Conn) (net.Conn, error), panicLog func(any)) net.Listener {
|
||||||
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
return &handleContextListener{
|
||||||
|
Listener: l,
|
||||||
|
ctx: ctx,
|
||||||
|
cancel: cancel,
|
||||||
|
conns: make(chan net.Conn),
|
||||||
|
handle: handle,
|
||||||
|
panicLog: panicLog,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,10 +3,10 @@ package packet
|
|||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/sagernet/sing/common/buf"
|
"github.com/metacubex/sing/common/buf"
|
||||||
"github.com/sagernet/sing/common/bufio"
|
"github.com/metacubex/sing/common/bufio"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/metacubex/sing/common/metadata"
|
||||||
N "github.com/sagernet/sing/common/network"
|
N "github.com/metacubex/sing/common/network"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SingPacketConn = N.NetPacketConn
|
type SingPacketConn = N.NetPacketConn
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ package packet
|
|||||||
import (
|
import (
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
"github.com/sagernet/sing/common/buf"
|
"github.com/metacubex/sing/common/buf"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/metacubex/sing/common/metadata"
|
||||||
N "github.com/sagernet/sing/common/network"
|
N "github.com/metacubex/sing/common/network"
|
||||||
)
|
)
|
||||||
|
|
||||||
type refSingPacketConn struct {
|
type refSingPacketConn struct {
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
package packet
|
package packet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/sagernet/sing/common/buf"
|
"github.com/metacubex/sing/common/buf"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/metacubex/sing/common/metadata"
|
||||||
N "github.com/sagernet/sing/common/network"
|
N "github.com/metacubex/sing/common/network"
|
||||||
)
|
)
|
||||||
|
|
||||||
type threadSafeSingPacketConn struct {
|
type threadSafeSingPacketConn struct {
|
||||||
|
|||||||
@@ -1,15 +1,14 @@
|
|||||||
package net
|
package net
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"runtime"
|
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/net/deadline"
|
"github.com/metacubex/mihomo/common/net/deadline"
|
||||||
|
|
||||||
"github.com/sagernet/sing/common"
|
"github.com/metacubex/sing/common"
|
||||||
"github.com/sagernet/sing/common/bufio"
|
"github.com/metacubex/sing/common/bufio"
|
||||||
"github.com/sagernet/sing/common/network"
|
"github.com/metacubex/sing/common/network"
|
||||||
)
|
)
|
||||||
|
|
||||||
var NewExtendedConn = bufio.NewExtendedConn
|
var NewExtendedConn = bufio.NewExtendedConn
|
||||||
@@ -22,11 +21,26 @@ type ExtendedReader = network.ExtendedReader
|
|||||||
|
|
||||||
var WriteBuffer = bufio.WriteBuffer
|
var WriteBuffer = bufio.WriteBuffer
|
||||||
|
|
||||||
|
type ReadWaitOptions = network.ReadWaitOptions
|
||||||
|
|
||||||
|
var NewReadWaitOptions = network.NewReadWaitOptions
|
||||||
|
var CalculateFrontHeadroom = network.CalculateFrontHeadroom
|
||||||
|
var CalculateRearHeadroom = network.CalculateRearHeadroom
|
||||||
|
|
||||||
|
type ReaderWithUpstream = network.ReaderWithUpstream
|
||||||
|
type WithUpstreamReader = network.WithUpstreamReader
|
||||||
|
type WriterWithUpstream = network.WriterWithUpstream
|
||||||
|
type WithUpstreamWriter = network.WithUpstreamWriter
|
||||||
|
type WithUpstream = common.WithUpstream
|
||||||
|
|
||||||
|
var UnwrapReader = network.UnwrapReader
|
||||||
|
var UnwrapWriter = network.UnwrapWriter
|
||||||
|
|
||||||
func NewDeadlineConn(conn net.Conn) ExtendedConn {
|
func NewDeadlineConn(conn net.Conn) ExtendedConn {
|
||||||
if deadline.IsPipe(conn) || deadline.IsPipe(network.UnwrapReader(conn)) {
|
if deadline.IsPipe(conn) || deadline.IsPipe(UnwrapReader(conn)) {
|
||||||
return NewExtendedConn(conn) // pipe always have correctly deadline implement
|
return NewExtendedConn(conn) // pipe always have correctly deadline implement
|
||||||
}
|
}
|
||||||
if deadline.IsConn(conn) || deadline.IsConn(network.UnwrapReader(conn)) {
|
if deadline.IsConn(conn) || deadline.IsConn(UnwrapReader(conn)) {
|
||||||
return NewExtendedConn(conn) // was a *deadline.Conn
|
return NewExtendedConn(conn) // was a *deadline.Conn
|
||||||
}
|
}
|
||||||
return deadline.NewConn(conn)
|
return deadline.NewConn(conn)
|
||||||
@@ -43,9 +57,37 @@ type CountFunc = network.CountFunc
|
|||||||
|
|
||||||
var Pipe = deadline.Pipe
|
var Pipe = deadline.Pipe
|
||||||
|
|
||||||
// Relay copies between left and right bidirectionally.
|
func closeWrite(writer io.Closer) error {
|
||||||
func Relay(leftConn, rightConn net.Conn) {
|
if c, ok := common.Cast[network.WriteCloser](writer); ok {
|
||||||
defer runtime.KeepAlive(leftConn)
|
return c.CloseWrite()
|
||||||
defer runtime.KeepAlive(rightConn)
|
}
|
||||||
_ = bufio.CopyConn(context.TODO(), leftConn, rightConn)
|
return writer.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Relay copies between left and right bidirectionally.
|
||||||
|
// like [bufio.CopyConn] but remove unneeded [context.Context] handle and the cost of [task.Group]
|
||||||
|
func Relay(leftConn, rightConn net.Conn) {
|
||||||
|
defer func() {
|
||||||
|
_ = leftConn.Close()
|
||||||
|
_ = rightConn.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
ch := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
_, err := bufio.Copy(leftConn, rightConn)
|
||||||
|
if err == nil {
|
||||||
|
_ = closeWrite(leftConn)
|
||||||
|
} else {
|
||||||
|
_ = leftConn.Close()
|
||||||
|
}
|
||||||
|
close(ch)
|
||||||
|
}()
|
||||||
|
|
||||||
|
_, err := bufio.Copy(rightConn, leftConn)
|
||||||
|
if err == nil {
|
||||||
|
_ = closeWrite(rightConn)
|
||||||
|
} else {
|
||||||
|
_ = rightConn.Close()
|
||||||
|
}
|
||||||
|
<-ch
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,65 +0,0 @@
|
|||||||
package net
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/rand"
|
|
||||||
"crypto/rsa"
|
|
||||||
"crypto/sha256"
|
|
||||||
"crypto/tls"
|
|
||||||
"crypto/x509"
|
|
||||||
"encoding/hex"
|
|
||||||
"encoding/pem"
|
|
||||||
"fmt"
|
|
||||||
"math/big"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Path interface {
|
|
||||||
Resolve(path string) string
|
|
||||||
}
|
|
||||||
|
|
||||||
func ParseCert(certificate, privateKey string, path Path) (tls.Certificate, error) {
|
|
||||||
if certificate == "" && privateKey == "" {
|
|
||||||
var err error
|
|
||||||
certificate, privateKey, _, err = NewRandomTLSKeyPair()
|
|
||||||
if err != nil {
|
|
||||||
return tls.Certificate{}, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cert, painTextErr := tls.X509KeyPair([]byte(certificate), []byte(privateKey))
|
|
||||||
if painTextErr == nil {
|
|
||||||
return cert, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
certificate = path.Resolve(certificate)
|
|
||||||
privateKey = path.Resolve(privateKey)
|
|
||||||
cert, loadErr := tls.LoadX509KeyPair(certificate, privateKey)
|
|
||||||
if loadErr != nil {
|
|
||||||
return tls.Certificate{}, fmt.Errorf("parse certificate failed, maybe format error:%s, or path error: %s", painTextErr.Error(), loadErr.Error())
|
|
||||||
}
|
|
||||||
return cert, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewRandomTLSKeyPair() (certificate string, privateKey string, fingerprint string, err error) {
|
|
||||||
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
template := x509.Certificate{SerialNumber: big.NewInt(1)}
|
|
||||||
certDER, err := x509.CreateCertificate(
|
|
||||||
rand.Reader,
|
|
||||||
&template,
|
|
||||||
&template,
|
|
||||||
&key.PublicKey,
|
|
||||||
key)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
cert, err := x509.ParseCertificate(certDER)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
hash := sha256.Sum256(cert.Raw)
|
|
||||||
fingerprint = hex.EncodeToString(hash[:])
|
|
||||||
privateKey = string(pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)}))
|
|
||||||
certificate = string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER}))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package net
|
package net
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha1"
|
||||||
|
"encoding/base64"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"math/bits"
|
"math/bits"
|
||||||
)
|
)
|
||||||
@@ -129,3 +131,13 @@ func MaskWebSocket(key uint32, b []byte) uint32 {
|
|||||||
|
|
||||||
return key
|
return key
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetWebSocketSecAccept(secKey string) string {
|
||||||
|
const magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
|
||||||
|
const nonceSize = 24 // base64.StdEncoding.EncodedLen(nonceKeySize)
|
||||||
|
p := make([]byte, nonceSize+len(magic))
|
||||||
|
copy(p[:nonceSize], secKey)
|
||||||
|
copy(p[nonceSize:], magic)
|
||||||
|
sum := sha1.Sum(p)
|
||||||
|
return base64.StdEncoding.EncodeToString(sum[:])
|
||||||
|
}
|
||||||
|
|||||||
102
common/once/oncefunc.go
Normal file
102
common/once/oncefunc.go
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
// Copyright 2022 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package once
|
||||||
|
|
||||||
|
import "sync"
|
||||||
|
|
||||||
|
// OnceFunc returns a function that invokes f only once. The returned function
|
||||||
|
// may be called concurrently.
|
||||||
|
//
|
||||||
|
// If f panics, the returned function will panic with the same value on every call.
|
||||||
|
func OnceFunc(f func()) func() {
|
||||||
|
var (
|
||||||
|
once sync.Once
|
||||||
|
valid bool
|
||||||
|
p any
|
||||||
|
)
|
||||||
|
// Construct the inner closure just once to reduce costs on the fast path.
|
||||||
|
g := func() {
|
||||||
|
defer func() {
|
||||||
|
p = recover()
|
||||||
|
if !valid {
|
||||||
|
// Re-panic immediately so on the first call the user gets a
|
||||||
|
// complete stack trace into f.
|
||||||
|
panic(p)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
f()
|
||||||
|
f = nil // Do not keep f alive after invoking it.
|
||||||
|
valid = true // Set only if f does not panic.
|
||||||
|
}
|
||||||
|
return func() {
|
||||||
|
once.Do(g)
|
||||||
|
if !valid {
|
||||||
|
panic(p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnceValue returns a function that invokes f only once and returns the value
|
||||||
|
// returned by f. The returned function may be called concurrently.
|
||||||
|
//
|
||||||
|
// If f panics, the returned function will panic with the same value on every call.
|
||||||
|
func OnceValue[T any](f func() T) func() T {
|
||||||
|
var (
|
||||||
|
once sync.Once
|
||||||
|
valid bool
|
||||||
|
p any
|
||||||
|
result T
|
||||||
|
)
|
||||||
|
g := func() {
|
||||||
|
defer func() {
|
||||||
|
p = recover()
|
||||||
|
if !valid {
|
||||||
|
panic(p)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
result = f()
|
||||||
|
f = nil
|
||||||
|
valid = true
|
||||||
|
}
|
||||||
|
return func() T {
|
||||||
|
once.Do(g)
|
||||||
|
if !valid {
|
||||||
|
panic(p)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnceValues returns a function that invokes f only once and returns the values
|
||||||
|
// returned by f. The returned function may be called concurrently.
|
||||||
|
//
|
||||||
|
// If f panics, the returned function will panic with the same value on every call.
|
||||||
|
func OnceValues[T1, T2 any](f func() (T1, T2)) func() (T1, T2) {
|
||||||
|
var (
|
||||||
|
once sync.Once
|
||||||
|
valid bool
|
||||||
|
p any
|
||||||
|
r1 T1
|
||||||
|
r2 T2
|
||||||
|
)
|
||||||
|
g := func() {
|
||||||
|
defer func() {
|
||||||
|
p = recover()
|
||||||
|
if !valid {
|
||||||
|
panic(p)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
r1, r2 = f()
|
||||||
|
f = nil
|
||||||
|
valid = true
|
||||||
|
}
|
||||||
|
return func() (T1, T2) {
|
||||||
|
once.Do(g)
|
||||||
|
if !valid {
|
||||||
|
panic(p)
|
||||||
|
}
|
||||||
|
return r1, r2
|
||||||
|
}
|
||||||
|
}
|
||||||
8
common/orderedmap/doc.go
Normal file
8
common/orderedmap/doc.go
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package orderedmap
|
||||||
|
|
||||||
|
// copy and modified from https://github.com/wk8/go-ordered-map/tree/v2.1.8
|
||||||
|
// which is licensed under Apache v2.
|
||||||
|
//
|
||||||
|
// mihomo modified:
|
||||||
|
// 1. remove dependence of mailru/easyjson for MarshalJSON
|
||||||
|
// 2. remove dependence of buger/jsonparser for UnmarshalJSON
|
||||||
139
common/orderedmap/json.go
Normal file
139
common/orderedmap/json.go
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
package orderedmap
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ json.Marshaler = &OrderedMap[int, any]{}
|
||||||
|
_ json.Unmarshaler = &OrderedMap[int, any]{}
|
||||||
|
)
|
||||||
|
|
||||||
|
// MarshalJSON implements the json.Marshaler interface.
|
||||||
|
func (om *OrderedMap[K, V]) MarshalJSON() ([]byte, error) { //nolint:funlen
|
||||||
|
if om == nil || om.list == nil {
|
||||||
|
return []byte("null"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var buf bytes.Buffer
|
||||||
|
buf.WriteByte('{')
|
||||||
|
enc := json.NewEncoder(&buf)
|
||||||
|
for pair, firstIteration := om.Oldest(), true; pair != nil; pair = pair.Next() {
|
||||||
|
if firstIteration {
|
||||||
|
firstIteration = false
|
||||||
|
} else {
|
||||||
|
buf.WriteByte(',')
|
||||||
|
}
|
||||||
|
|
||||||
|
switch key := any(pair.Key).(type) {
|
||||||
|
case string, encoding.TextMarshaler:
|
||||||
|
if err := enc.Encode(pair.Key); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
|
||||||
|
buf.WriteByte('"')
|
||||||
|
buf.WriteString(fmt.Sprint(key))
|
||||||
|
buf.WriteByte('"')
|
||||||
|
default:
|
||||||
|
// this switch takes care of wrapper types around primitive types, such as
|
||||||
|
// type myType string
|
||||||
|
switch keyValue := reflect.ValueOf(key); keyValue.Type().Kind() {
|
||||||
|
case reflect.String:
|
||||||
|
if err := enc.Encode(pair.Key); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||||
|
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||||
|
buf.WriteByte('"')
|
||||||
|
buf.WriteString(fmt.Sprint(key))
|
||||||
|
buf.WriteByte('"')
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("unsupported key type: %T", key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buf.WriteByte(':')
|
||||||
|
if err := enc.Encode(pair.Value); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buf.WriteByte('}')
|
||||||
|
return buf.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
||||||
|
func (om *OrderedMap[K, V]) UnmarshalJSON(data []byte) error {
|
||||||
|
if om.list == nil {
|
||||||
|
om.initialize(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
d := json.NewDecoder(bytes.NewReader(data))
|
||||||
|
tok, err := d.Token()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if tok != json.Delim('{') {
|
||||||
|
return errors.New("expect JSON object open with '{'")
|
||||||
|
}
|
||||||
|
|
||||||
|
for d.More() {
|
||||||
|
// key
|
||||||
|
tok, err = d.Token()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
keyStr, ok := tok.(string)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("key must be a string, got %T\n", tok)
|
||||||
|
}
|
||||||
|
|
||||||
|
var key K
|
||||||
|
switch typedKey := any(&key).(type) {
|
||||||
|
case *string:
|
||||||
|
*typedKey = keyStr
|
||||||
|
case encoding.TextUnmarshaler:
|
||||||
|
err = typedKey.UnmarshalText([]byte(keyStr))
|
||||||
|
case *int, *int8, *int16, *int32, *int64, *uint, *uint8, *uint16, *uint32, *uint64:
|
||||||
|
err = json.Unmarshal([]byte(keyStr), typedKey)
|
||||||
|
default:
|
||||||
|
// this switch takes care of wrapper types around primitive types, such as
|
||||||
|
// type myType string
|
||||||
|
switch reflect.TypeOf(key).Kind() {
|
||||||
|
case reflect.String:
|
||||||
|
convertedKeyData := reflect.ValueOf(keyStr).Convert(reflect.TypeOf(key))
|
||||||
|
reflect.ValueOf(&key).Elem().Set(convertedKeyData)
|
||||||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||||
|
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||||
|
err = json.Unmarshal([]byte(keyStr), &key)
|
||||||
|
default:
|
||||||
|
err = fmt.Errorf("unsupported key type: %T", key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// value
|
||||||
|
value, _ := om.Get(key)
|
||||||
|
err = d.Decode(&value)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
om.Set(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
tok, err = d.Token()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if tok != json.Delim('}') {
|
||||||
|
return errors.New("expect JSON object close with '}'")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
117
common/orderedmap/json_fuzz_test.go
Normal file
117
common/orderedmap/json_fuzz_test.go
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
package orderedmap
|
||||||
|
|
||||||
|
// Adapted from https://github.com/dvyukov/go-fuzz-corpus/blob/c42c1b2/json/json.go
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func FuzzRoundTripJSON(f *testing.F) {
|
||||||
|
f.Fuzz(func(t *testing.T, data []byte) {
|
||||||
|
for _, testCase := range []struct {
|
||||||
|
name string
|
||||||
|
constructor func() any
|
||||||
|
// should be a function that asserts that 2 objects of the type returned by constructor are equal
|
||||||
|
equalityAssertion func(*testing.T, any, any) bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "with a string -> string map",
|
||||||
|
constructor: func() any { return &OrderedMap[string, string]{} },
|
||||||
|
equalityAssertion: assertOrderedMapsEqual[string, string],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "with a string -> int map",
|
||||||
|
constructor: func() any { return &OrderedMap[string, int]{} },
|
||||||
|
equalityAssertion: assertOrderedMapsEqual[string, int],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "with a string -> any map",
|
||||||
|
constructor: func() any { return &OrderedMap[string, any]{} },
|
||||||
|
equalityAssertion: assertOrderedMapsEqual[string, any],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "with a struct with map fields",
|
||||||
|
constructor: func() any { return new(testFuzzStruct) },
|
||||||
|
equalityAssertion: assertTestFuzzStructEqual,
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
t.Run(testCase.name, func(t *testing.T) {
|
||||||
|
v1 := testCase.constructor()
|
||||||
|
if json.Unmarshal(data, v1) != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonData, err := json.Marshal(v1)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
v2 := testCase.constructor()
|
||||||
|
require.NoError(t, json.Unmarshal(jsonData, v2))
|
||||||
|
|
||||||
|
if !assert.True(t, testCase.equalityAssertion(t, v1, v2), "failed with input data %q", string(data)) {
|
||||||
|
// look at that what the standard lib does with regular map, to help with debugging
|
||||||
|
|
||||||
|
var m1 map[string]any
|
||||||
|
require.NoError(t, json.Unmarshal(data, &m1))
|
||||||
|
|
||||||
|
mapJsonData, err := json.Marshal(m1)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
var m2 map[string]any
|
||||||
|
require.NoError(t, json.Unmarshal(mapJsonData, &m2))
|
||||||
|
|
||||||
|
t.Logf("initial data = %s", string(data))
|
||||||
|
t.Logf("unmarshalled map = %v", m1)
|
||||||
|
t.Logf("re-marshalled from map = %s", string(mapJsonData))
|
||||||
|
t.Logf("re-marshalled from test obj = %s", string(jsonData))
|
||||||
|
t.Logf("re-unmarshalled map = %s", m2)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// only works for fairly basic maps, that's why it's just in this file
|
||||||
|
func assertOrderedMapsEqual[K comparable, V any](t *testing.T, v1, v2 any) bool {
|
||||||
|
om1, ok1 := v1.(*OrderedMap[K, V])
|
||||||
|
om2, ok2 := v2.(*OrderedMap[K, V])
|
||||||
|
|
||||||
|
if !assert.True(t, ok1, "v1 not an orderedmap") ||
|
||||||
|
!assert.True(t, ok2, "v2 not an orderedmap") {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
success := assert.Equal(t, om1.Len(), om2.Len(), "om1 and om2 have different lengths: %d vs %d", om1.Len(), om2.Len())
|
||||||
|
|
||||||
|
for i, pair1, pair2 := 0, om1.Oldest(), om2.Oldest(); pair1 != nil && pair2 != nil; i, pair1, pair2 = i+1, pair1.Next(), pair2.Next() {
|
||||||
|
success = assert.Equal(t, pair1.Key, pair2.Key, "different keys at position %d: %v vs %v", i, pair1.Key, pair2.Key) && success
|
||||||
|
success = assert.Equal(t, pair1.Value, pair2.Value, "different values at position %d: %v vs %v", i, pair1.Value, pair2.Value) && success
|
||||||
|
}
|
||||||
|
|
||||||
|
return success
|
||||||
|
}
|
||||||
|
|
||||||
|
type testFuzzStruct struct {
|
||||||
|
M1 *OrderedMap[int, any]
|
||||||
|
M2 *OrderedMap[int, string]
|
||||||
|
M3 *OrderedMap[string, string]
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertTestFuzzStructEqual(t *testing.T, v1, v2 any) bool {
|
||||||
|
s1, ok := v1.(*testFuzzStruct)
|
||||||
|
s2, ok := v2.(*testFuzzStruct)
|
||||||
|
|
||||||
|
if !assert.True(t, ok, "v1 not an testFuzzStruct") ||
|
||||||
|
!assert.True(t, ok, "v2 not an testFuzzStruct") {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
success := assertOrderedMapsEqual[int, any](t, s1.M1, s2.M1)
|
||||||
|
success = assertOrderedMapsEqual[int, string](t, s1.M2, s2.M2) && success
|
||||||
|
success = assertOrderedMapsEqual[string, string](t, s1.M3, s2.M3) && success
|
||||||
|
|
||||||
|
return success
|
||||||
|
}
|
||||||
338
common/orderedmap/json_test.go
Normal file
338
common/orderedmap/json_test.go
Normal file
@@ -0,0 +1,338 @@
|
|||||||
|
package orderedmap
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// to test marshalling TextMarshalers and unmarshalling TextUnmarshalers
|
||||||
|
type marshallable int
|
||||||
|
|
||||||
|
func (m marshallable) MarshalText() ([]byte, error) {
|
||||||
|
return []byte(fmt.Sprintf("#%d#", m)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *marshallable) UnmarshalText(text []byte) error {
|
||||||
|
if len(text) < 3 {
|
||||||
|
return errors.New("too short")
|
||||||
|
}
|
||||||
|
if text[0] != '#' || text[len(text)-1] != '#' {
|
||||||
|
return errors.New("missing prefix or suffix")
|
||||||
|
}
|
||||||
|
|
||||||
|
value, err := strconv.Atoi(string(text[1 : len(text)-1]))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
*m = marshallable(value)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMarshalJSON(t *testing.T) {
|
||||||
|
t.Run("int key", func(t *testing.T) {
|
||||||
|
om := New[int, any]()
|
||||||
|
om.Set(1, "bar")
|
||||||
|
om.Set(7, "baz")
|
||||||
|
om.Set(2, 28)
|
||||||
|
om.Set(3, 100)
|
||||||
|
om.Set(4, "baz")
|
||||||
|
om.Set(5, "28")
|
||||||
|
om.Set(6, "100")
|
||||||
|
om.Set(8, "baz")
|
||||||
|
om.Set(8, "baz")
|
||||||
|
om.Set(9, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque auctor augue accumsan mi maximus, quis viverra massa pretium. Phasellus imperdiet sapien a interdum sollicitudin. Duis at commodo lectus, a lacinia sem.")
|
||||||
|
|
||||||
|
b, err := json.Marshal(om)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, `{"1":"bar","7":"baz","2":28,"3":100,"4":"baz","5":"28","6":"100","8":"baz","9":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque auctor augue accumsan mi maximus, quis viverra massa pretium. Phasellus imperdiet sapien a interdum sollicitudin. Duis at commodo lectus, a lacinia sem."}`, string(b))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("string key", func(t *testing.T) {
|
||||||
|
om := New[string, any]()
|
||||||
|
om.Set("test", "bar")
|
||||||
|
om.Set("abc", true)
|
||||||
|
|
||||||
|
b, err := json.Marshal(om)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, `{"test":"bar","abc":true}`, string(b))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("typed string key", func(t *testing.T) {
|
||||||
|
type myString string
|
||||||
|
om := New[myString, any]()
|
||||||
|
om.Set("test", "bar")
|
||||||
|
om.Set("abc", true)
|
||||||
|
|
||||||
|
b, err := json.Marshal(om)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, `{"test":"bar","abc":true}`, string(b))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("typed int key", func(t *testing.T) {
|
||||||
|
type myInt uint32
|
||||||
|
om := New[myInt, any]()
|
||||||
|
om.Set(1, "bar")
|
||||||
|
om.Set(7, "baz")
|
||||||
|
om.Set(2, 28)
|
||||||
|
om.Set(3, 100)
|
||||||
|
om.Set(4, "baz")
|
||||||
|
|
||||||
|
b, err := json.Marshal(om)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, `{"1":"bar","7":"baz","2":28,"3":100,"4":"baz"}`, string(b))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("TextMarshaller key", func(t *testing.T) {
|
||||||
|
om := New[marshallable, any]()
|
||||||
|
om.Set(marshallable(1), "bar")
|
||||||
|
om.Set(marshallable(28), true)
|
||||||
|
|
||||||
|
b, err := json.Marshal(om)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, `{"#1#":"bar","#28#":true}`, string(b))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("empty map", func(t *testing.T) {
|
||||||
|
om := New[string, any]()
|
||||||
|
|
||||||
|
b, err := json.Marshal(om)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, `{}`, string(b))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnmarshallJSON(t *testing.T) {
|
||||||
|
t.Run("int key", func(t *testing.T) {
|
||||||
|
data := `{"1":"bar","7":"baz","2":28,"3":100,"4":"baz","5":"28","6":"100","8":"baz"}`
|
||||||
|
|
||||||
|
om := New[int, any]()
|
||||||
|
require.NoError(t, json.Unmarshal([]byte(data), &om))
|
||||||
|
|
||||||
|
assertOrderedPairsEqual(t, om,
|
||||||
|
[]int{1, 7, 2, 3, 4, 5, 6, 8},
|
||||||
|
[]any{"bar", "baz", float64(28), float64(100), "baz", "28", "100", "baz"})
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("string key", func(t *testing.T) {
|
||||||
|
data := `{"test":"bar","abc":true}`
|
||||||
|
|
||||||
|
om := New[string, any]()
|
||||||
|
require.NoError(t, json.Unmarshal([]byte(data), &om))
|
||||||
|
|
||||||
|
assertOrderedPairsEqual(t, om,
|
||||||
|
[]string{"test", "abc"},
|
||||||
|
[]any{"bar", true})
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("typed string key", func(t *testing.T) {
|
||||||
|
data := `{"test":"bar","abc":true}`
|
||||||
|
|
||||||
|
type myString string
|
||||||
|
om := New[myString, any]()
|
||||||
|
require.NoError(t, json.Unmarshal([]byte(data), &om))
|
||||||
|
|
||||||
|
assertOrderedPairsEqual(t, om,
|
||||||
|
[]myString{"test", "abc"},
|
||||||
|
[]any{"bar", true})
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("typed int key", func(t *testing.T) {
|
||||||
|
data := `{"1":"bar","7":"baz","2":28,"3":100,"4":"baz","5":"28","6":"100","8":"baz"}`
|
||||||
|
|
||||||
|
type myInt uint32
|
||||||
|
om := New[myInt, any]()
|
||||||
|
require.NoError(t, json.Unmarshal([]byte(data), &om))
|
||||||
|
|
||||||
|
assertOrderedPairsEqual(t, om,
|
||||||
|
[]myInt{1, 7, 2, 3, 4, 5, 6, 8},
|
||||||
|
[]any{"bar", "baz", float64(28), float64(100), "baz", "28", "100", "baz"})
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("TextUnmarshaler key", func(t *testing.T) {
|
||||||
|
data := `{"#1#":"bar","#28#":true}`
|
||||||
|
|
||||||
|
om := New[marshallable, any]()
|
||||||
|
require.NoError(t, json.Unmarshal([]byte(data), &om))
|
||||||
|
|
||||||
|
assertOrderedPairsEqual(t, om,
|
||||||
|
[]marshallable{1, 28},
|
||||||
|
[]any{"bar", true})
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("when fed with an input that's not an object", func(t *testing.T) {
|
||||||
|
for _, data := range []string{"true", `["foo"]`, "42", `"foo"`} {
|
||||||
|
om := New[int, any]()
|
||||||
|
require.Error(t, json.Unmarshal([]byte(data), &om))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("empty map", func(t *testing.T) {
|
||||||
|
data := `{}`
|
||||||
|
|
||||||
|
om := New[int, any]()
|
||||||
|
require.NoError(t, json.Unmarshal([]byte(data), &om))
|
||||||
|
|
||||||
|
assertLenEqual(t, om, 0)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// const specialCharacters = "\\\\/\"\b\f\n\r\t\x00\uffff\ufffd世界\u007f\u00ff\U0010FFFF"
|
||||||
|
const specialCharacters = "\uffff\ufffd世界\u007f\u00ff\U0010FFFF"
|
||||||
|
|
||||||
|
func TestJSONSpecialCharacters(t *testing.T) {
|
||||||
|
baselineMap := map[string]any{specialCharacters: specialCharacters}
|
||||||
|
baselineData, err := json.Marshal(baselineMap)
|
||||||
|
require.NoError(t, err) // baseline proves this key is supported by official json library
|
||||||
|
t.Logf("specialCharacters: %#v as []rune:%v", specialCharacters, []rune(specialCharacters))
|
||||||
|
t.Logf("baseline json data: %s", baselineData)
|
||||||
|
|
||||||
|
t.Run("marshal special characters", func(t *testing.T) {
|
||||||
|
om := New[string, any]()
|
||||||
|
om.Set(specialCharacters, specialCharacters)
|
||||||
|
b, err := json.Marshal(om)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, baselineData, b)
|
||||||
|
|
||||||
|
type myString string
|
||||||
|
om2 := New[myString, myString]()
|
||||||
|
om2.Set(specialCharacters, specialCharacters)
|
||||||
|
b, err = json.Marshal(om2)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, baselineData, b)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("unmarshall special characters", func(t *testing.T) {
|
||||||
|
om := New[string, any]()
|
||||||
|
require.NoError(t, json.Unmarshal(baselineData, &om))
|
||||||
|
assertOrderedPairsEqual(t, om,
|
||||||
|
[]string{specialCharacters},
|
||||||
|
[]any{specialCharacters})
|
||||||
|
|
||||||
|
type myString string
|
||||||
|
om2 := New[myString, myString]()
|
||||||
|
require.NoError(t, json.Unmarshal(baselineData, &om2))
|
||||||
|
assertOrderedPairsEqual(t, om2,
|
||||||
|
[]myString{specialCharacters},
|
||||||
|
[]myString{specialCharacters})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// to test structs that have nested map fields
|
||||||
|
type nestedMaps struct {
|
||||||
|
X int `json:"x" yaml:"x"`
|
||||||
|
M *OrderedMap[string, []*OrderedMap[int, *OrderedMap[string, any]]] `json:"m" yaml:"m"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestJSONRoundTrip(t *testing.T) {
|
||||||
|
for _, testCase := range []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
targetFactory func() any
|
||||||
|
isPrettyPrinted bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "",
|
||||||
|
input: `{
|
||||||
|
"x": 28,
|
||||||
|
"m": {
|
||||||
|
"foo": [
|
||||||
|
{
|
||||||
|
"12": {
|
||||||
|
"i": 12,
|
||||||
|
"b": true,
|
||||||
|
"n": null,
|
||||||
|
"m": {
|
||||||
|
"a": "b",
|
||||||
|
"c": 28
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"28": {
|
||||||
|
"a": false,
|
||||||
|
"b": [
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
3
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"3": {
|
||||||
|
"c": null,
|
||||||
|
"d": 87
|
||||||
|
},
|
||||||
|
"4": {
|
||||||
|
"e": true
|
||||||
|
},
|
||||||
|
"5": {
|
||||||
|
"f": 4,
|
||||||
|
"g": 5,
|
||||||
|
"h": 6
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"bar": [
|
||||||
|
{
|
||||||
|
"5": {
|
||||||
|
"foo": "bar"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}`,
|
||||||
|
targetFactory: func() any { return &nestedMaps{} },
|
||||||
|
isPrettyPrinted: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "with UTF-8 special chars in key",
|
||||||
|
input: `{"<22>":0}`,
|
||||||
|
targetFactory: func() any { return &OrderedMap[string, int]{} },
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
t.Run(testCase.name, func(t *testing.T) {
|
||||||
|
target := testCase.targetFactory()
|
||||||
|
|
||||||
|
require.NoError(t, json.Unmarshal([]byte(testCase.input), target))
|
||||||
|
|
||||||
|
var (
|
||||||
|
out []byte
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
if testCase.isPrettyPrinted {
|
||||||
|
out, err = json.MarshalIndent(target, "", " ")
|
||||||
|
} else {
|
||||||
|
out, err = json.Marshal(target)
|
||||||
|
}
|
||||||
|
|
||||||
|
if assert.NoError(t, err) {
|
||||||
|
assert.Equal(t, strings.TrimSpace(testCase.input), string(out))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkMarshalJSON(b *testing.B) {
|
||||||
|
om := New[int, any]()
|
||||||
|
om.Set(1, "bar")
|
||||||
|
om.Set(7, "baz")
|
||||||
|
om.Set(2, 28)
|
||||||
|
om.Set(3, 100)
|
||||||
|
om.Set(4, "baz")
|
||||||
|
om.Set(5, "28")
|
||||||
|
om.Set(6, "100")
|
||||||
|
om.Set(8, "baz")
|
||||||
|
om.Set(8, "baz")
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_, _ = json.Marshal(om)
|
||||||
|
}
|
||||||
|
}
|
||||||
295
common/orderedmap/orderedmap.go
Normal file
295
common/orderedmap/orderedmap.go
Normal file
@@ -0,0 +1,295 @@
|
|||||||
|
// Package orderedmap implements an ordered map, i.e. a map that also keeps track of
|
||||||
|
// the order in which keys were inserted.
|
||||||
|
//
|
||||||
|
// All operations are constant-time.
|
||||||
|
//
|
||||||
|
// Github repo: https://github.com/wk8/go-ordered-map
|
||||||
|
package orderedmap
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
list "github.com/bahlo/generic-list-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Pair[K comparable, V any] struct {
|
||||||
|
Key K
|
||||||
|
Value V
|
||||||
|
|
||||||
|
element *list.Element[*Pair[K, V]]
|
||||||
|
}
|
||||||
|
|
||||||
|
type OrderedMap[K comparable, V any] struct {
|
||||||
|
pairs map[K]*Pair[K, V]
|
||||||
|
list *list.List[*Pair[K, V]]
|
||||||
|
}
|
||||||
|
|
||||||
|
type initConfig[K comparable, V any] struct {
|
||||||
|
capacity int
|
||||||
|
initialData []Pair[K, V]
|
||||||
|
}
|
||||||
|
|
||||||
|
type InitOption[K comparable, V any] func(config *initConfig[K, V])
|
||||||
|
|
||||||
|
// WithCapacity allows giving a capacity hint for the map, akin to the standard make(map[K]V, capacity).
|
||||||
|
func WithCapacity[K comparable, V any](capacity int) InitOption[K, V] {
|
||||||
|
return func(c *initConfig[K, V]) {
|
||||||
|
c.capacity = capacity
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithInitialData allows passing in initial data for the map.
|
||||||
|
func WithInitialData[K comparable, V any](initialData ...Pair[K, V]) InitOption[K, V] {
|
||||||
|
return func(c *initConfig[K, V]) {
|
||||||
|
c.initialData = initialData
|
||||||
|
if c.capacity < len(initialData) {
|
||||||
|
c.capacity = len(initialData)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a new OrderedMap.
|
||||||
|
// options can either be one or several InitOption[K, V], or a single integer,
|
||||||
|
// which is then interpreted as a capacity hint, à la make(map[K]V, capacity).
|
||||||
|
func New[K comparable, V any](options ...any) *OrderedMap[K, V] { //nolint:varnamelen
|
||||||
|
orderedMap := &OrderedMap[K, V]{}
|
||||||
|
|
||||||
|
var config initConfig[K, V]
|
||||||
|
for _, untypedOption := range options {
|
||||||
|
switch option := untypedOption.(type) {
|
||||||
|
case int:
|
||||||
|
if len(options) != 1 {
|
||||||
|
invalidOption()
|
||||||
|
}
|
||||||
|
config.capacity = option
|
||||||
|
|
||||||
|
case InitOption[K, V]:
|
||||||
|
option(&config)
|
||||||
|
|
||||||
|
default:
|
||||||
|
invalidOption()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
orderedMap.initialize(config.capacity)
|
||||||
|
orderedMap.AddPairs(config.initialData...)
|
||||||
|
|
||||||
|
return orderedMap
|
||||||
|
}
|
||||||
|
|
||||||
|
const invalidOptionMessage = `when using orderedmap.New[K,V]() with options, either provide one or several InitOption[K, V]; or a single integer which is then interpreted as a capacity hint, à la make(map[K]V, capacity).` //nolint:lll
|
||||||
|
|
||||||
|
func invalidOption() { panic(invalidOptionMessage) }
|
||||||
|
|
||||||
|
func (om *OrderedMap[K, V]) initialize(capacity int) {
|
||||||
|
om.pairs = make(map[K]*Pair[K, V], capacity)
|
||||||
|
om.list = list.New[*Pair[K, V]]()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get looks for the given key, and returns the value associated with it,
|
||||||
|
// or V's nil value if not found. The boolean it returns says whether the key is present in the map.
|
||||||
|
func (om *OrderedMap[K, V]) Get(key K) (val V, present bool) {
|
||||||
|
if pair, present := om.pairs[key]; present {
|
||||||
|
return pair.Value, true
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load is an alias for Get, mostly to present an API similar to `sync.Map`'s.
|
||||||
|
func (om *OrderedMap[K, V]) Load(key K) (V, bool) {
|
||||||
|
return om.Get(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Value returns the value associated with the given key or the zero value.
|
||||||
|
func (om *OrderedMap[K, V]) Value(key K) (val V) {
|
||||||
|
if pair, present := om.pairs[key]; present {
|
||||||
|
val = pair.Value
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetPair looks for the given key, and returns the pair associated with it,
|
||||||
|
// or nil if not found. The Pair struct can then be used to iterate over the ordered map
|
||||||
|
// from that point, either forward or backward.
|
||||||
|
func (om *OrderedMap[K, V]) GetPair(key K) *Pair[K, V] {
|
||||||
|
return om.pairs[key]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set sets the key-value pair, and returns what `Get` would have returned
|
||||||
|
// on that key prior to the call to `Set`.
|
||||||
|
func (om *OrderedMap[K, V]) Set(key K, value V) (val V, present bool) {
|
||||||
|
if pair, present := om.pairs[key]; present {
|
||||||
|
oldValue := pair.Value
|
||||||
|
pair.Value = value
|
||||||
|
return oldValue, true
|
||||||
|
}
|
||||||
|
|
||||||
|
pair := &Pair[K, V]{
|
||||||
|
Key: key,
|
||||||
|
Value: value,
|
||||||
|
}
|
||||||
|
pair.element = om.list.PushBack(pair)
|
||||||
|
om.pairs[key] = pair
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddPairs allows setting multiple pairs at a time. It's equivalent to calling
|
||||||
|
// Set on each pair sequentially.
|
||||||
|
func (om *OrderedMap[K, V]) AddPairs(pairs ...Pair[K, V]) {
|
||||||
|
for _, pair := range pairs {
|
||||||
|
om.Set(pair.Key, pair.Value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store is an alias for Set, mostly to present an API similar to `sync.Map`'s.
|
||||||
|
func (om *OrderedMap[K, V]) Store(key K, value V) (V, bool) {
|
||||||
|
return om.Set(key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete removes the key-value pair, and returns what `Get` would have returned
|
||||||
|
// on that key prior to the call to `Delete`.
|
||||||
|
func (om *OrderedMap[K, V]) Delete(key K) (val V, present bool) {
|
||||||
|
if pair, present := om.pairs[key]; present {
|
||||||
|
om.list.Remove(pair.element)
|
||||||
|
delete(om.pairs, key)
|
||||||
|
return pair.Value, true
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Len returns the length of the ordered map.
|
||||||
|
func (om *OrderedMap[K, V]) Len() int {
|
||||||
|
if om == nil || om.pairs == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return len(om.pairs)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Oldest returns a pointer to the oldest pair. It's meant to be used to iterate on the ordered map's
|
||||||
|
// pairs from the oldest to the newest, e.g.:
|
||||||
|
// for pair := orderedMap.Oldest(); pair != nil; pair = pair.Next() { fmt.Printf("%v => %v\n", pair.Key, pair.Value) }
|
||||||
|
func (om *OrderedMap[K, V]) Oldest() *Pair[K, V] {
|
||||||
|
if om == nil || om.list == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return listElementToPair(om.list.Front())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Newest returns a pointer to the newest pair. It's meant to be used to iterate on the ordered map's
|
||||||
|
// pairs from the newest to the oldest, e.g.:
|
||||||
|
// for pair := orderedMap.Oldest(); pair != nil; pair = pair.Next() { fmt.Printf("%v => %v\n", pair.Key, pair.Value) }
|
||||||
|
func (om *OrderedMap[K, V]) Newest() *Pair[K, V] {
|
||||||
|
if om == nil || om.list == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return listElementToPair(om.list.Back())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Next returns a pointer to the next pair.
|
||||||
|
func (p *Pair[K, V]) Next() *Pair[K, V] {
|
||||||
|
return listElementToPair(p.element.Next())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prev returns a pointer to the previous pair.
|
||||||
|
func (p *Pair[K, V]) Prev() *Pair[K, V] {
|
||||||
|
return listElementToPair(p.element.Prev())
|
||||||
|
}
|
||||||
|
|
||||||
|
func listElementToPair[K comparable, V any](element *list.Element[*Pair[K, V]]) *Pair[K, V] {
|
||||||
|
if element == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return element.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
// KeyNotFoundError may be returned by functions in this package when they're called with keys that are not present
|
||||||
|
// in the map.
|
||||||
|
type KeyNotFoundError[K comparable] struct {
|
||||||
|
MissingKey K
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *KeyNotFoundError[K]) Error() string {
|
||||||
|
return fmt.Sprintf("missing key: %v", e.MissingKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MoveAfter moves the value associated with key to its new position after the one associated with markKey.
|
||||||
|
// Returns an error iff key or markKey are not present in the map. If an error is returned,
|
||||||
|
// it will be a KeyNotFoundError.
|
||||||
|
func (om *OrderedMap[K, V]) MoveAfter(key, markKey K) error {
|
||||||
|
elements, err := om.getElements(key, markKey)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
om.list.MoveAfter(elements[0], elements[1])
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MoveBefore moves the value associated with key to its new position before the one associated with markKey.
|
||||||
|
// Returns an error iff key or markKey are not present in the map. If an error is returned,
|
||||||
|
// it will be a KeyNotFoundError.
|
||||||
|
func (om *OrderedMap[K, V]) MoveBefore(key, markKey K) error {
|
||||||
|
elements, err := om.getElements(key, markKey)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
om.list.MoveBefore(elements[0], elements[1])
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (om *OrderedMap[K, V]) getElements(keys ...K) ([]*list.Element[*Pair[K, V]], error) {
|
||||||
|
elements := make([]*list.Element[*Pair[K, V]], len(keys))
|
||||||
|
for i, k := range keys {
|
||||||
|
pair, present := om.pairs[k]
|
||||||
|
if !present {
|
||||||
|
return nil, &KeyNotFoundError[K]{k}
|
||||||
|
}
|
||||||
|
elements[i] = pair.element
|
||||||
|
}
|
||||||
|
return elements, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MoveToBack moves the value associated with key to the back of the ordered map,
|
||||||
|
// i.e. makes it the newest pair in the map.
|
||||||
|
// Returns an error iff key is not present in the map. If an error is returned,
|
||||||
|
// it will be a KeyNotFoundError.
|
||||||
|
func (om *OrderedMap[K, V]) MoveToBack(key K) error {
|
||||||
|
_, err := om.GetAndMoveToBack(key)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// MoveToFront moves the value associated with key to the front of the ordered map,
|
||||||
|
// i.e. makes it the oldest pair in the map.
|
||||||
|
// Returns an error iff key is not present in the map. If an error is returned,
|
||||||
|
// it will be a KeyNotFoundError.
|
||||||
|
func (om *OrderedMap[K, V]) MoveToFront(key K) error {
|
||||||
|
_, err := om.GetAndMoveToFront(key)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAndMoveToBack combines Get and MoveToBack in the same call. If an error is returned,
|
||||||
|
// it will be a KeyNotFoundError.
|
||||||
|
func (om *OrderedMap[K, V]) GetAndMoveToBack(key K) (val V, err error) {
|
||||||
|
if pair, present := om.pairs[key]; present {
|
||||||
|
val = pair.Value
|
||||||
|
om.list.MoveToBack(pair.element)
|
||||||
|
} else {
|
||||||
|
err = &KeyNotFoundError[K]{key}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAndMoveToFront combines Get and MoveToFront in the same call. If an error is returned,
|
||||||
|
// it will be a KeyNotFoundError.
|
||||||
|
func (om *OrderedMap[K, V]) GetAndMoveToFront(key K) (val V, err error) {
|
||||||
|
if pair, present := om.pairs[key]; present {
|
||||||
|
val = pair.Value
|
||||||
|
om.list.MoveToFront(pair.element)
|
||||||
|
} else {
|
||||||
|
err = &KeyNotFoundError[K]{key}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
384
common/orderedmap/orderedmap_test.go
Normal file
384
common/orderedmap/orderedmap_test.go
Normal file
@@ -0,0 +1,384 @@
|
|||||||
|
package orderedmap
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBasicFeatures(t *testing.T) {
|
||||||
|
n := 100
|
||||||
|
om := New[int, int]()
|
||||||
|
|
||||||
|
// set(i, 2 * i)
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
assertLenEqual(t, om, i)
|
||||||
|
oldValue, present := om.Set(i, 2*i)
|
||||||
|
assertLenEqual(t, om, i+1)
|
||||||
|
|
||||||
|
assert.Equal(t, 0, oldValue)
|
||||||
|
assert.False(t, present)
|
||||||
|
}
|
||||||
|
|
||||||
|
// get what we just set
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
value, present := om.Get(i)
|
||||||
|
|
||||||
|
assert.Equal(t, 2*i, value)
|
||||||
|
assert.Equal(t, value, om.Value(i))
|
||||||
|
assert.True(t, present)
|
||||||
|
}
|
||||||
|
|
||||||
|
// get pairs of what we just set
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
pair := om.GetPair(i)
|
||||||
|
|
||||||
|
assert.NotNil(t, pair)
|
||||||
|
assert.Equal(t, 2*i, pair.Value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// forward iteration
|
||||||
|
i := 0
|
||||||
|
for pair := om.Oldest(); pair != nil; pair = pair.Next() {
|
||||||
|
assert.Equal(t, i, pair.Key)
|
||||||
|
assert.Equal(t, 2*i, pair.Value)
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
// backward iteration
|
||||||
|
i = n - 1
|
||||||
|
for pair := om.Newest(); pair != nil; pair = pair.Prev() {
|
||||||
|
assert.Equal(t, i, pair.Key)
|
||||||
|
assert.Equal(t, 2*i, pair.Value)
|
||||||
|
i--
|
||||||
|
}
|
||||||
|
|
||||||
|
// forward iteration starting from known key
|
||||||
|
i = 42
|
||||||
|
for pair := om.GetPair(i); pair != nil; pair = pair.Next() {
|
||||||
|
assert.Equal(t, i, pair.Key)
|
||||||
|
assert.Equal(t, 2*i, pair.Value)
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
|
||||||
|
// double values for pairs with even keys
|
||||||
|
for j := 0; j < n/2; j++ {
|
||||||
|
i = 2 * j
|
||||||
|
oldValue, present := om.Set(i, 4*i)
|
||||||
|
|
||||||
|
assert.Equal(t, 2*i, oldValue)
|
||||||
|
assert.True(t, present)
|
||||||
|
}
|
||||||
|
// and delete pairs with odd keys
|
||||||
|
for j := 0; j < n/2; j++ {
|
||||||
|
i = 2*j + 1
|
||||||
|
assertLenEqual(t, om, n-j)
|
||||||
|
value, present := om.Delete(i)
|
||||||
|
assertLenEqual(t, om, n-j-1)
|
||||||
|
|
||||||
|
assert.Equal(t, 2*i, value)
|
||||||
|
assert.True(t, present)
|
||||||
|
|
||||||
|
// deleting again shouldn't change anything
|
||||||
|
value, present = om.Delete(i)
|
||||||
|
assertLenEqual(t, om, n-j-1)
|
||||||
|
assert.Equal(t, 0, value)
|
||||||
|
assert.False(t, present)
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the whole range
|
||||||
|
for j := 0; j < n/2; j++ {
|
||||||
|
i = 2 * j
|
||||||
|
value, present := om.Get(i)
|
||||||
|
assert.Equal(t, 4*i, value)
|
||||||
|
assert.Equal(t, value, om.Value(i))
|
||||||
|
assert.True(t, present)
|
||||||
|
|
||||||
|
i = 2*j + 1
|
||||||
|
value, present = om.Get(i)
|
||||||
|
assert.Equal(t, 0, value)
|
||||||
|
assert.Equal(t, value, om.Value(i))
|
||||||
|
assert.False(t, present)
|
||||||
|
}
|
||||||
|
|
||||||
|
// check iterations again
|
||||||
|
i = 0
|
||||||
|
for pair := om.Oldest(); pair != nil; pair = pair.Next() {
|
||||||
|
assert.Equal(t, i, pair.Key)
|
||||||
|
assert.Equal(t, 4*i, pair.Value)
|
||||||
|
i += 2
|
||||||
|
}
|
||||||
|
i = 2 * ((n - 1) / 2)
|
||||||
|
for pair := om.Newest(); pair != nil; pair = pair.Prev() {
|
||||||
|
assert.Equal(t, i, pair.Key)
|
||||||
|
assert.Equal(t, 4*i, pair.Value)
|
||||||
|
i -= 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUpdatingDoesntChangePairsOrder(t *testing.T) {
|
||||||
|
om := New[string, any]()
|
||||||
|
om.Set("foo", "bar")
|
||||||
|
om.Set("wk", 28)
|
||||||
|
om.Set("po", 100)
|
||||||
|
om.Set("bar", "baz")
|
||||||
|
|
||||||
|
oldValue, present := om.Set("po", 102)
|
||||||
|
assert.Equal(t, 100, oldValue)
|
||||||
|
assert.True(t, present)
|
||||||
|
|
||||||
|
assertOrderedPairsEqual(t, om,
|
||||||
|
[]string{"foo", "wk", "po", "bar"},
|
||||||
|
[]any{"bar", 28, 102, "baz"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDeletingAndReinsertingChangesPairsOrder(t *testing.T) {
|
||||||
|
om := New[string, any]()
|
||||||
|
om.Set("foo", "bar")
|
||||||
|
om.Set("wk", 28)
|
||||||
|
om.Set("po", 100)
|
||||||
|
om.Set("bar", "baz")
|
||||||
|
|
||||||
|
// delete a pair
|
||||||
|
oldValue, present := om.Delete("po")
|
||||||
|
assert.Equal(t, 100, oldValue)
|
||||||
|
assert.True(t, present)
|
||||||
|
|
||||||
|
// re-insert the same pair
|
||||||
|
oldValue, present = om.Set("po", 100)
|
||||||
|
assert.Nil(t, oldValue)
|
||||||
|
assert.False(t, present)
|
||||||
|
|
||||||
|
assertOrderedPairsEqual(t, om,
|
||||||
|
[]string{"foo", "wk", "bar", "po"},
|
||||||
|
[]any{"bar", 28, "baz", 100})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEmptyMapOperations(t *testing.T) {
|
||||||
|
om := New[string, any]()
|
||||||
|
|
||||||
|
oldValue, present := om.Get("foo")
|
||||||
|
assert.Nil(t, oldValue)
|
||||||
|
assert.Nil(t, om.Value("foo"))
|
||||||
|
assert.False(t, present)
|
||||||
|
|
||||||
|
oldValue, present = om.Delete("bar")
|
||||||
|
assert.Nil(t, oldValue)
|
||||||
|
assert.False(t, present)
|
||||||
|
|
||||||
|
assertLenEqual(t, om, 0)
|
||||||
|
|
||||||
|
assert.Nil(t, om.Oldest())
|
||||||
|
assert.Nil(t, om.Newest())
|
||||||
|
}
|
||||||
|
|
||||||
|
type dummyTestStruct struct {
|
||||||
|
value string
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPackUnpackStructs(t *testing.T) {
|
||||||
|
om := New[string, dummyTestStruct]()
|
||||||
|
om.Set("foo", dummyTestStruct{"foo!"})
|
||||||
|
om.Set("bar", dummyTestStruct{"bar!"})
|
||||||
|
|
||||||
|
value, present := om.Get("foo")
|
||||||
|
assert.True(t, present)
|
||||||
|
assert.Equal(t, value, om.Value("foo"))
|
||||||
|
if assert.NotNil(t, value) {
|
||||||
|
assert.Equal(t, "foo!", value.value)
|
||||||
|
}
|
||||||
|
|
||||||
|
value, present = om.Set("bar", dummyTestStruct{"baz!"})
|
||||||
|
assert.True(t, present)
|
||||||
|
if assert.NotNil(t, value) {
|
||||||
|
assert.Equal(t, "bar!", value.value)
|
||||||
|
}
|
||||||
|
|
||||||
|
value, present = om.Get("bar")
|
||||||
|
assert.Equal(t, value, om.Value("bar"))
|
||||||
|
assert.True(t, present)
|
||||||
|
if assert.NotNil(t, value) {
|
||||||
|
assert.Equal(t, "baz!", value.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// shamelessly stolen from https://github.com/python/cpython/blob/e19a91e45fd54a56e39c2d12e6aaf4757030507f/Lib/test/test_ordered_dict.py#L55-L61
|
||||||
|
func TestShuffle(t *testing.T) {
|
||||||
|
ranLen := 100
|
||||||
|
|
||||||
|
for _, n := range []int{0, 10, 20, 100, 1000, 10000} {
|
||||||
|
t.Run(fmt.Sprintf("shuffle test with %d items", n), func(t *testing.T) {
|
||||||
|
om := New[string, string]()
|
||||||
|
|
||||||
|
keys := make([]string, n)
|
||||||
|
values := make([]string, n)
|
||||||
|
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
// we prefix with the number to ensure that we don't get any duplicates
|
||||||
|
keys[i] = fmt.Sprintf("%d_%s", i, randomHexString(t, ranLen))
|
||||||
|
values[i] = randomHexString(t, ranLen)
|
||||||
|
|
||||||
|
value, present := om.Set(keys[i], values[i])
|
||||||
|
assert.Equal(t, "", value)
|
||||||
|
assert.False(t, present)
|
||||||
|
}
|
||||||
|
|
||||||
|
assertOrderedPairsEqual(t, om, keys, values)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMove(t *testing.T) {
|
||||||
|
om := New[int, any]()
|
||||||
|
om.Set(1, "bar")
|
||||||
|
om.Set(2, 28)
|
||||||
|
om.Set(3, 100)
|
||||||
|
om.Set(4, "baz")
|
||||||
|
om.Set(5, "28")
|
||||||
|
om.Set(6, "100")
|
||||||
|
om.Set(7, "baz")
|
||||||
|
om.Set(8, "baz")
|
||||||
|
|
||||||
|
err := om.MoveAfter(2, 3)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assertOrderedPairsEqual(t, om,
|
||||||
|
[]int{1, 3, 2, 4, 5, 6, 7, 8},
|
||||||
|
[]any{"bar", 100, 28, "baz", "28", "100", "baz", "baz"})
|
||||||
|
|
||||||
|
err = om.MoveBefore(6, 4)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assertOrderedPairsEqual(t, om,
|
||||||
|
[]int{1, 3, 2, 6, 4, 5, 7, 8},
|
||||||
|
[]any{"bar", 100, 28, "100", "baz", "28", "baz", "baz"})
|
||||||
|
|
||||||
|
err = om.MoveToBack(3)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assertOrderedPairsEqual(t, om,
|
||||||
|
[]int{1, 2, 6, 4, 5, 7, 8, 3},
|
||||||
|
[]any{"bar", 28, "100", "baz", "28", "baz", "baz", 100})
|
||||||
|
|
||||||
|
err = om.MoveToFront(5)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assertOrderedPairsEqual(t, om,
|
||||||
|
[]int{5, 1, 2, 6, 4, 7, 8, 3},
|
||||||
|
[]any{"28", "bar", 28, "100", "baz", "baz", "baz", 100})
|
||||||
|
|
||||||
|
err = om.MoveToFront(100)
|
||||||
|
assert.Equal(t, &KeyNotFoundError[int]{100}, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetAndMove(t *testing.T) {
|
||||||
|
om := New[int, any]()
|
||||||
|
om.Set(1, "bar")
|
||||||
|
om.Set(2, 28)
|
||||||
|
om.Set(3, 100)
|
||||||
|
om.Set(4, "baz")
|
||||||
|
om.Set(5, "28")
|
||||||
|
om.Set(6, "100")
|
||||||
|
om.Set(7, "baz")
|
||||||
|
om.Set(8, "baz")
|
||||||
|
|
||||||
|
value, err := om.GetAndMoveToBack(3)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, 100, value)
|
||||||
|
assertOrderedPairsEqual(t, om,
|
||||||
|
[]int{1, 2, 4, 5, 6, 7, 8, 3},
|
||||||
|
[]any{"bar", 28, "baz", "28", "100", "baz", "baz", 100})
|
||||||
|
|
||||||
|
value, err = om.GetAndMoveToFront(5)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, "28", value)
|
||||||
|
assertOrderedPairsEqual(t, om,
|
||||||
|
[]int{5, 1, 2, 4, 6, 7, 8, 3},
|
||||||
|
[]any{"28", "bar", 28, "baz", "100", "baz", "baz", 100})
|
||||||
|
|
||||||
|
value, err = om.GetAndMoveToBack(100)
|
||||||
|
assert.Equal(t, &KeyNotFoundError[int]{100}, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddPairs(t *testing.T) {
|
||||||
|
om := New[int, any]()
|
||||||
|
om.AddPairs(
|
||||||
|
Pair[int, any]{
|
||||||
|
Key: 28,
|
||||||
|
Value: "foo",
|
||||||
|
},
|
||||||
|
Pair[int, any]{
|
||||||
|
Key: 12,
|
||||||
|
Value: "bar",
|
||||||
|
},
|
||||||
|
Pair[int, any]{
|
||||||
|
Key: 28,
|
||||||
|
Value: "baz",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
assertOrderedPairsEqual(t, om,
|
||||||
|
[]int{28, 12},
|
||||||
|
[]any{"baz", "bar"})
|
||||||
|
}
|
||||||
|
|
||||||
|
// sadly, we can't test the "actual" capacity here, see https://github.com/golang/go/issues/52157
|
||||||
|
func TestNewWithCapacity(t *testing.T) {
|
||||||
|
zero := New[int, string](0)
|
||||||
|
assert.Empty(t, zero.Len())
|
||||||
|
|
||||||
|
assert.PanicsWithValue(t, invalidOptionMessage, func() {
|
||||||
|
_ = New[int, string](1, 2)
|
||||||
|
})
|
||||||
|
assert.PanicsWithValue(t, invalidOptionMessage, func() {
|
||||||
|
_ = New[int, string](1, 2, 3)
|
||||||
|
})
|
||||||
|
|
||||||
|
om := New[int, string](-1)
|
||||||
|
om.Set(1337, "quarante-deux")
|
||||||
|
assert.Equal(t, 1, om.Len())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewWithOptions(t *testing.T) {
|
||||||
|
t.Run("wih capacity", func(t *testing.T) {
|
||||||
|
om := New[string, any](WithCapacity[string, any](98))
|
||||||
|
assert.Equal(t, 0, om.Len())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("with initial data", func(t *testing.T) {
|
||||||
|
om := New[string, int](WithInitialData(
|
||||||
|
Pair[string, int]{
|
||||||
|
Key: "a",
|
||||||
|
Value: 1,
|
||||||
|
},
|
||||||
|
Pair[string, int]{
|
||||||
|
Key: "b",
|
||||||
|
Value: 2,
|
||||||
|
},
|
||||||
|
Pair[string, int]{
|
||||||
|
Key: "c",
|
||||||
|
Value: 3,
|
||||||
|
},
|
||||||
|
))
|
||||||
|
|
||||||
|
assertOrderedPairsEqual(t, om,
|
||||||
|
[]string{"a", "b", "c"},
|
||||||
|
[]int{1, 2, 3})
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("with an invalid option type", func(t *testing.T) {
|
||||||
|
assert.PanicsWithValue(t, invalidOptionMessage, func() {
|
||||||
|
_ = New[int, string]("foo")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNilMap(t *testing.T) {
|
||||||
|
// we want certain behaviors of a nil ordered map to be the same as they are for standard nil maps
|
||||||
|
var om *OrderedMap[int, any]
|
||||||
|
|
||||||
|
t.Run("len", func(t *testing.T) {
|
||||||
|
assert.Equal(t, 0, om.Len())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("iterating - akin to range", func(t *testing.T) {
|
||||||
|
assert.Nil(t, om.Oldest())
|
||||||
|
assert.Nil(t, om.Newest())
|
||||||
|
})
|
||||||
|
}
|
||||||
76
common/orderedmap/utils_test.go
Normal file
76
common/orderedmap/utils_test.go
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
package orderedmap
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
// assertOrderedPairsEqual asserts that the map contains the given keys and values
|
||||||
|
// from oldest to newest.
|
||||||
|
func assertOrderedPairsEqual[K comparable, V any](
|
||||||
|
t *testing.T, orderedMap *OrderedMap[K, V], expectedKeys []K, expectedValues []V,
|
||||||
|
) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
assertOrderedPairsEqualFromNewest(t, orderedMap, expectedKeys, expectedValues)
|
||||||
|
assertOrderedPairsEqualFromOldest(t, orderedMap, expectedKeys, expectedValues)
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertOrderedPairsEqualFromNewest[K comparable, V any](
|
||||||
|
t *testing.T, orderedMap *OrderedMap[K, V], expectedKeys []K, expectedValues []V,
|
||||||
|
) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
if assert.Equal(t, len(expectedKeys), len(expectedValues)) && assert.Equal(t, len(expectedKeys), orderedMap.Len()) {
|
||||||
|
i := orderedMap.Len() - 1
|
||||||
|
for pair := orderedMap.Newest(); pair != nil; pair = pair.Prev() {
|
||||||
|
assert.Equal(t, expectedKeys[i], pair.Key, "from newest index=%d on key", i)
|
||||||
|
assert.Equal(t, expectedValues[i], pair.Value, "from newest index=%d on value", i)
|
||||||
|
i--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertOrderedPairsEqualFromOldest[K comparable, V any](
|
||||||
|
t *testing.T, orderedMap *OrderedMap[K, V], expectedKeys []K, expectedValues []V,
|
||||||
|
) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
if assert.Equal(t, len(expectedKeys), len(expectedValues)) && assert.Equal(t, len(expectedKeys), orderedMap.Len()) {
|
||||||
|
i := 0
|
||||||
|
for pair := orderedMap.Oldest(); pair != nil; pair = pair.Next() {
|
||||||
|
assert.Equal(t, expectedKeys[i], pair.Key, "from oldest index=%d on key", i)
|
||||||
|
assert.Equal(t, expectedValues[i], pair.Value, "from oldest index=%d on value", i)
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertLenEqual[K comparable, V any](t *testing.T, orderedMap *OrderedMap[K, V], expectedLen int) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
assert.Equal(t, expectedLen, orderedMap.Len())
|
||||||
|
|
||||||
|
// also check the list length, for good measure
|
||||||
|
assert.Equal(t, expectedLen, orderedMap.list.Len())
|
||||||
|
}
|
||||||
|
|
||||||
|
func randomHexString(t *testing.T, length int) string {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
b := length / 2 //nolint:gomnd
|
||||||
|
randBytes := make([]byte, b)
|
||||||
|
|
||||||
|
if n, err := rand.Read(randBytes); err != nil || n != b {
|
||||||
|
if err == nil {
|
||||||
|
err = fmt.Errorf("only got %v random bytes, expected %v", n, b)
|
||||||
|
}
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return hex.EncodeToString(randBytes)
|
||||||
|
}
|
||||||
71
common/orderedmap/yaml.go
Normal file
71
common/orderedmap/yaml.go
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
package orderedmap
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ yaml.Marshaler = &OrderedMap[int, any]{}
|
||||||
|
_ yaml.Unmarshaler = &OrderedMap[int, any]{}
|
||||||
|
)
|
||||||
|
|
||||||
|
// MarshalYAML implements the yaml.Marshaler interface.
|
||||||
|
func (om *OrderedMap[K, V]) MarshalYAML() (interface{}, error) {
|
||||||
|
if om == nil {
|
||||||
|
return []byte("null"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
node := yaml.Node{
|
||||||
|
Kind: yaml.MappingNode,
|
||||||
|
}
|
||||||
|
|
||||||
|
for pair := om.Oldest(); pair != nil; pair = pair.Next() {
|
||||||
|
key, value := pair.Key, pair.Value
|
||||||
|
|
||||||
|
keyNode := &yaml.Node{}
|
||||||
|
|
||||||
|
// serialize key to yaml, then deserialize it back into the node
|
||||||
|
// this is a hack to get the correct tag for the key
|
||||||
|
if err := keyNode.Encode(key); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
valueNode := &yaml.Node{}
|
||||||
|
if err := valueNode.Encode(value); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
node.Content = append(node.Content, keyNode, valueNode)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &node, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
||||||
|
func (om *OrderedMap[K, V]) UnmarshalYAML(value *yaml.Node) error {
|
||||||
|
if value.Kind != yaml.MappingNode {
|
||||||
|
return fmt.Errorf("pipeline must contain YAML mapping, has %v", value.Kind)
|
||||||
|
}
|
||||||
|
|
||||||
|
if om.list == nil {
|
||||||
|
om.initialize(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
for index := 0; index < len(value.Content); index += 2 {
|
||||||
|
var key K
|
||||||
|
var val V
|
||||||
|
|
||||||
|
if err := value.Content[index].Decode(&key); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := value.Content[index+1].Decode(&val); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
om.Set(key, val)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
82
common/orderedmap/yaml_fuzz_test.go
Normal file
82
common/orderedmap/yaml_fuzz_test.go
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
package orderedmap
|
||||||
|
|
||||||
|
// Adapted from https://github.com/dvyukov/go-fuzz-corpus/blob/c42c1b2/json/json.go
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func FuzzRoundTripYAML(f *testing.F) {
|
||||||
|
f.Fuzz(func(t *testing.T, data []byte) {
|
||||||
|
for _, testCase := range []struct {
|
||||||
|
name string
|
||||||
|
constructor func() any
|
||||||
|
// should be a function that asserts that 2 objects of the type returned by constructor are equal
|
||||||
|
equalityAssertion func(*testing.T, any, any) bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "with a string -> string map",
|
||||||
|
constructor: func() any { return &OrderedMap[string, string]{} },
|
||||||
|
equalityAssertion: assertOrderedMapsEqual[string, string],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "with a string -> int map",
|
||||||
|
constructor: func() any { return &OrderedMap[string, int]{} },
|
||||||
|
equalityAssertion: assertOrderedMapsEqual[string, int],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "with a string -> any map",
|
||||||
|
constructor: func() any { return &OrderedMap[string, any]{} },
|
||||||
|
equalityAssertion: assertOrderedMapsEqual[string, any],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "with a struct with map fields",
|
||||||
|
constructor: func() any { return new(testFuzzStruct) },
|
||||||
|
equalityAssertion: assertTestFuzzStructEqual,
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
t.Run(testCase.name, func(t *testing.T) {
|
||||||
|
v1 := testCase.constructor()
|
||||||
|
if yaml.Unmarshal(data, v1) != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t.Log(data)
|
||||||
|
t.Log(v1)
|
||||||
|
|
||||||
|
yamlData, err := yaml.Marshal(v1)
|
||||||
|
require.NoError(t, err)
|
||||||
|
t.Log(string(yamlData))
|
||||||
|
|
||||||
|
v2 := testCase.constructor()
|
||||||
|
err = yaml.Unmarshal(yamlData, v2)
|
||||||
|
if err != nil {
|
||||||
|
t.Log(string(yamlData))
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !assert.True(t, testCase.equalityAssertion(t, v1, v2), "failed with input data %q", string(data)) {
|
||||||
|
// look at that what the standard lib does with regular map, to help with debugging
|
||||||
|
|
||||||
|
var m1 map[string]any
|
||||||
|
require.NoError(t, yaml.Unmarshal(data, &m1))
|
||||||
|
|
||||||
|
mapJsonData, err := yaml.Marshal(m1)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
var m2 map[string]any
|
||||||
|
require.NoError(t, yaml.Unmarshal(mapJsonData, &m2))
|
||||||
|
|
||||||
|
t.Logf("initial data = %s", string(data))
|
||||||
|
t.Logf("unmarshalled map = %v", m1)
|
||||||
|
t.Logf("re-marshalled from map = %s", string(mapJsonData))
|
||||||
|
t.Logf("re-marshalled from test obj = %s", string(yamlData))
|
||||||
|
t.Logf("re-unmarshalled map = %s", m2)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user