Compare commits
292 Commits
Author | SHA1 | Date | |
---|---|---|---|
b9ffc82e53 | |||
78aaea6a45 | |||
7d6991dd65 | |||
95247154d6 | |||
be6142aa43 | |||
0069513780 | |||
0c9a23a53c | |||
0035fc2313 | |||
6f62d4d5c1 | |||
51f9b34a7c | |||
337be9124f | |||
dd4e4d7559 | |||
0f29c267be | |||
d38ceb78c9 | |||
0c354c748a | |||
3a8e7c8899 | |||
261b8a1d06 | |||
01d8b224db | |||
e9a7e104c0 | |||
b4503908df | |||
3645fbf161 | |||
a1d0f22132 | |||
fd48c6df8a | |||
cd7134e309 | |||
5fa6777239 | |||
908d0b0007 | |||
8e6989758e | |||
29b72df14c | |||
f100a33d98 | |||
fa73b0f4bf | |||
3b76a8b839 | |||
7a64c432b1 | |||
2301b909d2 | |||
89680de12b | |||
a03af85a6b | |||
fbca37c42b | |||
4a57917783 | |||
cdc7d449a6 | |||
daf0b23805 | |||
d8ac82be36 | |||
a6c144038b | |||
980454beb2 | |||
63922f86a2 | |||
22414ce399 | |||
7496d9c114 | |||
c63dd62ed2 | |||
ff01d845b4 | |||
57592ee840 | |||
432c4c2cf1 | |||
98b7377643 | |||
287ec7e851 | |||
8a2d1ec5a7 | |||
afb2364ca2 | |||
9711390c18 | |||
bffb0573a6 | |||
17cbbb5bf0 | |||
b3b5f17e03 | |||
88acf8e098 | |||
f87144f84b | |||
1333f1fd47 | |||
8fa6bd1743 | |||
02d3468516 | |||
f657ac97f6 | |||
57dfaf135d | |||
9df42d7b98 | |||
b5928c36a3 | |||
910e7fed97 | |||
a9839abd4c | |||
78c7b6259c | |||
a6f7e1472b | |||
e03fcd24dd | |||
cd99b2e795 | |||
b5b06ea49c | |||
f7fb5840cf | |||
3b96d54369 | |||
f390b9cf2f | |||
1c65a2c1b4 | |||
2d2b75a4bf | |||
dcbe25c3ae | |||
46d23d9b86 | |||
fd9c4cbfa5 | |||
5c410b8df4 | |||
8c58d8a8ad | |||
a0a2eb2106 | |||
b7d976796a | |||
2e22c712af | |||
c7f83d3ff1 | |||
62474e0ed6 | |||
62226e8b3d | |||
8144373725 | |||
e9d8dd09ac | |||
6fc62da7ae | |||
4f75201a98 | |||
667f42dcdc | |||
dfbe09860f | |||
ba884c29bd | |||
2fe271f19f | |||
cf5709aab1 | |||
654cdf3d5b | |||
6c79d9e63b | |||
0aefa3be85 | |||
bc5ab3120f | |||
df8e129fc6 | |||
84caee94af | |||
1d9e320087 | |||
2a3c4c1a33 | |||
8c0fbb3665 | |||
9ea09b2b94 | |||
9e20f9c26a | |||
db81db5363 | |||
e715ccbdd5 | |||
bc94c50783 | |||
b4b9ef2362 | |||
dd6f7e3701 | |||
01e382285d | |||
4b1d4a3e20 | |||
562819e3ca | |||
551283c16e | |||
cd53e2d4a7 | |||
a58234f0cd | |||
c8d7243b5b | |||
6b1ca7b07c | |||
b80e7c3c92 | |||
0da09c5ddd | |||
17c081a40c | |||
0647cee02a | |||
423850a7aa | |||
896d30b151 | |||
495fd191f2 | |||
ae76daf393 | |||
f968d0cb82 | |||
8056b5573b | |||
516623cbbb | |||
a5ae2e891c | |||
90b40a8e5a | |||
7f40645934 | |||
6c204d2b77 | |||
ed988dcdc5 | |||
7b44cde4bd | |||
c7bad89af3 | |||
21a91e88a1 | |||
76d2838721 | |||
9b1fe9f466 | |||
9976800a35 | |||
f542351404 | |||
a13dedb6e4 | |||
d47ce79a24 | |||
cce42b4b83 | |||
142d17ebad | |||
30ca59dab7 | |||
c89b1f0e96 | |||
59bd11a3a7 | |||
3880c3c1be | |||
efa4b9e0b8 | |||
8c6e205c5a | |||
d478728cb7 | |||
5b07d7b776 | |||
18d62c4a17 | |||
02830e0ad6 | |||
6d89bddf29 | |||
dbbd499349 | |||
d3562ce394 | |||
d5973cf8a6 | |||
1d3cc36eef | |||
8fcfecbed1 | |||
7c1b878c3f | |||
4ea4221380 | |||
b8b3c9ef9f | |||
f00dc69bb6 | |||
23f286f24e | |||
16f8f77f5d | |||
dfc0ec995c | |||
8b848b62bb | |||
2dc62024fe | |||
994e85425f | |||
1880a485f8 | |||
03645fb235 | |||
eb8431255d | |||
e5a81b6c35 | |||
0eecd11fdc | |||
9c8e39827f | |||
586dec5ba3 | |||
6db7c800d5 | |||
2a8e1778ad | |||
a3425c0e78 | |||
7300c917dc | |||
dc3e144b6a | |||
75d339392b | |||
901a47318d | |||
dbadf37823 | |||
3321ac95ca | |||
c0bd4af120 | |||
d78b2b1cfb | |||
3e20912339 | |||
b2d7149a95 | |||
64be213b66 | |||
68b28ed530 | |||
3eacce9a66 | |||
6dadc2357a | |||
698d8ca701 | |||
1a4b00c70e | |||
64552fbd00 | |||
7c8d8f56e1 | |||
93ada8989f | |||
4b4c3dc41e | |||
b699fb046b | |||
ae08d13de4 | |||
1d784231b0 | |||
5fd79890e7 | |||
53b2a480ef | |||
943137de3b | |||
2d3aad573e | |||
409cd4f6a1 | |||
bd526ad0a1 | |||
4673d2093a | |||
94a765ee31 | |||
dcd2417fce | |||
4c5853e5e7 | |||
52f4cb599a | |||
90f6cc233c | |||
de264c42a8 | |||
c2469162fb | |||
19b7c7f52a | |||
e20d01a679 | |||
9a5c0a4b6d | |||
1b0d09068b | |||
3373b62b02 | |||
508e257543 | |||
7b0cd14b00 | |||
22fb219ad8 | |||
c34c5ff1f9 | |||
c8bc11d61d | |||
f29b54898f | |||
3e2b08f9d0 | |||
fb85691fb9 | |||
d411394482 | |||
827d5289bc | |||
6995e98181 | |||
4f291fa513 | |||
22b9befbda | |||
425b6e0dc0 | |||
2516169f61 | |||
a3281712e2 | |||
bf079742cb | |||
6e058f8581 | |||
3946d771e5 | |||
5940f62794 | |||
71cad51e8f | |||
50105f0559 | |||
6648793e40 | |||
95e3a88608 | |||
bec4df7b12 | |||
93400cf44d | |||
a794819869 | |||
be8d63ba8f | |||
3b90e18047 | |||
f0952b55d0 | |||
8c7c8f4374 | |||
65a8e8f59c | |||
5497adaba1 | |||
aaf08dadff | |||
557297ac9a | |||
77a1e3a653 | |||
27e1d6cdae | |||
91c22b16bf | |||
fc5c9b931b | |||
c231fd1466 | |||
fbb27b84d1 | |||
e0c5a85314 | |||
2fa1a5c4b9 | |||
06d75da257 | |||
09d49bac95 | |||
3360839fe3 | |||
c1285adbf8 | |||
9d2fc976e2 | |||
7f41f94fff | |||
d1f0dac302 | |||
afb3e00067 | |||
9a31ad6151 | |||
09cc6b69e3 | |||
8603ac40a1 | |||
b384449717 | |||
da7ffc0da9 | |||
5dd94c8298 | |||
412b44a981 | |||
aef4dd3fe7 | |||
6a92c6af4e | |||
e010940b61 | |||
2c9a4d276a | |||
4dfba73e5c | |||
c282d662ca | |||
b3d7594813 |
12
.github/workflows/build.yaml
vendored
12
.github/workflows/build.yaml
vendored
@ -5,12 +5,14 @@ jobs:
|
|||||||
build:
|
build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Set up Go
|
- name: Check out code into the Go module directory
|
||||||
uses: actions/setup-go@v1
|
uses: actions/checkout@v3
|
||||||
|
- name: Setup Go
|
||||||
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: 1.18
|
go-version: '1.19'
|
||||||
- name: Check out code
|
check-latest: true
|
||||||
uses: actions/checkout@v1
|
cache: true
|
||||||
- name: Build
|
- name: Build
|
||||||
run: make all
|
run: make all
|
||||||
- name: Release
|
- name: Release
|
||||||
|
33
.github/workflows/prerelease.yml
vendored
33
.github/workflows/prerelease.yml
vendored
@ -1,5 +1,6 @@
|
|||||||
name: Prerelease
|
name: Prerelease
|
||||||
on:
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- Alpha
|
- Alpha
|
||||||
@ -12,26 +13,15 @@ jobs:
|
|||||||
Build:
|
Build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Get latest go version
|
|
||||||
id: version
|
|
||||||
run: |
|
|
||||||
echo ::set-output name=go_version::$(curl -s https://raw.githubusercontent.com/actions/go-versions/main/versions-manifest.json | grep -oE '"version": "[0-9]{1}.[0-9]{1,}(.[0-9]{1,})?"' | head -1 | cut -d':' -f2 | sed 's/ //g; s/"//g')
|
|
||||||
- name: Setup Go
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: ${{ steps.version.outputs.go_version }}
|
|
||||||
|
|
||||||
- name: Check out code into the Go module directory
|
- name: Check out code into the Go module directory
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
- name: Cache go module
|
- name: Setup Go
|
||||||
uses: actions/cache@v2
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
path: ~/go/pkg/mod
|
go-version: '1.19'
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
check-latest: true
|
||||||
restore-keys: |
|
cache: true
|
||||||
${{ runner.os }}-go-
|
|
||||||
|
|
||||||
|
|
||||||
- name: Test
|
- name: Test
|
||||||
if: ${{github.ref_name=='Beta'}}
|
if: ${{github.ref_name=='Beta'}}
|
||||||
@ -46,14 +36,16 @@ jobs:
|
|||||||
run: make -j$(($(nproc) + 1)) releases
|
run: make -j$(($(nproc) + 1)) releases
|
||||||
|
|
||||||
- name: Delete current release assets
|
- name: Delete current release assets
|
||||||
uses: andreaswilli/delete-release-assets-action@v2.0.0
|
uses: mknejp/delete-release-assets@v1
|
||||||
with:
|
with:
|
||||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
tag: Prerelease-${{ github.ref_name }}
|
tag_name: Prerelease-${{ github.ref_name }}
|
||||||
deleteOnlyFromDrafts: false
|
assets: |
|
||||||
|
*.zip
|
||||||
|
*.gz
|
||||||
|
|
||||||
- name: Tag Repo
|
- name: Tag Repo
|
||||||
uses: richardsimko/update-tag@v1
|
uses: richardsimko/update-tag@v1.0.6
|
||||||
with:
|
with:
|
||||||
tag_name: Prerelease-${{ github.ref_name }}
|
tag_name: Prerelease-${{ github.ref_name }}
|
||||||
env:
|
env:
|
||||||
@ -63,7 +55,6 @@ jobs:
|
|||||||
uses: softprops/action-gh-release@v1
|
uses: softprops/action-gh-release@v1
|
||||||
if: ${{ success() }}
|
if: ${{ success() }}
|
||||||
with:
|
with:
|
||||||
tag: ${{ github.ref_name }}
|
|
||||||
tag_name: Prerelease-${{ github.ref_name }}
|
tag_name: Prerelease-${{ github.ref_name }}
|
||||||
files: bin/*
|
files: bin/*
|
||||||
prerelease: true
|
prerelease: true
|
||||||
|
22
.github/workflows/release.yaml
vendored
22
.github/workflows/release.yaml
vendored
@ -7,24 +7,16 @@ jobs:
|
|||||||
Build:
|
Build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Get latest go version
|
|
||||||
id: version
|
|
||||||
run: |
|
|
||||||
echo ::set-output name=go_version::$(curl -s https://raw.githubusercontent.com/actions/go-versions/main/versions-manifest.json | grep -oE '"version": "[0-9]{1}.[0-9]{1,}(.[0-9]{1,})?"' | head -1 | cut -d':' -f2 | sed 's/ //g; s/"//g')
|
|
||||||
- name: Setup Go
|
|
||||||
uses: actions/setup-go@v2
|
|
||||||
with:
|
|
||||||
go-version: ${{ steps.version.outputs.go_version }}
|
|
||||||
|
|
||||||
- name: Check out code into the Go module directory
|
- name: Check out code into the Go module directory
|
||||||
uses: actions/checkout@v3
|
uses: actions/checkout@v3
|
||||||
- name: Cache go module
|
|
||||||
uses: actions/cache@v2
|
- name: Setup Go
|
||||||
|
uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
path: ~/go/pkg/mod
|
go-version: '1.19'
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
check-latest: true
|
||||||
restore-keys: |
|
cache: true
|
||||||
${{ runner.os }}-go-
|
|
||||||
- name: Test
|
- name: Test
|
||||||
run: |
|
run: |
|
||||||
go test ./...
|
go test ./...
|
||||||
|
@ -8,9 +8,10 @@ linters:
|
|||||||
|
|
||||||
linters-settings:
|
linters-settings:
|
||||||
gci:
|
gci:
|
||||||
|
custom-order: true
|
||||||
sections:
|
sections:
|
||||||
- standard
|
- standard
|
||||||
- prefix(github.com/Dreamacro/clash)
|
- prefix(github.com/Dreamacro/clash)
|
||||||
- default
|
- default
|
||||||
staticcheck:
|
staticcheck:
|
||||||
go: '1.18'
|
go: '1.19'
|
||||||
|
2
Makefile
2
Makefile
@ -1,4 +1,4 @@
|
|||||||
NAME=Clash.Meta
|
NAME=clash.meta
|
||||||
BINDIR=bin
|
BINDIR=bin
|
||||||
BRANCH=$(shell git branch --show-current)
|
BRANCH=$(shell git branch --show-current)
|
||||||
ifeq ($(BRANCH),Alpha)
|
ifeq ($(BRANCH),Alpha)
|
||||||
|
58
README.md
58
README.md
@ -34,6 +34,26 @@ Documentations are now moved to [GitHub Wiki](https://github.com/Dreamacro/clash
|
|||||||
|
|
||||||
## Advanced usage for this branch
|
## Advanced usage for this branch
|
||||||
|
|
||||||
|
## Build
|
||||||
|
|
||||||
|
You should install [golang](https://go.dev) first.
|
||||||
|
|
||||||
|
Then get the source code of Clash.Meta:
|
||||||
|
```shell
|
||||||
|
git clone https://github.com/MetaCubeX/Clash.Meta.git
|
||||||
|
cd Clash.Meta && go mod download
|
||||||
|
```
|
||||||
|
|
||||||
|
If you can't visit github,you should set proxy first:
|
||||||
|
```shell
|
||||||
|
go env -w GOPROXY=https://goproxy.io,direct
|
||||||
|
```
|
||||||
|
|
||||||
|
So now you can build it:
|
||||||
|
```shell
|
||||||
|
go build
|
||||||
|
```
|
||||||
|
|
||||||
### DNS configuration
|
### DNS configuration
|
||||||
|
|
||||||
Support `geosite` with `fallback-filter`.
|
Support `geosite` with `fallback-filter`.
|
||||||
@ -212,6 +232,43 @@ proxies:
|
|||||||
grpc-service-name: grpcname
|
grpc-service-name: grpcname
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
Support outbound transport protocol `Wireguard`
|
||||||
|
```yaml
|
||||||
|
proxies:
|
||||||
|
- name: "wg"
|
||||||
|
type: wireguard
|
||||||
|
server: 162.159.192.1
|
||||||
|
port: 2480
|
||||||
|
ip: 172.16.0.2
|
||||||
|
ipv6: fd01:5ca1:ab1e:80fa:ab85:6eea:213f:f4a5
|
||||||
|
private-key: eCtXsJZ27+4PbhDkHnB923tkUn2Gj59wZw5wFA75MnU=
|
||||||
|
public-key: Cr8hWlKvtDt7nrvf+f0brNQQzabAqrjfBvas9pmowjo=
|
||||||
|
udp: true
|
||||||
|
```
|
||||||
|
|
||||||
|
Support outbound transport protocol `Tuic`
|
||||||
|
```yaml
|
||||||
|
proxies:
|
||||||
|
- name: "tuic"
|
||||||
|
server: www.example.com
|
||||||
|
port: 10443
|
||||||
|
type: tuic
|
||||||
|
token: TOKEN
|
||||||
|
# ip: 127.0.0.1 # for overwriting the DNS lookup result of the server address set in option 'server'
|
||||||
|
# heartbeat-interval: 10000
|
||||||
|
# alpn: [h3]
|
||||||
|
# disable-sni: true
|
||||||
|
reduce-rtt: true
|
||||||
|
# request-timeout: 8000
|
||||||
|
udp-relay-mode: native # Available: "native", "quic". Default: "native"
|
||||||
|
# congestion-controller: bbr # Available: "cubic", "new_reno", "bbr". Default: "cubic"
|
||||||
|
# max-udp-relay-packet-size: 1500
|
||||||
|
# fast-open: true
|
||||||
|
# skip-cert-verify: true
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
### IPTABLES configuration
|
### IPTABLES configuration
|
||||||
Work on Linux OS who's supported `iptables`
|
Work on Linux OS who's supported `iptables`
|
||||||
|
|
||||||
@ -286,6 +343,7 @@ the [GitHub Wiki](https://github.com/Dreamacro/clash/wiki/use-clash-as-a-library
|
|||||||
## Credits
|
## Credits
|
||||||
|
|
||||||
* [Dreamacro/clash](https://github.com/Dreamacro/clash)
|
* [Dreamacro/clash](https://github.com/Dreamacro/clash)
|
||||||
|
* [SagerNet/sing-box](https://github.com/SagerNet/sing-box)
|
||||||
* [riobard/go-shadowsocks2](https://github.com/riobard/go-shadowsocks2)
|
* [riobard/go-shadowsocks2](https://github.com/riobard/go-shadowsocks2)
|
||||||
* [v2ray/v2ray-core](https://github.com/v2ray/v2ray-core)
|
* [v2ray/v2ray-core](https://github.com/v2ray/v2ray-core)
|
||||||
* [WireGuard/wireguard-go](https://github.com/WireGuard/wireguard-go)
|
* [WireGuard/wireguard-go](https://github.com/WireGuard/wireguard-go)
|
||||||
|
@ -92,6 +92,7 @@ func (p *Proxy) MarshalJSON() ([]byte, error) {
|
|||||||
mapping["history"] = p.DelayHistory()
|
mapping["history"] = p.DelayHistory()
|
||||||
mapping["name"] = p.Name()
|
mapping["name"] = p.Name()
|
||||||
mapping["udp"] = p.SupportUDP()
|
mapping["udp"] = p.SupportUDP()
|
||||||
|
mapping["tfo"] = p.SupportTFO()
|
||||||
return json.Marshal(mapping)
|
return json.Marshal(mapping)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -198,10 +199,9 @@ func urlToMetadata(rawURL string) (addr C.Metadata, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addr = C.Metadata{
|
addr = C.Metadata{
|
||||||
AddrType: C.AtypDomainName,
|
Host: u.Hostname(),
|
||||||
Host: u.Hostname(),
|
DstIP: netip.Addr{},
|
||||||
DstIP: netip.Addr{},
|
DstPort: port,
|
||||||
DstPort: port,
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
29
adapter/inbound/addition.go
Normal file
29
adapter/inbound/addition.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package inbound
|
||||||
|
|
||||||
|
import (
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Addition func(metadata *C.Metadata)
|
||||||
|
|
||||||
|
func (a Addition) Apply(metadata *C.Metadata) {
|
||||||
|
a(metadata)
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithInName(name string) Addition {
|
||||||
|
return func(metadata *C.Metadata) {
|
||||||
|
metadata.InName = name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithSpecialRules(specialRules string) Addition {
|
||||||
|
return func(metadata *C.Metadata) {
|
||||||
|
metadata.SpecialRules = specialRules
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithSpecialProxy(specialProxy string) Addition {
|
||||||
|
return func(metadata *C.Metadata) {
|
||||||
|
metadata.SpecialProxy = specialProxy
|
||||||
|
}
|
||||||
|
}
|
@ -9,13 +9,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// NewHTTP receive normal http request and return HTTPContext
|
// NewHTTP receive normal http request and return HTTPContext
|
||||||
func NewHTTP(target socks5.Addr, source net.Addr, conn net.Conn) *context.ConnContext {
|
func NewHTTP(target socks5.Addr, source net.Addr, conn net.Conn, additions ...Addition) *context.ConnContext {
|
||||||
metadata := parseSocksAddr(target)
|
metadata := parseSocksAddr(target)
|
||||||
metadata.NetWork = C.TCP
|
metadata.NetWork = C.TCP
|
||||||
metadata.Type = C.HTTP
|
metadata.Type = C.HTTP
|
||||||
|
for _, addition := range additions {
|
||||||
|
addition.Apply(metadata)
|
||||||
|
}
|
||||||
if ip, port, err := parseAddr(source.String()); err == nil {
|
if ip, port, err := parseAddr(source.String()); err == nil {
|
||||||
metadata.SrcIP = ip
|
metadata.SrcIP = ip
|
||||||
metadata.SrcPort = port
|
metadata.SrcPort = port
|
||||||
}
|
}
|
||||||
|
if ip, port, err := parseAddr(conn.LocalAddr().String()); err == nil {
|
||||||
|
metadata.InIP = ip
|
||||||
|
metadata.InPort = port
|
||||||
|
}
|
||||||
return context.NewConnContext(conn, metadata)
|
return context.NewConnContext(conn, metadata)
|
||||||
}
|
}
|
||||||
|
@ -9,12 +9,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// NewHTTPS receive CONNECT request and return ConnContext
|
// NewHTTPS receive CONNECT request and return ConnContext
|
||||||
func NewHTTPS(request *http.Request, conn net.Conn) *context.ConnContext {
|
func NewHTTPS(request *http.Request, conn net.Conn, additions ...Addition) *context.ConnContext {
|
||||||
metadata := parseHTTPAddr(request)
|
metadata := parseHTTPAddr(request)
|
||||||
metadata.Type = C.HTTPS
|
metadata.Type = C.HTTPS
|
||||||
|
for _, addition := range additions {
|
||||||
|
addition.Apply(metadata)
|
||||||
|
}
|
||||||
if ip, port, err := parseAddr(conn.RemoteAddr().String()); err == nil {
|
if ip, port, err := parseAddr(conn.RemoteAddr().String()); err == nil {
|
||||||
metadata.SrcIP = ip
|
metadata.SrcIP = ip
|
||||||
metadata.SrcPort = port
|
metadata.SrcPort = port
|
||||||
}
|
}
|
||||||
|
if ip, port, err := parseAddr(conn.LocalAddr().String()); err == nil {
|
||||||
|
metadata.InIP = ip
|
||||||
|
metadata.InPort = port
|
||||||
|
}
|
||||||
return context.NewConnContext(conn, metadata)
|
return context.NewConnContext(conn, metadata)
|
||||||
}
|
}
|
||||||
|
26
adapter/inbound/listen.go
Normal file
26
adapter/inbound/listen.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package inbound
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/database64128/tfo-go/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
lc = tfo.ListenConfig{
|
||||||
|
DisableTFO: true,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func SetTfo(open bool) {
|
||||||
|
lc.DisableTFO = !open
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListenContext(ctx context.Context, network, address string) (net.Listener, error) {
|
||||||
|
return lc.Listen(ctx, network, address)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Listen(network, address string) (net.Listener, error) {
|
||||||
|
return ListenContext(context.Background(), network, address)
|
||||||
|
}
|
@ -17,17 +17,26 @@ func (s *PacketAdapter) Metadata() *C.Metadata {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewPacket is PacketAdapter generator
|
// NewPacket is PacketAdapter generator
|
||||||
func NewPacket(target socks5.Addr, packet C.UDPPacket, source C.Type) *PacketAdapter {
|
func NewPacket(target socks5.Addr, packet C.UDPPacket, source C.Type, additions ...Addition) C.PacketAdapter {
|
||||||
metadata := parseSocksAddr(target)
|
metadata := parseSocksAddr(target)
|
||||||
metadata.NetWork = C.UDP
|
metadata.NetWork = C.UDP
|
||||||
metadata.Type = source
|
metadata.Type = source
|
||||||
|
for _, addition := range additions {
|
||||||
|
addition.Apply(metadata)
|
||||||
|
}
|
||||||
if ip, port, err := parseAddr(packet.LocalAddr().String()); err == nil {
|
if ip, port, err := parseAddr(packet.LocalAddr().String()); err == nil {
|
||||||
metadata.SrcIP = ip
|
metadata.SrcIP = ip
|
||||||
metadata.SrcPort = port
|
metadata.SrcPort = port
|
||||||
}
|
}
|
||||||
|
if p, ok := packet.(C.UDPPacketInAddr); ok {
|
||||||
|
if ip, port, err := parseAddr(p.InAddr().String()); err == nil {
|
||||||
|
metadata.InIP = ip
|
||||||
|
metadata.InPort = port
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return &PacketAdapter{
|
return &PacketAdapter{
|
||||||
UDPPacket: packet,
|
packet,
|
||||||
metadata: metadata,
|
metadata,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,11 +10,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// NewSocket receive TCP inbound and return ConnContext
|
// NewSocket receive TCP inbound and return ConnContext
|
||||||
func NewSocket(target socks5.Addr, conn net.Conn, source C.Type) *context.ConnContext {
|
func NewSocket(target socks5.Addr, conn net.Conn, source C.Type, additions ...Addition) *context.ConnContext {
|
||||||
metadata := parseSocksAddr(target)
|
metadata := parseSocksAddr(target)
|
||||||
metadata.NetWork = C.TCP
|
metadata.NetWork = C.TCP
|
||||||
metadata.Type = source
|
metadata.Type = source
|
||||||
|
for _, addition := range additions {
|
||||||
|
addition.Apply(metadata)
|
||||||
|
}
|
||||||
|
|
||||||
remoteAddr := conn.RemoteAddr()
|
remoteAddr := conn.RemoteAddr()
|
||||||
|
|
||||||
// Filter when net.Addr interface is nil
|
// Filter when net.Addr interface is nil
|
||||||
if remoteAddr != nil {
|
if remoteAddr != nil {
|
||||||
if ip, port, err := parseAddr(remoteAddr.String()); err == nil {
|
if ip, port, err := parseAddr(remoteAddr.String()); err == nil {
|
||||||
@ -22,6 +27,14 @@ func NewSocket(target socks5.Addr, conn net.Conn, source C.Type) *context.ConnCo
|
|||||||
metadata.SrcPort = port
|
metadata.SrcPort = port
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
localAddr := conn.LocalAddr()
|
||||||
|
// Filter when net.Addr interface is nil
|
||||||
|
if localAddr != nil {
|
||||||
|
if ip, port, err := parseAddr(localAddr.String()); err == nil {
|
||||||
|
metadata.InIP = ip
|
||||||
|
metadata.InPort = port
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return context.NewConnContext(conn, metadata)
|
return context.NewConnContext(conn, metadata)
|
||||||
}
|
}
|
||||||
@ -32,17 +45,12 @@ func NewInner(conn net.Conn, dst string, host string) *context.ConnContext {
|
|||||||
metadata.Type = C.INNER
|
metadata.Type = C.INNER
|
||||||
metadata.DNSMode = C.DNSMapping
|
metadata.DNSMode = C.DNSMapping
|
||||||
metadata.Host = host
|
metadata.Host = host
|
||||||
metadata.AddrType = C.AtypDomainName
|
|
||||||
metadata.Process = C.ClashName
|
metadata.Process = C.ClashName
|
||||||
if h, port, err := net.SplitHostPort(dst); err == nil {
|
if h, port, err := net.SplitHostPort(dst); err == nil {
|
||||||
metadata.DstPort = port
|
metadata.DstPort = port
|
||||||
if host == "" {
|
if host == "" {
|
||||||
if ip, err := netip.ParseAddr(h); err == nil {
|
if ip, err := netip.ParseAddr(h); err == nil {
|
||||||
metadata.DstIP = ip
|
metadata.DstIP = ip
|
||||||
metadata.AddrType = C.AtypIPv4
|
|
||||||
if ip.Is6() {
|
|
||||||
metadata.AddrType = C.AtypIPv6
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,9 +13,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func parseSocksAddr(target socks5.Addr) *C.Metadata {
|
func parseSocksAddr(target socks5.Addr) *C.Metadata {
|
||||||
metadata := &C.Metadata{
|
metadata := &C.Metadata{}
|
||||||
AddrType: int(target[0]),
|
|
||||||
}
|
|
||||||
|
|
||||||
switch target[0] {
|
switch target[0] {
|
||||||
case socks5.AtypDomainName:
|
case socks5.AtypDomainName:
|
||||||
@ -45,21 +43,14 @@ func parseHTTPAddr(request *http.Request) *C.Metadata {
|
|||||||
host = strings.TrimRight(host, ".")
|
host = strings.TrimRight(host, ".")
|
||||||
|
|
||||||
metadata := &C.Metadata{
|
metadata := &C.Metadata{
|
||||||
NetWork: C.TCP,
|
NetWork: C.TCP,
|
||||||
AddrType: C.AtypDomainName,
|
Host: host,
|
||||||
Host: host,
|
DstIP: netip.Addr{},
|
||||||
DstIP: netip.Addr{},
|
DstPort: port,
|
||||||
DstPort: port,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ip, err := netip.ParseAddr(host)
|
ip, err := netip.ParseAddr(host)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
switch {
|
|
||||||
case ip.Is6():
|
|
||||||
metadata.AddrType = C.AtypIPv6
|
|
||||||
default:
|
|
||||||
metadata.AddrType = C.AtypIPv4
|
|
||||||
}
|
|
||||||
metadata.DstIP = ip
|
metadata.DstIP = ip
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@ type Base struct {
|
|||||||
iface string
|
iface string
|
||||||
tp C.AdapterType
|
tp C.AdapterType
|
||||||
udp bool
|
udp bool
|
||||||
|
tfo bool
|
||||||
rmark int
|
rmark int
|
||||||
id string
|
id string
|
||||||
prefer C.DNSPrefer
|
prefer C.DNSPrefer
|
||||||
@ -56,16 +57,26 @@ func (b *Base) DialContext(ctx context.Context, metadata *C.Metadata, opts ...di
|
|||||||
return nil, errors.New("no support")
|
return nil, errors.New("no support")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DialContextWithDialer implements C.ProxyAdapter
|
||||||
|
func (b *Base) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
|
return nil, errors.New("no support")
|
||||||
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (b *Base) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
func (b *Base) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
||||||
return nil, errors.New("no support")
|
return nil, errors.New("no support")
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
// ListenPacketWithDialer implements C.ProxyAdapter
|
||||||
func (b *Base) ListenPacketOnStreamConn(c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
func (b *Base) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
return nil, errors.New("no support")
|
return nil, errors.New("no support")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SupportWithDialer implements C.ProxyAdapter
|
||||||
|
func (b *Base) SupportWithDialer() bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// SupportUOT implements C.ProxyAdapter
|
// SupportUOT implements C.ProxyAdapter
|
||||||
func (b *Base) SupportUOT() bool {
|
func (b *Base) SupportUOT() bool {
|
||||||
return false
|
return false
|
||||||
@ -76,6 +87,11 @@ func (b *Base) SupportUDP() bool {
|
|||||||
return b.udp
|
return b.udp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SupportTFO implements C.ProxyAdapter
|
||||||
|
func (b *Base) SupportTFO() bool {
|
||||||
|
return b.tfo
|
||||||
|
}
|
||||||
|
|
||||||
// MarshalJSON implements C.ProxyAdapter
|
// MarshalJSON implements C.ProxyAdapter
|
||||||
func (b *Base) MarshalJSON() ([]byte, error) {
|
func (b *Base) MarshalJSON() ([]byte, error) {
|
||||||
return json.Marshal(map[string]string{
|
return json.Marshal(map[string]string{
|
||||||
@ -130,6 +146,7 @@ type BaseOption struct {
|
|||||||
Addr string
|
Addr string
|
||||||
Type C.AdapterType
|
Type C.AdapterType
|
||||||
UDP bool
|
UDP bool
|
||||||
|
TFO bool
|
||||||
Interface string
|
Interface string
|
||||||
RoutingMark int
|
RoutingMark int
|
||||||
Prefer C.DNSPrefer
|
Prefer C.DNSPrefer
|
||||||
@ -141,6 +158,7 @@ func NewBase(opt BaseOption) *Base {
|
|||||||
addr: opt.Addr,
|
addr: opt.Addr,
|
||||||
tp: opt.Type,
|
tp: opt.Type,
|
||||||
udp: opt.UDP,
|
udp: opt.UDP,
|
||||||
|
tfo: opt.TFO,
|
||||||
iface: opt.Interface,
|
iface: opt.Interface,
|
||||||
rmark: opt.RoutingMark,
|
rmark: opt.RoutingMark,
|
||||||
prefer: opt.Prefer,
|
prefer: opt.Prefer,
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
|
"github.com/Dreamacro/clash/component/resolver"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -14,7 +15,7 @@ type Direct struct {
|
|||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (d *Direct) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
func (d *Direct) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
||||||
opts = append(opts, dialer.WithDirect())
|
opts = append(opts, dialer.WithResolver(resolver.DefaultResolver))
|
||||||
c, err := dialer.DialContext(ctx, "tcp", metadata.RemoteAddress(), d.Base.DialOptions(opts...)...)
|
c, err := dialer.DialContext(ctx, "tcp", metadata.RemoteAddress(), d.Base.DialOptions(opts...)...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -25,8 +26,8 @@ func (d *Direct) DialContext(ctx context.Context, metadata *C.Metadata, opts ...
|
|||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (d *Direct) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
func (d *Direct) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
||||||
opts = append(opts, dialer.WithDirect())
|
opts = append(opts, dialer.WithResolver(resolver.DefaultResolver))
|
||||||
pc, err := dialer.ListenPacket(ctx, "udp", "", d.Base.DialOptions(opts...)...)
|
pc, err := dialer.ListenPacket(ctx, dialer.ParseNetwork("udp", metadata.DstIP), "", d.Base.DialOptions(opts...)...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,9 @@ type HttpOption struct {
|
|||||||
func (h *Http) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
func (h *Http) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
||||||
if h.tlsConfig != nil {
|
if h.tlsConfig != nil {
|
||||||
cc := tls.Client(c, h.tlsConfig)
|
cc := tls.Client(c, h.tlsConfig)
|
||||||
err := cc.Handshake()
|
ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTLSTimeout)
|
||||||
|
defer cancel()
|
||||||
|
err := cc.HandshakeContext(ctx)
|
||||||
c = cc
|
c = cc
|
||||||
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)
|
||||||
@ -59,13 +61,20 @@ func (h *Http) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
|||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (h *Http) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
func (h *Http) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
||||||
c, err := dialer.DialContext(ctx, "tcp", h.addr, h.Base.DialOptions(opts...)...)
|
return h.DialContextWithDialer(ctx, dialer.NewDialer(h.Base.DialOptions(opts...)...), metadata)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DialContextWithDialer implements C.ProxyAdapter
|
||||||
|
func (h *Http) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
tcpKeepAlive(c)
|
||||||
|
|
||||||
defer safeConnClose(c, err)
|
defer func(c net.Conn) {
|
||||||
|
safeConnClose(c, err)
|
||||||
|
}(c)
|
||||||
|
|
||||||
c, err = h.StreamConn(c, metadata)
|
c, err = h.StreamConn(c, metadata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -75,6 +84,11 @@ func (h *Http) DialContext(ctx context.Context, metadata *C.Metadata, opts ...di
|
|||||||
return NewConn(c, h), nil
|
return NewConn(c, h), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SupportWithDialer implements C.ProxyAdapter
|
||||||
|
func (h *Http) SupportWithDialer() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Http) shakeHand(metadata *C.Metadata, rw io.ReadWriter) error {
|
func (h *Http) shakeHand(metadata *C.Metadata, rw io.ReadWriter) error {
|
||||||
addr := metadata.RemoteAddress()
|
addr := metadata.RemoteAddress()
|
||||||
req := &http.Request{
|
req := &http.Request{
|
||||||
|
@ -9,13 +9,14 @@ import (
|
|||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/lucas-clemente/quic-go"
|
"github.com/metacubex/quic-go"
|
||||||
"github.com/lucas-clemente/quic-go/congestion"
|
"github.com/metacubex/quic-go/congestion"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
@ -30,15 +31,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
mbpsToBps = 125000
|
mbpsToBps = 125000
|
||||||
minSpeedBPS = 16384
|
|
||||||
|
|
||||||
DefaultStreamReceiveWindow = 15728640 // 15 MB/s
|
DefaultStreamReceiveWindow = 15728640 // 15 MB/s
|
||||||
DefaultConnectionReceiveWindow = 67108864 // 64 MB/s
|
DefaultConnectionReceiveWindow = 67108864 // 64 MB/s
|
||||||
DefaultMaxIncomingStreams = 1024
|
|
||||||
|
|
||||||
DefaultALPN = "hysteria"
|
DefaultALPN = "hysteria"
|
||||||
DefaultProtocol = "udp"
|
DefaultProtocol = "udp"
|
||||||
|
DefaultHopInterval = 10
|
||||||
)
|
)
|
||||||
|
|
||||||
var rateStringRegexp = regexp.MustCompile(`^(\d+)\s*([KMGT]?)([Bb])ps$`)
|
var rateStringRegexp = regexp.MustCompile(`^(\d+)\s*([KMGT]?)([Bb])ps$`)
|
||||||
@ -52,11 +52,11 @@ type Hysteria struct {
|
|||||||
func (h *Hysteria) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
func (h *Hysteria) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
||||||
hdc := hyDialerWithContext{
|
hdc := hyDialerWithContext{
|
||||||
ctx: context.Background(),
|
ctx: context.Background(),
|
||||||
hyDialer: func() (net.PacketConn, error) {
|
hyDialer: func(network string) (net.PacketConn, error) {
|
||||||
return dialer.ListenPacket(ctx, "udp", "", h.Base.DialOptions(opts...)...)
|
return dialer.ListenPacket(ctx, network, "", h.Base.DialOptions(opts...)...)
|
||||||
},
|
},
|
||||||
remoteAddr: func(addr string) (net.Addr, error) {
|
remoteAddr: func(addr string) (net.Addr, error) {
|
||||||
return resolveUDPAddrWithPrefer("udp", addr, h.prefer)
|
return resolveUDPAddrWithPrefer(ctx, "udp", addr, h.prefer)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,11 +71,11 @@ func (h *Hysteria) DialContext(ctx context.Context, metadata *C.Metadata, opts .
|
|||||||
func (h *Hysteria) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
func (h *Hysteria) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
||||||
hdc := hyDialerWithContext{
|
hdc := hyDialerWithContext{
|
||||||
ctx: context.Background(),
|
ctx: context.Background(),
|
||||||
hyDialer: func() (net.PacketConn, error) {
|
hyDialer: func(network string) (net.PacketConn, error) {
|
||||||
return dialer.ListenPacket(ctx, "udp", "", h.Base.DialOptions(opts...)...)
|
return dialer.ListenPacket(ctx, network, "", h.Base.DialOptions(opts...)...)
|
||||||
},
|
},
|
||||||
remoteAddr: func(addr string) (net.Addr, error) {
|
remoteAddr: func(addr string) (net.Addr, error) {
|
||||||
return resolveUDPAddrWithPrefer("udp", addr, h.prefer)
|
return resolveUDPAddrWithPrefer(ctx, "udp", addr, h.prefer)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
udpConn, err := h.client.DialUDP(&hdc)
|
udpConn, err := h.client.DialUDP(&hdc)
|
||||||
@ -89,7 +89,8 @@ type HysteriaOption struct {
|
|||||||
BasicOption
|
BasicOption
|
||||||
Name string `proxy:"name"`
|
Name string `proxy:"name"`
|
||||||
Server string `proxy:"server"`
|
Server string `proxy:"server"`
|
||||||
Port int `proxy:"port"`
|
Port int `proxy:"port,omitempty"`
|
||||||
|
Ports string `proxy:"ports,omitempty"`
|
||||||
Protocol string `proxy:"protocol,omitempty"`
|
Protocol string `proxy:"protocol,omitempty"`
|
||||||
ObfsProtocol string `proxy:"obfs-protocol,omitempty"` // compatible with Stash
|
ObfsProtocol string `proxy:"obfs-protocol,omitempty"` // compatible with Stash
|
||||||
Up string `proxy:"up"`
|
Up string `proxy:"up"`
|
||||||
@ -97,17 +98,19 @@ type HysteriaOption struct {
|
|||||||
Down string `proxy:"down"`
|
Down string `proxy:"down"`
|
||||||
DownSpeed int `proxy:"down-speed,omitempty"` // compatible with Stash
|
DownSpeed int `proxy:"down-speed,omitempty"` // compatible with Stash
|
||||||
Auth string `proxy:"auth,omitempty"`
|
Auth string `proxy:"auth,omitempty"`
|
||||||
AuthString string `proxy:"auth_str,omitempty"`
|
AuthString string `proxy:"auth-str,omitempty"`
|
||||||
Obfs string `proxy:"obfs,omitempty"`
|
Obfs string `proxy:"obfs,omitempty"`
|
||||||
SNI string `proxy:"sni,omitempty"`
|
SNI string `proxy:"sni,omitempty"`
|
||||||
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
||||||
Fingerprint string `proxy:"fingerprint,omitempty"`
|
Fingerprint string `proxy:"fingerprint,omitempty"`
|
||||||
ALPN []string `proxy:"alpn,omitempty"`
|
ALPN []string `proxy:"alpn,omitempty"`
|
||||||
CustomCA string `proxy:"ca,omitempty"`
|
CustomCA string `proxy:"ca,omitempty"`
|
||||||
CustomCAString string `proxy:"ca_str,omitempty"`
|
CustomCAString string `proxy:"ca-str,omitempty"`
|
||||||
ReceiveWindowConn int `proxy:"recv_window_conn,omitempty"`
|
ReceiveWindowConn int `proxy:"recv-window-conn,omitempty"`
|
||||||
ReceiveWindow int `proxy:"recv_window,omitempty"`
|
ReceiveWindow int `proxy:"recv-window,omitempty"`
|
||||||
DisableMTUDiscovery bool `proxy:"disable_mtu_discovery,omitempty"`
|
DisableMTUDiscovery bool `proxy:"disable-mtu-discovery,omitempty"`
|
||||||
|
FastOpen bool `proxy:"fast-open,omitempty"`
|
||||||
|
HopInterval int `proxy:"hop-interval,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *HysteriaOption) Speed() (uint64, uint64, error) {
|
func (c *HysteriaOption) Speed() (uint64, uint64, error) {
|
||||||
@ -131,8 +134,9 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
|
|||||||
Timeout: 8 * time.Second,
|
Timeout: 8 * time.Second,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
||||||
|
ports := option.Ports
|
||||||
|
|
||||||
serverName := option.Server
|
serverName := option.Server
|
||||||
if option.SNI != "" {
|
if option.SNI != "" {
|
||||||
serverName = option.SNI
|
serverName = option.SNI
|
||||||
@ -182,7 +186,6 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
|
|||||||
} else {
|
} else {
|
||||||
tlsConfig.NextProtos = []string{DefaultALPN}
|
tlsConfig.NextProtos = []string{DefaultALPN}
|
||||||
}
|
}
|
||||||
|
|
||||||
quicConfig := &quic.Config{
|
quicConfig := &quic.Config{
|
||||||
InitialStreamReceiveWindow: uint64(option.ReceiveWindowConn),
|
InitialStreamReceiveWindow: uint64(option.ReceiveWindowConn),
|
||||||
MaxStreamReceiveWindow: uint64(option.ReceiveWindowConn),
|
MaxStreamReceiveWindow: uint64(option.ReceiveWindowConn),
|
||||||
@ -198,7 +201,11 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
|
|||||||
if option.Protocol == "" {
|
if option.Protocol == "" {
|
||||||
option.Protocol = DefaultProtocol
|
option.Protocol = DefaultProtocol
|
||||||
}
|
}
|
||||||
if option.ReceiveWindowConn == 0 {
|
if option.HopInterval == 0 {
|
||||||
|
option.HopInterval = DefaultHopInterval
|
||||||
|
}
|
||||||
|
hopInterval := time.Duration(int64(option.HopInterval)) * time.Second
|
||||||
|
if option.ReceiveWindow == 0 {
|
||||||
quicConfig.InitialStreamReceiveWindow = DefaultStreamReceiveWindow / 10
|
quicConfig.InitialStreamReceiveWindow = DefaultStreamReceiveWindow / 10
|
||||||
quicConfig.MaxStreamReceiveWindow = DefaultStreamReceiveWindow
|
quicConfig.MaxStreamReceiveWindow = DefaultStreamReceiveWindow
|
||||||
}
|
}
|
||||||
@ -233,9 +240,9 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
|
|||||||
down = uint64(option.DownSpeed * mbpsToBps)
|
down = uint64(option.DownSpeed * mbpsToBps)
|
||||||
}
|
}
|
||||||
client, err := core.NewClient(
|
client, err := core.NewClient(
|
||||||
addr, option.Protocol, auth, tlsConfig, quicConfig, clientTransport, up, down, func(refBPS uint64) congestion.CongestionControl {
|
addr, ports, option.Protocol, auth, tlsConfig, quicConfig, clientTransport, up, down, func(refBPS uint64) congestion.CongestionControl {
|
||||||
return hyCongestion.NewBrutalSender(congestion.ByteCount(refBPS))
|
return hyCongestion.NewBrutalSender(congestion.ByteCount(refBPS))
|
||||||
}, obfuscator,
|
}, obfuscator, hopInterval, option.FastOpen,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("hysteria %s create error: %w", addr, err)
|
return nil, fmt.Errorf("hysteria %s create error: %w", addr, err)
|
||||||
@ -246,6 +253,7 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
|
|||||||
addr: addr,
|
addr: addr,
|
||||||
tp: C.Hysteria,
|
tp: C.Hysteria,
|
||||||
udp: true,
|
udp: true,
|
||||||
|
tfo: option.FastOpen,
|
||||||
iface: option.Interface,
|
iface: option.Interface,
|
||||||
rmark: option.RoutingMark,
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: C.NewDNSPrefer(option.IPVersion),
|
||||||
@ -314,13 +322,17 @@ func (c *hyPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type hyDialerWithContext struct {
|
type hyDialerWithContext struct {
|
||||||
hyDialer func() (net.PacketConn, error)
|
hyDialer func(network string) (net.PacketConn, error)
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
remoteAddr func(host string) (net.Addr, error)
|
remoteAddr func(host string) (net.Addr, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *hyDialerWithContext) ListenPacket() (net.PacketConn, error) {
|
func (h *hyDialerWithContext) ListenPacket(rAddr net.Addr) (net.PacketConn, error) {
|
||||||
return h.hyDialer()
|
network := "udp"
|
||||||
|
if addrPort, err := netip.ParseAddrPort(rAddr.String()); err == nil {
|
||||||
|
network = dialer.ParseNetwork(network, addrPort.Addr())
|
||||||
|
}
|
||||||
|
return h.hyDialer(network)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *hyDialerWithContext) Context() context.Context {
|
func (h *hyDialerWithContext) Context() context.Context {
|
||||||
|
@ -2,6 +2,7 @@ package outbound
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
@ -9,12 +10,15 @@ import (
|
|||||||
|
|
||||||
"github.com/Dreamacro/clash/common/structure"
|
"github.com/Dreamacro/clash/common/structure"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
"github.com/Dreamacro/clash/transport/shadowtls"
|
||||||
obfs "github.com/Dreamacro/clash/transport/simple-obfs"
|
obfs "github.com/Dreamacro/clash/transport/simple-obfs"
|
||||||
"github.com/Dreamacro/clash/transport/socks5"
|
"github.com/Dreamacro/clash/transport/socks5"
|
||||||
v2rayObfs "github.com/Dreamacro/clash/transport/v2ray-plugin"
|
v2rayObfs "github.com/Dreamacro/clash/transport/v2ray-plugin"
|
||||||
"github.com/sagernet/sing-shadowsocks"
|
|
||||||
"github.com/sagernet/sing-shadowsocks/shadowimpl"
|
shadowsocks "github.com/metacubex/sing-shadowsocks"
|
||||||
|
"github.com/metacubex/sing-shadowsocks/shadowimpl"
|
||||||
"github.com/sagernet/sing/common/bufio"
|
"github.com/sagernet/sing/common/bufio"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
"github.com/sagernet/sing/common/uot"
|
"github.com/sagernet/sing/common/uot"
|
||||||
@ -26,9 +30,11 @@ type ShadowSocks struct {
|
|||||||
|
|
||||||
option *ShadowSocksOption
|
option *ShadowSocksOption
|
||||||
// obfs
|
// obfs
|
||||||
obfsMode string
|
obfsMode string
|
||||||
obfsOption *simpleObfsOption
|
obfsOption *simpleObfsOption
|
||||||
v2rayOption *v2rayObfs.Option
|
v2rayOption *v2rayObfs.Option
|
||||||
|
shadowTLSOption *shadowTLSOption
|
||||||
|
tlsConfig *tls.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
type ShadowSocksOption struct {
|
type ShadowSocksOption struct {
|
||||||
@ -60,6 +66,13 @@ type v2rayObfsOption struct {
|
|||||||
Mux bool `obfs:"mux,omitempty"`
|
Mux bool `obfs:"mux,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type shadowTLSOption struct {
|
||||||
|
Password string `obfs:"password"`
|
||||||
|
Host string `obfs:"host"`
|
||||||
|
Fingerprint string `obfs:"fingerprint,omitempty"`
|
||||||
|
SkipCertVerify bool `obfs:"skip-cert-verify,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
// StreamConn implements C.ProxyAdapter
|
// StreamConn implements C.ProxyAdapter
|
||||||
func (ss *ShadowSocks) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
func (ss *ShadowSocks) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
||||||
switch ss.obfsMode {
|
switch ss.obfsMode {
|
||||||
@ -74,6 +87,8 @@ func (ss *ShadowSocks) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, e
|
|||||||
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)
|
||||||
}
|
}
|
||||||
|
case shadowtls.Mode:
|
||||||
|
c = shadowtls.NewShadowTLS(c, ss.shadowTLSOption.Password, ss.tlsConfig)
|
||||||
}
|
}
|
||||||
if metadata.NetWork == C.UDP && ss.option.UDPOverTCP {
|
if metadata.NetWork == C.UDP && ss.option.UDPOverTCP {
|
||||||
return ss.method.DialConn(c, M.ParseSocksaddr(uot.UOTMagicAddress+":443"))
|
return ss.method.DialConn(c, M.ParseSocksaddr(uot.UOTMagicAddress+":443"))
|
||||||
@ -83,13 +98,20 @@ func (ss *ShadowSocks) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, e
|
|||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (ss *ShadowSocks) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
func (ss *ShadowSocks) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
||||||
c, err := dialer.DialContext(ctx, "tcp", ss.addr, ss.Base.DialOptions(opts...)...)
|
return ss.DialContextWithDialer(ctx, dialer.NewDialer(ss.Base.DialOptions(opts...)...), metadata)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DialContextWithDialer implements C.ProxyAdapter
|
||||||
|
func (ss *ShadowSocks) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
tcpKeepAlive(c)
|
||||||
|
|
||||||
defer safeConnClose(c, err)
|
defer func(c net.Conn) {
|
||||||
|
safeConnClose(c, err)
|
||||||
|
}(c)
|
||||||
|
|
||||||
c, err = ss.StreamConn(c, metadata)
|
c, err = ss.StreamConn(c, metadata)
|
||||||
return NewConn(c, ss), err
|
return NewConn(c, ss), err
|
||||||
@ -97,27 +119,36 @@ func (ss *ShadowSocks) DialContext(ctx context.Context, metadata *C.Metadata, op
|
|||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (ss *ShadowSocks) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
func (ss *ShadowSocks) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
||||||
|
return ss.ListenPacketWithDialer(ctx, dialer.NewDialer(ss.Base.DialOptions(opts...)...), metadata)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListenPacketWithDialer implements C.ProxyAdapter
|
||||||
|
func (ss *ShadowSocks) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
if ss.option.UDPOverTCP {
|
if ss.option.UDPOverTCP {
|
||||||
tcpConn, err := ss.DialContext(ctx, metadata, opts...)
|
tcpConn, err := ss.DialContextWithDialer(ctx, dialer, metadata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return newPacketConn(uot.NewClientConn(tcpConn), ss), nil
|
return newPacketConn(uot.NewClientConn(tcpConn), ss), nil
|
||||||
}
|
}
|
||||||
pc, err := dialer.ListenPacket(ctx, "udp", "", ss.Base.DialOptions(opts...)...)
|
addr, err := resolveUDPAddrWithPrefer(ctx, "udp", ss.addr, ss.prefer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
addr, err := resolveUDPAddrWithPrefer("udp", ss.addr, ss.prefer)
|
pc, err := dialer.ListenPacket(ctx, "udp", "", addr.AddrPort())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pc.Close()
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
pc = ss.method.DialPacketConn(&bufio.BindPacketConn{PacketConn: pc, Addr: addr})
|
pc = ss.method.DialPacketConn(&bufio.BindPacketConn{PacketConn: pc, Addr: addr})
|
||||||
return newPacketConn(pc, ss), nil
|
return newPacketConn(pc, ss), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SupportWithDialer implements C.ProxyAdapter
|
||||||
|
func (ss *ShadowSocks) SupportWithDialer() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
||||||
func (ss *ShadowSocks) ListenPacketOnStreamConn(c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
func (ss *ShadowSocks) ListenPacketOnStreamConn(c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
if ss.option.UDPOverTCP {
|
if ss.option.UDPOverTCP {
|
||||||
@ -140,6 +171,8 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
|||||||
|
|
||||||
var v2rayOption *v2rayObfs.Option
|
var v2rayOption *v2rayObfs.Option
|
||||||
var obfsOption *simpleObfsOption
|
var obfsOption *simpleObfsOption
|
||||||
|
var shadowTLSOpt *shadowTLSOption
|
||||||
|
var tlsConfig *tls.Config
|
||||||
obfsMode := ""
|
obfsMode := ""
|
||||||
|
|
||||||
decoder := structure.NewDecoder(structure.Option{TagName: "obfs", WeaklyTypedInput: true})
|
decoder := structure.NewDecoder(structure.Option{TagName: "obfs", WeaklyTypedInput: true})
|
||||||
@ -175,6 +208,27 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
|||||||
v2rayOption.TLS = true
|
v2rayOption.TLS = true
|
||||||
v2rayOption.SkipCertVerify = opts.SkipCertVerify
|
v2rayOption.SkipCertVerify = opts.SkipCertVerify
|
||||||
}
|
}
|
||||||
|
} else if option.Plugin == shadowtls.Mode {
|
||||||
|
obfsMode = shadowtls.Mode
|
||||||
|
shadowTLSOpt = &shadowTLSOption{}
|
||||||
|
if err := decoder.Decode(option.PluginOpts, shadowTLSOpt); err != nil {
|
||||||
|
return nil, fmt.Errorf("ss %s initialize shadow-tls-plugin error: %w", addr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tlsConfig = &tls.Config{
|
||||||
|
NextProtos: shadowtls.DefaultALPN,
|
||||||
|
MinVersion: tls.VersionTLS12,
|
||||||
|
InsecureSkipVerify: shadowTLSOpt.SkipCertVerify,
|
||||||
|
ServerName: shadowTLSOpt.Host,
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(shadowTLSOpt.Fingerprint) == 0 {
|
||||||
|
tlsConfig = tlsC.GetGlobalFingerprintTLCConfig(tlsConfig)
|
||||||
|
} else {
|
||||||
|
if tlsConfig, err = tlsC.GetSpecifiedFingerprintTLSConfig(tlsConfig, shadowTLSOpt.Fingerprint); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ShadowSocks{
|
return &ShadowSocks{
|
||||||
@ -189,10 +243,12 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
|||||||
},
|
},
|
||||||
method: method,
|
method: method,
|
||||||
|
|
||||||
option: &option,
|
option: &option,
|
||||||
obfsMode: obfsMode,
|
obfsMode: obfsMode,
|
||||||
v2rayOption: v2rayOption,
|
v2rayOption: v2rayOption,
|
||||||
obfsOption: obfsOption,
|
obfsOption: obfsOption,
|
||||||
|
shadowTLSOption: shadowTLSOpt,
|
||||||
|
tlsConfig: tlsConfig,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,13 +60,20 @@ func (ssr *ShadowSocksR) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn,
|
|||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (ssr *ShadowSocksR) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
func (ssr *ShadowSocksR) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
||||||
c, err := dialer.DialContext(ctx, "tcp", ssr.addr, ssr.Base.DialOptions(opts...)...)
|
return ssr.DialContextWithDialer(ctx, dialer.NewDialer(ssr.Base.DialOptions(opts...)...), metadata)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DialContextWithDialer implements C.ProxyAdapter
|
||||||
|
func (ssr *ShadowSocksR) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
tcpKeepAlive(c)
|
||||||
|
|
||||||
defer safeConnClose(c, err)
|
defer func(c net.Conn) {
|
||||||
|
safeConnClose(c, err)
|
||||||
|
}(c)
|
||||||
|
|
||||||
c, err = ssr.StreamConn(c, metadata)
|
c, err = ssr.StreamConn(c, metadata)
|
||||||
return NewConn(c, ssr), err
|
return NewConn(c, ssr), err
|
||||||
@ -74,14 +81,18 @@ func (ssr *ShadowSocksR) DialContext(ctx context.Context, metadata *C.Metadata,
|
|||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (ssr *ShadowSocksR) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
func (ssr *ShadowSocksR) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
||||||
pc, err := dialer.ListenPacket(ctx, "udp", "", ssr.Base.DialOptions(opts...)...)
|
return ssr.ListenPacketWithDialer(ctx, dialer.NewDialer(ssr.Base.DialOptions(opts...)...), metadata)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListenPacketWithDialer implements C.ProxyAdapter
|
||||||
|
func (ssr *ShadowSocksR) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
|
addr, err := resolveUDPAddrWithPrefer(ctx, "udp", ssr.addr, ssr.prefer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
addr, err := resolveUDPAddrWithPrefer("udp", ssr.addr, ssr.prefer)
|
pc, err := dialer.ListenPacket(ctx, "udp", "", addr.AddrPort())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pc.Close()
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,6 +101,11 @@ func (ssr *ShadowSocksR) ListenPacketContext(ctx context.Context, metadata *C.Me
|
|||||||
return newPacketConn(&ssPacketConn{PacketConn: pc, rAddr: addr}, ssr), nil
|
return newPacketConn(&ssPacketConn{PacketConn: pc, rAddr: addr}, ssr), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SupportWithDialer implements C.ProxyAdapter
|
||||||
|
func (ssr *ShadowSocksR) SupportWithDialer() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func NewShadowSocksR(option ShadowSocksROption) (*ShadowSocksR, error) {
|
func NewShadowSocksR(option ShadowSocksROption) (*ShadowSocksR, error) {
|
||||||
// SSR protocol compatibility
|
// SSR protocol compatibility
|
||||||
// https://github.com/Dreamacro/clash/pull/2056
|
// https://github.com/Dreamacro/clash/pull/2056
|
||||||
|
@ -78,13 +78,20 @@ func (s *Snell) DialContext(ctx context.Context, metadata *C.Metadata, opts ...d
|
|||||||
return NewConn(c, s), err
|
return NewConn(c, s), err
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err := dialer.DialContext(ctx, "tcp", s.addr, s.Base.DialOptions(opts...)...)
|
return s.DialContextWithDialer(ctx, dialer.NewDialer(s.Base.DialOptions(opts...)...), metadata)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DialContextWithDialer implements C.ProxyAdapter
|
||||||
|
func (s *Snell) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
tcpKeepAlive(c)
|
||||||
|
|
||||||
defer safeConnClose(c, err)
|
defer func(c net.Conn) {
|
||||||
|
safeConnClose(c, err)
|
||||||
|
}(c)
|
||||||
|
|
||||||
c, err = s.StreamConn(c, metadata)
|
c, err = s.StreamConn(c, metadata)
|
||||||
return NewConn(c, s), err
|
return NewConn(c, s), err
|
||||||
@ -92,7 +99,12 @@ func (s *Snell) DialContext(ctx context.Context, metadata *C.Metadata, opts ...d
|
|||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (s *Snell) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
func (s *Snell) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
||||||
c, err := dialer.DialContext(ctx, "tcp", s.addr, s.Base.DialOptions(opts...)...)
|
return s.ListenPacketWithDialer(ctx, dialer.NewDialer(s.Base.DialOptions(opts...)...), metadata)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListenPacketWithDialer implements C.ProxyAdapter
|
||||||
|
func (s *Snell) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (C.PacketConn, error) {
|
||||||
|
c, err := dialer.DialContext(ctx, "tcp", s.addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -108,10 +120,9 @@ func (s *Snell) ListenPacketContext(ctx context.Context, metadata *C.Metadata, o
|
|||||||
return newPacketConn(pc, s), nil
|
return newPacketConn(pc, s), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
// SupportWithDialer implements C.ProxyAdapter
|
||||||
func (s *Snell) ListenPacketOnStreamConn(c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
func (s *Snell) SupportWithDialer() bool {
|
||||||
pc := snell.PacketConn(c)
|
return true
|
||||||
return newPacketConn(pc, s), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SupportUOT implements C.ProxyAdapter
|
// SupportUOT implements C.ProxyAdapter
|
||||||
|
@ -41,7 +41,9 @@ type Socks5Option struct {
|
|||||||
func (ss *Socks5) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
func (ss *Socks5) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
||||||
if ss.tls {
|
if ss.tls {
|
||||||
cc := tls.Client(c, ss.tlsConfig)
|
cc := tls.Client(c, ss.tlsConfig)
|
||||||
err := cc.Handshake()
|
ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTLSTimeout)
|
||||||
|
defer cancel()
|
||||||
|
err := cc.HandshakeContext(ctx)
|
||||||
c = cc
|
c = cc
|
||||||
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)
|
||||||
@ -63,13 +65,20 @@ func (ss *Socks5) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error)
|
|||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (ss *Socks5) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
func (ss *Socks5) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
||||||
c, err := dialer.DialContext(ctx, "tcp", ss.addr, ss.Base.DialOptions(opts...)...)
|
return ss.DialContextWithDialer(ctx, dialer.NewDialer(ss.Base.DialOptions(opts...)...), metadata)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DialContextWithDialer implements C.ProxyAdapter
|
||||||
|
func (ss *Socks5) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
tcpKeepAlive(c)
|
||||||
|
|
||||||
defer safeConnClose(c, err)
|
defer func(c net.Conn) {
|
||||||
|
safeConnClose(c, err)
|
||||||
|
}(c)
|
||||||
|
|
||||||
c, err = ss.StreamConn(c, metadata)
|
c, err = ss.StreamConn(c, metadata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -79,6 +88,11 @@ func (ss *Socks5) DialContext(ctx context.Context, metadata *C.Metadata, opts ..
|
|||||||
return NewConn(c, ss), nil
|
return NewConn(c, ss), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SupportWithDialer implements C.ProxyAdapter
|
||||||
|
func (ss *Socks5) SupportWithDialer() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (ss *Socks5) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
func (ss *Socks5) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
||||||
c, err := dialer.DialContext(ctx, "tcp", ss.addr, ss.Base.DialOptions(opts...)...)
|
c, err := dialer.DialContext(ctx, "tcp", ss.addr, ss.Base.DialOptions(opts...)...)
|
||||||
@ -89,11 +103,15 @@ func (ss *Socks5) ListenPacketContext(ctx context.Context, metadata *C.Metadata,
|
|||||||
|
|
||||||
if ss.tls {
|
if ss.tls {
|
||||||
cc := tls.Client(c, ss.tlsConfig)
|
cc := tls.Client(c, ss.tlsConfig)
|
||||||
err = cc.Handshake()
|
ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTLSTimeout)
|
||||||
|
defer cancel()
|
||||||
|
err = cc.HandshakeContext(ctx)
|
||||||
c = cc
|
c = cc
|
||||||
}
|
}
|
||||||
|
|
||||||
defer safeConnClose(c, err)
|
defer func(c net.Conn) {
|
||||||
|
safeConnClose(c, err)
|
||||||
|
}(c)
|
||||||
|
|
||||||
tcpKeepAlive(c)
|
tcpKeepAlive(c)
|
||||||
var user *socks5.User
|
var user *socks5.User
|
||||||
@ -110,7 +128,21 @@ func (ss *Socks5) ListenPacketContext(ctx context.Context, metadata *C.Metadata,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
pc, err := dialer.ListenPacket(ctx, "udp", "", ss.Base.DialOptions(opts...)...)
|
// Support unspecified UDP bind address.
|
||||||
|
bindUDPAddr := bindAddr.UDPAddr()
|
||||||
|
if bindUDPAddr == nil {
|
||||||
|
err = errors.New("invalid UDP bind address")
|
||||||
|
return
|
||||||
|
} else if bindUDPAddr.IP.IsUnspecified() {
|
||||||
|
serverAddr, err := resolveUDPAddr(ctx, "udp", ss.Addr())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
bindUDPAddr.IP = serverAddr.IP
|
||||||
|
}
|
||||||
|
|
||||||
|
pc, err := dialer.ListenPacket(ctx, dialer.ParseNetwork("udp", bindUDPAddr.AddrPort().Addr()), "", ss.Base.DialOptions(opts...)...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -123,20 +155,6 @@ func (ss *Socks5) ListenPacketContext(ctx context.Context, metadata *C.Metadata,
|
|||||||
pc.Close()
|
pc.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Support unspecified UDP bind address.
|
|
||||||
bindUDPAddr := bindAddr.UDPAddr()
|
|
||||||
if bindUDPAddr == nil {
|
|
||||||
err = errors.New("invalid UDP bind address")
|
|
||||||
return
|
|
||||||
} else if bindUDPAddr.IP.IsUnspecified() {
|
|
||||||
serverAddr, err := resolveUDPAddr("udp", ss.Addr())
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
bindUDPAddr.IP = serverAddr.IP
|
|
||||||
}
|
|
||||||
|
|
||||||
return newPacketConn(&socksPacketConn{PacketConn: pc, rAddr: bindUDPAddr, tcpConn: c}, ss), nil
|
return newPacketConn(&socksPacketConn{PacketConn: pc, rAddr: bindUDPAddr, tcpConn: c}, ss), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,12 +4,12 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
tlsC "github.com/Dreamacro/clash/component/tls"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/transport/gun"
|
"github.com/Dreamacro/clash/transport/gun"
|
||||||
"github.com/Dreamacro/clash/transport/trojan"
|
"github.com/Dreamacro/clash/transport/trojan"
|
||||||
@ -120,14 +120,20 @@ func (t *Trojan) DialContext(ctx context.Context, metadata *C.Metadata, opts ...
|
|||||||
|
|
||||||
return NewConn(c, t), nil
|
return NewConn(c, t), nil
|
||||||
}
|
}
|
||||||
|
return t.DialContextWithDialer(ctx, dialer.NewDialer(t.Base.DialOptions(opts...)...), metadata)
|
||||||
|
}
|
||||||
|
|
||||||
c, err := dialer.DialContext(ctx, "tcp", t.addr, t.Base.DialOptions(opts...)...)
|
// DialContextWithDialer implements C.ProxyAdapter
|
||||||
|
func (t *Trojan) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
tcpKeepAlive(c)
|
||||||
|
|
||||||
defer safeConnClose(c, err)
|
defer func(c net.Conn) {
|
||||||
|
safeConnClose(c, err)
|
||||||
|
}(c)
|
||||||
|
|
||||||
c, err = t.StreamConn(c, metadata)
|
c, err = t.StreamConn(c, metadata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -147,18 +153,33 @@ func (t *Trojan) ListenPacketContext(ctx context.Context, metadata *C.Metadata,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error: %w", t.addr, err)
|
return nil, fmt.Errorf("%s connect error: %w", t.addr, err)
|
||||||
}
|
}
|
||||||
defer safeConnClose(c, err)
|
defer func(c net.Conn) {
|
||||||
} else {
|
safeConnClose(c, err)
|
||||||
c, err = dialer.DialContext(ctx, "tcp", t.addr, t.Base.DialOptions(opts...)...)
|
}(c)
|
||||||
|
err = t.instance.WriteHeader(c, trojan.CommandUDP, serializesSocksAddr(metadata))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error: %w", t.addr, err)
|
return nil, err
|
||||||
}
|
|
||||||
defer safeConnClose(c, err)
|
|
||||||
tcpKeepAlive(c)
|
|
||||||
c, err = t.plainStream(c)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%s connect error: %w", t.addr, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pc := t.instance.PacketConn(c)
|
||||||
|
return newPacketConn(pc, t), err
|
||||||
|
}
|
||||||
|
return t.ListenPacketWithDialer(ctx, dialer.NewDialer(t.Base.DialOptions(opts...)...), metadata)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListenPacketWithDialer implements C.ProxyAdapter
|
||||||
|
func (t *Trojan) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
|
c, err := dialer.DialContext(ctx, "tcp", t.addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("%s connect error: %w", t.addr, err)
|
||||||
|
}
|
||||||
|
defer func(c net.Conn) {
|
||||||
|
safeConnClose(c, err)
|
||||||
|
}(c)
|
||||||
|
tcpKeepAlive(c)
|
||||||
|
c, err = t.plainStream(c)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("%s connect error: %w", t.addr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = t.instance.WriteHeader(c, trojan.CommandUDP, serializesSocksAddr(metadata))
|
err = t.instance.WriteHeader(c, trojan.CommandUDP, serializesSocksAddr(metadata))
|
||||||
@ -170,6 +191,11 @@ func (t *Trojan) ListenPacketContext(ctx context.Context, metadata *C.Metadata,
|
|||||||
return newPacketConn(pc, t), err
|
return newPacketConn(pc, t), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SupportWithDialer implements C.ProxyAdapter
|
||||||
|
func (t *Trojan) SupportWithDialer() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
||||||
func (t *Trojan) ListenPacketOnStreamConn(c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
func (t *Trojan) ListenPacketOnStreamConn(c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
pc := t.instance.PacketConn(c)
|
pc := t.instance.PacketConn(c)
|
||||||
@ -193,13 +219,16 @@ func NewTrojan(option TrojanOption) (*Trojan, error) {
|
|||||||
Fingerprint: option.Fingerprint,
|
Fingerprint: option.Fingerprint,
|
||||||
}
|
}
|
||||||
|
|
||||||
if option.Network != "ws" && len(option.Flow) >= 16 {
|
switch option.Network {
|
||||||
option.Flow = option.Flow[:16]
|
case "", "tcp":
|
||||||
switch option.Flow {
|
if len(option.Flow) >= 16 {
|
||||||
case vless.XRO, vless.XRD, vless.XRS:
|
option.Flow = option.Flow[:16]
|
||||||
tOption.Flow = option.Flow
|
switch option.Flow {
|
||||||
default:
|
case vless.XRO, vless.XRD, vless.XRS:
|
||||||
return nil, fmt.Errorf("unsupported xtls flow type: %s", option.Flow)
|
tOption.Flow = option.Flow
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("unsupported xtls flow type: %s", option.Flow)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -247,11 +276,7 @@ func NewTrojan(option TrojanOption) (*Trojan, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if t.option.Flow != "" {
|
t.transport = gun.NewHTTP2Client(dialFn, tlsConfig)
|
||||||
t.transport = gun.NewHTTP2XTLSClient(dialFn, tlsConfig)
|
|
||||||
} else {
|
|
||||||
t.transport = gun.NewHTTP2Client(dialFn, tlsConfig)
|
|
||||||
}
|
|
||||||
|
|
||||||
t.gunTLSConfig = tlsConfig
|
t.gunTLSConfig = tlsConfig
|
||||||
t.gunConfig = &gun.Config{
|
t.gunConfig = &gun.Config{
|
||||||
|
242
adapter/outbound/tuic.go
Normal file
242
adapter/outbound/tuic.go
Normal file
@ -0,0 +1,242 @@
|
|||||||
|
package outbound
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/sha256"
|
||||||
|
"crypto/tls"
|
||||||
|
"encoding/hex"
|
||||||
|
"encoding/pem"
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/metacubex/quic-go"
|
||||||
|
|
||||||
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
"github.com/Dreamacro/clash/transport/tuic"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Tuic struct {
|
||||||
|
*Base
|
||||||
|
client *tuic.PoolClient
|
||||||
|
}
|
||||||
|
|
||||||
|
type TuicOption struct {
|
||||||
|
BasicOption
|
||||||
|
Name string `proxy:"name"`
|
||||||
|
Server string `proxy:"server"`
|
||||||
|
Port int `proxy:"port"`
|
||||||
|
Token string `proxy:"token"`
|
||||||
|
Ip string `proxy:"ip,omitempty"`
|
||||||
|
HeartbeatInterval int `proxy:"heartbeat-interval,omitempty"`
|
||||||
|
ALPN []string `proxy:"alpn,omitempty"`
|
||||||
|
ReduceRtt bool `proxy:"reduce-rtt,omitempty"`
|
||||||
|
RequestTimeout int `proxy:"request-timeout,omitempty"`
|
||||||
|
UdpRelayMode string `proxy:"udp-relay-mode,omitempty"`
|
||||||
|
CongestionController string `proxy:"congestion-controller,omitempty"`
|
||||||
|
DisableSni bool `proxy:"disable-sni,omitempty"`
|
||||||
|
MaxUdpRelayPacketSize int `proxy:"max-udp-relay-packet-size,omitempty"`
|
||||||
|
|
||||||
|
FastOpen bool `proxy:"fast-open,omitempty"`
|
||||||
|
MaxOpenStreams int `proxy:"max-open-streams,omitempty"`
|
||||||
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
||||||
|
Fingerprint string `proxy:"fingerprint,omitempty"`
|
||||||
|
CustomCA string `proxy:"ca,omitempty"`
|
||||||
|
CustomCAString string `proxy:"ca-str,omitempty"`
|
||||||
|
ReceiveWindowConn int `proxy:"recv-window-conn,omitempty"`
|
||||||
|
ReceiveWindow int `proxy:"recv-window,omitempty"`
|
||||||
|
DisableMTUDiscovery bool `proxy:"disable-mtu-discovery,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// DialContext implements C.ProxyAdapter
|
||||||
|
func (t *Tuic) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
||||||
|
return t.DialContextWithDialer(ctx, dialer.NewDialer(t.Base.DialOptions(opts...)...), 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 {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return NewConn(conn, t), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
|
func (t *Tuic) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
||||||
|
return t.ListenPacketWithDialer(ctx, dialer.NewDialer(t.Base.DialOptions(opts...)...), metadata)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListenPacketWithDialer implements C.ProxyAdapter
|
||||||
|
func (t *Tuic) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
|
pc, err := t.client.ListenPacketWithDialer(ctx, metadata, dialer, t.dialWithDialer)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return newPacketConn(pc, t), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SupportWithDialer implements C.ProxyAdapter
|
||||||
|
func (t *Tuic) SupportWithDialer() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Tuic) dial(ctx context.Context, opts ...dialer.Option) (pc net.PacketConn, addr net.Addr, err error) {
|
||||||
|
return t.dialWithDialer(ctx, dialer.NewDialer(opts...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Tuic) dialWithDialer(ctx context.Context, dialer C.Dialer) (pc net.PacketConn, addr net.Addr, err error) {
|
||||||
|
udpAddr, err := resolveUDPAddrWithPrefer(ctx, "udp", t.addr, t.prefer)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
addr = udpAddr
|
||||||
|
pc, err = dialer.ListenPacket(ctx, "udp", "", udpAddr.AddrPort())
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTuic(option TuicOption) (*Tuic, error) {
|
||||||
|
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
||||||
|
serverName := option.Server
|
||||||
|
|
||||||
|
tlsConfig := &tls.Config{
|
||||||
|
ServerName: serverName,
|
||||||
|
InsecureSkipVerify: option.SkipCertVerify,
|
||||||
|
MinVersion: tls.VersionTLS13,
|
||||||
|
}
|
||||||
|
|
||||||
|
var bs []byte
|
||||||
|
var err error
|
||||||
|
if len(option.CustomCA) > 0 {
|
||||||
|
bs, err = os.ReadFile(option.CustomCA)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("tuic %s load ca error: %w", addr, err)
|
||||||
|
}
|
||||||
|
} else if option.CustomCAString != "" {
|
||||||
|
bs = []byte(option.CustomCAString)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(bs) > 0 {
|
||||||
|
block, _ := pem.Decode(bs)
|
||||||
|
if block == nil {
|
||||||
|
return nil, fmt.Errorf("CA cert is not PEM")
|
||||||
|
}
|
||||||
|
|
||||||
|
fpBytes := sha256.Sum256(block.Bytes)
|
||||||
|
if len(option.Fingerprint) == 0 {
|
||||||
|
option.Fingerprint = hex.EncodeToString(fpBytes[:])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(option.Fingerprint) != 0 {
|
||||||
|
var err error
|
||||||
|
tlsConfig, err = tlsC.GetSpecifiedFingerprintTLSConfig(tlsConfig, option.Fingerprint)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
tlsConfig = tlsC.GetGlobalFingerprintTLCConfig(tlsConfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(option.ALPN) > 0 {
|
||||||
|
tlsConfig.NextProtos = option.ALPN
|
||||||
|
} else {
|
||||||
|
tlsConfig.NextProtos = []string{"h3"}
|
||||||
|
}
|
||||||
|
|
||||||
|
if option.RequestTimeout == 0 {
|
||||||
|
option.RequestTimeout = 8000
|
||||||
|
}
|
||||||
|
|
||||||
|
if option.HeartbeatInterval <= 0 {
|
||||||
|
option.HeartbeatInterval = 10000
|
||||||
|
}
|
||||||
|
|
||||||
|
if option.UdpRelayMode != "quic" {
|
||||||
|
option.UdpRelayMode = "native"
|
||||||
|
}
|
||||||
|
|
||||||
|
if option.MaxUdpRelayPacketSize == 0 {
|
||||||
|
option.MaxUdpRelayPacketSize = 1500
|
||||||
|
}
|
||||||
|
|
||||||
|
if option.MaxOpenStreams == 0 {
|
||||||
|
option.MaxOpenStreams = 100
|
||||||
|
}
|
||||||
|
|
||||||
|
// ensure server's incoming stream can handle correctly, increase to 1.1x
|
||||||
|
quicMaxOpenStreams := int64(option.MaxOpenStreams)
|
||||||
|
quicMaxOpenStreams = quicMaxOpenStreams + int64(math.Ceil(float64(quicMaxOpenStreams)/10.0))
|
||||||
|
quicConfig := &quic.Config{
|
||||||
|
InitialStreamReceiveWindow: uint64(option.ReceiveWindowConn),
|
||||||
|
MaxStreamReceiveWindow: uint64(option.ReceiveWindowConn),
|
||||||
|
InitialConnectionReceiveWindow: uint64(option.ReceiveWindow),
|
||||||
|
MaxConnectionReceiveWindow: uint64(option.ReceiveWindow),
|
||||||
|
MaxIncomingStreams: quicMaxOpenStreams,
|
||||||
|
MaxIncomingUniStreams: quicMaxOpenStreams,
|
||||||
|
KeepAlivePeriod: time.Duration(option.HeartbeatInterval) * time.Millisecond,
|
||||||
|
DisablePathMTUDiscovery: option.DisableMTUDiscovery,
|
||||||
|
EnableDatagrams: true,
|
||||||
|
}
|
||||||
|
if option.ReceiveWindowConn == 0 {
|
||||||
|
quicConfig.InitialStreamReceiveWindow = tuic.DefaultStreamReceiveWindow / 10
|
||||||
|
quicConfig.MaxStreamReceiveWindow = tuic.DefaultStreamReceiveWindow
|
||||||
|
}
|
||||||
|
if option.ReceiveWindow == 0 {
|
||||||
|
quicConfig.InitialConnectionReceiveWindow = tuic.DefaultConnectionReceiveWindow / 10
|
||||||
|
quicConfig.MaxConnectionReceiveWindow = tuic.DefaultConnectionReceiveWindow
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(option.Ip) > 0 {
|
||||||
|
addr = net.JoinHostPort(option.Ip, strconv.Itoa(option.Port))
|
||||||
|
}
|
||||||
|
host := option.Server
|
||||||
|
if option.DisableSni {
|
||||||
|
host = ""
|
||||||
|
tlsConfig.ServerName = ""
|
||||||
|
}
|
||||||
|
tkn := tuic.GenTKN(option.Token)
|
||||||
|
|
||||||
|
t := &Tuic{
|
||||||
|
Base: &Base{
|
||||||
|
name: option.Name,
|
||||||
|
addr: addr,
|
||||||
|
tp: C.Tuic,
|
||||||
|
udp: true,
|
||||||
|
tfo: option.FastOpen,
|
||||||
|
iface: option.Interface,
|
||||||
|
prefer: C.NewDNSPrefer(option.IPVersion),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
// to avoid tuic's "too many open streams", decrease to 0.9x
|
||||||
|
clientMaxOpenStreams := int64(option.MaxOpenStreams)
|
||||||
|
clientMaxOpenStreams = clientMaxOpenStreams - int64(math.Ceil(float64(clientMaxOpenStreams)/10.0))
|
||||||
|
if clientMaxOpenStreams < 1 {
|
||||||
|
clientMaxOpenStreams = 1
|
||||||
|
}
|
||||||
|
clientOption := &tuic.ClientOption{
|
||||||
|
TlsConfig: tlsConfig,
|
||||||
|
QuicConfig: quicConfig,
|
||||||
|
Host: host,
|
||||||
|
Token: tkn,
|
||||||
|
UdpRelayMode: option.UdpRelayMode,
|
||||||
|
CongestionController: option.CongestionController,
|
||||||
|
ReduceRtt: option.ReduceRtt,
|
||||||
|
RequestTimeout: time.Duration(option.RequestTimeout) * time.Millisecond,
|
||||||
|
MaxUdpRelayPacketSize: option.MaxUdpRelayPacketSize,
|
||||||
|
FastOpen: option.FastOpen,
|
||||||
|
MaxOpenStreams: clientMaxOpenStreams,
|
||||||
|
}
|
||||||
|
|
||||||
|
t.client = tuic.NewPoolClient(clientOption)
|
||||||
|
|
||||||
|
return t, nil
|
||||||
|
}
|
@ -2,6 +2,7 @@ package outbound
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
xtls "github.com/xtls/go"
|
xtls "github.com/xtls/go"
|
||||||
"net"
|
"net"
|
||||||
@ -44,10 +45,11 @@ func getClientXSessionCache() xtls.ClientSessionCache {
|
|||||||
|
|
||||||
func serializesSocksAddr(metadata *C.Metadata) []byte {
|
func serializesSocksAddr(metadata *C.Metadata) []byte {
|
||||||
var buf [][]byte
|
var buf [][]byte
|
||||||
aType := uint8(metadata.AddrType)
|
addrType := metadata.AddrType()
|
||||||
|
aType := uint8(addrType)
|
||||||
p, _ := strconv.ParseUint(metadata.DstPort, 10, 16)
|
p, _ := strconv.ParseUint(metadata.DstPort, 10, 16)
|
||||||
port := []byte{uint8(p >> 8), uint8(p & 0xff)}
|
port := []byte{uint8(p >> 8), uint8(p & 0xff)}
|
||||||
switch metadata.AddrType {
|
switch addrType {
|
||||||
case socks5.AtypDomainName:
|
case socks5.AtypDomainName:
|
||||||
lenM := uint8(len(metadata.Host))
|
lenM := uint8(len(metadata.Host))
|
||||||
host := []byte(metadata.Host)
|
host := []byte(metadata.Host)
|
||||||
@ -62,34 +64,34 @@ func serializesSocksAddr(metadata *C.Metadata) []byte {
|
|||||||
return bytes.Join(buf, nil)
|
return bytes.Join(buf, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveUDPAddr(network, address string) (*net.UDPAddr, error) {
|
func resolveUDPAddr(ctx context.Context, network, address string) (*net.UDPAddr, error) {
|
||||||
host, port, err := net.SplitHostPort(address)
|
host, port, err := net.SplitHostPort(address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
ip, err := resolver.ResolveProxyServerHost(host)
|
ip, err := resolver.ResolveProxyServerHost(ctx, host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return net.ResolveUDPAddr(network, net.JoinHostPort(ip.String(), port))
|
return net.ResolveUDPAddr(network, net.JoinHostPort(ip.String(), port))
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveUDPAddrWithPrefer(network, address string, prefer C.DNSPrefer) (*net.UDPAddr, error) {
|
func resolveUDPAddrWithPrefer(ctx context.Context, network, address string, prefer C.DNSPrefer) (*net.UDPAddr, error) {
|
||||||
host, port, err := net.SplitHostPort(address)
|
host, port, err := net.SplitHostPort(address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var ip netip.Addr
|
var ip netip.Addr
|
||||||
|
var fallback netip.Addr
|
||||||
switch prefer {
|
switch prefer {
|
||||||
case C.IPv4Only:
|
case C.IPv4Only:
|
||||||
ip, err = resolver.ResolveIPv4ProxyServerHost(host)
|
ip, err = resolver.ResolveIPv4ProxyServerHost(ctx, host)
|
||||||
case C.IPv6Only:
|
case C.IPv6Only:
|
||||||
ip, err = resolver.ResolveIPv6ProxyServerHost(host)
|
ip, err = resolver.ResolveIPv6ProxyServerHost(ctx, host)
|
||||||
case C.IPv6Prefer:
|
case C.IPv6Prefer:
|
||||||
var ips []netip.Addr
|
var ips []netip.Addr
|
||||||
ips, err = resolver.ResolveAllIPProxyServerHost(host)
|
ips, err = resolver.LookupIPProxyServerHost(ctx, host)
|
||||||
var fallback netip.Addr
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
for _, addr := range ips {
|
for _, addr := range ips {
|
||||||
if addr.Is6() {
|
if addr.Is6() {
|
||||||
@ -101,13 +103,11 @@ func resolveUDPAddrWithPrefer(network, address string, prefer C.DNSPrefer) (*net
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ip = fallback
|
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
// C.IPv4Prefer, C.DualStack and other
|
// C.IPv4Prefer, C.DualStack and other
|
||||||
var ips []netip.Addr
|
var ips []netip.Addr
|
||||||
ips, err = resolver.ResolveAllIPProxyServerHost(host)
|
ips, err = resolver.LookupIPProxyServerHost(ctx, host)
|
||||||
var fallback netip.Addr
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
for _, addr := range ips {
|
for _, addr := range ips {
|
||||||
if addr.Is4() {
|
if addr.Is4() {
|
||||||
@ -120,12 +120,13 @@ func resolveUDPAddrWithPrefer(network, address string, prefer C.DNSPrefer) (*net
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !ip.IsValid() && fallback.IsValid() {
|
|
||||||
ip = fallback
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !ip.IsValid() && fallback.IsValid() {
|
||||||
|
ip = fallback
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -133,7 +134,7 @@ func resolveUDPAddrWithPrefer(network, address string, prefer C.DNSPrefer) (*net
|
|||||||
}
|
}
|
||||||
|
|
||||||
func safeConnClose(c net.Conn, err error) {
|
func safeConnClose(c net.Conn, err error) {
|
||||||
if err != nil {
|
if err != nil && c != nil {
|
||||||
_ = c.Close()
|
_ = c.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,18 +6,23 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Dreamacro/clash/common/convert"
|
|
||||||
tlsC "github.com/Dreamacro/clash/component/tls"
|
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
vmessSing "github.com/sagernet/sing-vmess"
|
||||||
|
"github.com/sagernet/sing-vmess/packetaddr"
|
||||||
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
|
|
||||||
|
"github.com/Dreamacro/clash/common/convert"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
"github.com/Dreamacro/clash/component/resolver"
|
"github.com/Dreamacro/clash/component/resolver"
|
||||||
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/transport/gun"
|
"github.com/Dreamacro/clash/transport/gun"
|
||||||
|
"github.com/Dreamacro/clash/transport/socks5"
|
||||||
"github.com/Dreamacro/clash/transport/vless"
|
"github.com/Dreamacro/clash/transport/vless"
|
||||||
"github.com/Dreamacro/clash/transport/vmess"
|
"github.com/Dreamacro/clash/transport/vmess"
|
||||||
)
|
)
|
||||||
@ -48,6 +53,9 @@ type VlessOption struct {
|
|||||||
FlowShow bool `proxy:"flow-show,omitempty"`
|
FlowShow bool `proxy:"flow-show,omitempty"`
|
||||||
TLS bool `proxy:"tls,omitempty"`
|
TLS bool `proxy:"tls,omitempty"`
|
||||||
UDP bool `proxy:"udp,omitempty"`
|
UDP bool `proxy:"udp,omitempty"`
|
||||||
|
PacketAddr bool `proxy:"packet-addr,omitempty"`
|
||||||
|
XUDP bool `proxy:"xudp,omitempty"`
|
||||||
|
PacketEncoding string `proxy:"packet-encoding,omitempty"`
|
||||||
Network string `proxy:"network,omitempty"`
|
Network string `proxy:"network,omitempty"`
|
||||||
HTTPOpts HTTPOptions `proxy:"http-opts,omitempty"`
|
HTTPOpts HTTPOptions `proxy:"http-opts,omitempty"`
|
||||||
HTTP2Opts HTTP2Options `proxy:"h2-opts,omitempty"`
|
HTTP2Opts HTTP2Options `proxy:"h2-opts,omitempty"`
|
||||||
@ -136,11 +144,7 @@ func (v *Vless) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
|||||||
|
|
||||||
c, err = vmess.StreamH2Conn(c, h2Opts)
|
c, err = vmess.StreamH2Conn(c, h2Opts)
|
||||||
case "grpc":
|
case "grpc":
|
||||||
if v.isXTLSEnabled() {
|
c, err = gun.StreamGunWithConn(c, v.gunTLSConfig, v.gunConfig)
|
||||||
c, err = gun.StreamGunWithXTLSConn(c, v.gunTLSConfig, v.gunConfig)
|
|
||||||
} else {
|
|
||||||
c, err = gun.StreamGunWithConn(c, v.gunTLSConfig, v.gunConfig)
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
// default tcp network
|
// default tcp network
|
||||||
// handle TLS And XTLS
|
// handle TLS And XTLS
|
||||||
@ -151,21 +155,17 @@ func (v *Vless) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return v.client.StreamConn(c, parseVlessAddr(metadata))
|
return v.client.StreamConn(c, parseVlessAddr(metadata, v.option.XUDP))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Vless) streamTLSOrXTLSConn(conn net.Conn, isH2 bool) (net.Conn, error) {
|
func (v *Vless) streamTLSOrXTLSConn(conn net.Conn, isH2 bool) (net.Conn, error) {
|
||||||
host, _, _ := net.SplitHostPort(v.addr)
|
host, _, _ := net.SplitHostPort(v.addr)
|
||||||
|
|
||||||
if v.isXTLSEnabled() {
|
if v.isXTLSEnabled() && !isH2 {
|
||||||
xtlsOpts := vless.XTLSConfig{
|
xtlsOpts := vless.XTLSConfig{
|
||||||
Host: host,
|
Host: host,
|
||||||
SkipCertVerify: v.option.SkipCertVerify,
|
SkipCertVerify: v.option.SkipCertVerify,
|
||||||
FingerPrint: v.option.Fingerprint,
|
Fingerprint: v.option.Fingerprint,
|
||||||
}
|
|
||||||
|
|
||||||
if isH2 {
|
|
||||||
xtlsOpts.NextProtos = []string{"h2"}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.option.ServerName != "" {
|
if v.option.ServerName != "" {
|
||||||
@ -207,22 +207,30 @@ func (v *Vless) DialContext(ctx context.Context, metadata *C.Metadata, opts ...d
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer safeConnClose(c, err)
|
defer func(c net.Conn) {
|
||||||
|
safeConnClose(c, err)
|
||||||
|
}(c)
|
||||||
|
|
||||||
c, err = v.client.StreamConn(c, parseVlessAddr(metadata))
|
c, err = v.client.StreamConn(c, parseVlessAddr(metadata, v.option.XUDP))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return NewConn(c, v), nil
|
return NewConn(c, v), nil
|
||||||
}
|
}
|
||||||
|
return v.DialContextWithDialer(ctx, dialer.NewDialer(v.Base.DialOptions(opts...)...), metadata)
|
||||||
|
}
|
||||||
|
|
||||||
c, err := dialer.DialContext(ctx, "tcp", v.addr, v.Base.DialOptions(opts...)...)
|
// DialContextWithDialer implements C.ProxyAdapter
|
||||||
|
func (v *Vless) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
|
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())
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
tcpKeepAlive(c)
|
||||||
defer safeConnClose(c, err)
|
defer func(c net.Conn) {
|
||||||
|
safeConnClose(c, err)
|
||||||
|
}(c)
|
||||||
|
|
||||||
c, err = v.StreamConn(c, metadata)
|
c, err = v.StreamConn(c, metadata)
|
||||||
return NewConn(c, v), err
|
return NewConn(c, v), err
|
||||||
@ -232,7 +240,7 @@ func (v *Vless) DialContext(ctx context.Context, metadata *C.Metadata, opts ...d
|
|||||||
func (v *Vless) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
func (v *Vless) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
||||||
// vless use stream-oriented udp with a special address, so we needs a net.UDPAddr
|
// vless use stream-oriented udp with a special address, so we needs a net.UDPAddr
|
||||||
if !metadata.Resolved() {
|
if !metadata.Resolved() {
|
||||||
ip, err := resolver.ResolveIP(metadata.Host)
|
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New("can't resolve ip")
|
return nil, errors.New("can't resolve ip")
|
||||||
}
|
}
|
||||||
@ -246,17 +254,55 @@ func (v *Vless) ListenPacketContext(ctx context.Context, metadata *C.Metadata, o
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer safeConnClose(c, err)
|
defer func(c net.Conn) {
|
||||||
|
safeConnClose(c, err)
|
||||||
|
}(c)
|
||||||
|
|
||||||
c, err = v.client.StreamConn(c, parseVlessAddr(metadata))
|
if v.option.PacketAddr {
|
||||||
} else {
|
packetAddrMetadata := *metadata // make a copy
|
||||||
c, err = dialer.DialContext(ctx, "tcp", v.addr, v.Base.DialOptions(opts...)...)
|
packetAddrMetadata.Host = packetaddr.SeqPacketMagicAddress
|
||||||
if err != nil {
|
packetAddrMetadata.DstPort = "443"
|
||||||
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
|
||||||
|
c, err = v.client.StreamConn(c, parseVlessAddr(&packetAddrMetadata, false))
|
||||||
|
} else {
|
||||||
|
c, err = v.client.StreamConn(c, parseVlessAddr(metadata, v.option.XUDP))
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
|
||||||
defer safeConnClose(c, err)
|
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("new vless client error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return v.ListenPacketOnStreamConn(c, metadata)
|
||||||
|
}
|
||||||
|
return v.ListenPacketWithDialer(ctx, dialer.NewDialer(v.Base.DialOptions(opts...)...), metadata)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListenPacketWithDialer implements C.ProxyAdapter
|
||||||
|
func (v *Vless) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
|
// vless use stream-oriented udp with a special address, so we needs a net.UDPAddr
|
||||||
|
if !metadata.Resolved() {
|
||||||
|
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("can't resolve ip")
|
||||||
|
}
|
||||||
|
metadata.DstIP = ip
|
||||||
|
}
|
||||||
|
c, err := dialer.DialContext(ctx, "tcp", v.addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
||||||
|
}
|
||||||
|
tcpKeepAlive(c)
|
||||||
|
defer func(c net.Conn) {
|
||||||
|
safeConnClose(c, err)
|
||||||
|
}(c)
|
||||||
|
|
||||||
|
if v.option.PacketAddr {
|
||||||
|
packetAddrMetadata := *metadata // make a copy
|
||||||
|
packetAddrMetadata.Host = packetaddr.SeqPacketMagicAddress
|
||||||
|
packetAddrMetadata.DstPort = "443"
|
||||||
|
|
||||||
|
c, err = v.StreamConn(c, &packetAddrMetadata)
|
||||||
|
} else {
|
||||||
c, err = v.StreamConn(c, metadata)
|
c, err = v.StreamConn(c, metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -267,8 +313,24 @@ func (v *Vless) ListenPacketContext(ctx context.Context, metadata *C.Metadata, o
|
|||||||
return v.ListenPacketOnStreamConn(c, metadata)
|
return v.ListenPacketOnStreamConn(c, metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SupportWithDialer implements C.ProxyAdapter
|
||||||
|
func (v *Vless) SupportWithDialer() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
||||||
func (v *Vless) ListenPacketOnStreamConn(c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
func (v *Vless) ListenPacketOnStreamConn(c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
|
if v.option.XUDP {
|
||||||
|
return newPacketConn(&threadSafePacketConn{
|
||||||
|
PacketConn: vmessSing.NewXUDPConn(c, M.ParseSocksaddr(metadata.RemoteAddress())),
|
||||||
|
}, v), nil
|
||||||
|
} else if v.option.PacketAddr {
|
||||||
|
return newPacketConn(&threadSafePacketConn{
|
||||||
|
PacketConn: packetaddr.NewConn(&vlessPacketConn{
|
||||||
|
Conn: c, rAddr: metadata.UDPAddr(),
|
||||||
|
}, M.ParseSocksaddr(metadata.RemoteAddress())),
|
||||||
|
}, v), nil
|
||||||
|
}
|
||||||
return newPacketConn(&vlessPacketConn{Conn: c, rAddr: metadata.UDPAddr()}, v), nil
|
return newPacketConn(&vlessPacketConn{Conn: c, rAddr: metadata.UDPAddr()}, v), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -277,19 +339,19 @@ func (v *Vless) SupportUOT() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseVlessAddr(metadata *C.Metadata) *vless.DstAddr {
|
func parseVlessAddr(metadata *C.Metadata, xudp bool) *vless.DstAddr {
|
||||||
var addrType byte
|
var addrType byte
|
||||||
var addr []byte
|
var addr []byte
|
||||||
switch metadata.AddrType {
|
switch metadata.AddrType() {
|
||||||
case C.AtypIPv4:
|
case socks5.AtypIPv4:
|
||||||
addrType = vless.AtypIPv4
|
addrType = vless.AtypIPv4
|
||||||
addr = make([]byte, net.IPv4len)
|
addr = make([]byte, net.IPv4len)
|
||||||
copy(addr[:], metadata.DstIP.AsSlice())
|
copy(addr[:], metadata.DstIP.AsSlice())
|
||||||
case C.AtypIPv6:
|
case socks5.AtypIPv6:
|
||||||
addrType = vless.AtypIPv6
|
addrType = vless.AtypIPv6
|
||||||
addr = make([]byte, net.IPv6len)
|
addr = make([]byte, net.IPv6len)
|
||||||
copy(addr[:], metadata.DstIP.AsSlice())
|
copy(addr[:], metadata.DstIP.AsSlice())
|
||||||
case C.AtypDomainName:
|
case socks5.AtypDomainName:
|
||||||
addrType = vless.AtypDomainName
|
addrType = vless.AtypDomainName
|
||||||
addr = make([]byte, len(metadata.Host)+1)
|
addr = make([]byte, len(metadata.Host)+1)
|
||||||
addr[0] = byte(len(metadata.Host))
|
addr[0] = byte(len(metadata.Host))
|
||||||
@ -301,7 +363,8 @@ func parseVlessAddr(metadata *C.Metadata) *vless.DstAddr {
|
|||||||
UDP: metadata.NetWork == C.UDP,
|
UDP: metadata.NetWork == C.UDP,
|
||||||
AddrType: addrType,
|
AddrType: addrType,
|
||||||
Addr: addr,
|
Addr: addr,
|
||||||
Port: uint(port),
|
Port: uint16(port),
|
||||||
|
Mux: metadata.NetWork == C.UDP && xudp,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -423,12 +486,23 @@ func NewVless(option VlessOption) (*Vless, error) {
|
|||||||
tp: C.Vless,
|
tp: C.Vless,
|
||||||
udp: option.UDP,
|
udp: option.UDP,
|
||||||
iface: option.Interface,
|
iface: option.Interface,
|
||||||
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: C.NewDNSPrefer(option.IPVersion),
|
||||||
},
|
},
|
||||||
client: client,
|
client: client,
|
||||||
option: &option,
|
option: &option,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch option.PacketEncoding {
|
||||||
|
case "packetaddr", "packet":
|
||||||
|
option.PacketAddr = true
|
||||||
|
case "xudp":
|
||||||
|
option.XUDP = true
|
||||||
|
}
|
||||||
|
if option.XUDP {
|
||||||
|
option.PacketAddr = false
|
||||||
|
}
|
||||||
|
|
||||||
switch option.Network {
|
switch option.Network {
|
||||||
case "h2":
|
case "h2":
|
||||||
if len(option.HTTP2Opts.Host) == 0 {
|
if len(option.HTTP2Opts.Host) == 0 {
|
||||||
@ -461,11 +535,7 @@ func NewVless(option VlessOption) (*Vless, error) {
|
|||||||
|
|
||||||
v.gunTLSConfig = tlsConfig
|
v.gunTLSConfig = tlsConfig
|
||||||
v.gunConfig = gunConfig
|
v.gunConfig = gunConfig
|
||||||
if v.isXTLSEnabled() {
|
v.transport = gun.NewHTTP2Client(dialFn, tlsConfig)
|
||||||
v.transport = gun.NewHTTP2XTLSClient(dialFn, tlsConfig)
|
|
||||||
} else {
|
|
||||||
v.transport = gun.NewHTTP2Client(dialFn, tlsConfig)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return v, nil
|
return v, nil
|
||||||
|
@ -18,10 +18,13 @@ import (
|
|||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/transport/gun"
|
"github.com/Dreamacro/clash/transport/gun"
|
||||||
clashVMess "github.com/Dreamacro/clash/transport/vmess"
|
clashVMess "github.com/Dreamacro/clash/transport/vmess"
|
||||||
|
|
||||||
"github.com/sagernet/sing-vmess/packetaddr"
|
"github.com/sagernet/sing-vmess/packetaddr"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrUDPRemoteAddrMismatch = errors.New("udp packet dropped due to mismatched remote address")
|
||||||
|
|
||||||
type Vmess struct {
|
type Vmess struct {
|
||||||
*Base
|
*Base
|
||||||
client *vmess.Client
|
client *vmess.Client
|
||||||
@ -113,7 +116,6 @@ func (v *Vmess) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
|||||||
if len(v.option.Fingerprint) == 0 {
|
if len(v.option.Fingerprint) == 0 {
|
||||||
wsOpts.TLSConfig = tlsC.GetGlobalFingerprintTLCConfig(tlsConfig)
|
wsOpts.TLSConfig = tlsC.GetGlobalFingerprintTLCConfig(tlsConfig)
|
||||||
} else {
|
} else {
|
||||||
var err error
|
|
||||||
if wsOpts.TLSConfig, err = tlsC.GetSpecifiedFingerprintTLSConfig(tlsConfig, v.option.Fingerprint); err != nil {
|
if wsOpts.TLSConfig, err = tlsC.GetSpecifiedFingerprintTLSConfig(tlsConfig, v.option.Fingerprint); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -218,7 +220,9 @@ func (v *Vmess) DialContext(ctx context.Context, metadata *C.Metadata, opts ...d
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer safeConnClose(c, err)
|
defer func(c net.Conn) {
|
||||||
|
safeConnClose(c, err)
|
||||||
|
}(c)
|
||||||
|
|
||||||
c, err = v.client.DialConn(c, M.ParseSocksaddr(metadata.RemoteAddress()))
|
c, err = v.client.DialConn(c, M.ParseSocksaddr(metadata.RemoteAddress()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -227,13 +231,19 @@ func (v *Vmess) DialContext(ctx context.Context, metadata *C.Metadata, opts ...d
|
|||||||
|
|
||||||
return NewConn(c, v), nil
|
return NewConn(c, v), nil
|
||||||
}
|
}
|
||||||
|
return v.DialContextWithDialer(ctx, dialer.NewDialer(v.Base.DialOptions(opts...)...), metadata)
|
||||||
|
}
|
||||||
|
|
||||||
c, err := dialer.DialContext(ctx, "tcp", v.addr, v.Base.DialOptions(opts...)...)
|
// DialContextWithDialer implements C.ProxyAdapter
|
||||||
|
func (v *Vmess) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
||||||
|
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())
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
tcpKeepAlive(c)
|
||||||
defer safeConnClose(c, err)
|
defer func(c net.Conn) {
|
||||||
|
safeConnClose(c, err)
|
||||||
|
}(c)
|
||||||
|
|
||||||
c, err = v.StreamConn(c, metadata)
|
c, err = v.StreamConn(c, metadata)
|
||||||
return NewConn(c, v), err
|
return NewConn(c, v), err
|
||||||
@ -243,7 +253,7 @@ func (v *Vmess) DialContext(ctx context.Context, metadata *C.Metadata, opts ...d
|
|||||||
func (v *Vmess) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
func (v *Vmess) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
||||||
// vmess use stream-oriented udp with a special address, so we needs a net.UDPAddr
|
// vmess use stream-oriented udp with a special address, so we needs a net.UDPAddr
|
||||||
if !metadata.Resolved() {
|
if !metadata.Resolved() {
|
||||||
ip, err := resolver.ResolveIP(metadata.Host)
|
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New("can't resolve ip")
|
return nil, errors.New("can't resolve ip")
|
||||||
}
|
}
|
||||||
@ -264,34 +274,54 @@ func (v *Vmess) ListenPacketContext(ctx context.Context, metadata *C.Metadata, o
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer safeConnClose(c, err)
|
defer func(c net.Conn) {
|
||||||
|
safeConnClose(c, err)
|
||||||
|
}(c)
|
||||||
|
|
||||||
if v.option.XUDP {
|
if v.option.XUDP {
|
||||||
c, err = v.client.DialXUDPPacketConn(c, M.ParseSocksaddr(metadata.RemoteAddress()))
|
c, err = v.client.DialXUDPPacketConn(c, M.ParseSocksaddr(metadata.RemoteAddress()))
|
||||||
} else {
|
} else {
|
||||||
c, err = v.client.DialPacketConn(c, M.ParseSocksaddr(metadata.RemoteAddress()))
|
c, err = v.client.DialPacketConn(c, M.ParseSocksaddr(metadata.RemoteAddress()))
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
c, err = dialer.DialContext(ctx, "tcp", v.addr, v.Base.DialOptions(opts...)...)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
|
||||||
}
|
|
||||||
tcpKeepAlive(c)
|
|
||||||
defer safeConnClose(c, err)
|
|
||||||
|
|
||||||
c, err = v.StreamConn(c, metadata)
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("new vmess client error: %v", err)
|
||||||
|
}
|
||||||
|
return v.ListenPacketOnStreamConn(c, metadata)
|
||||||
|
}
|
||||||
|
return v.ListenPacketWithDialer(ctx, dialer.NewDialer(v.Base.DialOptions(opts...)...), metadata)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListenPacketWithDialer implements C.ProxyAdapter
|
||||||
|
func (v *Vmess) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
|
// vmess use stream-oriented udp with a special address, so we needs a net.UDPAddr
|
||||||
|
if !metadata.Resolved() {
|
||||||
|
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("can't resolve ip")
|
||||||
|
}
|
||||||
|
metadata.DstIP = ip
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c, err := dialer.DialContext(ctx, "tcp", v.addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
||||||
|
}
|
||||||
|
tcpKeepAlive(c)
|
||||||
|
defer func(c net.Conn) {
|
||||||
|
safeConnClose(c, err)
|
||||||
|
}(c)
|
||||||
|
|
||||||
|
c, err = v.StreamConn(c, metadata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("new vmess client error: %v", err)
|
return nil, fmt.Errorf("new vmess client error: %v", err)
|
||||||
}
|
}
|
||||||
|
return v.ListenPacketOnStreamConn(c, metadata)
|
||||||
|
}
|
||||||
|
|
||||||
if v.option.PacketAddr {
|
// SupportWithDialer implements C.ProxyAdapter
|
||||||
return newPacketConn(&threadSafePacketConn{PacketConn: packetaddr.NewBindConn(c)}, v), nil
|
func (v *Vmess) SupportWithDialer() bool {
|
||||||
} else if pc, ok := c.(net.PacketConn); ok {
|
return true
|
||||||
return newPacketConn(&threadSafePacketConn{PacketConn: pc}, v), nil
|
|
||||||
}
|
|
||||||
return newPacketConn(&vmessPacketConn{Conn: c, rAddr: metadata.UDPAddr()}, v), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
||||||
@ -408,7 +438,14 @@ type vmessPacketConn struct {
|
|||||||
access sync.Mutex
|
access sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteTo implments C.PacketConn.WriteTo
|
||||||
|
// Since VMess doesn't support full cone NAT by design, we verify if addr matches uc.rAddr, and drop the packet if not.
|
||||||
func (uc *vmessPacketConn) WriteTo(b []byte, addr net.Addr) (int, error) {
|
func (uc *vmessPacketConn) WriteTo(b []byte, addr net.Addr) (int, error) {
|
||||||
|
allowedAddr := uc.rAddr.(*net.UDPAddr)
|
||||||
|
destAddr := addr.(*net.UDPAddr)
|
||||||
|
if !(allowedAddr.IP.Equal(destAddr.IP) && allowedAddr.Port == destAddr.Port) {
|
||||||
|
return 0, ErrUDPRemoteAddrMismatch
|
||||||
|
}
|
||||||
uc.access.Lock()
|
uc.access.Lock()
|
||||||
defer uc.access.Unlock()
|
defer uc.access.Unlock()
|
||||||
return uc.Conn.Write(b)
|
return uc.Conn.Write(b)
|
||||||
|
258
adapter/outbound/wireguard.go
Normal file
258
adapter/outbound/wireguard.go
Normal file
@ -0,0 +1,258 @@
|
|||||||
|
package outbound
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/netip"
|
||||||
|
"runtime"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
CN "github.com/Dreamacro/clash/common/net"
|
||||||
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
|
"github.com/Dreamacro/clash/component/resolver"
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
"github.com/Dreamacro/clash/listener/sing"
|
||||||
|
|
||||||
|
wireguard "github.com/metacubex/sing-wireguard"
|
||||||
|
|
||||||
|
"github.com/sagernet/sing/common"
|
||||||
|
"github.com/sagernet/sing/common/debug"
|
||||||
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
|
N "github.com/sagernet/sing/common/network"
|
||||||
|
"github.com/sagernet/wireguard-go/device"
|
||||||
|
)
|
||||||
|
|
||||||
|
type WireGuard struct {
|
||||||
|
*Base
|
||||||
|
bind *wireguard.ClientBind
|
||||||
|
device *device.Device
|
||||||
|
tunDevice wireguard.Device
|
||||||
|
dialer *wgDialer
|
||||||
|
startOnce sync.Once
|
||||||
|
startErr error
|
||||||
|
}
|
||||||
|
|
||||||
|
type WireGuardOption struct {
|
||||||
|
BasicOption
|
||||||
|
Name string `proxy:"name"`
|
||||||
|
Server string `proxy:"server"`
|
||||||
|
Port int `proxy:"port"`
|
||||||
|
Ip string `proxy:"ip,omitempty"`
|
||||||
|
Ipv6 string `proxy:"ipv6,omitempty"`
|
||||||
|
PrivateKey string `proxy:"private-key"`
|
||||||
|
PublicKey string `proxy:"public-key"`
|
||||||
|
PreSharedKey string `proxy:"pre-shared-key,omitempty"`
|
||||||
|
Reserved []uint8 `proxy:"reserved,omitempty"`
|
||||||
|
Workers int `proxy:"workers,omitempty"`
|
||||||
|
MTU int `proxy:"mtu,omitempty"`
|
||||||
|
UDP bool `proxy:"udp,omitempty"`
|
||||||
|
PersistentKeepalive int `proxy:"persistent-keepalive,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type wgDialer struct {
|
||||||
|
options []dialer.Option
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *wgDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
||||||
|
return dialer.DialContext(ctx, network, destination.String(), d.options...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *wgDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
||||||
|
return dialer.ListenPacket(ctx, dialer.ParseNetwork("udp", destination.Addr), "", d.options...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewWireGuard(option WireGuardOption) (*WireGuard, error) {
|
||||||
|
outbound := &WireGuard{
|
||||||
|
Base: &Base{
|
||||||
|
name: option.Name,
|
||||||
|
addr: net.JoinHostPort(option.Server, strconv.Itoa(option.Port)),
|
||||||
|
tp: C.WireGuard,
|
||||||
|
udp: option.UDP,
|
||||||
|
iface: option.Interface,
|
||||||
|
rmark: option.RoutingMark,
|
||||||
|
prefer: C.NewDNSPrefer(option.IPVersion),
|
||||||
|
},
|
||||||
|
dialer: &wgDialer{},
|
||||||
|
}
|
||||||
|
runtime.SetFinalizer(outbound, closeWireGuard)
|
||||||
|
|
||||||
|
var reserved [3]uint8
|
||||||
|
if len(option.Reserved) > 0 {
|
||||||
|
if len(option.Reserved) != 3 {
|
||||||
|
return nil, E.New("invalid reserved value, required 3 bytes, got ", len(option.Reserved))
|
||||||
|
}
|
||||||
|
reserved[0] = uint8(option.Reserved[0])
|
||||||
|
reserved[1] = uint8(option.Reserved[1])
|
||||||
|
reserved[2] = uint8(option.Reserved[2])
|
||||||
|
}
|
||||||
|
peerAddr := M.ParseSocksaddrHostPort(option.Server, uint16(option.Port))
|
||||||
|
outbound.bind = wireguard.NewClientBind(context.Background(), outbound.dialer, peerAddr, reserved)
|
||||||
|
localPrefixes := make([]netip.Prefix, 0, 2)
|
||||||
|
if len(option.Ip) > 0 {
|
||||||
|
if !strings.Contains(option.Ip, "/") {
|
||||||
|
option.Ip = option.Ip + "/32"
|
||||||
|
}
|
||||||
|
if prefix, err := netip.ParsePrefix(option.Ip); err == nil {
|
||||||
|
localPrefixes = append(localPrefixes, prefix)
|
||||||
|
} else {
|
||||||
|
return nil, E.Cause(err, "ip address parse error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(option.Ipv6) > 0 {
|
||||||
|
if !strings.Contains(option.Ipv6, "/") {
|
||||||
|
option.Ipv6 = option.Ipv6 + "/128"
|
||||||
|
}
|
||||||
|
if prefix, err := netip.ParsePrefix(option.Ipv6); err == nil {
|
||||||
|
localPrefixes = append(localPrefixes, prefix)
|
||||||
|
} else {
|
||||||
|
return nil, E.Cause(err, "ipv6 address parse error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(localPrefixes) == 0 {
|
||||||
|
return nil, E.New("missing local address")
|
||||||
|
}
|
||||||
|
var privateKey, peerPublicKey, preSharedKey string
|
||||||
|
{
|
||||||
|
bytes, err := base64.StdEncoding.DecodeString(option.PrivateKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, E.Cause(err, "decode private key")
|
||||||
|
}
|
||||||
|
privateKey = hex.EncodeToString(bytes)
|
||||||
|
}
|
||||||
|
{
|
||||||
|
bytes, err := base64.StdEncoding.DecodeString(option.PublicKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, E.Cause(err, "decode peer public key")
|
||||||
|
}
|
||||||
|
peerPublicKey = hex.EncodeToString(bytes)
|
||||||
|
}
|
||||||
|
if option.PreSharedKey != "" {
|
||||||
|
bytes, err := base64.StdEncoding.DecodeString(option.PreSharedKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, E.Cause(err, "decode pre shared key")
|
||||||
|
}
|
||||||
|
preSharedKey = hex.EncodeToString(bytes)
|
||||||
|
}
|
||||||
|
ipcConf := "private_key=" + privateKey
|
||||||
|
ipcConf += "\npublic_key=" + peerPublicKey
|
||||||
|
ipcConf += "\nendpoint=" + peerAddr.String()
|
||||||
|
if preSharedKey != "" {
|
||||||
|
ipcConf += "\npreshared_key=" + preSharedKey
|
||||||
|
}
|
||||||
|
var has4, has6 bool
|
||||||
|
for _, address := range localPrefixes {
|
||||||
|
if address.Addr().Is4() {
|
||||||
|
has4 = true
|
||||||
|
} else {
|
||||||
|
has6 = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if has4 {
|
||||||
|
ipcConf += "\nallowed_ip=0.0.0.0/0"
|
||||||
|
}
|
||||||
|
if has6 {
|
||||||
|
ipcConf += "\nallowed_ip=::/0"
|
||||||
|
}
|
||||||
|
if option.PersistentKeepalive != 0 {
|
||||||
|
ipcConf += fmt.Sprintf("\npersistent_keepalive_interval=%d", option.PersistentKeepalive)
|
||||||
|
}
|
||||||
|
mtu := option.MTU
|
||||||
|
if mtu == 0 {
|
||||||
|
mtu = 1408
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
outbound.tunDevice, err = wireguard.NewStackDevice(localPrefixes, uint32(mtu))
|
||||||
|
if err != nil {
|
||||||
|
return nil, E.Cause(err, "create WireGuard device")
|
||||||
|
}
|
||||||
|
outbound.device = device.NewDevice(outbound.tunDevice, outbound.bind, &device.Logger{
|
||||||
|
Verbosef: func(format string, args ...interface{}) {
|
||||||
|
sing.Logger.Debug(fmt.Sprintf(strings.ToLower(format), args...))
|
||||||
|
},
|
||||||
|
Errorf: func(format string, args ...interface{}) {
|
||||||
|
sing.Logger.Error(fmt.Sprintf(strings.ToLower(format), args...))
|
||||||
|
},
|
||||||
|
}, option.Workers)
|
||||||
|
if debug.Enabled {
|
||||||
|
sing.Logger.Trace("created wireguard ipc conf: \n", ipcConf)
|
||||||
|
}
|
||||||
|
err = outbound.device.IpcSet(ipcConf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, E.Cause(err, "setup wireguard")
|
||||||
|
}
|
||||||
|
//err = outbound.tunDevice.Start()
|
||||||
|
return outbound, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func closeWireGuard(w *WireGuard) {
|
||||||
|
if w.device != nil {
|
||||||
|
w.device.Close()
|
||||||
|
}
|
||||||
|
_ = common.Close(w.tunDevice)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *WireGuard) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
||||||
|
w.dialer.options = opts
|
||||||
|
var conn net.Conn
|
||||||
|
w.startOnce.Do(func() {
|
||||||
|
w.startErr = w.tunDevice.Start()
|
||||||
|
})
|
||||||
|
if w.startErr != nil {
|
||||||
|
return nil, w.startErr
|
||||||
|
}
|
||||||
|
if !metadata.Resolved() {
|
||||||
|
var addrs []netip.Addr
|
||||||
|
addrs, err = resolver.LookupIP(ctx, metadata.Host)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
conn, err = N.DialSerial(ctx, w.tunDevice, "tcp", M.ParseSocksaddr(metadata.RemoteAddress()), addrs)
|
||||||
|
} else {
|
||||||
|
port, _ := strconv.Atoi(metadata.DstPort)
|
||||||
|
conn, err = w.tunDevice.DialContext(ctx, "tcp", M.SocksaddrFrom(metadata.DstIP, uint16(port)))
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if conn == nil {
|
||||||
|
return nil, E.New("conn is nil")
|
||||||
|
}
|
||||||
|
return NewConn(CN.NewRefConn(conn, w), w), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *WireGuard) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
||||||
|
w.dialer.options = opts
|
||||||
|
var pc net.PacketConn
|
||||||
|
w.startOnce.Do(func() {
|
||||||
|
w.startErr = w.tunDevice.Start()
|
||||||
|
})
|
||||||
|
if w.startErr != nil {
|
||||||
|
return nil, w.startErr
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !metadata.Resolved() {
|
||||||
|
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("can't resolve ip")
|
||||||
|
}
|
||||||
|
metadata.DstIP = ip
|
||||||
|
}
|
||||||
|
port, _ := strconv.Atoi(metadata.DstPort)
|
||||||
|
pc, err = w.tunDevice.ListenPacket(ctx, M.SocksaddrFrom(metadata.DstIP, uint16(port)))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if pc == nil {
|
||||||
|
return nil, E.New("packetConn is nil")
|
||||||
|
}
|
||||||
|
return newPacketConn(CN.NewRefPacketConn(pc, w), w), nil
|
||||||
|
}
|
@ -4,11 +4,12 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapter/outbound"
|
"github.com/Dreamacro/clash/adapter/outbound"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/constant/provider"
|
"github.com/Dreamacro/clash/constant/provider"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Fallback struct {
|
type Fallback struct {
|
||||||
@ -131,6 +132,8 @@ func NewFallback(option *GroupCommonOption, providers []provider.ProxyProvider)
|
|||||||
RoutingMark: option.RoutingMark,
|
RoutingMark: option.RoutingMark,
|
||||||
},
|
},
|
||||||
option.Filter,
|
option.Filter,
|
||||||
|
option.ExcludeFilter,
|
||||||
|
option.ExcludeType,
|
||||||
providers,
|
providers,
|
||||||
}),
|
}),
|
||||||
disableUDP: option.DisableUDP,
|
disableUDP: option.DisableUDP,
|
||||||
|
@ -3,38 +3,53 @@ package outboundgroup
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapter/outbound"
|
"github.com/Dreamacro/clash/adapter/outbound"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/constant/provider"
|
"github.com/Dreamacro/clash/constant/provider"
|
||||||
types "github.com/Dreamacro/clash/constant/provider"
|
types "github.com/Dreamacro/clash/constant/provider"
|
||||||
"github.com/Dreamacro/clash/log"
|
"github.com/Dreamacro/clash/log"
|
||||||
"github.com/Dreamacro/clash/tunnel"
|
"github.com/Dreamacro/clash/tunnel"
|
||||||
|
|
||||||
"github.com/dlclark/regexp2"
|
"github.com/dlclark/regexp2"
|
||||||
"go.uber.org/atomic"
|
"go.uber.org/atomic"
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type GroupBase struct {
|
type GroupBase struct {
|
||||||
*outbound.Base
|
*outbound.Base
|
||||||
filterRegs []*regexp2.Regexp
|
filterRegs []*regexp2.Regexp
|
||||||
providers []provider.ProxyProvider
|
excludeFilterReg *regexp2.Regexp
|
||||||
failedTestMux sync.Mutex
|
excludeTypeArray []string
|
||||||
failedTimes int
|
providers []provider.ProxyProvider
|
||||||
failedTime time.Time
|
failedTestMux sync.Mutex
|
||||||
failedTesting *atomic.Bool
|
failedTimes int
|
||||||
proxies [][]C.Proxy
|
failedTime time.Time
|
||||||
versions []atomic.Uint32
|
failedTesting *atomic.Bool
|
||||||
|
proxies [][]C.Proxy
|
||||||
|
versions []atomic.Uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
type GroupBaseOption struct {
|
type GroupBaseOption struct {
|
||||||
outbound.BaseOption
|
outbound.BaseOption
|
||||||
filter string
|
filter string
|
||||||
providers []provider.ProxyProvider
|
excludeFilter string
|
||||||
|
excludeType string
|
||||||
|
providers []provider.ProxyProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGroupBase(opt GroupBaseOption) *GroupBase {
|
func NewGroupBase(opt GroupBaseOption) *GroupBase {
|
||||||
|
var excludeFilterReg *regexp2.Regexp
|
||||||
|
if opt.excludeFilter != "" {
|
||||||
|
excludeFilterReg = regexp2.MustCompile(opt.excludeFilter, 0)
|
||||||
|
}
|
||||||
|
var excludeTypeArray []string
|
||||||
|
if opt.excludeType != "" {
|
||||||
|
excludeTypeArray = strings.Split(opt.excludeType, "|")
|
||||||
|
}
|
||||||
|
|
||||||
var filterRegs []*regexp2.Regexp
|
var filterRegs []*regexp2.Regexp
|
||||||
if opt.filter != "" {
|
if opt.filter != "" {
|
||||||
for _, filter := range strings.Split(opt.filter, "`") {
|
for _, filter := range strings.Split(opt.filter, "`") {
|
||||||
@ -44,10 +59,12 @@ func NewGroupBase(opt GroupBaseOption) *GroupBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
gb := &GroupBase{
|
gb := &GroupBase{
|
||||||
Base: outbound.NewBase(opt.BaseOption),
|
Base: outbound.NewBase(opt.BaseOption),
|
||||||
filterRegs: filterRegs,
|
filterRegs: filterRegs,
|
||||||
providers: opt.providers,
|
excludeFilterReg: excludeFilterReg,
|
||||||
failedTesting: atomic.NewBool(false),
|
excludeTypeArray: excludeTypeArray,
|
||||||
|
providers: opt.providers,
|
||||||
|
failedTesting: atomic.NewBool(false),
|
||||||
}
|
}
|
||||||
|
|
||||||
gb.proxies = make([][]C.Proxy, len(opt.providers))
|
gb.proxies = make([][]C.Proxy, len(opt.providers))
|
||||||
@ -63,59 +80,54 @@ func (gb *GroupBase) Touch() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
|
func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
|
||||||
|
var proxies []C.Proxy
|
||||||
if len(gb.filterRegs) == 0 {
|
if len(gb.filterRegs) == 0 {
|
||||||
var proxies []C.Proxy
|
|
||||||
for _, pd := range gb.providers {
|
for _, pd := range gb.providers {
|
||||||
if touch {
|
if touch {
|
||||||
pd.Touch()
|
pd.Touch()
|
||||||
}
|
}
|
||||||
proxies = append(proxies, pd.Proxies()...)
|
proxies = append(proxies, pd.Proxies()...)
|
||||||
}
|
}
|
||||||
if len(proxies) == 0 {
|
} else {
|
||||||
return append(proxies, tunnel.Proxies()["COMPATIBLE"])
|
for i, pd := range gb.providers {
|
||||||
}
|
if touch {
|
||||||
return proxies
|
pd.Touch()
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, pd := range gb.providers {
|
if pd.VehicleType() == types.Compatible {
|
||||||
if touch {
|
gb.versions[i].Store(pd.Version())
|
||||||
pd.Touch()
|
gb.proxies[i] = pd.Proxies()
|
||||||
}
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
if pd.VehicleType() == types.Compatible {
|
version := gb.versions[i].Load()
|
||||||
gb.versions[i].Store(pd.Version())
|
if version != pd.Version() && gb.versions[i].CompareAndSwap(version, pd.Version()) {
|
||||||
gb.proxies[i] = pd.Proxies()
|
var (
|
||||||
continue
|
proxies []C.Proxy
|
||||||
}
|
newProxies []C.Proxy
|
||||||
|
)
|
||||||
|
|
||||||
version := gb.versions[i].Load()
|
proxies = pd.Proxies()
|
||||||
if version != pd.Version() && gb.versions[i].CompareAndSwap(version, pd.Version()) {
|
proxiesSet := map[string]struct{}{}
|
||||||
var (
|
for _, filterReg := range gb.filterRegs {
|
||||||
proxies []C.Proxy
|
for _, p := range proxies {
|
||||||
newProxies []C.Proxy
|
name := p.Name()
|
||||||
)
|
if mat, _ := filterReg.FindStringMatch(name); mat != nil {
|
||||||
|
if _, ok := proxiesSet[name]; !ok {
|
||||||
proxies = pd.Proxies()
|
proxiesSet[name] = struct{}{}
|
||||||
proxiesSet := map[string]struct{}{}
|
newProxies = append(newProxies, p)
|
||||||
for _, filterReg := range gb.filterRegs {
|
}
|
||||||
for _, p := range proxies {
|
|
||||||
name := p.Name()
|
|
||||||
if mat, _ := filterReg.FindStringMatch(name); mat != nil {
|
|
||||||
if _, ok := proxiesSet[name]; !ok {
|
|
||||||
proxiesSet[name] = struct{}{}
|
|
||||||
newProxies = append(newProxies, p)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gb.proxies[i] = newProxies
|
||||||
}
|
}
|
||||||
|
|
||||||
gb.proxies[i] = newProxies
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
var proxies []C.Proxy
|
for _, p := range gb.proxies {
|
||||||
for _, p := range gb.proxies {
|
proxies = append(proxies, p...)
|
||||||
proxies = append(proxies, p...)
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(proxies) == 0 {
|
if len(proxies) == 0 {
|
||||||
@ -145,6 +157,36 @@ func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
|
|||||||
}
|
}
|
||||||
proxies = newProxies
|
proxies = newProxies
|
||||||
}
|
}
|
||||||
|
if gb.excludeTypeArray != nil {
|
||||||
|
var newProxies []C.Proxy
|
||||||
|
for _, p := range proxies {
|
||||||
|
mType := p.Type().String()
|
||||||
|
flag := false
|
||||||
|
for i := range gb.excludeTypeArray {
|
||||||
|
if strings.EqualFold(mType, gb.excludeTypeArray[i]) {
|
||||||
|
flag = true
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
if flag {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
newProxies = append(newProxies, p)
|
||||||
|
}
|
||||||
|
proxies = newProxies
|
||||||
|
}
|
||||||
|
|
||||||
|
if gb.excludeFilterReg != nil {
|
||||||
|
var newProxies []C.Proxy
|
||||||
|
for _, p := range proxies {
|
||||||
|
name := p.Name()
|
||||||
|
if mat, _ := gb.excludeFilterReg.FindStringMatch(name); mat != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
newProxies = append(newProxies, p)
|
||||||
|
}
|
||||||
|
proxies = newProxies
|
||||||
|
}
|
||||||
|
|
||||||
return proxies
|
return proxies
|
||||||
}
|
}
|
||||||
|
@ -5,11 +5,11 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Dreamacro/clash/common/cache"
|
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapter/outbound"
|
"github.com/Dreamacro/clash/adapter/outbound"
|
||||||
|
"github.com/Dreamacro/clash/common/cache"
|
||||||
"github.com/Dreamacro/clash/common/murmur3"
|
"github.com/Dreamacro/clash/common/murmur3"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
@ -157,7 +157,7 @@ func strategyConsistentHashing() strategyFn {
|
|||||||
func strategyStickySessions() strategyFn {
|
func strategyStickySessions() strategyFn {
|
||||||
ttl := time.Minute * 10
|
ttl := time.Minute * 10
|
||||||
maxRetry := 5
|
maxRetry := 5
|
||||||
lruCache := cache.NewLRUCache[uint64, int](
|
lruCache := cache.New[uint64, int](
|
||||||
cache.WithAge[uint64, int](int64(ttl.Seconds())),
|
cache.WithAge[uint64, int](int64(ttl.Seconds())),
|
||||||
cache.WithSize[uint64, int](1000))
|
cache.WithSize[uint64, int](1000))
|
||||||
return func(proxies []C.Proxy, metadata *C.Metadata) C.Proxy {
|
return func(proxies []C.Proxy, metadata *C.Metadata) C.Proxy {
|
||||||
@ -228,6 +228,8 @@ func NewLoadBalance(option *GroupCommonOption, providers []provider.ProxyProvide
|
|||||||
RoutingMark: option.RoutingMark,
|
RoutingMark: option.RoutingMark,
|
||||||
},
|
},
|
||||||
option.Filter,
|
option.Filter,
|
||||||
|
option.ExcludeFilter,
|
||||||
|
option.ExcludeType,
|
||||||
providers,
|
providers,
|
||||||
}),
|
}),
|
||||||
strategyFn: strategyFn,
|
strategyFn: strategyFn,
|
||||||
|
@ -21,15 +21,17 @@ var (
|
|||||||
|
|
||||||
type GroupCommonOption struct {
|
type GroupCommonOption struct {
|
||||||
outbound.BasicOption
|
outbound.BasicOption
|
||||||
Name string `group:"name"`
|
Name string `group:"name"`
|
||||||
Type string `group:"type"`
|
Type string `group:"type"`
|
||||||
Proxies []string `group:"proxies,omitempty"`
|
Proxies []string `group:"proxies,omitempty"`
|
||||||
Use []string `group:"use,omitempty"`
|
Use []string `group:"use,omitempty"`
|
||||||
URL string `group:"url,omitempty"`
|
URL string `group:"url,omitempty"`
|
||||||
Interval int `group:"interval,omitempty"`
|
Interval int `group:"interval,omitempty"`
|
||||||
Lazy bool `group:"lazy,omitempty"`
|
Lazy bool `group:"lazy,omitempty"`
|
||||||
DisableUDP bool `group:"disable-udp,omitempty"`
|
DisableUDP bool `group:"disable-udp,omitempty"`
|
||||||
Filter string `group:"filter,omitempty"`
|
Filter string `group:"filter,omitempty"`
|
||||||
|
ExcludeFilter string `group:"exclude-filter,omitempty"`
|
||||||
|
ExcludeType string `group:"exclude-type,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, providersMap map[string]types.ProxyProvider) (C.ProxyAdapter, error) {
|
func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, providersMap map[string]types.ProxyProvider) (C.ProxyAdapter, error) {
|
||||||
|
@ -3,9 +3,12 @@ package outboundgroup
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"net"
|
||||||
|
"net/netip"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapter/outbound"
|
"github.com/Dreamacro/clash/adapter/outbound"
|
||||||
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/constant/provider"
|
"github.com/Dreamacro/clash/constant/provider"
|
||||||
@ -15,6 +18,36 @@ type Relay struct {
|
|||||||
*GroupBase
|
*GroupBase
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type proxyDialer struct {
|
||||||
|
proxy C.Proxy
|
||||||
|
dialer C.Dialer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p proxyDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||||
|
currentMeta, err := addrToMetadata(address)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if strings.Contains(network, "udp") { // should not support this operation
|
||||||
|
currentMeta.NetWork = C.UDP
|
||||||
|
pc, err := p.proxy.ListenPacketWithDialer(ctx, p.dialer, currentMeta)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return N.NewBindPacketConn(pc, currentMeta.UDPAddr()), nil
|
||||||
|
}
|
||||||
|
return p.proxy.DialContextWithDialer(ctx, p.dialer, currentMeta)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p proxyDialer) ListenPacket(ctx context.Context, network, address string, rAddrPort netip.AddrPort) (net.PacketConn, error) {
|
||||||
|
currentMeta, err := addrToMetadata(rAddrPort.String())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
currentMeta.NetWork = C.UDP
|
||||||
|
return p.proxy.ListenPacketWithDialer(ctx, p.dialer, currentMeta)
|
||||||
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (r *Relay) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
func (r *Relay) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
||||||
proxies, chainProxies := r.proxies(metadata, true)
|
proxies, chainProxies := r.proxies(metadata, true)
|
||||||
@ -25,37 +58,19 @@ func (r *Relay) DialContext(ctx context.Context, metadata *C.Metadata, opts ...d
|
|||||||
case 1:
|
case 1:
|
||||||
return proxies[0].DialContext(ctx, metadata, r.Base.DialOptions(opts...)...)
|
return proxies[0].DialContext(ctx, metadata, r.Base.DialOptions(opts...)...)
|
||||||
}
|
}
|
||||||
|
var d C.Dialer
|
||||||
first := proxies[0]
|
d = dialer.NewDialer(r.Base.DialOptions(opts...)...)
|
||||||
|
for _, proxy := range proxies[:len(proxies)-1] {
|
||||||
|
d = proxyDialer{
|
||||||
|
proxy: proxy,
|
||||||
|
dialer: d,
|
||||||
|
}
|
||||||
|
}
|
||||||
last := proxies[len(proxies)-1]
|
last := proxies[len(proxies)-1]
|
||||||
|
conn, err := last.DialContextWithDialer(ctx, d, metadata)
|
||||||
c, err := dialer.DialContext(ctx, "tcp", first.Addr(), r.Base.DialOptions(opts...)...)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error: %w", first.Addr(), err)
|
return nil, err
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
|
||||||
|
|
||||||
var currentMeta *C.Metadata
|
|
||||||
for _, proxy := range proxies[1:] {
|
|
||||||
currentMeta, err = addrToMetadata(proxy.Addr())
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
c, err = first.StreamConn(c, currentMeta)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%s connect error: %w", first.Addr(), err)
|
|
||||||
}
|
|
||||||
|
|
||||||
first = proxy
|
|
||||||
}
|
|
||||||
|
|
||||||
c, err = last.StreamConn(c, metadata)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%s connect error: %w", last.Addr(), err)
|
|
||||||
}
|
|
||||||
|
|
||||||
conn := outbound.NewConn(c, last)
|
|
||||||
|
|
||||||
for i := len(chainProxies) - 2; i >= 0; i-- {
|
for i := len(chainProxies) - 2; i >= 0; i-- {
|
||||||
conn.AppendToChains(chainProxies[i])
|
conn.AppendToChains(chainProxies[i])
|
||||||
@ -77,39 +92,18 @@ func (r *Relay) ListenPacketContext(ctx context.Context, metadata *C.Metadata, o
|
|||||||
return proxies[0].ListenPacketContext(ctx, metadata, r.Base.DialOptions(opts...)...)
|
return proxies[0].ListenPacketContext(ctx, metadata, r.Base.DialOptions(opts...)...)
|
||||||
}
|
}
|
||||||
|
|
||||||
first := proxies[0]
|
var d C.Dialer
|
||||||
|
d = dialer.NewDialer(r.Base.DialOptions(opts...)...)
|
||||||
|
for _, proxy := range proxies[:len(proxies)-1] {
|
||||||
|
d = proxyDialer{
|
||||||
|
proxy: proxy,
|
||||||
|
dialer: d,
|
||||||
|
}
|
||||||
|
}
|
||||||
last := proxies[len(proxies)-1]
|
last := proxies[len(proxies)-1]
|
||||||
|
pc, err := last.ListenPacketWithDialer(ctx, d, metadata)
|
||||||
c, err := dialer.DialContext(ctx, "tcp", first.Addr(), r.Base.DialOptions(opts...)...)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error: %w", first.Addr(), err)
|
return nil, err
|
||||||
}
|
|
||||||
tcpKeepAlive(c)
|
|
||||||
|
|
||||||
var currentMeta *C.Metadata
|
|
||||||
for _, proxy := range proxies[1:] {
|
|
||||||
currentMeta, err = addrToMetadata(proxy.Addr())
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
c, err = first.StreamConn(c, currentMeta)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%s connect error: %w", first.Addr(), err)
|
|
||||||
}
|
|
||||||
|
|
||||||
first = proxy
|
|
||||||
}
|
|
||||||
|
|
||||||
c, err = last.StreamConn(c, metadata)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%s connect error: %w", last.Addr(), err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var pc C.PacketConn
|
|
||||||
pc, err = last.ListenPacketOnStreamConn(c, metadata)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%s connect error: %w", first.Addr(), err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := len(chainProxies) - 2; i >= 0; i-- {
|
for i := len(chainProxies) - 2; i >= 0; i-- {
|
||||||
@ -127,8 +121,19 @@ func (r *Relay) SupportUDP() bool {
|
|||||||
if len(proxies) == 0 { // C.Direct
|
if len(proxies) == 0 { // C.Direct
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
last := proxies[len(proxies)-1]
|
for i := len(proxies) - 1; i >= 0; i-- {
|
||||||
return last.SupportUDP() && last.SupportUOT()
|
proxy := proxies[i]
|
||||||
|
if !proxy.SupportUDP() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if proxy.SupportUOT() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if !proxy.SupportWithDialer() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSON implements C.ProxyAdapter
|
// MarshalJSON implements C.ProxyAdapter
|
||||||
@ -185,6 +190,8 @@ func NewRelay(option *GroupCommonOption, providers []provider.ProxyProvider) *Re
|
|||||||
RoutingMark: option.RoutingMark,
|
RoutingMark: option.RoutingMark,
|
||||||
},
|
},
|
||||||
"",
|
"",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
providers,
|
providers,
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
@ -99,6 +99,8 @@ func NewSelector(option *GroupCommonOption, providers []provider.ProxyProvider)
|
|||||||
RoutingMark: option.RoutingMark,
|
RoutingMark: option.RoutingMark,
|
||||||
},
|
},
|
||||||
option.Filter,
|
option.Filter,
|
||||||
|
option.ExcludeFilter,
|
||||||
|
option.ExcludeType,
|
||||||
providers,
|
providers,
|
||||||
}),
|
}),
|
||||||
selected: "COMPATIBLE",
|
selected: "COMPATIBLE",
|
||||||
|
@ -143,6 +143,8 @@ func NewURLTest(option *GroupCommonOption, providers []provider.ProxyProvider, o
|
|||||||
},
|
},
|
||||||
|
|
||||||
option.Filter,
|
option.Filter,
|
||||||
|
option.ExcludeFilter,
|
||||||
|
option.ExcludeType,
|
||||||
providers,
|
providers,
|
||||||
}),
|
}),
|
||||||
fastSingle: singledo.NewSingle[C.Proxy](time.Second * 10),
|
fastSingle: singledo.NewSingle[C.Proxy](time.Second * 10),
|
||||||
|
@ -16,32 +16,19 @@ func addrToMetadata(rawAddress string) (addr *C.Metadata, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ip, err := netip.ParseAddr(host)
|
if ip, err := netip.ParseAddr(host); err != nil {
|
||||||
if err != nil {
|
|
||||||
addr = &C.Metadata{
|
addr = &C.Metadata{
|
||||||
AddrType: C.AtypDomainName,
|
Host: host,
|
||||||
Host: host,
|
DstPort: port,
|
||||||
DstIP: netip.Addr{},
|
|
||||||
DstPort: port,
|
|
||||||
}
|
}
|
||||||
err = nil
|
} else {
|
||||||
return
|
|
||||||
} else if ip.Is4() {
|
|
||||||
addr = &C.Metadata{
|
addr = &C.Metadata{
|
||||||
AddrType: C.AtypIPv4,
|
Host: "",
|
||||||
Host: "",
|
DstIP: ip.Unmap(),
|
||||||
DstIP: ip,
|
DstPort: port,
|
||||||
DstPort: port,
|
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
addr = &C.Metadata{
|
|
||||||
AddrType: C.AtypIPv6,
|
|
||||||
Host: "",
|
|
||||||
DstIP: ip,
|
|
||||||
DstPort: port,
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,14 +2,13 @@ package adapter
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapter/outbound"
|
"github.com/Dreamacro/clash/adapter/outbound"
|
||||||
"github.com/Dreamacro/clash/common/structure"
|
"github.com/Dreamacro/clash/common/structure"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ParseProxy(mapping map[string]any) (C.Proxy, error) {
|
func ParseProxy(mapping map[string]any) (C.Proxy, error) {
|
||||||
decoder := structure.NewDecoder(structure.Option{TagName: "proxy", WeaklyTypedInput: true})
|
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")
|
||||||
@ -88,6 +87,20 @@ func ParseProxy(mapping map[string]any) (C.Proxy, error) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
proxy, err = outbound.NewHysteria(*hyOption)
|
proxy, err = outbound.NewHysteria(*hyOption)
|
||||||
|
case "wireguard":
|
||||||
|
wgOption := &outbound.WireGuardOption{}
|
||||||
|
err = decoder.Decode(mapping, wgOption)
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
proxy, err = outbound.NewWireGuard(*wgOption)
|
||||||
|
case "tuic":
|
||||||
|
tuicOption := &outbound.TuicOption{}
|
||||||
|
err = decoder.Decode(mapping, tuicOption)
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
proxy, err = outbound.NewTuic(*tuicOption)
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unsupport proxy type: %s", proxyType)
|
return nil, fmt.Errorf("unsupport proxy type: %s", proxyType)
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,10 @@ package provider
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Dreamacro/clash/component/resource"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/structure"
|
"github.com/Dreamacro/clash/common/structure"
|
||||||
|
"github.com/Dreamacro/clash/component/resource"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
types "github.com/Dreamacro/clash/constant/provider"
|
types "github.com/Dreamacro/clash/constant/provider"
|
||||||
)
|
)
|
||||||
@ -27,6 +27,7 @@ type proxyProviderSchema struct {
|
|||||||
Interval int `provider:"interval,omitempty"`
|
Interval int `provider:"interval,omitempty"`
|
||||||
Filter string `provider:"filter,omitempty"`
|
Filter string `provider:"filter,omitempty"`
|
||||||
ExcludeFilter string `provider:"exclude-filter,omitempty"`
|
ExcludeFilter string `provider:"exclude-filter,omitempty"`
|
||||||
|
ExcludeType string `provider:"exclude-type,omitempty"`
|
||||||
HealthCheck healthCheckSchema `provider:"health-check,omitempty"`
|
HealthCheck healthCheckSchema `provider:"health-check,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,5 +64,7 @@ func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvide
|
|||||||
interval := time.Duration(uint(schema.Interval)) * time.Second
|
interval := time.Duration(uint(schema.Interval)) * time.Second
|
||||||
filter := schema.Filter
|
filter := schema.Filter
|
||||||
excludeFilter := schema.ExcludeFilter
|
excludeFilter := schema.ExcludeFilter
|
||||||
return NewProxySetProvider(name, interval, filter, excludeFilter, vehicle, hc)
|
excludeType := schema.ExcludeType
|
||||||
|
|
||||||
|
return NewProxySetProvider(name, interval, filter, excludeFilter, excludeType, vehicle, hc)
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,24 @@
|
|||||||
package provider
|
package provider
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Dreamacro/clash/common/convert"
|
"net/http"
|
||||||
"github.com/Dreamacro/clash/component/resource"
|
|
||||||
"github.com/dlclark/regexp2"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapter"
|
"github.com/Dreamacro/clash/adapter"
|
||||||
|
"github.com/Dreamacro/clash/common/convert"
|
||||||
|
clashHttp "github.com/Dreamacro/clash/component/http"
|
||||||
|
"github.com/Dreamacro/clash/component/resource"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
types "github.com/Dreamacro/clash/constant/provider"
|
types "github.com/Dreamacro/clash/constant/provider"
|
||||||
|
"github.com/Dreamacro/clash/log"
|
||||||
|
|
||||||
|
"github.com/dlclark/regexp2"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -33,18 +37,20 @@ type ProxySetProvider struct {
|
|||||||
|
|
||||||
type proxySetProvider struct {
|
type proxySetProvider struct {
|
||||||
*resource.Fetcher[[]C.Proxy]
|
*resource.Fetcher[[]C.Proxy]
|
||||||
proxies []C.Proxy
|
proxies []C.Proxy
|
||||||
healthCheck *HealthCheck
|
healthCheck *HealthCheck
|
||||||
version uint32
|
version uint32
|
||||||
|
subscriptionInfo *SubscriptionInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pp *proxySetProvider) MarshalJSON() ([]byte, error) {
|
func (pp *proxySetProvider) MarshalJSON() ([]byte, error) {
|
||||||
return json.Marshal(map[string]any{
|
return json.Marshal(map[string]any{
|
||||||
"name": pp.Name(),
|
"name": pp.Name(),
|
||||||
"type": pp.Type().String(),
|
"type": pp.Type().String(),
|
||||||
"vehicleType": pp.VehicleType().String(),
|
"vehicleType": pp.VehicleType().String(),
|
||||||
"proxies": pp.Proxies(),
|
"proxies": pp.Proxies(),
|
||||||
"updatedAt": pp.UpdatedAt,
|
"updatedAt": pp.UpdatedAt,
|
||||||
|
"subscriptionInfo": pp.subscriptionInfo,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,16 +103,55 @@ func (pp *proxySetProvider) setProxies(proxies []C.Proxy) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (pp *proxySetProvider) getSubscriptionInfo() {
|
||||||
|
if pp.VehicleType() != types.HTTP {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*90)
|
||||||
|
defer cancel()
|
||||||
|
resp, err := clashHttp.HttpRequest(ctx, pp.Vehicle().(*resource.HTTPVehicle).Url(),
|
||||||
|
http.MethodGet, http.Header{"User-Agent": {"clash"}}, nil)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
userInfoStr := strings.TrimSpace(resp.Header.Get("subscription-userinfo"))
|
||||||
|
if userInfoStr == "" {
|
||||||
|
resp2, err := clashHttp.HttpRequest(ctx, pp.Vehicle().(*resource.HTTPVehicle).Url(),
|
||||||
|
http.MethodGet, http.Header{"User-Agent": {"Quantumultx"}}, nil)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer resp2.Body.Close()
|
||||||
|
userInfoStr = strings.TrimSpace(resp2.Header.Get("subscription-userinfo"))
|
||||||
|
if userInfoStr == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pp.subscriptionInfo, err = NewSubscriptionInfo(userInfoStr)
|
||||||
|
if err != nil {
|
||||||
|
log.Warnln("[Provider] get subscription-userinfo: %e", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
func stopProxyProvider(pd *ProxySetProvider) {
|
func stopProxyProvider(pd *ProxySetProvider) {
|
||||||
pd.healthCheck.close()
|
pd.healthCheck.close()
|
||||||
_ = pd.Fetcher.Destroy()
|
_ = pd.Fetcher.Destroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProxySetProvider(name string, interval time.Duration, filter string, excludeFilter string, vehicle types.Vehicle, hc *HealthCheck) (*ProxySetProvider, error) {
|
func NewProxySetProvider(name string, interval time.Duration, filter string, excludeFilter string, excludeType string, vehicle types.Vehicle, hc *HealthCheck) (*ProxySetProvider, error) {
|
||||||
excludeFilterReg, err := regexp2.Compile(excludeFilter, 0)
|
excludeFilterReg, err := regexp2.Compile(excludeFilter, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("invalid excludeFilter regex: %w", err)
|
return nil, fmt.Errorf("invalid excludeFilter regex: %w", err)
|
||||||
}
|
}
|
||||||
|
var excludeTypeArray []string
|
||||||
|
if excludeType != "" {
|
||||||
|
excludeTypeArray = strings.Split(excludeType, "|")
|
||||||
|
}
|
||||||
|
|
||||||
var filterRegs []*regexp2.Regexp
|
var filterRegs []*regexp2.Regexp
|
||||||
for _, filter := range strings.Split(filter, "`") {
|
for _, filter := range strings.Split(filter, "`") {
|
||||||
filterReg, err := regexp2.Compile(filter, 0)
|
filterReg, err := regexp2.Compile(filter, 0)
|
||||||
@ -125,9 +170,10 @@ func NewProxySetProvider(name string, interval time.Duration, filter string, exc
|
|||||||
healthCheck: hc,
|
healthCheck: hc,
|
||||||
}
|
}
|
||||||
|
|
||||||
fetcher := resource.NewFetcher[[]C.Proxy](name, interval, vehicle, proxiesParseAndFilter(filter, excludeFilter, filterRegs, excludeFilterReg), proxiesOnUpdate(pd))
|
fetcher := resource.NewFetcher[[]C.Proxy](name, interval, vehicle, proxiesParseAndFilter(filter, excludeFilter, excludeTypeArray, filterRegs, excludeFilterReg), proxiesOnUpdate(pd))
|
||||||
pd.Fetcher = fetcher
|
pd.Fetcher = fetcher
|
||||||
|
|
||||||
|
pd.getSubscriptionInfo()
|
||||||
wrapper := &ProxySetProvider{pd}
|
wrapper := &ProxySetProvider{pd}
|
||||||
runtime.SetFinalizer(wrapper, stopProxyProvider)
|
runtime.SetFinalizer(wrapper, stopProxyProvider)
|
||||||
return wrapper, nil
|
return wrapper, nil
|
||||||
@ -218,10 +264,11 @@ func proxiesOnUpdate(pd *proxySetProvider) func([]C.Proxy) {
|
|||||||
return func(elm []C.Proxy) {
|
return func(elm []C.Proxy) {
|
||||||
pd.setProxies(elm)
|
pd.setProxies(elm)
|
||||||
pd.version += 1
|
pd.version += 1
|
||||||
|
pd.getSubscriptionInfo()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func proxiesParseAndFilter(filter string, excludeFilter string, filterRegs []*regexp2.Regexp, excludeFilterReg *regexp2.Regexp) resource.Parser[[]C.Proxy] {
|
func proxiesParseAndFilter(filter string, excludeFilter string, excludeTypeArray []string, filterRegs []*regexp2.Regexp, excludeFilterReg *regexp2.Regexp) resource.Parser[[]C.Proxy] {
|
||||||
return func(buf []byte) ([]C.Proxy, error) {
|
return func(buf []byte) ([]C.Proxy, error) {
|
||||||
schema := &ProxySchema{}
|
schema := &ProxySchema{}
|
||||||
|
|
||||||
@ -241,6 +288,27 @@ func proxiesParseAndFilter(filter string, excludeFilter string, filterRegs []*re
|
|||||||
proxiesSet := map[string]struct{}{}
|
proxiesSet := map[string]struct{}{}
|
||||||
for _, filterReg := range filterRegs {
|
for _, filterReg := range filterRegs {
|
||||||
for idx, mapping := range schema.Proxies {
|
for idx, mapping := range schema.Proxies {
|
||||||
|
if nil != excludeTypeArray && len(excludeTypeArray) > 0 {
|
||||||
|
mType, ok := mapping["type"]
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
pType, ok := mType.(string)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
flag := false
|
||||||
|
for i := range excludeTypeArray {
|
||||||
|
if strings.EqualFold(pType, excludeTypeArray[i]) {
|
||||||
|
flag = true
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
if flag {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
mName, ok := mapping["name"]
|
mName, ok := mapping["name"]
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
|
57
adapter/provider/subscription_info.go
Normal file
57
adapter/provider/subscription_info.go
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/dlclark/regexp2"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SubscriptionInfo struct {
|
||||||
|
Upload int64
|
||||||
|
Download int64
|
||||||
|
Total int64
|
||||||
|
Expire int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSubscriptionInfo(str string) (si *SubscriptionInfo, err error) {
|
||||||
|
si = &SubscriptionInfo{}
|
||||||
|
str = strings.ToLower(str)
|
||||||
|
reTraffic := regexp2.MustCompile("upload=(\\d+); download=(\\d+); total=(\\d+)", 0)
|
||||||
|
reExpire := regexp2.MustCompile("expire=(\\d+)", 0)
|
||||||
|
|
||||||
|
match, err := reTraffic.FindStringMatch(str)
|
||||||
|
if err != nil || match == nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
group := match.Groups()
|
||||||
|
si.Upload, err = str2uint64(group[1].String())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
si.Download, err = str2uint64(group[2].String())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
si.Total, err = str2uint64(group[3].String())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
match, _ = reExpire.FindStringMatch(str)
|
||||||
|
if match != nil {
|
||||||
|
group = match.Groups()
|
||||||
|
si.Expire, err = str2uint64(group[1].String())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func str2uint64(str string) (int64, error) {
|
||||||
|
i, err := strconv.ParseInt(str, 10, 64)
|
||||||
|
return i, err
|
||||||
|
}
|
106
common/cache/cache.go
vendored
106
common/cache/cache.go
vendored
@ -1,106 +0,0 @@
|
|||||||
package cache
|
|
||||||
|
|
||||||
import (
|
|
||||||
"runtime"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Cache store element with a expired time
|
|
||||||
type Cache[K comparable, V any] struct {
|
|
||||||
*cache[K, V]
|
|
||||||
}
|
|
||||||
|
|
||||||
type cache[K comparable, V any] struct {
|
|
||||||
mapping sync.Map
|
|
||||||
janitor *janitor[K, V]
|
|
||||||
}
|
|
||||||
|
|
||||||
type element[V any] struct {
|
|
||||||
Expired time.Time
|
|
||||||
Payload V
|
|
||||||
}
|
|
||||||
|
|
||||||
// Put element in Cache with its ttl
|
|
||||||
func (c *cache[K, V]) Put(key K, payload V, ttl time.Duration) {
|
|
||||||
c.mapping.Store(key, &element[V]{
|
|
||||||
Payload: payload,
|
|
||||||
Expired: time.Now().Add(ttl),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get element in Cache, and drop when it expired
|
|
||||||
func (c *cache[K, V]) Get(key K) V {
|
|
||||||
item, exist := c.mapping.Load(key)
|
|
||||||
if !exist {
|
|
||||||
return getZero[V]()
|
|
||||||
}
|
|
||||||
elm := item.(*element[V])
|
|
||||||
// expired
|
|
||||||
if time.Since(elm.Expired) > 0 {
|
|
||||||
c.mapping.Delete(key)
|
|
||||||
return getZero[V]()
|
|
||||||
}
|
|
||||||
return elm.Payload
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetWithExpire element in Cache with Expire Time
|
|
||||||
func (c *cache[K, V]) GetWithExpire(key K) (payload V, expired time.Time) {
|
|
||||||
item, exist := c.mapping.Load(key)
|
|
||||||
if !exist {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
elm := item.(*element[V])
|
|
||||||
// expired
|
|
||||||
if time.Since(elm.Expired) > 0 {
|
|
||||||
c.mapping.Delete(key)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return elm.Payload, elm.Expired
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *cache[K, V]) cleanup() {
|
|
||||||
c.mapping.Range(func(k, v any) bool {
|
|
||||||
key := k.(string)
|
|
||||||
elm := v.(*element[V])
|
|
||||||
if time.Since(elm.Expired) > 0 {
|
|
||||||
c.mapping.Delete(key)
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
type janitor[K comparable, V any] struct {
|
|
||||||
interval time.Duration
|
|
||||||
stop chan struct{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (j *janitor[K, V]) process(c *cache[K, V]) {
|
|
||||||
ticker := time.NewTicker(j.interval)
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ticker.C:
|
|
||||||
c.cleanup()
|
|
||||||
case <-j.stop:
|
|
||||||
ticker.Stop()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func stopJanitor[K comparable, V any](c *Cache[K, V]) {
|
|
||||||
c.janitor.stop <- struct{}{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// New return *Cache
|
|
||||||
func New[K comparable, V any](interval time.Duration) *Cache[K, V] {
|
|
||||||
j := &janitor[K, V]{
|
|
||||||
interval: interval,
|
|
||||||
stop: make(chan struct{}),
|
|
||||||
}
|
|
||||||
c := &cache[K, V]{janitor: j}
|
|
||||||
go j.process(c)
|
|
||||||
C := &Cache[K, V]{c}
|
|
||||||
runtime.SetFinalizer(C, stopJanitor[K, V])
|
|
||||||
return C
|
|
||||||
}
|
|
72
common/cache/cache_test.go
vendored
72
common/cache/cache_test.go
vendored
@ -1,72 +0,0 @@
|
|||||||
package cache
|
|
||||||
|
|
||||||
import (
|
|
||||||
"runtime"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestCache_Basic(t *testing.T) {
|
|
||||||
interval := 200 * time.Millisecond
|
|
||||||
ttl := 20 * time.Millisecond
|
|
||||||
c := New[string, int](interval)
|
|
||||||
c.Put("int", 1, ttl)
|
|
||||||
|
|
||||||
d := New[string, string](interval)
|
|
||||||
d.Put("string", "a", ttl)
|
|
||||||
|
|
||||||
i := c.Get("int")
|
|
||||||
assert.Equal(t, i, 1, "should recv 1")
|
|
||||||
|
|
||||||
s := d.Get("string")
|
|
||||||
assert.Equal(t, s, "a", "should recv 'a'")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCache_TTL(t *testing.T) {
|
|
||||||
interval := 200 * time.Millisecond
|
|
||||||
ttl := 20 * time.Millisecond
|
|
||||||
now := time.Now()
|
|
||||||
c := New[string, int](interval)
|
|
||||||
c.Put("int", 1, ttl)
|
|
||||||
c.Put("int2", 2, ttl)
|
|
||||||
|
|
||||||
i := c.Get("int")
|
|
||||||
_, expired := c.GetWithExpire("int2")
|
|
||||||
assert.Equal(t, i, 1, "should recv 1")
|
|
||||||
assert.True(t, now.Before(expired))
|
|
||||||
|
|
||||||
time.Sleep(ttl * 2)
|
|
||||||
i = c.Get("int")
|
|
||||||
j, _ := c.GetWithExpire("int2")
|
|
||||||
assert.True(t, i == 0, "should recv 0")
|
|
||||||
assert.True(t, j == 0, "should recv 0")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCache_AutoCleanup(t *testing.T) {
|
|
||||||
interval := 10 * time.Millisecond
|
|
||||||
ttl := 15 * time.Millisecond
|
|
||||||
c := New[string, int](interval)
|
|
||||||
c.Put("int", 1, ttl)
|
|
||||||
|
|
||||||
time.Sleep(ttl * 2)
|
|
||||||
i := c.Get("int")
|
|
||||||
j, _ := c.GetWithExpire("int")
|
|
||||||
assert.True(t, i == 0, "should recv 0")
|
|
||||||
assert.True(t, j == 0, "should recv 0")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCache_AutoGC(t *testing.T) {
|
|
||||||
sign := make(chan struct{})
|
|
||||||
go func() {
|
|
||||||
interval := 10 * time.Millisecond
|
|
||||||
ttl := 15 * time.Millisecond
|
|
||||||
c := New[string, int](interval)
|
|
||||||
c.Put("int", 1, ttl)
|
|
||||||
sign <- struct{}{}
|
|
||||||
}()
|
|
||||||
|
|
||||||
<-sign
|
|
||||||
runtime.GC()
|
|
||||||
}
|
|
4
common/cache/lrucache.go
vendored
4
common/cache/lrucache.go
vendored
@ -65,8 +65,8 @@ type LruCache[K comparable, V any] struct {
|
|||||||
onEvict EvictCallback[K, V]
|
onEvict EvictCallback[K, V]
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLRUCache creates an LruCache
|
// New creates an LruCache
|
||||||
func NewLRUCache[K comparable, V any](options ...Option[K, V]) *LruCache[K, V] {
|
func New[K comparable, V any](options ...Option[K, V]) *LruCache[K, V] {
|
||||||
lc := &LruCache[K, V]{
|
lc := &LruCache[K, V]{
|
||||||
lru: list.New[*entry[K, V]](),
|
lru: list.New[*entry[K, V]](),
|
||||||
cache: make(map[K]*list.Element[*entry[K, V]]),
|
cache: make(map[K]*list.Element[*entry[K, V]]),
|
||||||
|
20
common/cache/lrucache_test.go
vendored
20
common/cache/lrucache_test.go
vendored
@ -19,7 +19,7 @@ var entries = []struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestLRUCache(t *testing.T) {
|
func TestLRUCache(t *testing.T) {
|
||||||
c := NewLRUCache[string, string]()
|
c := New[string, string]()
|
||||||
|
|
||||||
for _, e := range entries {
|
for _, e := range entries {
|
||||||
c.Set(e.key, e.value)
|
c.Set(e.key, e.value)
|
||||||
@ -45,7 +45,7 @@ func TestLRUCache(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestLRUMaxAge(t *testing.T) {
|
func TestLRUMaxAge(t *testing.T) {
|
||||||
c := NewLRUCache[string, string](WithAge[string, string](86400))
|
c := New[string, string](WithAge[string, string](86400))
|
||||||
|
|
||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
expected := now + 86400
|
expected := now + 86400
|
||||||
@ -88,7 +88,7 @@ func TestLRUMaxAge(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestLRUpdateOnGet(t *testing.T) {
|
func TestLRUpdateOnGet(t *testing.T) {
|
||||||
c := NewLRUCache[string, string](WithAge[string, string](86400), WithUpdateAgeOnGet[string, string]())
|
c := New[string, string](WithAge[string, string](86400), WithUpdateAgeOnGet[string, string]())
|
||||||
|
|
||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
expires := now + 86400/2
|
expires := now + 86400/2
|
||||||
@ -103,7 +103,7 @@ func TestLRUpdateOnGet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMaxSize(t *testing.T) {
|
func TestMaxSize(t *testing.T) {
|
||||||
c := NewLRUCache[string, string](WithSize[string, string](2))
|
c := New[string, string](WithSize[string, string](2))
|
||||||
// Add one expired entry
|
// Add one expired entry
|
||||||
c.Set("foo", "bar")
|
c.Set("foo", "bar")
|
||||||
_, ok := c.Get("foo")
|
_, ok := c.Get("foo")
|
||||||
@ -117,7 +117,7 @@ func TestMaxSize(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestExist(t *testing.T) {
|
func TestExist(t *testing.T) {
|
||||||
c := NewLRUCache[int, int](WithSize[int, int](1))
|
c := New[int, int](WithSize[int, int](1))
|
||||||
c.Set(1, 2)
|
c.Set(1, 2)
|
||||||
assert.True(t, c.Exist(1))
|
assert.True(t, c.Exist(1))
|
||||||
c.Set(2, 3)
|
c.Set(2, 3)
|
||||||
@ -130,7 +130,7 @@ func TestEvict(t *testing.T) {
|
|||||||
temp = key + value
|
temp = key + value
|
||||||
}
|
}
|
||||||
|
|
||||||
c := NewLRUCache[int, int](WithEvict[int, int](evict), WithSize[int, int](1))
|
c := New[int, int](WithEvict[int, int](evict), WithSize[int, int](1))
|
||||||
c.Set(1, 2)
|
c.Set(1, 2)
|
||||||
c.Set(2, 3)
|
c.Set(2, 3)
|
||||||
|
|
||||||
@ -138,7 +138,7 @@ func TestEvict(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSetWithExpire(t *testing.T) {
|
func TestSetWithExpire(t *testing.T) {
|
||||||
c := NewLRUCache[int, *struct{}](WithAge[int, *struct{}](1))
|
c := New[int, *struct{}](WithAge[int, *struct{}](1))
|
||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
|
|
||||||
tenSecBefore := time.Unix(now-10, 0)
|
tenSecBefore := time.Unix(now-10, 0)
|
||||||
@ -153,7 +153,7 @@ func TestSetWithExpire(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestStale(t *testing.T) {
|
func TestStale(t *testing.T) {
|
||||||
c := NewLRUCache[int, int](WithAge[int, int](1), WithStale[int, int](true))
|
c := New[int, int](WithAge[int, int](1), WithStale[int, int](true))
|
||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
|
|
||||||
tenSecBefore := time.Unix(now-10, 0)
|
tenSecBefore := time.Unix(now-10, 0)
|
||||||
@ -166,11 +166,11 @@ func TestStale(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCloneTo(t *testing.T) {
|
func TestCloneTo(t *testing.T) {
|
||||||
o := NewLRUCache[string, int](WithSize[string, int](10))
|
o := New[string, int](WithSize[string, int](10))
|
||||||
o.Set("1", 1)
|
o.Set("1", 1)
|
||||||
o.Set("2", 2)
|
o.Set("2", 2)
|
||||||
|
|
||||||
n := NewLRUCache[string, int](WithSize[string, int](2))
|
n := New[string, int](WithSize[string, int](2))
|
||||||
n.Set("3", 3)
|
n.Set("3", 3)
|
||||||
n.Set("4", 4)
|
n.Set("4", 4)
|
||||||
|
|
||||||
|
@ -144,14 +144,6 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|||||||
if encryption := query.Get("encryption"); encryption != "" {
|
if encryption := query.Get("encryption"); encryption != "" {
|
||||||
vmess["cipher"] = encryption
|
vmess["cipher"] = encryption
|
||||||
}
|
}
|
||||||
if packetEncoding := query.Get("packetEncoding"); packetEncoding != "" {
|
|
||||||
switch packetEncoding {
|
|
||||||
case "packet":
|
|
||||||
vmess["packet-addr"] = true
|
|
||||||
case "xudp":
|
|
||||||
vmess["xudp"] = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
proxies = append(proxies, vmess)
|
proxies = append(proxies, vmess)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -162,8 +154,11 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|||||||
if jsonDc.Decode(&values) != nil {
|
if jsonDc.Decode(&values) != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
tempName, ok := values["ps"].(string)
|
||||||
name := uniqueName(names, values["ps"].(string))
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
name := uniqueName(names, tempName)
|
||||||
vmess := make(map[string]any, 20)
|
vmess := make(map[string]any, 20)
|
||||||
|
|
||||||
vmess["name"] = name
|
vmess["name"] = name
|
||||||
@ -177,6 +172,7 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|||||||
vmess["alterId"] = 0
|
vmess["alterId"] = 0
|
||||||
}
|
}
|
||||||
vmess["udp"] = true
|
vmess["udp"] = true
|
||||||
|
vmess["xudp"] = true
|
||||||
vmess["tls"] = false
|
vmess["tls"] = false
|
||||||
vmess["skip-cert-verify"] = false
|
vmess["skip-cert-verify"] = false
|
||||||
|
|
||||||
@ -272,22 +268,25 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
cipher = urlSS.User.Username()
|
cipherRaw = urlSS.User.Username()
|
||||||
password string
|
cipher string
|
||||||
|
password string
|
||||||
)
|
)
|
||||||
|
|
||||||
if password, found = urlSS.User.Password(); !found {
|
if password, found = urlSS.User.Password(); !found {
|
||||||
dcBuf, _ := enc.DecodeString(cipher)
|
dcBuf, _ := enc.DecodeString(cipherRaw)
|
||||||
if !strings.Contains(string(dcBuf), "2022-blake3") {
|
|
||||||
dcBuf, _ = encRaw.DecodeString(cipher)
|
|
||||||
}
|
|
||||||
cipher, password, found = strings.Cut(string(dcBuf), ":")
|
cipher, password, found = strings.Cut(string(dcBuf), ":")
|
||||||
if !found {
|
if !found {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
err := VerifyMethod(cipher, password)
|
||||||
|
if err != nil {
|
||||||
|
dcBuf, _ := encRaw.DecodeString(cipherRaw)
|
||||||
|
cipher, password, found = strings.Cut(string(dcBuf), ":")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ss := make(map[string]any, 20)
|
ss := make(map[string]any, 10)
|
||||||
|
|
||||||
ss["name"] = name
|
ss["name"] = name
|
||||||
ss["type"] = scheme
|
ss["type"] = scheme
|
||||||
@ -297,6 +296,9 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|||||||
ss["password"] = password
|
ss["password"] = password
|
||||||
query := urlSS.Query()
|
query := urlSS.Query()
|
||||||
ss["udp"] = true
|
ss["udp"] = true
|
||||||
|
if query.Get("udp-over-tcp") == "true" || query.Get("uot") == "1" {
|
||||||
|
ss["udp-over-tcp"] = true
|
||||||
|
}
|
||||||
if strings.Contains(query.Get("plugin"), "obfs") {
|
if strings.Contains(query.Get("plugin"), "obfs") {
|
||||||
obfsParams := strings.Split(query.Get("plugin"), ";")
|
obfsParams := strings.Split(query.Get("plugin"), ";")
|
||||||
ss["plugin"] = "obfs"
|
ss["plugin"] = "obfs"
|
||||||
|
@ -2,6 +2,7 @@ package convert
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"github.com/metacubex/sing-shadowsocks/shadowimpl"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
@ -314,3 +315,8 @@ func SetUserAgent(header http.Header) {
|
|||||||
userAgent := RandUserAgent()
|
userAgent := RandUserAgent()
|
||||||
header.Set("User-Agent", userAgent)
|
header.Set("User-Agent", userAgent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func VerifyMethod(cipher, password string) (err error) {
|
||||||
|
_, err = shadowimpl.FetchMethod(cipher, password)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
@ -25,6 +25,14 @@ func handleVShareLink(names map[string]int, url *url.URL, scheme string, proxy m
|
|||||||
proxy["servername"] = sni
|
proxy["servername"] = sni
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch query.Get("packetEncoding") {
|
||||||
|
case "none":
|
||||||
|
case "packet":
|
||||||
|
proxy["packet-addr"] = true
|
||||||
|
default:
|
||||||
|
proxy["xudp"] = true
|
||||||
|
}
|
||||||
|
|
||||||
network := strings.ToLower(query.Get("type"))
|
network := strings.ToLower(query.Get("type"))
|
||||||
if network == "" {
|
if network == "" {
|
||||||
network = "tcp"
|
network = "tcp"
|
||||||
|
36
common/net/bind.go
Normal file
36
common/net/bind.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package net
|
||||||
|
|
||||||
|
import "net"
|
||||||
|
|
||||||
|
type bindPacketConn struct {
|
||||||
|
net.PacketConn
|
||||||
|
rAddr net.Addr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wpc *bindPacketConn) Read(b []byte) (n int, err error) {
|
||||||
|
n, _, err = wpc.PacketConn.ReadFrom(b)
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wpc *bindPacketConn) Write(b []byte) (n int, err error) {
|
||||||
|
return wpc.PacketConn.WriteTo(b, wpc.rAddr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wpc *bindPacketConn) RemoteAddr() net.Addr {
|
||||||
|
return wpc.rAddr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wpc *bindPacketConn) LocalAddr() net.Addr {
|
||||||
|
if wpc.PacketConn.LocalAddr() == nil {
|
||||||
|
return &net.UDPAddr{IP: net.IPv4zero, Port: 0}
|
||||||
|
} else {
|
||||||
|
return wpc.PacketConn.LocalAddr()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewBindPacketConn(pc net.PacketConn, rAddr net.Addr) net.Conn {
|
||||||
|
return &bindPacketConn{
|
||||||
|
PacketConn: pc,
|
||||||
|
rAddr: rAddr,
|
||||||
|
}
|
||||||
|
}
|
100
common/net/refconn.go
Normal file
100
common/net/refconn.go
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
package net
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"runtime"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type refConn struct {
|
||||||
|
conn net.Conn
|
||||||
|
ref any
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *refConn) Read(b []byte) (n int, err error) {
|
||||||
|
defer runtime.KeepAlive(c.ref)
|
||||||
|
return c.conn.Read(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *refConn) Write(b []byte) (n int, err error) {
|
||||||
|
defer runtime.KeepAlive(c.ref)
|
||||||
|
return c.conn.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *refConn) Close() error {
|
||||||
|
defer runtime.KeepAlive(c.ref)
|
||||||
|
return c.conn.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *refConn) LocalAddr() net.Addr {
|
||||||
|
defer runtime.KeepAlive(c.ref)
|
||||||
|
return c.conn.LocalAddr()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *refConn) RemoteAddr() net.Addr {
|
||||||
|
defer runtime.KeepAlive(c.ref)
|
||||||
|
return c.conn.RemoteAddr()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *refConn) SetDeadline(t time.Time) error {
|
||||||
|
defer runtime.KeepAlive(c.ref)
|
||||||
|
return c.conn.SetDeadline(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *refConn) SetReadDeadline(t time.Time) error {
|
||||||
|
defer runtime.KeepAlive(c.ref)
|
||||||
|
return c.conn.SetReadDeadline(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *refConn) SetWriteDeadline(t time.Time) error {
|
||||||
|
defer runtime.KeepAlive(c.ref)
|
||||||
|
return c.conn.SetWriteDeadline(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRefConn(conn net.Conn, ref any) net.Conn {
|
||||||
|
return &refConn{conn: conn, ref: ref}
|
||||||
|
}
|
||||||
|
|
||||||
|
type refPacketConn struct {
|
||||||
|
pc net.PacketConn
|
||||||
|
ref any
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pc *refPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
|
||||||
|
defer runtime.KeepAlive(pc.ref)
|
||||||
|
return pc.pc.ReadFrom(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pc *refPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
||||||
|
defer runtime.KeepAlive(pc.ref)
|
||||||
|
return pc.pc.WriteTo(p, addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pc *refPacketConn) Close() error {
|
||||||
|
defer runtime.KeepAlive(pc.ref)
|
||||||
|
return pc.pc.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pc *refPacketConn) LocalAddr() net.Addr {
|
||||||
|
defer runtime.KeepAlive(pc.ref)
|
||||||
|
return pc.pc.LocalAddr()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pc *refPacketConn) SetDeadline(t time.Time) error {
|
||||||
|
defer runtime.KeepAlive(pc.ref)
|
||||||
|
return pc.pc.SetDeadline(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pc *refPacketConn) SetReadDeadline(t time.Time) error {
|
||||||
|
defer runtime.KeepAlive(pc.ref)
|
||||||
|
return pc.pc.SetReadDeadline(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pc *refPacketConn) SetWriteDeadline(t time.Time) error {
|
||||||
|
defer runtime.KeepAlive(pc.ref)
|
||||||
|
return pc.pc.SetWriteDeadline(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRefPacketConn(pc net.PacketConn, ref any) net.PacketConn {
|
||||||
|
return &refPacketConn{pc: pc, ref: ref}
|
||||||
|
}
|
19
common/net/tls.go
Normal file
19
common/net/tls.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package net
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ParseCert(certificate, privateKey string) (tls.Certificate, error) {
|
||||||
|
cert, painTextErr := tls.X509KeyPair([]byte(certificate), []byte(privateKey))
|
||||||
|
if painTextErr == nil {
|
||||||
|
return cert, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
cert, loadErr := tls.LoadX509KeyPair(certificate, privateKey)
|
||||||
|
if loadErr != nil {
|
||||||
|
return tls.Certificate{}, fmt.Errorf("parse certificate failed, maybe format error:%s, or path error: %s", painTextErr.Error(), loadErr.Error())
|
||||||
|
}
|
||||||
|
return cert, nil
|
||||||
|
}
|
@ -3,6 +3,7 @@ package structure
|
|||||||
// references: https://github.com/mitchellh/mapstructure
|
// references: https://github.com/mitchellh/mapstructure
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -13,8 +14,11 @@ import (
|
|||||||
type Option struct {
|
type Option struct {
|
||||||
TagName string
|
TagName string
|
||||||
WeaklyTypedInput bool
|
WeaklyTypedInput bool
|
||||||
|
KeyReplacer *strings.Replacer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var DefaultKeyReplacer = strings.NewReplacer("_", "-")
|
||||||
|
|
||||||
// Decoder is the core of structure
|
// Decoder is the core of structure
|
||||||
type Decoder struct {
|
type Decoder struct {
|
||||||
option *Option
|
option *Option
|
||||||
@ -49,6 +53,23 @@ func (d *Decoder) Decode(src map[string]any, dst any) error {
|
|||||||
omitempty := found && omitKey == "omitempty"
|
omitempty := found && omitKey == "omitempty"
|
||||||
|
|
||||||
value, ok := src[key]
|
value, ok := src[key]
|
||||||
|
if !ok {
|
||||||
|
if d.option.KeyReplacer != nil {
|
||||||
|
key = d.option.KeyReplacer.Replace(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _strKey := range src {
|
||||||
|
strKey := _strKey
|
||||||
|
if d.option.KeyReplacer != nil {
|
||||||
|
strKey = d.option.KeyReplacer.Replace(strKey)
|
||||||
|
}
|
||||||
|
if strings.EqualFold(key, strKey) {
|
||||||
|
value = src[_strKey]
|
||||||
|
ok = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if !ok || value == nil {
|
if !ok || value == nil {
|
||||||
if omitempty {
|
if omitempty {
|
||||||
continue
|
continue
|
||||||
@ -65,9 +86,16 @@ func (d *Decoder) Decode(src map[string]any, dst any) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) decode(name string, data any, val reflect.Value) error {
|
func (d *Decoder) decode(name string, data any, val reflect.Value) error {
|
||||||
switch val.Kind() {
|
kind := val.Kind()
|
||||||
case reflect.Int:
|
switch {
|
||||||
|
case isInt(kind):
|
||||||
return d.decodeInt(name, data, val)
|
return d.decodeInt(name, data, val)
|
||||||
|
case isUint(kind):
|
||||||
|
return d.decodeUint(name, data, val)
|
||||||
|
case isFloat(kind):
|
||||||
|
return d.decodeFloat(name, data, val)
|
||||||
|
}
|
||||||
|
switch kind {
|
||||||
case reflect.String:
|
case reflect.String:
|
||||||
return d.decodeString(name, data, val)
|
return d.decodeString(name, data, val)
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
@ -85,13 +113,42 @@ func (d *Decoder) decode(name string, data any, val reflect.Value) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isInt(kind reflect.Kind) bool {
|
||||||
|
switch kind {
|
||||||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func isUint(kind reflect.Kind) bool {
|
||||||
|
switch kind {
|
||||||
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func isFloat(kind reflect.Kind) bool {
|
||||||
|
switch kind {
|
||||||
|
case reflect.Float32, reflect.Float64:
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (d *Decoder) decodeInt(name string, data any, val reflect.Value) (err error) {
|
func (d *Decoder) decodeInt(name string, data any, val reflect.Value) (err error) {
|
||||||
dataVal := reflect.ValueOf(data)
|
dataVal := reflect.ValueOf(data)
|
||||||
kind := dataVal.Kind()
|
kind := dataVal.Kind()
|
||||||
switch {
|
switch {
|
||||||
case kind == reflect.Int:
|
case isInt(kind):
|
||||||
val.SetInt(dataVal.Int())
|
val.SetInt(dataVal.Int())
|
||||||
case kind == reflect.Float64 && d.option.WeaklyTypedInput:
|
case isUint(kind) && d.option.WeaklyTypedInput:
|
||||||
|
val.SetInt(int64(dataVal.Uint()))
|
||||||
|
case isFloat(kind) && d.option.WeaklyTypedInput:
|
||||||
val.SetInt(int64(dataVal.Float()))
|
val.SetInt(int64(dataVal.Float()))
|
||||||
case kind == reflect.String && d.option.WeaklyTypedInput:
|
case kind == reflect.String && d.option.WeaklyTypedInput:
|
||||||
var i int64
|
var i int64
|
||||||
@ -110,14 +167,72 @@ func (d *Decoder) decodeInt(name string, data any, val reflect.Value) (err error
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Decoder) decodeUint(name string, data any, val reflect.Value) (err error) {
|
||||||
|
dataVal := reflect.ValueOf(data)
|
||||||
|
kind := dataVal.Kind()
|
||||||
|
switch {
|
||||||
|
case isUint(kind):
|
||||||
|
val.SetUint(dataVal.Uint())
|
||||||
|
case isInt(kind) && d.option.WeaklyTypedInput:
|
||||||
|
val.SetUint(uint64(dataVal.Int()))
|
||||||
|
case isFloat(kind) && d.option.WeaklyTypedInput:
|
||||||
|
val.SetUint(uint64(dataVal.Float()))
|
||||||
|
case kind == reflect.String && d.option.WeaklyTypedInput:
|
||||||
|
var i uint64
|
||||||
|
i, err = strconv.ParseUint(dataVal.String(), 0, val.Type().Bits())
|
||||||
|
if err == nil {
|
||||||
|
val.SetUint(i)
|
||||||
|
} else {
|
||||||
|
err = fmt.Errorf("cannot parse '%s' as int: %s", name, err)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
err = fmt.Errorf(
|
||||||
|
"'%s' expected type '%s', got unconvertible type '%s'",
|
||||||
|
name, val.Type(), dataVal.Type(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Decoder) decodeFloat(name string, data any, val reflect.Value) (err error) {
|
||||||
|
dataVal := reflect.ValueOf(data)
|
||||||
|
kind := dataVal.Kind()
|
||||||
|
switch {
|
||||||
|
case isFloat(kind):
|
||||||
|
val.SetFloat(dataVal.Float())
|
||||||
|
case isUint(kind):
|
||||||
|
val.SetFloat(float64(dataVal.Uint()))
|
||||||
|
case isInt(kind) && d.option.WeaklyTypedInput:
|
||||||
|
val.SetFloat(float64(dataVal.Int()))
|
||||||
|
case kind == reflect.String && d.option.WeaklyTypedInput:
|
||||||
|
var i float64
|
||||||
|
i, err = strconv.ParseFloat(dataVal.String(), val.Type().Bits())
|
||||||
|
if err == nil {
|
||||||
|
val.SetFloat(i)
|
||||||
|
} else {
|
||||||
|
err = fmt.Errorf("cannot parse '%s' as int: %s", name, err)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
err = fmt.Errorf(
|
||||||
|
"'%s' expected type '%s', got unconvertible type '%s'",
|
||||||
|
name, val.Type(), dataVal.Type(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func (d *Decoder) decodeString(name string, data any, val reflect.Value) (err error) {
|
func (d *Decoder) decodeString(name string, data any, val reflect.Value) (err error) {
|
||||||
dataVal := reflect.ValueOf(data)
|
dataVal := reflect.ValueOf(data)
|
||||||
kind := dataVal.Kind()
|
kind := dataVal.Kind()
|
||||||
switch {
|
switch {
|
||||||
case kind == reflect.String:
|
case kind == reflect.String:
|
||||||
val.SetString(dataVal.String())
|
val.SetString(dataVal.String())
|
||||||
case kind == reflect.Int && d.option.WeaklyTypedInput:
|
case isInt(kind) && d.option.WeaklyTypedInput:
|
||||||
val.SetString(strconv.FormatInt(dataVal.Int(), 10))
|
val.SetString(strconv.FormatInt(dataVal.Int(), 10))
|
||||||
|
case isUint(kind) && d.option.WeaklyTypedInput:
|
||||||
|
val.SetString(strconv.FormatUint(dataVal.Uint(), 10))
|
||||||
|
case isFloat(kind) && d.option.WeaklyTypedInput:
|
||||||
|
val.SetString(strconv.FormatFloat(dataVal.Float(), 'E', -1, dataVal.Type().Bits()))
|
||||||
default:
|
default:
|
||||||
err = fmt.Errorf(
|
err = fmt.Errorf(
|
||||||
"'%s' expected type '%s', got unconvertible type '%s'",
|
"'%s' expected type '%s', got unconvertible type '%s'",
|
||||||
@ -133,8 +248,10 @@ func (d *Decoder) decodeBool(name string, data any, val reflect.Value) (err erro
|
|||||||
switch {
|
switch {
|
||||||
case kind == reflect.Bool:
|
case kind == reflect.Bool:
|
||||||
val.SetBool(dataVal.Bool())
|
val.SetBool(dataVal.Bool())
|
||||||
case kind == reflect.Int && 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:
|
||||||
|
val.SetString(strconv.FormatUint(dataVal.Uint(), 10))
|
||||||
default:
|
default:
|
||||||
err = fmt.Errorf(
|
err = fmt.Errorf(
|
||||||
"'%s' expected type '%s', got unconvertible type '%s'",
|
"'%s' expected type '%s', got unconvertible type '%s'",
|
||||||
@ -149,6 +266,17 @@ func (d *Decoder) decodeSlice(name string, data any, val reflect.Value) error {
|
|||||||
valType := val.Type()
|
valType := val.Type()
|
||||||
valElemType := valType.Elem()
|
valElemType := valType.Elem()
|
||||||
|
|
||||||
|
if dataVal.Kind() == reflect.String && valElemType.Kind() == reflect.Uint8 { // from encoding/json
|
||||||
|
s := []byte(dataVal.String())
|
||||||
|
b := make([]byte, base64.StdEncoding.DecodedLen(len(s)))
|
||||||
|
n, err := base64.StdEncoding.Decode(b, s)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("try decode '%s' by base64 error: %w", name, err)
|
||||||
|
}
|
||||||
|
val.SetBytes(b[:n])
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
if dataVal.Kind() != reflect.Slice {
|
if dataVal.Kind() != reflect.Slice {
|
||||||
return fmt.Errorf("'%s' is not a slice", name)
|
return fmt.Errorf("'%s' is not a slice", name)
|
||||||
}
|
}
|
||||||
@ -353,12 +481,18 @@ func (d *Decoder) decodeStructFromMap(name string, dataVal, val reflect.Value) e
|
|||||||
if !rawMapVal.IsValid() {
|
if !rawMapVal.IsValid() {
|
||||||
// Do a slower search by iterating over each key and
|
// Do a slower search by iterating over each key and
|
||||||
// doing case-insensitive search.
|
// doing case-insensitive search.
|
||||||
|
if d.option.KeyReplacer != nil {
|
||||||
|
fieldName = d.option.KeyReplacer.Replace(fieldName)
|
||||||
|
}
|
||||||
for dataValKey := range dataValKeys {
|
for dataValKey := range dataValKeys {
|
||||||
mK, ok := dataValKey.Interface().(string)
|
mK, ok := dataValKey.Interface().(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
// Not a string key
|
// Not a string key
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if d.option.KeyReplacer != nil {
|
||||||
|
mK = d.option.KeyReplacer.Replace(mK)
|
||||||
|
}
|
||||||
|
|
||||||
if strings.EqualFold(mK, fieldName) {
|
if strings.EqualFold(mK, fieldName) {
|
||||||
rawMapKey = dataValKey
|
rawMapKey = dataValKey
|
||||||
|
@ -137,3 +137,45 @@ func TestStructure_Nest(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, s.BazOptional, goal)
|
assert.Equal(t, s.BazOptional, goal)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStructure_SliceNilValue(t *testing.T) {
|
||||||
|
rawMap := map[string]any{
|
||||||
|
"foo": 1,
|
||||||
|
"bar": []any{"bar", nil},
|
||||||
|
}
|
||||||
|
|
||||||
|
goal := &BazSlice{
|
||||||
|
Foo: 1,
|
||||||
|
Bar: []string{"bar", ""},
|
||||||
|
}
|
||||||
|
|
||||||
|
s := &BazSlice{}
|
||||||
|
err := weakTypeDecoder.Decode(rawMap, s)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, goal.Bar, s.Bar)
|
||||||
|
|
||||||
|
s = &BazSlice{}
|
||||||
|
err = decoder.Decode(rawMap, s)
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStructure_SliceNilValueComplex(t *testing.T) {
|
||||||
|
rawMap := map[string]any{
|
||||||
|
"bar": []any{map[string]any{"bar": "foo"}, nil},
|
||||||
|
}
|
||||||
|
|
||||||
|
s := &struct {
|
||||||
|
Bar []map[string]any `test:"bar"`
|
||||||
|
}{}
|
||||||
|
|
||||||
|
err := decoder.Decode(rawMap, s)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Nil(t, s.Bar[1])
|
||||||
|
|
||||||
|
ss := &struct {
|
||||||
|
Bar []Baz `test:"bar"`
|
||||||
|
}{}
|
||||||
|
|
||||||
|
err = decoder.Decode(rawMap, ss)
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
}
|
||||||
|
@ -4,12 +4,15 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Dreamacro/clash/component/resolver"
|
|
||||||
"go.uber.org/atomic"
|
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/Dreamacro/clash/component/resolver"
|
||||||
|
|
||||||
|
"go.uber.org/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -22,7 +25,18 @@ var (
|
|||||||
ErrorDisableIPv6 = errors.New("IPv6 is disabled, dialer cancel")
|
ErrorDisableIPv6 = errors.New("IPv6 is disabled, dialer cancel")
|
||||||
)
|
)
|
||||||
|
|
||||||
func DialContext(ctx context.Context, network, address string, options ...Option) (net.Conn, error) {
|
func ParseNetwork(network string, addr netip.Addr) string {
|
||||||
|
if runtime.GOOS == "windows" { // fix bindIfaceToListenConfig() in windows force bind to an ipv4 address
|
||||||
|
if !strings.HasSuffix(network, "4") &&
|
||||||
|
!strings.HasSuffix(network, "6") &&
|
||||||
|
addr.Unmap().Is6() {
|
||||||
|
network += "6"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return network
|
||||||
|
}
|
||||||
|
|
||||||
|
func applyOptions(options ...Option) *option {
|
||||||
opt := &option{
|
opt := &option{
|
||||||
interfaceName: DefaultInterface.Load(),
|
interfaceName: DefaultInterface.Load(),
|
||||||
routingMark: int(DefaultRoutingMark.Load()),
|
routingMark: int(DefaultRoutingMark.Load()),
|
||||||
@ -36,6 +50,12 @@ func DialContext(ctx context.Context, network, address string, options ...Option
|
|||||||
o(opt)
|
o(opt)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return opt
|
||||||
|
}
|
||||||
|
|
||||||
|
func DialContext(ctx context.Context, network, address string, options ...Option) (net.Conn, error) {
|
||||||
|
opt := applyOptions(options...)
|
||||||
|
|
||||||
if opt.network == 4 || opt.network == 6 {
|
if opt.network == 4 || opt.network == 6 {
|
||||||
if strings.Contains(network, "tcp") {
|
if strings.Contains(network, "tcp") {
|
||||||
network = "tcp"
|
network = "tcp"
|
||||||
@ -143,7 +163,7 @@ func dualStackDialContext(ctx context.Context, network, address string, opt *opt
|
|||||||
results := make(chan dialResult)
|
results := make(chan dialResult)
|
||||||
var primary, fallback dialResult
|
var primary, fallback dialResult
|
||||||
|
|
||||||
startRacer := func(ctx context.Context, network, host string, direct bool, ipv6 bool) {
|
startRacer := func(ctx context.Context, network, host string, r resolver.Resolver, ipv6 bool) {
|
||||||
result := dialResult{ipv6: ipv6, done: true}
|
result := dialResult{ipv6: ipv6, done: true}
|
||||||
defer func() {
|
defer func() {
|
||||||
select {
|
select {
|
||||||
@ -157,16 +177,16 @@ func dualStackDialContext(ctx context.Context, network, address string, opt *opt
|
|||||||
|
|
||||||
var ip netip.Addr
|
var ip netip.Addr
|
||||||
if ipv6 {
|
if ipv6 {
|
||||||
if !direct {
|
if r == nil {
|
||||||
ip, result.error = resolver.ResolveIPv6ProxyServerHost(host)
|
ip, result.error = resolver.ResolveIPv6ProxyServerHost(ctx, host)
|
||||||
} else {
|
} else {
|
||||||
ip, result.error = resolver.ResolveIPv6(host)
|
ip, result.error = resolver.ResolveIPv6WithResolver(ctx, host, r)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if !direct {
|
if r == nil {
|
||||||
ip, result.error = resolver.ResolveIPv4ProxyServerHost(host)
|
ip, result.error = resolver.ResolveIPv4ProxyServerHost(ctx, host)
|
||||||
} else {
|
} else {
|
||||||
ip, result.error = resolver.ResolveIPv4(host)
|
ip, result.error = resolver.ResolveIPv4WithResolver(ctx, host, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if result.error != nil {
|
if result.error != nil {
|
||||||
@ -177,8 +197,8 @@ func dualStackDialContext(ctx context.Context, network, address string, opt *opt
|
|||||||
result.Conn, result.error = dialContext(ctx, network, ip, port, opt)
|
result.Conn, result.error = dialContext(ctx, network, ip, port, opt)
|
||||||
}
|
}
|
||||||
|
|
||||||
go startRacer(ctx, network+"4", host, opt.direct, false)
|
go startRacer(ctx, network+"4", host, opt.resolver, false)
|
||||||
go startRacer(ctx, network+"6", host, opt.direct, true)
|
go startRacer(ctx, network+"6", host, opt.resolver, true)
|
||||||
|
|
||||||
count := 2
|
count := 2
|
||||||
for i := 0; i < count; i++ {
|
for i := 0; i < count; i++ {
|
||||||
@ -204,11 +224,17 @@ func dualStackDialContext(ctx context.Context, network, address string, opt *opt
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
|
err = ctx.Err()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, errors.New("dual stack tcp shake hands failed")
|
if err == nil {
|
||||||
|
err = fmt.Errorf("dual stack dial failed")
|
||||||
|
} else {
|
||||||
|
err = fmt.Errorf("dual stack dial failed:%w", err)
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func concurrentDualStackDialContext(ctx context.Context, network, address string, opt *option) (net.Conn, error) {
|
func concurrentDualStackDialContext(ctx context.Context, network, address string, opt *option) (net.Conn, error) {
|
||||||
@ -218,10 +244,10 @@ func concurrentDualStackDialContext(ctx context.Context, network, address string
|
|||||||
}
|
}
|
||||||
|
|
||||||
var ips []netip.Addr
|
var ips []netip.Addr
|
||||||
if opt.direct {
|
if opt.resolver != nil {
|
||||||
ips, err = resolver.ResolveAllIP(host)
|
ips, err = resolver.LookupIPWithResolver(ctx, host, opt.resolver)
|
||||||
} else {
|
} else {
|
||||||
ips, err = resolver.ResolveAllIPProxyServerHost(host)
|
ips, err = resolver.LookupIPProxyServerHost(ctx, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -291,6 +317,7 @@ func concurrentDialContext(ctx context.Context, network string, ips []netip.Addr
|
|||||||
connCount := len(ips)
|
connCount := len(ips)
|
||||||
var fallback dialResult
|
var fallback dialResult
|
||||||
var primaryError error
|
var primaryError error
|
||||||
|
var finalError error
|
||||||
for i := 0; i < connCount; i++ {
|
for i := 0; i < connCount; i++ {
|
||||||
select {
|
select {
|
||||||
case res := <-results:
|
case res := <-results:
|
||||||
@ -315,6 +342,7 @@ func concurrentDialContext(ctx context.Context, network string, ips []netip.Addr
|
|||||||
if fallback.done && fallback.error == nil {
|
if fallback.done && fallback.error == nil {
|
||||||
return fallback.Conn, nil
|
return fallback.Conn, nil
|
||||||
}
|
}
|
||||||
|
finalError = ctx.Err()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -331,7 +359,13 @@ func concurrentDialContext(ctx context.Context, network string, ips []netip.Addr
|
|||||||
return nil, fallback.error
|
return nil, fallback.error
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, fmt.Errorf("all ips %v tcp shake hands failed", ips)
|
if finalError == nil {
|
||||||
|
finalError = fmt.Errorf("all ips %v tcp shake hands failed", ips)
|
||||||
|
} else {
|
||||||
|
finalError = fmt.Errorf("concurrent dial failed:%w", finalError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, finalError
|
||||||
}
|
}
|
||||||
|
|
||||||
func singleDialContext(ctx context.Context, network string, address string, opt *option) (net.Conn, error) {
|
func singleDialContext(ctx context.Context, network string, address string, opt *option) (net.Conn, error) {
|
||||||
@ -343,16 +377,16 @@ func singleDialContext(ctx context.Context, network string, address string, opt
|
|||||||
var ip netip.Addr
|
var ip netip.Addr
|
||||||
switch network {
|
switch network {
|
||||||
case "tcp4", "udp4":
|
case "tcp4", "udp4":
|
||||||
if !opt.direct {
|
if opt.resolver == nil {
|
||||||
ip, err = resolver.ResolveIPv4ProxyServerHost(host)
|
ip, err = resolver.ResolveIPv4ProxyServerHost(ctx, host)
|
||||||
} else {
|
} else {
|
||||||
ip, err = resolver.ResolveIPv4(host)
|
ip, err = resolver.ResolveIPv4WithResolver(ctx, host, opt.resolver)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
if !opt.direct {
|
if opt.resolver == nil {
|
||||||
ip, err = resolver.ResolveIPv6ProxyServerHost(host)
|
ip, err = resolver.ResolveIPv6ProxyServerHost(ctx, host)
|
||||||
} else {
|
} else {
|
||||||
ip, err = resolver.ResolveIPv6(host)
|
ip, err = resolver.ResolveIPv6WithResolver(ctx, host, opt.resolver)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -378,10 +412,10 @@ func concurrentIPv4DialContext(ctx context.Context, network, address string, opt
|
|||||||
}
|
}
|
||||||
|
|
||||||
var ips []netip.Addr
|
var ips []netip.Addr
|
||||||
if !opt.direct {
|
if opt.resolver == nil {
|
||||||
ips, err = resolver.ResolveAllIPv4ProxyServerHost(host)
|
ips, err = resolver.LookupIPv4ProxyServerHost(ctx, host)
|
||||||
} else {
|
} else {
|
||||||
ips, err = resolver.ResolveAllIPv4(host)
|
ips, err = resolver.LookupIPv4WithResolver(ctx, host, opt.resolver)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -398,10 +432,10 @@ func concurrentIPv6DialContext(ctx context.Context, network, address string, opt
|
|||||||
}
|
}
|
||||||
|
|
||||||
var ips []netip.Addr
|
var ips []netip.Addr
|
||||||
if !opt.direct {
|
if opt.resolver == nil {
|
||||||
ips, err = resolver.ResolveAllIPv6ProxyServerHost(host)
|
ips, err = resolver.LookupIPv6ProxyServerHost(ctx, host)
|
||||||
} else {
|
} else {
|
||||||
ips, err = resolver.ResolveAllIPv6(host)
|
ips, err = resolver.LookupIPv6WithResolver(ctx, host, opt.resolver)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -410,3 +444,20 @@ func concurrentIPv6DialContext(ctx context.Context, network, address string, opt
|
|||||||
|
|
||||||
return concurrentDialContext(ctx, network, ips, port, opt)
|
return concurrentDialContext(ctx, network, ips, port, opt)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Dialer struct {
|
||||||
|
Opt option
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||||
|
return DialContext(ctx, network, address, WithOption(d.Opt))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Dialer) ListenPacket(ctx context.Context, network, address string, rAddrPort netip.AddrPort) (net.PacketConn, error) {
|
||||||
|
return ListenPacket(ctx, ParseNetwork(network, rAddrPort.Addr()), address, WithOption(d.Opt))
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDialer(options ...Option) Dialer {
|
||||||
|
opt := applyOptions(options...)
|
||||||
|
return Dialer{Opt: *opt}
|
||||||
|
}
|
||||||
|
@ -29,13 +29,13 @@ func bindMarkToControl(mark int, chain controlFn) controlFn {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Control(func(fd uintptr) {
|
var innerErr error
|
||||||
switch network {
|
err = c.Control(func(fd uintptr) {
|
||||||
case "tcp4", "udp4":
|
innerErr = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, mark)
|
||||||
_ = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, mark)
|
|
||||||
case "tcp6", "udp6":
|
|
||||||
_ = syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, mark)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
if innerErr != nil {
|
||||||
|
err = innerErr
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package dialer
|
package dialer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/Dreamacro/clash/component/resolver"
|
||||||
|
|
||||||
"go.uber.org/atomic"
|
"go.uber.org/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -14,9 +16,9 @@ type option struct {
|
|||||||
interfaceName string
|
interfaceName string
|
||||||
addrReuse bool
|
addrReuse bool
|
||||||
routingMark int
|
routingMark int
|
||||||
direct bool
|
|
||||||
network int
|
network int
|
||||||
prefer int
|
prefer int
|
||||||
|
resolver resolver.Resolver
|
||||||
}
|
}
|
||||||
|
|
||||||
type Option func(opt *option)
|
type Option func(opt *option)
|
||||||
@ -39,9 +41,9 @@ func WithRoutingMark(mark int) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithDirect() Option {
|
func WithResolver(r resolver.Resolver) Option {
|
||||||
return func(opt *option) {
|
return func(opt *option) {
|
||||||
opt.direct = true
|
opt.resolver = r
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,3 +68,9 @@ func WithOnlySingleStack(isIPv4 bool) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithOption(o option) Option {
|
||||||
|
return func(opt *option) {
|
||||||
|
*opt = o
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -6,13 +6,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
|
||||||
"github.com/vishvananda/netlink"
|
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/cmd"
|
"github.com/Dreamacro/clash/common/cmd"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
"github.com/Dreamacro/clash/component/ebpf/redir"
|
"github.com/Dreamacro/clash/component/ebpf/redir"
|
||||||
"github.com/Dreamacro/clash/component/ebpf/tc"
|
"github.com/Dreamacro/clash/component/ebpf/tc"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
"github.com/sagernet/netlink"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetAutoDetectInterface() (string, error) {
|
func GetAutoDetectInterface() (string, error) {
|
||||||
|
@ -13,7 +13,7 @@ import (
|
|||||||
|
|
||||||
"github.com/cilium/ebpf"
|
"github.com/cilium/ebpf"
|
||||||
"github.com/cilium/ebpf/rlimit"
|
"github.com/cilium/ebpf/rlimit"
|
||||||
"github.com/vishvananda/netlink"
|
"github.com/sagernet/netlink"
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/component/ebpf/byteorder"
|
"github.com/Dreamacro/clash/component/ebpf/byteorder"
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
|
|
||||||
"github.com/cilium/ebpf"
|
"github.com/cilium/ebpf"
|
||||||
"github.com/cilium/ebpf/rlimit"
|
"github.com/cilium/ebpf/rlimit"
|
||||||
"github.com/vishvananda/netlink"
|
"github.com/sagernet/netlink"
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
|
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
@ -73,7 +73,7 @@ func (m *memoryStore) FlushFakeIP() error {
|
|||||||
|
|
||||||
func newMemoryStore(size int) *memoryStore {
|
func newMemoryStore(size int) *memoryStore {
|
||||||
return &memoryStore{
|
return &memoryStore{
|
||||||
cacheIP: cache.NewLRUCache[string, netip.Addr](cache.WithSize[string, netip.Addr](size)),
|
cacheIP: cache.New[string, netip.Addr](cache.WithSize[string, netip.Addr](size)),
|
||||||
cacheHost: cache.NewLRUCache[netip.Addr, string](cache.WithSize[netip.Addr, string](size)),
|
cacheHost: cache.New[netip.Addr, string](cache.WithSize[netip.Addr, string](size)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package fakeip
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/nnip"
|
"github.com/Dreamacro/clash/common/nnip"
|
||||||
@ -26,7 +27,7 @@ type store interface {
|
|||||||
FlushFakeIP() error
|
FlushFakeIP() error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pool is a implementation about fake ip generator without storage
|
// Pool is an implementation about fake ip generator without storage
|
||||||
type Pool struct {
|
type Pool struct {
|
||||||
gateway netip.Addr
|
gateway netip.Addr
|
||||||
first netip.Addr
|
first netip.Addr
|
||||||
@ -34,7 +35,7 @@ type Pool struct {
|
|||||||
offset netip.Addr
|
offset netip.Addr
|
||||||
cycle bool
|
cycle bool
|
||||||
mux sync.Mutex
|
mux sync.Mutex
|
||||||
host *trie.DomainTrie[bool]
|
host *trie.DomainTrie[struct{}]
|
||||||
ipnet *netip.Prefix
|
ipnet *netip.Prefix
|
||||||
store store
|
store store
|
||||||
}
|
}
|
||||||
@ -43,6 +44,9 @@ type Pool struct {
|
|||||||
func (p *Pool) Lookup(host string) netip.Addr {
|
func (p *Pool) Lookup(host string) netip.Addr {
|
||||||
p.mux.Lock()
|
p.mux.Lock()
|
||||||
defer p.mux.Unlock()
|
defer p.mux.Unlock()
|
||||||
|
|
||||||
|
// RFC4343: DNS Case Insensitive, we SHOULD return result with all cases.
|
||||||
|
host = strings.ToLower(host)
|
||||||
if ip, exist := p.store.GetByHost(host); exist {
|
if ip, exist := p.store.GetByHost(host); exist {
|
||||||
return ip
|
return ip
|
||||||
}
|
}
|
||||||
@ -150,7 +154,7 @@ func (p *Pool) restoreState() {
|
|||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
IPNet *netip.Prefix
|
IPNet *netip.Prefix
|
||||||
Host *trie.DomainTrie[bool]
|
Host *trie.DomainTrie[struct{}]
|
||||||
|
|
||||||
// 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
|
||||||
|
@ -104,6 +104,27 @@ func TestPool_BasicV6(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPool_Case_Insensitive(t *testing.T) {
|
||||||
|
ipnet := netip.MustParsePrefix("192.168.0.1/29")
|
||||||
|
pools, tempfile, err := createPools(Options{
|
||||||
|
IPNet: &ipnet,
|
||||||
|
Size: 10,
|
||||||
|
})
|
||||||
|
assert.Nil(t, err)
|
||||||
|
defer os.Remove(tempfile)
|
||||||
|
|
||||||
|
for _, pool := range pools {
|
||||||
|
first := pool.Lookup("foo.com")
|
||||||
|
last := pool.Lookup("Foo.Com")
|
||||||
|
foo, exist := pool.LookBack(last)
|
||||||
|
|
||||||
|
assert.Equal(t, first, pool.Lookup("Foo.Com"))
|
||||||
|
assert.Equal(t, pool.Lookup("fOo.cOM"), first)
|
||||||
|
assert.True(t, exist)
|
||||||
|
assert.Equal(t, foo, "foo.com")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestPool_CycleUsed(t *testing.T) {
|
func TestPool_CycleUsed(t *testing.T) {
|
||||||
ipnet := netip.MustParsePrefix("192.168.0.16/28")
|
ipnet := netip.MustParsePrefix("192.168.0.16/28")
|
||||||
pools, tempfile, err := createPools(Options{
|
pools, tempfile, err := createPools(Options{
|
||||||
@ -128,8 +149,8 @@ func TestPool_CycleUsed(t *testing.T) {
|
|||||||
|
|
||||||
func TestPool_Skip(t *testing.T) {
|
func TestPool_Skip(t *testing.T) {
|
||||||
ipnet := netip.MustParsePrefix("192.168.0.1/29")
|
ipnet := netip.MustParsePrefix("192.168.0.1/29")
|
||||||
tree := trie.New[bool]()
|
tree := trie.New[struct{}]()
|
||||||
tree.Insert("example.com", true)
|
tree.Insert("example.com", struct{}{})
|
||||||
pools, tempfile, err := createPools(Options{
|
pools, tempfile, err := createPools(Options{
|
||||||
IPNet: &ipnet,
|
IPNet: &ipnet,
|
||||||
Size: 10,
|
Size: 10,
|
||||||
|
@ -2,6 +2,7 @@ package geodata
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/Dreamacro/clash/component/mmdb"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/log"
|
"github.com/Dreamacro/clash/log"
|
||||||
"io"
|
"io"
|
||||||
@ -9,7 +10,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
var initFlag bool
|
var initGeoSite bool
|
||||||
|
var initGeoIP int
|
||||||
|
|
||||||
func InitGeoSite() error {
|
func InitGeoSite() error {
|
||||||
if _, err := os.Stat(C.Path.GeoSite()); os.IsNotExist(err) {
|
if _, err := os.Stat(C.Path.GeoSite()); os.IsNotExist(err) {
|
||||||
@ -18,8 +20,9 @@ func InitGeoSite() error {
|
|||||||
return fmt.Errorf("can't download GeoSite.dat: %s", err.Error())
|
return fmt.Errorf("can't download GeoSite.dat: %s", err.Error())
|
||||||
}
|
}
|
||||||
log.Infoln("Download GeoSite.dat finish")
|
log.Infoln("Download GeoSite.dat finish")
|
||||||
|
initGeoSite = false
|
||||||
}
|
}
|
||||||
if !initFlag {
|
if !initGeoSite {
|
||||||
if err := Verify(C.GeositeName); err != nil {
|
if err := Verify(C.GeositeName); err != nil {
|
||||||
log.Warnln("GeoSite.dat invalid, remove and download: %s", err)
|
log.Warnln("GeoSite.dat invalid, remove and download: %s", err)
|
||||||
if err := os.Remove(C.Path.GeoSite()); err != nil {
|
if err := os.Remove(C.Path.GeoSite()); err != nil {
|
||||||
@ -29,7 +32,7 @@ func InitGeoSite() error {
|
|||||||
return fmt.Errorf("can't download GeoSite.dat: %s", err.Error())
|
return fmt.Errorf("can't download GeoSite.dat: %s", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
initFlag = true
|
initGeoSite = true
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -50,3 +53,68 @@ func downloadGeoSite(path string) (err error) {
|
|||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func downloadGeoIP(path string) (err error) {
|
||||||
|
resp, err := http.Get(C.GeoIpUrl)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
_, err = io.Copy(f, resp.Body)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func InitGeoIP() error {
|
||||||
|
if C.GeodataMode {
|
||||||
|
if _, err := os.Stat(C.Path.GeoIP()); os.IsNotExist(err) {
|
||||||
|
log.Infoln("Can't find GeoIP.dat, start download")
|
||||||
|
if err := downloadGeoIP(C.Path.GeoIP()); err != nil {
|
||||||
|
return fmt.Errorf("can't download GeoIP.dat: %s", err.Error())
|
||||||
|
}
|
||||||
|
log.Infoln("Download GeoIP.dat finish")
|
||||||
|
initGeoIP = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
if initGeoIP != 1 {
|
||||||
|
if err := Verify(C.GeoipName); err != nil {
|
||||||
|
log.Warnln("GeoIP.dat invalid, remove and download: %s", err)
|
||||||
|
if err := os.Remove(C.Path.GeoIP()); err != nil {
|
||||||
|
return fmt.Errorf("can't remove invalid GeoIP.dat: %s", err.Error())
|
||||||
|
}
|
||||||
|
if err := downloadGeoIP(C.Path.GeoIP()); err != nil {
|
||||||
|
return fmt.Errorf("can't download GeoIP.dat: %s", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
initGeoIP = 1
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := os.Stat(C.Path.MMDB()); os.IsNotExist(err) {
|
||||||
|
log.Infoln("Can't find MMDB, start download")
|
||||||
|
if err := mmdb.DownloadMMDB(C.Path.MMDB()); err != nil {
|
||||||
|
return fmt.Errorf("can't download MMDB: %s", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if initGeoIP != 2 {
|
||||||
|
if !mmdb.Verify() {
|
||||||
|
log.Warnln("MMDB invalid, remove and download")
|
||||||
|
if err := os.Remove(C.Path.MMDB()); err != nil {
|
||||||
|
return fmt.Errorf("can't remove invalid MMDB: %s", err.Error())
|
||||||
|
}
|
||||||
|
if err := mmdb.DownloadMMDB(C.Path.MMDB()); err != nil {
|
||||||
|
return fmt.Errorf("can't download MMDB: %s", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
initGeoIP = 2
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -2,6 +2,9 @@ package mmdb
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/oschwald/geoip2-golang"
|
"github.com/oschwald/geoip2-golang"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
@ -42,3 +45,20 @@ func Instance() *geoip2.Reader {
|
|||||||
|
|
||||||
return mmdb
|
return mmdb
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DownloadMMDB(path string) (err error) {
|
||||||
|
resp, err := http.Get(C.MmdbUrl)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
_, err = io.Copy(f, resp.Body)
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
57
component/process/find_process_mode.go
Normal file
57
component/process/find_process_mode.go
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
package process
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
FindProcessAlways = "always"
|
||||||
|
FindProcessStrict = "strict"
|
||||||
|
FindProcessOff = "off"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
validModes = map[string]struct{}{
|
||||||
|
FindProcessAlways: {},
|
||||||
|
FindProcessOff: {},
|
||||||
|
FindProcessStrict: {},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
type FindProcessMode string
|
||||||
|
|
||||||
|
func (m FindProcessMode) Always() bool {
|
||||||
|
return m == FindProcessAlways
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m FindProcessMode) Off() bool {
|
||||||
|
return m == FindProcessOff
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *FindProcessMode) UnmarshalYAML(unmarshal func(any) error) error {
|
||||||
|
var tp string
|
||||||
|
if err := unmarshal(&tp); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return m.Set(tp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *FindProcessMode) UnmarshalJSON(data []byte) error {
|
||||||
|
var tp string
|
||||||
|
if err := json.Unmarshal(data, &tp); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return m.Set(tp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *FindProcessMode) Set(value string) error {
|
||||||
|
mode := strings.ToLower(value)
|
||||||
|
_, exist := validModes[mode]
|
||||||
|
if !exist {
|
||||||
|
return errors.New("invalid find process mode")
|
||||||
|
}
|
||||||
|
*m = FindProcessMode(mode)
|
||||||
|
return nil
|
||||||
|
}
|
@ -16,14 +16,14 @@ const (
|
|||||||
UDP = "udp"
|
UDP = "udp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func FindProcessName(network string, srcIP netip.Addr, srcPort int) (int32, string, error) {
|
func FindProcessName(network string, srcIP netip.Addr, srcPort int) (*uint32, string, error) {
|
||||||
return findProcessName(network, srcIP, srcPort)
|
return findProcessName(network, srcIP, srcPort)
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindUid(network string, srcIP netip.Addr, srcPort int) (int32, error) {
|
func FindUid(network string, srcIP netip.Addr, srcPort int) (*uint32, error) {
|
||||||
_, uid, err := resolveSocketByNetlink(network, srcIP, srcPort)
|
_, uid, err := resolveSocketByNetlink(network, srcIP, srcPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return uid, nil
|
return &uid, nil
|
||||||
}
|
}
|
||||||
|
@ -33,11 +33,11 @@ var structSize = func() int {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
func resolveSocketByNetlink(network string, ip netip.Addr, srcPort int) (int32, int32, error) {
|
func resolveSocketByNetlink(network string, ip netip.Addr, srcPort int) (uint32, uint32, error) {
|
||||||
return 0, 0, ErrPlatformNotSupport
|
return 0, 0, ErrPlatformNotSupport
|
||||||
}
|
}
|
||||||
|
|
||||||
func findProcessName(network string, ip netip.Addr, port int) (int32, string, error) {
|
func findProcessName(network string, ip netip.Addr, port int) (*uint32, string, error) {
|
||||||
var spath string
|
var spath string
|
||||||
switch network {
|
switch network {
|
||||||
case TCP:
|
case TCP:
|
||||||
@ -45,14 +45,14 @@ func findProcessName(network string, ip netip.Addr, port int) (int32, string, er
|
|||||||
case UDP:
|
case UDP:
|
||||||
spath = "net.inet.udp.pcblist_n"
|
spath = "net.inet.udp.pcblist_n"
|
||||||
default:
|
default:
|
||||||
return -1, "", ErrInvalidNetwork
|
return nil, "", ErrInvalidNetwork
|
||||||
}
|
}
|
||||||
|
|
||||||
isIPv4 := ip.Is4()
|
isIPv4 := ip.Is4()
|
||||||
|
|
||||||
value, err := syscall.Sysctl(spath)
|
value, err := syscall.Sysctl(spath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, "", err
|
return nil, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := []byte(value)
|
buf := []byte(value)
|
||||||
@ -96,7 +96,7 @@ func findProcessName(network string, ip netip.Addr, port int) (int32, string, er
|
|||||||
// xsocket_n.so_last_pid
|
// xsocket_n.so_last_pid
|
||||||
pid := readNativeUint32(buf[so+68 : so+72])
|
pid := readNativeUint32(buf[so+68 : so+72])
|
||||||
pp, err := getExecPathFromPID(pid)
|
pp, err := getExecPathFromPID(pid)
|
||||||
return -1, pp, err
|
return nil, pp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// udp packet connection may be not equal with srcIP
|
// udp packet connection may be not equal with srcIP
|
||||||
@ -106,10 +106,10 @@ func findProcessName(network string, ip netip.Addr, port int) (int32, string, er
|
|||||||
}
|
}
|
||||||
|
|
||||||
if network == UDP && fallbackUDPProcess != "" {
|
if network == UDP && fallbackUDPProcess != "" {
|
||||||
return -1, fallbackUDPProcess, nil
|
return nil, fallbackUDPProcess, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1, "", ErrNotFound
|
return nil, "", ErrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
func getExecPathFromPID(pid uint32) (string, error) {
|
func getExecPathFromPID(pid uint32) (string, error) {
|
||||||
|
@ -21,11 +21,11 @@ var (
|
|||||||
once sync.Once
|
once sync.Once
|
||||||
)
|
)
|
||||||
|
|
||||||
func resolveSocketByNetlink(network string, ip netip.Addr, srcPort int) (int32, int32, error) {
|
func resolveSocketByNetlink(network string, ip netip.Addr, srcPort int) (uint32, uint32, error) {
|
||||||
return 0, 0, ErrPlatformNotSupport
|
return 0, 0, ErrPlatformNotSupport
|
||||||
}
|
}
|
||||||
|
|
||||||
func findProcessName(network string, ip netip.Addr, srcPort int) (int32, string, error) {
|
func findProcessName(network string, ip netip.Addr, srcPort int) (*uint32, string, error) {
|
||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
if err := initSearcher(); err != nil {
|
if err := initSearcher(); err != nil {
|
||||||
log.Errorln("Initialize PROCESS-NAME failed: %s", err.Error())
|
log.Errorln("Initialize PROCESS-NAME failed: %s", err.Error())
|
||||||
@ -35,7 +35,7 @@ func findProcessName(network string, ip netip.Addr, srcPort int) (int32, string,
|
|||||||
})
|
})
|
||||||
|
|
||||||
if defaultSearcher == nil {
|
if defaultSearcher == nil {
|
||||||
return -1, "", ErrPlatformNotSupport
|
return nil, "", ErrPlatformNotSupport
|
||||||
}
|
}
|
||||||
|
|
||||||
var spath string
|
var spath string
|
||||||
@ -46,22 +46,22 @@ func findProcessName(network string, ip netip.Addr, srcPort int) (int32, string,
|
|||||||
case UDP:
|
case UDP:
|
||||||
spath = "net.inet.udp.pcblist"
|
spath = "net.inet.udp.pcblist"
|
||||||
default:
|
default:
|
||||||
return -1, "", ErrInvalidNetwork
|
return nil, "", ErrInvalidNetwork
|
||||||
}
|
}
|
||||||
|
|
||||||
value, err := syscall.Sysctl(spath)
|
value, err := syscall.Sysctl(spath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, "", err
|
return nil, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := []byte(value)
|
buf := []byte(value)
|
||||||
pid, err := defaultSearcher.Search(buf, ip, uint16(srcPort), isTCP)
|
pid, err := defaultSearcher.Search(buf, ip, uint16(srcPort), isTCP)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, "", err
|
return nil, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
pp, err := getExecPathFromPID(pid)
|
pp, err := getExecPathFromPID(pid)
|
||||||
return -1, pp, err
|
return nil, pp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func getExecPathFromPID(pid uint32) (string, error) {
|
func getExecPathFromPID(pid uint32) (string, error) {
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@ -15,162 +14,125 @@ import (
|
|||||||
"unicode"
|
"unicode"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/pool"
|
"github.com/mdlayher/netlink"
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
// from https://github.com/vishvananda/netlink/blob/bca67dfc8220b44ef582c9da4e9172bf1c9ec973/nl/nl_linux.go#L52-L62
|
|
||||||
var nativeEndian = func() binary.ByteOrder {
|
|
||||||
var x uint32 = 0x01020304
|
|
||||||
if *(*byte)(unsafe.Pointer(&x)) == 0x01 {
|
|
||||||
return binary.BigEndian
|
|
||||||
}
|
|
||||||
|
|
||||||
return binary.LittleEndian
|
|
||||||
}()
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
sizeOfSocketDiagRequest = syscall.SizeofNlMsghdr + 8 + 48
|
SOCK_DIAG_BY_FAMILY = 20
|
||||||
socketDiagByFamily = 20
|
inetDiagRequestSize = int(unsafe.Sizeof(inetDiagRequest{}))
|
||||||
pathProc = "/proc"
|
inetDiagResponseSize = int(unsafe.Sizeof(inetDiagResponse{}))
|
||||||
)
|
)
|
||||||
|
|
||||||
func findProcessName(network string, ip netip.Addr, srcPort int) (int32, string, error) {
|
type inetDiagRequest struct {
|
||||||
|
Family byte
|
||||||
|
Protocol byte
|
||||||
|
Ext byte
|
||||||
|
Pad byte
|
||||||
|
States uint32
|
||||||
|
|
||||||
|
SrcPort [2]byte
|
||||||
|
DstPort [2]byte
|
||||||
|
Src [16]byte
|
||||||
|
Dst [16]byte
|
||||||
|
If uint32
|
||||||
|
Cookie [2]uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
type inetDiagResponse struct {
|
||||||
|
Family byte
|
||||||
|
State byte
|
||||||
|
Timer byte
|
||||||
|
ReTrans byte
|
||||||
|
|
||||||
|
SrcPort [2]byte
|
||||||
|
DstPort [2]byte
|
||||||
|
Src [16]byte
|
||||||
|
Dst [16]byte
|
||||||
|
If uint32
|
||||||
|
Cookie [2]uint32
|
||||||
|
|
||||||
|
Expires uint32
|
||||||
|
RQueue uint32
|
||||||
|
WQueue uint32
|
||||||
|
UID uint32
|
||||||
|
INode uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
func findProcessName(network string, ip netip.Addr, srcPort int) (*uint32, string, error) {
|
||||||
inode, uid, err := resolveSocketByNetlink(network, ip, srcPort)
|
inode, uid, err := resolveSocketByNetlink(network, ip, srcPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, "", err
|
return nil, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
pp, err := resolveProcessNameByProcSearch(inode, uid)
|
pp, err := resolveProcessNameByProcSearch(inode, uid)
|
||||||
return uid, pp, err
|
return &uid, pp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveSocketByNetlink(network string, ip netip.Addr, srcPort int) (int32, int32, error) {
|
func resolveSocketByNetlink(network string, ip netip.Addr, srcPort int) (uint32, uint32, error) {
|
||||||
var family byte
|
request := &inetDiagRequest{
|
||||||
var protocol byte
|
States: 0xffffffff,
|
||||||
|
Cookie: [2]uint32{0xffffffff, 0xffffffff},
|
||||||
switch network {
|
|
||||||
case TCP:
|
|
||||||
protocol = syscall.IPPROTO_TCP
|
|
||||||
case UDP:
|
|
||||||
protocol = syscall.IPPROTO_UDP
|
|
||||||
default:
|
|
||||||
return 0, 0, ErrInvalidNetwork
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ip.Is4() {
|
if ip.Is4() {
|
||||||
family = syscall.AF_INET
|
request.Family = unix.AF_INET
|
||||||
} else {
|
} else {
|
||||||
family = syscall.AF_INET6
|
request.Family = unix.AF_INET6
|
||||||
}
|
}
|
||||||
|
|
||||||
req := packSocketDiagRequest(family, protocol, ip, uint16(srcPort))
|
if strings.HasPrefix(network, "tcp") {
|
||||||
|
request.Protocol = unix.IPPROTO_TCP
|
||||||
|
} else if strings.HasPrefix(network, "udp") {
|
||||||
|
request.Protocol = unix.IPPROTO_UDP
|
||||||
|
} else {
|
||||||
|
return 0, 0, ErrInvalidNetwork
|
||||||
|
}
|
||||||
|
|
||||||
socket, err := syscall.Socket(syscall.AF_NETLINK, syscall.SOCK_DGRAM, syscall.NETLINK_INET_DIAG)
|
copy(request.Src[:], ip.AsSlice())
|
||||||
|
|
||||||
|
binary.BigEndian.PutUint16(request.SrcPort[:], uint16(srcPort))
|
||||||
|
|
||||||
|
conn, err := netlink.Dial(unix.NETLINK_INET_DIAG, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, 0, fmt.Errorf("dial netlink: %w", err)
|
return 0, 0, err
|
||||||
}
|
}
|
||||||
defer func() {
|
defer conn.Close()
|
||||||
_ = syscall.Close(socket)
|
|
||||||
}()
|
|
||||||
|
|
||||||
_ = syscall.SetsockoptTimeval(socket, syscall.SOL_SOCKET, syscall.SO_SNDTIMEO, &syscall.Timeval{Usec: 100})
|
message := netlink.Message{
|
||||||
_ = syscall.SetsockoptTimeval(socket, syscall.SOL_SOCKET, syscall.SO_RCVTIMEO, &syscall.Timeval{Usec: 100})
|
Header: netlink.Header{
|
||||||
|
Type: SOCK_DIAG_BY_FAMILY,
|
||||||
|
Flags: netlink.Request | netlink.Dump,
|
||||||
|
},
|
||||||
|
Data: (*(*[inetDiagRequestSize]byte)(unsafe.Pointer(request)))[:],
|
||||||
|
}
|
||||||
|
|
||||||
if err := syscall.Connect(socket, &syscall.SockaddrNetlink{
|
messages, err := conn.Execute(message)
|
||||||
Family: syscall.AF_NETLINK,
|
if err != nil {
|
||||||
Pad: 0,
|
|
||||||
Pid: 0,
|
|
||||||
Groups: 0,
|
|
||||||
}); err != nil {
|
|
||||||
return 0, 0, err
|
return 0, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := syscall.Write(socket, req); err != nil {
|
for _, msg := range messages {
|
||||||
return 0, 0, fmt.Errorf("write request: %w", err)
|
if len(msg.Data) < inetDiagResponseSize {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
response := (*inetDiagResponse)(unsafe.Pointer(&msg.Data[0]))
|
||||||
|
|
||||||
|
return response.INode, response.UID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
rb := pool.Get(pool.RelayBufferSize)
|
return 0, 0, ErrNotFound
|
||||||
defer func() {
|
|
||||||
_ = pool.Put(rb)
|
|
||||||
}()
|
|
||||||
|
|
||||||
n, err := syscall.Read(socket, rb)
|
|
||||||
if err != nil {
|
|
||||||
return 0, 0, fmt.Errorf("read response: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
messages, err := syscall.ParseNetlinkMessage(rb[:n])
|
|
||||||
if err != nil {
|
|
||||||
return 0, 0, fmt.Errorf("parse netlink message: %w", err)
|
|
||||||
} else if len(messages) == 0 {
|
|
||||||
return 0, 0, fmt.Errorf("unexcepted netlink response")
|
|
||||||
}
|
|
||||||
|
|
||||||
message := messages[0]
|
|
||||||
if message.Header.Type&syscall.NLMSG_ERROR != 0 {
|
|
||||||
return 0, 0, fmt.Errorf("netlink message: NLMSG_ERROR")
|
|
||||||
}
|
|
||||||
|
|
||||||
inode, uid := unpackSocketDiagResponse(&messages[0])
|
|
||||||
if inode < 0 || uid < 0 {
|
|
||||||
return 0, 0, fmt.Errorf("invalid inode(%d) or uid(%d)", inode, uid)
|
|
||||||
}
|
|
||||||
|
|
||||||
return inode, uid, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func packSocketDiagRequest(family, protocol byte, source netip.Addr, sourcePort uint16) []byte {
|
func resolveProcessNameByProcSearch(inode, uid uint32) (string, error) {
|
||||||
s := make([]byte, 16)
|
files, err := os.ReadDir("/proc")
|
||||||
|
|
||||||
copy(s, source.AsSlice())
|
|
||||||
|
|
||||||
buf := make([]byte, sizeOfSocketDiagRequest)
|
|
||||||
|
|
||||||
nativeEndian.PutUint32(buf[0:4], sizeOfSocketDiagRequest)
|
|
||||||
nativeEndian.PutUint16(buf[4:6], socketDiagByFamily)
|
|
||||||
nativeEndian.PutUint16(buf[6:8], syscall.NLM_F_REQUEST|syscall.NLM_F_DUMP)
|
|
||||||
nativeEndian.PutUint32(buf[8:12], 0)
|
|
||||||
nativeEndian.PutUint32(buf[12:16], 0)
|
|
||||||
|
|
||||||
buf[16] = family
|
|
||||||
buf[17] = protocol
|
|
||||||
buf[18] = 0
|
|
||||||
buf[19] = 0
|
|
||||||
nativeEndian.PutUint32(buf[20:24], 0xFFFFFFFF)
|
|
||||||
|
|
||||||
binary.BigEndian.PutUint16(buf[24:26], sourcePort)
|
|
||||||
binary.BigEndian.PutUint16(buf[26:28], 0)
|
|
||||||
|
|
||||||
copy(buf[28:44], s)
|
|
||||||
copy(buf[44:60], net.IPv6zero)
|
|
||||||
|
|
||||||
nativeEndian.PutUint32(buf[60:64], 0)
|
|
||||||
nativeEndian.PutUint64(buf[64:72], 0xFFFFFFFFFFFFFFFF)
|
|
||||||
|
|
||||||
return buf
|
|
||||||
}
|
|
||||||
|
|
||||||
func unpackSocketDiagResponse(msg *syscall.NetlinkMessage) (inode, uid int32) {
|
|
||||||
if len(msg.Data) < 72 {
|
|
||||||
return 0, 0
|
|
||||||
}
|
|
||||||
|
|
||||||
data := msg.Data
|
|
||||||
|
|
||||||
uid = int32(nativeEndian.Uint32(data[64:68]))
|
|
||||||
inode = int32(nativeEndian.Uint32(data[68:72]))
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func resolveProcessNameByProcSearch(inode, uid int32) (string, error) {
|
|
||||||
files, err := os.ReadDir(pathProc)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer := make([]byte, syscall.PathMax)
|
buffer := make([]byte, unix.PathMax)
|
||||||
socket := []byte(fmt.Sprintf("socket:[%d]", inode))
|
socket := fmt.Appendf(nil, "socket:[%d]", inode)
|
||||||
|
|
||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
if !f.IsDir() || !isPid(f.Name()) {
|
if !f.IsDir() || !isPid(f.Name()) {
|
||||||
@ -181,12 +143,12 @@ func resolveProcessNameByProcSearch(inode, uid int32) (string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if info.Sys().(*syscall.Stat_t).Uid != uint32(uid) {
|
if info.Sys().(*syscall.Stat_t).Uid != uid {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
processPath := path.Join(pathProc, f.Name())
|
processPath := filepath.Join("/proc", f.Name())
|
||||||
fdPath := path.Join(processPath, "fd")
|
fdPath := filepath.Join(processPath, "fd")
|
||||||
|
|
||||||
fds, err := os.ReadDir(fdPath)
|
fds, err := os.ReadDir(fdPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -194,7 +156,7 @@ func resolveProcessNameByProcSearch(inode, uid int32) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, fd := range fds {
|
for _, fd := range fds {
|
||||||
n, err := syscall.Readlink(path.Join(fdPath, fd.Name()), buffer)
|
n, err := unix.Readlink(filepath.Join(fdPath, fd.Name()), buffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -209,9 +171,10 @@ func resolveProcessNameByProcSearch(inode, uid int32) (string, error) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if bytes.Equal(buffer[:n], socket) {
|
if bytes.Equal(buffer[:n], socket) {
|
||||||
return os.Readlink(path.Join(processPath, "exe"))
|
return os.Readlink(filepath.Join(processPath, "exe"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -222,7 +185,7 @@ func splitCmdline(cmdline []byte) string {
|
|||||||
cmdline = bytes.Trim(cmdline, " ")
|
cmdline = bytes.Trim(cmdline, " ")
|
||||||
|
|
||||||
idx := bytes.IndexFunc(cmdline, func(r rune) bool {
|
idx := bytes.IndexFunc(cmdline, func(r rune) bool {
|
||||||
return unicode.IsControl(r) || unicode.IsSpace(r)
|
return unicode.IsControl(r) || unicode.IsSpace(r) || r == ':'
|
||||||
})
|
})
|
||||||
|
|
||||||
if idx == -1 {
|
if idx == -1 {
|
||||||
|
@ -4,10 +4,10 @@ package process
|
|||||||
|
|
||||||
import "net/netip"
|
import "net/netip"
|
||||||
|
|
||||||
func findProcessName(network string, ip netip.Addr, srcPort int) (int32, string, error) {
|
func findProcessName(network string, ip netip.Addr, srcPort int) (*uint32, string, error) {
|
||||||
return -1, "", ErrPlatformNotSupport
|
return nil, "", ErrPlatformNotSupport
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveSocketByNetlink(network string, ip netip.Addr, srcPort int) (int32, int32, error) {
|
func resolveSocketByNetlink(network string, ip netip.Addr, srcPort int) (uint32, uint32, error) {
|
||||||
return 0, 0, ErrPlatformNotSupport
|
return 0, 0, ErrPlatformNotSupport
|
||||||
}
|
}
|
||||||
|
@ -29,7 +29,7 @@ var (
|
|||||||
once sync.Once
|
once sync.Once
|
||||||
)
|
)
|
||||||
|
|
||||||
func resolveSocketByNetlink(network string, ip netip.Addr, srcPort int) (int32, int32, error) {
|
func resolveSocketByNetlink(network string, ip netip.Addr, srcPort int) (uint32, uint32, error) {
|
||||||
return 0, 0, ErrPlatformNotSupport
|
return 0, 0, ErrPlatformNotSupport
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,7 +62,7 @@ func initWin32API() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func findProcessName(network string, ip netip.Addr, srcPort int) (int32, string, error) {
|
func findProcessName(network string, ip netip.Addr, srcPort int) (*uint32, string, error) {
|
||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
err := initWin32API()
|
err := initWin32API()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -86,22 +86,22 @@ func findProcessName(network string, ip netip.Addr, srcPort int) (int32, string,
|
|||||||
fn = getExUDPTable
|
fn = getExUDPTable
|
||||||
class = udpTablePid
|
class = udpTablePid
|
||||||
default:
|
default:
|
||||||
return -1, "", ErrInvalidNetwork
|
return nil, "", ErrInvalidNetwork
|
||||||
}
|
}
|
||||||
|
|
||||||
buf, err := getTransportTable(fn, family, class)
|
buf, err := getTransportTable(fn, family, class)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, "", err
|
return nil, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
s := newSearcher(family == windows.AF_INET, network == TCP)
|
s := newSearcher(family == windows.AF_INET, network == TCP)
|
||||||
|
|
||||||
pid, err := s.Search(buf, ip, uint16(srcPort))
|
pid, err := s.Search(buf, ip, uint16(srcPort))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, "", err
|
return nil, "", err
|
||||||
}
|
}
|
||||||
pp, err := getExecPathFromPID(pid)
|
pp, err := getExecPathFromPID(pid)
|
||||||
return -1, pp, err
|
return nil, pp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
type searcher struct {
|
type searcher struct {
|
||||||
@ -220,7 +220,8 @@ func getExecPathFromPID(pid uint32) (string, error) {
|
|||||||
uintptr(h),
|
uintptr(h),
|
||||||
uintptr(1),
|
uintptr(1),
|
||||||
uintptr(unsafe.Pointer(&buf[0])),
|
uintptr(unsafe.Pointer(&buf[0])),
|
||||||
uintptr(unsafe.Pointer(&size)))
|
uintptr(unsafe.Pointer(&size)),
|
||||||
|
)
|
||||||
if r1 == 0 {
|
if r1 == 0 {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,21 @@
|
|||||||
package resolver
|
package resolver
|
||||||
|
|
||||||
import D "github.com/miekg/dns"
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
D "github.com/miekg/dns"
|
||||||
|
)
|
||||||
|
|
||||||
var DefaultLocalServer LocalServer
|
var DefaultLocalServer LocalServer
|
||||||
|
|
||||||
type LocalServer interface {
|
type LocalServer interface {
|
||||||
ServeMsg(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(msg *D.Msg) (*D.Msg, error) {
|
func ServeMsg(ctx context.Context, msg *D.Msg) (*D.Msg, error) {
|
||||||
if server := DefaultLocalServer; server != nil {
|
if server := DefaultLocalServer; server != nil {
|
||||||
return server.ServeMsg(msg)
|
return server.ServeMsg(ctx, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, ErrIPNotFound
|
return nil, ErrIPNotFound
|
||||||
|
@ -3,12 +3,13 @@ package resolver
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/nnip"
|
|
||||||
"github.com/Dreamacro/clash/component/trie"
|
"github.com/Dreamacro/clash/component/trie"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -37,132 +38,19 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Resolver interface {
|
type Resolver interface {
|
||||||
ResolveIP(host string) (ip netip.Addr, err error)
|
LookupIP(ctx context.Context, host string) (ips []netip.Addr, err error)
|
||||||
ResolveIPv4(host string) (ip netip.Addr, err error)
|
LookupIPv4(ctx context.Context, host string) (ips []netip.Addr, err error)
|
||||||
ResolveIPv6(host string) (ip netip.Addr, err error)
|
LookupIPv6(ctx context.Context, host string) (ips []netip.Addr, err error)
|
||||||
ResolveAllIP(host string) (ip []netip.Addr, err error)
|
ResolveIP(ctx context.Context, host string) (ip netip.Addr, err error)
|
||||||
ResolveAllIPv4(host string) (ips []netip.Addr, err error)
|
ResolveIPv4(ctx context.Context, host string) (ip netip.Addr, err error)
|
||||||
ResolveAllIPv6(host string) (ips []netip.Addr, err error)
|
ResolveIPv6(ctx context.Context, host string) (ip netip.Addr, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResolveIPv4 with a host, return ipv4
|
// LookupIPv4WithResolver same as LookupIPv4, but with a resolver
|
||||||
func ResolveIPv4(host string) (netip.Addr, error) {
|
func LookupIPv4WithResolver(ctx context.Context, host string, r Resolver) ([]netip.Addr, error) {
|
||||||
return ResolveIPv4WithResolver(host, DefaultResolver)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ResolveIPv4WithResolver(host string, r Resolver) (netip.Addr, error) {
|
|
||||||
if ips, err := ResolveAllIPv4WithResolver(host, r); err == nil {
|
|
||||||
return ips[rand.Intn(len(ips))], nil
|
|
||||||
} else {
|
|
||||||
return netip.Addr{}, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ResolveIPv6 with a host, return ipv6
|
|
||||||
func ResolveIPv6(host string) (netip.Addr, error) {
|
|
||||||
return ResolveIPv6WithResolver(host, DefaultResolver)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ResolveIPv6WithResolver(host string, r Resolver) (netip.Addr, error) {
|
|
||||||
if ips, err := ResolveAllIPv6WithResolver(host, r); err == nil {
|
|
||||||
return ips[rand.Intn(len(ips))], nil
|
|
||||||
} else {
|
|
||||||
return netip.Addr{}, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ResolveIPWithResolver same as ResolveIP, but with a resolver
|
|
||||||
func ResolveIPWithResolver(host string, r Resolver) (netip.Addr, error) {
|
|
||||||
if ip, err := ResolveIPv4WithResolver(host, r); err == nil {
|
|
||||||
return ip, nil
|
|
||||||
} else {
|
|
||||||
return ResolveIPv6WithResolver(host, r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ResolveIP with a host, return ip
|
|
||||||
func ResolveIP(host string) (netip.Addr, error) {
|
|
||||||
return ResolveIPWithResolver(host, DefaultResolver)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ResolveIPv4ProxyServerHost proxies server host only
|
|
||||||
func ResolveIPv4ProxyServerHost(host string) (netip.Addr, error) {
|
|
||||||
if ProxyServerHostResolver != nil {
|
|
||||||
if ip, err := ResolveIPv4WithResolver(host, ProxyServerHostResolver); err != nil {
|
|
||||||
return ResolveIPv4(host)
|
|
||||||
} else {
|
|
||||||
return ip, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ResolveIPv4(host)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ResolveIPv6ProxyServerHost proxies server host only
|
|
||||||
func ResolveIPv6ProxyServerHost(host string) (netip.Addr, error) {
|
|
||||||
if ProxyServerHostResolver != nil {
|
|
||||||
if ip, err := ResolveIPv6WithResolver(host, ProxyServerHostResolver); err != nil {
|
|
||||||
return ResolveIPv6(host)
|
|
||||||
} else {
|
|
||||||
return ip, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ResolveIPv6(host)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ResolveProxyServerHost proxies server host only
|
|
||||||
func ResolveProxyServerHost(host string) (netip.Addr, error) {
|
|
||||||
if ProxyServerHostResolver != nil {
|
|
||||||
if ip, err := ResolveIPWithResolver(host, ProxyServerHostResolver); err != nil {
|
|
||||||
return ResolveIP(host)
|
|
||||||
} else {
|
|
||||||
return ip, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ResolveIP(host)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ResolveAllIPv6WithResolver(host string, r Resolver) ([]netip.Addr, error) {
|
|
||||||
if DisableIPv6 {
|
|
||||||
return []netip.Addr{}, ErrIPv6Disabled
|
|
||||||
}
|
|
||||||
|
|
||||||
if node := DefaultHosts.Search(host); node != nil {
|
if node := DefaultHosts.Search(host); node != nil {
|
||||||
if ip := node.Data; ip.Is6() {
|
if ip := node.Data(); ip.Is4() {
|
||||||
return []netip.Addr{ip}, nil
|
return []netip.Addr{node.Data()}, nil
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ip, err := netip.ParseAddr(host)
|
|
||||||
if err == nil {
|
|
||||||
if ip.Is6() {
|
|
||||||
return []netip.Addr{ip}, nil
|
|
||||||
}
|
|
||||||
return []netip.Addr{}, ErrIPVersion
|
|
||||||
}
|
|
||||||
|
|
||||||
if r != nil {
|
|
||||||
return r.ResolveAllIPv6(host)
|
|
||||||
}
|
|
||||||
|
|
||||||
if DefaultResolver == nil {
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), DefaultDNSTimeout)
|
|
||||||
defer cancel()
|
|
||||||
ipAddrs, err := net.DefaultResolver.LookupIP(ctx, "ip6", host)
|
|
||||||
if err != nil {
|
|
||||||
return []netip.Addr{}, err
|
|
||||||
} else if len(ipAddrs) == 0 {
|
|
||||||
return []netip.Addr{}, ErrIPNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
return []netip.Addr{netip.AddrFrom16(*(*[16]byte)(ipAddrs[rand.Intn(len(ipAddrs))]))}, nil
|
|
||||||
}
|
|
||||||
return []netip.Addr{}, ErrIPNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
func ResolveAllIPv4WithResolver(host string, r Resolver) ([]netip.Addr, error) {
|
|
||||||
if node := DefaultHosts.Search(host); node != nil {
|
|
||||||
if ip := node.Data; ip.Is4() {
|
|
||||||
return []netip.Addr{node.Data}, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,89 +63,203 @@ func ResolveAllIPv4WithResolver(host string, r Resolver) ([]netip.Addr, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if r != nil {
|
if r != nil {
|
||||||
return r.ResolveAllIPv4(host)
|
return r.LookupIPv4(ctx, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
if DefaultResolver == nil {
|
if DefaultResolver != nil {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), DefaultDNSTimeout)
|
return DefaultResolver.LookupIPv4(ctx, host)
|
||||||
defer cancel()
|
|
||||||
ipAddrs, err := net.DefaultResolver.LookupIP(ctx, "ip4", host)
|
|
||||||
if err != nil {
|
|
||||||
return []netip.Addr{}, err
|
|
||||||
} else if len(ipAddrs) == 0 {
|
|
||||||
return []netip.Addr{}, ErrIPNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
ip := ipAddrs[rand.Intn(len(ipAddrs))].To4()
|
|
||||||
if ip == nil {
|
|
||||||
return []netip.Addr{}, ErrIPVersion
|
|
||||||
}
|
|
||||||
|
|
||||||
return []netip.Addr{netip.AddrFrom4(*(*[4]byte)(ip))}, nil
|
|
||||||
}
|
}
|
||||||
return []netip.Addr{}, ErrIPNotFound
|
|
||||||
|
ipAddrs, err := net.DefaultResolver.LookupNetIP(ctx, "ip4", host)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else if len(ipAddrs) == 0 {
|
||||||
|
return nil, ErrIPNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
return ipAddrs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ResolveAllIPWithResolver(host string, r Resolver) ([]netip.Addr, error) {
|
// LookupIPv4 with a host, return ipv4 list
|
||||||
if node := DefaultHosts.Search(host); node != nil {
|
func LookupIPv4(ctx context.Context, host string) ([]netip.Addr, error) {
|
||||||
return []netip.Addr{node.Data}, nil
|
return LookupIPv4WithResolver(ctx, host, DefaultResolver)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResolveIPv4WithResolver same as ResolveIPv4, but with a resolver
|
||||||
|
func ResolveIPv4WithResolver(ctx context.Context, host string, r Resolver) (netip.Addr, error) {
|
||||||
|
ips, err := LookupIPv4WithResolver(ctx, host, r)
|
||||||
|
if err != nil {
|
||||||
|
return netip.Addr{}, err
|
||||||
|
} else if len(ips) == 0 {
|
||||||
|
return netip.Addr{}, fmt.Errorf("%w: %s", ErrIPNotFound, host)
|
||||||
|
}
|
||||||
|
return ips[rand.Intn(len(ips))], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResolveIPv4 with a host, return ipv4
|
||||||
|
func ResolveIPv4(ctx context.Context, host string) (netip.Addr, error) {
|
||||||
|
return ResolveIPv4WithResolver(ctx, host, DefaultResolver)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LookupIPv6WithResolver same as LookupIPv6, but with a resolver
|
||||||
|
func LookupIPv6WithResolver(ctx context.Context, host string, r Resolver) ([]netip.Addr, error) {
|
||||||
|
if DisableIPv6 {
|
||||||
|
return nil, ErrIPv6Disabled
|
||||||
}
|
}
|
||||||
|
|
||||||
ip, err := netip.ParseAddr(host)
|
if node := DefaultHosts.Search(host); node != nil {
|
||||||
if err == nil {
|
if ip := node.Data(); ip.Is6() {
|
||||||
return []netip.Addr{ip}, nil
|
return []netip.Addr{ip}, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ip, err := netip.ParseAddr(host); err == nil {
|
||||||
|
if strings.Contains(host, ":") {
|
||||||
|
return []netip.Addr{ip}, nil
|
||||||
|
}
|
||||||
|
return nil, ErrIPVersion
|
||||||
|
}
|
||||||
|
|
||||||
|
if r != nil {
|
||||||
|
return r.LookupIPv6(ctx, host)
|
||||||
|
}
|
||||||
|
if DefaultResolver != nil {
|
||||||
|
return DefaultResolver.LookupIPv6(ctx, host)
|
||||||
|
}
|
||||||
|
|
||||||
|
ipAddrs, err := net.DefaultResolver.LookupNetIP(ctx, "ip6", host)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else if len(ipAddrs) == 0 {
|
||||||
|
return nil, ErrIPNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
return ipAddrs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// LookupIPv6 with a host, return ipv6 list
|
||||||
|
func LookupIPv6(ctx context.Context, host string) ([]netip.Addr, error) {
|
||||||
|
return LookupIPv6WithResolver(ctx, host, DefaultResolver)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResolveIPv6WithResolver same as ResolveIPv6, but with a resolver
|
||||||
|
func ResolveIPv6WithResolver(ctx context.Context, host string, r Resolver) (netip.Addr, error) {
|
||||||
|
ips, err := LookupIPv6WithResolver(ctx, host, r)
|
||||||
|
if err != nil {
|
||||||
|
return netip.Addr{}, err
|
||||||
|
} else if len(ips) == 0 {
|
||||||
|
return netip.Addr{}, fmt.Errorf("%w: %s", ErrIPNotFound, host)
|
||||||
|
}
|
||||||
|
return ips[rand.Intn(len(ips))], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ResolveIPv6(ctx context.Context, host string) (netip.Addr, error) {
|
||||||
|
return ResolveIPv6WithResolver(ctx, host, DefaultResolver)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LookupIPWithResolver same as LookupIP, but with a resolver
|
||||||
|
func LookupIPWithResolver(ctx context.Context, host string, r Resolver) ([]netip.Addr, error) {
|
||||||
|
if node := DefaultHosts.Search(host); node != nil {
|
||||||
|
return []netip.Addr{node.Data()}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if r != nil {
|
if r != nil {
|
||||||
if DisableIPv6 {
|
if DisableIPv6 {
|
||||||
return r.ResolveAllIPv4(host)
|
return r.LookupIPv4(ctx, host)
|
||||||
}
|
}
|
||||||
|
return r.LookupIP(ctx, host)
|
||||||
return r.ResolveAllIP(host)
|
|
||||||
} else if DisableIPv6 {
|
} else if DisableIPv6 {
|
||||||
return ResolveAllIPv4(host)
|
return LookupIPv4(ctx, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
if DefaultResolver == nil {
|
if ip, err := netip.ParseAddr(host); err == nil {
|
||||||
ipAddr, err := net.ResolveIPAddr("ip", host)
|
return []netip.Addr{ip}, nil
|
||||||
if err != nil {
|
}
|
||||||
return []netip.Addr{}, err
|
|
||||||
|
ips, err := net.DefaultResolver.LookupNetIP(ctx, "ip", host)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else if len(ips) == 0 {
|
||||||
|
return nil, ErrIPNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
return ips, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// LookupIP with a host, return ip
|
||||||
|
func LookupIP(ctx context.Context, host string) ([]netip.Addr, error) {
|
||||||
|
return LookupIPWithResolver(ctx, host, DefaultResolver)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResolveIPWithResolver same as ResolveIP, but with a resolver
|
||||||
|
func ResolveIPWithResolver(ctx context.Context, host string, r Resolver) (netip.Addr, error) {
|
||||||
|
ips, err := LookupIPWithResolver(ctx, host, r)
|
||||||
|
if err != nil {
|
||||||
|
return netip.Addr{}, err
|
||||||
|
} else if len(ips) == 0 {
|
||||||
|
return netip.Addr{}, fmt.Errorf("%w: %s", ErrIPNotFound, host)
|
||||||
|
}
|
||||||
|
return ips[rand.Intn(len(ips))], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResolveIP with a host, return ip
|
||||||
|
func ResolveIP(ctx context.Context, host string) (netip.Addr, error) {
|
||||||
|
return ResolveIPWithResolver(ctx, host, DefaultResolver)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResolveIPv4ProxyServerHost proxies server host only
|
||||||
|
func ResolveIPv4ProxyServerHost(ctx context.Context, host string) (netip.Addr, error) {
|
||||||
|
if ProxyServerHostResolver != nil {
|
||||||
|
if ip, err := ResolveIPv4WithResolver(ctx, host, ProxyServerHostResolver); err != nil {
|
||||||
|
return ResolveIPv4(ctx, host)
|
||||||
|
} else {
|
||||||
|
return ip, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return []netip.Addr{nnip.IpToAddr(ipAddr.IP)}, nil
|
|
||||||
}
|
}
|
||||||
return []netip.Addr{}, ErrIPNotFound
|
return ResolveIPv4(ctx, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ResolveAllIP(host string) ([]netip.Addr, error) {
|
// ResolveIPv6ProxyServerHost proxies server host only
|
||||||
return ResolveAllIPWithResolver(host, DefaultResolver)
|
func ResolveIPv6ProxyServerHost(ctx context.Context, host string) (netip.Addr, error) {
|
||||||
}
|
|
||||||
|
|
||||||
func ResolveAllIPv4(host string) ([]netip.Addr, error) {
|
|
||||||
return ResolveAllIPv4WithResolver(host, DefaultResolver)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ResolveAllIPv6(host string) ([]netip.Addr, error) {
|
|
||||||
return ResolveAllIPv6WithResolver(host, DefaultResolver)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ResolveAllIPv6ProxyServerHost(host string) ([]netip.Addr, error) {
|
|
||||||
if ProxyServerHostResolver != nil {
|
if ProxyServerHostResolver != nil {
|
||||||
return ResolveAllIPv6WithResolver(host, ProxyServerHostResolver)
|
if ip, err := ResolveIPv6WithResolver(ctx, host, ProxyServerHostResolver); err != nil {
|
||||||
|
return ResolveIPv6(ctx, host)
|
||||||
|
} else {
|
||||||
|
return ip, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return ResolveAllIPv6(host)
|
return ResolveIPv6(ctx, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ResolveAllIPv4ProxyServerHost(host string) ([]netip.Addr, error) {
|
// ResolveProxyServerHost proxies server host only
|
||||||
|
func ResolveProxyServerHost(ctx context.Context, host string) (netip.Addr, error) {
|
||||||
if ProxyServerHostResolver != nil {
|
if ProxyServerHostResolver != nil {
|
||||||
return ResolveAllIPv4WithResolver(host, ProxyServerHostResolver)
|
if ip, err := ResolveIPWithResolver(ctx, host, ProxyServerHostResolver); err != nil {
|
||||||
|
return ResolveIP(ctx, host)
|
||||||
|
} else {
|
||||||
|
return ip, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return ResolveAllIPv4(host)
|
return ResolveIP(ctx, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ResolveAllIPProxyServerHost(host string) ([]netip.Addr, error) {
|
func LookupIPv6ProxyServerHost(ctx context.Context, host string) ([]netip.Addr, error) {
|
||||||
if ProxyServerHostResolver != nil {
|
if ProxyServerHostResolver != nil {
|
||||||
return ResolveAllIPWithResolver(host, ProxyServerHostResolver)
|
return LookupIPv6WithResolver(ctx, host, ProxyServerHostResolver)
|
||||||
}
|
}
|
||||||
return ResolveAllIP(host)
|
return LookupIPv6(ctx, host)
|
||||||
|
}
|
||||||
|
|
||||||
|
func LookupIPv4ProxyServerHost(ctx context.Context, host string) ([]netip.Addr, error) {
|
||||||
|
if ProxyServerHostResolver != nil {
|
||||||
|
return LookupIPv4WithResolver(ctx, host, ProxyServerHostResolver)
|
||||||
|
}
|
||||||
|
return LookupIPv4(ctx, host)
|
||||||
|
}
|
||||||
|
|
||||||
|
func LookupIPProxyServerHost(ctx context.Context, host string) ([]netip.Addr, error) {
|
||||||
|
if ProxyServerHostResolver != nil {
|
||||||
|
return LookupIPWithResolver(ctx, host, ProxyServerHostResolver)
|
||||||
|
}
|
||||||
|
return LookupIP(ctx, host)
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,10 @@ func (f *Fetcher[V]) Name() string {
|
|||||||
return f.name
|
return f.name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *Fetcher[V]) Vehicle() types.Vehicle {
|
||||||
|
return f.vehicle
|
||||||
|
}
|
||||||
|
|
||||||
func (f *Fetcher[V]) VehicleType() types.VehicleType {
|
func (f *Fetcher[V]) VehicleType() types.VehicleType {
|
||||||
return f.vehicle.Type()
|
return f.vehicle.Type()
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ package resource
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
netHttp "github.com/Dreamacro/clash/component/http"
|
clashHttp "github.com/Dreamacro/clash/component/http"
|
||||||
types "github.com/Dreamacro/clash/constant/provider"
|
types "github.com/Dreamacro/clash/constant/provider"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -35,6 +35,10 @@ type HTTPVehicle struct {
|
|||||||
path string
|
path string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *HTTPVehicle) Url() string {
|
||||||
|
return h.url
|
||||||
|
}
|
||||||
|
|
||||||
func (h *HTTPVehicle) Type() types.VehicleType {
|
func (h *HTTPVehicle) Type() types.VehicleType {
|
||||||
return types.HTTP
|
return types.HTTP
|
||||||
}
|
}
|
||||||
@ -46,7 +50,7 @@ func (h *HTTPVehicle) Path() string {
|
|||||||
func (h *HTTPVehicle) Read() ([]byte, error) {
|
func (h *HTTPVehicle) Read() ([]byte, error) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
resp, err := netHttp.HttpRequest(ctx, h.url, http.MethodGet, nil, nil)
|
resp, err := clashHttp.HttpRequest(ctx, h.url, http.MethodGet, nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -31,8 +31,8 @@ type SnifferDispatcher struct {
|
|||||||
|
|
||||||
sniffers []sniffer.Sniffer
|
sniffers []sniffer.Sniffer
|
||||||
|
|
||||||
forceDomain *trie.DomainTrie[bool]
|
forceDomain *trie.DomainTrie[struct{}]
|
||||||
skipSNI *trie.DomainTrie[bool]
|
skipSNI *trie.DomainTrie[struct{}]
|
||||||
portRanges *[]utils.Range[uint16]
|
portRanges *[]utils.Range[uint16]
|
||||||
skipList *cache.LruCache[string, uint8]
|
skipList *cache.LruCache[string, uint8]
|
||||||
rwMux sync.RWMutex
|
rwMux sync.RWMutex
|
||||||
@ -112,7 +112,6 @@ func (sd *SnifferDispatcher) replaceDomain(metadata *C.Metadata, host string) {
|
|||||||
metadata.Host, host)
|
metadata.Host, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
metadata.AddrType = C.AtypDomainName
|
|
||||||
metadata.Host = host
|
metadata.Host = host
|
||||||
metadata.DNSMode = C.DNSNormal
|
metadata.DNSMode = C.DNSNormal
|
||||||
}
|
}
|
||||||
@ -183,15 +182,15 @@ func NewCloseSnifferDispatcher() (*SnifferDispatcher, error) {
|
|||||||
return &dispatcher, nil
|
return &dispatcher, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSnifferDispatcher(needSniffer []sniffer.Type, forceDomain *trie.DomainTrie[bool],
|
func NewSnifferDispatcher(needSniffer []sniffer.Type, forceDomain *trie.DomainTrie[struct{}],
|
||||||
skipSNI *trie.DomainTrie[bool], ports *[]utils.Range[uint16],
|
skipSNI *trie.DomainTrie[struct{}], ports *[]utils.Range[uint16],
|
||||||
forceDnsMapping bool, parsePureIp bool) (*SnifferDispatcher, error) {
|
forceDnsMapping bool, parsePureIp bool) (*SnifferDispatcher, error) {
|
||||||
dispatcher := SnifferDispatcher{
|
dispatcher := SnifferDispatcher{
|
||||||
enable: true,
|
enable: true,
|
||||||
forceDomain: forceDomain,
|
forceDomain: forceDomain,
|
||||||
skipSNI: skipSNI,
|
skipSNI: skipSNI,
|
||||||
portRanges: ports,
|
portRanges: ports,
|
||||||
skipList: cache.NewLRUCache[string, uint8](cache.WithSize[string, uint8](128), cache.WithAge[string, uint8](600)),
|
skipList: cache.New[string, uint8](cache.WithSize[string, uint8](128), cache.WithAge[string, uint8](600)),
|
||||||
forceDnsMapping: forceDnsMapping,
|
forceDnsMapping: forceDnsMapping,
|
||||||
parsePureIp: parsePureIp,
|
parsePureIp: parsePureIp,
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ var ErrInvalidDomain = errors.New("invalid domain")
|
|||||||
|
|
||||||
// DomainTrie contains the main logic for adding and searching nodes for domain segments.
|
// DomainTrie contains the main logic for adding and searching nodes for domain segments.
|
||||||
// support wildcard domain (e.g *.google.com)
|
// support wildcard domain (e.g *.google.com)
|
||||||
type DomainTrie[T comparable] struct {
|
type DomainTrie[T any] struct {
|
||||||
root *Node[T]
|
root *Node[T]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,14 +73,10 @@ func (t *DomainTrie[T]) insert(parts []string, data T) {
|
|||||||
// reverse storage domain part to save space
|
// reverse storage domain part to save space
|
||||||
for i := len(parts) - 1; i >= 0; i-- {
|
for i := len(parts) - 1; i >= 0; i-- {
|
||||||
part := parts[i]
|
part := parts[i]
|
||||||
if !node.hasChild(part) {
|
node = node.getOrNewChild(part)
|
||||||
node.addChild(part, newNode(getZero[T]()))
|
|
||||||
}
|
|
||||||
|
|
||||||
node = node.getChild(part)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
node.Data = data
|
node.setData(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search is the most important part of the Trie.
|
// Search is the most important part of the Trie.
|
||||||
@ -96,7 +92,7 @@ func (t *DomainTrie[T]) Search(domain string) *Node[T] {
|
|||||||
|
|
||||||
n := t.search(t.root, parts)
|
n := t.search(t.root, parts)
|
||||||
|
|
||||||
if n == nil || n.Data == getZero[T]() {
|
if n.isEmpty() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,13 +105,13 @@ func (t *DomainTrie[T]) search(node *Node[T], parts []string) *Node[T] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if c := node.getChild(parts[len(parts)-1]); c != nil {
|
if c := node.getChild(parts[len(parts)-1]); c != nil {
|
||||||
if n := t.search(c, parts[:len(parts)-1]); n != nil && n.Data != getZero[T]() {
|
if n := t.search(c, parts[:len(parts)-1]); !n.isEmpty() {
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if c := node.getChild(wildcard); c != nil {
|
if c := node.getChild(wildcard); c != nil {
|
||||||
if n := t.search(c, parts[:len(parts)-1]); n != nil && n.Data != getZero[T]() {
|
if n := t.search(c, parts[:len(parts)-1]); !n.isEmpty() {
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -123,7 +119,11 @@ func (t *DomainTrie[T]) search(node *Node[T], parts []string) *Node[T] {
|
|||||||
return node.getChild(dotWildcard)
|
return node.getChild(dotWildcard)
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a new, empty Trie.
|
func (t *DomainTrie[T]) Optimize() {
|
||||||
func New[T comparable]() *DomainTrie[T] {
|
t.root.optimize()
|
||||||
return &DomainTrie[T]{root: newNode[T](getZero[T]())}
|
}
|
||||||
|
|
||||||
|
// New returns a new, empty Trie.
|
||||||
|
func New[T any]() *DomainTrie[T] {
|
||||||
|
return &DomainTrie[T]{root: newNode[T]()}
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ func TestTrie_Basic(t *testing.T) {
|
|||||||
|
|
||||||
node := tree.Search("example.com")
|
node := tree.Search("example.com")
|
||||||
assert.NotNil(t, node)
|
assert.NotNil(t, node)
|
||||||
assert.True(t, node.Data == localIP)
|
assert.True(t, node.Data() == localIP)
|
||||||
assert.NotNil(t, tree.Insert("", localIP))
|
assert.NotNil(t, tree.Insert("", localIP))
|
||||||
assert.Nil(t, tree.Search(""))
|
assert.Nil(t, tree.Search(""))
|
||||||
assert.NotNil(t, tree.Search("localhost"))
|
assert.NotNil(t, tree.Search("localhost"))
|
||||||
@ -75,7 +75,7 @@ func TestTrie_Priority(t *testing.T) {
|
|||||||
assertFn := func(domain string, data int) {
|
assertFn := func(domain string, data int) {
|
||||||
node := tree.Search(domain)
|
node := tree.Search(domain)
|
||||||
assert.NotNil(t, node)
|
assert.NotNil(t, node)
|
||||||
assert.Equal(t, data, node.Data)
|
assert.Equal(t, data, node.Data())
|
||||||
}
|
}
|
||||||
|
|
||||||
for idx, domain := range domains {
|
for idx, domain := range domains {
|
||||||
|
@ -1,13 +1,24 @@
|
|||||||
package trie
|
package trie
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
// Node is the trie's node
|
// Node is the trie's node
|
||||||
type Node[T comparable] struct {
|
type Node[T any] struct {
|
||||||
children map[string]*Node[T]
|
childMap map[string]*Node[T]
|
||||||
Data T
|
childNode *Node[T] // optimize for only one child
|
||||||
|
childStr string
|
||||||
|
inited bool
|
||||||
|
data T
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node[T]) getChild(s string) *Node[T] {
|
func (n *Node[T]) getChild(s string) *Node[T] {
|
||||||
return n.children[s]
|
if n.childMap == nil {
|
||||||
|
if n.childNode != nil && n.childStr == s {
|
||||||
|
return n.childNode
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return n.childMap[s]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node[T]) hasChild(s string) bool {
|
func (n *Node[T]) hasChild(s string) bool {
|
||||||
@ -15,17 +26,100 @@ func (n *Node[T]) hasChild(s string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (n *Node[T]) addChild(s string, child *Node[T]) {
|
func (n *Node[T]) addChild(s string, child *Node[T]) {
|
||||||
n.children[s] = child
|
if n.childMap == nil {
|
||||||
}
|
if n.childNode == nil {
|
||||||
|
n.childStr = s
|
||||||
func newNode[T comparable](data T) *Node[T] {
|
n.childNode = child
|
||||||
return &Node[T]{
|
return
|
||||||
Data: data,
|
}
|
||||||
children: map[string]*Node[T]{},
|
n.childMap = map[string]*Node[T]{}
|
||||||
|
if n.childNode != nil {
|
||||||
|
n.childMap[n.childStr] = n.childNode
|
||||||
|
}
|
||||||
|
n.childStr = ""
|
||||||
|
n.childNode = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
n.childMap[s] = child
|
||||||
}
|
}
|
||||||
|
|
||||||
func getZero[T comparable]() T {
|
func (n *Node[T]) getOrNewChild(s string) *Node[T] {
|
||||||
var result T
|
node := n.getChild(s)
|
||||||
return result
|
if node == nil {
|
||||||
|
node = newNode[T]()
|
||||||
|
n.addChild(s, node)
|
||||||
|
}
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Node[T]) optimize() {
|
||||||
|
if len(n.childStr) > 0 {
|
||||||
|
n.childStr = strClone(n.childStr)
|
||||||
|
}
|
||||||
|
if n.childNode != nil {
|
||||||
|
n.childNode.optimize()
|
||||||
|
}
|
||||||
|
if n.childMap == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch len(n.childMap) {
|
||||||
|
case 0:
|
||||||
|
n.childMap = nil
|
||||||
|
return
|
||||||
|
case 1:
|
||||||
|
for key := range n.childMap {
|
||||||
|
n.childStr = key
|
||||||
|
n.childNode = n.childMap[key]
|
||||||
|
}
|
||||||
|
n.childMap = nil
|
||||||
|
n.optimize()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
children := make(map[string]*Node[T], len(n.childMap)) // avoid map reallocate memory
|
||||||
|
for key := range n.childMap {
|
||||||
|
child := n.childMap[key]
|
||||||
|
if child == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
key = strClone(key)
|
||||||
|
children[key] = child
|
||||||
|
child.optimize()
|
||||||
|
}
|
||||||
|
n.childMap = children
|
||||||
|
}
|
||||||
|
|
||||||
|
func strClone(key string) string {
|
||||||
|
switch key { // try to save string's memory
|
||||||
|
case wildcard:
|
||||||
|
key = wildcard
|
||||||
|
case dotWildcard:
|
||||||
|
key = dotWildcard
|
||||||
|
case complexWildcard:
|
||||||
|
key = complexWildcard
|
||||||
|
case domainStep:
|
||||||
|
key = domainStep
|
||||||
|
default:
|
||||||
|
key = strings.Clone(key)
|
||||||
|
}
|
||||||
|
return key
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Node[T]) isEmpty() bool {
|
||||||
|
if n == nil || n.inited == false {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Node[T]) setData(data T) {
|
||||||
|
n.data = data
|
||||||
|
n.inited = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Node[T]) Data() T {
|
||||||
|
return n.data
|
||||||
|
}
|
||||||
|
|
||||||
|
func newNode[T any]() *Node[T] {
|
||||||
|
return &Node[T]{}
|
||||||
}
|
}
|
||||||
|
604
config/config.go
604
config/config.go
@ -2,9 +2,9 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"container/list"
|
"container/list"
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
P "github.com/Dreamacro/clash/component/process"
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"net/url"
|
"net/url"
|
||||||
@ -14,14 +14,11 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/utils"
|
|
||||||
R "github.com/Dreamacro/clash/rules"
|
|
||||||
RP "github.com/Dreamacro/clash/rules/provider"
|
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapter"
|
"github.com/Dreamacro/clash/adapter"
|
||||||
"github.com/Dreamacro/clash/adapter/outbound"
|
"github.com/Dreamacro/clash/adapter/outbound"
|
||||||
"github.com/Dreamacro/clash/adapter/outboundgroup"
|
"github.com/Dreamacro/clash/adapter/outboundgroup"
|
||||||
"github.com/Dreamacro/clash/adapter/provider"
|
"github.com/Dreamacro/clash/adapter/provider"
|
||||||
|
"github.com/Dreamacro/clash/common/utils"
|
||||||
"github.com/Dreamacro/clash/component/auth"
|
"github.com/Dreamacro/clash/component/auth"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
"github.com/Dreamacro/clash/component/fakeip"
|
"github.com/Dreamacro/clash/component/fakeip"
|
||||||
@ -30,10 +27,13 @@ import (
|
|||||||
"github.com/Dreamacro/clash/component/trie"
|
"github.com/Dreamacro/clash/component/trie"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
providerTypes "github.com/Dreamacro/clash/constant/provider"
|
providerTypes "github.com/Dreamacro/clash/constant/provider"
|
||||||
"github.com/Dreamacro/clash/constant/sniffer"
|
|
||||||
snifferTypes "github.com/Dreamacro/clash/constant/sniffer"
|
snifferTypes "github.com/Dreamacro/clash/constant/sniffer"
|
||||||
"github.com/Dreamacro/clash/dns"
|
"github.com/Dreamacro/clash/dns"
|
||||||
|
L "github.com/Dreamacro/clash/listener"
|
||||||
|
LC "github.com/Dreamacro/clash/listener/config"
|
||||||
"github.com/Dreamacro/clash/log"
|
"github.com/Dreamacro/clash/log"
|
||||||
|
R "github.com/Dreamacro/clash/rules"
|
||||||
|
RP "github.com/Dreamacro/clash/rules/provider"
|
||||||
T "github.com/Dreamacro/clash/tunnel"
|
T "github.com/Dreamacro/clash/tunnel"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
@ -43,39 +43,44 @@ import (
|
|||||||
type General struct {
|
type General struct {
|
||||||
Inbound
|
Inbound
|
||||||
Controller
|
Controller
|
||||||
Mode T.TunnelMode `json:"mode"`
|
Mode T.TunnelMode `json:"mode"`
|
||||||
UnifiedDelay bool
|
UnifiedDelay bool
|
||||||
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:"-"`
|
RoutingMark int `json:"-"`
|
||||||
GeodataMode bool `json:"geodata-mode"`
|
GeodataMode bool `json:"geodata-mode"`
|
||||||
GeodataLoader string `json:"geodata-loader"`
|
GeodataLoader string `json:"geodata-loader"`
|
||||||
TCPConcurrent bool `json:"tcp-concurrent"`
|
TCPConcurrent bool `json:"tcp-concurrent"`
|
||||||
EnableProcess bool `json:"enable-process"`
|
EnableProcess bool `json:"enable-process"`
|
||||||
Tun Tun `json:"tun"`
|
FindProcessMode P.FindProcessMode `json:"find-process-mode"`
|
||||||
Sniffing bool `json:"sniffing"`
|
Sniffing bool `json:"sniffing"`
|
||||||
EBpf EBpf `json:"-"`
|
EBpf EBpf `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inbound config
|
// Inbound config
|
||||||
type Inbound struct {
|
type Inbound struct {
|
||||||
Port int `json:"port"`
|
Port int `json:"port"`
|
||||||
SocksPort int `json:"socks-port"`
|
SocksPort int `json:"socks-port"`
|
||||||
RedirPort int `json:"redir-port"`
|
RedirPort int `json:"redir-port"`
|
||||||
TProxyPort int `json:"tproxy-port"`
|
TProxyPort int `json:"tproxy-port"`
|
||||||
MixedPort int `json:"mixed-port"`
|
MixedPort int `json:"mixed-port"`
|
||||||
Authentication []string `json:"authentication"`
|
Tun LC.Tun `json:"tun"`
|
||||||
AllowLan bool `json:"allow-lan"`
|
TuicServer LC.TuicServer `json:"tuic-server"`
|
||||||
BindAddress string `json:"bind-address"`
|
ShadowSocksConfig string `json:"ss-config"`
|
||||||
InboundTfo bool `json:"inbound-tfo"`
|
VmessConfig string `json:"vmess-config"`
|
||||||
|
Authentication []string `json:"authentication"`
|
||||||
|
AllowLan bool `json:"allow-lan"`
|
||||||
|
BindAddress string `json:"bind-address"`
|
||||||
|
InboundTfo bool `json:"inbound-tfo"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Controller config
|
// Controller config
|
||||||
type Controller struct {
|
type Controller struct {
|
||||||
ExternalController string `json:"-"`
|
ExternalController string `json:"-"`
|
||||||
ExternalUI string `json:"-"`
|
ExternalControllerTLS string `json:"-"`
|
||||||
Secret string `json:"-"`
|
ExternalUI string `json:"-"`
|
||||||
|
Secret string `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// DNS config
|
// DNS config
|
||||||
@ -110,81 +115,9 @@ type Profile struct {
|
|||||||
StoreFakeIP bool `yaml:"store-fake-ip"`
|
StoreFakeIP bool `yaml:"store-fake-ip"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tun config
|
type TLS struct {
|
||||||
type Tun struct {
|
Certificate string `yaml:"certificate"`
|
||||||
Enable bool `yaml:"enable" json:"enable"`
|
PrivateKey string `yaml:"private-key"`
|
||||||
Device string `yaml:"device" json:"device"`
|
|
||||||
Stack C.TUNStack `yaml:"stack" json:"stack"`
|
|
||||||
DNSHijack []netip.AddrPort `yaml:"dns-hijack" json:"dns-hijack"`
|
|
||||||
AutoRoute bool `yaml:"auto-route" json:"auto-route"`
|
|
||||||
AutoDetectInterface bool `yaml:"auto-detect-interface" json:"auto-detect-interface"`
|
|
||||||
RedirectToTun []string `yaml:"-" json:"-"`
|
|
||||||
|
|
||||||
MTU uint32 `yaml:"mtu" json:"mtu,omitempty"`
|
|
||||||
Inet4Address []ListenPrefix `yaml:"inet4-address" json:"inet4_address,omitempty"`
|
|
||||||
Inet6Address []ListenPrefix `yaml:"inet6-address" json:"inet6_address,omitempty"`
|
|
||||||
StrictRoute bool `yaml:"strict-route" json:"strict_route,omitempty"`
|
|
||||||
Inet4RouteAddress []ListenPrefix `yaml:"inet4_route_address" json:"inet4_route_address,omitempty"`
|
|
||||||
Inet6RouteAddress []ListenPrefix `yaml:"inet6_route_address" json:"inet6_route_address,omitempty"`
|
|
||||||
IncludeUID []uint32 `yaml:"include-uid" json:"include_uid,omitempty"`
|
|
||||||
IncludeUIDRange []string `yaml:"include-uid-range" json:"include_uid_range,omitempty"`
|
|
||||||
ExcludeUID []uint32 `yaml:"exclude-uid" json:"exclude_uid,omitempty"`
|
|
||||||
ExcludeUIDRange []string `yaml:"exclude-uid-range" json:"exclude_uid_range,omitempty"`
|
|
||||||
IncludeAndroidUser []int `yaml:"include-android-user" json:"include_android_user,omitempty"`
|
|
||||||
IncludePackage []string `yaml:"include-package" json:"include_package,omitempty"`
|
|
||||||
ExcludePackage []string `yaml:"exclude-package" json:"exclude_package,omitempty"`
|
|
||||||
EndpointIndependentNat bool `yaml:"endpoint-independent-nat" json:"endpoint_independent_nat,omitempty"`
|
|
||||||
UDPTimeout int64 `yaml:"udp-timeout" json:"udp_timeout,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type ListenPrefix netip.Prefix
|
|
||||||
|
|
||||||
func (p ListenPrefix) MarshalJSON() ([]byte, error) {
|
|
||||||
prefix := netip.Prefix(p)
|
|
||||||
if !prefix.IsValid() {
|
|
||||||
return json.Marshal(nil)
|
|
||||||
}
|
|
||||||
return json.Marshal(prefix.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p ListenPrefix) MarshalYAML() (interface{}, error) {
|
|
||||||
prefix := netip.Prefix(p)
|
|
||||||
if !prefix.IsValid() {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
return prefix.String(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *ListenPrefix) UnmarshalJSON(bytes []byte) error {
|
|
||||||
var value string
|
|
||||||
err := json.Unmarshal(bytes, &value)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
prefix, err := netip.ParsePrefix(value)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*p = ListenPrefix(prefix)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *ListenPrefix) UnmarshalYAML(node *yaml.Node) error {
|
|
||||||
var value string
|
|
||||||
err := node.Decode(&value)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
prefix, err := netip.ParsePrefix(value)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*p = ListenPrefix(prefix)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p ListenPrefix) Build() netip.Prefix {
|
|
||||||
return netip.Prefix(p)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// IPTables config
|
// IPTables config
|
||||||
@ -196,10 +129,10 @@ type IPTables struct {
|
|||||||
|
|
||||||
type Sniffer struct {
|
type Sniffer struct {
|
||||||
Enable bool
|
Enable bool
|
||||||
Sniffers []sniffer.Type
|
Sniffers []snifferTypes.Type
|
||||||
Reverses *trie.DomainTrie[bool]
|
Reverses *trie.DomainTrie[struct{}]
|
||||||
ForceDomain *trie.DomainTrie[bool]
|
ForceDomain *trie.DomainTrie[struct{}]
|
||||||
SkipDomain *trie.DomainTrie[bool]
|
SkipDomain *trie.DomainTrie[struct{}]
|
||||||
Ports *[]utils.Range[uint16]
|
Ports *[]utils.Range[uint16]
|
||||||
ForceDnsMapping bool
|
ForceDnsMapping bool
|
||||||
ParsePureIp bool
|
ParsePureIp bool
|
||||||
@ -213,19 +146,21 @@ type Experimental struct {
|
|||||||
// Config is clash config manager
|
// Config is clash config manager
|
||||||
type Config struct {
|
type Config struct {
|
||||||
General *General
|
General *General
|
||||||
Tun *Tun
|
|
||||||
IPTables *IPTables
|
IPTables *IPTables
|
||||||
DNS *DNS
|
DNS *DNS
|
||||||
Experimental *Experimental
|
Experimental *Experimental
|
||||||
Hosts *trie.DomainTrie[netip.Addr]
|
Hosts *trie.DomainTrie[netip.Addr]
|
||||||
Profile *Profile
|
Profile *Profile
|
||||||
Rules []C.Rule
|
Rules []C.Rule
|
||||||
SubRules *map[string][]C.Rule
|
SubRules map[string][]C.Rule
|
||||||
Users []auth.AuthUser
|
Users []auth.AuthUser
|
||||||
Proxies map[string]C.Proxy
|
Proxies map[string]C.Proxy
|
||||||
|
Listeners map[string]C.InboundListener
|
||||||
Providers map[string]providerTypes.ProxyProvider
|
Providers map[string]providerTypes.ProxyProvider
|
||||||
RuleProviders map[string]providerTypes.RuleProvider
|
RuleProviders map[string]providerTypes.RuleProvider
|
||||||
|
Tunnels []LC.Tunnel
|
||||||
Sniffer *Sniffer
|
Sniffer *Sniffer
|
||||||
|
TLS *TLS
|
||||||
}
|
}
|
||||||
|
|
||||||
type RawDNS struct {
|
type RawDNS struct {
|
||||||
@ -263,45 +198,63 @@ type RawTun struct {
|
|||||||
RedirectToTun []string `yaml:"-" json:"-"`
|
RedirectToTun []string `yaml:"-" json:"-"`
|
||||||
|
|
||||||
MTU uint32 `yaml:"mtu" json:"mtu,omitempty"`
|
MTU uint32 `yaml:"mtu" json:"mtu,omitempty"`
|
||||||
//Inet4Address []ListenPrefix `yaml:"inet4-address" json:"inet4_address,omitempty"`
|
//Inet4Address []LC.ListenPrefix `yaml:"inet4-address" json:"inet4_address,omitempty"`
|
||||||
Inet6Address []ListenPrefix `yaml:"inet6-address" json:"inet6_address,omitempty"`
|
Inet6Address []LC.ListenPrefix `yaml:"inet6-address" json:"inet6_address,omitempty"`
|
||||||
StrictRoute bool `yaml:"strict-route" json:"strict_route,omitempty"`
|
StrictRoute bool `yaml:"strict-route" json:"strict_route,omitempty"`
|
||||||
Inet4RouteAddress []ListenPrefix `yaml:"inet4_route_address" json:"inet4_route_address,omitempty"`
|
Inet4RouteAddress []LC.ListenPrefix `yaml:"inet4_route_address" json:"inet4_route_address,omitempty"`
|
||||||
Inet6RouteAddress []ListenPrefix `yaml:"inet6_route_address" json:"inet6_route_address,omitempty"`
|
Inet6RouteAddress []LC.ListenPrefix `yaml:"inet6_route_address" json:"inet6_route_address,omitempty"`
|
||||||
IncludeUID []uint32 `yaml:"include-uid" json:"include_uid,omitempty"`
|
IncludeUID []uint32 `yaml:"include-uid" json:"include_uid,omitempty"`
|
||||||
IncludeUIDRange []string `yaml:"include-uid-range" json:"include_uid_range,omitempty"`
|
IncludeUIDRange []string `yaml:"include-uid-range" json:"include_uid_range,omitempty"`
|
||||||
ExcludeUID []uint32 `yaml:"exclude-uid" json:"exclude_uid,omitempty"`
|
ExcludeUID []uint32 `yaml:"exclude-uid" json:"exclude_uid,omitempty"`
|
||||||
ExcludeUIDRange []string `yaml:"exclude-uid-range" json:"exclude_uid_range,omitempty"`
|
ExcludeUIDRange []string `yaml:"exclude-uid-range" json:"exclude_uid_range,omitempty"`
|
||||||
IncludeAndroidUser []int `yaml:"include-android-user" json:"include_android_user,omitempty"`
|
IncludeAndroidUser []int `yaml:"include-android-user" json:"include_android_user,omitempty"`
|
||||||
IncludePackage []string `yaml:"include-package" json:"include_package,omitempty"`
|
IncludePackage []string `yaml:"include-package" json:"include_package,omitempty"`
|
||||||
ExcludePackage []string `yaml:"exclude-package" json:"exclude_package,omitempty"`
|
ExcludePackage []string `yaml:"exclude-package" json:"exclude_package,omitempty"`
|
||||||
EndpointIndependentNat bool `yaml:"endpoint-independent-nat" json:"endpoint_independent_nat,omitempty"`
|
EndpointIndependentNat bool `yaml:"endpoint-independent-nat" json:"endpoint_independent_nat,omitempty"`
|
||||||
UDPTimeout int64 `yaml:"udp-timeout" json:"udp_timeout,omitempty"`
|
UDPTimeout int64 `yaml:"udp-timeout" json:"udp_timeout,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type RawTuicServer struct {
|
||||||
|
Enable bool `yaml:"enable" json:"enable"`
|
||||||
|
Listen string `yaml:"listen" json:"listen"`
|
||||||
|
Token []string `yaml:"token" json:"token"`
|
||||||
|
Certificate string `yaml:"certificate" json:"certificate"`
|
||||||
|
PrivateKey string `yaml:"private-key" json:"private-key"`
|
||||||
|
CongestionController string `yaml:"congestion-controller" json:"congestion-controller,omitempty"`
|
||||||
|
MaxIdleTime int `yaml:"max-idle-time" json:"max-idle-time,omitempty"`
|
||||||
|
AuthenticationTimeout int `yaml:"authentication-timeout" json:"authentication-timeout,omitempty"`
|
||||||
|
ALPN []string `yaml:"alpn" json:"alpn,omitempty"`
|
||||||
|
MaxUdpRelayPacketSize int `yaml:"max-udp-relay-packet-size" json:"max-udp-relay-packet-size,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type RawConfig struct {
|
type RawConfig struct {
|
||||||
Port int `yaml:"port"`
|
Port int `yaml:"port"`
|
||||||
SocksPort int `yaml:"socks-port"`
|
SocksPort int `yaml:"socks-port"`
|
||||||
RedirPort int `yaml:"redir-port"`
|
RedirPort int `yaml:"redir-port"`
|
||||||
TProxyPort int `yaml:"tproxy-port"`
|
TProxyPort int `yaml:"tproxy-port"`
|
||||||
MixedPort int `yaml:"mixed-port"`
|
MixedPort int `yaml:"mixed-port"`
|
||||||
InboundTfo bool `yaml:"inbound-tfo"`
|
ShadowSocksConfig string `yaml:"ss-config"`
|
||||||
Authentication []string `yaml:"authentication"`
|
VmessConfig string `yaml:"vmess-config"`
|
||||||
AllowLan bool `yaml:"allow-lan"`
|
InboundTfo bool `yaml:"inbound-tfo"`
|
||||||
BindAddress string `yaml:"bind-address"`
|
Authentication []string `yaml:"authentication"`
|
||||||
Mode T.TunnelMode `yaml:"mode"`
|
AllowLan bool `yaml:"allow-lan"`
|
||||||
UnifiedDelay bool `yaml:"unified-delay"`
|
BindAddress string `yaml:"bind-address"`
|
||||||
LogLevel log.LogLevel `yaml:"log-level"`
|
Mode T.TunnelMode `yaml:"mode"`
|
||||||
IPv6 bool `yaml:"ipv6"`
|
UnifiedDelay bool `yaml:"unified-delay"`
|
||||||
ExternalController string `yaml:"external-controller"`
|
LogLevel log.LogLevel `yaml:"log-level"`
|
||||||
ExternalUI string `yaml:"external-ui"`
|
IPv6 bool `yaml:"ipv6"`
|
||||||
Secret string `yaml:"secret"`
|
ExternalController string `yaml:"external-controller"`
|
||||||
Interface string `yaml:"interface-name"`
|
ExternalControllerTLS string `yaml:"external-controller-tls"`
|
||||||
RoutingMark int `yaml:"routing-mark"`
|
ExternalUI string `yaml:"external-ui"`
|
||||||
GeodataMode bool `yaml:"geodata-mode"`
|
Secret string `yaml:"secret"`
|
||||||
GeodataLoader string `yaml:"geodata-loader"`
|
Interface string `yaml:"interface-name"`
|
||||||
TCPConcurrent bool `yaml:"tcp-concurrent" json:"tcp-concurrent"`
|
RoutingMark int `yaml:"routing-mark"`
|
||||||
EnableProcess bool `yaml:"enable-process" json:"enable-process"`
|
Tunnels []LC.Tunnel `yaml:"tunnels"`
|
||||||
|
GeodataMode bool `yaml:"geodata-mode"`
|
||||||
|
GeodataLoader string `yaml:"geodata-loader"`
|
||||||
|
TCPConcurrent bool `yaml:"tcp-concurrent" json:"tcp-concurrent"`
|
||||||
|
EnableProcess bool `yaml:"enable-process" json:"enable-process"`
|
||||||
|
FindProcessMode P.FindProcessMode `yaml:"find-process-mode" json:"find-process-mode"`
|
||||||
|
|
||||||
Sniffer RawSniffer `yaml:"sniffer"`
|
Sniffer RawSniffer `yaml:"sniffer"`
|
||||||
ProxyProvider map[string]map[string]any `yaml:"proxy-providers"`
|
ProxyProvider map[string]map[string]any `yaml:"proxy-providers"`
|
||||||
@ -309,6 +262,7 @@ type RawConfig struct {
|
|||||||
Hosts map[string]string `yaml:"hosts"`
|
Hosts map[string]string `yaml:"hosts"`
|
||||||
DNS RawDNS `yaml:"dns"`
|
DNS RawDNS `yaml:"dns"`
|
||||||
Tun RawTun `yaml:"tun"`
|
Tun RawTun `yaml:"tun"`
|
||||||
|
TuicServer RawTuicServer `yaml:"tuic-server"`
|
||||||
EBpf EBpf `yaml:"ebpf"`
|
EBpf EBpf `yaml:"ebpf"`
|
||||||
IPTables IPTables `yaml:"iptables"`
|
IPTables IPTables `yaml:"iptables"`
|
||||||
Experimental Experimental `yaml:"experimental"`
|
Experimental Experimental `yaml:"experimental"`
|
||||||
@ -318,6 +272,8 @@ type RawConfig struct {
|
|||||||
ProxyGroup []map[string]any `yaml:"proxy-groups"`
|
ProxyGroup []map[string]any `yaml:"proxy-groups"`
|
||||||
Rule []string `yaml:"rules"`
|
Rule []string `yaml:"rules"`
|
||||||
SubRules map[string][]string `yaml:"sub-rules"`
|
SubRules map[string][]string `yaml:"sub-rules"`
|
||||||
|
RawTLS TLS `yaml:"tls"`
|
||||||
|
Listeners []map[string]any `yaml:"listeners"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type RawGeoXUrl struct {
|
type RawGeoXUrl struct {
|
||||||
@ -361,21 +317,22 @@ func Parse(buf []byte) (*Config, error) {
|
|||||||
func UnmarshalRawConfig(buf []byte) (*RawConfig, error) {
|
func UnmarshalRawConfig(buf []byte) (*RawConfig, error) {
|
||||||
// config with default value
|
// config with default value
|
||||||
rawCfg := &RawConfig{
|
rawCfg := &RawConfig{
|
||||||
AllowLan: false,
|
AllowLan: false,
|
||||||
BindAddress: "*",
|
BindAddress: "*",
|
||||||
IPv6: true,
|
IPv6: true,
|
||||||
Mode: T.Rule,
|
Mode: T.Rule,
|
||||||
GeodataMode: C.GeodataMode,
|
GeodataMode: C.GeodataMode,
|
||||||
GeodataLoader: "memconservative",
|
GeodataLoader: "memconservative",
|
||||||
UnifiedDelay: false,
|
UnifiedDelay: false,
|
||||||
Authentication: []string{},
|
Authentication: []string{},
|
||||||
LogLevel: log.INFO,
|
LogLevel: log.INFO,
|
||||||
Hosts: map[string]string{},
|
Hosts: map[string]string{},
|
||||||
Rule: []string{},
|
Rule: []string{},
|
||||||
Proxy: []map[string]any{},
|
Proxy: []map[string]any{},
|
||||||
ProxyGroup: []map[string]any{},
|
ProxyGroup: []map[string]any{},
|
||||||
TCPConcurrent: false,
|
TCPConcurrent: false,
|
||||||
EnableProcess: false,
|
EnableProcess: false,
|
||||||
|
FindProcessMode: P.FindProcessStrict,
|
||||||
Tun: RawTun{
|
Tun: RawTun{
|
||||||
Enable: false,
|
Enable: false,
|
||||||
Device: "",
|
Device: "",
|
||||||
@ -383,7 +340,19 @@ func UnmarshalRawConfig(buf []byte) (*RawConfig, error) {
|
|||||||
DNSHijack: []string{"0.0.0.0:53"}, // default hijack all dns query
|
DNSHijack: []string{"0.0.0.0:53"}, // default hijack all dns query
|
||||||
AutoRoute: true,
|
AutoRoute: true,
|
||||||
AutoDetectInterface: true,
|
AutoDetectInterface: true,
|
||||||
Inet6Address: []ListenPrefix{ListenPrefix(netip.MustParsePrefix("fdfe:dcba:9876::1/126"))},
|
Inet6Address: []LC.ListenPrefix{LC.ListenPrefix(netip.MustParsePrefix("fdfe:dcba:9876::1/126"))},
|
||||||
|
},
|
||||||
|
TuicServer: RawTuicServer{
|
||||||
|
Enable: false,
|
||||||
|
Token: nil,
|
||||||
|
Certificate: "",
|
||||||
|
PrivateKey: "",
|
||||||
|
Listen: "",
|
||||||
|
CongestionController: "",
|
||||||
|
MaxIdleTime: 15000,
|
||||||
|
AuthenticationTimeout: 1000,
|
||||||
|
ALPN: []string{"h3"},
|
||||||
|
MaxUdpRelayPacketSize: 1500,
|
||||||
},
|
},
|
||||||
EBpf: EBpf{
|
EBpf: EBpf{
|
||||||
RedirectToTun: []string{},
|
RedirectToTun: []string{},
|
||||||
@ -455,6 +424,7 @@ func ParseRawConfig(rawCfg *RawConfig) (*Config, error) {
|
|||||||
config.Experimental = &rawCfg.Experimental
|
config.Experimental = &rawCfg.Experimental
|
||||||
config.Profile = &rawCfg.Profile
|
config.Profile = &rawCfg.Profile
|
||||||
config.IPTables = &rawCfg.IPTables
|
config.IPTables = &rawCfg.IPTables
|
||||||
|
config.TLS = &rawCfg.RawTLS
|
||||||
|
|
||||||
general, err := parseGeneral(rawCfg)
|
general, err := parseGeneral(rawCfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -463,7 +433,6 @@ func ParseRawConfig(rawCfg *RawConfig) (*Config, error) {
|
|||||||
config.General = general
|
config.General = general
|
||||||
|
|
||||||
dialer.DefaultInterface.Store(config.General.Interface)
|
dialer.DefaultInterface.Store(config.General.Interface)
|
||||||
|
|
||||||
proxies, providers, err := parseProxies(rawCfg)
|
proxies, providers, err := parseProxies(rawCfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -471,14 +440,26 @@ func ParseRawConfig(rawCfg *RawConfig) (*Config, error) {
|
|||||||
config.Proxies = proxies
|
config.Proxies = proxies
|
||||||
config.Providers = providers
|
config.Providers = providers
|
||||||
|
|
||||||
subRules, ruleProviders, err := parseSubRules(rawCfg, proxies)
|
listener, err := parseListeners(rawCfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
config.Listeners = listener
|
||||||
|
|
||||||
|
log.Infoln("Geodata Loader mode: %s", geodata.LoaderName())
|
||||||
|
ruleProviders, err := parseRuleProviders(rawCfg)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
config.RuleProviders = ruleProviders
|
||||||
|
|
||||||
|
subRules, err := parseSubRules(rawCfg, proxies)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
config.SubRules = subRules
|
config.SubRules = subRules
|
||||||
config.RuleProviders = ruleProviders
|
|
||||||
|
|
||||||
rules, err := parseRules(rawCfg, proxies, subRules)
|
rules, err := parseRules(rawCfg.Rule, proxies, subRules, "rules")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -496,14 +477,28 @@ func ParseRawConfig(rawCfg *RawConfig) (*Config, error) {
|
|||||||
}
|
}
|
||||||
config.DNS = dnsCfg
|
config.DNS = dnsCfg
|
||||||
|
|
||||||
tunCfg, err := parseTun(rawCfg.Tun, config.General, dnsCfg)
|
err = parseTun(rawCfg.Tun, config.General)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = parseTuicServer(rawCfg.TuicServer, config.General)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
config.Tun = tunCfg
|
|
||||||
|
|
||||||
config.Users = parseAuthentication(rawCfg.Authentication)
|
config.Users = parseAuthentication(rawCfg.Authentication)
|
||||||
|
|
||||||
|
config.Tunnels = rawCfg.Tunnels
|
||||||
|
// verify tunnels
|
||||||
|
for _, t := range config.Tunnels {
|
||||||
|
if len(t.Proxy) > 0 {
|
||||||
|
if _, ok := config.Proxies[t.Proxy]; !ok {
|
||||||
|
return nil, fmt.Errorf("tunnel proxy %s not found", t.Proxy)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
config.Sniffer, err = parseSniffer(rawCfg.Sniffer)
|
config.Sniffer, err = parseSniffer(rawCfg.Sniffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -511,6 +506,7 @@ func ParseRawConfig(rawCfg *RawConfig) (*Config, error) {
|
|||||||
|
|
||||||
elapsedTime := time.Since(startTime) / time.Millisecond // duration in ms
|
elapsedTime := time.Since(startTime) / time.Millisecond // duration in ms
|
||||||
log.Infoln("Initial configuration complete, total time: %dms", elapsedTime) //Segment finished in xxm
|
log.Infoln("Initial configuration complete, total time: %dms", elapsedTime) //Segment finished in xxm
|
||||||
|
|
||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -520,7 +516,6 @@ func parseGeneral(cfg *RawConfig) (*General, error) {
|
|||||||
// checkout externalUI exist
|
// checkout externalUI exist
|
||||||
if externalUI != "" {
|
if externalUI != "" {
|
||||||
externalUI = C.Path.Resolve(externalUI)
|
externalUI = C.Path.Resolve(externalUI)
|
||||||
|
|
||||||
if _, err := os.Stat(externalUI); os.IsNotExist(err) {
|
if _, err := os.Stat(externalUI); os.IsNotExist(err) {
|
||||||
return nil, fmt.Errorf("external-ui: %s not exist", externalUI)
|
return nil, fmt.Errorf("external-ui: %s not exist", externalUI)
|
||||||
}
|
}
|
||||||
@ -528,31 +523,35 @@ func parseGeneral(cfg *RawConfig) (*General, error) {
|
|||||||
cfg.Tun.RedirectToTun = cfg.EBpf.RedirectToTun
|
cfg.Tun.RedirectToTun = cfg.EBpf.RedirectToTun
|
||||||
return &General{
|
return &General{
|
||||||
Inbound: Inbound{
|
Inbound: Inbound{
|
||||||
Port: cfg.Port,
|
Port: cfg.Port,
|
||||||
SocksPort: cfg.SocksPort,
|
SocksPort: cfg.SocksPort,
|
||||||
RedirPort: cfg.RedirPort,
|
RedirPort: cfg.RedirPort,
|
||||||
TProxyPort: cfg.TProxyPort,
|
TProxyPort: cfg.TProxyPort,
|
||||||
MixedPort: cfg.MixedPort,
|
MixedPort: cfg.MixedPort,
|
||||||
AllowLan: cfg.AllowLan,
|
ShadowSocksConfig: cfg.ShadowSocksConfig,
|
||||||
BindAddress: cfg.BindAddress,
|
VmessConfig: cfg.VmessConfig,
|
||||||
InboundTfo: cfg.InboundTfo,
|
AllowLan: cfg.AllowLan,
|
||||||
|
BindAddress: cfg.BindAddress,
|
||||||
|
InboundTfo: cfg.InboundTfo,
|
||||||
},
|
},
|
||||||
Controller: Controller{
|
Controller: Controller{
|
||||||
ExternalController: cfg.ExternalController,
|
ExternalController: cfg.ExternalController,
|
||||||
ExternalUI: cfg.ExternalUI,
|
ExternalUI: cfg.ExternalUI,
|
||||||
Secret: cfg.Secret,
|
Secret: cfg.Secret,
|
||||||
|
ExternalControllerTLS: cfg.ExternalControllerTLS,
|
||||||
},
|
},
|
||||||
UnifiedDelay: cfg.UnifiedDelay,
|
UnifiedDelay: cfg.UnifiedDelay,
|
||||||
Mode: cfg.Mode,
|
Mode: cfg.Mode,
|
||||||
LogLevel: cfg.LogLevel,
|
LogLevel: cfg.LogLevel,
|
||||||
IPv6: cfg.IPv6,
|
IPv6: cfg.IPv6,
|
||||||
Interface: cfg.Interface,
|
Interface: cfg.Interface,
|
||||||
RoutingMark: cfg.RoutingMark,
|
RoutingMark: cfg.RoutingMark,
|
||||||
GeodataMode: cfg.GeodataMode,
|
GeodataMode: cfg.GeodataMode,
|
||||||
GeodataLoader: cfg.GeodataLoader,
|
GeodataLoader: cfg.GeodataLoader,
|
||||||
TCPConcurrent: cfg.TCPConcurrent,
|
TCPConcurrent: cfg.TCPConcurrent,
|
||||||
EnableProcess: cfg.EnableProcess,
|
EnableProcess: cfg.EnableProcess,
|
||||||
EBpf: cfg.EBpf,
|
FindProcessMode: cfg.FindProcessMode,
|
||||||
|
EBpf: cfg.EBpf,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -659,79 +658,62 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[
|
|||||||
return proxies, providersMap, nil
|
return proxies, providersMap, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseSubRules(cfg *RawConfig, proxies map[string]C.Proxy) (subRules *map[string][]C.Rule, ruleProviders map[string]providerTypes.RuleProvider, err error) {
|
func parseListeners(cfg *RawConfig) (listeners map[string]C.InboundListener, err error) {
|
||||||
|
listeners = make(map[string]C.InboundListener)
|
||||||
|
for index, mapping := range cfg.Listeners {
|
||||||
|
listener, err := L.ParseListener(mapping)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("proxy %d: %w", index, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, exist := mapping[listener.Name()]; exist {
|
||||||
|
return nil, fmt.Errorf("listener %s is the duplicate name", listener.Name())
|
||||||
|
}
|
||||||
|
|
||||||
|
listeners[listener.Name()] = listener
|
||||||
|
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseRuleProviders(cfg *RawConfig) (ruleProviders map[string]providerTypes.RuleProvider, err error) {
|
||||||
ruleProviders = map[string]providerTypes.RuleProvider{}
|
ruleProviders = map[string]providerTypes.RuleProvider{}
|
||||||
subRules = &map[string][]C.Rule{}
|
|
||||||
log.Infoln("Geodata Loader mode: %s", geodata.LoaderName())
|
|
||||||
// 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)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
ruleProviders[name] = rp
|
ruleProviders[name] = rp
|
||||||
RP.SetRuleProvider(rp)
|
RP.SetRuleProvider(rp)
|
||||||
}
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseSubRules(cfg *RawConfig, proxies map[string]C.Proxy) (subRules map[string][]C.Rule, err error) {
|
||||||
|
subRules = map[string][]C.Rule{}
|
||||||
for name, rawRules := range cfg.SubRules {
|
for name, rawRules := range cfg.SubRules {
|
||||||
var rules []C.Rule
|
if len(name) == 0 {
|
||||||
for idx, line := range rawRules {
|
return nil, fmt.Errorf("sub-rule name is empty")
|
||||||
rawRule := trimArr(strings.Split(line, ","))
|
|
||||||
var (
|
|
||||||
payload string
|
|
||||||
target string
|
|
||||||
params []string
|
|
||||||
ruleName = strings.ToUpper(rawRule[0])
|
|
||||||
)
|
|
||||||
|
|
||||||
l := len(rawRule)
|
|
||||||
|
|
||||||
if ruleName == "NOT" || ruleName == "OR" || ruleName == "AND" || ruleName == "SUB-RULE" {
|
|
||||||
target = rawRule[l-1]
|
|
||||||
payload = strings.Join(rawRule[1:l-1], ",")
|
|
||||||
} else {
|
|
||||||
if l < 2 {
|
|
||||||
return nil, nil, fmt.Errorf("sub-rules[%d] [%s] error: format invalid", idx, line)
|
|
||||||
}
|
|
||||||
if l < 4 {
|
|
||||||
rawRule = append(rawRule, make([]string, 4-l)...)
|
|
||||||
}
|
|
||||||
if ruleName == "MATCH" {
|
|
||||||
l = 2
|
|
||||||
}
|
|
||||||
if l >= 3 {
|
|
||||||
l = 3
|
|
||||||
payload = rawRule[1]
|
|
||||||
}
|
|
||||||
target = rawRule[l-1]
|
|
||||||
params = rawRule[l:]
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := proxies[target]; !ok && ruleName != "SUB-RULE" {
|
|
||||||
return nil, nil, fmt.Errorf("sub-rules[%d:%s] [%s] error: proxy [%s] not found", idx, name, line, target)
|
|
||||||
}
|
|
||||||
|
|
||||||
params = trimArr(params)
|
|
||||||
parsed, parseErr := R.ParseRule(ruleName, payload, target, params, subRules)
|
|
||||||
if parseErr != nil {
|
|
||||||
return nil, nil, fmt.Errorf("sub-rules[%d] [%s] error: %s", idx, line, parseErr.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
rules = append(rules, parsed)
|
|
||||||
}
|
}
|
||||||
(*subRules)[name] = rules
|
var rules []C.Rule
|
||||||
|
rules, err = parseRules(rawRules, proxies, subRules, fmt.Sprintf("sub-rules[%s]", name))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
subRules[name] = rules
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = verifySubRule(subRules); err != nil {
|
if err = verifySubRule(subRules); err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func verifySubRule(subRules *map[string][]C.Rule) error {
|
func verifySubRule(subRules map[string][]C.Rule) error {
|
||||||
for name := range *subRules {
|
for name := range subRules {
|
||||||
err := verifySubRuleCircularReferences(name, subRules, []string{})
|
err := verifySubRuleCircularReferences(name, subRules, []string{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -740,7 +722,7 @@ func verifySubRule(subRules *map[string][]C.Rule) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func verifySubRuleCircularReferences(n string, subRules *map[string][]C.Rule, arr []string) error {
|
func verifySubRuleCircularReferences(n string, subRules map[string][]C.Rule, arr []string) error {
|
||||||
isInArray := func(v string, array []string) bool {
|
isInArray := func(v string, array []string) bool {
|
||||||
for _, c := range array {
|
for _, c := range array {
|
||||||
if v == c {
|
if v == c {
|
||||||
@ -751,9 +733,9 @@ func verifySubRuleCircularReferences(n string, subRules *map[string][]C.Rule, ar
|
|||||||
}
|
}
|
||||||
|
|
||||||
arr = append(arr, n)
|
arr = append(arr, n)
|
||||||
for i, rule := range (*subRules)[n] {
|
for i, rule := range subRules[n] {
|
||||||
if rule.RuleType() == C.SubRules {
|
if rule.RuleType() == C.SubRules {
|
||||||
if _, ok := (*subRules)[rule.Adapter()]; !ok {
|
if _, ok := subRules[rule.Adapter()]; !ok {
|
||||||
return fmt.Errorf("sub-rule[%d:%s] error: [%s] not found", i, n, rule.Adapter())
|
return fmt.Errorf("sub-rule[%d:%s] error: [%s] not found", i, n, rule.Adapter())
|
||||||
}
|
}
|
||||||
if isInArray(rule.Adapter(), arr) {
|
if isInArray(rule.Adapter(), arr) {
|
||||||
@ -769,9 +751,8 @@ func verifySubRuleCircularReferences(n string, subRules *map[string][]C.Rule, ar
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseRules(cfg *RawConfig, proxies map[string]C.Proxy, subRules *map[string][]C.Rule) ([]C.Rule, error) {
|
func parseRules(rulesConfig []string, proxies map[string]C.Proxy, subRules map[string][]C.Rule, format string) ([]C.Rule, error) {
|
||||||
var rules []C.Rule
|
var rules []C.Rule
|
||||||
rulesConfig := cfg.Rule
|
|
||||||
|
|
||||||
// parse rules
|
// parse rules
|
||||||
for idx, line := range rulesConfig {
|
for idx, line := range rulesConfig {
|
||||||
@ -790,7 +771,7 @@ func parseRules(cfg *RawConfig, proxies map[string]C.Proxy, subRules *map[string
|
|||||||
payload = strings.Join(rule[1:l-1], ",")
|
payload = strings.Join(rule[1:l-1], ",")
|
||||||
} else {
|
} else {
|
||||||
if l < 2 {
|
if l < 2 {
|
||||||
return nil, fmt.Errorf("rules[%d] [%s] error: format invalid", idx, line)
|
return nil, fmt.Errorf("%s[%d] [%s] error: format invalid", format, idx, line)
|
||||||
}
|
}
|
||||||
if l < 4 {
|
if l < 4 {
|
||||||
rule = append(rule, make([]string, 4-l)...)
|
rule = append(rule, make([]string, 4-l)...)
|
||||||
@ -807,16 +788,16 @@ func parseRules(cfg *RawConfig, proxies map[string]C.Proxy, subRules *map[string
|
|||||||
}
|
}
|
||||||
if _, ok := proxies[target]; !ok {
|
if _, ok := proxies[target]; !ok {
|
||||||
if ruleName != "SUB-RULE" {
|
if ruleName != "SUB-RULE" {
|
||||||
return nil, fmt.Errorf("rules[%d] [%s] error: proxy [%s] not found", idx, line, target)
|
return nil, fmt.Errorf("%s[%d] [%s] error: proxy [%s] not found", format, idx, line, target)
|
||||||
} else if _, ok = (*subRules)[target]; !ok {
|
} else if _, ok = subRules[target]; !ok {
|
||||||
return nil, fmt.Errorf("rules[%d] [%s] error: sub-rule [%s] not found", idx, line, target)
|
return nil, fmt.Errorf("%s[%d] [%s] error: sub-rule [%s] not found", format, idx, line, target)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
params = trimArr(params)
|
params = trimArr(params)
|
||||||
parsed, parseErr := R.ParseRule(ruleName, payload, target, params, subRules)
|
parsed, parseErr := R.ParseRule(ruleName, payload, target, params, subRules)
|
||||||
if parseErr != nil {
|
if parseErr != nil {
|
||||||
return nil, fmt.Errorf("rules[%d] [%s] error: %s", idx, line, parseErr.Error())
|
return nil, fmt.Errorf("%s[%d] [%s] error: %s", format, idx, line, parseErr.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
rules = append(rules, parsed)
|
rules = append(rules, parsed)
|
||||||
@ -844,6 +825,7 @@ func parseHosts(cfg *RawConfig) (*trie.DomainTrie[netip.Addr], error) {
|
|||||||
_ = tree.Insert(domain, ip)
|
_ = tree.Insert(domain, ip)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
tree.Optimize()
|
||||||
|
|
||||||
return tree, nil
|
return tree, nil
|
||||||
}
|
}
|
||||||
@ -865,7 +847,7 @@ func hostWithDefaultPort(host string, defPort string) (string, error) {
|
|||||||
return net.JoinHostPort(hostname, port), nil
|
return net.JoinHostPort(hostname, port), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseNameServer(servers []string) ([]dns.NameServer, error) {
|
func parseNameServer(servers []string, preferH3 bool) ([]dns.NameServer, error) {
|
||||||
var nameservers []dns.NameServer
|
var nameservers []dns.NameServer
|
||||||
|
|
||||||
for idx, server := range servers {
|
for idx, server := range servers {
|
||||||
@ -878,7 +860,9 @@ func parseNameServer(servers []string) ([]dns.NameServer, error) {
|
|||||||
return nil, fmt.Errorf("DNS NameServer[%d] format error: %s", idx, err.Error())
|
return nil, fmt.Errorf("DNS NameServer[%d] format error: %s", idx, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
var addr, dnsNetType, proxyAdapter string
|
proxyAdapter := u.Fragment
|
||||||
|
|
||||||
|
var addr, dnsNetType string
|
||||||
params := map[string]string{}
|
params := map[string]string{}
|
||||||
switch u.Scheme {
|
switch u.Scheme {
|
||||||
case "udp":
|
case "udp":
|
||||||
@ -891,7 +875,16 @@ func parseNameServer(servers []string) ([]dns.NameServer, error) {
|
|||||||
addr, err = hostWithDefaultPort(u.Host, "853")
|
addr, err = hostWithDefaultPort(u.Host, "853")
|
||||||
dnsNetType = "tcp-tls" // DNS over TLS
|
dnsNetType = "tcp-tls" // DNS over TLS
|
||||||
case "https":
|
case "https":
|
||||||
clearURL := url.URL{Scheme: "https", Host: u.Host, Path: u.Path}
|
host := u.Host
|
||||||
|
proxyAdapter = ""
|
||||||
|
if _, _, err := net.SplitHostPort(host); err != nil && strings.Contains(err.Error(), "missing port in address") {
|
||||||
|
host = net.JoinHostPort(host, "443")
|
||||||
|
} else {
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
clearURL := url.URL{Scheme: "https", Host: host, Path: u.Path}
|
||||||
addr = clearURL.String()
|
addr = clearURL.String()
|
||||||
dnsNetType = "https" // DNS over HTTPS
|
dnsNetType = "https" // DNS over HTTPS
|
||||||
if len(u.Fragment) != 0 {
|
if len(u.Fragment) != 0 {
|
||||||
@ -930,17 +923,18 @@ func parseNameServer(servers []string) ([]dns.NameServer, error) {
|
|||||||
ProxyAdapter: proxyAdapter,
|
ProxyAdapter: proxyAdapter,
|
||||||
Interface: dialer.DefaultInterface,
|
Interface: dialer.DefaultInterface,
|
||||||
Params: params,
|
Params: params,
|
||||||
|
PreferH3: preferH3,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return nameservers, nil
|
return nameservers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseNameServerPolicy(nsPolicy map[string]string) (map[string]dns.NameServer, error) {
|
func parseNameServerPolicy(nsPolicy map[string]string, preferH3 bool) (map[string]dns.NameServer, error) {
|
||||||
policy := map[string]dns.NameServer{}
|
policy := map[string]dns.NameServer{}
|
||||||
|
|
||||||
for domain, server := range nsPolicy {
|
for domain, server := range nsPolicy {
|
||||||
nameservers, err := parseNameServer([]string{server})
|
nameservers, err := parseNameServer([]string{server}, preferH3)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -1020,26 +1014,26 @@ func parseDNS(rawCfg *RawConfig, hosts *trie.DomainTrie[netip.Addr], rules []C.R
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
if dnsCfg.NameServer, err = parseNameServer(cfg.NameServer); err != nil {
|
if dnsCfg.NameServer, err = parseNameServer(cfg.NameServer, cfg.PreferH3); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if dnsCfg.Fallback, err = parseNameServer(cfg.Fallback); err != nil {
|
if dnsCfg.Fallback, err = parseNameServer(cfg.Fallback, cfg.PreferH3); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if dnsCfg.NameServerPolicy, err = parseNameServerPolicy(cfg.NameServerPolicy); err != nil {
|
if dnsCfg.NameServerPolicy, err = parseNameServerPolicy(cfg.NameServerPolicy, cfg.PreferH3); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if dnsCfg.ProxyServerNameserver, err = parseNameServer(cfg.ProxyServerNameserver); err != nil {
|
if dnsCfg.ProxyServerNameserver, err = parseNameServer(cfg.ProxyServerNameserver, cfg.PreferH3); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(cfg.DefaultNameserver) == 0 {
|
if len(cfg.DefaultNameserver) == 0 {
|
||||||
return nil, errors.New("default nameserver should have at least one nameserver")
|
return nil, errors.New("default nameserver should have at least one nameserver")
|
||||||
}
|
}
|
||||||
if dnsCfg.DefaultNameserver, err = parseNameServer(cfg.DefaultNameserver); err != nil {
|
if dnsCfg.DefaultNameserver, err = parseNameServer(cfg.DefaultNameserver, cfg.PreferH3); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// check default nameserver is pure ip addr
|
// check default nameserver is pure ip addr
|
||||||
@ -1055,35 +1049,38 @@ func parseDNS(rawCfg *RawConfig, hosts *trie.DomainTrie[netip.Addr], rules []C.R
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fakeIPRange, err := netip.ParsePrefix(cfg.FakeIPRange)
|
||||||
|
T.SetFakeIPRange(fakeIPRange)
|
||||||
if cfg.EnhancedMode == C.DNSFakeIP {
|
if cfg.EnhancedMode == C.DNSFakeIP {
|
||||||
ipnet, err := netip.ParsePrefix(cfg.FakeIPRange)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var host *trie.DomainTrie[bool]
|
var host *trie.DomainTrie[struct{}]
|
||||||
// fake ip skip host filter
|
// fake ip skip host filter
|
||||||
if len(cfg.FakeIPFilter) != 0 {
|
if len(cfg.FakeIPFilter) != 0 {
|
||||||
host = trie.New[bool]()
|
host = trie.New[struct{}]()
|
||||||
for _, domain := range cfg.FakeIPFilter {
|
for _, domain := range cfg.FakeIPFilter {
|
||||||
_ = host.Insert(domain, true)
|
_ = host.Insert(domain, struct{}{})
|
||||||
}
|
}
|
||||||
|
host.Optimize()
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(dnsCfg.Fallback) != 0 {
|
if len(dnsCfg.Fallback) != 0 {
|
||||||
if host == nil {
|
if host == nil {
|
||||||
host = trie.New[bool]()
|
host = trie.New[struct{}]()
|
||||||
}
|
}
|
||||||
for _, fb := range dnsCfg.Fallback {
|
for _, fb := range dnsCfg.Fallback {
|
||||||
if net.ParseIP(fb.Addr) != nil {
|
if net.ParseIP(fb.Addr) != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
_ = host.Insert(fb.Addr, true)
|
_ = host.Insert(fb.Addr, struct{}{})
|
||||||
}
|
}
|
||||||
|
host.Optimize()
|
||||||
}
|
}
|
||||||
|
|
||||||
pool, err := fakeip.New(fakeip.Options{
|
pool, err := fakeip.New(fakeip.Options{
|
||||||
IPNet: &ipnet,
|
IPNet: &fakeIPRange,
|
||||||
Size: 1000,
|
Size: 1000,
|
||||||
Host: host,
|
Host: host,
|
||||||
Persistence: rawCfg.Profile.StoreFakeIP,
|
Persistence: rawCfg.Profile.StoreFakeIP,
|
||||||
@ -1126,41 +1123,28 @@ func parseAuthentication(rawRecords []string) []auth.AuthUser {
|
|||||||
return users
|
return users
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseTun(rawTun RawTun, general *General, dnsCfg *DNS) (*Tun, error) {
|
func parseTun(rawTun RawTun, general *General) error {
|
||||||
var dnsHijack []netip.AddrPort
|
tunAddressPrefix := T.FakeIPRange()
|
||||||
|
if !tunAddressPrefix.IsValid() {
|
||||||
for _, d := range rawTun.DNSHijack {
|
|
||||||
if _, after, ok := strings.Cut(d, "://"); ok {
|
|
||||||
d = after
|
|
||||||
}
|
|
||||||
d = strings.Replace(d, "any", "0.0.0.0", 1)
|
|
||||||
addrPort, err := netip.ParseAddrPort(d)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("parse dns-hijack url error: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
dnsHijack = append(dnsHijack, addrPort)
|
|
||||||
}
|
|
||||||
|
|
||||||
var tunAddressPrefix netip.Prefix
|
|
||||||
if dnsCfg.FakeIPRange != nil {
|
|
||||||
tunAddressPrefix = *dnsCfg.FakeIPRange.IPNet()
|
|
||||||
} else {
|
|
||||||
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)
|
||||||
|
|
||||||
return &Tun{
|
if !general.IPv6 || !verifyIP6() {
|
||||||
|
rawTun.Inet6Address = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
general.Tun = LC.Tun{
|
||||||
Enable: rawTun.Enable,
|
Enable: rawTun.Enable,
|
||||||
Device: rawTun.Device,
|
Device: rawTun.Device,
|
||||||
Stack: rawTun.Stack,
|
Stack: rawTun.Stack,
|
||||||
DNSHijack: dnsHijack,
|
DNSHijack: rawTun.DNSHijack,
|
||||||
AutoRoute: rawTun.AutoRoute,
|
AutoRoute: rawTun.AutoRoute,
|
||||||
AutoDetectInterface: rawTun.AutoDetectInterface,
|
AutoDetectInterface: rawTun.AutoDetectInterface,
|
||||||
RedirectToTun: rawTun.RedirectToTun,
|
RedirectToTun: rawTun.RedirectToTun,
|
||||||
|
|
||||||
MTU: rawTun.MTU,
|
MTU: rawTun.MTU,
|
||||||
Inet4Address: []ListenPrefix{ListenPrefix(tunAddressPrefix)},
|
Inet4Address: []LC.ListenPrefix{LC.ListenPrefix(tunAddressPrefix)},
|
||||||
Inet6Address: rawTun.Inet6Address,
|
Inet6Address: rawTun.Inet6Address,
|
||||||
StrictRoute: rawTun.StrictRoute,
|
StrictRoute: rawTun.StrictRoute,
|
||||||
Inet4RouteAddress: rawTun.Inet4RouteAddress,
|
Inet4RouteAddress: rawTun.Inet4RouteAddress,
|
||||||
@ -1174,7 +1158,25 @@ func parseTun(rawTun RawTun, general *General, dnsCfg *DNS) (*Tun, error) {
|
|||||||
ExcludePackage: rawTun.ExcludePackage,
|
ExcludePackage: rawTun.ExcludePackage,
|
||||||
EndpointIndependentNat: rawTun.EndpointIndependentNat,
|
EndpointIndependentNat: rawTun.EndpointIndependentNat,
|
||||||
UDPTimeout: rawTun.UDPTimeout,
|
UDPTimeout: rawTun.UDPTimeout,
|
||||||
}, nil
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseTuicServer(rawTuic RawTuicServer, general *General) error {
|
||||||
|
general.TuicServer = LC.TuicServer{
|
||||||
|
Enable: rawTuic.Enable,
|
||||||
|
Listen: rawTuic.Listen,
|
||||||
|
Token: rawTuic.Token,
|
||||||
|
Certificate: rawTuic.Certificate,
|
||||||
|
PrivateKey: rawTuic.PrivateKey,
|
||||||
|
CongestionController: rawTuic.CongestionController,
|
||||||
|
MaxIdleTime: rawTuic.MaxIdleTime,
|
||||||
|
AuthenticationTimeout: rawTuic.AuthenticationTimeout,
|
||||||
|
ALPN: rawTuic.ALPN,
|
||||||
|
MaxUdpRelayPacketSize: rawTuic.MaxUdpRelayPacketSize,
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseSniffer(snifferRaw RawSniffer) (*Sniffer, error) {
|
func parseSniffer(snifferRaw RawSniffer) (*Sniffer, error) {
|
||||||
@ -1232,21 +1234,23 @@ func parseSniffer(snifferRaw RawSniffer) (*Sniffer, error) {
|
|||||||
for st := range loadSniffer {
|
for st := range loadSniffer {
|
||||||
sniffer.Sniffers = append(sniffer.Sniffers, st)
|
sniffer.Sniffers = append(sniffer.Sniffers, st)
|
||||||
}
|
}
|
||||||
sniffer.ForceDomain = trie.New[bool]()
|
sniffer.ForceDomain = trie.New[struct{}]()
|
||||||
for _, domain := range snifferRaw.ForceDomain {
|
for _, domain := range snifferRaw.ForceDomain {
|
||||||
err := sniffer.ForceDomain.Insert(domain, true)
|
err := sniffer.ForceDomain.Insert(domain, struct{}{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error domian[%s] in force-domain, error:%v", domain, err)
|
return nil, fmt.Errorf("error domian[%s] in force-domain, error:%v", domain, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
sniffer.ForceDomain.Optimize()
|
||||||
|
|
||||||
sniffer.SkipDomain = trie.New[bool]()
|
sniffer.SkipDomain = trie.New[struct{}]()
|
||||||
for _, domain := range snifferRaw.SkipDomain {
|
for _, domain := range snifferRaw.SkipDomain {
|
||||||
err := sniffer.SkipDomain.Insert(domain, true)
|
err := sniffer.SkipDomain.Insert(domain, struct{}{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error domian[%s] in force-domain, error:%v", domain, err)
|
return nil, fmt.Errorf("error domian[%s] in force-domain, error:%v", domain, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
sniffer.SkipDomain.Optimize()
|
||||||
|
|
||||||
return sniffer, nil
|
return sniffer, nil
|
||||||
}
|
}
|
||||||
|
@ -3,92 +3,12 @@ package config
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Dreamacro/clash/component/geodata"
|
"github.com/Dreamacro/clash/component/geodata"
|
||||||
"github.com/Dreamacro/clash/component/mmdb"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/log"
|
"github.com/Dreamacro/clash/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func downloadMMDB(path string) (err error) {
|
|
||||||
resp, err := http.Get(C.MmdbUrl)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o644)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
_, err = io.Copy(f, resp.Body)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func downloadGeoIP(path string) (err error) {
|
|
||||||
resp, err := http.Get(C.GeoIpUrl)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o644)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
_, err = io.Copy(f, resp.Body)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func initGeoIP() error {
|
|
||||||
if C.GeodataMode {
|
|
||||||
if _, err := os.Stat(C.Path.GeoIP()); os.IsNotExist(err) {
|
|
||||||
log.Infoln("Can't find GeoIP.dat, start download")
|
|
||||||
if err := downloadGeoIP(C.Path.GeoIP()); err != nil {
|
|
||||||
return fmt.Errorf("can't download GeoIP.dat: %s", err.Error())
|
|
||||||
}
|
|
||||||
log.Infoln("Download GeoIP.dat finish")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := geodata.Verify(C.GeoipName); err != nil {
|
|
||||||
log.Warnln("GeoIP.dat invalid, remove and download: %s", err)
|
|
||||||
if err := os.Remove(C.Path.GeoIP()); err != nil {
|
|
||||||
return fmt.Errorf("can't remove invalid GeoIP.dat: %s", err.Error())
|
|
||||||
}
|
|
||||||
if err := downloadGeoIP(C.Path.GeoIP()); err != nil {
|
|
||||||
return fmt.Errorf("can't download GeoIP.dat: %s", err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := os.Stat(C.Path.MMDB()); os.IsNotExist(err) {
|
|
||||||
log.Infoln("Can't find MMDB, start download")
|
|
||||||
if err := downloadMMDB(C.Path.MMDB()); err != nil {
|
|
||||||
return fmt.Errorf("can't download MMDB: %s", err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !mmdb.Verify() {
|
|
||||||
log.Warnln("MMDB invalid, remove and download")
|
|
||||||
if err := os.Remove(C.Path.MMDB()); err != nil {
|
|
||||||
return fmt.Errorf("can't remove invalid MMDB: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := downloadMMDB(C.Path.MMDB()); err != nil {
|
|
||||||
return fmt.Errorf("can't download MMDB: %s", err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Init prepare necessary files
|
// Init prepare necessary files
|
||||||
func Init(dir string) error {
|
func Init(dir string) error {
|
||||||
// initial homedir
|
// initial homedir
|
||||||
@ -122,7 +42,7 @@ func Init(dir string) error {
|
|||||||
C.GeoSiteUrl = rawCfg.GeoXUrl.GeoSite
|
C.GeoSiteUrl = rawCfg.GeoXUrl.GeoSite
|
||||||
C.MmdbUrl = rawCfg.GeoXUrl.Mmdb
|
C.MmdbUrl = rawCfg.GeoXUrl.Mmdb
|
||||||
// initial GeoIP
|
// initial GeoIP
|
||||||
if err := initGeoIP(); err != nil {
|
if err := geodata.InitGeoIP(); err != nil {
|
||||||
return fmt.Errorf("can't initial GeoIP: %w", err)
|
return fmt.Errorf("can't initial GeoIP: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapter/outboundgroup"
|
"github.com/Dreamacro/clash/adapter/outboundgroup"
|
||||||
@ -146,3 +147,25 @@ func proxyGroupsDagSort(groupsConfig []map[string]any) error {
|
|||||||
}
|
}
|
||||||
return fmt.Errorf("loop is detected in ProxyGroup, please check following ProxyGroups: %v", loopElements)
|
return fmt.Errorf("loop is detected in ProxyGroup, please check following ProxyGroups: %v", loopElements)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func verifyIP6() bool {
|
||||||
|
addrs, err := net.InterfaceAddrs()
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, addr := range addrs {
|
||||||
|
ipNet, isIpNet := addr.(*net.IPNet)
|
||||||
|
if isIpNet && !ipNet.IP.IsLoopback() {
|
||||||
|
if ipNet.IP.To16() != nil {
|
||||||
|
s := ipNet.IP.String()
|
||||||
|
for i := 0; i < len(s); i++ {
|
||||||
|
switch s[i] {
|
||||||
|
case ':':
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"net/netip"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
@ -31,6 +32,8 @@ const (
|
|||||||
Vless
|
Vless
|
||||||
Trojan
|
Trojan
|
||||||
Hysteria
|
Hysteria
|
||||||
|
WireGuard
|
||||||
|
Tuic
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -79,13 +82,20 @@ type PacketConn interface {
|
|||||||
// WriteWithMetadata(p []byte, metadata *Metadata) (n int, err error)
|
// WriteWithMetadata(p []byte, metadata *Metadata) (n int, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Dialer interface {
|
||||||
|
DialContext(ctx context.Context, network, address string) (net.Conn, error)
|
||||||
|
ListenPacket(ctx context.Context, network, address string, rAddrPort netip.AddrPort) (net.PacketConn, error)
|
||||||
|
}
|
||||||
|
|
||||||
type ProxyAdapter interface {
|
type ProxyAdapter interface {
|
||||||
Name() string
|
Name() string
|
||||||
Type() AdapterType
|
Type() AdapterType
|
||||||
Addr() string
|
Addr() string
|
||||||
SupportUDP() bool
|
SupportUDP() bool
|
||||||
|
SupportTFO() bool
|
||||||
MarshalJSON() ([]byte, error)
|
MarshalJSON() ([]byte, error)
|
||||||
|
|
||||||
|
// Deprecated: use DialContextWithDialer and ListenPacketWithDialer instead.
|
||||||
// StreamConn wraps a protocol around net.Conn with Metadata.
|
// StreamConn wraps a protocol around net.Conn with Metadata.
|
||||||
//
|
//
|
||||||
// Examples:
|
// Examples:
|
||||||
@ -103,7 +113,10 @@ type ProxyAdapter interface {
|
|||||||
|
|
||||||
// SupportUOT return UDP over TCP support
|
// SupportUOT return UDP over TCP support
|
||||||
SupportUOT() bool
|
SupportUOT() bool
|
||||||
ListenPacketOnStreamConn(c net.Conn, metadata *Metadata) (PacketConn, error)
|
|
||||||
|
SupportWithDialer() bool
|
||||||
|
DialContextWithDialer(ctx context.Context, dialer Dialer, metadata *Metadata) (Conn, error)
|
||||||
|
ListenPacketWithDialer(ctx context.Context, dialer Dialer, metadata *Metadata) (PacketConn, error)
|
||||||
|
|
||||||
// Unwrap extracts the proxy from a proxy-group. It returns nil when nothing to extract.
|
// Unwrap extracts the proxy from a proxy-group. It returns nil when nothing to extract.
|
||||||
Unwrap(metadata *Metadata, touch bool) Proxy
|
Unwrap(metadata *Metadata, touch bool) Proxy
|
||||||
@ -165,6 +178,10 @@ func (at AdapterType) String() string {
|
|||||||
return "Trojan"
|
return "Trojan"
|
||||||
case Hysteria:
|
case Hysteria:
|
||||||
return "Hysteria"
|
return "Hysteria"
|
||||||
|
case WireGuard:
|
||||||
|
return "WireGuard"
|
||||||
|
case Tuic:
|
||||||
|
return "Tuic"
|
||||||
|
|
||||||
case Relay:
|
case Relay:
|
||||||
return "Relay"
|
return "Relay"
|
||||||
@ -199,3 +216,13 @@ type UDPPacket interface {
|
|||||||
// LocalAddr returns the source IP/Port of packet
|
// LocalAddr returns the source IP/Port of packet
|
||||||
LocalAddr() net.Addr
|
LocalAddr() net.Addr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UDPPacketInAddr interface {
|
||||||
|
InAddr() net.Addr
|
||||||
|
}
|
||||||
|
|
||||||
|
// PacketAdapter is a UDP Packet adapter for socks/redir/tun
|
||||||
|
type PacketAdapter interface {
|
||||||
|
UDPPacket
|
||||||
|
Metadata() *Metadata
|
||||||
|
}
|
||||||
|
@ -114,3 +114,14 @@ func NewDNSPrefer(prefer string) DNSPrefer {
|
|||||||
return DualStack
|
return DualStack
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type HTTPVersion string
|
||||||
|
|
||||||
|
const (
|
||||||
|
// HTTPVersion11 is HTTP/1.1.
|
||||||
|
HTTPVersion11 HTTPVersion = "http/1.1"
|
||||||
|
// HTTPVersion2 is HTTP/2.
|
||||||
|
HTTPVersion2 HTTPVersion = "h2"
|
||||||
|
// HTTPVersion3 is HTTP/3.
|
||||||
|
HTTPVersion3 HTTPVersion = "h3"
|
||||||
|
)
|
@ -1,7 +1,29 @@
|
|||||||
package constant
|
package constant
|
||||||
|
|
||||||
|
import "net"
|
||||||
|
|
||||||
type Listener interface {
|
type Listener interface {
|
||||||
RawAddress() string
|
RawAddress() string
|
||||||
Address() string
|
Address() string
|
||||||
Close() error
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MultiAddrListener interface {
|
||||||
|
Close() error
|
||||||
|
Config() string
|
||||||
|
AddrList() (addrList []net.Addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
type InboundListener interface {
|
||||||
|
Name() string
|
||||||
|
Listen(tcpIn chan<- ConnContext, udpIn chan<- PacketAdapter) error
|
||||||
|
Close() error
|
||||||
|
Address() string
|
||||||
|
RawAddress() string
|
||||||
|
Config() InboundConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
type InboundConfig interface {
|
||||||
|
Name() string
|
||||||
|
Equal(config InboundConfig) bool
|
||||||
|
}
|
||||||
|
@ -6,14 +6,12 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/Dreamacro/clash/transport/socks5"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Socks addr type
|
// Socks addr type
|
||||||
const (
|
const (
|
||||||
AtypIPv4 = 1
|
|
||||||
AtypDomainName = 3
|
|
||||||
AtypIPv6 = 4
|
|
||||||
|
|
||||||
TCP NetWork = iota
|
TCP NetWork = iota
|
||||||
UDP
|
UDP
|
||||||
ALLNet
|
ALLNet
|
||||||
@ -22,9 +20,13 @@ const (
|
|||||||
HTTPS
|
HTTPS
|
||||||
SOCKS4
|
SOCKS4
|
||||||
SOCKS5
|
SOCKS5
|
||||||
|
SHADOWSOCKS
|
||||||
|
VMESS
|
||||||
REDIR
|
REDIR
|
||||||
TPROXY
|
TPROXY
|
||||||
|
TUNNEL
|
||||||
TUN
|
TUN
|
||||||
|
TUIC
|
||||||
INNER
|
INNER
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -55,12 +57,20 @@ func (t Type) String() string {
|
|||||||
return "Socks4"
|
return "Socks4"
|
||||||
case SOCKS5:
|
case SOCKS5:
|
||||||
return "Socks5"
|
return "Socks5"
|
||||||
|
case SHADOWSOCKS:
|
||||||
|
return "ShadowSocks"
|
||||||
|
case VMESS:
|
||||||
|
return "Vmess"
|
||||||
case REDIR:
|
case REDIR:
|
||||||
return "Redir"
|
return "Redir"
|
||||||
case TPROXY:
|
case TPROXY:
|
||||||
return "TProxy"
|
return "TProxy"
|
||||||
|
case TUNNEL:
|
||||||
|
return "Tunnel"
|
||||||
case TUN:
|
case TUN:
|
||||||
return "Tun"
|
return "Tun"
|
||||||
|
case TUIC:
|
||||||
|
return "Tuic"
|
||||||
case INNER:
|
case INNER:
|
||||||
return "Inner"
|
return "Inner"
|
||||||
default:
|
default:
|
||||||
@ -79,12 +89,20 @@ func ParseType(t string) (*Type, error) {
|
|||||||
res = SOCKS4
|
res = SOCKS4
|
||||||
case "SOCKS5":
|
case "SOCKS5":
|
||||||
res = SOCKS5
|
res = SOCKS5
|
||||||
|
case "SHADOWSOCKS":
|
||||||
|
res = SHADOWSOCKS
|
||||||
|
case "VMESS":
|
||||||
|
res = VMESS
|
||||||
case "REDIR":
|
case "REDIR":
|
||||||
res = REDIR
|
res = REDIR
|
||||||
case "TPROXY":
|
case "TPROXY":
|
||||||
res = TPROXY
|
res = TPROXY
|
||||||
|
case "TUNNEL":
|
||||||
|
res = TUNNEL
|
||||||
case "TUN":
|
case "TUN":
|
||||||
res = TUN
|
res = TUN
|
||||||
|
case "TUIC":
|
||||||
|
res = TUIC
|
||||||
case "INNER":
|
case "INNER":
|
||||||
res = INNER
|
res = INNER
|
||||||
default:
|
default:
|
||||||
@ -99,19 +117,23 @@ func (t Type) MarshalJSON() ([]byte, error) {
|
|||||||
|
|
||||||
// Metadata is used to store connection address
|
// Metadata is used to store connection address
|
||||||
type Metadata struct {
|
type Metadata struct {
|
||||||
NetWork NetWork `json:"network"`
|
NetWork NetWork `json:"network"`
|
||||||
Type Type `json:"type"`
|
Type Type `json:"type"`
|
||||||
SrcIP netip.Addr `json:"sourceIP"`
|
SrcIP netip.Addr `json:"sourceIP"`
|
||||||
DstIP netip.Addr `json:"destinationIP"`
|
DstIP netip.Addr `json:"destinationIP"`
|
||||||
SrcPort string `json:"sourcePort"`
|
SrcPort string `json:"sourcePort"`
|
||||||
DstPort string `json:"destinationPort"`
|
DstPort string `json:"destinationPort"`
|
||||||
AddrType int `json:"-"`
|
InIP netip.Addr `json:"inboundIP"`
|
||||||
Host string `json:"host"`
|
InPort string `json:"inboundPort"`
|
||||||
DNSMode DNSMode `json:"dnsMode"`
|
InName string `json:"inboundName"`
|
||||||
Uid *int32 `json:"uid"`
|
Host string `json:"host"`
|
||||||
Process string `json:"process"`
|
DNSMode DNSMode `json:"dnsMode"`
|
||||||
ProcessPath string `json:"processPath"`
|
Uid *uint32 `json:"uid"`
|
||||||
RemoteDst string `json:"remoteDestination"`
|
Process string `json:"process"`
|
||||||
|
ProcessPath string `json:"processPath"`
|
||||||
|
SpecialProxy string `json:"specialProxy"`
|
||||||
|
SpecialRules string `json:"specialRules"`
|
||||||
|
RemoteDst string `json:"remoteDestination"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Metadata) RemoteAddress() string {
|
func (m *Metadata) RemoteAddress() string {
|
||||||
@ -138,6 +160,17 @@ func (m *Metadata) SourceDetail() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Metadata) AddrType() int {
|
||||||
|
switch true {
|
||||||
|
case m.Host != "" || !m.DstIP.IsValid():
|
||||||
|
return socks5.AtypDomainName
|
||||||
|
case m.DstIP.Is4():
|
||||||
|
return socks5.AtypIPv4
|
||||||
|
default:
|
||||||
|
return socks5.AtypIPv6
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Metadata) Resolved() bool {
|
func (m *Metadata) Resolved() bool {
|
||||||
return m.DstIP.IsValid()
|
return m.DstIP.IsValid()
|
||||||
}
|
}
|
||||||
@ -148,11 +181,6 @@ func (m *Metadata) Pure() *Metadata {
|
|||||||
if (m.DNSMode == DNSMapping || m.DNSMode == DNSHosts) && m.DstIP.IsValid() {
|
if (m.DNSMode == DNSMapping || m.DNSMode == DNSHosts) && m.DstIP.IsValid() {
|
||||||
copyM := *m
|
copyM := *m
|
||||||
copyM.Host = ""
|
copyM.Host = ""
|
||||||
if copyM.DstIP.Is4() {
|
|
||||||
copyM.AddrType = AtypIPv4
|
|
||||||
} else {
|
|
||||||
copyM.AddrType = AtypIPv6
|
|
||||||
}
|
|
||||||
return ©M
|
return ©M
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
package mime
|
|
||||||
|
|
||||||
import (
|
|
||||||
"mime"
|
|
||||||
)
|
|
||||||
|
|
||||||
var consensusMimes = map[string]string{
|
|
||||||
// rfc4329: text/javascript is obsolete, so we need to overwrite mime's builtin
|
|
||||||
".js": "application/javascript; charset=utf-8",
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
for ext, typ := range consensusMimes {
|
|
||||||
mime.AddExtensionType(ext, typ)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +1,7 @@
|
|||||||
package provider
|
package provider
|
||||||
|
|
||||||
import (
|
import (
|
||||||
C "github.com/Dreamacro/clash/constant"
|
"github.com/Dreamacro/clash/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Vehicle Type
|
// Vehicle Type
|
||||||
@ -65,7 +65,9 @@ type Provider interface {
|
|||||||
// ProxyProvider interface
|
// ProxyProvider interface
|
||||||
type ProxyProvider interface {
|
type ProxyProvider interface {
|
||||||
Provider
|
Provider
|
||||||
Proxies() []C.Proxy
|
Proxies() []constant.Proxy
|
||||||
|
// Touch is used to inform the provider that the proxy is actually being used while getting the list of proxies.
|
||||||
|
// Commonly used in DialContext and DialPacketConn
|
||||||
Touch()
|
Touch()
|
||||||
HealthCheck()
|
HealthCheck()
|
||||||
Version() uint32
|
Version() uint32
|
||||||
@ -98,7 +100,7 @@ func (rt RuleType) String() string {
|
|||||||
type RuleProvider interface {
|
type RuleProvider interface {
|
||||||
Provider
|
Provider
|
||||||
Behavior() RuleType
|
Behavior() RuleType
|
||||||
Match(*C.Metadata) bool
|
Match(*constant.Metadata) bool
|
||||||
ShouldResolveIP() bool
|
ShouldResolveIP() bool
|
||||||
AsRule(adaptor string) C.Rule
|
AsRule(adaptor string) constant.Rule
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ const (
|
|||||||
SrcIPSuffix
|
SrcIPSuffix
|
||||||
SrcPort
|
SrcPort
|
||||||
DstPort
|
DstPort
|
||||||
|
InPort
|
||||||
Process
|
Process
|
||||||
ProcessPath
|
ProcessPath
|
||||||
RuleSet
|
RuleSet
|
||||||
@ -52,6 +53,8 @@ func (rt RuleType) String() string {
|
|||||||
return "SrcPort"
|
return "SrcPort"
|
||||||
case DstPort:
|
case DstPort:
|
||||||
return "DstPort"
|
return "DstPort"
|
||||||
|
case InPort:
|
||||||
|
return "InPort"
|
||||||
case Process:
|
case Process:
|
||||||
return "Process"
|
return "Process"
|
||||||
case ProcessPath:
|
case ProcessPath:
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package context
|
package context
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
"github.com/gofrs/uuid"
|
"github.com/gofrs/uuid"
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
)
|
)
|
||||||
@ -12,14 +14,18 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type DNSContext struct {
|
type DNSContext struct {
|
||||||
|
context.Context
|
||||||
|
|
||||||
id uuid.UUID
|
id uuid.UUID
|
||||||
msg *dns.Msg
|
msg *dns.Msg
|
||||||
tp string
|
tp string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDNSContext(msg *dns.Msg) *DNSContext {
|
func NewDNSContext(ctx context.Context, msg *dns.Msg) *DNSContext {
|
||||||
id, _ := uuid.NewV4()
|
id, _ := uuid.NewV4()
|
||||||
return &DNSContext{
|
return &DNSContext{
|
||||||
|
Context: ctx,
|
||||||
|
|
||||||
id: id,
|
id: id,
|
||||||
msg: msg,
|
msg: msg,
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
tlsC "github.com/Dreamacro/clash/component/tls"
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
||||||
"go.uber.org/atomic"
|
"go.uber.org/atomic"
|
||||||
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"strings"
|
"strings"
|
||||||
@ -34,15 +35,19 @@ func (c *client) ExchangeContext(ctx context.Context, m *D.Msg) (*D.Msg, error)
|
|||||||
ip netip.Addr
|
ip netip.Addr
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
if ip, err = netip.ParseAddr(c.host); err != nil {
|
if c.r == nil {
|
||||||
if c.r == nil {
|
// a default ip dns
|
||||||
|
if ip, err = netip.ParseAddr(c.host); err != nil {
|
||||||
return nil, fmt.Errorf("dns %s not a valid ip", c.host)
|
return nil, fmt.Errorf("dns %s not a valid ip", c.host)
|
||||||
} else {
|
|
||||||
if ip, err = resolver.ResolveIPWithResolver(c.host, c.r); err != nil {
|
|
||||||
return nil, fmt.Errorf("use default dns resolve failed: %w", err)
|
|
||||||
}
|
|
||||||
c.host = ip.String()
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
ips, err := resolver.LookupIPWithResolver(ctx, c.host, c.r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("use default dns resolve failed: %w", err)
|
||||||
|
} else if len(ips) == 0 {
|
||||||
|
return nil, fmt.Errorf("%w: %s", resolver.ErrIPNotFound, c.host)
|
||||||
|
}
|
||||||
|
ip = ips[rand.Intn(len(ips))]
|
||||||
}
|
}
|
||||||
|
|
||||||
network := "udp"
|
network := "udp"
|
||||||
@ -55,13 +60,7 @@ func (c *client) ExchangeContext(ctx context.Context, m *D.Msg) (*D.Msg, error)
|
|||||||
options = append(options, dialer.WithInterface(c.iface.Load()))
|
options = append(options, dialer.WithInterface(c.iface.Load()))
|
||||||
}
|
}
|
||||||
|
|
||||||
var conn net.Conn
|
conn, err := getDialHandler(c.r, c.proxyAdapter, options...)(ctx, network, net.JoinHostPort(ip.String(), c.port))
|
||||||
if c.proxyAdapter != "" {
|
|
||||||
conn, err = dialContextExtra(ctx, c.proxyAdapter, network, ip, c.port, options...)
|
|
||||||
} else {
|
|
||||||
conn, err = dialer.DialContext(ctx, network, net.JoinHostPort(ip.String(), c.port), options...)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
19
dns/dhcp.go
19
dns/dhcp.go
@ -30,7 +30,7 @@ type dhcpClient struct {
|
|||||||
|
|
||||||
ifaceAddr *netip.Prefix
|
ifaceAddr *netip.Prefix
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
resolver *Resolver
|
clients []dnsClient
|
||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,15 +42,15 @@ func (d *dhcpClient) Exchange(m *D.Msg) (msg *D.Msg, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *dhcpClient) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) {
|
func (d *dhcpClient) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) {
|
||||||
res, err := d.resolve(ctx)
|
clients, err := d.resolve(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.ExchangeContext(ctx, m)
|
return batchExchange(ctx, clients, m)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *dhcpClient) resolve(ctx context.Context) (*Resolver, error) {
|
func (d *dhcpClient) resolve(ctx context.Context) ([]dnsClient, error) {
|
||||||
d.lock.Lock()
|
d.lock.Lock()
|
||||||
|
|
||||||
invalidated, err := d.invalidate()
|
invalidated, err := d.invalidate()
|
||||||
@ -65,8 +65,9 @@ func (d *dhcpClient) resolve(ctx context.Context) (*Resolver, error) {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), DHCPTimeout)
|
ctx, cancel := context.WithTimeout(context.Background(), DHCPTimeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
var res *Resolver
|
var res []dnsClient
|
||||||
dns, err := dhcp.ResolveDNSFromDHCP(ctx, d.ifaceName)
|
dns, err := dhcp.ResolveDNSFromDHCP(ctx, d.ifaceName)
|
||||||
|
// dns never empty if err is nil
|
||||||
if err == nil {
|
if err == nil {
|
||||||
nameserver := make([]NameServer, 0, len(dns))
|
nameserver := make([]NameServer, 0, len(dns))
|
||||||
for _, item := range dns {
|
for _, item := range dns {
|
||||||
@ -76,9 +77,7 @@ func (d *dhcpClient) resolve(ctx context.Context) (*Resolver, error) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
res = NewResolver(Config{
|
res = transform(nameserver, nil)
|
||||||
Main: nameserver,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
d.lock.Lock()
|
d.lock.Lock()
|
||||||
@ -87,7 +86,7 @@ func (d *dhcpClient) resolve(ctx context.Context) (*Resolver, error) {
|
|||||||
close(done)
|
close(done)
|
||||||
|
|
||||||
d.done = nil
|
d.done = nil
|
||||||
d.resolver = res
|
d.clients = res
|
||||||
d.err = err
|
d.err = err
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
@ -97,7 +96,7 @@ func (d *dhcpClient) resolve(ctx context.Context) (*Resolver, error) {
|
|||||||
for {
|
for {
|
||||||
d.lock.Lock()
|
d.lock.Lock()
|
||||||
|
|
||||||
res, err, done := d.resolver, d.err, d.done
|
res, err, done := d.clients, d.err, d.done
|
||||||
|
|
||||||
d.lock.Unlock()
|
d.lock.Unlock()
|
||||||
|
|
||||||
|
786
dns/doh.go
786
dns/doh.go
@ -1,164 +1,706 @@
|
|||||||
package dns
|
package dns
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
|
||||||
"github.com/Dreamacro/clash/component/resolver"
|
|
||||||
tlsC "github.com/Dreamacro/clash/component/tls"
|
|
||||||
"github.com/lucas-clemente/quic-go"
|
|
||||||
"github.com/lucas-clemente/quic-go/http3"
|
|
||||||
D "github.com/miekg/dns"
|
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
"github.com/Dreamacro/clash/log"
|
||||||
|
"github.com/metacubex/quic-go"
|
||||||
|
"github.com/metacubex/quic-go/http3"
|
||||||
|
D "github.com/miekg/dns"
|
||||||
|
"golang.org/x/net/http2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Values to configure HTTP and HTTP/2 transport.
|
||||||
const (
|
const (
|
||||||
// dotMimeType is the DoH mimetype that should be used.
|
// transportDefaultReadIdleTimeout is the default timeout for pinging
|
||||||
dotMimeType = "application/dns-message"
|
// idle connections in HTTP/2 transport.
|
||||||
|
transportDefaultReadIdleTimeout = 30 * time.Second
|
||||||
|
|
||||||
|
// transportDefaultIdleConnTimeout is the default timeout for idle
|
||||||
|
// connections in HTTP transport.
|
||||||
|
transportDefaultIdleConnTimeout = 5 * time.Minute
|
||||||
|
|
||||||
|
// dohMaxConnsPerHost controls the maximum number of connections for
|
||||||
|
// each host.
|
||||||
|
dohMaxConnsPerHost = 1
|
||||||
|
dialTimeout = 10 * time.Second
|
||||||
|
|
||||||
|
// dohMaxIdleConns controls the maximum number of connections being idle
|
||||||
|
// at the same time.
|
||||||
|
dohMaxIdleConns = 1
|
||||||
|
maxElapsedTime = time.Second * 30
|
||||||
)
|
)
|
||||||
|
|
||||||
type dohClient struct {
|
var DefaultHTTPVersions = []C.HTTPVersion{C.HTTPVersion11, C.HTTPVersion2}
|
||||||
url string
|
|
||||||
transport http.RoundTripper
|
// dnsOverHTTPS is a struct that implements the Upstream interface for the
|
||||||
|
// DNS-over-HTTPS protocol.
|
||||||
|
type dnsOverHTTPS struct {
|
||||||
|
// The Client's Transport typically has internal state (cached TCP
|
||||||
|
// connections), so Clients should be reused instead of created as
|
||||||
|
// needed. Clients are safe for concurrent use by multiple goroutines.
|
||||||
|
client *http.Client
|
||||||
|
clientMu sync.Mutex
|
||||||
|
|
||||||
|
// quicConfig is the QUIC configuration that is used if HTTP/3 is enabled
|
||||||
|
// for this upstream.
|
||||||
|
quicConfig *quic.Config
|
||||||
|
quicConfigGuard sync.Mutex
|
||||||
|
url *url.URL
|
||||||
|
r *Resolver
|
||||||
|
httpVersions []C.HTTPVersion
|
||||||
|
proxyAdapter string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dc *dohClient) Exchange(m *D.Msg) (msg *D.Msg, err error) {
|
// type check
|
||||||
return dc.ExchangeContext(context.Background(), m)
|
var _ dnsClient = (*dnsOverHTTPS)(nil)
|
||||||
|
|
||||||
|
// newDoH returns the DNS-over-HTTPS Upstream.
|
||||||
|
func newDoHClient(urlString string, r *Resolver, preferH3 bool, params map[string]string, proxyAdapter string) dnsClient {
|
||||||
|
u, _ := url.Parse(urlString)
|
||||||
|
httpVersions := DefaultHTTPVersions
|
||||||
|
if preferH3 {
|
||||||
|
httpVersions = append(httpVersions, C.HTTPVersion3)
|
||||||
|
}
|
||||||
|
|
||||||
|
if params["h3"] == "true" {
|
||||||
|
httpVersions = []C.HTTPVersion{C.HTTPVersion3}
|
||||||
|
}
|
||||||
|
|
||||||
|
doh := &dnsOverHTTPS{
|
||||||
|
url: u,
|
||||||
|
r: r,
|
||||||
|
proxyAdapter: proxyAdapter,
|
||||||
|
quicConfig: &quic.Config{
|
||||||
|
KeepAlivePeriod: QUICKeepAlivePeriod,
|
||||||
|
TokenStore: newQUICTokenStore(),
|
||||||
|
},
|
||||||
|
httpVersions: httpVersions,
|
||||||
|
}
|
||||||
|
|
||||||
|
runtime.SetFinalizer(doh, (*dnsOverHTTPS).Close)
|
||||||
|
|
||||||
|
return doh
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dc *dohClient) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) {
|
// Address implements the Upstream interface for *dnsOverHTTPS.
|
||||||
// https://datatracker.ietf.org/doc/html/rfc8484#section-4.1
|
func (doh *dnsOverHTTPS) Address() string { return doh.url.String() }
|
||||||
// In order to maximize cache friendliness, SHOULD use a DNS ID of 0 in every DNS request.
|
func (doh *dnsOverHTTPS) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) {
|
||||||
newM := *m
|
// Quote from https://www.rfc-editor.org/rfc/rfc8484.html:
|
||||||
newM.Id = 0
|
// In order to maximize HTTP cache friendliness, DoH clients using media
|
||||||
req, err := dc.newRequest(&newM)
|
// formats that include the ID field from the DNS message header, such
|
||||||
|
// as "application/dns-message", SHOULD use a DNS ID of 0 in every DNS
|
||||||
|
// request.
|
||||||
|
id := m.Id
|
||||||
|
m.Id = 0
|
||||||
|
defer func() {
|
||||||
|
// Restore the original ID to not break compatibility with proxies.
|
||||||
|
m.Id = id
|
||||||
|
if msg != nil {
|
||||||
|
msg.Id = id
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Check if there was already an active client before sending the request.
|
||||||
|
// We'll only attempt to re-connect if there was one.
|
||||||
|
client, isCached, err := doh.getClient(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("failed to init http client: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
req = req.WithContext(ctx)
|
// Make the first attempt to send the DNS query.
|
||||||
msg, err = dc.doRequest(req)
|
msg, err = doh.exchangeHTTPS(ctx, client, m)
|
||||||
if err == nil {
|
|
||||||
msg.Id = m.Id
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// newRequest returns a new DoH request given a dns.Msg.
|
// Make up to 2 attempts to re-create the HTTP client and send the request
|
||||||
func (dc *dohClient) newRequest(m *D.Msg) (*http.Request, error) {
|
// again. There are several cases (mostly, with QUIC) where this workaround
|
||||||
buf, err := m.Pack()
|
// is necessary to make HTTP client usable. We need to make 2 attempts in
|
||||||
if err != nil {
|
// the case when the connection was closed (due to inactivity for example)
|
||||||
return nil, err
|
// AND the server refuses to open a 0-RTT connection.
|
||||||
|
for i := 0; isCached && doh.shouldRetry(err) && i < 2; i++ {
|
||||||
|
client, err = doh.resetClient(ctx, err)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to reset http client: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
msg, err = doh.exchangeHTTPS(ctx, client, m)
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodPost, dc.url, bytes.NewReader(buf))
|
if err != nil && !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
|
||||||
if err != nil {
|
// If the request failed anyway, make sure we don't use this client.
|
||||||
return req, err
|
_, resErr := doh.resetClient(ctx, err)
|
||||||
|
|
||||||
|
return nil, fmt.Errorf("%w (resErr:%v)", err, resErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("content-type", dotMimeType)
|
|
||||||
req.Header.Set("accept", dotMimeType)
|
|
||||||
return req, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (dc *dohClient) doRequest(req *http.Request) (msg *D.Msg, err error) {
|
|
||||||
client := &http.Client{Transport: dc.transport}
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
buf, err := io.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
msg = &D.Msg{}
|
|
||||||
err = msg.Unpack(buf)
|
|
||||||
return msg, err
|
return msg, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDoHClient(url string, r *Resolver, params map[string]string, proxyAdapter string) *dohClient {
|
// Exchange implements the Upstream interface for *dnsOverHTTPS.
|
||||||
useH3 := params["h3"] == "true"
|
func (doh *dnsOverHTTPS) Exchange(m *D.Msg) (*D.Msg, error) {
|
||||||
TLCConfig := tlsC.GetDefaultTLSConfig()
|
return doh.ExchangeContext(context.Background(), m)
|
||||||
var transport http.RoundTripper
|
}
|
||||||
if useH3 {
|
|
||||||
transport = &http3.RoundTripper{
|
|
||||||
Dial: func(ctx context.Context, addr string, tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlyConnection, error) {
|
|
||||||
host, port, err := net.SplitHostPort(addr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
ip, err := resolver.ResolveIPWithResolver(host, r)
|
// Close implements the Upstream interface for *dnsOverHTTPS.
|
||||||
if err != nil {
|
func (doh *dnsOverHTTPS) Close() (err error) {
|
||||||
return nil, err
|
doh.clientMu.Lock()
|
||||||
}
|
defer doh.clientMu.Unlock()
|
||||||
|
|
||||||
portInt, err := strconv.Atoi(port)
|
runtime.SetFinalizer(doh, nil)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
udpAddr := net.UDPAddr{
|
if doh.client == nil {
|
||||||
IP: net.ParseIP(ip.String()),
|
return nil
|
||||||
Port: portInt,
|
}
|
||||||
}
|
|
||||||
|
|
||||||
var conn net.PacketConn
|
return doh.closeClient(doh.client)
|
||||||
if proxyAdapter == "" {
|
}
|
||||||
conn, err = dialer.ListenPacket(ctx, "udp", "")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if wrapConn, err := dialContextExtra(ctx, proxyAdapter, "udp", ip, port); err == nil {
|
|
||||||
if pc, ok := wrapConn.(*wrapPacketConn); ok {
|
|
||||||
conn = pc
|
|
||||||
} else {
|
|
||||||
return nil, fmt.Errorf("conn isn't wrapPacketConn")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return quic.DialEarlyContext(ctx, conn, &udpAddr, host, tlsCfg, cfg)
|
// closeClient cleans up resources used by client if necessary. Note, that at
|
||||||
},
|
// this point it should only be done for HTTP/3 as it may leak due to keep-alive
|
||||||
TLSClientConfig: TLCConfig,
|
// connections.
|
||||||
}
|
func (doh *dnsOverHTTPS) closeClient(client *http.Client) (err error) {
|
||||||
} else {
|
if isHTTP3(client) {
|
||||||
transport = &http.Transport{
|
return client.Transport.(io.Closer).Close()
|
||||||
ForceAttemptHTTP2: true,
|
}
|
||||||
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
||||||
host, port, err := net.SplitHostPort(addr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
ip, err := resolver.ResolveIPWithResolver(host, r)
|
return nil
|
||||||
if err != nil {
|
}
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if proxyAdapter == "" {
|
// exchangeHTTPS logs the request and its result and calls exchangeHTTPSClient.
|
||||||
return dialer.DialContext(ctx, "tcp", net.JoinHostPort(ip.String(), port))
|
func (doh *dnsOverHTTPS) exchangeHTTPS(ctx context.Context, client *http.Client, req *D.Msg) (resp *D.Msg, err error) {
|
||||||
} else {
|
resp, err = doh.exchangeHTTPSClient(ctx, client, req)
|
||||||
return dialContextExtra(ctx, proxyAdapter, "tcp", ip, port)
|
return resp, err
|
||||||
}
|
}
|
||||||
},
|
|
||||||
TLSClientConfig: TLCConfig,
|
// exchangeHTTPSClient sends the DNS query to a DoH resolver using the specified
|
||||||
|
// http.Client instance.
|
||||||
|
func (doh *dnsOverHTTPS) exchangeHTTPSClient(
|
||||||
|
ctx context.Context,
|
||||||
|
client *http.Client,
|
||||||
|
req *D.Msg,
|
||||||
|
) (resp *D.Msg, err error) {
|
||||||
|
buf, err := req.Pack()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("packing message: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// It appears, that GET requests are more memory-efficient with Golang
|
||||||
|
// implementation of HTTP/2.
|
||||||
|
method := http.MethodGet
|
||||||
|
if isHTTP3(client) {
|
||||||
|
// If we're using HTTP/3, use http3.MethodGet0RTT to force using 0-RTT.
|
||||||
|
method = http3.MethodGet0RTT
|
||||||
|
}
|
||||||
|
|
||||||
|
url := doh.url
|
||||||
|
url.RawQuery = fmt.Sprintf("dns=%s", base64.RawURLEncoding.EncodeToString(buf))
|
||||||
|
httpReq, err := http.NewRequestWithContext(ctx, method, url.String(), nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("creating http request to %s: %w", url, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
httpReq.Header.Set("Accept", "application/dns-message")
|
||||||
|
httpReq.Header.Set("User-Agent", "")
|
||||||
|
httpResp, err := client.Do(httpReq)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("requesting %s: %w", url, err)
|
||||||
|
}
|
||||||
|
defer httpResp.Body.Close()
|
||||||
|
|
||||||
|
body, err := io.ReadAll(httpResp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("reading %s: %w", url, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if httpResp.StatusCode != http.StatusOK {
|
||||||
|
return nil,
|
||||||
|
fmt.Errorf(
|
||||||
|
"expected status %d, got %d from %s",
|
||||||
|
http.StatusOK,
|
||||||
|
httpResp.StatusCode,
|
||||||
|
url,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp = &D.Msg{}
|
||||||
|
err = resp.Unpack(body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf(
|
||||||
|
"unpacking response from %s: body is %s: %w",
|
||||||
|
url,
|
||||||
|
body,
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.Id != req.Id {
|
||||||
|
err = D.ErrId
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// shouldRetry checks what error we have received and returns true if we should
|
||||||
|
// re-create the HTTP client and retry the request.
|
||||||
|
func (doh *dnsOverHTTPS) shouldRetry(err error) (ok bool) {
|
||||||
|
if err == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
var netErr net.Error
|
||||||
|
if errors.As(err, &netErr) && netErr.Timeout() {
|
||||||
|
// If this is a timeout error, trying to forcibly re-create the HTTP
|
||||||
|
// client instance. This is an attempt to fix an issue with DoH client
|
||||||
|
// stalling after a network change.
|
||||||
|
//
|
||||||
|
// See https://github.com/AdguardTeam/AdGuardHome/issues/3217.
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if isQUICRetryError(err) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// resetClient triggers re-creation of the *http.Client that is used by this
|
||||||
|
// upstream. This method accepts the error that caused resetting client as
|
||||||
|
// depending on the error we may also reset the QUIC config.
|
||||||
|
func (doh *dnsOverHTTPS) resetClient(ctx context.Context, resetErr error) (client *http.Client, err error) {
|
||||||
|
doh.clientMu.Lock()
|
||||||
|
defer doh.clientMu.Unlock()
|
||||||
|
|
||||||
|
if errors.Is(resetErr, quic.Err0RTTRejected) {
|
||||||
|
// Reset the TokenStore only if 0-RTT was rejected.
|
||||||
|
doh.resetQUICConfig()
|
||||||
|
}
|
||||||
|
|
||||||
|
oldClient := doh.client
|
||||||
|
if oldClient != nil {
|
||||||
|
closeErr := doh.closeClient(oldClient)
|
||||||
|
if closeErr != nil {
|
||||||
|
log.Warnln("warning: failed to close the old http client: %v", closeErr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &dohClient{
|
log.Debugln("re-creating the http client due to %v", resetErr)
|
||||||
url: url,
|
doh.client, err = doh.createClient(ctx)
|
||||||
transport: transport,
|
|
||||||
|
return doh.client, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// getQUICConfig returns the QUIC config in a thread-safe manner. Note, that
|
||||||
|
// this method returns a pointer, it is forbidden to change its properties.
|
||||||
|
func (doh *dnsOverHTTPS) getQUICConfig() (c *quic.Config) {
|
||||||
|
doh.quicConfigGuard.Lock()
|
||||||
|
defer doh.quicConfigGuard.Unlock()
|
||||||
|
|
||||||
|
return doh.quicConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
// resetQUICConfig Re-create the token store to make sure we're not trying to
|
||||||
|
// use invalid for 0-RTT.
|
||||||
|
func (doh *dnsOverHTTPS) resetQUICConfig() {
|
||||||
|
doh.quicConfigGuard.Lock()
|
||||||
|
defer doh.quicConfigGuard.Unlock()
|
||||||
|
|
||||||
|
doh.quicConfig = doh.quicConfig.Clone()
|
||||||
|
doh.quicConfig.TokenStore = newQUICTokenStore()
|
||||||
|
}
|
||||||
|
|
||||||
|
// getClient gets or lazily initializes an HTTP client (and transport) that will
|
||||||
|
// be used for this DoH resolver.
|
||||||
|
func (doh *dnsOverHTTPS) getClient(ctx context.Context) (c *http.Client, isCached bool, err error) {
|
||||||
|
startTime := time.Now()
|
||||||
|
|
||||||
|
doh.clientMu.Lock()
|
||||||
|
defer doh.clientMu.Unlock()
|
||||||
|
if doh.client != nil {
|
||||||
|
return doh.client, true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Timeout can be exceeded while waiting for the lock. This happens quite
|
||||||
|
// often on mobile devices.
|
||||||
|
elapsed := time.Since(startTime)
|
||||||
|
if elapsed > maxElapsedTime {
|
||||||
|
return nil, false, fmt.Errorf("timeout exceeded: %s", elapsed)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debugln("creating a new http client")
|
||||||
|
doh.client, err = doh.createClient(ctx)
|
||||||
|
|
||||||
|
return doh.client, false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// createClient creates a new *http.Client instance. The HTTP protocol version
|
||||||
|
// will depend on whether HTTP3 is allowed and provided by this upstream. Note,
|
||||||
|
// that we'll attempt to establish a QUIC connection when creating the client in
|
||||||
|
// order to check whether HTTP3 is supported.
|
||||||
|
func (doh *dnsOverHTTPS) createClient(ctx context.Context) (*http.Client, error) {
|
||||||
|
transport, err := doh.createTransport(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("[%s] initializing http transport: %w", doh.url.String(), err)
|
||||||
|
}
|
||||||
|
|
||||||
|
client := &http.Client{
|
||||||
|
Transport: transport,
|
||||||
|
Timeout: DefaultTimeout,
|
||||||
|
Jar: nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
doh.client = client
|
||||||
|
|
||||||
|
return doh.client, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// createTransport initializes an HTTP transport that will be used specifically
|
||||||
|
// for this DoH resolver. This HTTP transport ensures that the HTTP requests
|
||||||
|
// will be sent exactly to the IP address got from the bootstrap resolver. Note,
|
||||||
|
// that this function will first attempt to establish a QUIC connection (if
|
||||||
|
// HTTP3 is enabled in the upstream options). If this attempt is successful,
|
||||||
|
// it returns an HTTP3 transport, otherwise it returns the H1/H2 transport.
|
||||||
|
func (doh *dnsOverHTTPS) createTransport(ctx context.Context) (t http.RoundTripper, err error) {
|
||||||
|
tlsConfig := tlsC.GetGlobalFingerprintTLCConfig(
|
||||||
|
&tls.Config{
|
||||||
|
InsecureSkipVerify: false,
|
||||||
|
MinVersion: tls.VersionTLS12,
|
||||||
|
SessionTicketsDisabled: false,
|
||||||
|
})
|
||||||
|
var nextProtos []string
|
||||||
|
for _, v := range doh.httpVersions {
|
||||||
|
nextProtos = append(nextProtos, string(v))
|
||||||
|
}
|
||||||
|
tlsConfig.NextProtos = nextProtos
|
||||||
|
dialContext := getDialHandler(doh.r, doh.proxyAdapter)
|
||||||
|
// First, we attempt to create an HTTP3 transport. If the probe QUIC
|
||||||
|
// connection is established successfully, we'll be using HTTP3 for this
|
||||||
|
// upstream.
|
||||||
|
transportH3, err := doh.createTransportH3(ctx, tlsConfig, dialContext)
|
||||||
|
if err == nil {
|
||||||
|
log.Debugln("[%s] using HTTP/3 for this upstream: QUIC was faster", doh.url.String())
|
||||||
|
return transportH3, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debugln("[%s] using HTTP/2 for this upstream: %v", doh.url.String(), err)
|
||||||
|
|
||||||
|
if !doh.supportsHTTP() {
|
||||||
|
return nil, errors.New("HTTP1/1 and HTTP2 are not supported by this upstream")
|
||||||
|
}
|
||||||
|
|
||||||
|
transport := &http.Transport{
|
||||||
|
TLSClientConfig: tlsConfig,
|
||||||
|
DisableCompression: true,
|
||||||
|
DialContext: dialContext,
|
||||||
|
IdleConnTimeout: transportDefaultIdleConnTimeout,
|
||||||
|
MaxConnsPerHost: dohMaxConnsPerHost,
|
||||||
|
MaxIdleConns: dohMaxIdleConns,
|
||||||
|
// Since we have a custom DialContext, we need to use this field to
|
||||||
|
// make golang http.Client attempt to use HTTP/2. Otherwise, it would
|
||||||
|
// only be used when negotiated on the TLS level.
|
||||||
|
ForceAttemptHTTP2: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Explicitly configure transport to use HTTP/2.
|
||||||
|
//
|
||||||
|
// See https://github.com/AdguardTeam/dnsproxy/issues/11.
|
||||||
|
var transportH2 *http2.Transport
|
||||||
|
transportH2, err = http2.ConfigureTransports(transport)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enable HTTP/2 pings on idle connections.
|
||||||
|
transportH2.ReadIdleTimeout = transportDefaultReadIdleTimeout
|
||||||
|
|
||||||
|
return transport, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// http3Transport is a wrapper over *http3.RoundTripper that tries to optimize
|
||||||
|
// its behavior. The main thing that it does is trying to force use a single
|
||||||
|
// connection to a host instead of creating a new one all the time. It also
|
||||||
|
// helps mitigate race issues with quic-go.
|
||||||
|
type http3Transport struct {
|
||||||
|
baseTransport *http3.RoundTripper
|
||||||
|
|
||||||
|
closed bool
|
||||||
|
mu sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// type check
|
||||||
|
var _ http.RoundTripper = (*http3Transport)(nil)
|
||||||
|
|
||||||
|
// RoundTrip implements the http.RoundTripper interface for *http3Transport.
|
||||||
|
func (h *http3Transport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
|
||||||
|
h.mu.RLock()
|
||||||
|
defer h.mu.RUnlock()
|
||||||
|
|
||||||
|
if h.closed {
|
||||||
|
return nil, net.ErrClosed
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to use cached connection to the target host if it's available.
|
||||||
|
resp, err = h.baseTransport.RoundTripOpt(req, http3.RoundTripOpt{OnlyCachedConn: true})
|
||||||
|
|
||||||
|
if errors.Is(err, http3.ErrNoCachedConn) {
|
||||||
|
// If there are no cached connection, trigger creating a new one.
|
||||||
|
resp, err = h.baseTransport.RoundTrip(req)
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// type check
|
||||||
|
var _ io.Closer = (*http3Transport)(nil)
|
||||||
|
|
||||||
|
// Close implements the io.Closer interface for *http3Transport.
|
||||||
|
func (h *http3Transport) Close() (err error) {
|
||||||
|
h.mu.Lock()
|
||||||
|
defer h.mu.Unlock()
|
||||||
|
|
||||||
|
h.closed = true
|
||||||
|
|
||||||
|
return h.baseTransport.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// createTransportH3 tries to create an HTTP/3 transport for this upstream.
|
||||||
|
// We should be able to fall back to H1/H2 in case if HTTP/3 is unavailable or
|
||||||
|
// if it is too slow. In order to do that, this method will run two probes
|
||||||
|
// in parallel (one for TLS, the other one for QUIC) and if QUIC is faster it
|
||||||
|
// will create the *http3.RoundTripper instance.
|
||||||
|
func (doh *dnsOverHTTPS) createTransportH3(
|
||||||
|
ctx context.Context,
|
||||||
|
tlsConfig *tls.Config,
|
||||||
|
dialContext dialHandler,
|
||||||
|
) (roundTripper http.RoundTripper, err error) {
|
||||||
|
if !doh.supportsH3() {
|
||||||
|
return nil, errors.New("HTTP3 support is not enabled")
|
||||||
|
}
|
||||||
|
|
||||||
|
addr, err := doh.probeH3(ctx, tlsConfig, dialContext)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
rt := &http3.RoundTripper{
|
||||||
|
Dial: func(
|
||||||
|
ctx context.Context,
|
||||||
|
|
||||||
|
// Ignore the address and always connect to the one that we got
|
||||||
|
// from the bootstrapper.
|
||||||
|
_ string,
|
||||||
|
tlsCfg *tls.Config,
|
||||||
|
cfg *quic.Config,
|
||||||
|
) (c quic.EarlyConnection, err error) {
|
||||||
|
return doh.dialQuic(ctx, addr, tlsCfg, cfg)
|
||||||
|
},
|
||||||
|
DisableCompression: true,
|
||||||
|
TLSClientConfig: tlsConfig,
|
||||||
|
QuicConfig: doh.getQUICConfig(),
|
||||||
|
}
|
||||||
|
|
||||||
|
return &http3Transport{baseTransport: rt}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (doh *dnsOverHTTPS) dialQuic(ctx context.Context, addr string, tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlyConnection, error) {
|
||||||
|
ip, port, err := net.SplitHostPort(addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
portInt, err := strconv.Atoi(port)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
udpAddr := net.UDPAddr{
|
||||||
|
IP: net.ParseIP(ip),
|
||||||
|
Port: portInt,
|
||||||
|
}
|
||||||
|
conn, err := listenPacket(ctx, doh.proxyAdapter, "udp", addr, doh.r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return quic.DialEarlyContext(ctx, conn, &udpAddr, doh.url.Host, tlsCfg, cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// probeH3 runs a test to check whether QUIC is faster than TLS for this
|
||||||
|
// upstream. If the test is successful it will return the address that we
|
||||||
|
// should use to establish the QUIC connections.
|
||||||
|
func (doh *dnsOverHTTPS) probeH3(
|
||||||
|
ctx context.Context,
|
||||||
|
tlsConfig *tls.Config,
|
||||||
|
dialContext dialHandler,
|
||||||
|
) (addr string, err error) {
|
||||||
|
// We're using bootstrapped address instead of what's passed to the function
|
||||||
|
// it does not create an actual connection, but it helps us determine
|
||||||
|
// what IP is actually reachable (when there are v4/v6 addresses).
|
||||||
|
rawConn, err := dialContext(ctx, "udp", doh.url.Host)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to dial: %w", err)
|
||||||
|
}
|
||||||
|
addr = rawConn.RemoteAddr().String()
|
||||||
|
// It's never actually used.
|
||||||
|
_ = rawConn.Close()
|
||||||
|
|
||||||
|
// Avoid spending time on probing if this upstream only supports HTTP/3.
|
||||||
|
if doh.supportsH3() && !doh.supportsHTTP() {
|
||||||
|
return addr, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use a new *tls.Config with empty session cache for probe connections.
|
||||||
|
// Surprisingly, this is really important since otherwise it invalidates
|
||||||
|
// the existing cache.
|
||||||
|
// TODO(ameshkov): figure out why the sessions cache invalidates here.
|
||||||
|
probeTLSCfg := tlsConfig.Clone()
|
||||||
|
probeTLSCfg.ClientSessionCache = nil
|
||||||
|
|
||||||
|
// Do not expose probe connections to the callbacks that are passed to
|
||||||
|
// the bootstrap options to avoid side-effects.
|
||||||
|
// TODO(ameshkov): consider exposing, somehow mark that this is a probe.
|
||||||
|
probeTLSCfg.VerifyPeerCertificate = nil
|
||||||
|
probeTLSCfg.VerifyConnection = nil
|
||||||
|
|
||||||
|
// Run probeQUIC and probeTLS in parallel and see which one is faster.
|
||||||
|
chQuic := make(chan error, 1)
|
||||||
|
chTLS := make(chan error, 1)
|
||||||
|
go doh.probeQUIC(ctx, addr, probeTLSCfg, chQuic)
|
||||||
|
go doh.probeTLS(ctx, dialContext, probeTLSCfg, chTLS)
|
||||||
|
|
||||||
|
select {
|
||||||
|
case quicErr := <-chQuic:
|
||||||
|
if quicErr != nil {
|
||||||
|
// QUIC failed, return error since HTTP3 was not preferred.
|
||||||
|
return "", quicErr
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return immediately, QUIC was faster.
|
||||||
|
return addr, quicErr
|
||||||
|
case tlsErr := <-chTLS:
|
||||||
|
if tlsErr != nil {
|
||||||
|
// Return immediately, TLS failed.
|
||||||
|
log.Debugln("probing TLS: %v", tlsErr)
|
||||||
|
return addr, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", errors.New("TLS was faster than QUIC, prefer it")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// probeQUIC attempts to establish a QUIC connection to the specified address.
|
||||||
|
// We run probeQUIC and probeTLS in parallel and see which one is faster.
|
||||||
|
func (doh *dnsOverHTTPS) probeQUIC(ctx context.Context, addr string, tlsConfig *tls.Config, ch chan error) {
|
||||||
|
startTime := time.Now()
|
||||||
|
conn, err := doh.dialQuic(ctx, addr, tlsConfig, doh.getQUICConfig())
|
||||||
|
if err != nil {
|
||||||
|
ch <- fmt.Errorf("opening QUIC connection to %s: %w", doh.Address(), err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ignore the error since there's no way we can use it for anything useful.
|
||||||
|
_ = conn.CloseWithError(QUICCodeNoError, "")
|
||||||
|
|
||||||
|
ch <- nil
|
||||||
|
|
||||||
|
elapsed := time.Now().Sub(startTime)
|
||||||
|
log.Debugln("elapsed on establishing a QUIC connection: %s", elapsed)
|
||||||
|
}
|
||||||
|
|
||||||
|
// probeTLS attempts to establish a TLS connection to the specified address. We
|
||||||
|
// run probeQUIC and probeTLS in parallel and see which one is faster.
|
||||||
|
func (doh *dnsOverHTTPS) probeTLS(ctx context.Context, dialContext dialHandler, tlsConfig *tls.Config, ch chan error) {
|
||||||
|
startTime := time.Now()
|
||||||
|
|
||||||
|
conn, err := doh.tlsDial(ctx, dialContext, "tcp", tlsConfig)
|
||||||
|
if err != nil {
|
||||||
|
ch <- fmt.Errorf("opening TLS connection: %w", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ignore the error since there's no way we can use it for anything useful.
|
||||||
|
_ = conn.Close()
|
||||||
|
|
||||||
|
ch <- nil
|
||||||
|
|
||||||
|
elapsed := time.Now().Sub(startTime)
|
||||||
|
log.Debugln("elapsed on establishing a TLS connection: %s", elapsed)
|
||||||
|
}
|
||||||
|
|
||||||
|
// supportsH3 returns true if HTTP/3 is supported by this upstream.
|
||||||
|
func (doh *dnsOverHTTPS) supportsH3() (ok bool) {
|
||||||
|
for _, v := range doh.supportedHTTPVersions() {
|
||||||
|
if v == C.HTTPVersion3 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// supportsHTTP returns true if HTTP/1.1 or HTTP2 is supported by this upstream.
|
||||||
|
func (doh *dnsOverHTTPS) supportsHTTP() (ok bool) {
|
||||||
|
for _, v := range doh.supportedHTTPVersions() {
|
||||||
|
if v == C.HTTPVersion11 || v == C.HTTPVersion2 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// supportedHTTPVersions returns the list of supported HTTP versions.
|
||||||
|
func (doh *dnsOverHTTPS) supportedHTTPVersions() (v []C.HTTPVersion) {
|
||||||
|
v = doh.httpVersions
|
||||||
|
if v == nil {
|
||||||
|
v = DefaultHTTPVersions
|
||||||
|
}
|
||||||
|
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
// isHTTP3 checks if the *http.Client is an HTTP/3 client.
|
||||||
|
func isHTTP3(client *http.Client) (ok bool) {
|
||||||
|
_, ok = client.Transport.(*http3Transport)
|
||||||
|
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// tlsDial is basically the same as tls.DialWithDialer, but we will call our own
|
||||||
|
// dialContext function to get connection.
|
||||||
|
func (doh *dnsOverHTTPS) tlsDial(ctx context.Context, dialContext dialHandler, network string, config *tls.Config) (*tls.Conn, error) {
|
||||||
|
// We're using bootstrapped address instead of what's passed
|
||||||
|
// to the function.
|
||||||
|
rawConn, err := dialContext(ctx, network, doh.url.Host)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// We want the timeout to cover the whole process: TCP connection and
|
||||||
|
// TLS handshake dialTimeout will be used as connection deadLine.
|
||||||
|
conn := tls.Client(rawConn, config)
|
||||||
|
|
||||||
|
err = conn.SetDeadline(time.Now().Add(dialTimeout))
|
||||||
|
if err != nil {
|
||||||
|
// Must not happen in normal circumstances.
|
||||||
|
panic(fmt.Errorf("cannot set deadline: %w", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
err = conn.Handshake()
|
||||||
|
if err != nil {
|
||||||
|
defer conn.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return conn, nil
|
||||||
|
}
|
||||||
|
517
dns/doq.go
517
dns/doq.go
@ -1,134 +1,303 @@
|
|||||||
package dns
|
package dns
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
|
||||||
"github.com/Dreamacro/clash/component/resolver"
|
|
||||||
tlsC "github.com/Dreamacro/clash/component/tls"
|
|
||||||
"github.com/lucas-clemente/quic-go"
|
|
||||||
"net"
|
"net"
|
||||||
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
||||||
|
"github.com/metacubex/quic-go"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/log"
|
"github.com/Dreamacro/clash/log"
|
||||||
D "github.com/miekg/dns"
|
D "github.com/miekg/dns"
|
||||||
)
|
)
|
||||||
|
|
||||||
const NextProtoDQ = "doq"
|
const NextProtoDQ = "doq"
|
||||||
|
const (
|
||||||
|
// QUICCodeNoError is used when the connection or stream needs to be closed,
|
||||||
|
// but there is no error to signal.
|
||||||
|
QUICCodeNoError = quic.ApplicationErrorCode(0)
|
||||||
|
// QUICCodeInternalError signals that the DoQ implementation encountered
|
||||||
|
// an internal error and is incapable of pursuing the transaction or the
|
||||||
|
// connection.
|
||||||
|
QUICCodeInternalError = quic.ApplicationErrorCode(1)
|
||||||
|
// QUICKeepAlivePeriod is the value that we pass to *quic.Config and that
|
||||||
|
// controls the period with with keep-alive frames are being sent to the
|
||||||
|
// connection. We set it to 20s as it would be in the quic-go@v0.27.1 with
|
||||||
|
// KeepAlive field set to true This value is specified in
|
||||||
|
// https://pkg.go.dev/github.com/metacubex/quic-go/internal/protocol#MaxKeepAliveInterval.
|
||||||
|
//
|
||||||
|
// TODO(ameshkov): Consider making it configurable.
|
||||||
|
QUICKeepAlivePeriod = time.Second * 20
|
||||||
|
DefaultTimeout = time.Second * 5
|
||||||
|
)
|
||||||
|
|
||||||
var bytesPool = sync.Pool{New: func() interface{} { return &bytes.Buffer{} }}
|
// dnsOverQUIC is a struct that implements the Upstream interface for the
|
||||||
|
// DNS-over-QUIC protocol (spec: https://www.rfc-editor.org/rfc/rfc9250.html).
|
||||||
|
type dnsOverQUIC struct {
|
||||||
|
// quicConfig is the QUIC configuration that is used for establishing
|
||||||
|
// connections to the upstream. This configuration includes the TokenStore
|
||||||
|
// that needs to be stored for the lifetime of dnsOverQUIC since we can
|
||||||
|
// re-create the connection.
|
||||||
|
quicConfig *quic.Config
|
||||||
|
quicConfigGuard sync.Mutex
|
||||||
|
|
||||||
|
// conn is the current active QUIC connection. It can be closed and
|
||||||
|
// re-opened when needed.
|
||||||
|
conn quic.Connection
|
||||||
|
connMu sync.RWMutex
|
||||||
|
|
||||||
|
// bytesPool is a *sync.Pool we use to store byte buffers in. These byte
|
||||||
|
// buffers are used to read responses from the upstream.
|
||||||
|
bytesPool *sync.Pool
|
||||||
|
bytesPoolGuard sync.Mutex
|
||||||
|
|
||||||
type quicClient struct {
|
|
||||||
addr string
|
addr string
|
||||||
r *Resolver
|
|
||||||
connection quic.Connection
|
|
||||||
proxyAdapter string
|
proxyAdapter string
|
||||||
udp net.PacketConn
|
r *Resolver
|
||||||
sync.RWMutex // protects connection and bytesPool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDOQ(r *Resolver, addr, proxyAdapter string) *quicClient {
|
// type check
|
||||||
return &quicClient{
|
var _ dnsClient = (*dnsOverQUIC)(nil)
|
||||||
|
|
||||||
|
// newDoQ returns the DNS-over-QUIC Upstream.
|
||||||
|
func newDoQ(resolver *Resolver, addr string, adapter string) (dnsClient, error) {
|
||||||
|
doq := &dnsOverQUIC{
|
||||||
addr: addr,
|
addr: addr,
|
||||||
r: r,
|
proxyAdapter: adapter,
|
||||||
proxyAdapter: proxyAdapter,
|
r: resolver,
|
||||||
|
quicConfig: &quic.Config{
|
||||||
|
KeepAlivePeriod: QUICKeepAlivePeriod,
|
||||||
|
TokenStore: newQUICTokenStore(),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
runtime.SetFinalizer(doq, (*dnsOverQUIC).Close)
|
||||||
|
return doq, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dc *quicClient) Exchange(m *D.Msg) (msg *D.Msg, err error) {
|
// Address implements the Upstream interface for *dnsOverQUIC.
|
||||||
return dc.ExchangeContext(context.Background(), m)
|
func (doq *dnsOverQUIC) Address() string { return doq.addr }
|
||||||
}
|
|
||||||
|
func (doq *dnsOverQUIC) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) {
|
||||||
|
// When sending queries over a QUIC connection, the DNS Message ID MUST be
|
||||||
|
// set to zero.
|
||||||
|
id := m.Id
|
||||||
|
m.Id = 0
|
||||||
|
defer func() {
|
||||||
|
// Restore the original ID to not break compatibility with proxies.
|
||||||
|
m.Id = id
|
||||||
|
if msg != nil {
|
||||||
|
msg.Id = id
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Check if there was already an active conn before sending the request.
|
||||||
|
// We'll only attempt to re-connect if there was one.
|
||||||
|
hasConnection := doq.hasConnection()
|
||||||
|
|
||||||
|
// Make the first attempt to send the DNS query.
|
||||||
|
msg, err = doq.exchangeQUIC(ctx, m)
|
||||||
|
|
||||||
|
// Make up to 2 attempts to re-open the QUIC connection and send the request
|
||||||
|
// again. There are several cases where this workaround is necessary to
|
||||||
|
// make DoQ usable. We need to make 2 attempts in the case when the
|
||||||
|
// connection was closed (due to inactivity for example) AND the server
|
||||||
|
// refuses to open a 0-RTT connection.
|
||||||
|
for i := 0; hasConnection && doq.shouldRetry(err) && i < 2; i++ {
|
||||||
|
log.Debugln("re-creating the QUIC connection and retrying due to %v", err)
|
||||||
|
|
||||||
|
// Close the active connection to make sure we'll try to re-connect.
|
||||||
|
doq.closeConnWithError(err)
|
||||||
|
|
||||||
|
// Retry sending the request.
|
||||||
|
msg, err = doq.exchangeQUIC(ctx, m)
|
||||||
|
}
|
||||||
|
|
||||||
func (dc *quicClient) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) {
|
|
||||||
stream, err := dc.openStream(ctx)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to open new stream to %s", dc.addr)
|
// If we're unable to exchange messages, make sure the connection is
|
||||||
|
// closed and signal about an internal error.
|
||||||
|
doq.closeConnWithError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
buf, err := m.Pack()
|
return msg, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exchange implements the Upstream interface for *dnsOverQUIC.
|
||||||
|
func (doq *dnsOverQUIC) Exchange(m *D.Msg) (msg *D.Msg, err error) {
|
||||||
|
return doq.ExchangeContext(context.Background(), m)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close implements the Upstream interface for *dnsOverQUIC.
|
||||||
|
func (doq *dnsOverQUIC) Close() (err error) {
|
||||||
|
doq.connMu.Lock()
|
||||||
|
defer doq.connMu.Unlock()
|
||||||
|
|
||||||
|
runtime.SetFinalizer(doq, nil)
|
||||||
|
|
||||||
|
if doq.conn != nil {
|
||||||
|
err = doq.conn.CloseWithError(QUICCodeNoError, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// exchangeQUIC attempts to open a QUIC connection, send the DNS message
|
||||||
|
// through it and return the response it got from the server.
|
||||||
|
func (doq *dnsOverQUIC) exchangeQUIC(ctx context.Context, msg *D.Msg) (resp *D.Msg, err error) {
|
||||||
|
var conn quic.Connection
|
||||||
|
conn, err = doq.getConnection(ctx, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = stream.Write(buf)
|
var buf []byte
|
||||||
|
buf, err = msg.Pack()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to pack DNS message for DoQ: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var stream quic.Stream
|
||||||
|
stream, err = doq.openStream(ctx, conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, err = stream.Write(AddPrefix(buf))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to write to a QUIC stream: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
// The client MUST send the DNS query over the selected stream, and MUST
|
// The client MUST send the DNS query over the selected stream, and MUST
|
||||||
// indicate through the STREAM FIN mechanism that no further data will
|
// indicate through the STREAM FIN mechanism that no further data will
|
||||||
// be sent on that stream.
|
// be sent on that stream. Note, that stream.Close() closes the
|
||||||
// stream.Close() -- closes the write-direction of the stream.
|
// write-direction of the stream, but does not prevent reading from it.
|
||||||
_ = stream.Close()
|
_ = stream.Close()
|
||||||
|
|
||||||
respBuf := bytesPool.Get().(*bytes.Buffer)
|
return doq.readMsg(stream)
|
||||||
defer bytesPool.Put(respBuf)
|
|
||||||
defer respBuf.Reset()
|
|
||||||
|
|
||||||
n, err := respBuf.ReadFrom(stream)
|
|
||||||
if err != nil && n == 0 {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
reply := new(D.Msg)
|
|
||||||
err = reply.Unpack(respBuf.Bytes())
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return reply, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func isActive(s quic.Connection) bool {
|
// AddPrefix adds a 2-byte prefix with the DNS message length.
|
||||||
select {
|
func AddPrefix(b []byte) (m []byte) {
|
||||||
case <-s.Context().Done():
|
m = make([]byte, 2+len(b))
|
||||||
return false
|
binary.BigEndian.PutUint16(m, uint16(len(b)))
|
||||||
default:
|
copy(m[2:], b)
|
||||||
return true
|
|
||||||
}
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
// getConnection - opens or returns an existing quic.Connection
|
// shouldRetry checks what error we received and decides whether it is required
|
||||||
// useCached - if true and cached connection exists, return it right away
|
// to re-open the connection and retry sending the request.
|
||||||
// otherwise - forcibly creates a new connection
|
func (doq *dnsOverQUIC) shouldRetry(err error) (ok bool) {
|
||||||
func (dc *quicClient) getConnection(ctx context.Context) (quic.Connection, error) {
|
return isQUICRetryError(err)
|
||||||
var connection quic.Connection
|
}
|
||||||
dc.RLock()
|
|
||||||
connection = dc.connection
|
|
||||||
|
|
||||||
if connection != nil && isActive(connection) {
|
// getBytesPool returns (creates if needed) a pool we store byte buffers in.
|
||||||
dc.RUnlock()
|
func (doq *dnsOverQUIC) getBytesPool() (pool *sync.Pool) {
|
||||||
return connection, nil
|
doq.bytesPoolGuard.Lock()
|
||||||
}
|
defer doq.bytesPoolGuard.Unlock()
|
||||||
|
|
||||||
dc.RUnlock()
|
if doq.bytesPool == nil {
|
||||||
|
doq.bytesPool = &sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
b := make([]byte, MaxMsgSize)
|
||||||
|
|
||||||
dc.Lock()
|
return &b
|
||||||
defer dc.Unlock()
|
},
|
||||||
connection = dc.connection
|
|
||||||
if connection != nil {
|
|
||||||
if isActive(connection) {
|
|
||||||
return connection, nil
|
|
||||||
} else {
|
|
||||||
_ = connection.CloseWithError(quic.ApplicationErrorCode(0), "")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
return doq.bytesPool
|
||||||
connection, err = dc.openConnection(ctx)
|
|
||||||
dc.connection = connection
|
|
||||||
return connection, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dc *quicClient) openConnection(ctx context.Context) (quic.Connection, error) {
|
// getConnection opens or returns an existing quic.Connection. useCached
|
||||||
if dc.udp != nil {
|
// argument controls whether we should try to use the existing cached
|
||||||
_ = dc.udp.Close()
|
// connection. If it is false, we will forcibly create a new connection and
|
||||||
|
// close the existing one if needed.
|
||||||
|
func (doq *dnsOverQUIC) getConnection(ctx context.Context, useCached bool) (quic.Connection, error) {
|
||||||
|
var conn quic.Connection
|
||||||
|
doq.connMu.RLock()
|
||||||
|
conn = doq.conn
|
||||||
|
if conn != nil && useCached {
|
||||||
|
doq.connMu.RUnlock()
|
||||||
|
|
||||||
|
return conn, nil
|
||||||
|
}
|
||||||
|
if conn != nil {
|
||||||
|
// we're recreating the connection, let's create a new one.
|
||||||
|
_ = conn.CloseWithError(QUICCodeNoError, "")
|
||||||
|
}
|
||||||
|
doq.connMu.RUnlock()
|
||||||
|
|
||||||
|
doq.connMu.Lock()
|
||||||
|
defer doq.connMu.Unlock()
|
||||||
|
|
||||||
|
var err error
|
||||||
|
conn, err = doq.openConnection(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
doq.conn = conn
|
||||||
|
|
||||||
|
return conn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// hasConnection returns true if there's an active QUIC connection.
|
||||||
|
func (doq *dnsOverQUIC) hasConnection() (ok bool) {
|
||||||
|
doq.connMu.Lock()
|
||||||
|
defer doq.connMu.Unlock()
|
||||||
|
|
||||||
|
return doq.conn != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getQUICConfig returns the QUIC config in a thread-safe manner. Note, that
|
||||||
|
// this method returns a pointer, it is forbidden to change its properties.
|
||||||
|
func (doq *dnsOverQUIC) getQUICConfig() (c *quic.Config) {
|
||||||
|
doq.quicConfigGuard.Lock()
|
||||||
|
defer doq.quicConfigGuard.Unlock()
|
||||||
|
|
||||||
|
return doq.quicConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
// resetQUICConfig re-creates the tokens store as we may need to use a new one
|
||||||
|
// if we failed to connect.
|
||||||
|
func (doq *dnsOverQUIC) resetQUICConfig() {
|
||||||
|
doq.quicConfigGuard.Lock()
|
||||||
|
defer doq.quicConfigGuard.Unlock()
|
||||||
|
|
||||||
|
doq.quicConfig = doq.quicConfig.Clone()
|
||||||
|
doq.quicConfig.TokenStore = newQUICTokenStore()
|
||||||
|
}
|
||||||
|
|
||||||
|
// openStream opens a new QUIC stream for the specified connection.
|
||||||
|
func (doq *dnsOverQUIC) openStream(ctx context.Context, conn quic.Connection) (quic.Stream, error) {
|
||||||
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
stream, err := conn.OpenStreamSync(ctx)
|
||||||
|
if err == nil {
|
||||||
|
return stream, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We can get here if the old QUIC connection is not valid anymore. We
|
||||||
|
// should try to re-create the connection again in this case.
|
||||||
|
newConn, err := doq.getConnection(ctx, false)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// Open a new stream.
|
||||||
|
return newConn.OpenStreamSync(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// openConnection opens a new QUIC connection.
|
||||||
|
func (doq *dnsOverQUIC) openConnection(ctx context.Context) (conn quic.Connection, err error) {
|
||||||
tlsConfig := tlsC.GetGlobalFingerprintTLCConfig(
|
tlsConfig := tlsC.GetGlobalFingerprintTLCConfig(
|
||||||
&tls.Config{
|
&tls.Config{
|
||||||
InsecureSkipVerify: false,
|
InsecureSkipVerify: false,
|
||||||
@ -137,69 +306,161 @@ func (dc *quicClient) openConnection(ctx context.Context) (quic.Connection, erro
|
|||||||
},
|
},
|
||||||
SessionTicketsDisabled: false,
|
SessionTicketsDisabled: false,
|
||||||
})
|
})
|
||||||
|
// we're using bootstrapped address instead of what's passed to the function
|
||||||
quicConfig := &quic.Config{
|
// it does not create an actual connection, but it helps us determine
|
||||||
ConnectionIDLength: 12,
|
// what IP is actually reachable (when there're v4/v6 addresses).
|
||||||
HandshakeIdleTimeout: time.Second * 8,
|
rawConn, err := getDialHandler(doq.r, doq.proxyAdapter)(ctx, "udp", doq.addr)
|
||||||
MaxIncomingStreams: 4,
|
|
||||||
KeepAlivePeriod: 10 * time.Second,
|
|
||||||
MaxIdleTimeout: time.Second * 120,
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Debugln("opening new connection to %s", dc.addr)
|
|
||||||
var (
|
|
||||||
udp net.PacketConn
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
|
|
||||||
host, port, err := net.SplitHostPort(dc.addr)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("failed to open a QUIC connection: %w", err)
|
||||||
}
|
}
|
||||||
|
addr := rawConn.RemoteAddr().String()
|
||||||
|
// It's never actually used
|
||||||
|
_ = rawConn.Close()
|
||||||
|
|
||||||
ip, err := resolver.ResolveIPv4WithResolver(host, dc.r)
|
ip, port, err := net.SplitHostPort(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
p, err := strconv.Atoi(port)
|
p, err := strconv.Atoi(port)
|
||||||
udpAddr := net.UDPAddr{IP: ip.AsSlice(), Port: p}
|
udpAddr := net.UDPAddr{IP: net.ParseIP(ip), Port: p}
|
||||||
|
udp, err := listenPacket(ctx, doq.proxyAdapter, "udp", addr, doq.r)
|
||||||
if dc.proxyAdapter == "" {
|
|
||||||
udp, err = dialer.ListenPacket(ctx, "udp", "")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
conn, err := dialContextExtra(ctx, dc.proxyAdapter, "udp", ip, port)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
wrapConn, ok := conn.(*wrapPacketConn)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("quic create packet failed")
|
|
||||||
}
|
|
||||||
|
|
||||||
udp = wrapConn
|
|
||||||
}
|
|
||||||
|
|
||||||
session, err := quic.DialContext(ctx, udp, &udpAddr, host, tlsConfig, quicConfig)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to open QUIC connection: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
dc.udp = udp
|
|
||||||
return session, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (dc *quicClient) openStream(ctx context.Context) (quic.Stream, error) {
|
|
||||||
session, err := dc.getConnection(ctx)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// open a new stream
|
host, _, err := net.SplitHostPort(doq.addr)
|
||||||
return session.OpenStreamSync(ctx)
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
conn, err = quic.DialContext(ctx, udp, &udpAddr, host, tlsConfig, doq.getQUICConfig())
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("opening quic connection to %s: %w", doq.addr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return conn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// closeConnWithError closes the active connection with error to make sure that
|
||||||
|
// new queries were processed in another connection. We can do that in the case
|
||||||
|
// of a fatal error.
|
||||||
|
func (doq *dnsOverQUIC) closeConnWithError(err error) {
|
||||||
|
doq.connMu.Lock()
|
||||||
|
defer doq.connMu.Unlock()
|
||||||
|
|
||||||
|
if doq.conn == nil {
|
||||||
|
// Do nothing, there's no active conn anyways.
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
code := QUICCodeNoError
|
||||||
|
if err != nil {
|
||||||
|
code = QUICCodeInternalError
|
||||||
|
}
|
||||||
|
|
||||||
|
if errors.Is(err, quic.Err0RTTRejected) {
|
||||||
|
// Reset the TokenStore only if 0-RTT was rejected.
|
||||||
|
doq.resetQUICConfig()
|
||||||
|
}
|
||||||
|
|
||||||
|
err = doq.conn.CloseWithError(code, "")
|
||||||
|
if err != nil {
|
||||||
|
log.Errorln("failed to close the conn: %v", err)
|
||||||
|
}
|
||||||
|
doq.conn = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// readMsg reads the incoming DNS message from the QUIC stream.
|
||||||
|
func (doq *dnsOverQUIC) readMsg(stream quic.Stream) (m *D.Msg, err error) {
|
||||||
|
pool := doq.getBytesPool()
|
||||||
|
bufPtr := pool.Get().(*[]byte)
|
||||||
|
|
||||||
|
defer pool.Put(bufPtr)
|
||||||
|
|
||||||
|
respBuf := *bufPtr
|
||||||
|
n, err := stream.Read(respBuf)
|
||||||
|
if err != nil && n == 0 {
|
||||||
|
return nil, fmt.Errorf("reading response from %s: %w", doq.Address(), err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// All DNS messages (queries and responses) sent over DoQ connections MUST
|
||||||
|
// be encoded as a 2-octet length field followed by the message content as
|
||||||
|
// specified in [RFC1035].
|
||||||
|
// IMPORTANT: Note, that we ignore this prefix here as this implementation
|
||||||
|
// does not support receiving multiple messages over a single connection.
|
||||||
|
m = new(D.Msg)
|
||||||
|
err = m.Unpack(respBuf[2:])
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unpacking response from %s: %w", doq.Address(), err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// newQUICTokenStore creates a new quic.TokenStore that is necessary to have
|
||||||
|
// in order to benefit from 0-RTT.
|
||||||
|
func newQUICTokenStore() (s quic.TokenStore) {
|
||||||
|
// You can read more on address validation here:
|
||||||
|
// https://datatracker.ietf.org/doc/html/rfc9000#section-8.1
|
||||||
|
// Setting maxOrigins to 1 and tokensPerOrigin to 10 assuming that this is
|
||||||
|
// more than enough for the way we use it (one connection per upstream).
|
||||||
|
return quic.NewLRUTokenStore(1, 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
// isQUICRetryError checks the error and determines whether it may signal that
|
||||||
|
// we should re-create the QUIC connection. This requirement is caused by
|
||||||
|
// quic-go issues, see the comments inside this function.
|
||||||
|
// TODO(ameshkov): re-test when updating quic-go.
|
||||||
|
func isQUICRetryError(err error) (ok bool) {
|
||||||
|
var qAppErr *quic.ApplicationError
|
||||||
|
if errors.As(err, &qAppErr) && qAppErr.ErrorCode == 0 {
|
||||||
|
// This error is often returned when the server has been restarted,
|
||||||
|
// and we try to use the same connection on the client-side. It seems,
|
||||||
|
// that the old connections aren't closed immediately on the server-side
|
||||||
|
// and that's why one can run into this.
|
||||||
|
// In addition to that, quic-go HTTP3 client implementation does not
|
||||||
|
// clean up dead connections (this one is specific to DoH3 upstream):
|
||||||
|
// https://github.com/metacubex/quic-go/issues/765
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
var qIdleErr *quic.IdleTimeoutError
|
||||||
|
if errors.As(err, &qIdleErr) {
|
||||||
|
// This error means that the connection was closed due to being idle.
|
||||||
|
// In this case we should forcibly re-create the QUIC connection.
|
||||||
|
// Reproducing is rather simple, stop the server and wait for 30 seconds
|
||||||
|
// then try to send another request via the same upstream.
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
var resetErr *quic.StatelessResetError
|
||||||
|
if errors.As(err, &resetErr) {
|
||||||
|
// A stateless reset is sent when a server receives a QUIC packet that
|
||||||
|
// it doesn't know how to decrypt. For instance, it may happen when
|
||||||
|
// the server was recently rebooted. We should reconnect and try again
|
||||||
|
// in this case.
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
var qTransportError *quic.TransportError
|
||||||
|
if errors.As(err, &qTransportError) && qTransportError.ErrorCode == quic.NoError {
|
||||||
|
// A transport error with the NO_ERROR error code could be sent by the
|
||||||
|
// server when it considers that it's time to close the connection.
|
||||||
|
// For example, Google DNS eventually closes an active connection with
|
||||||
|
// the NO_ERROR code and "Connection max age expired" message:
|
||||||
|
// https://github.com/AdguardTeam/dnsproxy/issues/283
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if errors.Is(err, quic.Err0RTTRejected) {
|
||||||
|
// This error happens when we try to establish a 0-RTT connection with
|
||||||
|
// a token the server is no more aware of. This can be reproduced by
|
||||||
|
// restarting the QUIC server (it will clear its tokens cache). The
|
||||||
|
// next connection attempt will return this error until the client's
|
||||||
|
// tokens cache is purged.
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
@ -109,7 +109,7 @@ func NewEnhancer(cfg Config) *ResolverEnhancer {
|
|||||||
|
|
||||||
if cfg.EnhancedMode != C.DNSNormal {
|
if cfg.EnhancedMode != C.DNSNormal {
|
||||||
fakePool = cfg.Pool
|
fakePool = cfg.Pool
|
||||||
mapping = cache.NewLRUCache[netip.Addr, string](cache.WithSize[netip.Addr, string](4096), cache.WithStale[netip.Addr, string](true))
|
mapping = cache.New[netip.Addr, string](cache.WithSize[netip.Addr, string](4096), cache.WithStale[netip.Addr, string](true))
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ResolverEnhancer{
|
return &ResolverEnhancer{
|
||||||
|
@ -71,14 +71,15 @@ type fallbackDomainFilter interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type domainFilter struct {
|
type domainFilter struct {
|
||||||
tree *trie.DomainTrie[bool]
|
tree *trie.DomainTrie[struct{}]
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDomainFilter(domains []string) *domainFilter {
|
func NewDomainFilter(domains []string) *domainFilter {
|
||||||
df := domainFilter{tree: trie.New[bool]()}
|
df := domainFilter{tree: trie.New[struct{}]()}
|
||||||
for _, domain := range domains {
|
for _, domain := range domains {
|
||||||
_ = df.tree.Insert(domain, true)
|
_ = df.tree.Insert(domain, struct{}{})
|
||||||
}
|
}
|
||||||
|
df.tree.Optimize()
|
||||||
return &df
|
return &df
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ func withHosts(hosts *trie.DomainTrie[netip.Addr], mapping *cache.LruCache[netip
|
|||||||
return next(ctx, r)
|
return next(ctx, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
ip := record.Data
|
ip := record.Data()
|
||||||
msg := r.Copy()
|
msg := r.Copy()
|
||||||
|
|
||||||
if ip.Is4() && q.Qtype == D.TypeA {
|
if ip.Is4() && q.Qtype == D.TypeA {
|
||||||
@ -156,7 +156,7 @@ func withResolver(resolver *Resolver) handler {
|
|||||||
return handleMsgWithEmptyAnswer(r), nil
|
return handleMsgWithEmptyAnswer(r), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
msg, err := resolver.Exchange(r)
|
msg, err := resolver.ExchangeContext(ctx, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugln("[DNS Server] Exchange %s failed: %v", q.String(), err)
|
log.Debugln("[DNS Server] Exchange %s failed: %v", q.String(), err)
|
||||||
return msg, err
|
return msg, err
|
||||||
|
10
dns/patch.go
10
dns/patch.go
@ -1,14 +1,18 @@
|
|||||||
package dns
|
package dns
|
||||||
|
|
||||||
import D "github.com/miekg/dns"
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
D "github.com/miekg/dns"
|
||||||
|
)
|
||||||
|
|
||||||
type LocalServer struct {
|
type LocalServer struct {
|
||||||
handler handler
|
handler handler
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeMsg implement resolver.LocalServer ResolveMsg
|
// ServeMsg implement resolver.LocalServer ResolveMsg
|
||||||
func (s *LocalServer) ServeMsg(msg *D.Msg) (*D.Msg, error) {
|
func (s *LocalServer) ServeMsg(ctx context.Context, msg *D.Msg) (*D.Msg, error) {
|
||||||
return handlerWithContext(s.handler, msg)
|
return handlerWithContext(ctx, s.handler, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLocalServer(resolver *Resolver, mapper *ResolverEnhancer) *LocalServer {
|
func NewLocalServer(resolver *Resolver, mapper *ResolverEnhancer) *LocalServer {
|
||||||
|
151
dns/resolver.go
151
dns/resolver.go
@ -10,7 +10,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/cache"
|
"github.com/Dreamacro/clash/common/cache"
|
||||||
"github.com/Dreamacro/clash/common/picker"
|
|
||||||
"github.com/Dreamacro/clash/component/fakeip"
|
"github.com/Dreamacro/clash/component/fakeip"
|
||||||
"github.com/Dreamacro/clash/component/geodata/router"
|
"github.com/Dreamacro/clash/component/geodata/router"
|
||||||
"github.com/Dreamacro/clash/component/resolver"
|
"github.com/Dreamacro/clash/component/resolver"
|
||||||
@ -44,18 +43,18 @@ type Resolver struct {
|
|||||||
proxyServer []dnsClient
|
proxyServer []dnsClient
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resolver) ResolveAllIPPrimaryIPv4(host string) (ips []netip.Addr, err error) {
|
func (r *Resolver) LookupIPPrimaryIPv4(ctx context.Context, host string) (ips []netip.Addr, err error) {
|
||||||
ch := make(chan []netip.Addr, 1)
|
ch := make(chan []netip.Addr, 1)
|
||||||
go func() {
|
go func() {
|
||||||
defer close(ch)
|
defer close(ch)
|
||||||
ip, err := r.resolveIP(host, D.TypeAAAA)
|
ip, err := r.lookupIP(ctx, host, D.TypeAAAA)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ch <- ip
|
ch <- ip
|
||||||
}()
|
}()
|
||||||
|
|
||||||
ips, err = r.resolveIP(host, D.TypeA)
|
ips, err = r.lookupIP(ctx, host, D.TypeA)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -68,11 +67,11 @@ func (r *Resolver) ResolveAllIPPrimaryIPv4(host string) (ips []netip.Addr, err e
|
|||||||
return ip, nil
|
return ip, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resolver) ResolveAllIP(host string) (ips []netip.Addr, err error) {
|
func (r *Resolver) LookupIP(ctx context.Context, host string) (ips []netip.Addr, err error) {
|
||||||
ch := make(chan []netip.Addr, 1)
|
ch := make(chan []netip.Addr, 1)
|
||||||
go func() {
|
go func() {
|
||||||
defer close(ch)
|
defer close(ch)
|
||||||
ip, err := r.resolveIP(host, D.TypeAAAA)
|
ip, err := r.lookupIP(ctx, host, D.TypeAAAA)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -80,7 +79,7 @@ func (r *Resolver) ResolveAllIP(host string) (ips []netip.Addr, err error) {
|
|||||||
ch <- ip
|
ch <- ip
|
||||||
}()
|
}()
|
||||||
|
|
||||||
ips, err = r.resolveIP(host, D.TypeA)
|
ips, err = r.lookupIP(ctx, host, D.TypeA)
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case ipv6s, open := <-ch:
|
case ipv6s, open := <-ch:
|
||||||
@ -95,39 +94,47 @@ func (r *Resolver) ResolveAllIP(host string) (ips []netip.Addr, err error) {
|
|||||||
return ips, nil
|
return ips, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resolver) ResolveAllIPv4(host string) (ips []netip.Addr, err error) {
|
|
||||||
return r.resolveIP(host, D.TypeA)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Resolver) ResolveAllIPv6(host string) (ips []netip.Addr, err error) {
|
|
||||||
return r.resolveIP(host, D.TypeAAAA)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ResolveIP request with TypeA and TypeAAAA, priority return TypeA
|
// ResolveIP request with TypeA and TypeAAAA, priority return TypeA
|
||||||
func (r *Resolver) ResolveIP(host string) (ip netip.Addr, err error) {
|
func (r *Resolver) ResolveIP(ctx context.Context, host string) (ip netip.Addr, err error) {
|
||||||
if ips, err := r.ResolveAllIPPrimaryIPv4(host); err == nil {
|
ips, err := r.LookupIPPrimaryIPv4(ctx, host)
|
||||||
return ips[rand.Intn(len(ips))], nil
|
if err != nil {
|
||||||
} else {
|
|
||||||
return netip.Addr{}, err
|
return netip.Addr{}, err
|
||||||
|
} else if len(ips) == 0 {
|
||||||
|
return netip.Addr{}, fmt.Errorf("%w: %s", resolver.ErrIPNotFound, host)
|
||||||
}
|
}
|
||||||
|
return ips[rand.Intn(len(ips))], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// LookupIPv4 request with TypeA
|
||||||
|
func (r *Resolver) LookupIPv4(ctx context.Context, host string) ([]netip.Addr, error) {
|
||||||
|
return r.lookupIP(ctx, host, D.TypeA)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResolveIPv4 request with TypeA
|
// ResolveIPv4 request with TypeA
|
||||||
func (r *Resolver) ResolveIPv4(host string) (ip netip.Addr, err error) {
|
func (r *Resolver) ResolveIPv4(ctx context.Context, host string) (ip netip.Addr, err error) {
|
||||||
if ips, err := r.ResolveAllIPv4(host); err == nil {
|
ips, err := r.lookupIP(ctx, host, D.TypeA)
|
||||||
return ips[rand.Intn(len(ips))], nil
|
if err != nil {
|
||||||
} else {
|
|
||||||
return netip.Addr{}, err
|
return netip.Addr{}, err
|
||||||
|
} else if len(ips) == 0 {
|
||||||
|
return netip.Addr{}, fmt.Errorf("%w: %s", resolver.ErrIPNotFound, host)
|
||||||
}
|
}
|
||||||
|
return ips[rand.Intn(len(ips))], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// LookupIPv6 request with TypeAAAA
|
||||||
|
func (r *Resolver) LookupIPv6(ctx context.Context, host string) ([]netip.Addr, error) {
|
||||||
|
return r.lookupIP(ctx, host, D.TypeAAAA)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResolveIPv6 request with TypeAAAA
|
// ResolveIPv6 request with TypeAAAA
|
||||||
func (r *Resolver) ResolveIPv6(host string) (ip netip.Addr, err error) {
|
func (r *Resolver) ResolveIPv6(ctx context.Context, host string) (ip netip.Addr, err error) {
|
||||||
if ips, err := r.ResolveAllIPv6(host); err == nil {
|
ips, err := r.lookupIP(ctx, host, D.TypeAAAA)
|
||||||
return ips[rand.Intn(len(ips))], nil
|
if err != nil {
|
||||||
} else {
|
|
||||||
return netip.Addr{}, err
|
return netip.Addr{}, err
|
||||||
|
} else if len(ips) == 0 {
|
||||||
|
return netip.Addr{}, fmt.Errorf("%w: %s", resolver.ErrIPNotFound, host)
|
||||||
}
|
}
|
||||||
|
return ips[rand.Intn(len(ips))], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resolver) shouldIPFallback(ip netip.Addr) bool {
|
func (r *Resolver) shouldIPFallback(ip netip.Addr) bool {
|
||||||
@ -149,6 +156,16 @@ func (r *Resolver) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, e
|
|||||||
if len(m.Question) == 0 {
|
if len(m.Question) == 0 {
|
||||||
return nil, errors.New("should have one question at least")
|
return nil, errors.New("should have one question at least")
|
||||||
}
|
}
|
||||||
|
continueFetch := false
|
||||||
|
defer func() {
|
||||||
|
if continueFetch || errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
|
||||||
|
go func() {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), resolver.DefaultDNSTimeout)
|
||||||
|
defer cancel()
|
||||||
|
_, _ = r.exchangeWithoutCache(ctx, m) // ignore result, just for putMsgToCache
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
q := m.Question[0]
|
q := m.Question[0]
|
||||||
cacheM, expireTime, hit := r.lruCache.GetWithExpire(q.String())
|
cacheM, expireTime, hit := r.lruCache.GetWithExpire(q.String())
|
||||||
@ -157,7 +174,7 @@ func (r *Resolver) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, e
|
|||||||
msg = cacheM.Copy()
|
msg = cacheM.Copy()
|
||||||
if expireTime.Before(now) {
|
if expireTime.Before(now) {
|
||||||
setMsgTTL(msg, uint32(1)) // Continue fetch
|
setMsgTTL(msg, uint32(1)) // Continue fetch
|
||||||
go r.exchangeWithoutCache(ctx, m)
|
continueFetch = true
|
||||||
} else {
|
} else {
|
||||||
setMsgTTL(msg, uint32(time.Until(expireTime).Seconds()))
|
setMsgTTL(msg, uint32(time.Until(expireTime).Seconds()))
|
||||||
}
|
}
|
||||||
@ -170,9 +187,16 @@ func (r *Resolver) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, e
|
|||||||
func (r *Resolver) exchangeWithoutCache(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) {
|
func (r *Resolver) exchangeWithoutCache(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) {
|
||||||
q := m.Question[0]
|
q := m.Question[0]
|
||||||
|
|
||||||
ret, err, shared := r.group.Do(q.String(), func() (result any, err error) {
|
retryNum := 0
|
||||||
|
retryMax := 3
|
||||||
|
fn := func() (result any, err error) {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), resolver.DefaultDNSTimeout) // reset timeout in singleflight
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
result = retryNum
|
||||||
|
retryNum++
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,7 +214,35 @@ func (r *Resolver) exchangeWithoutCache(ctx context.Context, m *D.Msg) (msg *D.M
|
|||||||
return r.batchExchange(ctx, matched, m)
|
return r.batchExchange(ctx, matched, m)
|
||||||
}
|
}
|
||||||
return r.batchExchange(ctx, r.main, m)
|
return r.batchExchange(ctx, r.main, m)
|
||||||
})
|
}
|
||||||
|
|
||||||
|
ch := r.group.DoChan(q.String(), fn)
|
||||||
|
|
||||||
|
var result singleflight.Result
|
||||||
|
|
||||||
|
select {
|
||||||
|
case result = <-ch:
|
||||||
|
break
|
||||||
|
case <-ctx.Done():
|
||||||
|
select {
|
||||||
|
case result = <-ch: // maybe ctxDone and chFinish in same time, get DoChan's result as much as possible
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
go func() { // start a retrying monitor in background
|
||||||
|
result := <-ch
|
||||||
|
ret, err, shared := result.Val, result.Err, result.Shared
|
||||||
|
if err != nil && !shared && ret.(int) < retryMax { // retry
|
||||||
|
r.group.DoChan(q.String(), fn)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return nil, ctx.Err()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ret, err, shared := result.Val, result.Err, result.Shared
|
||||||
|
if err != nil && !shared && ret.(int) < retryMax { // retry
|
||||||
|
r.group.DoChan(q.String(), fn)
|
||||||
|
}
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
msg = ret.(*D.Msg)
|
msg = ret.(*D.Msg)
|
||||||
@ -203,31 +255,10 @@ func (r *Resolver) exchangeWithoutCache(ctx context.Context, m *D.Msg) (msg *D.M
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resolver) batchExchange(ctx context.Context, clients []dnsClient, m *D.Msg) (msg *D.Msg, err error) {
|
func (r *Resolver) batchExchange(ctx context.Context, clients []dnsClient, m *D.Msg) (msg *D.Msg, err error) {
|
||||||
fast, ctx := picker.WithTimeout[*D.Msg](ctx, resolver.DefaultDNSTimeout)
|
ctx, cancel := context.WithTimeout(ctx, resolver.DefaultDNSTimeout)
|
||||||
for _, client := range clients {
|
defer cancel()
|
||||||
r := client
|
|
||||||
fast.Go(func() (*D.Msg, error) {
|
|
||||||
m, err := r.ExchangeContext(ctx, m)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
} else if m.Rcode == D.RcodeServerFailure || m.Rcode == D.RcodeRefused {
|
|
||||||
return nil, errors.New("server failure")
|
|
||||||
}
|
|
||||||
return m, nil
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
elm := fast.Wait()
|
return batchExchange(ctx, clients, m)
|
||||||
if elm == nil {
|
|
||||||
err := errors.New("all DNS requests failed")
|
|
||||||
if fErr := fast.Error(); fErr != nil {
|
|
||||||
err = fmt.Errorf("%w, first error: %s", err, fErr.Error())
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
msg = elm
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resolver) matchPolicy(m *D.Msg) []dnsClient {
|
func (r *Resolver) matchPolicy(m *D.Msg) []dnsClient {
|
||||||
@ -245,7 +276,7 @@ func (r *Resolver) matchPolicy(m *D.Msg) []dnsClient {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
p := record.Data
|
p := record.Data()
|
||||||
return p.GetData()
|
return p.GetData()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -305,7 +336,7 @@ func (r *Resolver) ipExchange(ctx context.Context, m *D.Msg) (msg *D.Msg, err er
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resolver) resolveIP(host string, dnsType uint16) (ips []netip.Addr, err error) {
|
func (r *Resolver) lookupIP(ctx context.Context, host string, dnsType uint16) (ips []netip.Addr, err error) {
|
||||||
ip, err := netip.ParseAddr(host)
|
ip, err := netip.ParseAddr(host)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
isIPv4 := ip.Is4()
|
isIPv4 := ip.Is4()
|
||||||
@ -321,7 +352,7 @@ func (r *Resolver) resolveIP(host string, dnsType uint16) (ips []netip.Addr, err
|
|||||||
query := &D.Msg{}
|
query := &D.Msg{}
|
||||||
query.SetQuestion(D.Fqdn(host), dnsType)
|
query.SetQuestion(D.Fqdn(host), dnsType)
|
||||||
|
|
||||||
msg, err := r.Exchange(query)
|
msg, err := r.ExchangeContext(ctx, query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []netip.Addr{}, err
|
return []netip.Addr{}, err
|
||||||
}
|
}
|
||||||
@ -355,6 +386,7 @@ type NameServer struct {
|
|||||||
Interface *atomic.String
|
Interface *atomic.String
|
||||||
ProxyAdapter string
|
ProxyAdapter string
|
||||||
Params map[string]string
|
Params map[string]string
|
||||||
|
PreferH3 bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type FallbackFilter struct {
|
type FallbackFilter struct {
|
||||||
@ -380,13 +412,13 @@ type Config struct {
|
|||||||
func NewResolver(config Config) *Resolver {
|
func NewResolver(config Config) *Resolver {
|
||||||
defaultResolver := &Resolver{
|
defaultResolver := &Resolver{
|
||||||
main: transform(config.Default, nil),
|
main: transform(config.Default, nil),
|
||||||
lruCache: cache.NewLRUCache[string, *D.Msg](cache.WithSize[string, *D.Msg](4096), cache.WithStale[string, *D.Msg](true)),
|
lruCache: cache.New[string, *D.Msg](cache.WithSize[string, *D.Msg](4096), cache.WithStale[string, *D.Msg](true)),
|
||||||
}
|
}
|
||||||
|
|
||||||
r := &Resolver{
|
r := &Resolver{
|
||||||
ipv6: config.IPv6,
|
ipv6: config.IPv6,
|
||||||
main: transform(config.Main, defaultResolver),
|
main: transform(config.Main, defaultResolver),
|
||||||
lruCache: cache.NewLRUCache[string, *D.Msg](cache.WithSize[string, *D.Msg](4096), cache.WithStale[string, *D.Msg](true)),
|
lruCache: cache.New[string, *D.Msg](cache.WithSize[string, *D.Msg](4096), cache.WithStale[string, *D.Msg](true)),
|
||||||
hosts: config.Hosts,
|
hosts: config.Hosts,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -403,6 +435,7 @@ func NewResolver(config Config) *Resolver {
|
|||||||
for domain, nameserver := range config.Policy {
|
for domain, nameserver := range config.Policy {
|
||||||
_ = r.policy.Insert(domain, NewPolicy(transform([]NameServer{nameserver}, defaultResolver)))
|
_ = r.policy.Insert(domain, NewPolicy(transform([]NameServer{nameserver}, defaultResolver)))
|
||||||
}
|
}
|
||||||
|
r.policy.Optimize()
|
||||||
}
|
}
|
||||||
|
|
||||||
fallbackIPFilters := []fallbackIPFilter{}
|
fallbackIPFilters := []fallbackIPFilter{}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package dns
|
package dns
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
stdContext "context"
|
||||||
"errors"
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
@ -25,7 +26,7 @@ type Server struct {
|
|||||||
|
|
||||||
// ServeDNS implement D.Handler ServeDNS
|
// ServeDNS implement D.Handler ServeDNS
|
||||||
func (s *Server) ServeDNS(w D.ResponseWriter, r *D.Msg) {
|
func (s *Server) ServeDNS(w D.ResponseWriter, r *D.Msg) {
|
||||||
msg, err := handlerWithContext(s.handler, r)
|
msg, err := handlerWithContext(stdContext.Background(), s.handler, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
D.HandleFailed(w, r)
|
D.HandleFailed(w, r)
|
||||||
return
|
return
|
||||||
@ -34,12 +35,12 @@ func (s *Server) ServeDNS(w D.ResponseWriter, r *D.Msg) {
|
|||||||
w.WriteMsg(msg)
|
w.WriteMsg(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handlerWithContext(handler handler, msg *D.Msg) (*D.Msg, error) {
|
func handlerWithContext(stdCtx stdContext.Context, handler handler, msg *D.Msg) (*D.Msg, error) {
|
||||||
if len(msg.Question) == 0 {
|
if len(msg.Question) == 0 {
|
||||||
return nil, errors.New("at least one question is required")
|
return nil, errors.New("at least one question is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.NewDNSContext(msg)
|
ctx := context.NewDNSContext(stdCtx, msg)
|
||||||
return handler(ctx, msg)
|
return handler(ctx, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
204
dns/util.go
204
dns/util.go
@ -3,6 +3,7 @@ package dns
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
@ -10,8 +11,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/cache"
|
"github.com/Dreamacro/clash/common/cache"
|
||||||
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/Dreamacro/clash/common/nnip"
|
"github.com/Dreamacro/clash/common/nnip"
|
||||||
|
"github.com/Dreamacro/clash/common/picker"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
|
"github.com/Dreamacro/clash/component/resolver"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/log"
|
"github.com/Dreamacro/clash/log"
|
||||||
"github.com/Dreamacro/clash/tunnel"
|
"github.com/Dreamacro/clash/tunnel"
|
||||||
@ -19,7 +23,18 @@ import (
|
|||||||
D "github.com/miekg/dns"
|
D "github.com/miekg/dns"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
MaxMsgSize = 65535
|
||||||
|
)
|
||||||
|
|
||||||
func putMsgToCache(c *cache.LruCache[string, *D.Msg], key string, msg *D.Msg) {
|
func putMsgToCache(c *cache.LruCache[string, *D.Msg], key string, msg *D.Msg) {
|
||||||
|
// skip dns cache for acme challenge
|
||||||
|
if len(msg.Question) != 0 {
|
||||||
|
if q := msg.Question[0]; q.Qtype == D.TypeTXT && strings.HasPrefix(q.Name, "_acme-challenge") {
|
||||||
|
log.Debugln("[DNS] dns cache ignored because of acme challenge for: %s", q.Name)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
var ttl uint32
|
var ttl uint32
|
||||||
switch {
|
switch {
|
||||||
case len(msg.Answer) != 0:
|
case len(msg.Answer) != 0:
|
||||||
@ -59,13 +74,17 @@ func transform(servers []NameServer, resolver *Resolver) []dnsClient {
|
|||||||
for _, s := range servers {
|
for _, s := range servers {
|
||||||
switch s.Net {
|
switch s.Net {
|
||||||
case "https":
|
case "https":
|
||||||
ret = append(ret, newDoHClient(s.Addr, resolver, s.Params, s.ProxyAdapter))
|
ret = append(ret, newDoHClient(s.Addr, resolver, s.PreferH3, s.Params, s.ProxyAdapter))
|
||||||
continue
|
continue
|
||||||
case "dhcp":
|
case "dhcp":
|
||||||
ret = append(ret, newDHCPClient(s.Addr))
|
ret = append(ret, newDHCPClient(s.Addr))
|
||||||
continue
|
continue
|
||||||
case "quic":
|
case "quic":
|
||||||
ret = append(ret, newDOQ(resolver, s.Addr, s.ProxyAdapter))
|
if doq, err := newDoQ(resolver, s.Addr, s.ProxyAdapter); err == nil {
|
||||||
|
ret = append(ret, doq)
|
||||||
|
} else {
|
||||||
|
log.Fatalln("DoQ format error: %v", err)
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,86 +142,121 @@ func msgToDomain(msg *D.Msg) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
type wrapPacketConn struct {
|
type dialHandler func(ctx context.Context, network, addr string) (net.Conn, error)
|
||||||
net.PacketConn
|
|
||||||
rAddr net.Addr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (wpc *wrapPacketConn) Read(b []byte) (n int, err error) {
|
func getDialHandler(r *Resolver, proxyAdapter string, opts ...dialer.Option) dialHandler {
|
||||||
n, _, err = wpc.PacketConn.ReadFrom(b)
|
return func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
return n, err
|
if len(proxyAdapter) == 0 {
|
||||||
}
|
opts = append(opts, dialer.WithResolver(r))
|
||||||
|
return dialer.DialContext(ctx, network, addr, opts...)
|
||||||
func (wpc *wrapPacketConn) Write(b []byte) (n int, err error) {
|
|
||||||
return wpc.PacketConn.WriteTo(b, wpc.rAddr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (wpc *wrapPacketConn) RemoteAddr() net.Addr {
|
|
||||||
return wpc.rAddr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (wpc *wrapPacketConn) LocalAddr() net.Addr {
|
|
||||||
if wpc.PacketConn.LocalAddr() == nil {
|
|
||||||
return &net.UDPAddr{IP: net.IPv4zero, Port: 0}
|
|
||||||
} else {
|
|
||||||
return wpc.PacketConn.LocalAddr()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func dialContextExtra(ctx context.Context, adapterName string, network string, dstIP netip.Addr, port string, opts ...dialer.Option) (net.Conn, error) {
|
|
||||||
networkType := C.TCP
|
|
||||||
if network == "udp" {
|
|
||||||
|
|
||||||
networkType = C.UDP
|
|
||||||
}
|
|
||||||
|
|
||||||
addrType := C.AtypIPv4
|
|
||||||
if dstIP.Is6() {
|
|
||||||
addrType = C.AtypIPv6
|
|
||||||
}
|
|
||||||
|
|
||||||
metadata := &C.Metadata{
|
|
||||||
NetWork: networkType,
|
|
||||||
AddrType: addrType,
|
|
||||||
Host: "",
|
|
||||||
DstIP: dstIP,
|
|
||||||
DstPort: port,
|
|
||||||
}
|
|
||||||
|
|
||||||
adapter, ok := tunnel.Proxies()[adapterName]
|
|
||||||
if !ok {
|
|
||||||
opts = append(opts, dialer.WithInterface(adapterName))
|
|
||||||
if C.TCP == networkType {
|
|
||||||
return dialer.DialContext(ctx, network, dstIP.String()+":"+port, opts...)
|
|
||||||
} else {
|
} else {
|
||||||
packetConn, err := dialer.ListenPacket(ctx, network, dstIP.String()+":"+port, opts...)
|
host, port, err := net.SplitHostPort(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
adapter, ok := tunnel.Proxies()[proxyAdapter]
|
||||||
|
if !ok {
|
||||||
|
opts = append(opts, dialer.WithInterface(proxyAdapter))
|
||||||
|
}
|
||||||
|
if strings.Contains(network, "tcp") {
|
||||||
|
// tcp can resolve host by remote
|
||||||
|
metadata := &C.Metadata{
|
||||||
|
NetWork: C.TCP,
|
||||||
|
Host: host,
|
||||||
|
DstPort: port,
|
||||||
|
}
|
||||||
|
if ok {
|
||||||
|
return adapter.DialContext(ctx, metadata, opts...)
|
||||||
|
}
|
||||||
|
opts = append(opts, dialer.WithResolver(r))
|
||||||
|
return dialer.DialContext(ctx, network, addr, opts...)
|
||||||
|
} else {
|
||||||
|
// udp must resolve host first
|
||||||
|
dstIP, err := resolver.ResolveIPWithResolver(ctx, host, r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
metadata := &C.Metadata{
|
||||||
|
NetWork: C.UDP,
|
||||||
|
Host: "",
|
||||||
|
DstIP: dstIP,
|
||||||
|
DstPort: port,
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
return dialer.DialContext(ctx, network, addr, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
return &wrapPacketConn{
|
if !adapter.SupportUDP() {
|
||||||
PacketConn: packetConn,
|
return nil, fmt.Errorf("proxy adapter [%s] UDP is not supported", proxyAdapter)
|
||||||
rAddr: metadata.UDPAddr(),
|
}
|
||||||
}, nil
|
|
||||||
|
|
||||||
|
packetConn, err := adapter.ListenPacketContext(ctx, metadata, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return N.NewBindPacketConn(packetConn, metadata.UDPAddr()), nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if networkType == C.UDP && !adapter.SupportUDP() {
|
|
||||||
return nil, fmt.Errorf("proxy adapter [%s] UDP is not supported", adapterName)
|
func listenPacket(ctx context.Context, proxyAdapter string, network string, addr string, r *Resolver, opts ...dialer.Option) (net.PacketConn, error) {
|
||||||
}
|
host, port, err := net.SplitHostPort(addr)
|
||||||
|
if err != nil {
|
||||||
if networkType == C.UDP {
|
return nil, err
|
||||||
packetConn, err := adapter.ListenPacketContext(ctx, metadata, opts...)
|
}
|
||||||
if err != nil {
|
adapter, ok := tunnel.Proxies()[proxyAdapter]
|
||||||
return nil, err
|
if !ok && len(proxyAdapter) != 0 {
|
||||||
}
|
opts = append(opts, dialer.WithInterface(proxyAdapter))
|
||||||
|
}
|
||||||
return &wrapPacketConn{
|
|
||||||
PacketConn: packetConn,
|
// udp must resolve host first
|
||||||
rAddr: metadata.UDPAddr(),
|
dstIP, err := resolver.ResolveIPWithResolver(ctx, host, r)
|
||||||
}, nil
|
if err != nil {
|
||||||
}
|
return nil, err
|
||||||
|
}
|
||||||
return adapter.DialContext(ctx, metadata, opts...)
|
metadata := &C.Metadata{
|
||||||
|
NetWork: C.UDP,
|
||||||
|
Host: "",
|
||||||
|
DstIP: dstIP,
|
||||||
|
DstPort: port,
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
return dialer.ListenPacket(ctx, dialer.ParseNetwork(network, dstIP), "", opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !adapter.SupportUDP() {
|
||||||
|
return nil, fmt.Errorf("proxy adapter [%s] UDP is not supported", proxyAdapter)
|
||||||
|
}
|
||||||
|
|
||||||
|
return adapter.ListenPacketContext(ctx, metadata, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func batchExchange(ctx context.Context, clients []dnsClient, m *D.Msg) (msg *D.Msg, err error) {
|
||||||
|
fast, ctx := picker.WithTimeout[*D.Msg](ctx, resolver.DefaultDNSTimeout)
|
||||||
|
for _, client := range clients {
|
||||||
|
r := client
|
||||||
|
fast.Go(func() (*D.Msg, error) {
|
||||||
|
m, err := r.ExchangeContext(ctx, m)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else if m.Rcode == D.RcodeServerFailure || m.Rcode == D.RcodeRefused {
|
||||||
|
return nil, errors.New("server failure")
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
elm := fast.Wait()
|
||||||
|
if elm == nil {
|
||||||
|
err := errors.New("all DNS requests failed")
|
||||||
|
if fErr := fast.Error(); fErr != nil {
|
||||||
|
err = fmt.Errorf("%w, first error: %s", err, fErr.Error())
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
msg = elm
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user