mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-02-28 17:49:55 +00:00
Compare commits
12 Commits
v1.19.9
...
android-op
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
80fabce99c | ||
|
|
347e606853 | ||
|
|
7d58238e06 | ||
|
|
a883faae9d | ||
|
|
a7040af346 | ||
|
|
5cca3569e9 | ||
|
|
f2857b3243 | ||
|
|
8e84210852 | ||
|
|
8c2df79536 | ||
|
|
ebe33cff18 | ||
|
|
a551d7ebd9 | ||
|
|
79a29826a4 |
25
.github/ISSUE_TEMPLATE/bug_report.yml
vendored
25
.github/ISSUE_TEMPLATE/bug_report.yml
vendored
@@ -1,7 +1,6 @@
|
|||||||
name: Bug report
|
name: Bug report
|
||||||
description: Create a report to help us improve
|
description: Create a report to help us improve
|
||||||
title: "[Bug] "
|
title: "[Bug] "
|
||||||
labels: ["bug"]
|
|
||||||
body:
|
body:
|
||||||
- type: checkboxes
|
- type: checkboxes
|
||||||
id: ensure
|
id: ensure
|
||||||
@@ -13,8 +12,8 @@ Please verify that you've followed these steps
|
|||||||
"
|
"
|
||||||
options:
|
options:
|
||||||
- label: "
|
- label: "
|
||||||
确保你使用的是**本仓库**最新的的 mihomo 或 mihomo Alpha 版本
|
确保你使用的是**本仓库**最新的的 clash 或 clash Alpha 版本
|
||||||
Ensure you are using the latest version of Mihomo or Mihomo Alpha from **this repository**.
|
Ensure you are using the latest version of Clash or Clash Premium from **this repository**.
|
||||||
"
|
"
|
||||||
required: true
|
required: true
|
||||||
- label: "
|
- label: "
|
||||||
@@ -38,14 +37,14 @@ I have read the [documentation](https://wiki.metacubex.one/) and was unable to s
|
|||||||
"
|
"
|
||||||
required: true
|
required: true
|
||||||
- label: "
|
- label: "
|
||||||
这是 Mihomo 核心的问题,并非我所使用的 Mihomo 衍生版本(如 OpenMihomo、KoolMihomo 等)的特定问题
|
这是 Clash 核心的问题,并非我所使用的 Clash 衍生版本(如 OpenClash、KoolClash 等)的特定问题
|
||||||
This is an issue of the Mihomo core *per se*, not to the derivatives of Mihomo, like OpenMihomo or KoolMihomo.
|
This is an issue of the Clash core *per se*, not to the derivatives of Clash, like OpenClash or KoolClash.
|
||||||
"
|
"
|
||||||
required: true
|
required: true
|
||||||
- type: input
|
- type: input
|
||||||
attributes:
|
attributes:
|
||||||
label: Mihomo version
|
label: Clash version
|
||||||
description: "use `mihomo -v`"
|
description: "use `clash -v`"
|
||||||
validations:
|
validations:
|
||||||
required: true
|
required: true
|
||||||
- type: dropdown
|
- type: dropdown
|
||||||
@@ -61,20 +60,20 @@ This is an issue of the Mihomo core *per se*, not to the derivatives of Mihomo,
|
|||||||
- type: textarea
|
- type: textarea
|
||||||
attributes:
|
attributes:
|
||||||
render: yaml
|
render: yaml
|
||||||
label: "Mihomo config"
|
label: "Clash config"
|
||||||
description: "
|
description: "
|
||||||
在下方附上 Mihomo core 配置文件,请确保配置文件中没有敏感信息(比如:服务器地址,密码,端口等)
|
在下方附上 Clash core 配置文件,请确保配置文件中没有敏感信息(比如:服务器地址,密码,端口等)
|
||||||
Paste the Mihomo core configuration file below, please make sure that there is no sensitive information in the configuration file (e.g., server address/url, password, port)
|
Paste the Clash core configuration file below, please make sure that there is no sensitive information in the configuration file (e.g., server address/url, password, port)
|
||||||
"
|
"
|
||||||
validations:
|
validations:
|
||||||
required: true
|
required: true
|
||||||
- type: textarea
|
- type: textarea
|
||||||
attributes:
|
attributes:
|
||||||
render: shell
|
render: shell
|
||||||
label: Mihomo log
|
label: Clash log
|
||||||
description: "
|
description: "
|
||||||
在下方附上 Mihomo Core 的日志,log level 使用 DEBUG
|
在下方附上 Clash Core 的日志,log level 使用 DEBUG
|
||||||
Paste the Mihomo core log below with the log level set to `DEBUG`.
|
Paste the Clash core log below with the log level set to `DEBUG`.
|
||||||
"
|
"
|
||||||
- type: textarea
|
- type: textarea
|
||||||
attributes:
|
attributes:
|
||||||
|
|||||||
5
.github/ISSUE_TEMPLATE/config.yml
vendored
5
.github/ISSUE_TEMPLATE/config.yml
vendored
@@ -1,5 +0,0 @@
|
|||||||
blank_issues_enabled: false
|
|
||||||
contact_links:
|
|
||||||
- name: mihomo Community Support
|
|
||||||
url: https://github.com/MetaCubeX/mihomo/discussions
|
|
||||||
about: Please ask and answer questions about mihomo here.
|
|
||||||
3
.github/ISSUE_TEMPLATE/feature_request.yml
vendored
3
.github/ISSUE_TEMPLATE/feature_request.yml
vendored
@@ -1,7 +1,6 @@
|
|||||||
name: Feature request
|
name: Feature request
|
||||||
description: Suggest an idea for this project
|
description: Suggest an idea for this project
|
||||||
title: "[Feature] "
|
title: "[Feature] "
|
||||||
labels: ["enhancement"]
|
|
||||||
body:
|
body:
|
||||||
- type: checkboxes
|
- type: checkboxes
|
||||||
id: ensure
|
id: ensure
|
||||||
@@ -25,7 +24,7 @@ I have read the [documentation](https://wiki.metacubex.one/) and was unable to s
|
|||||||
- type: textarea
|
- type: textarea
|
||||||
attributes:
|
attributes:
|
||||||
label: Description
|
label: Description
|
||||||
description: 请详细、清晰地表达你要提出的论述,例如这个问题如何影响到你?你想实现什么功能?目前 Mihomo Core 的行为是什麽?
|
description: 请详细、清晰地表达你要提出的论述,例如这个问题如何影响到你?你想实现什么功能?目前 Clash Core 的行为是什麽?
|
||||||
validations:
|
validations:
|
||||||
required: true
|
required: true
|
||||||
- type: textarea
|
- type: textarea
|
||||||
|
|||||||
32
.github/genReleaseNote.sh
vendored
32
.github/genReleaseNote.sh
vendored
@@ -1,32 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
while getopts "v:" opt; do
|
|
||||||
case $opt in
|
|
||||||
v)
|
|
||||||
version_range=$OPTARG
|
|
||||||
;;
|
|
||||||
\?)
|
|
||||||
echo "Invalid option: -$OPTARG" >&2
|
|
||||||
exit 1
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
|
|
||||||
if [ -z "$version_range" ]; then
|
|
||||||
echo "Please provide the version range using -v option. Example: ./genReleashNote.sh -v v1.14.1...v1.14.2"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "## What's Changed" > release.md
|
|
||||||
git log --pretty=format:"* %h %s by @%an" --grep="^feat" -i $version_range | sort -f | uniq >> release.md
|
|
||||||
echo "" >> release.md
|
|
||||||
|
|
||||||
echo "## BUG & Fix" >> release.md
|
|
||||||
git log --pretty=format:"* %h %s by @%an" --grep="^fix" -i $version_range | sort -f | uniq >> release.md
|
|
||||||
echo "" >> release.md
|
|
||||||
|
|
||||||
echo "## Maintenance" >> release.md
|
|
||||||
git log --pretty=format:"* %h %s by @%an" --grep="^chore\|^docs\|^refactor" -i $version_range | sort -f | uniq >> release.md
|
|
||||||
echo "" >> release.md
|
|
||||||
|
|
||||||
echo "**Full Changelog**: https://github.com/MetaCubeX/mihomo/compare/$version_range" >> release.md
|
|
||||||
18
.github/release/.fpm_systemd
vendored
18
.github/release/.fpm_systemd
vendored
@@ -1,18 +0,0 @@
|
|||||||
-s dir
|
|
||||||
--name mihomo
|
|
||||||
--category net
|
|
||||||
--license GPL-3.0-or-later
|
|
||||||
--description "The universal proxy platform."
|
|
||||||
--url "https://wiki.metacubex.one/"
|
|
||||||
--maintainer "MetaCubeX <none@example.com>"
|
|
||||||
--deb-field "Bug: https://github.com/MetaCubeX/mihomo/issues"
|
|
||||||
--no-deb-generate-changes
|
|
||||||
--config-files /etc/mihomo/config.yaml
|
|
||||||
|
|
||||||
.github/release/config.yaml=/etc/mihomo/config.yaml
|
|
||||||
|
|
||||||
.github/release/mihomo.service=/usr/lib/systemd/system/mihomo.service
|
|
||||||
.github/release/mihomo@.service=/usr/lib/systemd/system/mihomo@.service
|
|
||||||
|
|
||||||
|
|
||||||
LICENSE=/usr/share/licenses/mihomo/LICENSE
|
|
||||||
15
.github/release/config.yaml
vendored
15
.github/release/config.yaml
vendored
@@ -1,15 +0,0 @@
|
|||||||
mixed-port: 7890
|
|
||||||
|
|
||||||
dns:
|
|
||||||
enable: true
|
|
||||||
ipv6: true
|
|
||||||
enhanced-mode: fake-ip
|
|
||||||
fake-ip-filter:
|
|
||||||
- "*"
|
|
||||||
- "+.lan"
|
|
||||||
- "+.local"
|
|
||||||
nameserver:
|
|
||||||
- system
|
|
||||||
|
|
||||||
rules:
|
|
||||||
- MATCH,DIRECT
|
|
||||||
17
.github/release/mihomo.service
vendored
17
.github/release/mihomo.service
vendored
@@ -1,17 +0,0 @@
|
|||||||
[Unit]
|
|
||||||
Description=mihomo Daemon, Another Clash Kernel.
|
|
||||||
Documentation=https://wiki.metacubex.one
|
|
||||||
After=network.target nss-lookup.target network-online.target
|
|
||||||
|
|
||||||
[Service]
|
|
||||||
Type=simple
|
|
||||||
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_RAW CAP_NET_BIND_SERVICE CAP_SYS_TIME CAP_SYS_PTRACE CAP_DAC_READ_SEARCH CAP_DAC_OVERRIDE
|
|
||||||
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_RAW CAP_NET_BIND_SERVICE CAP_SYS_TIME CAP_SYS_PTRACE CAP_DAC_READ_SEARCH CAP_DAC_OVERRIDE
|
|
||||||
ExecStart=/usr/bin/mihomo -d /etc/mihomo
|
|
||||||
ExecReload=/bin/kill -HUP $MAINPID
|
|
||||||
Restart=on-failure
|
|
||||||
RestartSec=10
|
|
||||||
LimitNOFILE=infinity
|
|
||||||
|
|
||||||
[Install]
|
|
||||||
WantedBy=multi-user.target
|
|
||||||
17
.github/release/mihomo@.service
vendored
17
.github/release/mihomo@.service
vendored
@@ -1,17 +0,0 @@
|
|||||||
[Unit]
|
|
||||||
Description=mihomo Daemon, Another Clash Kernel.
|
|
||||||
Documentation=https://wiki.metacubex.one
|
|
||||||
After=network.target nss-lookup.target network-online.target
|
|
||||||
|
|
||||||
[Service]
|
|
||||||
Type=simple
|
|
||||||
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_RAW CAP_NET_BIND_SERVICE CAP_SYS_TIME CAP_SYS_PTRACE CAP_DAC_READ_SEARCH CAP_DAC_OVERRIDE
|
|
||||||
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_RAW CAP_NET_BIND_SERVICE CAP_SYS_TIME CAP_SYS_PTRACE CAP_DAC_READ_SEARCH CAP_DAC_OVERRIDE
|
|
||||||
ExecStart=/usr/bin/mihomo -d /etc/mihomo
|
|
||||||
ExecReload=/bin/kill -HUP $MAINPID
|
|
||||||
Restart=on-failure
|
|
||||||
RestartSec=10
|
|
||||||
LimitNOFILE=infinity
|
|
||||||
|
|
||||||
[Install]
|
|
||||||
WantedBy=multi-user.target
|
|
||||||
26
.github/rename-cgo.sh
vendored
26
.github/rename-cgo.sh
vendored
@@ -5,25 +5,25 @@ for FILENAME in $FILENAMES
|
|||||||
do
|
do
|
||||||
if [[ $FILENAME =~ "darwin-10.16-arm64" ]];then
|
if [[ $FILENAME =~ "darwin-10.16-arm64" ]];then
|
||||||
echo "rename darwin-10.16-arm64 $FILENAME"
|
echo "rename darwin-10.16-arm64 $FILENAME"
|
||||||
mv $FILENAME mihomo-darwin-arm64-cgo
|
mv $FILENAME clash.meta-darwin-arm64-cgo
|
||||||
elif [[ $FILENAME =~ "darwin-10.16-amd64" ]];then
|
elif [[ $FILENAME =~ "darwin-10.16-amd64" ]];then
|
||||||
echo "rename darwin-10.16-amd64 $FILENAME"
|
echo "rename darwin-10.16-amd64 $FILENAME"
|
||||||
mv $FILENAME mihomo-darwin-amd64-cgo
|
mv $FILENAME clash.meta-darwin-amd64-cgo
|
||||||
elif [[ $FILENAME =~ "windows-4.0-386" ]];then
|
elif [[ $FILENAME =~ "windows-4.0-386" ]];then
|
||||||
echo "rename windows 386 $FILENAME"
|
echo "rename windows 386 $FILENAME"
|
||||||
mv $FILENAME mihomo-windows-386-cgo.exe
|
mv $FILENAME clash.meta-windows-386-cgo.exe
|
||||||
elif [[ $FILENAME =~ "windows-4.0-amd64" ]];then
|
elif [[ $FILENAME =~ "windows-4.0-amd64" ]];then
|
||||||
echo "rename windows amd64 $FILENAME"
|
echo "rename windows amd64 $FILENAME"
|
||||||
mv $FILENAME mihomo-windows-amd64-cgo.exe
|
mv $FILENAME clash.meta-windows-amd64-cgo.exe
|
||||||
elif [[ $FILENAME =~ "mihomo-linux-arm-5" ]];then
|
elif [[ $FILENAME =~ "clash.meta-linux-arm-5" ]];then
|
||||||
echo "rename mihomo-linux-arm-5 $FILENAME"
|
echo "rename clash.meta-linux-arm-5 $FILENAME"
|
||||||
mv $FILENAME mihomo-linux-armv5-cgo
|
mv $FILENAME clash.meta-linux-armv5-cgo
|
||||||
elif [[ $FILENAME =~ "mihomo-linux-arm-6" ]];then
|
elif [[ $FILENAME =~ "clash.meta-linux-arm-6" ]];then
|
||||||
echo "rename mihomo-linux-arm-6 $FILENAME"
|
echo "rename clash.meta-linux-arm-6 $FILENAME"
|
||||||
mv $FILENAME mihomo-linux-armv6-cgo
|
mv $FILENAME clash.meta-linux-armv6-cgo
|
||||||
elif [[ $FILENAME =~ "mihomo-linux-arm-7" ]];then
|
elif [[ $FILENAME =~ "clash.meta-linux-arm-7" ]];then
|
||||||
echo "rename mihomo-linux-arm-7 $FILENAME"
|
echo "rename clash.meta-linux-arm-7 $FILENAME"
|
||||||
mv $FILENAME mihomo-linux-armv7-cgo
|
mv $FILENAME clash.meta-linux-armv7-cgo
|
||||||
elif [[ $FILENAME =~ "linux" ]];then
|
elif [[ $FILENAME =~ "linux" ]];then
|
||||||
echo "rename linux $FILENAME"
|
echo "rename linux $FILENAME"
|
||||||
mv $FILENAME $FILENAME-cgo
|
mv $FILENAME $FILENAME-cgo
|
||||||
|
|||||||
12
.github/rename-go120.sh
vendored
12
.github/rename-go120.sh
vendored
@@ -1,12 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
FILENAMES=$(ls)
|
|
||||||
for FILENAME in $FILENAMES
|
|
||||||
do
|
|
||||||
if [[ ! ($FILENAME =~ ".exe" || $FILENAME =~ ".sh")]];then
|
|
||||||
mv $FILENAME ${FILENAME}-go120
|
|
||||||
elif [[ $FILENAME =~ ".exe" ]];then
|
|
||||||
mv $FILENAME ${FILENAME%.*}-go120.exe
|
|
||||||
else echo "skip $FILENAME"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
51
.github/workflows/android-branch-auto-sync.yml
vendored
Normal file
51
.github/workflows/android-branch-auto-sync.yml
vendored
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
name: Android Branch Auto Sync
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
push:
|
||||||
|
paths-ignore:
|
||||||
|
- "docs/**"
|
||||||
|
- "README.md"
|
||||||
|
- ".github/ISSUE_TEMPLATE/**"
|
||||||
|
branches:
|
||||||
|
- Alpha
|
||||||
|
- android-open
|
||||||
|
tags:
|
||||||
|
- "v*"
|
||||||
|
pull_request_target:
|
||||||
|
branches:
|
||||||
|
- Alpha
|
||||||
|
- android-open
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
update-dependencies:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout Repository
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Configure Git
|
||||||
|
run: |
|
||||||
|
git config --global user.name 'GitHub Action'
|
||||||
|
git config --global user.email 'action@github.com'
|
||||||
|
|
||||||
|
- name: Sync android-real with Alpha rebase android-open
|
||||||
|
run: |
|
||||||
|
git fetch origin
|
||||||
|
git checkout origin/Alpha -b android-real
|
||||||
|
git merge --squash origin/android-open
|
||||||
|
git commit -m "Android: patch"
|
||||||
|
|
||||||
|
- name: Check for conflicts
|
||||||
|
run: |
|
||||||
|
CONFLICTS=$(git diff --name-only --diff-filter=U)
|
||||||
|
if [ ! -z "$CONFLICTS" ]; then
|
||||||
|
echo "There are conflicts in the following files:"
|
||||||
|
echo $CONFLICTS
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Push changes
|
||||||
|
run: |
|
||||||
|
git push origin android-real --force
|
||||||
679
.github/workflows/build.yml
vendored
679
.github/workflows/build.yml
vendored
@@ -1,10 +1,6 @@
|
|||||||
name: Build
|
name: Build
|
||||||
on:
|
on:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
inputs:
|
|
||||||
version:
|
|
||||||
description: "Tag version to release"
|
|
||||||
required: true
|
|
||||||
push:
|
push:
|
||||||
paths-ignore:
|
paths-ignore:
|
||||||
- "docs/**"
|
- "docs/**"
|
||||||
@@ -14,483 +10,322 @@ on:
|
|||||||
- Alpha
|
- Alpha
|
||||||
tags:
|
tags:
|
||||||
- "v*"
|
- "v*"
|
||||||
pull_request:
|
pull_request_target:
|
||||||
branches:
|
branches:
|
||||||
- Alpha
|
- Alpha
|
||||||
|
|
||||||
concurrency:
|
concurrency:
|
||||||
group: "${{ github.workflow }}-${{ github.ref }}"
|
group: ${{ github.ref }}-${{ github.workflow }}
|
||||||
cancel-in-progress: true
|
cancel-in-progress: true
|
||||||
|
|
||||||
env:
|
env:
|
||||||
REGISTRY: docker.io
|
REGISTRY: docker.io
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
Build:
|
||||||
|
permissions: write-all
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
strategy:
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
jobs:
|
job:
|
||||||
- { goos: darwin, goarch: arm64, output: arm64 }
|
- {
|
||||||
- { goos: darwin, goarch: amd64, goamd64: v1, output: amd64-compatible }
|
type: "WithoutCGO",
|
||||||
- { goos: darwin, goarch: amd64, goamd64: v3, output: amd64 }
|
target: "linux-amd64 linux-amd64-compatible",
|
||||||
|
id: "1",
|
||||||
- { goos: linux, goarch: '386', go386: sse2, output: '386', debian: i386, rpm: i386}
|
}
|
||||||
- { goos: linux, goarch: '386', go386: softfloat, output: '386-softfloat' }
|
- {
|
||||||
- { goos: linux, goarch: amd64, goamd64: v1, output: amd64-compatible, test: test }
|
type: "WithoutCGO",
|
||||||
- { goos: linux, goarch: amd64, goamd64: v3, output: amd64, debian: amd64, rpm: x86_64, pacman: x86_64}
|
target: "linux-armv5 linux-armv6 linux-armv7",
|
||||||
- { goos: linux, goarch: arm64, output: arm64, debian: arm64, rpm: aarch64, pacman: aarch64}
|
id: "2",
|
||||||
- { goos: linux, goarch: arm, goarm: '5', output: armv5 }
|
}
|
||||||
- { goos: linux, goarch: arm, goarm: '6', output: armv6, debian: armel, rpm: armv6hl}
|
- {
|
||||||
- { goos: linux, goarch: arm, goarm: '7', output: armv7, debian: armhf, rpm: armv7hl, pacman: armv7hl}
|
type: "WithoutCGO",
|
||||||
- { goos: linux, goarch: mips, gomips: hardfloat, output: mips-hardfloat }
|
target: "linux-arm64 linux-mips64 linux-mips64le",
|
||||||
- { goos: linux, goarch: mips, gomips: softfloat, output: mips-softfloat }
|
id: "3",
|
||||||
- { goos: linux, goarch: mipsle, gomips: hardfloat, output: mipsle-hardfloat }
|
}
|
||||||
- { goos: linux, goarch: mipsle, gomips: softfloat, output: mipsle-softfloat }
|
- {
|
||||||
- { goos: linux, goarch: mips64, output: mips64 }
|
type: "WithoutCGO",
|
||||||
- { goos: linux, goarch: mips64le, output: mips64le, debian: mips64el, rpm: mips64el }
|
target: "linux-mips-softfloat linux-mips-hardfloat linux-mipsle-softfloat linux-mipsle-hardfloat",
|
||||||
- { goos: linux, goarch: loong64, output: loong64-abi1, abi: '1', debian: loongarch64, rpm: loongarch64 }
|
id: "4",
|
||||||
- { goos: linux, goarch: loong64, output: loong64-abi2, abi: '2', debian: loong64, rpm: loong64 }
|
}
|
||||||
- { goos: linux, goarch: riscv64, output: riscv64, debian: riscv64, rpm: riscv64 }
|
- { type: "WithoutCGO", target: "linux-386 linux-riscv64", id: "5" }
|
||||||
- { goos: linux, goarch: s390x, output: s390x, debian: s390x, rpm: s390x }
|
- {
|
||||||
- { goos: linux, goarch: ppc64le, output: ppc64le, debian: ppc64el, rpm: ppc64le }
|
type: "WithoutCGO",
|
||||||
|
target: "freebsd-386 freebsd-amd64 freebsd-arm64",
|
||||||
- { goos: windows, goarch: '386', output: '386' }
|
id: "6",
|
||||||
- { goos: windows, goarch: amd64, goamd64: v1, output: amd64-compatible }
|
}
|
||||||
- { goos: windows, goarch: amd64, goamd64: v3, output: amd64 }
|
- {
|
||||||
- { goos: windows, goarch: arm64, output: arm64 }
|
type: "WithoutCGO",
|
||||||
|
target: "windows-amd64-compatible windows-amd64 windows-386",
|
||||||
- { goos: freebsd, goarch: '386', output: '386' }
|
id: "7",
|
||||||
- { goos: freebsd, goarch: amd64, goamd64: v1, output: amd64-compatible }
|
}
|
||||||
- { goos: freebsd, goarch: amd64, goamd64: v3, output: amd64 }
|
- {
|
||||||
- { goos: freebsd, goarch: arm64, output: arm64 }
|
type: "WithoutCGO",
|
||||||
|
target: "windows-arm64 windows-arm32v7",
|
||||||
- { goos: android, goarch: '386', ndk: i686-linux-android34, output: '386' }
|
id: "8",
|
||||||
- { goos: android, goarch: amd64, ndk: x86_64-linux-android34, output: amd64 }
|
}
|
||||||
- { goos: android, goarch: arm, ndk: armv7a-linux-androideabi34, output: armv7 }
|
- {
|
||||||
- { goos: android, goarch: arm64, ndk: aarch64-linux-android34, output: arm64-v8 }
|
type: "WithoutCGO",
|
||||||
|
target: "darwin-amd64 darwin-arm64 android-arm64",
|
||||||
# Go 1.23 with special patch can work on Windows 7
|
id: "9",
|
||||||
# https://github.com/MetaCubeX/go/commits/release-branch.go1.23/
|
}
|
||||||
- { goos: windows, goarch: '386', output: '386-go123', goversion: '1.23' }
|
- { type: "WithCGO", target: "windows/*", id: "1" }
|
||||||
- { goos: windows, goarch: amd64, goamd64: v1, output: amd64-compatible-go123, goversion: '1.23' }
|
- { type: "WithCGO", target: "linux/386", id: "2" }
|
||||||
- { goos: windows, goarch: amd64, goamd64: v3, output: amd64-go123, goversion: '1.23' }
|
- { type: "WithCGO", target: "linux/amd64", id: "3" }
|
||||||
|
- { type: "WithCGO", target: "linux/arm64,linux/riscv64", id: "4" }
|
||||||
# Go 1.22 with special patch can work on Windows 7
|
- { type: "WithCGO", target: "linux/arm,", id: "5" }
|
||||||
# https://github.com/MetaCubeX/go/commits/release-branch.go1.22/
|
- { type: "WithCGO", target: "linux/arm-6,linux/arm-7", id: "6" }
|
||||||
- { goos: windows, goarch: '386', output: '386-go122', goversion: '1.22' }
|
- { type: "WithCGO", target: "linux/mips,linux/mipsle", id: "7" }
|
||||||
- { goos: windows, goarch: amd64, goamd64: v1, output: amd64-compatible-go122, goversion: '1.22' }
|
- { type: "WithCGO", target: "linux/mips64", id: "8" }
|
||||||
- { goos: windows, goarch: amd64, goamd64: v3, output: amd64-go122, goversion: '1.22' }
|
- { type: "WithCGO", target: "linux/mips64le", id: "9" }
|
||||||
|
- { type: "WithCGO", target: "darwin-10.16/*", id: "10" }
|
||||||
# Go 1.21 can revert commit `9e4385` to work on Windows 7
|
- { type: "WithCGO", target: "android", id: "11" }
|
||||||
# https://github.com/golang/go/issues/64622#issuecomment-1847475161
|
|
||||||
# (OR we can just use golang1.21.4 which unneeded any patch)
|
|
||||||
- { goos: windows, goarch: '386', output: '386-go121', goversion: '1.21' }
|
|
||||||
- { goos: windows, goarch: amd64, goamd64: v1, output: amd64-compatible-go121, goversion: '1.21' }
|
|
||||||
- { goos: windows, goarch: amd64, goamd64: v3, output: amd64-go121, goversion: '1.21' }
|
|
||||||
|
|
||||||
# Go 1.20 is the last release that will run on any release of Windows 7, 8, Server 2008 and Server 2012. Go 1.21 will require at least Windows 10 or Server 2016.
|
|
||||||
- { goos: windows, goarch: '386', output: '386-go120', goversion: '1.20' }
|
|
||||||
- { goos: windows, goarch: amd64, goamd64: v1, output: amd64-compatible-go120, goversion: '1.20' }
|
|
||||||
- { goos: windows, goarch: amd64, goamd64: v3, output: amd64-go120, goversion: '1.20' }
|
|
||||||
|
|
||||||
# Go 1.22 is the last release that will run on macOS 10.15 Catalina. Go 1.23 will require macOS 11 Big Sur or later.
|
|
||||||
- { goos: darwin, goarch: arm64, output: arm64-go122, goversion: '1.22' }
|
|
||||||
- { goos: darwin, goarch: amd64, goamd64: v1, output: amd64-compatible-go122, goversion: '1.22' }
|
|
||||||
- { goos: darwin, goarch: amd64, goamd64: v3, output: amd64-go122, goversion: '1.22' }
|
|
||||||
|
|
||||||
# Go 1.20 is the last release that will run on macOS 10.13 High Sierra or 10.14 Mojave. Go 1.21 will require macOS 10.15 Catalina or later.
|
|
||||||
- { goos: darwin, goarch: arm64, output: arm64-go120, goversion: '1.20' }
|
|
||||||
- { goos: darwin, goarch: amd64, goamd64: v1, output: amd64-compatible-go120, goversion: '1.20' }
|
|
||||||
- { goos: darwin, goarch: amd64, goamd64: v3, output: amd64-go120, goversion: '1.20' }
|
|
||||||
|
|
||||||
# Go 1.23 is the last release that requires Linux kernel version 2.6.32 or later. Go 1.24 will require Linux kernel version 3.2 or later.
|
|
||||||
- { goos: linux, goarch: '386', output: '386-go123', goversion: '1.23' }
|
|
||||||
- { goos: linux, goarch: amd64, goamd64: v1, output: amd64-compatible-go123, goversion: '1.23', test: test }
|
|
||||||
- { goos: linux, goarch: amd64, goamd64: v3, output: amd64-go123, goversion: '1.23' }
|
|
||||||
|
|
||||||
# only for test
|
|
||||||
- { goos: linux, goarch: '386', output: '386-go120', goversion: '1.20' }
|
|
||||||
- { goos: linux, goarch: amd64, goamd64: v1, output: amd64-compatible-go120, goversion: '1.20', test: test }
|
|
||||||
- { goos: linux, goarch: amd64, goamd64: v3, output: amd64-go120, goversion: '1.20' }
|
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- name: Check out code into the Go module directory
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
- name: Set up Go
|
- name: Set variables
|
||||||
if: ${{ matrix.jobs.goversion == '' && matrix.jobs.abi != '1' }}
|
run: echo "VERSION=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
|
||||||
uses: actions/setup-go@v5
|
shell: bash
|
||||||
with:
|
|
||||||
go-version: '1.24'
|
|
||||||
|
|
||||||
- name: Set up Go
|
- name: Set variables
|
||||||
if: ${{ matrix.jobs.goversion != '' && matrix.jobs.abi != '1' }}
|
if: ${{github.ref_name=='Alpha'}}
|
||||||
uses: actions/setup-go@v5
|
run: echo "VERSION=alpha-$(git rev-parse --short HEAD)" >> $GITHUB_ENV
|
||||||
with:
|
shell: bash
|
||||||
go-version: ${{ matrix.jobs.goversion }}
|
|
||||||
|
|
||||||
- name: Set up Go1.24 loongarch abi1
|
- name: Set variables
|
||||||
if: ${{ matrix.jobs.goarch == 'loong64' && matrix.jobs.abi == '1' }}
|
if: ${{github.ref_name=='Beta'}}
|
||||||
run: |
|
run: echo "VERSION=beta-$(git rev-parse --short HEAD)" >> $GITHUB_ENV
|
||||||
wget -q https://github.com/MetaCubeX/loongarch64-golang/releases/download/1.24.0/go1.24.0.linux-amd64-abi1.tar.gz
|
shell: bash
|
||||||
sudo tar zxf go1.24.0.linux-amd64-abi1.tar.gz -C /usr/local
|
|
||||||
echo "/usr/local/go/bin" >> $GITHUB_PATH
|
|
||||||
|
|
||||||
# modify from https://github.com/restic/restic/issues/4636#issuecomment-1896455557
|
- name: Set variables
|
||||||
# this patch file only works on golang1.24.x
|
if: ${{github.ref_name=='Meta'}}
|
||||||
# that means after golang1.25 release it must be changed
|
run: echo "VERSION=meta-$(git rev-parse --short HEAD)" >> $GITHUB_ENV
|
||||||
# see: https://github.com/MetaCubeX/go/commits/release-branch.go1.24/
|
shell: bash
|
||||||
# revert:
|
|
||||||
# 693def151adff1af707d82d28f55dba81ceb08e1: "crypto/rand,runtime: switch RtlGenRandom for ProcessPrng"
|
|
||||||
# 7c1157f9544922e96945196b47b95664b1e39108: "net: remove sysSocket fallback for Windows 7"
|
|
||||||
# 48042aa09c2f878c4faa576948b07fe625c4707a: "syscall: remove Windows 7 console handle workaround"
|
|
||||||
# a17d959debdb04cd550016a3501dd09d50cd62e7: "runtime: always use LoadLibraryEx to load system libraries"
|
|
||||||
- name: Revert Golang1.24 commit for Windows7/8
|
|
||||||
if: ${{ matrix.jobs.goos == 'windows' && matrix.jobs.goversion == '' }}
|
|
||||||
run: |
|
|
||||||
cd $(go env GOROOT)
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/2a406dc9f1ea7323d6ca9fccb2fe9ddebb6b1cc8.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/7b1fd7d39c6be0185fbe1d929578ab372ac5c632.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/979d6d8bab3823ff572ace26767fd2ce3cf351ae.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/ac3e93c061779dfefc0dd13a5b6e6f764a25621e.diff | patch --verbose -p 1
|
|
||||||
|
|
||||||
# modify from https://github.com/restic/restic/issues/4636#issuecomment-1896455557
|
- name: Set variables
|
||||||
# this patch file only works on golang1.23.x
|
if: ${{github.ref_name=='' || github.ref_type=='tag'}}
|
||||||
# that means after golang1.24 release it must be changed
|
run: echo "VERSION=$(git describe --tags)" >> $GITHUB_ENV
|
||||||
# see: https://github.com/MetaCubeX/go/commits/release-branch.go1.23/
|
shell: bash
|
||||||
# revert:
|
|
||||||
# 693def151adff1af707d82d28f55dba81ceb08e1: "crypto/rand,runtime: switch RtlGenRandom for ProcessPrng"
|
|
||||||
# 7c1157f9544922e96945196b47b95664b1e39108: "net: remove sysSocket fallback for Windows 7"
|
|
||||||
# 48042aa09c2f878c4faa576948b07fe625c4707a: "syscall: remove Windows 7 console handle workaround"
|
|
||||||
# a17d959debdb04cd550016a3501dd09d50cd62e7: "runtime: always use LoadLibraryEx to load system libraries"
|
|
||||||
- name: Revert Golang1.23 commit for Windows7/8
|
|
||||||
if: ${{ matrix.jobs.goos == 'windows' && matrix.jobs.goversion == '1.23' }}
|
|
||||||
run: |
|
|
||||||
cd $(go env GOROOT)
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/9ac42137ef6730e8b7daca016ece831297a1d75b.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/21290de8a4c91408de7c2b5b68757b1e90af49dd.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/6a31d3fa8e47ddabc10bd97bff10d9a85f4cfb76.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/69e2eed6dd0f6d815ebf15797761c13f31213dd6.diff | patch --verbose -p 1
|
|
||||||
|
|
||||||
# modify from https://github.com/restic/restic/issues/4636#issuecomment-1896455557
|
- name: Set ENV
|
||||||
# this patch file only works on golang1.22.x
|
run: |
|
||||||
# that means after golang1.23 release it must be changed
|
sudo timedatectl set-timezone "Asia/Shanghai"
|
||||||
# see: https://github.com/MetaCubeX/go/commits/release-branch.go1.22/
|
echo "NAME=clash.meta" >> $GITHUB_ENV
|
||||||
# revert:
|
echo "REPO=${{ github.repository }}" >> $GITHUB_ENV
|
||||||
# 693def151adff1af707d82d28f55dba81ceb08e1: "crypto/rand,runtime: switch RtlGenRandom for ProcessPrng"
|
echo "ShortSHA=$(git rev-parse --short ${{ github.sha }})" >> $GITHUB_ENV
|
||||||
# 7c1157f9544922e96945196b47b95664b1e39108: "net: remove sysSocket fallback for Windows 7"
|
echo "BUILDTIME=$(date)" >> $GITHUB_ENV
|
||||||
# 48042aa09c2f878c4faa576948b07fe625c4707a: "syscall: remove Windows 7 console handle workaround"
|
echo "BRANCH=$(git rev-parse --abbrev-ref HEAD)" >> $GITHUB_ENV
|
||||||
# a17d959debdb04cd550016a3501dd09d50cd62e7: "runtime: always use LoadLibraryEx to load system libraries"
|
shell: bash
|
||||||
- name: Revert Golang1.22 commit for Windows7/8
|
|
||||||
if: ${{ matrix.jobs.goos == 'windows' && matrix.jobs.goversion == '1.22' }}
|
|
||||||
run: |
|
|
||||||
cd $(go env GOROOT)
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/9779155f18b6556a034f7bb79fb7fb2aad1e26a9.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/ef0606261340e608017860b423ffae5c1ce78239.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/7f83badcb925a7e743188041cb6e561fc9b5b642.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/83ff9782e024cb328b690cbf0da4e7848a327f4f.diff | patch --verbose -p 1
|
|
||||||
|
|
||||||
# modify from https://github.com/restic/restic/issues/4636#issuecomment-1896455557
|
- name: Set ENV
|
||||||
- name: Revert Golang1.21 commit for Windows7/8
|
run: |
|
||||||
if: ${{ matrix.jobs.goos == 'windows' && matrix.jobs.goversion == '1.21' }}
|
echo "TAGS=with_gvisor,with_lwip" >> $GITHUB_ENV
|
||||||
run: |
|
echo "LDFLAGS=-X 'github.com/Dreamacro/clash/constant.Version=${VERSION}' -X 'github.com/Dreamacro/clash/constant.BuildTime=${BUILDTIME}' -w -s -buildid=" >> $GITHUB_ENV
|
||||||
cd $(go env GOROOT)
|
shell: bash
|
||||||
curl https://github.com/golang/go/commit/9e43850a3298a9b8b1162ba0033d4c53f8637571.diff | patch --verbose -R -p 1
|
|
||||||
|
|
||||||
- name: Set variables
|
- name: Setup Go
|
||||||
run: |
|
uses: actions/setup-go@v4
|
||||||
VERSION="${GITHUB_REF_NAME,,}-$(git rev-parse --short HEAD)"
|
with:
|
||||||
PackageVersion="$(curl -s "https://api.github.com/repos/MetaCubeX/mihomo/releases/latest" | jq -r '.tag_name' | sed 's/v//g' | awk -F '.' '{$NF = $NF + 1; print}' OFS='.').${VERSION/-/.}"
|
go-version: "1.21"
|
||||||
if [ -n "${{ github.event.inputs.version }}" ]; then
|
check-latest: true
|
||||||
VERSION=${{ github.event.inputs.version }}
|
|
||||||
PackageVersion="${VERSION#v}" >> $GITHUB_ENV
|
|
||||||
fi
|
|
||||||
echo "VERSION=${VERSION}" >> $GITHUB_ENV
|
|
||||||
echo "PackageVersion=${PackageVersion}" >> $GITHUB_ENV
|
|
||||||
|
|
||||||
echo "BUILDTIME=$(date)" >> $GITHUB_ENV
|
|
||||||
echo "CGO_ENABLED=0" >> $GITHUB_ENV
|
|
||||||
echo "BUILDTAG=-extldflags --static" >> $GITHUB_ENV
|
|
||||||
echo "GOTOOLCHAIN=local" >> $GITHUB_ENV
|
|
||||||
|
|
||||||
- name: Setup NDK
|
- name: Test
|
||||||
if: ${{ matrix.jobs.goos == 'android' }}
|
if: ${{ matrix.job.id=='1' && matrix.job.type=='WithoutCGO' }}
|
||||||
uses: nttld/setup-ndk@v1
|
run: |
|
||||||
id: setup-ndk
|
go test ./...
|
||||||
with:
|
|
||||||
ndk-version: r29-beta1
|
|
||||||
|
|
||||||
- name: Set NDK path
|
- name: Build WithoutCGO
|
||||||
if: ${{ matrix.jobs.goos == 'android' }}
|
if: ${{ matrix.job.type=='WithoutCGO' }}
|
||||||
run: |
|
env:
|
||||||
echo "CC=${{steps.setup-ndk.outputs.ndk-path}}/toolchains/llvm/prebuilt/linux-x86_64/bin/${{matrix.jobs.ndk}}-clang" >> $GITHUB_ENV
|
NAME: Clash.Meta
|
||||||
echo "CGO_ENABLED=1" >> $GITHUB_ENV
|
BINDIR: bin
|
||||||
echo "BUILDTAG=" >> $GITHUB_ENV
|
run: make -j$(($(nproc) + 1)) ${{ matrix.job.target }}
|
||||||
|
|
||||||
- name: Test
|
- uses: nttld/setup-ndk@v1
|
||||||
if: ${{ matrix.jobs.test == 'test' }}
|
if: ${{ matrix.job.type=='WithCGO' && matrix.job.target=='android' }}
|
||||||
run: |
|
id: setup-ndk
|
||||||
go test ./...
|
with:
|
||||||
echo "---test with_gvisor---"
|
ndk-version: r26
|
||||||
go test ./... -tags "with_gvisor" -count=1
|
add-to-path: false
|
||||||
|
local-cache: true
|
||||||
|
|
||||||
- name: Update CA
|
- name: Build Android
|
||||||
run: |
|
if: ${{ matrix.job.type=='WithCGO' && matrix.job.target=='android' }}
|
||||||
sudo apt-get update && sudo apt-get install ca-certificates
|
env:
|
||||||
sudo update-ca-certificates
|
ANDROID_NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }}
|
||||||
cp -f /etc/ssl/certs/ca-certificates.crt component/ca/ca-certificates.crt
|
run: |
|
||||||
|
mkdir bin
|
||||||
|
CC=${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android33-clang
|
||||||
|
CGO_ENABLED=1 CC=${CC} GOARCH=arm64 GOOS=android go build -tags ${TAGS} -trimpath -ldflags "${LDFLAGS}" -o bin/${NAME}-android-arm64
|
||||||
|
|
||||||
- name: Build core
|
- name: Set up xgo
|
||||||
env:
|
if: ${{ matrix.job.type=='WithCGO' && matrix.job.target!='android' }}
|
||||||
GOOS: ${{matrix.jobs.goos}}
|
run: |
|
||||||
GOARCH: ${{matrix.jobs.goarch}}
|
docker pull techknowlogick/xgo:latest
|
||||||
GOAMD64: ${{matrix.jobs.goamd64}}
|
go install src.techknowlogick.com/xgo@latest
|
||||||
GO386: ${{matrix.jobs.go386}}
|
|
||||||
GOARM: ${{matrix.jobs.goarm}}
|
|
||||||
GOMIPS: ${{matrix.jobs.gomips}}
|
|
||||||
run: |
|
|
||||||
go env
|
|
||||||
go build -v -tags "with_gvisor" -trimpath -ldflags "${BUILDTAG} -X 'github.com/metacubex/mihomo/constant.Version=${VERSION}' -X 'github.com/metacubex/mihomo/constant.BuildTime=${BUILDTIME}' -w -s -buildid="
|
|
||||||
if [ "${{matrix.jobs.goos}}" = "windows" ]; then
|
|
||||||
cp mihomo.exe mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}.exe
|
|
||||||
zip -r mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}-${VERSION}.zip mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}.exe
|
|
||||||
else
|
|
||||||
cp mihomo mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}
|
|
||||||
gzip -c mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}} > mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}-${VERSION}.gz
|
|
||||||
rm mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: Package DEB
|
- name: Build by xgo
|
||||||
if: matrix.jobs.debian != ''
|
if: ${{ matrix.job.type=='WithCGO' && matrix.job.target!='android' }}
|
||||||
run: |
|
env:
|
||||||
set -xeuo pipefail
|
ANDROID_NDK_HOME: ${{ steps.setup-ndk.outputs.ndk-path }}
|
||||||
sudo gem install fpm
|
run: |
|
||||||
cp .github/release/.fpm_systemd .fpm
|
mkdir bin
|
||||||
|
xgo --targets="${{ matrix.job.target }}" --tags="${TAGS}" -ldflags="${LDFLAGS}" --out bin/${NAME} ./
|
||||||
|
|
||||||
fpm -t deb \
|
- name: Rename
|
||||||
-v "${PackageVersion}" \
|
if: ${{ matrix.job.type=='WithCGO' }}
|
||||||
-p "mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}-${VERSION}.deb" \
|
run: |
|
||||||
--architecture ${{ matrix.jobs.debian }} \
|
cd bin
|
||||||
mihomo=/usr/bin/mihomo
|
ls -la
|
||||||
|
cp ../.github/rename-cgo.sh ./
|
||||||
|
bash ./rename-cgo.sh
|
||||||
|
rm ./rename-cgo.sh
|
||||||
|
ls -la
|
||||||
|
cd ..
|
||||||
|
|
||||||
- name: Package RPM
|
- name: Zip
|
||||||
if: matrix.jobs.rpm != ''
|
if: ${{ success() }}
|
||||||
run: |
|
run: |
|
||||||
set -xeuo pipefail
|
cd bin
|
||||||
sudo gem install fpm
|
ls -la
|
||||||
cp .github/release/.fpm_systemd .fpm
|
chmod +x *
|
||||||
|
cp ../.github/release.sh ./
|
||||||
|
bash ./release.sh
|
||||||
|
rm ./release.sh
|
||||||
|
ls -la
|
||||||
|
cd ..
|
||||||
|
|
||||||
fpm -t rpm \
|
- name: Save version
|
||||||
-v "${PackageVersion}" \
|
run: echo ${VERSION} > bin/version.txt
|
||||||
-p "mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}-${VERSION}.rpm" \
|
shell: bash
|
||||||
--architecture ${{ matrix.jobs.rpm }} \
|
|
||||||
mihomo=/usr/bin/mihomo
|
|
||||||
|
|
||||||
- name: Package Pacman
|
- uses: actions/upload-artifact@v3
|
||||||
if: matrix.jobs.pacman != ''
|
if: ${{ success() }}
|
||||||
run: |
|
with:
|
||||||
set -xeuo pipefail
|
name: artifact
|
||||||
sudo gem install fpm
|
path: bin/
|
||||||
sudo apt-get update && sudo apt-get install -y libarchive-tools
|
|
||||||
cp .github/release/.fpm_systemd .fpm
|
|
||||||
|
|
||||||
fpm -t pacman \
|
|
||||||
-v "${PackageVersion}" \
|
|
||||||
-p "mihomo-${{matrix.jobs.goos}}-${{matrix.jobs.output}}-${VERSION}.pkg.tar.zst" \
|
|
||||||
--architecture ${{ matrix.jobs.pacman }} \
|
|
||||||
mihomo=/usr/bin/mihomo
|
|
||||||
|
|
||||||
- name: Save version
|
|
||||||
run: |
|
|
||||||
echo ${VERSION} > version.txt
|
|
||||||
shell: bash
|
|
||||||
|
|
||||||
- name: Archive production artifacts
|
|
||||||
uses: actions/upload-artifact@v4
|
|
||||||
with:
|
|
||||||
name: "${{ matrix.jobs.goos }}-${{ matrix.jobs.output }}"
|
|
||||||
path: |
|
|
||||||
mihomo*.gz
|
|
||||||
mihomo*.deb
|
|
||||||
mihomo*.rpm
|
|
||||||
mihomo*.pkg.tar.zst
|
|
||||||
mihomo*.zip
|
|
||||||
version.txt
|
|
||||||
checksums.txt
|
|
||||||
|
|
||||||
Upload-Prerelease:
|
Upload-Prerelease:
|
||||||
permissions: write-all
|
permissions: write-all
|
||||||
if: ${{ github.event_name != 'workflow_dispatch' && github.ref_type == 'branch' && !startsWith(github.event_name, 'pull_request') }}
|
if: ${{ github.ref_type=='branch' && github.event_name != 'pull_request' }}
|
||||||
needs: [build]
|
needs: [Build]
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Download all workflow run artifacts
|
- uses: actions/download-artifact@v3
|
||||||
uses: actions/download-artifact@v4
|
|
||||||
with:
|
|
||||||
path: bin/
|
|
||||||
merge-multiple: true
|
|
||||||
|
|
||||||
- name: Calculate checksums
|
|
||||||
run: |
|
|
||||||
cd bin/
|
|
||||||
find . -type f -not -name "checksums.*" -not -name "version.txt" | sort | xargs sha256sum > checksums.txt
|
|
||||||
cat checksums.txt
|
|
||||||
shell: bash
|
|
||||||
|
|
||||||
- name: Delete current release assets
|
|
||||||
uses: 8Mi-Tech/delete-release-assets-action@main
|
|
||||||
with:
|
|
||||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
tag: Prerelease-${{ github.ref_name }}
|
|
||||||
deleteOnlyFromDrafts: false
|
|
||||||
- name: Set Env
|
|
||||||
run: |
|
|
||||||
echo "BUILDTIME=$(TZ=Asia/Shanghai date)" >> $GITHUB_ENV
|
|
||||||
shell: bash
|
|
||||||
|
|
||||||
- name: Tag Repo
|
|
||||||
uses: richardsimko/update-tag@v1
|
|
||||||
with:
|
|
||||||
tag_name: Prerelease-${{ github.ref_name }}
|
|
||||||
env:
|
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- run: |
|
|
||||||
cat > release.txt << 'EOF'
|
|
||||||
Release created at ${{ env.BUILDTIME }}
|
|
||||||
Synchronize ${{ github.ref_name }} branch code updates, keeping only the latest version
|
|
||||||
<br>
|
|
||||||
[我应该下载哪个文件? / Which file should I download?](https://github.com/MetaCubeX/mihomo/wiki/FAQ)
|
|
||||||
[二进制文件筛选 / Binary file selector](https://metacubex.github.io/Meta-Docs/startup/#_1)
|
|
||||||
[查看文档 / Docs](https://metacubex.github.io/Meta-Docs/)
|
|
||||||
EOF
|
|
||||||
|
|
||||||
- name: Upload Prerelease
|
|
||||||
uses: softprops/action-gh-release@v1
|
|
||||||
if: ${{ success() }}
|
|
||||||
with:
|
|
||||||
tag_name: Prerelease-${{ github.ref_name }}
|
|
||||||
files: |
|
|
||||||
bin/*
|
|
||||||
prerelease: true
|
|
||||||
generate_release_notes: true
|
|
||||||
body_path: release.txt
|
|
||||||
|
|
||||||
Upload-Release:
|
|
||||||
permissions: write-all
|
|
||||||
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.version != '' }}
|
|
||||||
needs: [build]
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
ref: Meta
|
|
||||||
fetch-depth: '0'
|
|
||||||
fetch-tags: 'true'
|
|
||||||
|
|
||||||
- name: Get tags
|
|
||||||
run: |
|
|
||||||
echo "CURRENTVERSION=${{ github.event.inputs.version }}" >> $GITHUB_ENV
|
|
||||||
git fetch --tags
|
|
||||||
echo "PREVERSION=$(git describe --tags --abbrev=0 HEAD)" >> $GITHUB_ENV
|
|
||||||
|
|
||||||
- name: Force push Alpha branch to Meta
|
|
||||||
run: |
|
|
||||||
git config --global user.email "github-actions[bot]@users.noreply.github.com"
|
|
||||||
git config --global user.name "github-actions[bot]"
|
|
||||||
git fetch origin Alpha:Alpha
|
|
||||||
git push origin Alpha:Meta --force
|
|
||||||
env:
|
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Tag the commit on Alpha
|
|
||||||
run: |
|
|
||||||
git checkout Alpha
|
|
||||||
git tag ${{ github.event.inputs.version }}
|
|
||||||
git push origin ${{ github.event.inputs.version }}
|
|
||||||
env:
|
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Generate release notes
|
|
||||||
run: |
|
|
||||||
cp ./.github/genReleaseNote.sh ./
|
|
||||||
bash ./genReleaseNote.sh -v ${PREVERSION}...${CURRENTVERSION}
|
|
||||||
rm ./genReleaseNote.sh
|
|
||||||
|
|
||||||
- uses: actions/download-artifact@v4
|
|
||||||
with:
|
with:
|
||||||
|
name: artifact
|
||||||
path: bin/
|
path: bin/
|
||||||
merge-multiple: true
|
|
||||||
|
|
||||||
- name: Display structure of downloaded files
|
- name: Display structure of downloaded files
|
||||||
run: ls -R
|
run: ls -R
|
||||||
working-directory: bin
|
working-directory: bin
|
||||||
|
|
||||||
- name: Upload Release
|
- name: Delete current release assets
|
||||||
uses: softprops/action-gh-release@v2
|
uses: 8Mi-Tech/delete-release-assets-action@main
|
||||||
if: ${{ success() }}
|
|
||||||
with:
|
with:
|
||||||
tag_name: ${{ github.event.inputs.version }}
|
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
tag: Prerelease-${{ github.ref_name }}
|
||||||
|
deleteOnlyFromDrafts: false
|
||||||
|
|
||||||
|
- name: Set Env
|
||||||
|
run: |
|
||||||
|
echo "BUILDTIME=$(TZ=Asia/Shanghai date)" >> $GITHUB_ENV
|
||||||
|
shell: bash
|
||||||
|
|
||||||
|
- name: Tag Repo
|
||||||
|
uses: richardsimko/update-tag@v1.0.6
|
||||||
|
with:
|
||||||
|
tag_name: Prerelease-${{ github.ref_name }}
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- run: |
|
||||||
|
cat > release.txt << 'EOF'
|
||||||
|
Release created at ${{ env.BUILDTIME }}
|
||||||
|
Synchronize ${{ github.ref_name }} branch code updates, keeping only the latest version
|
||||||
|
<br>
|
||||||
|
[我应该下载哪个文件? / Which file should I download?](https://github.com/MetaCubeX/Clash.Meta/wiki/FAQ)
|
||||||
|
[查看文档 / Docs](https://metacubex.github.io/Meta-Docs/)
|
||||||
|
EOF
|
||||||
|
|
||||||
|
- name: Upload Prerelease
|
||||||
|
uses: softprops/action-gh-release@v1
|
||||||
|
if: ${{ success() }}
|
||||||
|
with:
|
||||||
|
tag_name: Prerelease-${{ github.ref_name }}
|
||||||
|
files: |
|
||||||
|
bin/*
|
||||||
|
prerelease: true
|
||||||
|
generate_release_notes: true
|
||||||
|
body_path: release.txt
|
||||||
|
|
||||||
|
Upload-Release:
|
||||||
|
permissions: write-all
|
||||||
|
if: ${{ github.ref_type=='tag' }}
|
||||||
|
needs: [Build]
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/download-artifact@v3
|
||||||
|
with:
|
||||||
|
name: artifact
|
||||||
|
path: bin/
|
||||||
|
|
||||||
|
- name: Display structure of downloaded files
|
||||||
|
run: ls -R
|
||||||
|
working-directory: bin
|
||||||
|
|
||||||
|
- name: Upload Release
|
||||||
|
uses: softprops/action-gh-release@v1
|
||||||
|
if: ${{ success() }}
|
||||||
|
with:
|
||||||
|
tag_name: ${{ github.ref_name }}
|
||||||
files: bin/*
|
files: bin/*
|
||||||
body_path: release.md
|
generate_release_notes: true
|
||||||
|
|
||||||
Docker:
|
Docker:
|
||||||
if: ${{ !startsWith(github.event_name, 'pull_request') }}
|
if: ${{ github.event_name != 'pull_request' }}
|
||||||
permissions: write-all
|
permissions: write-all
|
||||||
needs: [build]
|
needs: [Build]
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
- uses: actions/download-artifact@v4
|
- uses: actions/download-artifact@v3
|
||||||
with:
|
with:
|
||||||
|
name: artifact
|
||||||
path: bin/
|
path: bin/
|
||||||
merge-multiple: true
|
|
||||||
|
|
||||||
- name: Display structure of downloaded files
|
- name: Display structure of downloaded files
|
||||||
run: ls -R
|
run: ls -R
|
||||||
working-directory: bin
|
working-directory: bin
|
||||||
|
|
||||||
- name: Set up QEMU
|
- name: Set up QEMU
|
||||||
uses: docker/setup-qemu-action@v3
|
uses: docker/setup-qemu-action@v2
|
||||||
|
|
||||||
- name: Setup Docker buildx
|
- name: Setup Docker buildx
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@v2
|
||||||
with:
|
with:
|
||||||
version: latest
|
version: latest
|
||||||
|
|
||||||
# Extract metadata (tags, labels) for Docker
|
# Extract metadata (tags, labels) for Docker
|
||||||
# https://github.com/docker/metadata-action
|
# https://github.com/docker/metadata-action
|
||||||
- name: Extract Docker metadata
|
- name: Extract Docker metadata
|
||||||
if: ${{ github.event_name != 'workflow_dispatch' }}
|
id: meta
|
||||||
id: meta_alpha
|
uses: docker/metadata-action@v4
|
||||||
uses: docker/metadata-action@v5
|
|
||||||
with:
|
with:
|
||||||
images: '${{ env.REGISTRY }}/${{ github.repository }}'
|
images: ${{ env.REGISTRY }}/${{ secrets.DOCKERHUB_ACCOUNT }}/${{secrets.DOCKERHUB_REPO}}
|
||||||
|
|
||||||
# Extract metadata (tags, labels) for Docker
|
|
||||||
# https://github.com/docker/metadata-action
|
|
||||||
- name: Extract Docker metadata
|
|
||||||
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.version != '' }}
|
|
||||||
id: meta_release
|
|
||||||
uses: docker/metadata-action@v5
|
|
||||||
with:
|
|
||||||
images: '${{ env.REGISTRY }}/${{ github.repository }}'
|
|
||||||
tags: |
|
|
||||||
${{ github.event.inputs.version }}
|
|
||||||
flavor: |
|
|
||||||
latest=true
|
|
||||||
labels: org.opencontainers.image.version=${{ github.event.inputs.version }}
|
|
||||||
|
|
||||||
- name: Show files
|
- name: Show files
|
||||||
run: |
|
run: |
|
||||||
ls .
|
ls .
|
||||||
ls bin/
|
ls bin/
|
||||||
|
- name: Log into registry
|
||||||
- name: login to docker REGISTRY
|
if: github.event_name != 'pull_request'
|
||||||
uses: docker/login-action@v3
|
uses: docker/login-action@v2
|
||||||
with:
|
with:
|
||||||
registry: ${{ env.REGISTRY }}
|
registry: ${{ env.REGISTRY }}
|
||||||
username: ${{ secrets.DOCKER_HUB_USER }}
|
username: ${{ secrets.DOCKER_HUB_USER }}
|
||||||
@@ -499,8 +334,8 @@ jobs:
|
|||||||
# Build and push Docker image with Buildx (don't push on PR)
|
# Build and push Docker image with Buildx (don't push on PR)
|
||||||
# https://github.com/docker/build-push-action
|
# https://github.com/docker/build-push-action
|
||||||
- name: Build and push Docker image
|
- name: Build and push Docker image
|
||||||
if: ${{ github.event_name != 'workflow_dispatch' }}
|
id: build-and-push
|
||||||
uses: docker/build-push-action@v5
|
uses: docker/build-push-action@v4
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
file: ./Dockerfile
|
file: ./Dockerfile
|
||||||
@@ -508,22 +343,8 @@ jobs:
|
|||||||
platforms: |
|
platforms: |
|
||||||
linux/386
|
linux/386
|
||||||
linux/amd64
|
linux/amd64
|
||||||
linux/arm64
|
linux/arm64/v8
|
||||||
linux/arm/v7
|
linux/arm/v7
|
||||||
tags: ${{ steps.meta_alpha.outputs.tags }}
|
# linux/riscv64
|
||||||
labels: ${{ steps.meta_alpha.outputs.labels }}
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
- name: Build and push Docker image
|
|
||||||
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.version != '' }}
|
|
||||||
uses: docker/build-push-action@v5
|
|
||||||
with:
|
|
||||||
context: .
|
|
||||||
file: ./Dockerfile
|
|
||||||
push: ${{ github.event_name != 'pull_request' }}
|
|
||||||
platforms: |
|
|
||||||
linux/386
|
|
||||||
linux/amd64
|
|
||||||
linux/arm64
|
|
||||||
linux/arm/v7
|
|
||||||
tags: ${{ steps.meta_release.outputs.tags }}
|
|
||||||
labels: ${{ steps.meta_release.outputs.labels }}
|
|
||||||
|
|||||||
@@ -1,13 +1,7 @@
|
|||||||
name: Trigger CMFA Update
|
name: CMFA auto update-dependencies trigger
|
||||||
on:
|
on:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
push:
|
push:
|
||||||
paths-ignore:
|
|
||||||
- "docs/**"
|
|
||||||
- "README.md"
|
|
||||||
- ".github/ISSUE_TEMPLATE/**"
|
|
||||||
branches:
|
|
||||||
- Alpha
|
|
||||||
tags:
|
tags:
|
||||||
- "v*"
|
- "v*"
|
||||||
pull_request_target:
|
pull_request_target:
|
||||||
@@ -15,8 +9,7 @@ on:
|
|||||||
- Alpha
|
- Alpha
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
# Send "core-updated" to MetaCubeX/ClashMetaForAndroid to trigger update-dependencies
|
update-dependencies:
|
||||||
trigger-CMFA-update:
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: tibdex/github-app-token@v1
|
- uses: tibdex/github-app-token@v1
|
||||||
@@ -31,3 +24,5 @@ jobs:
|
|||||||
-H "Accept: application/vnd.github.everest-preview+json" \
|
-H "Accept: application/vnd.github.everest-preview+json" \
|
||||||
-H "Authorization: token ${{ steps.generate-token.outputs.token }}" \
|
-H "Authorization: token ${{ steps.generate-token.outputs.token }}" \
|
||||||
-d '{"event_type": "core-updated"}'
|
-d '{"event_type": "core-updated"}'
|
||||||
|
# Send "core-updated" to MetaCubeX/ClashMetaForAndroid to trigger update-dependencies
|
||||||
|
|
||||||
115
.github/workflows/test.yml
vendored
115
.github/workflows/test.yml
vendored
@@ -1,115 +0,0 @@
|
|||||||
name: Test
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
paths-ignore:
|
|
||||||
- "docs/**"
|
|
||||||
- "README.md"
|
|
||||||
- ".github/ISSUE_TEMPLATE/**"
|
|
||||||
branches:
|
|
||||||
- Alpha
|
|
||||||
tags:
|
|
||||||
- "v*"
|
|
||||||
pull_request:
|
|
||||||
branches:
|
|
||||||
- Alpha
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
os:
|
|
||||||
- 'ubuntu-latest' # amd64 linux
|
|
||||||
- 'windows-latest' # amd64 windows
|
|
||||||
- 'macos-latest' # arm64 macos
|
|
||||||
- 'ubuntu-24.04-arm' # arm64 linux
|
|
||||||
- 'macos-13' # amd64 macos
|
|
||||||
go-version:
|
|
||||||
- '1.24'
|
|
||||||
- '1.23'
|
|
||||||
- '1.22'
|
|
||||||
- '1.21'
|
|
||||||
- '1.20'
|
|
||||||
fail-fast: false
|
|
||||||
runs-on: ${{ matrix.os }}
|
|
||||||
defaults:
|
|
||||||
run:
|
|
||||||
shell: bash
|
|
||||||
env:
|
|
||||||
CGO_ENABLED: 0
|
|
||||||
GOTOOLCHAIN: local
|
|
||||||
# Fix mingw trying to be smart and converting paths https://github.com/moby/moby/issues/24029#issuecomment-250412919
|
|
||||||
MSYS_NO_PATHCONV: true
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Setup Go
|
|
||||||
uses: actions/setup-go@v5
|
|
||||||
with:
|
|
||||||
go-version: ${{ matrix.go-version }}
|
|
||||||
|
|
||||||
# modify from https://github.com/restic/restic/issues/4636#issuecomment-1896455557
|
|
||||||
# this patch file only works on golang1.24.x
|
|
||||||
# that means after golang1.25 release it must be changed
|
|
||||||
# see: https://github.com/MetaCubeX/go/commits/release-branch.go1.24/
|
|
||||||
# revert:
|
|
||||||
# 693def151adff1af707d82d28f55dba81ceb08e1: "crypto/rand,runtime: switch RtlGenRandom for ProcessPrng"
|
|
||||||
# 7c1157f9544922e96945196b47b95664b1e39108: "net: remove sysSocket fallback for Windows 7"
|
|
||||||
# 48042aa09c2f878c4faa576948b07fe625c4707a: "syscall: remove Windows 7 console handle workaround"
|
|
||||||
# a17d959debdb04cd550016a3501dd09d50cd62e7: "runtime: always use LoadLibraryEx to load system libraries"
|
|
||||||
- name: Revert Golang1.24 commit for Windows7/8
|
|
||||||
if: ${{ runner.os == 'Windows' && matrix.go-version == '1.24' }}
|
|
||||||
run: |
|
|
||||||
cd $(go env GOROOT)
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/2a406dc9f1ea7323d6ca9fccb2fe9ddebb6b1cc8.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/7b1fd7d39c6be0185fbe1d929578ab372ac5c632.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/979d6d8bab3823ff572ace26767fd2ce3cf351ae.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/ac3e93c061779dfefc0dd13a5b6e6f764a25621e.diff | patch --verbose -p 1
|
|
||||||
|
|
||||||
# modify from https://github.com/restic/restic/issues/4636#issuecomment-1896455557
|
|
||||||
# this patch file only works on golang1.23.x
|
|
||||||
# that means after golang1.24 release it must be changed
|
|
||||||
# see: https://github.com/MetaCubeX/go/commits/release-branch.go1.23/
|
|
||||||
# revert:
|
|
||||||
# 693def151adff1af707d82d28f55dba81ceb08e1: "crypto/rand,runtime: switch RtlGenRandom for ProcessPrng"
|
|
||||||
# 7c1157f9544922e96945196b47b95664b1e39108: "net: remove sysSocket fallback for Windows 7"
|
|
||||||
# 48042aa09c2f878c4faa576948b07fe625c4707a: "syscall: remove Windows 7 console handle workaround"
|
|
||||||
# a17d959debdb04cd550016a3501dd09d50cd62e7: "runtime: always use LoadLibraryEx to load system libraries"
|
|
||||||
- name: Revert Golang1.23 commit for Windows7/8
|
|
||||||
if: ${{ runner.os == 'Windows' && matrix.go-version == '1.23' }}
|
|
||||||
run: |
|
|
||||||
cd $(go env GOROOT)
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/9ac42137ef6730e8b7daca016ece831297a1d75b.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/21290de8a4c91408de7c2b5b68757b1e90af49dd.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/6a31d3fa8e47ddabc10bd97bff10d9a85f4cfb76.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/69e2eed6dd0f6d815ebf15797761c13f31213dd6.diff | patch --verbose -p 1
|
|
||||||
|
|
||||||
# modify from https://github.com/restic/restic/issues/4636#issuecomment-1896455557
|
|
||||||
# this patch file only works on golang1.22.x
|
|
||||||
# that means after golang1.23 release it must be changed
|
|
||||||
# see: https://github.com/MetaCubeX/go/commits/release-branch.go1.22/
|
|
||||||
# revert:
|
|
||||||
# 693def151adff1af707d82d28f55dba81ceb08e1: "crypto/rand,runtime: switch RtlGenRandom for ProcessPrng"
|
|
||||||
# 7c1157f9544922e96945196b47b95664b1e39108: "net: remove sysSocket fallback for Windows 7"
|
|
||||||
# 48042aa09c2f878c4faa576948b07fe625c4707a: "syscall: remove Windows 7 console handle workaround"
|
|
||||||
# a17d959debdb04cd550016a3501dd09d50cd62e7: "runtime: always use LoadLibraryEx to load system libraries"
|
|
||||||
- name: Revert Golang1.22 commit for Windows7/8
|
|
||||||
if: ${{ runner.os == 'Windows' && matrix.go-version == '1.22' }}
|
|
||||||
run: |
|
|
||||||
cd $(go env GOROOT)
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/9779155f18b6556a034f7bb79fb7fb2aad1e26a9.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/ef0606261340e608017860b423ffae5c1ce78239.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/7f83badcb925a7e743188041cb6e561fc9b5b642.diff | patch --verbose -p 1
|
|
||||||
curl https://github.com/MetaCubeX/go/commit/83ff9782e024cb328b690cbf0da4e7848a327f4f.diff | patch --verbose -p 1
|
|
||||||
|
|
||||||
# modify from https://github.com/restic/restic/issues/4636#issuecomment-1896455557
|
|
||||||
- name: Revert Golang1.21 commit for Windows7/8
|
|
||||||
if: ${{ runner.os == 'Windows' && matrix.go-version == '1.21' }}
|
|
||||||
run: |
|
|
||||||
cd $(go env GOROOT)
|
|
||||||
curl https://github.com/golang/go/commit/9e43850a3298a9b8b1162ba0033d4c53f8637571.diff | patch --verbose -R -p 1
|
|
||||||
|
|
||||||
- name: Test
|
|
||||||
run: go test ./... -v -count=1
|
|
||||||
|
|
||||||
- name: Test with tag with_gvisor
|
|
||||||
run: go test ./... -v -count=1 -tags "with_gvisor"
|
|
||||||
@@ -11,7 +11,7 @@ linters-settings:
|
|||||||
custom-order: true
|
custom-order: true
|
||||||
sections:
|
sections:
|
||||||
- standard
|
- standard
|
||||||
- prefix(github.com/metacubex/mihomo)
|
- prefix(github.com/Dreamacro/clash)
|
||||||
- default
|
- default
|
||||||
staticcheck:
|
staticcheck:
|
||||||
go: '1.19'
|
go: '1.19'
|
||||||
|
|||||||
27
Dockerfile
27
Dockerfile
@@ -3,24 +3,25 @@ ARG TARGETPLATFORM
|
|||||||
RUN echo "I'm building for $TARGETPLATFORM"
|
RUN echo "I'm building for $TARGETPLATFORM"
|
||||||
|
|
||||||
RUN apk add --no-cache gzip && \
|
RUN apk add --no-cache gzip && \
|
||||||
mkdir /mihomo-config && \
|
mkdir /clash-config && \
|
||||||
wget -O /mihomo-config/geoip.metadb https://fastly.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geoip.metadb && \
|
wget -O /clash-config/geoip.metadb https://fastly.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geoip.metadb && \
|
||||||
wget -O /mihomo-config/geosite.dat https://fastly.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geosite.dat && \
|
wget -O /clash-config/geosite.dat https://fastly.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geosite.dat && \
|
||||||
wget -O /mihomo-config/geoip.dat https://fastly.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geoip.dat
|
wget -O /clash-config/geoip.dat https://fastly.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geoip.dat
|
||||||
|
|
||||||
COPY docker/file-name.sh /mihomo/file-name.sh
|
COPY docker/file-name.sh /clash/file-name.sh
|
||||||
WORKDIR /mihomo
|
WORKDIR /clash
|
||||||
COPY bin/ bin/
|
COPY bin/ bin/
|
||||||
RUN FILE_NAME=`sh file-name.sh` && echo $FILE_NAME && \
|
RUN FILE_NAME=`sh file-name.sh` && echo $FILE_NAME && \
|
||||||
FILE_NAME=`ls bin/ | egrep "$FILE_NAME.gz"|awk NR==1` && echo $FILE_NAME && \
|
FILE_NAME=`ls bin/ | egrep "$FILE_NAME.*"|awk NR==1` && echo $FILE_NAME && \
|
||||||
mv bin/$FILE_NAME mihomo.gz && gzip -d mihomo.gz && chmod +x mihomo && echo "$FILE_NAME" > /mihomo-config/test
|
mv bin/$FILE_NAME clash.gz && gzip -d clash.gz && echo "$FILE_NAME" > /clash-config/test
|
||||||
FROM alpine:latest
|
FROM alpine:latest
|
||||||
LABEL org.opencontainers.image.source="https://github.com/MetaCubeX/mihomo"
|
LABEL org.opencontainers.image.source="https://github.com/MetaCubeX/Clash.Meta"
|
||||||
|
|
||||||
RUN apk add --no-cache ca-certificates tzdata iptables
|
RUN apk add --no-cache ca-certificates tzdata iptables
|
||||||
|
|
||||||
VOLUME ["/root/.config/mihomo/"]
|
VOLUME ["/root/.config/clash/"]
|
||||||
|
|
||||||
COPY --from=builder /mihomo-config/ /root/.config/mihomo/
|
COPY --from=builder /clash-config/ /root/.config/clash/
|
||||||
COPY --from=builder /mihomo/mihomo /mihomo
|
COPY --from=builder /clash/clash /clash
|
||||||
ENTRYPOINT [ "/mihomo" ]
|
RUN chmod +x /clash
|
||||||
|
ENTRYPOINT [ "/clash" ]
|
||||||
|
|||||||
11
Makefile
11
Makefile
@@ -1,4 +1,4 @@
|
|||||||
NAME=mihomo
|
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)
|
||||||
@@ -12,12 +12,11 @@ VERSION=$(shell git rev-parse --short HEAD)
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
BUILDTIME=$(shell date -u)
|
BUILDTIME=$(shell date -u)
|
||||||
GOBUILD=CGO_ENABLED=0 go build -tags with_gvisor -trimpath -ldflags '-X "github.com/metacubex/mihomo/constant.Version=$(VERSION)" \
|
GOBUILD=CGO_ENABLED=0 go build -tags with_gvisor -trimpath -ldflags '-X "github.com/Dreamacro/clash/constant.Version=$(VERSION)" \
|
||||||
-X "github.com/metacubex/mihomo/constant.BuildTime=$(BUILDTIME)" \
|
-X "github.com/Dreamacro/clash/constant.BuildTime=$(BUILDTIME)" \
|
||||||
-w -s -buildid='
|
-w -s -buildid='
|
||||||
|
|
||||||
PLATFORM_LIST = \
|
PLATFORM_LIST = \
|
||||||
darwin-amd64-compatible \
|
|
||||||
darwin-amd64 \
|
darwin-amd64 \
|
||||||
darwin-arm64 \
|
darwin-arm64 \
|
||||||
linux-amd64-compatible \
|
linux-amd64-compatible \
|
||||||
@@ -163,3 +162,7 @@ clean:
|
|||||||
CLANG ?= clang-14
|
CLANG ?= clang-14
|
||||||
CFLAGS := -O2 -g -Wall -Werror $(CFLAGS)
|
CFLAGS := -O2 -g -Wall -Werror $(CFLAGS)
|
||||||
|
|
||||||
|
ebpf: export BPF_CLANG := $(CLANG)
|
||||||
|
ebpf: export BPF_CFLAGS := $(CFLAGS)
|
||||||
|
ebpf:
|
||||||
|
cd component/ebpf/ && go generate ./...
|
||||||
|
|||||||
28
README.md
28
README.md
@@ -3,17 +3,17 @@
|
|||||||
<br>Meta Kernel<br>
|
<br>Meta Kernel<br>
|
||||||
</h1>
|
</h1>
|
||||||
|
|
||||||
<h3 align="center">Another Mihomo Kernel.</h3>
|
<h3 align="center">Another Clash Kernel.</h3>
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://goreportcard.com/report/github.com/MetaCubeX/mihomo">
|
<a href="https://goreportcard.com/report/github.com/Clash-Mini/Clash.Meta">
|
||||||
<img src="https://goreportcard.com/badge/github.com/MetaCubeX/mihomo?style=flat-square">
|
<img src="https://goreportcard.com/badge/github.com/Clash-Mini/Clash.Meta?style=flat-square">
|
||||||
</a>
|
</a>
|
||||||
<img src="https://img.shields.io/github/go-mod/go-version/MetaCubeX/mihomo/Alpha?style=flat-square">
|
<img src="https://img.shields.io/github/go-mod/go-version/Dreamacro/clash?style=flat-square">
|
||||||
<a href="https://github.com/MetaCubeX/mihomo/releases">
|
<a href="https://github.com/Clash-Mini/Clash.Meta/releases">
|
||||||
<img src="https://img.shields.io/github/release/MetaCubeX/mihomo/all.svg?style=flat-square">
|
<img src="https://img.shields.io/github/release/Clash-Mini/Clash.Meta/all.svg?style=flat-square">
|
||||||
</a>
|
</a>
|
||||||
<a href="https://github.com/MetaCubeX/mihomo">
|
<a href="https://github.com/Clash-Mini/Clash.Meta">
|
||||||
<img src="https://img.shields.io/badge/release-Meta-00b4f0?style=flat-square">
|
<img src="https://img.shields.io/badge/release-Meta-00b4f0?style=flat-square">
|
||||||
</a>
|
</a>
|
||||||
</p>
|
</p>
|
||||||
@@ -27,7 +27,7 @@
|
|||||||
- Remote groups allow users to implement powerful rules. Supports automatic fallback, load balancing or auto select node
|
- Remote groups allow users to implement powerful rules. Supports automatic fallback, load balancing or auto select node
|
||||||
based off latency
|
based off latency
|
||||||
- Remote providers, allowing users to get node lists remotely instead of hard-coding in config
|
- Remote providers, allowing users to get node lists remotely instead of hard-coding in config
|
||||||
- Netfilter TCP redirecting. Deploy Mihomo on your Internet gateway with `iptables`.
|
- Netfilter TCP redirecting. Deploy Clash on your Internet gateway with `iptables`.
|
||||||
- Comprehensive HTTP RESTful API controller
|
- Comprehensive HTTP RESTful API controller
|
||||||
|
|
||||||
## Dashboard
|
## Dashboard
|
||||||
@@ -36,22 +36,22 @@ A web dashboard with first-class support for this project has been created; it c
|
|||||||
|
|
||||||
## Configration example
|
## Configration example
|
||||||
|
|
||||||
Configuration example is located at [/docs/config.yaml](https://github.com/MetaCubeX/mihomo/blob/Alpha/docs/config.yaml).
|
Configuration example is located at [/docs/config.yaml](https://github.com/MetaCubeX/Clash.Meta/blob/Alpha/docs/config.yaml).
|
||||||
|
|
||||||
## Docs
|
## Docs
|
||||||
|
|
||||||
Documentation can be found in [mihomo Docs](https://wiki.metacubex.one/).
|
Documentation can be found in [Clash.Meta Docs](https://clash-meta.wiki).
|
||||||
|
|
||||||
## For development
|
## For development
|
||||||
|
|
||||||
Requirements:
|
Requirements:
|
||||||
[Go 1.20 or newer](https://go.dev/dl/)
|
[Go 1.20 or newer](https://go.dev/dl/)
|
||||||
|
|
||||||
Build mihomo:
|
Build Clash.Meta:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
git clone https://github.com/MetaCubeX/mihomo.git
|
git clone https://github.com/MetaCubeX/Clash.Meta.git
|
||||||
cd mihomo && go mod download
|
cd Clash.Meta && go mod download
|
||||||
go build
|
go build
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -98,4 +98,4 @@ API.
|
|||||||
|
|
||||||
This software is released under the GPL-3.0 license.
|
This software is released under the GPL-3.0 license.
|
||||||
|
|
||||||
**In addition, any downstream projects not affiliated with `MetaCubeX` shall not contain the word `mihomo` in their names.**
|
[](https://app.fossa.io/projects/git%2Bgithub.com%2FDreamacro%2Fclash?ref=badge_large)
|
||||||
|
|||||||
@@ -2,24 +2,24 @@ package adapter
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/atomic"
|
"github.com/Dreamacro/clash/common/atomic"
|
||||||
"github.com/metacubex/mihomo/common/queue"
|
"github.com/Dreamacro/clash/common/queue"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/Dreamacro/clash/common/utils"
|
||||||
"github.com/metacubex/mihomo/component/ca"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/metacubex/mihomo/log"
|
"github.com/Dreamacro/clash/log"
|
||||||
"github.com/puzpuzpuz/xsync/v3"
|
|
||||||
|
"github.com/puzpuzpuz/xsync/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
var UnifiedDelay = atomic.NewBool(false)
|
var UnifiedDelay = atomic.NewBool(false)
|
||||||
@@ -28,21 +28,22 @@ const (
|
|||||||
defaultHistoriesNum = 10
|
defaultHistoriesNum = 10
|
||||||
)
|
)
|
||||||
|
|
||||||
type internalProxyState struct {
|
type extraProxyState struct {
|
||||||
alive atomic.Bool
|
|
||||||
history *queue.Queue[C.DelayHistory]
|
history *queue.Queue[C.DelayHistory]
|
||||||
|
alive atomic.Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Proxy struct {
|
type Proxy struct {
|
||||||
C.ProxyAdapter
|
C.ProxyAdapter
|
||||||
alive atomic.Bool
|
|
||||||
history *queue.Queue[C.DelayHistory]
|
history *queue.Queue[C.DelayHistory]
|
||||||
extra *xsync.MapOf[string, *internalProxyState]
|
alive atomic.Bool
|
||||||
|
url string
|
||||||
|
extra *xsync.MapOf[string, *extraProxyState]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adapter implements C.Proxy
|
// Alive implements C.Proxy
|
||||||
func (p *Proxy) Adapter() C.ProxyAdapter {
|
func (p *Proxy) Alive() bool {
|
||||||
return p.ProxyAdapter
|
return p.alive.Load()
|
||||||
}
|
}
|
||||||
|
|
||||||
// AliveForTestUrl implements C.Proxy
|
// AliveForTestUrl implements C.Proxy
|
||||||
@@ -62,8 +63,8 @@ func (p *Proxy) Dial(metadata *C.Metadata) (C.Conn, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (p *Proxy) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
func (p *Proxy) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
||||||
conn, err := p.ProxyAdapter.DialContext(ctx, metadata)
|
conn, err := p.ProxyAdapter.DialContext(ctx, metadata, opts...)
|
||||||
return conn, err
|
return conn, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,8 +76,8 @@ func (p *Proxy) DialUDP(metadata *C.Metadata) (C.PacketConn, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (p *Proxy) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
func (p *Proxy) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
||||||
pc, err := p.ProxyAdapter.ListenPacketContext(ctx, metadata)
|
pc, err := p.ProxyAdapter.ListenPacketContext(ctx, metadata, opts...)
|
||||||
return pc, err
|
return pc, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,6 +88,7 @@ func (p *Proxy) DelayHistory() []C.DelayHistory {
|
|||||||
for _, item := range queueM {
|
for _, item := range queueM {
|
||||||
histories = append(histories, item)
|
histories = append(histories, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
return histories
|
return histories
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,6 +99,11 @@ func (p *Proxy) DelayHistoryForTestUrl(url string) []C.DelayHistory {
|
|||||||
if state, ok := p.extra.Load(url); ok {
|
if state, ok := p.extra.Load(url); ok {
|
||||||
queueM = state.history.Copy()
|
queueM = state.history.Copy()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if queueM == nil {
|
||||||
|
queueM = p.history.Copy()
|
||||||
|
}
|
||||||
|
|
||||||
histories := []C.DelayHistory{}
|
histories := []C.DelayHistory{}
|
||||||
for _, item := range queueM {
|
for _, item := range queueM {
|
||||||
histories = append(histories, item)
|
histories = append(histories, item)
|
||||||
@@ -104,46 +111,61 @@ func (p *Proxy) DelayHistoryForTestUrl(url string) []C.DelayHistory {
|
|||||||
return histories
|
return histories
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExtraDelayHistories return all delay histories for each test URL
|
func (p *Proxy) ExtraDelayHistory() map[string][]C.DelayHistory {
|
||||||
// implements C.Proxy
|
extraHistory := map[string][]C.DelayHistory{}
|
||||||
func (p *Proxy) ExtraDelayHistories() map[string]C.ProxyState {
|
|
||||||
histories := map[string]C.ProxyState{}
|
p.extra.Range(func(k string, v *extraProxyState) bool {
|
||||||
|
|
||||||
p.extra.Range(func(k string, v *internalProxyState) bool {
|
|
||||||
testUrl := k
|
testUrl := k
|
||||||
state := v
|
state := v
|
||||||
|
|
||||||
|
histories := []C.DelayHistory{}
|
||||||
queueM := state.history.Copy()
|
queueM := state.history.Copy()
|
||||||
var history []C.DelayHistory
|
|
||||||
|
|
||||||
for _, item := range queueM {
|
for _, item := range queueM {
|
||||||
history = append(history, item)
|
histories = append(histories, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
histories[testUrl] = C.ProxyState{
|
extraHistory[testUrl] = histories
|
||||||
Alive: state.alive.Load(),
|
|
||||||
History: history,
|
|
||||||
}
|
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
return histories
|
return extraHistory
|
||||||
}
|
}
|
||||||
|
|
||||||
// LastDelayForTestUrl return last history record of the specified URL. if proxy is not alive, return the max value of uint16.
|
// LastDelay return last history record. if proxy is not alive, return the max value of uint16.
|
||||||
// implements C.Proxy
|
// implements C.Proxy
|
||||||
func (p *Proxy) LastDelayForTestUrl(url string) (delay uint16) {
|
func (p *Proxy) LastDelay() (delay uint16) {
|
||||||
var maxDelay uint16 = 0xffff
|
var max uint16 = 0xffff
|
||||||
|
if !p.alive.Load() {
|
||||||
|
return max
|
||||||
|
}
|
||||||
|
|
||||||
alive := false
|
history := p.history.Last()
|
||||||
var history C.DelayHistory
|
if history.Delay == 0 {
|
||||||
|
return max
|
||||||
|
}
|
||||||
|
return history.Delay
|
||||||
|
}
|
||||||
|
|
||||||
|
// LastDelayForTestUrl implements C.Proxy
|
||||||
|
func (p *Proxy) LastDelayForTestUrl(url string) (delay uint16) {
|
||||||
|
var max uint16 = 0xffff
|
||||||
|
|
||||||
|
alive := p.alive.Load()
|
||||||
|
history := p.history.Last()
|
||||||
|
|
||||||
if state, ok := p.extra.Load(url); ok {
|
if state, ok := p.extra.Load(url); ok {
|
||||||
alive = state.alive.Load()
|
alive = state.alive.Load()
|
||||||
history = state.history.Last()
|
history = state.history.Last()
|
||||||
}
|
}
|
||||||
|
|
||||||
if !alive || history.Delay == 0 {
|
if !alive {
|
||||||
return maxDelay
|
return max
|
||||||
|
}
|
||||||
|
|
||||||
|
if history.Delay == 0 {
|
||||||
|
return max
|
||||||
}
|
}
|
||||||
return history.Delay
|
return history.Delay
|
||||||
}
|
}
|
||||||
@@ -158,62 +180,65 @@ func (p *Proxy) MarshalJSON() ([]byte, error) {
|
|||||||
mapping := map[string]any{}
|
mapping := map[string]any{}
|
||||||
_ = json.Unmarshal(inner, &mapping)
|
_ = json.Unmarshal(inner, &mapping)
|
||||||
mapping["history"] = p.DelayHistory()
|
mapping["history"] = p.DelayHistory()
|
||||||
mapping["extra"] = p.ExtraDelayHistories()
|
mapping["extra"] = p.ExtraDelayHistory()
|
||||||
mapping["alive"] = p.alive.Load()
|
mapping["alive"] = p.Alive()
|
||||||
mapping["name"] = p.Name()
|
mapping["name"] = p.Name()
|
||||||
mapping["udp"] = p.SupportUDP()
|
mapping["udp"] = p.SupportUDP()
|
||||||
mapping["uot"] = p.SupportUOT()
|
mapping["xudp"] = p.SupportXUDP()
|
||||||
|
mapping["tfo"] = p.SupportTFO()
|
||||||
proxyInfo := p.ProxyInfo()
|
|
||||||
mapping["xudp"] = proxyInfo.XUDP
|
|
||||||
mapping["tfo"] = proxyInfo.TFO
|
|
||||||
mapping["mptcp"] = proxyInfo.MPTCP
|
|
||||||
mapping["smux"] = proxyInfo.SMUX
|
|
||||||
mapping["interface"] = proxyInfo.Interface
|
|
||||||
mapping["dialer-proxy"] = proxyInfo.DialerProxy
|
|
||||||
mapping["routing-mark"] = proxyInfo.RoutingMark
|
|
||||||
|
|
||||||
return json.Marshal(mapping)
|
return json.Marshal(mapping)
|
||||||
}
|
}
|
||||||
|
|
||||||
// URLTest get the delay for the specified URL
|
// URLTest get the delay for the specified URL
|
||||||
// implements C.Proxy
|
// implements C.Proxy
|
||||||
func (p *Proxy) URLTest(ctx context.Context, url string, expectedStatus utils.IntRanges[uint16]) (t uint16, err error) {
|
func (p *Proxy) URLTest(ctx context.Context, url string, expectedStatus utils.IntRanges[uint16], store C.DelayHistoryStoreType) (t uint16, err error) {
|
||||||
var satisfied bool
|
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
alive := err == nil
|
alive := err == nil
|
||||||
record := C.DelayHistory{Time: time.Now()}
|
store = p.determineFinalStoreType(store, url)
|
||||||
if alive {
|
|
||||||
record.Delay = t
|
|
||||||
}
|
|
||||||
|
|
||||||
p.alive.Store(alive)
|
switch store {
|
||||||
p.history.Put(record)
|
case C.OriginalHistory:
|
||||||
if p.history.Len() > defaultHistoriesNum {
|
p.alive.Store(alive)
|
||||||
p.history.Pop()
|
record := C.DelayHistory{Time: time.Now()}
|
||||||
}
|
if alive {
|
||||||
|
record.Delay = t
|
||||||
state, ok := p.extra.Load(url)
|
}
|
||||||
if !ok {
|
p.history.Put(record)
|
||||||
state = &internalProxyState{
|
if p.history.Len() > defaultHistoriesNum {
|
||||||
history: queue.New[C.DelayHistory](defaultHistoriesNum),
|
p.history.Pop()
|
||||||
alive: atomic.NewBool(true),
|
|
||||||
}
|
}
|
||||||
p.extra.Store(url, state)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !satisfied {
|
// test URL configured by the proxy provider
|
||||||
record.Delay = 0
|
if len(p.url) == 0 {
|
||||||
alive = false
|
p.url = url
|
||||||
}
|
}
|
||||||
|
case C.ExtraHistory:
|
||||||
|
record := C.DelayHistory{Time: time.Now()}
|
||||||
|
if alive {
|
||||||
|
record.Delay = t
|
||||||
|
}
|
||||||
|
p.history.Put(record)
|
||||||
|
if p.history.Len() > defaultHistoriesNum {
|
||||||
|
p.history.Pop()
|
||||||
|
}
|
||||||
|
|
||||||
state.alive.Store(alive)
|
state, ok := p.extra.Load(url)
|
||||||
state.history.Put(record)
|
if !ok {
|
||||||
if state.history.Len() > defaultHistoriesNum {
|
state = &extraProxyState{
|
||||||
state.history.Pop()
|
history: queue.New[C.DelayHistory](defaultHistoriesNum),
|
||||||
}
|
alive: atomic.NewBool(true),
|
||||||
|
}
|
||||||
|
p.extra.Store(url, state)
|
||||||
|
}
|
||||||
|
|
||||||
|
state.alive.Store(alive)
|
||||||
|
state.history.Put(record)
|
||||||
|
if state.history.Len() > defaultHistoriesNum {
|
||||||
|
state.history.Pop()
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
log.Debugln("health check result will be discarded, url: %s alive: %t, delay: %d", url, alive, t)
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
unifiedDelay := UnifiedDelay.Load()
|
unifiedDelay := UnifiedDelay.Load()
|
||||||
@@ -247,7 +272,6 @@ func (p *Proxy) URLTest(ctx context.Context, url string, expectedStatus utils.In
|
|||||||
IdleConnTimeout: 90 * time.Second,
|
IdleConnTimeout: 90 * time.Second,
|
||||||
TLSHandshakeTimeout: 10 * time.Second,
|
TLSHandshakeTimeout: 10 * time.Second,
|
||||||
ExpectContinueTimeout: 1 * time.Second,
|
ExpectContinueTimeout: 1 * time.Second,
|
||||||
TLSClientConfig: ca.GetGlobalTLSConfig(&tls.Config{}),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
client := http.Client{
|
client := http.Client{
|
||||||
@@ -270,22 +294,18 @@ func (p *Proxy) URLTest(ctx context.Context, url string, expectedStatus utils.In
|
|||||||
|
|
||||||
if unifiedDelay {
|
if unifiedDelay {
|
||||||
second := time.Now()
|
second := time.Now()
|
||||||
var ignoredErr error
|
resp, err = client.Do(req)
|
||||||
var secondResp *http.Response
|
if err == nil {
|
||||||
secondResp, ignoredErr = client.Do(req)
|
|
||||||
if ignoredErr == nil {
|
|
||||||
resp = secondResp
|
|
||||||
_ = resp.Body.Close()
|
_ = resp.Body.Close()
|
||||||
start = second
|
start = second
|
||||||
} else {
|
|
||||||
if strings.HasPrefix(url, "http://") {
|
|
||||||
log.Errorln("%s failed to get the second response from %s: %v", p.Name(), url, ignoredErr)
|
|
||||||
log.Warnln("It is recommended to use HTTPS for provider.health-check.url and group.url to ensure better reliability. Due to some proxy providers hijacking test addresses and not being compatible with repeated HEAD requests, using HTTP may result in failed tests.")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
satisfied = resp != nil && (expectedStatus == nil || expectedStatus.Check(uint16(resp.StatusCode)))
|
if expectedStatus != nil && !expectedStatus.Check(uint16(resp.StatusCode)) {
|
||||||
|
// maybe another value should be returned for differentiation
|
||||||
|
err = errors.New("response status is inconsistent with the expected status")
|
||||||
|
}
|
||||||
|
|
||||||
t = uint16(time.Since(start) / time.Millisecond)
|
t = uint16(time.Since(start) / time.Millisecond)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -295,7 +315,8 @@ func NewProxy(adapter C.ProxyAdapter) *Proxy {
|
|||||||
ProxyAdapter: adapter,
|
ProxyAdapter: adapter,
|
||||||
history: queue.New[C.DelayHistory](defaultHistoriesNum),
|
history: queue.New[C.DelayHistory](defaultHistoriesNum),
|
||||||
alive: atomic.NewBool(true),
|
alive: atomic.NewBool(true),
|
||||||
extra: xsync.NewMapOf[string, *internalProxyState]()}
|
url: "",
|
||||||
|
extra: xsync.NewMapOf[*extraProxyState]()}
|
||||||
}
|
}
|
||||||
|
|
||||||
func urlToMetadata(rawURL string) (addr C.Metadata, err error) {
|
func urlToMetadata(rawURL string) (addr C.Metadata, err error) {
|
||||||
@@ -328,3 +349,24 @@ func urlToMetadata(rawURL string) (addr C.Metadata, err error) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Proxy) determineFinalStoreType(store C.DelayHistoryStoreType, url string) C.DelayHistoryStoreType {
|
||||||
|
if store != C.DropHistory {
|
||||||
|
return store
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(p.url) == 0 || url == p.url {
|
||||||
|
return C.OriginalHistory
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.extra.Size() < 2*C.DefaultMaxHealthCheckUrlNum {
|
||||||
|
return C.ExtraHistory
|
||||||
|
}
|
||||||
|
|
||||||
|
_, ok := p.extra.Load(url)
|
||||||
|
if ok {
|
||||||
|
return C.ExtraHistory
|
||||||
|
}
|
||||||
|
|
||||||
|
return store
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package inbound
|
|||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Addition func(metadata *C.Metadata)
|
type Addition func(metadata *C.Metadata)
|
||||||
@@ -47,7 +47,7 @@ func WithDstAddr(addr net.Addr) Addition {
|
|||||||
func WithSrcAddr(addr net.Addr) Addition {
|
func WithSrcAddr(addr net.Addr) Addition {
|
||||||
return func(metadata *C.Metadata) {
|
return func(metadata *C.Metadata) {
|
||||||
m := C.Metadata{}
|
m := C.Metadata{}
|
||||||
if err := m.SetRemoteAddr(addr); err == nil {
|
if err := m.SetRemoteAddr(addr);err ==nil{
|
||||||
metadata.SrcIP = m.DstIP
|
metadata.SrcIP = m.DstIP
|
||||||
metadata.SrcPort = m.DstPort
|
metadata.SrcPort = m.DstPort
|
||||||
}
|
}
|
||||||
@@ -57,17 +57,9 @@ func WithSrcAddr(addr net.Addr) Addition {
|
|||||||
func WithInAddr(addr net.Addr) Addition {
|
func WithInAddr(addr net.Addr) Addition {
|
||||||
return func(metadata *C.Metadata) {
|
return func(metadata *C.Metadata) {
|
||||||
m := C.Metadata{}
|
m := C.Metadata{}
|
||||||
if err := m.SetRemoteAddr(addr); err == nil {
|
if err := m.SetRemoteAddr(addr);err ==nil{
|
||||||
metadata.InIP = m.DstIP
|
metadata.InIP = m.DstIP
|
||||||
metadata.InPort = m.DstPort
|
metadata.InPort = m.DstPort
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithDSCP(dscp uint8) Addition {
|
|
||||||
return func(metadata *C.Metadata) {
|
|
||||||
metadata.DSCP = dscp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Placeholder(metadata *C.Metadata) {}
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
var skipAuthPrefixes []netip.Prefix
|
var skipAuthPrefixes []netip.Prefix
|
||||||
@@ -34,5 +34,12 @@ func SkipAuthRemoteAddress(addr string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func skipAuth(addr netip.Addr) bool {
|
func skipAuth(addr netip.Addr) bool {
|
||||||
return prefixesContains(skipAuthPrefixes, addr)
|
if addr.IsValid() {
|
||||||
|
for _, prefix := range skipAuthPrefixes {
|
||||||
|
if prefix.Contains(addr.Unmap()) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ package inbound
|
|||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/metacubex/mihomo/transport/socks5"
|
"github.com/Dreamacro/clash/transport/socks5"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewHTTP receive normal http request and return HTTPContext
|
// NewHTTP receive normal http request and return HTTPContext
|
||||||
@@ -14,7 +14,7 @@ func NewHTTP(target socks5.Addr, srcConn net.Conn, conn net.Conn, additions ...A
|
|||||||
metadata.Type = C.HTTP
|
metadata.Type = C.HTTP
|
||||||
metadata.RawSrcAddr = srcConn.RemoteAddr()
|
metadata.RawSrcAddr = srcConn.RemoteAddr()
|
||||||
metadata.RawDstAddr = srcConn.LocalAddr()
|
metadata.RawDstAddr = srcConn.LocalAddr()
|
||||||
ApplyAdditions(metadata, WithSrcAddr(srcConn.RemoteAddr()), WithInAddr(srcConn.LocalAddr()))
|
ApplyAdditions(metadata, WithSrcAddr(srcConn.RemoteAddr()), WithInAddr(conn.LocalAddr()))
|
||||||
ApplyAdditions(metadata, additions...)
|
ApplyAdditions(metadata, additions...)
|
||||||
return conn, metadata
|
return conn, metadata
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,15 +4,13 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewHTTPS receive CONNECT request and return ConnContext
|
// NewHTTPS receive CONNECT request and return ConnContext
|
||||||
func NewHTTPS(request *http.Request, conn net.Conn, additions ...Addition) (net.Conn, *C.Metadata) {
|
func NewHTTPS(request *http.Request, conn net.Conn, additions ...Addition) (net.Conn, *C.Metadata) {
|
||||||
metadata := parseHTTPAddr(request)
|
metadata := parseHTTPAddr(request)
|
||||||
metadata.Type = C.HTTPS
|
metadata.Type = C.HTTPS
|
||||||
metadata.RawSrcAddr = conn.RemoteAddr()
|
|
||||||
metadata.RawDstAddr = conn.LocalAddr()
|
|
||||||
ApplyAdditions(metadata, WithSrcAddr(conn.RemoteAddr()), WithInAddr(conn.LocalAddr()))
|
ApplyAdditions(metadata, WithSrcAddr(conn.RemoteAddr()), WithInAddr(conn.LocalAddr()))
|
||||||
ApplyAdditions(metadata, additions...)
|
ApplyAdditions(metadata, additions...)
|
||||||
return conn, metadata
|
return conn, metadata
|
||||||
|
|||||||
@@ -1,47 +0,0 @@
|
|||||||
package inbound
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net"
|
|
||||||
"net/netip"
|
|
||||||
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
|
||||||
)
|
|
||||||
|
|
||||||
var lanAllowedIPs []netip.Prefix
|
|
||||||
var lanDisAllowedIPs []netip.Prefix
|
|
||||||
|
|
||||||
func SetAllowedIPs(prefixes []netip.Prefix) {
|
|
||||||
lanAllowedIPs = prefixes
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetDisAllowedIPs(prefixes []netip.Prefix) {
|
|
||||||
lanDisAllowedIPs = prefixes
|
|
||||||
}
|
|
||||||
|
|
||||||
func AllowedIPs() []netip.Prefix {
|
|
||||||
return lanAllowedIPs
|
|
||||||
}
|
|
||||||
|
|
||||||
func DisAllowedIPs() []netip.Prefix {
|
|
||||||
return lanDisAllowedIPs
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsRemoteAddrDisAllowed(addr net.Addr) bool {
|
|
||||||
m := C.Metadata{}
|
|
||||||
if err := m.SetRemoteAddr(addr); err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
ipAddr := m.AddrPort().Addr()
|
|
||||||
if ipAddr.IsValid() {
|
|
||||||
return isAllowed(ipAddr) && !isDisAllowed(ipAddr)
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func isAllowed(addr netip.Addr) bool {
|
|
||||||
return prefixesContains(lanAllowedIPs, addr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func isDisAllowed(addr netip.Addr) bool {
|
|
||||||
return prefixesContains(lanDisAllowedIPs, addr)
|
|
||||||
}
|
|
||||||
@@ -2,105 +2,29 @@ package inbound
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/component/keepalive"
|
"github.com/sagernet/tfo-go"
|
||||||
|
|
||||||
"github.com/metacubex/tfo-go"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
lc = tfo.ListenConfig{
|
lc = tfo.ListenConfig{
|
||||||
DisableTFO: true,
|
DisableTFO: true,
|
||||||
}
|
}
|
||||||
mutex sync.RWMutex
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetTfo(open bool) {
|
func SetTfo(open bool) {
|
||||||
mutex.Lock()
|
|
||||||
defer mutex.Unlock()
|
|
||||||
lc.DisableTFO = !open
|
lc.DisableTFO = !open
|
||||||
}
|
}
|
||||||
|
|
||||||
func Tfo() bool {
|
|
||||||
mutex.RLock()
|
|
||||||
defer mutex.RUnlock()
|
|
||||||
return !lc.DisableTFO
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetMPTCP(open bool) {
|
func SetMPTCP(open bool) {
|
||||||
mutex.Lock()
|
|
||||||
defer mutex.Unlock()
|
|
||||||
setMultiPathTCP(&lc.ListenConfig, open)
|
setMultiPathTCP(&lc.ListenConfig, open)
|
||||||
}
|
}
|
||||||
|
|
||||||
func MPTCP() bool {
|
|
||||||
mutex.RLock()
|
|
||||||
defer mutex.RUnlock()
|
|
||||||
return getMultiPathTCP(&lc.ListenConfig)
|
|
||||||
}
|
|
||||||
|
|
||||||
func preResolve(network, address string) (string, error) {
|
|
||||||
switch network { // like net.Resolver.internetAddrList but filter domain to avoid call net.Resolver.lookupIPAddr
|
|
||||||
case "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6", "ip", "ip4", "ip6":
|
|
||||||
if host, port, err := net.SplitHostPort(address); err == nil {
|
|
||||||
switch host {
|
|
||||||
case "localhost":
|
|
||||||
switch network {
|
|
||||||
case "tcp6", "udp6", "ip6":
|
|
||||||
address = net.JoinHostPort("::1", port)
|
|
||||||
default:
|
|
||||||
address = net.JoinHostPort("127.0.0.1", port)
|
|
||||||
}
|
|
||||||
case "": // internetAddrList can handle this special case
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
if _, err := netip.ParseAddr(host); err != nil { // not ip
|
|
||||||
return "", fmt.Errorf("invalid network address: %s", address)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return address, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func ListenContext(ctx context.Context, network, address string) (net.Listener, error) {
|
func ListenContext(ctx context.Context, network, address string) (net.Listener, error) {
|
||||||
address, err := preResolve(network, address)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
mutex.RLock()
|
|
||||||
defer mutex.RUnlock()
|
|
||||||
return lc.Listen(ctx, network, address)
|
return lc.Listen(ctx, network, address)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Listen(network, address string) (net.Listener, error) {
|
func Listen(network, address string) (net.Listener, error) {
|
||||||
return ListenContext(context.Background(), network, address)
|
return ListenContext(context.Background(), network, address)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ListenPacketContext(ctx context.Context, network, address string) (net.PacketConn, error) {
|
|
||||||
address, err := preResolve(network, address)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
mutex.RLock()
|
|
||||||
defer mutex.RUnlock()
|
|
||||||
return lc.ListenPacket(ctx, network, address)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ListenPacket(network, address string) (net.PacketConn, error) {
|
|
||||||
return ListenPacketContext(context.Background(), network, address)
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
keepalive.SetDisableKeepAliveCallback.Register(func(b bool) {
|
|
||||||
mutex.Lock()
|
|
||||||
defer mutex.Unlock()
|
|
||||||
keepalive.SetNetListenConfig(&lc.ListenConfig)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
//go:build !windows
|
|
||||||
|
|
||||||
package inbound
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
const SupportNamedPipe = false
|
|
||||||
|
|
||||||
func ListenNamedPipe(path string) (net.Listener, error) {
|
|
||||||
return nil, os.ErrInvalid
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
package inbound
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/metacubex/wireguard-go/ipc/namedpipe"
|
|
||||||
"golang.org/x/sys/windows"
|
|
||||||
)
|
|
||||||
|
|
||||||
const SupportNamedPipe = true
|
|
||||||
|
|
||||||
// windowsSDDL is the Security Descriptor set on the namedpipe.
|
|
||||||
// It provides read/write access to all users and the local system.
|
|
||||||
const windowsSDDL = "D:PAI(A;OICI;GWGR;;;BU)(A;OICI;GWGR;;;SY)"
|
|
||||||
|
|
||||||
func ListenNamedPipe(path string) (net.Listener, error) {
|
|
||||||
sddl := os.Getenv("LISTEN_NAMEDPIPE_SDDL")
|
|
||||||
if sddl == "" {
|
|
||||||
sddl = windowsSDDL
|
|
||||||
}
|
|
||||||
securityDescriptor, err := windows.SecurityDescriptorFromString(sddl)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
namedpipeLC := namedpipe.ListenConfig{
|
|
||||||
SecurityDescriptor: securityDescriptor,
|
|
||||||
InputBufferSize: 256 * 1024,
|
|
||||||
OutputBufferSize: 256 * 1024,
|
|
||||||
}
|
|
||||||
return namedpipeLC.Listen(path)
|
|
||||||
}
|
|
||||||
@@ -8,7 +8,3 @@ const multipathTCPAvailable = false
|
|||||||
|
|
||||||
func setMultiPathTCP(listenConfig *net.ListenConfig, open bool) {
|
func setMultiPathTCP(listenConfig *net.ListenConfig, open bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getMultiPathTCP(listenConfig *net.ListenConfig) bool {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -9,7 +9,3 @@ const multipathTCPAvailable = true
|
|||||||
func setMultiPathTCP(listenConfig *net.ListenConfig, open bool) {
|
func setMultiPathTCP(listenConfig *net.ListenConfig, open bool) {
|
||||||
listenConfig.SetMultipathTCP(open)
|
listenConfig.SetMultipathTCP(open)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getMultiPathTCP(listenConfig *net.ListenConfig) bool {
|
|
||||||
return listenConfig.MultipathTCP()
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package inbound
|
package inbound
|
||||||
|
|
||||||
import (
|
import (
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/metacubex/mihomo/transport/socks5"
|
"github.com/Dreamacro/clash/transport/socks5"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewPacket is PacketAdapter generator
|
// NewPacket is PacketAdapter generator
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ package inbound
|
|||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/metacubex/mihomo/transport/socks5"
|
"github.com/Dreamacro/clash/transport/socks5"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewSocket receive TCP inbound and return ConnContext
|
// NewSocket receive TCP inbound and return ConnContext
|
||||||
|
|||||||
@@ -7,8 +7,9 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
"github.com/Dreamacro/clash/common/nnip"
|
||||||
"github.com/metacubex/mihomo/transport/socks5"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
"github.com/Dreamacro/clash/transport/socks5"
|
||||||
)
|
)
|
||||||
|
|
||||||
func parseSocksAddr(target socks5.Addr) *C.Metadata {
|
func parseSocksAddr(target socks5.Addr) *C.Metadata {
|
||||||
@@ -20,13 +21,13 @@ func parseSocksAddr(target socks5.Addr) *C.Metadata {
|
|||||||
metadata.Host = strings.TrimRight(string(target[2:2+target[1]]), ".")
|
metadata.Host = strings.TrimRight(string(target[2:2+target[1]]), ".")
|
||||||
metadata.DstPort = uint16((int(target[2+target[1]]) << 8) | int(target[2+target[1]+1]))
|
metadata.DstPort = uint16((int(target[2+target[1]]) << 8) | int(target[2+target[1]+1]))
|
||||||
case socks5.AtypIPv4:
|
case socks5.AtypIPv4:
|
||||||
metadata.DstIP, _ = netip.AddrFromSlice(target[1 : 1+net.IPv4len])
|
metadata.DstIP = nnip.IpToAddr(net.IP(target[1 : 1+net.IPv4len]))
|
||||||
metadata.DstPort = uint16((int(target[1+net.IPv4len]) << 8) | int(target[1+net.IPv4len+1]))
|
metadata.DstPort = uint16((int(target[1+net.IPv4len]) << 8) | int(target[1+net.IPv4len+1]))
|
||||||
case socks5.AtypIPv6:
|
case socks5.AtypIPv6:
|
||||||
metadata.DstIP, _ = netip.AddrFromSlice(target[1 : 1+net.IPv6len])
|
ip6, _ := netip.AddrFromSlice(target[1 : 1+net.IPv6len])
|
||||||
|
metadata.DstIP = ip6.Unmap()
|
||||||
metadata.DstPort = uint16((int(target[1+net.IPv6len]) << 8) | int(target[1+net.IPv6len+1]))
|
metadata.DstPort = uint16((int(target[1+net.IPv6len]) << 8) | int(target[1+net.IPv6len+1]))
|
||||||
}
|
}
|
||||||
metadata.DstIP = metadata.DstIP.Unmap()
|
|
||||||
|
|
||||||
return metadata
|
return metadata
|
||||||
}
|
}
|
||||||
@@ -60,19 +61,3 @@ func parseHTTPAddr(request *http.Request) *C.Metadata {
|
|||||||
|
|
||||||
return metadata
|
return metadata
|
||||||
}
|
}
|
||||||
|
|
||||||
func prefixesContains(prefixes []netip.Prefix, addr netip.Addr) bool {
|
|
||||||
if len(prefixes) == 0 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if !addr.IsValid() {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
addr = addr.Unmap().WithZone("") // netip.Prefix.Contains returns false if ip has an IPv6 zone
|
|
||||||
for _, prefix := range prefixes {
|
|
||||||
if prefix.Contains(addr) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,140 +0,0 @@
|
|||||||
package outbound
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"net"
|
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
CN "github.com/metacubex/mihomo/common/net"
|
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
|
||||||
"github.com/metacubex/mihomo/component/resolver"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
|
||||||
"github.com/metacubex/mihomo/transport/anytls"
|
|
||||||
"github.com/metacubex/mihomo/transport/vmess"
|
|
||||||
|
|
||||||
M "github.com/metacubex/sing/common/metadata"
|
|
||||||
"github.com/metacubex/sing/common/uot"
|
|
||||||
)
|
|
||||||
|
|
||||||
type AnyTLS struct {
|
|
||||||
*Base
|
|
||||||
client *anytls.Client
|
|
||||||
dialer proxydialer.SingDialer
|
|
||||||
option *AnyTLSOption
|
|
||||||
}
|
|
||||||
|
|
||||||
type AnyTLSOption struct {
|
|
||||||
BasicOption
|
|
||||||
Name string `proxy:"name"`
|
|
||||||
Server string `proxy:"server"`
|
|
||||||
Port int `proxy:"port"`
|
|
||||||
Password string `proxy:"password"`
|
|
||||||
ALPN []string `proxy:"alpn,omitempty"`
|
|
||||||
SNI string `proxy:"sni,omitempty"`
|
|
||||||
ECHOpts ECHOptions `proxy:"ech-opts,omitempty"`
|
|
||||||
ClientFingerprint string `proxy:"client-fingerprint,omitempty"`
|
|
||||||
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
|
||||||
Fingerprint string `proxy:"fingerprint,omitempty"`
|
|
||||||
UDP bool `proxy:"udp,omitempty"`
|
|
||||||
IdleSessionCheckInterval int `proxy:"idle-session-check-interval,omitempty"`
|
|
||||||
IdleSessionTimeout int `proxy:"idle-session-timeout,omitempty"`
|
|
||||||
MinIdleSession int `proxy:"min-idle-session,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *AnyTLS) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
|
||||||
c, err := t.client.CreateProxy(ctx, M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return NewConn(c, t), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *AnyTLS) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
|
||||||
// create tcp
|
|
||||||
c, err := t.client.CreateProxy(ctx, uot.RequestDestination(2))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// create uot on tcp
|
|
||||||
if !metadata.Resolved() {
|
|
||||||
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("can't resolve ip")
|
|
||||||
}
|
|
||||||
metadata.DstIP = ip
|
|
||||||
}
|
|
||||||
destination := M.SocksaddrFromNet(metadata.UDPAddr())
|
|
||||||
return newPacketConn(CN.NewThreadSafePacketConn(uot.NewLazyConn(c, uot.Request{Destination: destination})), t), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SupportUOT implements C.ProxyAdapter
|
|
||||||
func (t *AnyTLS) SupportUOT() bool {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// ProxyInfo implements C.ProxyAdapter
|
|
||||||
func (t *AnyTLS) ProxyInfo() C.ProxyInfo {
|
|
||||||
info := t.Base.ProxyInfo()
|
|
||||||
info.DialerProxy = t.option.DialerProxy
|
|
||||||
return info
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close implements C.ProxyAdapter
|
|
||||||
func (t *AnyTLS) Close() error {
|
|
||||||
return t.client.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewAnyTLS(option AnyTLSOption) (*AnyTLS, error) {
|
|
||||||
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
|
||||||
outbound := &AnyTLS{
|
|
||||||
Base: &Base{
|
|
||||||
name: option.Name,
|
|
||||||
addr: addr,
|
|
||||||
tp: C.AnyTLS,
|
|
||||||
udp: option.UDP,
|
|
||||||
tfo: option.TFO,
|
|
||||||
mpTcp: option.MPTCP,
|
|
||||||
iface: option.Interface,
|
|
||||||
rmark: option.RoutingMark,
|
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
|
||||||
},
|
|
||||||
option: &option,
|
|
||||||
}
|
|
||||||
|
|
||||||
singDialer := proxydialer.NewByNameSingDialer(option.DialerProxy, dialer.NewDialer(outbound.DialOptions()...))
|
|
||||||
outbound.dialer = singDialer
|
|
||||||
|
|
||||||
tOption := anytls.ClientConfig{
|
|
||||||
Password: option.Password,
|
|
||||||
Server: M.ParseSocksaddrHostPort(option.Server, uint16(option.Port)),
|
|
||||||
Dialer: singDialer,
|
|
||||||
IdleSessionCheckInterval: time.Duration(option.IdleSessionCheckInterval) * time.Second,
|
|
||||||
IdleSessionTimeout: time.Duration(option.IdleSessionTimeout) * time.Second,
|
|
||||||
MinIdleSession: option.MinIdleSession,
|
|
||||||
}
|
|
||||||
echConfig, err := option.ECHOpts.Parse()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
tlsConfig := &vmess.TLSConfig{
|
|
||||||
Host: option.SNI,
|
|
||||||
SkipCertVerify: option.SkipCertVerify,
|
|
||||||
NextProtos: option.ALPN,
|
|
||||||
FingerPrint: option.Fingerprint,
|
|
||||||
ClientFingerprint: option.ClientFingerprint,
|
|
||||||
ECH: echConfig,
|
|
||||||
}
|
|
||||||
if tlsConfig.Host == "" {
|
|
||||||
tlsConfig.Host = option.Server
|
|
||||||
}
|
|
||||||
tOption.TLSConfig = tlsConfig
|
|
||||||
|
|
||||||
client := anytls.NewClient(context.TODO(), tOption)
|
|
||||||
outbound.client = client
|
|
||||||
|
|
||||||
return outbound, nil
|
|
||||||
}
|
|
||||||
@@ -4,23 +4,15 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net"
|
"net"
|
||||||
"runtime"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/Dreamacro/clash/common/utils"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/metacubex/mihomo/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ProxyAdapter interface {
|
|
||||||
C.ProxyAdapter
|
|
||||||
DialOptions() []dialer.Option
|
|
||||||
}
|
|
||||||
|
|
||||||
type Base struct {
|
type Base struct {
|
||||||
name string
|
name string
|
||||||
addr string
|
addr string
|
||||||
@@ -59,7 +51,7 @@ func (b *Base) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Me
|
|||||||
return c, C.ErrNotSupport
|
return c, C.ErrNotSupport
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Base) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
func (b *Base) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
||||||
return nil, C.ErrNotSupport
|
return nil, C.ErrNotSupport
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,7 +61,7 @@ func (b *Base) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metad
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (b *Base) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
func (b *Base) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
||||||
return nil, C.ErrNotSupport
|
return nil, C.ErrNotSupport
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,15 +85,14 @@ func (b *Base) SupportUDP() bool {
|
|||||||
return b.udp
|
return b.udp
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProxyInfo implements C.ProxyAdapter
|
// SupportXUDP implements C.ProxyAdapter
|
||||||
func (b *Base) ProxyInfo() (info C.ProxyInfo) {
|
func (b *Base) SupportXUDP() bool {
|
||||||
info.XUDP = b.xudp
|
return b.xudp
|
||||||
info.TFO = b.tfo
|
}
|
||||||
info.MPTCP = b.mpTcp
|
|
||||||
info.SMUX = false
|
// SupportTFO implements C.ProxyAdapter
|
||||||
info.Interface = b.iface
|
func (b *Base) SupportTFO() bool {
|
||||||
info.RoutingMark = b.rmark
|
return b.tfo
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsL3Protocol implements C.ProxyAdapter
|
// IsL3Protocol implements C.ProxyAdapter
|
||||||
@@ -128,7 +119,7 @@ func (b *Base) Unwrap(metadata *C.Metadata, touch bool) C.Proxy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialOptions return []dialer.Option from struct
|
// DialOptions return []dialer.Option from struct
|
||||||
func (b *Base) DialOptions() (opts []dialer.Option) {
|
func (b *Base) DialOptions(opts ...dialer.Option) []dialer.Option {
|
||||||
if b.iface != "" {
|
if b.iface != "" {
|
||||||
opts = append(opts, dialer.WithInterface(b.iface))
|
opts = append(opts, dialer.WithInterface(b.iface))
|
||||||
}
|
}
|
||||||
@@ -160,16 +151,12 @@ func (b *Base) DialOptions() (opts []dialer.Option) {
|
|||||||
return opts
|
return opts
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Base) Close() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type BasicOption struct {
|
type BasicOption struct {
|
||||||
TFO bool `proxy:"tfo,omitempty"`
|
TFO bool `proxy:"tfo,omitempty" group:"tfo,omitempty"`
|
||||||
MPTCP bool `proxy:"mptcp,omitempty"`
|
MPTCP bool `proxy:"mptcp,omitempty" group:"mptcp,omitempty"`
|
||||||
Interface string `proxy:"interface-name,omitempty"`
|
Interface string `proxy:"interface-name,omitempty" group:"interface-name,omitempty"`
|
||||||
RoutingMark int `proxy:"routing-mark,omitempty"`
|
RoutingMark int `proxy:"routing-mark,omitempty" group:"routing-mark,omitempty"`
|
||||||
IPVersion string `proxy:"ip-version,omitempty"`
|
IPVersion string `proxy:"ip-version,omitempty" group:"ip-version,omitempty"`
|
||||||
DialerProxy string `proxy:"dialer-proxy,omitempty"` // don't apply this option into groups, but can set a group name in a proxy
|
DialerProxy string `proxy:"dialer-proxy,omitempty"` // don't apply this option into groups, but can set a group name in a proxy
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -233,10 +220,6 @@ func (c *conn) ReaderReplaceable() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *conn) AddRef(ref any) {
|
|
||||||
c.ExtendedConn = N.NewRefConn(c.ExtendedConn, ref) // add ref for autoCloseProxyAdapter
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewConn(c net.Conn, a C.ProxyAdapter) C.Conn {
|
func NewConn(c net.Conn, a C.ProxyAdapter) C.Conn {
|
||||||
if _, ok := c.(syscall.Conn); !ok { // exclusion system conn like *net.TCPConn
|
if _, ok := c.(syscall.Conn); !ok { // exclusion system conn like *net.TCPConn
|
||||||
c = N.NewDeadlineConn(c) // most conn from outbound can't handle readDeadline correctly
|
c = N.NewDeadlineConn(c) // most conn from outbound can't handle readDeadline correctly
|
||||||
@@ -283,10 +266,6 @@ func (c *packetConn) ReaderReplaceable() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *packetConn) AddRef(ref any) {
|
|
||||||
c.EnhancePacketConn = N.NewRefPacketConn(c.EnhancePacketConn, ref) // add ref for autoCloseProxyAdapter
|
|
||||||
}
|
|
||||||
|
|
||||||
func newPacketConn(pc net.PacketConn, a C.ProxyAdapter) C.PacketConn {
|
func newPacketConn(pc net.PacketConn, a C.ProxyAdapter) C.PacketConn {
|
||||||
epc := N.NewEnhancePacketConn(pc)
|
epc := N.NewEnhancePacketConn(pc)
|
||||||
if _, ok := pc.(syscall.Conn); !ok { // exclusion system conn like *net.UDPConn
|
if _, ok := pc.(syscall.Conn); !ok { // exclusion system conn like *net.UDPConn
|
||||||
@@ -306,75 +285,3 @@ func parseRemoteDestination(addr string) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type AddRef interface {
|
|
||||||
AddRef(ref any)
|
|
||||||
}
|
|
||||||
|
|
||||||
type autoCloseProxyAdapter struct {
|
|
||||||
ProxyAdapter
|
|
||||||
closeOnce sync.Once
|
|
||||||
closeErr error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *autoCloseProxyAdapter) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
|
||||||
c, err := p.ProxyAdapter.DialContext(ctx, metadata)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if c, ok := c.(AddRef); ok {
|
|
||||||
c.AddRef(p)
|
|
||||||
}
|
|
||||||
return c, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *autoCloseProxyAdapter) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) {
|
|
||||||
c, err := p.ProxyAdapter.DialContextWithDialer(ctx, dialer, metadata)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if c, ok := c.(AddRef); ok {
|
|
||||||
c.AddRef(p)
|
|
||||||
}
|
|
||||||
return c, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *autoCloseProxyAdapter) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
|
||||||
pc, err := p.ProxyAdapter.ListenPacketContext(ctx, metadata)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if pc, ok := pc.(AddRef); ok {
|
|
||||||
pc.AddRef(p)
|
|
||||||
}
|
|
||||||
return pc, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *autoCloseProxyAdapter) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
|
||||||
pc, err := p.ProxyAdapter.ListenPacketWithDialer(ctx, dialer, metadata)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if pc, ok := pc.(AddRef); ok {
|
|
||||||
pc.AddRef(p)
|
|
||||||
}
|
|
||||||
return pc, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *autoCloseProxyAdapter) Close() error {
|
|
||||||
p.closeOnce.Do(func() {
|
|
||||||
log.Debugln("Closing outdated proxy [%s]", p.Name())
|
|
||||||
runtime.SetFinalizer(p, nil)
|
|
||||||
p.closeErr = p.ProxyAdapter.Close()
|
|
||||||
})
|
|
||||||
return p.closeErr
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewAutoCloseProxyAdapter(adapter ProxyAdapter) ProxyAdapter {
|
|
||||||
proxy := &autoCloseProxyAdapter{
|
|
||||||
ProxyAdapter: adapter,
|
|
||||||
}
|
|
||||||
// auto close ProxyAdapter
|
|
||||||
runtime.SetFinalizer(proxy, (*autoCloseProxyAdapter).Close)
|
|
||||||
return proxy
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -3,15 +3,16 @@ package outbound
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"net/netip"
|
||||||
"github.com/metacubex/mihomo/component/loopback"
|
|
||||||
"github.com/metacubex/mihomo/component/resolver"
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
|
"github.com/Dreamacro/clash/component/resolver"
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Direct struct {
|
type Direct struct {
|
||||||
*Base
|
*Base
|
||||||
loopBack *loopback.Detector
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type DirectOption struct {
|
type DirectOption struct {
|
||||||
@@ -20,41 +21,31 @@ type DirectOption struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (d *Direct) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
func (d *Direct) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
||||||
if err := d.loopBack.CheckConn(metadata); err != nil {
|
opts = append(opts, dialer.WithResolver(resolver.DefaultResolver))
|
||||||
return nil, err
|
c, err := dialer.DialContext(ctx, "tcp", metadata.RemoteAddress(), d.Base.DialOptions(opts...)...)
|
||||||
}
|
|
||||||
opts := d.DialOptions()
|
|
||||||
opts = append(opts, dialer.WithResolver(resolver.DirectHostResolver))
|
|
||||||
c, err := dialer.DialContext(ctx, "tcp", metadata.RemoteAddress(), opts...)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return d.loopBack.NewConn(NewConn(c, d)), nil
|
N.TCPKeepAlive(c)
|
||||||
|
return NewConn(c, d), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (d *Direct) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
func (d *Direct) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
||||||
if err := d.loopBack.CheckPacketConn(metadata); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// net.UDPConn.WriteTo only working with *net.UDPAddr, so we need a net.UDPAddr
|
// net.UDPConn.WriteTo only working with *net.UDPAddr, so we need a net.UDPAddr
|
||||||
if !metadata.Resolved() {
|
if !metadata.Resolved() {
|
||||||
ip, err := resolver.ResolveIPWithResolver(ctx, metadata.Host, resolver.DirectHostResolver)
|
ip, err := resolver.ResolveIPWithResolver(ctx, metadata.Host, resolver.DefaultResolver)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New("can't resolve ip")
|
return nil, errors.New("can't resolve ip")
|
||||||
}
|
}
|
||||||
metadata.DstIP = ip
|
metadata.DstIP = ip
|
||||||
}
|
}
|
||||||
pc, err := dialer.NewDialer(d.DialOptions()...).ListenPacket(ctx, "udp", "", metadata.AddrPort())
|
pc, err := dialer.NewDialer(d.Base.DialOptions(opts...)...).ListenPacket(ctx, "udp", "", netip.AddrPortFrom(metadata.DstIP, metadata.DstPort))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return d.loopBack.NewPacketConn(newPacketConn(pc, d)), nil
|
return newPacketConn(pc, d), nil
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Direct) IsL3Protocol(metadata *C.Metadata) bool {
|
|
||||||
return true // tell DNSDialer don't send domain to DialContext, avoid lookback to DefaultResolver
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDirectWithOption(option DirectOption) *Direct {
|
func NewDirectWithOption(option DirectOption) *Direct {
|
||||||
@@ -69,7 +60,6 @@ func NewDirectWithOption(option DirectOption) *Direct {
|
|||||||
rmark: option.RoutingMark,
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: C.NewDNSPrefer(option.IPVersion),
|
||||||
},
|
},
|
||||||
loopBack: loopback.NewDetector(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,7 +71,6 @@ func NewDirect() *Direct {
|
|||||||
udp: true,
|
udp: true,
|
||||||
prefer: C.DualStack,
|
prefer: C.DualStack,
|
||||||
},
|
},
|
||||||
loopBack: loopback.NewDetector(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,6 +82,5 @@ func NewCompatible() *Direct {
|
|||||||
udp: true,
|
udp: true,
|
||||||
prefer: C.DualStack,
|
prefer: C.DualStack,
|
||||||
},
|
},
|
||||||
loopBack: loopback.NewDetector(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,158 +0,0 @@
|
|||||||
package outbound
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"net"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
|
||||||
"github.com/metacubex/mihomo/common/pool"
|
|
||||||
"github.com/metacubex/mihomo/component/resolver"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
|
||||||
"github.com/metacubex/mihomo/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Dns struct {
|
|
||||||
*Base
|
|
||||||
}
|
|
||||||
|
|
||||||
type DnsOption struct {
|
|
||||||
BasicOption
|
|
||||||
Name string `proxy:"name"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
|
||||||
func (d *Dns) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
|
||||||
left, right := N.Pipe()
|
|
||||||
go resolver.RelayDnsConn(context.Background(), right, 0)
|
|
||||||
return NewConn(left, d), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
|
||||||
func (d *Dns) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
|
||||||
log.Debugln("[DNS] hijack udp:%s from %s", metadata.RemoteAddress(), metadata.SourceAddrPort())
|
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
|
|
||||||
return newPacketConn(&dnsPacketConn{
|
|
||||||
response: make(chan dnsPacket, 1),
|
|
||||||
ctx: ctx,
|
|
||||||
cancel: cancel,
|
|
||||||
}, d), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type dnsPacket struct {
|
|
||||||
data []byte
|
|
||||||
put func()
|
|
||||||
addr net.Addr
|
|
||||||
}
|
|
||||||
|
|
||||||
// dnsPacketConn implements net.PacketConn
|
|
||||||
type dnsPacketConn struct {
|
|
||||||
response chan dnsPacket
|
|
||||||
ctx context.Context
|
|
||||||
cancel context.CancelFunc
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *dnsPacketConn) WaitReadFrom() (data []byte, put func(), addr net.Addr, err error) {
|
|
||||||
select {
|
|
||||||
case packet := <-d.response:
|
|
||||||
return packet.data, packet.put, packet.addr, nil
|
|
||||||
case <-d.ctx.Done():
|
|
||||||
return nil, nil, nil, net.ErrClosed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *dnsPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
|
|
||||||
select {
|
|
||||||
case packet := <-d.response:
|
|
||||||
n = copy(p, packet.data)
|
|
||||||
if packet.put != nil {
|
|
||||||
packet.put()
|
|
||||||
}
|
|
||||||
return n, packet.addr, nil
|
|
||||||
case <-d.ctx.Done():
|
|
||||||
return 0, nil, net.ErrClosed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *dnsPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
|
||||||
select {
|
|
||||||
case <-d.ctx.Done():
|
|
||||||
return 0, net.ErrClosed
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(p) > resolver.SafeDnsPacketSize {
|
|
||||||
// wtf???
|
|
||||||
return len(p), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
buf := pool.Get(resolver.SafeDnsPacketSize)
|
|
||||||
put := func() { _ = pool.Put(buf) }
|
|
||||||
copy(buf, p) // avoid p be changed after WriteTo returned
|
|
||||||
|
|
||||||
go func() { // don't block the WriteTo function
|
|
||||||
ctx, cancel := context.WithTimeout(d.ctx, resolver.DefaultDnsRelayTimeout)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
buf, err = resolver.RelayDnsPacket(ctx, buf[:len(p)], buf)
|
|
||||||
if err != nil {
|
|
||||||
put()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
packet := dnsPacket{
|
|
||||||
data: buf,
|
|
||||||
put: put,
|
|
||||||
addr: addr,
|
|
||||||
}
|
|
||||||
select {
|
|
||||||
case d.response <- packet:
|
|
||||||
break
|
|
||||||
case <-d.ctx.Done():
|
|
||||||
put()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
return len(p), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *dnsPacketConn) Close() error {
|
|
||||||
d.cancel()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*dnsPacketConn) LocalAddr() net.Addr {
|
|
||||||
return &net.UDPAddr{
|
|
||||||
IP: net.IPv4(127, 0, 0, 1),
|
|
||||||
Port: 53,
|
|
||||||
Zone: "",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*dnsPacketConn) SetDeadline(t time.Time) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*dnsPacketConn) SetReadDeadline(t time.Time) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*dnsPacketConn) SetWriteDeadline(t time.Time) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewDnsWithOption(option DnsOption) *Dns {
|
|
||||||
return &Dns{
|
|
||||||
Base: &Base{
|
|
||||||
name: option.Name,
|
|
||||||
tp: C.Dns,
|
|
||||||
udp: true,
|
|
||||||
tfo: option.TFO,
|
|
||||||
mpTcp: option.MPTCP,
|
|
||||||
iface: option.Interface,
|
|
||||||
rmark: option.RoutingMark,
|
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
package outbound
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/base64"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/component/ech"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ECHOptions struct {
|
|
||||||
Enable bool `proxy:"enable,omitempty" obfs:"enable,omitempty"`
|
|
||||||
Config string `proxy:"config,omitempty" obfs:"config,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (o ECHOptions) Parse() (*ech.Config, error) {
|
|
||||||
if !o.Enable {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
echConfig := &ech.Config{}
|
|
||||||
if o.Config != "" {
|
|
||||||
list, err := base64.StdEncoding.DecodeString(o.Config)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("base64 decode ech config string failed: %v", err)
|
|
||||||
}
|
|
||||||
echConfig.EncryptedClientHelloConfigList = list
|
|
||||||
}
|
|
||||||
return echConfig, nil
|
|
||||||
}
|
|
||||||
@@ -7,15 +7,17 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/metacubex/mihomo/component/ca"
|
"github.com/Dreamacro/clash/component/ca"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
"github.com/Dreamacro/clash/component/proxydialer"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Http struct {
|
type Http struct {
|
||||||
@@ -51,15 +53,15 @@ func (h *Http) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Me
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := h.shakeHandContext(ctx, c, metadata); err != nil {
|
if err := h.shakeHand(metadata, c); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (h *Http) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
func (h *Http) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
||||||
return h.DialContextWithDialer(ctx, dialer.NewDialer(h.DialOptions()...), metadata)
|
return h.DialContextWithDialer(ctx, dialer.NewDialer(h.Base.DialOptions(opts...)...), metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialContextWithDialer implements C.ProxyAdapter
|
// DialContextWithDialer implements C.ProxyAdapter
|
||||||
@@ -74,6 +76,7 @@ func (h *Http) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metad
|
|||||||
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)
|
||||||
}
|
}
|
||||||
|
N.TCPKeepAlive(c)
|
||||||
|
|
||||||
defer func(c net.Conn) {
|
defer func(c net.Conn) {
|
||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
@@ -92,19 +95,7 @@ func (h *Http) SupportWithDialer() C.NetWork {
|
|||||||
return C.TCP
|
return C.TCP
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProxyInfo implements C.ProxyAdapter
|
func (h *Http) shakeHand(metadata *C.Metadata, rw io.ReadWriter) error {
|
||||||
func (h *Http) ProxyInfo() C.ProxyInfo {
|
|
||||||
info := h.Base.ProxyInfo()
|
|
||||||
info.DialerProxy = h.option.DialerProxy
|
|
||||||
return info
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Http) shakeHandContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (err error) {
|
|
||||||
if ctx.Done() != nil {
|
|
||||||
done := N.SetupContextForConn(ctx, c)
|
|
||||||
defer done(&err)
|
|
||||||
}
|
|
||||||
|
|
||||||
addr := metadata.RemoteAddress()
|
addr := metadata.RemoteAddress()
|
||||||
HeaderString := "CONNECT " + addr + " HTTP/1.1\r\n"
|
HeaderString := "CONNECT " + addr + " HTTP/1.1\r\n"
|
||||||
tempHeaders := map[string]string{
|
tempHeaders := map[string]string{
|
||||||
@@ -128,13 +119,13 @@ func (h *Http) shakeHandContext(ctx context.Context, c net.Conn, metadata *C.Met
|
|||||||
|
|
||||||
HeaderString += "\r\n"
|
HeaderString += "\r\n"
|
||||||
|
|
||||||
_, err = c.Write([]byte(HeaderString))
|
_, err := rw.Write([]byte(HeaderString))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := http.ReadResponse(bufio.NewReader(c), nil)
|
resp, err := http.ReadResponse(bufio.NewReader(rw), nil)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -10,23 +10,21 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/component/ca"
|
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
|
||||||
"github.com/metacubex/mihomo/component/ech"
|
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
|
||||||
tlsC "github.com/metacubex/mihomo/component/tls"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
|
||||||
"github.com/metacubex/mihomo/log"
|
|
||||||
hyCongestion "github.com/metacubex/mihomo/transport/hysteria/congestion"
|
|
||||||
"github.com/metacubex/mihomo/transport/hysteria/core"
|
|
||||||
"github.com/metacubex/mihomo/transport/hysteria/obfs"
|
|
||||||
"github.com/metacubex/mihomo/transport/hysteria/pmtud_fix"
|
|
||||||
"github.com/metacubex/mihomo/transport/hysteria/transport"
|
|
||||||
"github.com/metacubex/mihomo/transport/hysteria/utils"
|
|
||||||
|
|
||||||
"github.com/metacubex/quic-go"
|
"github.com/metacubex/quic-go"
|
||||||
"github.com/metacubex/quic-go/congestion"
|
"github.com/metacubex/quic-go/congestion"
|
||||||
M "github.com/metacubex/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
|
|
||||||
|
"github.com/Dreamacro/clash/component/ca"
|
||||||
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
|
"github.com/Dreamacro/clash/component/proxydialer"
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
"github.com/Dreamacro/clash/log"
|
||||||
|
hyCongestion "github.com/Dreamacro/clash/transport/hysteria/congestion"
|
||||||
|
"github.com/Dreamacro/clash/transport/hysteria/core"
|
||||||
|
"github.com/Dreamacro/clash/transport/hysteria/obfs"
|
||||||
|
"github.com/Dreamacro/clash/transport/hysteria/pmtud_fix"
|
||||||
|
"github.com/Dreamacro/clash/transport/hysteria/transport"
|
||||||
|
"github.com/Dreamacro/clash/transport/hysteria/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -45,13 +43,10 @@ type Hysteria struct {
|
|||||||
|
|
||||||
option *HysteriaOption
|
option *HysteriaOption
|
||||||
client *core.Client
|
client *core.Client
|
||||||
|
|
||||||
tlsConfig *tlsC.Config
|
|
||||||
echConfig *ech.Config
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hysteria) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
func (h *Hysteria) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
||||||
tcpConn, err := h.client.DialTCP(metadata.String(), metadata.DstPort, h.genHdc(ctx))
|
tcpConn, err := h.client.DialTCP(metadata.String(), metadata.DstPort, h.genHdc(ctx, opts...))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -59,77 +54,61 @@ func (h *Hysteria) DialContext(ctx context.Context, metadata *C.Metadata) (C.Con
|
|||||||
return NewConn(tcpConn, h), nil
|
return NewConn(tcpConn, h), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hysteria) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
func (h *Hysteria) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
||||||
udpConn, err := h.client.DialUDP(h.genHdc(ctx))
|
udpConn, err := h.client.DialUDP(h.genHdc(ctx, opts...))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return newPacketConn(&hyPacketConn{udpConn}, h), nil
|
return newPacketConn(&hyPacketConn{udpConn}, h), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hysteria) genHdc(ctx context.Context) utils.PacketDialer {
|
func (h *Hysteria) genHdc(ctx context.Context, opts ...dialer.Option) utils.PacketDialer {
|
||||||
return &hyDialerWithContext{
|
return &hyDialerWithContext{
|
||||||
ctx: context.Background(),
|
ctx: context.Background(),
|
||||||
hyDialer: func(network string, rAddr net.Addr) (net.PacketConn, error) {
|
hyDialer: func(network string) (net.PacketConn, error) {
|
||||||
var err error
|
var err error
|
||||||
var cDialer C.Dialer = dialer.NewDialer(h.DialOptions()...)
|
var cDialer C.Dialer = dialer.NewDialer(h.Base.DialOptions(opts...)...)
|
||||||
if len(h.option.DialerProxy) > 0 {
|
if len(h.option.DialerProxy) > 0 {
|
||||||
cDialer, err = proxydialer.NewByName(h.option.DialerProxy, cDialer)
|
cDialer, err = proxydialer.NewByName(h.option.DialerProxy, cDialer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rAddrPort, _ := netip.ParseAddrPort(rAddr.String())
|
rAddrPort, _ := netip.ParseAddrPort(h.Addr())
|
||||||
return cDialer.ListenPacket(ctx, network, "", rAddrPort)
|
return cDialer.ListenPacket(ctx, network, "", rAddrPort)
|
||||||
},
|
},
|
||||||
remoteAddr: func(addr string) (net.Addr, error) {
|
remoteAddr: func(addr string) (net.Addr, error) {
|
||||||
udpAddr, err := resolveUDPAddr(ctx, "udp", addr, h.prefer)
|
return resolveUDPAddrWithPrefer(ctx, "udp", addr, h.prefer)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
err = h.echConfig.ClientHandle(ctx, h.tlsConfig)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return udpAddr, nil
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProxyInfo implements C.ProxyAdapter
|
|
||||||
func (h *Hysteria) ProxyInfo() C.ProxyInfo {
|
|
||||||
info := h.Base.ProxyInfo()
|
|
||||||
info.DialerProxy = h.option.DialerProxy
|
|
||||||
return info
|
|
||||||
}
|
|
||||||
|
|
||||||
type HysteriaOption struct {
|
type HysteriaOption struct {
|
||||||
BasicOption
|
BasicOption
|
||||||
Name string `proxy:"name"`
|
Name string `proxy:"name"`
|
||||||
Server string `proxy:"server"`
|
Server string `proxy:"server"`
|
||||||
Port int `proxy:"port,omitempty"`
|
Port int `proxy:"port,omitempty"`
|
||||||
Ports string `proxy:"ports,omitempty"`
|
Ports string `proxy:"ports,omitempty"`
|
||||||
Protocol string `proxy:"protocol,omitempty"`
|
Protocol string `proxy:"protocol,omitempty"`
|
||||||
ObfsProtocol string `proxy:"obfs-protocol,omitempty"` // compatible with Stash
|
ObfsProtocol string `proxy:"obfs-protocol,omitempty"` // compatible with Stash
|
||||||
Up string `proxy:"up"`
|
Up string `proxy:"up"`
|
||||||
UpSpeed int `proxy:"up-speed,omitempty"` // compatible with Stash
|
UpSpeed int `proxy:"up-speed,omitempty"` // compatible with Stash
|
||||||
Down string `proxy:"down"`
|
Down string `proxy:"down"`
|
||||||
DownSpeed int `proxy:"down-speed,omitempty"` // compatible with Stash
|
DownSpeed int `proxy:"down-speed,omitempty"` // compatible with Stash
|
||||||
Auth string `proxy:"auth,omitempty"`
|
Auth string `proxy:"auth,omitempty"`
|
||||||
AuthString string `proxy:"auth-str,omitempty"`
|
AuthString string `proxy:"auth-str,omitempty"`
|
||||||
Obfs string `proxy:"obfs,omitempty"`
|
Obfs string `proxy:"obfs,omitempty"`
|
||||||
SNI string `proxy:"sni,omitempty"`
|
SNI string `proxy:"sni,omitempty"`
|
||||||
ECHOpts ECHOptions `proxy:"ech-opts,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"`
|
||||||
FastOpen bool `proxy:"fast-open,omitempty"`
|
HopInterval int `proxy:"hop-interval,omitempty"`
|
||||||
HopInterval int `proxy:"hop-interval,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *HysteriaOption) Speed() (uint64, uint64, error) {
|
func (c *HysteriaOption) Speed() (uint64, uint64, error) {
|
||||||
@@ -148,7 +127,11 @@ func (c *HysteriaOption) Speed() (uint64, uint64, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewHysteria(option HysteriaOption) (*Hysteria, error) {
|
func NewHysteria(option HysteriaOption) (*Hysteria, error) {
|
||||||
clientTransport := &transport.ClientTransport{}
|
clientTransport := &transport.ClientTransport{
|
||||||
|
Dialer: &net.Dialer{
|
||||||
|
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
|
ports := option.Ports
|
||||||
|
|
||||||
@@ -174,13 +157,6 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
|
|||||||
} else {
|
} else {
|
||||||
tlsConfig.NextProtos = []string{DefaultALPN}
|
tlsConfig.NextProtos = []string{DefaultALPN}
|
||||||
}
|
}
|
||||||
|
|
||||||
echConfig, err := option.ECHOpts.Parse()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
tlsClientConfig := tlsC.UConfig(tlsConfig)
|
|
||||||
|
|
||||||
quicConfig := &quic.Config{
|
quicConfig := &quic.Config{
|
||||||
InitialStreamReceiveWindow: uint64(option.ReceiveWindowConn),
|
InitialStreamReceiveWindow: uint64(option.ReceiveWindowConn),
|
||||||
MaxStreamReceiveWindow: uint64(option.ReceiveWindowConn),
|
MaxStreamReceiveWindow: uint64(option.ReceiveWindowConn),
|
||||||
@@ -235,14 +211,14 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
|
|||||||
down = uint64(option.DownSpeed * mbpsToBps)
|
down = uint64(option.DownSpeed * mbpsToBps)
|
||||||
}
|
}
|
||||||
client, err := core.NewClient(
|
client, err := core.NewClient(
|
||||||
addr, ports, option.Protocol, auth, tlsClientConfig, 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, hopInterval, option.FastOpen,
|
}, 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)
|
||||||
}
|
}
|
||||||
outbound := &Hysteria{
|
return &Hysteria{
|
||||||
Base: &Base{
|
Base: &Base{
|
||||||
name: option.Name,
|
name: option.Name,
|
||||||
addr: addr,
|
addr: addr,
|
||||||
@@ -253,21 +229,9 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
|
|||||||
rmark: option.RoutingMark,
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: C.NewDNSPrefer(option.IPVersion),
|
||||||
},
|
},
|
||||||
option: &option,
|
option: &option,
|
||||||
client: client,
|
client: client,
|
||||||
tlsConfig: tlsClientConfig,
|
}, nil
|
||||||
echConfig: echConfig,
|
|
||||||
}
|
|
||||||
|
|
||||||
return outbound, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close implements C.ProxyAdapter
|
|
||||||
func (h *Hysteria) Close() error {
|
|
||||||
if h.client != nil {
|
|
||||||
return h.client.Close()
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type hyPacketConn struct {
|
type hyPacketConn struct {
|
||||||
@@ -304,7 +268,7 @@ func (c *hyPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type hyDialerWithContext struct {
|
type hyDialerWithContext struct {
|
||||||
hyDialer func(network string, rAddr net.Addr) (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)
|
||||||
}
|
}
|
||||||
@@ -314,7 +278,7 @@ func (h *hyDialerWithContext) ListenPacket(rAddr net.Addr) (net.PacketConn, erro
|
|||||||
if addrPort, err := netip.ParseAddrPort(rAddr.String()); err == nil {
|
if addrPort, err := netip.ParseAddrPort(rAddr.String()); err == nil {
|
||||||
network = dialer.ParseNetwork(network, addrPort.Addr())
|
network = dialer.ParseNetwork(network, addrPort.Addr())
|
||||||
}
|
}
|
||||||
return h.hyDialer(network, rAddr)
|
return h.hyDialer(network)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *hyDialerWithContext) Context() context.Context {
|
func (h *hyDialerWithContext) Context() context.Context {
|
||||||
|
|||||||
@@ -6,31 +6,25 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
|
||||||
|
|
||||||
CN "github.com/metacubex/mihomo/common/net"
|
CN "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/Dreamacro/clash/component/ca"
|
||||||
"github.com/metacubex/mihomo/component/ca"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"github.com/Dreamacro/clash/component/proxydialer"
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
tlsC "github.com/metacubex/mihomo/component/tls"
|
tuicCommon "github.com/Dreamacro/clash/transport/tuic/common"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
|
||||||
"github.com/metacubex/mihomo/log"
|
|
||||||
tuicCommon "github.com/metacubex/mihomo/transport/tuic/common"
|
|
||||||
|
|
||||||
"github.com/metacubex/quic-go"
|
|
||||||
"github.com/metacubex/sing-quic/hysteria2"
|
"github.com/metacubex/sing-quic/hysteria2"
|
||||||
M "github.com/metacubex/sing/common/metadata"
|
|
||||||
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
hysteria2.SetCongestionController = tuicCommon.SetCongestionController
|
hysteria2.SetCongestionController = tuicCommon.SetCongestionController
|
||||||
}
|
}
|
||||||
|
|
||||||
const minHopInterval = 5
|
|
||||||
const defaultHopInterval = 30
|
|
||||||
|
|
||||||
type Hysteria2 struct {
|
type Hysteria2 struct {
|
||||||
*Base
|
*Base
|
||||||
|
|
||||||
@@ -41,42 +35,36 @@ type Hysteria2 struct {
|
|||||||
|
|
||||||
type Hysteria2Option struct {
|
type Hysteria2Option struct {
|
||||||
BasicOption
|
BasicOption
|
||||||
Name string `proxy:"name"`
|
Name string `proxy:"name"`
|
||||||
Server string `proxy:"server"`
|
Server string `proxy:"server"`
|
||||||
Port int `proxy:"port,omitempty"`
|
Port int `proxy:"port"`
|
||||||
Ports string `proxy:"ports,omitempty"`
|
Up string `proxy:"up,omitempty"`
|
||||||
HopInterval int `proxy:"hop-interval,omitempty"`
|
Down string `proxy:"down,omitempty"`
|
||||||
Up string `proxy:"up,omitempty"`
|
Password string `proxy:"password,omitempty"`
|
||||||
Down string `proxy:"down,omitempty"`
|
Obfs string `proxy:"obfs,omitempty"`
|
||||||
Password string `proxy:"password,omitempty"`
|
ObfsPassword string `proxy:"obfs-password,omitempty"`
|
||||||
Obfs string `proxy:"obfs,omitempty"`
|
SNI string `proxy:"sni,omitempty"`
|
||||||
ObfsPassword string `proxy:"obfs-password,omitempty"`
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
||||||
SNI string `proxy:"sni,omitempty"`
|
Fingerprint string `proxy:"fingerprint,omitempty"`
|
||||||
ECHOpts ECHOptions `proxy:"ech-opts,omitempty"`
|
ALPN []string `proxy:"alpn,omitempty"`
|
||||||
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
CustomCA string `proxy:"ca,omitempty"`
|
||||||
Fingerprint string `proxy:"fingerprint,omitempty"`
|
CustomCAString string `proxy:"ca-str,omitempty"`
|
||||||
ALPN []string `proxy:"alpn,omitempty"`
|
CWND int `proxy:"cwnd,omitempty"`
|
||||||
CustomCA string `proxy:"ca,omitempty"`
|
|
||||||
CustomCAString string `proxy:"ca-str,omitempty"`
|
|
||||||
CWND int `proxy:"cwnd,omitempty"`
|
|
||||||
UdpMTU int `proxy:"udp-mtu,omitempty"`
|
|
||||||
|
|
||||||
// quic-go special config
|
|
||||||
InitialStreamReceiveWindow uint64 `proxy:"initial-stream-receive-window,omitempty"`
|
|
||||||
MaxStreamReceiveWindow uint64 `proxy:"max-stream-receive-window,omitempty"`
|
|
||||||
InitialConnectionReceiveWindow uint64 `proxy:"initial-connection-receive-window,omitempty"`
|
|
||||||
MaxConnectionReceiveWindow uint64 `proxy:"max-connection-receive-window,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hysteria2) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
func (h *Hysteria2) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
||||||
|
options := h.Base.DialOptions(opts...)
|
||||||
|
h.dialer.SetDialer(dialer.NewDialer(options...))
|
||||||
c, err := h.client.DialConn(ctx, M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort))
|
c, err := h.client.DialConn(ctx, M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return NewConn(c, h), nil
|
return NewConn(CN.NewRefConn(c, h), h), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hysteria2) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
func (h *Hysteria2) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
||||||
|
options := h.Base.DialOptions(opts...)
|
||||||
|
h.dialer.SetDialer(dialer.NewDialer(options...))
|
||||||
pc, err := h.client.ListenPacket(ctx)
|
pc, err := h.client.ListenPacket(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -84,42 +72,17 @@ func (h *Hysteria2) ListenPacketContext(ctx context.Context, metadata *C.Metadat
|
|||||||
if pc == nil {
|
if pc == nil {
|
||||||
return nil, errors.New("packetConn is nil")
|
return nil, errors.New("packetConn is nil")
|
||||||
}
|
}
|
||||||
return newPacketConn(CN.NewThreadSafePacketConn(pc), h), nil
|
return newPacketConn(CN.NewRefPacketConn(CN.NewThreadSafePacketConn(pc), h), h), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close implements C.ProxyAdapter
|
func closeHysteria2(h *Hysteria2) {
|
||||||
func (h *Hysteria2) Close() error {
|
|
||||||
if h.client != nil {
|
if h.client != nil {
|
||||||
return h.client.CloseWithError(errors.New("proxy removed"))
|
_ = h.client.CloseWithError(errors.New("proxy removed"))
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ProxyInfo implements C.ProxyAdapter
|
|
||||||
func (h *Hysteria2) ProxyInfo() C.ProxyInfo {
|
|
||||||
info := h.Base.ProxyInfo()
|
|
||||||
info.DialerProxy = h.option.DialerProxy
|
|
||||||
return info
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHysteria2(option Hysteria2Option) (*Hysteria2, error) {
|
func NewHysteria2(option Hysteria2Option) (*Hysteria2, error) {
|
||||||
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
||||||
outbound := &Hysteria2{
|
|
||||||
Base: &Base{
|
|
||||||
name: option.Name,
|
|
||||||
addr: addr,
|
|
||||||
tp: C.Hysteria2,
|
|
||||||
udp: true,
|
|
||||||
iface: option.Interface,
|
|
||||||
rmark: option.RoutingMark,
|
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
|
||||||
},
|
|
||||||
option: &option,
|
|
||||||
}
|
|
||||||
|
|
||||||
singDialer := proxydialer.NewByNameSingDialer(option.DialerProxy, dialer.NewDialer(outbound.DialOptions()...))
|
|
||||||
outbound.dialer = singDialer
|
|
||||||
|
|
||||||
var salamanderPassword string
|
var salamanderPassword string
|
||||||
if len(option.Obfs) > 0 {
|
if len(option.Obfs) > 0 {
|
||||||
if option.ObfsPassword == "" {
|
if option.ObfsPassword == "" {
|
||||||
@@ -154,81 +117,41 @@ func NewHysteria2(option Hysteria2Option) (*Hysteria2, error) {
|
|||||||
tlsConfig.NextProtos = option.ALPN
|
tlsConfig.NextProtos = option.ALPN
|
||||||
}
|
}
|
||||||
|
|
||||||
tlsClientConfig := tlsC.UConfig(tlsConfig)
|
singDialer := proxydialer.NewByNameSingDialer(option.DialerProxy, dialer.NewDialer())
|
||||||
echConfig, err := option.ECHOpts.Parse()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if option.UdpMTU == 0 {
|
|
||||||
// "1200" from quic-go's MaxDatagramSize
|
|
||||||
// "-3" from quic-go's DatagramFrame.MaxDataLen
|
|
||||||
option.UdpMTU = 1200 - 3
|
|
||||||
}
|
|
||||||
|
|
||||||
quicConfig := &quic.Config{
|
|
||||||
InitialStreamReceiveWindow: option.InitialStreamReceiveWindow,
|
|
||||||
MaxStreamReceiveWindow: option.MaxStreamReceiveWindow,
|
|
||||||
InitialConnectionReceiveWindow: option.InitialConnectionReceiveWindow,
|
|
||||||
MaxConnectionReceiveWindow: option.MaxConnectionReceiveWindow,
|
|
||||||
}
|
|
||||||
|
|
||||||
clientOptions := hysteria2.ClientOptions{
|
clientOptions := hysteria2.ClientOptions{
|
||||||
Context: context.TODO(),
|
Context: context.TODO(),
|
||||||
Dialer: singDialer,
|
Dialer: singDialer,
|
||||||
Logger: log.SingLogger,
|
ServerAddress: M.ParseSocksaddrHostPort(option.Server, uint16(option.Port)),
|
||||||
SendBPS: StringToBps(option.Up),
|
SendBPS: StringToBps(option.Up),
|
||||||
ReceiveBPS: StringToBps(option.Down),
|
ReceiveBPS: StringToBps(option.Down),
|
||||||
SalamanderPassword: salamanderPassword,
|
SalamanderPassword: salamanderPassword,
|
||||||
Password: option.Password,
|
Password: option.Password,
|
||||||
TLSConfig: tlsClientConfig,
|
TLSConfig: tlsConfig,
|
||||||
QUICConfig: quicConfig,
|
|
||||||
UDPDisabled: false,
|
UDPDisabled: false,
|
||||||
CWND: option.CWND,
|
CWND: option.CWND,
|
||||||
UdpMTU: option.UdpMTU,
|
|
||||||
ServerAddress: func(ctx context.Context) (*net.UDPAddr, error) {
|
|
||||||
udpAddr, err := resolveUDPAddr(ctx, "udp", addr, C.NewDNSPrefer(option.IPVersion))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
err = echConfig.ClientHandle(ctx, tlsClientConfig)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return udpAddr, nil
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
var ranges utils.IntRanges[uint16]
|
|
||||||
var serverPorts []uint16
|
|
||||||
if option.Ports != "" {
|
|
||||||
ranges, err = utils.NewUnsignedRanges[uint16](option.Ports)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
ranges.Range(func(port uint16) bool {
|
|
||||||
serverPorts = append(serverPorts, port)
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
if len(serverPorts) > 0 {
|
|
||||||
if option.HopInterval == 0 {
|
|
||||||
option.HopInterval = defaultHopInterval
|
|
||||||
} else if option.HopInterval < minHopInterval {
|
|
||||||
option.HopInterval = minHopInterval
|
|
||||||
}
|
|
||||||
clientOptions.HopInterval = time.Duration(option.HopInterval) * time.Second
|
|
||||||
clientOptions.ServerPorts = serverPorts
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if option.Port == 0 && len(serverPorts) == 0 {
|
|
||||||
return nil, errors.New("invalid port")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := hysteria2.NewClient(clientOptions)
|
client, err := hysteria2.NewClient(clientOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
outbound.client = client
|
|
||||||
|
outbound := &Hysteria2{
|
||||||
|
Base: &Base{
|
||||||
|
name: option.Name,
|
||||||
|
addr: addr,
|
||||||
|
tp: C.Hysteria2,
|
||||||
|
udp: true,
|
||||||
|
iface: option.Interface,
|
||||||
|
rmark: option.RoutingMark,
|
||||||
|
prefer: C.NewDNSPrefer(option.IPVersion),
|
||||||
|
},
|
||||||
|
option: &option,
|
||||||
|
client: client,
|
||||||
|
dialer: singDialer,
|
||||||
|
}
|
||||||
|
runtime.SetFinalizer(outbound, closeHysteria2)
|
||||||
|
|
||||||
return outbound, nil
|
return outbound, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,301 +0,0 @@
|
|||||||
package outbound
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
"strconv"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
CN "github.com/metacubex/mihomo/common/net"
|
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
|
||||||
|
|
||||||
mieruclient "github.com/enfein/mieru/v3/apis/client"
|
|
||||||
mierucommon "github.com/enfein/mieru/v3/apis/common"
|
|
||||||
mierumodel "github.com/enfein/mieru/v3/apis/model"
|
|
||||||
mierupb "github.com/enfein/mieru/v3/pkg/appctl/appctlpb"
|
|
||||||
"google.golang.org/protobuf/proto"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Mieru struct {
|
|
||||||
*Base
|
|
||||||
option *MieruOption
|
|
||||||
client mieruclient.Client
|
|
||||||
mu sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
type MieruOption struct {
|
|
||||||
BasicOption
|
|
||||||
Name string `proxy:"name"`
|
|
||||||
Server string `proxy:"server"`
|
|
||||||
Port int `proxy:"port,omitempty"`
|
|
||||||
PortRange string `proxy:"port-range,omitempty"`
|
|
||||||
Transport string `proxy:"transport"`
|
|
||||||
UDP bool `proxy:"udp,omitempty"`
|
|
||||||
UserName string `proxy:"username"`
|
|
||||||
Password string `proxy:"password"`
|
|
||||||
Multiplexing string `proxy:"multiplexing,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
|
||||||
func (m *Mieru) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
|
||||||
if err := m.ensureClientIsRunning(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
addr := metadataToMieruNetAddrSpec(metadata)
|
|
||||||
c, err := m.client.DialContext(ctx, addr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("dial to %s failed: %w", addr, err)
|
|
||||||
}
|
|
||||||
return NewConn(c, m), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
|
||||||
func (m *Mieru) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
|
||||||
if err := m.ensureClientIsRunning(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
c, err := m.client.DialContext(ctx, metadata.UDPAddr())
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("dial to %s failed: %w", metadata.UDPAddr(), err)
|
|
||||||
}
|
|
||||||
return newPacketConn(CN.NewThreadSafePacketConn(mierucommon.NewUDPAssociateWrapper(mierucommon.NewPacketOverStreamTunnel(c))), m), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SupportUOT implements C.ProxyAdapter
|
|
||||||
func (m *Mieru) SupportUOT() bool {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// ProxyInfo implements C.ProxyAdapter
|
|
||||||
func (m *Mieru) ProxyInfo() C.ProxyInfo {
|
|
||||||
info := m.Base.ProxyInfo()
|
|
||||||
info.DialerProxy = m.option.DialerProxy
|
|
||||||
return info
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Mieru) ensureClientIsRunning() error {
|
|
||||||
m.mu.Lock()
|
|
||||||
defer m.mu.Unlock()
|
|
||||||
|
|
||||||
if m.client.IsRunning() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a dialer and add it to the client config, before starting the client.
|
|
||||||
var dialer C.Dialer = dialer.NewDialer(m.DialOptions()...)
|
|
||||||
var err error
|
|
||||||
if len(m.option.DialerProxy) > 0 {
|
|
||||||
dialer, err = proxydialer.NewByName(m.option.DialerProxy, dialer)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
config, err := m.client.Load()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
config.Dialer = dialer
|
|
||||||
if err := m.client.Store(config); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := m.client.Start(); err != nil {
|
|
||||||
return fmt.Errorf("failed to start mieru client: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMieru(option MieruOption) (*Mieru, error) {
|
|
||||||
config, err := buildMieruClientConfig(option)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to build mieru client config: %w", err)
|
|
||||||
}
|
|
||||||
c := mieruclient.NewClient()
|
|
||||||
if err := c.Store(config); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to store mieru client config: %w", err)
|
|
||||||
}
|
|
||||||
// Client is started lazily on the first use.
|
|
||||||
|
|
||||||
var addr string
|
|
||||||
if option.Port != 0 {
|
|
||||||
addr = net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
|
||||||
} else {
|
|
||||||
beginPort, _, _ := beginAndEndPortFromPortRange(option.PortRange)
|
|
||||||
addr = net.JoinHostPort(option.Server, strconv.Itoa(beginPort))
|
|
||||||
}
|
|
||||||
outbound := &Mieru{
|
|
||||||
Base: &Base{
|
|
||||||
name: option.Name,
|
|
||||||
addr: addr,
|
|
||||||
iface: option.Interface,
|
|
||||||
tp: C.Mieru,
|
|
||||||
udp: option.UDP,
|
|
||||||
xudp: false,
|
|
||||||
rmark: option.RoutingMark,
|
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
|
||||||
},
|
|
||||||
option: &option,
|
|
||||||
client: c,
|
|
||||||
}
|
|
||||||
return outbound, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close implements C.ProxyAdapter
|
|
||||||
func (m *Mieru) Close() error {
|
|
||||||
m.mu.Lock()
|
|
||||||
defer m.mu.Unlock()
|
|
||||||
if m.client != nil && m.client.IsRunning() {
|
|
||||||
return m.client.Stop()
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func metadataToMieruNetAddrSpec(metadata *C.Metadata) mierumodel.NetAddrSpec {
|
|
||||||
if metadata.Host != "" {
|
|
||||||
return mierumodel.NetAddrSpec{
|
|
||||||
AddrSpec: mierumodel.AddrSpec{
|
|
||||||
FQDN: metadata.Host,
|
|
||||||
Port: int(metadata.DstPort),
|
|
||||||
},
|
|
||||||
Net: "tcp",
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return mierumodel.NetAddrSpec{
|
|
||||||
AddrSpec: mierumodel.AddrSpec{
|
|
||||||
IP: metadata.DstIP.AsSlice(),
|
|
||||||
Port: int(metadata.DstPort),
|
|
||||||
},
|
|
||||||
Net: "tcp",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func buildMieruClientConfig(option MieruOption) (*mieruclient.ClientConfig, error) {
|
|
||||||
if err := validateMieruOption(option); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to validate mieru option: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
transportProtocol := mierupb.TransportProtocol_TCP.Enum()
|
|
||||||
var server *mierupb.ServerEndpoint
|
|
||||||
if net.ParseIP(option.Server) != nil {
|
|
||||||
// server is an IP address
|
|
||||||
if option.PortRange != "" {
|
|
||||||
server = &mierupb.ServerEndpoint{
|
|
||||||
IpAddress: proto.String(option.Server),
|
|
||||||
PortBindings: []*mierupb.PortBinding{
|
|
||||||
{
|
|
||||||
PortRange: proto.String(option.PortRange),
|
|
||||||
Protocol: transportProtocol,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
server = &mierupb.ServerEndpoint{
|
|
||||||
IpAddress: proto.String(option.Server),
|
|
||||||
PortBindings: []*mierupb.PortBinding{
|
|
||||||
{
|
|
||||||
Port: proto.Int32(int32(option.Port)),
|
|
||||||
Protocol: transportProtocol,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// server is a domain name
|
|
||||||
if option.PortRange != "" {
|
|
||||||
server = &mierupb.ServerEndpoint{
|
|
||||||
DomainName: proto.String(option.Server),
|
|
||||||
PortBindings: []*mierupb.PortBinding{
|
|
||||||
{
|
|
||||||
PortRange: proto.String(option.PortRange),
|
|
||||||
Protocol: transportProtocol,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
server = &mierupb.ServerEndpoint{
|
|
||||||
DomainName: proto.String(option.Server),
|
|
||||||
PortBindings: []*mierupb.PortBinding{
|
|
||||||
{
|
|
||||||
Port: proto.Int32(int32(option.Port)),
|
|
||||||
Protocol: transportProtocol,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
config := &mieruclient.ClientConfig{
|
|
||||||
Profile: &mierupb.ClientProfile{
|
|
||||||
ProfileName: proto.String(option.Name),
|
|
||||||
User: &mierupb.User{
|
|
||||||
Name: proto.String(option.UserName),
|
|
||||||
Password: proto.String(option.Password),
|
|
||||||
},
|
|
||||||
Servers: []*mierupb.ServerEndpoint{server},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
if multiplexing, ok := mierupb.MultiplexingLevel_value[option.Multiplexing]; ok {
|
|
||||||
config.Profile.Multiplexing = &mierupb.MultiplexingConfig{
|
|
||||||
Level: mierupb.MultiplexingLevel(multiplexing).Enum(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return config, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func validateMieruOption(option MieruOption) error {
|
|
||||||
if option.Name == "" {
|
|
||||||
return fmt.Errorf("name is empty")
|
|
||||||
}
|
|
||||||
if option.Server == "" {
|
|
||||||
return fmt.Errorf("server is empty")
|
|
||||||
}
|
|
||||||
if option.Port == 0 && option.PortRange == "" {
|
|
||||||
return fmt.Errorf("either port or port-range must be set")
|
|
||||||
}
|
|
||||||
if option.Port != 0 && option.PortRange != "" {
|
|
||||||
return fmt.Errorf("port and port-range cannot be set at the same time")
|
|
||||||
}
|
|
||||||
if option.Port != 0 && (option.Port < 1 || option.Port > 65535) {
|
|
||||||
return fmt.Errorf("port must be between 1 and 65535")
|
|
||||||
}
|
|
||||||
if option.PortRange != "" {
|
|
||||||
begin, end, err := beginAndEndPortFromPortRange(option.PortRange)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("invalid port-range format")
|
|
||||||
}
|
|
||||||
if begin < 1 || begin > 65535 {
|
|
||||||
return fmt.Errorf("begin port must be between 1 and 65535")
|
|
||||||
}
|
|
||||||
if end < 1 || end > 65535 {
|
|
||||||
return fmt.Errorf("end port must be between 1 and 65535")
|
|
||||||
}
|
|
||||||
if begin > end {
|
|
||||||
return fmt.Errorf("begin port must be less than or equal to end port")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if option.Transport != "TCP" {
|
|
||||||
return fmt.Errorf("transport must be TCP")
|
|
||||||
}
|
|
||||||
if option.UserName == "" {
|
|
||||||
return fmt.Errorf("username is empty")
|
|
||||||
}
|
|
||||||
if option.Password == "" {
|
|
||||||
return fmt.Errorf("password is empty")
|
|
||||||
}
|
|
||||||
if option.Multiplexing != "" {
|
|
||||||
if _, ok := mierupb.MultiplexingLevel_value[option.Multiplexing]; !ok {
|
|
||||||
return fmt.Errorf("invalid multiplexing level: %s", option.Multiplexing)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func beginAndEndPortFromPortRange(portRange string) (int, int, error) {
|
|
||||||
var begin, end int
|
|
||||||
_, err := fmt.Sscanf(portRange, "%d-%d", &begin, &end)
|
|
||||||
return begin, end, err
|
|
||||||
}
|
|
||||||
@@ -1,92 +0,0 @@
|
|||||||
package outbound
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func TestNewMieru(t *testing.T) {
|
|
||||||
testCases := []struct {
|
|
||||||
option MieruOption
|
|
||||||
wantBaseAddr string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
option: MieruOption{
|
|
||||||
Name: "test",
|
|
||||||
Server: "1.2.3.4",
|
|
||||||
Port: 10000,
|
|
||||||
Transport: "TCP",
|
|
||||||
UserName: "test",
|
|
||||||
Password: "test",
|
|
||||||
},
|
|
||||||
wantBaseAddr: "1.2.3.4:10000",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
option: MieruOption{
|
|
||||||
Name: "test",
|
|
||||||
Server: "2001:db8::1",
|
|
||||||
PortRange: "10001-10002",
|
|
||||||
Transport: "TCP",
|
|
||||||
UserName: "test",
|
|
||||||
Password: "test",
|
|
||||||
},
|
|
||||||
wantBaseAddr: "[2001:db8::1]:10001",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
option: MieruOption{
|
|
||||||
Name: "test",
|
|
||||||
Server: "example.com",
|
|
||||||
Port: 10003,
|
|
||||||
Transport: "TCP",
|
|
||||||
UserName: "test",
|
|
||||||
Password: "test",
|
|
||||||
},
|
|
||||||
wantBaseAddr: "example.com:10003",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, testCase := range testCases {
|
|
||||||
mieru, err := NewMieru(testCase.option)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
if mieru.addr != testCase.wantBaseAddr {
|
|
||||||
t.Errorf("got addr %q, want %q", mieru.addr, testCase.wantBaseAddr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBeginAndEndPortFromPortRange(t *testing.T) {
|
|
||||||
testCases := []struct {
|
|
||||||
input string
|
|
||||||
begin int
|
|
||||||
end int
|
|
||||||
hasErr bool
|
|
||||||
}{
|
|
||||||
{"1-10", 1, 10, false},
|
|
||||||
{"1000-2000", 1000, 2000, false},
|
|
||||||
{"65535-65535", 65535, 65535, false},
|
|
||||||
{"1", 0, 0, true},
|
|
||||||
{"1-", 0, 0, true},
|
|
||||||
{"-10", 0, 0, true},
|
|
||||||
{"a-b", 0, 0, true},
|
|
||||||
{"1-b", 0, 0, true},
|
|
||||||
{"a-10", 0, 0, true},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, testCase := range testCases {
|
|
||||||
begin, end, err := beginAndEndPortFromPortRange(testCase.input)
|
|
||||||
if testCase.hasErr {
|
|
||||||
if err == nil {
|
|
||||||
t.Errorf("beginAndEndPortFromPortRange(%s) should return an error", testCase.input)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("beginAndEndPortFromPortRange(%s) should not return an error, but got %v", testCase.input, err)
|
|
||||||
}
|
|
||||||
if begin != testCase.begin {
|
|
||||||
t.Errorf("beginAndEndPortFromPortRange(%s) begin port mismatch, got %d, want %d", testCase.input, begin, testCase.begin)
|
|
||||||
}
|
|
||||||
if end != testCase.end {
|
|
||||||
t.Errorf("beginAndEndPortFromPortRange(%s) end port mismatch, got %d, want %d", testCase.input, end, testCase.end)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,41 +1,29 @@
|
|||||||
package outbound
|
package outbound
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/ecdh"
|
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
tlsC "github.com/metacubex/mihomo/component/tls"
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/curve25519"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RealityOptions struct {
|
type RealityOptions struct {
|
||||||
PublicKey string `proxy:"public-key"`
|
PublicKey string `proxy:"public-key"`
|
||||||
ShortID string `proxy:"short-id"`
|
ShortID string `proxy:"short-id"`
|
||||||
|
|
||||||
SupportX25519MLKEM768 bool `proxy:"support-x25519mlkem768"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o RealityOptions) Parse() (*tlsC.RealityConfig, error) {
|
func (o RealityOptions) Parse() (*tlsC.RealityConfig, error) {
|
||||||
if o.PublicKey != "" {
|
if o.PublicKey != "" {
|
||||||
config := new(tlsC.RealityConfig)
|
config := new(tlsC.RealityConfig)
|
||||||
config.SupportX25519MLKEM768 = o.SupportX25519MLKEM768
|
|
||||||
|
|
||||||
const x25519ScalarSize = 32
|
n, err := base64.RawURLEncoding.Decode(config.PublicKey[:], []byte(o.PublicKey))
|
||||||
publicKey, err := base64.RawURLEncoding.DecodeString(o.PublicKey)
|
if err != nil || n != curve25519.ScalarSize {
|
||||||
if err != nil || len(publicKey) != x25519ScalarSize {
|
|
||||||
return nil, errors.New("invalid REALITY public key")
|
return nil, errors.New("invalid REALITY public key")
|
||||||
}
|
}
|
||||||
config.PublicKey, err = ecdh.X25519().NewPublicKey(publicKey)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("fail to create REALITY public key: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
n := hex.DecodedLen(len(o.ShortID))
|
|
||||||
if n > tlsC.RealityMaxShortIDLen {
|
|
||||||
return nil, errors.New("invalid REALITY short id")
|
|
||||||
}
|
|
||||||
n, err = hex.Decode(config.ShortID[:], []byte(o.ShortID))
|
n, err = hex.Decode(config.ShortID[:], []byte(o.ShortID))
|
||||||
if err != nil || n > tlsC.RealityMaxShortIDLen {
|
if err != nil || n > tlsC.RealityMaxShortIDLen {
|
||||||
return nil, errors.New("invalid REALITY short ID")
|
return nil, errors.New("invalid REALITY short ID")
|
||||||
|
|||||||
@@ -6,13 +6,13 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/buf"
|
"github.com/Dreamacro/clash/common/buf"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Reject struct {
|
type Reject struct {
|
||||||
*Base
|
*Base
|
||||||
drop bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type RejectOption struct {
|
type RejectOption struct {
|
||||||
@@ -20,23 +20,20 @@ type RejectOption struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (r *Reject) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
func (r *Reject) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
||||||
if r.drop {
|
|
||||||
return NewConn(dropConn{}, r), nil
|
|
||||||
}
|
|
||||||
return NewConn(nopConn{}, r), nil
|
return NewConn(nopConn{}, r), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (r *Reject) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
func (r *Reject) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
||||||
return newPacketConn(&nopPacketConn{}, r), nil
|
return newPacketConn(nopPacketConn{}, r), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRejectWithOption(option RejectOption) *Reject {
|
func NewRejectWithOption(option RejectOption) *Reject {
|
||||||
return &Reject{
|
return &Reject{
|
||||||
Base: &Base{
|
Base: &Base{
|
||||||
name: option.Name,
|
name: option.Name,
|
||||||
tp: C.Reject,
|
tp: C.Direct,
|
||||||
udp: true,
|
udp: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -53,18 +50,6 @@ func NewReject() *Reject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRejectDrop() *Reject {
|
|
||||||
return &Reject{
|
|
||||||
Base: &Base{
|
|
||||||
name: "REJECT-DROP",
|
|
||||||
tp: C.RejectDrop,
|
|
||||||
udp: true,
|
|
||||||
prefer: C.DualStack,
|
|
||||||
},
|
|
||||||
drop: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewPass() *Reject {
|
func NewPass() *Reject {
|
||||||
return &Reject{
|
return &Reject{
|
||||||
Base: &Base{
|
Base: &Base{
|
||||||
@@ -78,29 +63,35 @@ func NewPass() *Reject {
|
|||||||
|
|
||||||
type nopConn struct{}
|
type nopConn struct{}
|
||||||
|
|
||||||
func (rw nopConn) Read(b []byte) (int, error) { return 0, io.EOF }
|
func (rw nopConn) Read(b []byte) (int, error) {
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
|
||||||
func (rw nopConn) ReadBuffer(buffer *buf.Buffer) error { return io.EOF }
|
func (rw nopConn) ReadBuffer(buffer *buf.Buffer) error {
|
||||||
|
return io.EOF
|
||||||
|
}
|
||||||
|
|
||||||
func (rw nopConn) Write(b []byte) (int, error) { return 0, io.EOF }
|
func (rw nopConn) Write(b []byte) (int, error) {
|
||||||
func (rw nopConn) WriteBuffer(buffer *buf.Buffer) error { return io.EOF }
|
return 0, io.EOF
|
||||||
func (rw nopConn) Close() error { return nil }
|
}
|
||||||
func (rw nopConn) LocalAddr() net.Addr { return nil }
|
|
||||||
func (rw nopConn) RemoteAddr() net.Addr { return nil }
|
func (rw nopConn) WriteBuffer(buffer *buf.Buffer) error {
|
||||||
func (rw nopConn) SetDeadline(time.Time) error { return nil }
|
return io.EOF
|
||||||
func (rw nopConn) SetReadDeadline(time.Time) error { return nil }
|
}
|
||||||
func (rw nopConn) SetWriteDeadline(time.Time) error { return nil }
|
|
||||||
|
func (rw nopConn) Close() error { return nil }
|
||||||
|
func (rw nopConn) LocalAddr() net.Addr { return nil }
|
||||||
|
func (rw nopConn) RemoteAddr() net.Addr { return nil }
|
||||||
|
func (rw nopConn) SetDeadline(time.Time) error { return nil }
|
||||||
|
func (rw nopConn) SetReadDeadline(time.Time) error { return nil }
|
||||||
|
func (rw nopConn) SetWriteDeadline(time.Time) error { return nil }
|
||||||
|
|
||||||
var udpAddrIPv4Unspecified = &net.UDPAddr{IP: net.IPv4zero, Port: 0}
|
var udpAddrIPv4Unspecified = &net.UDPAddr{IP: net.IPv4zero, Port: 0}
|
||||||
|
|
||||||
type nopPacketConn struct{}
|
type nopPacketConn struct{}
|
||||||
|
|
||||||
func (npc nopPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
func (npc nopPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) { return len(b), nil }
|
||||||
return len(b), nil
|
func (npc nopPacketConn) ReadFrom(b []byte) (int, net.Addr, error) { return 0, nil, io.EOF }
|
||||||
}
|
|
||||||
func (npc nopPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
|
||||||
return 0, nil, io.EOF
|
|
||||||
}
|
|
||||||
func (npc nopPacketConn) WaitReadFrom() ([]byte, func(), net.Addr, error) {
|
func (npc nopPacketConn) WaitReadFrom() ([]byte, func(), net.Addr, error) {
|
||||||
return nil, nil, nil, io.EOF
|
return nil, nil, nil, io.EOF
|
||||||
}
|
}
|
||||||
@@ -109,19 +100,3 @@ func (npc nopPacketConn) LocalAddr() net.Addr { return udpAddrIPv4U
|
|||||||
func (npc nopPacketConn) SetDeadline(time.Time) error { return nil }
|
func (npc nopPacketConn) SetDeadline(time.Time) error { return nil }
|
||||||
func (npc nopPacketConn) SetReadDeadline(time.Time) error { return nil }
|
func (npc nopPacketConn) SetReadDeadline(time.Time) error { return nil }
|
||||||
func (npc nopPacketConn) SetWriteDeadline(time.Time) error { return nil }
|
func (npc nopPacketConn) SetWriteDeadline(time.Time) error { return nil }
|
||||||
|
|
||||||
type dropConn struct{}
|
|
||||||
|
|
||||||
func (rw dropConn) Read(b []byte) (int, error) { return 0, io.EOF }
|
|
||||||
func (rw dropConn) ReadBuffer(buffer *buf.Buffer) error {
|
|
||||||
time.Sleep(C.DefaultDropTime)
|
|
||||||
return io.EOF
|
|
||||||
}
|
|
||||||
func (rw dropConn) Write(b []byte) (int, error) { return 0, io.EOF }
|
|
||||||
func (rw dropConn) WriteBuffer(buffer *buf.Buffer) error { return io.EOF }
|
|
||||||
func (rw dropConn) Close() error { return nil }
|
|
||||||
func (rw dropConn) LocalAddr() net.Addr { return nil }
|
|
||||||
func (rw dropConn) RemoteAddr() net.Addr { return nil }
|
|
||||||
func (rw dropConn) SetDeadline(time.Time) error { return nil }
|
|
||||||
func (rw dropConn) SetReadDeadline(time.Time) error { return nil }
|
|
||||||
func (rw dropConn) SetWriteDeadline(time.Time) error { return nil }
|
|
||||||
|
|||||||
@@ -7,22 +7,21 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/metacubex/mihomo/common/structure"
|
"github.com/Dreamacro/clash/common/structure"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
"github.com/Dreamacro/clash/component/proxydialer"
|
||||||
"github.com/metacubex/mihomo/component/resolver"
|
"github.com/Dreamacro/clash/component/resolver"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
gost "github.com/metacubex/mihomo/transport/gost-plugin"
|
"github.com/Dreamacro/clash/transport/restls"
|
||||||
"github.com/metacubex/mihomo/transport/restls"
|
obfs "github.com/Dreamacro/clash/transport/simple-obfs"
|
||||||
obfs "github.com/metacubex/mihomo/transport/simple-obfs"
|
shadowtls "github.com/Dreamacro/clash/transport/sing-shadowtls"
|
||||||
shadowtls "github.com/metacubex/mihomo/transport/sing-shadowtls"
|
v2rayObfs "github.com/Dreamacro/clash/transport/v2ray-plugin"
|
||||||
v2rayObfs "github.com/metacubex/mihomo/transport/v2ray-plugin"
|
|
||||||
|
|
||||||
|
restlsC "github.com/3andne/restls-client-go"
|
||||||
shadowsocks "github.com/metacubex/sing-shadowsocks2"
|
shadowsocks "github.com/metacubex/sing-shadowsocks2"
|
||||||
"github.com/metacubex/sing/common/bufio"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
M "github.com/metacubex/sing/common/metadata"
|
"github.com/sagernet/sing/common/uot"
|
||||||
"github.com/metacubex/sing/common/uot"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ShadowSocks struct {
|
type ShadowSocks struct {
|
||||||
@@ -34,9 +33,8 @@ type ShadowSocks struct {
|
|||||||
obfsMode string
|
obfsMode string
|
||||||
obfsOption *simpleObfsOption
|
obfsOption *simpleObfsOption
|
||||||
v2rayOption *v2rayObfs.Option
|
v2rayOption *v2rayObfs.Option
|
||||||
gostOption *gost.Option
|
|
||||||
shadowTLSOption *shadowtls.ShadowTLSOption
|
shadowTLSOption *shadowtls.ShadowTLSOption
|
||||||
restlsConfig *restls.Config
|
restlsConfig *restlsC.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
type ShadowSocksOption struct {
|
type ShadowSocksOption struct {
|
||||||
@@ -60,25 +58,10 @@ type simpleObfsOption struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type v2rayObfsOption struct {
|
type v2rayObfsOption struct {
|
||||||
Mode string `obfs:"mode"`
|
|
||||||
Host string `obfs:"host,omitempty"`
|
|
||||||
Path string `obfs:"path,omitempty"`
|
|
||||||
TLS bool `obfs:"tls,omitempty"`
|
|
||||||
ECHOpts ECHOptions `obfs:"ech-opts,omitempty"`
|
|
||||||
Fingerprint string `obfs:"fingerprint,omitempty"`
|
|
||||||
Headers map[string]string `obfs:"headers,omitempty"`
|
|
||||||
SkipCertVerify bool `obfs:"skip-cert-verify,omitempty"`
|
|
||||||
Mux bool `obfs:"mux,omitempty"`
|
|
||||||
V2rayHttpUpgrade bool `obfs:"v2ray-http-upgrade,omitempty"`
|
|
||||||
V2rayHttpUpgradeFastOpen bool `obfs:"v2ray-http-upgrade-fast-open,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type gostObfsOption struct {
|
|
||||||
Mode string `obfs:"mode"`
|
Mode string `obfs:"mode"`
|
||||||
Host string `obfs:"host,omitempty"`
|
Host string `obfs:"host,omitempty"`
|
||||||
Path string `obfs:"path,omitempty"`
|
Path string `obfs:"path,omitempty"`
|
||||||
TLS bool `obfs:"tls,omitempty"`
|
TLS bool `obfs:"tls,omitempty"`
|
||||||
ECHOpts ECHOptions `obfs:"ech-opts,omitempty"`
|
|
||||||
Fingerprint string `obfs:"fingerprint,omitempty"`
|
Fingerprint string `obfs:"fingerprint,omitempty"`
|
||||||
Headers map[string]string `obfs:"headers,omitempty"`
|
Headers map[string]string `obfs:"headers,omitempty"`
|
||||||
SkipCertVerify bool `obfs:"skip-cert-verify,omitempty"`
|
SkipCertVerify bool `obfs:"skip-cert-verify,omitempty"`
|
||||||
@@ -86,12 +69,11 @@ type gostObfsOption struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type shadowTLSOption struct {
|
type shadowTLSOption struct {
|
||||||
Password string `obfs:"password,omitempty"`
|
Password string `obfs:"password"`
|
||||||
Host string `obfs:"host"`
|
Host string `obfs:"host"`
|
||||||
Fingerprint string `obfs:"fingerprint,omitempty"`
|
Fingerprint string `obfs:"fingerprint,omitempty"`
|
||||||
SkipCertVerify bool `obfs:"skip-cert-verify,omitempty"`
|
SkipCertVerify bool `obfs:"skip-cert-verify,omitempty"`
|
||||||
Version int `obfs:"version,omitempty"`
|
Version int `obfs:"version,omitempty"`
|
||||||
ALPN []string `obfs:"alpn,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type restlsOption struct {
|
type restlsOption struct {
|
||||||
@@ -102,7 +84,7 @@ type restlsOption struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// StreamConnContext implements C.ProxyAdapter
|
// StreamConnContext implements C.ProxyAdapter
|
||||||
func (ss *ShadowSocks) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ net.Conn, err error) {
|
func (ss *ShadowSocks) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
||||||
useEarly := false
|
useEarly := false
|
||||||
switch ss.obfsMode {
|
switch ss.obfsMode {
|
||||||
case "tls":
|
case "tls":
|
||||||
@@ -111,23 +93,20 @@ func (ss *ShadowSocks) StreamConnContext(ctx context.Context, c net.Conn, metada
|
|||||||
_, port, _ := net.SplitHostPort(ss.addr)
|
_, port, _ := net.SplitHostPort(ss.addr)
|
||||||
c = obfs.NewHTTPObfs(c, ss.obfsOption.Host, port)
|
c = obfs.NewHTTPObfs(c, ss.obfsOption.Host, port)
|
||||||
case "websocket":
|
case "websocket":
|
||||||
if ss.v2rayOption != nil {
|
var err error
|
||||||
c, err = v2rayObfs.NewV2rayObfs(ctx, c, ss.v2rayOption)
|
c, err = v2rayObfs.NewV2rayObfs(ctx, c, ss.v2rayOption)
|
||||||
} else if ss.gostOption != nil {
|
|
||||||
c, err = gost.NewGostWebsocket(ctx, c, ss.gostOption)
|
|
||||||
} else {
|
|
||||||
return nil, fmt.Errorf("plugin options is required")
|
|
||||||
}
|
|
||||||
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:
|
case shadowtls.Mode:
|
||||||
|
var err error
|
||||||
c, err = shadowtls.NewShadowTLS(ctx, c, ss.shadowTLSOption)
|
c, err = shadowtls.NewShadowTLS(ctx, c, ss.shadowTLSOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
useEarly = true
|
useEarly = true
|
||||||
case restls.Mode:
|
case restls.Mode:
|
||||||
|
var err error
|
||||||
c, err = restls.NewRestls(ctx, c, ss.restlsConfig)
|
c, err = restls.NewRestls(ctx, c, ss.restlsConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s (restls) connect error: %w", ss.addr, err)
|
return nil, fmt.Errorf("%s (restls) connect error: %w", ss.addr, err)
|
||||||
@@ -135,12 +114,6 @@ func (ss *ShadowSocks) StreamConnContext(ctx context.Context, c net.Conn, metada
|
|||||||
useEarly = true
|
useEarly = true
|
||||||
}
|
}
|
||||||
useEarly = useEarly || N.NeedHandshake(c)
|
useEarly = useEarly || N.NeedHandshake(c)
|
||||||
if !useEarly {
|
|
||||||
if ctx.Done() != nil {
|
|
||||||
done := N.SetupContextForConn(ctx, c)
|
|
||||||
defer done(&err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if metadata.NetWork == C.UDP && ss.option.UDPOverTCP {
|
if metadata.NetWork == C.UDP && ss.option.UDPOverTCP {
|
||||||
uotDestination := uot.RequestDestination(uint8(ss.option.UDPOverTCPVersion))
|
uotDestination := uot.RequestDestination(uint8(ss.option.UDPOverTCPVersion))
|
||||||
if useEarly {
|
if useEarly {
|
||||||
@@ -157,8 +130,8 @@ func (ss *ShadowSocks) StreamConnContext(ctx context.Context, c net.Conn, metada
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (ss *ShadowSocks) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
func (ss *ShadowSocks) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
||||||
return ss.DialContextWithDialer(ctx, dialer.NewDialer(ss.DialOptions()...), metadata)
|
return ss.DialContextWithDialer(ctx, dialer.NewDialer(ss.Base.DialOptions(opts...)...), metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialContextWithDialer implements C.ProxyAdapter
|
// DialContextWithDialer implements C.ProxyAdapter
|
||||||
@@ -173,6 +146,7 @@ func (ss *ShadowSocks) DialContextWithDialer(ctx context.Context, dialer C.Diale
|
|||||||
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)
|
||||||
}
|
}
|
||||||
|
N.TCPKeepAlive(c)
|
||||||
|
|
||||||
defer func(c net.Conn) {
|
defer func(c net.Conn) {
|
||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
@@ -183,12 +157,18 @@ func (ss *ShadowSocks) DialContextWithDialer(ctx context.Context, dialer C.Diale
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (ss *ShadowSocks) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
func (ss *ShadowSocks) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
||||||
return ss.ListenPacketWithDialer(ctx, dialer.NewDialer(ss.DialOptions()...), metadata)
|
return ss.ListenPacketWithDialer(ctx, dialer.NewDialer(ss.Base.DialOptions(opts...)...), metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketWithDialer implements C.ProxyAdapter
|
// ListenPacketWithDialer implements C.ProxyAdapter
|
||||||
func (ss *ShadowSocks) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
func (ss *ShadowSocks) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
|
if len(ss.option.DialerProxy) > 0 {
|
||||||
|
dialer, err = proxydialer.NewByName(ss.option.DialerProxy, dialer)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
if ss.option.UDPOverTCP {
|
if ss.option.UDPOverTCP {
|
||||||
tcpConn, err := ss.DialContextWithDialer(ctx, dialer, metadata)
|
tcpConn, err := ss.DialContextWithDialer(ctx, dialer, metadata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -196,13 +176,7 @@ func (ss *ShadowSocks) ListenPacketWithDialer(ctx context.Context, dialer C.Dial
|
|||||||
}
|
}
|
||||||
return ss.ListenPacketOnStreamConn(ctx, tcpConn, metadata)
|
return ss.ListenPacketOnStreamConn(ctx, tcpConn, metadata)
|
||||||
}
|
}
|
||||||
if len(ss.option.DialerProxy) > 0 {
|
addr, err := resolveUDPAddrWithPrefer(ctx, "udp", ss.addr, ss.prefer)
|
||||||
dialer, err = proxydialer.NewByName(ss.option.DialerProxy, dialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
addr, err := resolveUDPAddr(ctx, "udp", ss.addr, ss.prefer)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -211,7 +185,7 @@ func (ss *ShadowSocks) ListenPacketWithDialer(ctx context.Context, dialer C.Dial
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
pc = ss.method.DialPacketConn(bufio.NewBindPacketConn(pc, addr))
|
pc = ss.method.DialPacketConn(N.NewBindPacketConn(pc, addr))
|
||||||
return newPacketConn(pc, ss), nil
|
return newPacketConn(pc, ss), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -220,13 +194,6 @@ func (ss *ShadowSocks) SupportWithDialer() C.NetWork {
|
|||||||
return C.ALLNet
|
return C.ALLNet
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProxyInfo implements C.ProxyAdapter
|
|
||||||
func (ss *ShadowSocks) ProxyInfo() C.ProxyInfo {
|
|
||||||
info := ss.Base.ProxyInfo()
|
|
||||||
info.DialerProxy = ss.option.DialerProxy
|
|
||||||
return info
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
||||||
func (ss *ShadowSocks) ListenPacketOnStreamConn(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
func (ss *ShadowSocks) ListenPacketOnStreamConn(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
if ss.option.UDPOverTCP {
|
if ss.option.UDPOverTCP {
|
||||||
@@ -241,9 +208,9 @@ func (ss *ShadowSocks) ListenPacketOnStreamConn(ctx context.Context, c net.Conn,
|
|||||||
|
|
||||||
destination := M.SocksaddrFromNet(metadata.UDPAddr())
|
destination := M.SocksaddrFromNet(metadata.UDPAddr())
|
||||||
if ss.option.UDPOverTCPVersion == uot.LegacyVersion {
|
if ss.option.UDPOverTCPVersion == uot.LegacyVersion {
|
||||||
return newPacketConn(N.NewThreadSafePacketConn(uot.NewConn(c, uot.Request{Destination: destination})), ss), nil
|
return newPacketConn(uot.NewConn(c, uot.Request{Destination: destination}), ss), nil
|
||||||
} else {
|
} else {
|
||||||
return newPacketConn(N.NewThreadSafePacketConn(uot.NewLazyConn(c, uot.Request{Destination: destination})), ss), nil
|
return newPacketConn(uot.NewLazyConn(c, uot.Request{Destination: destination}), ss), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, C.ErrNotSupport
|
return nil, C.ErrNotSupport
|
||||||
@@ -260,14 +227,13 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
|||||||
Password: option.Password,
|
Password: option.Password,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("ss %s cipher: %s initialize error: %w", addr, option.Cipher, err)
|
return nil, fmt.Errorf("ss %s initialize error: %w", addr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var v2rayOption *v2rayObfs.Option
|
var v2rayOption *v2rayObfs.Option
|
||||||
var gostOption *gost.Option
|
|
||||||
var obfsOption *simpleObfsOption
|
var obfsOption *simpleObfsOption
|
||||||
var shadowTLSOpt *shadowtls.ShadowTLSOption
|
var shadowTLSOpt *shadowtls.ShadowTLSOption
|
||||||
var restlsConfig *restls.Config
|
var restlsConfig *restlsC.Config
|
||||||
obfsMode := ""
|
obfsMode := ""
|
||||||
|
|
||||||
decoder := structure.NewDecoder(structure.Option{TagName: "obfs", WeaklyTypedInput: true})
|
decoder := structure.NewDecoder(structure.Option{TagName: "obfs", WeaklyTypedInput: true})
|
||||||
@@ -293,36 +259,6 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
|||||||
}
|
}
|
||||||
obfsMode = opts.Mode
|
obfsMode = opts.Mode
|
||||||
v2rayOption = &v2rayObfs.Option{
|
v2rayOption = &v2rayObfs.Option{
|
||||||
Host: opts.Host,
|
|
||||||
Path: opts.Path,
|
|
||||||
Headers: opts.Headers,
|
|
||||||
Mux: opts.Mux,
|
|
||||||
V2rayHttpUpgrade: opts.V2rayHttpUpgrade,
|
|
||||||
V2rayHttpUpgradeFastOpen: opts.V2rayHttpUpgradeFastOpen,
|
|
||||||
}
|
|
||||||
|
|
||||||
if opts.TLS {
|
|
||||||
v2rayOption.TLS = true
|
|
||||||
v2rayOption.SkipCertVerify = opts.SkipCertVerify
|
|
||||||
v2rayOption.Fingerprint = opts.Fingerprint
|
|
||||||
|
|
||||||
echConfig, err := opts.ECHOpts.Parse()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("ss %s initialize v2ray-plugin error: %w", addr, err)
|
|
||||||
}
|
|
||||||
v2rayOption.ECHConfig = echConfig
|
|
||||||
}
|
|
||||||
} else if option.Plugin == "gost-plugin" {
|
|
||||||
opts := gostObfsOption{Host: "bing.com", Mux: true}
|
|
||||||
if err := decoder.Decode(option.PluginOpts, &opts); err != nil {
|
|
||||||
return nil, fmt.Errorf("ss %s initialize gost-plugin error: %w", addr, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if opts.Mode != "websocket" {
|
|
||||||
return nil, fmt.Errorf("ss %s obfs mode error: %s", addr, opts.Mode)
|
|
||||||
}
|
|
||||||
obfsMode = opts.Mode
|
|
||||||
gostOption = &gost.Option{
|
|
||||||
Host: opts.Host,
|
Host: opts.Host,
|
||||||
Path: opts.Path,
|
Path: opts.Path,
|
||||||
Headers: opts.Headers,
|
Headers: opts.Headers,
|
||||||
@@ -330,15 +266,8 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if opts.TLS {
|
if opts.TLS {
|
||||||
gostOption.TLS = true
|
v2rayOption.TLS = true
|
||||||
gostOption.SkipCertVerify = opts.SkipCertVerify
|
v2rayOption.SkipCertVerify = opts.SkipCertVerify
|
||||||
gostOption.Fingerprint = opts.Fingerprint
|
|
||||||
|
|
||||||
echConfig, err := opts.ECHOpts.Parse()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("ss %s initialize gost-plugin error: %w", addr, err)
|
|
||||||
}
|
|
||||||
gostOption.ECHConfig = echConfig
|
|
||||||
}
|
}
|
||||||
} else if option.Plugin == shadowtls.Mode {
|
} else if option.Plugin == shadowtls.Mode {
|
||||||
obfsMode = shadowtls.Mode
|
obfsMode = shadowtls.Mode
|
||||||
@@ -357,12 +286,6 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
|||||||
SkipCertVerify: opt.SkipCertVerify,
|
SkipCertVerify: opt.SkipCertVerify,
|
||||||
Version: opt.Version,
|
Version: opt.Version,
|
||||||
}
|
}
|
||||||
|
|
||||||
if opt.ALPN != nil { // structure's Decode will ensure value not nil when input has value even it was set an empty array
|
|
||||||
shadowTLSOpt.ALPN = opt.ALPN
|
|
||||||
} else {
|
|
||||||
shadowTLSOpt.ALPN = shadowtls.DefaultALPN
|
|
||||||
}
|
|
||||||
} else if option.Plugin == restls.Mode {
|
} else if option.Plugin == restls.Mode {
|
||||||
obfsMode = restls.Mode
|
obfsMode = restls.Mode
|
||||||
restlsOpt := &restlsOption{}
|
restlsOpt := &restlsOption{}
|
||||||
@@ -370,7 +293,7 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
|||||||
return nil, fmt.Errorf("ss %s initialize restls-plugin error: %w", addr, err)
|
return nil, fmt.Errorf("ss %s initialize restls-plugin error: %w", addr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
restlsConfig, err = restls.NewRestlsConfig(restlsOpt.Host, restlsOpt.Password, restlsOpt.VersionHint, restlsOpt.RestlsScript, option.ClientFingerprint)
|
restlsConfig, err = restlsC.NewRestlsConfig(restlsOpt.Host, restlsOpt.Password, restlsOpt.VersionHint, restlsOpt.RestlsScript, option.ClientFingerprint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("ss %s initialize restls-plugin error: %w", addr, err)
|
return nil, fmt.Errorf("ss %s initialize restls-plugin error: %w", addr, err)
|
||||||
}
|
}
|
||||||
@@ -401,7 +324,6 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
|||||||
option: &option,
|
option: &option,
|
||||||
obfsMode: obfsMode,
|
obfsMode: obfsMode,
|
||||||
v2rayOption: v2rayOption,
|
v2rayOption: v2rayOption,
|
||||||
gostOption: gostOption,
|
|
||||||
obfsOption: obfsOption,
|
obfsOption: obfsOption,
|
||||||
shadowTLSOption: shadowTLSOpt,
|
shadowTLSOption: shadowTLSOpt,
|
||||||
restlsConfig: restlsConfig,
|
restlsConfig: restlsConfig,
|
||||||
|
|||||||
@@ -7,16 +7,16 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
"github.com/Dreamacro/clash/component/proxydialer"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/metacubex/mihomo/transport/shadowsocks/core"
|
"github.com/Dreamacro/clash/transport/shadowsocks/core"
|
||||||
"github.com/metacubex/mihomo/transport/shadowsocks/shadowaead"
|
"github.com/Dreamacro/clash/transport/shadowsocks/shadowaead"
|
||||||
"github.com/metacubex/mihomo/transport/shadowsocks/shadowstream"
|
"github.com/Dreamacro/clash/transport/shadowsocks/shadowstream"
|
||||||
"github.com/metacubex/mihomo/transport/socks5"
|
"github.com/Dreamacro/clash/transport/socks5"
|
||||||
"github.com/metacubex/mihomo/transport/ssr/obfs"
|
"github.com/Dreamacro/clash/transport/ssr/obfs"
|
||||||
"github.com/metacubex/mihomo/transport/ssr/protocol"
|
"github.com/Dreamacro/clash/transport/ssr/protocol"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ShadowSocksR struct {
|
type ShadowSocksR struct {
|
||||||
@@ -42,15 +42,12 @@ type ShadowSocksROption struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// StreamConnContext implements C.ProxyAdapter
|
// StreamConnContext implements C.ProxyAdapter
|
||||||
func (ssr *ShadowSocksR) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ net.Conn, err error) {
|
func (ssr *ShadowSocksR) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
||||||
if ctx.Done() != nil {
|
|
||||||
done := N.SetupContextForConn(ctx, c)
|
|
||||||
defer done(&err)
|
|
||||||
}
|
|
||||||
c = ssr.obfs.StreamConn(c)
|
c = ssr.obfs.StreamConn(c)
|
||||||
c = ssr.cipher.StreamConn(c)
|
c = ssr.cipher.StreamConn(c)
|
||||||
var (
|
var (
|
||||||
iv []byte
|
iv []byte
|
||||||
|
err error
|
||||||
)
|
)
|
||||||
switch conn := c.(type) {
|
switch conn := c.(type) {
|
||||||
case *shadowstream.Conn:
|
case *shadowstream.Conn:
|
||||||
@@ -67,8 +64,8 @@ func (ssr *ShadowSocksR) StreamConnContext(ctx context.Context, c net.Conn, meta
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (ssr *ShadowSocksR) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
func (ssr *ShadowSocksR) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
||||||
return ssr.DialContextWithDialer(ctx, dialer.NewDialer(ssr.DialOptions()...), metadata)
|
return ssr.DialContextWithDialer(ctx, dialer.NewDialer(ssr.Base.DialOptions(opts...)...), metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialContextWithDialer implements C.ProxyAdapter
|
// DialContextWithDialer implements C.ProxyAdapter
|
||||||
@@ -83,6 +80,7 @@ func (ssr *ShadowSocksR) DialContextWithDialer(ctx context.Context, dialer C.Dia
|
|||||||
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)
|
||||||
}
|
}
|
||||||
|
N.TCPKeepAlive(c)
|
||||||
|
|
||||||
defer func(c net.Conn) {
|
defer func(c net.Conn) {
|
||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
@@ -93,8 +91,8 @@ func (ssr *ShadowSocksR) DialContextWithDialer(ctx context.Context, dialer C.Dia
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (ssr *ShadowSocksR) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
func (ssr *ShadowSocksR) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
||||||
return ssr.ListenPacketWithDialer(ctx, dialer.NewDialer(ssr.DialOptions()...), metadata)
|
return ssr.ListenPacketWithDialer(ctx, dialer.NewDialer(ssr.Base.DialOptions(opts...)...), metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketWithDialer implements C.ProxyAdapter
|
// ListenPacketWithDialer implements C.ProxyAdapter
|
||||||
@@ -105,7 +103,7 @@ func (ssr *ShadowSocksR) ListenPacketWithDialer(ctx context.Context, dialer C.Di
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
addr, err := resolveUDPAddr(ctx, "udp", ssr.addr, ssr.prefer)
|
addr, err := resolveUDPAddrWithPrefer(ctx, "udp", ssr.addr, ssr.prefer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -125,16 +123,9 @@ func (ssr *ShadowSocksR) SupportWithDialer() C.NetWork {
|
|||||||
return C.ALLNet
|
return C.ALLNet
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProxyInfo implements C.ProxyAdapter
|
|
||||||
func (ssr *ShadowSocksR) ProxyInfo() C.ProxyInfo {
|
|
||||||
info := ssr.Base.ProxyInfo()
|
|
||||||
info.DialerProxy = ssr.option.DialerProxy
|
|
||||||
return info
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewShadowSocksR(option ShadowSocksROption) (*ShadowSocksR, error) {
|
func NewShadowSocksR(option ShadowSocksROption) (*ShadowSocksR, error) {
|
||||||
// SSR protocol compatibility
|
// SSR protocol compatibility
|
||||||
// https://github.com/metacubex/mihomo/pull/2056
|
// https://github.com/Dreamacro/clash/pull/2056
|
||||||
if option.Cipher == "none" {
|
if option.Cipher == "none" {
|
||||||
option.Cipher = "dummy"
|
option.Cipher = "dummy"
|
||||||
}
|
}
|
||||||
@@ -144,7 +135,7 @@ func NewShadowSocksR(option ShadowSocksROption) (*ShadowSocksR, error) {
|
|||||||
password := option.Password
|
password := option.Password
|
||||||
coreCiph, err := core.PickCipher(cipher, nil, password)
|
coreCiph, err := core.PickCipher(cipher, nil, password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("ssr %s cipher: %s initialize error: %w", addr, cipher, err)
|
return nil, fmt.Errorf("ssr %s initialize error: %w", addr, err)
|
||||||
}
|
}
|
||||||
var (
|
var (
|
||||||
ivSize int
|
ivSize int
|
||||||
|
|||||||
@@ -3,56 +3,58 @@ package outbound
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
CN "github.com/metacubex/mihomo/common/net"
|
CN "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
"github.com/Dreamacro/clash/component/proxydialer"
|
||||||
"github.com/metacubex/mihomo/component/resolver"
|
"github.com/Dreamacro/clash/component/resolver"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/metacubex/mihomo/log"
|
|
||||||
|
|
||||||
mux "github.com/metacubex/sing-mux"
|
mux "github.com/sagernet/sing-mux"
|
||||||
E "github.com/metacubex/sing/common/exceptions"
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
M "github.com/metacubex/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SingMux struct {
|
type SingMux struct {
|
||||||
ProxyAdapter
|
C.ProxyAdapter
|
||||||
|
base ProxyBase
|
||||||
client *mux.Client
|
client *mux.Client
|
||||||
dialer proxydialer.SingDialer
|
dialer proxydialer.SingDialer
|
||||||
onlyTcp bool
|
onlyTcp bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type SingMuxOption struct {
|
type SingMuxOption struct {
|
||||||
Enabled bool `proxy:"enabled,omitempty"`
|
Enabled bool `proxy:"enabled,omitempty"`
|
||||||
Protocol string `proxy:"protocol,omitempty"`
|
Protocol string `proxy:"protocol,omitempty"`
|
||||||
MaxConnections int `proxy:"max-connections,omitempty"`
|
MaxConnections int `proxy:"max-connections,omitempty"`
|
||||||
MinStreams int `proxy:"min-streams,omitempty"`
|
MinStreams int `proxy:"min-streams,omitempty"`
|
||||||
MaxStreams int `proxy:"max-streams,omitempty"`
|
MaxStreams int `proxy:"max-streams,omitempty"`
|
||||||
Padding bool `proxy:"padding,omitempty"`
|
Padding bool `proxy:"padding,omitempty"`
|
||||||
Statistic bool `proxy:"statistic,omitempty"`
|
Statistic bool `proxy:"statistic,omitempty"`
|
||||||
OnlyTcp bool `proxy:"only-tcp,omitempty"`
|
OnlyTcp bool `proxy:"only-tcp,omitempty"`
|
||||||
BrutalOpts BrutalOption `proxy:"brutal-opts,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type BrutalOption struct {
|
type ProxyBase interface {
|
||||||
Enabled bool `proxy:"enabled,omitempty"`
|
DialOptions(opts ...dialer.Option) []dialer.Option
|
||||||
Up string `proxy:"up,omitempty"`
|
|
||||||
Down string `proxy:"down,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SingMux) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
func (s *SingMux) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
||||||
|
options := s.base.DialOptions(opts...)
|
||||||
|
s.dialer.SetDialer(dialer.NewDialer(options...))
|
||||||
c, err := s.client.DialContext(ctx, "tcp", M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort))
|
c, err := s.client.DialContext(ctx, "tcp", M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return NewConn(c, s), err
|
return NewConn(CN.NewRefConn(c, s), s.ProxyAdapter), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SingMux) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
func (s *SingMux) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
||||||
if s.onlyTcp {
|
if s.onlyTcp {
|
||||||
return s.ProxyAdapter.ListenPacketContext(ctx, metadata)
|
return s.ProxyAdapter.ListenPacketContext(ctx, metadata, opts...)
|
||||||
}
|
}
|
||||||
|
options := s.base.DialOptions(opts...)
|
||||||
|
s.dialer.SetDialer(dialer.NewDialer(options...))
|
||||||
|
|
||||||
// sing-mux use stream-oriented udp with a special address, so we need a net.UDPAddr
|
// sing-mux use stream-oriented udp with a special address, so we need a net.UDPAddr
|
||||||
if !metadata.Resolved() {
|
if !metadata.Resolved() {
|
||||||
@@ -70,7 +72,7 @@ func (s *SingMux) ListenPacketContext(ctx context.Context, metadata *C.Metadata)
|
|||||||
if pc == nil {
|
if pc == nil {
|
||||||
return nil, E.New("packetConn is nil")
|
return nil, E.New("packetConn is nil")
|
||||||
}
|
}
|
||||||
return newPacketConn(CN.NewThreadSafePacketConn(pc), s), nil
|
return newPacketConn(CN.NewRefPacketConn(CN.NewThreadSafePacketConn(pc), s), s.ProxyAdapter), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SingMux) SupportUDP() bool {
|
func (s *SingMux) SupportUDP() bool {
|
||||||
@@ -87,47 +89,30 @@ func (s *SingMux) SupportUOT() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SingMux) ProxyInfo() C.ProxyInfo {
|
func closeSingMux(s *SingMux) {
|
||||||
info := s.ProxyAdapter.ProxyInfo()
|
_ = s.client.Close()
|
||||||
info.SMUX = true
|
|
||||||
return info
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close implements C.ProxyAdapter
|
func NewSingMux(option SingMuxOption, proxy C.ProxyAdapter, base ProxyBase) (C.ProxyAdapter, error) {
|
||||||
func (s *SingMux) Close() error {
|
singDialer := proxydialer.NewSingDialer(proxy, dialer.NewDialer(), option.Statistic)
|
||||||
if s.client != nil {
|
|
||||||
_ = s.client.Close()
|
|
||||||
}
|
|
||||||
return s.ProxyAdapter.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSingMux(option SingMuxOption, proxy ProxyAdapter) (ProxyAdapter, error) {
|
|
||||||
// TODO
|
|
||||||
// "TCP Brutal is only supported on Linux-based systems"
|
|
||||||
|
|
||||||
singDialer := proxydialer.NewSingDialer(proxy, dialer.NewDialer(proxy.DialOptions()...), option.Statistic)
|
|
||||||
client, err := mux.NewClient(mux.Options{
|
client, err := mux.NewClient(mux.Options{
|
||||||
Dialer: singDialer,
|
Dialer: singDialer,
|
||||||
Logger: log.SingLogger,
|
|
||||||
Protocol: option.Protocol,
|
Protocol: option.Protocol,
|
||||||
MaxConnections: option.MaxConnections,
|
MaxConnections: option.MaxConnections,
|
||||||
MinStreams: option.MinStreams,
|
MinStreams: option.MinStreams,
|
||||||
MaxStreams: option.MaxStreams,
|
MaxStreams: option.MaxStreams,
|
||||||
Padding: option.Padding,
|
Padding: option.Padding,
|
||||||
Brutal: mux.BrutalOptions{
|
|
||||||
Enabled: option.BrutalOpts.Enabled,
|
|
||||||
SendBPS: StringToBps(option.BrutalOpts.Up),
|
|
||||||
ReceiveBPS: StringToBps(option.BrutalOpts.Down),
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
outbound := &SingMux{
|
outbound := &SingMux{
|
||||||
ProxyAdapter: proxy,
|
ProxyAdapter: proxy,
|
||||||
|
base: base,
|
||||||
client: client,
|
client: client,
|
||||||
dialer: singDialer,
|
dialer: singDialer,
|
||||||
onlyTcp: option.OnlyTcp,
|
onlyTcp: option.OnlyTcp,
|
||||||
}
|
}
|
||||||
|
runtime.SetFinalizer(outbound, closeSingMux)
|
||||||
return outbound, nil
|
return outbound, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,13 +6,13 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/metacubex/mihomo/common/structure"
|
"github.com/Dreamacro/clash/common/structure"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
"github.com/Dreamacro/clash/component/proxydialer"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
obfs "github.com/metacubex/mihomo/transport/simple-obfs"
|
obfs "github.com/Dreamacro/clash/transport/simple-obfs"
|
||||||
"github.com/metacubex/mihomo/transport/snell"
|
"github.com/Dreamacro/clash/transport/snell"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Snell struct {
|
type Snell struct {
|
||||||
@@ -42,7 +42,7 @@ type streamOption struct {
|
|||||||
obfsOption *simpleObfsOption
|
obfsOption *simpleObfsOption
|
||||||
}
|
}
|
||||||
|
|
||||||
func snellStreamConn(c net.Conn, option streamOption) *snell.Snell {
|
func streamConn(c net.Conn, option streamOption) *snell.Snell {
|
||||||
switch option.obfsOption.Mode {
|
switch option.obfsOption.Mode {
|
||||||
case "tls":
|
case "tls":
|
||||||
c = obfs.NewTLSObfs(c, option.obfsOption.Host)
|
c = obfs.NewTLSObfs(c, option.obfsOption.Host)
|
||||||
@@ -55,41 +55,31 @@ func snellStreamConn(c net.Conn, option streamOption) *snell.Snell {
|
|||||||
|
|
||||||
// StreamConnContext implements C.ProxyAdapter
|
// StreamConnContext implements C.ProxyAdapter
|
||||||
func (s *Snell) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
func (s *Snell) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
||||||
c = snellStreamConn(c, streamOption{s.psk, s.version, s.addr, s.obfsOption})
|
c = streamConn(c, streamOption{s.psk, s.version, s.addr, s.obfsOption})
|
||||||
err := s.writeHeaderContext(ctx, c, metadata)
|
if metadata.NetWork == C.UDP {
|
||||||
|
err := snell.WriteUDPHeader(c, s.version)
|
||||||
|
return c, err
|
||||||
|
}
|
||||||
|
err := snell.WriteHeader(c, metadata.String(), uint(metadata.DstPort), s.version)
|
||||||
return c, err
|
return c, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Snell) writeHeaderContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (err error) {
|
|
||||||
if ctx.Done() != nil {
|
|
||||||
done := N.SetupContextForConn(ctx, c)
|
|
||||||
defer done(&err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if metadata.NetWork == C.UDP {
|
|
||||||
err = snell.WriteUDPHeader(c, s.version)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = snell.WriteHeader(c, metadata.String(), uint(metadata.DstPort), s.version)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (s *Snell) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
func (s *Snell) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
||||||
if s.version == snell.Version2 {
|
if s.version == snell.Version2 && len(opts) == 0 {
|
||||||
c, err := s.pool.Get()
|
c, err := s.pool.Get()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = s.writeHeaderContext(ctx, c, metadata); err != nil {
|
if err = snell.WriteHeader(c, metadata.String(), uint(metadata.DstPort), s.version); err != nil {
|
||||||
_ = c.Close()
|
c.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return NewConn(c, s), err
|
return NewConn(c, s), err
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.DialContextWithDialer(ctx, dialer.NewDialer(s.DialOptions()...), metadata)
|
return s.DialContextWithDialer(ctx, dialer.NewDialer(s.Base.DialOptions(opts...)...), metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialContextWithDialer implements C.ProxyAdapter
|
// DialContextWithDialer implements C.ProxyAdapter
|
||||||
@@ -104,6 +94,7 @@ func (s *Snell) DialContextWithDialer(ctx context.Context, dialer C.Dialer, meta
|
|||||||
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)
|
||||||
}
|
}
|
||||||
|
N.TCPKeepAlive(c)
|
||||||
|
|
||||||
defer func(c net.Conn) {
|
defer func(c net.Conn) {
|
||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
@@ -114,8 +105,8 @@ func (s *Snell) DialContextWithDialer(ctx context.Context, dialer C.Dialer, meta
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (s *Snell) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
func (s *Snell) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
||||||
return s.ListenPacketWithDialer(ctx, dialer.NewDialer(s.DialOptions()...), metadata)
|
return s.ListenPacketWithDialer(ctx, dialer.NewDialer(s.Base.DialOptions(opts...)...), metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketWithDialer implements C.ProxyAdapter
|
// ListenPacketWithDialer implements C.ProxyAdapter
|
||||||
@@ -131,8 +122,13 @@ func (s *Snell) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, met
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
N.TCPKeepAlive(c)
|
||||||
|
c = streamConn(c, streamOption{s.psk, s.version, s.addr, s.obfsOption})
|
||||||
|
|
||||||
c, err = s.StreamConnContext(ctx, c, metadata)
|
err = snell.WriteUDPHeader(c, s.version)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
pc := snell.PacketConn(c)
|
pc := snell.PacketConn(c)
|
||||||
return newPacketConn(pc, s), nil
|
return newPacketConn(pc, s), nil
|
||||||
@@ -148,13 +144,6 @@ func (s *Snell) SupportUOT() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProxyInfo implements C.ProxyAdapter
|
|
||||||
func (s *Snell) ProxyInfo() C.ProxyInfo {
|
|
||||||
info := s.Base.ProxyInfo()
|
|
||||||
info.DialerProxy = s.option.DialerProxy
|
|
||||||
return info
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSnell(option SnellOption) (*Snell, error) {
|
func NewSnell(option SnellOption) (*Snell, error) {
|
||||||
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
||||||
psk := []byte(option.Psk)
|
psk := []byte(option.Psk)
|
||||||
@@ -207,7 +196,7 @@ func NewSnell(option SnellOption) (*Snell, error) {
|
|||||||
if option.Version == snell.Version2 {
|
if option.Version == snell.Version2 {
|
||||||
s.pool = snell.NewPool(func(ctx context.Context) (*snell.Snell, error) {
|
s.pool = snell.NewPool(func(ctx context.Context) (*snell.Snell, error) {
|
||||||
var err error
|
var err error
|
||||||
var cDialer C.Dialer = dialer.NewDialer(s.DialOptions()...)
|
var cDialer C.Dialer = dialer.NewDialer(s.Base.DialOptions()...)
|
||||||
if len(s.option.DialerProxy) > 0 {
|
if len(s.option.DialerProxy) > 0 {
|
||||||
cDialer, err = proxydialer.NewByName(s.option.DialerProxy, cDialer)
|
cDialer, err = proxydialer.NewByName(s.option.DialerProxy, cDialer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -219,7 +208,8 @@ func NewSnell(option SnellOption) (*Snell, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return snellStreamConn(c, streamOption{psk, option.Version, addr, obfsOption}), nil
|
N.TCPKeepAlive(c)
|
||||||
|
return streamConn(c, streamOption{psk, option.Version, addr, obfsOption}), nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return s, nil
|
return s, nil
|
||||||
|
|||||||
@@ -10,12 +10,12 @@ import (
|
|||||||
"net/netip"
|
"net/netip"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/metacubex/mihomo/component/ca"
|
"github.com/Dreamacro/clash/component/ca"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
"github.com/Dreamacro/clash/component/proxydialer"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/metacubex/mihomo/transport/socks5"
|
"github.com/Dreamacro/clash/transport/socks5"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Socks5 struct {
|
type Socks5 struct {
|
||||||
@@ -59,15 +59,15 @@ func (ss *Socks5) StreamConnContext(ctx context.Context, c net.Conn, metadata *C
|
|||||||
Password: ss.pass,
|
Password: ss.pass,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if _, err := ss.clientHandshakeContext(ctx, c, serializesSocksAddr(metadata), socks5.CmdConnect, user); err != nil {
|
if _, err := socks5.ClientHandshake(c, serializesSocksAddr(metadata), socks5.CmdConnect, user); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (ss *Socks5) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
func (ss *Socks5) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
||||||
return ss.DialContextWithDialer(ctx, dialer.NewDialer(ss.DialOptions()...), metadata)
|
return ss.DialContextWithDialer(ctx, dialer.NewDialer(ss.Base.DialOptions(opts...)...), metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialContextWithDialer implements C.ProxyAdapter
|
// DialContextWithDialer implements C.ProxyAdapter
|
||||||
@@ -82,6 +82,7 @@ func (ss *Socks5) DialContextWithDialer(ctx context.Context, dialer C.Dialer, me
|
|||||||
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)
|
||||||
}
|
}
|
||||||
|
N.TCPKeepAlive(c)
|
||||||
|
|
||||||
defer func(c net.Conn) {
|
defer func(c net.Conn) {
|
||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
@@ -101,8 +102,8 @@ func (ss *Socks5) SupportWithDialer() C.NetWork {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (ss *Socks5) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
func (ss *Socks5) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
||||||
var cDialer C.Dialer = dialer.NewDialer(ss.DialOptions()...)
|
var cDialer C.Dialer = dialer.NewDialer(ss.Base.DialOptions(opts...)...)
|
||||||
if len(ss.option.DialerProxy) > 0 {
|
if len(ss.option.DialerProxy) > 0 {
|
||||||
cDialer, err = proxydialer.NewByName(ss.option.DialerProxy, cDialer)
|
cDialer, err = proxydialer.NewByName(ss.option.DialerProxy, cDialer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -127,6 +128,7 @@ func (ss *Socks5) ListenPacketContext(ctx context.Context, metadata *C.Metadata)
|
|||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
}(c)
|
}(c)
|
||||||
|
|
||||||
|
N.TCPKeepAlive(c)
|
||||||
var user *socks5.User
|
var user *socks5.User
|
||||||
if ss.user != "" {
|
if ss.user != "" {
|
||||||
user = &socks5.User{
|
user = &socks5.User{
|
||||||
@@ -136,7 +138,7 @@ func (ss *Socks5) ListenPacketContext(ctx context.Context, metadata *C.Metadata)
|
|||||||
}
|
}
|
||||||
|
|
||||||
udpAssocateAddr := socks5.AddrFromStdAddrPort(netip.AddrPortFrom(netip.IPv4Unspecified(), 0))
|
udpAssocateAddr := socks5.AddrFromStdAddrPort(netip.AddrPortFrom(netip.IPv4Unspecified(), 0))
|
||||||
bindAddr, err := ss.clientHandshakeContext(ctx, c, udpAssocateAddr, socks5.CmdUDPAssociate, user)
|
bindAddr, err := socks5.ClientHandshake(c, udpAssocateAddr, socks5.CmdUDPAssociate, user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("client hanshake error: %w", err)
|
err = fmt.Errorf("client hanshake error: %w", err)
|
||||||
return
|
return
|
||||||
@@ -148,7 +150,7 @@ func (ss *Socks5) ListenPacketContext(ctx context.Context, metadata *C.Metadata)
|
|||||||
err = errors.New("invalid UDP bind address")
|
err = errors.New("invalid UDP bind address")
|
||||||
return
|
return
|
||||||
} else if bindUDPAddr.IP.IsUnspecified() {
|
} else if bindUDPAddr.IP.IsUnspecified() {
|
||||||
serverAddr, err := resolveUDPAddr(ctx, "udp", ss.Addr(), C.IPv4Prefer)
|
serverAddr, err := resolveUDPAddr(ctx, "udp", ss.Addr())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -172,21 +174,6 @@ func (ss *Socks5) ListenPacketContext(ctx context.Context, metadata *C.Metadata)
|
|||||||
return newPacketConn(&socksPacketConn{PacketConn: pc, rAddr: bindUDPAddr, tcpConn: c}, ss), nil
|
return newPacketConn(&socksPacketConn{PacketConn: pc, rAddr: bindUDPAddr, tcpConn: c}, ss), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProxyInfo implements C.ProxyAdapter
|
|
||||||
func (ss *Socks5) ProxyInfo() C.ProxyInfo {
|
|
||||||
info := ss.Base.ProxyInfo()
|
|
||||||
info.DialerProxy = ss.option.DialerProxy
|
|
||||||
return info
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ss *Socks5) clientHandshakeContext(ctx context.Context, c net.Conn, addr socks5.Addr, command socks5.Command, user *socks5.User) (_ socks5.Addr, err error) {
|
|
||||||
if ctx.Done() != nil {
|
|
||||||
done := N.SetupContextForConn(ctx, c)
|
|
||||||
defer done(&err)
|
|
||||||
}
|
|
||||||
return socks5.ClientHandshake(c, addr, command, user)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSocks5(option Socks5Option) (*Socks5, error) {
|
func NewSocks5(option Socks5Option) (*Socks5, error) {
|
||||||
var tlsConfig *tls.Config
|
var tlsConfig *tls.Config
|
||||||
if option.TLS {
|
if option.TLS {
|
||||||
|
|||||||
@@ -1,208 +0,0 @@
|
|||||||
package outbound
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"context"
|
|
||||||
"encoding/base64"
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
|
||||||
|
|
||||||
"github.com/metacubex/randv2"
|
|
||||||
"golang.org/x/crypto/ssh"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Ssh struct {
|
|
||||||
*Base
|
|
||||||
|
|
||||||
option *SshOption
|
|
||||||
|
|
||||||
config *ssh.ClientConfig
|
|
||||||
client *ssh.Client
|
|
||||||
cMutex sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
type SshOption struct {
|
|
||||||
BasicOption
|
|
||||||
Name string `proxy:"name"`
|
|
||||||
Server string `proxy:"server"`
|
|
||||||
Port int `proxy:"port"`
|
|
||||||
UserName string `proxy:"username"`
|
|
||||||
Password string `proxy:"password,omitempty"`
|
|
||||||
PrivateKey string `proxy:"private-key,omitempty"`
|
|
||||||
PrivateKeyPassphrase string `proxy:"private-key-passphrase,omitempty"`
|
|
||||||
HostKey []string `proxy:"host-key,omitempty"`
|
|
||||||
HostKeyAlgorithms []string `proxy:"host-key-algorithms,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Ssh) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
|
||||||
var cDialer C.Dialer = dialer.NewDialer(s.DialOptions()...)
|
|
||||||
if len(s.option.DialerProxy) > 0 {
|
|
||||||
cDialer, err = proxydialer.NewByName(s.option.DialerProxy, cDialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
client, err := s.connect(ctx, cDialer, s.addr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
c, err := client.DialContext(ctx, "tcp", metadata.RemoteAddress())
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return NewConn(c, s), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Ssh) connect(ctx context.Context, cDialer C.Dialer, addr string) (client *ssh.Client, err error) {
|
|
||||||
s.cMutex.Lock()
|
|
||||||
defer s.cMutex.Unlock()
|
|
||||||
if s.client != nil {
|
|
||||||
return s.client, nil
|
|
||||||
}
|
|
||||||
c, err := cDialer.DialContext(ctx, "tcp", addr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
defer func(c net.Conn) {
|
|
||||||
safeConnClose(c, err)
|
|
||||||
}(c)
|
|
||||||
|
|
||||||
if ctx.Done() != nil {
|
|
||||||
done := N.SetupContextForConn(ctx, c)
|
|
||||||
defer done(&err)
|
|
||||||
}
|
|
||||||
|
|
||||||
clientConn, chans, reqs, err := ssh.NewClientConn(c, addr, s.config)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
client = ssh.NewClient(clientConn, chans, reqs)
|
|
||||||
|
|
||||||
s.client = client
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
_ = client.Wait() // wait shutdown
|
|
||||||
_ = client.Close()
|
|
||||||
s.cMutex.Lock()
|
|
||||||
defer s.cMutex.Unlock()
|
|
||||||
if s.client == client {
|
|
||||||
s.client = nil
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
return client, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ProxyInfo implements C.ProxyAdapter
|
|
||||||
func (s *Ssh) ProxyInfo() C.ProxyInfo {
|
|
||||||
info := s.Base.ProxyInfo()
|
|
||||||
info.DialerProxy = s.option.DialerProxy
|
|
||||||
return info
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close implements C.ProxyAdapter
|
|
||||||
func (s *Ssh) Close() error {
|
|
||||||
s.cMutex.Lock()
|
|
||||||
defer s.cMutex.Unlock()
|
|
||||||
if s.client != nil {
|
|
||||||
return s.client.Close()
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSsh(option SshOption) (*Ssh, error) {
|
|
||||||
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
|
||||||
|
|
||||||
config := ssh.ClientConfig{
|
|
||||||
User: option.UserName,
|
|
||||||
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
|
||||||
HostKeyAlgorithms: option.HostKeyAlgorithms,
|
|
||||||
}
|
|
||||||
|
|
||||||
if option.PrivateKey != "" {
|
|
||||||
var b []byte
|
|
||||||
var err error
|
|
||||||
if strings.Contains(option.PrivateKey, "PRIVATE KEY") {
|
|
||||||
b = []byte(option.PrivateKey)
|
|
||||||
} else {
|
|
||||||
path := C.Path.Resolve(option.PrivateKey)
|
|
||||||
if !C.Path.IsSafePath(path) {
|
|
||||||
return nil, C.Path.ErrNotSafePath(path)
|
|
||||||
}
|
|
||||||
b, err = os.ReadFile(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var pKey ssh.Signer
|
|
||||||
if option.PrivateKeyPassphrase != "" {
|
|
||||||
pKey, err = ssh.ParsePrivateKeyWithPassphrase(b, []byte(option.PrivateKeyPassphrase))
|
|
||||||
} else {
|
|
||||||
pKey, err = ssh.ParsePrivateKey(b)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
config.Auth = append(config.Auth, ssh.PublicKeys(pKey))
|
|
||||||
}
|
|
||||||
|
|
||||||
if option.Password != "" {
|
|
||||||
config.Auth = append(config.Auth, ssh.Password(option.Password))
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(option.HostKey) != 0 {
|
|
||||||
keys := make([]ssh.PublicKey, len(option.HostKey))
|
|
||||||
for i, hostKey := range option.HostKey {
|
|
||||||
key, _, _, _, err := ssh.ParseAuthorizedKey([]byte(hostKey))
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("parse host key :%s", key)
|
|
||||||
}
|
|
||||||
keys[i] = key
|
|
||||||
}
|
|
||||||
config.HostKeyCallback = func(hostname string, remote net.Addr, key ssh.PublicKey) error {
|
|
||||||
serverKey := key.Marshal()
|
|
||||||
for _, hostKey := range keys {
|
|
||||||
if bytes.Equal(serverKey, hostKey.Marshal()) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return fmt.Errorf("host key mismatch, server send :%s %s", key.Type(), base64.StdEncoding.EncodeToString(serverKey))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
version := "SSH-2.0-OpenSSH_"
|
|
||||||
if randv2.IntN(2) == 0 {
|
|
||||||
version += "7." + strconv.Itoa(randv2.IntN(10))
|
|
||||||
} else {
|
|
||||||
version += "8." + strconv.Itoa(randv2.IntN(9))
|
|
||||||
}
|
|
||||||
config.ClientVersion = version
|
|
||||||
|
|
||||||
outbound := &Ssh{
|
|
||||||
Base: &Base{
|
|
||||||
name: option.Name,
|
|
||||||
addr: addr,
|
|
||||||
tp: C.Ssh,
|
|
||||||
udp: false,
|
|
||||||
iface: option.Interface,
|
|
||||||
rmark: option.RoutingMark,
|
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
|
||||||
},
|
|
||||||
option: &option,
|
|
||||||
config: &config,
|
|
||||||
}
|
|
||||||
|
|
||||||
return outbound, nil
|
|
||||||
}
|
|
||||||
@@ -3,29 +3,25 @@ package outbound
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/metacubex/mihomo/component/ca"
|
"github.com/Dreamacro/clash/component/ca"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
"github.com/metacubex/mihomo/component/ech"
|
"github.com/Dreamacro/clash/component/proxydialer"
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
||||||
tlsC "github.com/metacubex/mihomo/component/tls"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
"github.com/Dreamacro/clash/transport/gun"
|
||||||
"github.com/metacubex/mihomo/transport/gun"
|
"github.com/Dreamacro/clash/transport/trojan"
|
||||||
"github.com/metacubex/mihomo/transport/shadowsocks/core"
|
|
||||||
"github.com/metacubex/mihomo/transport/trojan"
|
|
||||||
"github.com/metacubex/mihomo/transport/vmess"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Trojan struct {
|
type Trojan struct {
|
||||||
*Base
|
*Base
|
||||||
option *TrojanOption
|
instance *trojan.Trojan
|
||||||
hexPassword [trojan.KeyLength]byte
|
option *TrojanOption
|
||||||
|
|
||||||
// for gun mux
|
// for gun mux
|
||||||
gunTLSConfig *tls.Config
|
gunTLSConfig *tls.Config
|
||||||
@@ -33,9 +29,6 @@ type Trojan struct {
|
|||||||
transport *gun.TransportWrap
|
transport *gun.TransportWrap
|
||||||
|
|
||||||
realityConfig *tlsC.RealityConfig
|
realityConfig *tlsC.RealityConfig
|
||||||
echConfig *ech.Config
|
|
||||||
|
|
||||||
ssCipher core.Cipher
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type TrojanOption struct {
|
type TrojanOption struct {
|
||||||
@@ -50,38 +43,19 @@ type TrojanOption struct {
|
|||||||
Fingerprint string `proxy:"fingerprint,omitempty"`
|
Fingerprint string `proxy:"fingerprint,omitempty"`
|
||||||
UDP bool `proxy:"udp,omitempty"`
|
UDP bool `proxy:"udp,omitempty"`
|
||||||
Network string `proxy:"network,omitempty"`
|
Network string `proxy:"network,omitempty"`
|
||||||
ECHOpts ECHOptions `proxy:"ech-opts,omitempty"`
|
|
||||||
RealityOpts RealityOptions `proxy:"reality-opts,omitempty"`
|
RealityOpts RealityOptions `proxy:"reality-opts,omitempty"`
|
||||||
GrpcOpts GrpcOptions `proxy:"grpc-opts,omitempty"`
|
GrpcOpts GrpcOptions `proxy:"grpc-opts,omitempty"`
|
||||||
WSOpts WSOptions `proxy:"ws-opts,omitempty"`
|
WSOpts WSOptions `proxy:"ws-opts,omitempty"`
|
||||||
SSOpts TrojanSSOption `proxy:"ss-opts,omitempty"`
|
|
||||||
ClientFingerprint string `proxy:"client-fingerprint,omitempty"`
|
ClientFingerprint string `proxy:"client-fingerprint,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TrojanSSOption from https://github.com/p4gefau1t/trojan-go/blob/v0.10.6/tunnel/shadowsocks/config.go#L5
|
func (t *Trojan) plainStream(ctx context.Context, c net.Conn) (net.Conn, error) {
|
||||||
type TrojanSSOption struct {
|
if t.option.Network == "ws" {
|
||||||
Enabled bool `proxy:"enabled,omitempty"`
|
|
||||||
Method string `proxy:"method,omitempty"`
|
|
||||||
Password string `proxy:"password,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// StreamConnContext implements C.ProxyAdapter
|
|
||||||
func (t *Trojan) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ net.Conn, err error) {
|
|
||||||
switch t.option.Network {
|
|
||||||
case "ws":
|
|
||||||
host, port, _ := net.SplitHostPort(t.addr)
|
host, port, _ := net.SplitHostPort(t.addr)
|
||||||
|
wsOpts := &trojan.WebsocketOption{
|
||||||
wsOpts := &vmess.WebsocketConfig{
|
Host: host,
|
||||||
Host: host,
|
Port: port,
|
||||||
Port: port,
|
Path: t.option.WSOpts.Path,
|
||||||
Path: t.option.WSOpts.Path,
|
|
||||||
MaxEarlyData: t.option.WSOpts.MaxEarlyData,
|
|
||||||
EarlyDataHeaderName: t.option.WSOpts.EarlyDataHeaderName,
|
|
||||||
V2rayHttpUpgrade: t.option.WSOpts.V2rayHttpUpgrade,
|
|
||||||
V2rayHttpUpgradeFastOpen: t.option.WSOpts.V2rayHttpUpgradeFastOpen,
|
|
||||||
ClientFingerprint: t.option.ClientFingerprint,
|
|
||||||
ECHConfig: t.echConfig,
|
|
||||||
Headers: http.Header{},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if t.option.SNI != "" {
|
if t.option.SNI != "" {
|
||||||
@@ -89,107 +63,62 @@ func (t *Trojan) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(t.option.WSOpts.Headers) != 0 {
|
if len(t.option.WSOpts.Headers) != 0 {
|
||||||
|
header := http.Header{}
|
||||||
for key, value := range t.option.WSOpts.Headers {
|
for key, value := range t.option.WSOpts.Headers {
|
||||||
wsOpts.Headers.Add(key, value)
|
header.Add(key, value)
|
||||||
}
|
}
|
||||||
|
wsOpts.Headers = header
|
||||||
}
|
}
|
||||||
|
|
||||||
alpn := trojan.DefaultWebsocketALPN
|
return t.instance.StreamWebsocketConn(ctx, c, wsOpts)
|
||||||
if len(t.option.ALPN) != 0 {
|
|
||||||
alpn = t.option.ALPN
|
|
||||||
}
|
|
||||||
|
|
||||||
wsOpts.TLS = true
|
|
||||||
tlsConfig := &tls.Config{
|
|
||||||
NextProtos: alpn,
|
|
||||||
MinVersion: tls.VersionTLS12,
|
|
||||||
InsecureSkipVerify: t.option.SkipCertVerify,
|
|
||||||
ServerName: t.option.SNI,
|
|
||||||
}
|
|
||||||
|
|
||||||
wsOpts.TLSConfig, err = ca.GetSpecifiedFingerprintTLSConfig(tlsConfig, t.option.Fingerprint)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
c, err = vmess.StreamWebsocketConn(ctx, c, wsOpts)
|
|
||||||
case "grpc":
|
|
||||||
c, err = gun.StreamGunWithConn(c, t.gunTLSConfig, t.gunConfig, t.echConfig, t.realityConfig)
|
|
||||||
default:
|
|
||||||
// default tcp network
|
|
||||||
// handle TLS
|
|
||||||
alpn := trojan.DefaultALPN
|
|
||||||
if len(t.option.ALPN) != 0 {
|
|
||||||
alpn = t.option.ALPN
|
|
||||||
}
|
|
||||||
c, err = vmess.StreamTLSConn(ctx, c, &vmess.TLSConfig{
|
|
||||||
Host: t.option.SNI,
|
|
||||||
SkipCertVerify: t.option.SkipCertVerify,
|
|
||||||
FingerPrint: t.option.Fingerprint,
|
|
||||||
ClientFingerprint: t.option.ClientFingerprint,
|
|
||||||
NextProtos: alpn,
|
|
||||||
ECH: t.echConfig,
|
|
||||||
Reality: t.realityConfig,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return t.instance.StreamConn(ctx, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// StreamConnContext implements C.ProxyAdapter
|
||||||
|
func (t *Trojan) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if tlsC.HaveGlobalFingerprint() && len(t.option.ClientFingerprint) == 0 {
|
||||||
|
t.option.ClientFingerprint = tlsC.GetGlobalFingerprint()
|
||||||
|
}
|
||||||
|
|
||||||
|
if t.transport != nil {
|
||||||
|
c, err = gun.StreamGunWithConn(c, t.gunTLSConfig, t.gunConfig, t.realityConfig)
|
||||||
|
} else {
|
||||||
|
c, err = t.plainStream(ctx, c)
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
return t.streamConnContext(ctx, c, metadata)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *Trojan) streamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ net.Conn, err error) {
|
|
||||||
if t.ssCipher != nil {
|
|
||||||
c = t.ssCipher.StreamConn(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
if ctx.Done() != nil {
|
|
||||||
done := N.SetupContextForConn(ctx, c)
|
|
||||||
defer done(&err)
|
|
||||||
}
|
|
||||||
command := trojan.CommandTCP
|
|
||||||
if metadata.NetWork == C.UDP {
|
if metadata.NetWork == C.UDP {
|
||||||
command = trojan.CommandUDP
|
err = t.instance.WriteHeader(c, trojan.CommandUDP, serializesSocksAddr(metadata))
|
||||||
|
return c, err
|
||||||
}
|
}
|
||||||
err = trojan.WriteHeader(c, t.hexPassword, command, serializesSocksAddr(metadata))
|
err = t.instance.WriteHeader(c, trojan.CommandTCP, serializesSocksAddr(metadata))
|
||||||
return c, err
|
return c, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Trojan) writeHeaderContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (err error) {
|
|
||||||
if ctx.Done() != nil {
|
|
||||||
done := N.SetupContextForConn(ctx, c)
|
|
||||||
defer done(&err)
|
|
||||||
}
|
|
||||||
command := trojan.CommandTCP
|
|
||||||
if metadata.NetWork == C.UDP {
|
|
||||||
command = trojan.CommandUDP
|
|
||||||
}
|
|
||||||
err = trojan.WriteHeader(c, t.hexPassword, command, serializesSocksAddr(metadata))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (t *Trojan) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
func (t *Trojan) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
||||||
var c net.Conn
|
|
||||||
// gun transport
|
// gun transport
|
||||||
if t.transport != nil {
|
if t.transport != nil && len(opts) == 0 {
|
||||||
c, err = gun.StreamGunWithTransport(t.transport, t.gunConfig)
|
c, err := gun.StreamGunWithTransport(t.transport, t.gunConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer func(c net.Conn) {
|
|
||||||
safeConnClose(c, err)
|
|
||||||
}(c)
|
|
||||||
|
|
||||||
c, err = t.streamConnContext(ctx, c, metadata)
|
if err = t.instance.WriteHeader(c, trojan.CommandTCP, serializesSocksAddr(metadata)); err != nil {
|
||||||
if err != nil {
|
c.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return NewConn(c, t), nil
|
return NewConn(c, t), nil
|
||||||
}
|
}
|
||||||
return t.DialContextWithDialer(ctx, dialer.NewDialer(t.DialOptions()...), metadata)
|
return t.DialContextWithDialer(ctx, dialer.NewDialer(t.Base.DialOptions(opts...)...), metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialContextWithDialer implements C.ProxyAdapter
|
// DialContextWithDialer implements C.ProxyAdapter
|
||||||
@@ -204,6 +133,7 @@ func (t *Trojan) DialContextWithDialer(ctx context.Context, dialer C.Dialer, met
|
|||||||
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)
|
||||||
}
|
}
|
||||||
|
N.TCPKeepAlive(c)
|
||||||
|
|
||||||
defer func(c net.Conn) {
|
defer func(c net.Conn) {
|
||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
@@ -218,11 +148,11 @@ func (t *Trojan) DialContextWithDialer(ctx context.Context, dialer C.Dialer, met
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (t *Trojan) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
func (t *Trojan) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
||||||
var c net.Conn
|
var c net.Conn
|
||||||
|
|
||||||
// grpc transport
|
// grpc transport
|
||||||
if t.transport != nil {
|
if t.transport != nil && len(opts) == 0 {
|
||||||
c, err = gun.StreamGunWithTransport(t.transport, t.gunConfig)
|
c, err = gun.StreamGunWithTransport(t.transport, t.gunConfig)
|
||||||
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)
|
||||||
@@ -230,16 +160,15 @@ func (t *Trojan) ListenPacketContext(ctx context.Context, metadata *C.Metadata)
|
|||||||
defer func(c net.Conn) {
|
defer func(c net.Conn) {
|
||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
}(c)
|
}(c)
|
||||||
|
err = t.instance.WriteHeader(c, trojan.CommandUDP, serializesSocksAddr(metadata))
|
||||||
c, err = t.streamConnContext(ctx, c, metadata)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
pc := trojan.NewPacketConn(c)
|
pc := t.instance.PacketConn(c)
|
||||||
return newPacketConn(pc, t), err
|
return newPacketConn(pc, t), err
|
||||||
}
|
}
|
||||||
return t.ListenPacketWithDialer(ctx, dialer.NewDialer(t.DialOptions()...), metadata)
|
return t.ListenPacketWithDialer(ctx, dialer.NewDialer(t.Base.DialOptions(opts...)...), metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketWithDialer implements C.ProxyAdapter
|
// ListenPacketWithDialer implements C.ProxyAdapter
|
||||||
@@ -257,12 +186,18 @@ func (t *Trojan) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, me
|
|||||||
defer func(c net.Conn) {
|
defer func(c net.Conn) {
|
||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
}(c)
|
}(c)
|
||||||
c, err = t.StreamConnContext(ctx, c, metadata)
|
N.TCPKeepAlive(c)
|
||||||
|
c, err = t.plainStream(ctx, c)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("%s connect error: %w", t.addr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = t.instance.WriteHeader(c, trojan.CommandUDP, serializesSocksAddr(metadata))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
pc := trojan.NewPacketConn(c)
|
pc := t.instance.PacketConn(c)
|
||||||
return newPacketConn(pc, t), err
|
return newPacketConn(pc, t), err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,7 +208,7 @@ func (t *Trojan) SupportWithDialer() C.NetWork {
|
|||||||
|
|
||||||
// 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 := trojan.NewPacketConn(c)
|
pc := t.instance.PacketConn(c)
|
||||||
return newPacketConn(pc, t), err
|
return newPacketConn(pc, t), err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -282,26 +217,20 @@ func (t *Trojan) SupportUOT() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProxyInfo implements C.ProxyAdapter
|
|
||||||
func (t *Trojan) ProxyInfo() C.ProxyInfo {
|
|
||||||
info := t.Base.ProxyInfo()
|
|
||||||
info.DialerProxy = t.option.DialerProxy
|
|
||||||
return info
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close implements C.ProxyAdapter
|
|
||||||
func (t *Trojan) Close() error {
|
|
||||||
if t.transport != nil {
|
|
||||||
return t.transport.Close()
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewTrojan(option TrojanOption) (*Trojan, error) {
|
func NewTrojan(option TrojanOption) (*Trojan, error) {
|
||||||
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
||||||
|
|
||||||
if option.SNI == "" {
|
tOption := &trojan.Option{
|
||||||
option.SNI = option.Server
|
Password: option.Password,
|
||||||
|
ALPN: option.ALPN,
|
||||||
|
ServerName: option.Server,
|
||||||
|
SkipCertVerify: option.SkipCertVerify,
|
||||||
|
Fingerprint: option.Fingerprint,
|
||||||
|
ClientFingerprint: option.ClientFingerprint,
|
||||||
|
}
|
||||||
|
|
||||||
|
if option.SNI != "" {
|
||||||
|
tOption.ServerName = option.SNI
|
||||||
}
|
}
|
||||||
|
|
||||||
t := &Trojan{
|
t := &Trojan{
|
||||||
@@ -316,8 +245,8 @@ func NewTrojan(option TrojanOption) (*Trojan, error) {
|
|||||||
rmark: option.RoutingMark,
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: C.NewDNSPrefer(option.IPVersion),
|
||||||
},
|
},
|
||||||
option: &option,
|
instance: trojan.New(tOption),
|
||||||
hexPassword: trojan.Key(option.Password),
|
option: &option,
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
@@ -325,48 +254,31 @@ func NewTrojan(option TrojanOption) (*Trojan, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
tOption.Reality = t.realityConfig
|
||||||
t.echConfig, err = option.ECHOpts.Parse()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if option.SSOpts.Enabled {
|
|
||||||
if option.SSOpts.Password == "" {
|
|
||||||
return nil, errors.New("empty password")
|
|
||||||
}
|
|
||||||
if option.SSOpts.Method == "" {
|
|
||||||
option.SSOpts.Method = "AES-128-GCM"
|
|
||||||
}
|
|
||||||
ciph, err := core.PickCipher(option.SSOpts.Method, nil, option.SSOpts.Password)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
t.ssCipher = ciph
|
|
||||||
}
|
|
||||||
|
|
||||||
if option.Network == "grpc" {
|
if option.Network == "grpc" {
|
||||||
dialFn := func(ctx context.Context, network, addr string) (net.Conn, error) {
|
dialFn := func(network, addr string) (net.Conn, error) {
|
||||||
var err error
|
var err error
|
||||||
var cDialer C.Dialer = dialer.NewDialer(t.DialOptions()...)
|
var cDialer C.Dialer = dialer.NewDialer(t.Base.DialOptions()...)
|
||||||
if len(t.option.DialerProxy) > 0 {
|
if len(t.option.DialerProxy) > 0 {
|
||||||
cDialer, err = proxydialer.NewByName(t.option.DialerProxy, cDialer)
|
cDialer, err = proxydialer.NewByName(t.option.DialerProxy, cDialer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c, err := cDialer.DialContext(ctx, "tcp", t.addr)
|
c, err := cDialer.DialContext(context.Background(), "tcp", t.addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error: %s", t.addr, err.Error())
|
return nil, fmt.Errorf("%s connect error: %s", t.addr, err.Error())
|
||||||
}
|
}
|
||||||
|
N.TCPKeepAlive(c)
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
tlsConfig := &tls.Config{
|
tlsConfig := &tls.Config{
|
||||||
NextProtos: option.ALPN,
|
NextProtos: option.ALPN,
|
||||||
MinVersion: tls.VersionTLS12,
|
MinVersion: tls.VersionTLS12,
|
||||||
InsecureSkipVerify: option.SkipCertVerify,
|
InsecureSkipVerify: tOption.SkipCertVerify,
|
||||||
ServerName: option.SNI,
|
ServerName: tOption.ServerName,
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
@@ -375,13 +287,12 @@ func NewTrojan(option TrojanOption) (*Trojan, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
t.transport = gun.NewHTTP2Client(dialFn, tlsConfig, option.ClientFingerprint, t.echConfig, t.realityConfig)
|
t.transport = gun.NewHTTP2Client(dialFn, tlsConfig, tOption.ClientFingerprint, t.realityConfig)
|
||||||
|
|
||||||
t.gunTLSConfig = tlsConfig
|
t.gunTLSConfig = tlsConfig
|
||||||
t.gunConfig = &gun.Config{
|
t.gunConfig = &gun.Config{
|
||||||
ServiceName: option.GrpcOpts.GrpcServiceName,
|
ServiceName: option.GrpcOpts.GrpcServiceName,
|
||||||
Host: option.SNI,
|
Host: tOption.ServerName,
|
||||||
ClientFingerprint: option.ClientFingerprint,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,28 +10,23 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/component/ca"
|
"github.com/Dreamacro/clash/component/ca"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
"github.com/metacubex/mihomo/component/ech"
|
"github.com/Dreamacro/clash/component/proxydialer"
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
"github.com/Dreamacro/clash/component/resolver"
|
||||||
"github.com/metacubex/mihomo/component/resolver"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
tlsC "github.com/metacubex/mihomo/component/tls"
|
"github.com/Dreamacro/clash/transport/tuic"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
|
||||||
"github.com/metacubex/mihomo/transport/tuic"
|
|
||||||
|
|
||||||
"github.com/gofrs/uuid/v5"
|
"github.com/gofrs/uuid/v5"
|
||||||
"github.com/metacubex/quic-go"
|
"github.com/metacubex/quic-go"
|
||||||
M "github.com/metacubex/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
"github.com/metacubex/sing/common/uot"
|
"github.com/sagernet/sing/common/uot"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Tuic struct {
|
type Tuic struct {
|
||||||
*Base
|
*Base
|
||||||
option *TuicOption
|
option *TuicOption
|
||||||
client *tuic.PoolClient
|
client *tuic.PoolClient
|
||||||
|
|
||||||
tlsConfig *tlsC.Config
|
|
||||||
echConfig *ech.Config
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type TuicOption struct {
|
type TuicOption struct {
|
||||||
@@ -52,27 +47,26 @@ type TuicOption struct {
|
|||||||
DisableSni bool `proxy:"disable-sni,omitempty"`
|
DisableSni bool `proxy:"disable-sni,omitempty"`
|
||||||
MaxUdpRelayPacketSize int `proxy:"max-udp-relay-packet-size,omitempty"`
|
MaxUdpRelayPacketSize int `proxy:"max-udp-relay-packet-size,omitempty"`
|
||||||
|
|
||||||
FastOpen bool `proxy:"fast-open,omitempty"`
|
FastOpen bool `proxy:"fast-open,omitempty"`
|
||||||
MaxOpenStreams int `proxy:"max-open-streams,omitempty"`
|
MaxOpenStreams int `proxy:"max-open-streams,omitempty"`
|
||||||
CWND int `proxy:"cwnd,omitempty"`
|
CWND int `proxy:"cwnd,omitempty"`
|
||||||
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
||||||
Fingerprint string `proxy:"fingerprint,omitempty"`
|
Fingerprint string `proxy:"fingerprint,omitempty"`
|
||||||
CustomCA string `proxy:"ca,omitempty"`
|
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"`
|
||||||
MaxDatagramFrameSize int `proxy:"max-datagram-frame-size,omitempty"`
|
MaxDatagramFrameSize int `proxy:"max-datagram-frame-size,omitempty"`
|
||||||
SNI string `proxy:"sni,omitempty"`
|
SNI string `proxy:"sni,omitempty"`
|
||||||
ECHOpts ECHOptions `proxy:"ech-opts,omitempty"`
|
|
||||||
|
|
||||||
UDPOverStream bool `proxy:"udp-over-stream,omitempty"`
|
UDPOverStream bool `proxy:"udp-over-stream,omitempty"`
|
||||||
UDPOverStreamVersion int `proxy:"udp-over-stream-version,omitempty"`
|
UDPOverStreamVersion int `proxy:"udp-over-stream-version,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (t *Tuic) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
func (t *Tuic) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
||||||
return t.DialContextWithDialer(ctx, dialer.NewDialer(t.DialOptions()...), metadata)
|
return t.DialContextWithDialer(ctx, dialer.NewDialer(t.Base.DialOptions(opts...)...), metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialContextWithDialer implements C.ProxyAdapter
|
// DialContextWithDialer implements C.ProxyAdapter
|
||||||
@@ -85,8 +79,8 @@ func (t *Tuic) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metad
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (t *Tuic) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
func (t *Tuic) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
||||||
return t.ListenPacketWithDialer(ctx, dialer.NewDialer(t.DialOptions()...), metadata)
|
return t.ListenPacketWithDialer(ctx, dialer.NewDialer(t.Base.DialOptions(opts...)...), metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketWithDialer implements C.ProxyAdapter
|
// ListenPacketWithDialer implements C.ProxyAdapter
|
||||||
@@ -136,11 +130,7 @@ func (t *Tuic) dialWithDialer(ctx context.Context, dialer C.Dialer) (transport *
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
udpAddr, err := resolveUDPAddr(ctx, "udp", t.addr, t.prefer)
|
udpAddr, err := resolveUDPAddrWithPrefer(ctx, "udp", t.addr, t.prefer)
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
err = t.echConfig.ClientHandle(ctx, t.tlsConfig)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@@ -156,13 +146,6 @@ func (t *Tuic) dialWithDialer(ctx context.Context, dialer C.Dialer) (transport *
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProxyInfo implements C.ProxyAdapter
|
|
||||||
func (t *Tuic) ProxyInfo() C.ProxyInfo {
|
|
||||||
info := t.Base.ProxyInfo()
|
|
||||||
info.DialerProxy = t.option.DialerProxy
|
|
||||||
return info
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewTuic(option TuicOption) (*Tuic, error) {
|
func NewTuic(option TuicOption) (*Tuic, error) {
|
||||||
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
||||||
serverName := option.Server
|
serverName := option.Server
|
||||||
@@ -258,12 +241,6 @@ func NewTuic(option TuicOption) (*Tuic, error) {
|
|||||||
tlsConfig.InsecureSkipVerify = true // tls: either ServerName or InsecureSkipVerify must be specified in the tls.Config
|
tlsConfig.InsecureSkipVerify = true // tls: either ServerName or InsecureSkipVerify must be specified in the tls.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
tlsClientConfig := tlsC.UConfig(tlsConfig)
|
|
||||||
echConfig, err := option.ECHOpts.Parse()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
switch option.UDPOverStreamVersion {
|
switch option.UDPOverStreamVersion {
|
||||||
case uot.Version, uot.LegacyVersion:
|
case uot.Version, uot.LegacyVersion:
|
||||||
case 0:
|
case 0:
|
||||||
@@ -283,9 +260,7 @@ func NewTuic(option TuicOption) (*Tuic, error) {
|
|||||||
rmark: option.RoutingMark,
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: C.NewDNSPrefer(option.IPVersion),
|
||||||
},
|
},
|
||||||
option: &option,
|
option: &option,
|
||||||
tlsConfig: tlsClientConfig,
|
|
||||||
echConfig: echConfig,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
clientMaxOpenStreams := int64(option.MaxOpenStreams)
|
clientMaxOpenStreams := int64(option.MaxOpenStreams)
|
||||||
@@ -302,7 +277,7 @@ func NewTuic(option TuicOption) (*Tuic, error) {
|
|||||||
if len(option.Token) > 0 {
|
if len(option.Token) > 0 {
|
||||||
tkn := tuic.GenTKN(option.Token)
|
tkn := tuic.GenTKN(option.Token)
|
||||||
clientOption := &tuic.ClientOptionV4{
|
clientOption := &tuic.ClientOptionV4{
|
||||||
TlsConfig: tlsClientConfig,
|
TlsConfig: tlsConfig,
|
||||||
QuicConfig: quicConfig,
|
QuicConfig: quicConfig,
|
||||||
Token: tkn,
|
Token: tkn,
|
||||||
UdpRelayMode: udpRelayMode,
|
UdpRelayMode: udpRelayMode,
|
||||||
@@ -322,7 +297,7 @@ func NewTuic(option TuicOption) (*Tuic, error) {
|
|||||||
maxUdpRelayPacketSize = tuic.MaxFragSizeV5
|
maxUdpRelayPacketSize = tuic.MaxFragSizeV5
|
||||||
}
|
}
|
||||||
clientOption := &tuic.ClientOptionV5{
|
clientOption := &tuic.ClientOptionV5{
|
||||||
TlsConfig: tlsClientConfig,
|
TlsConfig: tlsConfig,
|
||||||
QuicConfig: quicConfig,
|
QuicConfig: quicConfig,
|
||||||
Uuid: uuid.FromStringOrNil(option.UUID),
|
Uuid: uuid.FromStringOrNil(option.UUID),
|
||||||
Password: option.Password,
|
Password: option.Password,
|
||||||
|
|||||||
@@ -3,59 +3,118 @@ package outbound
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/component/resolver"
|
"github.com/Dreamacro/clash/component/resolver"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/metacubex/mihomo/transport/socks5"
|
"github.com/Dreamacro/clash/transport/socks5"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
globalClientSessionCache tls.ClientSessionCache
|
||||||
|
once sync.Once
|
||||||
|
)
|
||||||
|
|
||||||
|
func getClientSessionCache() tls.ClientSessionCache {
|
||||||
|
once.Do(func() {
|
||||||
|
globalClientSessionCache = tls.NewLRUClientSessionCache(128)
|
||||||
|
})
|
||||||
|
return globalClientSessionCache
|
||||||
|
}
|
||||||
|
|
||||||
func serializesSocksAddr(metadata *C.Metadata) []byte {
|
func serializesSocksAddr(metadata *C.Metadata) []byte {
|
||||||
var buf [][]byte
|
var buf [][]byte
|
||||||
addrType := metadata.AddrType()
|
addrType := metadata.AddrType()
|
||||||
|
aType := uint8(addrType)
|
||||||
p := uint(metadata.DstPort)
|
p := uint(metadata.DstPort)
|
||||||
port := []byte{uint8(p >> 8), uint8(p & 0xff)}
|
port := []byte{uint8(p >> 8), uint8(p & 0xff)}
|
||||||
switch addrType {
|
switch addrType {
|
||||||
case C.AtypDomainName:
|
case socks5.AtypDomainName:
|
||||||
lenM := uint8(len(metadata.Host))
|
lenM := uint8(len(metadata.Host))
|
||||||
host := []byte(metadata.Host)
|
host := []byte(metadata.Host)
|
||||||
buf = [][]byte{{socks5.AtypDomainName, lenM}, host, port}
|
buf = [][]byte{{aType, lenM}, host, port}
|
||||||
case C.AtypIPv4:
|
case socks5.AtypIPv4:
|
||||||
host := metadata.DstIP.AsSlice()
|
host := metadata.DstIP.AsSlice()
|
||||||
buf = [][]byte{{socks5.AtypIPv4}, host, port}
|
buf = [][]byte{{aType}, host, port}
|
||||||
case C.AtypIPv6:
|
case socks5.AtypIPv6:
|
||||||
host := metadata.DstIP.AsSlice()
|
host := metadata.DstIP.AsSlice()
|
||||||
buf = [][]byte{{socks5.AtypIPv6}, host, port}
|
buf = [][]byte{{aType}, host, port}
|
||||||
}
|
}
|
||||||
return bytes.Join(buf, nil)
|
return bytes.Join(buf, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveUDPAddr(ctx context.Context, network, address string, prefer C.DNSPrefer) (*net.UDPAddr, error) {
|
func resolveUDPAddr(ctx context.Context, network, address string) (*net.UDPAddr, error) {
|
||||||
|
host, port, err := net.SplitHostPort(address)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ip, err := resolver.ResolveProxyServerHost(ctx, host)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return net.ResolveUDPAddr(network, net.JoinHostPort(ip.String(), port))
|
||||||
|
}
|
||||||
|
|
||||||
|
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.ResolveIPv4WithResolver(ctx, host, resolver.ProxyServerHostResolver)
|
ip, err = resolver.ResolveIPv4ProxyServerHost(ctx, host)
|
||||||
case C.IPv6Only:
|
case C.IPv6Only:
|
||||||
ip, err = resolver.ResolveIPv6WithResolver(ctx, host, resolver.ProxyServerHostResolver)
|
ip, err = resolver.ResolveIPv6ProxyServerHost(ctx, host)
|
||||||
case C.IPv6Prefer:
|
case C.IPv6Prefer:
|
||||||
ip, err = resolver.ResolveIPPrefer6WithResolver(ctx, host, resolver.ProxyServerHostResolver)
|
var ips []netip.Addr
|
||||||
|
ips, err = resolver.LookupIPProxyServerHost(ctx, host)
|
||||||
|
if err == nil {
|
||||||
|
for _, addr := range ips {
|
||||||
|
if addr.Is6() {
|
||||||
|
ip = addr
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
if !fallback.IsValid() {
|
||||||
|
fallback = addr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
ip, err = resolver.ResolveIPWithResolver(ctx, host, resolver.ProxyServerHostResolver)
|
// C.IPv4Prefer, C.DualStack and other
|
||||||
|
var ips []netip.Addr
|
||||||
|
ips, err = resolver.LookupIPProxyServerHost(ctx, host)
|
||||||
|
if err == nil {
|
||||||
|
for _, addr := range ips {
|
||||||
|
if addr.Is4() {
|
||||||
|
ip = addr
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
if !fallback.IsValid() {
|
||||||
|
fallback = addr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !ip.IsValid() && fallback.IsValid() {
|
||||||
|
ip = fallback
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
ip, port = resolver.LookupIP4P(ip, port)
|
|
||||||
return net.ResolveUDPAddr(network, net.JoinHostPort(ip.String(), port))
|
return net.ResolveUDPAddr(network, net.JoinHostPort(ip.String(), port))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,25 +140,24 @@ func StringToBps(s string) uint64 {
|
|||||||
if m == nil {
|
if m == nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
var n uint64 = 1
|
var n uint64
|
||||||
switch m[2] {
|
switch m[2] {
|
||||||
case "T":
|
|
||||||
n *= 1000
|
|
||||||
fallthrough
|
|
||||||
case "G":
|
|
||||||
n *= 1000
|
|
||||||
fallthrough
|
|
||||||
case "M":
|
|
||||||
n *= 1000
|
|
||||||
fallthrough
|
|
||||||
case "K":
|
case "K":
|
||||||
n *= 1000
|
n = 1 << 10
|
||||||
|
case "M":
|
||||||
|
n = 1 << 20
|
||||||
|
case "G":
|
||||||
|
n = 1 << 30
|
||||||
|
case "T":
|
||||||
|
n = 1 << 40
|
||||||
|
default:
|
||||||
|
n = 1
|
||||||
}
|
}
|
||||||
v, _ := strconv.ParseUint(m[1], 10, 64)
|
v, _ := strconv.ParseUint(m[1], 10, 64)
|
||||||
n *= v
|
n = v * n
|
||||||
if m[3] == "b" {
|
if m[3] == "b" {
|
||||||
// Bits, need to convert to bytes
|
// Bits, need to convert to bytes
|
||||||
n /= 8
|
n = n >> 3
|
||||||
}
|
}
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,23 +12,24 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/convert"
|
"github.com/Dreamacro/clash/common/convert"
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/Dreamacro/clash/common/utils"
|
||||||
"github.com/metacubex/mihomo/component/ca"
|
"github.com/Dreamacro/clash/component/ca"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
"github.com/metacubex/mihomo/component/ech"
|
"github.com/Dreamacro/clash/component/proxydialer"
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
"github.com/Dreamacro/clash/component/resolver"
|
||||||
"github.com/metacubex/mihomo/component/resolver"
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
||||||
tlsC "github.com/metacubex/mihomo/component/tls"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
"github.com/Dreamacro/clash/log"
|
||||||
"github.com/metacubex/mihomo/transport/gun"
|
"github.com/Dreamacro/clash/transport/gun"
|
||||||
"github.com/metacubex/mihomo/transport/vless"
|
"github.com/Dreamacro/clash/transport/socks5"
|
||||||
"github.com/metacubex/mihomo/transport/vmess"
|
"github.com/Dreamacro/clash/transport/vless"
|
||||||
|
"github.com/Dreamacro/clash/transport/vmess"
|
||||||
|
|
||||||
vmessSing "github.com/metacubex/sing-vmess"
|
vmessSing "github.com/metacubex/sing-vmess"
|
||||||
"github.com/metacubex/sing-vmess/packetaddr"
|
"github.com/metacubex/sing-vmess/packetaddr"
|
||||||
M "github.com/metacubex/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -47,7 +48,6 @@ type Vless struct {
|
|||||||
transport *gun.TransportWrap
|
transport *gun.TransportWrap
|
||||||
|
|
||||||
realityConfig *tlsC.RealityConfig
|
realityConfig *tlsC.RealityConfig
|
||||||
echConfig *ech.Config
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type VlessOption struct {
|
type VlessOption struct {
|
||||||
@@ -64,7 +64,6 @@ type VlessOption struct {
|
|||||||
XUDP bool `proxy:"xudp,omitempty"`
|
XUDP bool `proxy:"xudp,omitempty"`
|
||||||
PacketEncoding string `proxy:"packet-encoding,omitempty"`
|
PacketEncoding string `proxy:"packet-encoding,omitempty"`
|
||||||
Network string `proxy:"network,omitempty"`
|
Network string `proxy:"network,omitempty"`
|
||||||
ECHOpts ECHOptions `proxy:"ech-opts,omitempty"`
|
|
||||||
RealityOpts RealityOptions `proxy:"reality-opts,omitempty"`
|
RealityOpts RealityOptions `proxy:"reality-opts,omitempty"`
|
||||||
HTTPOpts HTTPOptions `proxy:"http-opts,omitempty"`
|
HTTPOpts HTTPOptions `proxy:"http-opts,omitempty"`
|
||||||
HTTP2Opts HTTP2Options `proxy:"h2-opts,omitempty"`
|
HTTP2Opts HTTP2Options `proxy:"h2-opts,omitempty"`
|
||||||
@@ -78,21 +77,24 @@ type VlessOption struct {
|
|||||||
ClientFingerprint string `proxy:"client-fingerprint,omitempty"`
|
ClientFingerprint string `proxy:"client-fingerprint,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Vless) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ net.Conn, err error) {
|
func (v *Vless) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if tlsC.HaveGlobalFingerprint() && len(v.option.ClientFingerprint) == 0 {
|
||||||
|
v.option.ClientFingerprint = tlsC.GetGlobalFingerprint()
|
||||||
|
}
|
||||||
|
|
||||||
switch v.option.Network {
|
switch v.option.Network {
|
||||||
case "ws":
|
case "ws":
|
||||||
host, port, _ := net.SplitHostPort(v.addr)
|
host, port, _ := net.SplitHostPort(v.addr)
|
||||||
wsOpts := &vmess.WebsocketConfig{
|
wsOpts := &vmess.WebsocketConfig{
|
||||||
Host: host,
|
Host: host,
|
||||||
Port: port,
|
Port: port,
|
||||||
Path: v.option.WSOpts.Path,
|
Path: v.option.WSOpts.Path,
|
||||||
MaxEarlyData: v.option.WSOpts.MaxEarlyData,
|
MaxEarlyData: v.option.WSOpts.MaxEarlyData,
|
||||||
EarlyDataHeaderName: v.option.WSOpts.EarlyDataHeaderName,
|
EarlyDataHeaderName: v.option.WSOpts.EarlyDataHeaderName,
|
||||||
V2rayHttpUpgrade: v.option.WSOpts.V2rayHttpUpgrade,
|
ClientFingerprint: v.option.ClientFingerprint,
|
||||||
V2rayHttpUpgradeFastOpen: v.option.WSOpts.V2rayHttpUpgradeFastOpen,
|
Headers: http.Header{},
|
||||||
ClientFingerprint: v.option.ClientFingerprint,
|
|
||||||
ECHConfig: v.echConfig,
|
|
||||||
Headers: http.Header{},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(v.option.WSOpts.Headers) != 0 {
|
if len(v.option.WSOpts.Headers) != 0 {
|
||||||
@@ -153,9 +155,9 @@ func (v *Vless) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
Path: v.option.HTTP2Opts.Path,
|
Path: v.option.HTTP2Opts.Path,
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err = vmess.StreamH2Conn(ctx, c, h2Opts)
|
c, err = vmess.StreamH2Conn(c, h2Opts)
|
||||||
case "grpc":
|
case "grpc":
|
||||||
c, err = gun.StreamGunWithConn(c, v.gunTLSConfig, v.gunConfig, v.echConfig, v.realityConfig)
|
c, err = gun.StreamGunWithConn(c, v.gunTLSConfig, v.gunConfig, v.realityConfig)
|
||||||
default:
|
default:
|
||||||
// default tcp network
|
// default tcp network
|
||||||
// handle TLS
|
// handle TLS
|
||||||
@@ -166,14 +168,10 @@ func (v *Vless) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return v.streamConnContext(ctx, c, metadata)
|
return v.streamConn(c, metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Vless) streamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (conn net.Conn, err error) {
|
func (v *Vless) streamConn(c net.Conn, metadata *C.Metadata) (conn net.Conn, err error) {
|
||||||
if ctx.Done() != nil {
|
|
||||||
done := N.SetupContextForConn(ctx, c)
|
|
||||||
defer done(&err)
|
|
||||||
}
|
|
||||||
if metadata.NetWork == C.UDP {
|
if metadata.NetWork == C.UDP {
|
||||||
if v.option.PacketAddr {
|
if v.option.PacketAddr {
|
||||||
metadata = &C.Metadata{
|
metadata = &C.Metadata{
|
||||||
@@ -210,7 +208,6 @@ func (v *Vless) streamTLSConn(ctx context.Context, conn net.Conn, isH2 bool) (ne
|
|||||||
SkipCertVerify: v.option.SkipCertVerify,
|
SkipCertVerify: v.option.SkipCertVerify,
|
||||||
FingerPrint: v.option.Fingerprint,
|
FingerPrint: v.option.Fingerprint,
|
||||||
ClientFingerprint: v.option.ClientFingerprint,
|
ClientFingerprint: v.option.ClientFingerprint,
|
||||||
ECH: v.echConfig,
|
|
||||||
Reality: v.realityConfig,
|
Reality: v.realityConfig,
|
||||||
NextProtos: v.option.ALPN,
|
NextProtos: v.option.ALPN,
|
||||||
}
|
}
|
||||||
@@ -230,11 +227,10 @@ func (v *Vless) streamTLSConn(ctx context.Context, conn net.Conn, isH2 bool) (ne
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (v *Vless) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
func (v *Vless) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
||||||
var c net.Conn
|
|
||||||
// gun transport
|
// gun transport
|
||||||
if v.transport != nil {
|
if v.transport != nil && len(opts) == 0 {
|
||||||
c, err = gun.StreamGunWithTransport(v.transport, v.gunConfig)
|
c, err := gun.StreamGunWithTransport(v.transport, v.gunConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -242,14 +238,14 @@ func (v *Vless) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn
|
|||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
}(c)
|
}(c)
|
||||||
|
|
||||||
c, err = v.streamConnContext(ctx, c, 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.DialOptions()...), metadata)
|
return v.DialContextWithDialer(ctx, dialer.NewDialer(v.Base.DialOptions(opts...)...), metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialContextWithDialer implements C.ProxyAdapter
|
// DialContextWithDialer implements C.ProxyAdapter
|
||||||
@@ -264,6 +260,7 @@ func (v *Vless) DialContextWithDialer(ctx context.Context, dialer C.Dialer, meta
|
|||||||
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())
|
||||||
}
|
}
|
||||||
|
N.TCPKeepAlive(c)
|
||||||
defer func(c net.Conn) {
|
defer func(c net.Conn) {
|
||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
}(c)
|
}(c)
|
||||||
@@ -276,7 +273,7 @@ func (v *Vless) DialContextWithDialer(ctx context.Context, dialer C.Dialer, meta
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (v *Vless) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ 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 need a net.UDPAddr
|
// vless use stream-oriented udp with a special address, so we need a net.UDPAddr
|
||||||
if !metadata.Resolved() {
|
if !metadata.Resolved() {
|
||||||
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
||||||
@@ -287,7 +284,7 @@ func (v *Vless) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (
|
|||||||
}
|
}
|
||||||
var c net.Conn
|
var c net.Conn
|
||||||
// gun transport
|
// gun transport
|
||||||
if v.transport != nil {
|
if v.transport != nil && len(opts) == 0 {
|
||||||
c, err = gun.StreamGunWithTransport(v.transport, v.gunConfig)
|
c, err = gun.StreamGunWithTransport(v.transport, v.gunConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -296,14 +293,14 @@ func (v *Vless) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (
|
|||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
}(c)
|
}(c)
|
||||||
|
|
||||||
c, err = v.streamConnContext(ctx, c, metadata)
|
c, err = v.streamConn(c, metadata)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("new vless client error: %v", err)
|
return nil, fmt.Errorf("new vless client error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return v.ListenPacketOnStreamConn(ctx, c, metadata)
|
return v.ListenPacketOnStreamConn(ctx, c, metadata)
|
||||||
}
|
}
|
||||||
return v.ListenPacketWithDialer(ctx, dialer.NewDialer(v.DialOptions()...), metadata)
|
return v.ListenPacketWithDialer(ctx, dialer.NewDialer(v.Base.DialOptions(opts...)...), metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketWithDialer implements C.ProxyAdapter
|
// ListenPacketWithDialer implements C.ProxyAdapter
|
||||||
@@ -328,6 +325,7 @@ func (v *Vless) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, met
|
|||||||
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())
|
||||||
}
|
}
|
||||||
|
N.TCPKeepAlive(c)
|
||||||
defer func(c net.Conn) {
|
defer func(c net.Conn) {
|
||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
}(c)
|
}(c)
|
||||||
@@ -373,7 +371,7 @@ func (v *Vless) ListenPacketOnStreamConn(ctx context.Context, c net.Conn, metada
|
|||||||
}, M.SocksaddrFromNet(metadata.UDPAddr())),
|
}, M.SocksaddrFromNet(metadata.UDPAddr())),
|
||||||
), v), nil
|
), v), nil
|
||||||
}
|
}
|
||||||
return newPacketConn(N.NewThreadSafePacketConn(&vlessPacketConn{Conn: c, rAddr: metadata.UDPAddr()}), v), nil
|
return newPacketConn(&vlessPacketConn{Conn: c, rAddr: metadata.UDPAddr()}, v), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SupportUOT implements C.ProxyAdapter
|
// SupportUOT implements C.ProxyAdapter
|
||||||
@@ -381,34 +379,19 @@ func (v *Vless) SupportUOT() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProxyInfo implements C.ProxyAdapter
|
|
||||||
func (v *Vless) ProxyInfo() C.ProxyInfo {
|
|
||||||
info := v.Base.ProxyInfo()
|
|
||||||
info.DialerProxy = v.option.DialerProxy
|
|
||||||
return info
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close implements C.ProxyAdapter
|
|
||||||
func (v *Vless) Close() error {
|
|
||||||
if v.transport != nil {
|
|
||||||
return v.transport.Close()
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseVlessAddr(metadata *C.Metadata, xudp bool) *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))
|
||||||
@@ -520,12 +503,17 @@ func NewVless(option VlessOption) (*Vless, error) {
|
|||||||
var addons *vless.Addons
|
var addons *vless.Addons
|
||||||
if option.Network != "ws" && len(option.Flow) >= 16 {
|
if option.Network != "ws" && len(option.Flow) >= 16 {
|
||||||
option.Flow = option.Flow[:16]
|
option.Flow = option.Flow[:16]
|
||||||
if option.Flow != vless.XRV {
|
switch option.Flow {
|
||||||
|
case vless.XRV:
|
||||||
|
log.Warnln("To use %s, ensure your server is upgrade to Xray-core v1.8.0+", vless.XRV)
|
||||||
|
addons = &vless.Addons{
|
||||||
|
Flow: option.Flow,
|
||||||
|
}
|
||||||
|
case vless.XRO, vless.XRD, vless.XRS:
|
||||||
|
log.Fatalln("Legacy XTLS protocol %s is deprecated and no longer supported", option.Flow)
|
||||||
|
default:
|
||||||
return nil, fmt.Errorf("unsupported xtls flow type: %s", option.Flow)
|
return nil, fmt.Errorf("unsupported xtls flow type: %s", option.Flow)
|
||||||
}
|
}
|
||||||
addons = &vless.Addons{
|
|
||||||
Flow: option.Flow,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch option.PacketEncoding {
|
switch option.PacketEncoding {
|
||||||
@@ -568,30 +556,26 @@ func NewVless(option VlessOption) (*Vless, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
v.echConfig, err = v.option.ECHOpts.Parse()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
switch option.Network {
|
switch option.Network {
|
||||||
case "h2":
|
case "h2":
|
||||||
if len(option.HTTP2Opts.Host) == 0 {
|
if len(option.HTTP2Opts.Host) == 0 {
|
||||||
option.HTTP2Opts.Host = append(option.HTTP2Opts.Host, "www.example.com")
|
option.HTTP2Opts.Host = append(option.HTTP2Opts.Host, "www.example.com")
|
||||||
}
|
}
|
||||||
case "grpc":
|
case "grpc":
|
||||||
dialFn := func(ctx context.Context, network, addr string) (net.Conn, error) {
|
dialFn := func(network, addr string) (net.Conn, error) {
|
||||||
var err error
|
var err error
|
||||||
var cDialer C.Dialer = dialer.NewDialer(v.DialOptions()...)
|
var cDialer C.Dialer = dialer.NewDialer(v.Base.DialOptions()...)
|
||||||
if len(v.option.DialerProxy) > 0 {
|
if len(v.option.DialerProxy) > 0 {
|
||||||
cDialer, err = proxydialer.NewByName(v.option.DialerProxy, cDialer)
|
cDialer, err = proxydialer.NewByName(v.option.DialerProxy, cDialer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c, err := cDialer.DialContext(ctx, "tcp", v.addr)
|
c, err := cDialer.DialContext(context.Background(), "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())
|
||||||
}
|
}
|
||||||
|
N.TCPKeepAlive(c)
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -605,13 +589,10 @@ func NewVless(option VlessOption) (*Vless, error) {
|
|||||||
}
|
}
|
||||||
var tlsConfig *tls.Config
|
var tlsConfig *tls.Config
|
||||||
if option.TLS {
|
if option.TLS {
|
||||||
tlsConfig, err = ca.GetSpecifiedFingerprintTLSConfig(&tls.Config{
|
tlsConfig = ca.GetGlobalTLSConfig(&tls.Config{
|
||||||
InsecureSkipVerify: v.option.SkipCertVerify,
|
InsecureSkipVerify: v.option.SkipCertVerify,
|
||||||
ServerName: v.option.ServerName,
|
ServerName: v.option.ServerName,
|
||||||
}, v.option.Fingerprint)
|
})
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if option.ServerName == "" {
|
if option.ServerName == "" {
|
||||||
host, _, _ := net.SplitHostPort(v.addr)
|
host, _, _ := net.SplitHostPort(v.addr)
|
||||||
tlsConfig.ServerName = host
|
tlsConfig.ServerName = host
|
||||||
@@ -621,7 +602,7 @@ func NewVless(option VlessOption) (*Vless, error) {
|
|||||||
v.gunTLSConfig = tlsConfig
|
v.gunTLSConfig = tlsConfig
|
||||||
v.gunConfig = gunConfig
|
v.gunConfig = gunConfig
|
||||||
|
|
||||||
v.transport = gun.NewHTTP2Client(dialFn, tlsConfig, v.option.ClientFingerprint, v.echConfig, v.realityConfig)
|
v.transport = gun.NewHTTP2Client(dialFn, tlsConfig, v.option.ClientFingerprint, v.realityConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
return v, nil
|
return v, nil
|
||||||
|
|||||||
@@ -11,22 +11,21 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/Dreamacro/clash/common/utils"
|
||||||
"github.com/metacubex/mihomo/component/ca"
|
"github.com/Dreamacro/clash/component/ca"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
"github.com/metacubex/mihomo/component/ech"
|
"github.com/Dreamacro/clash/component/proxydialer"
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
"github.com/Dreamacro/clash/component/resolver"
|
||||||
"github.com/metacubex/mihomo/component/resolver"
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
||||||
tlsC "github.com/metacubex/mihomo/component/tls"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
"github.com/Dreamacro/clash/ntp"
|
||||||
"github.com/metacubex/mihomo/ntp"
|
"github.com/Dreamacro/clash/transport/gun"
|
||||||
"github.com/metacubex/mihomo/transport/gun"
|
clashVMess "github.com/Dreamacro/clash/transport/vmess"
|
||||||
mihomoVMess "github.com/metacubex/mihomo/transport/vmess"
|
|
||||||
|
|
||||||
vmess "github.com/metacubex/sing-vmess"
|
vmess "github.com/metacubex/sing-vmess"
|
||||||
"github.com/metacubex/sing-vmess/packetaddr"
|
"github.com/metacubex/sing-vmess/packetaddr"
|
||||||
M "github.com/metacubex/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrUDPRemoteAddrMismatch = errors.New("udp packet dropped due to mismatched remote address")
|
var ErrUDPRemoteAddrMismatch = errors.New("udp packet dropped due to mismatched remote address")
|
||||||
@@ -42,7 +41,6 @@ type Vmess struct {
|
|||||||
transport *gun.TransportWrap
|
transport *gun.TransportWrap
|
||||||
|
|
||||||
realityConfig *tlsC.RealityConfig
|
realityConfig *tlsC.RealityConfig
|
||||||
echConfig *ech.Config
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type VmessOption struct {
|
type VmessOption struct {
|
||||||
@@ -60,7 +58,6 @@ type VmessOption struct {
|
|||||||
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
||||||
Fingerprint string `proxy:"fingerprint,omitempty"`
|
Fingerprint string `proxy:"fingerprint,omitempty"`
|
||||||
ServerName string `proxy:"servername,omitempty"`
|
ServerName string `proxy:"servername,omitempty"`
|
||||||
ECHOpts ECHOptions `proxy:"ech-opts,omitempty"`
|
|
||||||
RealityOpts RealityOptions `proxy:"reality-opts,omitempty"`
|
RealityOpts RealityOptions `proxy:"reality-opts,omitempty"`
|
||||||
HTTPOpts HTTPOptions `proxy:"http-opts,omitempty"`
|
HTTPOpts HTTPOptions `proxy:"http-opts,omitempty"`
|
||||||
HTTP2Opts HTTP2Options `proxy:"h2-opts,omitempty"`
|
HTTP2Opts HTTP2Options `proxy:"h2-opts,omitempty"`
|
||||||
@@ -90,30 +87,31 @@ type GrpcOptions struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type WSOptions struct {
|
type WSOptions struct {
|
||||||
Path string `proxy:"path,omitempty"`
|
Path string `proxy:"path,omitempty"`
|
||||||
Headers map[string]string `proxy:"headers,omitempty"`
|
Headers map[string]string `proxy:"headers,omitempty"`
|
||||||
MaxEarlyData int `proxy:"max-early-data,omitempty"`
|
MaxEarlyData int `proxy:"max-early-data,omitempty"`
|
||||||
EarlyDataHeaderName string `proxy:"early-data-header-name,omitempty"`
|
EarlyDataHeaderName string `proxy:"early-data-header-name,omitempty"`
|
||||||
V2rayHttpUpgrade bool `proxy:"v2ray-http-upgrade,omitempty"`
|
|
||||||
V2rayHttpUpgradeFastOpen bool `proxy:"v2ray-http-upgrade-fast-open,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// StreamConnContext implements C.ProxyAdapter
|
// StreamConnContext implements C.ProxyAdapter
|
||||||
func (v *Vmess) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ net.Conn, err error) {
|
func (v *Vmess) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if tlsC.HaveGlobalFingerprint() && (len(v.option.ClientFingerprint) == 0) {
|
||||||
|
v.option.ClientFingerprint = tlsC.GetGlobalFingerprint()
|
||||||
|
}
|
||||||
|
|
||||||
switch v.option.Network {
|
switch v.option.Network {
|
||||||
case "ws":
|
case "ws":
|
||||||
host, port, _ := net.SplitHostPort(v.addr)
|
host, port, _ := net.SplitHostPort(v.addr)
|
||||||
wsOpts := &mihomoVMess.WebsocketConfig{
|
wsOpts := &clashVMess.WebsocketConfig{
|
||||||
Host: host,
|
Host: host,
|
||||||
Port: port,
|
Port: port,
|
||||||
Path: v.option.WSOpts.Path,
|
Path: v.option.WSOpts.Path,
|
||||||
MaxEarlyData: v.option.WSOpts.MaxEarlyData,
|
MaxEarlyData: v.option.WSOpts.MaxEarlyData,
|
||||||
EarlyDataHeaderName: v.option.WSOpts.EarlyDataHeaderName,
|
EarlyDataHeaderName: v.option.WSOpts.EarlyDataHeaderName,
|
||||||
V2rayHttpUpgrade: v.option.WSOpts.V2rayHttpUpgrade,
|
ClientFingerprint: v.option.ClientFingerprint,
|
||||||
V2rayHttpUpgradeFastOpen: v.option.WSOpts.V2rayHttpUpgradeFastOpen,
|
Headers: http.Header{},
|
||||||
ClientFingerprint: v.option.ClientFingerprint,
|
|
||||||
ECHConfig: v.echConfig,
|
|
||||||
Headers: http.Header{},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(v.option.WSOpts.Headers) != 0 {
|
if len(v.option.WSOpts.Headers) != 0 {
|
||||||
@@ -141,16 +139,15 @@ func (v *Vmess) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
wsOpts.TLSConfig.ServerName = host
|
wsOpts.TLSConfig.ServerName = host
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c, err = mihomoVMess.StreamWebsocketConn(ctx, c, wsOpts)
|
c, err = clashVMess.StreamWebsocketConn(ctx, c, wsOpts)
|
||||||
case "http":
|
case "http":
|
||||||
// readability first, so just copy default TLS logic
|
// readability first, so just copy default TLS logic
|
||||||
if v.option.TLS {
|
if v.option.TLS {
|
||||||
host, _, _ := net.SplitHostPort(v.addr)
|
host, _, _ := net.SplitHostPort(v.addr)
|
||||||
tlsOpts := &mihomoVMess.TLSConfig{
|
tlsOpts := &clashVMess.TLSConfig{
|
||||||
Host: host,
|
Host: host,
|
||||||
SkipCertVerify: v.option.SkipCertVerify,
|
SkipCertVerify: v.option.SkipCertVerify,
|
||||||
ClientFingerprint: v.option.ClientFingerprint,
|
ClientFingerprint: v.option.ClientFingerprint,
|
||||||
ECH: v.echConfig,
|
|
||||||
Reality: v.realityConfig,
|
Reality: v.realityConfig,
|
||||||
NextProtos: v.option.ALPN,
|
NextProtos: v.option.ALPN,
|
||||||
}
|
}
|
||||||
@@ -158,27 +155,26 @@ func (v *Vmess) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
if v.option.ServerName != "" {
|
if v.option.ServerName != "" {
|
||||||
tlsOpts.Host = v.option.ServerName
|
tlsOpts.Host = v.option.ServerName
|
||||||
}
|
}
|
||||||
c, err = mihomoVMess.StreamTLSConn(ctx, c, tlsOpts)
|
c, err = clashVMess.StreamTLSConn(ctx, c, tlsOpts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
host, _, _ := net.SplitHostPort(v.addr)
|
host, _, _ := net.SplitHostPort(v.addr)
|
||||||
httpOpts := &mihomoVMess.HTTPConfig{
|
httpOpts := &clashVMess.HTTPConfig{
|
||||||
Host: host,
|
Host: host,
|
||||||
Method: v.option.HTTPOpts.Method,
|
Method: v.option.HTTPOpts.Method,
|
||||||
Path: v.option.HTTPOpts.Path,
|
Path: v.option.HTTPOpts.Path,
|
||||||
Headers: v.option.HTTPOpts.Headers,
|
Headers: v.option.HTTPOpts.Headers,
|
||||||
}
|
}
|
||||||
|
|
||||||
c = mihomoVMess.StreamHTTPConn(c, httpOpts)
|
c = clashVMess.StreamHTTPConn(c, httpOpts)
|
||||||
case "h2":
|
case "h2":
|
||||||
host, _, _ := net.SplitHostPort(v.addr)
|
host, _, _ := net.SplitHostPort(v.addr)
|
||||||
tlsOpts := mihomoVMess.TLSConfig{
|
tlsOpts := clashVMess.TLSConfig{
|
||||||
Host: host,
|
Host: host,
|
||||||
SkipCertVerify: v.option.SkipCertVerify,
|
SkipCertVerify: v.option.SkipCertVerify,
|
||||||
FingerPrint: v.option.Fingerprint,
|
|
||||||
NextProtos: []string{"h2"},
|
NextProtos: []string{"h2"},
|
||||||
ClientFingerprint: v.option.ClientFingerprint,
|
ClientFingerprint: v.option.ClientFingerprint,
|
||||||
Reality: v.realityConfig,
|
Reality: v.realityConfig,
|
||||||
@@ -188,29 +184,27 @@ func (v *Vmess) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
tlsOpts.Host = v.option.ServerName
|
tlsOpts.Host = v.option.ServerName
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err = mihomoVMess.StreamTLSConn(ctx, c, &tlsOpts)
|
c, err = clashVMess.StreamTLSConn(ctx, c, &tlsOpts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
h2Opts := &mihomoVMess.H2Config{
|
h2Opts := &clashVMess.H2Config{
|
||||||
Hosts: v.option.HTTP2Opts.Host,
|
Hosts: v.option.HTTP2Opts.Host,
|
||||||
Path: v.option.HTTP2Opts.Path,
|
Path: v.option.HTTP2Opts.Path,
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err = mihomoVMess.StreamH2Conn(ctx, c, h2Opts)
|
c, err = clashVMess.StreamH2Conn(c, h2Opts)
|
||||||
case "grpc":
|
case "grpc":
|
||||||
c, err = gun.StreamGunWithConn(c, v.gunTLSConfig, v.gunConfig, v.echConfig, v.realityConfig)
|
c, err = gun.StreamGunWithConn(c, v.gunTLSConfig, v.gunConfig, v.realityConfig)
|
||||||
default:
|
default:
|
||||||
// handle TLS
|
// handle TLS
|
||||||
if v.option.TLS {
|
if v.option.TLS {
|
||||||
host, _, _ := net.SplitHostPort(v.addr)
|
host, _, _ := net.SplitHostPort(v.addr)
|
||||||
tlsOpts := &mihomoVMess.TLSConfig{
|
tlsOpts := &clashVMess.TLSConfig{
|
||||||
Host: host,
|
Host: host,
|
||||||
SkipCertVerify: v.option.SkipCertVerify,
|
SkipCertVerify: v.option.SkipCertVerify,
|
||||||
FingerPrint: v.option.Fingerprint,
|
|
||||||
ClientFingerprint: v.option.ClientFingerprint,
|
ClientFingerprint: v.option.ClientFingerprint,
|
||||||
ECH: v.echConfig,
|
|
||||||
Reality: v.realityConfig,
|
Reality: v.realityConfig,
|
||||||
NextProtos: v.option.ALPN,
|
NextProtos: v.option.ALPN,
|
||||||
}
|
}
|
||||||
@@ -219,31 +213,24 @@ func (v *Vmess) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
tlsOpts.Host = v.option.ServerName
|
tlsOpts.Host = v.option.ServerName
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err = mihomoVMess.StreamTLSConn(ctx, c, tlsOpts)
|
c, err = clashVMess.StreamTLSConn(ctx, c, tlsOpts)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return v.streamConnContext(ctx, c, metadata)
|
return v.streamConn(c, metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Vmess) streamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (conn net.Conn, err error) {
|
func (v *Vmess) streamConn(c net.Conn, metadata *C.Metadata) (conn net.Conn, err error) {
|
||||||
useEarly := N.NeedHandshake(c)
|
|
||||||
if !useEarly {
|
|
||||||
if ctx.Done() != nil {
|
|
||||||
done := N.SetupContextForConn(ctx, c)
|
|
||||||
defer done(&err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if metadata.NetWork == C.UDP {
|
if metadata.NetWork == C.UDP {
|
||||||
if v.option.XUDP {
|
if v.option.XUDP {
|
||||||
var globalID [8]byte
|
var globalID [8]byte
|
||||||
if metadata.SourceValid() {
|
if metadata.SourceValid() {
|
||||||
globalID = utils.GlobalID(metadata.SourceAddress())
|
globalID = utils.GlobalID(metadata.SourceAddress())
|
||||||
}
|
}
|
||||||
if useEarly {
|
if N.NeedHandshake(c) {
|
||||||
conn = v.client.DialEarlyXUDPPacketConn(c,
|
conn = v.client.DialEarlyXUDPPacketConn(c,
|
||||||
globalID,
|
globalID,
|
||||||
M.SocksaddrFromNet(metadata.UDPAddr()))
|
M.SocksaddrFromNet(metadata.UDPAddr()))
|
||||||
@@ -253,7 +240,7 @@ func (v *Vmess) streamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
M.SocksaddrFromNet(metadata.UDPAddr()))
|
M.SocksaddrFromNet(metadata.UDPAddr()))
|
||||||
}
|
}
|
||||||
} else if v.option.PacketAddr {
|
} else if v.option.PacketAddr {
|
||||||
if useEarly {
|
if N.NeedHandshake(c) {
|
||||||
conn = v.client.DialEarlyPacketConn(c,
|
conn = v.client.DialEarlyPacketConn(c,
|
||||||
M.ParseSocksaddrHostPort(packetaddr.SeqPacketMagicAddress, 443))
|
M.ParseSocksaddrHostPort(packetaddr.SeqPacketMagicAddress, 443))
|
||||||
} else {
|
} else {
|
||||||
@@ -262,7 +249,7 @@ func (v *Vmess) streamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
}
|
}
|
||||||
conn = packetaddr.NewBindConn(conn)
|
conn = packetaddr.NewBindConn(conn)
|
||||||
} else {
|
} else {
|
||||||
if useEarly {
|
if N.NeedHandshake(c) {
|
||||||
conn = v.client.DialEarlyPacketConn(c,
|
conn = v.client.DialEarlyPacketConn(c,
|
||||||
M.SocksaddrFromNet(metadata.UDPAddr()))
|
M.SocksaddrFromNet(metadata.UDPAddr()))
|
||||||
} else {
|
} else {
|
||||||
@@ -271,7 +258,7 @@ func (v *Vmess) streamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if useEarly {
|
if N.NeedHandshake(c) {
|
||||||
conn = v.client.DialEarlyConn(c,
|
conn = v.client.DialEarlyConn(c,
|
||||||
M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort))
|
M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort))
|
||||||
} else {
|
} else {
|
||||||
@@ -286,11 +273,10 @@ func (v *Vmess) streamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (v *Vmess) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
func (v *Vmess) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
||||||
var c net.Conn
|
|
||||||
// gun transport
|
// gun transport
|
||||||
if v.transport != nil {
|
if v.transport != nil && len(opts) == 0 {
|
||||||
c, err = gun.StreamGunWithTransport(v.transport, v.gunConfig)
|
c, err := gun.StreamGunWithTransport(v.transport, v.gunConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -298,14 +284,14 @@ func (v *Vmess) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn
|
|||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
}(c)
|
}(c)
|
||||||
|
|
||||||
c, err = v.streamConnContext(ctx, c, metadata)
|
c, err = v.client.DialConn(c, M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort))
|
||||||
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.DialOptions()...), metadata)
|
return v.DialContextWithDialer(ctx, dialer.NewDialer(v.Base.DialOptions(opts...)...), metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialContextWithDialer implements C.ProxyAdapter
|
// DialContextWithDialer implements C.ProxyAdapter
|
||||||
@@ -320,6 +306,7 @@ func (v *Vmess) DialContextWithDialer(ctx context.Context, dialer C.Dialer, meta
|
|||||||
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())
|
||||||
}
|
}
|
||||||
|
N.TCPKeepAlive(c)
|
||||||
defer func(c net.Conn) {
|
defer func(c net.Conn) {
|
||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
}(c)
|
}(c)
|
||||||
@@ -329,7 +316,7 @@ func (v *Vmess) DialContextWithDialer(ctx context.Context, dialer C.Dialer, meta
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (v *Vmess) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ 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 need a net.UDPAddr
|
// vmess use stream-oriented udp with a special address, so we need a net.UDPAddr
|
||||||
if !metadata.Resolved() {
|
if !metadata.Resolved() {
|
||||||
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
||||||
@@ -340,7 +327,7 @@ func (v *Vmess) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (
|
|||||||
}
|
}
|
||||||
var c net.Conn
|
var c net.Conn
|
||||||
// gun transport
|
// gun transport
|
||||||
if v.transport != nil {
|
if v.transport != nil && len(opts) == 0 {
|
||||||
c, err = gun.StreamGunWithTransport(v.transport, v.gunConfig)
|
c, err = gun.StreamGunWithTransport(v.transport, v.gunConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -349,13 +336,13 @@ func (v *Vmess) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (
|
|||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
}(c)
|
}(c)
|
||||||
|
|
||||||
c, err = v.streamConnContext(ctx, c, metadata)
|
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(ctx, c, metadata)
|
return v.ListenPacketOnStreamConn(ctx, c, metadata)
|
||||||
}
|
}
|
||||||
return v.ListenPacketWithDialer(ctx, dialer.NewDialer(v.DialOptions()...), metadata)
|
return v.ListenPacketWithDialer(ctx, dialer.NewDialer(v.Base.DialOptions(opts...)...), metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketWithDialer implements C.ProxyAdapter
|
// ListenPacketWithDialer implements C.ProxyAdapter
|
||||||
@@ -380,6 +367,7 @@ func (v *Vmess) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, met
|
|||||||
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())
|
||||||
}
|
}
|
||||||
|
N.TCPKeepAlive(c)
|
||||||
defer func(c net.Conn) {
|
defer func(c net.Conn) {
|
||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
}(c)
|
}(c)
|
||||||
@@ -396,21 +384,6 @@ func (v *Vmess) SupportWithDialer() C.NetWork {
|
|||||||
return C.ALLNet
|
return C.ALLNet
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProxyInfo implements C.ProxyAdapter
|
|
||||||
func (v *Vmess) ProxyInfo() C.ProxyInfo {
|
|
||||||
info := v.Base.ProxyInfo()
|
|
||||||
info.DialerProxy = v.option.DialerProxy
|
|
||||||
return info
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close implements C.ProxyAdapter
|
|
||||||
func (v *Vmess) Close() error {
|
|
||||||
if v.transport != nil {
|
|
||||||
return v.transport.Close()
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
// ListenPacketOnStreamConn implements C.ProxyAdapter
|
||||||
func (v *Vmess) ListenPacketOnStreamConn(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
func (v *Vmess) ListenPacketOnStreamConn(ctx context.Context, c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
// vmess use stream-oriented udp with a special address, so we need a net.UDPAddr
|
// vmess use stream-oriented udp with a special address, so we need a net.UDPAddr
|
||||||
@@ -475,35 +448,26 @@ func NewVmess(option VmessOption) (*Vmess, error) {
|
|||||||
option: &option,
|
option: &option,
|
||||||
}
|
}
|
||||||
|
|
||||||
v.realityConfig, err = v.option.RealityOpts.Parse()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
v.echConfig, err = v.option.ECHOpts.Parse()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
switch option.Network {
|
switch option.Network {
|
||||||
case "h2":
|
case "h2":
|
||||||
if len(option.HTTP2Opts.Host) == 0 {
|
if len(option.HTTP2Opts.Host) == 0 {
|
||||||
option.HTTP2Opts.Host = append(option.HTTP2Opts.Host, "www.example.com")
|
option.HTTP2Opts.Host = append(option.HTTP2Opts.Host, "www.example.com")
|
||||||
}
|
}
|
||||||
case "grpc":
|
case "grpc":
|
||||||
dialFn := func(ctx context.Context, network, addr string) (net.Conn, error) {
|
dialFn := func(network, addr string) (net.Conn, error) {
|
||||||
var err error
|
var err error
|
||||||
var cDialer C.Dialer = dialer.NewDialer(v.DialOptions()...)
|
var cDialer C.Dialer = dialer.NewDialer(v.Base.DialOptions()...)
|
||||||
if len(v.option.DialerProxy) > 0 {
|
if len(v.option.DialerProxy) > 0 {
|
||||||
cDialer, err = proxydialer.NewByName(v.option.DialerProxy, cDialer)
|
cDialer, err = proxydialer.NewByName(v.option.DialerProxy, cDialer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c, err := cDialer.DialContext(ctx, "tcp", v.addr)
|
c, err := cDialer.DialContext(context.Background(), "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())
|
||||||
}
|
}
|
||||||
|
N.TCPKeepAlive(c)
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -517,13 +481,10 @@ func NewVmess(option VmessOption) (*Vmess, error) {
|
|||||||
}
|
}
|
||||||
var tlsConfig *tls.Config
|
var tlsConfig *tls.Config
|
||||||
if option.TLS {
|
if option.TLS {
|
||||||
tlsConfig, err = ca.GetSpecifiedFingerprintTLSConfig(&tls.Config{
|
tlsConfig = ca.GetGlobalTLSConfig(&tls.Config{
|
||||||
InsecureSkipVerify: v.option.SkipCertVerify,
|
InsecureSkipVerify: v.option.SkipCertVerify,
|
||||||
ServerName: v.option.ServerName,
|
ServerName: v.option.ServerName,
|
||||||
}, v.option.Fingerprint)
|
})
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if option.ServerName == "" {
|
if option.ServerName == "" {
|
||||||
host, _, _ := net.SplitHostPort(v.addr)
|
host, _, _ := net.SplitHostPort(v.addr)
|
||||||
tlsConfig.ServerName = host
|
tlsConfig.ServerName = host
|
||||||
@@ -533,7 +494,12 @@ func NewVmess(option VmessOption) (*Vmess, error) {
|
|||||||
v.gunTLSConfig = tlsConfig
|
v.gunTLSConfig = tlsConfig
|
||||||
v.gunConfig = gunConfig
|
v.gunConfig = gunConfig
|
||||||
|
|
||||||
v.transport = gun.NewHTTP2Client(dialFn, tlsConfig, v.option.ClientFingerprint, v.echConfig, v.realityConfig)
|
v.transport = gun.NewHTTP2Client(dialFn, tlsConfig, v.option.ClientFingerprint, v.realityConfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
v.realityConfig, err = v.option.RealityOpts.Parse()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return v, nil
|
return v, nil
|
||||||
|
|||||||
@@ -8,97 +8,67 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/atomic"
|
CN "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
"github.com/Dreamacro/clash/component/proxydialer"
|
||||||
"github.com/metacubex/mihomo/component/resolver"
|
"github.com/Dreamacro/clash/component/resolver"
|
||||||
"github.com/metacubex/mihomo/component/slowdown"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
"github.com/Dreamacro/clash/dns"
|
||||||
"github.com/metacubex/mihomo/dns"
|
"github.com/Dreamacro/clash/log"
|
||||||
"github.com/metacubex/mihomo/log"
|
|
||||||
|
|
||||||
amnezia "github.com/metacubex/amneziawg-go/device"
|
|
||||||
wireguard "github.com/metacubex/sing-wireguard"
|
wireguard "github.com/metacubex/sing-wireguard"
|
||||||
"github.com/metacubex/wireguard-go/device"
|
|
||||||
|
|
||||||
"github.com/metacubex/sing/common/debug"
|
"github.com/sagernet/sing/common"
|
||||||
E "github.com/metacubex/sing/common/exceptions"
|
"github.com/sagernet/sing/common/debug"
|
||||||
M "github.com/metacubex/sing/common/metadata"
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
|
"github.com/sagernet/wireguard-go/device"
|
||||||
)
|
)
|
||||||
|
|
||||||
type wireguardGoDevice interface {
|
|
||||||
Close()
|
|
||||||
IpcSet(uapiConf string) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type WireGuard struct {
|
type WireGuard struct {
|
||||||
*Base
|
*Base
|
||||||
bind *wireguard.ClientBind
|
bind *wireguard.ClientBind
|
||||||
device wireguardGoDevice
|
device *device.Device
|
||||||
tunDevice wireguard.Device
|
tunDevice wireguard.Device
|
||||||
dialer proxydialer.SingDialer
|
dialer proxydialer.SingDialer
|
||||||
resolver resolver.Resolver
|
startOnce sync.Once
|
||||||
|
startErr error
|
||||||
initOk atomic.Bool
|
resolver *dns.Resolver
|
||||||
initMutex sync.Mutex
|
refP *refProxyAdapter
|
||||||
initErr error
|
|
||||||
option WireGuardOption
|
|
||||||
connectAddr M.Socksaddr
|
|
||||||
localPrefixes []netip.Prefix
|
|
||||||
|
|
||||||
serverAddrMap map[M.Socksaddr]netip.AddrPort
|
|
||||||
serverAddrTime atomic.TypedValue[time.Time]
|
|
||||||
serverAddrMutex sync.Mutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type WireGuardOption struct {
|
type WireGuardOption struct {
|
||||||
BasicOption
|
BasicOption
|
||||||
WireGuardPeerOption
|
WireGuardPeerOption
|
||||||
Name string `proxy:"name"`
|
Name string `proxy:"name"`
|
||||||
Ip string `proxy:"ip,omitempty"`
|
|
||||||
Ipv6 string `proxy:"ipv6,omitempty"`
|
|
||||||
PrivateKey string `proxy:"private-key"`
|
PrivateKey string `proxy:"private-key"`
|
||||||
Workers int `proxy:"workers,omitempty"`
|
Workers int `proxy:"workers,omitempty"`
|
||||||
MTU int `proxy:"mtu,omitempty"`
|
MTU int `proxy:"mtu,omitempty"`
|
||||||
UDP bool `proxy:"udp,omitempty"`
|
UDP bool `proxy:"udp,omitempty"`
|
||||||
PersistentKeepalive int `proxy:"persistent-keepalive,omitempty"`
|
PersistentKeepalive int `proxy:"persistent-keepalive,omitempty"`
|
||||||
|
|
||||||
AmneziaWGOption *AmneziaWGOption `proxy:"amnezia-wg-option,omitempty"`
|
|
||||||
|
|
||||||
Peers []WireGuardPeerOption `proxy:"peers,omitempty"`
|
Peers []WireGuardPeerOption `proxy:"peers,omitempty"`
|
||||||
|
|
||||||
RemoteDnsResolve bool `proxy:"remote-dns-resolve,omitempty"`
|
RemoteDnsResolve bool `proxy:"remote-dns-resolve,omitempty"`
|
||||||
Dns []string `proxy:"dns,omitempty"`
|
Dns []string `proxy:"dns,omitempty"`
|
||||||
|
|
||||||
RefreshServerIPInterval int `proxy:"refresh-server-ip-interval,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type WireGuardPeerOption struct {
|
type WireGuardPeerOption struct {
|
||||||
Server string `proxy:"server"`
|
Server string `proxy:"server"`
|
||||||
Port int `proxy:"port"`
|
Port int `proxy:"port"`
|
||||||
|
Ip string `proxy:"ip,omitempty"`
|
||||||
|
Ipv6 string `proxy:"ipv6,omitempty"`
|
||||||
PublicKey string `proxy:"public-key,omitempty"`
|
PublicKey string `proxy:"public-key,omitempty"`
|
||||||
PreSharedKey string `proxy:"pre-shared-key,omitempty"`
|
PreSharedKey string `proxy:"pre-shared-key,omitempty"`
|
||||||
Reserved []uint8 `proxy:"reserved,omitempty"`
|
Reserved []uint8 `proxy:"reserved,omitempty"`
|
||||||
AllowedIPs []string `proxy:"allowed-ips,omitempty"`
|
AllowedIPs []string `proxy:"allowed-ips,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AmneziaWGOption struct {
|
|
||||||
JC int `proxy:"jc"`
|
|
||||||
JMin int `proxy:"jmin"`
|
|
||||||
JMax int `proxy:"jmax"`
|
|
||||||
S1 int `proxy:"s1"`
|
|
||||||
S2 int `proxy:"s2"`
|
|
||||||
H1 uint32 `proxy:"h1"`
|
|
||||||
H2 uint32 `proxy:"h2"`
|
|
||||||
H3 uint32 `proxy:"h3"`
|
|
||||||
H4 uint32 `proxy:"h4"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type wgSingErrorHandler struct {
|
type wgSingErrorHandler struct {
|
||||||
name string
|
name string
|
||||||
}
|
}
|
||||||
@@ -127,7 +97,7 @@ func (option WireGuardPeerOption) Addr() M.Socksaddr {
|
|||||||
return M.ParseSocksaddrHostPort(option.Server, uint16(option.Port))
|
return M.ParseSocksaddrHostPort(option.Server, uint16(option.Port))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (option WireGuardOption) Prefixes() ([]netip.Prefix, error) {
|
func (option WireGuardPeerOption) Prefixes() ([]netip.Prefix, error) {
|
||||||
localPrefixes := make([]netip.Prefix, 0, 2)
|
localPrefixes := make([]netip.Prefix, 0, 2)
|
||||||
if len(option.Ip) > 0 {
|
if len(option.Ip) > 0 {
|
||||||
if !strings.Contains(option.Ip, "/") {
|
if !strings.Contains(option.Ip, "/") {
|
||||||
@@ -166,9 +136,9 @@ func NewWireGuard(option WireGuardOption) (*WireGuard, error) {
|
|||||||
rmark: option.RoutingMark,
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: C.NewDNSPrefer(option.IPVersion),
|
||||||
},
|
},
|
||||||
|
dialer: proxydialer.NewByNameSingDialer(option.DialerProxy, dialer.NewDialer()),
|
||||||
}
|
}
|
||||||
singDialer := proxydialer.NewSlowDownSingDialer(proxydialer.NewByNameSingDialer(option.DialerProxy, dialer.NewDialer(outbound.DialOptions()...)), slowdown.New())
|
runtime.SetFinalizer(outbound, closeWireGuard)
|
||||||
outbound.dialer = singDialer
|
|
||||||
|
|
||||||
var reserved [3]uint8
|
var reserved [3]uint8
|
||||||
if len(option.Reserved) > 0 {
|
if len(option.Reserved) > 0 {
|
||||||
@@ -178,116 +148,162 @@ func NewWireGuard(option WireGuardOption) (*WireGuard, error) {
|
|||||||
copy(reserved[:], option.Reserved)
|
copy(reserved[:], option.Reserved)
|
||||||
}
|
}
|
||||||
var isConnect bool
|
var isConnect bool
|
||||||
|
var connectAddr M.Socksaddr
|
||||||
if len(option.Peers) < 2 {
|
if len(option.Peers) < 2 {
|
||||||
isConnect = true
|
isConnect = true
|
||||||
if len(option.Peers) == 1 {
|
if len(option.Peers) == 1 {
|
||||||
outbound.connectAddr = option.Peers[0].Addr()
|
connectAddr = option.Peers[0].Addr()
|
||||||
} else {
|
} else {
|
||||||
outbound.connectAddr = option.Addr()
|
connectAddr = option.Addr()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
outbound.bind = wireguard.NewClientBind(context.Background(), wgSingErrorHandler{outbound.Name()}, outbound.dialer, isConnect, outbound.connectAddr.AddrPort(), reserved)
|
outbound.bind = wireguard.NewClientBind(context.Background(), wgSingErrorHandler{outbound.Name()}, outbound.dialer, isConnect, connectAddr, reserved)
|
||||||
|
|
||||||
var err error
|
var localPrefixes []netip.Prefix
|
||||||
outbound.localPrefixes, err = option.Prefixes()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
|
var privateKey string
|
||||||
{
|
{
|
||||||
bytes, err := base64.StdEncoding.DecodeString(option.PrivateKey)
|
bytes, err := base64.StdEncoding.DecodeString(option.PrivateKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, E.Cause(err, "decode private key")
|
return nil, E.Cause(err, "decode private key")
|
||||||
}
|
}
|
||||||
option.PrivateKey = hex.EncodeToString(bytes)
|
privateKey = hex.EncodeToString(bytes)
|
||||||
}
|
}
|
||||||
|
ipcConf := "private_key=" + privateKey
|
||||||
if len(option.Peers) > 0 {
|
if peersLen := len(option.Peers); peersLen > 0 {
|
||||||
for i := range option.Peers {
|
localPrefixes = make([]netip.Prefix, 0, peersLen*2)
|
||||||
peer := &option.Peers[i] // we need modify option here
|
for i, peer := range option.Peers {
|
||||||
bytes, err := base64.StdEncoding.DecodeString(peer.PublicKey)
|
var peerPublicKey, preSharedKey string
|
||||||
if err != nil {
|
{
|
||||||
return nil, E.Cause(err, "decode public key for peer ", i)
|
bytes, err := base64.StdEncoding.DecodeString(peer.PublicKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, E.Cause(err, "decode public key for peer ", i)
|
||||||
|
}
|
||||||
|
peerPublicKey = hex.EncodeToString(bytes)
|
||||||
}
|
}
|
||||||
peer.PublicKey = hex.EncodeToString(bytes)
|
|
||||||
|
|
||||||
if peer.PreSharedKey != "" {
|
if peer.PreSharedKey != "" {
|
||||||
bytes, err := base64.StdEncoding.DecodeString(peer.PreSharedKey)
|
bytes, err := base64.StdEncoding.DecodeString(peer.PreSharedKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, E.Cause(err, "decode pre shared key for peer ", i)
|
return nil, E.Cause(err, "decode pre shared key for peer ", i)
|
||||||
}
|
}
|
||||||
peer.PreSharedKey = hex.EncodeToString(bytes)
|
preSharedKey = hex.EncodeToString(bytes)
|
||||||
|
}
|
||||||
|
destination := peer.Addr()
|
||||||
|
ipcConf += "\npublic_key=" + peerPublicKey
|
||||||
|
ipcConf += "\nendpoint=" + destination.String()
|
||||||
|
if preSharedKey != "" {
|
||||||
|
ipcConf += "\npreshared_key=" + preSharedKey
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(peer.AllowedIPs) == 0 {
|
if len(peer.AllowedIPs) == 0 {
|
||||||
return nil, E.New("missing allowed_ips for peer ", i)
|
return nil, E.New("missing allowed_ips for peer ", i)
|
||||||
}
|
}
|
||||||
|
for _, allowedIP := range peer.AllowedIPs {
|
||||||
|
ipcConf += "\nallowed_ip=" + allowedIP
|
||||||
|
}
|
||||||
if len(peer.Reserved) > 0 {
|
if len(peer.Reserved) > 0 {
|
||||||
if len(peer.Reserved) != 3 {
|
if len(peer.Reserved) != 3 {
|
||||||
return nil, E.New("invalid reserved value for peer ", i, ", required 3 bytes, got ", len(peer.Reserved))
|
return nil, E.New("invalid reserved value for peer ", i, ", required 3 bytes, got ", len(peer.Reserved))
|
||||||
}
|
}
|
||||||
|
copy(reserved[:], option.Reserved)
|
||||||
|
outbound.bind.SetReservedForEndpoint(destination, reserved)
|
||||||
}
|
}
|
||||||
|
prefixes, err := peer.Prefixes()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
localPrefixes = append(localPrefixes, prefixes...)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
var peerPublicKey, preSharedKey string
|
||||||
{
|
{
|
||||||
bytes, err := base64.StdEncoding.DecodeString(option.PublicKey)
|
bytes, err := base64.StdEncoding.DecodeString(option.PublicKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, E.Cause(err, "decode peer public key")
|
return nil, E.Cause(err, "decode peer public key")
|
||||||
}
|
}
|
||||||
option.PublicKey = hex.EncodeToString(bytes)
|
peerPublicKey = hex.EncodeToString(bytes)
|
||||||
}
|
}
|
||||||
if option.PreSharedKey != "" {
|
if option.PreSharedKey != "" {
|
||||||
bytes, err := base64.StdEncoding.DecodeString(option.PreSharedKey)
|
bytes, err := base64.StdEncoding.DecodeString(option.PreSharedKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, E.Cause(err, "decode pre shared key")
|
return nil, E.Cause(err, "decode pre shared key")
|
||||||
}
|
}
|
||||||
option.PreSharedKey = hex.EncodeToString(bytes)
|
preSharedKey = hex.EncodeToString(bytes)
|
||||||
|
}
|
||||||
|
ipcConf += "\npublic_key=" + peerPublicKey
|
||||||
|
ipcConf += "\nendpoint=" + connectAddr.String()
|
||||||
|
if preSharedKey != "" {
|
||||||
|
ipcConf += "\npreshared_key=" + preSharedKey
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
localPrefixes, err = option.Prefixes()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
outbound.option = option
|
|
||||||
|
|
||||||
|
if option.PersistentKeepalive != 0 {
|
||||||
|
ipcConf += fmt.Sprintf("\npersistent_keepalive_interval=%d", option.PersistentKeepalive)
|
||||||
|
}
|
||||||
mtu := option.MTU
|
mtu := option.MTU
|
||||||
if mtu == 0 {
|
if mtu == 0 {
|
||||||
mtu = 1408
|
mtu = 1408
|
||||||
}
|
}
|
||||||
if len(outbound.localPrefixes) == 0 {
|
if len(localPrefixes) == 0 {
|
||||||
return nil, E.New("missing local address")
|
return nil, E.New("missing local address")
|
||||||
}
|
}
|
||||||
outbound.tunDevice, err = wireguard.NewStackDevice(outbound.localPrefixes, uint32(mtu))
|
var err error
|
||||||
|
outbound.tunDevice, err = wireguard.NewStackDevice(localPrefixes, uint32(mtu))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, E.Cause(err, "create WireGuard device")
|
return nil, E.Cause(err, "create WireGuard device")
|
||||||
}
|
}
|
||||||
logger := &device.Logger{
|
outbound.device = device.NewDevice(context.Background(), outbound.tunDevice, outbound.bind, &device.Logger{
|
||||||
Verbosef: func(format string, args ...interface{}) {
|
Verbosef: func(format string, args ...interface{}) {
|
||||||
log.SingLogger.Debug(fmt.Sprintf("[WG](%s) %s", option.Name, fmt.Sprintf(format, args...)))
|
log.SingLogger.Debug(fmt.Sprintf("[WG](%s) %s", option.Name, fmt.Sprintf(format, args...)))
|
||||||
},
|
},
|
||||||
Errorf: func(format string, args ...interface{}) {
|
Errorf: func(format string, args ...interface{}) {
|
||||||
log.SingLogger.Error(fmt.Sprintf("[WG](%s) %s", option.Name, fmt.Sprintf(format, args...)))
|
log.SingLogger.Error(fmt.Sprintf("[WG](%s) %s", option.Name, fmt.Sprintf(format, args...)))
|
||||||
},
|
},
|
||||||
|
}, option.Workers)
|
||||||
|
if debug.Enabled {
|
||||||
|
log.SingLogger.Trace(fmt.Sprintf("[WG](%s) created wireguard ipc conf: \n %s", option.Name, ipcConf))
|
||||||
}
|
}
|
||||||
if option.AmneziaWGOption != nil {
|
err = outbound.device.IpcSet(ipcConf)
|
||||||
outbound.bind.SetParseReserved(false) // AmneziaWG don't need parse reserved
|
if err != nil {
|
||||||
outbound.device = amnezia.NewDevice(outbound.tunDevice, outbound.bind, logger, option.Workers)
|
return nil, E.Cause(err, "setup wireguard")
|
||||||
} else {
|
|
||||||
outbound.device = device.NewDevice(outbound.tunDevice, outbound.bind, logger, option.Workers)
|
|
||||||
}
|
}
|
||||||
|
//err = outbound.tunDevice.Start()
|
||||||
|
|
||||||
var has6 bool
|
var has6 bool
|
||||||
for _, address := range outbound.localPrefixes {
|
for _, address := range localPrefixes {
|
||||||
if !address.Addr().Unmap().Is4() {
|
if !address.Addr().Unmap().Is4() {
|
||||||
has6 = true
|
has6 = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
refP := &refProxyAdapter{}
|
||||||
|
outbound.refP = refP
|
||||||
if option.RemoteDnsResolve && len(option.Dns) > 0 {
|
if option.RemoteDnsResolve && len(option.Dns) > 0 {
|
||||||
nss, err := dns.ParseNameServer(option.Dns)
|
nss, err := dns.ParseNameServer(option.Dns)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for i := range nss {
|
for i := range nss {
|
||||||
nss[i].ProxyAdapter = outbound
|
nss[i].ProxyAdapter = refP
|
||||||
}
|
}
|
||||||
outbound.resolver = dns.NewResolver(dns.Config{
|
outbound.resolver = dns.NewResolver(dns.Config{
|
||||||
Main: nss,
|
Main: nss,
|
||||||
@@ -298,208 +314,30 @@ func NewWireGuard(option WireGuardOption) (*WireGuard, error) {
|
|||||||
return outbound, nil
|
return outbound, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WireGuard) resolve(ctx context.Context, address M.Socksaddr) (netip.AddrPort, error) {
|
func closeWireGuard(w *WireGuard) {
|
||||||
if address.Addr.IsValid() {
|
|
||||||
return address.AddrPort(), nil
|
|
||||||
}
|
|
||||||
udpAddr, err := resolveUDPAddr(ctx, "udp", address.String(), w.prefer)
|
|
||||||
if err != nil {
|
|
||||||
return netip.AddrPort{}, err
|
|
||||||
}
|
|
||||||
// net.ResolveUDPAddr maybe return 4in6 address, so unmap at here
|
|
||||||
addrPort := udpAddr.AddrPort()
|
|
||||||
return netip.AddrPortFrom(addrPort.Addr().Unmap(), addrPort.Port()), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *WireGuard) init(ctx context.Context) error {
|
|
||||||
err := w.init0(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
w.updateServerAddr(ctx)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *WireGuard) init0(ctx context.Context) error {
|
|
||||||
if w.initOk.Load() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
w.initMutex.Lock()
|
|
||||||
defer w.initMutex.Unlock()
|
|
||||||
// double check like sync.Once
|
|
||||||
if w.initOk.Load() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if w.initErr != nil {
|
|
||||||
return w.initErr
|
|
||||||
}
|
|
||||||
|
|
||||||
w.bind.ResetReservedForEndpoint()
|
|
||||||
w.serverAddrMap = make(map[M.Socksaddr]netip.AddrPort)
|
|
||||||
ipcConf, err := w.genIpcConf(ctx, false)
|
|
||||||
if err != nil {
|
|
||||||
// !!! do not set initErr here !!!
|
|
||||||
// let us can retry domain resolve in next time
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if debug.Enabled {
|
|
||||||
log.SingLogger.Trace(fmt.Sprintf("[WG](%s) created wireguard ipc conf: \n %s", w.option.Name, ipcConf))
|
|
||||||
}
|
|
||||||
err = w.device.IpcSet(ipcConf)
|
|
||||||
if err != nil {
|
|
||||||
w.initErr = E.Cause(err, "setup wireguard")
|
|
||||||
return w.initErr
|
|
||||||
}
|
|
||||||
w.serverAddrTime.Store(time.Now())
|
|
||||||
|
|
||||||
err = w.tunDevice.Start()
|
|
||||||
if err != nil {
|
|
||||||
w.initErr = err
|
|
||||||
return w.initErr
|
|
||||||
}
|
|
||||||
|
|
||||||
w.initOk.Store(true)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *WireGuard) updateServerAddr(ctx context.Context) {
|
|
||||||
if w.option.RefreshServerIPInterval != 0 && time.Since(w.serverAddrTime.Load()) > time.Second*time.Duration(w.option.RefreshServerIPInterval) {
|
|
||||||
if w.serverAddrMutex.TryLock() {
|
|
||||||
defer w.serverAddrMutex.Unlock()
|
|
||||||
ipcConf, err := w.genIpcConf(ctx, true)
|
|
||||||
if err != nil {
|
|
||||||
log.Warnln("[WG](%s)UpdateServerAddr failed to generate wireguard ipc conf: %s", w.option.Name, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = w.device.IpcSet(ipcConf)
|
|
||||||
if err != nil {
|
|
||||||
log.Warnln("[WG](%s)UpdateServerAddr failed to update wireguard ipc conf: %s", w.option.Name, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w.serverAddrTime.Store(time.Now())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *WireGuard) genIpcConf(ctx context.Context, updateOnly bool) (string, error) {
|
|
||||||
ipcConf := ""
|
|
||||||
if !updateOnly {
|
|
||||||
ipcConf += "private_key=" + w.option.PrivateKey + "\n"
|
|
||||||
if w.option.AmneziaWGOption != nil {
|
|
||||||
ipcConf += "jc=" + strconv.Itoa(w.option.AmneziaWGOption.JC) + "\n"
|
|
||||||
ipcConf += "jmin=" + strconv.Itoa(w.option.AmneziaWGOption.JMin) + "\n"
|
|
||||||
ipcConf += "jmax=" + strconv.Itoa(w.option.AmneziaWGOption.JMax) + "\n"
|
|
||||||
ipcConf += "s1=" + strconv.Itoa(w.option.AmneziaWGOption.S1) + "\n"
|
|
||||||
ipcConf += "s2=" + strconv.Itoa(w.option.AmneziaWGOption.S2) + "\n"
|
|
||||||
ipcConf += "h1=" + strconv.FormatUint(uint64(w.option.AmneziaWGOption.H1), 10) + "\n"
|
|
||||||
ipcConf += "h2=" + strconv.FormatUint(uint64(w.option.AmneziaWGOption.H2), 10) + "\n"
|
|
||||||
ipcConf += "h3=" + strconv.FormatUint(uint64(w.option.AmneziaWGOption.H3), 10) + "\n"
|
|
||||||
ipcConf += "h4=" + strconv.FormatUint(uint64(w.option.AmneziaWGOption.H4), 10) + "\n"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(w.option.Peers) > 0 {
|
|
||||||
for i, peer := range w.option.Peers {
|
|
||||||
peerAddr := peer.Addr()
|
|
||||||
destination, err := w.resolve(ctx, peerAddr)
|
|
||||||
if err != nil {
|
|
||||||
return "", E.Cause(err, "resolve endpoint domain for peer ", i)
|
|
||||||
}
|
|
||||||
if w.serverAddrMap[peerAddr] != destination {
|
|
||||||
w.serverAddrMap[peerAddr] = destination
|
|
||||||
} else if updateOnly {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(w.option.Peers) == 1 { // must call SetConnectAddr if isConnect == true
|
|
||||||
w.bind.SetConnectAddr(destination)
|
|
||||||
}
|
|
||||||
ipcConf += "public_key=" + peer.PublicKey + "\n"
|
|
||||||
if updateOnly {
|
|
||||||
ipcConf += "update_only=true\n"
|
|
||||||
}
|
|
||||||
ipcConf += "endpoint=" + destination.String() + "\n"
|
|
||||||
if len(peer.Reserved) > 0 {
|
|
||||||
var reserved [3]uint8
|
|
||||||
copy(reserved[:], w.option.Reserved)
|
|
||||||
w.bind.SetReservedForEndpoint(destination, reserved)
|
|
||||||
}
|
|
||||||
if updateOnly {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if peer.PreSharedKey != "" {
|
|
||||||
ipcConf += "preshared_key=" + peer.PreSharedKey + "\n"
|
|
||||||
}
|
|
||||||
for _, allowedIP := range peer.AllowedIPs {
|
|
||||||
ipcConf += "allowed_ip=" + allowedIP + "\n"
|
|
||||||
}
|
|
||||||
if w.option.PersistentKeepalive != 0 {
|
|
||||||
ipcConf += fmt.Sprintf("persistent_keepalive_interval=%d\n", w.option.PersistentKeepalive)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
destination, err := w.resolve(ctx, w.connectAddr)
|
|
||||||
if err != nil {
|
|
||||||
return "", E.Cause(err, "resolve endpoint domain")
|
|
||||||
}
|
|
||||||
if w.serverAddrMap[w.connectAddr] != destination {
|
|
||||||
w.serverAddrMap[w.connectAddr] = destination
|
|
||||||
} else if updateOnly {
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
w.bind.SetConnectAddr(destination) // must call SetConnectAddr if isConnect == true
|
|
||||||
ipcConf += "public_key=" + w.option.PublicKey + "\n"
|
|
||||||
if updateOnly {
|
|
||||||
ipcConf += "update_only=true\n"
|
|
||||||
}
|
|
||||||
ipcConf += "endpoint=" + destination.String() + "\n"
|
|
||||||
if updateOnly {
|
|
||||||
return ipcConf, nil
|
|
||||||
}
|
|
||||||
if w.option.PreSharedKey != "" {
|
|
||||||
ipcConf += "preshared_key=" + w.option.PreSharedKey + "\n"
|
|
||||||
}
|
|
||||||
var has4, has6 bool
|
|
||||||
for _, address := range w.localPrefixes {
|
|
||||||
if address.Addr().Is4() {
|
|
||||||
has4 = true
|
|
||||||
} else {
|
|
||||||
has6 = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if has4 {
|
|
||||||
ipcConf += "allowed_ip=0.0.0.0/0\n"
|
|
||||||
}
|
|
||||||
if has6 {
|
|
||||||
ipcConf += "allowed_ip=::/0\n"
|
|
||||||
}
|
|
||||||
|
|
||||||
if w.option.PersistentKeepalive != 0 {
|
|
||||||
ipcConf += fmt.Sprintf("persistent_keepalive_interval=%d\n", w.option.PersistentKeepalive)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ipcConf, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close implements C.ProxyAdapter
|
|
||||||
func (w *WireGuard) Close() error {
|
|
||||||
if w.device != nil {
|
if w.device != nil {
|
||||||
w.device.Close()
|
w.device.Close()
|
||||||
}
|
}
|
||||||
return nil
|
_ = common.Close(w.tunDevice)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WireGuard) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Conn, err error) {
|
func (w *WireGuard) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
||||||
|
options := w.Base.DialOptions(opts...)
|
||||||
|
w.dialer.SetDialer(dialer.NewDialer(options...))
|
||||||
var conn net.Conn
|
var conn net.Conn
|
||||||
if err = w.init(ctx); err != nil {
|
w.startOnce.Do(func() {
|
||||||
return nil, err
|
w.startErr = w.tunDevice.Start()
|
||||||
|
})
|
||||||
|
if w.startErr != nil {
|
||||||
|
return nil, w.startErr
|
||||||
}
|
}
|
||||||
if !metadata.Resolved() || w.resolver != nil {
|
if !metadata.Resolved() || w.resolver != nil {
|
||||||
r := resolver.DefaultResolver
|
r := resolver.DefaultResolver
|
||||||
if w.resolver != nil {
|
if w.resolver != nil {
|
||||||
|
w.refP.SetProxyAdapter(w)
|
||||||
|
defer w.refP.ClearProxyAdapter()
|
||||||
r = w.resolver
|
r = w.resolver
|
||||||
}
|
}
|
||||||
options := w.DialOptions()
|
|
||||||
options = append(options, dialer.WithResolver(r))
|
options = append(options, dialer.WithResolver(r))
|
||||||
options = append(options, dialer.WithNetDialer(wgNetDialer{tunDevice: w.tunDevice}))
|
options = append(options, dialer.WithNetDialer(wgNetDialer{tunDevice: w.tunDevice}))
|
||||||
conn, err = dialer.NewDialer(options...).DialContext(ctx, "tcp", metadata.RemoteAddress())
|
conn, err = dialer.NewDialer(options...).DialContext(ctx, "tcp", metadata.RemoteAddress())
|
||||||
@@ -512,17 +350,27 @@ func (w *WireGuard) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.
|
|||||||
if conn == nil {
|
if conn == nil {
|
||||||
return nil, E.New("conn is nil")
|
return nil, E.New("conn is nil")
|
||||||
}
|
}
|
||||||
return NewConn(conn, w), nil
|
return NewConn(CN.NewRefConn(conn, w), w), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WireGuard) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
func (w *WireGuard) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
||||||
|
options := w.Base.DialOptions(opts...)
|
||||||
|
w.dialer.SetDialer(dialer.NewDialer(options...))
|
||||||
var pc net.PacketConn
|
var pc net.PacketConn
|
||||||
if err = w.init(ctx); err != nil {
|
w.startOnce.Do(func() {
|
||||||
|
w.startErr = w.tunDevice.Start()
|
||||||
|
})
|
||||||
|
if w.startErr != nil {
|
||||||
|
return nil, w.startErr
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if (!metadata.Resolved() || w.resolver != nil) && metadata.Host != "" {
|
if (!metadata.Resolved() || w.resolver != nil) && metadata.Host != "" {
|
||||||
r := resolver.DefaultResolver
|
r := resolver.DefaultResolver
|
||||||
if w.resolver != nil {
|
if w.resolver != nil {
|
||||||
|
w.refP.SetProxyAdapter(w)
|
||||||
|
defer w.refP.ClearProxyAdapter()
|
||||||
r = w.resolver
|
r = w.resolver
|
||||||
}
|
}
|
||||||
ip, err := resolver.ResolveIPWithResolver(ctx, metadata.Host, r)
|
ip, err := resolver.ResolveIPWithResolver(ctx, metadata.Host, r)
|
||||||
@@ -538,10 +386,146 @@ func (w *WireGuard) ListenPacketContext(ctx context.Context, metadata *C.Metadat
|
|||||||
if pc == nil {
|
if pc == nil {
|
||||||
return nil, E.New("packetConn is nil")
|
return nil, E.New("packetConn is nil")
|
||||||
}
|
}
|
||||||
return newPacketConn(pc, w), nil
|
return newPacketConn(CN.NewRefPacketConn(pc, w), w), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsL3Protocol implements C.ProxyAdapter
|
// IsL3Protocol implements C.ProxyAdapter
|
||||||
func (w *WireGuard) IsL3Protocol(metadata *C.Metadata) bool {
|
func (w *WireGuard) IsL3Protocol(metadata *C.Metadata) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type refProxyAdapter struct {
|
||||||
|
proxyAdapter C.ProxyAdapter
|
||||||
|
count int
|
||||||
|
mutex sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *refProxyAdapter) SetProxyAdapter(proxyAdapter C.ProxyAdapter) {
|
||||||
|
r.mutex.Lock()
|
||||||
|
defer r.mutex.Unlock()
|
||||||
|
r.proxyAdapter = proxyAdapter
|
||||||
|
r.count++
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *refProxyAdapter) ClearProxyAdapter() {
|
||||||
|
r.mutex.Lock()
|
||||||
|
defer r.mutex.Unlock()
|
||||||
|
r.count--
|
||||||
|
if r.count == 0 {
|
||||||
|
r.proxyAdapter = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *refProxyAdapter) Name() string {
|
||||||
|
if r.proxyAdapter != nil {
|
||||||
|
return r.proxyAdapter.Name()
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *refProxyAdapter) Type() C.AdapterType {
|
||||||
|
if r.proxyAdapter != nil {
|
||||||
|
return r.proxyAdapter.Type()
|
||||||
|
}
|
||||||
|
return C.AdapterType(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *refProxyAdapter) Addr() string {
|
||||||
|
if r.proxyAdapter != nil {
|
||||||
|
return r.proxyAdapter.Addr()
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *refProxyAdapter) SupportUDP() bool {
|
||||||
|
if r.proxyAdapter != nil {
|
||||||
|
return r.proxyAdapter.SupportUDP()
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *refProxyAdapter) SupportXUDP() bool {
|
||||||
|
if r.proxyAdapter != nil {
|
||||||
|
return r.proxyAdapter.SupportXUDP()
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *refProxyAdapter) SupportTFO() bool {
|
||||||
|
if r.proxyAdapter != nil {
|
||||||
|
return r.proxyAdapter.SupportTFO()
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *refProxyAdapter) MarshalJSON() ([]byte, error) {
|
||||||
|
if r.proxyAdapter != nil {
|
||||||
|
return r.proxyAdapter.MarshalJSON()
|
||||||
|
}
|
||||||
|
return nil, C.ErrNotSupport
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *refProxyAdapter) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.Metadata) (net.Conn, error) {
|
||||||
|
if r.proxyAdapter != nil {
|
||||||
|
return r.proxyAdapter.StreamConnContext(ctx, c, metadata)
|
||||||
|
}
|
||||||
|
return nil, C.ErrNotSupport
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *refProxyAdapter) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
||||||
|
if r.proxyAdapter != nil {
|
||||||
|
return r.proxyAdapter.DialContext(ctx, metadata, opts...)
|
||||||
|
}
|
||||||
|
return nil, C.ErrNotSupport
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *refProxyAdapter) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
||||||
|
if r.proxyAdapter != nil {
|
||||||
|
return r.proxyAdapter.ListenPacketContext(ctx, metadata, opts...)
|
||||||
|
}
|
||||||
|
return nil, C.ErrNotSupport
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *refProxyAdapter) SupportUOT() bool {
|
||||||
|
if r.proxyAdapter != nil {
|
||||||
|
return r.proxyAdapter.SupportUOT()
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *refProxyAdapter) SupportWithDialer() C.NetWork {
|
||||||
|
if r.proxyAdapter != nil {
|
||||||
|
return r.proxyAdapter.SupportWithDialer()
|
||||||
|
}
|
||||||
|
return C.InvalidNet
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *refProxyAdapter) DialContextWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (C.Conn, error) {
|
||||||
|
if r.proxyAdapter != nil {
|
||||||
|
return r.proxyAdapter.DialContextWithDialer(ctx, dialer, metadata)
|
||||||
|
}
|
||||||
|
return nil, C.ErrNotSupport
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *refProxyAdapter) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (C.PacketConn, error) {
|
||||||
|
if r.proxyAdapter != nil {
|
||||||
|
return r.proxyAdapter.ListenPacketWithDialer(ctx, dialer, metadata)
|
||||||
|
}
|
||||||
|
return nil, C.ErrNotSupport
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *refProxyAdapter) IsL3Protocol(metadata *C.Metadata) bool {
|
||||||
|
if r.proxyAdapter != nil {
|
||||||
|
return r.proxyAdapter.IsL3Protocol(metadata)
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *refProxyAdapter) Unwrap(metadata *C.Metadata, touch bool) C.Proxy {
|
||||||
|
if r.proxyAdapter != nil {
|
||||||
|
return r.proxyAdapter.Unwrap(metadata, touch)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ C.ProxyAdapter = (*refProxyAdapter)(nil)
|
||||||
|
|||||||
@@ -6,11 +6,13 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/callback"
|
"github.com/Dreamacro/clash/adapter/outbound"
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
"github.com/Dreamacro/clash/common/callback"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
"github.com/Dreamacro/clash/common/utils"
|
||||||
"github.com/metacubex/mihomo/constant/provider"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
"github.com/Dreamacro/clash/constant/provider"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Fallback struct {
|
type Fallback struct {
|
||||||
@@ -19,8 +21,6 @@ type Fallback struct {
|
|||||||
testUrl string
|
testUrl string
|
||||||
selected string
|
selected string
|
||||||
expectedStatus string
|
expectedStatus string
|
||||||
Hidden bool
|
|
||||||
Icon string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fallback) Now() string {
|
func (f *Fallback) Now() string {
|
||||||
@@ -29,13 +29,13 @@ func (f *Fallback) Now() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (f *Fallback) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
func (f *Fallback) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
||||||
proxy := f.findAliveProxy(true)
|
proxy := f.findAliveProxy(true)
|
||||||
c, err := proxy.DialContext(ctx, metadata)
|
c, err := proxy.DialContext(ctx, metadata, f.Base.DialOptions(opts...)...)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
c.AppendToChains(f)
|
c.AppendToChains(f)
|
||||||
} else {
|
} else {
|
||||||
f.onDialFailed(proxy.Type(), err, f.healthCheck)
|
f.onDialFailed(proxy.Type(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if N.NeedHandshake(c) {
|
if N.NeedHandshake(c) {
|
||||||
@@ -43,7 +43,7 @@ func (f *Fallback) DialContext(ctx context.Context, metadata *C.Metadata) (C.Con
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
f.onDialSuccess()
|
f.onDialSuccess()
|
||||||
} else {
|
} else {
|
||||||
f.onDialFailed(proxy.Type(), err, f.healthCheck)
|
f.onDialFailed(proxy.Type(), err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -52,9 +52,9 @@ func (f *Fallback) DialContext(ctx context.Context, metadata *C.Metadata) (C.Con
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (f *Fallback) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
func (f *Fallback) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
||||||
proxy := f.findAliveProxy(true)
|
proxy := f.findAliveProxy(true)
|
||||||
pc, err := proxy.ListenPacketContext(ctx, metadata)
|
pc, err := proxy.ListenPacketContext(ctx, metadata, f.Base.DialOptions(opts...)...)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
pc.AppendToChains(f)
|
pc.AppendToChains(f)
|
||||||
}
|
}
|
||||||
@@ -84,14 +84,11 @@ func (f *Fallback) MarshalJSON() ([]byte, error) {
|
|||||||
all = append(all, proxy.Name())
|
all = append(all, proxy.Name())
|
||||||
}
|
}
|
||||||
return json.Marshal(map[string]any{
|
return json.Marshal(map[string]any{
|
||||||
"type": f.Type().String(),
|
"type": f.Type().String(),
|
||||||
"now": f.Now(),
|
"now": f.Now(),
|
||||||
"all": all,
|
"all": all,
|
||||||
"testUrl": f.testUrl,
|
"testUrl": f.testUrl,
|
||||||
"expectedStatus": f.expectedStatus,
|
"expected": f.expectedStatus,
|
||||||
"fixed": f.selected,
|
|
||||||
"hidden": f.Hidden,
|
|
||||||
"icon": f.Icon,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,11 +102,13 @@ func (f *Fallback) findAliveProxy(touch bool) C.Proxy {
|
|||||||
proxies := f.GetProxies(touch)
|
proxies := f.GetProxies(touch)
|
||||||
for _, proxy := range proxies {
|
for _, proxy := range proxies {
|
||||||
if len(f.selected) == 0 {
|
if len(f.selected) == 0 {
|
||||||
|
// if proxy.Alive() {
|
||||||
if proxy.AliveForTestUrl(f.testUrl) {
|
if proxy.AliveForTestUrl(f.testUrl) {
|
||||||
return proxy
|
return proxy
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if proxy.Name() == f.selected {
|
if proxy.Name() == f.selected {
|
||||||
|
// if proxy.Alive() {
|
||||||
if proxy.AliveForTestUrl(f.testUrl) {
|
if proxy.AliveForTestUrl(f.testUrl) {
|
||||||
return proxy
|
return proxy
|
||||||
} else {
|
} else {
|
||||||
@@ -136,11 +135,12 @@ func (f *Fallback) Set(name string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
f.selected = name
|
f.selected = name
|
||||||
|
// if !p.Alive() {
|
||||||
if !p.AliveForTestUrl(f.testUrl) {
|
if !p.AliveForTestUrl(f.testUrl) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(5000))
|
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*time.Duration(5000))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
expectedStatus, _ := utils.NewUnsignedRanges[uint16](f.expectedStatus)
|
expectedStatus, _ := utils.NewIntRanges[uint16](f.expectedStatus)
|
||||||
_, _ = p.URLTest(ctx, f.testUrl, expectedStatus)
|
_, _ = p.URLTest(ctx, f.testUrl, expectedStatus, C.ExtraHistory)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -153,19 +153,19 @@ func (f *Fallback) ForceSet(name string) {
|
|||||||
func NewFallback(option *GroupCommonOption, providers []provider.ProxyProvider) *Fallback {
|
func NewFallback(option *GroupCommonOption, providers []provider.ProxyProvider) *Fallback {
|
||||||
return &Fallback{
|
return &Fallback{
|
||||||
GroupBase: NewGroupBase(GroupBaseOption{
|
GroupBase: NewGroupBase(GroupBaseOption{
|
||||||
Name: option.Name,
|
outbound.BaseOption{
|
||||||
Type: C.Fallback,
|
Name: option.Name,
|
||||||
Filter: option.Filter,
|
Type: C.Fallback,
|
||||||
ExcludeFilter: option.ExcludeFilter,
|
Interface: option.Interface,
|
||||||
ExcludeType: option.ExcludeType,
|
RoutingMark: option.RoutingMark,
|
||||||
TestTimeout: option.TestTimeout,
|
},
|
||||||
MaxFailedTimes: option.MaxFailedTimes,
|
option.Filter,
|
||||||
Providers: providers,
|
option.ExcludeFilter,
|
||||||
|
option.ExcludeType,
|
||||||
|
providers,
|
||||||
}),
|
}),
|
||||||
disableUDP: option.DisableUDP,
|
disableUDP: option.DisableUDP,
|
||||||
testUrl: option.URL,
|
testUrl: option.URL,
|
||||||
expectedStatus: option.ExpectedStatus,
|
expectedStatus: option.ExpectedStatus,
|
||||||
Hidden: option.Hidden,
|
|
||||||
Icon: option.Icon,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,94 +2,74 @@ package outboundgroup
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/adapter/outbound"
|
"github.com/Dreamacro/clash/adapter/outbound"
|
||||||
"github.com/metacubex/mihomo/common/atomic"
|
"github.com/Dreamacro/clash/common/atomic"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/Dreamacro/clash/common/utils"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/metacubex/mihomo/constant/provider"
|
"github.com/Dreamacro/clash/constant/provider"
|
||||||
types "github.com/metacubex/mihomo/constant/provider"
|
types "github.com/Dreamacro/clash/constant/provider"
|
||||||
"github.com/metacubex/mihomo/log"
|
"github.com/Dreamacro/clash/log"
|
||||||
"github.com/metacubex/mihomo/tunnel"
|
"github.com/Dreamacro/clash/tunnel"
|
||||||
|
|
||||||
"github.com/dlclark/regexp2"
|
"github.com/dlclark/regexp2"
|
||||||
"golang.org/x/exp/slices"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type GroupBase struct {
|
type GroupBase struct {
|
||||||
*outbound.Base
|
*outbound.Base
|
||||||
filterRegs []*regexp2.Regexp
|
filterRegs []*regexp2.Regexp
|
||||||
excludeFilterRegs []*regexp2.Regexp
|
excludeFilterReg *regexp2.Regexp
|
||||||
excludeTypeArray []string
|
excludeTypeArray []string
|
||||||
providers []provider.ProxyProvider
|
providers []provider.ProxyProvider
|
||||||
failedTestMux sync.Mutex
|
failedTestMux sync.Mutex
|
||||||
failedTimes int
|
failedTimes int
|
||||||
failedTime time.Time
|
failedTime time.Time
|
||||||
failedTesting atomic.Bool
|
failedTesting atomic.Bool
|
||||||
TestTimeout int
|
proxies [][]C.Proxy
|
||||||
maxFailedTimes int
|
versions []atomic.Uint32
|
||||||
|
|
||||||
// for GetProxies
|
|
||||||
getProxiesMutex sync.Mutex
|
|
||||||
providerVersions []uint32
|
|
||||||
providerProxies []C.Proxy
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type GroupBaseOption struct {
|
type GroupBaseOption struct {
|
||||||
Name string
|
outbound.BaseOption
|
||||||
Type C.AdapterType
|
filter string
|
||||||
Filter string
|
excludeFilter string
|
||||||
ExcludeFilter string
|
excludeType string
|
||||||
ExcludeType string
|
providers []provider.ProxyProvider
|
||||||
TestTimeout int
|
|
||||||
MaxFailedTimes int
|
|
||||||
Providers []provider.ProxyProvider
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGroupBase(opt GroupBaseOption) *GroupBase {
|
func NewGroupBase(opt GroupBaseOption) *GroupBase {
|
||||||
var excludeTypeArray []string
|
var excludeFilterReg *regexp2.Regexp
|
||||||
if opt.ExcludeType != "" {
|
if opt.excludeFilter != "" {
|
||||||
excludeTypeArray = strings.Split(opt.ExcludeType, "|")
|
excludeFilterReg = regexp2.MustCompile(opt.excludeFilter, 0)
|
||||||
}
|
}
|
||||||
|
var excludeTypeArray []string
|
||||||
var excludeFilterRegs []*regexp2.Regexp
|
if opt.excludeType != "" {
|
||||||
if opt.ExcludeFilter != "" {
|
excludeTypeArray = strings.Split(opt.excludeType, "|")
|
||||||
for _, excludeFilter := range strings.Split(opt.ExcludeFilter, "`") {
|
|
||||||
excludeFilterReg := regexp2.MustCompile(excludeFilter, regexp2.None)
|
|
||||||
excludeFilterRegs = append(excludeFilterRegs, excludeFilterReg)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var filterRegs []*regexp2.Regexp
|
var filterRegs []*regexp2.Regexp
|
||||||
if opt.Filter != "" {
|
if opt.filter != "" {
|
||||||
for _, filter := range strings.Split(opt.Filter, "`") {
|
for _, filter := range strings.Split(opt.filter, "`") {
|
||||||
filterReg := regexp2.MustCompile(filter, regexp2.None)
|
filterReg := regexp2.MustCompile(filter, 0)
|
||||||
filterRegs = append(filterRegs, filterReg)
|
filterRegs = append(filterRegs, filterReg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
gb := &GroupBase{
|
gb := &GroupBase{
|
||||||
Base: outbound.NewBase(outbound.BaseOption{Name: opt.Name, Type: opt.Type}),
|
Base: outbound.NewBase(opt.BaseOption),
|
||||||
filterRegs: filterRegs,
|
filterRegs: filterRegs,
|
||||||
excludeFilterRegs: excludeFilterRegs,
|
excludeFilterReg: excludeFilterReg,
|
||||||
excludeTypeArray: excludeTypeArray,
|
excludeTypeArray: excludeTypeArray,
|
||||||
providers: opt.Providers,
|
providers: opt.providers,
|
||||||
failedTesting: atomic.NewBool(false),
|
failedTesting: atomic.NewBool(false),
|
||||||
TestTimeout: opt.TestTimeout,
|
|
||||||
maxFailedTimes: opt.MaxFailedTimes,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if gb.TestTimeout == 0 {
|
gb.proxies = make([][]C.Proxy, len(opt.providers))
|
||||||
gb.TestTimeout = 5000
|
gb.versions = make([]atomic.Uint32, len(opt.providers))
|
||||||
}
|
|
||||||
if gb.maxFailedTimes == 0 {
|
|
||||||
gb.maxFailedTimes = 5
|
|
||||||
}
|
|
||||||
|
|
||||||
return gb
|
return gb
|
||||||
}
|
}
|
||||||
@@ -101,62 +81,63 @@ func (gb *GroupBase) Touch() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
|
func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
|
||||||
providerVersions := make([]uint32, len(gb.providers))
|
|
||||||
for i, pd := range gb.providers {
|
|
||||||
if touch { // touch first
|
|
||||||
pd.Touch()
|
|
||||||
}
|
|
||||||
providerVersions[i] = pd.Version()
|
|
||||||
}
|
|
||||||
|
|
||||||
// thread safe
|
|
||||||
gb.getProxiesMutex.Lock()
|
|
||||||
defer gb.getProxiesMutex.Unlock()
|
|
||||||
|
|
||||||
// return the cached proxies if version not changed
|
|
||||||
if slices.Equal(providerVersions, gb.providerVersions) {
|
|
||||||
return gb.providerProxies
|
|
||||||
}
|
|
||||||
|
|
||||||
var proxies []C.Proxy
|
var proxies []C.Proxy
|
||||||
if len(gb.filterRegs) == 0 {
|
if len(gb.filterRegs) == 0 {
|
||||||
for _, pd := range gb.providers {
|
for _, pd := range gb.providers {
|
||||||
|
if touch {
|
||||||
|
pd.Touch()
|
||||||
|
}
|
||||||
proxies = append(proxies, pd.Proxies()...)
|
proxies = append(proxies, pd.Proxies()...)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for _, pd := range gb.providers {
|
for i, pd := range gb.providers {
|
||||||
if pd.VehicleType() == types.Compatible { // compatible provider unneeded filter
|
if touch {
|
||||||
proxies = append(proxies, pd.Proxies()...)
|
pd.Touch()
|
||||||
|
}
|
||||||
|
|
||||||
|
if pd.VehicleType() == types.Compatible {
|
||||||
|
gb.versions[i].Store(pd.Version())
|
||||||
|
gb.proxies[i] = pd.Proxies()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
var newProxies []C.Proxy
|
version := gb.versions[i].Load()
|
||||||
proxiesSet := map[string]struct{}{}
|
if version != pd.Version() && gb.versions[i].CompareAndSwap(version, pd.Version()) {
|
||||||
for _, filterReg := range gb.filterRegs {
|
var (
|
||||||
for _, p := range pd.Proxies() {
|
proxies []C.Proxy
|
||||||
name := p.Name()
|
newProxies []C.Proxy
|
||||||
if mat, _ := filterReg.MatchString(name); mat {
|
)
|
||||||
if _, ok := proxiesSet[name]; !ok {
|
|
||||||
proxiesSet[name] = struct{}{}
|
proxies = pd.Proxies()
|
||||||
newProxies = append(newProxies, p)
|
proxiesSet := map[string]struct{}{}
|
||||||
|
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
|
||||||
}
|
}
|
||||||
proxies = append(proxies, newProxies...)
|
}
|
||||||
|
|
||||||
|
for _, p := range gb.proxies {
|
||||||
|
proxies = append(proxies, p...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Multiple filers means that proxies are sorted in the order in which the filers appear.
|
|
||||||
// Although the filter has been performed once in the previous process,
|
|
||||||
// when there are multiple providers, the array needs to be reordered as a whole.
|
|
||||||
if len(gb.providers) > 1 && len(gb.filterRegs) > 1 {
|
if len(gb.providers) > 1 && len(gb.filterRegs) > 1 {
|
||||||
var newProxies []C.Proxy
|
var newProxies []C.Proxy
|
||||||
proxiesSet := map[string]struct{}{}
|
proxiesSet := map[string]struct{}{}
|
||||||
for _, filterReg := range gb.filterRegs {
|
for _, filterReg := range gb.filterRegs {
|
||||||
for _, p := range proxies {
|
for _, p := range proxies {
|
||||||
name := p.Name()
|
name := p.Name()
|
||||||
if mat, _ := filterReg.MatchString(name); mat {
|
if mat, _ := filterReg.FindStringMatch(name); mat != nil {
|
||||||
if _, ok := proxiesSet[name]; !ok {
|
if _, ok := proxiesSet[name]; !ok {
|
||||||
proxiesSet[name] = struct{}{}
|
proxiesSet[name] = struct{}{}
|
||||||
newProxies = append(newProxies, p)
|
newProxies = append(newProxies, p)
|
||||||
@@ -173,31 +154,32 @@ func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
|
|||||||
}
|
}
|
||||||
proxies = newProxies
|
proxies = newProxies
|
||||||
}
|
}
|
||||||
|
if gb.excludeTypeArray != nil {
|
||||||
if len(gb.excludeFilterRegs) > 0 {
|
|
||||||
var newProxies []C.Proxy
|
var newProxies []C.Proxy
|
||||||
LOOP1:
|
|
||||||
for _, p := range proxies {
|
for _, p := range proxies {
|
||||||
name := p.Name()
|
mType := p.Type().String()
|
||||||
for _, excludeFilterReg := range gb.excludeFilterRegs {
|
flag := false
|
||||||
if mat, _ := excludeFilterReg.MatchString(name); mat {
|
for i := range gb.excludeTypeArray {
|
||||||
continue LOOP1
|
if strings.EqualFold(mType, gb.excludeTypeArray[i]) {
|
||||||
|
flag = true
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
if flag {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
newProxies = append(newProxies, p)
|
newProxies = append(newProxies, p)
|
||||||
}
|
}
|
||||||
proxies = newProxies
|
proxies = newProxies
|
||||||
}
|
}
|
||||||
|
|
||||||
if gb.excludeTypeArray != nil {
|
if gb.excludeFilterReg != nil {
|
||||||
var newProxies []C.Proxy
|
var newProxies []C.Proxy
|
||||||
LOOP2:
|
|
||||||
for _, p := range proxies {
|
for _, p := range proxies {
|
||||||
mType := p.Type().String()
|
name := p.Name()
|
||||||
for _, excludeType := range gb.excludeTypeArray {
|
if mat, _ := gb.excludeFilterReg.FindStringMatch(name); mat != nil {
|
||||||
if strings.EqualFold(mType, excludeType) {
|
continue
|
||||||
continue LOOP2
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
newProxies = append(newProxies, p)
|
newProxies = append(newProxies, p)
|
||||||
}
|
}
|
||||||
@@ -205,13 +187,9 @@ func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(proxies) == 0 {
|
if len(proxies) == 0 {
|
||||||
return []C.Proxy{tunnel.Proxies()["COMPATIBLE"]}
|
return append(proxies, tunnel.Proxies()["COMPATIBLE"])
|
||||||
}
|
}
|
||||||
|
|
||||||
// only cache when proxies not empty
|
|
||||||
gb.providerVersions = providerVersions
|
|
||||||
gb.providerProxies = proxies
|
|
||||||
|
|
||||||
return proxies
|
return proxies
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,7 +202,7 @@ func (gb *GroupBase) URLTest(ctx context.Context, url string, expectedStatus uti
|
|||||||
proxy := proxy
|
proxy := proxy
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
delay, err := proxy.URLTest(ctx, url, expectedStatus)
|
delay, err := proxy.URLTest(ctx, url, expectedStatus, C.DropHistory)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
lock.Lock()
|
lock.Lock()
|
||||||
mp[proxy.Name()] = delay
|
mp[proxy.Name()] = delay
|
||||||
@@ -243,21 +221,17 @@ func (gb *GroupBase) URLTest(ctx context.Context, url string, expectedStatus uti
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gb *GroupBase) onDialFailed(adapterType C.AdapterType, err error, fn func()) {
|
func (gb *GroupBase) onDialFailed(adapterType C.AdapterType, err error) {
|
||||||
if adapterType == C.Direct || adapterType == C.Compatible || adapterType == C.Reject || adapterType == C.Pass || adapterType == C.RejectDrop {
|
if adapterType == C.Direct || adapterType == C.Compatible || adapterType == C.Reject || adapterType == C.Pass {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if errors.Is(err, C.ErrNotSupport) {
|
if strings.Contains(err.Error(), "connection refused") {
|
||||||
|
go gb.healthCheck()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
if strings.Contains(err.Error(), "connection refused") {
|
|
||||||
fn()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
gb.failedTestMux.Lock()
|
gb.failedTestMux.Lock()
|
||||||
defer gb.failedTestMux.Unlock()
|
defer gb.failedTestMux.Unlock()
|
||||||
|
|
||||||
@@ -266,15 +240,15 @@ func (gb *GroupBase) onDialFailed(adapterType C.AdapterType, err error, fn func(
|
|||||||
log.Debugln("ProxyGroup: %s first failed", gb.Name())
|
log.Debugln("ProxyGroup: %s first failed", gb.Name())
|
||||||
gb.failedTime = time.Now()
|
gb.failedTime = time.Now()
|
||||||
} else {
|
} else {
|
||||||
if time.Since(gb.failedTime) > time.Duration(gb.TestTimeout)*time.Millisecond {
|
if time.Since(gb.failedTime) > gb.failedTimeoutInterval() {
|
||||||
gb.failedTimes = 0
|
gb.failedTimes = 0
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugln("ProxyGroup: %s failed count: %d", gb.Name(), gb.failedTimes)
|
log.Debugln("ProxyGroup: %s failed count: %d", gb.Name(), gb.failedTimes)
|
||||||
if gb.failedTimes >= gb.maxFailedTimes {
|
if gb.failedTimes >= gb.maxFailedTimes() {
|
||||||
log.Warnln("because %s failed multiple times, active health check", gb.Name())
|
log.Warnln("because %s failed multiple times, active health check", gb.Name())
|
||||||
fn()
|
gb.healthCheck()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@@ -301,8 +275,20 @@ func (gb *GroupBase) healthCheck() {
|
|||||||
gb.failedTimes = 0
|
gb.failedTimes = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (gb *GroupBase) failedIntervalTime() int64 {
|
||||||
|
return 5 * time.Second.Milliseconds()
|
||||||
|
}
|
||||||
|
|
||||||
func (gb *GroupBase) onDialSuccess() {
|
func (gb *GroupBase) onDialSuccess() {
|
||||||
if !gb.failedTesting.Load() {
|
if !gb.failedTesting.Load() {
|
||||||
gb.failedTimes = 0
|
gb.failedTimes = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (gb *GroupBase) maxFailedTimes() int {
|
||||||
|
return 5
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gb *GroupBase) failedTimeoutInterval() time.Duration {
|
||||||
|
return 5 * time.Second
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,12 +9,14 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/callback"
|
"github.com/Dreamacro/clash/adapter/outbound"
|
||||||
"github.com/metacubex/mihomo/common/lru"
|
"github.com/Dreamacro/clash/common/cache"
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
"github.com/Dreamacro/clash/common/callback"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
"github.com/Dreamacro/clash/common/utils"
|
||||||
"github.com/metacubex/mihomo/constant/provider"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
"github.com/Dreamacro/clash/constant/provider"
|
||||||
|
|
||||||
"golang.org/x/net/publicsuffix"
|
"golang.org/x/net/publicsuffix"
|
||||||
)
|
)
|
||||||
@@ -27,8 +29,6 @@ type LoadBalance struct {
|
|||||||
strategyFn strategyFn
|
strategyFn strategyFn
|
||||||
testUrl string
|
testUrl string
|
||||||
expectedStatus string
|
expectedStatus string
|
||||||
Hidden bool
|
|
||||||
Icon string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var errStrategy = errors.New("unsupported strategy")
|
var errStrategy = errors.New("unsupported strategy")
|
||||||
@@ -86,14 +86,14 @@ func jumpHash(key uint64, buckets int32) int32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (lb *LoadBalance) DialContext(ctx context.Context, metadata *C.Metadata) (c C.Conn, err error) {
|
func (lb *LoadBalance) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (c C.Conn, err error) {
|
||||||
proxy := lb.Unwrap(metadata, true)
|
proxy := lb.Unwrap(metadata, true)
|
||||||
c, err = proxy.DialContext(ctx, metadata)
|
c, err = proxy.DialContext(ctx, metadata, lb.Base.DialOptions(opts...)...)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
c.AppendToChains(lb)
|
c.AppendToChains(lb)
|
||||||
} else {
|
} else {
|
||||||
lb.onDialFailed(proxy.Type(), err, lb.healthCheck)
|
lb.onDialFailed(proxy.Type(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if N.NeedHandshake(c) {
|
if N.NeedHandshake(c) {
|
||||||
@@ -101,7 +101,7 @@ func (lb *LoadBalance) DialContext(ctx context.Context, metadata *C.Metadata) (c
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
lb.onDialSuccess()
|
lb.onDialSuccess()
|
||||||
} else {
|
} else {
|
||||||
lb.onDialFailed(proxy.Type(), err, lb.healthCheck)
|
lb.onDialFailed(proxy.Type(), err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -110,7 +110,7 @@ func (lb *LoadBalance) DialContext(ctx context.Context, metadata *C.Metadata) (c
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (lb *LoadBalance) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (pc C.PacketConn, err error) {
|
func (lb *LoadBalance) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (pc C.PacketConn, err error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
pc.AppendToChains(lb)
|
pc.AppendToChains(lb)
|
||||||
@@ -118,7 +118,7 @@ func (lb *LoadBalance) ListenPacketContext(ctx context.Context, metadata *C.Meta
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
proxy := lb.Unwrap(metadata, true)
|
proxy := lb.Unwrap(metadata, true)
|
||||||
return proxy.ListenPacketContext(ctx, metadata)
|
return proxy.ListenPacketContext(ctx, metadata, lb.Base.DialOptions(opts...)...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SupportUDP implements C.ProxyAdapter
|
// SupportUDP implements C.ProxyAdapter
|
||||||
@@ -150,6 +150,7 @@ func strategyRoundRobin(url string) strategyFn {
|
|||||||
for ; i < length; i++ {
|
for ; i < length; i++ {
|
||||||
id := (idx + i) % length
|
id := (idx + i) % length
|
||||||
proxy := proxies[id]
|
proxy := proxies[id]
|
||||||
|
// if proxy.Alive() {
|
||||||
if proxy.AliveForTestUrl(url) {
|
if proxy.AliveForTestUrl(url) {
|
||||||
i++
|
i++
|
||||||
return proxy
|
return proxy
|
||||||
@@ -168,6 +169,7 @@ func strategyConsistentHashing(url string) strategyFn {
|
|||||||
for i := 0; i < maxRetry; i, key = i+1, key+1 {
|
for i := 0; i < maxRetry; i, key = i+1, key+1 {
|
||||||
idx := jumpHash(key, buckets)
|
idx := jumpHash(key, buckets)
|
||||||
proxy := proxies[idx]
|
proxy := proxies[idx]
|
||||||
|
// if proxy.Alive() {
|
||||||
if proxy.AliveForTestUrl(url) {
|
if proxy.AliveForTestUrl(url) {
|
||||||
return proxy
|
return proxy
|
||||||
}
|
}
|
||||||
@@ -175,6 +177,7 @@ func strategyConsistentHashing(url string) strategyFn {
|
|||||||
|
|
||||||
// when availability is poor, traverse the entire list to get the available nodes
|
// when availability is poor, traverse the entire list to get the available nodes
|
||||||
for _, proxy := range proxies {
|
for _, proxy := range proxies {
|
||||||
|
// if proxy.Alive() {
|
||||||
if proxy.AliveForTestUrl(url) {
|
if proxy.AliveForTestUrl(url) {
|
||||||
return proxy
|
return proxy
|
||||||
}
|
}
|
||||||
@@ -187,9 +190,9 @@ func strategyConsistentHashing(url string) strategyFn {
|
|||||||
func strategyStickySessions(url string) strategyFn {
|
func strategyStickySessions(url string) strategyFn {
|
||||||
ttl := time.Minute * 10
|
ttl := time.Minute * 10
|
||||||
maxRetry := 5
|
maxRetry := 5
|
||||||
lruCache := lru.New[uint64, int](
|
lruCache := cache.New[uint64, int](
|
||||||
lru.WithAge[uint64, int](int64(ttl.Seconds())),
|
cache.WithAge[uint64, int](int64(ttl.Seconds())),
|
||||||
lru.WithSize[uint64, int](1000))
|
cache.WithSize[uint64, int](1000))
|
||||||
return func(proxies []C.Proxy, metadata *C.Metadata, touch bool) C.Proxy {
|
return func(proxies []C.Proxy, metadata *C.Metadata, touch bool) C.Proxy {
|
||||||
key := utils.MapHash(getKeyWithSrcAndDst(metadata))
|
key := utils.MapHash(getKeyWithSrcAndDst(metadata))
|
||||||
length := len(proxies)
|
length := len(proxies)
|
||||||
@@ -201,8 +204,10 @@ func strategyStickySessions(url string) strategyFn {
|
|||||||
nowIdx := idx
|
nowIdx := idx
|
||||||
for i := 1; i < maxRetry; i++ {
|
for i := 1; i < maxRetry; i++ {
|
||||||
proxy := proxies[nowIdx]
|
proxy := proxies[nowIdx]
|
||||||
|
// if proxy.Alive() {
|
||||||
if proxy.AliveForTestUrl(url) {
|
if proxy.AliveForTestUrl(url) {
|
||||||
if !has || nowIdx != idx {
|
if nowIdx != idx {
|
||||||
|
lruCache.Delete(key)
|
||||||
lruCache.Set(key, nowIdx)
|
lruCache.Set(key, nowIdx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,6 +217,7 @@ func strategyStickySessions(url string) strategyFn {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lruCache.Delete(key)
|
||||||
lruCache.Set(key, 0)
|
lruCache.Set(key, 0)
|
||||||
return proxies[0]
|
return proxies[0]
|
||||||
}
|
}
|
||||||
@@ -234,8 +240,6 @@ func (lb *LoadBalance) MarshalJSON() ([]byte, error) {
|
|||||||
"all": all,
|
"all": all,
|
||||||
"testUrl": lb.testUrl,
|
"testUrl": lb.testUrl,
|
||||||
"expectedStatus": lb.expectedStatus,
|
"expectedStatus": lb.expectedStatus,
|
||||||
"hidden": lb.Hidden,
|
|
||||||
"icon": lb.Icon,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -253,20 +257,20 @@ func NewLoadBalance(option *GroupCommonOption, providers []provider.ProxyProvide
|
|||||||
}
|
}
|
||||||
return &LoadBalance{
|
return &LoadBalance{
|
||||||
GroupBase: NewGroupBase(GroupBaseOption{
|
GroupBase: NewGroupBase(GroupBaseOption{
|
||||||
Name: option.Name,
|
outbound.BaseOption{
|
||||||
Type: C.LoadBalance,
|
Name: option.Name,
|
||||||
Filter: option.Filter,
|
Type: C.LoadBalance,
|
||||||
ExcludeFilter: option.ExcludeFilter,
|
Interface: option.Interface,
|
||||||
ExcludeType: option.ExcludeType,
|
RoutingMark: option.RoutingMark,
|
||||||
TestTimeout: option.TestTimeout,
|
},
|
||||||
MaxFailedTimes: option.MaxFailedTimes,
|
option.Filter,
|
||||||
Providers: providers,
|
option.ExcludeFilter,
|
||||||
|
option.ExcludeType,
|
||||||
|
providers,
|
||||||
}),
|
}),
|
||||||
strategyFn: strategyFn,
|
strategyFn: strategyFn,
|
||||||
disableUDP: option.DisableUDP,
|
disableUDP: option.DisableUDP,
|
||||||
testUrl: option.URL,
|
testUrl: option.URL,
|
||||||
expectedStatus: option.ExpectedStatus,
|
expectedStatus: option.ExpectedStatus,
|
||||||
Hidden: option.Hidden,
|
|
||||||
Icon: option.Icon,
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,14 +5,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/dlclark/regexp2"
|
"github.com/Dreamacro/clash/adapter/outbound"
|
||||||
|
"github.com/Dreamacro/clash/adapter/provider"
|
||||||
"github.com/metacubex/mihomo/adapter/provider"
|
"github.com/Dreamacro/clash/common/structure"
|
||||||
"github.com/metacubex/mihomo/common/structure"
|
"github.com/Dreamacro/clash/common/utils"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
types "github.com/Dreamacro/clash/constant/provider"
|
||||||
types "github.com/metacubex/mihomo/constant/provider"
|
|
||||||
"github.com/metacubex/mihomo/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -23,32 +21,22 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type GroupCommonOption struct {
|
type GroupCommonOption struct {
|
||||||
Name string `group:"name"`
|
outbound.BasicOption
|
||||||
Type string `group:"type"`
|
Name string `group:"name"`
|
||||||
Proxies []string `group:"proxies,omitempty"`
|
Type string `group:"type"`
|
||||||
Use []string `group:"use,omitempty"`
|
Proxies []string `group:"proxies,omitempty"`
|
||||||
URL string `group:"url,omitempty"`
|
Use []string `group:"use,omitempty"`
|
||||||
Interval int `group:"interval,omitempty"`
|
URL string `group:"url,omitempty"`
|
||||||
TestTimeout int `group:"timeout,omitempty"`
|
Interval int `group:"interval,omitempty"`
|
||||||
MaxFailedTimes int `group:"max-failed-times,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"`
|
||||||
ExcludeFilter string `group:"exclude-filter,omitempty"`
|
ExcludeType string `group:"exclude-type,omitempty"`
|
||||||
ExcludeType string `group:"exclude-type,omitempty"`
|
ExpectedStatus string `group:"expected-status,omitempty"`
|
||||||
ExpectedStatus string `group:"expected-status,omitempty"`
|
|
||||||
IncludeAll bool `group:"include-all,omitempty"`
|
|
||||||
IncludeAllProxies bool `group:"include-all-proxies,omitempty"`
|
|
||||||
IncludeAllProviders bool `group:"include-all-providers,omitempty"`
|
|
||||||
Hidden bool `group:"hidden,omitempty"`
|
|
||||||
Icon string `group:"icon,omitempty"`
|
|
||||||
|
|
||||||
// removed configs, only for error logging
|
|
||||||
Interface string `group:"interface-name,omitempty"`
|
|
||||||
RoutingMark int `group:"routing-mark,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, providersMap map[string]types.ProxyProvider, AllProxies []string, AllProviders []string) (C.ProxyAdapter, error) {
|
func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, providersMap map[string]types.ProxyProvider) (C.ProxyAdapter, error) {
|
||||||
decoder := structure.NewDecoder(structure.Option{TagName: "group", WeaklyTypedInput: true})
|
decoder := structure.NewDecoder(structure.Option{TagName: "group", WeaklyTypedInput: true})
|
||||||
|
|
||||||
groupOption := &GroupCommonOption{
|
groupOption := &GroupCommonOption{
|
||||||
@@ -62,52 +50,15 @@ func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, provide
|
|||||||
return nil, errFormat
|
return nil, errFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
if groupOption.RoutingMark != 0 {
|
|
||||||
log.Errorln("The group [%s] with routing-mark configuration was removed, please set it directly on the proxy instead", groupOption.Name)
|
|
||||||
}
|
|
||||||
if groupOption.Interface != "" {
|
|
||||||
log.Errorln("The group [%s] with interface-name configuration was removed, please set it directly on the proxy instead", groupOption.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
groupName := groupOption.Name
|
groupName := groupOption.Name
|
||||||
|
|
||||||
providers := []types.ProxyProvider{}
|
providers := []types.ProxyProvider{}
|
||||||
|
|
||||||
if groupOption.IncludeAll {
|
|
||||||
groupOption.IncludeAllProviders = true
|
|
||||||
groupOption.IncludeAllProxies = true
|
|
||||||
}
|
|
||||||
|
|
||||||
if groupOption.IncludeAllProviders {
|
|
||||||
groupOption.Use = AllProviders
|
|
||||||
}
|
|
||||||
if groupOption.IncludeAllProxies {
|
|
||||||
if groupOption.Filter != "" {
|
|
||||||
var filterRegs []*regexp2.Regexp
|
|
||||||
for _, filter := range strings.Split(groupOption.Filter, "`") {
|
|
||||||
filterReg := regexp2.MustCompile(filter, regexp2.None)
|
|
||||||
filterRegs = append(filterRegs, filterReg)
|
|
||||||
}
|
|
||||||
for _, p := range AllProxies {
|
|
||||||
for _, filterReg := range filterRegs {
|
|
||||||
if mat, _ := filterReg.MatchString(p); mat {
|
|
||||||
groupOption.Proxies = append(groupOption.Proxies, p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
groupOption.Proxies = append(groupOption.Proxies, AllProxies...)
|
|
||||||
}
|
|
||||||
if len(groupOption.Proxies) == 0 && len(groupOption.Use) == 0 {
|
|
||||||
groupOption.Proxies = []string{"COMPATIBLE"}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(groupOption.Proxies) == 0 && len(groupOption.Use) == 0 {
|
if len(groupOption.Proxies) == 0 && len(groupOption.Use) == 0 {
|
||||||
return nil, fmt.Errorf("%s: %w", groupName, errMissProxy)
|
return nil, fmt.Errorf("%s: %w", groupName, errMissProxy)
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedStatus, err := utils.NewUnsignedRanges[uint16](groupOption.ExpectedStatus)
|
expectedStatus, err := utils.NewIntRanges[uint16](groupOption.ExpectedStatus)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s: %w", groupName, err)
|
return nil, fmt.Errorf("%s: %w", groupName, err)
|
||||||
}
|
}
|
||||||
@@ -117,29 +68,7 @@ func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, provide
|
|||||||
status = "*"
|
status = "*"
|
||||||
}
|
}
|
||||||
groupOption.ExpectedStatus = status
|
groupOption.ExpectedStatus = status
|
||||||
|
testUrl := groupOption.URL
|
||||||
if len(groupOption.Use) != 0 {
|
|
||||||
PDs, err := getProviders(providersMap, groupOption.Use)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%s: %w", groupName, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// if test URL is empty, use the first health check URL of providers
|
|
||||||
if groupOption.URL == "" {
|
|
||||||
for _, pd := range PDs {
|
|
||||||
if pd.HealthCheckURL() != "" {
|
|
||||||
groupOption.URL = pd.HealthCheckURL()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if groupOption.URL == "" {
|
|
||||||
groupOption.URL = C.DefaultTestURL
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
addTestUrlToProviders(PDs, groupOption.URL, expectedStatus, groupOption.Filter, uint(groupOption.Interval))
|
|
||||||
}
|
|
||||||
providers = append(providers, PDs...)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(groupOption.Proxies) != 0 {
|
if len(groupOption.Proxies) != 0 {
|
||||||
ps, err := getProxies(proxyMap, groupOption.Proxies)
|
ps, err := getProxies(proxyMap, groupOption.Proxies)
|
||||||
@@ -151,28 +80,47 @@ func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, provide
|
|||||||
return nil, fmt.Errorf("%s: %w", groupName, errDuplicateProvider)
|
return nil, fmt.Errorf("%s: %w", groupName, errDuplicateProvider)
|
||||||
}
|
}
|
||||||
|
|
||||||
if groupOption.URL == "" {
|
var url string
|
||||||
groupOption.URL = C.DefaultTestURL
|
var interval uint
|
||||||
}
|
|
||||||
|
|
||||||
// select don't need auto health check
|
// select don't need health check
|
||||||
if groupOption.Type != "select" && groupOption.Type != "relay" {
|
if groupOption.Type != "select" && groupOption.Type != "relay" {
|
||||||
|
if groupOption.URL == "" {
|
||||||
|
groupOption.URL = "https://cp.cloudflare.com/generate_204"
|
||||||
|
}
|
||||||
|
|
||||||
if groupOption.Interval == 0 {
|
if groupOption.Interval == 0 {
|
||||||
groupOption.Interval = 300
|
groupOption.Interval = 300
|
||||||
}
|
}
|
||||||
|
|
||||||
|
url = groupOption.URL
|
||||||
|
interval = uint(groupOption.Interval)
|
||||||
}
|
}
|
||||||
|
|
||||||
hc := provider.NewHealthCheck(ps, groupOption.URL, uint(groupOption.TestTimeout), uint(groupOption.Interval), groupOption.Lazy, expectedStatus)
|
hc := provider.NewHealthCheck(ps, url, interval, true, expectedStatus)
|
||||||
|
|
||||||
pd, err := provider.NewCompatibleProvider(groupName, ps, hc)
|
pd, err := provider.NewCompatibleProvider(groupName, ps, hc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s: %w", groupName, err)
|
return nil, fmt.Errorf("%s: %w", groupName, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
providers = append([]types.ProxyProvider{pd}, providers...)
|
providers = append(providers, pd)
|
||||||
providersMap[groupName] = pd
|
providersMap[groupName] = pd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(groupOption.Use) != 0 {
|
||||||
|
list, err := getProviders(providersMap, groupOption.Use)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("%s: %w", groupName, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// different proxy groups use different test URL
|
||||||
|
addTestUrlToProviders(list, testUrl, expectedStatus, groupOption.Filter, uint(groupOption.Interval))
|
||||||
|
|
||||||
|
providers = append(providers, list...)
|
||||||
|
} else {
|
||||||
|
groupOption.Filter = ""
|
||||||
|
}
|
||||||
|
|
||||||
var group C.ProxyAdapter
|
var group C.ProxyAdapter
|
||||||
switch groupOption.Type {
|
switch groupOption.Type {
|
||||||
case "url-test":
|
case "url-test":
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
//go:build android && cmfa
|
|
||||||
|
|
||||||
package outboundgroup
|
package outboundgroup
|
||||||
|
|
||||||
import (
|
import (
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/metacubex/mihomo/constant/provider"
|
"github.com/Dreamacro/clash/constant/provider"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ProxyGroup interface {
|
type ProxyGroup interface {
|
||||||
@@ -3,33 +3,29 @@ package outboundgroup
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/Dreamacro/clash/adapter/outbound"
|
||||||
"github.com/metacubex/mihomo/adapter/outbound"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
"github.com/metacubex/mihomo/component/dialer"
|
"github.com/Dreamacro/clash/component/proxydialer"
|
||||||
"github.com/metacubex/mihomo/component/proxydialer"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
"github.com/Dreamacro/clash/constant/provider"
|
||||||
"github.com/metacubex/mihomo/constant/provider"
|
|
||||||
"github.com/metacubex/mihomo/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Relay struct {
|
type Relay struct {
|
||||||
*GroupBase
|
*GroupBase
|
||||||
Hidden bool
|
|
||||||
Icon string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (r *Relay) DialContext(ctx context.Context, metadata *C.Metadata) (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)
|
||||||
|
|
||||||
switch len(proxies) {
|
switch len(proxies) {
|
||||||
case 0:
|
case 0:
|
||||||
return outbound.NewDirect().DialContext(ctx, metadata)
|
return outbound.NewDirect().DialContext(ctx, metadata, r.Base.DialOptions(opts...)...)
|
||||||
case 1:
|
case 1:
|
||||||
return proxies[0].DialContext(ctx, metadata)
|
return proxies[0].DialContext(ctx, metadata, r.Base.DialOptions(opts...)...)
|
||||||
}
|
}
|
||||||
var d C.Dialer
|
var d C.Dialer
|
||||||
d = dialer.NewDialer()
|
d = dialer.NewDialer(r.Base.DialOptions(opts...)...)
|
||||||
for _, proxy := range proxies[:len(proxies)-1] {
|
for _, proxy := range proxies[:len(proxies)-1] {
|
||||||
d = proxydialer.New(proxy, d, false)
|
d = proxydialer.New(proxy, d, false)
|
||||||
}
|
}
|
||||||
@@ -49,18 +45,18 @@ func (r *Relay) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (r *Relay) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
func (r *Relay) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
||||||
proxies, chainProxies := r.proxies(metadata, true)
|
proxies, chainProxies := r.proxies(metadata, true)
|
||||||
|
|
||||||
switch len(proxies) {
|
switch len(proxies) {
|
||||||
case 0:
|
case 0:
|
||||||
return outbound.NewDirect().ListenPacketContext(ctx, metadata)
|
return outbound.NewDirect().ListenPacketContext(ctx, metadata, r.Base.DialOptions(opts...)...)
|
||||||
case 1:
|
case 1:
|
||||||
return proxies[0].ListenPacketContext(ctx, metadata)
|
return proxies[0].ListenPacketContext(ctx, metadata, r.Base.DialOptions(opts...)...)
|
||||||
}
|
}
|
||||||
|
|
||||||
var d C.Dialer
|
var d C.Dialer
|
||||||
d = dialer.NewDialer()
|
d = dialer.NewDialer(r.Base.DialOptions(opts...)...)
|
||||||
for _, proxy := range proxies[:len(proxies)-1] {
|
for _, proxy := range proxies[:len(proxies)-1] {
|
||||||
d = proxydialer.New(proxy, d, false)
|
d = proxydialer.New(proxy, d, false)
|
||||||
}
|
}
|
||||||
@@ -110,10 +106,8 @@ func (r *Relay) MarshalJSON() ([]byte, error) {
|
|||||||
all = append(all, proxy.Name())
|
all = append(all, proxy.Name())
|
||||||
}
|
}
|
||||||
return json.Marshal(map[string]any{
|
return json.Marshal(map[string]any{
|
||||||
"type": r.Type().String(),
|
"type": r.Type().String(),
|
||||||
"all": all,
|
"all": all,
|
||||||
"hidden": r.Hidden,
|
|
||||||
"icon": r.Icon,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,14 +144,18 @@ func (r *Relay) Addr() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewRelay(option *GroupCommonOption, providers []provider.ProxyProvider) *Relay {
|
func NewRelay(option *GroupCommonOption, providers []provider.ProxyProvider) *Relay {
|
||||||
log.Warnln("The group [%s] with relay type is deprecated, please using dialer-proxy instead", option.Name)
|
|
||||||
return &Relay{
|
return &Relay{
|
||||||
GroupBase: NewGroupBase(GroupBaseOption{
|
GroupBase: NewGroupBase(GroupBaseOption{
|
||||||
Name: option.Name,
|
outbound.BaseOption{
|
||||||
Type: C.Relay,
|
Name: option.Name,
|
||||||
Providers: providers,
|
Type: C.Relay,
|
||||||
|
Interface: option.Interface,
|
||||||
|
RoutingMark: option.RoutingMark,
|
||||||
|
},
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
providers,
|
||||||
}),
|
}),
|
||||||
Hidden: option.Hidden,
|
|
||||||
Icon: option.Icon,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,22 +5,21 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
"github.com/Dreamacro/clash/adapter/outbound"
|
||||||
"github.com/metacubex/mihomo/constant/provider"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
"github.com/Dreamacro/clash/constant/provider"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Selector struct {
|
type Selector struct {
|
||||||
*GroupBase
|
*GroupBase
|
||||||
disableUDP bool
|
disableUDP bool
|
||||||
selected string
|
selected string
|
||||||
testUrl string
|
|
||||||
Hidden bool
|
|
||||||
Icon string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (s *Selector) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) {
|
func (s *Selector) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
||||||
c, err := s.selectedProxy(true).DialContext(ctx, metadata)
|
c, err := s.selectedProxy(true).DialContext(ctx, metadata, s.Base.DialOptions(opts...)...)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
c.AppendToChains(s)
|
c.AppendToChains(s)
|
||||||
}
|
}
|
||||||
@@ -28,8 +27,8 @@ func (s *Selector) DialContext(ctx context.Context, metadata *C.Metadata) (C.Con
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (s *Selector) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
func (s *Selector) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
||||||
pc, err := s.selectedProxy(true).ListenPacketContext(ctx, metadata)
|
pc, err := s.selectedProxy(true).ListenPacketContext(ctx, metadata, s.Base.DialOptions(opts...)...)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
pc.AppendToChains(s)
|
pc.AppendToChains(s)
|
||||||
}
|
}
|
||||||
@@ -56,20 +55,11 @@ func (s *Selector) MarshalJSON() ([]byte, error) {
|
|||||||
for _, proxy := range s.GetProxies(false) {
|
for _, proxy := range s.GetProxies(false) {
|
||||||
all = append(all, proxy.Name())
|
all = append(all, proxy.Name())
|
||||||
}
|
}
|
||||||
// When testurl is the default value
|
|
||||||
// do not append a value to ensure that the web dashboard follows the settings of the dashboard
|
|
||||||
var url string
|
|
||||||
if s.testUrl != C.DefaultTestURL {
|
|
||||||
url = s.testUrl
|
|
||||||
}
|
|
||||||
|
|
||||||
return json.Marshal(map[string]any{
|
return json.Marshal(map[string]any{
|
||||||
"type": s.Type().String(),
|
"type": s.Type().String(),
|
||||||
"now": s.Now(),
|
"now": s.Now(),
|
||||||
"all": all,
|
"all": all,
|
||||||
"testUrl": url,
|
|
||||||
"hidden": s.Hidden,
|
|
||||||
"icon": s.Icon,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,19 +101,18 @@ func (s *Selector) selectedProxy(touch bool) C.Proxy {
|
|||||||
func NewSelector(option *GroupCommonOption, providers []provider.ProxyProvider) *Selector {
|
func NewSelector(option *GroupCommonOption, providers []provider.ProxyProvider) *Selector {
|
||||||
return &Selector{
|
return &Selector{
|
||||||
GroupBase: NewGroupBase(GroupBaseOption{
|
GroupBase: NewGroupBase(GroupBaseOption{
|
||||||
Name: option.Name,
|
outbound.BaseOption{
|
||||||
Type: C.Selector,
|
Name: option.Name,
|
||||||
Filter: option.Filter,
|
Type: C.Selector,
|
||||||
ExcludeFilter: option.ExcludeFilter,
|
Interface: option.Interface,
|
||||||
ExcludeType: option.ExcludeType,
|
RoutingMark: option.RoutingMark,
|
||||||
TestTimeout: option.TestTimeout,
|
},
|
||||||
MaxFailedTimes: option.MaxFailedTimes,
|
option.Filter,
|
||||||
Providers: providers,
|
option.ExcludeFilter,
|
||||||
|
option.ExcludeType,
|
||||||
|
providers,
|
||||||
}),
|
}),
|
||||||
selected: "COMPATIBLE",
|
selected: "COMPATIBLE",
|
||||||
disableUDP: option.DisableUDP,
|
disableUDP: option.DisableUDP,
|
||||||
testUrl: option.URL,
|
|
||||||
Hidden: option.Hidden,
|
|
||||||
Icon: option.Icon,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,12 +6,13 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/callback"
|
"github.com/Dreamacro/clash/adapter/outbound"
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
"github.com/Dreamacro/clash/common/callback"
|
||||||
"github.com/metacubex/mihomo/common/singledo"
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/Dreamacro/clash/common/singledo"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
"github.com/Dreamacro/clash/component/dialer"
|
||||||
"github.com/metacubex/mihomo/constant/provider"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
"github.com/Dreamacro/clash/constant/provider"
|
||||||
)
|
)
|
||||||
|
|
||||||
type urlTestOption func(*URLTest)
|
type urlTestOption func(*URLTest)
|
||||||
@@ -29,8 +30,6 @@ type URLTest struct {
|
|||||||
expectedStatus string
|
expectedStatus string
|
||||||
tolerance uint16
|
tolerance uint16
|
||||||
disableUDP bool
|
disableUDP bool
|
||||||
Hidden bool
|
|
||||||
Icon string
|
|
||||||
fastNode C.Proxy
|
fastNode C.Proxy
|
||||||
fastSingle *singledo.Single[C.Proxy]
|
fastSingle *singledo.Single[C.Proxy]
|
||||||
}
|
}
|
||||||
@@ -50,23 +49,23 @@ func (u *URLTest) Set(name string) error {
|
|||||||
if p == nil {
|
if p == nil {
|
||||||
return errors.New("proxy not exist")
|
return errors.New("proxy not exist")
|
||||||
}
|
}
|
||||||
u.ForceSet(name)
|
u.selected = name
|
||||||
|
u.fast(false)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *URLTest) ForceSet(name string) {
|
func (u *URLTest) ForceSet(name string) {
|
||||||
u.selected = name
|
u.selected = name
|
||||||
u.fastSingle.Reset()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (u *URLTest) DialContext(ctx context.Context, metadata *C.Metadata) (c C.Conn, err error) {
|
func (u *URLTest) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (c C.Conn, err error) {
|
||||||
proxy := u.fast(true)
|
proxy := u.fast(true)
|
||||||
c, err = proxy.DialContext(ctx, metadata)
|
c, err = proxy.DialContext(ctx, metadata, u.Base.DialOptions(opts...)...)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
c.AppendToChains(u)
|
c.AppendToChains(u)
|
||||||
} else {
|
} else {
|
||||||
u.onDialFailed(proxy.Type(), err, u.healthCheck)
|
u.onDialFailed(proxy.Type(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if N.NeedHandshake(c) {
|
if N.NeedHandshake(c) {
|
||||||
@@ -74,7 +73,7 @@ func (u *URLTest) DialContext(ctx context.Context, metadata *C.Metadata) (c C.Co
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
u.onDialSuccess()
|
u.onDialSuccess()
|
||||||
} else {
|
} else {
|
||||||
u.onDialFailed(proxy.Type(), err, u.healthCheck)
|
u.onDialFailed(proxy.Type(), err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -83,13 +82,10 @@ func (u *URLTest) DialContext(ctx context.Context, metadata *C.Metadata) (c C.Co
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ListenPacketContext implements C.ProxyAdapter
|
// ListenPacketContext implements C.ProxyAdapter
|
||||||
func (u *URLTest) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) {
|
func (u *URLTest) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
||||||
proxy := u.fast(true)
|
pc, err := u.fast(true).ListenPacketContext(ctx, metadata, u.Base.DialOptions(opts...)...)
|
||||||
pc, err := proxy.ListenPacketContext(ctx, metadata)
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
pc.AppendToChains(u)
|
pc.AppendToChains(u)
|
||||||
} else {
|
|
||||||
u.onDialFailed(proxy.Type(), err, u.healthCheck)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return pc, err
|
return pc, err
|
||||||
@@ -100,29 +96,25 @@ func (u *URLTest) Unwrap(metadata *C.Metadata, touch bool) C.Proxy {
|
|||||||
return u.fast(touch)
|
return u.fast(touch)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *URLTest) healthCheck() {
|
|
||||||
u.fastSingle.Reset()
|
|
||||||
u.GroupBase.healthCheck()
|
|
||||||
u.fastSingle.Reset()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *URLTest) fast(touch bool) C.Proxy {
|
func (u *URLTest) fast(touch bool) C.Proxy {
|
||||||
elm, _, shared := u.fastSingle.Do(func() (C.Proxy, error) {
|
|
||||||
proxies := u.GetProxies(touch)
|
proxies := u.GetProxies(touch)
|
||||||
if u.selected != "" {
|
if u.selected != "" {
|
||||||
for _, proxy := range proxies {
|
for _, proxy := range proxies {
|
||||||
if !proxy.AliveForTestUrl(u.testUrl) {
|
if !proxy.Alive() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if proxy.Name() == u.selected {
|
if proxy.Name() == u.selected {
|
||||||
u.fastNode = proxy
|
u.fastNode = proxy
|
||||||
return proxy, nil
|
return proxy
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
elm, _, shared := u.fastSingle.Do(func() (C.Proxy, error) {
|
||||||
fast := proxies[0]
|
fast := proxies[0]
|
||||||
minDelay := fast.LastDelayForTestUrl(u.testUrl)
|
// min := fast.LastDelay()
|
||||||
|
min := fast.LastDelayForTestUrl(u.testUrl)
|
||||||
fastNotExist := true
|
fastNotExist := true
|
||||||
|
|
||||||
for _, proxy := range proxies[1:] {
|
for _, proxy := range proxies[1:] {
|
||||||
@@ -130,18 +122,21 @@ func (u *URLTest) fast(touch bool) C.Proxy {
|
|||||||
fastNotExist = false
|
fastNotExist = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if !proxy.Alive() {
|
||||||
if !proxy.AliveForTestUrl(u.testUrl) {
|
if !proxy.AliveForTestUrl(u.testUrl) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// delay := proxy.LastDelay()
|
||||||
delay := proxy.LastDelayForTestUrl(u.testUrl)
|
delay := proxy.LastDelayForTestUrl(u.testUrl)
|
||||||
if delay < minDelay {
|
if delay < min {
|
||||||
fast = proxy
|
fast = proxy
|
||||||
minDelay = delay
|
min = delay
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
// tolerance
|
// tolerance
|
||||||
|
// if u.fastNode == nil || fastNotExist || !u.fastNode.Alive() || u.fastNode.LastDelay() > fast.LastDelay()+u.tolerance {
|
||||||
if u.fastNode == nil || fastNotExist || !u.fastNode.AliveForTestUrl(u.testUrl) || u.fastNode.LastDelayForTestUrl(u.testUrl) > fast.LastDelayForTestUrl(u.testUrl)+u.tolerance {
|
if u.fastNode == nil || fastNotExist || !u.fastNode.AliveForTestUrl(u.testUrl) || u.fastNode.LastDelayForTestUrl(u.testUrl) > fast.LastDelayForTestUrl(u.testUrl)+u.tolerance {
|
||||||
u.fastNode = fast
|
u.fastNode = fast
|
||||||
}
|
}
|
||||||
@@ -174,21 +169,14 @@ func (u *URLTest) MarshalJSON() ([]byte, error) {
|
|||||||
all = append(all, proxy.Name())
|
all = append(all, proxy.Name())
|
||||||
}
|
}
|
||||||
return json.Marshal(map[string]any{
|
return json.Marshal(map[string]any{
|
||||||
"type": u.Type().String(),
|
"type": u.Type().String(),
|
||||||
"now": u.Now(),
|
"now": u.Now(),
|
||||||
"all": all,
|
"all": all,
|
||||||
"testUrl": u.testUrl,
|
"testUrl": u.testUrl,
|
||||||
"expectedStatus": u.expectedStatus,
|
"expected": u.expectedStatus,
|
||||||
"fixed": u.selected,
|
|
||||||
"hidden": u.Hidden,
|
|
||||||
"icon": u.Icon,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *URLTest) URLTest(ctx context.Context, url string, expectedStatus utils.IntRanges[uint16]) (map[string]uint16, error) {
|
|
||||||
return u.GroupBase.URLTest(ctx, u.testUrl, expectedStatus)
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseURLTestOption(config map[string]any) []urlTestOption {
|
func parseURLTestOption(config map[string]any) []urlTestOption {
|
||||||
opts := []urlTestOption{}
|
opts := []urlTestOption{}
|
||||||
|
|
||||||
@@ -205,21 +193,22 @@ func parseURLTestOption(config map[string]any) []urlTestOption {
|
|||||||
func NewURLTest(option *GroupCommonOption, providers []provider.ProxyProvider, options ...urlTestOption) *URLTest {
|
func NewURLTest(option *GroupCommonOption, providers []provider.ProxyProvider, options ...urlTestOption) *URLTest {
|
||||||
urlTest := &URLTest{
|
urlTest := &URLTest{
|
||||||
GroupBase: NewGroupBase(GroupBaseOption{
|
GroupBase: NewGroupBase(GroupBaseOption{
|
||||||
Name: option.Name,
|
outbound.BaseOption{
|
||||||
Type: C.URLTest,
|
Name: option.Name,
|
||||||
Filter: option.Filter,
|
Type: C.URLTest,
|
||||||
ExcludeFilter: option.ExcludeFilter,
|
Interface: option.Interface,
|
||||||
ExcludeType: option.ExcludeType,
|
RoutingMark: option.RoutingMark,
|
||||||
TestTimeout: option.TestTimeout,
|
},
|
||||||
MaxFailedTimes: option.MaxFailedTimes,
|
|
||||||
Providers: providers,
|
option.Filter,
|
||||||
|
option.ExcludeFilter,
|
||||||
|
option.ExcludeType,
|
||||||
|
providers,
|
||||||
}),
|
}),
|
||||||
fastSingle: singledo.NewSingle[C.Proxy](time.Second * 10),
|
fastSingle: singledo.NewSingle[C.Proxy](time.Second * 10),
|
||||||
disableUDP: option.DisableUDP,
|
disableUDP: option.DisableUDP,
|
||||||
testUrl: option.URL,
|
testUrl: option.URL,
|
||||||
expectedStatus: option.ExpectedStatus,
|
expectedStatus: option.ExpectedStatus,
|
||||||
Hidden: option.Hidden,
|
|
||||||
Icon: option.Icon,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, option := range options {
|
for _, option := range options {
|
||||||
|
|||||||
@@ -4,7 +4,3 @@ type SelectAble interface {
|
|||||||
Set(string) error
|
Set(string) error
|
||||||
ForceSet(name string)
|
ForceSet(name string)
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ SelectAble = (*Fallback)(nil)
|
|
||||||
var _ SelectAble = (*URLTest)(nil)
|
|
||||||
var _ SelectAble = (*Selector)(nil)
|
|
||||||
|
|||||||
@@ -3,9 +3,11 @@ package adapter
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/adapter/outbound"
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
||||||
"github.com/metacubex/mihomo/common/structure"
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
"github.com/Dreamacro/clash/adapter/outbound"
|
||||||
|
"github.com/Dreamacro/clash/common/structure"
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ParseProxy(mapping map[string]any) (C.Proxy, error) {
|
func ParseProxy(mapping map[string]any) (C.Proxy, error) {
|
||||||
@@ -16,12 +18,12 @@ func ParseProxy(mapping map[string]any) (C.Proxy, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
proxy outbound.ProxyAdapter
|
proxy C.ProxyAdapter
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
switch proxyType {
|
switch proxyType {
|
||||||
case "ss":
|
case "ss":
|
||||||
ssOption := &outbound.ShadowSocksOption{}
|
ssOption := &outbound.ShadowSocksOption{ClientFingerprint: tlsC.GetGlobalFingerprint()}
|
||||||
err = decoder.Decode(mapping, ssOption)
|
err = decoder.Decode(mapping, ssOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
@@ -54,6 +56,7 @@ func ParseProxy(mapping map[string]any) (C.Proxy, error) {
|
|||||||
Method: "GET",
|
Method: "GET",
|
||||||
Path: []string{"/"},
|
Path: []string{"/"},
|
||||||
},
|
},
|
||||||
|
ClientFingerprint: tlsC.GetGlobalFingerprint(),
|
||||||
}
|
}
|
||||||
|
|
||||||
err = decoder.Decode(mapping, vmessOption)
|
err = decoder.Decode(mapping, vmessOption)
|
||||||
@@ -62,7 +65,7 @@ func ParseProxy(mapping map[string]any) (C.Proxy, error) {
|
|||||||
}
|
}
|
||||||
proxy, err = outbound.NewVmess(*vmessOption)
|
proxy, err = outbound.NewVmess(*vmessOption)
|
||||||
case "vless":
|
case "vless":
|
||||||
vlessOption := &outbound.VlessOption{}
|
vlessOption := &outbound.VlessOption{ClientFingerprint: tlsC.GetGlobalFingerprint()}
|
||||||
err = decoder.Decode(mapping, vlessOption)
|
err = decoder.Decode(mapping, vlessOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
@@ -76,7 +79,7 @@ func ParseProxy(mapping map[string]any) (C.Proxy, error) {
|
|||||||
}
|
}
|
||||||
proxy, err = outbound.NewSnell(*snellOption)
|
proxy, err = outbound.NewSnell(*snellOption)
|
||||||
case "trojan":
|
case "trojan":
|
||||||
trojanOption := &outbound.TrojanOption{}
|
trojanOption := &outbound.TrojanOption{ClientFingerprint: tlsC.GetGlobalFingerprint()}
|
||||||
err = decoder.Decode(mapping, trojanOption)
|
err = decoder.Decode(mapping, trojanOption)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
@@ -117,13 +120,6 @@ func ParseProxy(mapping map[string]any) (C.Proxy, error) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
proxy = outbound.NewDirectWithOption(*directOption)
|
proxy = outbound.NewDirectWithOption(*directOption)
|
||||||
case "dns":
|
|
||||||
dnsOptions := &outbound.DnsOption{}
|
|
||||||
err = decoder.Decode(mapping, dnsOptions)
|
|
||||||
if err != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
proxy = outbound.NewDnsWithOption(*dnsOptions)
|
|
||||||
case "reject":
|
case "reject":
|
||||||
rejectOption := &outbound.RejectOption{}
|
rejectOption := &outbound.RejectOption{}
|
||||||
err = decoder.Decode(mapping, rejectOption)
|
err = decoder.Decode(mapping, rejectOption)
|
||||||
@@ -131,27 +127,6 @@ func ParseProxy(mapping map[string]any) (C.Proxy, error) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
proxy = outbound.NewRejectWithOption(*rejectOption)
|
proxy = outbound.NewRejectWithOption(*rejectOption)
|
||||||
case "ssh":
|
|
||||||
sshOption := &outbound.SshOption{}
|
|
||||||
err = decoder.Decode(mapping, sshOption)
|
|
||||||
if err != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
proxy, err = outbound.NewSsh(*sshOption)
|
|
||||||
case "mieru":
|
|
||||||
mieruOption := &outbound.MieruOption{}
|
|
||||||
err = decoder.Decode(mapping, mieruOption)
|
|
||||||
if err != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
proxy, err = outbound.NewMieru(*mieruOption)
|
|
||||||
case "anytls":
|
|
||||||
anytlsOption := &outbound.AnyTLSOption{}
|
|
||||||
err = decoder.Decode(mapping, anytlsOption)
|
|
||||||
if err != nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
proxy, err = outbound.NewAnyTLS(*anytlsOption)
|
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unsupport proxy type: %s", proxyType)
|
return nil, fmt.Errorf("unsupport proxy type: %s", proxyType)
|
||||||
}
|
}
|
||||||
@@ -167,13 +142,12 @@ func ParseProxy(mapping map[string]any) (C.Proxy, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if muxOption.Enabled {
|
if muxOption.Enabled {
|
||||||
proxy, err = outbound.NewSingMux(*muxOption, proxy)
|
proxy, err = outbound.NewSingMux(*muxOption, proxy, proxy.(outbound.ProxyBase))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
proxy = outbound.NewAutoCloseProxyAdapter(proxy)
|
|
||||||
return NewProxy(proxy), nil
|
return NewProxy(proxy), nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,14 +6,19 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/atomic"
|
"github.com/Dreamacro/clash/common/atomic"
|
||||||
"github.com/metacubex/mihomo/common/singledo"
|
"github.com/Dreamacro/clash/common/batch"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/Dreamacro/clash/common/singledo"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
"github.com/Dreamacro/clash/common/utils"
|
||||||
"github.com/metacubex/mihomo/log"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
"github.com/Dreamacro/clash/log"
|
||||||
|
|
||||||
"github.com/dlclark/regexp2"
|
"github.com/dlclark/regexp2"
|
||||||
"golang.org/x/sync/errgroup"
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultURLTestTimeout = time.Second * 5
|
||||||
|
defaultURLTestURL = "https://www.gstatic.com/generate_204"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HealthCheckOption struct {
|
type HealthCheckOption struct {
|
||||||
@@ -27,23 +32,27 @@ type extraOption struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type HealthCheck struct {
|
type HealthCheck struct {
|
||||||
ctx context.Context
|
|
||||||
ctxCancel context.CancelFunc
|
|
||||||
url string
|
url string
|
||||||
extra map[string]*extraOption
|
extra map[string]*extraOption
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
|
started atomic.Bool
|
||||||
proxies []C.Proxy
|
proxies []C.Proxy
|
||||||
interval time.Duration
|
interval time.Duration
|
||||||
lazy bool
|
lazy bool
|
||||||
expectedStatus utils.IntRanges[uint16]
|
expectedStatus utils.IntRanges[uint16]
|
||||||
lastTouch atomic.TypedValue[time.Time]
|
lastTouch atomic.TypedValue[time.Time]
|
||||||
|
done chan struct{}
|
||||||
singleDo *singledo.Single[struct{}]
|
singleDo *singledo.Single[struct{}]
|
||||||
timeout time.Duration
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hc *HealthCheck) process() {
|
func (hc *HealthCheck) process() {
|
||||||
|
if hc.started.Load() {
|
||||||
|
log.Warnln("Skip start health check timer due to it's started")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
ticker := time.NewTicker(hc.interval)
|
ticker := time.NewTicker(hc.interval)
|
||||||
go hc.check()
|
hc.start()
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
@@ -54,14 +63,15 @@ func (hc *HealthCheck) process() {
|
|||||||
} else {
|
} else {
|
||||||
log.Debugln("Skip once health check because we are lazy")
|
log.Debugln("Skip once health check because we are lazy")
|
||||||
}
|
}
|
||||||
case <-hc.ctx.Done():
|
case <-hc.done:
|
||||||
ticker.Stop()
|
ticker.Stop()
|
||||||
|
hc.stop()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hc *HealthCheck) setProxies(proxies []C.Proxy) {
|
func (hc *HealthCheck) setProxy(proxies []C.Proxy) {
|
||||||
hc.proxies = proxies
|
hc.proxies = proxies
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,9 +105,19 @@ func (hc *HealthCheck) registerHealthCheckTask(url string, expectedStatus utils.
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// due to the time-consuming nature of health checks, a maximum of defaultMaxTestURLNum URLs can be set for testing
|
||||||
|
if len(hc.extra) > C.DefaultMaxHealthCheckUrlNum {
|
||||||
|
log.Debugln("skip add url: %s to health check because it has reached the maximum limit: %d", url, C.DefaultMaxHealthCheckUrlNum)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
option := &extraOption{filters: map[string]struct{}{}, expectedStatus: expectedStatus}
|
option := &extraOption{filters: map[string]struct{}{}, expectedStatus: expectedStatus}
|
||||||
splitAndAddFiltersToExtra(filter, option)
|
splitAndAddFiltersToExtra(filter, option)
|
||||||
hc.extra[url] = option
|
hc.extra[url] = option
|
||||||
|
|
||||||
|
if hc.auto() && !hc.started.Load() {
|
||||||
|
go hc.process()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func splitAndAddFiltersToExtra(filter string, option *extraOption) {
|
func splitAndAddFiltersToExtra(filter string, option *extraOption) {
|
||||||
@@ -120,7 +140,16 @@ func (hc *HealthCheck) touch() {
|
|||||||
hc.lastTouch.Store(time.Now())
|
hc.lastTouch.Store(time.Now())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (hc *HealthCheck) start() {
|
||||||
|
hc.started.Store(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hc *HealthCheck) stop() {
|
||||||
|
hc.started.Store(false)
|
||||||
|
}
|
||||||
|
|
||||||
func (hc *HealthCheck) check() {
|
func (hc *HealthCheck) check() {
|
||||||
|
|
||||||
if len(hc.proxies) == 0 {
|
if len(hc.proxies) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -128,8 +157,7 @@ func (hc *HealthCheck) check() {
|
|||||||
_, _, _ = hc.singleDo.Do(func() (struct{}, error) {
|
_, _, _ = hc.singleDo.Do(func() (struct{}, error) {
|
||||||
id := utils.NewUUIDV4().String()
|
id := utils.NewUUIDV4().String()
|
||||||
log.Debugln("Start New Health Checking {%s}", id)
|
log.Debugln("Start New Health Checking {%s}", id)
|
||||||
b := new(errgroup.Group)
|
b, _ := batch.New[bool](context.Background(), batch.WithConcurrencyNum[bool](10))
|
||||||
b.SetLimit(10)
|
|
||||||
|
|
||||||
// execute default health check
|
// execute default health check
|
||||||
option := &extraOption{filters: nil, expectedStatus: hc.expectedStatus}
|
option := &extraOption{filters: nil, expectedStatus: hc.expectedStatus}
|
||||||
@@ -141,13 +169,13 @@ func (hc *HealthCheck) check() {
|
|||||||
hc.execute(b, url, id, option)
|
hc.execute(b, url, id, option)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ = b.Wait()
|
b.Wait()
|
||||||
log.Debugln("Finish A Health Checking {%s}", id)
|
log.Debugln("Finish A Health Checking {%s}", id)
|
||||||
return struct{}{}, nil
|
return struct{}{}, nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hc *HealthCheck) execute(b *errgroup.Group, url, uid string, option *extraOption) {
|
func (hc *HealthCheck) execute(b *batch.Batch[bool], url, uid string, option *extraOption) {
|
||||||
url = strings.TrimSpace(url)
|
url = strings.TrimSpace(url)
|
||||||
if len(url) == 0 {
|
if len(url) == 0 {
|
||||||
log.Debugln("Health Check has been skipped due to testUrl is empty, {%s}", uid)
|
log.Debugln("Health Check has been skipped due to testUrl is empty, {%s}", uid)
|
||||||
@@ -155,8 +183,13 @@ func (hc *HealthCheck) execute(b *errgroup.Group, url, uid string, option *extra
|
|||||||
}
|
}
|
||||||
|
|
||||||
var filterReg *regexp2.Regexp
|
var filterReg *regexp2.Regexp
|
||||||
|
var store = C.OriginalHistory
|
||||||
var expectedStatus utils.IntRanges[uint16]
|
var expectedStatus utils.IntRanges[uint16]
|
||||||
if option != nil {
|
if option != nil {
|
||||||
|
if url != hc.url {
|
||||||
|
store = C.ExtraHistory
|
||||||
|
}
|
||||||
|
|
||||||
expectedStatus = option.expectedStatus
|
expectedStatus = option.expectedStatus
|
||||||
if len(option.filters) != 0 {
|
if len(option.filters) != 0 {
|
||||||
filters := make([]string, 0, len(option.filters))
|
filters := make([]string, 0, len(option.filters))
|
||||||
@@ -164,54 +197,49 @@ func (hc *HealthCheck) execute(b *errgroup.Group, url, uid string, option *extra
|
|||||||
filters = append(filters, filter)
|
filters = append(filters, filter)
|
||||||
}
|
}
|
||||||
|
|
||||||
filterReg = regexp2.MustCompile(strings.Join(filters, "|"), regexp2.None)
|
filterReg = regexp2.MustCompile(strings.Join(filters, "|"), 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, proxy := range hc.proxies {
|
for _, proxy := range hc.proxies {
|
||||||
// skip proxies that do not require health check
|
// skip proxies that do not require health check
|
||||||
if filterReg != nil {
|
if filterReg != nil {
|
||||||
if match, _ := filterReg.MatchString(proxy.Name()); !match {
|
if match, _ := filterReg.FindStringMatch(proxy.Name()); match == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
p := proxy
|
p := proxy
|
||||||
b.Go(func() error {
|
b.Go(p.Name(), func() (bool, error) {
|
||||||
ctx, cancel := context.WithTimeout(hc.ctx, hc.timeout)
|
ctx, cancel := context.WithTimeout(context.Background(), defaultURLTestTimeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
log.Debugln("Health Checking, proxy: %s, url: %s, id: {%s}", p.Name(), url, uid)
|
log.Debugln("Health Checking, proxy: %s, url: %s, id: {%s}", p.Name(), url, uid)
|
||||||
_, _ = p.URLTest(ctx, url, expectedStatus)
|
_, _ = p.URLTest(ctx, url, expectedStatus, store)
|
||||||
log.Debugln("Health Checked, proxy: %s, url: %s, alive: %t, delay: %d ms uid: {%s}", p.Name(), url, p.AliveForTestUrl(url), p.LastDelayForTestUrl(url), uid)
|
log.Debugln("Health Checked, proxy: %s, url: %s, alive: %t, delay: %d ms uid: {%s}", p.Name(), url, p.AliveForTestUrl(url), p.LastDelayForTestUrl(url), uid)
|
||||||
return nil
|
return false, nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hc *HealthCheck) close() {
|
func (hc *HealthCheck) close() {
|
||||||
hc.ctxCancel()
|
hc.done <- struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHealthCheck(proxies []C.Proxy, url string, timeout uint, interval uint, lazy bool, expectedStatus utils.IntRanges[uint16]) *HealthCheck {
|
func NewHealthCheck(proxies []C.Proxy, url string, interval uint, lazy bool, expectedStatus utils.IntRanges[uint16]) *HealthCheck {
|
||||||
if url == "" {
|
if len(url) == 0 {
|
||||||
expectedStatus = nil
|
|
||||||
interval = 0
|
interval = 0
|
||||||
|
expectedStatus = nil
|
||||||
|
url = defaultURLTestURL
|
||||||
}
|
}
|
||||||
if timeout == 0 {
|
|
||||||
timeout = 5000
|
|
||||||
}
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
|
|
||||||
return &HealthCheck{
|
return &HealthCheck{
|
||||||
ctx: ctx,
|
|
||||||
ctxCancel: cancel,
|
|
||||||
proxies: proxies,
|
proxies: proxies,
|
||||||
url: url,
|
url: url,
|
||||||
timeout: time.Duration(timeout) * time.Millisecond,
|
|
||||||
extra: map[string]*extraOption{},
|
extra: map[string]*extraOption{},
|
||||||
interval: time.Duration(interval) * time.Second,
|
interval: time.Duration(interval) * time.Second,
|
||||||
lazy: lazy,
|
lazy: lazy,
|
||||||
expectedStatus: expectedStatus,
|
expectedStatus: expectedStatus,
|
||||||
|
done: make(chan struct{}, 1),
|
||||||
singleDo: singledo.NewSingle[struct{}](time.Second),
|
singleDo: singledo.NewSingle[struct{}](time.Second),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,76 +1,40 @@
|
|||||||
package provider
|
package provider
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/structure"
|
"github.com/Dreamacro/clash/common/structure"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/Dreamacro/clash/common/utils"
|
||||||
"github.com/metacubex/mihomo/component/resource"
|
"github.com/Dreamacro/clash/component/resource"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
types "github.com/metacubex/mihomo/constant/provider"
|
types "github.com/Dreamacro/clash/constant/provider"
|
||||||
|
|
||||||
"github.com/dlclark/regexp2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
errVehicleType = errors.New("unsupport vehicle type")
|
errVehicleType = errors.New("unsupport vehicle type")
|
||||||
|
errSubPath = errors.New("path is not subpath of home directory")
|
||||||
)
|
)
|
||||||
|
|
||||||
type healthCheckSchema struct {
|
type healthCheckSchema struct {
|
||||||
Enable bool `provider:"enable"`
|
Enable bool `provider:"enable"`
|
||||||
URL string `provider:"url"`
|
URL string `provider:"url"`
|
||||||
Interval int `provider:"interval"`
|
Interval int `provider:"interval"`
|
||||||
TestTimeout int `provider:"timeout,omitempty"`
|
|
||||||
Lazy bool `provider:"lazy,omitempty"`
|
Lazy bool `provider:"lazy,omitempty"`
|
||||||
ExpectedStatus string `provider:"expected-status,omitempty"`
|
ExpectedStatus string `provider:"expected-status,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type OverrideProxyNameSchema struct {
|
|
||||||
// matching expression for regex replacement
|
|
||||||
Pattern *regexp2.Regexp `provider:"pattern"`
|
|
||||||
// the new content after regex matching
|
|
||||||
Target string `provider:"target"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ encoding.TextUnmarshaler = (*regexp2.Regexp)(nil) // ensure *regexp2.Regexp can decode direct by structure package
|
|
||||||
|
|
||||||
type OverrideSchema struct {
|
|
||||||
TFO *bool `provider:"tfo,omitempty"`
|
|
||||||
MPTcp *bool `provider:"mptcp,omitempty"`
|
|
||||||
UDP *bool `provider:"udp,omitempty"`
|
|
||||||
UDPOverTCP *bool `provider:"udp-over-tcp,omitempty"`
|
|
||||||
Up *string `provider:"up,omitempty"`
|
|
||||||
Down *string `provider:"down,omitempty"`
|
|
||||||
DialerProxy *string `provider:"dialer-proxy,omitempty"`
|
|
||||||
SkipCertVerify *bool `provider:"skip-cert-verify,omitempty"`
|
|
||||||
Interface *string `provider:"interface-name,omitempty"`
|
|
||||||
RoutingMark *int `provider:"routing-mark,omitempty"`
|
|
||||||
IPVersion *string `provider:"ip-version,omitempty"`
|
|
||||||
AdditionalPrefix *string `provider:"additional-prefix,omitempty"`
|
|
||||||
AdditionalSuffix *string `provider:"additional-suffix,omitempty"`
|
|
||||||
|
|
||||||
ProxyName []OverrideProxyNameSchema `provider:"proxy-name,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type proxyProviderSchema struct {
|
type proxyProviderSchema struct {
|
||||||
Type string `provider:"type"`
|
Type string `provider:"type"`
|
||||||
Path string `provider:"path,omitempty"`
|
Path string `provider:"path,omitempty"`
|
||||||
URL string `provider:"url,omitempty"`
|
URL string `provider:"url,omitempty"`
|
||||||
Proxy string `provider:"proxy,omitempty"`
|
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"`
|
||||||
ExcludeType string `provider:"exclude-type,omitempty"`
|
DialerProxy string `provider:"dialer-proxy,omitempty"`
|
||||||
DialerProxy string `provider:"dialer-proxy,omitempty"`
|
HealthCheck healthCheckSchema `provider:"health-check,omitempty"`
|
||||||
SizeLimit int64 `provider:"size-limit,omitempty"`
|
|
||||||
Payload []map[string]any `provider:"payload,omitempty"`
|
|
||||||
|
|
||||||
HealthCheck healthCheckSchema `provider:"health-check,omitempty"`
|
|
||||||
Override OverrideSchema `provider:"override,omitempty"`
|
|
||||||
Header map[string][]string `provider:"header,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvider, error) {
|
func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvider, error) {
|
||||||
@@ -85,24 +49,16 @@ func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvide
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedStatus, err := utils.NewUnsignedRanges[uint16](schema.HealthCheck.ExpectedStatus)
|
expectedStatus, err := utils.NewIntRanges[uint16](schema.HealthCheck.ExpectedStatus)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var hcInterval uint
|
var hcInterval uint
|
||||||
if schema.HealthCheck.Enable {
|
if schema.HealthCheck.Enable {
|
||||||
if schema.HealthCheck.Interval == 0 {
|
|
||||||
schema.HealthCheck.Interval = 300
|
|
||||||
}
|
|
||||||
hcInterval = uint(schema.HealthCheck.Interval)
|
hcInterval = uint(schema.HealthCheck.Interval)
|
||||||
}
|
}
|
||||||
hc := NewHealthCheck([]C.Proxy{}, schema.HealthCheck.URL, uint(schema.HealthCheck.TestTimeout), hcInterval, schema.HealthCheck.Lazy, expectedStatus)
|
hc := NewHealthCheck([]C.Proxy{}, schema.HealthCheck.URL, hcInterval, schema.HealthCheck.Lazy, expectedStatus)
|
||||||
|
|
||||||
parser, err := NewProxiesParser(schema.Filter, schema.ExcludeFilter, schema.ExcludeType, schema.DialerProxy, schema.Override)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var vehicle types.Vehicle
|
var vehicle types.Vehicle
|
||||||
switch schema.Type {
|
switch schema.Type {
|
||||||
@@ -110,21 +66,22 @@ func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvide
|
|||||||
path := C.Path.Resolve(schema.Path)
|
path := C.Path.Resolve(schema.Path)
|
||||||
vehicle = resource.NewFileVehicle(path)
|
vehicle = resource.NewFileVehicle(path)
|
||||||
case "http":
|
case "http":
|
||||||
path := C.Path.GetPathByHash("proxies", schema.URL)
|
|
||||||
if schema.Path != "" {
|
if schema.Path != "" {
|
||||||
path = C.Path.Resolve(schema.Path)
|
path := C.Path.Resolve(schema.Path)
|
||||||
if !C.Path.IsSafePath(path) {
|
vehicle = resource.NewHTTPVehicle(schema.URL, path)
|
||||||
return nil, C.Path.ErrNotSafePath(path)
|
} else {
|
||||||
}
|
path := C.Path.GetPathByHash("proxies", schema.URL)
|
||||||
|
vehicle = resource.NewHTTPVehicle(schema.URL, path)
|
||||||
}
|
}
|
||||||
vehicle = resource.NewHTTPVehicle(schema.URL, path, schema.Proxy, schema.Header, resource.DefaultHttpTimeout, schema.SizeLimit)
|
|
||||||
case "inline":
|
|
||||||
return NewInlineProvider(name, schema.Payload, parser, hc)
|
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("%w: %s", errVehicleType, schema.Type)
|
return nil, fmt.Errorf("%w: %s", errVehicleType, schema.Type)
|
||||||
}
|
}
|
||||||
|
|
||||||
interval := time.Duration(uint(schema.Interval)) * time.Second
|
interval := time.Duration(uint(schema.Interval)) * time.Second
|
||||||
|
filter := schema.Filter
|
||||||
|
excludeFilter := schema.ExcludeFilter
|
||||||
|
excludeType := schema.ExcludeType
|
||||||
|
dialerProxy := schema.DialerProxy
|
||||||
|
|
||||||
return NewProxySetProvider(name, interval, schema.Payload, parser, vehicle, hc)
|
return NewProxySetProvider(name, interval, filter, excludeFilter, excludeType, dialerProxy, vehicle, hc)
|
||||||
}
|
}
|
||||||
|
|||||||
34
adapter/provider/patch.go
Normal file
34
adapter/provider/patch.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
suspended bool
|
||||||
|
)
|
||||||
|
|
||||||
|
type UpdatableProvider interface {
|
||||||
|
UpdatedAt() time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pp *proxySetProvider) UpdatedAt() time.Time {
|
||||||
|
return pp.Fetcher.UpdatedAt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pp *proxySetProvider) Close() error {
|
||||||
|
pp.healthCheck.close()
|
||||||
|
pp.Fetcher.Destroy()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cp *compatibleProvider) Close() error {
|
||||||
|
cp.healthCheck.close()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Suspend(s bool) {
|
||||||
|
suspended = s
|
||||||
|
}
|
||||||
@@ -1,23 +1,24 @@
|
|||||||
package provider
|
package provider
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/adapter"
|
"github.com/Dreamacro/clash/adapter"
|
||||||
"github.com/metacubex/mihomo/common/convert"
|
"github.com/Dreamacro/clash/common/convert"
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/Dreamacro/clash/common/utils"
|
||||||
"github.com/metacubex/mihomo/component/profile/cachefile"
|
clashHttp "github.com/Dreamacro/clash/component/http"
|
||||||
"github.com/metacubex/mihomo/component/resource"
|
"github.com/Dreamacro/clash/component/resource"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
types "github.com/metacubex/mihomo/constant/provider"
|
types "github.com/Dreamacro/clash/constant/provider"
|
||||||
"github.com/metacubex/mihomo/tunnel/statistic"
|
"github.com/Dreamacro/clash/log"
|
||||||
|
"github.com/Dreamacro/clash/tunnel/statistic"
|
||||||
|
|
||||||
"github.com/dlclark/regexp2"
|
"github.com/dlclark/regexp2"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
@@ -31,129 +32,120 @@ type ProxySchema struct {
|
|||||||
Proxies []map[string]any `yaml:"proxies"`
|
Proxies []map[string]any `yaml:"proxies"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type providerForApi struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
Type string `json:"type"`
|
|
||||||
VehicleType string `json:"vehicleType"`
|
|
||||||
Proxies []C.Proxy `json:"proxies"`
|
|
||||||
TestUrl string `json:"testUrl"`
|
|
||||||
ExpectedStatus string `json:"expectedStatus"`
|
|
||||||
UpdatedAt time.Time `json:"updatedAt,omitempty"`
|
|
||||||
SubscriptionInfo *SubscriptionInfo `json:"subscriptionInfo,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type baseProvider struct {
|
|
||||||
name string
|
|
||||||
proxies []C.Proxy
|
|
||||||
healthCheck *HealthCheck
|
|
||||||
version uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bp *baseProvider) Name() string {
|
|
||||||
return bp.name
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bp *baseProvider) Version() uint32 {
|
|
||||||
return bp.version
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bp *baseProvider) Initial() error {
|
|
||||||
if bp.healthCheck.auto() {
|
|
||||||
go bp.healthCheck.process()
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bp *baseProvider) HealthCheck() {
|
|
||||||
bp.healthCheck.check()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bp *baseProvider) Type() types.ProviderType {
|
|
||||||
return types.Proxy
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bp *baseProvider) Proxies() []C.Proxy {
|
|
||||||
return bp.proxies
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bp *baseProvider) Count() int {
|
|
||||||
return len(bp.proxies)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bp *baseProvider) Touch() {
|
|
||||||
bp.healthCheck.touch()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bp *baseProvider) HealthCheckURL() string {
|
|
||||||
return bp.healthCheck.url
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bp *baseProvider) RegisterHealthCheckTask(url string, expectedStatus utils.IntRanges[uint16], filter string, interval uint) {
|
|
||||||
bp.healthCheck.registerHealthCheckTask(url, expectedStatus, filter, interval)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bp *baseProvider) setProxies(proxies []C.Proxy) {
|
|
||||||
bp.proxies = proxies
|
|
||||||
bp.version += 1
|
|
||||||
bp.healthCheck.setProxies(proxies)
|
|
||||||
if bp.healthCheck.auto() {
|
|
||||||
go bp.healthCheck.check()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (bp *baseProvider) Close() error {
|
|
||||||
bp.healthCheck.close()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ProxySetProvider for auto gc
|
// ProxySetProvider for auto gc
|
||||||
type ProxySetProvider struct {
|
type ProxySetProvider struct {
|
||||||
*proxySetProvider
|
*proxySetProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
type proxySetProvider struct {
|
type proxySetProvider struct {
|
||||||
baseProvider
|
|
||||||
*resource.Fetcher[[]C.Proxy]
|
*resource.Fetcher[[]C.Proxy]
|
||||||
|
proxies []C.Proxy
|
||||||
|
healthCheck *HealthCheck
|
||||||
|
version uint32
|
||||||
subscriptionInfo *SubscriptionInfo
|
subscriptionInfo *SubscriptionInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pp *proxySetProvider) MarshalJSON() ([]byte, error) {
|
func (pp *proxySetProvider) MarshalJSON() ([]byte, error) {
|
||||||
return json.Marshal(providerForApi{
|
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(),
|
||||||
TestUrl: pp.healthCheck.url,
|
"testUrl": pp.healthCheck.url,
|
||||||
ExpectedStatus: pp.healthCheck.expectedStatus.String(),
|
"updatedAt": pp.UpdatedAt,
|
||||||
UpdatedAt: pp.UpdatedAt(),
|
"subscriptionInfo": pp.subscriptionInfo,
|
||||||
SubscriptionInfo: pp.subscriptionInfo,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (pp *proxySetProvider) Version() uint32 {
|
||||||
|
return pp.version
|
||||||
|
}
|
||||||
|
|
||||||
func (pp *proxySetProvider) Name() string {
|
func (pp *proxySetProvider) Name() string {
|
||||||
return pp.Fetcher.Name()
|
return pp.Fetcher.Name()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (pp *proxySetProvider) HealthCheck() {
|
||||||
|
pp.healthCheck.check()
|
||||||
|
}
|
||||||
|
|
||||||
func (pp *proxySetProvider) Update() error {
|
func (pp *proxySetProvider) Update() error {
|
||||||
_, _, err := pp.Fetcher.Update()
|
elm, same, err := pp.Fetcher.Update()
|
||||||
|
if err == nil && !same {
|
||||||
|
pp.OnUpdate(elm)
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pp *proxySetProvider) Initial() error {
|
func (pp *proxySetProvider) Initial() error {
|
||||||
if err := pp.baseProvider.Initial(); err != nil {
|
elm, err := pp.Fetcher.Initial()
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err := pp.Fetcher.Initial()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if subscriptionInfo := cachefile.Cache().GetSubscriptionInfo(pp.Name()); subscriptionInfo != "" {
|
pp.OnUpdate(elm)
|
||||||
pp.subscriptionInfo = NewSubscriptionInfo(subscriptionInfo)
|
pp.getSubscriptionInfo()
|
||||||
}
|
|
||||||
pp.closeAllConnections()
|
pp.closeAllConnections()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (pp *proxySetProvider) Type() types.ProviderType {
|
||||||
|
return types.Proxy
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pp *proxySetProvider) Proxies() []C.Proxy {
|
||||||
|
return pp.proxies
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pp *proxySetProvider) Touch() {
|
||||||
|
pp.healthCheck.touch()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pp *proxySetProvider) RegisterHealthCheckTask(url string, expectedStatus utils.IntRanges[uint16], filter string, interval uint) {
|
||||||
|
pp.healthCheck.registerHealthCheckTask(url, expectedStatus, filter, interval)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pp *proxySetProvider) setProxies(proxies []C.Proxy) {
|
||||||
|
pp.proxies = proxies
|
||||||
|
pp.healthCheck.setProxy(proxies)
|
||||||
|
if pp.healthCheck.auto() {
|
||||||
|
go pp.healthCheck.check()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 (pp *proxySetProvider) closeAllConnections() {
|
func (pp *proxySetProvider) closeAllConnections() {
|
||||||
statistic.DefaultManager.Range(func(c statistic.Tracker) bool {
|
statistic.DefaultManager.Range(func(c statistic.Tracker) bool {
|
||||||
for _, chain := range c.Chains() {
|
for _, chain := range c.Chains() {
|
||||||
@@ -166,172 +158,13 @@ func (pp *proxySetProvider) closeAllConnections() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pp *proxySetProvider) Close() error {
|
func stopProxyProvider(pd *ProxySetProvider) {
|
||||||
_ = pp.baseProvider.Close()
|
pd.healthCheck.close()
|
||||||
return pp.Fetcher.Close()
|
_ = pd.Fetcher.Destroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProxySetProvider(name string, interval time.Duration, payload []map[string]any, parser resource.Parser[[]C.Proxy], vehicle types.Vehicle, hc *HealthCheck) (*ProxySetProvider, error) {
|
func NewProxySetProvider(name string, interval time.Duration, filter string, excludeFilter string, excludeType string, dialerProxy string, vehicle types.Vehicle, hc *HealthCheck) (*ProxySetProvider, error) {
|
||||||
pd := &proxySetProvider{
|
excludeFilterReg, err := regexp2.Compile(excludeFilter, 0)
|
||||||
baseProvider: baseProvider{
|
|
||||||
name: name,
|
|
||||||
proxies: []C.Proxy{},
|
|
||||||
healthCheck: hc,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(payload) > 0 { // using as fallback proxies
|
|
||||||
ps := ProxySchema{Proxies: payload}
|
|
||||||
buf, err := yaml.Marshal(ps)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
proxies, err := parser(buf)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
pd.proxies = proxies
|
|
||||||
// direct call setProxies on hc to avoid starting a health check process immediately, it should be done by Initial()
|
|
||||||
hc.setProxies(proxies)
|
|
||||||
}
|
|
||||||
|
|
||||||
fetcher := resource.NewFetcher[[]C.Proxy](name, interval, vehicle, parser, pd.setProxies)
|
|
||||||
pd.Fetcher = fetcher
|
|
||||||
if httpVehicle, ok := vehicle.(*resource.HTTPVehicle); ok {
|
|
||||||
httpVehicle.SetInRead(func(resp *http.Response) {
|
|
||||||
if subscriptionInfo := resp.Header.Get("subscription-userinfo"); subscriptionInfo != "" {
|
|
||||||
cachefile.Cache().SetSubscriptionInfo(name, subscriptionInfo)
|
|
||||||
pd.subscriptionInfo = NewSubscriptionInfo(subscriptionInfo)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
wrapper := &ProxySetProvider{pd}
|
|
||||||
runtime.SetFinalizer(wrapper, (*ProxySetProvider).Close)
|
|
||||||
return wrapper, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pp *ProxySetProvider) Close() error {
|
|
||||||
runtime.SetFinalizer(pp, nil)
|
|
||||||
return pp.proxySetProvider.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
// InlineProvider for auto gc
|
|
||||||
type InlineProvider struct {
|
|
||||||
*inlineProvider
|
|
||||||
}
|
|
||||||
|
|
||||||
type inlineProvider struct {
|
|
||||||
baseProvider
|
|
||||||
updateAt time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ip *inlineProvider) MarshalJSON() ([]byte, error) {
|
|
||||||
return json.Marshal(providerForApi{
|
|
||||||
Name: ip.Name(),
|
|
||||||
Type: ip.Type().String(),
|
|
||||||
VehicleType: ip.VehicleType().String(),
|
|
||||||
Proxies: ip.Proxies(),
|
|
||||||
TestUrl: ip.healthCheck.url,
|
|
||||||
ExpectedStatus: ip.healthCheck.expectedStatus.String(),
|
|
||||||
UpdatedAt: ip.updateAt,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ip *inlineProvider) VehicleType() types.VehicleType {
|
|
||||||
return types.Inline
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ip *inlineProvider) Update() error {
|
|
||||||
// make api update happy
|
|
||||||
ip.updateAt = time.Now()
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewInlineProvider(name string, payload []map[string]any, parser resource.Parser[[]C.Proxy], hc *HealthCheck) (*InlineProvider, error) {
|
|
||||||
ps := ProxySchema{Proxies: payload}
|
|
||||||
buf, err := yaml.Marshal(ps)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
proxies, err := parser(buf)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// direct call setProxies on hc to avoid starting a health check process immediately, it should be done by Initial()
|
|
||||||
hc.setProxies(proxies)
|
|
||||||
|
|
||||||
ip := &inlineProvider{
|
|
||||||
baseProvider: baseProvider{
|
|
||||||
name: name,
|
|
||||||
proxies: proxies,
|
|
||||||
healthCheck: hc,
|
|
||||||
},
|
|
||||||
updateAt: time.Now(),
|
|
||||||
}
|
|
||||||
wrapper := &InlineProvider{ip}
|
|
||||||
runtime.SetFinalizer(wrapper, (*InlineProvider).Close)
|
|
||||||
return wrapper, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ip *InlineProvider) Close() error {
|
|
||||||
runtime.SetFinalizer(ip, nil)
|
|
||||||
return ip.baseProvider.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
// CompatibleProvider for auto gc
|
|
||||||
type CompatibleProvider struct {
|
|
||||||
*compatibleProvider
|
|
||||||
}
|
|
||||||
|
|
||||||
type compatibleProvider struct {
|
|
||||||
baseProvider
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cp *compatibleProvider) MarshalJSON() ([]byte, error) {
|
|
||||||
return json.Marshal(providerForApi{
|
|
||||||
Name: cp.Name(),
|
|
||||||
Type: cp.Type().String(),
|
|
||||||
VehicleType: cp.VehicleType().String(),
|
|
||||||
Proxies: cp.Proxies(),
|
|
||||||
TestUrl: cp.healthCheck.url,
|
|
||||||
ExpectedStatus: cp.healthCheck.expectedStatus.String(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cp *compatibleProvider) Update() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cp *compatibleProvider) VehicleType() types.VehicleType {
|
|
||||||
return types.Compatible
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCompatibleProvider(name string, proxies []C.Proxy, hc *HealthCheck) (*CompatibleProvider, error) {
|
|
||||||
if len(proxies) == 0 {
|
|
||||||
return nil, errors.New("provider need one proxy at least")
|
|
||||||
}
|
|
||||||
|
|
||||||
pd := &compatibleProvider{
|
|
||||||
baseProvider: baseProvider{
|
|
||||||
name: name,
|
|
||||||
proxies: proxies,
|
|
||||||
healthCheck: hc,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
wrapper := &CompatibleProvider{pd}
|
|
||||||
runtime.SetFinalizer(wrapper, (*CompatibleProvider).Close)
|
|
||||||
return wrapper, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cp *CompatibleProvider) Close() error {
|
|
||||||
runtime.SetFinalizer(cp, nil)
|
|
||||||
return cp.compatibleProvider.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewProxiesParser(filter string, excludeFilter string, excludeType string, dialerProxy string, override OverrideSchema) (resource.Parser[[]C.Proxy], error) {
|
|
||||||
excludeFilterReg, err := regexp2.Compile(excludeFilter, regexp2.None)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("invalid excludeFilter regex: %w", err)
|
return nil, fmt.Errorf("invalid excludeFilter regex: %w", err)
|
||||||
}
|
}
|
||||||
@@ -342,13 +175,124 @@ func NewProxiesParser(filter string, excludeFilter string, excludeType string, d
|
|||||||
|
|
||||||
var filterRegs []*regexp2.Regexp
|
var filterRegs []*regexp2.Regexp
|
||||||
for _, filter := range strings.Split(filter, "`") {
|
for _, filter := range strings.Split(filter, "`") {
|
||||||
filterReg, err := regexp2.Compile(filter, regexp2.None)
|
filterReg, err := regexp2.Compile(filter, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("invalid filter regex: %w", err)
|
return nil, fmt.Errorf("invalid filter regex: %w", err)
|
||||||
}
|
}
|
||||||
filterRegs = append(filterRegs, filterReg)
|
filterRegs = append(filterRegs, filterReg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if hc.auto() {
|
||||||
|
go hc.process()
|
||||||
|
}
|
||||||
|
|
||||||
|
pd := &proxySetProvider{
|
||||||
|
proxies: []C.Proxy{},
|
||||||
|
healthCheck: hc,
|
||||||
|
}
|
||||||
|
|
||||||
|
fetcher := resource.NewFetcher[[]C.Proxy](name, interval, vehicle, proxiesParseAndFilter(filter, excludeFilter, excludeTypeArray, filterRegs, excludeFilterReg, dialerProxy), proxiesOnUpdate(pd))
|
||||||
|
pd.Fetcher = fetcher
|
||||||
|
wrapper := &ProxySetProvider{pd}
|
||||||
|
runtime.SetFinalizer(wrapper, stopProxyProvider)
|
||||||
|
return wrapper, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CompatibleProvider for auto gc
|
||||||
|
type CompatibleProvider struct {
|
||||||
|
*compatibleProvider
|
||||||
|
}
|
||||||
|
|
||||||
|
type compatibleProvider struct {
|
||||||
|
name string
|
||||||
|
healthCheck *HealthCheck
|
||||||
|
proxies []C.Proxy
|
||||||
|
version uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cp *compatibleProvider) MarshalJSON() ([]byte, error) {
|
||||||
|
return json.Marshal(map[string]any{
|
||||||
|
"name": cp.Name(),
|
||||||
|
"type": cp.Type().String(),
|
||||||
|
"vehicleType": cp.VehicleType().String(),
|
||||||
|
"proxies": cp.Proxies(),
|
||||||
|
"testUrl": cp.healthCheck.url,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cp *compatibleProvider) Version() uint32 {
|
||||||
|
return cp.version
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cp *compatibleProvider) Name() string {
|
||||||
|
return cp.name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cp *compatibleProvider) HealthCheck() {
|
||||||
|
cp.healthCheck.check()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cp *compatibleProvider) Update() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cp *compatibleProvider) Initial() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cp *compatibleProvider) VehicleType() types.VehicleType {
|
||||||
|
return types.Compatible
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cp *compatibleProvider) Type() types.ProviderType {
|
||||||
|
return types.Proxy
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cp *compatibleProvider) Proxies() []C.Proxy {
|
||||||
|
return cp.proxies
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cp *compatibleProvider) Touch() {
|
||||||
|
cp.healthCheck.touch()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cp *compatibleProvider) RegisterHealthCheckTask(url string, expectedStatus utils.IntRanges[uint16], filter string, interval uint) {
|
||||||
|
cp.healthCheck.registerHealthCheckTask(url, expectedStatus, filter, interval)
|
||||||
|
}
|
||||||
|
|
||||||
|
func stopCompatibleProvider(pd *CompatibleProvider) {
|
||||||
|
pd.healthCheck.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCompatibleProvider(name string, proxies []C.Proxy, hc *HealthCheck) (*CompatibleProvider, error) {
|
||||||
|
if len(proxies) == 0 {
|
||||||
|
return nil, errors.New("provider need one proxy at least")
|
||||||
|
}
|
||||||
|
|
||||||
|
if hc.auto() {
|
||||||
|
go hc.process()
|
||||||
|
}
|
||||||
|
|
||||||
|
pd := &compatibleProvider{
|
||||||
|
name: name,
|
||||||
|
proxies: proxies,
|
||||||
|
healthCheck: hc,
|
||||||
|
}
|
||||||
|
|
||||||
|
wrapper := &CompatibleProvider{pd}
|
||||||
|
runtime.SetFinalizer(wrapper, stopCompatibleProvider)
|
||||||
|
return wrapper, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func proxiesOnUpdate(pd *proxySetProvider) func([]C.Proxy) {
|
||||||
|
return func(elm []C.Proxy) {
|
||||||
|
pd.setProxies(elm)
|
||||||
|
pd.version += 1
|
||||||
|
pd.getSubscriptionInfo()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func proxiesParseAndFilter(filter string, excludeFilter string, excludeTypeArray []string, filterRegs []*regexp2.Regexp, excludeFilterReg *regexp2.Regexp, dialerProxy string) resource.Parser[[]C.Proxy] {
|
||||||
return func(buf []byte) ([]C.Proxy, error) {
|
return func(buf []byte) ([]C.Proxy, error) {
|
||||||
schema := &ProxySchema{}
|
schema := &ProxySchema{}
|
||||||
|
|
||||||
@@ -399,57 +343,25 @@ func NewProxiesParser(filter string, excludeFilter string, excludeType string, d
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if len(excludeFilter) > 0 {
|
if len(excludeFilter) > 0 {
|
||||||
if mat, _ := excludeFilterReg.MatchString(name); mat {
|
if mat, _ := excludeFilterReg.FindStringMatch(name); mat != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(filter) > 0 {
|
if len(filter) > 0 {
|
||||||
if mat, _ := filterReg.MatchString(name); !mat {
|
if mat, _ := filterReg.FindStringMatch(name); mat == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if _, ok := proxiesSet[name]; ok {
|
if _, ok := proxiesSet[name]; ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(dialerProxy) > 0 {
|
if len(dialerProxy) > 0 {
|
||||||
mapping["dialer-proxy"] = dialerProxy
|
mapping["dialer-proxy"] = dialerProxy
|
||||||
}
|
}
|
||||||
|
|
||||||
val := reflect.ValueOf(override)
|
|
||||||
for i := 0; i < val.NumField(); i++ {
|
|
||||||
field := val.Field(i)
|
|
||||||
if field.IsNil() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
fieldName := strings.Split(val.Type().Field(i).Tag.Get("provider"), ",")[0]
|
|
||||||
switch fieldName {
|
|
||||||
case "additional-prefix":
|
|
||||||
name := mapping["name"].(string)
|
|
||||||
mapping["name"] = *field.Interface().(*string) + name
|
|
||||||
case "additional-suffix":
|
|
||||||
name := mapping["name"].(string)
|
|
||||||
mapping["name"] = name + *field.Interface().(*string)
|
|
||||||
case "proxy-name":
|
|
||||||
// Iterate through all naming replacement rules and perform the replacements.
|
|
||||||
for _, expr := range override.ProxyName {
|
|
||||||
name := mapping["name"].(string)
|
|
||||||
newName, err := expr.Pattern.Replace(name, expr.Target, 0, -1)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("proxy name replace error: %w", err)
|
|
||||||
}
|
|
||||||
mapping["name"] = newName
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
mapping[fieldName] = field.Elem().Interface()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
proxy, err := adapter.ParseProxy(mapping)
|
proxy, err := adapter.ParseProxy(mapping)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("proxy %d error: %w", idx, err)
|
return nil, fmt.Errorf("proxy %d error: %w", idx, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
proxiesSet[name] = struct{}{}
|
proxiesSet[name] = struct{}{}
|
||||||
proxies = append(proxies, proxy)
|
proxies = append(proxies, proxy)
|
||||||
}
|
}
|
||||||
@@ -463,5 +375,5 @@ func NewProxiesParser(filter string, excludeFilter string, excludeType string, d
|
|||||||
}
|
}
|
||||||
|
|
||||||
return proxies, nil
|
return proxies, nil
|
||||||
}, nil
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,8 @@
|
|||||||
package provider
|
package provider
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type SubscriptionInfo struct {
|
type SubscriptionInfo struct {
|
||||||
@@ -15,44 +12,24 @@ type SubscriptionInfo struct {
|
|||||||
Expire int64
|
Expire int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSubscriptionInfo(userinfo string) (si *SubscriptionInfo) {
|
func NewSubscriptionInfo(userinfo string) (si *SubscriptionInfo, err error) {
|
||||||
userinfo = strings.ReplaceAll(strings.ToLower(userinfo), " ", "")
|
userinfo = strings.ToLower(userinfo)
|
||||||
|
userinfo = strings.ReplaceAll(userinfo, " ", "")
|
||||||
si = new(SubscriptionInfo)
|
si = new(SubscriptionInfo)
|
||||||
|
|
||||||
for _, field := range strings.Split(userinfo, ";") {
|
for _, field := range strings.Split(userinfo, ";") {
|
||||||
name, value, ok := strings.Cut(field, "=")
|
switch name, value, _ := strings.Cut(field, "="); name {
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
intValue, err := parseValue(value)
|
|
||||||
if err != nil {
|
|
||||||
log.Warnln("[Provider] get subscription-userinfo: %e", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
switch name {
|
|
||||||
case "upload":
|
case "upload":
|
||||||
si.Upload = intValue
|
si.Upload, err = strconv.ParseInt(value, 10, 64)
|
||||||
case "download":
|
case "download":
|
||||||
si.Download = intValue
|
si.Download, err = strconv.ParseInt(value, 10, 64)
|
||||||
case "total":
|
case "total":
|
||||||
si.Total = intValue
|
si.Total, err = strconv.ParseInt(value, 10, 64)
|
||||||
case "expire":
|
case "expire":
|
||||||
si.Expire = intValue
|
si.Expire, err = strconv.ParseInt(value, 10, 64)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return si
|
return
|
||||||
}
|
|
||||||
|
|
||||||
func parseValue(value string) (int64, error) {
|
|
||||||
if intValue, err := strconv.ParseInt(value, 10, 64); err == nil {
|
|
||||||
return intValue, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if floatValue, err := strconv.ParseFloat(value, 64); err == nil {
|
|
||||||
return int64(floatValue), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0, fmt.Errorf("failed to parse value '%s'", value)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
// Copyright 2014 The Go Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
// kanged from https://github.com/golang/mobile/blob/c713f31d574bb632a93f169b2cc99c9e753fef0e/app/android.go#L89
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
// #include <time.h>
|
|
||||||
import "C"
|
|
||||||
import "time"
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
var currentT C.time_t
|
|
||||||
var currentTM C.struct_tm
|
|
||||||
C.time(¤tT)
|
|
||||||
C.localtime_r(¤tT, ¤tTM)
|
|
||||||
tzOffset := int(currentTM.tm_gmtoff)
|
|
||||||
tz := C.GoString(currentTM.tm_zone)
|
|
||||||
time.Local = time.FixedZone(tz, tzOffset)
|
|
||||||
}
|
|
||||||
@@ -1,241 +0,0 @@
|
|||||||
package arc
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
list "github.com/bahlo/generic-list-go"
|
|
||||||
"github.com/samber/lo"
|
|
||||||
)
|
|
||||||
|
|
||||||
//modify from https://github.com/alexanderGugel/arc
|
|
||||||
|
|
||||||
// Option is part of Functional Options Pattern
|
|
||||||
type Option[K comparable, V any] func(*ARC[K, V])
|
|
||||||
|
|
||||||
func WithSize[K comparable, V any](maxSize int) Option[K, V] {
|
|
||||||
return func(a *ARC[K, V]) {
|
|
||||||
a.c = maxSize
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type ARC[K comparable, V any] struct {
|
|
||||||
p int
|
|
||||||
c int
|
|
||||||
t1 *list.List[*entry[K, V]]
|
|
||||||
b1 *list.List[*entry[K, V]]
|
|
||||||
t2 *list.List[*entry[K, V]]
|
|
||||||
b2 *list.List[*entry[K, V]]
|
|
||||||
mutex sync.Mutex
|
|
||||||
len int
|
|
||||||
cache map[K]*entry[K, V]
|
|
||||||
}
|
|
||||||
|
|
||||||
// New returns a new Adaptive Replacement Cache (ARC).
|
|
||||||
func New[K comparable, V any](options ...Option[K, V]) *ARC[K, V] {
|
|
||||||
arc := &ARC[K, V]{}
|
|
||||||
arc.Clear()
|
|
||||||
|
|
||||||
for _, option := range options {
|
|
||||||
option(arc)
|
|
||||||
}
|
|
||||||
return arc
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *ARC[K, V]) Clear() {
|
|
||||||
a.mutex.Lock()
|
|
||||||
defer a.mutex.Unlock()
|
|
||||||
|
|
||||||
a.p = 0
|
|
||||||
a.t1 = list.New[*entry[K, V]]()
|
|
||||||
a.b1 = list.New[*entry[K, V]]()
|
|
||||||
a.t2 = list.New[*entry[K, V]]()
|
|
||||||
a.b2 = list.New[*entry[K, V]]()
|
|
||||||
a.len = 0
|
|
||||||
a.cache = make(map[K]*entry[K, V])
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set inserts a new key-value pair into the cache.
|
|
||||||
// This optimizes future access to this entry (side effect).
|
|
||||||
func (a *ARC[K, V]) Set(key K, value V) {
|
|
||||||
a.mutex.Lock()
|
|
||||||
defer a.mutex.Unlock()
|
|
||||||
|
|
||||||
a.set(key, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *ARC[K, V]) set(key K, value V) {
|
|
||||||
a.setWithExpire(key, value, time.Unix(0, 0))
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetWithExpire stores any representation of a response for a given key and given expires.
|
|
||||||
// The expires time will round to second.
|
|
||||||
func (a *ARC[K, V]) SetWithExpire(key K, value V, expires time.Time) {
|
|
||||||
a.mutex.Lock()
|
|
||||||
defer a.mutex.Unlock()
|
|
||||||
|
|
||||||
a.setWithExpire(key, value, expires)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *ARC[K, V]) setWithExpire(key K, value V, expires time.Time) {
|
|
||||||
ent, ok := a.cache[key]
|
|
||||||
if !ok {
|
|
||||||
a.len++
|
|
||||||
ent := &entry[K, V]{key: key, value: value, ghost: false, expires: expires.Unix()}
|
|
||||||
a.req(ent)
|
|
||||||
a.cache[key] = ent
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if ent.ghost {
|
|
||||||
a.len++
|
|
||||||
}
|
|
||||||
|
|
||||||
ent.value = value
|
|
||||||
ent.ghost = false
|
|
||||||
ent.expires = expires.Unix()
|
|
||||||
a.req(ent)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get retrieves a previously via Set inserted entry.
|
|
||||||
// This optimizes future access to this entry (side effect).
|
|
||||||
func (a *ARC[K, V]) Get(key K) (value V, ok bool) {
|
|
||||||
a.mutex.Lock()
|
|
||||||
defer a.mutex.Unlock()
|
|
||||||
|
|
||||||
ent, ok := a.get(key)
|
|
||||||
if !ok {
|
|
||||||
return lo.Empty[V](), false
|
|
||||||
}
|
|
||||||
return ent.value, true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *ARC[K, V]) get(key K) (e *entry[K, V], ok bool) {
|
|
||||||
ent, ok := a.cache[key]
|
|
||||||
if !ok {
|
|
||||||
return ent, false
|
|
||||||
}
|
|
||||||
a.req(ent)
|
|
||||||
return ent, !ent.ghost
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetWithExpire returns any representation of a cached response,
|
|
||||||
// a time.Time Give expected expires,
|
|
||||||
// and a bool set to true if the key was found.
|
|
||||||
// This method will NOT update the expires.
|
|
||||||
func (a *ARC[K, V]) GetWithExpire(key K) (V, time.Time, bool) {
|
|
||||||
a.mutex.Lock()
|
|
||||||
defer a.mutex.Unlock()
|
|
||||||
|
|
||||||
ent, ok := a.get(key)
|
|
||||||
if !ok {
|
|
||||||
return lo.Empty[V](), time.Time{}, false
|
|
||||||
}
|
|
||||||
|
|
||||||
return ent.value, time.Unix(ent.expires, 0), true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Len determines the number of currently cached entries.
|
|
||||||
// This method is side-effect free in the sense that it does not attempt to optimize random cache access.
|
|
||||||
func (a *ARC[K, V]) Len() int {
|
|
||||||
a.mutex.Lock()
|
|
||||||
defer a.mutex.Unlock()
|
|
||||||
|
|
||||||
return a.len
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *ARC[K, V]) req(ent *entry[K, V]) {
|
|
||||||
switch {
|
|
||||||
case ent.ll == a.t1 || ent.ll == a.t2:
|
|
||||||
// Case I
|
|
||||||
ent.setMRU(a.t2)
|
|
||||||
case ent.ll == a.b1:
|
|
||||||
// Case II
|
|
||||||
// Cache Miss in t1 and t2
|
|
||||||
|
|
||||||
// Adaptation
|
|
||||||
var d int
|
|
||||||
if a.b1.Len() >= a.b2.Len() {
|
|
||||||
d = 1
|
|
||||||
} else {
|
|
||||||
d = a.b2.Len() / a.b1.Len()
|
|
||||||
}
|
|
||||||
a.p = min(a.p+d, a.c)
|
|
||||||
|
|
||||||
a.replace(ent)
|
|
||||||
ent.setMRU(a.t2)
|
|
||||||
case ent.ll == a.b2:
|
|
||||||
// Case III
|
|
||||||
// Cache Miss in t1 and t2
|
|
||||||
|
|
||||||
// Adaptation
|
|
||||||
var d int
|
|
||||||
if a.b2.Len() >= a.b1.Len() {
|
|
||||||
d = 1
|
|
||||||
} else {
|
|
||||||
d = a.b1.Len() / a.b2.Len()
|
|
||||||
}
|
|
||||||
a.p = max(a.p-d, 0)
|
|
||||||
|
|
||||||
a.replace(ent)
|
|
||||||
ent.setMRU(a.t2)
|
|
||||||
case ent.ll == nil && a.t1.Len()+a.b1.Len() == a.c:
|
|
||||||
// Case IV A
|
|
||||||
if a.t1.Len() < a.c {
|
|
||||||
a.delLRU(a.b1)
|
|
||||||
a.replace(ent)
|
|
||||||
} else {
|
|
||||||
a.delLRU(a.t1)
|
|
||||||
}
|
|
||||||
ent.setMRU(a.t1)
|
|
||||||
case ent.ll == nil && a.t1.Len()+a.b1.Len() < a.c:
|
|
||||||
// Case IV B
|
|
||||||
if a.t1.Len()+a.t2.Len()+a.b1.Len()+a.b2.Len() >= a.c {
|
|
||||||
if a.t1.Len()+a.t2.Len()+a.b1.Len()+a.b2.Len() == 2*a.c {
|
|
||||||
a.delLRU(a.b2)
|
|
||||||
}
|
|
||||||
a.replace(ent)
|
|
||||||
}
|
|
||||||
ent.setMRU(a.t1)
|
|
||||||
case ent.ll == nil:
|
|
||||||
// Case IV, not A nor B
|
|
||||||
ent.setMRU(a.t1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *ARC[K, V]) delLRU(list *list.List[*entry[K, V]]) {
|
|
||||||
lru := list.Back()
|
|
||||||
list.Remove(lru)
|
|
||||||
a.len--
|
|
||||||
delete(a.cache, lru.Value.key)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *ARC[K, V]) replace(ent *entry[K, V]) {
|
|
||||||
if a.t1.Len() > 0 && ((a.t1.Len() > a.p) || (ent.ll == a.b2 && a.t1.Len() == a.p)) {
|
|
||||||
lru := a.t1.Back().Value
|
|
||||||
lru.value = lo.Empty[V]()
|
|
||||||
lru.ghost = true
|
|
||||||
a.len--
|
|
||||||
lru.setMRU(a.b1)
|
|
||||||
} else {
|
|
||||||
lru := a.t2.Back().Value
|
|
||||||
lru.value = lo.Empty[V]()
|
|
||||||
lru.ghost = true
|
|
||||||
a.len--
|
|
||||||
lru.setMRU(a.b2)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func min(a, b int) int {
|
|
||||||
if a < b {
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
func max(a int, b int) int {
|
|
||||||
if a < b {
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
@@ -1,105 +0,0 @@
|
|||||||
package arc
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestInsertion(t *testing.T) {
|
|
||||||
cache := New[string, string](WithSize[string, string](3))
|
|
||||||
if got, want := cache.Len(), 0; got != want {
|
|
||||||
t.Errorf("empty cache.Len(): got %d want %d", cache.Len(), want)
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
|
||||||
k1 = "Hello"
|
|
||||||
k2 = "Hallo"
|
|
||||||
k3 = "Ciao"
|
|
||||||
k4 = "Salut"
|
|
||||||
|
|
||||||
v1 = "World"
|
|
||||||
v2 = "Worlds"
|
|
||||||
v3 = "Welt"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Insert the first value
|
|
||||||
cache.Set(k1, v1)
|
|
||||||
if got, want := cache.Len(), 1; got != want {
|
|
||||||
t.Errorf("insertion of key #%d: cache.Len(): got %d want %d", want, cache.Len(), want)
|
|
||||||
}
|
|
||||||
if got, ok := cache.Get(k1); !ok || got != v1 {
|
|
||||||
t.Errorf("cache.Get(%v): got (%v,%t) want (%v,true)", k1, got, ok, v1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Replace existing value for a given key
|
|
||||||
cache.Set(k1, v2)
|
|
||||||
if got, want := cache.Len(), 1; got != want {
|
|
||||||
t.Errorf("re-insertion: cache.Len(): got %d want %d", cache.Len(), want)
|
|
||||||
}
|
|
||||||
if got, ok := cache.Get(k1); !ok || got != v2 {
|
|
||||||
t.Errorf("re-insertion: cache.Get(%v): got (%v,%t) want (%v,true)", k1, got, ok, v2)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add a second different key
|
|
||||||
cache.Set(k2, v3)
|
|
||||||
if got, want := cache.Len(), 2; got != want {
|
|
||||||
t.Errorf("insertion of key #%d: cache.Len(): got %d want %d", want, cache.Len(), want)
|
|
||||||
}
|
|
||||||
if got, ok := cache.Get(k1); !ok || got != v2 {
|
|
||||||
t.Errorf("cache.Get(%v): got (%v,%t) want (%v,true)", k1, got, ok, v2)
|
|
||||||
}
|
|
||||||
if got, ok := cache.Get(k2); !ok || got != v3 {
|
|
||||||
t.Errorf("cache.Get(%v): got (%v,%t) want (%v,true)", k2, got, ok, v3)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fill cache
|
|
||||||
cache.Set(k3, v1)
|
|
||||||
if got, want := cache.Len(), 3; got != want {
|
|
||||||
t.Errorf("insertion of key #%d: cache.Len(): got %d want %d", want, cache.Len(), want)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Exceed size, this should not exceed size:
|
|
||||||
cache.Set(k4, v1)
|
|
||||||
if got, want := cache.Len(), 3; got != want {
|
|
||||||
t.Errorf("insertion of key out of size: cache.Len(): got %d want %d", cache.Len(), want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestEviction(t *testing.T) {
|
|
||||||
size := 3
|
|
||||||
cache := New[string, string](WithSize[string, string](size))
|
|
||||||
if got, want := cache.Len(), 0; got != want {
|
|
||||||
t.Errorf("empty cache.Len(): got %d want %d", cache.Len(), want)
|
|
||||||
}
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
k, v string
|
|
||||||
}{
|
|
||||||
{"k1", "v1"},
|
|
||||||
{"k2", "v2"},
|
|
||||||
{"k3", "v3"},
|
|
||||||
{"k4", "v4"},
|
|
||||||
}
|
|
||||||
for i, tt := range tests[:size] {
|
|
||||||
cache.Set(tt.k, tt.v)
|
|
||||||
if got, want := cache.Len(), i+1; got != want {
|
|
||||||
t.Errorf("insertion of key #%d: cache.Len(): got %d want %d", want, cache.Len(), want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Exceed size and check we don't outgrow it:
|
|
||||||
cache.Set(tests[size].k, tests[size].v)
|
|
||||||
if got := cache.Len(); got != size {
|
|
||||||
t.Errorf("insertion of overflow key #%d: cache.Len(): got %d want %d", 4, cache.Len(), size)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check that LRU got evicted:
|
|
||||||
if got, ok := cache.Get(tests[0].k); ok || got != "" {
|
|
||||||
t.Errorf("cache.Get(%v): got (%v,%t) want (<nil>,true)", tests[0].k, got, ok)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests[1:] {
|
|
||||||
if got, ok := cache.Get(tt.k); !ok || got != tt.v {
|
|
||||||
t.Errorf("cache.Get(%v): got (%v,%t) want (%v,true)", tt.k, got, ok, tt.v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
package arc
|
|
||||||
|
|
||||||
import (
|
|
||||||
list "github.com/bahlo/generic-list-go"
|
|
||||||
)
|
|
||||||
|
|
||||||
type entry[K comparable, V any] struct {
|
|
||||||
key K
|
|
||||||
value V
|
|
||||||
ll *list.List[*entry[K, V]]
|
|
||||||
el *list.Element[*entry[K, V]]
|
|
||||||
ghost bool
|
|
||||||
expires int64
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *entry[K, V]) setLRU(list *list.List[*entry[K, V]]) {
|
|
||||||
e.detach()
|
|
||||||
e.ll = list
|
|
||||||
e.el = e.ll.PushBack(e)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *entry[K, V]) setMRU(list *list.List[*entry[K, V]]) {
|
|
||||||
e.detach()
|
|
||||||
e.ll = list
|
|
||||||
e.el = e.ll.PushFront(e)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *entry[K, V]) detach() {
|
|
||||||
if e.ll != nil {
|
|
||||||
e.ll.Remove(e.el)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -11,43 +11,31 @@ func DefaultValue[T any]() T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type TypedValue[T any] struct {
|
type TypedValue[T any] struct {
|
||||||
_ noCopy
|
|
||||||
value atomic.Value
|
value atomic.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
// tValue is a struct with determined type to resolve atomic.Value usages with interface types
|
|
||||||
// https://github.com/golang/go/issues/22550
|
|
||||||
//
|
|
||||||
// The intention to have an atomic value store for errors. However, running this code panics:
|
|
||||||
// panic: sync/atomic: store of inconsistently typed value into Value
|
|
||||||
// This is because atomic.Value requires that the underlying concrete type be the same (which is a reasonable expectation for its implementation).
|
|
||||||
// When going through the atomic.Value.Store method call, the fact that both these are of the error interface is lost.
|
|
||||||
type tValue[T any] struct {
|
|
||||||
value T
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *TypedValue[T]) Load() T {
|
func (t *TypedValue[T]) Load() T {
|
||||||
value := t.value.Load()
|
value := t.value.Load()
|
||||||
if value == nil {
|
if value == nil {
|
||||||
return DefaultValue[T]()
|
return DefaultValue[T]()
|
||||||
}
|
}
|
||||||
return value.(tValue[T]).value
|
return value.(T)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TypedValue[T]) Store(value T) {
|
func (t *TypedValue[T]) Store(value T) {
|
||||||
t.value.Store(tValue[T]{value})
|
t.value.Store(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TypedValue[T]) Swap(new T) T {
|
func (t *TypedValue[T]) Swap(new T) T {
|
||||||
old := t.value.Swap(tValue[T]{new})
|
old := t.value.Swap(new)
|
||||||
if old == nil {
|
if old == nil {
|
||||||
return DefaultValue[T]()
|
return DefaultValue[T]()
|
||||||
}
|
}
|
||||||
return old.(tValue[T]).value
|
return old.(T)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TypedValue[T]) CompareAndSwap(old, new T) bool {
|
func (t *TypedValue[T]) CompareAndSwap(old, new T) bool {
|
||||||
return t.value.CompareAndSwap(tValue[T]{old}, tValue[T]{new})
|
return t.value.CompareAndSwap(old, new)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TypedValue[T]) MarshalJSON() ([]byte, error) {
|
func (t *TypedValue[T]) MarshalJSON() ([]byte, error) {
|
||||||
@@ -67,9 +55,3 @@ func NewTypedValue[T any](t T) (v TypedValue[T]) {
|
|||||||
v.Store(t)
|
v.Store(t)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type noCopy struct{}
|
|
||||||
|
|
||||||
// Lock is a no-op used by -copylocks checker from `go vet`.
|
|
||||||
func (*noCopy) Lock() {}
|
|
||||||
func (*noCopy) Unlock() {}
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package buf
|
package buf
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/metacubex/sing/common"
|
"github.com/sagernet/sing/common"
|
||||||
"github.com/metacubex/sing/common/buf"
|
"github.com/sagernet/sing/common/buf"
|
||||||
)
|
)
|
||||||
|
|
||||||
const BufferSize = buf.BufferSize
|
const BufferSize = buf.BufferSize
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package lru
|
package cache
|
||||||
|
|
||||||
// Modified by https://github.com/die-net/lrucache
|
// Modified by https://github.com/die-net/lrucache
|
||||||
|
|
||||||
@@ -6,7 +6,8 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
list "github.com/bahlo/generic-list-go"
|
"github.com/Dreamacro/clash/common/generics/list"
|
||||||
|
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -68,8 +69,10 @@ type LruCache[K comparable, V any] struct {
|
|||||||
|
|
||||||
// New creates an LruCache
|
// New creates an LruCache
|
||||||
func New[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]{
|
||||||
lc.Clear()
|
lru: list.New[*entry[K, V]](),
|
||||||
|
cache: make(map[K]*list.Element[*entry[K, V]]),
|
||||||
|
}
|
||||||
|
|
||||||
for _, option := range options {
|
for _, option := range options {
|
||||||
option(lc)
|
option(lc)
|
||||||
@@ -78,15 +81,7 @@ func New[K comparable, V any](options ...Option[K, V]) *LruCache[K, V] {
|
|||||||
return lc
|
return lc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *LruCache[K, V]) Clear() {
|
// Get returns the any representation of a cached response and a bool
|
||||||
c.mu.Lock()
|
|
||||||
defer c.mu.Unlock()
|
|
||||||
|
|
||||||
c.lru = list.New[*entry[K, V]]()
|
|
||||||
c.cache = make(map[K]*list.Element[*entry[K, V]])
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get returns any representation of a cached response and a bool
|
|
||||||
// set to true if the key was found.
|
// set to true if the key was found.
|
||||||
func (c *LruCache[K, V]) Get(key K) (V, bool) {
|
func (c *LruCache[K, V]) Get(key K) (V, bool) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
@@ -116,7 +111,7 @@ func (c *LruCache[K, V]) GetOrStore(key K, constructor func() V) (V, bool) {
|
|||||||
return value, true
|
return value, true
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetWithExpire returns any representation of a cached response,
|
// GetWithExpire returns the any representation of a cached response,
|
||||||
// a time.Time Give expected expires,
|
// a time.Time Give expected expires,
|
||||||
// and a bool set to true if the key was found.
|
// and a bool set to true if the key was found.
|
||||||
// This method will NOT check the maxAge of element and will NOT update the expires.
|
// This method will NOT check the maxAge of element and will NOT update the expires.
|
||||||
@@ -141,7 +136,7 @@ func (c *LruCache[K, V]) Exist(key K) bool {
|
|||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set stores any representation of a response for a given key.
|
// Set stores the any representation of a response for a given key.
|
||||||
func (c *LruCache[K, V]) Set(key K, value V) {
|
func (c *LruCache[K, V]) Set(key K, value V) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
@@ -157,7 +152,7 @@ func (c *LruCache[K, V]) set(key K, value V) {
|
|||||||
c.setWithExpire(key, value, time.Unix(expires, 0))
|
c.setWithExpire(key, value, time.Unix(expires, 0))
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetWithExpire stores any representation of a response for a given key and given expires.
|
// SetWithExpire stores the any representation of a response for a given key and given expires.
|
||||||
// The expires time will round to second.
|
// The expires time will round to second.
|
||||||
func (c *LruCache[K, V]) SetWithExpire(key K, value V, expires time.Time) {
|
func (c *LruCache[K, V]) SetWithExpire(key K, value V, expires time.Time) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
@@ -229,10 +224,6 @@ func (c *LruCache[K, V]) Delete(key K) {
|
|||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
|
|
||||||
c.delete(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *LruCache[K, V]) delete(key K) {
|
|
||||||
if le, ok := c.cache[key]; ok {
|
if le, ok := c.cache[key]; ok {
|
||||||
c.deleteElement(le)
|
c.deleteElement(le)
|
||||||
}
|
}
|
||||||
@@ -256,32 +247,13 @@ func (c *LruCache[K, V]) deleteElement(le *list.Element[*entry[K, V]]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute either sets the computed new value for the key or deletes
|
func (c *LruCache[K, V]) Clear() error {
|
||||||
// the value for the key. When the delete result of the valueFn function
|
|
||||||
// is set to true, the value will be deleted, if it exists. When delete
|
|
||||||
// is set to false, the value is updated to the newValue.
|
|
||||||
// The ok result indicates whether value was computed and stored, thus, is
|
|
||||||
// present in the map. The actual result contains the new value in cases where
|
|
||||||
// the value was computed and stored.
|
|
||||||
func (c *LruCache[K, V]) Compute(
|
|
||||||
key K,
|
|
||||||
valueFn func(oldValue V, loaded bool) (newValue V, delete bool),
|
|
||||||
) (actual V, ok bool) {
|
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
|
|
||||||
if el := c.get(key); el != nil {
|
c.cache = make(map[K]*list.Element[*entry[K, V]])
|
||||||
actual, ok = el.value, true
|
|
||||||
}
|
return nil
|
||||||
if newValue, del := valueFn(actual, ok); del {
|
|
||||||
if ok { // data not in cache, so needn't delete
|
|
||||||
c.delete(key)
|
|
||||||
}
|
|
||||||
return lo.Empty[V](), false
|
|
||||||
} else {
|
|
||||||
c.set(key, newValue)
|
|
||||||
return newValue, true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type entry[K comparable, V any] struct {
|
type entry[K comparable, V any] struct {
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package lru
|
package cache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
package callback
|
package callback
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/metacubex/mihomo/common/buf"
|
"github.com/Dreamacro/clash/common/buf"
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
N "github.com/Dreamacro/clash/common/net"
|
||||||
C "github.com/metacubex/mihomo/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
type firstWriteCallBackConn struct {
|
type firstWriteCallBackConn struct {
|
||||||
|
|||||||
@@ -1,61 +0,0 @@
|
|||||||
package callback
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
C "github.com/metacubex/mihomo/constant"
|
|
||||||
)
|
|
||||||
|
|
||||||
type closeCallbackConn struct {
|
|
||||||
C.Conn
|
|
||||||
closeFunc func()
|
|
||||||
closeOnce sync.Once
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *closeCallbackConn) Close() error {
|
|
||||||
w.closeOnce.Do(w.closeFunc)
|
|
||||||
return w.Conn.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *closeCallbackConn) ReaderReplaceable() bool {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *closeCallbackConn) WriterReplaceable() bool {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *closeCallbackConn) Upstream() any {
|
|
||||||
return w.Conn
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCloseCallbackConn(conn C.Conn, callback func()) C.Conn {
|
|
||||||
return &closeCallbackConn{Conn: conn, closeFunc: callback}
|
|
||||||
}
|
|
||||||
|
|
||||||
type closeCallbackPacketConn struct {
|
|
||||||
C.PacketConn
|
|
||||||
closeFunc func()
|
|
||||||
closeOnce sync.Once
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *closeCallbackPacketConn) Close() error {
|
|
||||||
w.closeOnce.Do(w.closeFunc)
|
|
||||||
return w.PacketConn.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *closeCallbackPacketConn) ReaderReplaceable() bool {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *closeCallbackPacketConn) WriterReplaceable() bool {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *closeCallbackPacketConn) Upstream() any {
|
|
||||||
return w.PacketConn
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCloseCallbackPacketConn(conn C.PacketConn, callback func()) C.PacketConn {
|
|
||||||
return &closeCallbackPacketConn{PacketConn: conn, closeFunc: callback}
|
|
||||||
}
|
|
||||||
56
common/collections/stack.go
Normal file
56
common/collections/stack.go
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
package collections
|
||||||
|
|
||||||
|
import "sync"
|
||||||
|
|
||||||
|
type (
|
||||||
|
stack struct {
|
||||||
|
top *node
|
||||||
|
length int
|
||||||
|
lock *sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
node struct {
|
||||||
|
value interface{}
|
||||||
|
prev *node
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewStack Create a new stack
|
||||||
|
func NewStack() *stack {
|
||||||
|
return &stack{nil, 0, &sync.RWMutex{}}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Len Return the number of items in the stack
|
||||||
|
func (this *stack) Len() int {
|
||||||
|
return this.length
|
||||||
|
}
|
||||||
|
|
||||||
|
// Peek View the top item on the stack
|
||||||
|
func (this *stack) Peek() interface{} {
|
||||||
|
if this.length == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return this.top.value
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pop the top item of the stack and return it
|
||||||
|
func (this *stack) Pop() interface{} {
|
||||||
|
this.lock.Lock()
|
||||||
|
defer this.lock.Unlock()
|
||||||
|
if this.length == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
n := this.top
|
||||||
|
this.top = n.prev
|
||||||
|
this.length--
|
||||||
|
return n.value
|
||||||
|
}
|
||||||
|
|
||||||
|
// Push a value onto the top of the stack
|
||||||
|
func (this *stack) Push(value interface{}) {
|
||||||
|
this.lock.Lock()
|
||||||
|
defer this.lock.Unlock()
|
||||||
|
n := &node{value, this.top}
|
||||||
|
this.top = n
|
||||||
|
this.length++
|
||||||
|
}
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
package contextutils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
func afterFunc(ctx context.Context, f func()) (stop func() bool) {
|
|
||||||
stopc := make(chan struct{})
|
|
||||||
once := sync.Once{} // either starts running f or stops f from running
|
|
||||||
if ctx.Done() != nil {
|
|
||||||
go func() {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
once.Do(func() {
|
|
||||||
go f()
|
|
||||||
})
|
|
||||||
case <-stopc:
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
return func() bool {
|
|
||||||
stopped := false
|
|
||||||
once.Do(func() {
|
|
||||||
stopped = true
|
|
||||||
close(stopc)
|
|
||||||
})
|
|
||||||
return stopped
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
//go:build !go1.21
|
|
||||||
|
|
||||||
package contextutils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
)
|
|
||||||
|
|
||||||
func AfterFunc(ctx context.Context, f func()) (stop func() bool) {
|
|
||||||
return afterFunc(ctx, f)
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
//go:build go1.21
|
|
||||||
|
|
||||||
package contextutils
|
|
||||||
|
|
||||||
import "context"
|
|
||||||
|
|
||||||
func AfterFunc(ctx context.Context, f func()) (stop func() bool) {
|
|
||||||
return context.AfterFunc(ctx, f)
|
|
||||||
}
|
|
||||||
@@ -1,100 +0,0 @@
|
|||||||
package contextutils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
shortDuration = 1 * time.Millisecond // a reasonable duration to block in a test
|
|
||||||
veryLongDuration = 1000 * time.Hour // an arbitrary upper bound on the test's running time
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestAfterFuncCalledAfterCancel(t *testing.T) {
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
donec := make(chan struct{})
|
|
||||||
stop := afterFunc(ctx, func() {
|
|
||||||
close(donec)
|
|
||||||
})
|
|
||||||
select {
|
|
||||||
case <-donec:
|
|
||||||
t.Fatalf("AfterFunc called before context is done")
|
|
||||||
case <-time.After(shortDuration):
|
|
||||||
}
|
|
||||||
cancel()
|
|
||||||
select {
|
|
||||||
case <-donec:
|
|
||||||
case <-time.After(veryLongDuration):
|
|
||||||
t.Fatalf("AfterFunc not called after context is canceled")
|
|
||||||
}
|
|
||||||
if stop() {
|
|
||||||
t.Fatalf("stop() = true, want false")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAfterFuncCalledAfterTimeout(t *testing.T) {
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), shortDuration)
|
|
||||||
defer cancel()
|
|
||||||
donec := make(chan struct{})
|
|
||||||
afterFunc(ctx, func() {
|
|
||||||
close(donec)
|
|
||||||
})
|
|
||||||
select {
|
|
||||||
case <-donec:
|
|
||||||
case <-time.After(veryLongDuration):
|
|
||||||
t.Fatalf("AfterFunc not called after context is canceled")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAfterFuncCalledImmediately(t *testing.T) {
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
cancel()
|
|
||||||
donec := make(chan struct{})
|
|
||||||
afterFunc(ctx, func() {
|
|
||||||
close(donec)
|
|
||||||
})
|
|
||||||
select {
|
|
||||||
case <-donec:
|
|
||||||
case <-time.After(veryLongDuration):
|
|
||||||
t.Fatalf("AfterFunc not called for already-canceled context")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAfterFuncNotCalledAfterStop(t *testing.T) {
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
donec := make(chan struct{})
|
|
||||||
stop := afterFunc(ctx, func() {
|
|
||||||
close(donec)
|
|
||||||
})
|
|
||||||
if !stop() {
|
|
||||||
t.Fatalf("stop() = false, want true")
|
|
||||||
}
|
|
||||||
cancel()
|
|
||||||
select {
|
|
||||||
case <-donec:
|
|
||||||
t.Fatalf("AfterFunc called for already-canceled context")
|
|
||||||
case <-time.After(shortDuration):
|
|
||||||
}
|
|
||||||
if stop() {
|
|
||||||
t.Fatalf("stop() = true, want false")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// This test verifies that canceling a context does not block waiting for AfterFuncs to finish.
|
|
||||||
func TestAfterFuncCalledAsynchronously(t *testing.T) {
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
donec := make(chan struct{})
|
|
||||||
stop := afterFunc(ctx, func() {
|
|
||||||
// The channel send blocks until donec is read from.
|
|
||||||
donec <- struct{}{}
|
|
||||||
})
|
|
||||||
defer stop()
|
|
||||||
cancel()
|
|
||||||
// After cancel returns, read from donec and unblock the AfterFunc.
|
|
||||||
select {
|
|
||||||
case <-donec:
|
|
||||||
case <-time.After(veryLongDuration):
|
|
||||||
t.Fatalf("AfterFunc not called after context is canceled")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -9,10 +9,10 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/log"
|
"github.com/Dreamacro/clash/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ConvertsV2Ray convert V2Ray subscribe proxies data to mihomo proxies config
|
// ConvertsV2Ray convert V2Ray subscribe proxies data to clash proxies config
|
||||||
func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
||||||
data := DecodeBase64(buf)
|
data := DecodeBase64(buf)
|
||||||
|
|
||||||
@@ -68,8 +68,7 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|||||||
hysteria["skip-cert-verify"], _ = strconv.ParseBool(query.Get("insecure"))
|
hysteria["skip-cert-verify"], _ = strconv.ParseBool(query.Get("insecure"))
|
||||||
|
|
||||||
proxies = append(proxies, hysteria)
|
proxies = append(proxies, hysteria)
|
||||||
|
case "hysteria2":
|
||||||
case "hysteria2", "hy2":
|
|
||||||
urlHysteria2, err := url.Parse(line)
|
urlHysteria2, err := url.Parse(line)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
@@ -80,7 +79,7 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|||||||
hysteria2 := make(map[string]any, 20)
|
hysteria2 := make(map[string]any, 20)
|
||||||
|
|
||||||
hysteria2["name"] = name
|
hysteria2["name"] = name
|
||||||
hysteria2["type"] = "hysteria2"
|
hysteria2["type"] = scheme
|
||||||
hysteria2["server"] = urlHysteria2.Hostname()
|
hysteria2["server"] = urlHysteria2.Hostname()
|
||||||
if port := urlHysteria2.Port(); port != "" {
|
if port := urlHysteria2.Port(); port != "" {
|
||||||
hysteria2["port"] = port
|
hysteria2["port"] = port
|
||||||
@@ -102,7 +101,6 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|||||||
hysteria2["up"] = query.Get("up")
|
hysteria2["up"] = query.Get("up")
|
||||||
|
|
||||||
proxies = append(proxies, hysteria2)
|
proxies = append(proxies, hysteria2)
|
||||||
|
|
||||||
case "tuic":
|
case "tuic":
|
||||||
// A temporary unofficial TUIC share link standard
|
// A temporary unofficial TUIC share link standard
|
||||||
// Modified from https://github.com/daeuniverse/dae/discussions/182
|
// Modified from https://github.com/daeuniverse/dae/discussions/182
|
||||||
@@ -144,8 +142,6 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|||||||
tuic["udp-relay-mode"] = udpRelayMode
|
tuic["udp-relay-mode"] = udpRelayMode
|
||||||
}
|
}
|
||||||
|
|
||||||
proxies = append(proxies, tuic)
|
|
||||||
|
|
||||||
case "trojan":
|
case "trojan":
|
||||||
urlTrojan, err := url.Parse(line)
|
urlTrojan, err := url.Parse(line)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -275,24 +271,22 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|||||||
vmess["skip-cert-verify"] = false
|
vmess["skip-cert-verify"] = false
|
||||||
|
|
||||||
vmess["cipher"] = "auto"
|
vmess["cipher"] = "auto"
|
||||||
if cipher, ok := values["scy"].(string); ok && cipher != "" {
|
if cipher, ok := values["scy"]; ok && cipher != "" {
|
||||||
vmess["cipher"] = cipher
|
vmess["cipher"] = cipher
|
||||||
}
|
}
|
||||||
|
|
||||||
if sni, ok := values["sni"].(string); ok && sni != "" {
|
if sni, ok := values["sni"]; ok && sni != "" {
|
||||||
vmess["servername"] = sni
|
vmess["servername"] = sni
|
||||||
}
|
}
|
||||||
|
|
||||||
network, ok := values["net"].(string)
|
network, _ := values["net"].(string)
|
||||||
if ok {
|
network = strings.ToLower(network)
|
||||||
network = strings.ToLower(network)
|
if values["type"] == "http" {
|
||||||
if values["type"] == "http" {
|
network = "http"
|
||||||
network = "http"
|
} else if network == "http" {
|
||||||
} else if network == "http" {
|
network = "h2"
|
||||||
network = "h2"
|
|
||||||
}
|
|
||||||
vmess["network"] = network
|
|
||||||
}
|
}
|
||||||
|
vmess["network"] = network
|
||||||
|
|
||||||
tls, ok := values["tls"].(string)
|
tls, ok := values["tls"].(string)
|
||||||
if ok {
|
if ok {
|
||||||
@@ -309,12 +303,12 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|||||||
case "http":
|
case "http":
|
||||||
headers := make(map[string]any)
|
headers := make(map[string]any)
|
||||||
httpOpts := make(map[string]any)
|
httpOpts := make(map[string]any)
|
||||||
if host, ok := values["host"].(string); ok && host != "" {
|
if host, ok := values["host"]; ok && host != "" {
|
||||||
headers["Host"] = []string{host}
|
headers["Host"] = []string{host.(string)}
|
||||||
}
|
}
|
||||||
httpOpts["path"] = []string{"/"}
|
httpOpts["path"] = []string{"/"}
|
||||||
if path, ok := values["path"].(string); ok && path != "" {
|
if path, ok := values["path"]; ok && path != "" {
|
||||||
httpOpts["path"] = []string{path}
|
httpOpts["path"] = []string{path.(string)}
|
||||||
}
|
}
|
||||||
httpOpts["headers"] = headers
|
httpOpts["headers"] = headers
|
||||||
|
|
||||||
@@ -323,8 +317,8 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|||||||
case "h2":
|
case "h2":
|
||||||
headers := make(map[string]any)
|
headers := make(map[string]any)
|
||||||
h2Opts := make(map[string]any)
|
h2Opts := make(map[string]any)
|
||||||
if host, ok := values["host"].(string); ok && host != "" {
|
if host, ok := values["host"]; ok && host != "" {
|
||||||
headers["Host"] = []string{host}
|
headers["Host"] = []string{host.(string)}
|
||||||
}
|
}
|
||||||
|
|
||||||
h2Opts["path"] = values["path"]
|
h2Opts["path"] = values["path"]
|
||||||
@@ -332,38 +326,15 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|||||||
|
|
||||||
vmess["h2-opts"] = h2Opts
|
vmess["h2-opts"] = h2Opts
|
||||||
|
|
||||||
case "ws", "httpupgrade":
|
case "ws":
|
||||||
headers := make(map[string]any)
|
headers := make(map[string]any)
|
||||||
wsOpts := make(map[string]any)
|
wsOpts := make(map[string]any)
|
||||||
wsOpts["path"] = "/"
|
wsOpts["path"] = []string{"/"}
|
||||||
if host, ok := values["host"].(string); ok && host != "" {
|
if host, ok := values["host"]; ok && host != "" {
|
||||||
headers["Host"] = host
|
headers["Host"] = host.(string)
|
||||||
}
|
}
|
||||||
if path, ok := values["path"].(string); ok && path != "" {
|
if path, ok := values["path"]; ok && path != "" {
|
||||||
path := path
|
wsOpts["path"] = path.(string)
|
||||||
pathURL, err := url.Parse(path)
|
|
||||||
if err == nil {
|
|
||||||
query := pathURL.Query()
|
|
||||||
if earlyData := query.Get("ed"); earlyData != "" {
|
|
||||||
med, err := strconv.Atoi(earlyData)
|
|
||||||
if err == nil {
|
|
||||||
switch network {
|
|
||||||
case "ws":
|
|
||||||
wsOpts["max-early-data"] = med
|
|
||||||
wsOpts["early-data-header-name"] = "Sec-WebSocket-Protocol"
|
|
||||||
case "httpupgrade":
|
|
||||||
wsOpts["v2ray-http-upgrade-fast-open"] = true
|
|
||||||
}
|
|
||||||
query.Del("ed")
|
|
||||||
pathURL.RawQuery = query.Encode()
|
|
||||||
path = pathURL.String()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if earlyDataHeader := query.Get("eh"); earlyDataHeader != "" {
|
|
||||||
wsOpts["early-data-header-name"] = earlyDataHeader
|
|
||||||
}
|
|
||||||
}
|
|
||||||
wsOpts["path"] = path
|
|
||||||
}
|
}
|
||||||
wsOpts["headers"] = headers
|
wsOpts["headers"] = headers
|
||||||
vmess["ws-opts"] = wsOpts
|
vmess["ws-opts"] = wsOpts
|
||||||
@@ -432,27 +403,14 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|||||||
if query.Get("udp-over-tcp") == "true" || query.Get("uot") == "1" {
|
if query.Get("udp-over-tcp") == "true" || query.Get("uot") == "1" {
|
||||||
ss["udp-over-tcp"] = true
|
ss["udp-over-tcp"] = true
|
||||||
}
|
}
|
||||||
plugin := query.Get("plugin")
|
if strings.Contains(query.Get("plugin"), "obfs") {
|
||||||
if strings.Contains(plugin, ";") {
|
obfsParams := strings.Split(query.Get("plugin"), ";")
|
||||||
pluginInfo, _ := url.ParseQuery("pluginName=" + strings.ReplaceAll(plugin, ";", "&"))
|
ss["plugin"] = "obfs"
|
||||||
pluginName := pluginInfo.Get("pluginName")
|
ss["plugin-opts"] = map[string]any{
|
||||||
if strings.Contains(pluginName, "obfs") {
|
"host": obfsParams[2][10:],
|
||||||
ss["plugin"] = "obfs"
|
"mode": obfsParams[1][5:],
|
||||||
ss["plugin-opts"] = map[string]any{
|
|
||||||
"mode": pluginInfo.Get("obfs"),
|
|
||||||
"host": pluginInfo.Get("obfs-host"),
|
|
||||||
}
|
|
||||||
} else if strings.Contains(pluginName, "v2ray-plugin") {
|
|
||||||
ss["plugin"] = "v2ray-plugin"
|
|
||||||
ss["plugin-opts"] = map[string]any{
|
|
||||||
"mode": pluginInfo.Get("mode"),
|
|
||||||
"host": pluginInfo.Get("host"),
|
|
||||||
"path": pluginInfo.Get("path"),
|
|
||||||
"tls": strings.Contains(plugin, "tls"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
proxies = append(proxies, ss)
|
proxies = append(proxies, ss)
|
||||||
|
|
||||||
case "ssr":
|
case "ssr":
|
||||||
|
|||||||
@@ -6,10 +6,10 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/utils"
|
"github.com/Dreamacro/clash/common/utils"
|
||||||
|
|
||||||
"github.com/metacubex/randv2"
|
|
||||||
"github.com/metacubex/sing-shadowsocks/shadowimpl"
|
"github.com/metacubex/sing-shadowsocks/shadowimpl"
|
||||||
|
"github.com/zhangyunhao116/fastrand"
|
||||||
)
|
)
|
||||||
|
|
||||||
var hostsSuffix = []string{
|
var hostsSuffix = []string{
|
||||||
@@ -302,11 +302,11 @@ func RandHost() string {
|
|||||||
prefix += string(buf[6:8]) + "-"
|
prefix += string(buf[6:8]) + "-"
|
||||||
prefix += string(buf[len(buf)-8:])
|
prefix += string(buf[len(buf)-8:])
|
||||||
|
|
||||||
return prefix + hostsSuffix[randv2.IntN(hostsLen)]
|
return prefix + hostsSuffix[fastrand.Intn(hostsLen)]
|
||||||
}
|
}
|
||||||
|
|
||||||
func RandUserAgent() string {
|
func RandUserAgent() string {
|
||||||
return userAgents[randv2.IntN(uaLen)]
|
return userAgents[fastrand.Intn(uaLen)]
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetUserAgent(header http.Header) {
|
func SetUserAgent(header http.Header) {
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ func handleVShareLink(names map[string]int, url *url.URL, scheme string, proxy m
|
|||||||
h2Opts["headers"] = headers
|
h2Opts["headers"] = headers
|
||||||
proxy["h2-opts"] = h2Opts
|
proxy["h2-opts"] = h2Opts
|
||||||
|
|
||||||
case "ws", "httpupgrade":
|
case "ws":
|
||||||
headers := make(map[string]any)
|
headers := make(map[string]any)
|
||||||
wsOpts := make(map[string]any)
|
wsOpts := make(map[string]any)
|
||||||
headers["User-Agent"] = RandUserAgent()
|
headers["User-Agent"] = RandUserAgent()
|
||||||
@@ -113,13 +113,7 @@ func handleVShareLink(names map[string]int, url *url.URL, scheme string, proxy m
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("bad WebSocket max early data size: %v", err)
|
return fmt.Errorf("bad WebSocket max early data size: %v", err)
|
||||||
}
|
}
|
||||||
switch network {
|
wsOpts["max-early-data"] = med
|
||||||
case "ws":
|
|
||||||
wsOpts["max-early-data"] = med
|
|
||||||
wsOpts["early-data-header-name"] = "Sec-WebSocket-Protocol"
|
|
||||||
case "httpupgrade":
|
|
||||||
wsOpts["v2ray-http-upgrade-fast-open"] = true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if earlyDataHeader := query.Get("eh"); earlyDataHeader != "" {
|
if earlyDataHeader := query.Get("eh"); earlyDataHeader != "" {
|
||||||
wsOpts["early-data-header-name"] = earlyDataHeader
|
wsOpts["early-data-header-name"] = earlyDataHeader
|
||||||
|
|||||||
235
common/generics/list/list.go
Normal file
235
common/generics/list/list.go
Normal file
@@ -0,0 +1,235 @@
|
|||||||
|
// Copyright 2009 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package list implements a doubly linked list.
|
||||||
|
//
|
||||||
|
// To iterate over a list (where l is a *List):
|
||||||
|
//
|
||||||
|
// for e := l.Front(); e != nil; e = e.Next() {
|
||||||
|
// // do something with e.Value
|
||||||
|
// }
|
||||||
|
package list
|
||||||
|
|
||||||
|
// Element is an element of a linked list.
|
||||||
|
type Element[T any] struct {
|
||||||
|
// Next and previous pointers in the doubly-linked list of elements.
|
||||||
|
// To simplify the implementation, internally a list l is implemented
|
||||||
|
// as a ring, such that &l.root is both the next element of the last
|
||||||
|
// list element (l.Back()) and the previous element of the first list
|
||||||
|
// element (l.Front()).
|
||||||
|
next, prev *Element[T]
|
||||||
|
|
||||||
|
// The list to which this element belongs.
|
||||||
|
list *List[T]
|
||||||
|
|
||||||
|
// The value stored with this element.
|
||||||
|
Value T
|
||||||
|
}
|
||||||
|
|
||||||
|
// Next returns the next list element or nil.
|
||||||
|
func (e *Element[T]) Next() *Element[T] {
|
||||||
|
if p := e.next; e.list != nil && p != &e.list.root {
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prev returns the previous list element or nil.
|
||||||
|
func (e *Element[T]) Prev() *Element[T] {
|
||||||
|
if p := e.prev; e.list != nil && p != &e.list.root {
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// List represents a doubly linked list.
|
||||||
|
// The zero value for List is an empty list ready to use.
|
||||||
|
type List[T any] struct {
|
||||||
|
root Element[T] // sentinel list element, only &root, root.prev, and root.next are used
|
||||||
|
len int // current list length excluding (this) sentinel element
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init initializes or clears list l.
|
||||||
|
func (l *List[T]) Init() *List[T] {
|
||||||
|
l.root.next = &l.root
|
||||||
|
l.root.prev = &l.root
|
||||||
|
l.len = 0
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns an initialized list.
|
||||||
|
func New[T any]() *List[T] { return new(List[T]).Init() }
|
||||||
|
|
||||||
|
// Len returns the number of elements of list l.
|
||||||
|
// The complexity is O(1).
|
||||||
|
func (l *List[T]) Len() int { return l.len }
|
||||||
|
|
||||||
|
// Front returns the first element of list l or nil if the list is empty.
|
||||||
|
func (l *List[T]) Front() *Element[T] {
|
||||||
|
if l.len == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return l.root.next
|
||||||
|
}
|
||||||
|
|
||||||
|
// Back returns the last element of list l or nil if the list is empty.
|
||||||
|
func (l *List[T]) Back() *Element[T] {
|
||||||
|
if l.len == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return l.root.prev
|
||||||
|
}
|
||||||
|
|
||||||
|
// lazyInit lazily initializes a zero List value.
|
||||||
|
func (l *List[T]) lazyInit() {
|
||||||
|
if l.root.next == nil {
|
||||||
|
l.Init()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// insert inserts e after at, increments l.len, and returns e.
|
||||||
|
func (l *List[T]) insert(e, at *Element[T]) *Element[T] {
|
||||||
|
e.prev = at
|
||||||
|
e.next = at.next
|
||||||
|
e.prev.next = e
|
||||||
|
e.next.prev = e
|
||||||
|
e.list = l
|
||||||
|
l.len++
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
|
||||||
|
// insertValue is a convenience wrapper for insert(&Element{Value: v}, at).
|
||||||
|
func (l *List[T]) insertValue(v T, at *Element[T]) *Element[T] {
|
||||||
|
return l.insert(&Element[T]{Value: v}, at)
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove removes e from its list, decrements l.len
|
||||||
|
func (l *List[T]) remove(e *Element[T]) {
|
||||||
|
e.prev.next = e.next
|
||||||
|
e.next.prev = e.prev
|
||||||
|
e.next = nil // avoid memory leaks
|
||||||
|
e.prev = nil // avoid memory leaks
|
||||||
|
e.list = nil
|
||||||
|
l.len--
|
||||||
|
}
|
||||||
|
|
||||||
|
// move moves e to next to at.
|
||||||
|
func (l *List[T]) move(e, at *Element[T]) {
|
||||||
|
if e == at {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
e.prev.next = e.next
|
||||||
|
e.next.prev = e.prev
|
||||||
|
|
||||||
|
e.prev = at
|
||||||
|
e.next = at.next
|
||||||
|
e.prev.next = e
|
||||||
|
e.next.prev = e
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove removes e from l if e is an element of list l.
|
||||||
|
// It returns the element value e.Value.
|
||||||
|
// The element must not be nil.
|
||||||
|
func (l *List[T]) Remove(e *Element[T]) T {
|
||||||
|
if e.list == l {
|
||||||
|
// if e.list == l, l must have been initialized when e was inserted
|
||||||
|
// in l or l == nil (e is a zero Element) and l.remove will crash
|
||||||
|
l.remove(e)
|
||||||
|
}
|
||||||
|
return e.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
// PushFront inserts a new element e with value v at the front of list l and returns e.
|
||||||
|
func (l *List[T]) PushFront(v T) *Element[T] {
|
||||||
|
l.lazyInit()
|
||||||
|
return l.insertValue(v, &l.root)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PushBack inserts a new element e with value v at the back of list l and returns e.
|
||||||
|
func (l *List[T]) PushBack(v T) *Element[T] {
|
||||||
|
l.lazyInit()
|
||||||
|
return l.insertValue(v, l.root.prev)
|
||||||
|
}
|
||||||
|
|
||||||
|
// InsertBefore inserts a new element e with value v immediately before mark and returns e.
|
||||||
|
// If mark is not an element of l, the list is not modified.
|
||||||
|
// The mark must not be nil.
|
||||||
|
func (l *List[T]) InsertBefore(v T, mark *Element[T]) *Element[T] {
|
||||||
|
if mark.list != l {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// see comment in List.Remove about initialization of l
|
||||||
|
return l.insertValue(v, mark.prev)
|
||||||
|
}
|
||||||
|
|
||||||
|
// InsertAfter inserts a new element e with value v immediately after mark and returns e.
|
||||||
|
// If mark is not an element of l, the list is not modified.
|
||||||
|
// The mark must not be nil.
|
||||||
|
func (l *List[T]) InsertAfter(v T, mark *Element[T]) *Element[T] {
|
||||||
|
if mark.list != l {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// see comment in List.Remove about initialization of l
|
||||||
|
return l.insertValue(v, mark)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MoveToFront moves element e to the front of list l.
|
||||||
|
// If e is not an element of l, the list is not modified.
|
||||||
|
// The element must not be nil.
|
||||||
|
func (l *List[T]) MoveToFront(e *Element[T]) {
|
||||||
|
if e.list != l || l.root.next == e {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// see comment in List.Remove about initialization of l
|
||||||
|
l.move(e, &l.root)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MoveToBack moves element e to the back of list l.
|
||||||
|
// If e is not an element of l, the list is not modified.
|
||||||
|
// The element must not be nil.
|
||||||
|
func (l *List[T]) MoveToBack(e *Element[T]) {
|
||||||
|
if e.list != l || l.root.prev == e {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// see comment in List.Remove about initialization of l
|
||||||
|
l.move(e, l.root.prev)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MoveBefore moves element e to its new position before mark.
|
||||||
|
// If e or mark is not an element of l, or e == mark, the list is not modified.
|
||||||
|
// The element and mark must not be nil.
|
||||||
|
func (l *List[T]) MoveBefore(e, mark *Element[T]) {
|
||||||
|
if e.list != l || e == mark || mark.list != l {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
l.move(e, mark.prev)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MoveAfter moves element e to its new position after mark.
|
||||||
|
// If e or mark is not an element of l, or e == mark, the list is not modified.
|
||||||
|
// The element and mark must not be nil.
|
||||||
|
func (l *List[T]) MoveAfter(e, mark *Element[T]) {
|
||||||
|
if e.list != l || e == mark || mark.list != l {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
l.move(e, mark)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PushBackList inserts a copy of another list at the back of list l.
|
||||||
|
// The lists l and other may be the same. They must not be nil.
|
||||||
|
func (l *List[T]) PushBackList(other *List[T]) {
|
||||||
|
l.lazyInit()
|
||||||
|
for i, e := other.Len(), other.Front(); i > 0; i, e = i-1, e.Next() {
|
||||||
|
l.insertValue(e.Value, l.root.prev)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// PushFrontList inserts a copy of another list at the front of list l.
|
||||||
|
// The lists l and other may be the same. They must not be nil.
|
||||||
|
func (l *List[T]) PushFrontList(other *List[T]) {
|
||||||
|
l.lazyInit()
|
||||||
|
for i, e := other.Len(), other.Back(); i > 0; i, e = i-1, e.Prev() {
|
||||||
|
l.insertValue(e.Value, &l.root)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/buf"
|
"github.com/Dreamacro/clash/common/buf"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ ExtendedConn = (*BufferedConn)(nil)
|
var _ ExtendedConn = (*BufferedConn)(nil)
|
||||||
@@ -22,16 +22,6 @@ func NewBufferedConn(c net.Conn) *BufferedConn {
|
|||||||
return &BufferedConn{bufio.NewReader(c), NewExtendedConn(c), false}
|
return &BufferedConn{bufio.NewReader(c), NewExtendedConn(c), false}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WarpConnWithBioReader(c net.Conn, br *bufio.Reader) net.Conn {
|
|
||||||
if br != nil && br.Buffered() > 0 {
|
|
||||||
if bc, ok := c.(*BufferedConn); ok && bc.r == br {
|
|
||||||
return bc
|
|
||||||
}
|
|
||||||
return &BufferedConn{br, NewExtendedConn(c), true}
|
|
||||||
}
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reader returns the internal bufio.Reader.
|
// Reader returns the internal bufio.Reader.
|
||||||
func (c *BufferedConn) Reader() *bufio.Reader {
|
func (c *BufferedConn) Reader() *bufio.Reader {
|
||||||
return c.r
|
return c.r
|
||||||
@@ -84,9 +74,9 @@ func (c *BufferedConn) ReadCached() *buf.Buffer { // call in sing/common/bufio.C
|
|||||||
length := c.r.Buffered()
|
length := c.r.Buffered()
|
||||||
b, _ := c.r.Peek(length)
|
b, _ := c.r.Peek(length)
|
||||||
_, _ = c.r.Discard(length)
|
_, _ = c.r.Discard(length)
|
||||||
|
c.r = nil // drop bufio.Reader to let gc can clean up its internal buf
|
||||||
return buf.As(b)
|
return buf.As(b)
|
||||||
}
|
}
|
||||||
c.r = nil // drop bufio.Reader to let gc can clean up its internal buf
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,34 +0,0 @@
|
|||||||
package net
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
"unsafe"
|
|
||||||
)
|
|
||||||
|
|
||||||
// bufioReader copy from stdlib bufio/bufio.go
|
|
||||||
// This structure has remained unchanged from go1.5 to go1.21.
|
|
||||||
type bufioReader struct {
|
|
||||||
buf []byte
|
|
||||||
rd io.Reader // reader provided by the client
|
|
||||||
r, w int // buf read and write positions
|
|
||||||
err error
|
|
||||||
lastByte int // last byte read for UnreadByte; -1 means invalid
|
|
||||||
lastRuneSize int // size of last rune read for UnreadRune; -1 means invalid
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *BufferedConn) AppendData(buf []byte) (ok bool) {
|
|
||||||
b := (*bufioReader)(unsafe.Pointer(c.r))
|
|
||||||
pos := len(b.buf) - b.w - len(buf)
|
|
||||||
if pos >= -b.r { // len(b.buf)-(b.w - b.r) >= len(buf)
|
|
||||||
if pos < 0 { // len(b.buf)-b.w < len(buf)
|
|
||||||
// Slide existing data to beginning.
|
|
||||||
copy(b.buf, b.buf[b.r:b.w])
|
|
||||||
b.w -= b.r
|
|
||||||
b.r = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
b.w += copy(b.buf[b.w:], buf)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
package net
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net"
|
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/buf"
|
|
||||||
)
|
|
||||||
|
|
||||||
var _ ExtendedConn = (*CachedConn)(nil)
|
|
||||||
|
|
||||||
type CachedConn struct {
|
|
||||||
ExtendedConn
|
|
||||||
data []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCachedConn(c net.Conn, data []byte) *CachedConn {
|
|
||||||
return &CachedConn{NewExtendedConn(c), data}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *CachedConn) Read(b []byte) (n int, err error) {
|
|
||||||
if len(c.data) > 0 {
|
|
||||||
n = copy(b, c.data)
|
|
||||||
c.data = c.data[n:]
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return c.ExtendedConn.Read(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *CachedConn) ReadCached() *buf.Buffer { // call in sing/common/bufio.Copy
|
|
||||||
if len(c.data) > 0 {
|
|
||||||
return buf.As(c.data)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *CachedConn) Upstream() any {
|
|
||||||
return c.ExtendedConn
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *CachedConn) ReaderReplaceable() bool {
|
|
||||||
if len(c.data) > 0 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *CachedConn) WriterReplaceable() bool {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
package net
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"net"
|
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/contextutils"
|
|
||||||
)
|
|
||||||
|
|
||||||
// SetupContextForConn is a helper function that starts connection I/O interrupter.
|
|
||||||
// if ctx be canceled before done called, it will close the connection.
|
|
||||||
// should use like this:
|
|
||||||
//
|
|
||||||
// func streamConn(ctx context.Context, conn net.Conn) (_ net.Conn, err error) {
|
|
||||||
// if ctx.Done() != nil {
|
|
||||||
// done := N.SetupContextForConn(ctx, conn)
|
|
||||||
// defer done(&err)
|
|
||||||
// }
|
|
||||||
// conn, err := xxx
|
|
||||||
// return conn, err
|
|
||||||
// }
|
|
||||||
func SetupContextForConn(ctx context.Context, conn net.Conn) (done func(*error)) {
|
|
||||||
stopc := make(chan struct{})
|
|
||||||
stop := contextutils.AfterFunc(ctx, func() {
|
|
||||||
// Close the connection, discarding the error
|
|
||||||
_ = conn.Close()
|
|
||||||
close(stopc)
|
|
||||||
})
|
|
||||||
return func(inputErr *error) {
|
|
||||||
if !stop() {
|
|
||||||
// The AfterFunc was started, wait for it to complete.
|
|
||||||
<-stopc
|
|
||||||
if ctxErr := ctx.Err(); ctxErr != nil && inputErr != nil {
|
|
||||||
// Return context error to user.
|
|
||||||
*inputErr = ctxErr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,103 +0,0 @@
|
|||||||
package net_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"net"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
N "github.com/metacubex/mihomo/common/net"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func testRead(ctx context.Context, conn net.Conn) (err error) {
|
|
||||||
if ctx.Done() != nil {
|
|
||||||
done := N.SetupContextForConn(ctx, conn)
|
|
||||||
defer done(&err)
|
|
||||||
}
|
|
||||||
_, err = conn.Read(make([]byte, 1))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSetupContextForConnWithCancel(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
c1, c2 := N.Pipe()
|
|
||||||
defer c1.Close()
|
|
||||||
defer c2.Close()
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
errc := make(chan error)
|
|
||||||
go func() {
|
|
||||||
errc <- testRead(ctx, c1)
|
|
||||||
}()
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-errc:
|
|
||||||
t.Fatal("conn closed before cancel")
|
|
||||||
case <-time.After(100 * time.Millisecond):
|
|
||||||
cancel()
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case err := <-errc:
|
|
||||||
assert.ErrorIs(t, err, context.Canceled)
|
|
||||||
case <-time.After(100 * time.Millisecond):
|
|
||||||
t.Fatal("conn not be canceled")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSetupContextForConnWithTimeout1(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
c1, c2 := N.Pipe()
|
|
||||||
defer c1.Close()
|
|
||||||
defer c2.Close()
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
errc := make(chan error)
|
|
||||||
go func() {
|
|
||||||
errc <- testRead(ctx, c1)
|
|
||||||
}()
|
|
||||||
|
|
||||||
select {
|
|
||||||
case err := <-errc:
|
|
||||||
if !errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
|
||||||
t.Fatal("conn closed before timeout")
|
|
||||||
}
|
|
||||||
assert.ErrorIs(t, err, context.DeadlineExceeded)
|
|
||||||
case <-time.After(200 * time.Millisecond):
|
|
||||||
t.Fatal("conn not be canceled")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSetupContextForConnWithTimeout2(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
c1, c2 := N.Pipe()
|
|
||||||
defer c1.Close()
|
|
||||||
defer c2.Close()
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
errc := make(chan error)
|
|
||||||
go func() {
|
|
||||||
errc <- testRead(ctx, c1)
|
|
||||||
}()
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-errc:
|
|
||||||
t.Fatal("conn closed before cancel")
|
|
||||||
case <-time.After(100 * time.Millisecond):
|
|
||||||
c2.Write(make([]byte, 1))
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case err := <-errc:
|
|
||||||
assert.Nil(t, ctx.Err())
|
|
||||||
assert.Nil(t, err)
|
|
||||||
case <-time.After(200 * time.Millisecond):
|
|
||||||
t.Fatal("conn not be canceled")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,154 +0,0 @@
|
|||||||
package deadline
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net"
|
|
||||||
"os"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/atomic"
|
|
||||||
|
|
||||||
"github.com/metacubex/sing/common/buf"
|
|
||||||
"github.com/metacubex/sing/common/bufio"
|
|
||||||
"github.com/metacubex/sing/common/network"
|
|
||||||
)
|
|
||||||
|
|
||||||
type connReadResult struct {
|
|
||||||
buffer []byte
|
|
||||||
err error
|
|
||||||
}
|
|
||||||
|
|
||||||
type Conn struct {
|
|
||||||
network.ExtendedConn
|
|
||||||
deadline atomic.TypedValue[time.Time]
|
|
||||||
pipeDeadline pipeDeadline
|
|
||||||
disablePipe atomic.Bool
|
|
||||||
inRead atomic.Bool
|
|
||||||
resultCh chan *connReadResult
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsConn(conn any) bool {
|
|
||||||
_, ok := conn.(*Conn)
|
|
||||||
return ok
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewConn(conn net.Conn) *Conn {
|
|
||||||
c := &Conn{
|
|
||||||
ExtendedConn: bufio.NewExtendedConn(conn),
|
|
||||||
pipeDeadline: makePipeDeadline(),
|
|
||||||
resultCh: make(chan *connReadResult, 1),
|
|
||||||
}
|
|
||||||
c.resultCh <- nil
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Conn) Read(p []byte) (n int, err error) {
|
|
||||||
select {
|
|
||||||
case result := <-c.resultCh:
|
|
||||||
if result != nil {
|
|
||||||
n = copy(p, result.buffer)
|
|
||||||
err = result.err
|
|
||||||
if n >= len(result.buffer) {
|
|
||||||
c.resultCh <- nil // finish cache read
|
|
||||||
} else {
|
|
||||||
result.buffer = result.buffer[n:]
|
|
||||||
c.resultCh <- result // push back for next call
|
|
||||||
}
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
c.resultCh <- nil
|
|
||||||
break
|
|
||||||
}
|
|
||||||
case <-c.pipeDeadline.wait():
|
|
||||||
return 0, os.ErrDeadlineExceeded
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.disablePipe.Load() {
|
|
||||||
return c.ExtendedConn.Read(p)
|
|
||||||
} else if c.deadline.Load().IsZero() {
|
|
||||||
c.inRead.Store(true)
|
|
||||||
defer c.inRead.Store(false)
|
|
||||||
return c.ExtendedConn.Read(p)
|
|
||||||
}
|
|
||||||
|
|
||||||
<-c.resultCh
|
|
||||||
go c.pipeRead(len(p))
|
|
||||||
|
|
||||||
return c.Read(p)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Conn) pipeRead(size int) {
|
|
||||||
buffer := make([]byte, size)
|
|
||||||
n, err := c.ExtendedConn.Read(buffer)
|
|
||||||
buffer = buffer[:n]
|
|
||||||
c.resultCh <- &connReadResult{
|
|
||||||
buffer: buffer,
|
|
||||||
err: err,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Conn) ReadBuffer(buffer *buf.Buffer) (err error) {
|
|
||||||
select {
|
|
||||||
case result := <-c.resultCh:
|
|
||||||
if result != nil {
|
|
||||||
n, _ := buffer.Write(result.buffer)
|
|
||||||
err = result.err
|
|
||||||
|
|
||||||
if n >= len(result.buffer) {
|
|
||||||
c.resultCh <- nil // finish cache read
|
|
||||||
} else {
|
|
||||||
result.buffer = result.buffer[n:]
|
|
||||||
c.resultCh <- result // push back for next call
|
|
||||||
}
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
c.resultCh <- nil
|
|
||||||
break
|
|
||||||
}
|
|
||||||
case <-c.pipeDeadline.wait():
|
|
||||||
return os.ErrDeadlineExceeded
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.disablePipe.Load() {
|
|
||||||
return c.ExtendedConn.ReadBuffer(buffer)
|
|
||||||
} else if c.deadline.Load().IsZero() {
|
|
||||||
c.inRead.Store(true)
|
|
||||||
defer c.inRead.Store(false)
|
|
||||||
return c.ExtendedConn.ReadBuffer(buffer)
|
|
||||||
}
|
|
||||||
|
|
||||||
<-c.resultCh
|
|
||||||
go c.pipeRead(buffer.FreeLen())
|
|
||||||
|
|
||||||
return c.ReadBuffer(buffer)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Conn) SetReadDeadline(t time.Time) error {
|
|
||||||
if c.disablePipe.Load() {
|
|
||||||
return c.ExtendedConn.SetReadDeadline(t)
|
|
||||||
} else if c.inRead.Load() {
|
|
||||||
c.disablePipe.Store(true)
|
|
||||||
return c.ExtendedConn.SetReadDeadline(t)
|
|
||||||
}
|
|
||||||
c.deadline.Store(t)
|
|
||||||
c.pipeDeadline.set(t)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Conn) ReaderReplaceable() bool {
|
|
||||||
select {
|
|
||||||
case result := <-c.resultCh:
|
|
||||||
c.resultCh <- result
|
|
||||||
if result != nil {
|
|
||||||
return false // cache reading
|
|
||||||
} else {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return false // pipe reading
|
|
||||||
}
|
|
||||||
return c.disablePipe.Load() || c.deadline.Load().IsZero()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Conn) Upstream() any {
|
|
||||||
return c.ExtendedConn
|
|
||||||
}
|
|
||||||
@@ -6,8 +6,8 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/atomic"
|
"github.com/Dreamacro/clash/common/atomic"
|
||||||
"github.com/metacubex/mihomo/common/net/packet"
|
"github.com/Dreamacro/clash/common/net/packet"
|
||||||
)
|
)
|
||||||
|
|
||||||
type readResult struct {
|
type readResult struct {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/net/packet"
|
"github.com/Dreamacro/clash/common/net/packet"
|
||||||
)
|
)
|
||||||
|
|
||||||
type EnhancePacketConn struct {
|
type EnhancePacketConn struct {
|
||||||
|
|||||||
@@ -4,12 +4,11 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/common/net/packet"
|
"github.com/Dreamacro/clash/common/net/packet"
|
||||||
|
"github.com/sagernet/sing/common/buf"
|
||||||
"github.com/metacubex/sing/common/buf"
|
"github.com/sagernet/sing/common/bufio"
|
||||||
"github.com/metacubex/sing/common/bufio"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
M "github.com/metacubex/sing/common/metadata"
|
N "github.com/sagernet/sing/common/network"
|
||||||
N "github.com/metacubex/sing/common/network"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type SingPacketConn struct {
|
type SingPacketConn struct {
|
||||||
@@ -122,18 +121,17 @@ type singPacketReadWaiter struct {
|
|||||||
|
|
||||||
type singWaitReadResult singReadResult
|
type singWaitReadResult singReadResult
|
||||||
|
|
||||||
func (c *singPacketReadWaiter) InitializeReadWaiter(options N.ReadWaitOptions) (needCopy bool) {
|
func (c *singPacketReadWaiter) InitializeReadWaiter(newBuffer func() *buf.Buffer) {
|
||||||
return c.packetReadWaiter.InitializeReadWaiter(options)
|
c.packetReadWaiter.InitializeReadWaiter(newBuffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *singPacketReadWaiter) WaitReadPacket() (buffer *buf.Buffer, destination M.Socksaddr, err error) {
|
func (c *singPacketReadWaiter) WaitReadPacket() (destination M.Socksaddr, err error) {
|
||||||
FOR:
|
FOR:
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case result := <-c.netPacketConn.resultCh:
|
case result := <-c.netPacketConn.resultCh:
|
||||||
if result != nil {
|
if result != nil {
|
||||||
if result, ok := result.(*singWaitReadResult); ok {
|
if result, ok := result.(*singWaitReadResult); ok {
|
||||||
buffer = result.buffer
|
|
||||||
destination = result.destination
|
destination = result.destination
|
||||||
err = result.err
|
err = result.err
|
||||||
c.netPacketConn.resultCh <- nil // finish cache read
|
c.netPacketConn.resultCh <- nil // finish cache read
|
||||||
@@ -147,7 +145,7 @@ FOR:
|
|||||||
break FOR
|
break FOR
|
||||||
}
|
}
|
||||||
case <-c.netPacketConn.pipeDeadline.wait():
|
case <-c.netPacketConn.pipeDeadline.wait():
|
||||||
return nil, M.Socksaddr{}, os.ErrDeadlineExceeded
|
return M.Socksaddr{}, os.ErrDeadlineExceeded
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,7 +154,8 @@ FOR:
|
|||||||
} else if c.netPacketConn.deadline.Load().IsZero() {
|
} else if c.netPacketConn.deadline.Load().IsZero() {
|
||||||
c.netPacketConn.inRead.Store(true)
|
c.netPacketConn.inRead.Store(true)
|
||||||
defer c.netPacketConn.inRead.Store(false)
|
defer c.netPacketConn.inRead.Store(false)
|
||||||
return c.packetReadWaiter.WaitReadPacket()
|
destination, err = c.packetReadWaiter.WaitReadPacket()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
<-c.netPacketConn.resultCh
|
<-c.netPacketConn.resultCh
|
||||||
@@ -166,9 +165,8 @@ FOR:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *singPacketReadWaiter) pipeWaitReadPacket() {
|
func (c *singPacketReadWaiter) pipeWaitReadPacket() {
|
||||||
buffer, destination, err := c.packetReadWaiter.WaitReadPacket()
|
destination, err := c.packetReadWaiter.WaitReadPacket()
|
||||||
result := &singWaitReadResult{}
|
result := &singWaitReadResult{}
|
||||||
result.buffer = buffer
|
|
||||||
result.destination = destination
|
result.destination = destination
|
||||||
result.err = err
|
result.err = err
|
||||||
c.netPacketConn.resultCh <- result
|
c.netPacketConn.resultCh <- result
|
||||||
|
|||||||
@@ -1,222 +0,0 @@
|
|||||||
package deadline
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
"net"
|
|
||||||
"os"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/metacubex/sing/common/buf"
|
|
||||||
N "github.com/metacubex/sing/common/network"
|
|
||||||
)
|
|
||||||
|
|
||||||
type pipeAddr struct{}
|
|
||||||
|
|
||||||
func (pipeAddr) Network() string { return "pipe" }
|
|
||||||
func (pipeAddr) String() string { return "pipe" }
|
|
||||||
|
|
||||||
type pipe struct {
|
|
||||||
wrMu sync.Mutex // Serialize Write operations
|
|
||||||
|
|
||||||
// Used by local Read to interact with remote Write.
|
|
||||||
// Successful receive on rdRx is always followed by send on rdTx.
|
|
||||||
rdRx <-chan []byte
|
|
||||||
rdTx chan<- int
|
|
||||||
|
|
||||||
// Used by local Write to interact with remote Read.
|
|
||||||
// Successful send on wrTx is always followed by receive on wrRx.
|
|
||||||
wrTx chan<- []byte
|
|
||||||
wrRx <-chan int
|
|
||||||
|
|
||||||
once sync.Once // Protects closing localDone
|
|
||||||
localDone chan struct{}
|
|
||||||
remoteDone <-chan struct{}
|
|
||||||
|
|
||||||
readDeadline pipeDeadline
|
|
||||||
writeDeadline pipeDeadline
|
|
||||||
|
|
||||||
readWaitOptions N.ReadWaitOptions
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pipe creates a synchronous, in-memory, full duplex
|
|
||||||
// network connection; both ends implement the Conn interface.
|
|
||||||
// Reads on one end are matched with writes on the other,
|
|
||||||
// copying data directly between the two; there is no internal
|
|
||||||
// buffering.
|
|
||||||
func Pipe() (net.Conn, net.Conn) {
|
|
||||||
cb1 := make(chan []byte)
|
|
||||||
cb2 := make(chan []byte)
|
|
||||||
cn1 := make(chan int)
|
|
||||||
cn2 := make(chan int)
|
|
||||||
done1 := make(chan struct{})
|
|
||||||
done2 := make(chan struct{})
|
|
||||||
|
|
||||||
p1 := &pipe{
|
|
||||||
rdRx: cb1, rdTx: cn1,
|
|
||||||
wrTx: cb2, wrRx: cn2,
|
|
||||||
localDone: done1, remoteDone: done2,
|
|
||||||
readDeadline: makePipeDeadline(),
|
|
||||||
writeDeadline: makePipeDeadline(),
|
|
||||||
}
|
|
||||||
p2 := &pipe{
|
|
||||||
rdRx: cb2, rdTx: cn2,
|
|
||||||
wrTx: cb1, wrRx: cn1,
|
|
||||||
localDone: done2, remoteDone: done1,
|
|
||||||
readDeadline: makePipeDeadline(),
|
|
||||||
writeDeadline: makePipeDeadline(),
|
|
||||||
}
|
|
||||||
return p1, p2
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*pipe) LocalAddr() net.Addr { return pipeAddr{} }
|
|
||||||
func (*pipe) RemoteAddr() net.Addr { return pipeAddr{} }
|
|
||||||
|
|
||||||
func (p *pipe) Read(b []byte) (int, error) {
|
|
||||||
n, err := p.read(b)
|
|
||||||
if err != nil && err != io.EOF && err != io.ErrClosedPipe {
|
|
||||||
err = &net.OpError{Op: "read", Net: "pipe", Err: err}
|
|
||||||
}
|
|
||||||
return n, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *pipe) read(b []byte) (n int, err error) {
|
|
||||||
switch {
|
|
||||||
case isClosedChan(p.localDone):
|
|
||||||
return 0, io.ErrClosedPipe
|
|
||||||
case isClosedChan(p.remoteDone):
|
|
||||||
return 0, io.EOF
|
|
||||||
case isClosedChan(p.readDeadline.wait()):
|
|
||||||
return 0, os.ErrDeadlineExceeded
|
|
||||||
}
|
|
||||||
|
|
||||||
select {
|
|
||||||
case bw := <-p.rdRx:
|
|
||||||
nr := copy(b, bw)
|
|
||||||
p.rdTx <- nr
|
|
||||||
return nr, nil
|
|
||||||
case <-p.localDone:
|
|
||||||
return 0, io.ErrClosedPipe
|
|
||||||
case <-p.remoteDone:
|
|
||||||
return 0, io.EOF
|
|
||||||
case <-p.readDeadline.wait():
|
|
||||||
return 0, os.ErrDeadlineExceeded
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *pipe) Write(b []byte) (int, error) {
|
|
||||||
n, err := p.write(b)
|
|
||||||
if err != nil && err != io.ErrClosedPipe {
|
|
||||||
err = &net.OpError{Op: "write", Net: "pipe", Err: err}
|
|
||||||
}
|
|
||||||
return n, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *pipe) write(b []byte) (n int, err error) {
|
|
||||||
switch {
|
|
||||||
case isClosedChan(p.localDone):
|
|
||||||
return 0, io.ErrClosedPipe
|
|
||||||
case isClosedChan(p.remoteDone):
|
|
||||||
return 0, io.ErrClosedPipe
|
|
||||||
case isClosedChan(p.writeDeadline.wait()):
|
|
||||||
return 0, os.ErrDeadlineExceeded
|
|
||||||
}
|
|
||||||
|
|
||||||
p.wrMu.Lock() // Ensure entirety of b is written together
|
|
||||||
defer p.wrMu.Unlock()
|
|
||||||
for once := true; once || len(b) > 0; once = false {
|
|
||||||
select {
|
|
||||||
case p.wrTx <- b:
|
|
||||||
nw := <-p.wrRx
|
|
||||||
b = b[nw:]
|
|
||||||
n += nw
|
|
||||||
case <-p.localDone:
|
|
||||||
return n, io.ErrClosedPipe
|
|
||||||
case <-p.remoteDone:
|
|
||||||
return n, io.ErrClosedPipe
|
|
||||||
case <-p.writeDeadline.wait():
|
|
||||||
return n, os.ErrDeadlineExceeded
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return n, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *pipe) SetDeadline(t time.Time) error {
|
|
||||||
if isClosedChan(p.localDone) || isClosedChan(p.remoteDone) {
|
|
||||||
return io.ErrClosedPipe
|
|
||||||
}
|
|
||||||
p.readDeadline.set(t)
|
|
||||||
p.writeDeadline.set(t)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *pipe) SetReadDeadline(t time.Time) error {
|
|
||||||
if isClosedChan(p.localDone) || isClosedChan(p.remoteDone) {
|
|
||||||
return io.ErrClosedPipe
|
|
||||||
}
|
|
||||||
p.readDeadline.set(t)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *pipe) SetWriteDeadline(t time.Time) error {
|
|
||||||
if isClosedChan(p.localDone) || isClosedChan(p.remoteDone) {
|
|
||||||
return io.ErrClosedPipe
|
|
||||||
}
|
|
||||||
p.writeDeadline.set(t)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *pipe) Close() error {
|
|
||||||
p.once.Do(func() { close(p.localDone) })
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ N.ReadWaiter = (*pipe)(nil)
|
|
||||||
|
|
||||||
func (p *pipe) InitializeReadWaiter(options N.ReadWaitOptions) (needCopy bool) {
|
|
||||||
p.readWaitOptions = options
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *pipe) WaitReadBuffer() (buffer *buf.Buffer, err error) {
|
|
||||||
buffer, err = p.waitReadBuffer()
|
|
||||||
if err != nil && err != io.EOF && err != io.ErrClosedPipe {
|
|
||||||
err = &net.OpError{Op: "read", Net: "pipe", Err: err}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *pipe) waitReadBuffer() (buffer *buf.Buffer, err error) {
|
|
||||||
switch {
|
|
||||||
case isClosedChan(p.localDone):
|
|
||||||
return nil, io.ErrClosedPipe
|
|
||||||
case isClosedChan(p.remoteDone):
|
|
||||||
return nil, io.EOF
|
|
||||||
case isClosedChan(p.readDeadline.wait()):
|
|
||||||
return nil, os.ErrDeadlineExceeded
|
|
||||||
}
|
|
||||||
select {
|
|
||||||
case bw := <-p.rdRx:
|
|
||||||
buffer = p.readWaitOptions.NewBuffer()
|
|
||||||
var nr int
|
|
||||||
nr, err = buffer.Write(bw)
|
|
||||||
if err != nil {
|
|
||||||
buffer.Release()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
p.readWaitOptions.PostReturn(buffer)
|
|
||||||
p.rdTx <- nr
|
|
||||||
return
|
|
||||||
case <-p.localDone:
|
|
||||||
return nil, io.ErrClosedPipe
|
|
||||||
case <-p.remoteDone:
|
|
||||||
return nil, io.EOF
|
|
||||||
case <-p.readDeadline.wait():
|
|
||||||
return nil, os.ErrDeadlineExceeded
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsPipe(conn any) bool {
|
|
||||||
_, ok := conn.(*pipe)
|
|
||||||
return ok
|
|
||||||
}
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user