mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-02-28 09:39:54 +00:00
Compare commits
127 Commits
v1.19.14
...
dev-conn-p
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
58c4110b57 | ||
|
|
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 |
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)
|
||||||
|
}
|
||||||
843
.github/patch/go1.25.patch
vendored
Normal file
843
.github/patch/go1.25.patch
vendored
Normal file
@@ -0,0 +1,843 @@
|
|||||||
|
Subject: [PATCH] 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)
|
||||||
|
+}
|
||||||
842
.github/patch/go1.26.patch
vendored
Normal file
842
.github/patch/go1.26.patch
vendored
Normal file
@@ -0,0 +1,842 @@
|
|||||||
|
Subject: [PATCH] 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)
|
||||||
|
+}
|
||||||
41
.github/workflows/build.yml
vendored
41
.github/workflows/build.yml
vendored
@@ -59,6 +59,8 @@ jobs:
|
|||||||
- { goos: linux, goarch: s390x, output: s390x, debian: s390x, rpm: s390x }
|
- { goos: linux, goarch: s390x, output: s390x, debian: s390x, rpm: s390x }
|
||||||
- { goos: linux, goarch: ppc64le, output: ppc64le, debian: ppc64el, rpm: ppc64le }
|
- { goos: linux, goarch: ppc64le, output: ppc64le, debian: ppc64el, rpm: ppc64le }
|
||||||
|
|
||||||
|
# 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' }
|
- { goos: windows, goarch: '386', output: '386' }
|
||||||
- { goos: windows, goarch: amd64, goamd64: v1, output: amd64-compatible } # old style file name will be removed in next released
|
- { 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 }
|
||||||
@@ -146,17 +148,17 @@ jobs:
|
|||||||
- { goos: linux, goarch: amd64, goamd64: v3, output: amd64-v3-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.25'
|
go-version: '1.25'
|
||||||
|
|
||||||
- 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 }}
|
||||||
|
|
||||||
@@ -176,15 +178,13 @@ jobs:
|
|||||||
# 7c1157f9544922e96945196b47b95664b1e39108: "net: remove sysSocket fallback for Windows 7"
|
# 7c1157f9544922e96945196b47b95664b1e39108: "net: remove sysSocket fallback for Windows 7"
|
||||||
# 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"
|
||||||
|
# sepical fix:
|
||||||
|
# - os.RemoveAll not working on Windows7
|
||||||
- name: Revert Golang1.25 commit for Windows7/8
|
- name: Revert Golang1.25 commit for Windows7/8
|
||||||
if: ${{ matrix.jobs.goos == 'windows' && matrix.jobs.goversion == '' }}
|
if: ${{ matrix.jobs.goos == 'windows' && matrix.jobs.goversion == '' }}
|
||||||
run: |
|
run: |
|
||||||
alias curl='curl -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}"'
|
|
||||||
cd $(go env GOROOT)
|
cd $(go env GOROOT)
|
||||||
curl https://github.com/MetaCubeX/go/commit/8cb5472d94c34b88733a81091bd328e70ee565a4.diff | patch --verbose -p 1
|
patch --verbose -p 1 < $GITHUB_WORKSPACE/.github/patch/go1.25.patch
|
||||||
curl https://github.com/MetaCubeX/go/commit/6788c4c6f9fafb56729bad6b660f7ee2272d699f.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/a5b2168bb836ed9d6601c626f95e56c07923f906.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/f56f1e23507e646c85243a71bde7b9629b2f970c.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
|
||||||
# this patch file only works on golang1.24.x
|
# this patch file only works on golang1.24.x
|
||||||
@@ -198,12 +198,8 @@ jobs:
|
|||||||
- name: Revert Golang1.24 commit for Windows7/8
|
- name: Revert Golang1.24 commit for Windows7/8
|
||||||
if: ${{ matrix.jobs.goos == 'windows' && matrix.jobs.goversion == '1.24' }}
|
if: ${{ matrix.jobs.goos == 'windows' && matrix.jobs.goversion == '1.24' }}
|
||||||
run: |
|
run: |
|
||||||
alias curl='curl -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}"'
|
|
||||||
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
|
# 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
|
||||||
@@ -217,12 +213,8 @@ jobs:
|
|||||||
- name: Revert Golang1.23 commit for Windows7/8
|
- name: Revert Golang1.23 commit for Windows7/8
|
||||||
if: ${{ matrix.jobs.goos == 'windows' && matrix.jobs.goversion == '1.23' }}
|
if: ${{ matrix.jobs.goos == 'windows' && matrix.jobs.goversion == '1.23' }}
|
||||||
run: |
|
run: |
|
||||||
alias curl='curl -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}"'
|
|
||||||
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
|
# 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
|
||||||
@@ -236,20 +228,15 @@ jobs:
|
|||||||
- name: Revert Golang1.22 commit for Windows7/8
|
- name: Revert Golang1.22 commit for Windows7/8
|
||||||
if: ${{ matrix.jobs.goos == 'windows' && matrix.jobs.goversion == '1.22' }}
|
if: ${{ matrix.jobs.goos == 'windows' && matrix.jobs.goversion == '1.22' }}
|
||||||
run: |
|
run: |
|
||||||
alias curl='curl -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}"'
|
|
||||||
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: |
|
||||||
alias curl='curl -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}"'
|
|
||||||
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
|
||||||
run: |
|
run: |
|
||||||
@@ -438,7 +425,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'
|
||||||
@@ -497,7 +484,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
|
||||||
|
|
||||||
|
|||||||
92
.github/workflows/test.yml
vendored
92
.github/workflows/test.yml
vendored
@@ -22,8 +22,9 @@ 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.0-rc.1'
|
||||||
- '1.25'
|
- '1.25'
|
||||||
- '1.24'
|
- '1.24'
|
||||||
- '1.23'
|
- '1.23'
|
||||||
@@ -41,96 +42,29 @@ 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 }}
|
||||||
|
|
||||||
# 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.25.x
|
if: ${{ runner.os == 'Windows' && matrix.go-version != '1.20' && matrix.go-version != '1.26.0-rc.1' }}
|
||||||
# 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"
|
|
||||||
- name: Revert Golang1.25 commit for Windows7/8
|
|
||||||
if: ${{ runner.os == 'Windows' && matrix.go-version == '1.25' }}
|
|
||||||
run: |
|
run: |
|
||||||
alias curl='curl -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}"'
|
|
||||||
cd $(go env GOROOT)
|
cd $(go env GOROOT)
|
||||||
curl https://github.com/MetaCubeX/go/commit/8cb5472d94c34b88733a81091bd328e70ee565a4.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/6788c4c6f9fafb56729bad6b660f7ee2272d699f.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/a5b2168bb836ed9d6601c626f95e56c07923f906.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/f56f1e23507e646c85243a71bde7b9629b2f970c.diff | patch --verbose -p 1
|
|
||||||
|
|
||||||
# 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.26.0-rc.1' }}
|
||||||
# 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: |
|
||||||
alias curl='curl -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}"'
|
|
||||||
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.26.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: |
|
||||||
alias curl='curl -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}"'
|
rm -rf listener/inbound/*_test.go
|
||||||
cd $(go env GOROOT)
|
|
||||||
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: |
|
|
||||||
alias curl='curl -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}"'
|
|
||||||
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: |
|
|
||||||
alias curl='curl -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}"'
|
|
||||||
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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -17,6 +16,8 @@ import (
|
|||||||
"github.com/metacubex/mihomo/component/ca"
|
"github.com/metacubex/mihomo/component/ca"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/log"
|
"github.com/metacubex/mihomo/log"
|
||||||
|
|
||||||
|
"github.com/metacubex/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
var UnifiedDelay = atomic.NewBool(false)
|
var UnifiedDelay = atomic.NewBool(false)
|
||||||
@@ -51,26 +52,12 @@ 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) (C.Conn, error) {
|
func (p *Proxy) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
||||||
conn, err := p.ProxyAdapter.DialContext(ctx, metadata)
|
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) (C.PacketConn, error) {
|
func (p *Proxy) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
||||||
pc, err := p.ProxyAdapter.ListenPacketContext(ctx, metadata)
|
pc, err := p.ProxyAdapter.ListenPacketContext(ctx, metadata)
|
||||||
@@ -167,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)
|
||||||
}
|
}
|
||||||
@@ -191,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
|
||||||
|
|||||||
@@ -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,12 +2,13 @@ package inbound
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"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 {
|
||||||
|
|||||||
@@ -6,8 +6,7 @@ import (
|
|||||||
"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"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/transport/anytls"
|
"github.com/metacubex/mihomo/transport/anytls"
|
||||||
@@ -20,7 +19,6 @@ import (
|
|||||||
type AnyTLS struct {
|
type AnyTLS struct {
|
||||||
*Base
|
*Base
|
||||||
client *anytls.Client
|
client *anytls.Client
|
||||||
dialer proxydialer.SingDialer
|
|
||||||
option *AnyTLSOption
|
option *AnyTLSOption
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,7 +63,7 @@ func (t *AnyTLS) ListenPacketContext(ctx context.Context, metadata *C.Metadata)
|
|||||||
|
|
||||||
// create uot on tcp
|
// create uot on tcp
|
||||||
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
|
||||||
@@ -92,18 +90,18 @@ func NewAnyTLS(option AnyTLSOption) (*AnyTLS, error) {
|
|||||||
name: option.Name,
|
name: option.Name,
|
||||||
addr: addr,
|
addr: addr,
|
||||||
tp: C.AnyTLS,
|
tp: C.AnyTLS,
|
||||||
|
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,
|
||||||
}
|
}
|
||||||
|
outbound.dialer = option.NewDialer(outbound.DialOptions())
|
||||||
singDialer := proxydialer.NewByNameSingDialer(option.DialerProxy, dialer.NewDialer(outbound.DialOptions()...))
|
singDialer := proxydialer.NewSingDialer(outbound.dialer)
|
||||||
outbound.dialer = singDialer
|
|
||||||
|
|
||||||
tOption := anytls.ClientConfig{
|
tOption := anytls.ClientConfig{
|
||||||
Password: option.Password,
|
Password: option.Password,
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ 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/dialer"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
|
"github.com/metacubex/mihomo/component/proxydialer"
|
||||||
"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"
|
||||||
@@ -26,15 +27,17 @@ type ProxyAdapter interface {
|
|||||||
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
|
||||||
@@ -56,35 +59,15 @@ func (b *Base) Type() C.AdapterType {
|
|||||||
return b.tp
|
return b.tp
|
||||||
}
|
}
|
||||||
|
|
||||||
// StreamConnContext implements C.ProxyAdapter
|
|
||||||
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) (C.Conn, error) {
|
func (b *Base) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
||||||
return nil, C.ErrNotSupport
|
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (b *Base) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (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
|
||||||
@@ -103,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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -178,12 +162,30 @@ func (b *Base) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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"`
|
Interface string `proxy:"interface-name,omitempty"`
|
||||||
RoutingMark int `proxy:"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 {
|
||||||
@@ -217,6 +219,7 @@ func NewBase(opt BaseOption) *Base {
|
|||||||
type conn struct {
|
type conn struct {
|
||||||
N.ExtendedConn
|
N.ExtendedConn
|
||||||
chain C.Chain
|
chain C.Chain
|
||||||
|
pdChain C.Chain
|
||||||
adapterAddr string
|
adapterAddr string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -238,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 {
|
||||||
@@ -263,12 +272,15 @@ 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()}, 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
|
||||||
|
pdChain C.Chain
|
||||||
adapterName string
|
adapterName string
|
||||||
connID string
|
connID string
|
||||||
adapterAddr string
|
adapterAddr string
|
||||||
@@ -289,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 {
|
||||||
@@ -320,7 +338,9 @@ func newPacketConn(pc net.PacketConn, a ProxyAdapter) C.PacketConn {
|
|||||||
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(), a.Addr(), a.ResolveUDP}
|
cpc := &packetConn{epc, nil, nil, a.Name(), utils.NewUUIDV4().String(), a.Addr(), a.ResolveUDP}
|
||||||
|
cpc.AppendToChains(a)
|
||||||
|
return cpc
|
||||||
}
|
}
|
||||||
|
|
||||||
type AddRef interface {
|
type AddRef interface {
|
||||||
@@ -344,17 +364,6 @@ 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) {
|
|
||||||
c, err := p.ProxyAdapter.DialContextWithDialer(ctx, dialer, 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) (_ C.PacketConn, err error) {
|
func (p *autoCloseProxyAdapter) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
pc, err := p.ProxyAdapter.ListenPacketContext(ctx, metadata)
|
pc, err := p.ProxyAdapter.ListenPacketContext(ctx, metadata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -366,17 +375,6 @@ func (p *autoCloseProxyAdapter) ListenPacketContext(ctx context.Context, metadat
|
|||||||
return pc, nil
|
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 {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if pc, ok := pc.(AddRef); ok {
|
|
||||||
pc.AddRef(p)
|
|
||||||
}
|
|
||||||
return pc, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *autoCloseProxyAdapter) Close() error {
|
func (p *autoCloseProxyAdapter) Close() error {
|
||||||
p.closeOnce.Do(func() {
|
p.closeOnce.Do(func() {
|
||||||
log.Debugln("Closing outdated proxy [%s]", p.Name())
|
log.Debugln("Closing outdated proxy [%s]", p.Name())
|
||||||
|
|||||||
@@ -69,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(),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -158,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,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 {
|
||||||
@@ -61,18 +60,7 @@ 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) (_ 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.DialOptions()...), 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)
|
||||||
}
|
}
|
||||||
@@ -89,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()
|
||||||
@@ -183,20 +166,23 @@ func NewHttp(option HttpOption) (*Http, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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"
|
||||||
@@ -13,8 +12,6 @@ import (
|
|||||||
"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/ech"
|
"github.com/metacubex/mihomo/component/ech"
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
|
||||||
tlsC "github.com/metacubex/mihomo/component/tls"
|
|
||||||
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"
|
||||||
@@ -24,6 +21,8 @@ import (
|
|||||||
"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"
|
||||||
"github.com/metacubex/quic-go/congestion"
|
"github.com/metacubex/quic-go/congestion"
|
||||||
M "github.com/metacubex/sing/common/metadata"
|
M "github.com/metacubex/sing/common/metadata"
|
||||||
@@ -46,7 +45,7 @@ type Hysteria struct {
|
|||||||
option *HysteriaOption
|
option *HysteriaOption
|
||||||
client *core.Client
|
client *core.Client
|
||||||
|
|
||||||
tlsConfig *tlsC.Config
|
tlsConfig *tls.Config
|
||||||
echConfig *ech.Config
|
echConfig *ech.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,16 +73,8 @@ 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.DialOptions()...)
|
|
||||||
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) {
|
||||||
udpAddr, err := resolveUDPAddr(ctx, "udp", addr, h.prefer)
|
udpAddr, err := resolveUDPAddr(ctx, "udp", addr, h.prefer)
|
||||||
@@ -184,7 +175,7 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
tlsClientConfig := tlsC.UConfig(tlsConfig)
|
tlsClientConfig := tlsConfig
|
||||||
|
|
||||||
quicConfig := &quic.Config{
|
quicConfig := &quic.Config{
|
||||||
InitialStreamReceiveWindow: uint64(option.ReceiveWindowConn),
|
InitialStreamReceiveWindow: uint64(option.ReceiveWindowConn),
|
||||||
@@ -252,17 +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,
|
tlsConfig: tlsClientConfig,
|
||||||
echConfig: echConfig,
|
echConfig: echConfig,
|
||||||
}
|
}
|
||||||
|
outbound.dialer = option.NewDialer(outbound.DialOptions())
|
||||||
|
|
||||||
return outbound, nil
|
return outbound, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,19 +2,16 @@ 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"
|
||||||
tlsC "github.com/metacubex/mihomo/component/tls"
|
|
||||||
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"
|
||||||
@@ -22,6 +19,7 @@ import (
|
|||||||
"github.com/metacubex/quic-go"
|
"github.com/metacubex/quic-go"
|
||||||
"github.com/metacubex/sing-quic/hysteria2"
|
"github.com/metacubex/sing-quic/hysteria2"
|
||||||
M "github.com/metacubex/sing/common/metadata"
|
M "github.com/metacubex/sing/common/metadata"
|
||||||
|
"github.com/metacubex/tls"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -36,7 +34,6 @@ type Hysteria2 struct {
|
|||||||
|
|
||||||
option *Hysteria2Option
|
option *Hysteria2Option
|
||||||
client *hysteria2.Client
|
client *hysteria2.Client
|
||||||
dialer proxydialer.SingDialer
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Hysteria2Option struct {
|
type Hysteria2Option struct {
|
||||||
@@ -87,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
|
||||||
@@ -112,16 +109,16 @@ func NewHysteria2(option Hysteria2Option) (*Hysteria2, error) {
|
|||||||
name: option.Name,
|
name: option.Name,
|
||||||
addr: addr,
|
addr: addr,
|
||||||
tp: C.Hysteria2,
|
tp: C.Hysteria2,
|
||||||
|
pdName: option.ProviderName,
|
||||||
udp: true,
|
udp: true,
|
||||||
iface: option.Interface,
|
iface: option.Interface,
|
||||||
rmark: option.RoutingMark,
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: option.IPVersion,
|
||||||
},
|
},
|
||||||
option: &option,
|
option: &option,
|
||||||
}
|
}
|
||||||
|
outbound.dialer = option.NewDialer(outbound.DialOptions())
|
||||||
singDialer := proxydialer.NewByNameSingDialer(option.DialerProxy, dialer.NewDialer(outbound.DialOptions()...))
|
singDialer := proxydialer.NewSingDialer(outbound.dialer)
|
||||||
outbound.dialer = singDialer
|
|
||||||
|
|
||||||
var salamanderPassword string
|
var salamanderPassword string
|
||||||
if len(option.Obfs) > 0 {
|
if len(option.Obfs) > 0 {
|
||||||
@@ -159,7 +156,7 @@ func NewHysteria2(option Hysteria2Option) (*Hysteria2, error) {
|
|||||||
tlsConfig.NextProtos = option.ALPN
|
tlsConfig.NextProtos = option.ALPN
|
||||||
}
|
}
|
||||||
|
|
||||||
tlsClientConfig := tlsC.UConfig(tlsConfig)
|
tlsClientConfig := tlsConfig
|
||||||
echConfig, err := option.ECHOpts.Parse()
|
echConfig, err := option.ECHOpts.Parse()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -192,7 +189,7 @@ func NewHysteria2(option Hysteria2Option) (*Hysteria2, error) {
|
|||||||
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) {
|
||||||
udpAddr, err := resolveUDPAddr(ctx, "udp", addr, C.NewDNSPrefer(option.IPVersion))
|
udpAddr, err := resolveUDPAddr(ctx, "udp", addr, option.IPVersion)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,13 +4,12 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"net/netip"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"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"
|
||||||
@@ -31,8 +30,8 @@ type MieruOption struct {
|
|||||||
BasicOption
|
BasicOption
|
||||||
Name string `proxy:"name"`
|
Name string `proxy:"name"`
|
||||||
Server string `proxy:"server"`
|
Server string `proxy:"server"`
|
||||||
Port string `proxy:"port,omitempty"`
|
Port int `proxy:"port,omitempty"`
|
||||||
PortRange string `proxy:"port-range,omitempty"` // deprecated
|
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"`
|
||||||
@@ -41,6 +40,45 @@ type MieruOption struct {
|
|||||||
HandshakeMode string `proxy:"handshake-mode,omitempty"`
|
HandshakeMode string `proxy:"handshake-mode,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) (C.Conn, error) {
|
func (m *Mieru) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
||||||
if err := m.ensureClientIsRunning(); err != nil {
|
if err := m.ensureClientIsRunning(); err != nil {
|
||||||
@@ -66,7 +104,7 @@ func (m *Mieru) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (
|
|||||||
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
|
||||||
@@ -90,19 +128,13 @@ func (m *Mieru) ensureClientIsRunning() 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.DialOptions()...)
|
|
||||||
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
|
||||||
}
|
}
|
||||||
@@ -124,33 +156,29 @@ func NewMieru(option MieruOption) (*Mieru, error) {
|
|||||||
}
|
}
|
||||||
// Client is started lazily on the first use.
|
// Client is started lazily on the first use.
|
||||||
|
|
||||||
// Use the first port to construct the address.
|
|
||||||
var addr string
|
var addr string
|
||||||
var portStr string
|
if option.Port != 0 {
|
||||||
if option.Port != "" {
|
addr = net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
||||||
portStr = option.Port
|
|
||||||
} else {
|
} else {
|
||||||
portStr = option.PortRange
|
beginPort, _, _ := beginAndEndPortFromPortRange(option.PortRange)
|
||||||
|
addr = net.JoinHostPort(option.Server, strconv.Itoa(beginPort))
|
||||||
}
|
}
|
||||||
firstPort, err := getFirstPort(portStr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to get first port from port string %q: %w", portStr, err)
|
|
||||||
}
|
|
||||||
addr = net.JoinHostPort(option.Server, strconv.Itoa(firstPort))
|
|
||||||
outbound := &Mieru{
|
outbound := &Mieru{
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,23 +193,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) {
|
||||||
@@ -189,63 +215,61 @@ 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 {
|
||||||
portBindings := make([]*mierupb.PortBinding, 0)
|
case "TCP":
|
||||||
if option.Port != "" {
|
transportProtocol = mierupb.TransportProtocol_TCP.Enum()
|
||||||
parts := strings.Split(option.Port, ",")
|
case "UDP":
|
||||||
for _, part := range parts {
|
transportProtocol = mierupb.TransportProtocol_UDP.Enum()
|
||||||
part = strings.TrimSpace(part)
|
|
||||||
if strings.Contains(part, "-") {
|
|
||||||
_, _, err := beginAndEndPortFromPortRange(part)
|
|
||||||
if err == nil {
|
|
||||||
portBindings = append(portBindings, &mierupb.PortBinding{
|
|
||||||
PortRange: proto.String(part),
|
|
||||||
Protocol: transportProtocol,
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
p, err := strconv.Atoi(part)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("invalid port value: %s", part)
|
|
||||||
}
|
|
||||||
portBindings = append(portBindings, &mierupb.PortBinding{
|
|
||||||
Port: proto.Int32(int32(p)),
|
|
||||||
Protocol: transportProtocol,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if option.PortRange != "" {
|
|
||||||
parts := strings.Split(option.PortRange, ",")
|
|
||||||
for _, part := range parts {
|
|
||||||
part = strings.TrimSpace(part)
|
|
||||||
if _, _, err := beginAndEndPortFromPortRange(part); err == nil {
|
|
||||||
portBindings = append(portBindings, &mierupb.PortBinding{
|
|
||||||
PortRange: proto.String(part),
|
|
||||||
Protocol: transportProtocol,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
||||||
server = &mierupb.ServerEndpoint{
|
if option.PortRange != "" {
|
||||||
IpAddress: proto.String(option.Server),
|
server = &mierupb.ServerEndpoint{
|
||||||
PortBindings: portBindings,
|
IpAddress: proto.String(option.Server),
|
||||||
|
PortBindings: []*mierupb.PortBinding{
|
||||||
|
{
|
||||||
|
PortRange: proto.String(option.PortRange),
|
||||||
|
Protocol: transportProtocol,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
server = &mierupb.ServerEndpoint{
|
||||||
|
IpAddress: proto.String(option.Server),
|
||||||
|
PortBindings: []*mierupb.PortBinding{
|
||||||
|
{
|
||||||
|
Port: proto.Int32(int32(option.Port)),
|
||||||
|
Protocol: transportProtocol,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// server is a domain name
|
// server is a domain name
|
||||||
server = &mierupb.ServerEndpoint{
|
if option.PortRange != "" {
|
||||||
DomainName: proto.String(option.Server),
|
server = &mierupb.ServerEndpoint{
|
||||||
PortBindings: portBindings,
|
DomainName: proto.String(option.Server),
|
||||||
|
PortBindings: []*mierupb.PortBinding{
|
||||||
|
{
|
||||||
|
PortRange: proto.String(option.PortRange),
|
||||||
|
Protocol: transportProtocol,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
server = &mierupb.ServerEndpoint{
|
||||||
|
DomainName: proto.String(option.Server),
|
||||||
|
PortBindings: []*mierupb.PortBinding{
|
||||||
|
{
|
||||||
|
Port: proto.Int32(int32(option.Port)),
|
||||||
|
Protocol: transportProtocol,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
config := &mieruclient.ClientConfig{
|
config := &mieruclient.ClientConfig{
|
||||||
Profile: &mierupb.ClientProfile{
|
Profile: &mierupb.ClientProfile{
|
||||||
ProfileName: proto.String(option.Name),
|
ProfileName: proto.String(option.Name),
|
||||||
@@ -255,6 +279,9 @@ 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{
|
||||||
@@ -274,11 +301,33 @@ func validateMieruOption(option MieruOption) error {
|
|||||||
if option.Server == "" {
|
if option.Server == "" {
|
||||||
return fmt.Errorf("server is empty")
|
return fmt.Errorf("server is empty")
|
||||||
}
|
}
|
||||||
if option.Port == "" && option.PortRange == "" {
|
if option.Port == 0 && option.PortRange == "" {
|
||||||
return fmt.Errorf("port must be set")
|
return fmt.Errorf("either port or port-range must be set")
|
||||||
}
|
}
|
||||||
if option.Transport != "TCP" {
|
if option.Port != 0 && option.PortRange != "" {
|
||||||
return fmt.Errorf("transport must be TCP")
|
return fmt.Errorf("port and port-range cannot be set at the same time")
|
||||||
|
}
|
||||||
|
if option.Port != 0 && (option.Port < 1 || option.Port > 65535) {
|
||||||
|
return fmt.Errorf("port must be between 1 and 65535")
|
||||||
|
}
|
||||||
|
if option.PortRange != "" {
|
||||||
|
begin, end, err := beginAndEndPortFromPortRange(option.PortRange)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid port-range format")
|
||||||
|
}
|
||||||
|
if begin < 1 || begin > 65535 {
|
||||||
|
return fmt.Errorf("begin port must be between 1 and 65535")
|
||||||
|
}
|
||||||
|
if end < 1 || end > 65535 {
|
||||||
|
return fmt.Errorf("end port must be between 1 and 65535")
|
||||||
|
}
|
||||||
|
if begin > end {
|
||||||
|
return fmt.Errorf("begin port must be less than or equal to end port")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if option.Transport != "TCP" && option.Transport != "UDP" {
|
||||||
|
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")
|
||||||
@@ -299,36 +348,8 @@ func validateMieruOption(option MieruOption) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getFirstPort(portStr string) (int, error) {
|
|
||||||
if portStr == "" {
|
|
||||||
return 0, fmt.Errorf("port string is empty")
|
|
||||||
}
|
|
||||||
parts := strings.Split(portStr, ",")
|
|
||||||
firstPart := parts[0]
|
|
||||||
|
|
||||||
if strings.Contains(firstPart, "-") {
|
|
||||||
begin, _, err := beginAndEndPortFromPortRange(firstPart)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return begin, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
port, err := strconv.Atoi(firstPart)
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("invalid port format: %s", firstPart)
|
|
||||||
}
|
|
||||||
return port, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func beginAndEndPortFromPortRange(portRange string) (int, int, error) {
|
func beginAndEndPortFromPortRange(portRange string) (int, int, error) {
|
||||||
var begin, end int
|
var begin, end int
|
||||||
_, err := fmt.Sscanf(portRange, "%d-%d", &begin, &end)
|
_, err := fmt.Sscanf(portRange, "%d-%d", &begin, &end)
|
||||||
if err != nil {
|
|
||||||
return 0, 0, fmt.Errorf("invalid port range format: %w", err)
|
|
||||||
}
|
|
||||||
if begin > end {
|
|
||||||
return 0, 0, fmt.Errorf("begin port is greater than end port: %s", portRange)
|
|
||||||
}
|
|
||||||
return begin, end, err
|
return begin, end, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,51 +1,22 @@
|
|||||||
package outbound
|
package outbound
|
||||||
|
|
||||||
import (
|
import "testing"
|
||||||
"reflect"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
mieruclient "github.com/enfein/mieru/v3/apis/client"
|
|
||||||
mierupb "github.com/enfein/mieru/v3/pkg/appctl/appctlpb"
|
|
||||||
"google.golang.org/protobuf/proto"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestNewMieru(t *testing.T) {
|
func TestNewMieru(t *testing.T) {
|
||||||
transportProtocol := mierupb.TransportProtocol_TCP.Enum()
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
option MieruOption
|
option MieruOption
|
||||||
wantBaseAddr string
|
wantBaseAddr string
|
||||||
wantConfig *mieruclient.ClientConfig
|
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
option: MieruOption{
|
option: MieruOption{
|
||||||
Name: "test",
|
Name: "test",
|
||||||
Server: "1.2.3.4",
|
Server: "1.2.3.4",
|
||||||
Port: "10000",
|
Port: 10000,
|
||||||
Transport: "TCP",
|
Transport: "TCP",
|
||||||
UserName: "test",
|
UserName: "test",
|
||||||
Password: "test",
|
Password: "test",
|
||||||
},
|
},
|
||||||
wantBaseAddr: "1.2.3.4:10000",
|
wantBaseAddr: "1.2.3.4:10000",
|
||||||
wantConfig: &mieruclient.ClientConfig{
|
|
||||||
Profile: &mierupb.ClientProfile{
|
|
||||||
ProfileName: proto.String("test"),
|
|
||||||
User: &mierupb.User{
|
|
||||||
Name: proto.String("test"),
|
|
||||||
Password: proto.String("test"),
|
|
||||||
},
|
|
||||||
Servers: []*mierupb.ServerEndpoint{
|
|
||||||
{
|
|
||||||
IpAddress: proto.String("1.2.3.4"),
|
|
||||||
PortBindings: []*mierupb.PortBinding{
|
|
||||||
{
|
|
||||||
Port: proto.Int32(10000),
|
|
||||||
Protocol: transportProtocol,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
option: MieruOption{
|
option: MieruOption{
|
||||||
@@ -57,212 +28,28 @@ func TestNewMieru(t *testing.T) {
|
|||||||
Password: "test",
|
Password: "test",
|
||||||
},
|
},
|
||||||
wantBaseAddr: "[2001:db8::1]:10001",
|
wantBaseAddr: "[2001:db8::1]:10001",
|
||||||
wantConfig: &mieruclient.ClientConfig{
|
|
||||||
Profile: &mierupb.ClientProfile{
|
|
||||||
ProfileName: proto.String("test"),
|
|
||||||
User: &mierupb.User{
|
|
||||||
Name: proto.String("test"),
|
|
||||||
Password: proto.String("test"),
|
|
||||||
},
|
|
||||||
Servers: []*mierupb.ServerEndpoint{
|
|
||||||
{
|
|
||||||
IpAddress: proto.String("2001:db8::1"),
|
|
||||||
PortBindings: []*mierupb.PortBinding{
|
|
||||||
{
|
|
||||||
PortRange: proto.String("10001-10002"),
|
|
||||||
Protocol: transportProtocol,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
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",
|
||||||
},
|
},
|
||||||
wantBaseAddr: "example.com:10003",
|
wantBaseAddr: "example.com:10003",
|
||||||
wantConfig: &mieruclient.ClientConfig{
|
|
||||||
Profile: &mierupb.ClientProfile{
|
|
||||||
ProfileName: proto.String("test"),
|
|
||||||
User: &mierupb.User{
|
|
||||||
Name: proto.String("test"),
|
|
||||||
Password: proto.String("test"),
|
|
||||||
},
|
|
||||||
Servers: []*mierupb.ServerEndpoint{
|
|
||||||
{
|
|
||||||
DomainName: proto.String("example.com"),
|
|
||||||
PortBindings: []*mierupb.PortBinding{
|
|
||||||
{
|
|
||||||
Port: proto.Int32(10003),
|
|
||||||
Protocol: transportProtocol,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
option: MieruOption{
|
|
||||||
Name: "test",
|
|
||||||
Server: "example.com",
|
|
||||||
Port: "10004,10005",
|
|
||||||
Transport: "TCP",
|
|
||||||
UserName: "test",
|
|
||||||
Password: "test",
|
|
||||||
},
|
|
||||||
wantBaseAddr: "example.com:10004",
|
|
||||||
wantConfig: &mieruclient.ClientConfig{
|
|
||||||
Profile: &mierupb.ClientProfile{
|
|
||||||
ProfileName: proto.String("test"),
|
|
||||||
User: &mierupb.User{
|
|
||||||
Name: proto.String("test"),
|
|
||||||
Password: proto.String("test"),
|
|
||||||
},
|
|
||||||
Servers: []*mierupb.ServerEndpoint{
|
|
||||||
{
|
|
||||||
DomainName: proto.String("example.com"),
|
|
||||||
PortBindings: []*mierupb.PortBinding{
|
|
||||||
{
|
|
||||||
Port: proto.Int32(10004),
|
|
||||||
Protocol: transportProtocol,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Port: proto.Int32(10005),
|
|
||||||
Protocol: transportProtocol,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
option: MieruOption{
|
|
||||||
Name: "test",
|
|
||||||
Server: "example.com",
|
|
||||||
Port: "10006-10007,11000",
|
|
||||||
Transport: "TCP",
|
|
||||||
UserName: "test",
|
|
||||||
Password: "test",
|
|
||||||
},
|
|
||||||
wantBaseAddr: "example.com:10006",
|
|
||||||
wantConfig: &mieruclient.ClientConfig{
|
|
||||||
Profile: &mierupb.ClientProfile{
|
|
||||||
ProfileName: proto.String("test"),
|
|
||||||
User: &mierupb.User{
|
|
||||||
Name: proto.String("test"),
|
|
||||||
Password: proto.String("test"),
|
|
||||||
},
|
|
||||||
Servers: []*mierupb.ServerEndpoint{
|
|
||||||
{
|
|
||||||
DomainName: proto.String("example.com"),
|
|
||||||
PortBindings: []*mierupb.PortBinding{
|
|
||||||
{
|
|
||||||
PortRange: proto.String("10006-10007"),
|
|
||||||
Protocol: transportProtocol,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Port: proto.Int32(11000),
|
|
||||||
Protocol: transportProtocol,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
option: MieruOption{
|
|
||||||
Name: "test",
|
|
||||||
Server: "example.com",
|
|
||||||
Port: "10008",
|
|
||||||
PortRange: "10009-10010",
|
|
||||||
Transport: "TCP",
|
|
||||||
UserName: "test",
|
|
||||||
Password: "test",
|
|
||||||
},
|
|
||||||
wantBaseAddr: "example.com:10008",
|
|
||||||
wantConfig: &mieruclient.ClientConfig{
|
|
||||||
Profile: &mierupb.ClientProfile{
|
|
||||||
ProfileName: proto.String("test"),
|
|
||||||
User: &mierupb.User{
|
|
||||||
Name: proto.String("test"),
|
|
||||||
Password: proto.String("test"),
|
|
||||||
},
|
|
||||||
Servers: []*mierupb.ServerEndpoint{
|
|
||||||
{
|
|
||||||
DomainName: proto.String("example.com"),
|
|
||||||
PortBindings: []*mierupb.PortBinding{
|
|
||||||
{
|
|
||||||
Port: proto.Int32(10008),
|
|
||||||
Protocol: transportProtocol,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
PortRange: proto.String("10009-10010"),
|
|
||||||
Protocol: transportProtocol,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, testCase := range testCases {
|
for _, testCase := range testCases {
|
||||||
mieru, err := NewMieru(testCase.option)
|
mieru, err := NewMieru(testCase.option)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
config, err := mieru.client.Load()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
config.Dialer = nil
|
|
||||||
if mieru.addr != testCase.wantBaseAddr {
|
if mieru.addr != testCase.wantBaseAddr {
|
||||||
t.Errorf("got addr %q, want %q", mieru.addr, testCase.wantBaseAddr)
|
t.Errorf("got addr %q, want %q", mieru.addr, testCase.wantBaseAddr)
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(config, testCase.wantConfig) {
|
|
||||||
t.Errorf("got config %+v, want %+v", config, testCase.wantConfig)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNewMieruError(t *testing.T) {
|
|
||||||
testCases := []MieruOption{
|
|
||||||
{
|
|
||||||
Name: "test",
|
|
||||||
Server: "example.com",
|
|
||||||
Port: "invalid",
|
|
||||||
PortRange: "invalid",
|
|
||||||
Transport: "TCP",
|
|
||||||
UserName: "test",
|
|
||||||
Password: "test",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "test",
|
|
||||||
Server: "example.com",
|
|
||||||
Port: "",
|
|
||||||
PortRange: "",
|
|
||||||
Transport: "TCP",
|
|
||||||
UserName: "test",
|
|
||||||
Password: "test",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, option := range testCases {
|
|
||||||
_, err := NewMieru(option)
|
|
||||||
if err == nil {
|
|
||||||
t.Errorf("expected error for option %+v, but got nil", option)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -276,7 +63,6 @@ func TestBeginAndEndPortFromPortRange(t *testing.T) {
|
|||||||
{"1-10", 1, 10, false},
|
{"1-10", 1, 10, false},
|
||||||
{"1000-2000", 1000, 2000, false},
|
{"1000-2000", 1000, 2000, false},
|
||||||
{"65535-65535", 65535, 65535, false},
|
{"65535-65535", 65535, 65535, false},
|
||||||
{"2000-1000", 0, 0, true},
|
|
||||||
{"1", 0, 0, true},
|
{"1", 0, 0, true},
|
||||||
{"1-", 0, 0, true},
|
{"1-", 0, 0, true},
|
||||||
{"-10", 0, 0, true},
|
{"-10", 0, 0, true},
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ type Reject struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type RejectOption struct {
|
type RejectOption struct {
|
||||||
|
BasicOption
|
||||||
Name string `proxy:"name"`
|
Name string `proxy:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,7 +34,10 @@ func (r *Reject) ListenPacketContext(ctx context.Context, metadata *C.Metadata)
|
|||||||
if err := r.ResolveUDP(ctx, metadata); err != nil {
|
if err := r.ResolveUDP(ctx, metadata); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return newPacketConn(&nopPacketConn{}, r), nil
|
if r.drop {
|
||||||
|
return newPacketConn(dropPacketConn{}, r), nil
|
||||||
|
}
|
||||||
|
return newPacketConn(nopPacketConn{}, r), C.ErrResetByRule
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reject) ResolveUDP(ctx context.Context, metadata *C.Metadata) error {
|
func (r *Reject) ResolveUDP(ctx context.Context, metadata *C.Metadata) error {
|
||||||
@@ -89,12 +93,10 @@ func NewPass() *Reject {
|
|||||||
|
|
||||||
type nopConn struct{}
|
type nopConn struct{}
|
||||||
|
|
||||||
func (rw nopConn) Read(b []byte) (int, error) { return 0, io.EOF }
|
func (rw nopConn) Read(b []byte) (int, error) { return 0, C.ErrResetByRule }
|
||||||
|
func (rw nopConn) ReadBuffer(buffer *buf.Buffer) error { return C.ErrResetByRule }
|
||||||
func (rw nopConn) ReadBuffer(buffer *buf.Buffer) error { return io.EOF }
|
func (rw nopConn) Write(b []byte) (int, error) { return 0, C.ErrResetByRule }
|
||||||
|
func (rw nopConn) WriteBuffer(buffer *buf.Buffer) error { return C.ErrResetByRule }
|
||||||
func (rw nopConn) Write(b []byte) (int, error) { return 0, io.EOF }
|
|
||||||
func (rw nopConn) WriteBuffer(buffer *buf.Buffer) error { return io.EOF }
|
|
||||||
func (rw nopConn) Close() error { return nil }
|
func (rw nopConn) Close() error { return nil }
|
||||||
func (rw nopConn) LocalAddr() net.Addr { return nil }
|
func (rw nopConn) LocalAddr() net.Addr { return nil }
|
||||||
func (rw nopConn) RemoteAddr() net.Addr { return nil }
|
func (rw nopConn) RemoteAddr() net.Addr { return nil }
|
||||||
@@ -109,11 +111,9 @@ type nopPacketConn struct{}
|
|||||||
func (npc nopPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
func (npc nopPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
||||||
return len(b), nil
|
return len(b), nil
|
||||||
}
|
}
|
||||||
func (npc nopPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
func (npc nopPacketConn) ReadFrom(b []byte) (int, net.Addr, error) { return 0, nil, C.ErrResetByRule }
|
||||||
return 0, nil, io.EOF
|
|
||||||
}
|
|
||||||
func (npc nopPacketConn) WaitReadFrom() ([]byte, func(), net.Addr, error) {
|
func (npc nopPacketConn) WaitReadFrom() ([]byte, func(), net.Addr, error) {
|
||||||
return nil, nil, nil, io.EOF
|
return nil, nil, nil, C.ErrResetByRule
|
||||||
}
|
}
|
||||||
func (npc nopPacketConn) Close() error { return nil }
|
func (npc nopPacketConn) Close() error { return nil }
|
||||||
func (npc nopPacketConn) LocalAddr() net.Addr { return udpAddrIPv4Unspecified }
|
func (npc nopPacketConn) LocalAddr() net.Addr { return udpAddrIPv4Unspecified }
|
||||||
@@ -136,3 +136,18 @@ func (rw dropConn) RemoteAddr() net.Addr { return nil }
|
|||||||
func (rw dropConn) SetDeadline(time.Time) error { return nil }
|
func (rw dropConn) SetDeadline(time.Time) error { return nil }
|
||||||
func (rw dropConn) SetReadDeadline(time.Time) error { return nil }
|
func (rw dropConn) SetReadDeadline(time.Time) error { return nil }
|
||||||
func (rw dropConn) SetWriteDeadline(time.Time) error { return nil }
|
func (rw dropConn) SetWriteDeadline(time.Time) error { return nil }
|
||||||
|
|
||||||
|
type dropPacketConn struct{}
|
||||||
|
|
||||||
|
func (npc dropPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
||||||
|
return len(b), nil
|
||||||
|
}
|
||||||
|
func (npc dropPacketConn) ReadFrom(b []byte) (int, net.Addr, error) { return 0, nil, io.EOF }
|
||||||
|
func (npc dropPacketConn) WaitReadFrom() ([]byte, func(), net.Addr, error) {
|
||||||
|
return nil, nil, nil, io.EOF
|
||||||
|
}
|
||||||
|
func (npc dropPacketConn) Close() error { return nil }
|
||||||
|
func (npc dropPacketConn) LocalAddr() net.Addr { return udpAddrIPv4Unspecified }
|
||||||
|
func (npc dropPacketConn) SetDeadline(time.Time) error { return nil }
|
||||||
|
func (npc dropPacketConn) SetReadDeadline(time.Time) error { return nil }
|
||||||
|
func (npc dropPacketConn) SetWriteDeadline(time.Time) error { return 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"
|
||||||
"github.com/metacubex/mihomo/ntp"
|
"github.com/metacubex/mihomo/ntp"
|
||||||
gost "github.com/metacubex/mihomo/transport/gost-plugin"
|
gost "github.com/metacubex/mihomo/transport/gost-plugin"
|
||||||
@@ -191,17 +189,6 @@ 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) (_ 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.DialOptions()...), metadata)
|
|
||||||
}
|
|
||||||
|
|
||||||
// DialContextWithDialer implements C.ProxyAdapter
|
|
||||||
func (ss *ShadowSocks) 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var c net.Conn
|
var c net.Conn
|
||||||
if ss.kcptunClient != nil {
|
if ss.kcptunClient != nil {
|
||||||
c, err = ss.kcptunClient.OpenStream(ctx, func(ctx context.Context) (net.PacketConn, net.Addr, error) {
|
c, err = ss.kcptunClient.OpenStream(ctx, func(ctx context.Context) (net.PacketConn, net.Addr, error) {
|
||||||
@@ -213,7 +200,7 @@ func (ss *ShadowSocks) DialContextWithDialer(ctx context.Context, dialer C.Diale
|
|||||||
return nil, nil, err
|
return nil, 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, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@@ -221,7 +208,7 @@ func (ss *ShadowSocks) DialContextWithDialer(ctx context.Context, dialer C.Diale
|
|||||||
return pc, addr, nil
|
return pc, addr, nil
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
c, err = dialer.DialContext(ctx, "tcp", ss.addr)
|
c, err = ss.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)
|
||||||
@@ -237,25 +224,14 @@ 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) (C.PacketConn, error) {
|
func (ss *ShadowSocks) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
||||||
return ss.ListenPacketWithDialer(ctx, dialer.NewDialer(ss.DialOptions()...), 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)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err = ss.ResolveUDP(ctx, metadata); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
addr, err := resolveUDPAddr(ctx, "udp", ss.addr, ss.prefer)
|
addr, err := resolveUDPAddr(ctx, "udp", ss.addr, ss.prefer)
|
||||||
@@ -263,7 +239,7 @@ func (ss *ShadowSocks) ListenPacketWithDialer(ctx context.Context, dialer C.Dial
|
|||||||
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
|
||||||
}
|
}
|
||||||
@@ -271,11 +247,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()
|
||||||
@@ -482,17 +453,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,
|
||||||
|
|
||||||
@@ -504,5 +476,7 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
|||||||
shadowTLSOption: shadowTLSOpt,
|
shadowTLSOption: shadowTLSOpt,
|
||||||
restlsConfig: restlsConfig,
|
restlsConfig: restlsConfig,
|
||||||
kcptunClient: kcptunClient,
|
kcptunClient: kcptunClient,
|
||||||
}, nil
|
}
|
||||||
|
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"
|
||||||
@@ -68,18 +66,7 @@ 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) (_ 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.DialOptions()...), 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)
|
||||||
}
|
}
|
||||||
@@ -94,18 +81,7 @@ 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) (C.PacketConn, error) {
|
func (ssr *ShadowSocksR) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
||||||
return ssr.ListenPacketWithDialer(ctx, dialer.NewDialer(ssr.DialOptions()...), metadata)
|
if err := ssr.ResolveUDP(ctx, metadata); err != nil {
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err = ssr.ResolveUDP(ctx, metadata); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
addr, err := resolveUDPAddr(ctx, "udp", ssr.addr, ssr.prefer)
|
addr, err := resolveUDPAddr(ctx, "udp", ssr.addr, ssr.prefer)
|
||||||
@@ -113,7 +89,7 @@ func (ssr *ShadowSocksR) ListenPacketWithDialer(ctx context.Context, dialer C.Di
|
|||||||
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
|
||||||
}
|
}
|
||||||
@@ -123,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()
|
||||||
@@ -186,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 {
|
||||||
@@ -252,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
|
||||||
|
|||||||
@@ -3,8 +3,7 @@ package outbound
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
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"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/log"
|
"github.com/metacubex/mihomo/log"
|
||||||
@@ -17,7 +16,6 @@ import (
|
|||||||
type SingMux struct {
|
type SingMux struct {
|
||||||
ProxyAdapter
|
ProxyAdapter
|
||||||
client *mux.Client
|
client *mux.Client
|
||||||
dialer proxydialer.SingDialer
|
|
||||||
onlyTcp bool
|
onlyTcp bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,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 {
|
||||||
@@ -96,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(proxy.DialOptions()...), 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,
|
||||||
@@ -117,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"
|
||||||
@@ -89,18 +87,7 @@ func (s *Snell) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn
|
|||||||
return NewConn(c, s), err
|
return NewConn(c, s), err
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.DialContextWithDialer(ctx, dialer.NewDialer(s.DialOptions()...), 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)
|
||||||
}
|
}
|
||||||
@@ -115,22 +102,11 @@ 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) (C.PacketConn, error) {
|
func (s *Snell) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
||||||
return s.ListenPacketWithDialer(ctx, dialer.NewDialer(s.DialOptions()...), 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 {
|
|
||||||
dialer, err = proxydialer.NewByName(s.option.DialerProxy, dialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err = s.ResolveUDP(ctx, metadata); err != nil {
|
if err = s.ResolveUDP(ctx, metadata); err != nil {
|
||||||
return nil, err
|
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
|
||||||
}
|
}
|
||||||
@@ -141,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
|
||||||
@@ -194,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.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 {
|
||||||
@@ -69,18 +68,7 @@ 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) (_ 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.DialOptions()...), 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)
|
||||||
}
|
}
|
||||||
@@ -97,24 +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) (_ 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.DialOptions()...)
|
|
||||||
if len(ss.option.DialerProxy) > 0 {
|
|
||||||
cDialer, err = proxydialer.NewByName(ss.option.DialerProxy, cDialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err = ss.ResolveUDP(ctx, metadata); err != nil {
|
if err = ss.ResolveUDP(ctx, metadata); err != nil {
|
||||||
return nil, err
|
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
|
||||||
@@ -161,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
|
||||||
}
|
}
|
||||||
@@ -210,17 +186,18 @@ func NewSocks5(option Socks5Option) (*Socks5, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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,
|
||||||
@@ -228,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"
|
||||||
@@ -44,14 +42,7 @@ type SshOption struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Ssh) DialContext(ctx context.Context, metadata *C.Metadata) (_ 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.DialOptions()...)
|
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) (_ C.Conn,
|
|||||||
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
|
||||||
}
|
}
|
||||||
@@ -195,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
|
||||||
}
|
}
|
||||||
|
|||||||
262
adapter/outbound/sudoku.go
Normal file
262
adapter/outbound/sudoku.go
Normal file
@@ -0,0 +1,262 @@
|
|||||||
|
package outbound
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
HTTPMaskStrategy string `proxy:"http-mask-strategy,omitempty"` // "random" (default), "post", "websocket"
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
var c net.Conn
|
||||||
|
if !cfg.DisableHTTPMask {
|
||||||
|
switch strings.ToLower(strings.TrimSpace(cfg.HTTPMaskMode)) {
|
||||||
|
case "stream", "poll", "auto":
|
||||||
|
c, err = sudoku.DialHTTPMaskTunnel(ctx, cfg.ServerAddress, cfg, s.dialer.DialContext)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
handshakeCfg := *cfg
|
||||||
|
if !handshakeCfg.DisableHTTPMask {
|
||||||
|
switch strings.ToLower(strings.TrimSpace(handshakeCfg.HTTPMaskMode)) {
|
||||||
|
case "stream", "poll", "auto":
|
||||||
|
handshakeCfg.DisableHTTPMask = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c, err = sudoku.ClientHandshakeWithOptions(c, &handshakeCfg, sudoku.ClientHandshakeOptions{HTTPMaskStrategy: s.option.HTTPMaskStrategy})
|
||||||
|
if err != nil {
|
||||||
|
return nil, 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 {
|
||||||
|
_ = c.Close()
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
var c net.Conn
|
||||||
|
if !cfg.DisableHTTPMask {
|
||||||
|
switch strings.ToLower(strings.TrimSpace(cfg.HTTPMaskMode)) {
|
||||||
|
case "stream", "poll", "auto":
|
||||||
|
c, err = sudoku.DialHTTPMaskTunnel(ctx, cfg.ServerAddress, cfg, s.dialer.DialContext)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
handshakeCfg := *cfg
|
||||||
|
if !handshakeCfg.DisableHTTPMask {
|
||||||
|
switch strings.ToLower(strings.TrimSpace(handshakeCfg.HTTPMaskMode)) {
|
||||||
|
case "stream", "poll", "auto":
|
||||||
|
handshakeCfg.DisableHTTPMask = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c, err = sudoku.ClientHandshakeWithOptions(c, &handshakeCfg, sudoku.ClientHandshakeOptions{HTTPMaskStrategy: s.option.HTTPMaskStrategy})
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
if option.HTTPMaskMode != "" {
|
||||||
|
baseConf.HTTPMaskMode = option.HTTPMaskMode
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
@@ -2,24 +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/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 {
|
||||||
@@ -196,18 +195,7 @@ func (t *Trojan) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Con
|
|||||||
|
|
||||||
return NewConn(c, t), nil
|
return NewConn(c, t), nil
|
||||||
}
|
}
|
||||||
return t.DialContextWithDialer(ctx, dialer.NewDialer(t.DialOptions()...), metadata)
|
c, err = t.dialer.DialContext(ctx, "tcp", t.addr)
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
||||||
}
|
}
|
||||||
@@ -250,21 +238,10 @@ func (t *Trojan) ListenPacketContext(ctx context.Context, metadata *C.Metadata)
|
|||||||
pc := trojan.NewPacketConn(c)
|
pc := trojan.NewPacketConn(c)
|
||||||
return newPacketConn(pc, t), err
|
return newPacketConn(pc, t), err
|
||||||
}
|
}
|
||||||
return t.ListenPacketWithDialer(ctx, dialer.NewDialer(t.DialOptions()...), 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err = t.ResolveUDP(ctx, metadata); err != nil {
|
if err = t.ResolveUDP(ctx, metadata); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
c, err := dialer.DialContext(ctx, "tcp", t.addr)
|
c, err = t.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)
|
||||||
}
|
}
|
||||||
@@ -280,11 +257,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
|
|
||||||
}
|
|
||||||
|
|
||||||
// SupportUOT implements C.ProxyAdapter
|
// SupportUOT implements C.ProxyAdapter
|
||||||
func (t *Trojan) SupportUOT() bool {
|
func (t *Trojan) SupportUOT() bool {
|
||||||
return true
|
return true
|
||||||
@@ -317,16 +289,18 @@ func NewTrojan(option TrojanOption) (*Trojan, error) {
|
|||||||
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()
|
||||||
@@ -355,15 +329,7 @@ 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.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())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package outbound
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"net"
|
"net"
|
||||||
@@ -10,10 +9,7 @@ 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/ech"
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
|
||||||
tlsC "github.com/metacubex/mihomo/component/tls"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/transport/tuic"
|
"github.com/metacubex/mihomo/transport/tuic"
|
||||||
|
|
||||||
@@ -21,6 +17,7 @@ import (
|
|||||||
"github.com/metacubex/quic-go"
|
"github.com/metacubex/quic-go"
|
||||||
M "github.com/metacubex/sing/common/metadata"
|
M "github.com/metacubex/sing/common/metadata"
|
||||||
"github.com/metacubex/sing/common/uot"
|
"github.com/metacubex/sing/common/uot"
|
||||||
|
"github.com/metacubex/tls"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Tuic struct {
|
type Tuic struct {
|
||||||
@@ -28,7 +25,7 @@ type Tuic struct {
|
|||||||
option *TuicOption
|
option *TuicOption
|
||||||
client *tuic.PoolClient
|
client *tuic.PoolClient
|
||||||
|
|
||||||
tlsConfig *tlsC.Config
|
tlsConfig *tls.Config
|
||||||
echConfig *ech.Config
|
echConfig *ech.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,12 +67,7 @@ type TuicOption struct {
|
|||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (t *Tuic) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
func (t *Tuic) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
||||||
return t.DialContextWithDialer(ctx, dialer.NewDialer(t.DialOptions()...), 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
|
||||||
}
|
}
|
||||||
@@ -84,11 +76,6 @@ 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) (_ 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.DialOptions()...), metadata)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListenPacketWithDialer implements C.ProxyAdapter
|
|
||||||
func (t *Tuic) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
|
||||||
if err = t.ResolveUDP(ctx, metadata); err != nil {
|
if err = t.ResolveUDP(ctx, metadata); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -98,7 +85,7 @@ func (t *Tuic) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, meta
|
|||||||
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
|
||||||
}
|
}
|
||||||
@@ -112,25 +99,14 @@ 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
|
||||||
@@ -141,7 +117,7 @@ func (t *Tuic) dialWithDialer(ctx context.Context, dialer C.Dialer) (transport *
|
|||||||
}
|
}
|
||||||
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
|
||||||
}
|
}
|
||||||
@@ -256,7 +232,7 @@ 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 := tlsC.UConfig(tlsConfig)
|
tlsClientConfig := tlsConfig
|
||||||
echConfig, err := option.ECHOpts.Parse()
|
echConfig, err := option.ECHOpts.Parse()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -275,16 +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,
|
tlsConfig: tlsClientConfig,
|
||||||
echConfig: echConfig,
|
echConfig: echConfig,
|
||||||
}
|
}
|
||||||
|
t.dialer = option.NewDialer(t.DialOptions())
|
||||||
|
|
||||||
clientMaxOpenStreams := int64(option.MaxOpenStreams)
|
clientMaxOpenStreams := int64(option.MaxOpenStreams)
|
||||||
|
|
||||||
@@ -313,7 +291,7 @@ 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 {
|
||||||
@@ -332,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
|
||||||
|
|||||||
@@ -2,19 +2,15 @@ package outbound
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"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/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"
|
||||||
@@ -22,9 +18,11 @@ import (
|
|||||||
"github.com/metacubex/mihomo/transport/vless/encryption"
|
"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/metacubex/sing/common/metadata"
|
M "github.com/metacubex/sing/common/metadata"
|
||||||
|
"github.com/metacubex/tls"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Vless struct {
|
type Vless struct {
|
||||||
@@ -252,18 +250,7 @@ func (v *Vless) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn
|
|||||||
|
|
||||||
return NewConn(c, v), nil
|
return NewConn(c, v), nil
|
||||||
}
|
}
|
||||||
return v.DialContextWithDialer(ctx, dialer.NewDialer(v.DialOptions()...), metadata)
|
c, err = v.dialer.DialContext(ctx, "tcp", v.addr)
|
||||||
}
|
|
||||||
|
|
||||||
// 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())
|
||||||
}
|
}
|
||||||
@@ -301,23 +288,12 @@ func (v *Vless) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (
|
|||||||
|
|
||||||
return v.ListenPacketOnStreamConn(ctx, c, metadata)
|
return v.ListenPacketOnStreamConn(ctx, c, metadata)
|
||||||
}
|
}
|
||||||
return v.ListenPacketWithDialer(ctx, dialer.NewDialer(v.DialOptions()...), 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = v.ResolveUDP(ctx, metadata); err != nil {
|
if err = v.ResolveUDP(ctx, metadata); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err := dialer.DialContext(ctx, "tcp", v.addr)
|
c, err = v.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())
|
||||||
}
|
}
|
||||||
@@ -333,11 +309,6 @@ 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) {
|
||||||
if err = v.ResolveUDP(ctx, metadata); err != nil {
|
if err = v.ResolveUDP(ctx, metadata); err != nil {
|
||||||
@@ -446,17 +417,19 @@ 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)
|
v.encryption, err = encryption.NewClient(option.Encryption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -480,15 +453,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.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())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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/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/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/metacubex/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")
|
||||||
@@ -313,18 +311,7 @@ func (v *Vmess) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn
|
|||||||
|
|
||||||
return NewConn(c, v), nil
|
return NewConn(c, v), nil
|
||||||
}
|
}
|
||||||
return v.DialContextWithDialer(ctx, dialer.NewDialer(v.DialOptions()...), metadata)
|
c, err = v.dialer.DialContext(ctx, "tcp", v.addr)
|
||||||
}
|
|
||||||
|
|
||||||
// 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())
|
||||||
}
|
}
|
||||||
@@ -358,23 +345,12 @@ func (v *Vmess) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (
|
|||||||
}
|
}
|
||||||
return v.ListenPacketOnStreamConn(ctx, c, metadata)
|
return v.ListenPacketOnStreamConn(ctx, c, metadata)
|
||||||
}
|
}
|
||||||
return v.ListenPacketWithDialer(ctx, dialer.NewDialer(v.DialOptions()...), 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = v.ResolveUDP(ctx, metadata); err != nil {
|
if err = v.ResolveUDP(ctx, metadata); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err := dialer.DialContext(ctx, "tcp", v.addr)
|
c, err = v.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())
|
||||||
}
|
}
|
||||||
@@ -389,11 +365,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()
|
||||||
@@ -456,17 +427,19 @@ 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 {
|
||||||
@@ -485,15 +458,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.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())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,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
|
||||||
@@ -171,14 +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,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
singDialer := proxydialer.NewSlowDownSingDialer(proxydialer.NewByNameSingDialer(option.DialerProxy, dialer.NewDialer(outbound.DialOptions()...)), slowdown.New())
|
outbound.dialer = option.NewDialer(outbound.DialOptions())
|
||||||
outbound.dialer = singDialer
|
singDialer := proxydialer.NewSlowDownSingDialer(proxydialer.NewSingDialer(outbound.dialer), slowdown.New())
|
||||||
|
|
||||||
var reserved [3]uint8
|
var reserved [3]uint8
|
||||||
if len(option.Reserved) > 0 {
|
if len(option.Reserved) > 0 {
|
||||||
@@ -196,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()
|
||||||
@@ -609,6 +609,13 @@ func (w *WireGuard) ResolveUDP(ctx context.Context, metadata *C.Metadata) error
|
|||||||
return nil
|
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
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ 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"
|
||||||
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 {
|
||||||
@@ -150,7 +150,15 @@ 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{
|
||||||
Name: option.Name,
|
Name: option.Name,
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -48,7 +47,7 @@ type GroupBaseOption struct {
|
|||||||
ExcludeType 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 {
|
||||||
@@ -125,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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ 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"
|
||||||
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"
|
||||||
)
|
)
|
||||||
@@ -194,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)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -239,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":
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
"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"
|
"github.com/metacubex/mihomo/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -48,7 +48,7 @@ type GroupCommonOption struct {
|
|||||||
RoutingMark int `group:"routing-mark,omitempty"`
|
RoutingMark int `group:"routing-mark,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{
|
||||||
@@ -71,7 +71,7 @@ func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, provide
|
|||||||
|
|
||||||
groupName := groupOption.Name
|
groupName := groupOption.Name
|
||||||
|
|
||||||
providers := []types.ProxyProvider{}
|
providers := []P.ProxyProvider{}
|
||||||
|
|
||||||
if groupOption.IncludeAll {
|
if groupOption.IncludeAll {
|
||||||
groupOption.IncludeAllProviders = true
|
groupOption.IncludeAllProviders = true
|
||||||
@@ -169,7 +169,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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,7 +186,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)
|
||||||
}
|
}
|
||||||
@@ -206,15 +206,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)
|
||||||
@@ -222,7 +222,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,163 +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) (C.Conn, error) {
|
|
||||||
proxies, chainProxies := r.proxies(metadata, true)
|
|
||||||
|
|
||||||
switch len(proxies) {
|
|
||||||
case 0:
|
|
||||||
return outbound.NewDirect().DialContext(ctx, metadata)
|
|
||||||
case 1:
|
|
||||||
return proxies[0].DialContext(ctx, metadata)
|
|
||||||
}
|
|
||||||
var d C.Dialer
|
|
||||||
d = dialer.NewDialer()
|
|
||||||
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) (_ C.PacketConn, err error) {
|
|
||||||
proxies, chainProxies := r.proxies(metadata, true)
|
|
||||||
|
|
||||||
switch len(proxies) {
|
|
||||||
case 0:
|
|
||||||
return outbound.NewDirect().ListenPacketContext(ctx, metadata)
|
|
||||||
case 1:
|
|
||||||
return proxies[0].ListenPacketContext(ctx, metadata)
|
|
||||||
}
|
|
||||||
|
|
||||||
var d C.Dialer
|
|
||||||
d = dialer.NewDialer()
|
|
||||||
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{
|
|
||||||
Name: option.Name,
|
|
||||||
Type: C.Relay,
|
|
||||||
Providers: providers,
|
|
||||||
}),
|
|
||||||
Hidden: option.Hidden,
|
|
||||||
Icon: option.Icon,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
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 {
|
||||||
@@ -108,7 +108,15 @@ 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{
|
||||||
Name: option.Name,
|
Name: option.Name,
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
"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/constant/provider"
|
P "github.com/metacubex/mihomo/constant/provider"
|
||||||
)
|
)
|
||||||
|
|
||||||
type urlTestOption func(*URLTest)
|
type urlTestOption func(*URLTest)
|
||||||
@@ -185,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)
|
||||||
}
|
}
|
||||||
@@ -202,7 +210,7 @@ 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{
|
||||||
Name: option.Name,
|
Name: option.Name,
|
||||||
|
|||||||
@@ -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,157 @@ 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)
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unsupport proxy type: %s", proxyType)
|
return nil, fmt.Errorf("unsupport proxy type: %s", proxyType)
|
||||||
}
|
}
|
||||||
@@ -177,3 +184,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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ 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"
|
"github.com/dlclark/regexp2"
|
||||||
)
|
)
|
||||||
@@ -73,7 +73,7 @@ type proxyProviderSchema struct {
|
|||||||
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{
|
||||||
@@ -99,12 +99,12 @@ 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)
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/adapter"
|
"github.com/metacubex/mihomo/adapter"
|
||||||
@@ -16,10 +16,11 @@ import (
|
|||||||
"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"
|
||||||
|
"github.com/metacubex/http"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -43,6 +44,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,6 +56,8 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,15 +72,19 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,6 +101,8 @@ 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.setProxies(proxies)
|
bp.healthCheck.setProxies(proxies)
|
||||||
@@ -156,7 +166,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
|
||||||
@@ -171,7 +181,7 @@ func (pp *proxySetProvider) Close() error {
|
|||||||
return pp.Fetcher.Close()
|
return pp.Fetcher.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProxySetProvider(name string, interval time.Duration, payload []map[string]any, 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) {
|
||||||
pd := &proxySetProvider{
|
pd := &proxySetProvider{
|
||||||
baseProvider: baseProvider{
|
baseProvider: baseProvider{
|
||||||
name: name,
|
name: name,
|
||||||
@@ -238,8 +248,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) Update() error {
|
func (ip *inlineProvider) Update() error {
|
||||||
@@ -303,8 +313,8 @@ func (cp *compatibleProvider) Update() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cp *compatibleProvider) VehicleType() types.VehicleType {
|
func (cp *compatibleProvider) VehicleType() P.VehicleType {
|
||||||
return types.Compatible
|
return P.Compatible
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCompatibleProvider(name string, proxies []C.Proxy, hc *HealthCheck) (*CompatibleProvider, error) {
|
func NewCompatibleProvider(name string, proxies []C.Proxy, hc *HealthCheck) (*CompatibleProvider, error) {
|
||||||
@@ -330,7 +340,7 @@ 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) {
|
||||||
var excludeTypeArray []string
|
var excludeTypeArray []string
|
||||||
if excludeType != "" {
|
if excludeType != "" {
|
||||||
excludeTypeArray = strings.Split(excludeType, "|")
|
excludeTypeArray = strings.Split(excludeType, "|")
|
||||||
@@ -448,7 +458,7 @@ func NewProxiesParser(filter string, excludeFilter string, excludeType string, d
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -138,3 +138,5 @@ func escape[T any](x T) T {
|
|||||||
// ptrSize is the size of a pointer in bytes - unsafe.Sizeof(uintptr(0)) but as an ideal constant.
|
// 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).
|
// 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 ptrSize = 4 << (^uintptr(0) >> 63)
|
||||||
|
|
||||||
|
const testComparableAllocations = false
|
||||||
|
|||||||
@@ -11,3 +11,5 @@ func Comparable[T comparable](seed Seed, v T) uint64 {
|
|||||||
func WriteComparable[T comparable](h *Hash, x T) {
|
func WriteComparable[T comparable](h *Hash, x T) {
|
||||||
maphash.WriteComparable(h, x)
|
maphash.WriteComparable(h, x)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const testComparableAllocations = true
|
||||||
|
|||||||
@@ -423,7 +423,9 @@ func TestWriteComparableNoncommute(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestComparableAllocations(t *testing.T) {
|
func TestComparableAllocations(t *testing.T) {
|
||||||
t.Skip("test broken in old golang version")
|
if !testComparableAllocations {
|
||||||
|
t.Skip("test broken in old golang version")
|
||||||
|
}
|
||||||
seed := MakeSeed()
|
seed := MakeSeed()
|
||||||
x := heapStr(t)
|
x := heapStr(t)
|
||||||
allocs := testing.AllocsPerRun(10, func() {
|
allocs := testing.AllocsPerRun(10, func() {
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ var WriteBuffer = bufio.WriteBuffer
|
|||||||
type ReadWaitOptions = network.ReadWaitOptions
|
type ReadWaitOptions = network.ReadWaitOptions
|
||||||
|
|
||||||
var NewReadWaitOptions = network.NewReadWaitOptions
|
var NewReadWaitOptions = network.NewReadWaitOptions
|
||||||
|
var CalculateFrontHeadroom = network.CalculateFrontHeadroom
|
||||||
|
var CalculateRearHeadroom = network.CalculateRearHeadroom
|
||||||
|
|
||||||
type ReaderWithUpstream = network.ReaderWithUpstream
|
type ReaderWithUpstream = network.ReaderWithUpstream
|
||||||
type WithUpstreamReader = network.WithUpstreamReader
|
type WithUpstreamReader = network.WithUpstreamReader
|
||||||
@@ -31,6 +33,9 @@ type WriterWithUpstream = network.WriterWithUpstream
|
|||||||
type WithUpstreamWriter = network.WithUpstreamWriter
|
type WithUpstreamWriter = network.WithUpstreamWriter
|
||||||
type WithUpstream = common.WithUpstream
|
type WithUpstream = common.WithUpstream
|
||||||
|
|
||||||
|
type HandshakeSuccess = network.HandshakeSuccess
|
||||||
|
type HandshakeFailure = network.HandshakeFailure
|
||||||
|
|
||||||
var UnwrapReader = network.UnwrapReader
|
var UnwrapReader = network.UnwrapReader
|
||||||
var UnwrapWriter = network.UnwrapWriter
|
var UnwrapWriter = network.UnwrapWriter
|
||||||
|
|
||||||
|
|||||||
@@ -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[:])
|
||||||
|
}
|
||||||
|
|||||||
@@ -53,6 +53,12 @@ func (d *Decoder) Decode(src map[string]any, dst any) error {
|
|||||||
key, omitKey, found := strings.Cut(tag, ",")
|
key, omitKey, found := strings.Cut(tag, ",")
|
||||||
omitempty := found && omitKey == "omitempty"
|
omitempty := found && omitKey == "omitempty"
|
||||||
|
|
||||||
|
// As a special case, if the field tag is "-", the field is always omitted.
|
||||||
|
// Note that a field with name "-" can still be generated using the tag "-,".
|
||||||
|
if key == "-" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
value, ok := src[key]
|
value, ok := src[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
if d.option.KeyReplacer != nil {
|
if d.option.KeyReplacer != nil {
|
||||||
@@ -283,7 +289,7 @@ func (d *Decoder) decodeBool(name string, data any, val reflect.Value) (err erro
|
|||||||
case isInt(kind) && d.option.WeaklyTypedInput:
|
case isInt(kind) && d.option.WeaklyTypedInput:
|
||||||
val.SetBool(dataVal.Int() != 0)
|
val.SetBool(dataVal.Int() != 0)
|
||||||
case isUint(kind) && d.option.WeaklyTypedInput:
|
case isUint(kind) && d.option.WeaklyTypedInput:
|
||||||
val.SetString(strconv.FormatUint(dataVal.Uint(), 10))
|
val.SetBool(dataVal.Uint() != 0)
|
||||||
default:
|
default:
|
||||||
err = fmt.Errorf(
|
err = fmt.Errorf(
|
||||||
"'%s' expected type '%s', got unconvertible type '%s'",
|
"'%s' expected type '%s', got unconvertible type '%s'",
|
||||||
@@ -511,6 +517,10 @@ func (d *Decoder) decodeStructFromMap(name string, dataVal, val reflect.Value) e
|
|||||||
fieldName = tagValue
|
fieldName = tagValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if tagValue == "-" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
rawMapKey := reflect.ValueOf(fieldName)
|
rawMapKey := reflect.ValueOf(fieldName)
|
||||||
rawMapVal := dataVal.MapIndex(rawMapKey)
|
rawMapVal := dataVal.MapIndex(rawMapKey)
|
||||||
if !rawMapVal.IsValid() {
|
if !rawMapVal.IsValid() {
|
||||||
|
|||||||
@@ -288,3 +288,47 @@ func TestStructure_Null(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, s.Opt.Bar, "")
|
assert.Equal(t, s.Opt.Bar, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStructure_Ignore(t *testing.T) {
|
||||||
|
rawMap := map[string]any{
|
||||||
|
"-": "newData",
|
||||||
|
}
|
||||||
|
|
||||||
|
s := struct {
|
||||||
|
MustIgnore string `test:"-"`
|
||||||
|
}{MustIgnore: "oldData"}
|
||||||
|
|
||||||
|
err := decoder.Decode(rawMap, &s)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, s.MustIgnore, "oldData")
|
||||||
|
|
||||||
|
// test omitempty
|
||||||
|
delete(rawMap, "-")
|
||||||
|
err = decoder.Decode(rawMap, &s)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, s.MustIgnore, "oldData")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStructure_IgnoreInNest(t *testing.T) {
|
||||||
|
rawMap := map[string]any{
|
||||||
|
"-": "newData",
|
||||||
|
}
|
||||||
|
|
||||||
|
type TP struct {
|
||||||
|
MustIgnore string `test:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
s := struct {
|
||||||
|
TP
|
||||||
|
}{TP{MustIgnore: "oldData"}}
|
||||||
|
|
||||||
|
err := decoder.Decode(rawMap, &s)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, s.MustIgnore, "oldData")
|
||||||
|
|
||||||
|
// test omitempty
|
||||||
|
delete(rawMap, "-")
|
||||||
|
err = decoder.Decode(rawMap, &s)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, s.MustIgnore, "oldData")
|
||||||
|
}
|
||||||
|
|||||||
31
common/utils/cast.go
Normal file
31
common/utils/cast.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
)
|
||||||
|
|
||||||
|
type WithUpstream interface {
|
||||||
|
Upstream() any
|
||||||
|
}
|
||||||
|
|
||||||
|
type stdWithUpstreamNetConn interface {
|
||||||
|
NetConn() net.Conn
|
||||||
|
}
|
||||||
|
|
||||||
|
func Cast[T any](obj any) (_ T, _ bool) {
|
||||||
|
if c, ok := obj.(T); ok {
|
||||||
|
fmt.Printf("Got 1: %T\n", obj) // TODO
|
||||||
|
return c, true
|
||||||
|
}
|
||||||
|
if u, ok := obj.(WithUpstream); ok {
|
||||||
|
fmt.Printf("Upstream 2: %T\n", obj) // TODO
|
||||||
|
return Cast[T](u.Upstream())
|
||||||
|
}
|
||||||
|
if u, ok := obj.(stdWithUpstreamNetConn); ok {
|
||||||
|
fmt.Printf("Std 3: %T\n", obj) // TODO
|
||||||
|
return Cast[T](u.NetConn())
|
||||||
|
}
|
||||||
|
fmt.Printf("Failed: %T\n", obj) // TODO
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -1,16 +1,17 @@
|
|||||||
package xsync
|
package xsync
|
||||||
|
|
||||||
// copy and modified from https://github.com/puzpuzpuz/xsync/blob/v4.1.0/map.go
|
// copy and modified from https://github.com/puzpuzpuz/xsync/blob/v4.2.0/map.go
|
||||||
// which is licensed under Apache v2.
|
// which is licensed under Apache v2.
|
||||||
//
|
//
|
||||||
// mihomo modified:
|
// mihomo modified:
|
||||||
// 1. parallel Map resize has been removed to decrease the memory using.
|
// 1. restore xsync/v3's LoadOrCompute api and rename to LoadOrStoreFn.
|
||||||
// 2. the zero Map is ready for use.
|
// 2. the zero Map is ready for use.
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"math/bits"
|
"math/bits"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@@ -41,8 +42,28 @@ const (
|
|||||||
metaMask uint64 = 0xffffffffff
|
metaMask uint64 = 0xffffffffff
|
||||||
defaultMetaMasked uint64 = defaultMeta & metaMask
|
defaultMetaMasked uint64 = defaultMeta & metaMask
|
||||||
emptyMetaSlot uint8 = 0x80
|
emptyMetaSlot uint8 = 0x80
|
||||||
|
// minimal number of buckets to transfer when participating in cooperative
|
||||||
|
// resize; should be at least defaultMinMapTableLen
|
||||||
|
minResizeTransferStride = 64
|
||||||
|
// upper limit for max number of additional goroutines that participate
|
||||||
|
// in cooperative resize; must be changed simultaneously with resizeCtl
|
||||||
|
// and the related code
|
||||||
|
maxResizeHelpersLimit = (1 << 5) - 1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// max number of additional goroutines that participate in cooperative resize;
|
||||||
|
// "resize owner" goroutine isn't counted
|
||||||
|
var maxResizeHelpers = func() int32 {
|
||||||
|
v := int32(parallelism() - 1)
|
||||||
|
if v < 1 {
|
||||||
|
v = 1
|
||||||
|
}
|
||||||
|
if v > maxResizeHelpersLimit {
|
||||||
|
v = maxResizeHelpersLimit
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}()
|
||||||
|
|
||||||
type mapResizeHint int
|
type mapResizeHint int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -100,16 +121,25 @@ type Map[K comparable, V any] struct {
|
|||||||
initOnce sync.Once
|
initOnce sync.Once
|
||||||
totalGrowths atomic.Int64
|
totalGrowths atomic.Int64
|
||||||
totalShrinks atomic.Int64
|
totalShrinks atomic.Int64
|
||||||
resizing atomic.Bool // resize in progress flag
|
|
||||||
resizeMu sync.Mutex // only used along with resizeCond
|
|
||||||
resizeCond sync.Cond // used to wake up resize waiters (concurrent modifications)
|
|
||||||
table atomic.Pointer[mapTable[K, V]]
|
table atomic.Pointer[mapTable[K, V]]
|
||||||
minTableLen int
|
// table being transferred to
|
||||||
growOnly bool
|
nextTable atomic.Pointer[mapTable[K, V]]
|
||||||
|
// resize control state: combines resize sequence number (upper 59 bits) and
|
||||||
|
// the current number of resize helpers (lower 5 bits);
|
||||||
|
// odd values of resize sequence mean in-progress resize
|
||||||
|
resizeCtl atomic.Uint64
|
||||||
|
// only used along with resizeCond
|
||||||
|
resizeMu sync.Mutex
|
||||||
|
// used to wake up resize waiters (concurrent writes)
|
||||||
|
resizeCond sync.Cond
|
||||||
|
// transfer progress index for resize
|
||||||
|
resizeIdx atomic.Int64
|
||||||
|
minTableLen int
|
||||||
|
growOnly bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type mapTable[K comparable, V any] struct {
|
type mapTable[K comparable, V any] struct {
|
||||||
buckets []bucketPadded[K, V]
|
buckets []bucketPadded
|
||||||
// striped counter for number of table entries;
|
// striped counter for number of table entries;
|
||||||
// used to determine if a table shrinking is needed
|
// used to determine if a table shrinking is needed
|
||||||
// occupies min(buckets_memory/1024, 64KB) of memory
|
// occupies min(buckets_memory/1024, 64KB) of memory
|
||||||
@@ -125,16 +155,16 @@ type counterStripe struct {
|
|||||||
|
|
||||||
// bucketPadded is a CL-sized map bucket holding up to
|
// bucketPadded is a CL-sized map bucket holding up to
|
||||||
// entriesPerMapBucket entries.
|
// entriesPerMapBucket entries.
|
||||||
type bucketPadded[K comparable, V any] struct {
|
type bucketPadded struct {
|
||||||
//lint:ignore U1000 ensure each bucket takes two cache lines on both 32 and 64-bit archs
|
//lint:ignore U1000 ensure each bucket takes two cache lines on both 32 and 64-bit archs
|
||||||
pad [cacheLineSize - unsafe.Sizeof(bucket[K, V]{})]byte
|
pad [cacheLineSize - unsafe.Sizeof(bucket{})]byte
|
||||||
bucket[K, V]
|
bucket
|
||||||
}
|
}
|
||||||
|
|
||||||
type bucket[K comparable, V any] struct {
|
type bucket struct {
|
||||||
meta atomic.Uint64
|
meta uint64
|
||||||
entries [entriesPerMapBucket]atomic.Pointer[entry[K, V]] // *entry
|
entries [entriesPerMapBucket]unsafe.Pointer // *entry
|
||||||
next atomic.Pointer[bucketPadded[K, V]] // *bucketPadded
|
next unsafe.Pointer // *bucketPadded
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,15 +224,15 @@ func (m *Map[K, V]) init() {
|
|||||||
m.minTableLen = defaultMinMapTableLen
|
m.minTableLen = defaultMinMapTableLen
|
||||||
}
|
}
|
||||||
m.resizeCond = *sync.NewCond(&m.resizeMu)
|
m.resizeCond = *sync.NewCond(&m.resizeMu)
|
||||||
table := newMapTable[K, V](m.minTableLen)
|
table := newMapTable[K, V](m.minTableLen, maphash.MakeSeed())
|
||||||
m.minTableLen = len(table.buckets)
|
m.minTableLen = len(table.buckets)
|
||||||
m.table.Store(table)
|
m.table.Store(table)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newMapTable[K comparable, V any](minTableLen int) *mapTable[K, V] {
|
func newMapTable[K comparable, V any](minTableLen int, seed maphash.Seed) *mapTable[K, V] {
|
||||||
buckets := make([]bucketPadded[K, V], minTableLen)
|
buckets := make([]bucketPadded, minTableLen)
|
||||||
for i := range buckets {
|
for i := range buckets {
|
||||||
buckets[i].meta.Store(defaultMeta)
|
buckets[i].meta = defaultMeta
|
||||||
}
|
}
|
||||||
counterLen := minTableLen >> 10
|
counterLen := minTableLen >> 10
|
||||||
if counterLen < minMapCounterLen {
|
if counterLen < minMapCounterLen {
|
||||||
@@ -214,7 +244,7 @@ func newMapTable[K comparable, V any](minTableLen int) *mapTable[K, V] {
|
|||||||
t := &mapTable[K, V]{
|
t := &mapTable[K, V]{
|
||||||
buckets: buckets,
|
buckets: buckets,
|
||||||
size: counter,
|
size: counter,
|
||||||
seed: maphash.MakeSeed(),
|
seed: seed,
|
||||||
}
|
}
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
@@ -246,22 +276,24 @@ func (m *Map[K, V]) Load(key K) (value V, ok bool) {
|
|||||||
bidx := uint64(len(table.buckets)-1) & h1
|
bidx := uint64(len(table.buckets)-1) & h1
|
||||||
b := &table.buckets[bidx]
|
b := &table.buckets[bidx]
|
||||||
for {
|
for {
|
||||||
metaw := b.meta.Load()
|
metaw := atomic.LoadUint64(&b.meta)
|
||||||
markedw := markZeroBytes(metaw^h2w) & metaMask
|
markedw := markZeroBytes(metaw^h2w) & metaMask
|
||||||
for markedw != 0 {
|
for markedw != 0 {
|
||||||
idx := firstMarkedByteIndex(markedw)
|
idx := firstMarkedByteIndex(markedw)
|
||||||
e := b.entries[idx].Load()
|
eptr := atomic.LoadPointer(&b.entries[idx])
|
||||||
if e != nil {
|
if eptr != nil {
|
||||||
|
e := (*entry[K, V])(eptr)
|
||||||
if e.key == key {
|
if e.key == key {
|
||||||
return e.value, true
|
return e.value, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
markedw &= markedw - 1
|
markedw &= markedw - 1
|
||||||
}
|
}
|
||||||
b = b.next.Load()
|
bptr := atomic.LoadPointer(&b.next)
|
||||||
if b == nil {
|
if bptr == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
b = (*bucketPadded)(bptr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -399,7 +431,7 @@ func (m *Map[K, V]) doCompute(
|
|||||||
for {
|
for {
|
||||||
compute_attempt:
|
compute_attempt:
|
||||||
var (
|
var (
|
||||||
emptyb *bucketPadded[K, V]
|
emptyb *bucketPadded
|
||||||
emptyidx int
|
emptyidx int
|
||||||
)
|
)
|
||||||
table := m.table.Load()
|
table := m.table.Load()
|
||||||
@@ -415,12 +447,13 @@ func (m *Map[K, V]) doCompute(
|
|||||||
b := rootb
|
b := rootb
|
||||||
load:
|
load:
|
||||||
for {
|
for {
|
||||||
metaw := b.meta.Load()
|
metaw := atomic.LoadUint64(&b.meta)
|
||||||
markedw := markZeroBytes(metaw^h2w) & metaMask
|
markedw := markZeroBytes(metaw^h2w) & metaMask
|
||||||
for markedw != 0 {
|
for markedw != 0 {
|
||||||
idx := firstMarkedByteIndex(markedw)
|
idx := firstMarkedByteIndex(markedw)
|
||||||
e := b.entries[idx].Load()
|
eptr := atomic.LoadPointer(&b.entries[idx])
|
||||||
if e != nil {
|
if eptr != nil {
|
||||||
|
e := (*entry[K, V])(eptr)
|
||||||
if e.key == key {
|
if e.key == key {
|
||||||
if loadOp == loadOrComputeOp {
|
if loadOp == loadOrComputeOp {
|
||||||
return e.value, true
|
return e.value, true
|
||||||
@@ -430,23 +463,24 @@ func (m *Map[K, V]) doCompute(
|
|||||||
}
|
}
|
||||||
markedw &= markedw - 1
|
markedw &= markedw - 1
|
||||||
}
|
}
|
||||||
b = b.next.Load()
|
bptr := atomic.LoadPointer(&b.next)
|
||||||
if b == nil {
|
if bptr == nil {
|
||||||
if loadOp == loadAndDeleteOp {
|
if loadOp == loadAndDeleteOp {
|
||||||
return *new(V), false
|
return *new(V), false
|
||||||
}
|
}
|
||||||
break load
|
break load
|
||||||
}
|
}
|
||||||
|
b = (*bucketPadded)(bptr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rootb.mu.Lock()
|
rootb.mu.Lock()
|
||||||
// The following two checks must go in reverse to what's
|
// The following two checks must go in reverse to what's
|
||||||
// in the resize method.
|
// in the resize method.
|
||||||
if m.resizeInProgress() {
|
if seq := resizeSeq(m.resizeCtl.Load()); seq&1 == 1 {
|
||||||
// Resize is in progress. Wait, then go for another attempt.
|
// Resize is in progress. Help with the transfer, then go for another attempt.
|
||||||
rootb.mu.Unlock()
|
rootb.mu.Unlock()
|
||||||
m.waitForResize()
|
m.helpResize(seq)
|
||||||
goto compute_attempt
|
goto compute_attempt
|
||||||
}
|
}
|
||||||
if m.newerTableExists(table) {
|
if m.newerTableExists(table) {
|
||||||
@@ -456,12 +490,13 @@ func (m *Map[K, V]) doCompute(
|
|||||||
}
|
}
|
||||||
b := rootb
|
b := rootb
|
||||||
for {
|
for {
|
||||||
metaw := b.meta.Load()
|
metaw := b.meta
|
||||||
markedw := markZeroBytes(metaw^h2w) & metaMask
|
markedw := markZeroBytes(metaw^h2w) & metaMask
|
||||||
for markedw != 0 {
|
for markedw != 0 {
|
||||||
idx := firstMarkedByteIndex(markedw)
|
idx := firstMarkedByteIndex(markedw)
|
||||||
e := b.entries[idx].Load()
|
eptr := b.entries[idx]
|
||||||
if e != nil {
|
if eptr != nil {
|
||||||
|
e := (*entry[K, V])(eptr)
|
||||||
if e.key == key {
|
if e.key == key {
|
||||||
// In-place update/delete.
|
// In-place update/delete.
|
||||||
// We get a copy of the value via an interface{} on each call,
|
// We get a copy of the value via an interface{} on each call,
|
||||||
@@ -475,8 +510,8 @@ func (m *Map[K, V]) doCompute(
|
|||||||
// Deletion.
|
// Deletion.
|
||||||
// First we update the hash, then the entry.
|
// First we update the hash, then the entry.
|
||||||
newmetaw := setByte(metaw, emptyMetaSlot, idx)
|
newmetaw := setByte(metaw, emptyMetaSlot, idx)
|
||||||
b.meta.Store(newmetaw)
|
atomic.StoreUint64(&b.meta, newmetaw)
|
||||||
b.entries[idx].Store(nil)
|
atomic.StorePointer(&b.entries[idx], nil)
|
||||||
rootb.mu.Unlock()
|
rootb.mu.Unlock()
|
||||||
table.addSize(bidx, -1)
|
table.addSize(bidx, -1)
|
||||||
// Might need to shrink the table if we left bucket empty.
|
// Might need to shrink the table if we left bucket empty.
|
||||||
@@ -488,7 +523,7 @@ func (m *Map[K, V]) doCompute(
|
|||||||
newe := new(entry[K, V])
|
newe := new(entry[K, V])
|
||||||
newe.key = key
|
newe.key = key
|
||||||
newe.value = newv
|
newe.value = newv
|
||||||
b.entries[idx].Store(newe)
|
atomic.StorePointer(&b.entries[idx], unsafe.Pointer(newe))
|
||||||
case CancelOp:
|
case CancelOp:
|
||||||
newv = oldv
|
newv = oldv
|
||||||
}
|
}
|
||||||
@@ -512,7 +547,7 @@ func (m *Map[K, V]) doCompute(
|
|||||||
emptyidx = idx
|
emptyidx = idx
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if b.next.Load() == nil {
|
if b.next == nil {
|
||||||
if emptyb != nil {
|
if emptyb != nil {
|
||||||
// Insertion into an existing bucket.
|
// Insertion into an existing bucket.
|
||||||
var zeroV V
|
var zeroV V
|
||||||
@@ -526,8 +561,8 @@ func (m *Map[K, V]) doCompute(
|
|||||||
newe.key = key
|
newe.key = key
|
||||||
newe.value = newValue
|
newe.value = newValue
|
||||||
// First we update meta, then the entry.
|
// First we update meta, then the entry.
|
||||||
emptyb.meta.Store(setByte(emptyb.meta.Load(), h2, emptyidx))
|
atomic.StoreUint64(&emptyb.meta, setByte(emptyb.meta, h2, emptyidx))
|
||||||
emptyb.entries[emptyidx].Store(newe)
|
atomic.StorePointer(&emptyb.entries[emptyidx], unsafe.Pointer(newe))
|
||||||
rootb.mu.Unlock()
|
rootb.mu.Unlock()
|
||||||
table.addSize(bidx, 1)
|
table.addSize(bidx, 1)
|
||||||
return newValue, computeOnly
|
return newValue, computeOnly
|
||||||
@@ -549,19 +584,19 @@ func (m *Map[K, V]) doCompute(
|
|||||||
return newValue, false
|
return newValue, false
|
||||||
default:
|
default:
|
||||||
// Create and append a bucket.
|
// Create and append a bucket.
|
||||||
newb := new(bucketPadded[K, V])
|
newb := new(bucketPadded)
|
||||||
newb.meta.Store(setByte(defaultMeta, h2, 0))
|
newb.meta = setByte(defaultMeta, h2, 0)
|
||||||
newe := new(entry[K, V])
|
newe := new(entry[K, V])
|
||||||
newe.key = key
|
newe.key = key
|
||||||
newe.value = newValue
|
newe.value = newValue
|
||||||
newb.entries[0].Store(newe)
|
newb.entries[0] = unsafe.Pointer(newe)
|
||||||
b.next.Store(newb)
|
atomic.StorePointer(&b.next, unsafe.Pointer(newb))
|
||||||
rootb.mu.Unlock()
|
rootb.mu.Unlock()
|
||||||
table.addSize(bidx, 1)
|
table.addSize(bidx, 1)
|
||||||
return newValue, computeOnly
|
return newValue, computeOnly
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
b = b.next.Load()
|
b = (*bucketPadded)(b.next)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -570,13 +605,21 @@ func (m *Map[K, V]) newerTableExists(table *mapTable[K, V]) bool {
|
|||||||
return table != m.table.Load()
|
return table != m.table.Load()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Map[K, V]) resizeInProgress() bool {
|
func resizeSeq(ctl uint64) uint64 {
|
||||||
return m.resizing.Load()
|
return ctl >> 5
|
||||||
|
}
|
||||||
|
|
||||||
|
func resizeHelpers(ctl uint64) uint64 {
|
||||||
|
return ctl & maxResizeHelpersLimit
|
||||||
|
}
|
||||||
|
|
||||||
|
func resizeCtl(seq uint64, helpers uint64) uint64 {
|
||||||
|
return (seq << 5) | (helpers & maxResizeHelpersLimit)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Map[K, V]) waitForResize() {
|
func (m *Map[K, V]) waitForResize() {
|
||||||
m.resizeMu.Lock()
|
m.resizeMu.Lock()
|
||||||
for m.resizeInProgress() {
|
for resizeSeq(m.resizeCtl.Load())&1 == 1 {
|
||||||
m.resizeCond.Wait()
|
m.resizeCond.Wait()
|
||||||
}
|
}
|
||||||
m.resizeMu.Unlock()
|
m.resizeMu.Unlock()
|
||||||
@@ -593,9 +636,9 @@ func (m *Map[K, V]) resize(knownTable *mapTable[K, V], hint mapResizeHint) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Slow path.
|
// Slow path.
|
||||||
if !m.resizing.CompareAndSwap(false, true) {
|
seq := resizeSeq(m.resizeCtl.Load())
|
||||||
// Someone else started resize. Wait for it to finish.
|
if seq&1 == 1 || !m.resizeCtl.CompareAndSwap(resizeCtl(seq, 0), resizeCtl(seq+1, 0)) {
|
||||||
m.waitForResize()
|
m.helpResize(seq)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var newTable *mapTable[K, V]
|
var newTable *mapTable[K, V]
|
||||||
@@ -604,64 +647,189 @@ func (m *Map[K, V]) resize(knownTable *mapTable[K, V], hint mapResizeHint) {
|
|||||||
switch hint {
|
switch hint {
|
||||||
case mapGrowHint:
|
case mapGrowHint:
|
||||||
// Grow the table with factor of 2.
|
// Grow the table with factor of 2.
|
||||||
|
// We must keep the same table seed here to keep the same hash codes
|
||||||
|
// allowing us to avoid locking destination buckets when resizing.
|
||||||
m.totalGrowths.Add(1)
|
m.totalGrowths.Add(1)
|
||||||
newTable = newMapTable[K, V](tableLen << 1)
|
newTable = newMapTable[K, V](tableLen<<1, table.seed)
|
||||||
case mapShrinkHint:
|
case mapShrinkHint:
|
||||||
shrinkThreshold := int64((tableLen * entriesPerMapBucket) / mapShrinkFraction)
|
shrinkThreshold := int64((tableLen * entriesPerMapBucket) / mapShrinkFraction)
|
||||||
if tableLen > m.minTableLen && table.sumSize() <= shrinkThreshold {
|
if tableLen > m.minTableLen && table.sumSize() <= shrinkThreshold {
|
||||||
// Shrink the table with factor of 2.
|
// Shrink the table with factor of 2.
|
||||||
|
// It's fine to generate a new seed since full locking
|
||||||
|
// is required anyway.
|
||||||
m.totalShrinks.Add(1)
|
m.totalShrinks.Add(1)
|
||||||
newTable = newMapTable[K, V](tableLen >> 1)
|
newTable = newMapTable[K, V](tableLen>>1, maphash.MakeSeed())
|
||||||
} else {
|
} else {
|
||||||
// No need to shrink. Wake up all waiters and give up.
|
// No need to shrink. Wake up all waiters and give up.
|
||||||
m.resizeMu.Lock()
|
m.resizeMu.Lock()
|
||||||
m.resizing.Store(false)
|
m.resizeCtl.Store(resizeCtl(seq+2, 0))
|
||||||
m.resizeCond.Broadcast()
|
m.resizeCond.Broadcast()
|
||||||
m.resizeMu.Unlock()
|
m.resizeMu.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case mapClearHint:
|
case mapClearHint:
|
||||||
newTable = newMapTable[K, V](m.minTableLen)
|
newTable = newMapTable[K, V](m.minTableLen, maphash.MakeSeed())
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("unexpected resize hint: %d", hint))
|
panic(fmt.Sprintf("unexpected resize hint: %d", hint))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy the data only if we're not clearing the map.
|
// Copy the data only if we're not clearing the map.
|
||||||
if hint != mapClearHint {
|
if hint != mapClearHint {
|
||||||
for i := 0; i < tableLen; i++ {
|
// Set up cooperative transfer state.
|
||||||
copied := copyBucket(&table.buckets[i], newTable)
|
// Next table must be published as the last step.
|
||||||
newTable.addSizePlain(uint64(i), copied)
|
m.resizeIdx.Store(0)
|
||||||
}
|
m.nextTable.Store(newTable)
|
||||||
|
// Copy the buckets.
|
||||||
|
m.transfer(table, newTable)
|
||||||
|
}
|
||||||
|
|
||||||
|
// We're about to publish the new table, but before that
|
||||||
|
// we must wait for all helpers to finish.
|
||||||
|
for resizeHelpers(m.resizeCtl.Load()) != 0 {
|
||||||
|
runtime.Gosched()
|
||||||
}
|
}
|
||||||
// Publish the new table and wake up all waiters.
|
|
||||||
m.table.Store(newTable)
|
m.table.Store(newTable)
|
||||||
|
m.nextTable.Store(nil)
|
||||||
|
ctl := resizeCtl(seq+1, 0)
|
||||||
|
newCtl := resizeCtl(seq+2, 0)
|
||||||
|
// Increment the sequence number and wake up all waiters.
|
||||||
m.resizeMu.Lock()
|
m.resizeMu.Lock()
|
||||||
m.resizing.Store(false)
|
// There may be slowpoke helpers who have just incremented
|
||||||
|
// the helper counter. This CAS loop makes sure to wait
|
||||||
|
// for them to back off.
|
||||||
|
for !m.resizeCtl.CompareAndSwap(ctl, newCtl) {
|
||||||
|
runtime.Gosched()
|
||||||
|
}
|
||||||
m.resizeCond.Broadcast()
|
m.resizeCond.Broadcast()
|
||||||
m.resizeMu.Unlock()
|
m.resizeMu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func copyBucket[K comparable, V any](
|
func (m *Map[K, V]) helpResize(seq uint64) {
|
||||||
b *bucketPadded[K, V],
|
for {
|
||||||
|
table := m.table.Load()
|
||||||
|
nextTable := m.nextTable.Load()
|
||||||
|
if resizeSeq(m.resizeCtl.Load()) == seq {
|
||||||
|
if nextTable == nil || nextTable == table {
|
||||||
|
// Carry on until the next table is set by the main
|
||||||
|
// resize goroutine or until the resize finishes.
|
||||||
|
runtime.Gosched()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// The resize is still in-progress, so let's try registering
|
||||||
|
// as a helper.
|
||||||
|
for {
|
||||||
|
ctl := m.resizeCtl.Load()
|
||||||
|
if resizeSeq(ctl) != seq || resizeHelpers(ctl) >= uint64(maxResizeHelpers) {
|
||||||
|
// The resize has ended or there are too many helpers.
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if m.resizeCtl.CompareAndSwap(ctl, ctl+1) {
|
||||||
|
// Yay, we're a resize helper!
|
||||||
|
m.transfer(table, nextTable)
|
||||||
|
// Don't forget to unregister as a helper.
|
||||||
|
m.resizeCtl.Add(^uint64(0))
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m.waitForResize()
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Map[K, V]) transfer(table, newTable *mapTable[K, V]) {
|
||||||
|
tableLen := len(table.buckets)
|
||||||
|
newTableLen := len(newTable.buckets)
|
||||||
|
stride := (tableLen >> 3) / int(maxResizeHelpers)
|
||||||
|
if stride < minResizeTransferStride {
|
||||||
|
stride = minResizeTransferStride
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
// Claim work by incrementing resizeIdx.
|
||||||
|
nextIdx := m.resizeIdx.Add(int64(stride))
|
||||||
|
start := int(nextIdx) - stride
|
||||||
|
if start < 0 {
|
||||||
|
start = 0
|
||||||
|
}
|
||||||
|
if start > tableLen {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
end := int(nextIdx)
|
||||||
|
if end > tableLen {
|
||||||
|
end = tableLen
|
||||||
|
}
|
||||||
|
// Transfer buckets in this range.
|
||||||
|
total := 0
|
||||||
|
if newTableLen > tableLen {
|
||||||
|
// We're growing the table with 2x multiplier, so entries from a N bucket can
|
||||||
|
// only be transferred to N and 2*N buckets in the new table. Thus, destination
|
||||||
|
// buckets written by the resize helpers don't intersect, so we don't need to
|
||||||
|
// acquire locks in the destination buckets.
|
||||||
|
for i := start; i < end; i++ {
|
||||||
|
total += transferBucketUnsafe(&table.buckets[i], newTable)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// We're shrinking the table, so all locks must be acquired.
|
||||||
|
for i := start; i < end; i++ {
|
||||||
|
total += transferBucket(&table.buckets[i], newTable)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// The exact counter stripe doesn't matter here, so pick up the one
|
||||||
|
// that corresponds to the start value to avoid contention.
|
||||||
|
newTable.addSize(uint64(start), total)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Doesn't acquire dest bucket lock.
|
||||||
|
func transferBucketUnsafe[K comparable, V any](
|
||||||
|
b *bucketPadded,
|
||||||
destTable *mapTable[K, V],
|
destTable *mapTable[K, V],
|
||||||
) (copied int) {
|
) (copied int) {
|
||||||
rootb := b
|
rootb := b
|
||||||
rootb.mu.Lock()
|
rootb.mu.Lock()
|
||||||
for {
|
for {
|
||||||
for i := 0; i < entriesPerMapBucket; i++ {
|
for i := 0; i < entriesPerMapBucket; i++ {
|
||||||
if e := b.entries[i].Load(); e != nil {
|
if eptr := b.entries[i]; eptr != nil {
|
||||||
|
e := (*entry[K, V])(eptr)
|
||||||
hash := maphash.Comparable(destTable.seed, e.key)
|
hash := maphash.Comparable(destTable.seed, e.key)
|
||||||
bidx := uint64(len(destTable.buckets)-1) & h1(hash)
|
bidx := uint64(len(destTable.buckets)-1) & h1(hash)
|
||||||
destb := &destTable.buckets[bidx]
|
destb := &destTable.buckets[bidx]
|
||||||
appendToBucket(h2(hash), b.entries[i].Load(), destb)
|
appendToBucket(h2(hash), e, destb)
|
||||||
copied++
|
copied++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if next := b.next.Load(); next == nil {
|
if b.next == nil {
|
||||||
rootb.mu.Unlock()
|
rootb.mu.Unlock()
|
||||||
return
|
return
|
||||||
} else {
|
|
||||||
b = next
|
|
||||||
}
|
}
|
||||||
|
b = (*bucketPadded)(b.next)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func transferBucket[K comparable, V any](
|
||||||
|
b *bucketPadded,
|
||||||
|
destTable *mapTable[K, V],
|
||||||
|
) (copied int) {
|
||||||
|
rootb := b
|
||||||
|
rootb.mu.Lock()
|
||||||
|
for {
|
||||||
|
for i := 0; i < entriesPerMapBucket; i++ {
|
||||||
|
if eptr := b.entries[i]; eptr != nil {
|
||||||
|
e := (*entry[K, V])(eptr)
|
||||||
|
hash := maphash.Comparable(destTable.seed, e.key)
|
||||||
|
bidx := uint64(len(destTable.buckets)-1) & h1(hash)
|
||||||
|
destb := &destTable.buckets[bidx]
|
||||||
|
destb.mu.Lock()
|
||||||
|
appendToBucket(h2(hash), e, destb)
|
||||||
|
destb.mu.Unlock()
|
||||||
|
copied++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if b.next == nil {
|
||||||
|
rootb.mu.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
b = (*bucketPadded)(b.next)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -691,16 +859,15 @@ func (m *Map[K, V]) Range(f func(key K, value V) bool) {
|
|||||||
rootb.mu.Lock()
|
rootb.mu.Lock()
|
||||||
for {
|
for {
|
||||||
for i := 0; i < entriesPerMapBucket; i++ {
|
for i := 0; i < entriesPerMapBucket; i++ {
|
||||||
if entry := b.entries[i].Load(); entry != nil {
|
if b.entries[i] != nil {
|
||||||
bentries = append(bentries, entry)
|
bentries = append(bentries, (*entry[K, V])(b.entries[i]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if next := b.next.Load(); next == nil {
|
if b.next == nil {
|
||||||
rootb.mu.Unlock()
|
rootb.mu.Unlock()
|
||||||
break
|
break
|
||||||
} else {
|
|
||||||
b = next
|
|
||||||
}
|
}
|
||||||
|
b = (*bucketPadded)(b.next)
|
||||||
}
|
}
|
||||||
// Call the function for all copied entries.
|
// Call the function for all copied entries.
|
||||||
for j, e := range bentries {
|
for j, e := range bentries {
|
||||||
@@ -727,24 +894,25 @@ func (m *Map[K, V]) Size() int {
|
|||||||
return int(m.table.Load().sumSize())
|
return int(m.table.Load().sumSize())
|
||||||
}
|
}
|
||||||
|
|
||||||
func appendToBucket[K comparable, V any](h2 uint8, e *entry[K, V], b *bucketPadded[K, V]) {
|
// It is safe to use plain stores here because the destination bucket must be
|
||||||
|
// either locked or exclusively written to by the helper during resize.
|
||||||
|
func appendToBucket[K comparable, V any](h2 uint8, e *entry[K, V], b *bucketPadded) {
|
||||||
for {
|
for {
|
||||||
for i := 0; i < entriesPerMapBucket; i++ {
|
for i := 0; i < entriesPerMapBucket; i++ {
|
||||||
if b.entries[i].Load() == nil {
|
if b.entries[i] == nil {
|
||||||
b.meta.Store(setByte(b.meta.Load(), h2, i))
|
b.meta = setByte(b.meta, h2, i)
|
||||||
b.entries[i].Store(e)
|
b.entries[i] = unsafe.Pointer(e)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if next := b.next.Load(); next == nil {
|
if b.next == nil {
|
||||||
newb := new(bucketPadded[K, V])
|
newb := new(bucketPadded)
|
||||||
newb.meta.Store(setByte(defaultMeta, h2, 0))
|
newb.meta = setByte(defaultMeta, h2, 0)
|
||||||
newb.entries[0].Store(e)
|
newb.entries[0] = unsafe.Pointer(e)
|
||||||
b.next.Store(newb)
|
b.next = unsafe.Pointer(newb)
|
||||||
return
|
return
|
||||||
} else {
|
|
||||||
b = next
|
|
||||||
}
|
}
|
||||||
|
b = (*bucketPadded)(b.next)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -753,11 +921,6 @@ func (table *mapTable[K, V]) addSize(bucketIdx uint64, delta int) {
|
|||||||
atomic.AddInt64(&table.size[cidx].c, int64(delta))
|
atomic.AddInt64(&table.size[cidx].c, int64(delta))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (table *mapTable[K, V]) addSizePlain(bucketIdx uint64, delta int) {
|
|
||||||
cidx := uint64(len(table.size)-1) & bucketIdx
|
|
||||||
table.size[cidx].c += int64(delta)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (table *mapTable[K, V]) sumSize() int64 {
|
func (table *mapTable[K, V]) sumSize() int64 {
|
||||||
sum := int64(0)
|
sum := int64(0)
|
||||||
for i := range table.size {
|
for i := range table.size {
|
||||||
@@ -856,7 +1019,7 @@ func (m *Map[K, V]) Stats() MapStats {
|
|||||||
nentriesLocal := 0
|
nentriesLocal := 0
|
||||||
stats.Capacity += entriesPerMapBucket
|
stats.Capacity += entriesPerMapBucket
|
||||||
for i := 0; i < entriesPerMapBucket; i++ {
|
for i := 0; i < entriesPerMapBucket; i++ {
|
||||||
if b.entries[i].Load() != nil {
|
if atomic.LoadPointer(&b.entries[i]) != nil {
|
||||||
stats.Size++
|
stats.Size++
|
||||||
nentriesLocal++
|
nentriesLocal++
|
||||||
}
|
}
|
||||||
@@ -865,11 +1028,10 @@ func (m *Map[K, V]) Stats() MapStats {
|
|||||||
if nentriesLocal == 0 {
|
if nentriesLocal == 0 {
|
||||||
stats.EmptyBuckets++
|
stats.EmptyBuckets++
|
||||||
}
|
}
|
||||||
if next := b.next.Load(); next == nil {
|
if b.next == nil {
|
||||||
break
|
break
|
||||||
} else {
|
|
||||||
b = next
|
|
||||||
}
|
}
|
||||||
|
b = (*bucketPadded)(atomic.LoadPointer(&b.next))
|
||||||
stats.TotalBuckets++
|
stats.TotalBuckets++
|
||||||
}
|
}
|
||||||
if nentries < stats.MinEntries {
|
if nentries < stats.MinEntries {
|
||||||
@@ -906,6 +1068,15 @@ func nextPowOf2(v uint32) uint32 {
|
|||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parallelism() uint32 {
|
||||||
|
maxProcs := uint32(runtime.GOMAXPROCS(0))
|
||||||
|
numCores := uint32(runtime.NumCPU())
|
||||||
|
if maxProcs < numCores {
|
||||||
|
return maxProcs
|
||||||
|
}
|
||||||
|
return numCores
|
||||||
|
}
|
||||||
|
|
||||||
func broadcast(b uint8) uint64 {
|
func broadcast(b uint8) uint64 {
|
||||||
return 0x101010101010101 * uint64(b)
|
return 0x101010101010101 * uint64(b)
|
||||||
}
|
}
|
||||||
@@ -920,6 +1091,7 @@ func markZeroBytes(w uint64) uint64 {
|
|||||||
return ((w - 0x0101010101010101) & (^w) & 0x8080808080808080)
|
return ((w - 0x0101010101010101) & (^w) & 0x8080808080808080)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sets byte of the input word at the specified index to the given value.
|
||||||
func setByte(w uint64, b uint8, idx int) uint64 {
|
func setByte(w uint64, b uint8, idx int) uint64 {
|
||||||
shift := idx << 3
|
shift := idx << 3
|
||||||
return (w &^ (0xff << shift)) | (uint64(b) << shift)
|
return (w &^ (0xff << shift)) | (uint64(b) << shift)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package xsync
|
|||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@@ -53,11 +54,11 @@ func runParallel(b *testing.B, benchFn func(pb *testing.PB)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMap_BucketStructSize(t *testing.T) {
|
func TestMap_BucketStructSize(t *testing.T) {
|
||||||
size := unsafe.Sizeof(bucketPadded[string, int64]{})
|
size := unsafe.Sizeof(bucketPadded{})
|
||||||
if size != 64 {
|
if size != 64 {
|
||||||
t.Fatalf("size of 64B (one cache line) is expected, got: %d", size)
|
t.Fatalf("size of 64B (one cache line) is expected, got: %d", size)
|
||||||
}
|
}
|
||||||
size = unsafe.Sizeof(bucketPadded[struct{}, int32]{})
|
size = unsafe.Sizeof(bucketPadded{})
|
||||||
if size != 64 {
|
if size != 64 {
|
||||||
t.Fatalf("size of 64B (one cache line) is expected, got: %d", size)
|
t.Fatalf("size of 64B (one cache line) is expected, got: %d", size)
|
||||||
}
|
}
|
||||||
@@ -743,10 +744,7 @@ func TestNewMapGrowOnly_OnlyShrinksOnClear(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMapResize(t *testing.T) {
|
func TestMapResize(t *testing.T) {
|
||||||
testMapResize(t, NewMap[string, int]())
|
m := NewMap[string, int]()
|
||||||
}
|
|
||||||
|
|
||||||
func testMapResize(t *testing.T, m *Map[string, int]) {
|
|
||||||
const numEntries = 100_000
|
const numEntries = 100_000
|
||||||
|
|
||||||
for i := 0; i < numEntries; i++ {
|
for i := 0; i < numEntries; i++ {
|
||||||
@@ -810,6 +808,147 @@ func TestMapResize_CounterLenLimit(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testParallelResize(t *testing.T, numGoroutines int) {
|
||||||
|
m := NewMap[int, int]()
|
||||||
|
|
||||||
|
// Fill the map to trigger resizing
|
||||||
|
const initialEntries = 10000
|
||||||
|
const newEntries = 5000
|
||||||
|
for i := 0; i < initialEntries; i++ {
|
||||||
|
m.Store(i, i*2)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start concurrent operations that should trigger helping behavior
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
|
// Launch goroutines that will encounter resize operations
|
||||||
|
for g := 0; g < numGoroutines; g++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go func(goroutineID int) {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
|
// Perform many operations to trigger resize and helping
|
||||||
|
for i := 0; i < newEntries; i++ {
|
||||||
|
key := goroutineID*newEntries + i + initialEntries
|
||||||
|
m.Store(key, key*2)
|
||||||
|
|
||||||
|
// Verify the value
|
||||||
|
if val, ok := m.Load(key); !ok || val != key*2 {
|
||||||
|
t.Errorf("Failed to load key %d: got %v, %v", key, val, ok)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}(g)
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
// Verify all entries are present
|
||||||
|
finalSize := m.Size()
|
||||||
|
expectedSize := initialEntries + numGoroutines*newEntries
|
||||||
|
if finalSize != expectedSize {
|
||||||
|
t.Errorf("Expected size %d, got %d", expectedSize, finalSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
stats := m.Stats()
|
||||||
|
if stats.TotalGrowths == 0 {
|
||||||
|
t.Error("Expected at least one table growth due to concurrent operations")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMapParallelResize(t *testing.T) {
|
||||||
|
testParallelResize(t, 1)
|
||||||
|
testParallelResize(t, runtime.GOMAXPROCS(0))
|
||||||
|
testParallelResize(t, 100)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testParallelResizeWithSameKeys(t *testing.T, numGoroutines int) {
|
||||||
|
m := NewMap[int, int]()
|
||||||
|
|
||||||
|
// Fill the map to trigger resizing
|
||||||
|
const entries = 1000
|
||||||
|
for i := 0; i < entries; i++ {
|
||||||
|
m.Store(2*i, 2*i)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start concurrent operations that should trigger helping behavior
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
|
// Launch goroutines that will encounter resize operations
|
||||||
|
for g := 0; g < numGoroutines; g++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go func(goroutineID int) {
|
||||||
|
defer wg.Done()
|
||||||
|
for i := 0; i < 10*entries; i++ {
|
||||||
|
m.Store(i, i)
|
||||||
|
}
|
||||||
|
}(g)
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
// Verify all entries are present
|
||||||
|
finalSize := m.Size()
|
||||||
|
expectedSize := 10 * entries
|
||||||
|
if finalSize != expectedSize {
|
||||||
|
t.Errorf("Expected size %d, got %d", expectedSize, finalSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
stats := m.Stats()
|
||||||
|
if stats.TotalGrowths == 0 {
|
||||||
|
t.Error("Expected at least one table growth due to concurrent operations")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMapParallelResize_IntersectingKeys(t *testing.T) {
|
||||||
|
testParallelResizeWithSameKeys(t, 1)
|
||||||
|
testParallelResizeWithSameKeys(t, runtime.GOMAXPROCS(0))
|
||||||
|
testParallelResizeWithSameKeys(t, 100)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testParallelShrinking(t *testing.T, numGoroutines int) {
|
||||||
|
m := NewMap[int, int]()
|
||||||
|
|
||||||
|
// Fill the map to trigger resizing
|
||||||
|
const entries = 100000
|
||||||
|
for i := 0; i < entries; i++ {
|
||||||
|
m.Store(i, i)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start concurrent operations that should trigger helping behavior
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
|
// Launch goroutines that will encounter resize operations
|
||||||
|
for g := 0; g < numGoroutines; g++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go func(goroutineID int) {
|
||||||
|
defer wg.Done()
|
||||||
|
for i := 0; i < entries; i++ {
|
||||||
|
m.Delete(i)
|
||||||
|
}
|
||||||
|
}(g)
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
// Verify all entries are present
|
||||||
|
finalSize := m.Size()
|
||||||
|
if finalSize != 0 {
|
||||||
|
t.Errorf("Expected size 0, got %d", finalSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
stats := m.Stats()
|
||||||
|
if stats.TotalShrinks == 0 {
|
||||||
|
t.Error("Expected at least one table shrinking due to concurrent operations")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMapParallelShrinking(t *testing.T) {
|
||||||
|
testParallelShrinking(t, 1)
|
||||||
|
testParallelShrinking(t, runtime.GOMAXPROCS(0))
|
||||||
|
testParallelShrinking(t, 100)
|
||||||
|
}
|
||||||
|
|
||||||
func parallelSeqMapGrower(m *Map[int, int], numEntries int, positive bool, cdone chan bool) {
|
func parallelSeqMapGrower(m *Map[int, int], numEntries int, positive bool, cdone chan bool) {
|
||||||
for i := 0; i < numEntries; i++ {
|
for i := 0; i < numEntries; i++ {
|
||||||
if positive {
|
if positive {
|
||||||
@@ -1459,7 +1598,7 @@ func BenchmarkMapRange(b *testing.B) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Benchmarks noop performance of Compute
|
// Benchmarks noop performance of Compute
|
||||||
func BenchmarkCompute(b *testing.B) {
|
func BenchmarkMapCompute(b *testing.B) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
Name string
|
Name string
|
||||||
Op ComputeOp
|
Op ComputeOp
|
||||||
@@ -1487,6 +1626,57 @@ func BenchmarkCompute(b *testing.B) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkMapParallelRehashing(b *testing.B) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
goroutines int
|
||||||
|
numEntries int
|
||||||
|
}{
|
||||||
|
{"1goroutine_10M", 1, 10_000_000},
|
||||||
|
{"4goroutines_10M", 4, 10_000_000},
|
||||||
|
{"8goroutines_10M", 8, 10_000_000},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
b.Run(test.name, func(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
m := NewMap[int, int]()
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
entriesPerGoroutine := test.numEntries / test.goroutines
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
|
||||||
|
for g := 0; g < test.goroutines; g++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go func(goroutineID int) {
|
||||||
|
defer wg.Done()
|
||||||
|
base := goroutineID * entriesPerGoroutine
|
||||||
|
for j := 0; j < entriesPerGoroutine; j++ {
|
||||||
|
key := base + j
|
||||||
|
m.Store(key, key)
|
||||||
|
}
|
||||||
|
}(g)
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
duration := time.Since(start)
|
||||||
|
|
||||||
|
b.ReportMetric(float64(test.numEntries)/duration.Seconds(), "entries/s")
|
||||||
|
|
||||||
|
finalSize := m.Size()
|
||||||
|
if finalSize != test.numEntries {
|
||||||
|
b.Fatalf("Expected size %d, got %d", test.numEntries, finalSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
stats := m.Stats()
|
||||||
|
if stats.TotalGrowths == 0 {
|
||||||
|
b.Error("Expected at least one table growth during rehashing")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestNextPowOf2(t *testing.T) {
|
func TestNextPowOf2(t *testing.T) {
|
||||||
if nextPowOf2(0) != 1 {
|
if nextPowOf2(0) != 1 {
|
||||||
t.Error("nextPowOf2 failed")
|
t.Error("nextPowOf2 failed")
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
package tls
|
package ca
|
||||||
|
|
||||||
import (
|
import (
|
||||||
utls "github.com/metacubex/utls"
|
"github.com/metacubex/tls"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ClientAuthType = utls.ClientAuthType
|
type ClientAuthType = tls.ClientAuthType
|
||||||
|
|
||||||
const (
|
const (
|
||||||
NoClientCert = utls.NoClientCert
|
NoClientCert = tls.NoClientCert
|
||||||
RequestClientCert = utls.RequestClientCert
|
RequestClientCert = tls.RequestClientCert
|
||||||
RequireAnyClientCert = utls.RequireAnyClientCert
|
RequireAnyClientCert = tls.RequireAnyClientCert
|
||||||
VerifyClientCertIfGiven = utls.VerifyClientCertIfGiven
|
VerifyClientCertIfGiven = tls.VerifyClientCertIfGiven
|
||||||
RequireAndVerifyClientCert = utls.RequireAndVerifyClientCert
|
RequireAndVerifyClientCert = tls.RequireAndVerifyClientCert
|
||||||
)
|
)
|
||||||
|
|
||||||
func ClientAuthTypeFromString(s string) ClientAuthType {
|
func ClientAuthTypeFromString(s string) ClientAuthType {
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package ca
|
package ca
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"errors"
|
"errors"
|
||||||
@@ -11,8 +10,9 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/once"
|
"github.com/metacubex/mihomo/common/once"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
|
||||||
"github.com/metacubex/mihomo/ntp"
|
"github.com/metacubex/mihomo/ntp"
|
||||||
|
|
||||||
|
"github.com/metacubex/tls"
|
||||||
)
|
)
|
||||||
|
|
||||||
var globalCertPool *x509.CertPool
|
var globalCertPool *x509.CertPool
|
||||||
@@ -106,12 +106,13 @@ func GetTLSConfig(opt Option) (tlsConfig *tls.Config, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(opt.Certificate) > 0 || len(opt.PrivateKey) > 0 {
|
if len(opt.Certificate) > 0 || len(opt.PrivateKey) > 0 {
|
||||||
var cert tls.Certificate
|
certLoader, err := NewTLSKeyPairLoader(opt.Certificate, opt.PrivateKey)
|
||||||
cert, err = LoadTLSKeyPair(opt.Certificate, opt.PrivateKey, C.Path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
tlsConfig.Certificates = []tls.Certificate{cert}
|
tlsConfig.GetClientCertificate = func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
|
||||||
|
return certLoader()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return tlsConfig, nil
|
return tlsConfig, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,71 +7,85 @@ import (
|
|||||||
"crypto/elliptic"
|
"crypto/elliptic"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"crypto/tls"
|
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
C "github.com/metacubex/mihomo/constant"
|
||||||
|
|
||||||
|
"github.com/metacubex/fswatch"
|
||||||
|
"github.com/metacubex/tls"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Path interface {
|
// NewTLSKeyPairLoader creates a loader function for TLS key pairs from the provided certificate and private key data or file paths.
|
||||||
Resolve(path string) string
|
|
||||||
IsSafePath(path string) bool
|
|
||||||
ErrNotSafePath(path string) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// LoadTLSKeyPair loads a TLS key pair from the provided certificate and private key data or file paths, supporting fallback resolution.
|
|
||||||
// Returns a tls.Certificate and an error, where the error indicates issues during parsing or file loading.
|
|
||||||
// If both certificate and privateKey are empty, generates a random TLS RSA key pair.
|
// If both certificate and privateKey are empty, generates a random TLS RSA key pair.
|
||||||
// Accepts a Path interface for resolving file paths when necessary.
|
func NewTLSKeyPairLoader(certificate, privateKey string) (func() (*tls.Certificate, error), error) {
|
||||||
func LoadTLSKeyPair(certificate, privateKey string, path Path) (tls.Certificate, error) {
|
|
||||||
if certificate == "" && privateKey == "" {
|
if certificate == "" && privateKey == "" {
|
||||||
var err error
|
var err error
|
||||||
certificate, privateKey, _, err = NewRandomTLSKeyPair(KeyPairTypeRSA)
|
certificate, privateKey, _, err = NewRandomTLSKeyPair(KeyPairTypeRSA)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return tls.Certificate{}, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cert, painTextErr := tls.X509KeyPair([]byte(certificate), []byte(privateKey))
|
cert, painTextErr := tls.X509KeyPair([]byte(certificate), []byte(privateKey))
|
||||||
if painTextErr == nil {
|
if painTextErr == nil {
|
||||||
return cert, nil
|
return func() (*tls.Certificate, error) {
|
||||||
}
|
return &cert, nil
|
||||||
if path == nil {
|
}, nil
|
||||||
return tls.Certificate{}, painTextErr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
certificate = path.Resolve(certificate)
|
certificate = C.Path.Resolve(certificate)
|
||||||
privateKey = path.Resolve(privateKey)
|
privateKey = C.Path.Resolve(privateKey)
|
||||||
var loadErr error
|
var loadErr error
|
||||||
if !path.IsSafePath(certificate) {
|
if !C.Path.IsSafePath(certificate) {
|
||||||
loadErr = path.ErrNotSafePath(certificate)
|
loadErr = C.Path.ErrNotSafePath(certificate)
|
||||||
} else if !path.IsSafePath(privateKey) {
|
} else if !C.Path.IsSafePath(privateKey) {
|
||||||
loadErr = path.ErrNotSafePath(privateKey)
|
loadErr = C.Path.ErrNotSafePath(privateKey)
|
||||||
} else {
|
} else {
|
||||||
cert, loadErr = tls.LoadX509KeyPair(certificate, privateKey)
|
cert, loadErr = tls.LoadX509KeyPair(certificate, privateKey)
|
||||||
}
|
}
|
||||||
if loadErr != nil {
|
if loadErr != nil {
|
||||||
return tls.Certificate{}, fmt.Errorf("parse certificate failed, maybe format error:%s, or path error: %s", painTextErr.Error(), loadErr.Error())
|
return nil, fmt.Errorf("parse certificate failed, maybe format error:%s, or path error: %s", painTextErr.Error(), loadErr.Error())
|
||||||
}
|
}
|
||||||
return cert, nil
|
gcFlag := new(os.File) // tiny (on the order of 16 bytes or less) and pointer-free objects may never run the finalizer, so we choose new an os.File
|
||||||
|
updateMutex := sync.RWMutex{}
|
||||||
|
if watcher, err := fswatch.NewWatcher(fswatch.Options{Path: []string{certificate, privateKey}, Callback: func(path string) {
|
||||||
|
updateMutex.Lock()
|
||||||
|
defer updateMutex.Unlock()
|
||||||
|
if newCert, err := tls.LoadX509KeyPair(certificate, privateKey); err == nil {
|
||||||
|
cert = newCert
|
||||||
|
}
|
||||||
|
}}); err == nil {
|
||||||
|
if err = watcher.Start(); err == nil {
|
||||||
|
runtime.SetFinalizer(gcFlag, func(f *os.File) {
|
||||||
|
_ = watcher.Close()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return func() (*tls.Certificate, error) {
|
||||||
|
defer runtime.KeepAlive(gcFlag)
|
||||||
|
updateMutex.RLock()
|
||||||
|
defer updateMutex.RUnlock()
|
||||||
|
return &cert, nil
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadCertificates(certificate string, path Path) (*x509.CertPool, error) {
|
func LoadCertificates(certificate string) (*x509.CertPool, error) {
|
||||||
pool := x509.NewCertPool()
|
pool := x509.NewCertPool()
|
||||||
if pool.AppendCertsFromPEM([]byte(certificate)) {
|
if pool.AppendCertsFromPEM([]byte(certificate)) {
|
||||||
return pool, nil
|
return pool, nil
|
||||||
}
|
}
|
||||||
painTextErr := fmt.Errorf("invalid certificate: %s", certificate)
|
painTextErr := fmt.Errorf("invalid certificate: %s", certificate)
|
||||||
if path == nil {
|
|
||||||
return nil, painTextErr
|
|
||||||
}
|
|
||||||
|
|
||||||
certificate = path.Resolve(certificate)
|
certificate = C.Path.Resolve(certificate)
|
||||||
var loadErr error
|
var loadErr error
|
||||||
if !path.IsSafePath(certificate) {
|
if !C.Path.IsSafePath(certificate) {
|
||||||
loadErr = path.ErrNotSafePath(certificate)
|
loadErr = C.Path.ErrNotSafePath(certificate)
|
||||||
} else {
|
} else {
|
||||||
certPEMBlock, err := os.ReadFile(certificate)
|
certPEMBlock, err := os.ReadFile(certificate)
|
||||||
if pool.AppendCertsFromPEM(certPEMBlock) {
|
if pool.AppendCertsFromPEM(certPEMBlock) {
|
||||||
@@ -82,6 +96,9 @@ func LoadCertificates(certificate string, path Path) (*x509.CertPool, error) {
|
|||||||
if loadErr != nil {
|
if loadErr != nil {
|
||||||
return nil, fmt.Errorf("parse certificate failed, maybe format error:%s, or path error: %s", painTextErr.Error(), loadErr.Error())
|
return nil, fmt.Errorf("parse certificate failed, maybe format error:%s, or path error: %s", painTextErr.Error(), loadErr.Error())
|
||||||
}
|
}
|
||||||
|
//TODO: support dynamic update pool too
|
||||||
|
// blocked by: https://github.com/golang/go/issues/64796
|
||||||
|
// maybe we can direct add `GetRootCAs` and `GetClientCAs` to ourselves tls fork
|
||||||
return pool, nil
|
return pool, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/component/keepalive"
|
"github.com/metacubex/mihomo/component/keepalive"
|
||||||
|
"github.com/metacubex/mihomo/component/mptcp"
|
||||||
"github.com/metacubex/mihomo/component/resolver"
|
"github.com/metacubex/mihomo/component/resolver"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -140,9 +141,7 @@ func dialContext(ctx context.Context, network string, destination netip.Addr, po
|
|||||||
|
|
||||||
dialer := netDialer.(*net.Dialer)
|
dialer := netDialer.(*net.Dialer)
|
||||||
keepalive.SetNetDialer(dialer)
|
keepalive.SetNetDialer(dialer)
|
||||||
if opt.mpTcp {
|
mptcp.SetNetDialer(dialer, opt.mpTcp)
|
||||||
setMultiPathTCP(dialer)
|
|
||||||
}
|
|
||||||
|
|
||||||
if DefaultSocketHook != nil { // ignore interfaceName, routingMark and tfo when DefaultSocketHook not null (in CMFA)
|
if DefaultSocketHook != nil { // ignore interfaceName, routingMark and tfo when DefaultSocketHook not null (in CMFA)
|
||||||
socketHookToToDialer(dialer)
|
socketHookToToDialer(dialer)
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
//go:build !go1.21
|
|
||||||
|
|
||||||
package dialer
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net"
|
|
||||||
)
|
|
||||||
|
|
||||||
const multipathTCPAvailable = false
|
|
||||||
|
|
||||||
func setMultiPathTCP(dialer *net.Dialer) {
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
//go:build go1.21
|
|
||||||
|
|
||||||
package dialer
|
|
||||||
|
|
||||||
import "net"
|
|
||||||
|
|
||||||
const multipathTCPAvailable = true
|
|
||||||
|
|
||||||
func setMultiPathTCP(dialer *net.Dialer) {
|
|
||||||
dialer.SetMultipathTCP(true)
|
|
||||||
}
|
|
||||||
@@ -5,13 +5,33 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
tlsC "github.com/metacubex/mihomo/component/tls"
|
tlsC "github.com/metacubex/mihomo/component/tls"
|
||||||
|
"github.com/metacubex/tls"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
GetEncryptedClientHelloConfigList func(ctx context.Context, serverName string) ([]byte, error)
|
GetEncryptedClientHelloConfigList func(ctx context.Context, serverName string) ([]byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cfg *Config) ClientHandle(ctx context.Context, tlsConfig *tlsC.Config) (err error) {
|
func (cfg *Config) ClientHandle(ctx context.Context, tlsConfig *tls.Config) (err error) {
|
||||||
|
if cfg == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
echConfigList, err := cfg.GetEncryptedClientHelloConfigList(ctx, tlsConfig.ServerName)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("resolve ECH config error: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tlsConfig.EncryptedClientHelloConfigList = echConfigList
|
||||||
|
if tlsConfig.MinVersion != 0 && tlsConfig.MinVersion < tls.VersionTLS13 {
|
||||||
|
tlsConfig.MinVersion = tls.VersionTLS13
|
||||||
|
}
|
||||||
|
if tlsConfig.MaxVersion != 0 && tlsConfig.MaxVersion < tls.VersionTLS13 {
|
||||||
|
tlsConfig.MaxVersion = tls.VersionTLS13
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cfg *Config) ClientHandleUTLS(ctx context.Context, tlsConfig *tlsC.Config) (err error) {
|
||||||
if cfg == nil {
|
if cfg == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
147
component/ech/echparser/echparser.go
Normal file
147
component/ech/echparser/echparser.go
Normal file
@@ -0,0 +1,147 @@
|
|||||||
|
package echparser
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/cryptobyte"
|
||||||
|
)
|
||||||
|
|
||||||
|
// export from std's crypto/tls/ech.go
|
||||||
|
|
||||||
|
const extensionEncryptedClientHello = 0xfe0d
|
||||||
|
|
||||||
|
type ECHCipher struct {
|
||||||
|
KDFID uint16
|
||||||
|
AEADID uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
type ECHExtension struct {
|
||||||
|
Type uint16
|
||||||
|
Data []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
type ECHConfig struct {
|
||||||
|
raw []byte
|
||||||
|
|
||||||
|
Version uint16
|
||||||
|
Length uint16
|
||||||
|
|
||||||
|
ConfigID uint8
|
||||||
|
KemID uint16
|
||||||
|
PublicKey []byte
|
||||||
|
SymmetricCipherSuite []ECHCipher
|
||||||
|
|
||||||
|
MaxNameLength uint8
|
||||||
|
PublicName []byte
|
||||||
|
Extensions []ECHExtension
|
||||||
|
}
|
||||||
|
|
||||||
|
var ErrMalformedECHConfigList = errors.New("tls: malformed ECHConfigList")
|
||||||
|
|
||||||
|
type EchConfigErr struct {
|
||||||
|
field string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *EchConfigErr) Error() string {
|
||||||
|
if e.field == "" {
|
||||||
|
return "tls: malformed ECHConfig"
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("tls: malformed ECHConfig, invalid %s field", e.field)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseECHConfig(enc []byte) (skip bool, ec ECHConfig, err error) {
|
||||||
|
s := cryptobyte.String(enc)
|
||||||
|
ec.raw = []byte(enc)
|
||||||
|
if !s.ReadUint16(&ec.Version) {
|
||||||
|
return false, ECHConfig{}, &EchConfigErr{"version"}
|
||||||
|
}
|
||||||
|
if !s.ReadUint16(&ec.Length) {
|
||||||
|
return false, ECHConfig{}, &EchConfigErr{"length"}
|
||||||
|
}
|
||||||
|
if len(ec.raw) < int(ec.Length)+4 {
|
||||||
|
return false, ECHConfig{}, &EchConfigErr{"length"}
|
||||||
|
}
|
||||||
|
ec.raw = ec.raw[:ec.Length+4]
|
||||||
|
if ec.Version != extensionEncryptedClientHello {
|
||||||
|
s.Skip(int(ec.Length))
|
||||||
|
return true, ECHConfig{}, nil
|
||||||
|
}
|
||||||
|
if !s.ReadUint8(&ec.ConfigID) {
|
||||||
|
return false, ECHConfig{}, &EchConfigErr{"config_id"}
|
||||||
|
}
|
||||||
|
if !s.ReadUint16(&ec.KemID) {
|
||||||
|
return false, ECHConfig{}, &EchConfigErr{"kem_id"}
|
||||||
|
}
|
||||||
|
if !s.ReadUint16LengthPrefixed((*cryptobyte.String)(&ec.PublicKey)) {
|
||||||
|
return false, ECHConfig{}, &EchConfigErr{"public_key"}
|
||||||
|
}
|
||||||
|
var cipherSuites cryptobyte.String
|
||||||
|
if !s.ReadUint16LengthPrefixed(&cipherSuites) {
|
||||||
|
return false, ECHConfig{}, &EchConfigErr{"cipher_suites"}
|
||||||
|
}
|
||||||
|
for !cipherSuites.Empty() {
|
||||||
|
var c ECHCipher
|
||||||
|
if !cipherSuites.ReadUint16(&c.KDFID) {
|
||||||
|
return false, ECHConfig{}, &EchConfigErr{"cipher_suites kdf_id"}
|
||||||
|
}
|
||||||
|
if !cipherSuites.ReadUint16(&c.AEADID) {
|
||||||
|
return false, ECHConfig{}, &EchConfigErr{"cipher_suites aead_id"}
|
||||||
|
}
|
||||||
|
ec.SymmetricCipherSuite = append(ec.SymmetricCipherSuite, c)
|
||||||
|
}
|
||||||
|
if !s.ReadUint8(&ec.MaxNameLength) {
|
||||||
|
return false, ECHConfig{}, &EchConfigErr{"maximum_name_length"}
|
||||||
|
}
|
||||||
|
var publicName cryptobyte.String
|
||||||
|
if !s.ReadUint8LengthPrefixed(&publicName) {
|
||||||
|
return false, ECHConfig{}, &EchConfigErr{"public_name"}
|
||||||
|
}
|
||||||
|
ec.PublicName = publicName
|
||||||
|
var extensions cryptobyte.String
|
||||||
|
if !s.ReadUint16LengthPrefixed(&extensions) {
|
||||||
|
return false, ECHConfig{}, &EchConfigErr{"extensions"}
|
||||||
|
}
|
||||||
|
for !extensions.Empty() {
|
||||||
|
var e ECHExtension
|
||||||
|
if !extensions.ReadUint16(&e.Type) {
|
||||||
|
return false, ECHConfig{}, &EchConfigErr{"extensions type"}
|
||||||
|
}
|
||||||
|
if !extensions.ReadUint16LengthPrefixed((*cryptobyte.String)(&e.Data)) {
|
||||||
|
return false, ECHConfig{}, &EchConfigErr{"extensions data"}
|
||||||
|
}
|
||||||
|
ec.Extensions = append(ec.Extensions, e)
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, ec, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseECHConfigList parses a draft-ietf-tls-esni-18 ECHConfigList, returning a
|
||||||
|
// slice of parsed ECHConfigs, in the same order they were parsed, or an error
|
||||||
|
// if the list is malformed.
|
||||||
|
func ParseECHConfigList(data []byte) ([]ECHConfig, error) {
|
||||||
|
s := cryptobyte.String(data)
|
||||||
|
var length uint16
|
||||||
|
if !s.ReadUint16(&length) {
|
||||||
|
return nil, ErrMalformedECHConfigList
|
||||||
|
}
|
||||||
|
if length != uint16(len(data)-2) {
|
||||||
|
return nil, ErrMalformedECHConfigList
|
||||||
|
}
|
||||||
|
var configs []ECHConfig
|
||||||
|
for len(s) > 0 {
|
||||||
|
if len(s) < 4 {
|
||||||
|
return nil, errors.New("tls: malformed ECHConfig")
|
||||||
|
}
|
||||||
|
configLen := uint16(s[2])<<8 | uint16(s[3])
|
||||||
|
skip, ec, err := ParseECHConfig(s)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
s = s[configLen+4:]
|
||||||
|
if !skip {
|
||||||
|
configs = append(configs, ec)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return configs, nil
|
||||||
|
}
|
||||||
@@ -8,10 +8,13 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/component/ca"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
tlsC "github.com/metacubex/mihomo/component/tls"
|
|
||||||
|
|
||||||
|
"github.com/metacubex/fswatch"
|
||||||
|
"github.com/metacubex/tls"
|
||||||
"golang.org/x/crypto/cryptobyte"
|
"golang.org/x/crypto/cryptobyte"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -85,11 +88,11 @@ func GenECHConfig(publicName string) (configBase64 string, keyPem string, err er
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func UnmarshalECHKeys(raw []byte) ([]tlsC.EncryptedClientHelloKey, error) {
|
func UnmarshalECHKeys(raw []byte) ([]tls.EncryptedClientHelloKey, error) {
|
||||||
var keys []tlsC.EncryptedClientHelloKey
|
var keys []tls.EncryptedClientHelloKey
|
||||||
rawString := cryptobyte.String(raw)
|
rawString := cryptobyte.String(raw)
|
||||||
for !rawString.Empty() {
|
for !rawString.Empty() {
|
||||||
var key tlsC.EncryptedClientHelloKey
|
var key tls.EncryptedClientHelloKey
|
||||||
if !rawString.ReadUint16LengthPrefixed((*cryptobyte.String)(&key.PrivateKey)) {
|
if !rawString.ReadUint16LengthPrefixed((*cryptobyte.String)(&key.PrivateKey)) {
|
||||||
return nil, errors.New("error parsing private key")
|
return nil, errors.New("error parsing private key")
|
||||||
}
|
}
|
||||||
@@ -104,40 +107,65 @@ func UnmarshalECHKeys(raw []byte) ([]tlsC.EncryptedClientHelloKey, error) {
|
|||||||
return keys, nil
|
return keys, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadECHKey(key string, tlsConfig *tlsC.Config, path ca.Path) error {
|
func LoadECHKey(key string, tlsConfig *tls.Config) error {
|
||||||
if key == "" {
|
if key == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
painTextErr := loadECHKey([]byte(key), tlsConfig)
|
echKeys, painTextErr := loadECHKey([]byte(key))
|
||||||
if painTextErr == nil {
|
if painTextErr == nil {
|
||||||
|
tlsConfig.GetEncryptedClientHelloKeys = func(info *tls.ClientHelloInfo) ([]tls.EncryptedClientHelloKey, error) {
|
||||||
|
return echKeys, nil
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
key = path.Resolve(key)
|
key = C.Path.Resolve(key)
|
||||||
var loadErr error
|
var loadErr error
|
||||||
if !path.IsSafePath(key) {
|
if !C.Path.IsSafePath(key) {
|
||||||
loadErr = path.ErrNotSafePath(key)
|
loadErr = C.Path.ErrNotSafePath(key)
|
||||||
} else {
|
} else {
|
||||||
var echKey []byte
|
var echKey []byte
|
||||||
echKey, loadErr = os.ReadFile(key)
|
echKey, loadErr = os.ReadFile(key)
|
||||||
if loadErr == nil {
|
if loadErr == nil {
|
||||||
loadErr = loadECHKey(echKey, tlsConfig)
|
echKeys, loadErr = loadECHKey(echKey)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if loadErr != nil {
|
if loadErr != nil {
|
||||||
return fmt.Errorf("parse ECH keys failed, maybe format error:%s, or path error: %s", painTextErr.Error(), loadErr.Error())
|
return fmt.Errorf("parse ECH keys failed, maybe format error:%s, or path error: %s", painTextErr.Error(), loadErr.Error())
|
||||||
}
|
}
|
||||||
|
gcFlag := new(os.File) // tiny (on the order of 16 bytes or less) and pointer-free objects may never run the finalizer, so we choose new an os.File
|
||||||
|
updateMutex := sync.RWMutex{}
|
||||||
|
if watcher, err := fswatch.NewWatcher(fswatch.Options{Path: []string{key}, Callback: func(path string) {
|
||||||
|
updateMutex.Lock()
|
||||||
|
defer updateMutex.Unlock()
|
||||||
|
if echKey, err := os.ReadFile(key); err == nil {
|
||||||
|
if newEchKeys, err := loadECHKey(echKey); err == nil {
|
||||||
|
echKeys = newEchKeys
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}}); err == nil {
|
||||||
|
if err = watcher.Start(); err == nil {
|
||||||
|
runtime.SetFinalizer(gcFlag, func(f *os.File) {
|
||||||
|
_ = watcher.Close()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tlsConfig.GetEncryptedClientHelloKeys = func(info *tls.ClientHelloInfo) ([]tls.EncryptedClientHelloKey, error) {
|
||||||
|
defer runtime.KeepAlive(gcFlag)
|
||||||
|
updateMutex.RLock()
|
||||||
|
defer updateMutex.RUnlock()
|
||||||
|
return echKeys, nil
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadECHKey(echKey []byte, tlsConfig *tlsC.Config) error {
|
func loadECHKey(echKey []byte) ([]tls.EncryptedClientHelloKey, error) {
|
||||||
block, rest := pem.Decode(echKey)
|
block, rest := pem.Decode(echKey)
|
||||||
if block == nil || block.Type != "ECH KEYS" || len(rest) > 0 {
|
if block == nil || block.Type != "ECH KEYS" || len(rest) > 0 {
|
||||||
return errors.New("invalid ECH keys pem")
|
return nil, errors.New("invalid ECH keys pem")
|
||||||
}
|
}
|
||||||
echKeys, err := UnmarshalECHKeys(block.Bytes)
|
echKeys, err := UnmarshalECHKeys(block.Bytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("parse ECH keys: %w", err)
|
return nil, fmt.Errorf("parse ECH keys: %w", err)
|
||||||
}
|
}
|
||||||
tlsConfig.EncryptedClientHelloKeys = echKeys
|
return echKeys, err
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
30
component/ech/key_test.go
Normal file
30
component/ech/key_test.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package ech
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/metacubex/mihomo/component/ech/echparser"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGenECHConfig(t *testing.T) {
|
||||||
|
domain := "www.example.com"
|
||||||
|
configBase64, _, err := GenECHConfig(domain)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
echConfigList, err := base64.StdEncoding.DecodeString(configBase64)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
echConfigs, err := echparser.ParseECHConfigList(echConfigList)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if len(echConfigs) == 0 {
|
||||||
|
t.Error("no ech config")
|
||||||
|
}
|
||||||
|
if publicName := string(echConfigs[0].PublicName); publicName != domain {
|
||||||
|
t.Error("ech config domain error, expect ", domain, " got", publicName)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -50,6 +50,10 @@ func (c *cachefileStore) FlushFakeIP() error {
|
|||||||
return c.cache.FlushFakeIP()
|
return c.cache.FlushFakeIP()
|
||||||
}
|
}
|
||||||
|
|
||||||
func newCachefileStore(cache *cachefile.CacheFile) *cachefileStore {
|
func newCachefileStore(cache *cachefile.CacheFile, prefix netip.Prefix) *cachefileStore {
|
||||||
return &cachefileStore{cache.FakeIpStore()}
|
if prefix.Addr().Is6() {
|
||||||
|
return &cachefileStore{cache.FakeIpStore6()}
|
||||||
|
} else {
|
||||||
|
return &cachefileStore{cache.FakeIpStore()}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/component/profile/cachefile"
|
"github.com/metacubex/mihomo/component/profile/cachefile"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
|
||||||
|
|
||||||
"go4.org/netipx"
|
"go4.org/netipx"
|
||||||
)
|
)
|
||||||
@@ -36,8 +35,6 @@ type Pool struct {
|
|||||||
offset netip.Addr
|
offset netip.Addr
|
||||||
cycle bool
|
cycle bool
|
||||||
mux sync.Mutex
|
mux sync.Mutex
|
||||||
host []C.DomainMatcher
|
|
||||||
mode C.FilterMode
|
|
||||||
ipnet netip.Prefix
|
ipnet netip.Prefix
|
||||||
store store
|
store store
|
||||||
}
|
}
|
||||||
@@ -66,24 +63,6 @@ func (p *Pool) LookBack(ip netip.Addr) (string, bool) {
|
|||||||
return p.store.GetByIP(ip)
|
return p.store.GetByIP(ip)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ShouldSkipped return if domain should be skipped
|
|
||||||
func (p *Pool) ShouldSkipped(domain string) bool {
|
|
||||||
should := p.shouldSkipped(domain)
|
|
||||||
if p.mode == C.FilterWhiteList {
|
|
||||||
return !should
|
|
||||||
}
|
|
||||||
return should
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Pool) shouldSkipped(domain string) bool {
|
|
||||||
for _, matcher := range p.host {
|
|
||||||
if matcher.MatchDomain(domain) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Exist returns if given ip exists in fake-ip pool
|
// Exist returns if given ip exists in fake-ip pool
|
||||||
func (p *Pool) Exist(ip netip.Addr) bool {
|
func (p *Pool) Exist(ip netip.Addr) bool {
|
||||||
p.mux.Lock()
|
p.mux.Lock()
|
||||||
@@ -166,8 +145,6 @@ func (p *Pool) restoreState() {
|
|||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
IPNet netip.Prefix
|
IPNet netip.Prefix
|
||||||
Host []C.DomainMatcher
|
|
||||||
Mode C.FilterMode
|
|
||||||
|
|
||||||
// Size sets the maximum number of entries in memory
|
// Size sets the maximum number of entries in memory
|
||||||
// and does not work if Persistence is true
|
// and does not work if Persistence is true
|
||||||
@@ -197,12 +174,10 @@ func New(options Options) (*Pool, error) {
|
|||||||
last: last,
|
last: last,
|
||||||
offset: first.Prev(),
|
offset: first.Prev(),
|
||||||
cycle: false,
|
cycle: false,
|
||||||
host: options.Host,
|
|
||||||
mode: options.Mode,
|
|
||||||
ipnet: options.IPNet,
|
ipnet: options.IPNet,
|
||||||
}
|
}
|
||||||
if options.Persistence {
|
if options.Persistence {
|
||||||
pool.store = newCachefileStore(cachefile.Cache())
|
pool.store = newCachefileStore(cachefile.Cache(), options.IPNet)
|
||||||
} else {
|
} else {
|
||||||
pool.store = newMemoryStore(options.Size)
|
pool.store = newMemoryStore(options.Size)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/component/profile/cachefile"
|
"github.com/metacubex/mihomo/component/profile/cachefile"
|
||||||
"github.com/metacubex/mihomo/component/trie"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
|
||||||
|
|
||||||
"github.com/metacubex/bbolt"
|
"github.com/metacubex/bbolt"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@@ -43,7 +41,7 @@ func createCachefileStore(options Options) (*Pool, string, error) {
|
|||||||
return nil, "", err
|
return nil, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
pool.store = newCachefileStore(&cachefile.CacheFile{DB: db})
|
pool.store = newCachefileStore(&cachefile.CacheFile{DB: db}, options.IPNet)
|
||||||
return pool, f.Name(), nil
|
return pool, f.Name(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,47 +144,6 @@ func TestPool_CycleUsed(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPool_Skip(t *testing.T) {
|
|
||||||
ipnet := netip.MustParsePrefix("192.168.0.1/29")
|
|
||||||
tree := trie.New[struct{}]()
|
|
||||||
assert.NoError(t, tree.Insert("example.com", struct{}{}))
|
|
||||||
assert.False(t, tree.IsEmpty())
|
|
||||||
pools, tempfile, err := createPools(Options{
|
|
||||||
IPNet: ipnet,
|
|
||||||
Size: 10,
|
|
||||||
Host: []C.DomainMatcher{tree.NewDomainSet()},
|
|
||||||
})
|
|
||||||
assert.Nil(t, err)
|
|
||||||
defer os.Remove(tempfile)
|
|
||||||
|
|
||||||
for _, pool := range pools {
|
|
||||||
assert.True(t, pool.ShouldSkipped("example.com"))
|
|
||||||
assert.False(t, pool.ShouldSkipped("foo.com"))
|
|
||||||
assert.False(t, pool.shouldSkipped("baz.com"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPool_SkipWhiteList(t *testing.T) {
|
|
||||||
ipnet := netip.MustParsePrefix("192.168.0.1/29")
|
|
||||||
tree := trie.New[struct{}]()
|
|
||||||
assert.NoError(t, tree.Insert("example.com", struct{}{}))
|
|
||||||
assert.False(t, tree.IsEmpty())
|
|
||||||
pools, tempfile, err := createPools(Options{
|
|
||||||
IPNet: ipnet,
|
|
||||||
Size: 10,
|
|
||||||
Host: []C.DomainMatcher{tree.NewDomainSet()},
|
|
||||||
Mode: C.FilterWhiteList,
|
|
||||||
})
|
|
||||||
assert.Nil(t, err)
|
|
||||||
defer os.Remove(tempfile)
|
|
||||||
|
|
||||||
for _, pool := range pools {
|
|
||||||
assert.False(t, pool.ShouldSkipped("example.com"))
|
|
||||||
assert.True(t, pool.ShouldSkipped("foo.com"))
|
|
||||||
assert.True(t, pool.ShouldSkipped("baz.com"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPool_MaxCacheSize(t *testing.T) {
|
func TestPool_MaxCacheSize(t *testing.T) {
|
||||||
ipnet := netip.MustParsePrefix("192.168.0.1/24")
|
ipnet := netip.MustParsePrefix("192.168.0.1/24")
|
||||||
pool, _ := New(Options{
|
pool, _ := New(Options{
|
||||||
|
|||||||
44
component/fakeip/skipper.go
Normal file
44
component/fakeip/skipper.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package fakeip
|
||||||
|
|
||||||
|
import (
|
||||||
|
C "github.com/metacubex/mihomo/constant"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
UseFakeIP = "fake-ip"
|
||||||
|
UseRealIP = "real-ip"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Skipper struct {
|
||||||
|
Rules []C.Rule
|
||||||
|
Host []C.DomainMatcher
|
||||||
|
Mode C.FilterMode
|
||||||
|
}
|
||||||
|
|
||||||
|
// ShouldSkipped return if domain should be skipped
|
||||||
|
func (p *Skipper) ShouldSkipped(domain string) bool {
|
||||||
|
if len(p.Rules) > 0 {
|
||||||
|
metadata := &C.Metadata{Host: domain}
|
||||||
|
for _, rule := range p.Rules {
|
||||||
|
if matched, action := rule.Match(metadata, C.RuleMatchHelper{}); matched {
|
||||||
|
return action == UseRealIP
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
should := p.shouldSkipped(domain)
|
||||||
|
if p.Mode == C.FilterWhiteList {
|
||||||
|
return !should
|
||||||
|
}
|
||||||
|
return should
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Skipper) shouldSkipped(domain string) bool {
|
||||||
|
for _, matcher := range p.Host {
|
||||||
|
if matcher.MatchDomain(domain) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
35
component/fakeip/skipper_test.go
Normal file
35
component/fakeip/skipper_test.go
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package fakeip
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/metacubex/mihomo/component/trie"
|
||||||
|
C "github.com/metacubex/mihomo/constant"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSkipper_BlackList(t *testing.T) {
|
||||||
|
tree := trie.New[struct{}]()
|
||||||
|
assert.NoError(t, tree.Insert("example.com", struct{}{}))
|
||||||
|
assert.False(t, tree.IsEmpty())
|
||||||
|
skipper := &Skipper{
|
||||||
|
Host: []C.DomainMatcher{tree.NewDomainSet()},
|
||||||
|
}
|
||||||
|
assert.True(t, skipper.ShouldSkipped("example.com"))
|
||||||
|
assert.False(t, skipper.ShouldSkipped("foo.com"))
|
||||||
|
assert.False(t, skipper.shouldSkipped("baz.com"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSkipper_WhiteList(t *testing.T) {
|
||||||
|
tree := trie.New[struct{}]()
|
||||||
|
assert.NoError(t, tree.Insert("example.com", struct{}{}))
|
||||||
|
assert.False(t, tree.IsEmpty())
|
||||||
|
skipper := &Skipper{
|
||||||
|
Host: []C.DomainMatcher{tree.NewDomainSet()},
|
||||||
|
Mode: C.FilterWhiteList,
|
||||||
|
}
|
||||||
|
assert.False(t, skipper.ShouldSkipped("example.com"))
|
||||||
|
assert.True(t, skipper.ShouldSkipped("foo.com"))
|
||||||
|
assert.True(t, skipper.ShouldSkipped("baz.com"))
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/component/ech"
|
"github.com/metacubex/mihomo/component/ech"
|
||||||
|
"github.com/metacubex/mihomo/transport/sudoku"
|
||||||
"github.com/metacubex/mihomo/transport/vless/encryption"
|
"github.com/metacubex/mihomo/transport/vless/encryption"
|
||||||
|
|
||||||
"github.com/gofrs/uuid/v5"
|
"github.com/gofrs/uuid/v5"
|
||||||
@@ -12,7 +13,7 @@ import (
|
|||||||
|
|
||||||
func Main(args []string) {
|
func Main(args []string) {
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
panic("Using: generate uuid/reality-keypair/wg-keypair/ech-keypair/vless-mlkem768/vless-x25519")
|
panic("Using: generate uuid/reality-keypair/wg-keypair/ech-keypair/vless-mlkem768/vless-x25519/sudoku-keypair")
|
||||||
}
|
}
|
||||||
switch args[0] {
|
switch args[0] {
|
||||||
case "uuid":
|
case "uuid":
|
||||||
@@ -69,5 +70,13 @@ func Main(args []string) {
|
|||||||
fmt.Println("PrivateKey: " + privateKeyBase64)
|
fmt.Println("PrivateKey: " + privateKeyBase64)
|
||||||
fmt.Println("Password: " + passwordBase64)
|
fmt.Println("Password: " + passwordBase64)
|
||||||
fmt.Println("Hash32: " + hash32Base64)
|
fmt.Println("Hash32: " + hash32Base64)
|
||||||
|
case "sudoku-keypair":
|
||||||
|
privateKey, publicKey, err := sudoku.GenKeyPair()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
// Output: Available Private Key for client, Master Public Key for server
|
||||||
|
fmt.Println("PrivateKey: " + privateKey)
|
||||||
|
fmt.Println("PublicKey: " + publicKey)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -14,6 +13,8 @@ import (
|
|||||||
"github.com/metacubex/mihomo/component/mmdb"
|
"github.com/metacubex/mihomo/component/mmdb"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/log"
|
"github.com/metacubex/mihomo/log"
|
||||||
|
|
||||||
|
"github.com/metacubex/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
|
||||||
URL "net/url"
|
URL "net/url"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -13,6 +12,8 @@ import (
|
|||||||
"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/listener/inner"
|
"github.com/metacubex/mihomo/listener/inner"
|
||||||
|
|
||||||
|
"github.com/metacubex/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
package memory
|
package memory
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"syscall"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
_ "unsafe"
|
||||||
"github.com/ebitengine/purego"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const PROC_PIDTASKINFO = 4
|
const PROC_PIDTASKINFO = 4
|
||||||
@@ -29,24 +29,12 @@ type ProcTaskInfo struct {
|
|||||||
Priority int32
|
Priority int32
|
||||||
}
|
}
|
||||||
|
|
||||||
const System = "/usr/lib/libSystem.B.dylib"
|
|
||||||
|
|
||||||
type ProcPidInfoFunc func(pid, flavor int32, arg uint64, buffer uintptr, bufferSize int32) int32
|
|
||||||
|
|
||||||
const ProcPidInfoSym = "proc_pidinfo"
|
|
||||||
|
|
||||||
func GetMemoryInfo(pid int32) (*MemoryInfoStat, error) {
|
func GetMemoryInfo(pid int32) (*MemoryInfoStat, error) {
|
||||||
lib, err := purego.Dlopen(System, purego.RTLD_LAZY|purego.RTLD_GLOBAL)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer purego.Dlclose(lib)
|
|
||||||
|
|
||||||
var procPidInfo ProcPidInfoFunc
|
|
||||||
purego.RegisterLibFunc(&procPidInfo, lib, ProcPidInfoSym)
|
|
||||||
|
|
||||||
var ti ProcTaskInfo
|
var ti ProcTaskInfo
|
||||||
procPidInfo(pid, PROC_PIDTASKINFO, 0, uintptr(unsafe.Pointer(&ti)), int32(unsafe.Sizeof(ti)))
|
_, _, errno := syscall_syscall6(proc_pidinfo_trampoline_addr, uintptr(pid), PROC_PIDTASKINFO, 0, uintptr(unsafe.Pointer(&ti)), unsafe.Sizeof(ti), 0)
|
||||||
|
if errno != 0 {
|
||||||
|
return nil, errno
|
||||||
|
}
|
||||||
|
|
||||||
ret := &MemoryInfoStat{
|
ret := &MemoryInfoStat{
|
||||||
RSS: uint64(ti.Resident_size),
|
RSS: uint64(ti.Resident_size),
|
||||||
@@ -54,3 +42,26 @@ func GetMemoryInfo(pid int32) (*MemoryInfoStat, error) {
|
|||||||
}
|
}
|
||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var proc_pidinfo_trampoline_addr uintptr
|
||||||
|
|
||||||
|
//go:cgo_import_dynamic proc_pidinfo proc_pidinfo "/usr/lib/libSystem.B.dylib"
|
||||||
|
|
||||||
|
// from golang.org/x/sys@v0.30.0/unix/syscall_darwin_libSystem.go
|
||||||
|
|
||||||
|
// Implemented in the runtime package (runtime/sys_darwin.go)
|
||||||
|
func syscall_syscall(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno)
|
||||||
|
func syscall_syscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err syscall.Errno)
|
||||||
|
func syscall_syscall6X(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err syscall.Errno)
|
||||||
|
func syscall_syscall9(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9 uintptr) (r1, r2 uintptr, err syscall.Errno) // 32-bit only
|
||||||
|
func syscall_rawSyscall(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno)
|
||||||
|
func syscall_rawSyscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err syscall.Errno)
|
||||||
|
func syscall_syscallPtr(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno)
|
||||||
|
|
||||||
|
//go:linkname syscall_syscall syscall.syscall
|
||||||
|
//go:linkname syscall_syscall6 syscall.syscall6
|
||||||
|
//go:linkname syscall_syscall6X syscall.syscall6X
|
||||||
|
//go:linkname syscall_syscall9 syscall.syscall9
|
||||||
|
//go:linkname syscall_rawSyscall syscall.rawSyscall
|
||||||
|
//go:linkname syscall_rawSyscall6 syscall.rawSyscall6
|
||||||
|
//go:linkname syscall_syscallPtr syscall.syscallPtr
|
||||||
|
|||||||
9
component/memory/memory_darwin_amd64.s
Normal file
9
component/memory/memory_darwin_amd64.s
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
// go run mkasm.go darwin amd64
|
||||||
|
// Code generated by the command above; DO NOT EDIT.
|
||||||
|
|
||||||
|
#include "textflag.h"
|
||||||
|
|
||||||
|
TEXT proc_pidinfo_trampoline<>(SB),NOSPLIT,$0-0
|
||||||
|
JMP proc_pidinfo(SB)
|
||||||
|
GLOBL ·proc_pidinfo_trampoline_addr(SB), RODATA, $8
|
||||||
|
DATA ·proc_pidinfo_trampoline_addr(SB)/8, $proc_pidinfo_trampoline<>(SB)
|
||||||
9
component/memory/memory_darwin_arm64.s
Normal file
9
component/memory/memory_darwin_arm64.s
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
// go run mkasm.go darwin arm64
|
||||||
|
// Code generated by the command above; DO NOT EDIT.
|
||||||
|
|
||||||
|
#include "textflag.h"
|
||||||
|
|
||||||
|
TEXT proc_pidinfo_trampoline<>(SB),NOSPLIT,$0-0
|
||||||
|
JMP proc_pidinfo(SB)
|
||||||
|
GLOBL ·proc_pidinfo_trampoline_addr(SB), RODATA, $8
|
||||||
|
DATA ·proc_pidinfo_trampoline_addr(SB)/8, $proc_pidinfo_trampoline<>(SB)
|
||||||
23
component/mptcp/mptcp_go120.go
Normal file
23
component/mptcp/mptcp_go120.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
//go:build !go1.21
|
||||||
|
|
||||||
|
package mptcp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
)
|
||||||
|
|
||||||
|
const MultipathTCPAvailable = false
|
||||||
|
|
||||||
|
func SetNetDialer(dialer *net.Dialer, open bool) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetNetDialer(dialer *net.Dialer) bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetNetListenConfig(listenConfig *net.ListenConfig, open bool) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetNetListenConfig(listenConfig *net.ListenConfig) bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
23
component/mptcp/mptcp_go121.go
Normal file
23
component/mptcp/mptcp_go121.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
//go:build go1.21
|
||||||
|
|
||||||
|
package mptcp
|
||||||
|
|
||||||
|
import "net"
|
||||||
|
|
||||||
|
const MultipathTCPAvailable = true
|
||||||
|
|
||||||
|
func SetNetDialer(dialer *net.Dialer, open bool) {
|
||||||
|
dialer.SetMultipathTCP(open)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetNetDialer(dialer *net.Dialer) bool {
|
||||||
|
return dialer.MultipathTCP()
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetNetListenConfig(listenConfig *net.ListenConfig, open bool) {
|
||||||
|
listenConfig.SetMultipathTCP(open)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetNetListenConfig(listenConfig *net.ListenConfig) bool {
|
||||||
|
return listenConfig.MultipathTCP()
|
||||||
|
}
|
||||||
@@ -197,6 +197,10 @@ func newSearcher(major int) *searcher {
|
|||||||
case 12:
|
case 12:
|
||||||
fallthrough
|
fallthrough
|
||||||
case 13:
|
case 13:
|
||||||
|
fallthrough
|
||||||
|
case 14:
|
||||||
|
fallthrough
|
||||||
|
case 15:
|
||||||
s = &searcher{
|
s = &searcher{
|
||||||
headSize: 64,
|
headSize: 64,
|
||||||
tcpItemSize: 744,
|
tcpItemSize: 744,
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ var (
|
|||||||
|
|
||||||
bucketSelected = []byte("selected")
|
bucketSelected = []byte("selected")
|
||||||
bucketFakeip = []byte("fakeip")
|
bucketFakeip = []byte("fakeip")
|
||||||
|
bucketFakeip6 = []byte("fakeip6")
|
||||||
bucketETag = []byte("etag")
|
bucketETag = []byte("etag")
|
||||||
bucketSubscriptionInfo = []byte("subscriptioninfo")
|
bucketSubscriptionInfo = []byte("subscriptioninfo")
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -10,10 +10,15 @@ import (
|
|||||||
|
|
||||||
type FakeIpStore struct {
|
type FakeIpStore struct {
|
||||||
*CacheFile
|
*CacheFile
|
||||||
|
bucketName []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CacheFile) FakeIpStore() *FakeIpStore {
|
func (c *CacheFile) FakeIpStore() *FakeIpStore {
|
||||||
return &FakeIpStore{c}
|
return &FakeIpStore{c, bucketFakeip}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CacheFile) FakeIpStore6() *FakeIpStore {
|
||||||
|
return &FakeIpStore{c, bucketFakeip6}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *FakeIpStore) GetByHost(host string) (ip netip.Addr, exist bool) {
|
func (c *FakeIpStore) GetByHost(host string) (ip netip.Addr, exist bool) {
|
||||||
@@ -21,7 +26,7 @@ func (c *FakeIpStore) GetByHost(host string) (ip netip.Addr, exist bool) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.DB.View(func(t *bbolt.Tx) error {
|
c.DB.View(func(t *bbolt.Tx) error {
|
||||||
if bucket := t.Bucket(bucketFakeip); bucket != nil {
|
if bucket := t.Bucket(c.bucketName); bucket != nil {
|
||||||
if v := bucket.Get([]byte(host)); v != nil {
|
if v := bucket.Get([]byte(host)); v != nil {
|
||||||
ip, exist = netip.AddrFromSlice(v)
|
ip, exist = netip.AddrFromSlice(v)
|
||||||
}
|
}
|
||||||
@@ -36,7 +41,7 @@ func (c *FakeIpStore) PutByHost(host string, ip netip.Addr) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
err := c.DB.Batch(func(t *bbolt.Tx) error {
|
err := c.DB.Batch(func(t *bbolt.Tx) error {
|
||||||
bucket, err := t.CreateBucketIfNotExists(bucketFakeip)
|
bucket, err := t.CreateBucketIfNotExists(c.bucketName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -52,7 +57,7 @@ func (c *FakeIpStore) GetByIP(ip netip.Addr) (host string, exist bool) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.DB.View(func(t *bbolt.Tx) error {
|
c.DB.View(func(t *bbolt.Tx) error {
|
||||||
if bucket := t.Bucket(bucketFakeip); bucket != nil {
|
if bucket := t.Bucket(c.bucketName); bucket != nil {
|
||||||
if v := bucket.Get(ip.AsSlice()); v != nil {
|
if v := bucket.Get(ip.AsSlice()); v != nil {
|
||||||
host, exist = string(v), true
|
host, exist = string(v), true
|
||||||
}
|
}
|
||||||
@@ -67,7 +72,7 @@ func (c *FakeIpStore) PutByIP(ip netip.Addr, host string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
err := c.DB.Batch(func(t *bbolt.Tx) error {
|
err := c.DB.Batch(func(t *bbolt.Tx) error {
|
||||||
bucket, err := t.CreateBucketIfNotExists(bucketFakeip)
|
bucket, err := t.CreateBucketIfNotExists(c.bucketName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -85,7 +90,7 @@ func (c *FakeIpStore) DelByIP(ip netip.Addr) {
|
|||||||
|
|
||||||
addr := ip.AsSlice()
|
addr := ip.AsSlice()
|
||||||
err := c.DB.Batch(func(t *bbolt.Tx) error {
|
err := c.DB.Batch(func(t *bbolt.Tx) error {
|
||||||
bucket, err := t.CreateBucketIfNotExists(bucketFakeip)
|
bucket, err := t.CreateBucketIfNotExists(c.bucketName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -105,11 +110,11 @@ func (c *FakeIpStore) DelByIP(ip netip.Addr) {
|
|||||||
|
|
||||||
func (c *FakeIpStore) FlushFakeIP() error {
|
func (c *FakeIpStore) FlushFakeIP() error {
|
||||||
err := c.DB.Batch(func(t *bbolt.Tx) error {
|
err := c.DB.Batch(func(t *bbolt.Tx) error {
|
||||||
bucket := t.Bucket(bucketFakeip)
|
bucket := t.Bucket(c.bucketName)
|
||||||
if bucket == nil {
|
if bucket == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return t.DeleteBucket(bucketFakeip)
|
return t.DeleteBucket(c.bucketName)
|
||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
37
component/proxydialer/byname.go
Normal file
37
component/proxydialer/byname.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package proxydialer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/netip"
|
||||||
|
|
||||||
|
C "github.com/metacubex/mihomo/constant"
|
||||||
|
"github.com/metacubex/mihomo/tunnel"
|
||||||
|
)
|
||||||
|
|
||||||
|
type byNameProxyDialer struct {
|
||||||
|
proxyName string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d byNameProxyDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||||
|
proxies := tunnel.Proxies()
|
||||||
|
proxy, ok := proxies[d.proxyName]
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("proxyName[%s] not found", d.proxyName)
|
||||||
|
}
|
||||||
|
return New(proxy, true).DialContext(ctx, network, address)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d byNameProxyDialer) ListenPacket(ctx context.Context, network, address string, rAddrPort netip.AddrPort) (net.PacketConn, error) {
|
||||||
|
proxies := tunnel.Proxies()
|
||||||
|
proxy, ok := proxies[d.proxyName]
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("proxyName[%s] not found", d.proxyName)
|
||||||
|
}
|
||||||
|
return New(proxy, true).ListenPacket(ctx, network, address, rAddrPort)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewByName(proxyName string) C.Dialer {
|
||||||
|
return byNameProxyDialer{proxyName: proxyName}
|
||||||
|
}
|
||||||
@@ -2,34 +2,22 @@ package proxydialer
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/tunnel"
|
|
||||||
"github.com/metacubex/mihomo/tunnel/statistic"
|
"github.com/metacubex/mihomo/tunnel/statistic"
|
||||||
)
|
)
|
||||||
|
|
||||||
type proxyDialer struct {
|
type proxyDialer struct {
|
||||||
proxy C.ProxyAdapter
|
proxy C.ProxyAdapter
|
||||||
dialer C.Dialer
|
|
||||||
statistic bool
|
statistic bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(proxy C.ProxyAdapter, dialer C.Dialer, statistic bool) C.Dialer {
|
func New(proxy C.ProxyAdapter, statistic bool) C.Dialer {
|
||||||
return proxyDialer{proxy: proxy, dialer: dialer, statistic: statistic}
|
return proxyDialer{proxy: proxy, statistic: statistic}
|
||||||
}
|
|
||||||
|
|
||||||
func NewByName(proxyName string, dialer C.Dialer) (C.Dialer, error) {
|
|
||||||
proxies := tunnel.Proxies()
|
|
||||||
if proxy, ok := proxies[proxyName]; ok {
|
|
||||||
return New(proxy, dialer, true), nil
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("proxyName[%s] not found", proxyName)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p proxyDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
func (p proxyDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||||
@@ -50,13 +38,7 @@ func (p proxyDialer) DialContext(ctx context.Context, network, address string) (
|
|||||||
}
|
}
|
||||||
return N.NewBindPacketConn(pc, currentMeta.UDPAddr()), nil
|
return N.NewBindPacketConn(pc, currentMeta.UDPAddr()), nil
|
||||||
}
|
}
|
||||||
var conn C.Conn
|
conn, err := p.proxy.DialContext(ctx, currentMeta)
|
||||||
var err error
|
|
||||||
if _, ok := p.dialer.(dialer.Dialer); ok { // first using old function to let mux work
|
|
||||||
conn, err = p.proxy.DialContext(ctx, currentMeta)
|
|
||||||
} else {
|
|
||||||
conn, err = p.proxy.DialContextWithDialer(ctx, p.dialer, currentMeta)
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -72,14 +54,8 @@ func (p proxyDialer) ListenPacket(ctx context.Context, network, address string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p proxyDialer) listenPacket(ctx context.Context, currentMeta *C.Metadata) (C.PacketConn, error) {
|
func (p proxyDialer) listenPacket(ctx context.Context, currentMeta *C.Metadata) (C.PacketConn, error) {
|
||||||
var pc C.PacketConn
|
|
||||||
var err error
|
|
||||||
currentMeta.NetWork = C.UDP
|
currentMeta.NetWork = C.UDP
|
||||||
if _, ok := p.dialer.(dialer.Dialer); ok { // first using old function to let mux work
|
pc, err := p.proxy.ListenPacketContext(ctx, currentMeta)
|
||||||
pc, err = p.proxy.ListenPacketContext(ctx, currentMeta)
|
|
||||||
} else {
|
|
||||||
pc, err = p.proxy.ListenPacketWithDialer(ctx, p.dialer, currentMeta)
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,71 +12,22 @@ import (
|
|||||||
|
|
||||||
type SingDialer interface {
|
type SingDialer interface {
|
||||||
N.Dialer
|
N.Dialer
|
||||||
SetDialer(dialer C.Dialer)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type singDialer proxyDialer
|
type singDialer struct {
|
||||||
|
cDialer C.Dialer
|
||||||
|
}
|
||||||
|
|
||||||
var _ N.Dialer = (*singDialer)(nil)
|
var _ N.Dialer = (*singDialer)(nil)
|
||||||
|
|
||||||
func (d *singDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
func (d singDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
||||||
return (*proxyDialer)(d).DialContext(ctx, network, destination.String())
|
return d.cDialer.DialContext(ctx, network, destination.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *singDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
func (d singDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
||||||
return (*proxyDialer)(d).ListenPacket(ctx, "udp", "", destination.AddrPort())
|
return d.cDialer.ListenPacket(ctx, "udp", "", destination.AddrPort())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *singDialer) SetDialer(dialer C.Dialer) {
|
func NewSingDialer(cDialer C.Dialer) SingDialer {
|
||||||
(*proxyDialer)(d).dialer = dialer
|
return singDialer{cDialer: cDialer}
|
||||||
}
|
|
||||||
|
|
||||||
func NewSingDialer(proxy C.ProxyAdapter, dialer C.Dialer, statistic bool) SingDialer {
|
|
||||||
return (*singDialer)(&proxyDialer{
|
|
||||||
proxy: proxy,
|
|
||||||
dialer: dialer,
|
|
||||||
statistic: statistic,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
type byNameSingDialer struct {
|
|
||||||
dialer C.Dialer
|
|
||||||
proxyName string
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ N.Dialer = (*byNameSingDialer)(nil)
|
|
||||||
|
|
||||||
func (d *byNameSingDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
|
||||||
var cDialer C.Dialer = d.dialer
|
|
||||||
if len(d.proxyName) > 0 {
|
|
||||||
pd, err := NewByName(d.proxyName, d.dialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
cDialer = pd
|
|
||||||
}
|
|
||||||
return cDialer.DialContext(ctx, network, destination.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *byNameSingDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
|
||||||
var cDialer C.Dialer = d.dialer
|
|
||||||
if len(d.proxyName) > 0 {
|
|
||||||
pd, err := NewByName(d.proxyName, d.dialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
cDialer = pd
|
|
||||||
}
|
|
||||||
return cDialer.ListenPacket(ctx, "udp", "", destination.AddrPort())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *byNameSingDialer) SetDialer(dialer C.Dialer) {
|
|
||||||
d.dialer = dialer
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewByNameSingDialer(proxyName string, dialer C.Dialer) SingDialer {
|
|
||||||
return &byNameSingDialer{
|
|
||||||
dialer: dialer,
|
|
||||||
proxyName: proxyName,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,15 +6,15 @@ import (
|
|||||||
D "github.com/miekg/dns"
|
D "github.com/miekg/dns"
|
||||||
)
|
)
|
||||||
|
|
||||||
var DefaultLocalServer LocalServer
|
var DefaultService Service
|
||||||
|
|
||||||
type LocalServer interface {
|
type Service interface {
|
||||||
ServeMsg(ctx context.Context, msg *D.Msg) (*D.Msg, error)
|
ServeMsg(ctx context.Context, msg *D.Msg) (*D.Msg, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeMsg with a dns.Msg, return resolve dns.Msg
|
// ServeMsg with a dns.Msg, return resolve dns.Msg
|
||||||
func ServeMsg(ctx context.Context, msg *D.Msg) (*D.Msg, error) {
|
func ServeMsg(ctx context.Context, msg *D.Msg) (*D.Msg, error) {
|
||||||
if server := DefaultLocalServer; server != nil {
|
if server := DefaultService; server != nil {
|
||||||
return server.ServeMsg(ctx, msg)
|
return server.ServeMsg(ctx, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
"github.com/metacubex/mihomo/component/slowdown"
|
"github.com/metacubex/mihomo/component/slowdown"
|
||||||
types "github.com/metacubex/mihomo/constant/provider"
|
P "github.com/metacubex/mihomo/constant/provider"
|
||||||
"github.com/metacubex/mihomo/log"
|
"github.com/metacubex/mihomo/log"
|
||||||
|
|
||||||
"github.com/metacubex/fswatch"
|
"github.com/metacubex/fswatch"
|
||||||
@@ -22,7 +22,7 @@ type Fetcher[V any] struct {
|
|||||||
ctxCancel context.CancelFunc
|
ctxCancel context.CancelFunc
|
||||||
resourceType string
|
resourceType string
|
||||||
name string
|
name string
|
||||||
vehicle types.Vehicle
|
vehicle P.Vehicle
|
||||||
updatedAt time.Time
|
updatedAt time.Time
|
||||||
hash utils.HashType
|
hash utils.HashType
|
||||||
parser Parser[V]
|
parser Parser[V]
|
||||||
@@ -37,11 +37,11 @@ func (f *Fetcher[V]) Name() string {
|
|||||||
return f.name
|
return f.name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fetcher[V]) Vehicle() types.Vehicle {
|
func (f *Fetcher[V]) Vehicle() P.Vehicle {
|
||||||
return f.vehicle
|
return f.vehicle
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fetcher[V]) VehicleType() types.VehicleType {
|
func (f *Fetcher[V]) VehicleType() P.VehicleType {
|
||||||
return f.vehicle.Type()
|
return f.vehicle.Type()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +88,7 @@ func (f *Fetcher[V]) Update() (V, bool, error) {
|
|||||||
f.backoff.AddAttempt() // add a failed attempt to backoff
|
f.backoff.AddAttempt() // add a failed attempt to backoff
|
||||||
return lo.Empty[V](), false, err
|
return lo.Empty[V](), false, err
|
||||||
}
|
}
|
||||||
return f.loadBuf(buf, hash, f.vehicle.Type() != types.File)
|
return f.loadBuf(buf, hash, f.vehicle.Type() != P.File)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fetcher[V]) SideUpdate(buf []byte) (V, bool, error) {
|
func (f *Fetcher[V]) SideUpdate(buf []byte) (V, bool, error) {
|
||||||
@@ -180,7 +180,7 @@ func (f *Fetcher[V]) pullLoop(forceUpdate bool) {
|
|||||||
|
|
||||||
func (f *Fetcher[V]) startPullLoop(forceUpdate bool) (err error) {
|
func (f *Fetcher[V]) startPullLoop(forceUpdate bool) (err error) {
|
||||||
// pull contents automatically
|
// pull contents automatically
|
||||||
if f.vehicle.Type() == types.File {
|
if f.vehicle.Type() == P.File {
|
||||||
f.watcher, err = fswatch.NewWatcher(fswatch.Options{
|
f.watcher, err = fswatch.NewWatcher(fswatch.Options{
|
||||||
Path: []string{f.vehicle.Path()},
|
Path: []string{f.vehicle.Path()},
|
||||||
Callback: f.updateCallback,
|
Callback: f.updateCallback,
|
||||||
@@ -218,7 +218,7 @@ func (f *Fetcher[V]) updateWithLog() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFetcher[V any](name string, interval time.Duration, vehicle types.Vehicle, parser Parser[V], onUpdate func(V)) *Fetcher[V] {
|
func NewFetcher[V any](name string, interval time.Duration, vehicle P.Vehicle, parser Parser[V], onUpdate func(V)) *Fetcher[V] {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
minBackoff := 10 * time.Second
|
minBackoff := 10 * time.Second
|
||||||
if interval < minBackoff {
|
if interval < minBackoff {
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
@@ -12,7 +11,9 @@ import (
|
|||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
mihomoHttp "github.com/metacubex/mihomo/component/http"
|
mihomoHttp "github.com/metacubex/mihomo/component/http"
|
||||||
"github.com/metacubex/mihomo/component/profile/cachefile"
|
"github.com/metacubex/mihomo/component/profile/cachefile"
|
||||||
types "github.com/metacubex/mihomo/constant/provider"
|
P "github.com/metacubex/mihomo/constant/provider"
|
||||||
|
|
||||||
|
"github.com/metacubex/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -50,8 +51,8 @@ type FileVehicle struct {
|
|||||||
path string
|
path string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FileVehicle) Type() types.VehicleType {
|
func (f *FileVehicle) Type() P.VehicleType {
|
||||||
return types.File
|
return P.File
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FileVehicle) Path() string {
|
func (f *FileVehicle) Path() string {
|
||||||
@@ -91,15 +92,15 @@ type HTTPVehicle struct {
|
|||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
sizeLimit int64
|
sizeLimit int64
|
||||||
inRead func(response *http.Response)
|
inRead func(response *http.Response)
|
||||||
provider types.ProxyProvider
|
provider P.ProxyProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *HTTPVehicle) Url() string {
|
func (h *HTTPVehicle) Url() string {
|
||||||
return h.url
|
return h.url
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *HTTPVehicle) Type() types.VehicleType {
|
func (h *HTTPVehicle) Type() P.VehicleType {
|
||||||
return types.HTTP
|
return P.HTTP
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *HTTPVehicle) Path() string {
|
func (h *HTTPVehicle) Path() string {
|
||||||
|
|||||||
@@ -3,14 +3,13 @@ package tls
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/metacubex/mihomo/log"
|
"github.com/metacubex/mihomo/log"
|
||||||
|
|
||||||
"golang.org/x/net/http2"
|
"github.com/metacubex/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func extractTlsHandshakeTimeoutFromServer(s *http.Server) time.Duration {
|
func extractTlsHandshakeTimeoutFromServer(s *http.Server) time.Duration {
|
||||||
@@ -35,8 +34,8 @@ func extractTlsHandshakeTimeoutFromServer(s *http.Server) time.Duration {
|
|||||||
// only do tls handshake and check NegotiatedProtocol with std's *tls.Conn
|
// only do tls handshake and check NegotiatedProtocol with std's *tls.Conn
|
||||||
// so we do the same logic to let http2 (not h2c) work fine
|
// so we do the same logic to let http2 (not h2c) work fine
|
||||||
func NewListenerForHttps(l net.Listener, httpServer *http.Server, tlsConfig *Config) net.Listener {
|
func NewListenerForHttps(l net.Listener, httpServer *http.Server, tlsConfig *Config) net.Listener {
|
||||||
http2Server := &http2.Server{}
|
http2Server := &http.Http2Server{}
|
||||||
_ = http2.ConfigureServer(httpServer, http2Server)
|
_ = http.Http2ConfigureServer(httpServer, http2Server)
|
||||||
return N.NewHandleContextListener(context.Background(), l, func(ctx context.Context, conn net.Conn) (net.Conn, error) {
|
return N.NewHandleContextListener(context.Background(), l, func(ctx context.Context, conn net.Conn) (net.Conn, error) {
|
||||||
c := Server(conn, tlsConfig)
|
c := Server(conn, tlsConfig)
|
||||||
|
|
||||||
@@ -58,8 +57,8 @@ func NewListenerForHttps(l net.Listener, httpServer *http.Server, tlsConfig *Con
|
|||||||
_ = conn.SetWriteDeadline(time.Time{})
|
_ = conn.SetWriteDeadline(time.Time{})
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.ConnectionState().NegotiatedProtocol == http2.NextProtoTLS {
|
if c.ConnectionState().NegotiatedProtocol == http.Http2NextProtoTLS {
|
||||||
http2Server.ServeConn(c, &http2.ServeConnOpts{BaseConfig: httpServer})
|
http2Server.ServeConn(c, &http.Http2ServeConnOpts{BaseConfig: httpServer})
|
||||||
return nil, net.ErrClosed
|
return nil, net.ErrClosed
|
||||||
}
|
}
|
||||||
return c, nil
|
return c, nil
|
||||||
|
|||||||
@@ -10,22 +10,21 @@ import (
|
|||||||
"crypto/hmac"
|
"crypto/hmac"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"crypto/sha512"
|
"crypto/sha512"
|
||||||
"crypto/tls"
|
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/log"
|
"github.com/metacubex/mihomo/log"
|
||||||
"github.com/metacubex/mihomo/ntp"
|
"github.com/metacubex/mihomo/ntp"
|
||||||
|
|
||||||
|
"github.com/metacubex/http"
|
||||||
"github.com/metacubex/randv2"
|
"github.com/metacubex/randv2"
|
||||||
|
"github.com/metacubex/tls"
|
||||||
utls "github.com/metacubex/utls"
|
utls "github.com/metacubex/utls"
|
||||||
"golang.org/x/crypto/hkdf"
|
"golang.org/x/crypto/hkdf"
|
||||||
"golang.org/x/net/http2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const RealityMaxShortIDLen = 8
|
const RealityMaxShortIDLen = 8
|
||||||
@@ -37,13 +36,14 @@ type RealityConfig struct {
|
|||||||
SupportX25519MLKEM768 bool
|
SupportX25519MLKEM768 bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRealityConn(ctx context.Context, conn net.Conn, fingerprint UClientHelloID, tlsConfig *Config, realityConfig *RealityConfig) (net.Conn, error) {
|
func GetRealityConn(ctx context.Context, conn net.Conn, fingerprint UClientHelloID, serverName string, realityConfig *RealityConfig) (net.Conn, error) {
|
||||||
for retry := 0; ; retry++ {
|
for retry := 0; ; retry++ {
|
||||||
verifier := &realityVerifier{
|
verifier := &realityVerifier{
|
||||||
serverName: tlsConfig.ServerName,
|
serverName: serverName,
|
||||||
}
|
}
|
||||||
uConfig := &utls.Config{
|
uConfig := &utls.Config{
|
||||||
ServerName: tlsConfig.ServerName,
|
Time: ntp.Now,
|
||||||
|
ServerName: serverName,
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
SessionTicketsDisabled: true,
|
SessionTicketsDisabled: true,
|
||||||
VerifyPeerCertificate: verifier.VerifyPeerCertificate,
|
VerifyPeerCertificate: verifier.VerifyPeerCertificate,
|
||||||
@@ -132,7 +132,7 @@ func GetRealityConn(ctx context.Context, conn net.Conn, fingerprint UClientHello
|
|||||||
func realityClientFallback(uConn net.Conn, serverName string, fingerprint utls.ClientHelloID) {
|
func realityClientFallback(uConn net.Conn, serverName string, fingerprint utls.ClientHelloID) {
|
||||||
defer uConn.Close()
|
defer uConn.Close()
|
||||||
client := http.Client{
|
client := http.Client{
|
||||||
Transport: &http2.Transport{
|
Transport: &http.Http2Transport{
|
||||||
DialTLSContext: func(ctx context.Context, network, addr string, config *tls.Config) (net.Conn, error) {
|
DialTLSContext: func(ctx context.Context, network, addr string, config *tls.Config) (net.Conn, error) {
|
||||||
return uConn, nil
|
return uConn, nil
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
package tls
|
package tls
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
|
"reflect"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/once"
|
"github.com/metacubex/mihomo/common/once"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
"github.com/metacubex/mihomo/log"
|
"github.com/metacubex/mihomo/log"
|
||||||
|
|
||||||
|
"github.com/metacubex/tls"
|
||||||
utls "github.com/metacubex/utls"
|
utls "github.com/metacubex/utls"
|
||||||
"github.com/mroth/weightedrand/v2"
|
"github.com/mroth/weightedrand/v2"
|
||||||
)
|
)
|
||||||
@@ -126,8 +129,11 @@ type EncryptedClientHelloKey = utls.EncryptedClientHelloKey
|
|||||||
|
|
||||||
type Config = utls.Config
|
type Config = utls.Config
|
||||||
|
|
||||||
|
var tlsCertificateRequestInfoCtxOffset = utils.MustOK(reflect.TypeOf((*tls.CertificateRequestInfo)(nil)).Elem().FieldByName("ctx")).Offset
|
||||||
|
var tlsClientHelloInfoCtxOffset = utils.MustOK(reflect.TypeOf((*tls.ClientHelloInfo)(nil)).Elem().FieldByName("ctx")).Offset
|
||||||
|
|
||||||
func UConfig(config *tls.Config) *utls.Config {
|
func UConfig(config *tls.Config) *utls.Config {
|
||||||
return &utls.Config{
|
cfg := &utls.Config{
|
||||||
Rand: config.Rand,
|
Rand: config.Rand,
|
||||||
Time: config.Time,
|
Time: config.Time,
|
||||||
Certificates: utils.Map(config.Certificates, UCertificate),
|
Certificates: utils.Map(config.Certificates, UCertificate),
|
||||||
@@ -147,6 +153,52 @@ func UConfig(config *tls.Config) *utls.Config {
|
|||||||
SessionTicketsDisabled: config.SessionTicketsDisabled,
|
SessionTicketsDisabled: config.SessionTicketsDisabled,
|
||||||
Renegotiation: utls.RenegotiationSupport(config.Renegotiation),
|
Renegotiation: utls.RenegotiationSupport(config.Renegotiation),
|
||||||
}
|
}
|
||||||
|
if config.GetClientCertificate != nil {
|
||||||
|
cfg.GetClientCertificate = func(info *utls.CertificateRequestInfo) (*utls.Certificate, error) {
|
||||||
|
tlsInfo := &tls.CertificateRequestInfo{
|
||||||
|
AcceptableCAs: info.AcceptableCAs,
|
||||||
|
SignatureSchemes: utils.Map(info.SignatureSchemes, func(it utls.SignatureScheme) tls.SignatureScheme {
|
||||||
|
return tls.SignatureScheme(it)
|
||||||
|
}),
|
||||||
|
Version: info.Version,
|
||||||
|
}
|
||||||
|
*(*context.Context)(unsafe.Add(unsafe.Pointer(tlsInfo), tlsCertificateRequestInfoCtxOffset)) = info.Context() // for tlsInfo.ctx
|
||||||
|
cert, err := config.GetClientCertificate(tlsInfo)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
uCert := UCertificate(*cert)
|
||||||
|
return &uCert, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if config.GetCertificate != nil {
|
||||||
|
cfg.GetCertificate = func(info *utls.ClientHelloInfo) (*utls.Certificate, error) {
|
||||||
|
tlsInfo := &tls.ClientHelloInfo{
|
||||||
|
CipherSuites: info.CipherSuites,
|
||||||
|
ServerName: info.ServerName,
|
||||||
|
SupportedCurves: utils.Map(info.SupportedCurves, func(it utls.CurveID) tls.CurveID {
|
||||||
|
return tls.CurveID(it)
|
||||||
|
}),
|
||||||
|
SupportedPoints: info.SupportedPoints,
|
||||||
|
SignatureSchemes: utils.Map(info.SignatureSchemes, func(it utls.SignatureScheme) tls.SignatureScheme {
|
||||||
|
return tls.SignatureScheme(it)
|
||||||
|
}),
|
||||||
|
SupportedProtos: info.SupportedProtos,
|
||||||
|
SupportedVersions: info.SupportedVersions,
|
||||||
|
Extensions: info.Extensions,
|
||||||
|
Conn: info.Conn,
|
||||||
|
//HelloRetryRequest: info.HelloRetryRequest,
|
||||||
|
}
|
||||||
|
*(*context.Context)(unsafe.Add(unsafe.Pointer(tlsInfo), tlsClientHelloInfoCtxOffset)) = info.Context() // for tlsInfo.ctx
|
||||||
|
cert, err := config.GetCertificate(tlsInfo)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
uCert := UCertificate(*cert)
|
||||||
|
return &uCert, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
// BuildWebsocketHandshakeState it will only send http/1.1 in its ALPN.
|
// BuildWebsocketHandshakeState it will only send http/1.1 in its ALPN.
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -20,6 +19,8 @@ import (
|
|||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/metacubex/mihomo/constant/features"
|
"github.com/metacubex/mihomo/constant/features"
|
||||||
"github.com/metacubex/mihomo/log"
|
"github.com/metacubex/mihomo/log"
|
||||||
|
|
||||||
|
"github.com/metacubex/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|||||||
@@ -3,11 +3,12 @@ package updater
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
mihomoHttp "github.com/metacubex/mihomo/component/http"
|
mihomoHttp "github.com/metacubex/mihomo/component/http"
|
||||||
|
|
||||||
|
"github.com/metacubex/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultHttpTimeout = time.Second * 90
|
const defaultHttpTimeout = time.Second * 90
|
||||||
|
|||||||
372
config/config.go
372
config/config.go
@@ -20,15 +20,15 @@ import (
|
|||||||
"github.com/metacubex/mihomo/component/cidr"
|
"github.com/metacubex/mihomo/component/cidr"
|
||||||
"github.com/metacubex/mihomo/component/fakeip"
|
"github.com/metacubex/mihomo/component/fakeip"
|
||||||
"github.com/metacubex/mihomo/component/geodata"
|
"github.com/metacubex/mihomo/component/geodata"
|
||||||
P "github.com/metacubex/mihomo/component/process"
|
"github.com/metacubex/mihomo/component/process"
|
||||||
"github.com/metacubex/mihomo/component/resolver"
|
"github.com/metacubex/mihomo/component/resolver"
|
||||||
"github.com/metacubex/mihomo/component/sniffer"
|
"github.com/metacubex/mihomo/component/sniffer"
|
||||||
"github.com/metacubex/mihomo/component/trie"
|
"github.com/metacubex/mihomo/component/trie"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
providerTypes "github.com/metacubex/mihomo/constant/provider"
|
P "github.com/metacubex/mihomo/constant/provider"
|
||||||
snifferTypes "github.com/metacubex/mihomo/constant/sniffer"
|
snifferTypes "github.com/metacubex/mihomo/constant/sniffer"
|
||||||
"github.com/metacubex/mihomo/dns"
|
"github.com/metacubex/mihomo/dns"
|
||||||
L "github.com/metacubex/mihomo/listener"
|
"github.com/metacubex/mihomo/listener"
|
||||||
LC "github.com/metacubex/mihomo/listener/config"
|
LC "github.com/metacubex/mihomo/listener/config"
|
||||||
"github.com/metacubex/mihomo/log"
|
"github.com/metacubex/mihomo/log"
|
||||||
R "github.com/metacubex/mihomo/rules"
|
R "github.com/metacubex/mihomo/rules"
|
||||||
@@ -44,27 +44,27 @@ import (
|
|||||||
// General config
|
// General config
|
||||||
type General struct {
|
type General struct {
|
||||||
Inbound
|
Inbound
|
||||||
Mode T.TunnelMode `json:"mode"`
|
Mode T.TunnelMode `json:"mode"`
|
||||||
UnifiedDelay bool `json:"unified-delay"`
|
UnifiedDelay bool `json:"unified-delay"`
|
||||||
LogLevel log.LogLevel `json:"log-level"`
|
LogLevel log.LogLevel `json:"log-level"`
|
||||||
IPv6 bool `json:"ipv6"`
|
IPv6 bool `json:"ipv6"`
|
||||||
Interface string `json:"interface-name"`
|
Interface string `json:"interface-name"`
|
||||||
RoutingMark int `json:"routing-mark"`
|
RoutingMark int `json:"routing-mark"`
|
||||||
GeoXUrl GeoXUrl `json:"geox-url"`
|
GeoXUrl GeoXUrl `json:"geox-url"`
|
||||||
GeoAutoUpdate bool `json:"geo-auto-update"`
|
GeoAutoUpdate bool `json:"geo-auto-update"`
|
||||||
GeoUpdateInterval int `json:"geo-update-interval"`
|
GeoUpdateInterval int `json:"geo-update-interval"`
|
||||||
GeodataMode bool `json:"geodata-mode"`
|
GeodataMode bool `json:"geodata-mode"`
|
||||||
GeodataLoader string `json:"geodata-loader"`
|
GeodataLoader string `json:"geodata-loader"`
|
||||||
GeositeMatcher string `json:"geosite-matcher"`
|
GeositeMatcher string `json:"geosite-matcher"`
|
||||||
TCPConcurrent bool `json:"tcp-concurrent"`
|
TCPConcurrent bool `json:"tcp-concurrent"`
|
||||||
FindProcessMode P.FindProcessMode `json:"find-process-mode"`
|
FindProcessMode process.FindProcessMode `json:"find-process-mode"`
|
||||||
Sniffing bool `json:"sniffing"`
|
Sniffing bool `json:"sniffing"`
|
||||||
GlobalClientFingerprint string `json:"global-client-fingerprint"`
|
GlobalClientFingerprint string `json:"global-client-fingerprint"`
|
||||||
GlobalUA string `json:"global-ua"`
|
GlobalUA string `json:"global-ua"`
|
||||||
ETagSupport bool `json:"etag-support"`
|
ETagSupport bool `json:"etag-support"`
|
||||||
KeepAliveIdle int `json:"keep-alive-idle"`
|
KeepAliveIdle int `json:"keep-alive-idle"`
|
||||||
KeepAliveInterval int `json:"keep-alive-interval"`
|
KeepAliveInterval int `json:"keep-alive-interval"`
|
||||||
DisableKeepAlive bool `json:"disable-keep-alive"`
|
DisableKeepAlive bool `json:"disable-keep-alive"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inbound config
|
// Inbound config
|
||||||
@@ -146,6 +146,7 @@ type DNS struct {
|
|||||||
PreferH3 bool
|
PreferH3 bool
|
||||||
IPv6 bool
|
IPv6 bool
|
||||||
IPv6Timeout uint
|
IPv6Timeout uint
|
||||||
|
UseHosts bool
|
||||||
UseSystemHosts bool
|
UseSystemHosts bool
|
||||||
NameServer []dns.NameServer
|
NameServer []dns.NameServer
|
||||||
Fallback []dns.NameServer
|
Fallback []dns.NameServer
|
||||||
@@ -156,8 +157,12 @@ type DNS struct {
|
|||||||
DefaultNameserver []dns.NameServer
|
DefaultNameserver []dns.NameServer
|
||||||
CacheAlgorithm string
|
CacheAlgorithm string
|
||||||
CacheMaxSize int
|
CacheMaxSize int
|
||||||
FakeIPRange *fakeip.Pool
|
FakeIPRange netip.Prefix
|
||||||
Hosts *trie.DomainTrie[resolver.HostValue]
|
FakeIPPool *fakeip.Pool
|
||||||
|
FakeIPRange6 netip.Prefix
|
||||||
|
FakeIPPool6 *fakeip.Pool
|
||||||
|
FakeIPSkipper *fakeip.Skipper
|
||||||
|
FakeIPTTL int
|
||||||
NameServerPolicy []dns.Policy
|
NameServerPolicy []dns.Policy
|
||||||
ProxyServerNameserver []dns.NameServer
|
ProxyServerNameserver []dns.NameServer
|
||||||
DirectNameServer []dns.NameServer
|
DirectNameServer []dns.NameServer
|
||||||
@@ -195,8 +200,8 @@ type Config struct {
|
|||||||
Users []auth.AuthUser
|
Users []auth.AuthUser
|
||||||
Proxies map[string]C.Proxy
|
Proxies map[string]C.Proxy
|
||||||
Listeners map[string]C.InboundListener
|
Listeners map[string]C.InboundListener
|
||||||
Providers map[string]providerTypes.ProxyProvider
|
Providers map[string]P.ProxyProvider
|
||||||
RuleProviders map[string]providerTypes.RuleProvider
|
RuleProviders map[string]P.RuleProvider
|
||||||
Tunnels []LC.Tunnel
|
Tunnels []LC.Tunnel
|
||||||
Sniffer *sniffer.Config
|
Sniffer *sniffer.Config
|
||||||
TLS *TLS
|
TLS *TLS
|
||||||
@@ -221,8 +226,10 @@ type RawDNS struct {
|
|||||||
Listen string `yaml:"listen" json:"listen"`
|
Listen string `yaml:"listen" json:"listen"`
|
||||||
EnhancedMode C.DNSMode `yaml:"enhanced-mode" json:"enhanced-mode"`
|
EnhancedMode C.DNSMode `yaml:"enhanced-mode" json:"enhanced-mode"`
|
||||||
FakeIPRange string `yaml:"fake-ip-range" json:"fake-ip-range"`
|
FakeIPRange string `yaml:"fake-ip-range" json:"fake-ip-range"`
|
||||||
|
FakeIPRange6 string `yaml:"fake-ip-range6" json:"fake-ip-range6"`
|
||||||
FakeIPFilter []string `yaml:"fake-ip-filter" json:"fake-ip-filter"`
|
FakeIPFilter []string `yaml:"fake-ip-filter" json:"fake-ip-filter"`
|
||||||
FakeIPFilterMode C.FilterMode `yaml:"fake-ip-filter-mode" json:"fake-ip-filter-mode"`
|
FakeIPFilterMode C.FilterMode `yaml:"fake-ip-filter-mode" json:"fake-ip-filter-mode"`
|
||||||
|
FakeIPTTL int `yaml:"fake-ip-ttl" json:"fake-ip-ttl"`
|
||||||
DefaultNameserver []string `yaml:"default-nameserver" json:"default-nameserver"`
|
DefaultNameserver []string `yaml:"default-nameserver" json:"default-nameserver"`
|
||||||
CacheAlgorithm string `yaml:"cache-algorithm" json:"cache-algorithm"`
|
CacheAlgorithm string `yaml:"cache-algorithm" json:"cache-algorithm"`
|
||||||
CacheMaxSize int `yaml:"cache-max-size" json:"cache-max-size"`
|
CacheMaxSize int `yaml:"cache-max-size" json:"cache-max-size"`
|
||||||
@@ -377,51 +384,51 @@ type RawTLS struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type RawConfig struct {
|
type RawConfig struct {
|
||||||
Port int `yaml:"port" json:"port"`
|
Port int `yaml:"port" json:"port"`
|
||||||
SocksPort int `yaml:"socks-port" json:"socks-port"`
|
SocksPort int `yaml:"socks-port" json:"socks-port"`
|
||||||
RedirPort int `yaml:"redir-port" json:"redir-port"`
|
RedirPort int `yaml:"redir-port" json:"redir-port"`
|
||||||
TProxyPort int `yaml:"tproxy-port" json:"tproxy-port"`
|
TProxyPort int `yaml:"tproxy-port" json:"tproxy-port"`
|
||||||
MixedPort int `yaml:"mixed-port" json:"mixed-port"`
|
MixedPort int `yaml:"mixed-port" json:"mixed-port"`
|
||||||
ShadowSocksConfig string `yaml:"ss-config" json:"ss-config"`
|
ShadowSocksConfig string `yaml:"ss-config" json:"ss-config"`
|
||||||
VmessConfig string `yaml:"vmess-config" json:"vmess-config"`
|
VmessConfig string `yaml:"vmess-config" json:"vmess-config"`
|
||||||
InboundTfo bool `yaml:"inbound-tfo" json:"inbound-tfo"`
|
InboundTfo bool `yaml:"inbound-tfo" json:"inbound-tfo"`
|
||||||
InboundMPTCP bool `yaml:"inbound-mptcp" json:"inbound-mptcp"`
|
InboundMPTCP bool `yaml:"inbound-mptcp" json:"inbound-mptcp"`
|
||||||
Authentication []string `yaml:"authentication" json:"authentication"`
|
Authentication []string `yaml:"authentication" json:"authentication"`
|
||||||
SkipAuthPrefixes []netip.Prefix `yaml:"skip-auth-prefixes" json:"skip-auth-prefixes"`
|
SkipAuthPrefixes []netip.Prefix `yaml:"skip-auth-prefixes" json:"skip-auth-prefixes"`
|
||||||
LanAllowedIPs []netip.Prefix `yaml:"lan-allowed-ips" json:"lan-allowed-ips"`
|
LanAllowedIPs []netip.Prefix `yaml:"lan-allowed-ips" json:"lan-allowed-ips"`
|
||||||
LanDisAllowedIPs []netip.Prefix `yaml:"lan-disallowed-ips" json:"lan-disallowed-ips"`
|
LanDisAllowedIPs []netip.Prefix `yaml:"lan-disallowed-ips" json:"lan-disallowed-ips"`
|
||||||
AllowLan bool `yaml:"allow-lan" json:"allow-lan"`
|
AllowLan bool `yaml:"allow-lan" json:"allow-lan"`
|
||||||
BindAddress string `yaml:"bind-address" json:"bind-address"`
|
BindAddress string `yaml:"bind-address" json:"bind-address"`
|
||||||
Mode T.TunnelMode `yaml:"mode" json:"mode"`
|
Mode T.TunnelMode `yaml:"mode" json:"mode"`
|
||||||
UnifiedDelay bool `yaml:"unified-delay" json:"unified-delay"`
|
UnifiedDelay bool `yaml:"unified-delay" json:"unified-delay"`
|
||||||
LogLevel log.LogLevel `yaml:"log-level" json:"log-level"`
|
LogLevel log.LogLevel `yaml:"log-level" json:"log-level"`
|
||||||
IPv6 bool `yaml:"ipv6" json:"ipv6"`
|
IPv6 bool `yaml:"ipv6" json:"ipv6"`
|
||||||
ExternalController string `yaml:"external-controller" json:"external-controller"`
|
ExternalController string `yaml:"external-controller" json:"external-controller"`
|
||||||
ExternalControllerPipe string `yaml:"external-controller-pipe" json:"external-controller-pipe"`
|
ExternalControllerPipe string `yaml:"external-controller-pipe" json:"external-controller-pipe"`
|
||||||
ExternalControllerUnix string `yaml:"external-controller-unix" json:"external-controller-unix"`
|
ExternalControllerUnix string `yaml:"external-controller-unix" json:"external-controller-unix"`
|
||||||
ExternalControllerTLS string `yaml:"external-controller-tls" json:"external-controller-tls"`
|
ExternalControllerTLS string `yaml:"external-controller-tls" json:"external-controller-tls"`
|
||||||
ExternalControllerCors RawCors `yaml:"external-controller-cors" json:"external-controller-cors"`
|
ExternalControllerCors RawCors `yaml:"external-controller-cors" json:"external-controller-cors"`
|
||||||
ExternalUI string `yaml:"external-ui" json:"external-ui"`
|
ExternalUI string `yaml:"external-ui" json:"external-ui"`
|
||||||
ExternalUIURL string `yaml:"external-ui-url" json:"external-ui-url"`
|
ExternalUIURL string `yaml:"external-ui-url" json:"external-ui-url"`
|
||||||
ExternalUIName string `yaml:"external-ui-name" json:"external-ui-name"`
|
ExternalUIName string `yaml:"external-ui-name" json:"external-ui-name"`
|
||||||
ExternalDohServer string `yaml:"external-doh-server" json:"external-doh-server"`
|
ExternalDohServer string `yaml:"external-doh-server" json:"external-doh-server"`
|
||||||
Secret string `yaml:"secret" json:"secret"`
|
Secret string `yaml:"secret" json:"secret"`
|
||||||
Interface string `yaml:"interface-name" json:"interface-name"`
|
Interface string `yaml:"interface-name" json:"interface-name"`
|
||||||
RoutingMark int `yaml:"routing-mark" json:"routing-mark"`
|
RoutingMark int `yaml:"routing-mark" json:"routing-mark"`
|
||||||
Tunnels []LC.Tunnel `yaml:"tunnels" json:"tunnels"`
|
Tunnels []LC.Tunnel `yaml:"tunnels" json:"tunnels"`
|
||||||
GeoAutoUpdate bool `yaml:"geo-auto-update" json:"geo-auto-update"`
|
GeoAutoUpdate bool `yaml:"geo-auto-update" json:"geo-auto-update"`
|
||||||
GeoUpdateInterval int `yaml:"geo-update-interval" json:"geo-update-interval"`
|
GeoUpdateInterval int `yaml:"geo-update-interval" json:"geo-update-interval"`
|
||||||
GeodataMode bool `yaml:"geodata-mode" json:"geodata-mode"`
|
GeodataMode bool `yaml:"geodata-mode" json:"geodata-mode"`
|
||||||
GeodataLoader string `yaml:"geodata-loader" json:"geodata-loader"`
|
GeodataLoader string `yaml:"geodata-loader" json:"geodata-loader"`
|
||||||
GeositeMatcher string `yaml:"geosite-matcher" json:"geosite-matcher"`
|
GeositeMatcher string `yaml:"geosite-matcher" json:"geosite-matcher"`
|
||||||
TCPConcurrent bool `yaml:"tcp-concurrent" json:"tcp-concurrent"`
|
TCPConcurrent bool `yaml:"tcp-concurrent" json:"tcp-concurrent"`
|
||||||
FindProcessMode P.FindProcessMode `yaml:"find-process-mode" json:"find-process-mode"`
|
FindProcessMode process.FindProcessMode `yaml:"find-process-mode" json:"find-process-mode"`
|
||||||
GlobalClientFingerprint string `yaml:"global-client-fingerprint" json:"global-client-fingerprint"`
|
GlobalClientFingerprint string `yaml:"global-client-fingerprint" json:"global-client-fingerprint"`
|
||||||
GlobalUA string `yaml:"global-ua" json:"global-ua"`
|
GlobalUA string `yaml:"global-ua" json:"global-ua"`
|
||||||
ETagSupport bool `yaml:"etag-support" json:"etag-support"`
|
ETagSupport bool `yaml:"etag-support" json:"etag-support"`
|
||||||
KeepAliveIdle int `yaml:"keep-alive-idle" json:"keep-alive-idle"`
|
KeepAliveIdle int `yaml:"keep-alive-idle" json:"keep-alive-idle"`
|
||||||
KeepAliveInterval int `yaml:"keep-alive-interval" json:"keep-alive-interval"`
|
KeepAliveInterval int `yaml:"keep-alive-interval" json:"keep-alive-interval"`
|
||||||
DisableKeepAlive bool `yaml:"disable-keep-alive" json:"disable-keep-alive"`
|
DisableKeepAlive bool `yaml:"disable-keep-alive" json:"disable-keep-alive"`
|
||||||
|
|
||||||
ProxyProvider map[string]map[string]any `yaml:"proxy-providers" json:"proxy-providers"`
|
ProxyProvider map[string]map[string]any `yaml:"proxy-providers" json:"proxy-providers"`
|
||||||
RuleProvider map[string]map[string]any `yaml:"rule-providers" json:"rule-providers"`
|
RuleProvider map[string]map[string]any `yaml:"rule-providers" json:"rule-providers"`
|
||||||
@@ -474,7 +481,7 @@ func DefaultRawConfig() *RawConfig {
|
|||||||
Proxy: []map[string]any{},
|
Proxy: []map[string]any{},
|
||||||
ProxyGroup: []map[string]any{},
|
ProxyGroup: []map[string]any{},
|
||||||
TCPConcurrent: false,
|
TCPConcurrent: false,
|
||||||
FindProcessMode: P.FindProcessStrict,
|
FindProcessMode: process.FindProcessStrict,
|
||||||
GlobalUA: "clash.meta/" + C.Version,
|
GlobalUA: "clash.meta/" + C.Version,
|
||||||
ETagSupport: true,
|
ETagSupport: true,
|
||||||
DNS: RawDNS{
|
DNS: RawDNS{
|
||||||
@@ -485,6 +492,7 @@ func DefaultRawConfig() *RawConfig {
|
|||||||
IPv6Timeout: 100,
|
IPv6Timeout: 100,
|
||||||
EnhancedMode: C.DNSMapping,
|
EnhancedMode: C.DNSMapping,
|
||||||
FakeIPRange: "198.18.0.1/16",
|
FakeIPRange: "198.18.0.1/16",
|
||||||
|
FakeIPTTL: 1,
|
||||||
FallbackFilter: RawFallbackFilter{
|
FallbackFilter: RawFallbackFilter{
|
||||||
GeoIP: true,
|
GeoIP: true,
|
||||||
GeoIPCode: "CN",
|
GeoIPCode: "CN",
|
||||||
@@ -648,11 +656,11 @@ func ParseRawConfig(rawCfg *RawConfig) (*Config, error) {
|
|||||||
config.Proxies = proxies
|
config.Proxies = proxies
|
||||||
config.Providers = providers
|
config.Providers = providers
|
||||||
|
|
||||||
listener, err := parseListeners(rawCfg)
|
listeners, err := parseListeners(rawCfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
config.Listeners = listener
|
config.Listeners = listeners
|
||||||
|
|
||||||
log.Infoln("Geodata Loader mode: %s", geodata.LoaderName())
|
log.Infoln("Geodata Loader mode: %s", geodata.LoaderName())
|
||||||
log.Infoln("Geosite Matcher implementation: %s", geodata.SiteMatcherName())
|
log.Infoln("Geosite Matcher implementation: %s", geodata.SiteMatcherName())
|
||||||
@@ -680,13 +688,15 @@ func ParseRawConfig(rawCfg *RawConfig) (*Config, error) {
|
|||||||
}
|
}
|
||||||
config.Hosts = hosts
|
config.Hosts = hosts
|
||||||
|
|
||||||
dnsCfg, err := parseDNS(rawCfg, hosts, ruleProviders)
|
parseIPV6(rawCfg) // must before DNS and Tun
|
||||||
|
|
||||||
|
dnsCfg, err := parseDNS(rawCfg, ruleProviders)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
config.DNS = dnsCfg
|
config.DNS = dnsCfg
|
||||||
|
|
||||||
err = parseTun(rawCfg.Tun, config.General)
|
err = parseTun(rawCfg.Tun, dnsCfg, config.General)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -838,9 +848,9 @@ func parseTLS(cfg *RawConfig) (*TLS, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[string]providerTypes.ProxyProvider, err error) {
|
func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[string]P.ProxyProvider, err error) {
|
||||||
proxies = make(map[string]C.Proxy)
|
proxies = make(map[string]C.Proxy)
|
||||||
providersMap = make(map[string]providerTypes.ProxyProvider)
|
providersMap = make(map[string]P.ProxyProvider)
|
||||||
proxiesConfig := cfg.Proxy
|
proxiesConfig := cfg.Proxy
|
||||||
groupsConfig := cfg.ProxyGroup
|
groupsConfig := cfg.ProxyGroup
|
||||||
providersConfig := cfg.ProxyProvider
|
providersConfig := cfg.ProxyProvider
|
||||||
@@ -940,7 +950,7 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[
|
|||||||
&outboundgroup.GroupCommonOption{
|
&outboundgroup.GroupCommonOption{
|
||||||
Name: "GLOBAL",
|
Name: "GLOBAL",
|
||||||
},
|
},
|
||||||
[]providerTypes.ProxyProvider{pd},
|
[]P.ProxyProvider{pd},
|
||||||
)
|
)
|
||||||
proxies["GLOBAL"] = adapter.NewProxy(global)
|
proxies["GLOBAL"] = adapter.NewProxy(global)
|
||||||
}
|
}
|
||||||
@@ -950,24 +960,25 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[
|
|||||||
func parseListeners(cfg *RawConfig) (listeners map[string]C.InboundListener, err error) {
|
func parseListeners(cfg *RawConfig) (listeners map[string]C.InboundListener, err error) {
|
||||||
listeners = make(map[string]C.InboundListener)
|
listeners = make(map[string]C.InboundListener)
|
||||||
for index, mapping := range cfg.Listeners {
|
for index, mapping := range cfg.Listeners {
|
||||||
listener, err := L.ParseListener(mapping)
|
inboundListener, err := listener.ParseListener(mapping)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("proxy %d: %w", index, err)
|
return nil, fmt.Errorf("proxy %d: %w", index, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, exist := mapping[listener.Name()]; exist {
|
name := inboundListener.Name()
|
||||||
return nil, fmt.Errorf("listener %s is the duplicate name", listener.Name())
|
if _, exist := mapping[name]; exist {
|
||||||
|
return nil, fmt.Errorf("listener %s is the duplicate name", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
listeners[listener.Name()] = listener
|
listeners[name] = inboundListener
|
||||||
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseRuleProviders(cfg *RawConfig) (ruleProviders map[string]providerTypes.RuleProvider, err error) {
|
func parseRuleProviders(cfg *RawConfig) (ruleProviders map[string]P.RuleProvider, err error) {
|
||||||
RP.SetTunnel(T.Tunnel)
|
RP.SetTunnel(T.Tunnel)
|
||||||
ruleProviders = map[string]providerTypes.RuleProvider{}
|
ruleProviders = map[string]P.RuleProvider{}
|
||||||
// parse rule provider
|
// parse rule provider
|
||||||
for name, mapping := range cfg.RuleProvider {
|
for name, mapping := range cfg.RuleProvider {
|
||||||
rp, err := RP.ParseRuleProvider(name, mapping, R.ParseRule)
|
rp, err := RP.ParseRuleProvider(name, mapping, R.ParseRule)
|
||||||
@@ -980,7 +991,7 @@ func parseRuleProviders(cfg *RawConfig) (ruleProviders map[string]providerTypes.
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseSubRules(cfg *RawConfig, proxies map[string]C.Proxy, ruleProviders map[string]providerTypes.RuleProvider) (subRules map[string][]C.Rule, err error) {
|
func parseSubRules(cfg *RawConfig, proxies map[string]C.Proxy, ruleProviders map[string]P.RuleProvider) (subRules map[string][]C.Rule, err error) {
|
||||||
subRules = map[string][]C.Rule{}
|
subRules = map[string][]C.Rule{}
|
||||||
for name := range cfg.SubRules {
|
for name := range cfg.SubRules {
|
||||||
subRules[name] = make([]C.Rule, 0)
|
subRules[name] = make([]C.Rule, 0)
|
||||||
@@ -1043,7 +1054,7 @@ func verifySubRuleCircularReferences(n string, subRules map[string][]C.Rule, arr
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseRules(rulesConfig []string, proxies map[string]C.Proxy, ruleProviders map[string]providerTypes.RuleProvider, subRules map[string][]C.Rule, format string) ([]C.Rule, error) {
|
func parseRules(rulesConfig []string, proxies map[string]C.Proxy, ruleProviders map[string]P.RuleProvider, subRules map[string][]C.Rule, format string) ([]C.Rule, error) {
|
||||||
var rules []C.Rule
|
var rules []C.Rule
|
||||||
|
|
||||||
// parse rules
|
// parse rules
|
||||||
@@ -1266,7 +1277,7 @@ func parsePureDNSServer(server string) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseNameServerPolicy(nsPolicy *orderedmap.OrderedMap[string, any], ruleProviders map[string]providerTypes.RuleProvider, respectRules bool, preferH3 bool) ([]dns.Policy, error) {
|
func parseNameServerPolicy(nsPolicy *orderedmap.OrderedMap[string, any], ruleProviders map[string]P.RuleProvider, respectRules bool, preferH3 bool) ([]dns.Policy, error) {
|
||||||
var policy []dns.Policy
|
var policy []dns.Policy
|
||||||
|
|
||||||
for pair := nsPolicy.Oldest(); pair != nil; pair = pair.Next() {
|
for pair := nsPolicy.Oldest(); pair != nil; pair = pair.Next() {
|
||||||
@@ -1281,7 +1292,7 @@ func parseNameServerPolicy(nsPolicy *orderedmap.OrderedMap[string, any], rulePro
|
|||||||
}
|
}
|
||||||
kLower := strings.ToLower(k)
|
kLower := strings.ToLower(k)
|
||||||
if strings.Contains(kLower, ",") {
|
if strings.Contains(kLower, ",") {
|
||||||
if strings.Contains(kLower, "geosite:") {
|
if strings.HasPrefix(kLower, "geosite:") {
|
||||||
subkeys := strings.Split(k, ":")
|
subkeys := strings.Split(k, ":")
|
||||||
subkeys = subkeys[1:]
|
subkeys = subkeys[1:]
|
||||||
subkeys = strings.Split(subkeys[0], ",")
|
subkeys = strings.Split(subkeys[0], ",")
|
||||||
@@ -1289,7 +1300,7 @@ func parseNameServerPolicy(nsPolicy *orderedmap.OrderedMap[string, any], rulePro
|
|||||||
newKey := "geosite:" + subkey
|
newKey := "geosite:" + subkey
|
||||||
policy = append(policy, dns.Policy{Domain: newKey, NameServers: nameservers})
|
policy = append(policy, dns.Policy{Domain: newKey, NameServers: nameservers})
|
||||||
}
|
}
|
||||||
} else if strings.Contains(kLower, "rule-set:") {
|
} else if strings.HasPrefix(kLower, "rule-set:") {
|
||||||
subkeys := strings.Split(k, ":")
|
subkeys := strings.Split(k, ":")
|
||||||
subkeys = subkeys[1:]
|
subkeys = subkeys[1:]
|
||||||
subkeys = strings.Split(subkeys[0], ",")
|
subkeys = strings.Split(subkeys[0], ",")
|
||||||
@@ -1304,9 +1315,9 @@ func parseNameServerPolicy(nsPolicy *orderedmap.OrderedMap[string, any], rulePro
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if strings.Contains(kLower, "geosite:") {
|
if strings.HasPrefix(kLower, "geosite:") {
|
||||||
policy = append(policy, dns.Policy{Domain: "geosite:" + k[8:], NameServers: nameservers})
|
policy = append(policy, dns.Policy{Domain: "geosite:" + k[8:], NameServers: nameservers})
|
||||||
} else if strings.Contains(kLower, "rule-set:") {
|
} else if strings.HasPrefix(kLower, "rule-set:") {
|
||||||
policy = append(policy, dns.Policy{Domain: "rule-set:" + k[9:], NameServers: nameservers})
|
policy = append(policy, dns.Policy{Domain: "rule-set:" + k[9:], NameServers: nameservers})
|
||||||
} else {
|
} else {
|
||||||
policy = append(policy, dns.Policy{Domain: k, NameServers: nameservers})
|
policy = append(policy, dns.Policy{Domain: k, NameServers: nameservers})
|
||||||
@@ -1341,7 +1352,7 @@ func parseNameServerPolicy(nsPolicy *orderedmap.OrderedMap[string, any], rulePro
|
|||||||
return policy, nil
|
return policy, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseDNS(rawCfg *RawConfig, hosts *trie.DomainTrie[resolver.HostValue], ruleProviders map[string]providerTypes.RuleProvider) (*DNS, error) {
|
func parseDNS(rawCfg *RawConfig, ruleProviders map[string]P.RuleProvider) (*DNS, error) {
|
||||||
cfg := rawCfg.DNS
|
cfg := rawCfg.DNS
|
||||||
if cfg.Enable && len(cfg.NameServer) == 0 {
|
if cfg.Enable && len(cfg.NameServer) == 0 {
|
||||||
return nil, fmt.Errorf("if DNS configuration is turned on, NameServer cannot be empty")
|
return nil, fmt.Errorf("if DNS configuration is turned on, NameServer cannot be empty")
|
||||||
@@ -1357,6 +1368,7 @@ func parseDNS(rawCfg *RawConfig, hosts *trie.DomainTrie[resolver.HostValue], rul
|
|||||||
PreferH3: cfg.PreferH3,
|
PreferH3: cfg.PreferH3,
|
||||||
IPv6Timeout: cfg.IPv6Timeout,
|
IPv6Timeout: cfg.IPv6Timeout,
|
||||||
IPv6: cfg.IPv6,
|
IPv6: cfg.IPv6,
|
||||||
|
UseHosts: cfg.UseHosts,
|
||||||
UseSystemHosts: cfg.UseSystemHosts,
|
UseSystemHosts: cfg.UseSystemHosts,
|
||||||
EnhancedMode: cfg.EnhancedMode,
|
EnhancedMode: cfg.EnhancedMode,
|
||||||
CacheAlgorithm: cfg.CacheAlgorithm,
|
CacheAlgorithm: cfg.CacheAlgorithm,
|
||||||
@@ -1406,13 +1418,27 @@ func parseDNS(rawCfg *RawConfig, hosts *trie.DomainTrie[resolver.HostValue], rul
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fakeIPRange, err := netip.ParsePrefix(cfg.FakeIPRange)
|
if cfg.FakeIPRange != "" {
|
||||||
T.SetFakeIPRange(fakeIPRange)
|
dnsCfg.FakeIPRange, err = netip.ParsePrefix(cfg.FakeIPRange)
|
||||||
if cfg.EnhancedMode == C.DNSFakeIP {
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if !dnsCfg.FakeIPRange.Addr().Is4() {
|
||||||
|
return nil, errors.New("dns.fake-ip-range must be a IPv4 prefix")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.FakeIPRange6 != "" {
|
||||||
|
dnsCfg.FakeIPRange6, err = netip.ParsePrefix(cfg.FakeIPRange6)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !dnsCfg.FakeIPRange6.Addr().Is6() {
|
||||||
|
return nil, errors.New("dns.fake-ip-range6 must be a IPv6 prefix")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.EnhancedMode == C.DNSFakeIP {
|
||||||
var fakeIPTrie *trie.DomainTrie[struct{}]
|
var fakeIPTrie *trie.DomainTrie[struct{}]
|
||||||
if len(dnsCfg.Fallback) != 0 {
|
if len(dnsCfg.Fallback) != 0 {
|
||||||
fakeIPTrie = trie.New[struct{}]()
|
fakeIPTrie = trie.New[struct{}]()
|
||||||
@@ -1424,24 +1450,52 @@ func parseDNS(rawCfg *RawConfig, hosts *trie.DomainTrie[resolver.HostValue], rul
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// fake ip skip host filter
|
skipper := &fakeip.Skipper{Mode: cfg.FakeIPFilterMode}
|
||||||
host, err := parseDomain(cfg.FakeIPFilter, fakeIPTrie, "dns.fake-ip-filter", ruleProviders)
|
|
||||||
if err != nil {
|
if cfg.FakeIPFilterMode == C.FilterRule {
|
||||||
return nil, err
|
rules, err := parseFakeIPRules(cfg.FakeIPFilter, ruleProviders)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
skipper.Rules = rules
|
||||||
|
} else {
|
||||||
|
host, err := parseDomain(cfg.FakeIPFilter, fakeIPTrie, "dns.fake-ip-filter", ruleProviders)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
skipper.Host = host
|
||||||
}
|
}
|
||||||
|
|
||||||
pool, err := fakeip.New(fakeip.Options{
|
dnsCfg.FakeIPSkipper = skipper
|
||||||
IPNet: fakeIPRange,
|
dnsCfg.FakeIPTTL = cfg.FakeIPTTL
|
||||||
Size: 1000,
|
|
||||||
Host: host,
|
if dnsCfg.FakeIPRange.IsValid() {
|
||||||
Mode: cfg.FakeIPFilterMode,
|
pool, err := fakeip.New(fakeip.Options{
|
||||||
Persistence: rawCfg.Profile.StoreFakeIP,
|
IPNet: dnsCfg.FakeIPRange,
|
||||||
})
|
Size: 1000,
|
||||||
if err != nil {
|
Persistence: rawCfg.Profile.StoreFakeIP,
|
||||||
return nil, err
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
dnsCfg.FakeIPPool = pool
|
||||||
}
|
}
|
||||||
|
|
||||||
dnsCfg.FakeIPRange = pool
|
if dnsCfg.FakeIPRange6.IsValid() {
|
||||||
|
pool6, err := fakeip.New(fakeip.Options{
|
||||||
|
IPNet: dnsCfg.FakeIPRange6,
|
||||||
|
Size: 1000,
|
||||||
|
Persistence: rawCfg.Profile.StoreFakeIP,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
dnsCfg.FakeIPPool6 = pool6
|
||||||
|
}
|
||||||
|
|
||||||
|
if dnsCfg.FakeIPPool == nil && dnsCfg.FakeIPPool6 == nil {
|
||||||
|
return nil, errors.New("disallow `fake-ip-range` and `fake-ip-range6` both empty with fake-ip mode")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(cfg.Fallback) != 0 {
|
if len(cfg.Fallback) != 0 {
|
||||||
@@ -1490,11 +1544,56 @@ func parseDNS(rawCfg *RawConfig, hosts *trie.DomainTrie[resolver.HostValue], rul
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.UseHosts {
|
return dnsCfg, nil
|
||||||
dnsCfg.Hosts = hosts
|
}
|
||||||
|
|
||||||
|
func parseFakeIPRules(rawRules []string, ruleProviders map[string]P.RuleProvider) ([]C.Rule, error) {
|
||||||
|
var rules []C.Rule
|
||||||
|
|
||||||
|
for idx, line := range rawRules {
|
||||||
|
tp, payload, action, params := RC.ParseRulePayload(line, true)
|
||||||
|
|
||||||
|
action = strings.ToLower(action)
|
||||||
|
if action != fakeip.UseFakeIP && action != fakeip.UseRealIP {
|
||||||
|
return nil, fmt.Errorf("dns.fake-ip-filter[%d] [%s] error: invalid action '%s', must be 'fake-ip' or 'real-ip'", idx, line, action)
|
||||||
|
}
|
||||||
|
|
||||||
|
if tp == "RULE-SET" {
|
||||||
|
if rp, ok := ruleProviders[payload]; !ok {
|
||||||
|
return nil, fmt.Errorf("dns.fake-ip-filter[%d] [%s] error: rule-set '%s' not found", idx, line, payload)
|
||||||
|
} else {
|
||||||
|
switch rp.Behavior() {
|
||||||
|
case P.IPCIDR:
|
||||||
|
return nil, fmt.Errorf("dns.fake-ip-filter[%d] [%s] error: rule-set behavior is %s, must be domain or classical", idx, line, rp.Behavior())
|
||||||
|
case P.Classical:
|
||||||
|
log.Warnln("%s provider is %s, only matching domain rules in fake-ip-filter", rp.Name(), rp.Behavior())
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parsed, err := R.ParseRule(tp, payload, action, params, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("dns.fake-ip-filter[%d] [%s] error: %w", idx, line, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isDomainRule(parsed.RuleType()) && parsed.RuleType() != C.MATCH {
|
||||||
|
return nil, fmt.Errorf("dns.fake-ip-filter[%d] [%s] error: rule type '%s' not supported, only domain-based rules allowed", idx, line, tp)
|
||||||
|
}
|
||||||
|
|
||||||
|
rules = append(rules, parsed)
|
||||||
}
|
}
|
||||||
|
|
||||||
return dnsCfg, nil
|
return rules, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func isDomainRule(rt C.RuleType) bool {
|
||||||
|
switch rt {
|
||||||
|
case C.Domain, C.DomainSuffix, C.DomainKeyword, C.DomainRegex, C.DomainWildcard, C.GEOSITE, C.RuleSet:
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseAuthentication(rawRecords []string) []auth.AuthUser {
|
func parseAuthentication(rawRecords []string) []auth.AuthUser {
|
||||||
@@ -1507,17 +1606,20 @@ func parseAuthentication(rawRecords []string) []auth.AuthUser {
|
|||||||
return users
|
return users
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseTun(rawTun RawTun, general *General) error {
|
func parseIPV6(rawCfg *RawConfig) {
|
||||||
tunAddressPrefix := T.FakeIPRange()
|
if !rawCfg.IPv6 || !verifyIP6() {
|
||||||
|
rawCfg.DNS.FakeIPRange6 = ""
|
||||||
|
rawCfg.Tun.Inet6Address = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseTun(rawTun RawTun, dns *DNS, general *General) error {
|
||||||
|
tunAddressPrefix := dns.FakeIPRange
|
||||||
if !tunAddressPrefix.IsValid() {
|
if !tunAddressPrefix.IsValid() {
|
||||||
tunAddressPrefix = netip.MustParsePrefix("198.18.0.1/16")
|
tunAddressPrefix = netip.MustParsePrefix("198.18.0.1/16")
|
||||||
}
|
}
|
||||||
tunAddressPrefix = netip.PrefixFrom(tunAddressPrefix.Addr(), 30)
|
tunAddressPrefix = netip.PrefixFrom(tunAddressPrefix.Addr(), 30)
|
||||||
|
|
||||||
if !general.IPv6 || !verifyIP6() {
|
|
||||||
rawTun.Inet6Address = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
general.Tun = LC.Tun{
|
general.Tun = LC.Tun{
|
||||||
Enable: rawTun.Enable,
|
Enable: rawTun.Enable,
|
||||||
Device: rawTun.Device,
|
Device: rawTun.Device,
|
||||||
@@ -1590,7 +1692,7 @@ func parseTuicServer(rawTuic RawTuicServer, general *General) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseSniffer(snifferRaw RawSniffer, ruleProviders map[string]providerTypes.RuleProvider) (*sniffer.Config, error) {
|
func parseSniffer(snifferRaw RawSniffer, ruleProviders map[string]P.RuleProvider) (*sniffer.Config, error) {
|
||||||
snifferConfig := &sniffer.Config{
|
snifferConfig := &sniffer.Config{
|
||||||
Enable: snifferRaw.Enable,
|
Enable: snifferRaw.Enable,
|
||||||
ForceDnsMapping: snifferRaw.ForceDnsMapping,
|
ForceDnsMapping: snifferRaw.ForceDnsMapping,
|
||||||
@@ -1665,7 +1767,7 @@ func parseSniffer(snifferRaw RawSniffer, ruleProviders map[string]providerTypes.
|
|||||||
}
|
}
|
||||||
snifferConfig.SkipSrcAddress = skipSrcAddress
|
snifferConfig.SkipSrcAddress = skipSrcAddress
|
||||||
|
|
||||||
skipDstAddress, err := parseIPCIDR(snifferRaw.SkipDstAddress, nil, "sniffer.skip-src-address", ruleProviders)
|
skipDstAddress, err := parseIPCIDR(snifferRaw.SkipDstAddress, nil, "sniffer.skip-dst-address", ruleProviders)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error in skip-dst-address, error:%w", err)
|
return nil, fmt.Errorf("error in skip-dst-address, error:%w", err)
|
||||||
}
|
}
|
||||||
@@ -1680,11 +1782,11 @@ func parseSniffer(snifferRaw RawSniffer, ruleProviders map[string]providerTypes.
|
|||||||
return snifferConfig, nil
|
return snifferConfig, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseIPCIDR(addresses []string, cidrSet *cidr.IpCidrSet, adapterName string, ruleProviders map[string]providerTypes.RuleProvider) (matchers []C.IpMatcher, err error) {
|
func parseIPCIDR(addresses []string, cidrSet *cidr.IpCidrSet, adapterName string, ruleProviders map[string]P.RuleProvider) (matchers []C.IpMatcher, err error) {
|
||||||
var matcher C.IpMatcher
|
var matcher C.IpMatcher
|
||||||
for _, ipcidr := range addresses {
|
for _, ipcidr := range addresses {
|
||||||
ipcidrLower := strings.ToLower(ipcidr)
|
ipcidrLower := strings.ToLower(ipcidr)
|
||||||
if strings.Contains(ipcidrLower, "geoip:") {
|
if strings.HasPrefix(ipcidrLower, "geoip:") {
|
||||||
subkeys := strings.Split(ipcidr, ":")
|
subkeys := strings.Split(ipcidr, ":")
|
||||||
subkeys = subkeys[1:]
|
subkeys = subkeys[1:]
|
||||||
subkeys = strings.Split(subkeys[0], ",")
|
subkeys = strings.Split(subkeys[0], ",")
|
||||||
@@ -1695,7 +1797,7 @@ func parseIPCIDR(addresses []string, cidrSet *cidr.IpCidrSet, adapterName string
|
|||||||
}
|
}
|
||||||
matchers = append(matchers, matcher)
|
matchers = append(matchers, matcher)
|
||||||
}
|
}
|
||||||
} else if strings.Contains(ipcidrLower, "rule-set:") {
|
} else if strings.HasPrefix(ipcidrLower, "rule-set:") {
|
||||||
subkeys := strings.Split(ipcidr, ":")
|
subkeys := strings.Split(ipcidr, ":")
|
||||||
subkeys = subkeys[1:]
|
subkeys = subkeys[1:]
|
||||||
subkeys = strings.Split(subkeys[0], ",")
|
subkeys = strings.Split(subkeys[0], ",")
|
||||||
@@ -1727,11 +1829,11 @@ func parseIPCIDR(addresses []string, cidrSet *cidr.IpCidrSet, adapterName string
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseDomain(domains []string, domainTrie *trie.DomainTrie[struct{}], adapterName string, ruleProviders map[string]providerTypes.RuleProvider) (matchers []C.DomainMatcher, err error) {
|
func parseDomain(domains []string, domainTrie *trie.DomainTrie[struct{}], adapterName string, ruleProviders map[string]P.RuleProvider) (matchers []C.DomainMatcher, err error) {
|
||||||
var matcher C.DomainMatcher
|
var matcher C.DomainMatcher
|
||||||
for _, domain := range domains {
|
for _, domain := range domains {
|
||||||
domainLower := strings.ToLower(domain)
|
domainLower := strings.ToLower(domain)
|
||||||
if strings.Contains(domainLower, "geosite:") {
|
if strings.HasPrefix(domainLower, "geosite:") {
|
||||||
subkeys := strings.Split(domain, ":")
|
subkeys := strings.Split(domain, ":")
|
||||||
subkeys = subkeys[1:]
|
subkeys = subkeys[1:]
|
||||||
subkeys = strings.Split(subkeys[0], ",")
|
subkeys = strings.Split(subkeys[0], ",")
|
||||||
@@ -1742,7 +1844,7 @@ func parseDomain(domains []string, domainTrie *trie.DomainTrie[struct{}], adapte
|
|||||||
}
|
}
|
||||||
matchers = append(matchers, matcher)
|
matchers = append(matchers, matcher)
|
||||||
}
|
}
|
||||||
} else if strings.Contains(domainLower, "rule-set:") {
|
} else if strings.HasPrefix(domainLower, "rule-set:") {
|
||||||
subkeys := strings.Split(domain, ":")
|
subkeys := strings.Split(domain, ":")
|
||||||
subkeys = subkeys[1:]
|
subkeys = subkeys[1:]
|
||||||
subkeys = strings.Split(subkeys[0], ",")
|
subkeys = strings.Split(subkeys[0], ",")
|
||||||
@@ -1770,14 +1872,14 @@ func parseDomain(domains []string, domainTrie *trie.DomainTrie[struct{}], adapte
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseIPRuleSet(domainSetName string, adapterName string, ruleProviders map[string]providerTypes.RuleProvider) (C.IpMatcher, error) {
|
func parseIPRuleSet(domainSetName string, adapterName string, ruleProviders map[string]P.RuleProvider) (C.IpMatcher, error) {
|
||||||
if rp, ok := ruleProviders[domainSetName]; !ok {
|
if rp, ok := ruleProviders[domainSetName]; !ok {
|
||||||
return nil, fmt.Errorf("not found rule-set: %s", domainSetName)
|
return nil, fmt.Errorf("not found rule-set: %s", domainSetName)
|
||||||
} else {
|
} else {
|
||||||
switch rp.Behavior() {
|
switch rp.Behavior() {
|
||||||
case providerTypes.Domain:
|
case P.Domain:
|
||||||
return nil, fmt.Errorf("rule provider type error, except ipcidr,actual %s", rp.Behavior())
|
return nil, fmt.Errorf("rule provider type error, except ipcidr,actual %s", rp.Behavior())
|
||||||
case providerTypes.Classical:
|
case P.Classical:
|
||||||
log.Warnln("%s provider is %s, only matching it contain ip rule", rp.Name(), rp.Behavior())
|
log.Warnln("%s provider is %s, only matching it contain ip rule", rp.Name(), rp.Behavior())
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
@@ -1785,14 +1887,14 @@ func parseIPRuleSet(domainSetName string, adapterName string, ruleProviders map[
|
|||||||
return RP.NewRuleSet(domainSetName, adapterName, false, true)
|
return RP.NewRuleSet(domainSetName, adapterName, false, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseDomainRuleSet(domainSetName string, adapterName string, ruleProviders map[string]providerTypes.RuleProvider) (C.DomainMatcher, error) {
|
func parseDomainRuleSet(domainSetName string, adapterName string, ruleProviders map[string]P.RuleProvider) (C.DomainMatcher, error) {
|
||||||
if rp, ok := ruleProviders[domainSetName]; !ok {
|
if rp, ok := ruleProviders[domainSetName]; !ok {
|
||||||
return nil, fmt.Errorf("not found rule-set: %s", domainSetName)
|
return nil, fmt.Errorf("not found rule-set: %s", domainSetName)
|
||||||
} else {
|
} else {
|
||||||
switch rp.Behavior() {
|
switch rp.Behavior() {
|
||||||
case providerTypes.IPCIDR:
|
case P.IPCIDR:
|
||||||
return nil, fmt.Errorf("rule provider type error, except domain,actual %s", rp.Behavior())
|
return nil, fmt.Errorf("rule provider type error, except domain,actual %s", rp.Behavior())
|
||||||
case providerTypes.Classical:
|
case P.Classical:
|
||||||
log.Warnln("%s provider is %s, only matching it contain domain rule", rp.Name(), rp.Behavior())
|
log.Warnln("%s provider is %s, only matching it contain domain rule", rp.Name(), rp.Behavior())
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -155,6 +155,10 @@ func verifyIP6() bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// eg: Calling net.InterfaceAddrs() fails on Android SDK 30
|
||||||
|
// https://github.com/golang/go/issues/40569
|
||||||
|
return true // just ignore
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ const (
|
|||||||
Ssh
|
Ssh
|
||||||
Mieru
|
Mieru
|
||||||
AnyTLS
|
AnyTLS
|
||||||
|
Sudoku
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -58,6 +59,7 @@ var ErrNotSupport = errors.New("no support")
|
|||||||
|
|
||||||
type Connection interface {
|
type Connection interface {
|
||||||
Chains() Chain
|
Chains() Chain
|
||||||
|
ProviderChains() Chain
|
||||||
AppendToChains(adapter ProxyAdapter)
|
AppendToChains(adapter ProxyAdapter)
|
||||||
RemoteDestination() string
|
RemoteDestination() string
|
||||||
}
|
}
|
||||||
@@ -101,13 +103,14 @@ type Dialer interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ProxyInfo struct {
|
type ProxyInfo struct {
|
||||||
XUDP bool
|
XUDP bool
|
||||||
TFO bool
|
TFO bool
|
||||||
MPTCP bool
|
MPTCP bool
|
||||||
SMUX bool
|
SMUX bool
|
||||||
Interface string
|
Interface string
|
||||||
RoutingMark int
|
RoutingMark int
|
||||||
DialerProxy string
|
ProviderName string
|
||||||
|
DialerProxy string
|
||||||
}
|
}
|
||||||
|
|
||||||
type ProxyAdapter interface {
|
type ProxyAdapter interface {
|
||||||
@@ -120,17 +123,6 @@ type ProxyAdapter interface {
|
|||||||
ProxyInfo() ProxyInfo
|
ProxyInfo() ProxyInfo
|
||||||
MarshalJSON() ([]byte, error)
|
MarshalJSON() ([]byte, error)
|
||||||
|
|
||||||
// Deprecated: use DialContextWithDialer and ListenPacketWithDialer instead.
|
|
||||||
// StreamConn wraps a protocol around net.Conn with Metadata.
|
|
||||||
//
|
|
||||||
// Examples:
|
|
||||||
// conn, _ := net.DialContext(context.Background(), "tcp", "host:port")
|
|
||||||
// conn, _ = adapter.StreamConnContext(context.Background(), conn, metadata)
|
|
||||||
//
|
|
||||||
// It returns a C.Conn with protocol which start with
|
|
||||||
// a new session (if any)
|
|
||||||
StreamConnContext(ctx context.Context, c net.Conn, metadata *Metadata) (net.Conn, error)
|
|
||||||
|
|
||||||
// DialContext return a C.Conn with protocol which
|
// DialContext return a C.Conn with protocol which
|
||||||
// contains multiplexing-related reuse logic (if any)
|
// contains multiplexing-related reuse logic (if any)
|
||||||
DialContext(ctx context.Context, metadata *Metadata) (Conn, error)
|
DialContext(ctx context.Context, metadata *Metadata) (Conn, error)
|
||||||
@@ -139,10 +131,6 @@ type ProxyAdapter interface {
|
|||||||
// SupportUOT return UDP over TCP support
|
// SupportUOT return UDP over TCP support
|
||||||
SupportUOT() bool
|
SupportUOT() bool
|
||||||
|
|
||||||
SupportWithDialer() NetWork
|
|
||||||
DialContextWithDialer(ctx context.Context, dialer Dialer, metadata *Metadata) (Conn, error)
|
|
||||||
ListenPacketWithDialer(ctx context.Context, dialer Dialer, metadata *Metadata) (PacketConn, error)
|
|
||||||
|
|
||||||
// IsL3Protocol return ProxyAdapter working in L3 (tell dns module not pass the domain to avoid loopback)
|
// IsL3Protocol return ProxyAdapter working in L3 (tell dns module not pass the domain to avoid loopback)
|
||||||
IsL3Protocol(metadata *Metadata) bool
|
IsL3Protocol(metadata *Metadata) bool
|
||||||
|
|
||||||
@@ -153,11 +141,6 @@ type ProxyAdapter interface {
|
|||||||
Close() error
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
||||||
type Group interface {
|
|
||||||
URLTest(ctx context.Context, url string, expectedStatus utils.IntRanges[uint16]) (mp map[string]uint16, err error)
|
|
||||||
Touch()
|
|
||||||
}
|
|
||||||
|
|
||||||
type DelayHistory struct {
|
type DelayHistory struct {
|
||||||
Time time.Time `json:"time"`
|
Time time.Time `json:"time"`
|
||||||
Delay uint16 `json:"delay"`
|
Delay uint16 `json:"delay"`
|
||||||
@@ -178,12 +161,6 @@ type Proxy interface {
|
|||||||
ExtraDelayHistories() map[string]ProxyState
|
ExtraDelayHistories() map[string]ProxyState
|
||||||
LastDelayForTestUrl(url string) uint16
|
LastDelayForTestUrl(url string) uint16
|
||||||
URLTest(ctx context.Context, url string, expectedStatus utils.IntRanges[uint16]) (uint16, error)
|
URLTest(ctx context.Context, url string, expectedStatus utils.IntRanges[uint16]) (uint16, error)
|
||||||
|
|
||||||
// Deprecated: use DialContext instead.
|
|
||||||
Dial(metadata *Metadata) (Conn, error)
|
|
||||||
|
|
||||||
// Deprecated: use DialPacketConn instead.
|
|
||||||
DialUDP(metadata *Metadata) (PacketConn, error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AdapterType is enum of adapter type
|
// AdapterType is enum of adapter type
|
||||||
@@ -233,6 +210,8 @@ func (at AdapterType) String() string {
|
|||||||
return "Mieru"
|
return "Mieru"
|
||||||
case AnyTLS:
|
case AnyTLS:
|
||||||
return "AnyTLS"
|
return "AnyTLS"
|
||||||
|
case Sudoku:
|
||||||
|
return "Sudoku"
|
||||||
case Relay:
|
case Relay:
|
||||||
return "Relay"
|
return "Relay"
|
||||||
case Selector:
|
case Selector:
|
||||||
@@ -295,6 +274,10 @@ func (s *packetAdapter) Key() string {
|
|||||||
return s.key
|
return s.key
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *packetAdapter) Upstream() any {
|
||||||
|
return s.UDPPacket
|
||||||
|
}
|
||||||
|
|
||||||
func NewPacketAdapter(packet UDPPacket, metadata *Metadata) PacketAdapter {
|
func NewPacketAdapter(packet UDPPacket, metadata *Metadata) PacketAdapter {
|
||||||
return &packetAdapter{
|
return &packetAdapter{
|
||||||
packet,
|
packet,
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user