mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-03-03 20:27:31 +00:00
Compare commits
232 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bda71dbfa1 | ||
|
|
1a0932c210 | ||
|
|
bc74c943b8 | ||
|
|
cc6429722a | ||
|
|
3b57a923fd | ||
|
|
7efd692bbc | ||
|
|
d773d335a2 | ||
|
|
78ae8815c2 | ||
|
|
8f61b0e180 | ||
|
|
a974e810c2 | ||
|
|
599ce784d2 | ||
|
|
db973de7bd | ||
|
|
5f7053c519 | ||
|
|
84a334dd3a | ||
|
|
7d15ce2b33 | ||
|
|
37791acb59 | ||
|
|
96f0254a48 | ||
|
|
8b4499e461 | ||
|
|
6a3e28c384 | ||
|
|
b05cf14b98 | ||
|
|
a6b816b1c6 | ||
|
|
bb9ad6cac0 | ||
|
|
b9d48f4115 | ||
|
|
84299606f4 | ||
|
|
8efb699231 | ||
|
|
3c088b33a2 | ||
|
|
4362dfacc9 | ||
|
|
05b9071ca6 | ||
|
|
117228fa8c | ||
|
|
3a3d88c668 | ||
|
|
54a7f52fe3 | ||
|
|
1479b449df | ||
|
|
fef5ad780d | ||
|
|
aa3c1ac623 | ||
|
|
b5a8f0fce1 | ||
|
|
d9cfdc3242 | ||
|
|
b73382f60a | ||
|
|
9e96d70840 | ||
|
|
d28c3b50e3 | ||
|
|
2f203330e4 | ||
|
|
7d222b1b71 | ||
|
|
d85d8ac13f | ||
|
|
7979eb654f | ||
|
|
2577dd3af4 | ||
|
|
daa332e7b0 | ||
|
|
288c0c27d6 | ||
|
|
832dae3421 | ||
|
|
fe7c1a2cdb | ||
|
|
e8e4288d85 | ||
|
|
6901afb406 | ||
|
|
f260d8cf01 | ||
|
|
575c1d4129 | ||
|
|
253b023442 | ||
|
|
17c9d507be | ||
|
|
8c3557e96b | ||
|
|
228990472d | ||
|
|
09e7866a5c | ||
|
|
665ba7f9f1 | ||
|
|
ee3038d5e4 | ||
|
|
885ee7a820 | ||
|
|
ef303b11f2 | ||
|
|
a82ce85707 | ||
|
|
5bfe7ba169 | ||
|
|
ceac5bfaa4 | ||
|
|
b0638cfc49 | ||
|
|
96220aa8ea | ||
|
|
8ff476a3a1 | ||
|
|
261b6e8dce | ||
|
|
2b9141e0e5 | ||
|
|
55255faa52 | ||
|
|
d42e3f74ad | ||
|
|
81a8a63861 | ||
|
|
c3a61e2db5 | ||
|
|
bffe47a974 | ||
|
|
4314b37d04 | ||
|
|
cf93f69f40 | ||
|
|
55f626424f | ||
|
|
431d52f250 | ||
|
|
c1f24d8f0e | ||
|
|
fc5a3cf80c | ||
|
|
e1e999180a | ||
|
|
8755618910 | ||
|
|
aede97571f | ||
|
|
01bc84db02 | ||
|
|
3564e96a00 | ||
|
|
f6f8f27668 | ||
|
|
dff54464c6 | ||
|
|
e987cdaaae | ||
|
|
6cd0e58fd0 | ||
|
|
f794c090a5 | ||
|
|
0d3197e437 | ||
|
|
150bf7fc65 | ||
|
|
51004b14d9 | ||
|
|
ea7e15b447 | ||
|
|
8e637a2ec7 | ||
|
|
96d886380a | ||
|
|
981c69040f | ||
|
|
de90c276af | ||
|
|
0129a8579f | ||
|
|
11ed4a56bd | ||
|
|
d75a0e69a0 | ||
|
|
1faad73381 | ||
|
|
d2499cd69d | ||
|
|
98df77439c | ||
|
|
81bbbe4eec | ||
|
|
9f530525d7 | ||
|
|
129283066f | ||
|
|
0dc6a726c1 | ||
|
|
4636499439 | ||
|
|
9a16eb2895 | ||
|
|
270a080b55 | ||
|
|
1cf9a55e3e | ||
|
|
6bcd91a801 | ||
|
|
7ed25ddc74 | ||
|
|
ae557c30d3 | ||
|
|
5a1800d642 | ||
|
|
d8fe7a52d6 | ||
|
|
791ecfbb32 | ||
|
|
5ff4473083 | ||
|
|
d1e88a30cb | ||
|
|
7eae7756f5 | ||
|
|
4e3cd01aad | ||
|
|
dbaee284e4 | ||
|
|
8253bfe2e0 | ||
|
|
828b5ad8bb | ||
|
|
fedad26c13 | ||
|
|
a526bb70ea | ||
|
|
5f6de610e1 | ||
|
|
02397868fc | ||
|
|
265a6b9b68 | ||
|
|
10e7c533d7 | ||
|
|
0ed3c5a5ec | ||
|
|
c2b06a02bf | ||
|
|
e0458a8fde | ||
|
|
21fb5f75b8 | ||
|
|
fb99412193 | ||
|
|
fdd327d58d | ||
|
|
0dfe696300 | ||
|
|
c0ba798708 | ||
|
|
67d7e53f7a | ||
|
|
e6366f7442 | ||
|
|
89d9cb0539 | ||
|
|
0d300a3540 | ||
|
|
7c59916c22 | ||
|
|
8f515ecc05 | ||
|
|
34f62a0919 | ||
|
|
0207a7ac96 | ||
|
|
bf619d8586 | ||
|
|
d48f9c2a6c | ||
|
|
90a5aa609a | ||
|
|
4fe7a463c5 | ||
|
|
7f49c91267 | ||
|
|
f6bf9c0857 | ||
|
|
da24810da2 | ||
|
|
ee3213c28f | ||
|
|
233eeb0b38 | ||
|
|
6c3b973748 | ||
|
|
9b8e2d9343 | ||
|
|
24fd577767 | ||
|
|
42b85de83e | ||
|
|
62266010ac | ||
|
|
0d7a57fa9d | ||
|
|
f909b3c0dc | ||
|
|
8b518161a3 | ||
|
|
20fafdca65 | ||
|
|
fd96efd456 | ||
|
|
7c21768e99 | ||
|
|
6a5a94f48f | ||
|
|
33d41338ef | ||
|
|
2d3b9364bf | ||
|
|
fa49fd7ba2 | ||
|
|
c3d72f6883 | ||
|
|
af99b52527 | ||
|
|
f241e1f81a | ||
|
|
90acce7fa1 | ||
|
|
7286391883 | ||
|
|
a1eab125ee | ||
|
|
1d4af2d92b | ||
|
|
d6cf2a837f | ||
|
|
d6b80acfbc | ||
|
|
1cad615b25 | ||
|
|
73fa79bf3f | ||
|
|
d79c13064e | ||
|
|
427a377c2a | ||
|
|
9feb4d6668 | ||
|
|
a366e9a4b5 | ||
|
|
cbdf33c42c | ||
|
|
9ceaf20584 | ||
|
|
54fee7bd3a | ||
|
|
414d8f2162 | ||
|
|
86cf1dd54b | ||
|
|
d099375200 | ||
|
|
9536372cfb | ||
|
|
630a17cf90 | ||
|
|
0a7b7894bd | ||
|
|
3a9fc39cd9 | ||
|
|
1181fd4560 | ||
|
|
b8a60261ef | ||
|
|
db68d55a0e | ||
|
|
574efb4526 | ||
|
|
03b0252589 | ||
|
|
ed09df4e13 | ||
|
|
f89ecd97d6 | ||
|
|
8293b7fdae | ||
|
|
0ba415866e | ||
|
|
53b41ca166 | ||
|
|
8a75f78e63 | ||
|
|
d9692c6366 | ||
|
|
f4b0062dfc | ||
|
|
b9ffc82e53 | ||
|
|
78aaea6a45 | ||
|
|
3645fbf161 | ||
|
|
a1d0f22132 | ||
|
|
fa73b0f4bf | ||
|
|
3b76a8b839 | ||
|
|
667f42dcdc | ||
|
|
dfbe09860f | ||
|
|
9e20f9c26a | ||
|
|
f968d0cb82 | ||
|
|
2ad84f4379 | ||
|
|
c7aa16426f | ||
|
|
5987f8e3b5 | ||
|
|
3a8eb72de2 | ||
|
|
33abbdfd24 | ||
|
|
0703d6cbff | ||
|
|
10d2d14938 | ||
|
|
691cf1d8d6 | ||
|
|
d1decb8e58 | ||
|
|
7d04904109 | ||
|
|
a5acd3aa97 | ||
|
|
eea9a12560 | ||
|
|
0a4570b55c |
83
.github/ISSUE_TEMPLATE/bug_report.yml
vendored
Normal file
83
.github/ISSUE_TEMPLATE/bug_report.yml
vendored
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
name: Bug report
|
||||||
|
description: Create a report to help us improve
|
||||||
|
title: "[Bug] "
|
||||||
|
labels: ["bug"]
|
||||||
|
body:
|
||||||
|
- type: checkboxes
|
||||||
|
id: ensure
|
||||||
|
attributes:
|
||||||
|
label: Verify steps
|
||||||
|
description: "
|
||||||
|
在提交之前,请确认
|
||||||
|
Please verify that you've followed these steps
|
||||||
|
"
|
||||||
|
options:
|
||||||
|
- label: "
|
||||||
|
确保你使用的是**本仓库**最新的的 mihomo 或 mihomo Alpha 版本
|
||||||
|
Ensure you are using the latest version of Mihomo or Mihomo Alpha from **this repository**.
|
||||||
|
"
|
||||||
|
required: true
|
||||||
|
- label: "
|
||||||
|
如果你可以自己 debug 并解决的话,提交 PR 吧
|
||||||
|
Is this something you can **debug and fix**? Send a pull request! Bug fixes and documentation fixes are welcome.
|
||||||
|
"
|
||||||
|
required: false
|
||||||
|
- label: "
|
||||||
|
我已经在 [Issue Tracker](……/) 中找过我要提出的问题
|
||||||
|
I have searched on the [issue tracker](……/) for a related issue.
|
||||||
|
"
|
||||||
|
required: true
|
||||||
|
- label: "
|
||||||
|
我已经使用 Alpha 分支版本测试过,问题依旧存在
|
||||||
|
I have tested using the dev branch, and the issue still exists.
|
||||||
|
"
|
||||||
|
required: true
|
||||||
|
- label: "
|
||||||
|
我已经仔细看过 [Documentation](https://wiki.metacubex.one/) 并无法自行解决问题
|
||||||
|
I have read the [documentation](https://wiki.metacubex.one/) and was unable to solve the issue.
|
||||||
|
"
|
||||||
|
required: true
|
||||||
|
- label: "
|
||||||
|
这是 Mihomo 核心的问题,并非我所使用的 Mihomo 衍生版本(如 OpenMihomo、KoolMihomo 等)的特定问题
|
||||||
|
This is an issue of the Mihomo core *per se*, not to the derivatives of Mihomo, like OpenMihomo or KoolMihomo.
|
||||||
|
"
|
||||||
|
required: true
|
||||||
|
- type: input
|
||||||
|
attributes:
|
||||||
|
label: Mihomo version
|
||||||
|
description: "use `mihomo -v`"
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: dropdown
|
||||||
|
id: os
|
||||||
|
attributes:
|
||||||
|
label: What OS are you seeing the problem on?
|
||||||
|
multiple: true
|
||||||
|
options:
|
||||||
|
- macOS
|
||||||
|
- Windows
|
||||||
|
- Linux
|
||||||
|
- OpenBSD/FreeBSD
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
render: yaml
|
||||||
|
label: "Mihomo config"
|
||||||
|
description: "
|
||||||
|
在下方附上 Mihomo 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)
|
||||||
|
"
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
render: shell
|
||||||
|
label: Mihomo log
|
||||||
|
description: "
|
||||||
|
在下方附上 Mihomo Core 的日志,log level 使用 DEBUG
|
||||||
|
Paste the Mihomo core log below with the log level set to `DEBUG`.
|
||||||
|
"
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: Description
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
5
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
5
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
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.
|
||||||
37
.github/ISSUE_TEMPLATE/feature_request.yml
vendored
Normal file
37
.github/ISSUE_TEMPLATE/feature_request.yml
vendored
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
name: Feature request
|
||||||
|
description: Suggest an idea for this project
|
||||||
|
title: "[Feature] "
|
||||||
|
labels: ["enhancement"]
|
||||||
|
body:
|
||||||
|
- type: checkboxes
|
||||||
|
id: ensure
|
||||||
|
attributes:
|
||||||
|
label: Verify steps
|
||||||
|
description: "
|
||||||
|
在提交之前,请确认
|
||||||
|
Please verify that you've followed these steps
|
||||||
|
"
|
||||||
|
options:
|
||||||
|
- label: "
|
||||||
|
我已经在 [Issue Tracker](……/) 中找过我要提出的请求
|
||||||
|
I have searched on the [issue tracker](……/) for a related feature request.
|
||||||
|
"
|
||||||
|
required: true
|
||||||
|
- label: "
|
||||||
|
我已经仔细看过 [Documentation](https://wiki.metacubex.one/) 并无法找到这个功能
|
||||||
|
I have read the [documentation](https://wiki.metacubex.one/) and was unable to solve the issue.
|
||||||
|
"
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: Description
|
||||||
|
description: 请详细、清晰地表达你要提出的论述,例如这个问题如何影响到你?你想实现什么功能?目前 Mihomo Core 的行为是什麽?
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: Possible Solution
|
||||||
|
description: "
|
||||||
|
此项非必须,但是如果你有想法的话欢迎提出。
|
||||||
|
Not obligatory, but suggest a fix/reason for the bug, or ideas how to implement the addition or change
|
||||||
|
"
|
||||||
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 clash.meta-darwin-arm64-cgo
|
mv $FILENAME mihomo-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 clash.meta-darwin-amd64-cgo
|
mv $FILENAME mihomo-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 clash.meta-windows-386-cgo.exe
|
mv $FILENAME mihomo-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 clash.meta-windows-amd64-cgo.exe
|
mv $FILENAME mihomo-windows-amd64-cgo.exe
|
||||||
elif [[ $FILENAME =~ "clash.meta-linux-arm-5" ]];then
|
elif [[ $FILENAME =~ "mihomo-linux-arm-5" ]];then
|
||||||
echo "rename clash.meta-linux-arm-5 $FILENAME"
|
echo "rename mihomo-linux-arm-5 $FILENAME"
|
||||||
mv $FILENAME clash.meta-linux-armv5-cgo
|
mv $FILENAME mihomo-linux-armv5-cgo
|
||||||
elif [[ $FILENAME =~ "clash.meta-linux-arm-6" ]];then
|
elif [[ $FILENAME =~ "mihomo-linux-arm-6" ]];then
|
||||||
echo "rename clash.meta-linux-arm-6 $FILENAME"
|
echo "rename mihomo-linux-arm-6 $FILENAME"
|
||||||
mv $FILENAME clash.meta-linux-armv6-cgo
|
mv $FILENAME mihomo-linux-armv6-cgo
|
||||||
elif [[ $FILENAME =~ "clash.meta-linux-arm-7" ]];then
|
elif [[ $FILENAME =~ "mihomo-linux-arm-7" ]];then
|
||||||
echo "rename clash.meta-linux-arm-7 $FILENAME"
|
echo "rename mihomo-linux-arm-7 $FILENAME"
|
||||||
mv $FILENAME clash.meta-linux-armv7-cgo
|
mv $FILENAME mihomo-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
Normal file
12
.github/rename-go120.sh
vendored
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#!/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
|
||||||
16
.github/workflows/Delete.yml
vendored
Normal file
16
.github/workflows/Delete.yml
vendored
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
name: Delete old workflow runs
|
||||||
|
on:
|
||||||
|
schedule:
|
||||||
|
- cron: '0 0 1 * *'
|
||||||
|
# Run monthly, at 00:00 on the 1st day of month.
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
del_runs:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Delete workflow runs
|
||||||
|
uses: GitRML/delete-workflow-runs@main
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.AUTH_PAT }}
|
||||||
|
repository: ${{ github.repository }}
|
||||||
|
retain_days: 30
|
||||||
66
.github/workflows/build.yml
vendored
66
.github/workflows/build.yml
vendored
@@ -19,7 +19,7 @@ concurrency:
|
|||||||
cancel-in-progress: true
|
cancel-in-progress: true
|
||||||
|
|
||||||
env:
|
env:
|
||||||
REGISTRY: docker.io
|
REGISTRY: ghcr.io
|
||||||
jobs:
|
jobs:
|
||||||
Build:
|
Build:
|
||||||
permissions: write-all
|
permissions: write-all
|
||||||
@@ -69,6 +69,12 @@ jobs:
|
|||||||
target: "darwin-amd64 darwin-arm64 android-arm64",
|
target: "darwin-amd64 darwin-arm64 android-arm64",
|
||||||
id: "9",
|
id: "9",
|
||||||
}
|
}
|
||||||
|
# only for test
|
||||||
|
- { type: "WithoutCGO-GO120", target: "linux-amd64 linux-amd64-compatible",id: "1" }
|
||||||
|
# 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.
|
||||||
|
- { type: "WithoutCGO-GO120", target: "windows-amd64-compatible windows-amd64 windows-386",id: "2" }
|
||||||
|
# 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.
|
||||||
|
- { type: "WithoutCGO-GO120", target: "darwin-amd64 darwin-arm64 android-arm64",id: "3" }
|
||||||
- { type: "WithCGO", target: "windows/*", id: "1" }
|
- { type: "WithCGO", target: "windows/*", id: "1" }
|
||||||
- { type: "WithCGO", target: "linux/386", id: "2" }
|
- { type: "WithCGO", target: "linux/386", id: "2" }
|
||||||
- { type: "WithCGO", target: "linux/amd64", id: "3" }
|
- { type: "WithCGO", target: "linux/amd64", id: "3" }
|
||||||
@@ -112,7 +118,7 @@ jobs:
|
|||||||
- name: Set ENV
|
- name: Set ENV
|
||||||
run: |
|
run: |
|
||||||
sudo timedatectl set-timezone "Asia/Shanghai"
|
sudo timedatectl set-timezone "Asia/Shanghai"
|
||||||
echo "NAME=clash.meta" >> $GITHUB_ENV
|
echo "NAME=mihomo" >> $GITHUB_ENV
|
||||||
echo "REPO=${{ github.repository }}" >> $GITHUB_ENV
|
echo "REPO=${{ github.repository }}" >> $GITHUB_ENV
|
||||||
echo "ShortSHA=$(git rev-parse --short ${{ github.sha }})" >> $GITHUB_ENV
|
echo "ShortSHA=$(git rev-parse --short ${{ github.sha }})" >> $GITHUB_ENV
|
||||||
echo "BUILDTIME=$(date)" >> $GITHUB_ENV
|
echo "BUILDTIME=$(date)" >> $GITHUB_ENV
|
||||||
@@ -122,24 +128,32 @@ jobs:
|
|||||||
- name: Set ENV
|
- name: Set ENV
|
||||||
run: |
|
run: |
|
||||||
echo "TAGS=with_gvisor,with_lwip" >> $GITHUB_ENV
|
echo "TAGS=with_gvisor,with_lwip" >> $GITHUB_ENV
|
||||||
echo "LDFLAGS=-X 'github.com/Dreamacro/clash/constant.Version=${VERSION}' -X 'github.com/Dreamacro/clash/constant.BuildTime=${BUILDTIME}' -w -s -buildid=" >> $GITHUB_ENV
|
echo "LDFLAGS=-X 'github.com/metacubex/mihomo/constant.Version=${VERSION}' -X 'github.com/metacubex/mihomo/constant.BuildTime=${BUILDTIME}' -w -s -buildid=" >> $GITHUB_ENV
|
||||||
shell: bash
|
shell: bash
|
||||||
|
|
||||||
- name: Setup Go
|
- name: Setup Go
|
||||||
|
if: ${{ matrix.job.type!='WithoutCGO-GO120' }}
|
||||||
uses: actions/setup-go@v4
|
uses: actions/setup-go@v4
|
||||||
with:
|
with:
|
||||||
go-version: "1.21"
|
go-version: "1.21"
|
||||||
check-latest: true
|
check-latest: true
|
||||||
|
|
||||||
|
- name: Setup Go
|
||||||
|
if: ${{ matrix.job.type=='WithoutCGO-GO120' }}
|
||||||
|
uses: actions/setup-go@v4
|
||||||
|
with:
|
||||||
|
go-version: "1.20"
|
||||||
|
check-latest: true
|
||||||
|
|
||||||
- name: Test
|
- name: Test
|
||||||
if: ${{ matrix.job.id=='1' && matrix.job.type=='WithoutCGO' }}
|
if: ${{ matrix.job.id=='1' && matrix.job.type!='WithCGO' }}
|
||||||
run: |
|
run: |
|
||||||
go test ./...
|
go test ./...
|
||||||
|
|
||||||
- name: Build WithoutCGO
|
- name: Build WithoutCGO
|
||||||
if: ${{ matrix.job.type=='WithoutCGO' }}
|
if: ${{ matrix.job.type!='WithCGO' }}
|
||||||
env:
|
env:
|
||||||
NAME: Clash.Meta
|
NAME: mihomo
|
||||||
BINDIR: bin
|
BINDIR: bin
|
||||||
run: make -j$(($(nproc) + 1)) ${{ matrix.job.target }}
|
run: make -j$(($(nproc) + 1)) ${{ matrix.job.target }}
|
||||||
|
|
||||||
@@ -147,9 +161,8 @@ jobs:
|
|||||||
if: ${{ matrix.job.type=='WithCGO' && matrix.job.target=='android' }}
|
if: ${{ matrix.job.type=='WithCGO' && matrix.job.target=='android' }}
|
||||||
id: setup-ndk
|
id: setup-ndk
|
||||||
with:
|
with:
|
||||||
ndk-version: r25b
|
ndk-version: r26b
|
||||||
add-to-path: false
|
add-to-path: true
|
||||||
local-cache: true
|
|
||||||
|
|
||||||
- name: Build Android
|
- name: Build Android
|
||||||
if: ${{ matrix.job.type=='WithCGO' && matrix.job.target=='android' }}
|
if: ${{ matrix.job.type=='WithCGO' && matrix.job.target=='android' }}
|
||||||
@@ -185,6 +198,17 @@ jobs:
|
|||||||
ls -la
|
ls -la
|
||||||
cd ..
|
cd ..
|
||||||
|
|
||||||
|
- name: Rename
|
||||||
|
if: ${{ matrix.job.type=='WithoutCGO-GO120' }}
|
||||||
|
run: |
|
||||||
|
cd bin
|
||||||
|
ls -la
|
||||||
|
cp ../.github/rename-go120.sh ./
|
||||||
|
bash ./rename-go120.sh
|
||||||
|
rm ./rename-go120.sh
|
||||||
|
ls -la
|
||||||
|
cd ..
|
||||||
|
|
||||||
- name: Zip
|
- name: Zip
|
||||||
if: ${{ success() }}
|
if: ${{ success() }}
|
||||||
run: |
|
run: |
|
||||||
@@ -209,7 +233,7 @@ jobs:
|
|||||||
|
|
||||||
Upload-Prerelease:
|
Upload-Prerelease:
|
||||||
permissions: write-all
|
permissions: write-all
|
||||||
if: ${{ github.ref_type=='branch' }}
|
if: ${{ github.ref_type=='branch' && github.event_name != 'pull_request' }}
|
||||||
needs: [Build]
|
needs: [Build]
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
@@ -246,7 +270,7 @@ jobs:
|
|||||||
Release created at ${{ env.BUILDTIME }}
|
Release created at ${{ env.BUILDTIME }}
|
||||||
Synchronize ${{ github.ref_name }} branch code updates, keeping only the latest version
|
Synchronize ${{ github.ref_name }} branch code updates, keeping only the latest version
|
||||||
<br>
|
<br>
|
||||||
[我应该下载哪个文件? / Which file should I download?](https://github.com/MetaCubeX/Clash.Meta/wiki/FAQ)
|
[我应该下载哪个文件? / Which file should I download?](https://github.com/MetaCubeX/mihomo/wiki/FAQ)
|
||||||
[查看文档 / Docs](https://metacubex.github.io/Meta-Docs/)
|
[查看文档 / Docs](https://metacubex.github.io/Meta-Docs/)
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
@@ -318,24 +342,25 @@ jobs:
|
|||||||
id: meta
|
id: meta
|
||||||
uses: docker/metadata-action@v4
|
uses: docker/metadata-action@v4
|
||||||
with:
|
with:
|
||||||
images: ${{ env.REGISTRY }}/${{ secrets.DOCKERHUB_ACCOUNT }}/${{secrets.DOCKERHUB_REPO}}
|
images: ${{ env.REGISTRY }}/${{ github.repository }}
|
||||||
|
|
||||||
- name: Show files
|
- name: Show files
|
||||||
run: |
|
run: |
|
||||||
ls .
|
ls .
|
||||||
ls bin/
|
ls bin/
|
||||||
- name: Log into registry
|
|
||||||
if: github.event_name != 'pull_request'
|
- name: login to ghcr.io
|
||||||
uses: docker/login-action@v2
|
uses: docker/login-action@v3
|
||||||
with:
|
with:
|
||||||
registry: ${{ env.REGISTRY }}
|
registry: ghcr.io
|
||||||
username: ${{ secrets.DOCKER_HUB_USER }}
|
username: ${{ github.actor }}
|
||||||
password: ${{ secrets.DOCKER_HUB_TOKEN }}
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
# 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
|
||||||
id: build-and-push
|
id: build-and-push
|
||||||
uses: docker/build-push-action@v4
|
uses: docker/build-push-action@v5
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
file: ./Dockerfile
|
file: ./Dockerfile
|
||||||
@@ -343,8 +368,7 @@ jobs:
|
|||||||
platforms: |
|
platforms: |
|
||||||
linux/386
|
linux/386
|
||||||
linux/amd64
|
linux/amd64
|
||||||
linux/arm64/v8
|
linux/arm64
|
||||||
linux/arm/v7
|
linux/arm/v7
|
||||||
# linux/riscv64
|
|
||||||
tags: ${{ steps.meta.outputs.tags }}
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
labels: ${{ steps.meta.outputs.labels }}
|
labels: ${{ steps.meta.outputs.labels }}
|
||||||
|
|||||||
33
.github/workflows/trigger-cmfa-update.yml
vendored
Normal file
33
.github/workflows/trigger-cmfa-update.yml
vendored
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
name: Trigger CMFA Update
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
push:
|
||||||
|
paths-ignore:
|
||||||
|
- "docs/**"
|
||||||
|
- "README.md"
|
||||||
|
- ".github/ISSUE_TEMPLATE/**"
|
||||||
|
branches:
|
||||||
|
- Alpha
|
||||||
|
tags:
|
||||||
|
- "v*"
|
||||||
|
pull_request_target:
|
||||||
|
branches:
|
||||||
|
- Alpha
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
# Send "core-updated" to MetaCubeX/MihomoForAndroid to trigger update-dependencies
|
||||||
|
trigger-CMFA-update:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: tibdex/github-app-token@v1
|
||||||
|
id: generate-token
|
||||||
|
with:
|
||||||
|
app_id: ${{ secrets.MAINTAINER_APPID }}
|
||||||
|
private_key: ${{ secrets.MAINTAINER_APP_PRIVATE_KEY }}
|
||||||
|
|
||||||
|
- name: Trigger update-dependencies
|
||||||
|
run: |
|
||||||
|
curl -X POST https://api.github.com/repos/MetaCubeX/MihomoForAndroid/dispatches \
|
||||||
|
-H "Accept: application/vnd.github.everest-preview+json" \
|
||||||
|
-H "Authorization: token ${{ steps.generate-token.outputs.token }}" \
|
||||||
|
-d '{"event_type": "core-updated"}'
|
||||||
@@ -11,7 +11,7 @@ linters-settings:
|
|||||||
custom-order: true
|
custom-order: true
|
||||||
sections:
|
sections:
|
||||||
- standard
|
- standard
|
||||||
- prefix(github.com/Dreamacro/clash)
|
- prefix(github.com/metacubex/mihomo)
|
||||||
- default
|
- default
|
||||||
staticcheck:
|
staticcheck:
|
||||||
go: '1.19'
|
go: '1.19'
|
||||||
|
|||||||
26
Dockerfile
26
Dockerfile
@@ -3,25 +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 /clash-config && \
|
mkdir /mihomo-config && \
|
||||||
wget -O /clash-config/geoip.metadb https://fastly.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geoip.metadb && \
|
wget -O /mihomo-config/geoip.metadb https://fastly.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geoip.metadb && \
|
||||||
wget -O /clash-config/geosite.dat https://fastly.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geosite.dat && \
|
wget -O /mihomo-config/geosite.dat https://fastly.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geosite.dat && \
|
||||||
wget -O /clash-config/geoip.dat https://fastly.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geoip.dat
|
wget -O /mihomo-config/geoip.dat https://fastly.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geoip.dat
|
||||||
|
|
||||||
COPY docker/file-name.sh /clash/file-name.sh
|
COPY docker/file-name.sh /mihomo/file-name.sh
|
||||||
WORKDIR /clash
|
WORKDIR /mihomo
|
||||||
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.*"|awk NR==1` && echo $FILE_NAME && \
|
FILE_NAME=`ls bin/ | egrep "$FILE_NAME.*"|awk NR==1` && echo $FILE_NAME && \
|
||||||
mv bin/$FILE_NAME clash.gz && gzip -d clash.gz && echo "$FILE_NAME" > /clash-config/test
|
mv bin/$FILE_NAME mihomo.gz && gzip -d mihomo.gz && echo "$FILE_NAME" > /mihomo-config/test
|
||||||
FROM alpine:latest
|
FROM alpine:latest
|
||||||
LABEL org.opencontainers.image.source="https://github.com/MetaCubeX/Clash.Meta"
|
LABEL org.opencontainers.image.source="https://github.com/MetaCubeX/mihomo"
|
||||||
|
|
||||||
RUN apk add --no-cache ca-certificates tzdata iptables
|
RUN apk add --no-cache ca-certificates tzdata iptables
|
||||||
|
|
||||||
VOLUME ["/root/.config/clash/"]
|
VOLUME ["/root/.config/mihomo/"]
|
||||||
|
|
||||||
COPY --from=builder /clash-config/ /root/.config/clash/
|
COPY --from=builder /mihomo-config/ /root/.config/mihomo/
|
||||||
COPY --from=builder /clash/clash /clash
|
COPY --from=builder /mihomo/mihomo /mihomo
|
||||||
RUN chmod +x /clash
|
RUN chmod +x /mihomo
|
||||||
ENTRYPOINT [ "/clash" ]
|
ENTRYPOINT [ "/mihomo" ]
|
||||||
|
|||||||
6
Makefile
6
Makefile
@@ -1,4 +1,4 @@
|
|||||||
NAME=clash.meta
|
NAME=mihomo
|
||||||
BINDIR=bin
|
BINDIR=bin
|
||||||
BRANCH=$(shell git branch --show-current)
|
BRANCH=$(shell git branch --show-current)
|
||||||
ifeq ($(BRANCH),Alpha)
|
ifeq ($(BRANCH),Alpha)
|
||||||
@@ -12,8 +12,8 @@ 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/Dreamacro/clash/constant.Version=$(VERSION)" \
|
GOBUILD=CGO_ENABLED=0 go build -tags with_gvisor -trimpath -ldflags '-X "github.com/metacubex/mihomo/constant.Version=$(VERSION)" \
|
||||||
-X "github.com/Dreamacro/clash/constant.BuildTime=$(BUILDTIME)" \
|
-X "github.com/metacubex/mihomo/constant.BuildTime=$(BUILDTIME)" \
|
||||||
-w -s -buildid='
|
-w -s -buildid='
|
||||||
|
|
||||||
PLATFORM_LIST = \
|
PLATFORM_LIST = \
|
||||||
|
|||||||
340
README.md
340
README.md
@@ -3,17 +3,17 @@
|
|||||||
<br>Meta Kernel<br>
|
<br>Meta Kernel<br>
|
||||||
</h1>
|
</h1>
|
||||||
|
|
||||||
<h3 align="center">Another Clash Kernel.</h3>
|
<h3 align="center">Another Mihomo Kernel.</h3>
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://goreportcard.com/report/github.com/Clash-Mini/Clash.Meta">
|
<a href="https://goreportcard.com/report/github.com/MetaCubeX/mihomo">
|
||||||
<img src="https://goreportcard.com/badge/github.com/Clash-Mini/Clash.Meta?style=flat-square">
|
<img src="https://goreportcard.com/badge/github.com/MetaCubeX/mihomo?style=flat-square">
|
||||||
</a>
|
</a>
|
||||||
<img src="https://img.shields.io/github/go-mod/go-version/Dreamacro/clash?style=flat-square">
|
<img src="https://img.shields.io/github/go-mod/go-version/MetaCubeX/mihomo?style=flat-square">
|
||||||
<a href="https://github.com/Clash-Mini/Clash.Meta/releases">
|
<a href="https://github.com/MetaCubeX/mihomo/releases">
|
||||||
<img src="https://img.shields.io/github/release/Clash-Mini/Clash.Meta/all.svg?style=flat-square">
|
<img src="https://img.shields.io/github/release/MetaCubeX/mihomo/all.svg?style=flat-square">
|
||||||
</a>
|
</a>
|
||||||
<a href="https://github.com/Clash-Mini/Clash.Meta">
|
<a href="https://github.com/MetaCubeX/mihomo">
|
||||||
<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>
|
||||||
@@ -21,261 +21,52 @@
|
|||||||
## Features
|
## Features
|
||||||
|
|
||||||
- Local HTTP/HTTPS/SOCKS server with authentication support
|
- Local HTTP/HTTPS/SOCKS server with authentication support
|
||||||
- VMess, Shadowsocks, Trojan, Snell protocol support for remote connections
|
- VMess, VLESS, Shadowsocks, Trojan, Snell, TUIC, Hysteria protocol support
|
||||||
- Built-in DNS server that aims to minimize DNS pollution attack impact, supports DoH/DoT upstream and fake IP.
|
- Built-in DNS server that aims to minimize DNS pollution attack impact, supports DoH/DoT upstream and fake IP.
|
||||||
- Rules based off domains, GEOIP, IPCIDR or Process to forward packets to different nodes
|
- Rules based off domains, GEOIP, IPCIDR or Process to forward packets to different nodes
|
||||||
- Remote groups allow users to implement powerful rules. Supports automatic fallback, load balancing or auto select node based off latency
|
- Remote groups allow users to implement powerful rules. Supports automatic fallback, load balancing or auto select node
|
||||||
- Remote providers, allowing users to get node lists remotely instead of hardcoding in config
|
based off latency
|
||||||
- Netfilter TCP redirecting. Deploy Clash on your Internet gateway with `iptables`.
|
- 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`.
|
||||||
- Comprehensive HTTP RESTful API controller
|
- Comprehensive HTTP RESTful API controller
|
||||||
|
|
||||||
## Wiki
|
## Dashboard
|
||||||
Configuration examples can be found at [/docs/config.yaml](https://github.com/MetaCubeX/Clash.Meta/blob/Alpha/docs/config.yaml), while documentation can be found [Clash.Meta Wiki](https://clash-meta.wiki).
|
|
||||||
|
|
||||||
## Build
|
A web dashboard with first-class support for this project has been created; it can be checked out at [metacubexd](https://github.com/MetaCubeX/metacubexd).
|
||||||
|
|
||||||
You should install [golang](https://go.dev) first.
|
## Configration example
|
||||||
|
|
||||||
Then get the source code of Clash.Meta:
|
Configuration example is located at [/docs/config.yaml](https://github.com/MetaCubeX/mihomo/blob/Alpha/docs/config.yaml).
|
||||||
|
|
||||||
|
## Docs
|
||||||
|
|
||||||
|
Documentation can be found in [mihomo Docs](https://wiki.metacubex.one/).
|
||||||
|
|
||||||
|
## For development
|
||||||
|
|
||||||
|
Requirements:
|
||||||
|
[Go 1.20 or newer](https://go.dev/dl/)
|
||||||
|
|
||||||
|
Build mihomo:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
git clone https://github.com/MetaCubeX/Clash.Meta.git
|
git clone https://github.com/MetaCubeX/mihomo.git
|
||||||
cd Clash.Meta && go mod download
|
cd mihomo && go mod download
|
||||||
|
go build
|
||||||
```
|
```
|
||||||
|
|
||||||
If you can't visit github,you should set proxy first:
|
Set go proxy if a connection to GitHub is not possible:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
go env -w GOPROXY=https://goproxy.io,direct
|
go env -w GOPROXY=https://goproxy.io,direct
|
||||||
```
|
```
|
||||||
|
|
||||||
Now you can build it:
|
Build with gvisor tun stack:
|
||||||
|
|
||||||
```shell
|
|
||||||
go build
|
|
||||||
```
|
|
||||||
|
|
||||||
If you need gvisor for tun stack, build with:
|
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
go build -tags with_gvisor
|
go build -tags with_gvisor
|
||||||
```
|
```
|
||||||
|
|
||||||
<!-- ## Advanced usage of this fork -->
|
|
||||||
|
|
||||||
<!-- ### DNS configuration
|
|
||||||
|
|
||||||
Support `geosite` with `fallback-filter`.
|
|
||||||
|
|
||||||
Restore `Redir remote resolution`.
|
|
||||||
|
|
||||||
Support resolve ip with a `Proxy Tunnel`.
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
proxy-groups:
|
|
||||||
- name: DNS
|
|
||||||
type: url-test
|
|
||||||
use:
|
|
||||||
- HK
|
|
||||||
url: http://cp.cloudflare.com
|
|
||||||
interval: 180
|
|
||||||
lazy: true
|
|
||||||
```
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
dns:
|
|
||||||
enable: true
|
|
||||||
use-hosts: true
|
|
||||||
ipv6: false
|
|
||||||
enhanced-mode: redir-host
|
|
||||||
fake-ip-range: 198.18.0.1/16
|
|
||||||
listen: 127.0.0.1:6868
|
|
||||||
default-nameserver:
|
|
||||||
- 119.29.29.29
|
|
||||||
- 114.114.114.114
|
|
||||||
nameserver:
|
|
||||||
- https://doh.pub/dns-query
|
|
||||||
- tls://223.5.5.5:853
|
|
||||||
fallback:
|
|
||||||
- "https://1.0.0.1/dns-query#DNS" # append the proxy adapter name or group name to the end of DNS URL with '#' prefix.
|
|
||||||
- "tls://8.8.4.4:853#DNS"
|
|
||||||
fallback-filter:
|
|
||||||
geoip: false
|
|
||||||
geosite:
|
|
||||||
- gfw # `geosite` filter only use fallback server to resolve ip, prevent DNS leaks to unsafe DNS providers.
|
|
||||||
domain:
|
|
||||||
- +.example.com
|
|
||||||
ipcidr:
|
|
||||||
- 0.0.0.0/32
|
|
||||||
```
|
|
||||||
|
|
||||||
### TUN configuration
|
|
||||||
|
|
||||||
Supports macOS, Linux and Windows.
|
|
||||||
|
|
||||||
Built-in [Wintun](https://www.wintun.net) driver.
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
# Enable the TUN listener
|
|
||||||
tun:
|
|
||||||
enable: true
|
|
||||||
stack: system # system/gvisor
|
|
||||||
dns-hijack:
|
|
||||||
- 0.0.0.0:53 # additional dns server listen on TUN
|
|
||||||
auto-route: true # auto set global route
|
|
||||||
```
|
|
||||||
|
|
||||||
### Rules configuration
|
|
||||||
|
|
||||||
- Support rule `GEOSITE`.
|
|
||||||
- Support rule-providers `RULE-SET`.
|
|
||||||
- Support `multiport` condition for rule `SRC-PORT` and `DST-PORT`.
|
|
||||||
- Support `network` condition for all rules.
|
|
||||||
- Support source IPCIDR condition for all rules, just append to the end.
|
|
||||||
- The `GEOSITE` databases via https://github.com/Loyalsoldier/v2ray-rules-dat.
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
rules:
|
|
||||||
# network(tcp/udp) condition for all rules
|
|
||||||
- DOMAIN-SUFFIX,bilibili.com,DIRECT,tcp
|
|
||||||
- DOMAIN-SUFFIX,bilibili.com,REJECT,udp
|
|
||||||
|
|
||||||
# multiport condition for rules SRC-PORT and DST-PORT
|
|
||||||
- DST-PORT,123/136/137-139,DIRECT,udp
|
|
||||||
|
|
||||||
# rule GEOSITE
|
|
||||||
- GEOSITE,category-ads-all,REJECT
|
|
||||||
- GEOSITE,icloud@cn,DIRECT
|
|
||||||
- GEOSITE,apple@cn,DIRECT
|
|
||||||
- GEOSITE,apple-cn,DIRECT
|
|
||||||
- GEOSITE,microsoft@cn,DIRECT
|
|
||||||
- GEOSITE,facebook,PROXY
|
|
||||||
- GEOSITE,youtube,PROXY
|
|
||||||
- GEOSITE,geolocation-cn,DIRECT
|
|
||||||
- GEOSITE,geolocation-!cn,PROXY
|
|
||||||
|
|
||||||
# source IPCIDR condition for all rules in gateway proxy
|
|
||||||
#- GEOSITE,geolocation-!cn,REJECT,192.168.1.88/32,192.168.1.99/32
|
|
||||||
|
|
||||||
- GEOIP,telegram,PROXY,no-resolve
|
|
||||||
- GEOIP,private,DIRECT,no-resolve
|
|
||||||
- GEOIP,cn,DIRECT
|
|
||||||
|
|
||||||
- MATCH,PROXY
|
|
||||||
```
|
|
||||||
|
|
||||||
### Proxies configuration
|
|
||||||
|
|
||||||
Active health detection `urltest / fallback` (based on tcp handshake, multiple failures within a limited time will actively trigger health detection to use the node)
|
|
||||||
|
|
||||||
Support `Policy Group Filter`
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
proxy-groups:
|
|
||||||
- name: 🚀 HK Group
|
|
||||||
type: select
|
|
||||||
use:
|
|
||||||
- ALL
|
|
||||||
filter: "HK"
|
|
||||||
|
|
||||||
- name: 🚀 US Group
|
|
||||||
type: select
|
|
||||||
use:
|
|
||||||
- ALL
|
|
||||||
filter: "US"
|
|
||||||
|
|
||||||
proxy-providers:
|
|
||||||
ALL:
|
|
||||||
type: http
|
|
||||||
url: "xxxxx"
|
|
||||||
interval: 3600
|
|
||||||
path: "xxxxx"
|
|
||||||
health-check:
|
|
||||||
enable: true
|
|
||||||
interval: 600
|
|
||||||
url: http://www.gstatic.com/generate_204
|
|
||||||
```
|
|
||||||
|
|
||||||
Support outbound transport protocol `VLESS`.
|
|
||||||
|
|
||||||
The XTLS support (TCP/UDP) transport by the XRAY-CORE.
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
proxies:
|
|
||||||
- name: "vless"
|
|
||||||
type: vless
|
|
||||||
server: server
|
|
||||||
port: 443
|
|
||||||
uuid: uuid
|
|
||||||
servername: example.com # AKA SNI
|
|
||||||
# flow: xtls-rprx-direct # xtls-rprx-origin # enable XTLS
|
|
||||||
# skip-cert-verify: true
|
|
||||||
|
|
||||||
- name: "vless-ws"
|
|
||||||
type: vless
|
|
||||||
server: server
|
|
||||||
port: 443
|
|
||||||
uuid: uuid
|
|
||||||
tls: true
|
|
||||||
udp: true
|
|
||||||
network: ws
|
|
||||||
servername: example.com # priority over wss host
|
|
||||||
# skip-cert-verify: true
|
|
||||||
ws-opts:
|
|
||||||
path: /path
|
|
||||||
headers: { Host: example.com, Edge: "12a00c4.fm.huawei.com:82897" }
|
|
||||||
|
|
||||||
- name: "vless-grpc"
|
|
||||||
type: vless
|
|
||||||
server: server
|
|
||||||
port: 443
|
|
||||||
uuid: uuid
|
|
||||||
tls: true
|
|
||||||
udp: true
|
|
||||||
network: grpc
|
|
||||||
servername: example.com # priority over wss host
|
|
||||||
# skip-cert-verify: true
|
|
||||||
grpc-opts:
|
|
||||||
grpc-service-name: grpcname
|
|
||||||
```
|
|
||||||
|
|
||||||
Support outbound transport protocol `Wireguard`
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
proxies:
|
|
||||||
- name: "wg"
|
|
||||||
type: wireguard
|
|
||||||
server: 162.159.192.1
|
|
||||||
port: 2480
|
|
||||||
ip: 172.16.0.2
|
|
||||||
ipv6: fd01:5ca1:ab1e:80fa:ab85:6eea:213f:f4a5
|
|
||||||
private-key: eCtXsJZ27+4PbhDkHnB923tkUn2Gj59wZw5wFA75MnU=
|
|
||||||
public-key: Cr8hWlKvtDt7nrvf+f0brNQQzabAqrjfBvas9pmowjo=
|
|
||||||
udp: true
|
|
||||||
```
|
|
||||||
|
|
||||||
Support outbound transport protocol `Tuic`
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
proxies:
|
|
||||||
- name: "tuic"
|
|
||||||
server: www.example.com
|
|
||||||
port: 10443
|
|
||||||
type: tuic
|
|
||||||
token: TOKEN
|
|
||||||
# ip: 127.0.0.1 # for overwriting the DNS lookup result of the server address set in option 'server'
|
|
||||||
# heartbeat-interval: 10000
|
|
||||||
# alpn: [h3]
|
|
||||||
# disable-sni: true
|
|
||||||
reduce-rtt: true
|
|
||||||
# request-timeout: 8000
|
|
||||||
udp-relay-mode: native # Available: "native", "quic". Default: "native"
|
|
||||||
# congestion-controller: bbr # Available: "cubic", "new_reno", "bbr". Default: "cubic"
|
|
||||||
# max-udp-relay-packet-size: 1500
|
|
||||||
# fast-open: true
|
|
||||||
# skip-cert-verify: true
|
|
||||||
``` -->
|
|
||||||
|
|
||||||
### IPTABLES configuration
|
### IPTABLES configuration
|
||||||
|
|
||||||
Work on Linux OS which supported `iptables`
|
Work on Linux OS which supported `iptables`
|
||||||
@@ -289,71 +80,10 @@ iptables:
|
|||||||
inbound-interface: eth0 # detect the inbound interface, default is 'lo'
|
inbound-interface: eth0 # detect the inbound interface, default is 'lo'
|
||||||
```
|
```
|
||||||
|
|
||||||
### General installation guide for Linux
|
|
||||||
|
|
||||||
- Create user given name `clash-meta`
|
|
||||||
|
|
||||||
- Download and decompress pre-built binaries from [releases](https://github.com/MetaCubeX/Clash.Meta/releases)
|
|
||||||
|
|
||||||
- Rename executable file to `Clash-Meta` and move to `/usr/local/bin/`
|
|
||||||
|
|
||||||
- Create folder `/etc/Clash-Meta/` as working directory
|
|
||||||
|
|
||||||
Run Meta Kernel by user `clash-meta` as a daemon.
|
|
||||||
|
|
||||||
Create the systemd configuration file at `/etc/systemd/system/Clash-Meta.service`:
|
|
||||||
|
|
||||||
```
|
|
||||||
[Unit]
|
|
||||||
Description=Clash-Meta Daemon, Another Clash Kernel.
|
|
||||||
After=network.target NetworkManager.service systemd-networkd.service iwd.service
|
|
||||||
|
|
||||||
[Service]
|
|
||||||
Type=simple
|
|
||||||
User=clash-meta
|
|
||||||
Group=clash-meta
|
|
||||||
LimitNPROC=500
|
|
||||||
LimitNOFILE=1000000
|
|
||||||
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_RAW CAP_NET_BIND_SERVICE
|
|
||||||
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_RAW CAP_NET_BIND_SERVICE
|
|
||||||
Restart=always
|
|
||||||
ExecStartPre=/usr/bin/sleep 1s
|
|
||||||
ExecStart=/usr/local/bin/Clash-Meta -d /etc/Clash-Meta
|
|
||||||
|
|
||||||
[Install]
|
|
||||||
WantedBy=multi-user.target
|
|
||||||
```
|
|
||||||
|
|
||||||
Launch clashd on system startup with:
|
|
||||||
|
|
||||||
```shell
|
|
||||||
$ systemctl enable Clash-Meta
|
|
||||||
```
|
|
||||||
|
|
||||||
Launch clashd immediately with:
|
|
||||||
|
|
||||||
```shell
|
|
||||||
$ systemctl start Clash-Meta
|
|
||||||
```
|
|
||||||
|
|
||||||
### Display Process name
|
|
||||||
|
|
||||||
Clash add field `Process` to `Metadata` and prepare to get process name for Restful API `GET /connections`.
|
|
||||||
|
|
||||||
To display process name in GUI please use [Razord-meta](https://github.com/MetaCubeX/Razord-meta).
|
|
||||||
|
|
||||||
### Dashboard
|
|
||||||
|
|
||||||
We also made a custom fork of yacd provide better support for this project, check it out at [Yacd-meta](https://github.com/MetaCubeX/Yacd-meta)
|
|
||||||
|
|
||||||
## Development
|
|
||||||
|
|
||||||
If you want to build an application that uses clash as a library, check out the
|
|
||||||
the [GitHub Wiki](https://github.com/Dreamacro/clash/wiki/use-clash-as-a-library)
|
|
||||||
|
|
||||||
## Debugging
|
## Debugging
|
||||||
Check [wiki](https://github.com/MetaCubeX/Clash.Meta/wiki/How-to-use-debug-api) to get an instruction on using debug API.
|
|
||||||
|
|
||||||
|
Check [wiki](https://wiki.metacubex.one/api/#debug) to get an instruction on using debug
|
||||||
|
API.
|
||||||
|
|
||||||
## Credits
|
## Credits
|
||||||
|
|
||||||
@@ -368,4 +98,4 @@ Check [wiki](https://github.com/MetaCubeX/Clash.Meta/wiki/How-to-use-debug-api)
|
|||||||
|
|
||||||
This software is released under the GPL-3.0 license.
|
This software is released under the GPL-3.0 license.
|
||||||
|
|
||||||
[](https://app.fossa.io/projects/git%2Bgithub.com%2FDreamacro%2Fclash?ref=badge_large)
|
[](https://app.fossa.io/projects/git%2Bgithub.com%2FMetaCubeX%2Fmihomo?ref=badge_large)
|
||||||
|
|||||||
@@ -12,12 +12,12 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/atomic"
|
"github.com/metacubex/mihomo/common/atomic"
|
||||||
"github.com/Dreamacro/clash/common/queue"
|
"github.com/metacubex/mihomo/common/queue"
|
||||||
"github.com/Dreamacro/clash/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/Dreamacro/clash/log"
|
"github.com/puzpuzpuz/xsync/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
var UnifiedDelay = atomic.NewBool(false)
|
var UnifiedDelay = atomic.NewBool(false)
|
||||||
@@ -28,28 +28,21 @@ const (
|
|||||||
|
|
||||||
type extraProxyState struct {
|
type extraProxyState struct {
|
||||||
history *queue.Queue[C.DelayHistory]
|
history *queue.Queue[C.DelayHistory]
|
||||||
alive *atomic.Bool
|
alive atomic.Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Proxy struct {
|
type Proxy struct {
|
||||||
C.ProxyAdapter
|
C.ProxyAdapter
|
||||||
history *queue.Queue[C.DelayHistory]
|
history *queue.Queue[C.DelayHistory]
|
||||||
alive *atomic.Bool
|
alive atomic.Bool
|
||||||
url string
|
url string
|
||||||
extra map[string]*extraProxyState
|
extra *xsync.MapOf[string, *extraProxyState]
|
||||||
}
|
|
||||||
|
|
||||||
// Alive implements C.Proxy
|
|
||||||
func (p *Proxy) Alive() bool {
|
|
||||||
return p.alive.Load()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AliveForTestUrl implements C.Proxy
|
// AliveForTestUrl implements C.Proxy
|
||||||
func (p *Proxy) AliveForTestUrl(url string) bool {
|
func (p *Proxy) AliveForTestUrl(url string) bool {
|
||||||
if p.extra != nil {
|
if state, ok := p.extra.Load(url); ok {
|
||||||
if state, ok := p.extra[url]; ok {
|
return state.alive.Load()
|
||||||
return state.alive.Load()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return p.alive.Load()
|
return p.alive.Load()
|
||||||
@@ -88,16 +81,16 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
// DelayHistoryForTestUrl implements C.Proxy
|
// DelayHistoryForTestUrl implements C.Proxy
|
||||||
func (p *Proxy) DelayHistoryForTestUrl(url string) []C.DelayHistory {
|
func (p *Proxy) DelayHistoryForTestUrl(url string) []C.DelayHistory {
|
||||||
var queueM []C.DelayHistory
|
var queueM []C.DelayHistory
|
||||||
if p.extra != nil {
|
|
||||||
if state, ok := p.extra[url]; ok {
|
if state, ok := p.extra.Load(url); ok {
|
||||||
queueM = state.history.Copy()
|
queueM = state.history.Copy()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if queueM == nil {
|
if queueM == nil {
|
||||||
@@ -112,19 +105,25 @@ func (p *Proxy) DelayHistoryForTestUrl(url string) []C.DelayHistory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Proxy) ExtraDelayHistory() map[string][]C.DelayHistory {
|
func (p *Proxy) ExtraDelayHistory() map[string][]C.DelayHistory {
|
||||||
extra := map[string][]C.DelayHistory{}
|
extraHistory := map[string][]C.DelayHistory{}
|
||||||
if p.extra != nil && len(p.extra) != 0 {
|
|
||||||
for testUrl, option := range p.extra {
|
|
||||||
histories := []C.DelayHistory{}
|
|
||||||
queueM := option.history.Copy()
|
|
||||||
for _, item := range queueM {
|
|
||||||
histories = append(histories, item)
|
|
||||||
}
|
|
||||||
|
|
||||||
extra[testUrl] = histories
|
p.extra.Range(func(k string, v *extraProxyState) bool {
|
||||||
|
|
||||||
|
testUrl := k
|
||||||
|
state := v
|
||||||
|
|
||||||
|
histories := []C.DelayHistory{}
|
||||||
|
queueM := state.history.Copy()
|
||||||
|
|
||||||
|
for _, item := range queueM {
|
||||||
|
histories = append(histories, item)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return extra
|
extraHistory[testUrl] = histories
|
||||||
|
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
return extraHistory
|
||||||
}
|
}
|
||||||
|
|
||||||
// LastDelay return last history record. 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.
|
||||||
@@ -149,11 +148,9 @@ func (p *Proxy) LastDelayForTestUrl(url string) (delay uint16) {
|
|||||||
alive := p.alive.Load()
|
alive := p.alive.Load()
|
||||||
history := p.history.Last()
|
history := p.history.Last()
|
||||||
|
|
||||||
if p.extra != nil {
|
if state, ok := p.extra.Load(url); ok {
|
||||||
if state, ok := p.extra[url]; ok {
|
alive = state.alive.Load()
|
||||||
alive = state.alive.Load()
|
history = state.history.Last()
|
||||||
history = state.history.Last()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !alive {
|
if !alive {
|
||||||
@@ -177,7 +174,7 @@ func (p *Proxy) MarshalJSON() ([]byte, error) {
|
|||||||
_ = json.Unmarshal(inner, &mapping)
|
_ = json.Unmarshal(inner, &mapping)
|
||||||
mapping["history"] = p.DelayHistory()
|
mapping["history"] = p.DelayHistory()
|
||||||
mapping["extra"] = p.ExtraDelayHistory()
|
mapping["extra"] = p.ExtraDelayHistory()
|
||||||
mapping["alive"] = p.Alive()
|
mapping["alive"] = p.AliveForTestUrl(p.url)
|
||||||
mapping["name"] = p.Name()
|
mapping["name"] = p.Name()
|
||||||
mapping["udp"] = p.SupportUDP()
|
mapping["udp"] = p.SupportUDP()
|
||||||
mapping["xudp"] = p.SupportXUDP()
|
mapping["xudp"] = p.SupportXUDP()
|
||||||
@@ -187,13 +184,11 @@ func (p *Proxy) MarshalJSON() ([]byte, error) {
|
|||||||
|
|
||||||
// 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], store C.DelayHistoryStoreType) (t uint16, err error) {
|
func (p *Proxy) URLTest(ctx context.Context, url string, expectedStatus utils.IntRanges[uint16]) (t uint16, err error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
alive := err == nil
|
alive := err == nil
|
||||||
store = p.determineFinalStoreType(store, url)
|
|
||||||
|
|
||||||
switch store {
|
if len(p.url) == 0 || url == p.url {
|
||||||
case C.OriginalHistory:
|
|
||||||
p.alive.Store(alive)
|
p.alive.Store(alive)
|
||||||
record := C.DelayHistory{Time: time.Now()}
|
record := C.DelayHistory{Time: time.Now()}
|
||||||
if alive {
|
if alive {
|
||||||
@@ -208,23 +203,23 @@ func (p *Proxy) URLTest(ctx context.Context, url string, expectedStatus utils.In
|
|||||||
if len(p.url) == 0 {
|
if len(p.url) == 0 {
|
||||||
p.url = url
|
p.url = url
|
||||||
}
|
}
|
||||||
case C.ExtraHistory:
|
} else {
|
||||||
record := C.DelayHistory{Time: time.Now()}
|
record := C.DelayHistory{Time: time.Now()}
|
||||||
if alive {
|
if alive {
|
||||||
record.Delay = t
|
record.Delay = t
|
||||||
}
|
}
|
||||||
|
p.history.Put(record)
|
||||||
if p.extra == nil {
|
if p.history.Len() > defaultHistoriesNum {
|
||||||
p.extra = map[string]*extraProxyState{}
|
p.history.Pop()
|
||||||
}
|
}
|
||||||
|
|
||||||
state, ok := p.extra[url]
|
state, ok := p.extra.Load(url)
|
||||||
if !ok {
|
if !ok {
|
||||||
state = &extraProxyState{
|
state = &extraProxyState{
|
||||||
history: queue.New[C.DelayHistory](defaultHistoriesNum),
|
history: queue.New[C.DelayHistory](defaultHistoriesNum),
|
||||||
alive: atomic.NewBool(true),
|
alive: atomic.NewBool(true),
|
||||||
}
|
}
|
||||||
p.extra[url] = state
|
p.extra.Store(url, state)
|
||||||
}
|
}
|
||||||
|
|
||||||
state.alive.Store(alive)
|
state.alive.Store(alive)
|
||||||
@@ -232,8 +227,6 @@ func (p *Proxy) URLTest(ctx context.Context, url string, expectedStatus utils.In
|
|||||||
if state.history.Len() > defaultHistoriesNum {
|
if state.history.Len() > defaultHistoriesNum {
|
||||||
state.history.Pop()
|
state.history.Pop()
|
||||||
}
|
}
|
||||||
default:
|
|
||||||
log.Debugln("health check result will be discarded, url: %s alive: %t, delay: %d", url, alive, t)
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -307,7 +300,12 @@ func (p *Proxy) URLTest(ctx context.Context, url string, expectedStatus utils.In
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewProxy(adapter C.ProxyAdapter) *Proxy {
|
func NewProxy(adapter C.ProxyAdapter) *Proxy {
|
||||||
return &Proxy{adapter, queue.New[C.DelayHistory](defaultHistoriesNum), atomic.NewBool(true), "", map[string]*extraProxyState{}}
|
return &Proxy{
|
||||||
|
ProxyAdapter: adapter,
|
||||||
|
history: queue.New[C.DelayHistory](defaultHistoriesNum),
|
||||||
|
alive: atomic.NewBool(true),
|
||||||
|
url: "",
|
||||||
|
extra: xsync.NewMapOf[string, *extraProxyState]()}
|
||||||
}
|
}
|
||||||
|
|
||||||
func urlToMetadata(rawURL string) (addr C.Metadata, err error) {
|
func urlToMetadata(rawURL string) (addr C.Metadata, err error) {
|
||||||
@@ -340,24 +338,3 @@ 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 == nil {
|
|
||||||
store = C.ExtraHistory
|
|
||||||
} else {
|
|
||||||
if _, ok := p.extra[url]; ok {
|
|
||||||
store = C.ExtraHistory
|
|
||||||
} else if len(p.extra) < 2*C.DefaultMaxHealthCheckUrlNum {
|
|
||||||
store = C.ExtraHistory
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return store
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,13 +1,17 @@
|
|||||||
package inbound
|
package inbound
|
||||||
|
|
||||||
import (
|
import (
|
||||||
C "github.com/Dreamacro/clash/constant"
|
"net"
|
||||||
|
|
||||||
|
C "github.com/metacubex/mihomo/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Addition func(metadata *C.Metadata)
|
type Addition func(metadata *C.Metadata)
|
||||||
|
|
||||||
func (a Addition) Apply(metadata *C.Metadata) {
|
func ApplyAdditions(metadata *C.Metadata, additions ...Addition) {
|
||||||
a(metadata)
|
for _, addition := range additions {
|
||||||
|
addition(metadata)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithInName(name string) Addition {
|
func WithInName(name string) Addition {
|
||||||
@@ -33,3 +37,29 @@ func WithSpecialProxy(specialProxy string) Addition {
|
|||||||
metadata.SpecialProxy = specialProxy
|
metadata.SpecialProxy = specialProxy
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithDstAddr(addr net.Addr) Addition {
|
||||||
|
return func(metadata *C.Metadata) {
|
||||||
|
_ = metadata.SetRemoteAddr(addr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithSrcAddr(addr net.Addr) Addition {
|
||||||
|
return func(metadata *C.Metadata) {
|
||||||
|
m := C.Metadata{}
|
||||||
|
if err := m.SetRemoteAddr(addr);err ==nil{
|
||||||
|
metadata.SrcIP = m.DstIP
|
||||||
|
metadata.SrcPort = m.DstPort
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithInAddr(addr net.Addr) Addition {
|
||||||
|
return func(metadata *C.Metadata) {
|
||||||
|
m := C.Metadata{}
|
||||||
|
if err := m.SetRemoteAddr(addr);err ==nil{
|
||||||
|
metadata.InIP = m.DstIP
|
||||||
|
metadata.InPort = m.DstPort
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
45
adapter/inbound/auth.go
Normal file
45
adapter/inbound/auth.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package inbound
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"net/netip"
|
||||||
|
|
||||||
|
C "github.com/metacubex/mihomo/constant"
|
||||||
|
)
|
||||||
|
|
||||||
|
var skipAuthPrefixes []netip.Prefix
|
||||||
|
|
||||||
|
func SetSkipAuthPrefixes(prefixes []netip.Prefix) {
|
||||||
|
skipAuthPrefixes = prefixes
|
||||||
|
}
|
||||||
|
|
||||||
|
func SkipAuthPrefixes() []netip.Prefix {
|
||||||
|
return skipAuthPrefixes
|
||||||
|
}
|
||||||
|
|
||||||
|
func SkipAuthRemoteAddr(addr net.Addr) bool {
|
||||||
|
m := C.Metadata{}
|
||||||
|
if err := m.SetRemoteAddr(addr); err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return skipAuth(m.AddrPort().Addr())
|
||||||
|
}
|
||||||
|
|
||||||
|
func SkipAuthRemoteAddress(addr string) bool {
|
||||||
|
m := C.Metadata{}
|
||||||
|
if err := m.SetRemoteAddress(addr); err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return skipAuth(m.AddrPort().Addr())
|
||||||
|
}
|
||||||
|
|
||||||
|
func skipAuth(addr netip.Addr) bool {
|
||||||
|
if addr.IsValid() {
|
||||||
|
for _, prefix := range skipAuthPrefixes {
|
||||||
|
if prefix.Contains(addr.Unmap()) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
@@ -3,26 +3,18 @@ package inbound
|
|||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/Dreamacro/clash/context"
|
"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
|
||||||
func NewHTTP(target socks5.Addr, source net.Addr, conn net.Conn, additions ...Addition) *context.ConnContext {
|
func NewHTTP(target socks5.Addr, srcConn net.Conn, conn net.Conn, additions ...Addition) (net.Conn, *C.Metadata) {
|
||||||
metadata := parseSocksAddr(target)
|
metadata := parseSocksAddr(target)
|
||||||
metadata.NetWork = C.TCP
|
metadata.NetWork = C.TCP
|
||||||
metadata.Type = C.HTTP
|
metadata.Type = C.HTTP
|
||||||
for _, addition := range additions {
|
metadata.RawSrcAddr = srcConn.RemoteAddr()
|
||||||
addition.Apply(metadata)
|
metadata.RawDstAddr = srcConn.LocalAddr()
|
||||||
}
|
ApplyAdditions(metadata, WithSrcAddr(srcConn.RemoteAddr()), WithInAddr(conn.LocalAddr()))
|
||||||
if ip, port, err := parseAddr(source); err == nil {
|
ApplyAdditions(metadata, additions...)
|
||||||
metadata.SrcIP = ip
|
return conn, metadata
|
||||||
metadata.SrcPort = port
|
|
||||||
}
|
|
||||||
if ip, port, err := parseAddr(conn.LocalAddr()); err == nil {
|
|
||||||
metadata.InIP = ip
|
|
||||||
metadata.InPort = port
|
|
||||||
}
|
|
||||||
return context.NewConnContext(conn, metadata)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,24 +4,14 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/Dreamacro/clash/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewHTTPS receive CONNECT request and return ConnContext
|
// NewHTTPS receive CONNECT request and return ConnContext
|
||||||
func NewHTTPS(request *http.Request, conn net.Conn, additions ...Addition) *context.ConnContext {
|
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
|
||||||
for _, addition := range additions {
|
ApplyAdditions(metadata, WithSrcAddr(conn.RemoteAddr()), WithInAddr(conn.LocalAddr()))
|
||||||
addition.Apply(metadata)
|
ApplyAdditions(metadata, additions...)
|
||||||
}
|
return conn, metadata
|
||||||
if ip, port, err := parseAddr(conn.RemoteAddr()); err == nil {
|
|
||||||
metadata.SrcIP = ip
|
|
||||||
metadata.SrcPort = port
|
|
||||||
}
|
|
||||||
if ip, port, err := parseAddr(conn.LocalAddr()); err == nil {
|
|
||||||
metadata.InIP = ip
|
|
||||||
metadata.InPort = port
|
|
||||||
}
|
|
||||||
return context.NewConnContext(conn, metadata)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,42 +1,22 @@
|
|||||||
package inbound
|
package inbound
|
||||||
|
|
||||||
import (
|
import (
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/Dreamacro/clash/transport/socks5"
|
"github.com/metacubex/mihomo/transport/socks5"
|
||||||
)
|
)
|
||||||
|
|
||||||
// PacketAdapter is a UDP Packet adapter for socks/redir/tun
|
|
||||||
type PacketAdapter struct {
|
|
||||||
C.UDPPacket
|
|
||||||
metadata *C.Metadata
|
|
||||||
}
|
|
||||||
|
|
||||||
// Metadata returns destination metadata
|
|
||||||
func (s *PacketAdapter) Metadata() *C.Metadata {
|
|
||||||
return s.metadata
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewPacket is PacketAdapter generator
|
// NewPacket is PacketAdapter generator
|
||||||
func NewPacket(target socks5.Addr, packet C.UDPPacket, source C.Type, additions ...Addition) C.PacketAdapter {
|
func NewPacket(target socks5.Addr, packet C.UDPPacket, source C.Type, additions ...Addition) (C.UDPPacket, *C.Metadata) {
|
||||||
metadata := parseSocksAddr(target)
|
metadata := parseSocksAddr(target)
|
||||||
metadata.NetWork = C.UDP
|
metadata.NetWork = C.UDP
|
||||||
metadata.Type = source
|
metadata.Type = source
|
||||||
for _, addition := range additions {
|
metadata.RawSrcAddr = packet.LocalAddr()
|
||||||
addition.Apply(metadata)
|
metadata.RawDstAddr = metadata.UDPAddr()
|
||||||
}
|
ApplyAdditions(metadata, WithSrcAddr(packet.LocalAddr()))
|
||||||
if ip, port, err := parseAddr(packet.LocalAddr()); err == nil {
|
|
||||||
metadata.SrcIP = ip
|
|
||||||
metadata.SrcPort = port
|
|
||||||
}
|
|
||||||
if p, ok := packet.(C.UDPPacketInAddr); ok {
|
if p, ok := packet.(C.UDPPacketInAddr); ok {
|
||||||
if ip, port, err := parseAddr(p.InAddr()); err == nil {
|
ApplyAdditions(metadata, WithInAddr(p.InAddr()))
|
||||||
metadata.InIP = ip
|
|
||||||
metadata.InPort = port
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
ApplyAdditions(metadata, additions...)
|
||||||
|
|
||||||
return &PacketAdapter{
|
return packet, metadata
|
||||||
packet,
|
|
||||||
metadata,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,51 +2,17 @@ package inbound
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/Dreamacro/clash/context"
|
"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
|
||||||
func NewSocket(target socks5.Addr, conn net.Conn, source C.Type, additions ...Addition) *context.ConnContext {
|
func NewSocket(target socks5.Addr, conn net.Conn, source C.Type, additions ...Addition) (net.Conn, *C.Metadata) {
|
||||||
metadata := parseSocksAddr(target)
|
metadata := parseSocksAddr(target)
|
||||||
metadata.NetWork = C.TCP
|
metadata.NetWork = C.TCP
|
||||||
metadata.Type = source
|
metadata.Type = source
|
||||||
for _, addition := range additions {
|
ApplyAdditions(metadata, WithSrcAddr(conn.RemoteAddr()), WithInAddr(conn.LocalAddr()))
|
||||||
addition.Apply(metadata)
|
ApplyAdditions(metadata, additions...)
|
||||||
}
|
return conn, metadata
|
||||||
|
|
||||||
if ip, port, err := parseAddr(conn.RemoteAddr()); err == nil {
|
|
||||||
metadata.SrcIP = ip
|
|
||||||
metadata.SrcPort = port
|
|
||||||
}
|
|
||||||
if ip, port, err := parseAddr(conn.LocalAddr()); err == nil {
|
|
||||||
metadata.InIP = ip
|
|
||||||
metadata.InPort = port
|
|
||||||
}
|
|
||||||
|
|
||||||
return context.NewConnContext(conn, metadata)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewInner(conn net.Conn, address string) *context.ConnContext {
|
|
||||||
metadata := &C.Metadata{}
|
|
||||||
metadata.NetWork = C.TCP
|
|
||||||
metadata.Type = C.INNER
|
|
||||||
metadata.DNSMode = C.DNSNormal
|
|
||||||
metadata.Process = C.ClashName
|
|
||||||
if h, port, err := net.SplitHostPort(address); err == nil {
|
|
||||||
if port, err := strconv.ParseUint(port, 10, 16); err == nil {
|
|
||||||
metadata.DstPort = uint16(port)
|
|
||||||
}
|
|
||||||
if ip, err := netip.ParseAddr(h); err == nil {
|
|
||||||
metadata.DstIP = ip
|
|
||||||
} else {
|
|
||||||
metadata.Host = h
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return context.NewConnContext(conn, metadata)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,15 @@
|
|||||||
package inbound
|
package inbound
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/nnip"
|
"github.com/metacubex/mihomo/common/nnip"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/Dreamacro/clash/transport/socks5"
|
"github.com/metacubex/mihomo/transport/socks5"
|
||||||
)
|
)
|
||||||
|
|
||||||
func parseSocksAddr(target socks5.Addr) *C.Metadata {
|
func parseSocksAddr(target socks5.Addr) *C.Metadata {
|
||||||
@@ -62,29 +61,3 @@ func parseHTTPAddr(request *http.Request) *C.Metadata {
|
|||||||
|
|
||||||
return metadata
|
return metadata
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseAddr(addr net.Addr) (netip.Addr, uint16, error) {
|
|
||||||
// Filter when net.Addr interface is nil
|
|
||||||
if addr == nil {
|
|
||||||
return netip.Addr{}, 0, errors.New("nil addr")
|
|
||||||
}
|
|
||||||
if rawAddr, ok := addr.(interface{ RawAddr() net.Addr }); ok {
|
|
||||||
ip, port, err := parseAddr(rawAddr.RawAddr())
|
|
||||||
if err == nil {
|
|
||||||
return ip, port, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
addrStr := addr.String()
|
|
||||||
host, port, err := net.SplitHostPort(addrStr)
|
|
||||||
if err != nil {
|
|
||||||
return netip.Addr{}, 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var uint16Port uint16
|
|
||||||
if port, err := strconv.ParseUint(port, 10, 16); err == nil {
|
|
||||||
uint16Port = uint16(port)
|
|
||||||
}
|
|
||||||
|
|
||||||
ip, err := netip.ParseAddr(host)
|
|
||||||
return ip, uint16Port, err
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -7,10 +7,10 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
N "github.com/Dreamacro/clash/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/Dreamacro/clash/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Base struct {
|
type Base struct {
|
||||||
|
|||||||
@@ -3,15 +3,23 @@ package outbound
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"net/netip"
|
||||||
"github.com/Dreamacro/clash/component/resolver"
|
|
||||||
C "github.com/Dreamacro/clash/constant"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
|
"github.com/metacubex/mihomo/component/resolver"
|
||||||
|
C "github.com/metacubex/mihomo/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Direct struct {
|
type Direct struct {
|
||||||
*Base
|
*Base
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DirectOption struct {
|
||||||
|
BasicOption
|
||||||
|
Name string `proxy:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (d *Direct) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
func (d *Direct) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
||||||
opts = append(opts, dialer.WithResolver(resolver.DefaultResolver))
|
opts = append(opts, dialer.WithResolver(resolver.DefaultResolver))
|
||||||
@@ -19,7 +27,7 @@ func (d *Direct) DialContext(ctx context.Context, metadata *C.Metadata, opts ...
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
N.TCPKeepAlive(c)
|
||||||
return NewConn(c, d), nil
|
return NewConn(c, d), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,13 +41,28 @@ func (d *Direct) ListenPacketContext(ctx context.Context, metadata *C.Metadata,
|
|||||||
}
|
}
|
||||||
metadata.DstIP = ip
|
metadata.DstIP = ip
|
||||||
}
|
}
|
||||||
pc, err := dialer.ListenPacket(ctx, dialer.ParseNetwork("udp", metadata.DstIP), "", d.Base.DialOptions(opts...)...)
|
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 newPacketConn(pc, d), nil
|
return newPacketConn(pc, d), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewDirectWithOption(option DirectOption) *Direct {
|
||||||
|
return &Direct{
|
||||||
|
Base: &Base{
|
||||||
|
name: option.Name,
|
||||||
|
tp: C.Direct,
|
||||||
|
udp: true,
|
||||||
|
tfo: option.TFO,
|
||||||
|
mpTcp: option.MPTCP,
|
||||||
|
iface: option.Interface,
|
||||||
|
rmark: option.RoutingMark,
|
||||||
|
prefer: C.NewDNSPrefer(option.IPVersion),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func NewDirect() *Direct {
|
func NewDirect() *Direct {
|
||||||
return &Direct{
|
return &Direct{
|
||||||
Base: &Base{
|
Base: &Base{
|
||||||
|
|||||||
@@ -7,15 +7,17 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/Dreamacro/clash/component/proxydialer"
|
"github.com/metacubex/mihomo/component/ca"
|
||||||
tlsC "github.com/Dreamacro/clash/component/tls"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
"github.com/metacubex/mihomo/component/proxydialer"
|
||||||
|
C "github.com/metacubex/mihomo/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Http struct {
|
type Http struct {
|
||||||
@@ -74,7 +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)
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
N.TCPKeepAlive(c)
|
||||||
|
|
||||||
defer func(c net.Conn) {
|
defer func(c net.Conn) {
|
||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
@@ -155,19 +157,13 @@ func NewHttp(option HttpOption) (*Http, error) {
|
|||||||
if option.SNI != "" {
|
if option.SNI != "" {
|
||||||
sni = option.SNI
|
sni = option.SNI
|
||||||
}
|
}
|
||||||
if len(option.Fingerprint) == 0 {
|
var err error
|
||||||
tlsConfig = tlsC.GetGlobalTLSConfig(&tls.Config{
|
tlsConfig, err = ca.GetSpecifiedFingerprintTLSConfig(&tls.Config{
|
||||||
InsecureSkipVerify: option.SkipCertVerify,
|
InsecureSkipVerify: option.SkipCertVerify,
|
||||||
ServerName: sni,
|
ServerName: sni,
|
||||||
})
|
}, option.Fingerprint)
|
||||||
} else {
|
if err != nil {
|
||||||
var err error
|
return nil, err
|
||||||
if tlsConfig, err = tlsC.GetSpecifiedFingerprintTLSConfig(&tls.Config{
|
|
||||||
InsecureSkipVerify: option.SkipCertVerify,
|
|
||||||
ServerName: sni,
|
|
||||||
}, option.Fingerprint); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,16 +2,11 @@ package outbound
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/sha256"
|
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/hex"
|
|
||||||
"encoding/pem"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
|
||||||
"regexp"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -19,17 +14,17 @@ import (
|
|||||||
"github.com/metacubex/quic-go/congestion"
|
"github.com/metacubex/quic-go/congestion"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/metacubex/mihomo/component/ca"
|
||||||
"github.com/Dreamacro/clash/component/proxydialer"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
tlsC "github.com/Dreamacro/clash/component/tls"
|
"github.com/metacubex/mihomo/component/proxydialer"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/Dreamacro/clash/log"
|
"github.com/metacubex/mihomo/log"
|
||||||
hyCongestion "github.com/Dreamacro/clash/transport/hysteria/congestion"
|
hyCongestion "github.com/metacubex/mihomo/transport/hysteria/congestion"
|
||||||
"github.com/Dreamacro/clash/transport/hysteria/core"
|
"github.com/metacubex/mihomo/transport/hysteria/core"
|
||||||
"github.com/Dreamacro/clash/transport/hysteria/obfs"
|
"github.com/metacubex/mihomo/transport/hysteria/obfs"
|
||||||
"github.com/Dreamacro/clash/transport/hysteria/pmtud_fix"
|
"github.com/metacubex/mihomo/transport/hysteria/pmtud_fix"
|
||||||
"github.com/Dreamacro/clash/transport/hysteria/transport"
|
"github.com/metacubex/mihomo/transport/hysteria/transport"
|
||||||
"github.com/Dreamacro/clash/transport/hysteria/utils"
|
"github.com/metacubex/mihomo/transport/hysteria/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -43,8 +38,6 @@ const (
|
|||||||
DefaultHopInterval = 10
|
DefaultHopInterval = 10
|
||||||
)
|
)
|
||||||
|
|
||||||
var rateStringRegexp = regexp.MustCompile(`^(\d+)\s*([KMGT]?)([Bb])ps$`)
|
|
||||||
|
|
||||||
type Hysteria struct {
|
type Hysteria struct {
|
||||||
*Base
|
*Base
|
||||||
|
|
||||||
@@ -53,7 +46,7 @@ type Hysteria struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hysteria) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
func (h *Hysteria) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
||||||
tcpConn, err := h.client.DialTCP(metadata.RemoteAddress(), h.genHdc(ctx, opts...))
|
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
|
||||||
}
|
}
|
||||||
@@ -120,12 +113,12 @@ type HysteriaOption struct {
|
|||||||
|
|
||||||
func (c *HysteriaOption) Speed() (uint64, uint64, error) {
|
func (c *HysteriaOption) Speed() (uint64, uint64, error) {
|
||||||
var up, down uint64
|
var up, down uint64
|
||||||
up = stringToBps(c.Up)
|
up = StringToBps(c.Up)
|
||||||
if up == 0 {
|
if up == 0 {
|
||||||
return 0, 0, fmt.Errorf("invaild upload speed: %s", c.Up)
|
return 0, 0, fmt.Errorf("invaild upload speed: %s", c.Up)
|
||||||
}
|
}
|
||||||
|
|
||||||
down = stringToBps(c.Down)
|
down = StringToBps(c.Down)
|
||||||
if down == 0 {
|
if down == 0 {
|
||||||
return 0, 0, fmt.Errorf("invaild download speed: %s", c.Down)
|
return 0, 0, fmt.Errorf("invaild download speed: %s", c.Down)
|
||||||
}
|
}
|
||||||
@@ -153,37 +146,10 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
|
|||||||
MinVersion: tls.VersionTLS13,
|
MinVersion: tls.VersionTLS13,
|
||||||
}
|
}
|
||||||
|
|
||||||
var bs []byte
|
|
||||||
var err error
|
var err error
|
||||||
if len(option.CustomCA) > 0 {
|
tlsConfig, err = ca.GetTLSConfig(tlsConfig, option.Fingerprint, option.CustomCA, option.CustomCAString)
|
||||||
bs, err = os.ReadFile(option.CustomCA)
|
if err != nil {
|
||||||
if err != nil {
|
return nil, err
|
||||||
return nil, fmt.Errorf("hysteria %s load ca error: %w", addr, err)
|
|
||||||
}
|
|
||||||
} else if option.CustomCAString != "" {
|
|
||||||
bs = []byte(option.CustomCAString)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(bs) > 0 {
|
|
||||||
block, _ := pem.Decode(bs)
|
|
||||||
if block == nil {
|
|
||||||
return nil, fmt.Errorf("CA cert is not PEM")
|
|
||||||
}
|
|
||||||
|
|
||||||
fpBytes := sha256.Sum256(block.Bytes)
|
|
||||||
if len(option.Fingerprint) == 0 {
|
|
||||||
option.Fingerprint = hex.EncodeToString(fpBytes[:])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(option.Fingerprint) != 0 {
|
|
||||||
var err error
|
|
||||||
tlsConfig, err = tlsC.GetSpecifiedFingerprintTLSConfig(tlsConfig, option.Fingerprint)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
tlsConfig = tlsC.GetGlobalTLSConfig(tlsConfig)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(option.ALPN) > 0 {
|
if len(option.ALPN) > 0 {
|
||||||
@@ -268,42 +234,6 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func stringToBps(s string) uint64 {
|
|
||||||
if s == "" {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// when have not unit, use Mbps
|
|
||||||
if v, err := strconv.Atoi(s); err == nil {
|
|
||||||
return stringToBps(fmt.Sprintf("%d Mbps", v))
|
|
||||||
}
|
|
||||||
|
|
||||||
m := rateStringRegexp.FindStringSubmatch(s)
|
|
||||||
if m == nil {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
var n uint64
|
|
||||||
switch m[2] {
|
|
||||||
case "K":
|
|
||||||
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)
|
|
||||||
n = v * n
|
|
||||||
if m[3] == "b" {
|
|
||||||
// Bits, need to convert to bytes
|
|
||||||
n = n >> 3
|
|
||||||
}
|
|
||||||
return n
|
|
||||||
}
|
|
||||||
|
|
||||||
type hyPacketConn struct {
|
type hyPacketConn struct {
|
||||||
core.UDPConn
|
core.UDPConn
|
||||||
}
|
}
|
||||||
|
|||||||
157
adapter/outbound/hysteria2.go
Normal file
157
adapter/outbound/hysteria2.go
Normal file
@@ -0,0 +1,157 @@
|
|||||||
|
package outbound
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"runtime"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
CN "github.com/metacubex/mihomo/common/net"
|
||||||
|
"github.com/metacubex/mihomo/component/ca"
|
||||||
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
|
"github.com/metacubex/mihomo/component/proxydialer"
|
||||||
|
C "github.com/metacubex/mihomo/constant"
|
||||||
|
tuicCommon "github.com/metacubex/mihomo/transport/tuic/common"
|
||||||
|
|
||||||
|
"github.com/metacubex/sing-quic/hysteria2"
|
||||||
|
|
||||||
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
hysteria2.SetCongestionController = tuicCommon.SetCongestionController
|
||||||
|
}
|
||||||
|
|
||||||
|
type Hysteria2 struct {
|
||||||
|
*Base
|
||||||
|
|
||||||
|
option *Hysteria2Option
|
||||||
|
client *hysteria2.Client
|
||||||
|
dialer proxydialer.SingDialer
|
||||||
|
}
|
||||||
|
|
||||||
|
type Hysteria2Option struct {
|
||||||
|
BasicOption
|
||||||
|
Name string `proxy:"name"`
|
||||||
|
Server string `proxy:"server"`
|
||||||
|
Port int `proxy:"port"`
|
||||||
|
Up string `proxy:"up,omitempty"`
|
||||||
|
Down string `proxy:"down,omitempty"`
|
||||||
|
Password string `proxy:"password,omitempty"`
|
||||||
|
Obfs string `proxy:"obfs,omitempty"`
|
||||||
|
ObfsPassword string `proxy:"obfs-password,omitempty"`
|
||||||
|
SNI string `proxy:"sni,omitempty"`
|
||||||
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
||||||
|
Fingerprint string `proxy:"fingerprint,omitempty"`
|
||||||
|
ALPN []string `proxy:"alpn,omitempty"`
|
||||||
|
CustomCA string `proxy:"ca,omitempty"`
|
||||||
|
CustomCAString string `proxy:"ca-str,omitempty"`
|
||||||
|
CWND int `proxy:"cwnd,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return NewConn(CN.NewRefConn(c, h), h), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if pc == nil {
|
||||||
|
return nil, errors.New("packetConn is nil")
|
||||||
|
}
|
||||||
|
return newPacketConn(CN.NewRefPacketConn(CN.NewThreadSafePacketConn(pc), h), h), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func closeHysteria2(h *Hysteria2) {
|
||||||
|
if h.client != nil {
|
||||||
|
_ = h.client.CloseWithError(errors.New("proxy removed"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHysteria2(option Hysteria2Option) (*Hysteria2, error) {
|
||||||
|
addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port))
|
||||||
|
var salamanderPassword string
|
||||||
|
if len(option.Obfs) > 0 {
|
||||||
|
if option.ObfsPassword == "" {
|
||||||
|
return nil, errors.New("missing obfs password")
|
||||||
|
}
|
||||||
|
switch option.Obfs {
|
||||||
|
case hysteria2.ObfsTypeSalamander:
|
||||||
|
salamanderPassword = option.ObfsPassword
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("unknown obfs type: %s", option.Obfs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
serverName := option.Server
|
||||||
|
if option.SNI != "" {
|
||||||
|
serverName = option.SNI
|
||||||
|
}
|
||||||
|
|
||||||
|
tlsConfig := &tls.Config{
|
||||||
|
ServerName: serverName,
|
||||||
|
InsecureSkipVerify: option.SkipCertVerify,
|
||||||
|
MinVersion: tls.VersionTLS13,
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
tlsConfig, err = ca.GetTLSConfig(tlsConfig, option.Fingerprint, option.CustomCA, option.CustomCAString)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(option.ALPN) > 0 {
|
||||||
|
tlsConfig.NextProtos = option.ALPN
|
||||||
|
}
|
||||||
|
|
||||||
|
singDialer := proxydialer.NewByNameSingDialer(option.DialerProxy, dialer.NewDialer())
|
||||||
|
|
||||||
|
clientOptions := hysteria2.ClientOptions{
|
||||||
|
Context: context.TODO(),
|
||||||
|
Dialer: singDialer,
|
||||||
|
ServerAddress: M.ParseSocksaddrHostPort(option.Server, uint16(option.Port)),
|
||||||
|
SendBPS: StringToBps(option.Up),
|
||||||
|
ReceiveBPS: StringToBps(option.Down),
|
||||||
|
SalamanderPassword: salamanderPassword,
|
||||||
|
Password: option.Password,
|
||||||
|
TLSConfig: tlsConfig,
|
||||||
|
UDPDisabled: false,
|
||||||
|
CWND: option.CWND,
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := hysteria2.NewClient(clientOptions)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
tlsC "github.com/Dreamacro/clash/component/tls"
|
tlsC "github.com/metacubex/mihomo/component/tls"
|
||||||
|
|
||||||
"golang.org/x/crypto/curve25519"
|
"golang.org/x/crypto/curve25519"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -6,23 +6,44 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/buf"
|
"github.com/metacubex/mihomo/common/buf"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Reject struct {
|
type Reject struct {
|
||||||
*Base
|
*Base
|
||||||
|
drop bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type RejectOption struct {
|
||||||
|
Name string `proxy:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
func (r *Reject) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.Conn, error) {
|
func (r *Reject) DialContext(ctx context.Context, metadata *C.Metadata, 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, opts ...dialer.Option) (C.PacketConn, error) {
|
func (r *Reject) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (C.PacketConn, error) {
|
||||||
return newPacketConn(nopPacketConn{}, r), nil
|
if r.drop {
|
||||||
|
return newPacketConn(&dropPacketConn{}, r), nil
|
||||||
|
}
|
||||||
|
return newPacketConn(&nopPacketConn{}, r), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRejectWithOption(option RejectOption) *Reject {
|
||||||
|
return &Reject{
|
||||||
|
Base: &Base{
|
||||||
|
name: option.Name,
|
||||||
|
tp: C.Direct,
|
||||||
|
udp: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewReject() *Reject {
|
func NewReject() *Reject {
|
||||||
@@ -36,6 +57,18 @@ 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{
|
||||||
@@ -49,35 +82,29 @@ func NewPass() *Reject {
|
|||||||
|
|
||||||
type nopConn struct{}
|
type nopConn struct{}
|
||||||
|
|
||||||
func (rw nopConn) Read(b []byte) (int, error) {
|
func (rw nopConn) Read(b []byte) (int, error) { return 0, io.EOF }
|
||||||
return 0, io.EOF
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rw nopConn) ReadBuffer(buffer *buf.Buffer) error {
|
func (rw nopConn) ReadBuffer(buffer *buf.Buffer) error { return io.EOF }
|
||||||
return io.EOF
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rw nopConn) Write(b []byte) (int, error) {
|
func (rw nopConn) Write(b []byte) (int, error) { return 0, io.EOF }
|
||||||
return 0, io.EOF
|
func (rw nopConn) WriteBuffer(buffer *buf.Buffer) error { return io.EOF }
|
||||||
}
|
func (rw nopConn) Close() error { return nil }
|
||||||
|
func (rw nopConn) LocalAddr() net.Addr { return nil }
|
||||||
func (rw nopConn) WriteBuffer(buffer *buf.Buffer) error {
|
func (rw nopConn) RemoteAddr() net.Addr { return nil }
|
||||||
return io.EOF
|
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 }
|
||||||
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) { return len(b), nil }
|
func (npc nopPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
||||||
func (npc nopPacketConn) ReadFrom(b []byte) (int, net.Addr, error) { return 0, nil, io.EOF }
|
return len(b), nil
|
||||||
|
}
|
||||||
|
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
|
||||||
}
|
}
|
||||||
@@ -86,3 +113,38 @@ 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 }
|
||||||
|
|
||||||
|
type dropPacketConn struct{}
|
||||||
|
|
||||||
|
func (npc dropPacketConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
|
||||||
|
time.Sleep(C.DefaultDropTime)
|
||||||
|
return len(b), nil
|
||||||
|
}
|
||||||
|
func (npc dropPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||||
|
time.Sleep(C.DefaultDropTime)
|
||||||
|
return 0, nil, io.EOF
|
||||||
|
}
|
||||||
|
func (npc dropPacketConn) WaitReadFrom() ([]byte, func(), net.Addr, error) {
|
||||||
|
return nil, nil, nil, io.EOF
|
||||||
|
}
|
||||||
|
func (npc dropPacketConn) Close() error { return nil }
|
||||||
|
func (npc dropPacketConn) LocalAddr() net.Addr { return udpAddrIPv4Unspecified }
|
||||||
|
func (npc dropPacketConn) SetDeadline(time.Time) error { return nil }
|
||||||
|
func (npc dropPacketConn) SetReadDeadline(time.Time) error { return nil }
|
||||||
|
func (npc dropPacketConn) SetWriteDeadline(time.Time) error { return nil }
|
||||||
|
|||||||
@@ -7,19 +7,19 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
N "github.com/Dreamacro/clash/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/Dreamacro/clash/common/structure"
|
"github.com/metacubex/mihomo/common/structure"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
"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"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"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"
|
restlsC "github.com/3andne/restls-client-go"
|
||||||
"github.com/metacubex/sing-shadowsocks2"
|
shadowsocks "github.com/metacubex/sing-shadowsocks2"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
"github.com/sagernet/sing/common/uot"
|
"github.com/sagernet/sing/common/uot"
|
||||||
)
|
)
|
||||||
@@ -58,14 +58,16 @@ type simpleObfsOption struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type v2rayObfsOption struct {
|
type v2rayObfsOption 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"`
|
||||||
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"`
|
||||||
Mux bool `obfs:"mux,omitempty"`
|
Mux bool `obfs:"mux,omitempty"`
|
||||||
|
V2rayHttpUpgrade bool `obfs:"v2ray-http-upgrade,omitempty"`
|
||||||
|
V2rayHttpUpgradeFastOpen bool `obfs:"v2ray-http-upgrade-fast-open,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type shadowTLSOption struct {
|
type shadowTLSOption struct {
|
||||||
@@ -123,9 +125,9 @@ func (ss *ShadowSocks) StreamConnContext(ctx context.Context, c net.Conn, metada
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if useEarly {
|
if useEarly {
|
||||||
return ss.method.DialEarlyConn(c, M.ParseSocksaddr(metadata.RemoteAddress())), nil
|
return ss.method.DialEarlyConn(c, M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort)), nil
|
||||||
} else {
|
} else {
|
||||||
return ss.method.DialConn(c, M.ParseSocksaddr(metadata.RemoteAddress()))
|
return ss.method.DialConn(c, M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,7 +148,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)
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
N.TCPKeepAlive(c)
|
||||||
|
|
||||||
defer func(c net.Conn) {
|
defer func(c net.Conn) {
|
||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
@@ -259,10 +261,12 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
|||||||
}
|
}
|
||||||
obfsMode = opts.Mode
|
obfsMode = opts.Mode
|
||||||
v2rayOption = &v2rayObfs.Option{
|
v2rayOption = &v2rayObfs.Option{
|
||||||
Host: opts.Host,
|
Host: opts.Host,
|
||||||
Path: opts.Path,
|
Path: opts.Path,
|
||||||
Headers: opts.Headers,
|
Headers: opts.Headers,
|
||||||
Mux: opts.Mux,
|
Mux: opts.Mux,
|
||||||
|
V2rayHttpUpgrade: opts.V2rayHttpUpgrade,
|
||||||
|
V2rayHttpUpgradeFastOpen: opts.V2rayHttpUpgradeFastOpen,
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.TLS {
|
if opts.TLS {
|
||||||
@@ -294,7 +298,6 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
restlsConfig, err = restlsC.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)
|
||||||
restlsConfig.SessionTicketsDisabled = true
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,16 +7,16 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
N "github.com/Dreamacro/clash/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"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/transport/shadowsocks/core"
|
"github.com/metacubex/mihomo/transport/shadowsocks/core"
|
||||||
"github.com/Dreamacro/clash/transport/shadowsocks/shadowaead"
|
"github.com/metacubex/mihomo/transport/shadowsocks/shadowaead"
|
||||||
"github.com/Dreamacro/clash/transport/shadowsocks/shadowstream"
|
"github.com/metacubex/mihomo/transport/shadowsocks/shadowstream"
|
||||||
"github.com/Dreamacro/clash/transport/socks5"
|
"github.com/metacubex/mihomo/transport/socks5"
|
||||||
"github.com/Dreamacro/clash/transport/ssr/obfs"
|
"github.com/metacubex/mihomo/transport/ssr/obfs"
|
||||||
"github.com/Dreamacro/clash/transport/ssr/protocol"
|
"github.com/metacubex/mihomo/transport/ssr/protocol"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ShadowSocksR struct {
|
type ShadowSocksR struct {
|
||||||
@@ -80,7 +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)
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
N.TCPKeepAlive(c)
|
||||||
|
|
||||||
defer func(c net.Conn) {
|
defer func(c net.Conn) {
|
||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
@@ -125,7 +125,7 @@ func (ssr *ShadowSocksR) SupportWithDialer() C.NetWork {
|
|||||||
|
|
||||||
func NewShadowSocksR(option ShadowSocksROption) (*ShadowSocksR, error) {
|
func NewShadowSocksR(option ShadowSocksROption) (*ShadowSocksR, error) {
|
||||||
// SSR protocol compatibility
|
// SSR protocol compatibility
|
||||||
// https://github.com/Dreamacro/clash/pull/2056
|
// https://github.com/metacubex/mihomo/pull/2056
|
||||||
if option.Cipher == "none" {
|
if option.Cipher == "none" {
|
||||||
option.Cipher = "dummy"
|
option.Cipher = "dummy"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,66 +3,54 @@ package outbound
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"net"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
CN "github.com/Dreamacro/clash/common/net"
|
CN "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
"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"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
|
"github.com/metacubex/mihomo/log"
|
||||||
|
|
||||||
mux "github.com/sagernet/sing-mux"
|
mux "github.com/sagernet/sing-mux"
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
N "github.com/sagernet/sing/common/network"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type SingMux struct {
|
type SingMux struct {
|
||||||
C.ProxyAdapter
|
C.ProxyAdapter
|
||||||
base ProxyBase
|
base ProxyBase
|
||||||
client *mux.Client
|
client *mux.Client
|
||||||
dialer *muxSingDialer
|
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 {
|
||||||
|
Enabled bool `proxy:"enabled,omitempty"`
|
||||||
|
Up string `proxy:"up,omitempty"`
|
||||||
|
Down string `proxy:"down,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ProxyBase interface {
|
type ProxyBase interface {
|
||||||
DialOptions(opts ...dialer.Option) []dialer.Option
|
DialOptions(opts ...dialer.Option) []dialer.Option
|
||||||
}
|
}
|
||||||
|
|
||||||
type muxSingDialer struct {
|
|
||||||
dialer dialer.Dialer
|
|
||||||
proxy C.ProxyAdapter
|
|
||||||
statistic bool
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ N.Dialer = (*muxSingDialer)(nil)
|
|
||||||
|
|
||||||
func (d *muxSingDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
|
||||||
var cDialer C.Dialer = proxydialer.New(d.proxy, d.dialer, d.statistic)
|
|
||||||
return cDialer.DialContext(ctx, network, destination.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *muxSingDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
|
||||||
var cDialer C.Dialer = proxydialer.New(d.proxy, d.dialer, d.statistic)
|
|
||||||
return cDialer.ListenPacket(ctx, "udp", "", destination.AddrPort())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SingMux) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
func (s *SingMux) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
||||||
options := s.base.DialOptions(opts...)
|
options := s.base.DialOptions(opts...)
|
||||||
s.dialer.dialer = dialer.NewDialer(options...)
|
s.dialer.SetDialer(dialer.NewDialer(options...))
|
||||||
c, err := s.client.DialContext(ctx, "tcp", M.ParseSocksaddr(metadata.RemoteAddress()))
|
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
|
||||||
}
|
}
|
||||||
@@ -74,7 +62,7 @@ func (s *SingMux) ListenPacketContext(ctx context.Context, metadata *C.Metadata,
|
|||||||
return s.ProxyAdapter.ListenPacketContext(ctx, metadata, opts...)
|
return s.ProxyAdapter.ListenPacketContext(ctx, metadata, opts...)
|
||||||
}
|
}
|
||||||
options := s.base.DialOptions(opts...)
|
options := s.base.DialOptions(opts...)
|
||||||
s.dialer.dialer = dialer.NewDialer(options...)
|
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() {
|
||||||
@@ -114,14 +102,23 @@ func closeSingMux(s *SingMux) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewSingMux(option SingMuxOption, proxy C.ProxyAdapter, base ProxyBase) (C.ProxyAdapter, error) {
|
func NewSingMux(option SingMuxOption, proxy C.ProxyAdapter, base ProxyBase) (C.ProxyAdapter, error) {
|
||||||
singDialer := &muxSingDialer{dialer: dialer.NewDialer(), proxy: proxy, statistic: option.Statistic}
|
// TODO
|
||||||
|
// "TCP Brutal is only supported on Linux-based systems"
|
||||||
|
|
||||||
|
singDialer := proxydialer.NewSingDialer(proxy, dialer.NewDialer(), 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
|
||||||
|
|||||||
@@ -6,12 +6,13 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/structure"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/metacubex/mihomo/common/structure"
|
||||||
"github.com/Dreamacro/clash/component/proxydialer"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
"github.com/metacubex/mihomo/component/proxydialer"
|
||||||
obfs "github.com/Dreamacro/clash/transport/simple-obfs"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/Dreamacro/clash/transport/snell"
|
obfs "github.com/metacubex/mihomo/transport/simple-obfs"
|
||||||
|
"github.com/metacubex/mihomo/transport/snell"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Snell struct {
|
type Snell struct {
|
||||||
@@ -93,7 +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)
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
N.TCPKeepAlive(c)
|
||||||
|
|
||||||
defer func(c net.Conn) {
|
defer func(c net.Conn) {
|
||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
@@ -121,7 +122,7 @@ func (s *Snell) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, met
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
N.TCPKeepAlive(c)
|
||||||
c = streamConn(c, streamOption{s.psk, s.version, s.addr, s.obfsOption})
|
c = streamConn(c, streamOption{s.psk, s.version, s.addr, s.obfsOption})
|
||||||
|
|
||||||
err = snell.WriteUDPHeader(c, s.version)
|
err = snell.WriteUDPHeader(c, s.version)
|
||||||
@@ -207,7 +208,7 @@ func NewSnell(option SnellOption) (*Snell, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
tcpKeepAlive(c)
|
N.TCPKeepAlive(c)
|
||||||
return streamConn(c, streamOption{psk, option.Version, addr, obfsOption}), nil
|
return streamConn(c, streamOption{psk, option.Version, addr, obfsOption}), nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,13 +7,15 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
"net/netip"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/Dreamacro/clash/component/proxydialer"
|
"github.com/metacubex/mihomo/component/ca"
|
||||||
tlsC "github.com/Dreamacro/clash/component/tls"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
"github.com/metacubex/mihomo/component/proxydialer"
|
||||||
"github.com/Dreamacro/clash/transport/socks5"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
|
"github.com/metacubex/mihomo/transport/socks5"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Socks5 struct {
|
type Socks5 struct {
|
||||||
@@ -80,7 +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)
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
N.TCPKeepAlive(c)
|
||||||
|
|
||||||
defer func(c net.Conn) {
|
defer func(c net.Conn) {
|
||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
@@ -126,7 +128,7 @@ func (ss *Socks5) ListenPacketContext(ctx context.Context, metadata *C.Metadata,
|
|||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
}(c)
|
}(c)
|
||||||
|
|
||||||
tcpKeepAlive(c)
|
N.TCPKeepAlive(c)
|
||||||
var user *socks5.User
|
var user *socks5.User
|
||||||
if ss.user != "" {
|
if ss.user != "" {
|
||||||
user = &socks5.User{
|
user = &socks5.User{
|
||||||
@@ -135,7 +137,8 @@ func (ss *Socks5) ListenPacketContext(ctx context.Context, metadata *C.Metadata,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bindAddr, err := socks5.ClientHandshake(c, serializesSocksAddr(metadata), socks5.CmdUDPAssociate, user)
|
udpAssocateAddr := socks5.AddrFromStdAddrPort(netip.AddrPortFrom(netip.IPv4Unspecified(), 0))
|
||||||
|
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
|
||||||
@@ -155,7 +158,7 @@ func (ss *Socks5) ListenPacketContext(ctx context.Context, metadata *C.Metadata,
|
|||||||
bindUDPAddr.IP = serverAddr.IP
|
bindUDPAddr.IP = serverAddr.IP
|
||||||
}
|
}
|
||||||
|
|
||||||
pc, err := dialer.ListenPacket(ctx, dialer.ParseNetwork("udp", bindUDPAddr.AddrPort().Addr()), "", ss.Base.DialOptions(opts...)...)
|
pc, err := cDialer.ListenPacket(ctx, "udp", "", bindUDPAddr.AddrPort())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -179,13 +182,10 @@ func NewSocks5(option Socks5Option) (*Socks5, error) {
|
|||||||
ServerName: option.Server,
|
ServerName: option.Server,
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(option.Fingerprint) == 0 {
|
var err error
|
||||||
tlsConfig = tlsC.GetGlobalTLSConfig(tlsConfig)
|
tlsConfig, err = ca.GetSpecifiedFingerprintTLSConfig(tlsConfig, option.Fingerprint)
|
||||||
} else {
|
if err != nil {
|
||||||
var err error
|
return nil, err
|
||||||
if tlsConfig, err = tlsC.GetSpecifiedFingerprintTLSConfig(tlsConfig, option.Fingerprint); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,12 +8,14 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/Dreamacro/clash/component/proxydialer"
|
"github.com/metacubex/mihomo/component/ca"
|
||||||
tlsC "github.com/Dreamacro/clash/component/tls"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
"github.com/metacubex/mihomo/component/proxydialer"
|
||||||
"github.com/Dreamacro/clash/transport/gun"
|
tlsC "github.com/metacubex/mihomo/component/tls"
|
||||||
"github.com/Dreamacro/clash/transport/trojan"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
|
"github.com/metacubex/mihomo/transport/gun"
|
||||||
|
"github.com/metacubex/mihomo/transport/trojan"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Trojan struct {
|
type Trojan struct {
|
||||||
@@ -51,9 +53,12 @@ func (t *Trojan) plainStream(ctx context.Context, c net.Conn) (net.Conn, error)
|
|||||||
if t.option.Network == "ws" {
|
if t.option.Network == "ws" {
|
||||||
host, port, _ := net.SplitHostPort(t.addr)
|
host, port, _ := net.SplitHostPort(t.addr)
|
||||||
wsOpts := &trojan.WebsocketOption{
|
wsOpts := &trojan.WebsocketOption{
|
||||||
Host: host,
|
Host: host,
|
||||||
Port: port,
|
Port: port,
|
||||||
Path: t.option.WSOpts.Path,
|
Path: t.option.WSOpts.Path,
|
||||||
|
V2rayHttpUpgrade: t.option.WSOpts.V2rayHttpUpgrade,
|
||||||
|
V2rayHttpUpgradeFastOpen: t.option.WSOpts.V2rayHttpUpgradeFastOpen,
|
||||||
|
Headers: http.Header{},
|
||||||
}
|
}
|
||||||
|
|
||||||
if t.option.SNI != "" {
|
if t.option.SNI != "" {
|
||||||
@@ -61,11 +66,9 @@ func (t *Trojan) plainStream(ctx context.Context, c net.Conn) (net.Conn, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
header.Add(key, value)
|
wsOpts.Headers.Add(key, value)
|
||||||
}
|
}
|
||||||
wsOpts.Headers = header
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return t.instance.StreamWebsocketConn(ctx, c, wsOpts)
|
return t.instance.StreamWebsocketConn(ctx, c, wsOpts)
|
||||||
@@ -131,7 +134,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)
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
N.TCPKeepAlive(c)
|
||||||
|
|
||||||
defer func(c net.Conn) {
|
defer func(c net.Conn) {
|
||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
@@ -184,7 +187,7 @@ 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)
|
||||||
tcpKeepAlive(c)
|
N.TCPKeepAlive(c)
|
||||||
c, err = t.plainStream(ctx, c)
|
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)
|
||||||
@@ -268,7 +271,7 @@ func NewTrojan(option TrojanOption) (*Trojan, error) {
|
|||||||
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())
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
N.TCPKeepAlive(c)
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -279,13 +282,10 @@ func NewTrojan(option TrojanOption) (*Trojan, error) {
|
|||||||
ServerName: tOption.ServerName,
|
ServerName: tOption.ServerName,
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(option.Fingerprint) == 0 {
|
var err error
|
||||||
tlsConfig = tlsC.GetGlobalTLSConfig(tlsConfig)
|
tlsConfig, err = ca.GetSpecifiedFingerprintTLSConfig(tlsConfig, option.Fingerprint)
|
||||||
} else {
|
if err != nil {
|
||||||
var err error
|
return nil, err
|
||||||
if tlsConfig, err = tlsC.GetSpecifiedFingerprintTLSConfig(tlsConfig, option.Fingerprint); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
t.transport = gun.NewHTTP2Client(dialFn, tlsConfig, tOption.ClientFingerprint, t.realityConfig)
|
t.transport = gun.NewHTTP2Client(dialFn, tlsConfig, tOption.ClientFingerprint, t.realityConfig)
|
||||||
|
|||||||
@@ -2,25 +2,25 @@ package outbound
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/sha256"
|
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/hex"
|
"errors"
|
||||||
"encoding/pem"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/metacubex/mihomo/component/ca"
|
||||||
"github.com/Dreamacro/clash/component/proxydialer"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
tlsC "github.com/Dreamacro/clash/component/tls"
|
"github.com/metacubex/mihomo/component/proxydialer"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
"github.com/metacubex/mihomo/component/resolver"
|
||||||
"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/sagernet/sing/common/metadata"
|
||||||
|
"github.com/sagernet/sing/common/uot"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Tuic struct {
|
type Tuic struct {
|
||||||
@@ -59,6 +59,9 @@ type TuicOption struct {
|
|||||||
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"`
|
||||||
|
|
||||||
|
UDPOverStream bool `proxy:"udp-over-stream,omitempty"`
|
||||||
|
UDPOverStreamVersion int `proxy:"udp-over-stream-version,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialContext implements C.ProxyAdapter
|
// DialContext implements C.ProxyAdapter
|
||||||
@@ -82,6 +85,32 @@ func (t *Tuic) ListenPacketContext(ctx context.Context, metadata *C.Metadata, op
|
|||||||
|
|
||||||
// ListenPacketWithDialer implements C.ProxyAdapter
|
// ListenPacketWithDialer implements C.ProxyAdapter
|
||||||
func (t *Tuic) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
func (t *Tuic) ListenPacketWithDialer(ctx context.Context, dialer C.Dialer, metadata *C.Metadata) (_ C.PacketConn, err error) {
|
||||||
|
if t.option.UDPOverStream {
|
||||||
|
uotDestination := uot.RequestDestination(uint8(t.option.UDPOverStreamVersion))
|
||||||
|
uotMetadata := *metadata
|
||||||
|
uotMetadata.Host = uotDestination.Fqdn
|
||||||
|
uotMetadata.DstPort = uotDestination.Port
|
||||||
|
c, err := t.DialContextWithDialer(ctx, dialer, &uotMetadata)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// tuic uos use stream-oriented udp with a special address, so we need a net.UDPAddr
|
||||||
|
if !metadata.Resolved() {
|
||||||
|
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("can't resolve ip")
|
||||||
|
}
|
||||||
|
metadata.DstIP = ip
|
||||||
|
}
|
||||||
|
|
||||||
|
destination := M.SocksaddrFromNet(metadata.UDPAddr())
|
||||||
|
if t.option.UDPOverStreamVersion == uot.LegacyVersion {
|
||||||
|
return newPacketConn(uot.NewConn(c, uot.Request{Destination: destination}), t), nil
|
||||||
|
} else {
|
||||||
|
return newPacketConn(uot.NewLazyConn(c, uot.Request{Destination: destination}), t), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
pc, err := t.client.ListenPacketWithDialer(ctx, metadata, dialer, t.dialWithDialer)
|
pc, err := t.client.ListenPacketWithDialer(ctx, metadata, dialer, t.dialWithDialer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -129,37 +158,10 @@ func NewTuic(option TuicOption) (*Tuic, error) {
|
|||||||
tlsConfig.ServerName = option.SNI
|
tlsConfig.ServerName = option.SNI
|
||||||
}
|
}
|
||||||
|
|
||||||
var bs []byte
|
|
||||||
var err error
|
var err error
|
||||||
if len(option.CustomCA) > 0 {
|
tlsConfig, err = ca.GetTLSConfig(tlsConfig, option.Fingerprint, option.CustomCA, option.CustomCAString)
|
||||||
bs, err = os.ReadFile(option.CustomCA)
|
if err != nil {
|
||||||
if err != nil {
|
return nil, err
|
||||||
return nil, fmt.Errorf("tuic %s load ca error: %w", addr, err)
|
|
||||||
}
|
|
||||||
} else if option.CustomCAString != "" {
|
|
||||||
bs = []byte(option.CustomCAString)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(bs) > 0 {
|
|
||||||
block, _ := pem.Decode(bs)
|
|
||||||
if block == nil {
|
|
||||||
return nil, fmt.Errorf("CA cert is not PEM")
|
|
||||||
}
|
|
||||||
|
|
||||||
fpBytes := sha256.Sum256(block.Bytes)
|
|
||||||
if len(option.Fingerprint) == 0 {
|
|
||||||
option.Fingerprint = hex.EncodeToString(fpBytes[:])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(option.Fingerprint) != 0 {
|
|
||||||
var err error
|
|
||||||
tlsConfig, err = tlsC.GetSpecifiedFingerprintTLSConfig(tlsConfig, option.Fingerprint)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
tlsConfig = tlsC.GetGlobalTLSConfig(tlsConfig)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if option.ALPN != nil { // structure's Decode will ensure value not nil when input has value even it was set an empty array
|
if option.ALPN != nil { // structure's Decode will ensure value not nil when input has value even it was set an empty array
|
||||||
@@ -239,6 +241,14 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch option.UDPOverStreamVersion {
|
||||||
|
case uot.Version, uot.LegacyVersion:
|
||||||
|
case 0:
|
||||||
|
option.UDPOverStreamVersion = uot.LegacyVersion
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("tuic %s unknown udp over stream protocol version: %d", addr, option.UDPOverStreamVersion)
|
||||||
|
}
|
||||||
|
|
||||||
t := &Tuic{
|
t := &Tuic{
|
||||||
Base: &Base{
|
Base: &Base{
|
||||||
name: option.Name,
|
name: option.Name,
|
||||||
@@ -282,6 +292,10 @@ func NewTuic(option TuicOption) (*Tuic, error) {
|
|||||||
|
|
||||||
t.client = tuic.NewPoolClientV4(clientOption)
|
t.client = tuic.NewPoolClientV4(clientOption)
|
||||||
} else {
|
} else {
|
||||||
|
maxUdpRelayPacketSize := option.MaxUdpRelayPacketSize
|
||||||
|
if maxUdpRelayPacketSize > tuic.MaxFragSizeV5 {
|
||||||
|
maxUdpRelayPacketSize = tuic.MaxFragSizeV5
|
||||||
|
}
|
||||||
clientOption := &tuic.ClientOptionV5{
|
clientOption := &tuic.ClientOptionV5{
|
||||||
TlsConfig: tlsConfig,
|
TlsConfig: tlsConfig,
|
||||||
QuicConfig: quicConfig,
|
QuicConfig: quicConfig,
|
||||||
@@ -290,7 +304,7 @@ func NewTuic(option TuicOption) (*Tuic, error) {
|
|||||||
UdpRelayMode: udpRelayMode,
|
UdpRelayMode: udpRelayMode,
|
||||||
CongestionController: option.CongestionController,
|
CongestionController: option.CongestionController,
|
||||||
ReduceRtt: option.ReduceRtt,
|
ReduceRtt: option.ReduceRtt,
|
||||||
MaxUdpRelayPacketSize: option.MaxUdpRelayPacketSize,
|
MaxUdpRelayPacketSize: maxUdpRelayPacketSize,
|
||||||
MaxOpenStreams: clientMaxOpenStreams,
|
MaxOpenStreams: clientMaxOpenStreams,
|
||||||
CWND: option.CWND,
|
CWND: option.CWND,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,14 +4,16 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/component/resolver"
|
"github.com/metacubex/mihomo/component/resolver"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/Dreamacro/clash/transport/socks5"
|
"github.com/metacubex/mihomo/transport/socks5"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -19,13 +21,6 @@ var (
|
|||||||
once sync.Once
|
once sync.Once
|
||||||
)
|
)
|
||||||
|
|
||||||
func tcpKeepAlive(c net.Conn) {
|
|
||||||
if tcp, ok := c.(*net.TCPConn); ok {
|
|
||||||
_ = tcp.SetKeepAlive(true)
|
|
||||||
_ = tcp.SetKeepAlivePeriod(30 * time.Second)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func getClientSessionCache() tls.ClientSessionCache {
|
func getClientSessionCache() tls.ClientSessionCache {
|
||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
globalClientSessionCache = tls.NewLRUClientSessionCache(128)
|
globalClientSessionCache = tls.NewLRUClientSessionCache(128)
|
||||||
@@ -128,3 +123,41 @@ func safeConnClose(c net.Conn, err error) {
|
|||||||
_ = c.Close()
|
_ = c.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var rateStringRegexp = regexp.MustCompile(`^(\d+)\s*([KMGT]?)([Bb])ps$`)
|
||||||
|
|
||||||
|
func StringToBps(s string) uint64 {
|
||||||
|
if s == "" {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// when have not unit, use Mbps
|
||||||
|
if v, err := strconv.Atoi(s); err == nil {
|
||||||
|
return StringToBps(fmt.Sprintf("%d Mbps", v))
|
||||||
|
}
|
||||||
|
|
||||||
|
m := rateStringRegexp.FindStringSubmatch(s)
|
||||||
|
if m == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
var n uint64
|
||||||
|
switch m[2] {
|
||||||
|
case "K":
|
||||||
|
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)
|
||||||
|
n = v * n
|
||||||
|
if m[3] == "b" {
|
||||||
|
// Bits, need to convert to bytes
|
||||||
|
n = n >> 3
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|||||||
@@ -12,19 +12,20 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/convert"
|
"github.com/metacubex/mihomo/common/convert"
|
||||||
N "github.com/Dreamacro/clash/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/Dreamacro/clash/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/metacubex/mihomo/component/ca"
|
||||||
"github.com/Dreamacro/clash/component/proxydialer"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
"github.com/Dreamacro/clash/component/resolver"
|
"github.com/metacubex/mihomo/component/proxydialer"
|
||||||
tlsC "github.com/Dreamacro/clash/component/tls"
|
"github.com/metacubex/mihomo/component/resolver"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
tlsC "github.com/metacubex/mihomo/component/tls"
|
||||||
"github.com/Dreamacro/clash/log"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/Dreamacro/clash/transport/gun"
|
"github.com/metacubex/mihomo/log"
|
||||||
"github.com/Dreamacro/clash/transport/socks5"
|
"github.com/metacubex/mihomo/transport/gun"
|
||||||
"github.com/Dreamacro/clash/transport/vless"
|
"github.com/metacubex/mihomo/transport/socks5"
|
||||||
"github.com/Dreamacro/clash/transport/vmess"
|
"github.com/metacubex/mihomo/transport/vless"
|
||||||
|
"github.com/metacubex/mihomo/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"
|
||||||
@@ -57,6 +58,7 @@ type VlessOption struct {
|
|||||||
UUID string `proxy:"uuid"`
|
UUID string `proxy:"uuid"`
|
||||||
Flow string `proxy:"flow,omitempty"`
|
Flow string `proxy:"flow,omitempty"`
|
||||||
TLS bool `proxy:"tls,omitempty"`
|
TLS bool `proxy:"tls,omitempty"`
|
||||||
|
ALPN []string `proxy:"alpn,omitempty"`
|
||||||
UDP bool `proxy:"udp,omitempty"`
|
UDP bool `proxy:"udp,omitempty"`
|
||||||
PacketAddr bool `proxy:"packet-addr,omitempty"`
|
PacketAddr bool `proxy:"packet-addr,omitempty"`
|
||||||
XUDP bool `proxy:"xudp,omitempty"`
|
XUDP bool `proxy:"xudp,omitempty"`
|
||||||
@@ -86,13 +88,15 @@ func (v *Vless) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
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,
|
||||||
ClientFingerprint: v.option.ClientFingerprint,
|
V2rayHttpUpgrade: v.option.WSOpts.V2rayHttpUpgrade,
|
||||||
Headers: http.Header{},
|
V2rayHttpUpgradeFastOpen: v.option.WSOpts.V2rayHttpUpgradeFastOpen,
|
||||||
|
ClientFingerprint: v.option.ClientFingerprint,
|
||||||
|
Headers: http.Header{},
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(v.option.WSOpts.Headers) != 0 {
|
if len(v.option.WSOpts.Headers) != 0 {
|
||||||
@@ -109,13 +113,9 @@ func (v *Vless) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
NextProtos: []string{"http/1.1"},
|
NextProtos: []string{"http/1.1"},
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(v.option.Fingerprint) == 0 {
|
wsOpts.TLSConfig, err = ca.GetSpecifiedFingerprintTLSConfig(tlsConfig, v.option.Fingerprint)
|
||||||
wsOpts.TLSConfig = tlsC.GetGlobalTLSConfig(tlsConfig)
|
if err != nil {
|
||||||
} else {
|
return nil, err
|
||||||
wsOpts.TLSConfig, err = tlsC.GetSpecifiedFingerprintTLSConfig(tlsConfig, v.option.Fingerprint)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.option.ServerName != "" {
|
if v.option.ServerName != "" {
|
||||||
@@ -211,6 +211,7 @@ func (v *Vless) streamTLSConn(ctx context.Context, conn net.Conn, isH2 bool) (ne
|
|||||||
FingerPrint: v.option.Fingerprint,
|
FingerPrint: v.option.Fingerprint,
|
||||||
ClientFingerprint: v.option.ClientFingerprint,
|
ClientFingerprint: v.option.ClientFingerprint,
|
||||||
Reality: v.realityConfig,
|
Reality: v.realityConfig,
|
||||||
|
NextProtos: v.option.ALPN,
|
||||||
}
|
}
|
||||||
|
|
||||||
if isH2 {
|
if isH2 {
|
||||||
@@ -261,7 +262,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())
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
N.TCPKeepAlive(c)
|
||||||
defer func(c net.Conn) {
|
defer func(c net.Conn) {
|
||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
}(c)
|
}(c)
|
||||||
@@ -326,7 +327,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())
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
N.TCPKeepAlive(c)
|
||||||
defer func(c net.Conn) {
|
defer func(c net.Conn) {
|
||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
}(c)
|
}(c)
|
||||||
@@ -576,7 +577,7 @@ func NewVless(option VlessOption) (*Vless, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
N.TCPKeepAlive(c)
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -590,7 +591,7 @@ func NewVless(option VlessOption) (*Vless, error) {
|
|||||||
}
|
}
|
||||||
var tlsConfig *tls.Config
|
var tlsConfig *tls.Config
|
||||||
if option.TLS {
|
if option.TLS {
|
||||||
tlsConfig = tlsC.GetGlobalTLSConfig(&tls.Config{
|
tlsConfig = ca.GetGlobalTLSConfig(&tls.Config{
|
||||||
InsecureSkipVerify: v.option.SkipCertVerify,
|
InsecureSkipVerify: v.option.SkipCertVerify,
|
||||||
ServerName: v.option.ServerName,
|
ServerName: v.option.ServerName,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -11,15 +11,17 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
N "github.com/Dreamacro/clash/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/Dreamacro/clash/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/metacubex/mihomo/component/ca"
|
||||||
"github.com/Dreamacro/clash/component/proxydialer"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
"github.com/Dreamacro/clash/component/resolver"
|
"github.com/metacubex/mihomo/component/proxydialer"
|
||||||
tlsC "github.com/Dreamacro/clash/component/tls"
|
"github.com/metacubex/mihomo/component/resolver"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
tlsC "github.com/metacubex/mihomo/component/tls"
|
||||||
"github.com/Dreamacro/clash/transport/gun"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
clashVMess "github.com/Dreamacro/clash/transport/vmess"
|
"github.com/metacubex/mihomo/ntp"
|
||||||
|
"github.com/metacubex/mihomo/transport/gun"
|
||||||
|
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"
|
||||||
@@ -52,6 +54,7 @@ type VmessOption struct {
|
|||||||
UDP bool `proxy:"udp,omitempty"`
|
UDP bool `proxy:"udp,omitempty"`
|
||||||
Network string `proxy:"network,omitempty"`
|
Network string `proxy:"network,omitempty"`
|
||||||
TLS bool `proxy:"tls,omitempty"`
|
TLS bool `proxy:"tls,omitempty"`
|
||||||
|
ALPN []string `proxy:"alpn,omitempty"`
|
||||||
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
|
||||||
Fingerprint string `proxy:"fingerprint,omitempty"`
|
Fingerprint string `proxy:"fingerprint,omitempty"`
|
||||||
ServerName string `proxy:"servername,omitempty"`
|
ServerName string `proxy:"servername,omitempty"`
|
||||||
@@ -84,10 +87,12 @@ 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
|
||||||
@@ -101,14 +106,16 @@ func (v *Vmess) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
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 := &clashVMess.WebsocketConfig{
|
wsOpts := &mihomoVMess.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,
|
||||||
ClientFingerprint: v.option.ClientFingerprint,
|
V2rayHttpUpgrade: v.option.WSOpts.V2rayHttpUpgrade,
|
||||||
Headers: http.Header{},
|
V2rayHttpUpgradeFastOpen: v.option.WSOpts.V2rayHttpUpgradeFastOpen,
|
||||||
|
ClientFingerprint: v.option.ClientFingerprint,
|
||||||
|
Headers: http.Header{},
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(v.option.WSOpts.Headers) != 0 {
|
if len(v.option.WSOpts.Headers) != 0 {
|
||||||
@@ -125,12 +132,9 @@ func (v *Vmess) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
NextProtos: []string{"http/1.1"},
|
NextProtos: []string{"http/1.1"},
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(v.option.Fingerprint) == 0 {
|
wsOpts.TLSConfig, err = ca.GetSpecifiedFingerprintTLSConfig(tlsConfig, v.option.Fingerprint)
|
||||||
wsOpts.TLSConfig = tlsC.GetGlobalTLSConfig(tlsConfig)
|
if err != nil {
|
||||||
} else {
|
return nil, err
|
||||||
if wsOpts.TLSConfig, err = tlsC.GetSpecifiedFingerprintTLSConfig(tlsConfig, v.option.Fingerprint); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.option.ServerName != "" {
|
if v.option.ServerName != "" {
|
||||||
@@ -139,39 +143,40 @@ func (v *Vmess) StreamConnContext(ctx context.Context, c net.Conn, metadata *C.M
|
|||||||
wsOpts.TLSConfig.ServerName = host
|
wsOpts.TLSConfig.ServerName = host
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c, err = clashVMess.StreamWebsocketConn(ctx, c, wsOpts)
|
c, err = mihomoVMess.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 := &clashVMess.TLSConfig{
|
tlsOpts := &mihomoVMess.TLSConfig{
|
||||||
Host: host,
|
Host: host,
|
||||||
SkipCertVerify: v.option.SkipCertVerify,
|
SkipCertVerify: v.option.SkipCertVerify,
|
||||||
ClientFingerprint: v.option.ClientFingerprint,
|
ClientFingerprint: v.option.ClientFingerprint,
|
||||||
Reality: v.realityConfig,
|
Reality: v.realityConfig,
|
||||||
|
NextProtos: v.option.ALPN,
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.option.ServerName != "" {
|
if v.option.ServerName != "" {
|
||||||
tlsOpts.Host = v.option.ServerName
|
tlsOpts.Host = v.option.ServerName
|
||||||
}
|
}
|
||||||
c, err = clashVMess.StreamTLSConn(ctx, c, tlsOpts)
|
c, err = mihomoVMess.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 := &clashVMess.HTTPConfig{
|
httpOpts := &mihomoVMess.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 = clashVMess.StreamHTTPConn(c, httpOpts)
|
c = mihomoVMess.StreamHTTPConn(c, httpOpts)
|
||||||
case "h2":
|
case "h2":
|
||||||
host, _, _ := net.SplitHostPort(v.addr)
|
host, _, _ := net.SplitHostPort(v.addr)
|
||||||
tlsOpts := clashVMess.TLSConfig{
|
tlsOpts := mihomoVMess.TLSConfig{
|
||||||
Host: host,
|
Host: host,
|
||||||
SkipCertVerify: v.option.SkipCertVerify,
|
SkipCertVerify: v.option.SkipCertVerify,
|
||||||
NextProtos: []string{"h2"},
|
NextProtos: []string{"h2"},
|
||||||
@@ -183,35 +188,36 @@ 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 = clashVMess.StreamTLSConn(ctx, c, &tlsOpts)
|
c, err = mihomoVMess.StreamTLSConn(ctx, c, &tlsOpts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
h2Opts := &clashVMess.H2Config{
|
h2Opts := &mihomoVMess.H2Config{
|
||||||
Hosts: v.option.HTTP2Opts.Host,
|
Hosts: v.option.HTTP2Opts.Host,
|
||||||
Path: v.option.HTTP2Opts.Path,
|
Path: v.option.HTTP2Opts.Path,
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err = clashVMess.StreamH2Conn(c, h2Opts)
|
c, err = mihomoVMess.StreamH2Conn(c, h2Opts)
|
||||||
case "grpc":
|
case "grpc":
|
||||||
c, err = gun.StreamGunWithConn(c, v.gunTLSConfig, v.gunConfig, 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 := &clashVMess.TLSConfig{
|
tlsOpts := &mihomoVMess.TLSConfig{
|
||||||
Host: host,
|
Host: host,
|
||||||
SkipCertVerify: v.option.SkipCertVerify,
|
SkipCertVerify: v.option.SkipCertVerify,
|
||||||
ClientFingerprint: v.option.ClientFingerprint,
|
ClientFingerprint: v.option.ClientFingerprint,
|
||||||
Reality: v.realityConfig,
|
Reality: v.realityConfig,
|
||||||
|
NextProtos: v.option.ALPN,
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.option.ServerName != "" {
|
if v.option.ServerName != "" {
|
||||||
tlsOpts.Host = v.option.ServerName
|
tlsOpts.Host = v.option.ServerName
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err = clashVMess.StreamTLSConn(ctx, c, tlsOpts)
|
c, err = mihomoVMess.StreamTLSConn(ctx, c, tlsOpts)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -258,10 +264,10 @@ func (v *Vmess) streamConn(c net.Conn, metadata *C.Metadata) (conn net.Conn, err
|
|||||||
} else {
|
} else {
|
||||||
if N.NeedHandshake(c) {
|
if N.NeedHandshake(c) {
|
||||||
conn = v.client.DialEarlyConn(c,
|
conn = v.client.DialEarlyConn(c,
|
||||||
M.ParseSocksaddr(metadata.RemoteAddress()))
|
M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort))
|
||||||
} else {
|
} else {
|
||||||
conn, err = v.client.DialConn(c,
|
conn, err = v.client.DialConn(c,
|
||||||
M.ParseSocksaddr(metadata.RemoteAddress()))
|
M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -282,7 +288,7 @@ func (v *Vmess) DialContext(ctx context.Context, metadata *C.Metadata, opts ...d
|
|||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
}(c)
|
}(c)
|
||||||
|
|
||||||
c, err = v.client.DialConn(c, M.ParseSocksaddr(metadata.RemoteAddress()))
|
c, err = v.client.DialConn(c, M.ParseSocksaddrHostPort(metadata.String(), metadata.DstPort))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -304,7 +310,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())
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
N.TCPKeepAlive(c)
|
||||||
defer func(c net.Conn) {
|
defer func(c net.Conn) {
|
||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
}(c)
|
}(c)
|
||||||
@@ -365,7 +371,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())
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
N.TCPKeepAlive(c)
|
||||||
defer func(c net.Conn) {
|
defer func(c net.Conn) {
|
||||||
safeConnClose(c, err)
|
safeConnClose(c, err)
|
||||||
}(c)
|
}(c)
|
||||||
@@ -413,6 +419,7 @@ func NewVmess(option VmessOption) (*Vmess, error) {
|
|||||||
if option.AuthenticatedLength {
|
if option.AuthenticatedLength {
|
||||||
options = append(options, vmess.ClientWithAuthenticatedLength())
|
options = append(options, vmess.ClientWithAuthenticatedLength())
|
||||||
}
|
}
|
||||||
|
options = append(options, vmess.ClientWithTimeFunc(ntp.Now))
|
||||||
client, err := vmess.NewClient(option.UUID, security, option.AlterID, options...)
|
client, err := vmess.NewClient(option.UUID, security, option.AlterID, options...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -464,7 +471,7 @@ func NewVmess(option VmessOption) (*Vmess, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
return nil, fmt.Errorf("%s connect error: %s", v.addr, err.Error())
|
||||||
}
|
}
|
||||||
tcpKeepAlive(c)
|
N.TCPKeepAlive(c)
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -478,7 +485,7 @@ func NewVmess(option VmessOption) (*Vmess, error) {
|
|||||||
}
|
}
|
||||||
var tlsConfig *tls.Config
|
var tlsConfig *tls.Config
|
||||||
if option.TLS {
|
if option.TLS {
|
||||||
tlsConfig = tlsC.GetGlobalTLSConfig(&tls.Config{
|
tlsConfig = ca.GetGlobalTLSConfig(&tls.Config{
|
||||||
InsecureSkipVerify: v.option.SkipCertVerify,
|
InsecureSkipVerify: v.option.SkipCertVerify,
|
||||||
ServerName: v.option.ServerName,
|
ServerName: v.option.ServerName,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -13,13 +13,13 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
CN "github.com/Dreamacro/clash/common/net"
|
CN "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
"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"
|
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"
|
||||||
|
|
||||||
wireguard "github.com/metacubex/sing-wireguard"
|
wireguard "github.com/metacubex/sing-wireguard"
|
||||||
|
|
||||||
@@ -27,7 +27,6 @@ import (
|
|||||||
"github.com/sagernet/sing/common/debug"
|
"github.com/sagernet/sing/common/debug"
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
N "github.com/sagernet/sing/common/network"
|
|
||||||
"github.com/sagernet/wireguard-go/device"
|
"github.com/sagernet/wireguard-go/device"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -36,7 +35,7 @@ type WireGuard struct {
|
|||||||
bind *wireguard.ClientBind
|
bind *wireguard.ClientBind
|
||||||
device *device.Device
|
device *device.Device
|
||||||
tunDevice wireguard.Device
|
tunDevice wireguard.Device
|
||||||
dialer *wgSingDialer
|
dialer proxydialer.SingDialer
|
||||||
startOnce sync.Once
|
startOnce sync.Once
|
||||||
startErr error
|
startErr error
|
||||||
resolver *dns.Resolver
|
resolver *dns.Resolver
|
||||||
@@ -70,37 +69,6 @@ type WireGuardPeerOption struct {
|
|||||||
AllowedIPs []string `proxy:"allowed-ips,omitempty"`
|
AllowedIPs []string `proxy:"allowed-ips,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type wgSingDialer struct {
|
|
||||||
dialer dialer.Dialer
|
|
||||||
proxyName string
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ N.Dialer = (*wgSingDialer)(nil)
|
|
||||||
|
|
||||||
func (d *wgSingDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
|
||||||
var cDialer C.Dialer = d.dialer
|
|
||||||
if len(d.proxyName) > 0 {
|
|
||||||
pd, err := proxydialer.NewByName(d.proxyName, d.dialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
cDialer = pd
|
|
||||||
}
|
|
||||||
return cDialer.DialContext(ctx, network, destination.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *wgSingDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
|
||||||
var cDialer C.Dialer = d.dialer
|
|
||||||
if len(d.proxyName) > 0 {
|
|
||||||
pd, err := proxydialer.NewByName(d.proxyName, d.dialer)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
cDialer = pd
|
|
||||||
}
|
|
||||||
return cDialer.ListenPacket(ctx, "udp", "", destination.AddrPort())
|
|
||||||
}
|
|
||||||
|
|
||||||
type wgSingErrorHandler struct {
|
type wgSingErrorHandler struct {
|
||||||
name string
|
name string
|
||||||
}
|
}
|
||||||
@@ -168,7 +136,7 @@ func NewWireGuard(option WireGuardOption) (*WireGuard, error) {
|
|||||||
rmark: option.RoutingMark,
|
rmark: option.RoutingMark,
|
||||||
prefer: C.NewDNSPrefer(option.IPVersion),
|
prefer: C.NewDNSPrefer(option.IPVersion),
|
||||||
},
|
},
|
||||||
dialer: &wgSingDialer{dialer: dialer.NewDialer(), proxyName: option.DialerProxy},
|
dialer: proxydialer.NewByNameSingDialer(option.DialerProxy, dialer.NewDialer()),
|
||||||
}
|
}
|
||||||
runtime.SetFinalizer(outbound, closeWireGuard)
|
runtime.SetFinalizer(outbound, closeWireGuard)
|
||||||
|
|
||||||
@@ -302,7 +270,7 @@ func NewWireGuard(option WireGuardOption) (*WireGuard, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, E.Cause(err, "create WireGuard device")
|
return nil, E.Cause(err, "create WireGuard device")
|
||||||
}
|
}
|
||||||
outbound.device = device.NewDevice(outbound.tunDevice, outbound.bind, &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...)))
|
||||||
},
|
},
|
||||||
@@ -355,7 +323,7 @@ func closeWireGuard(w *WireGuard) {
|
|||||||
|
|
||||||
func (w *WireGuard) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
func (w *WireGuard) DialContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.Conn, err error) {
|
||||||
options := w.Base.DialOptions(opts...)
|
options := w.Base.DialOptions(opts...)
|
||||||
w.dialer.dialer = dialer.NewDialer(options...)
|
w.dialer.SetDialer(dialer.NewDialer(options...))
|
||||||
var conn net.Conn
|
var conn net.Conn
|
||||||
w.startOnce.Do(func() {
|
w.startOnce.Do(func() {
|
||||||
w.startErr = w.tunDevice.Start()
|
w.startErr = w.tunDevice.Start()
|
||||||
@@ -387,7 +355,7 @@ func (w *WireGuard) DialContext(ctx context.Context, metadata *C.Metadata, opts
|
|||||||
|
|
||||||
func (w *WireGuard) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
func (w *WireGuard) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
||||||
options := w.Base.DialOptions(opts...)
|
options := w.Base.DialOptions(opts...)
|
||||||
w.dialer.dialer = dialer.NewDialer(options...)
|
w.dialer.SetDialer(dialer.NewDialer(options...))
|
||||||
var pc net.PacketConn
|
var pc net.PacketConn
|
||||||
w.startOnce.Do(func() {
|
w.startOnce.Do(func() {
|
||||||
w.startErr = w.tunDevice.Start()
|
w.startErr = w.tunDevice.Start()
|
||||||
|
|||||||
@@ -6,13 +6,13 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapter/outbound"
|
"github.com/metacubex/mihomo/adapter/outbound"
|
||||||
"github.com/Dreamacro/clash/common/callback"
|
"github.com/metacubex/mihomo/common/callback"
|
||||||
N "github.com/Dreamacro/clash/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/Dreamacro/clash/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/Dreamacro/clash/constant/provider"
|
"github.com/metacubex/mihomo/constant/provider"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Fallback struct {
|
type Fallback struct {
|
||||||
@@ -84,11 +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,
|
||||||
"expected": f.expectedStatus,
|
"expectedStatus": f.expectedStatus,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,13 +102,11 @@ 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 {
|
||||||
@@ -135,12 +133,11 @@ 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.NewIntRanges[uint16](f.expectedStatus)
|
expectedStatus, _ := utils.NewIntRanges[uint16](f.expectedStatus)
|
||||||
_, _ = p.URLTest(ctx, f.testUrl, expectedStatus, C.ExtraHistory)
|
_, _ = p.URLTest(ctx, f.testUrl, expectedStatus)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -7,14 +7,14 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapter/outbound"
|
"github.com/metacubex/mihomo/adapter/outbound"
|
||||||
"github.com/Dreamacro/clash/common/atomic"
|
"github.com/metacubex/mihomo/common/atomic"
|
||||||
"github.com/Dreamacro/clash/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/Dreamacro/clash/constant/provider"
|
"github.com/metacubex/mihomo/constant/provider"
|
||||||
types "github.com/Dreamacro/clash/constant/provider"
|
types "github.com/metacubex/mihomo/constant/provider"
|
||||||
"github.com/Dreamacro/clash/log"
|
"github.com/metacubex/mihomo/log"
|
||||||
"github.com/Dreamacro/clash/tunnel"
|
"github.com/metacubex/mihomo/tunnel"
|
||||||
|
|
||||||
"github.com/dlclark/regexp2"
|
"github.com/dlclark/regexp2"
|
||||||
)
|
)
|
||||||
@@ -28,7 +28,7 @@ type GroupBase struct {
|
|||||||
failedTestMux sync.Mutex
|
failedTestMux sync.Mutex
|
||||||
failedTimes int
|
failedTimes int
|
||||||
failedTime time.Time
|
failedTime time.Time
|
||||||
failedTesting *atomic.Bool
|
failedTesting atomic.Bool
|
||||||
proxies [][]C.Proxy
|
proxies [][]C.Proxy
|
||||||
versions []atomic.Uint32
|
versions []atomic.Uint32
|
||||||
}
|
}
|
||||||
@@ -202,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, C.DropHistory)
|
delay, err := proxy.URLTest(ctx, url, expectedStatus)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
lock.Lock()
|
lock.Lock()
|
||||||
mp[proxy.Name()] = delay
|
mp[proxy.Name()] = delay
|
||||||
@@ -222,7 +222,7 @@ func (gb *GroupBase) URLTest(ctx context.Context, url string, expectedStatus uti
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (gb *GroupBase) onDialFailed(adapterType C.AdapterType, err error) {
|
func (gb *GroupBase) onDialFailed(adapterType C.AdapterType, err error) {
|
||||||
if adapterType == C.Direct || adapterType == C.Compatible || adapterType == C.Reject || adapterType == C.Pass {
|
if adapterType == C.Direct || adapterType == C.Compatible || adapterType == C.Reject || adapterType == C.Pass || adapterType == C.RejectDrop {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,14 +9,14 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapter/outbound"
|
"github.com/metacubex/mihomo/adapter/outbound"
|
||||||
"github.com/Dreamacro/clash/common/cache"
|
"github.com/metacubex/mihomo/common/callback"
|
||||||
"github.com/Dreamacro/clash/common/callback"
|
"github.com/metacubex/mihomo/common/lru"
|
||||||
N "github.com/Dreamacro/clash/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/Dreamacro/clash/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/Dreamacro/clash/constant/provider"
|
"github.com/metacubex/mihomo/constant/provider"
|
||||||
|
|
||||||
"golang.org/x/net/publicsuffix"
|
"golang.org/x/net/publicsuffix"
|
||||||
)
|
)
|
||||||
@@ -190,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 := cache.New[uint64, int](
|
lruCache := lru.New[uint64, int](
|
||||||
cache.WithAge[uint64, int](int64(ttl.Seconds())),
|
lru.WithAge[uint64, int](int64(ttl.Seconds())),
|
||||||
cache.WithSize[uint64, int](1000))
|
lru.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)
|
||||||
|
|||||||
@@ -5,12 +5,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapter/outbound"
|
"github.com/metacubex/mihomo/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"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -22,18 +22,19 @@ var (
|
|||||||
|
|
||||||
type GroupCommonOption struct {
|
type GroupCommonOption struct {
|
||||||
outbound.BasicOption
|
outbound.BasicOption
|
||||||
Name string `group:"name"`
|
Name string `group:"name"`
|
||||||
Type string `group:"type"`
|
Type string `group:"type"`
|
||||||
Proxies []string `group:"proxies,omitempty"`
|
Proxies []string `group:"proxies,omitempty"`
|
||||||
Use []string `group:"use,omitempty"`
|
Use []string `group:"use,omitempty"`
|
||||||
URL string `group:"url,omitempty"`
|
URL string `group:"url,omitempty"`
|
||||||
Interval int `group:"interval,omitempty"`
|
Interval int `group:"interval,omitempty"`
|
||||||
Lazy bool `group:"lazy,omitempty"`
|
Lazy bool `group:"lazy,omitempty"`
|
||||||
DisableUDP bool `group:"disable-udp,omitempty"`
|
DisableUDP bool `group:"disable-udp,omitempty"`
|
||||||
Filter string `group:"filter,omitempty"`
|
Filter string `group:"filter,omitempty"`
|
||||||
ExcludeFilter string `group:"exclude-filter,omitempty"`
|
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"`
|
||||||
|
IncludeAllProviders bool `group:"include-all-providers,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, providersMap map[string]types.ProxyProvider) (C.ProxyAdapter, error) {
|
func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, providersMap map[string]types.ProxyProvider) (C.ProxyAdapter, error) {
|
||||||
@@ -54,7 +55,18 @@ func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, provide
|
|||||||
|
|
||||||
providers := []types.ProxyProvider{}
|
providers := []types.ProxyProvider{}
|
||||||
|
|
||||||
if len(groupOption.Proxies) == 0 && len(groupOption.Use) == 0 {
|
var GroupUse []string
|
||||||
|
visited := make(map[string]bool)
|
||||||
|
if groupOption.IncludeAllProviders {
|
||||||
|
for name := range provider.ProxyProviderName {
|
||||||
|
GroupUse = append(GroupUse, name)
|
||||||
|
visited[name] = true
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
GroupUse = groupOption.Use
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(groupOption.Proxies) == 0 && len(GroupUse) == 0 {
|
||||||
return nil, fmt.Errorf("%s: %w", groupName, errMissProxy)
|
return nil, fmt.Errorf("%s: %w", groupName, errMissProxy)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,24 +92,20 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
var url string
|
|
||||||
var interval uint
|
|
||||||
|
|
||||||
// select don't need 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 == "" {
|
if groupOption.URL == "" {
|
||||||
groupOption.URL = "https://cp.cloudflare.com/generate_204"
|
groupOption.URL = C.DefaultTestURL
|
||||||
|
testUrl = groupOption.URL
|
||||||
}
|
}
|
||||||
|
|
||||||
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, url, interval, true, expectedStatus)
|
hc := provider.NewHealthCheck(ps, groupOption.URL, uint(groupOption.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)
|
||||||
@@ -107,8 +115,8 @@ func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, provide
|
|||||||
providersMap[groupName] = pd
|
providersMap[groupName] = pd
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(groupOption.Use) != 0 {
|
if len(GroupUse) != 0 {
|
||||||
list, err := getProviders(providersMap, groupOption.Use)
|
list, err := getProviders(providersMap, GroupUse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s: %w", groupName, err)
|
return nil, fmt.Errorf("%s: %w", groupName, err)
|
||||||
}
|
}
|
||||||
|
|||||||
64
adapter/outboundgroup/patch_android.go
Normal file
64
adapter/outboundgroup/patch_android.go
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
//go:build android && cmfa
|
||||||
|
|
||||||
|
package outboundgroup
|
||||||
|
|
||||||
|
import (
|
||||||
|
C "github.com/metacubex/mihomo/constant"
|
||||||
|
"github.com/metacubex/mihomo/constant/provider"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ProxyGroup interface {
|
||||||
|
C.ProxyAdapter
|
||||||
|
|
||||||
|
Providers() []provider.ProxyProvider
|
||||||
|
Proxies() []C.Proxy
|
||||||
|
Now() string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Fallback) Providers() []provider.ProxyProvider {
|
||||||
|
return f.providers
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lb *LoadBalance) Providers() []provider.ProxyProvider {
|
||||||
|
return lb.providers
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Fallback) Proxies() []C.Proxy {
|
||||||
|
return f.GetProxies(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lb *LoadBalance) Proxies() []C.Proxy {
|
||||||
|
return lb.GetProxies(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (lb *LoadBalance) Now() string {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Relay) Providers() []provider.ProxyProvider {
|
||||||
|
return r.providers
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Relay) Proxies() []C.Proxy {
|
||||||
|
return r.GetProxies(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Relay) Now() string {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Selector) Providers() []provider.ProxyProvider {
|
||||||
|
return s.providers
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Selector) Proxies() []C.Proxy {
|
||||||
|
return s.GetProxies(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *URLTest) Providers() []provider.ProxyProvider {
|
||||||
|
return u.providers
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *URLTest) Proxies() []C.Proxy {
|
||||||
|
return u.GetProxies(false)
|
||||||
|
}
|
||||||
@@ -3,11 +3,11 @@ 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"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Relay struct {
|
type Relay struct {
|
||||||
|
|||||||
@@ -5,10 +5,10 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapter/outbound"
|
"github.com/metacubex/mihomo/adapter/outbound"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/Dreamacro/clash/constant/provider"
|
"github.com/metacubex/mihomo/constant/provider"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Selector struct {
|
type Selector struct {
|
||||||
|
|||||||
@@ -6,13 +6,13 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapter/outbound"
|
"github.com/metacubex/mihomo/adapter/outbound"
|
||||||
"github.com/Dreamacro/clash/common/callback"
|
"github.com/metacubex/mihomo/common/callback"
|
||||||
N "github.com/Dreamacro/clash/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
"github.com/Dreamacro/clash/common/singledo"
|
"github.com/metacubex/mihomo/common/singledo"
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/Dreamacro/clash/constant/provider"
|
"github.com/metacubex/mihomo/constant/provider"
|
||||||
)
|
)
|
||||||
|
|
||||||
type urlTestOption func(*URLTest)
|
type urlTestOption func(*URLTest)
|
||||||
@@ -101,7 +101,7 @@ func (u *URLTest) fast(touch bool) C.Proxy {
|
|||||||
proxies := u.GetProxies(touch)
|
proxies := u.GetProxies(touch)
|
||||||
if u.selected != "" {
|
if u.selected != "" {
|
||||||
for _, proxy := range proxies {
|
for _, proxy := range proxies {
|
||||||
if !proxy.Alive() {
|
if !proxy.AliveForTestUrl(u.testUrl) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if proxy.Name() == u.selected {
|
if proxy.Name() == u.selected {
|
||||||
@@ -113,7 +113,6 @@ func (u *URLTest) fast(touch bool) C.Proxy {
|
|||||||
|
|
||||||
elm, _, shared := u.fastSingle.Do(func() (C.Proxy, error) {
|
elm, _, shared := u.fastSingle.Do(func() (C.Proxy, error) {
|
||||||
fast := proxies[0]
|
fast := proxies[0]
|
||||||
// min := fast.LastDelay()
|
|
||||||
min := fast.LastDelayForTestUrl(u.testUrl)
|
min := fast.LastDelayForTestUrl(u.testUrl)
|
||||||
fastNotExist := true
|
fastNotExist := true
|
||||||
|
|
||||||
@@ -122,12 +121,10 @@ 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 < min {
|
if delay < min {
|
||||||
fast = proxy
|
fast = proxy
|
||||||
@@ -136,7 +133,6 @@ func (u *URLTest) fast(touch bool) C.Proxy {
|
|||||||
|
|
||||||
}
|
}
|
||||||
// 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
|
||||||
}
|
}
|
||||||
@@ -169,11 +165,11 @@ 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,
|
||||||
"expected": u.expectedStatus,
|
"expectedStatus": u.expectedStatus,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,5 @@
|
|||||||
package outboundgroup
|
package outboundgroup
|
||||||
|
|
||||||
import (
|
|
||||||
"net"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func tcpKeepAlive(c net.Conn) {
|
|
||||||
if tcp, ok := c.(*net.TCPConn); ok {
|
|
||||||
_ = tcp.SetKeepAlive(true)
|
|
||||||
_ = tcp.SetKeepAlivePeriod(30 * time.Second)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type SelectAble interface {
|
type SelectAble interface {
|
||||||
Set(string) error
|
Set(string) error
|
||||||
ForceSet(name string)
|
ForceSet(name string)
|
||||||
|
|||||||
@@ -3,11 +3,11 @@ package adapter
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
tlsC "github.com/Dreamacro/clash/component/tls"
|
tlsC "github.com/metacubex/mihomo/component/tls"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapter/outbound"
|
"github.com/metacubex/mihomo/adapter/outbound"
|
||||||
"github.com/Dreamacro/clash/common/structure"
|
"github.com/metacubex/mihomo/common/structure"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ParseProxy(mapping map[string]any) (C.Proxy, error) {
|
func ParseProxy(mapping map[string]any) (C.Proxy, error) {
|
||||||
@@ -92,6 +92,13 @@ func ParseProxy(mapping map[string]any) (C.Proxy, error) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
proxy, err = outbound.NewHysteria(*hyOption)
|
proxy, err = outbound.NewHysteria(*hyOption)
|
||||||
|
case "hysteria2":
|
||||||
|
hyOption := &outbound.Hysteria2Option{}
|
||||||
|
err = decoder.Decode(mapping, hyOption)
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
proxy, err = outbound.NewHysteria2(*hyOption)
|
||||||
case "wireguard":
|
case "wireguard":
|
||||||
wgOption := &outbound.WireGuardOption{}
|
wgOption := &outbound.WireGuardOption{}
|
||||||
err = decoder.Decode(mapping, wgOption)
|
err = decoder.Decode(mapping, wgOption)
|
||||||
@@ -106,6 +113,20 @@ func ParseProxy(mapping map[string]any) (C.Proxy, error) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
proxy, err = outbound.NewTuic(*tuicOption)
|
proxy, err = outbound.NewTuic(*tuicOption)
|
||||||
|
case "direct":
|
||||||
|
directOption := &outbound.DirectOption{}
|
||||||
|
err = decoder.Decode(mapping, directOption)
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
proxy = outbound.NewDirectWithOption(*directOption)
|
||||||
|
case "reject":
|
||||||
|
rejectOption := &outbound.RejectOption{}
|
||||||
|
err = decoder.Decode(mapping, rejectOption)
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
proxy = outbound.NewRejectWithOption(*rejectOption)
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unsupport proxy type: %s", proxyType)
|
return nil, fmt.Errorf("unsupport proxy type: %s", proxyType)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,12 +6,12 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/atomic"
|
"github.com/metacubex/mihomo/common/atomic"
|
||||||
"github.com/Dreamacro/clash/common/batch"
|
"github.com/metacubex/mihomo/common/batch"
|
||||||
"github.com/Dreamacro/clash/common/singledo"
|
"github.com/metacubex/mihomo/common/singledo"
|
||||||
"github.com/Dreamacro/clash/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
"github.com/Dreamacro/clash/log"
|
"github.com/metacubex/mihomo/log"
|
||||||
|
|
||||||
"github.com/dlclark/regexp2"
|
"github.com/dlclark/regexp2"
|
||||||
)
|
)
|
||||||
@@ -34,12 +34,12 @@ type HealthCheck struct {
|
|||||||
url string
|
url string
|
||||||
extra map[string]*extraOption
|
extra map[string]*extraOption
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
started *atomic.Bool
|
started atomic.Bool
|
||||||
proxies []C.Proxy
|
proxies []C.Proxy
|
||||||
interval uint
|
interval time.Duration
|
||||||
lazy bool
|
lazy bool
|
||||||
expectedStatus utils.IntRanges[uint16]
|
expectedStatus utils.IntRanges[uint16]
|
||||||
lastTouch *atomic.Int64
|
lastTouch atomic.TypedValue[time.Time]
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
singleDo *singledo.Single[struct{}]
|
singleDo *singledo.Single[struct{}]
|
||||||
}
|
}
|
||||||
@@ -50,13 +50,14 @@ func (hc *HealthCheck) process() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ticker := time.NewTicker(time.Duration(hc.interval) * time.Second)
|
ticker := time.NewTicker(hc.interval)
|
||||||
hc.start()
|
hc.start()
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
now := time.Now().Unix()
|
lastTouch := hc.lastTouch.Load()
|
||||||
if !hc.lazy || now-hc.lastTouch.Load() < int64(hc.interval) {
|
since := time.Since(lastTouch)
|
||||||
|
if !hc.lazy || since < hc.interval {
|
||||||
hc.check()
|
hc.check()
|
||||||
} else {
|
} else {
|
||||||
log.Debugln("Skip once health check because we are lazy")
|
log.Debugln("Skip once health check because we are lazy")
|
||||||
@@ -85,7 +86,7 @@ func (hc *HealthCheck) registerHealthCheckTask(url string, expectedStatus utils.
|
|||||||
|
|
||||||
// if the provider has not set up health checks, then modify it to be the same as the group's interval
|
// if the provider has not set up health checks, then modify it to be the same as the group's interval
|
||||||
if hc.interval == 0 {
|
if hc.interval == 0 {
|
||||||
hc.interval = interval
|
hc.interval = time.Duration(interval) * time.Second
|
||||||
}
|
}
|
||||||
|
|
||||||
if hc.extra == nil {
|
if hc.extra == nil {
|
||||||
@@ -103,12 +104,6 @@ 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
|
||||||
@@ -135,7 +130,7 @@ func (hc *HealthCheck) auto() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (hc *HealthCheck) touch() {
|
func (hc *HealthCheck) touch() {
|
||||||
hc.lastTouch.Store(time.Now().Unix())
|
hc.lastTouch.Store(time.Now())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hc *HealthCheck) start() {
|
func (hc *HealthCheck) start() {
|
||||||
@@ -147,6 +142,10 @@ func (hc *HealthCheck) stop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (hc *HealthCheck) check() {
|
func (hc *HealthCheck) check() {
|
||||||
|
if len(hc.proxies) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
_, _, _ = 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)
|
||||||
@@ -176,13 +175,8 @@ func (hc *HealthCheck) execute(b *batch.Batch[bool], url, uid string, option *ex
|
|||||||
}
|
}
|
||||||
|
|
||||||
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))
|
||||||
@@ -207,7 +201,7 @@ func (hc *HealthCheck) execute(b *batch.Batch[bool], url, uid string, option *ex
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), defaultURLTestTimeout)
|
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, store)
|
_, _ = p.URLTest(ctx, url, expectedStatus)
|
||||||
log.Debugln("Health Checked, proxy: %s, url: %s, alive: %t, delay: %d ms uid: {%s}", p.Name(), url, p.AliveForTestUrl(url), p.LastDelayForTestUrl(url), uid)
|
log.Debugln("Health Checked, proxy: %s, url: %s, alive: %t, delay: %d ms uid: {%s}", p.Name(), url, p.AliveForTestUrl(url), p.LastDelayForTestUrl(url), uid)
|
||||||
return false, nil
|
return false, nil
|
||||||
})
|
})
|
||||||
@@ -222,17 +216,16 @@ func NewHealthCheck(proxies []C.Proxy, url string, interval uint, lazy bool, exp
|
|||||||
if len(url) == 0 {
|
if len(url) == 0 {
|
||||||
interval = 0
|
interval = 0
|
||||||
expectedStatus = nil
|
expectedStatus = nil
|
||||||
|
url = C.DefaultTestURL
|
||||||
}
|
}
|
||||||
|
|
||||||
return &HealthCheck{
|
return &HealthCheck{
|
||||||
proxies: proxies,
|
proxies: proxies,
|
||||||
url: url,
|
url: url,
|
||||||
extra: map[string]*extraOption{},
|
extra: map[string]*extraOption{},
|
||||||
started: atomic.NewBool(false),
|
interval: time.Duration(interval) * time.Second,
|
||||||
interval: interval,
|
|
||||||
lazy: lazy,
|
lazy: lazy,
|
||||||
expectedStatus: expectedStatus,
|
expectedStatus: expectedStatus,
|
||||||
lastTouch: atomic.NewInt64(0),
|
|
||||||
done: make(chan struct{}, 1),
|
done: make(chan struct{}, 1),
|
||||||
singleDo: singledo.NewSingle[struct{}](time.Second),
|
singleDo: singledo.NewSingle[struct{}](time.Second),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,11 +5,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/structure"
|
"github.com/metacubex/mihomo/common/structure"
|
||||||
"github.com/Dreamacro/clash/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
"github.com/Dreamacro/clash/component/resource"
|
"github.com/metacubex/mihomo/component/resource"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
types "github.com/Dreamacro/clash/constant/provider"
|
"github.com/metacubex/mihomo/constant/features"
|
||||||
|
types "github.com/metacubex/mihomo/constant/provider"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -25,16 +26,26 @@ type healthCheckSchema struct {
|
|||||||
ExpectedStatus string `provider:"expected-status,omitempty"`
|
ExpectedStatus string `provider:"expected-status,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type OverrideSchema struct {
|
||||||
|
UDP *bool `provider:"udp,omitempty"`
|
||||||
|
Up *string `provider:"up,omitempty"`
|
||||||
|
Down *string `provider:"down,omitempty"`
|
||||||
|
DialerProxy *string `provider:"dialer-proxy,omitempty"`
|
||||||
|
SkipCertVerify *bool `provider:"skip-cert-verify,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"`
|
||||||
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"`
|
|
||||||
|
HealthCheck healthCheckSchema `provider:"health-check,omitempty"`
|
||||||
|
Override OverrideSchema `provider:"override,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvider, error) {
|
func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvider, error) {
|
||||||
@@ -68,7 +79,7 @@ func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvide
|
|||||||
case "http":
|
case "http":
|
||||||
if schema.Path != "" {
|
if schema.Path != "" {
|
||||||
path := C.Path.Resolve(schema.Path)
|
path := C.Path.Resolve(schema.Path)
|
||||||
if !C.Path.IsSafePath(path) {
|
if !features.CMFA && !C.Path.IsSafePath(path) {
|
||||||
return nil, fmt.Errorf("%w: %s", errSubPath, path)
|
return nil, fmt.Errorf("%w: %s", errSubPath, path)
|
||||||
}
|
}
|
||||||
vehicle = resource.NewHTTPVehicle(schema.URL, path)
|
vehicle = resource.NewHTTPVehicle(schema.URL, path)
|
||||||
@@ -85,6 +96,7 @@ func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvide
|
|||||||
excludeFilter := schema.ExcludeFilter
|
excludeFilter := schema.ExcludeFilter
|
||||||
excludeType := schema.ExcludeType
|
excludeType := schema.ExcludeType
|
||||||
dialerProxy := schema.DialerProxy
|
dialerProxy := schema.DialerProxy
|
||||||
|
override := schema.Override
|
||||||
|
|
||||||
return NewProxySetProvider(name, interval, filter, excludeFilter, excludeType, dialerProxy, vehicle, hc)
|
return NewProxySetProvider(name, interval, filter, excludeFilter, excludeType, dialerProxy, override, vehicle, hc)
|
||||||
}
|
}
|
||||||
|
|||||||
36
adapter/provider/patch_android.go
Normal file
36
adapter/provider/patch_android.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
//go:build android && cmfa
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
@@ -10,20 +10,22 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapter"
|
"github.com/metacubex/mihomo/adapter"
|
||||||
"github.com/Dreamacro/clash/common/convert"
|
"github.com/metacubex/mihomo/common/convert"
|
||||||
"github.com/Dreamacro/clash/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
clashHttp "github.com/Dreamacro/clash/component/http"
|
mihomoHttp "github.com/metacubex/mihomo/component/http"
|
||||||
"github.com/Dreamacro/clash/component/resource"
|
"github.com/metacubex/mihomo/component/resource"
|
||||||
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/Dreamacro/clash/log"
|
"github.com/metacubex/mihomo/log"
|
||||||
"github.com/Dreamacro/clash/tunnel/statistic"
|
"github.com/metacubex/mihomo/tunnel/statistic"
|
||||||
|
|
||||||
"github.com/dlclark/regexp2"
|
"github.com/dlclark/regexp2"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ProxyProviderName = make(map[string]struct{})
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ReservedName = "default"
|
ReservedName = "default"
|
||||||
)
|
)
|
||||||
@@ -46,12 +48,18 @@ type proxySetProvider struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (pp *proxySetProvider) MarshalJSON() ([]byte, error) {
|
func (pp *proxySetProvider) MarshalJSON() ([]byte, error) {
|
||||||
|
expectedStatus := "*"
|
||||||
|
if pp.healthCheck.expectedStatus != nil {
|
||||||
|
expectedStatus = pp.healthCheck.expectedStatus.ToString()
|
||||||
|
}
|
||||||
|
|
||||||
return json.Marshal(map[string]any{
|
return json.Marshal(map[string]any{
|
||||||
"name": pp.Name(),
|
"name": pp.Name(),
|
||||||
"type": pp.Type().String(),
|
"type": pp.Type().String(),
|
||||||
"vehicleType": pp.VehicleType().String(),
|
"vehicleType": pp.VehicleType().String(),
|
||||||
"proxies": pp.Proxies(),
|
"proxies": pp.Proxies(),
|
||||||
"testUrl": pp.healthCheck.url,
|
"testUrl": pp.healthCheck.url,
|
||||||
|
"expectedStatus": expectedStatus,
|
||||||
"updatedAt": pp.UpdatedAt,
|
"updatedAt": pp.UpdatedAt,
|
||||||
"subscriptionInfo": pp.subscriptionInfo,
|
"subscriptionInfo": pp.subscriptionInfo,
|
||||||
})
|
})
|
||||||
@@ -119,8 +127,8 @@ func (pp *proxySetProvider) getSubscriptionInfo() {
|
|||||||
go func() {
|
go func() {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*90)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*90)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
resp, err := clashHttp.HttpRequest(ctx, pp.Vehicle().(*resource.HTTPVehicle).Url(),
|
resp, err := mihomoHttp.HttpRequest(ctx, pp.Vehicle().(*resource.HTTPVehicle).Url(),
|
||||||
http.MethodGet, http.Header{"User-Agent": {"clash"}}, nil)
|
http.MethodGet, http.Header{"User-Agent": {C.UA}}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -128,7 +136,7 @@ func (pp *proxySetProvider) getSubscriptionInfo() {
|
|||||||
|
|
||||||
userInfoStr := strings.TrimSpace(resp.Header.Get("subscription-userinfo"))
|
userInfoStr := strings.TrimSpace(resp.Header.Get("subscription-userinfo"))
|
||||||
if userInfoStr == "" {
|
if userInfoStr == "" {
|
||||||
resp2, err := clashHttp.HttpRequest(ctx, pp.Vehicle().(*resource.HTTPVehicle).Url(),
|
resp2, err := mihomoHttp.HttpRequest(ctx, pp.Vehicle().(*resource.HTTPVehicle).Url(),
|
||||||
http.MethodGet, http.Header{"User-Agent": {"Quantumultx"}}, nil)
|
http.MethodGet, http.Header{"User-Agent": {"Quantumultx"}}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -163,7 +171,7 @@ func stopProxyProvider(pd *ProxySetProvider) {
|
|||||||
_ = pd.Fetcher.Destroy()
|
_ = pd.Fetcher.Destroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProxySetProvider(name string, interval time.Duration, filter string, excludeFilter string, excludeType string, dialerProxy string, vehicle types.Vehicle, hc *HealthCheck) (*ProxySetProvider, error) {
|
func NewProxySetProvider(name string, interval time.Duration, filter string, excludeFilter string, excludeType string, dialerProxy string, override OverrideSchema, vehicle types.Vehicle, hc *HealthCheck) (*ProxySetProvider, error) {
|
||||||
excludeFilterReg, err := regexp2.Compile(excludeFilter, 0)
|
excludeFilterReg, err := regexp2.Compile(excludeFilter, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("invalid excludeFilter regex: %w", err)
|
return nil, fmt.Errorf("invalid excludeFilter regex: %w", err)
|
||||||
@@ -191,8 +199,9 @@ func NewProxySetProvider(name string, interval time.Duration, filter string, exc
|
|||||||
healthCheck: hc,
|
healthCheck: hc,
|
||||||
}
|
}
|
||||||
|
|
||||||
fetcher := resource.NewFetcher[[]C.Proxy](name, interval, vehicle, proxiesParseAndFilter(filter, excludeFilter, excludeTypeArray, filterRegs, excludeFilterReg, dialerProxy), proxiesOnUpdate(pd))
|
fetcher := resource.NewFetcher[[]C.Proxy](name, interval, vehicle, proxiesParseAndFilter(filter, excludeFilter, excludeTypeArray, filterRegs, excludeFilterReg, dialerProxy, override), proxiesOnUpdate(pd))
|
||||||
pd.Fetcher = fetcher
|
pd.Fetcher = fetcher
|
||||||
|
ProxyProviderName[name] = struct{}{}
|
||||||
wrapper := &ProxySetProvider{pd}
|
wrapper := &ProxySetProvider{pd}
|
||||||
runtime.SetFinalizer(wrapper, stopProxyProvider)
|
runtime.SetFinalizer(wrapper, stopProxyProvider)
|
||||||
return wrapper, nil
|
return wrapper, nil
|
||||||
@@ -211,12 +220,18 @@ type compatibleProvider struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cp *compatibleProvider) MarshalJSON() ([]byte, error) {
|
func (cp *compatibleProvider) MarshalJSON() ([]byte, error) {
|
||||||
|
expectedStatus := "*"
|
||||||
|
if cp.healthCheck.expectedStatus != nil {
|
||||||
|
expectedStatus = cp.healthCheck.expectedStatus.ToString()
|
||||||
|
}
|
||||||
|
|
||||||
return json.Marshal(map[string]any{
|
return json.Marshal(map[string]any{
|
||||||
"name": cp.Name(),
|
"name": cp.Name(),
|
||||||
"type": cp.Type().String(),
|
"type": cp.Type().String(),
|
||||||
"vehicleType": cp.VehicleType().String(),
|
"vehicleType": cp.VehicleType().String(),
|
||||||
"proxies": cp.Proxies(),
|
"proxies": cp.Proxies(),
|
||||||
"testUrl": cp.healthCheck.url,
|
"testUrl": cp.healthCheck.url,
|
||||||
|
"expectedStatus": expectedStatus,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -292,7 +307,7 @@ func proxiesOnUpdate(pd *proxySetProvider) func([]C.Proxy) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func proxiesParseAndFilter(filter string, excludeFilter string, excludeTypeArray []string, filterRegs []*regexp2.Regexp, excludeFilterReg *regexp2.Regexp, dialerProxy string) resource.Parser[[]C.Proxy] {
|
func proxiesParseAndFilter(filter string, excludeFilter string, excludeTypeArray []string, filterRegs []*regexp2.Regexp, excludeFilterReg *regexp2.Regexp, dialerProxy string, override OverrideSchema) resource.Parser[[]C.Proxy] {
|
||||||
return func(buf []byte) ([]C.Proxy, error) {
|
return func(buf []byte) ([]C.Proxy, error) {
|
||||||
schema := &ProxySchema{}
|
schema := &ProxySchema{}
|
||||||
|
|
||||||
@@ -355,13 +370,32 @@ func proxiesParseAndFilter(filter string, excludeFilter string, excludeTypeArray
|
|||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if override.UDP != nil {
|
||||||
|
mapping["udp"] = *override.UDP
|
||||||
|
}
|
||||||
|
if override.Up != nil {
|
||||||
|
mapping["up"] = *override.Up
|
||||||
|
}
|
||||||
|
if override.Down != nil {
|
||||||
|
mapping["down"] = *override.Down
|
||||||
|
}
|
||||||
|
if override.DialerProxy != nil {
|
||||||
|
mapping["dialer-proxy"] = *override.DialerProxy
|
||||||
|
}
|
||||||
|
if override.SkipCertVerify != nil {
|
||||||
|
mapping["skip-cert-verify"] = *override.SkipCertVerify
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package provider
|
package provider
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/dlclark/regexp2"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -13,45 +12,28 @@ type SubscriptionInfo struct {
|
|||||||
Expire int64
|
Expire int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSubscriptionInfo(str string) (si *SubscriptionInfo, err error) {
|
func NewSubscriptionInfo(userinfo string) (si *SubscriptionInfo, err error) {
|
||||||
si = &SubscriptionInfo{}
|
userinfo = strings.ToLower(userinfo)
|
||||||
str = strings.ToLower(str)
|
userinfo = strings.ReplaceAll(userinfo, " ", "")
|
||||||
reTraffic := regexp2.MustCompile("upload=(\\d+); download=(\\d+); total=(\\d+)", 0)
|
si = new(SubscriptionInfo)
|
||||||
reExpire := regexp2.MustCompile("expire=(\\d+)", 0)
|
for _, field := range strings.Split(userinfo, ";") {
|
||||||
|
switch name, value, _ := strings.Cut(field, "="); name {
|
||||||
match, err := reTraffic.FindStringMatch(str)
|
case "upload":
|
||||||
if err != nil || match == nil {
|
si.Upload, err = strconv.ParseInt(value, 10, 64)
|
||||||
return nil, err
|
case "download":
|
||||||
}
|
si.Download, err = strconv.ParseInt(value, 10, 64)
|
||||||
group := match.Groups()
|
case "total":
|
||||||
si.Upload, err = str2uint64(group[1].String())
|
si.Total, err = strconv.ParseInt(value, 10, 64)
|
||||||
if err != nil {
|
case "expire":
|
||||||
return nil, err
|
if value == "" {
|
||||||
}
|
si.Expire = 0
|
||||||
|
} else {
|
||||||
si.Download, err = str2uint64(group[2].String())
|
si.Expire, err = strconv.ParseInt(value, 10, 64)
|
||||||
if err != nil {
|
}
|
||||||
return nil, err
|
}
|
||||||
}
|
|
||||||
|
|
||||||
si.Total, err = str2uint64(group[3].String())
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
match, _ = reExpire.FindStringMatch(str)
|
|
||||||
if match != nil {
|
|
||||||
group = match.Groups()
|
|
||||||
si.Expire, err = str2uint64(group[1].String())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func str2uint64(str string) (int64, error) {
|
|
||||||
i, err := strconv.ParseInt(str, 10, 64)
|
|
||||||
return i, err
|
|
||||||
}
|
|
||||||
|
|||||||
229
common/arc/arc.go
Normal file
229
common/arc/arc.go
Normal file
@@ -0,0 +1,229 @@
|
|||||||
|
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]
|
||||||
|
staleReturn bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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]{
|
||||||
|
p: 0,
|
||||||
|
t1: list.New[*entry[K, V]](),
|
||||||
|
b1: list.New[*entry[K, V]](),
|
||||||
|
t2: list.New[*entry[K, V]](),
|
||||||
|
b2: list.New[*entry[K, V]](),
|
||||||
|
len: 0,
|
||||||
|
cache: make(map[K]*entry[K, V]),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, option := range options {
|
||||||
|
option(arc)
|
||||||
|
}
|
||||||
|
return arc
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 != true {
|
||||||
|
a.len++
|
||||||
|
ent := &entry[K, V]{key: key, value: value, ghost: false, expires: expires.Unix()}
|
||||||
|
a.req(ent)
|
||||||
|
a.cache[key] = ent
|
||||||
|
} else {
|
||||||
|
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 ent.value, true
|
||||||
|
}
|
||||||
|
return lo.Empty[V](), false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *ARC[K, V]) get(key K) (e *entry[K, V], ok bool) {
|
||||||
|
ent, ok := a.cache[key]
|
||||||
|
if ok {
|
||||||
|
a.req(ent)
|
||||||
|
return ent, !ent.ghost
|
||||||
|
}
|
||||||
|
return ent, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 check the maxAge of element and 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]) {
|
||||||
|
if ent.ll == a.t1 || ent.ll == a.t2 {
|
||||||
|
// Case I
|
||||||
|
ent.setMRU(a.t2)
|
||||||
|
} else if 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.p = a.p + d
|
||||||
|
if a.c < a.p {
|
||||||
|
a.p = a.c
|
||||||
|
}
|
||||||
|
|
||||||
|
a.replace(ent)
|
||||||
|
ent.setMRU(a.t2)
|
||||||
|
} else if 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.p = a.p - d
|
||||||
|
if a.p < 0 {
|
||||||
|
a.p = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
a.replace(ent)
|
||||||
|
ent.setMRU(a.t2)
|
||||||
|
} else if ent.ll == nil {
|
||||||
|
// Case IV
|
||||||
|
|
||||||
|
if a.t1.Len()+a.b1.Len() == a.c {
|
||||||
|
// Case A
|
||||||
|
if a.t1.Len() < a.c {
|
||||||
|
a.delLRU(a.b1)
|
||||||
|
a.replace(ent)
|
||||||
|
} else {
|
||||||
|
a.delLRU(a.t1)
|
||||||
|
}
|
||||||
|
} else if a.t1.Len()+a.b1.Len() < a.c {
|
||||||
|
// Case 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
59
common/arc/arc_test.go
Normal file
59
common/arc/arc_test.go
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
package arc
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestBasic(t *testing.T) {
|
||||||
|
cache := New[string, string](WithSize[string, string](3))
|
||||||
|
if cache.Len() != 0 {
|
||||||
|
t.Error("Empty cache should have length 0")
|
||||||
|
}
|
||||||
|
|
||||||
|
cache.Set("Hello", "World")
|
||||||
|
if cache.Len() != 1 {
|
||||||
|
t.Error("Cache should have length 1")
|
||||||
|
}
|
||||||
|
|
||||||
|
var val interface{}
|
||||||
|
var ok bool
|
||||||
|
|
||||||
|
if val, ok = cache.Get("Hello"); val != "World" || ok != true {
|
||||||
|
t.Error("Didn't set \"Hello\" to \"World\"")
|
||||||
|
}
|
||||||
|
|
||||||
|
cache.Set("Hello", "World1")
|
||||||
|
if cache.Len() != 1 {
|
||||||
|
t.Error("Inserting the same entry multiple times shouldn't increase cache size")
|
||||||
|
}
|
||||||
|
|
||||||
|
if val, ok = cache.Get("Hello"); val != "World1" || ok != true {
|
||||||
|
t.Error("Didn't update \"Hello\" to \"World1\"")
|
||||||
|
}
|
||||||
|
|
||||||
|
cache.Set("Hallo", "Welt")
|
||||||
|
if cache.Len() != 2 {
|
||||||
|
t.Error("Inserting two different entries should result into lenght=2")
|
||||||
|
}
|
||||||
|
|
||||||
|
if val, ok = cache.Get("Hallo"); val != "Welt" || ok != true {
|
||||||
|
t.Error("Didn't set \"Hallo\" to \"Welt\"")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBasicReplace(t *testing.T) {
|
||||||
|
cache := New[string, string](WithSize[string, string](3))
|
||||||
|
|
||||||
|
cache.Set("Hello", "Hallo")
|
||||||
|
cache.Set("World", "Welt")
|
||||||
|
cache.Get("World")
|
||||||
|
cache.Set("Cache", "Cache")
|
||||||
|
cache.Set("Replace", "Ersetzen")
|
||||||
|
|
||||||
|
value, ok := cache.Get("World")
|
||||||
|
if !ok || value != "Welt" {
|
||||||
|
t.Error("ARC should have replaced \"Hello\"")
|
||||||
|
}
|
||||||
|
|
||||||
|
if cache.Len() != 3 {
|
||||||
|
t.Error("ARC should have a maximum size of 3")
|
||||||
|
}
|
||||||
|
}
|
||||||
32
common/arc/entry.go
Normal file
32
common/arc/entry.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
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,10 +11,9 @@ type Bool struct {
|
|||||||
atomic.Bool
|
atomic.Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBool(val bool) *Bool {
|
func NewBool(val bool) (i Bool) {
|
||||||
i := &Bool{}
|
|
||||||
i.Store(val)
|
i.Store(val)
|
||||||
return i
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Bool) MarshalJSON() ([]byte, error) {
|
func (i *Bool) MarshalJSON() ([]byte, error) {
|
||||||
@@ -39,12 +38,11 @@ type Pointer[T any] struct {
|
|||||||
atomic.Pointer[T]
|
atomic.Pointer[T]
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPointer[T any](v *T) *Pointer[T] {
|
func NewPointer[T any](v *T) (p Pointer[T]) {
|
||||||
var p Pointer[T]
|
|
||||||
if v != nil {
|
if v != nil {
|
||||||
p.Store(v)
|
p.Store(v)
|
||||||
}
|
}
|
||||||
return &p
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pointer[T]) MarshalJSON() ([]byte, error) {
|
func (p *Pointer[T]) MarshalJSON() ([]byte, error) {
|
||||||
@@ -68,10 +66,9 @@ type Int32 struct {
|
|||||||
atomic.Int32
|
atomic.Int32
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewInt32(val int32) *Int32 {
|
func NewInt32(val int32) (i Int32) {
|
||||||
i := &Int32{}
|
|
||||||
i.Store(val)
|
i.Store(val)
|
||||||
return i
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Int32) MarshalJSON() ([]byte, error) {
|
func (i *Int32) MarshalJSON() ([]byte, error) {
|
||||||
@@ -96,10 +93,9 @@ type Int64 struct {
|
|||||||
atomic.Int64
|
atomic.Int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewInt64(val int64) *Int64 {
|
func NewInt64(val int64) (i Int64) {
|
||||||
i := &Int64{}
|
|
||||||
i.Store(val)
|
i.Store(val)
|
||||||
return i
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Int64) MarshalJSON() ([]byte, error) {
|
func (i *Int64) MarshalJSON() ([]byte, error) {
|
||||||
@@ -124,10 +120,9 @@ type Uint32 struct {
|
|||||||
atomic.Uint32
|
atomic.Uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUint32(val uint32) *Uint32 {
|
func NewUint32(val uint32) (i Uint32) {
|
||||||
i := &Uint32{}
|
|
||||||
i.Store(val)
|
i.Store(val)
|
||||||
return i
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Uint32) MarshalJSON() ([]byte, error) {
|
func (i *Uint32) MarshalJSON() ([]byte, error) {
|
||||||
@@ -152,10 +147,9 @@ type Uint64 struct {
|
|||||||
atomic.Uint64
|
atomic.Uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUint64(val uint64) *Uint64 {
|
func NewUint64(val uint64) (i Uint64) {
|
||||||
i := &Uint64{}
|
|
||||||
i.Store(val)
|
i.Store(val)
|
||||||
return i
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Uint64) MarshalJSON() ([]byte, error) {
|
func (i *Uint64) MarshalJSON() ([]byte, error) {
|
||||||
@@ -180,10 +174,9 @@ type Uintptr struct {
|
|||||||
atomic.Uintptr
|
atomic.Uintptr
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUintptr(val uintptr) *Uintptr {
|
func NewUintptr(val uintptr) (i Uintptr) {
|
||||||
i := &Uintptr{}
|
|
||||||
i.Store(val)
|
i.Store(val)
|
||||||
return i
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Uintptr) MarshalJSON() ([]byte, error) {
|
func (i *Uintptr) MarshalJSON() ([]byte, error) {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ func DefaultValue[T any]() T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type TypedValue[T any] struct {
|
type TypedValue[T any] struct {
|
||||||
|
_ noCopy
|
||||||
value atomic.Value
|
value atomic.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,8 +52,13 @@ func (t *TypedValue[T]) UnmarshalJSON(b []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTypedValue[T any](t T) *TypedValue[T] {
|
func NewTypedValue[T any](t T) (v TypedValue[T]) {
|
||||||
v := &TypedValue[T]{}
|
|
||||||
v.Store(t)
|
v.Store(t)
|
||||||
return v
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type noCopy struct{}
|
||||||
|
|
||||||
|
// Lock is a no-op used by -copylocks checker from `go vet`.
|
||||||
|
func (*noCopy) Lock() {}
|
||||||
|
func (*noCopy) Unlock() {}
|
||||||
|
|||||||
@@ -10,17 +10,11 @@ const BufferSize = buf.BufferSize
|
|||||||
type Buffer = buf.Buffer
|
type Buffer = buf.Buffer
|
||||||
|
|
||||||
var New = buf.New
|
var New = buf.New
|
||||||
|
var NewPacket = buf.NewPacket
|
||||||
var NewSize = buf.NewSize
|
var NewSize = buf.NewSize
|
||||||
var With = buf.With
|
var With = buf.With
|
||||||
var As = buf.As
|
var As = buf.As
|
||||||
|
|
||||||
var KeepAlive = common.KeepAlive
|
|
||||||
|
|
||||||
//go:norace
|
|
||||||
func Dup[T any](obj T) T {
|
|
||||||
return common.Dup(obj)
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Must = common.Must
|
Must = common.Must
|
||||||
Error = common.Error
|
Error = common.Error
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
package callback
|
package callback
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/Dreamacro/clash/common/buf"
|
"github.com/metacubex/mihomo/common/buf"
|
||||||
N "github.com/Dreamacro/clash/common/net"
|
N "github.com/metacubex/mihomo/common/net"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/metacubex/mihomo/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
type firstWriteCallBackConn struct {
|
type firstWriteCallBackConn struct {
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ func TestSplitArgs(t *testing.T) {
|
|||||||
|
|
||||||
func TestExecCmd(t *testing.T) {
|
func TestExecCmd(t *testing.T) {
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
_, err := ExecCmd("dir")
|
_, err := ExecCmd("cmd -c 'dir'")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,56 +0,0 @@
|
|||||||
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++
|
|
||||||
}
|
|
||||||
@@ -9,10 +9,10 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/log"
|
"github.com/metacubex/mihomo/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ConvertsV2Ray convert V2Ray subscribe proxies data to clash proxies config
|
// ConvertsV2Ray convert V2Ray subscribe proxies data to mihomo proxies config
|
||||||
func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
||||||
data := DecodeBase64(buf)
|
data := DecodeBase64(buf)
|
||||||
|
|
||||||
@@ -50,7 +50,9 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|||||||
hysteria["port"] = urlHysteria.Port()
|
hysteria["port"] = urlHysteria.Port()
|
||||||
hysteria["sni"] = query.Get("peer")
|
hysteria["sni"] = query.Get("peer")
|
||||||
hysteria["obfs"] = query.Get("obfs")
|
hysteria["obfs"] = query.Get("obfs")
|
||||||
hysteria["alpn"] = []string{query.Get("alpn")}
|
if alpn := query.Get("alpn"); alpn != "" {
|
||||||
|
hysteria["alpn"] = strings.Split(alpn, ",")
|
||||||
|
}
|
||||||
hysteria["auth_str"] = query.Get("auth")
|
hysteria["auth_str"] = query.Get("auth")
|
||||||
hysteria["protocol"] = query.Get("protocol")
|
hysteria["protocol"] = query.Get("protocol")
|
||||||
up := query.Get("up")
|
up := query.Get("up")
|
||||||
@@ -66,7 +68,82 @@ 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":
|
||||||
|
urlHysteria2, err := url.Parse(line)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
query := urlHysteria2.Query()
|
||||||
|
name := uniqueName(names, urlHysteria2.Fragment)
|
||||||
|
hysteria2 := make(map[string]any, 20)
|
||||||
|
|
||||||
|
hysteria2["name"] = name
|
||||||
|
hysteria2["type"] = scheme
|
||||||
|
hysteria2["server"] = urlHysteria2.Hostname()
|
||||||
|
if port := urlHysteria2.Port(); port != "" {
|
||||||
|
hysteria2["port"] = port
|
||||||
|
} else {
|
||||||
|
hysteria2["port"] = "443"
|
||||||
|
}
|
||||||
|
hysteria2["obfs"] = query.Get("obfs")
|
||||||
|
hysteria2["obfs-password"] = query.Get("obfs-password")
|
||||||
|
hysteria2["sni"] = query.Get("sni")
|
||||||
|
hysteria2["skip-cert-verify"], _ = strconv.ParseBool(query.Get("insecure"))
|
||||||
|
if alpn := query.Get("alpn"); alpn != "" {
|
||||||
|
hysteria2["alpn"] = strings.Split(alpn, ",")
|
||||||
|
}
|
||||||
|
if auth := urlHysteria2.User.String(); auth != "" {
|
||||||
|
hysteria2["password"] = auth
|
||||||
|
}
|
||||||
|
hysteria2["fingerprint"] = query.Get("pinSHA256")
|
||||||
|
hysteria2["down"] = query.Get("down")
|
||||||
|
hysteria2["up"] = query.Get("up")
|
||||||
|
|
||||||
|
proxies = append(proxies, hysteria2)
|
||||||
|
case "tuic":
|
||||||
|
// A temporary unofficial TUIC share link standard
|
||||||
|
// Modified from https://github.com/daeuniverse/dae/discussions/182
|
||||||
|
// Changes:
|
||||||
|
// 1. Support TUICv4, just replace uuid:password with token
|
||||||
|
// 2. Remove `allow_insecure` field
|
||||||
|
urlTUIC, err := url.Parse(line)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
query := urlTUIC.Query()
|
||||||
|
|
||||||
|
tuic := make(map[string]any, 20)
|
||||||
|
tuic["name"] = uniqueName(names, urlTUIC.Fragment)
|
||||||
|
tuic["type"] = scheme
|
||||||
|
tuic["server"] = urlTUIC.Hostname()
|
||||||
|
tuic["port"] = urlTUIC.Port()
|
||||||
|
tuic["udp"] = true
|
||||||
|
password, v5 := urlTUIC.User.Password()
|
||||||
|
if v5 {
|
||||||
|
tuic["uuid"] = urlTUIC.User.Username()
|
||||||
|
tuic["password"] = password
|
||||||
|
} else {
|
||||||
|
tuic["token"] = urlTUIC.User.Username()
|
||||||
|
}
|
||||||
|
if cc := query.Get("congestion_control"); cc != "" {
|
||||||
|
tuic["congestion-controller"] = cc
|
||||||
|
}
|
||||||
|
if alpn := query.Get("alpn"); alpn != "" {
|
||||||
|
tuic["alpn"] = strings.Split(alpn, ",")
|
||||||
|
}
|
||||||
|
if sni := query.Get("sni"); sni != "" {
|
||||||
|
tuic["sni"] = sni
|
||||||
|
}
|
||||||
|
if query.Get("disable_sni") == "1" {
|
||||||
|
tuic["disable-sni"] = true
|
||||||
|
}
|
||||||
|
if udpRelayMode := query.Get("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 {
|
||||||
@@ -86,10 +163,12 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|||||||
trojan["udp"] = true
|
trojan["udp"] = true
|
||||||
trojan["skip-cert-verify"], _ = strconv.ParseBool(query.Get("allowInsecure"))
|
trojan["skip-cert-verify"], _ = strconv.ParseBool(query.Get("allowInsecure"))
|
||||||
|
|
||||||
sni := query.Get("sni")
|
if sni := query.Get("sni"); sni != "" {
|
||||||
if sni != "" {
|
|
||||||
trojan["sni"] = sni
|
trojan["sni"] = sni
|
||||||
}
|
}
|
||||||
|
if alpn := query.Get("alpn"); alpn != "" {
|
||||||
|
trojan["alpn"] = strings.Split(alpn, ",")
|
||||||
|
}
|
||||||
|
|
||||||
network := strings.ToLower(query.Get("type"))
|
network := strings.ToLower(query.Get("type"))
|
||||||
if network != "" {
|
if network != "" {
|
||||||
@@ -217,6 +296,9 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|||||||
if strings.HasSuffix(tls, "tls") {
|
if strings.HasSuffix(tls, "tls") {
|
||||||
vmess["tls"] = true
|
vmess["tls"] = true
|
||||||
}
|
}
|
||||||
|
if alpn, ok := values["alpn"].(string); ok {
|
||||||
|
vmess["alpn"] = strings.Split(alpn, ",")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch network {
|
switch network {
|
||||||
@@ -332,6 +414,7 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
proxies = append(proxies, ss)
|
proxies = append(proxies, ss)
|
||||||
|
|
||||||
case "ssr":
|
case "ssr":
|
||||||
dcBuf, err := encRaw.DecodeString(body)
|
dcBuf, err := encRaw.DecodeString(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
35
common/convert/converter_test.go
Normal file
35
common/convert/converter_test.go
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package convert
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
// https://v2.hysteria.network/zh/docs/developers/URI-Scheme/
|
||||||
|
func TestConvertsV2Ray_normal(t *testing.T) {
|
||||||
|
hy2test := "hysteria2://letmein@example.com:8443/?insecure=1&obfs=salamander&obfs-password=gawrgura&pinSHA256=deadbeef&sni=real.example.com&up=114&down=514&alpn=h3,h4#hy2test"
|
||||||
|
|
||||||
|
expected := []map[string]interface{}{
|
||||||
|
{
|
||||||
|
"name": "hy2test",
|
||||||
|
"type": "hysteria2",
|
||||||
|
"server": "example.com",
|
||||||
|
"port": "8443",
|
||||||
|
"sni": "real.example.com",
|
||||||
|
"obfs": "salamander",
|
||||||
|
"obfs-password": "gawrgura",
|
||||||
|
"alpn": []string{"h3", "h4"},
|
||||||
|
"password": "letmein",
|
||||||
|
"up": "114",
|
||||||
|
"down": "514",
|
||||||
|
"skip-cert-verify": true,
|
||||||
|
"fingerprint": "deadbeef",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
proxies, err := ConvertsV2Ray([]byte(hy2test))
|
||||||
|
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, expected, proxies)
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/utils"
|
"github.com/metacubex/mihomo/common/utils"
|
||||||
|
|
||||||
"github.com/metacubex/sing-shadowsocks/shadowimpl"
|
"github.com/metacubex/sing-shadowsocks/shadowimpl"
|
||||||
"github.com/zhangyunhao116/fastrand"
|
"github.com/zhangyunhao116/fastrand"
|
||||||
|
|||||||
@@ -24,8 +24,6 @@ func handleVShareLink(names map[string]int, url *url.URL, scheme string, proxy m
|
|||||||
proxy["port"] = url.Port()
|
proxy["port"] = url.Port()
|
||||||
proxy["uuid"] = url.User.Username()
|
proxy["uuid"] = url.User.Username()
|
||||||
proxy["udp"] = true
|
proxy["udp"] = true
|
||||||
proxy["skip-cert-verify"] = false
|
|
||||||
proxy["tls"] = false
|
|
||||||
tls := strings.ToLower(query.Get("security"))
|
tls := strings.ToLower(query.Get("security"))
|
||||||
if strings.HasSuffix(tls, "tls") || tls == "reality" {
|
if strings.HasSuffix(tls, "tls") || tls == "reality" {
|
||||||
proxy["tls"] = true
|
proxy["tls"] = true
|
||||||
@@ -34,6 +32,9 @@ func handleVShareLink(names map[string]int, url *url.URL, scheme string, proxy m
|
|||||||
} else {
|
} else {
|
||||||
proxy["client-fingerprint"] = fingerprint
|
proxy["client-fingerprint"] = fingerprint
|
||||||
}
|
}
|
||||||
|
if alpn := query.Get("alpn"); alpn != "" {
|
||||||
|
proxy["alpn"] = strings.Split(alpn, ",")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if sni := query.Get("sni"); sni != "" {
|
if sni := query.Get("sni"); sni != "" {
|
||||||
proxy["servername"] = sni
|
proxy["servername"] = sni
|
||||||
|
|||||||
@@ -1,235 +0,0 @@
|
|||||||
// 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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package cache
|
package lru
|
||||||
|
|
||||||
// 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"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/generics/list"
|
list "github.com/bahlo/generic-list-go"
|
||||||
|
"github.com/samber/lo"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Option is part of Functional Options Pattern
|
// Option is part of Functional Options Pattern
|
||||||
@@ -87,7 +88,7 @@ func (c *LruCache[K, V]) Get(key K) (V, bool) {
|
|||||||
|
|
||||||
el := c.get(key)
|
el := c.get(key)
|
||||||
if el == nil {
|
if el == nil {
|
||||||
return getZero[V](), false
|
return lo.Empty[V](), false
|
||||||
}
|
}
|
||||||
value := el.value
|
value := el.value
|
||||||
|
|
||||||
@@ -119,7 +120,7 @@ func (c *LruCache[K, V]) GetWithExpire(key K) (V, time.Time, bool) {
|
|||||||
|
|
||||||
el := c.get(key)
|
el := c.get(key)
|
||||||
if el == nil {
|
if el == nil {
|
||||||
return getZero[V](), time.Time{}, false
|
return lo.Empty[V](), time.Time{}, false
|
||||||
}
|
}
|
||||||
|
|
||||||
return el.value, time.Unix(el.expires, 0), true
|
return el.value, time.Unix(el.expires, 0), true
|
||||||
@@ -259,8 +260,3 @@ type entry[K comparable, V any] struct {
|
|||||||
value V
|
value V
|
||||||
expires int64
|
expires int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func getZero[T any]() T {
|
|
||||||
var result T
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package cache
|
package lru
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/buf"
|
"github.com/metacubex/mihomo/common/buf"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ ExtendedConn = (*BufferedConn)(nil)
|
var _ ExtendedConn = (*BufferedConn)(nil)
|
||||||
@@ -22,6 +22,16 @@ 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
|
||||||
@@ -74,9 +84,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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
34
common/net/bufconn_unsafe.go
Normal file
34
common/net/bufconn_unsafe.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
49
common/net/cached.go
Normal file
49
common/net/cached.go
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
31
common/net/context.go
Normal file
31
common/net/context.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package net
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SetupContextForConn is a helper function that starts connection I/O interrupter goroutine.
|
||||||
|
func SetupContextForConn(ctx context.Context, conn net.Conn) (done func(*error)) {
|
||||||
|
var (
|
||||||
|
quit = make(chan struct{})
|
||||||
|
interrupt = make(chan error, 1)
|
||||||
|
)
|
||||||
|
go func() {
|
||||||
|
select {
|
||||||
|
case <-quit:
|
||||||
|
interrupt <- nil
|
||||||
|
case <-ctx.Done():
|
||||||
|
// Close the connection, discarding the error
|
||||||
|
_ = conn.Close()
|
||||||
|
interrupt <- ctx.Err()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return func(inputErr *error) {
|
||||||
|
close(quit)
|
||||||
|
if ctxErr := <-interrupt; ctxErr != nil && inputErr != nil {
|
||||||
|
// Return context error to user.
|
||||||
|
inputErr = &ctxErr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,8 +6,8 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/atomic"
|
"github.com/metacubex/mihomo/common/atomic"
|
||||||
"github.com/Dreamacro/clash/common/net/packet"
|
"github.com/metacubex/mihomo/common/net/packet"
|
||||||
)
|
)
|
||||||
|
|
||||||
type readResult struct {
|
type readResult struct {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/net/packet"
|
"github.com/metacubex/mihomo/common/net/packet"
|
||||||
)
|
)
|
||||||
|
|
||||||
type EnhancePacketConn struct {
|
type EnhancePacketConn struct {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/net/packet"
|
"github.com/metacubex/mihomo/common/net/packet"
|
||||||
"github.com/sagernet/sing/common/buf"
|
"github.com/sagernet/sing/common/buf"
|
||||||
"github.com/sagernet/sing/common/bufio"
|
"github.com/sagernet/sing/common/bufio"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
|
|||||||
67
common/net/earlyconn.go
Normal file
67
common/net/earlyconn.go
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
package net
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/metacubex/mihomo/common/buf"
|
||||||
|
)
|
||||||
|
|
||||||
|
type earlyConn struct {
|
||||||
|
ExtendedConn // only expose standard N.ExtendedConn function to outside
|
||||||
|
resFunc func() error
|
||||||
|
resOnce sync.Once
|
||||||
|
resErr error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (conn *earlyConn) Response() error {
|
||||||
|
conn.resOnce.Do(func() {
|
||||||
|
conn.resErr = conn.resFunc()
|
||||||
|
})
|
||||||
|
return conn.resErr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (conn *earlyConn) Read(b []byte) (n int, err error) {
|
||||||
|
err = conn.Response()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return conn.ExtendedConn.Read(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (conn *earlyConn) ReadBuffer(buffer *buf.Buffer) (err error) {
|
||||||
|
err = conn.Response()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return conn.ExtendedConn.ReadBuffer(buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (conn *earlyConn) Upstream() any {
|
||||||
|
return conn.ExtendedConn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (conn *earlyConn) Success() bool {
|
||||||
|
// atomic visit sync.Once.done
|
||||||
|
return atomic.LoadUint32((*uint32)(unsafe.Pointer(&conn.resOnce))) == 1 && conn.resErr == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (conn *earlyConn) ReaderReplaceable() bool {
|
||||||
|
return conn.Success()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (conn *earlyConn) ReaderPossiblyReplaceable() bool {
|
||||||
|
return !conn.Success()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (conn *earlyConn) WriterReplaceable() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ ExtendedConn = (*earlyConn)(nil)
|
||||||
|
|
||||||
|
func NewEarlyConn(c net.Conn, f func() error) net.Conn {
|
||||||
|
return &earlyConn{ExtendedConn: NewExtendedConn(c), resFunc: f}
|
||||||
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package net
|
package net
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/Dreamacro/clash/common/net/deadline"
|
"github.com/metacubex/mihomo/common/net/deadline"
|
||||||
"github.com/Dreamacro/clash/common/net/packet"
|
"github.com/metacubex/mihomo/common/net/packet"
|
||||||
)
|
)
|
||||||
|
|
||||||
type EnhancePacketConn = packet.EnhancePacketConn
|
type EnhancePacketConn = packet.EnhancePacketConn
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package packet
|
|||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/pool"
|
"github.com/metacubex/mihomo/common/pool"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WaitReadFrom interface {
|
type WaitReadFrom interface {
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/pool"
|
"github.com/metacubex/mihomo/common/pool"
|
||||||
)
|
)
|
||||||
|
|
||||||
type enhanceUDPConn struct {
|
type enhanceUDPConn struct {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/buf"
|
"github.com/metacubex/mihomo/common/buf"
|
||||||
)
|
)
|
||||||
|
|
||||||
type refConn struct {
|
type refConn struct {
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ package net
|
|||||||
//
|
//
|
||||||
// go func() {
|
// go func() {
|
||||||
// // Wrapping to avoid using *net.TCPConn.(ReadFrom)
|
// // Wrapping to avoid using *net.TCPConn.(ReadFrom)
|
||||||
// // See also https://github.com/Dreamacro/clash/pull/1209
|
// // See also https://github.com/metacubex/mihomo/pull/1209
|
||||||
// _, err := io.Copy(WriteOnlyWriter{Writer: leftConn}, ReadOnlyReader{Reader: rightConn})
|
// _, err := io.Copy(WriteOnlyWriter{Writer: leftConn}, ReadOnlyReader{Reader: rightConn})
|
||||||
// leftConn.SetReadDeadline(time.Now())
|
// leftConn.SetReadDeadline(time.Now())
|
||||||
// ch <- err
|
// ch <- err
|
||||||
|
|||||||
@@ -4,8 +4,11 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var KeepAliveInterval = 15 * time.Second
|
||||||
|
|
||||||
func SplitNetworkType(s string) (string, string, error) {
|
func SplitNetworkType(s string) (string, string, error) {
|
||||||
var (
|
var (
|
||||||
shecme string
|
shecme string
|
||||||
@@ -44,3 +47,10 @@ func SplitHostPort(s string) (host, port string, hasPort bool, err error) {
|
|||||||
host, port, err = net.SplitHostPort(temp)
|
host, port, err = net.SplitHostPort(temp)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TCPKeepAlive(c net.Conn) {
|
||||||
|
if tcp, ok := c.(*net.TCPConn); ok {
|
||||||
|
_ = tcp.SetKeepAlive(true)
|
||||||
|
_ = tcp.SetKeepAlivePeriod(KeepAliveInterval)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,7 +10,11 @@ import (
|
|||||||
"math/big"
|
"math/big"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ParseCert(certificate, privateKey string) (tls.Certificate, error) {
|
type Path interface {
|
||||||
|
Resolve(path string) string
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseCert(certificate, privateKey string, path Path) (tls.Certificate, error) {
|
||||||
if certificate == "" && privateKey == "" {
|
if certificate == "" && privateKey == "" {
|
||||||
return newRandomTLSKeyPair()
|
return newRandomTLSKeyPair()
|
||||||
}
|
}
|
||||||
@@ -19,6 +23,8 @@ func ParseCert(certificate, privateKey string) (tls.Certificate, error) {
|
|||||||
return cert, nil
|
return cert, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
certificate = path.Resolve(certificate)
|
||||||
|
privateKey = path.Resolve(privateKey)
|
||||||
cert, loadErr := tls.LoadX509KeyPair(certificate, privateKey)
|
cert, loadErr := tls.LoadX509KeyPair(certificate, privateKey)
|
||||||
if loadErr != nil {
|
if loadErr != nil {
|
||||||
return tls.Certificate{}, fmt.Errorf("parse certificate failed, maybe format error:%s, or path error: %s", painTextErr.Error(), loadErr.Error())
|
return tls.Certificate{}, fmt.Errorf("parse certificate failed, maybe format error:%s, or path error: %s", painTextErr.Error(), loadErr.Error())
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/atomic"
|
"github.com/metacubex/mihomo/common/atomic"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/samber/lo"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -15,7 +16,7 @@ func sleepAndSend[T any](ctx context.Context, delay int, input T) func() (T, err
|
|||||||
case <-timer.C:
|
case <-timer.C:
|
||||||
return input, nil
|
return input, nil
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return getZero[T](), ctx.Err()
|
return lo.Empty[T](), ctx.Err()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -35,11 +36,6 @@ func TestPicker_Timeout(t *testing.T) {
|
|||||||
picker.Go(sleepAndSend(ctx, 20, 1))
|
picker.Go(sleepAndSend(ctx, 20, 1))
|
||||||
|
|
||||||
number := picker.Wait()
|
number := picker.Wait()
|
||||||
assert.Equal(t, number, getZero[int]())
|
assert.Equal(t, number, lo.Empty[int]())
|
||||||
assert.NotNil(t, picker.Error())
|
assert.NotNil(t, picker.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
func getZero[T any]() T {
|
|
||||||
var result T
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -12,22 +12,28 @@ var defaultAllocator = NewAllocator()
|
|||||||
|
|
||||||
// Allocator for incoming frames, optimized to prevent overwriting after zeroing
|
// Allocator for incoming frames, optimized to prevent overwriting after zeroing
|
||||||
type Allocator struct {
|
type Allocator struct {
|
||||||
buffers []sync.Pool
|
buffers [11]sync.Pool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewAllocator initiates a []byte allocator for frames less than 65536 bytes,
|
// NewAllocator initiates a []byte allocator for frames less than 65536 bytes,
|
||||||
// the waste(memory fragmentation) of space allocation is guaranteed to be
|
// the waste(memory fragmentation) of space allocation is guaranteed to be
|
||||||
// no more than 50%.
|
// no more than 50%.
|
||||||
func NewAllocator() *Allocator {
|
func NewAllocator() *Allocator {
|
||||||
alloc := new(Allocator)
|
return &Allocator{
|
||||||
alloc.buffers = make([]sync.Pool, 17) // 1B -> 64K
|
buffers: [...]sync.Pool{ // 64B -> 64K
|
||||||
for k := range alloc.buffers {
|
{New: func() any { return new([1 << 6]byte) }},
|
||||||
i := k
|
{New: func() any { return new([1 << 7]byte) }},
|
||||||
alloc.buffers[k].New = func() any {
|
{New: func() any { return new([1 << 8]byte) }},
|
||||||
return make([]byte, 1<<uint32(i))
|
{New: func() any { return new([1 << 9]byte) }},
|
||||||
}
|
{New: func() any { return new([1 << 10]byte) }},
|
||||||
|
{New: func() any { return new([1 << 11]byte) }},
|
||||||
|
{New: func() any { return new([1 << 12]byte) }},
|
||||||
|
{New: func() any { return new([1 << 13]byte) }},
|
||||||
|
{New: func() any { return new([1 << 14]byte) }},
|
||||||
|
{New: func() any { return new([1 << 15]byte) }},
|
||||||
|
{New: func() any { return new([1 << 16]byte) }},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
return alloc
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get a []byte from pool with most appropriate cap
|
// Get a []byte from pool with most appropriate cap
|
||||||
@@ -40,12 +46,42 @@ func (alloc *Allocator) Get(size int) []byte {
|
|||||||
case size > 65536:
|
case size > 65536:
|
||||||
return make([]byte, size)
|
return make([]byte, size)
|
||||||
default:
|
default:
|
||||||
bits := msb(size)
|
var index uint16
|
||||||
if size == 1<<bits {
|
if size > 64 {
|
||||||
return alloc.buffers[bits].Get().([]byte)[:size]
|
index = msb(size)
|
||||||
|
if size != 1<<index {
|
||||||
|
index += 1
|
||||||
|
}
|
||||||
|
index -= 6
|
||||||
}
|
}
|
||||||
|
|
||||||
return alloc.buffers[bits+1].Get().([]byte)[:size]
|
buffer := alloc.buffers[index].Get()
|
||||||
|
switch index {
|
||||||
|
case 0:
|
||||||
|
return buffer.(*[1 << 6]byte)[:size]
|
||||||
|
case 1:
|
||||||
|
return buffer.(*[1 << 7]byte)[:size]
|
||||||
|
case 2:
|
||||||
|
return buffer.(*[1 << 8]byte)[:size]
|
||||||
|
case 3:
|
||||||
|
return buffer.(*[1 << 9]byte)[:size]
|
||||||
|
case 4:
|
||||||
|
return buffer.(*[1 << 10]byte)[:size]
|
||||||
|
case 5:
|
||||||
|
return buffer.(*[1 << 11]byte)[:size]
|
||||||
|
case 6:
|
||||||
|
return buffer.(*[1 << 12]byte)[:size]
|
||||||
|
case 7:
|
||||||
|
return buffer.(*[1 << 13]byte)[:size]
|
||||||
|
case 8:
|
||||||
|
return buffer.(*[1 << 14]byte)[:size]
|
||||||
|
case 9:
|
||||||
|
return buffer.(*[1 << 15]byte)[:size]
|
||||||
|
case 10:
|
||||||
|
return buffer.(*[1 << 16]byte)[:size]
|
||||||
|
default:
|
||||||
|
panic("invalid pool index")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,15 +91,45 @@ func (alloc *Allocator) Put(buf []byte) error {
|
|||||||
if cap(buf) == 0 || cap(buf) > 65536 {
|
if cap(buf) == 0 || cap(buf) > 65536 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
bits := msb(cap(buf))
|
bits := msb(cap(buf))
|
||||||
if cap(buf) != 1<<bits {
|
if cap(buf) != 1<<bits {
|
||||||
return errors.New("allocator Put() incorrect buffer size")
|
return errors.New("allocator Put() incorrect buffer size")
|
||||||
}
|
}
|
||||||
|
if cap(buf) < 1<<6 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
bits -= 6
|
||||||
|
buf = buf[:cap(buf)]
|
||||||
|
|
||||||
//nolint
|
//nolint
|
||||||
//lint:ignore SA6002 ignore temporarily
|
//lint:ignore SA6002 ignore temporarily
|
||||||
alloc.buffers[bits].Put(buf)
|
switch bits {
|
||||||
|
case 0:
|
||||||
|
alloc.buffers[bits].Put((*[1 << 6]byte)(buf))
|
||||||
|
case 1:
|
||||||
|
alloc.buffers[bits].Put((*[1 << 7]byte)(buf))
|
||||||
|
case 2:
|
||||||
|
alloc.buffers[bits].Put((*[1 << 8]byte)(buf))
|
||||||
|
case 3:
|
||||||
|
alloc.buffers[bits].Put((*[1 << 9]byte)(buf))
|
||||||
|
case 4:
|
||||||
|
alloc.buffers[bits].Put((*[1 << 10]byte)(buf))
|
||||||
|
case 5:
|
||||||
|
alloc.buffers[bits].Put((*[1 << 11]byte)(buf))
|
||||||
|
case 6:
|
||||||
|
alloc.buffers[bits].Put((*[1 << 12]byte)(buf))
|
||||||
|
case 7:
|
||||||
|
alloc.buffers[bits].Put((*[1 << 13]byte)(buf))
|
||||||
|
case 8:
|
||||||
|
alloc.buffers[bits].Put((*[1 << 14]byte)(buf))
|
||||||
|
case 9:
|
||||||
|
alloc.buffers[bits].Put((*[1 << 15]byte)(buf))
|
||||||
|
case 10:
|
||||||
|
alloc.buffers[bits].Put((*[1 << 16]byte)(buf))
|
||||||
|
default:
|
||||||
|
panic("invalid pool index")
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ func TestAllocGet(t *testing.T) {
|
|||||||
assert.Equal(t, 1, len(alloc.Get(1)))
|
assert.Equal(t, 1, len(alloc.Get(1)))
|
||||||
assert.Equal(t, 2, len(alloc.Get(2)))
|
assert.Equal(t, 2, len(alloc.Get(2)))
|
||||||
assert.Equal(t, 3, len(alloc.Get(3)))
|
assert.Equal(t, 3, len(alloc.Get(3)))
|
||||||
assert.Equal(t, 4, cap(alloc.Get(3)))
|
assert.Equal(t, 64, cap(alloc.Get(3)))
|
||||||
assert.Equal(t, 4, cap(alloc.Get(4)))
|
assert.Equal(t, 64, cap(alloc.Get(4)))
|
||||||
assert.Equal(t, 1023, len(alloc.Get(1023)))
|
assert.Equal(t, 1023, len(alloc.Get(1023)))
|
||||||
assert.Equal(t, 1024, cap(alloc.Get(1023)))
|
assert.Equal(t, 1024, cap(alloc.Get(1023)))
|
||||||
assert.Equal(t, 1024, len(alloc.Get(1024)))
|
assert.Equal(t, 1024, len(alloc.Get(1024)))
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package queue
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/samber/lo"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Queue is a simple concurrent safe queue
|
// Queue is a simple concurrent safe queue
|
||||||
@@ -24,7 +26,7 @@ func (q *Queue[T]) Put(items ...T) {
|
|||||||
// Pop returns the head of items.
|
// Pop returns the head of items.
|
||||||
func (q *Queue[T]) Pop() T {
|
func (q *Queue[T]) Pop() T {
|
||||||
if len(q.items) == 0 {
|
if len(q.items) == 0 {
|
||||||
return GetZero[T]()
|
return lo.Empty[T]()
|
||||||
}
|
}
|
||||||
|
|
||||||
q.lock.Lock()
|
q.lock.Lock()
|
||||||
@@ -37,7 +39,7 @@ func (q *Queue[T]) Pop() T {
|
|||||||
// Last returns the last of item.
|
// Last returns the last of item.
|
||||||
func (q *Queue[T]) Last() T {
|
func (q *Queue[T]) Last() T {
|
||||||
if len(q.items) == 0 {
|
if len(q.items) == 0 {
|
||||||
return GetZero[T]()
|
return lo.Empty[T]()
|
||||||
}
|
}
|
||||||
|
|
||||||
q.lock.RLock()
|
q.lock.RLock()
|
||||||
@@ -69,8 +71,3 @@ func New[T any](hint int64) *Queue[T] {
|
|||||||
items: make([]T, 0, hint),
|
items: make([]T, 0, hint),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetZero[T any]() T {
|
|
||||||
var result T
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/atomic"
|
"github.com/metacubex/mihomo/common/atomic"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|||||||
8
common/utils/manipulation.go
Normal file
8
common/utils/manipulation.go
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import "github.com/samber/lo"
|
||||||
|
|
||||||
|
func EmptyOr[T comparable](v T, def T) T {
|
||||||
|
ret, _ := lo.Coalesce(v, def)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
@@ -75,3 +75,26 @@ func (ranges IntRanges[T]) Check(status T) bool {
|
|||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ranges IntRanges[T]) ToString() string {
|
||||||
|
if len(ranges) == 0 {
|
||||||
|
return "*"
|
||||||
|
}
|
||||||
|
|
||||||
|
terms := make([]string, len(ranges))
|
||||||
|
for i, r := range ranges {
|
||||||
|
start := r.Start()
|
||||||
|
end := r.End()
|
||||||
|
|
||||||
|
var term string
|
||||||
|
if start == end {
|
||||||
|
term = strconv.Itoa(int(start))
|
||||||
|
} else {
|
||||||
|
term = strconv.Itoa(int(start)) + "-" + strconv.Itoa(int(end))
|
||||||
|
}
|
||||||
|
|
||||||
|
terms[i] = term
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Join(terms, "/")
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,9 +1,5 @@
|
|||||||
package auth
|
package auth
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Authenticator interface {
|
type Authenticator interface {
|
||||||
Verify(user string, pass string) bool
|
Verify(user string, pass string) bool
|
||||||
Users() []string
|
Users() []string
|
||||||
@@ -15,12 +11,12 @@ type AuthUser struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type inMemoryAuthenticator struct {
|
type inMemoryAuthenticator struct {
|
||||||
storage *sync.Map
|
storage map[string]string
|
||||||
usernames []string
|
usernames []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (au *inMemoryAuthenticator) Verify(user string, pass string) bool {
|
func (au *inMemoryAuthenticator) Verify(user string, pass string) bool {
|
||||||
realPass, ok := au.storage.Load(user)
|
realPass, ok := au.storage[user]
|
||||||
return ok && realPass == pass
|
return ok && realPass == pass
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,17 +26,13 @@ func NewAuthenticator(users []AuthUser) Authenticator {
|
|||||||
if len(users) == 0 {
|
if len(users) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
au := &inMemoryAuthenticator{
|
||||||
au := &inMemoryAuthenticator{storage: &sync.Map{}}
|
storage: make(map[string]string),
|
||||||
for _, user := range users {
|
usernames: make([]string, 0, len(users)),
|
||||||
au.storage.Store(user.User, user.Pass)
|
}
|
||||||
|
for _, user := range users {
|
||||||
|
au.storage[user.User] = user.Pass
|
||||||
|
au.usernames = append(au.usernames, user.User)
|
||||||
}
|
}
|
||||||
usernames := make([]string, 0, len(users))
|
|
||||||
au.storage.Range(func(key, value any) bool {
|
|
||||||
usernames = append(usernames, key.(string))
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
au.usernames = usernames
|
|
||||||
|
|
||||||
return au
|
return au
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package tls
|
package ca
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -8,12 +8,13 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
var trustCerts []*x509.Certificate
|
var trustCerts []*x509.Certificate
|
||||||
var certPool *x509.CertPool
|
var globalCertPool *x509.CertPool
|
||||||
var mutex sync.RWMutex
|
var mutex sync.RWMutex
|
||||||
var errNotMatch = errors.New("certificate fingerprints do not match")
|
var errNotMatch = errors.New("certificate fingerprints do not match")
|
||||||
|
|
||||||
@@ -33,12 +34,12 @@ func AddCertificate(certificate string) error {
|
|||||||
|
|
||||||
func initializeCertPool() {
|
func initializeCertPool() {
|
||||||
var err error
|
var err error
|
||||||
certPool, err = x509.SystemCertPool()
|
globalCertPool, err = x509.SystemCertPool()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
certPool = x509.NewCertPool()
|
globalCertPool = x509.NewCertPool()
|
||||||
}
|
}
|
||||||
for _, cert := range trustCerts {
|
for _, cert := range trustCerts {
|
||||||
certPool.AddCert(cert)
|
globalCertPool.AddCert(cert)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,15 +54,15 @@ func getCertPool() *x509.CertPool {
|
|||||||
if len(trustCerts) == 0 {
|
if len(trustCerts) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if certPool == nil {
|
if globalCertPool == nil {
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
defer mutex.Unlock()
|
defer mutex.Unlock()
|
||||||
if certPool != nil {
|
if globalCertPool != nil {
|
||||||
return certPool
|
return globalCertPool
|
||||||
}
|
}
|
||||||
initializeCertPool()
|
initializeCertPool()
|
||||||
}
|
}
|
||||||
return certPool
|
return globalCertPool
|
||||||
}
|
}
|
||||||
|
|
||||||
func verifyFingerprint(fingerprint *[32]byte) func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
|
func verifyFingerprint(fingerprint *[32]byte) func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
|
||||||
@@ -94,29 +95,49 @@ func convertFingerprint(fingerprint string) (*[32]byte, error) {
|
|||||||
return (*[32]byte)(fpByte), nil
|
return (*[32]byte)(fpByte), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDefaultTLSConfig() *tls.Config {
|
// GetTLSConfig specified fingerprint, customCA and customCAString
|
||||||
return GetGlobalTLSConfig(nil)
|
func GetTLSConfig(tlsConfig *tls.Config, fingerprint string, customCA string, customCAString string) (*tls.Config, error) {
|
||||||
|
if tlsConfig == nil {
|
||||||
|
tlsConfig = &tls.Config{}
|
||||||
|
}
|
||||||
|
var certificate []byte
|
||||||
|
var err error
|
||||||
|
if len(customCA) > 0 {
|
||||||
|
certificate, err = os.ReadFile(customCA)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("load ca error: %w", err)
|
||||||
|
}
|
||||||
|
} else if customCAString != "" {
|
||||||
|
certificate = []byte(customCAString)
|
||||||
|
}
|
||||||
|
if len(certificate) > 0 {
|
||||||
|
certPool := x509.NewCertPool()
|
||||||
|
if !certPool.AppendCertsFromPEM(certificate) {
|
||||||
|
return nil, fmt.Errorf("failed to parse certificate:\n\n %s", certificate)
|
||||||
|
}
|
||||||
|
tlsConfig.RootCAs = certPool
|
||||||
|
} else {
|
||||||
|
tlsConfig.RootCAs = getCertPool()
|
||||||
|
}
|
||||||
|
if len(fingerprint) > 0 {
|
||||||
|
var fingerprintBytes *[32]byte
|
||||||
|
fingerprintBytes, err = convertFingerprint(fingerprint)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
tlsConfig = GetGlobalTLSConfig(tlsConfig)
|
||||||
|
tlsConfig.VerifyPeerCertificate = verifyFingerprint(fingerprintBytes)
|
||||||
|
tlsConfig.InsecureSkipVerify = true
|
||||||
|
}
|
||||||
|
return tlsConfig, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSpecifiedFingerprintTLSConfig specified fingerprint
|
// GetSpecifiedFingerprintTLSConfig specified fingerprint
|
||||||
func GetSpecifiedFingerprintTLSConfig(tlsConfig *tls.Config, fingerprint string) (*tls.Config, error) {
|
func GetSpecifiedFingerprintTLSConfig(tlsConfig *tls.Config, fingerprint string) (*tls.Config, error) {
|
||||||
if fingerprintBytes, err := convertFingerprint(fingerprint); err != nil {
|
return GetTLSConfig(tlsConfig, fingerprint, "", "")
|
||||||
return nil, err
|
|
||||||
} else {
|
|
||||||
tlsConfig = GetGlobalTLSConfig(tlsConfig)
|
|
||||||
tlsConfig.VerifyPeerCertificate = verifyFingerprint(fingerprintBytes)
|
|
||||||
tlsConfig.InsecureSkipVerify = true
|
|
||||||
return tlsConfig, nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetGlobalTLSConfig(tlsConfig *tls.Config) *tls.Config {
|
func GetGlobalTLSConfig(tlsConfig *tls.Config) *tls.Config {
|
||||||
certPool := getCertPool()
|
tlsConfig, _ = GetTLSConfig(tlsConfig, "", "", "")
|
||||||
if tlsConfig == nil {
|
|
||||||
return &tls.Config{
|
|
||||||
RootCAs: certPool,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tlsConfig.RootCAs = certPool
|
|
||||||
return tlsConfig
|
return tlsConfig
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/component/dialer"
|
"github.com/metacubex/mihomo/component/dialer"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ListenDHCPClient(ctx context.Context, ifaceName string) (net.PacketConn, error) {
|
func ListenDHCPClient(ctx context.Context, ifaceName string) (net.PacketConn, error) {
|
||||||
@@ -14,5 +14,15 @@ func ListenDHCPClient(ctx context.Context, ifaceName string) (net.PacketConn, er
|
|||||||
listenAddr = "255.255.255.255:68"
|
listenAddr = "255.255.255.255:68"
|
||||||
}
|
}
|
||||||
|
|
||||||
return dialer.ListenPacket(ctx, "udp4", listenAddr, dialer.WithInterface(ifaceName), dialer.WithAddrReuse(true))
|
options := []dialer.Option{
|
||||||
|
dialer.WithInterface(ifaceName),
|
||||||
|
dialer.WithAddrReuse(true),
|
||||||
|
}
|
||||||
|
|
||||||
|
// fallback bind on windows, because syscall bind can not receive broadcast
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
options = append(options, dialer.WithFallbackBind(true))
|
||||||
|
}
|
||||||
|
|
||||||
|
return dialer.ListenPacket(ctx, "udp4", listenAddr, options...)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/common/nnip"
|
"github.com/metacubex/mihomo/common/nnip"
|
||||||
"github.com/Dreamacro/clash/component/iface"
|
"github.com/metacubex/mihomo/component/iface"
|
||||||
|
|
||||||
"github.com/insomniacslk/dhcp/dhcpv4"
|
"github.com/insomniacslk/dhcp/dhcpv4"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -3,9 +3,10 @@ package dialer
|
|||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/component/iface"
|
"github.com/metacubex/mihomo/component/iface"
|
||||||
)
|
)
|
||||||
|
|
||||||
func LookupLocalAddrFromIfaceName(ifaceName string, network string, destination netip.Addr, port int) (net.Addr, error) {
|
func LookupLocalAddrFromIfaceName(ifaceName string, network string, destination netip.Addr, port int) (net.Addr, error) {
|
||||||
@@ -14,7 +15,7 @@ func LookupLocalAddrFromIfaceName(ifaceName string, network string, destination
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var addr *netip.Prefix
|
var addr netip.Prefix
|
||||||
switch network {
|
switch network {
|
||||||
case "udp4", "tcp4":
|
case "udp4", "tcp4":
|
||||||
addr, err = ifaceObj.PickIPv4Addr(destination)
|
addr, err = ifaceObj.PickIPv4Addr(destination)
|
||||||
@@ -49,3 +50,52 @@ func LookupLocalAddrFromIfaceName(ifaceName string, network string, destination
|
|||||||
|
|
||||||
return nil, iface.ErrAddrNotFound
|
return nil, iface.ErrAddrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func fallbackBindIfaceToDialer(ifaceName string, dialer *net.Dialer, network string, destination netip.Addr) error {
|
||||||
|
if !destination.IsGlobalUnicast() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
local := uint64(0)
|
||||||
|
if dialer.LocalAddr != nil {
|
||||||
|
_, port, err := net.SplitHostPort(dialer.LocalAddr.String())
|
||||||
|
if err == nil {
|
||||||
|
local, _ = strconv.ParseUint(port, 10, 16)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
addr, err := LookupLocalAddrFromIfaceName(ifaceName, network, destination, int(local))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
dialer.LocalAddr = addr
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func fallbackBindIfaceToListenConfig(ifaceName string, _ *net.ListenConfig, network, address string) (string, error) {
|
||||||
|
_, port, err := net.SplitHostPort(address)
|
||||||
|
if err != nil {
|
||||||
|
port = "0"
|
||||||
|
}
|
||||||
|
|
||||||
|
local, _ := strconv.ParseUint(port, 10, 16)
|
||||||
|
|
||||||
|
addr, err := LookupLocalAddrFromIfaceName(ifaceName, network, netip.Addr{}, int(local))
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return addr.String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func fallbackParseNetwork(network string, addr netip.Addr) string {
|
||||||
|
// fix fallbackBindIfaceToListenConfig() force bind to an ipv4 address
|
||||||
|
if !strings.HasSuffix(network, "4") &&
|
||||||
|
!strings.HasSuffix(network, "6") &&
|
||||||
|
addr.Unmap().Is6() {
|
||||||
|
network += "6"
|
||||||
|
}
|
||||||
|
return network
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"net/netip"
|
"net/netip"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/component/iface"
|
"github.com/metacubex/mihomo/component/iface"
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -5,55 +5,16 @@ package dialer
|
|||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func bindIfaceToDialer(ifaceName string, dialer *net.Dialer, network string, destination netip.Addr) error {
|
func bindIfaceToDialer(ifaceName string, dialer *net.Dialer, network string, destination netip.Addr) error {
|
||||||
if !destination.IsGlobalUnicast() {
|
return fallbackBindIfaceToDialer(ifaceName, dialer, network, destination)
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
local := uint64(0)
|
|
||||||
if dialer.LocalAddr != nil {
|
|
||||||
_, port, err := net.SplitHostPort(dialer.LocalAddr.String())
|
|
||||||
if err == nil {
|
|
||||||
local, _ = strconv.ParseUint(port, 10, 16)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
addr, err := LookupLocalAddrFromIfaceName(ifaceName, network, destination, int(local))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
dialer.LocalAddr = addr
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func bindIfaceToListenConfig(ifaceName string, _ *net.ListenConfig, network, address string) (string, error) {
|
func bindIfaceToListenConfig(ifaceName string, lc *net.ListenConfig, network, address string) (string, error) {
|
||||||
_, port, err := net.SplitHostPort(address)
|
return fallbackBindIfaceToListenConfig(ifaceName, lc, network, address)
|
||||||
if err != nil {
|
|
||||||
port = "0"
|
|
||||||
}
|
|
||||||
|
|
||||||
local, _ := strconv.ParseUint(port, 10, 16)
|
|
||||||
|
|
||||||
addr, err := LookupLocalAddrFromIfaceName(ifaceName, network, netip.Addr{}, int(local))
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return addr.String(), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseNetwork(network string, addr netip.Addr) string {
|
func ParseNetwork(network string, addr netip.Addr) string {
|
||||||
// fix bindIfaceToListenConfig() force bind to an ipv4 address
|
return fallbackParseNetwork(network, addr)
|
||||||
if !strings.HasSuffix(network, "4") &&
|
|
||||||
!strings.HasSuffix(network, "6") &&
|
|
||||||
addr.Unmap().Is6() {
|
|
||||||
network += "6"
|
|
||||||
}
|
|
||||||
return network
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/component/iface"
|
"github.com/metacubex/mihomo/component/iface"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user