Compare commits
57 Commits
Author | SHA1 | Date | |
---|---|---|---|
3e68faecb2 | |||
eb778ad6e2 | |||
0caa8e05a3 | |||
1d0d5667fd | |||
0724d4ea52 | |||
5d34cba681 | |||
4082a2c700 | |||
fcb46e7706 | |||
834baa9e27 | |||
af13acc171 | |||
f2dbabeaa0 | |||
f4c51cdb0e | |||
a5f2bd3152 | |||
613a0c36dd | |||
8ec025b56a | |||
2a2e61652f | |||
ad25a89dd2 | |||
26618901d4 | |||
ebe1cee6dc | |||
fc4f119049 | |||
2b87b907ae | |||
35e572406b | |||
228d674d93 | |||
752944d381 | |||
63308472ad | |||
0208e32933 | |||
410b272b50 | |||
ea424a7694 | |||
36f4ceafa1 | |||
674f4be24d | |||
63c967b2b3 | |||
a1a58c31ae | |||
295d649624 | |||
7347c28f75 | |||
8389150318 | |||
7357d2d0c2 | |||
d540a0d29f | |||
d55e1b664b | |||
88c7ccf749 | |||
3cacfb8a7f | |||
0eef9bbf5d | |||
39b45513af | |||
283e4e1f4f | |||
bd0a1ae1a7 | |||
018a6ba041 | |||
9c2ace1f91 | |||
17224a32cb | |||
05ab653103 | |||
c2c8f82f96 | |||
330a3391e3 | |||
79e5338113 | |||
bc4ca2818b | |||
46b8e2eec1 | |||
cd9f496fc9 | |||
ef301af04f | |||
6ffbb7c89e | |||
961250f998 |
10
.travis.yml
10
.travis.yml
@ -1,7 +1,7 @@
|
|||||||
language: go
|
language: go
|
||||||
sudo: false
|
sudo: false
|
||||||
go:
|
go:
|
||||||
- "1.10"
|
- "1.11"
|
||||||
before_install:
|
before_install:
|
||||||
- go get -u github.com/golang/dep/cmd/dep
|
- go get -u github.com/golang/dep/cmd/dep
|
||||||
install:
|
install:
|
||||||
@ -11,12 +11,8 @@ env:
|
|||||||
- NAME=clash
|
- NAME=clash
|
||||||
- BINDIR=bin
|
- BINDIR=bin
|
||||||
script:
|
script:
|
||||||
- go test
|
- go test ./...
|
||||||
- GOARCH=amd64 GOOS=linux go build -o $BINDIR/$NAME-linux
|
before_deploy: make -j releases
|
||||||
- GOARCH=amd64 GOOS=darwin go build -o $BINDIR/$NAME-macos
|
|
||||||
- chmod +x $BINDIR/$NAME-*
|
|
||||||
- gzip $BINDIR/$NAME-linux
|
|
||||||
- gzip $BINDIR/$NAME-macos
|
|
||||||
deploy:
|
deploy:
|
||||||
provider: releases
|
provider: releases
|
||||||
prerelease: true
|
prerelease: true
|
||||||
|
19
Dockerfile
Normal file
19
Dockerfile
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
FROM golang:latest as builder
|
||||||
|
RUN wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz -O /tmp/GeoLite2-Country.tar.gz && \
|
||||||
|
tar zxvf /tmp/GeoLite2-Country.tar.gz -C /tmp && \
|
||||||
|
cp /tmp/GeoLite2-Country_*/GeoLite2-Country.mmdb /Country.mmdb
|
||||||
|
RUN curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh && \
|
||||||
|
mkdir -p /go/src/github.com/Dreamacro/clash
|
||||||
|
WORKDIR /go/src/github.com/Dreamacro/clash
|
||||||
|
COPY . /go/src/github.com/Dreamacro/clash
|
||||||
|
RUN dep ensure && \
|
||||||
|
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags '-w -s' -o /clash && \
|
||||||
|
chmod +x /clash
|
||||||
|
|
||||||
|
FROM alpine:latest
|
||||||
|
RUN apk --no-cache add ca-certificates && \
|
||||||
|
mkdir -p /root/.config/clash
|
||||||
|
COPY --from=builder /Country.mmdb /root/.config/clash/
|
||||||
|
COPY --from=builder /clash .
|
||||||
|
EXPOSE 7890 7891
|
||||||
|
ENTRYPOINT ["/clash"]
|
147
Gopkg.lock
generated
147
Gopkg.lock
generated
@ -2,83 +2,148 @@
|
|||||||
|
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
branch = "master"
|
digest = "1:d21e998147c1c8cd727bd34148f373d3faa46fd8669c389b53c4eaabbe4eb2b3"
|
||||||
name = "github.com/Yawning/chacha20"
|
name = "github.com/Dreamacro/go-shadowsocks2"
|
||||||
packages = ["."]
|
|
||||||
revision = "e3b1f968fc6397b51d963fee8ec8711a47bc0ce8"
|
|
||||||
|
|
||||||
[[projects]]
|
|
||||||
name = "github.com/eapache/queue"
|
|
||||||
packages = ["."]
|
|
||||||
revision = "44cc805cf13205b55f69e14bcb69867d1ae92f98"
|
|
||||||
version = "v1.1.0"
|
|
||||||
|
|
||||||
[[projects]]
|
|
||||||
name = "github.com/oschwald/geoip2-golang"
|
|
||||||
packages = ["."]
|
|
||||||
revision = "7118115686e16b77967cdbf55d1b944fe14ad312"
|
|
||||||
version = "v1.2.1"
|
|
||||||
|
|
||||||
[[projects]]
|
|
||||||
name = "github.com/oschwald/maxminddb-golang"
|
|
||||||
packages = ["."]
|
|
||||||
revision = "c5bec84d1963260297932a1b7a1753c8420717a7"
|
|
||||||
version = "v1.3.0"
|
|
||||||
|
|
||||||
[[projects]]
|
|
||||||
name = "github.com/riobard/go-shadowsocks2"
|
|
||||||
packages = [
|
packages = [
|
||||||
"core",
|
"core",
|
||||||
"shadowaead",
|
"shadowaead",
|
||||||
"shadowstream",
|
"shadowstream",
|
||||||
"socks"
|
"socks",
|
||||||
]
|
]
|
||||||
revision = "8346403248229fc7e10d7a259de8e9352a9d8830"
|
pruneopts = "UT"
|
||||||
version = "v0.1.0"
|
revision = "1c1fd6c192eb76261ea3ccd80e3b141b25f20db4"
|
||||||
|
version = "v0.1.1"
|
||||||
[[projects]]
|
|
||||||
name = "github.com/sirupsen/logrus"
|
|
||||||
packages = ["."]
|
|
||||||
revision = "c155da19408a8799da419ed3eeb0cb5db0ad5dbc"
|
|
||||||
version = "v1.0.5"
|
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
branch = "master"
|
branch = "master"
|
||||||
|
digest = "1:8fa55a6e302771a90a86ceae1ca3c0df4ef15d21092198e8313f61dde9eea963"
|
||||||
|
name = "github.com/Yawning/chacha20"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "e3b1f968fc6397b51d963fee8ec8711a47bc0ce8"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:444b82bfe35c83bbcaf84e310fb81a1f9ece03edfed586483c869e2c046aef69"
|
||||||
|
name = "github.com/eapache/queue"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "44cc805cf13205b55f69e14bcb69867d1ae92f98"
|
||||||
|
version = "v1.1.0"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:b9914f85d95a0968bafd1be1908ba29e2eafafd88d6fd13696be42bf5368c380"
|
||||||
|
name = "github.com/go-chi/chi"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "b5294d10673813fac8558e7f47242bc9e61b4c25"
|
||||||
|
version = "v3.3.3"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:dfa416a1bb8139f30832543340f972f65c0db9932034cb6a1b42c5ac615a3fb8"
|
||||||
|
name = "github.com/go-chi/cors"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "dba6525398619dead495962a916728e7ee2ca322"
|
||||||
|
version = "v1.0.0"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:54d7b4b9ab2bb2bae35b55eea900bc8fe15cba05a95fc78bf7fd7b82a9a07afa"
|
||||||
|
name = "github.com/go-chi/render"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "3215478343fbc559bd3fc08f7031bb134d6bdad5"
|
||||||
|
version = "v1.0.1"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:ce579162ae1341f3e5ab30c0dce767f28b1eb6a81359aad01723f1ba6b4becdf"
|
||||||
|
name = "github.com/gofrs/uuid"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "370558f003bfe29580cd0f698d8640daccdcc45c"
|
||||||
|
version = "v3.1.1"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:2e8bdbc8a11d716dab1bf66d326285d7e5c92fa4c996c1574ba1153e57534b85"
|
||||||
|
name = "github.com/oschwald/geoip2-golang"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "7118115686e16b77967cdbf55d1b944fe14ad312"
|
||||||
|
version = "v1.2.1"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:07e8589503b7ec22430dae6eed6f2c17e4249ab245574f4fd0ba8fc9c597d138"
|
||||||
|
name = "github.com/oschwald/maxminddb-golang"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "c5bec84d1963260297932a1b7a1753c8420717a7"
|
||||||
|
version = "v1.3.0"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:d867dfa6751c8d7a435821ad3b736310c2ed68945d05b50fb9d23aee0540c8cc"
|
||||||
|
name = "github.com/sirupsen/logrus"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "3e01752db0189b9157070a0e1668a620f9a85da2"
|
||||||
|
version = "v1.0.6"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
branch = "master"
|
||||||
|
digest = "1:d33888518d56c3f0cc9009594f56be4faf33ffff358fe10ff8e7e8cccf0e6617"
|
||||||
name = "golang.org/x/crypto"
|
name = "golang.org/x/crypto"
|
||||||
packages = [
|
packages = [
|
||||||
"chacha20poly1305",
|
"chacha20poly1305",
|
||||||
"hkdf",
|
"hkdf",
|
||||||
"internal/chacha20",
|
"internal/chacha20",
|
||||||
|
"internal/subtle",
|
||||||
"poly1305",
|
"poly1305",
|
||||||
"ssh/terminal"
|
"ssh/terminal",
|
||||||
]
|
]
|
||||||
revision = "8ac0e0d97ce45cd83d1d7243c060cb8461dda5e9"
|
pruneopts = "UT"
|
||||||
|
revision = "0e37d006457bf46f9e6692014ba72ef82c33022c"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
branch = "master"
|
branch = "master"
|
||||||
|
digest = "1:ddbafa32d1899456edbf7a64aec7afe5aa287b840e6a12b996f8a8425c1d9a6a"
|
||||||
name = "golang.org/x/sys"
|
name = "golang.org/x/sys"
|
||||||
packages = [
|
packages = [
|
||||||
"cpu",
|
"cpu",
|
||||||
"unix",
|
"unix",
|
||||||
"windows"
|
"windows",
|
||||||
]
|
]
|
||||||
revision = "9527bec2660bd847c050fda93a0f0c6dee0800bb"
|
pruneopts = "UT"
|
||||||
|
revision = "d641721ec2dead6fe5ca284096fe4b1fcd49e427"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
|
digest = "1:975a4480c40f2d0b95e1f83d3ec1aa29a2774e80179e08a9a4ba2aab86721b23"
|
||||||
name = "gopkg.in/eapache/channels.v1"
|
name = "gopkg.in/eapache/channels.v1"
|
||||||
packages = ["."]
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
revision = "47238d5aae8c0fefd518ef2bee46290909cf8263"
|
revision = "47238d5aae8c0fefd518ef2bee46290909cf8263"
|
||||||
version = "v1.1.0"
|
version = "v1.1.0"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
|
digest = "1:5abd6a22805b1919f6a6bca0ae58b13cef1f3412812f38569978f43ef02743d4"
|
||||||
name = "gopkg.in/ini.v1"
|
name = "gopkg.in/ini.v1"
|
||||||
packages = ["."]
|
packages = ["."]
|
||||||
revision = "06f5f3d67269ccec1fe5fe4134ba6e982984f7f5"
|
pruneopts = "UT"
|
||||||
version = "v1.37.0"
|
revision = "5cf292cae48347c2490ac1a58fe36735fb78df7e"
|
||||||
|
version = "v1.38.2"
|
||||||
|
|
||||||
[solve-meta]
|
[solve-meta]
|
||||||
analyzer-name = "dep"
|
analyzer-name = "dep"
|
||||||
analyzer-version = 1
|
analyzer-version = 1
|
||||||
inputs-digest = "4297c505508c6cdd8c94fd4ef29bc6940c65fea81e125fcf871a316b6e671a71"
|
input-imports = [
|
||||||
|
"github.com/Dreamacro/go-shadowsocks2/core",
|
||||||
|
"github.com/Dreamacro/go-shadowsocks2/socks",
|
||||||
|
"github.com/go-chi/chi",
|
||||||
|
"github.com/go-chi/cors",
|
||||||
|
"github.com/go-chi/render",
|
||||||
|
"github.com/gofrs/uuid",
|
||||||
|
"github.com/oschwald/geoip2-golang",
|
||||||
|
"github.com/sirupsen/logrus",
|
||||||
|
"golang.org/x/crypto/chacha20poly1305",
|
||||||
|
"gopkg.in/eapache/channels.v1",
|
||||||
|
"gopkg.in/ini.v1",
|
||||||
|
]
|
||||||
solver-name = "gps-cdcl"
|
solver-name = "gps-cdcl"
|
||||||
solver-version = 1
|
solver-version = 1
|
||||||
|
32
Gopkg.toml
32
Gopkg.toml
@ -1,6 +1,6 @@
|
|||||||
# Gopkg.toml example
|
# Gopkg.toml example
|
||||||
#
|
#
|
||||||
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
|
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
|
||||||
# for detailed Gopkg.toml documentation.
|
# for detailed Gopkg.toml documentation.
|
||||||
#
|
#
|
||||||
# required = ["github.com/user/thing/cmd/thing"]
|
# required = ["github.com/user/thing/cmd/thing"]
|
||||||
@ -25,17 +25,37 @@
|
|||||||
# unused-packages = true
|
# unused-packages = true
|
||||||
|
|
||||||
|
|
||||||
|
[[constraint]]
|
||||||
|
name = "github.com/Dreamacro/go-shadowsocks2"
|
||||||
|
version = "0.1.1"
|
||||||
|
|
||||||
|
[[constraint]]
|
||||||
|
name = "github.com/go-chi/chi"
|
||||||
|
version = "3.3.3"
|
||||||
|
|
||||||
|
[[constraint]]
|
||||||
|
name = "github.com/go-chi/cors"
|
||||||
|
version = "1.0.0"
|
||||||
|
|
||||||
|
[[constraint]]
|
||||||
|
name = "github.com/go-chi/render"
|
||||||
|
version = "1.0.1"
|
||||||
|
|
||||||
|
[[constraint]]
|
||||||
|
name = "github.com/gofrs/uuid"
|
||||||
|
version = "3.1.1"
|
||||||
|
|
||||||
[[constraint]]
|
[[constraint]]
|
||||||
name = "github.com/oschwald/geoip2-golang"
|
name = "github.com/oschwald/geoip2-golang"
|
||||||
version = "1.2.1"
|
version = "1.2.1"
|
||||||
|
|
||||||
[[constraint]]
|
[[constraint]]
|
||||||
name = "github.com/riobard/go-shadowsocks2"
|
name = "github.com/sirupsen/logrus"
|
||||||
version = "0.1.0"
|
version = "1.0.6"
|
||||||
|
|
||||||
[[constraint]]
|
[[constraint]]
|
||||||
name = "github.com/sirupsen/logrus"
|
branch = "master"
|
||||||
version = "1.0.5"
|
name = "golang.org/x/crypto"
|
||||||
|
|
||||||
[[constraint]]
|
[[constraint]]
|
||||||
name = "gopkg.in/eapache/channels.v1"
|
name = "gopkg.in/eapache/channels.v1"
|
||||||
@ -43,7 +63,7 @@
|
|||||||
|
|
||||||
[[constraint]]
|
[[constraint]]
|
||||||
name = "gopkg.in/ini.v1"
|
name = "gopkg.in/ini.v1"
|
||||||
version = "1.37.0"
|
version = "1.38.2"
|
||||||
|
|
||||||
[prune]
|
[prune]
|
||||||
go-tests = true
|
go-tests = true
|
||||||
|
23
Makefile
Normal file
23
Makefile
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
NAME=clash
|
||||||
|
BINDIR=bin
|
||||||
|
GOBUILD=CGO_ENABLED=0 go build -ldflags '-w -s'
|
||||||
|
|
||||||
|
all: linux macos win64
|
||||||
|
|
||||||
|
linux:
|
||||||
|
GOARCH=amd64 GOOS=linux $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||||
|
|
||||||
|
macos:
|
||||||
|
GOARCH=amd64 GOOS=darwin $(GOBUILD) -o $(BINDIR)/$(NAME)-$@
|
||||||
|
|
||||||
|
win64:
|
||||||
|
GOARCH=amd64 GOOS=windows $(GOBUILD) -o $(BINDIR)/$(NAME)-$@.exe
|
||||||
|
|
||||||
|
releases: linux macos win64
|
||||||
|
chmod +x $(BINDIR)/$(NAME)-*
|
||||||
|
gzip $(BINDIR)/$(NAME)-linux
|
||||||
|
gzip $(BINDIR)/$(NAME)-macos
|
||||||
|
zip -m -j $(BINDIR)/$(NAME)-win64.zip $(BINDIR)/$(NAME)-win64.exe
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm $(BINDIR)/*
|
77
README.md
77
README.md
@ -1,12 +1,36 @@
|
|||||||
# Clash
|
<h1 align="center">
|
||||||
|
<img src="https://github.com/Dreamacro/clash/raw/master/docs/logo.png" alt="Clash" width="200">
|
||||||
|
<br>
|
||||||
|
Clash
|
||||||
|
<br>
|
||||||
|
</h1>
|
||||||
|
|
||||||
A rule based proxy in Go.
|
<h4 align="center">A rule based proxy in Go.</h4>
|
||||||
|
|
||||||
|
<p align="center">
|
||||||
|
<a href="https://travis-ci.org/Dreamacro/clash">
|
||||||
|
<img src="https://img.shields.io/travis/Dreamacro/clash.svg?style=flat-square"
|
||||||
|
alt="Travis-CI">
|
||||||
|
</a>
|
||||||
|
<a href="https://goreportcard.com/report/github.com/Dreamacro/clash">
|
||||||
|
<img src="https://goreportcard.com/badge/github.com/Dreamacro/clash?style=flat-square">
|
||||||
|
</a>
|
||||||
|
<a href="https://github.com/Dreamacro/clash/releases">
|
||||||
|
<img src="https://img.shields.io/github/release/Dreamacro/clash/all.svg?style=flat-square">
|
||||||
|
</a>
|
||||||
|
</p>
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- HTTP/HTTPS and SOCKS proxy
|
- HTTP/HTTPS and SOCKS proxy
|
||||||
- Surge like configuration
|
- Surge like configuration
|
||||||
- GeoIP rule support
|
- GeoIP rule support
|
||||||
|
- Support Vmess/Shadowsocks/Socks5
|
||||||
|
- Support for Netfilter TCP redirect
|
||||||
|
|
||||||
|
## Discussion
|
||||||
|
|
||||||
|
[Telegram Group](https://t.me/clash_discuss)
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
@ -26,12 +50,16 @@ Unfortunately, there is no native elegant way to implement golang's daemon.
|
|||||||
|
|
||||||
So we can use third-party daemon tools like pm2, supervisor, and so on.
|
So we can use third-party daemon tools like pm2, supervisor, and so on.
|
||||||
|
|
||||||
In the case of pm2, we can start the daemon this way:
|
In the case of [pm2](https://github.com/Unitech/pm2), we can start the daemon this way:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
pm2 start clash
|
pm2 start clash
|
||||||
```
|
```
|
||||||
|
|
||||||
|
If you have Docker installed, you can run clash directly using `docker-compose`.
|
||||||
|
|
||||||
|
[Run clash in docker](https://github.com/Dreamacro/clash/wiki/Run-clash-in-docker)
|
||||||
|
|
||||||
## Config
|
## Config
|
||||||
|
|
||||||
Configuration file at `$HOME/.config/clash/config.ini`
|
Configuration file at `$HOME/.config/clash/config.ini`
|
||||||
@ -43,11 +71,35 @@ Below is a simple demo configuration file:
|
|||||||
port = 7890
|
port = 7890
|
||||||
socks-port = 7891
|
socks-port = 7891
|
||||||
|
|
||||||
|
# redir proxy for Linux and macOS
|
||||||
|
redir-port = 7892
|
||||||
|
|
||||||
|
# A RESTful API for clash
|
||||||
|
external-controller = 127.0.0.1:8080
|
||||||
|
|
||||||
[Proxy]
|
[Proxy]
|
||||||
# name = ss, server, port, cipher, password
|
# name = ss, server, port, cipher, password(, obfs=tls/http, obfs-host=bing.com)
|
||||||
# The types of cipher are consistent with go-shadowsocks2
|
# The types of cipher are consistent with go-shadowsocks2
|
||||||
# support AEAD_AES_128_GCM AEAD_AES_192_GCM AEAD_AES_256_GCM AEAD_CHACHA20_POLY1305 AES-128-CTR AES-192-CTR AES-256-CTR AES-128-CFB AES-192-CFB AES-256-CFB CHACHA20-IETF XCHACHA20
|
# support AEAD_AES_128_GCM AEAD_AES_192_GCM AEAD_AES_256_GCM AEAD_CHACHA20_POLY1305 AES-128-CTR AES-192-CTR AES-256-CTR AES-128-CFB AES-192-CFB AES-256-CFB CHACHA20-IETF XCHACHA20
|
||||||
Proxy = ss, server, port, AEAD_CHACHA20_POLY1305, password
|
ss1 = ss, server1, port, AEAD_CHACHA20_POLY1305, password
|
||||||
|
ss2 = ss, server2, port, AEAD_CHACHA20_POLY1305, password
|
||||||
|
|
||||||
|
# name = vmess, server, port, uuid, alterId, cipher(, tls=true)
|
||||||
|
# cipher support auto/aes-128-gcm/chacha20-poly1305/none
|
||||||
|
vmess1 = vmess, server, port, uuid, 32, auto, tls=true
|
||||||
|
|
||||||
|
# name = socks5, server, port
|
||||||
|
socks = socks5, server, port
|
||||||
|
|
||||||
|
[Proxy Group]
|
||||||
|
# url-test select which proxy will be used by benchmarking speed to a URL.
|
||||||
|
# name = url-test, [proxies], url, interval(second)
|
||||||
|
auto = url-test, ss1, ss2, http://www.google.com/generate_204, 300
|
||||||
|
|
||||||
|
# select is used for selecting proxy or proxy group
|
||||||
|
# you can use RESTful API to switch proxy, is recommended for use in GUI.
|
||||||
|
# name = select, [proxies]
|
||||||
|
Proxy = select, ss1, ss2, auto
|
||||||
|
|
||||||
[Rule]
|
[Rule]
|
||||||
DOMAIN-SUFFIX,google.com,Proxy
|
DOMAIN-SUFFIX,google.com,Proxy
|
||||||
@ -57,6 +109,19 @@ GEOIP,CN,DIRECT
|
|||||||
FINAL,,Proxy # note: there is two ","
|
FINAL,,Proxy # note: there is two ","
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Thanks
|
||||||
|
|
||||||
|
[riobard/go-shadowsocks2](https://github.com/riobard/go-shadowsocks2)
|
||||||
|
|
||||||
|
[v2ray/v2ray-core](https://github.com/v2ray/v2ray-core)
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
[](https://app.fossa.io/projects/git%2Bgithub.com%2FDreamacro%2Fclash?ref=badge_large)
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
- [ ] Complementing the necessary rule operators
|
- [x] Complementing the necessary rule operators
|
||||||
|
- [x] Redir proxy
|
||||||
|
- [ ] UDP support
|
||||||
|
- [ ] Connection manager
|
||||||
|
64
adapters/local/http.go
Normal file
64
adapters/local/http.go
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
package adapters
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HTTPAdapter is a adapter for HTTP connection
|
||||||
|
type HTTPAdapter struct {
|
||||||
|
addr *C.Addr
|
||||||
|
conn net.Conn
|
||||||
|
R *http.Request
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close HTTP connection
|
||||||
|
func (h *HTTPAdapter) Close() {
|
||||||
|
h.conn.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Addr return destination address
|
||||||
|
func (h *HTTPAdapter) Addr() *C.Addr {
|
||||||
|
return h.addr
|
||||||
|
}
|
||||||
|
|
||||||
|
// Conn return raw net.Conn of HTTP
|
||||||
|
func (h *HTTPAdapter) Conn() net.Conn {
|
||||||
|
return h.conn
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewHTTP is HTTPAdapter generator
|
||||||
|
func NewHTTP(request *http.Request, conn net.Conn) *HTTPAdapter {
|
||||||
|
return &HTTPAdapter{
|
||||||
|
addr: parseHTTPAddr(request),
|
||||||
|
R: request,
|
||||||
|
conn: conn,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoveHopByHopHeaders remove hop-by-hop header
|
||||||
|
func RemoveHopByHopHeaders(header http.Header) {
|
||||||
|
// Strip hop-by-hop header based on RFC:
|
||||||
|
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.5.1
|
||||||
|
// https://www.mnot.net/blog/2011/07/11/what_proxies_must_do
|
||||||
|
|
||||||
|
header.Del("Proxy-Connection")
|
||||||
|
header.Del("Proxy-Authenticate")
|
||||||
|
header.Del("Proxy-Authorization")
|
||||||
|
header.Del("TE")
|
||||||
|
header.Del("Trailers")
|
||||||
|
header.Del("Transfer-Encoding")
|
||||||
|
header.Del("Upgrade")
|
||||||
|
|
||||||
|
connections := header.Get("Connection")
|
||||||
|
header.Del("Connection")
|
||||||
|
if len(connections) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, h := range strings.Split(connections, ",") {
|
||||||
|
header.Del(strings.TrimSpace(h))
|
||||||
|
}
|
||||||
|
}
|
14
adapters/local/https.go
Normal file
14
adapters/local/https.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package adapters
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewHTTPS is HTTPAdapter generator
|
||||||
|
func NewHTTPS(request *http.Request, conn net.Conn) *SocketAdapter {
|
||||||
|
return &SocketAdapter{
|
||||||
|
addr: parseHTTPAddr(request),
|
||||||
|
conn: conn,
|
||||||
|
}
|
||||||
|
}
|
37
adapters/local/socket.go
Normal file
37
adapters/local/socket.go
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package adapters
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
"github.com/Dreamacro/go-shadowsocks2/socks"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SocketAdapter is a adapter for socks and redir connection
|
||||||
|
type SocketAdapter struct {
|
||||||
|
conn net.Conn
|
||||||
|
addr *C.Addr
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close socks and redir connection
|
||||||
|
func (s *SocketAdapter) Close() {
|
||||||
|
s.conn.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Addr return destination address
|
||||||
|
func (s *SocketAdapter) Addr() *C.Addr {
|
||||||
|
return s.addr
|
||||||
|
}
|
||||||
|
|
||||||
|
// Conn return raw net.Conn
|
||||||
|
func (s *SocketAdapter) Conn() net.Conn {
|
||||||
|
return s.conn
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewSocket is SocketAdapter generator
|
||||||
|
func NewSocket(target socks.Addr, conn net.Conn) *SocketAdapter {
|
||||||
|
return &SocketAdapter{
|
||||||
|
conn: conn,
|
||||||
|
addr: parseSocksAddr(target),
|
||||||
|
}
|
||||||
|
}
|
71
adapters/local/util.go
Normal file
71
adapters/local/util.go
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
package adapters
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
"github.com/Dreamacro/go-shadowsocks2/socks"
|
||||||
|
)
|
||||||
|
|
||||||
|
func parseSocksAddr(target socks.Addr) *C.Addr {
|
||||||
|
var host, port string
|
||||||
|
var ip net.IP
|
||||||
|
|
||||||
|
switch target[0] {
|
||||||
|
case socks.AtypDomainName:
|
||||||
|
host = string(target[2 : 2+target[1]])
|
||||||
|
port = strconv.Itoa((int(target[2+target[1]]) << 8) | int(target[2+target[1]+1]))
|
||||||
|
ipAddr, err := net.ResolveIPAddr("ip", host)
|
||||||
|
if err == nil {
|
||||||
|
ip = ipAddr.IP
|
||||||
|
}
|
||||||
|
case socks.AtypIPv4:
|
||||||
|
ip = net.IP(target[1 : 1+net.IPv4len])
|
||||||
|
port = strconv.Itoa((int(target[1+net.IPv4len]) << 8) | int(target[1+net.IPv4len+1]))
|
||||||
|
case socks.AtypIPv6:
|
||||||
|
ip = net.IP(target[1 : 1+net.IPv6len])
|
||||||
|
port = strconv.Itoa((int(target[1+net.IPv6len]) << 8) | int(target[1+net.IPv6len+1]))
|
||||||
|
}
|
||||||
|
|
||||||
|
return &C.Addr{
|
||||||
|
NetWork: C.TCP,
|
||||||
|
AddrType: int(target[0]),
|
||||||
|
Host: host,
|
||||||
|
IP: &ip,
|
||||||
|
Port: port,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseHTTPAddr(request *http.Request) *C.Addr {
|
||||||
|
host := request.URL.Hostname()
|
||||||
|
port := request.URL.Port()
|
||||||
|
if port == "" {
|
||||||
|
port = "80"
|
||||||
|
}
|
||||||
|
ipAddr, err := net.ResolveIPAddr("ip", host)
|
||||||
|
var resolveIP *net.IP
|
||||||
|
if err == nil {
|
||||||
|
resolveIP = &ipAddr.IP
|
||||||
|
}
|
||||||
|
|
||||||
|
var addType int
|
||||||
|
ip := net.ParseIP(host)
|
||||||
|
switch {
|
||||||
|
case ip == nil:
|
||||||
|
addType = socks.AtypDomainName
|
||||||
|
case ip.To4() == nil:
|
||||||
|
addType = socks.AtypIPv6
|
||||||
|
default:
|
||||||
|
addType = socks.AtypIPv4
|
||||||
|
}
|
||||||
|
|
||||||
|
return &C.Addr{
|
||||||
|
NetWork: C.TCP,
|
||||||
|
AddrType: addType,
|
||||||
|
Host: host,
|
||||||
|
IP: resolveIP,
|
||||||
|
Port: port,
|
||||||
|
}
|
||||||
|
}
|
@ -1,46 +0,0 @@
|
|||||||
package adapters
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
"net"
|
|
||||||
|
|
||||||
C "github.com/Dreamacro/clash/constant"
|
|
||||||
)
|
|
||||||
|
|
||||||
// RejectAdapter is a reject connected adapter
|
|
||||||
type RejectAdapter struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadWriter is used to handle network traffic
|
|
||||||
func (r *RejectAdapter) ReadWriter() io.ReadWriter {
|
|
||||||
return &NopRW{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close is used to close connection
|
|
||||||
func (r *RejectAdapter) Close() {}
|
|
||||||
|
|
||||||
// Close is used to close connection
|
|
||||||
func (r *RejectAdapter) Conn() net.Conn {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type Reject struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Reject) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) {
|
|
||||||
return &RejectAdapter{}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewReject() *Reject {
|
|
||||||
return &Reject{}
|
|
||||||
}
|
|
||||||
|
|
||||||
type NopRW struct{}
|
|
||||||
|
|
||||||
func (rw *NopRW) Read(b []byte) (int, error) {
|
|
||||||
return len(b), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rw *NopRW) Write(b []byte) (int, error) {
|
|
||||||
return 0, io.EOF
|
|
||||||
}
|
|
@ -1,7 +1,6 @@
|
|||||||
package adapters
|
package adapters
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
@ -12,22 +11,24 @@ type DirectAdapter struct {
|
|||||||
conn net.Conn
|
conn net.Conn
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadWriter is used to handle network traffic
|
|
||||||
func (d *DirectAdapter) ReadWriter() io.ReadWriter {
|
|
||||||
return d.conn
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close is used to close connection
|
// Close is used to close connection
|
||||||
func (d *DirectAdapter) Close() {
|
func (d *DirectAdapter) Close() {
|
||||||
d.conn.Close()
|
d.conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close is used to close connection
|
// Conn is used to http request
|
||||||
func (d *DirectAdapter) Conn() net.Conn {
|
func (d *DirectAdapter) Conn() net.Conn {
|
||||||
return d.conn
|
return d.conn
|
||||||
}
|
}
|
||||||
|
|
||||||
type Direct struct {
|
type Direct struct{}
|
||||||
|
|
||||||
|
func (d *Direct) Name() string {
|
||||||
|
return "Direct"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Direct) Type() C.AdapterType {
|
||||||
|
return C.Direct
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Direct) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) {
|
func (d *Direct) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) {
|
||||||
@ -35,7 +36,7 @@ func (d *Direct) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.(*net.TCPConn).SetKeepAlive(true)
|
tcpKeepAlive(c)
|
||||||
return &DirectAdapter{conn: c}, nil
|
return &DirectAdapter{conn: c}, nil
|
||||||
}
|
}
|
||||||
|
|
69
adapters/remote/reject.go
Normal file
69
adapters/remote/reject.go
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
package adapters
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RejectAdapter is a reject connected adapter
|
||||||
|
type RejectAdapter struct {
|
||||||
|
conn net.Conn
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close is used to close connection
|
||||||
|
func (r *RejectAdapter) Close() {}
|
||||||
|
|
||||||
|
// Conn is used to http request
|
||||||
|
func (r *RejectAdapter) Conn() net.Conn {
|
||||||
|
return r.conn
|
||||||
|
}
|
||||||
|
|
||||||
|
type Reject struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reject) Name() string {
|
||||||
|
return "Reject"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reject) Type() C.AdapterType {
|
||||||
|
return C.Reject
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Reject) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) {
|
||||||
|
return &RejectAdapter{conn: &NopConn{}}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewReject() *Reject {
|
||||||
|
return &Reject{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type NopConn struct{}
|
||||||
|
|
||||||
|
func (rw *NopConn) Read(b []byte) (int, error) {
|
||||||
|
return len(b), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rw *NopConn) Write(b []byte) (int, error) {
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close is fake function for net.Conn
|
||||||
|
func (rw *NopConn) Close() error { return nil }
|
||||||
|
|
||||||
|
// LocalAddr is fake function for net.Conn
|
||||||
|
func (rw *NopConn) LocalAddr() net.Addr { return nil }
|
||||||
|
|
||||||
|
// RemoteAddr is fake function for net.Conn
|
||||||
|
func (rw *NopConn) RemoteAddr() net.Addr { return nil }
|
||||||
|
|
||||||
|
// SetDeadline is fake function for net.Conn
|
||||||
|
func (rw *NopConn) SetDeadline(time.Time) error { return nil }
|
||||||
|
|
||||||
|
// SetReadDeadline is fake function for net.Conn
|
||||||
|
func (rw *NopConn) SetReadDeadline(time.Time) error { return nil }
|
||||||
|
|
||||||
|
// SetWriteDeadline is fake function for net.Conn
|
||||||
|
func (rw *NopConn) SetWriteDeadline(time.Time) error { return nil }
|
67
adapters/remote/selector.go
Normal file
67
adapters/remote/selector.go
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
package adapters
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"sort"
|
||||||
|
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Selector struct {
|
||||||
|
name string
|
||||||
|
selected C.Proxy
|
||||||
|
proxies map[string]C.Proxy
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Selector) Name() string {
|
||||||
|
return s.name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Selector) Type() C.AdapterType {
|
||||||
|
return C.Selector
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Selector) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) {
|
||||||
|
return s.selected.Generator(addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Selector) Now() string {
|
||||||
|
return s.selected.Name()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Selector) All() []string {
|
||||||
|
var all []string
|
||||||
|
for k := range s.proxies {
|
||||||
|
all = append(all, k)
|
||||||
|
}
|
||||||
|
sort.Strings(all)
|
||||||
|
return all
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Selector) Set(name string) error {
|
||||||
|
proxy, exist := s.proxies[name]
|
||||||
|
if !exist {
|
||||||
|
return errors.New("Proxy does not exist")
|
||||||
|
}
|
||||||
|
s.selected = proxy
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSelector(name string, proxies map[string]C.Proxy) (*Selector, error) {
|
||||||
|
if len(proxies) == 0 {
|
||||||
|
return nil, errors.New("Provide at least one proxy")
|
||||||
|
}
|
||||||
|
|
||||||
|
mapping := make(map[string]C.Proxy)
|
||||||
|
var init string
|
||||||
|
for k, v := range proxies {
|
||||||
|
mapping[k] = v
|
||||||
|
init = k
|
||||||
|
}
|
||||||
|
s := &Selector{
|
||||||
|
name: name,
|
||||||
|
proxies: mapping,
|
||||||
|
selected: proxies[init],
|
||||||
|
}
|
||||||
|
return s, nil
|
||||||
|
}
|
@ -3,15 +3,15 @@ package adapters
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"net"
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/Dreamacro/clash/common/simple-obfs"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
|
||||||
"github.com/riobard/go-shadowsocks2/core"
|
"github.com/Dreamacro/go-shadowsocks2/core"
|
||||||
"github.com/riobard/go-shadowsocks2/socks"
|
"github.com/Dreamacro/go-shadowsocks2/socks"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ShadowsocksAdapter is a shadowsocks adapter
|
// ShadowsocksAdapter is a shadowsocks adapter
|
||||||
@ -19,11 +19,6 @@ type ShadowsocksAdapter struct {
|
|||||||
conn net.Conn
|
conn net.Conn
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadWriter is used to handle network traffic
|
|
||||||
func (ss *ShadowsocksAdapter) ReadWriter() io.ReadWriter {
|
|
||||||
return ss.conn
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close is used to close connection
|
// Close is used to close connection
|
||||||
func (ss *ShadowsocksAdapter) Close() {
|
func (ss *ShadowsocksAdapter) Close() {
|
||||||
ss.conn.Close()
|
ss.conn.Close()
|
||||||
@ -35,30 +30,62 @@ func (ss *ShadowsocksAdapter) Conn() net.Conn {
|
|||||||
|
|
||||||
type ShadowSocks struct {
|
type ShadowSocks struct {
|
||||||
server string
|
server string
|
||||||
cipher string
|
name string
|
||||||
password string
|
obfs string
|
||||||
|
obfsHost string
|
||||||
|
cipher core.Cipher
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ss *ShadowSocks) Name() string {
|
||||||
|
return ss.name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ss *ShadowSocks) Type() C.AdapterType {
|
||||||
|
return C.Shadowsocks
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ss *ShadowSocks) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) {
|
func (ss *ShadowSocks) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) {
|
||||||
var key []byte
|
|
||||||
ciph, _ := core.PickCipher(ss.cipher, key, ss.password)
|
|
||||||
c, err := net.Dial("tcp", ss.server)
|
c, err := net.Dial("tcp", ss.server)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%s connect error", ss.server)
|
return nil, fmt.Errorf("%s connect error", ss.server)
|
||||||
}
|
}
|
||||||
c.(*net.TCPConn).SetKeepAlive(true)
|
tcpKeepAlive(c)
|
||||||
c = ciph.StreamConn(c)
|
switch ss.obfs {
|
||||||
|
case "tls":
|
||||||
|
c = obfs.NewTLSObfs(c, ss.obfsHost)
|
||||||
|
case "http":
|
||||||
|
_, port, _ := net.SplitHostPort(ss.server)
|
||||||
|
c = obfs.NewHTTPObfs(c, ss.obfsHost, port)
|
||||||
|
}
|
||||||
|
c = ss.cipher.StreamConn(c)
|
||||||
_, err = c.Write(serializesSocksAddr(addr))
|
_, err = c.Write(serializesSocksAddr(addr))
|
||||||
return &ShadowsocksAdapter{conn: c}, err
|
return &ShadowsocksAdapter{conn: c}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewShadowSocks(ssURL string) *ShadowSocks {
|
func NewShadowSocks(name string, ssURL string, option map[string]string) (*ShadowSocks, error) {
|
||||||
server, cipher, password, _ := parseURL(ssURL)
|
server, cipher, password, _ := parseURL(ssURL)
|
||||||
|
ciph, err := core.PickCipher(cipher, nil, password)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("ss %s initialize error: %s", server, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
obfs := ""
|
||||||
|
obfsHost := "bing.com"
|
||||||
|
if value, ok := option["obfs"]; ok {
|
||||||
|
obfs = value
|
||||||
|
}
|
||||||
|
|
||||||
|
if value, ok := option["obfs-host"]; ok {
|
||||||
|
obfsHost = value
|
||||||
|
}
|
||||||
|
|
||||||
return &ShadowSocks{
|
return &ShadowSocks{
|
||||||
server: server,
|
server: server,
|
||||||
cipher: cipher,
|
name: name,
|
||||||
password: password,
|
cipher: ciph,
|
||||||
}
|
obfs: obfs,
|
||||||
|
obfsHost: obfsHost,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseURL(s string) (addr, cipher, password string, err error) {
|
func parseURL(s string) (addr, cipher, password string, err error) {
|
||||||
@ -84,13 +111,13 @@ func serializesSocksAddr(addr *C.Addr) []byte {
|
|||||||
case socks.AtypDomainName:
|
case socks.AtypDomainName:
|
||||||
len := uint8(len(addr.Host))
|
len := uint8(len(addr.Host))
|
||||||
host := []byte(addr.Host)
|
host := []byte(addr.Host)
|
||||||
buf = [][]byte{[]byte{aType, len}, host, port}
|
buf = [][]byte{{aType, len}, host, port}
|
||||||
case socks.AtypIPv4:
|
case socks.AtypIPv4:
|
||||||
host := addr.IP.To4()
|
host := addr.IP.To4()
|
||||||
buf = [][]byte{[]byte{aType}, host, port}
|
buf = [][]byte{{aType}, host, port}
|
||||||
case socks.AtypIPv6:
|
case socks.AtypIPv6:
|
||||||
host := addr.IP.To16()
|
host := addr.IP.To16()
|
||||||
buf = [][]byte{[]byte{aType}, host, port}
|
buf = [][]byte{{aType}, host, port}
|
||||||
}
|
}
|
||||||
return bytes.Join(buf, []byte(""))
|
return bytes.Join(buf, nil)
|
||||||
}
|
}
|
90
adapters/remote/socks5.go
Normal file
90
adapters/remote/socks5.go
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
package adapters
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
|
||||||
|
"github.com/Dreamacro/go-shadowsocks2/socks"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Socks5Adapter is a shadowsocks adapter
|
||||||
|
type Socks5Adapter struct {
|
||||||
|
conn net.Conn
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close is used to close connection
|
||||||
|
func (ss *Socks5Adapter) Close() {
|
||||||
|
ss.conn.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ss *Socks5Adapter) Conn() net.Conn {
|
||||||
|
return ss.conn
|
||||||
|
}
|
||||||
|
|
||||||
|
type Socks5 struct {
|
||||||
|
addr string
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ss *Socks5) Name() string {
|
||||||
|
return ss.name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ss *Socks5) Type() C.AdapterType {
|
||||||
|
return C.Socks5
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ss *Socks5) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) {
|
||||||
|
c, err := net.Dial("tcp", ss.addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("%s connect error", ss.addr)
|
||||||
|
}
|
||||||
|
tcpKeepAlive(c)
|
||||||
|
if err := ss.shakeHand(addr, c); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Socks5Adapter{conn: c}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ss *Socks5) shakeHand(addr *C.Addr, rw io.ReadWriter) error {
|
||||||
|
buf := make([]byte, socks.MaxAddrLen)
|
||||||
|
|
||||||
|
// VER, CMD, RSV
|
||||||
|
_, err := rw.Write([]byte{5, 1, 0})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := io.ReadFull(rw, buf[:2]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if buf[0] != 5 {
|
||||||
|
return errors.New("SOCKS version error")
|
||||||
|
} else if buf[1] != 0 {
|
||||||
|
return errors.New("SOCKS need auth")
|
||||||
|
}
|
||||||
|
|
||||||
|
// VER, CMD, RSV, ADDR
|
||||||
|
if _, err := rw.Write(bytes.Join([][]byte{{5, 1, 0}, serializesSocksAddr(addr)}, []byte(""))); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := io.ReadFull(rw, buf[:10]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSocks5(name, addr string) *Socks5 {
|
||||||
|
return &Socks5{
|
||||||
|
addr: addr,
|
||||||
|
name: name,
|
||||||
|
}
|
||||||
|
}
|
102
adapters/remote/urltest.go
Normal file
102
adapters/remote/urltest.go
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
package adapters
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
)
|
||||||
|
|
||||||
|
type URLTest struct {
|
||||||
|
name string
|
||||||
|
proxies []C.Proxy
|
||||||
|
rawURL string
|
||||||
|
fast C.Proxy
|
||||||
|
delay time.Duration
|
||||||
|
done chan struct{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *URLTest) Name() string {
|
||||||
|
return u.name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *URLTest) Type() C.AdapterType {
|
||||||
|
return C.URLTest
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *URLTest) Now() string {
|
||||||
|
return u.fast.Name()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *URLTest) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) {
|
||||||
|
return u.fast.Generator(addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *URLTest) Close() {
|
||||||
|
u.done <- struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *URLTest) loop() {
|
||||||
|
tick := time.NewTicker(u.delay)
|
||||||
|
go u.speedTest()
|
||||||
|
Loop:
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-tick.C:
|
||||||
|
go u.speedTest()
|
||||||
|
case <-u.done:
|
||||||
|
break Loop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *URLTest) speedTest() {
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
|
wg.Add(len(u.proxies))
|
||||||
|
c := make(chan interface{})
|
||||||
|
fast := selectFast(c)
|
||||||
|
timer := time.NewTimer(u.delay)
|
||||||
|
|
||||||
|
for _, p := range u.proxies {
|
||||||
|
go func(p C.Proxy) {
|
||||||
|
_, err := DelayTest(p, u.rawURL)
|
||||||
|
if err == nil {
|
||||||
|
c <- p
|
||||||
|
}
|
||||||
|
wg.Done()
|
||||||
|
}(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
wg.Wait()
|
||||||
|
close(c)
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-timer.C:
|
||||||
|
// Wait for fast to return or close.
|
||||||
|
<-fast
|
||||||
|
case p, open := <-fast:
|
||||||
|
if open {
|
||||||
|
u.fast = p.(C.Proxy)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewURLTest(name string, proxies []C.Proxy, rawURL string, delay time.Duration) (*URLTest, error) {
|
||||||
|
_, err := urlToAddr(rawURL)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
urlTest := &URLTest{
|
||||||
|
name: name,
|
||||||
|
proxies: proxies[:],
|
||||||
|
rawURL: rawURL,
|
||||||
|
fast: proxies[0],
|
||||||
|
delay: delay,
|
||||||
|
done: make(chan struct{}),
|
||||||
|
}
|
||||||
|
go urlTest.loop()
|
||||||
|
return urlTest, nil
|
||||||
|
}
|
93
adapters/remote/util.go
Normal file
93
adapters/remote/util.go
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
package adapters
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DelayTest get the delay for the specified URL
|
||||||
|
func DelayTest(proxy C.Proxy, url string) (t int16, err error) {
|
||||||
|
addr, err := urlToAddr(url)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
instance, err := proxy.Generator(&addr)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer instance.Close()
|
||||||
|
transport := &http.Transport{
|
||||||
|
Dial: func(string, string) (net.Conn, error) {
|
||||||
|
return instance.Conn(), nil
|
||||||
|
},
|
||||||
|
// from http.DefaultTransport
|
||||||
|
MaxIdleConns: 100,
|
||||||
|
IdleConnTimeout: 90 * time.Second,
|
||||||
|
TLSHandshakeTimeout: 10 * time.Second,
|
||||||
|
ExpectContinueTimeout: 1 * time.Second,
|
||||||
|
}
|
||||||
|
client := http.Client{Transport: transport}
|
||||||
|
req, err := client.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req.Body.Close()
|
||||||
|
t = int16(time.Since(start) / time.Millisecond)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func urlToAddr(rawURL string) (addr C.Addr, err error) {
|
||||||
|
u, err := url.Parse(rawURL)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
port := u.Port()
|
||||||
|
if port == "" {
|
||||||
|
if u.Scheme == "https" {
|
||||||
|
port = "443"
|
||||||
|
} else if u.Scheme == "http" {
|
||||||
|
port = "80"
|
||||||
|
} else {
|
||||||
|
err = fmt.Errorf("%s scheme not Support", rawURL)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
addr = C.Addr{
|
||||||
|
AddrType: C.AtypDomainName,
|
||||||
|
Host: u.Hostname(),
|
||||||
|
IP: nil,
|
||||||
|
Port: port,
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func selectFast(in chan interface{}) chan interface{} {
|
||||||
|
out := make(chan interface{})
|
||||||
|
go func() {
|
||||||
|
p, open := <-in
|
||||||
|
if open {
|
||||||
|
out <- p
|
||||||
|
}
|
||||||
|
close(out)
|
||||||
|
for range in {
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func tcpKeepAlive(c net.Conn) {
|
||||||
|
if tcp, ok := c.(*net.TCPConn); ok {
|
||||||
|
tcp.SetKeepAlive(true)
|
||||||
|
tcp.SetKeepAlivePeriod(3 * time.Minute)
|
||||||
|
}
|
||||||
|
}
|
95
adapters/remote/vmess.go
Normal file
95
adapters/remote/vmess.go
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
package adapters
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/Dreamacro/clash/common/vmess"
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
)
|
||||||
|
|
||||||
|
// VmessAdapter is a vmess adapter
|
||||||
|
type VmessAdapter struct {
|
||||||
|
conn net.Conn
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close is used to close connection
|
||||||
|
func (v *VmessAdapter) Close() {
|
||||||
|
v.conn.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *VmessAdapter) Conn() net.Conn {
|
||||||
|
return v.conn
|
||||||
|
}
|
||||||
|
|
||||||
|
type Vmess struct {
|
||||||
|
name string
|
||||||
|
server string
|
||||||
|
client *vmess.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ss *Vmess) Name() string {
|
||||||
|
return ss.name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ss *Vmess) Type() C.AdapterType {
|
||||||
|
return C.Vmess
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ss *Vmess) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) {
|
||||||
|
c, err := net.Dial("tcp", ss.server)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("%s connect error", ss.server)
|
||||||
|
}
|
||||||
|
tcpKeepAlive(c)
|
||||||
|
c = ss.client.New(c, parseVmessAddr(addr))
|
||||||
|
return &VmessAdapter{conn: c}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewVmess(name string, server string, uuid string, alterID uint16, security string, option map[string]string) (*Vmess, error) {
|
||||||
|
security = strings.ToLower(security)
|
||||||
|
client, err := vmess.NewClient(vmess.Config{
|
||||||
|
UUID: uuid,
|
||||||
|
AlterID: alterID,
|
||||||
|
Security: security,
|
||||||
|
TLS: option["tls"] == "true",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Vmess{
|
||||||
|
name: name,
|
||||||
|
server: server,
|
||||||
|
client: client,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseVmessAddr(info *C.Addr) *vmess.DstAddr {
|
||||||
|
var addrType byte
|
||||||
|
var addr []byte
|
||||||
|
switch info.AddrType {
|
||||||
|
case C.AtypIPv4:
|
||||||
|
addrType = byte(vmess.AtypIPv4)
|
||||||
|
addr = make([]byte, net.IPv4len)
|
||||||
|
copy(addr[:], info.IP.To4())
|
||||||
|
case C.AtypIPv6:
|
||||||
|
addrType = byte(vmess.AtypIPv6)
|
||||||
|
addr = make([]byte, net.IPv6len)
|
||||||
|
copy(addr[:], info.IP.To16())
|
||||||
|
case C.AtypDomainName:
|
||||||
|
addrType = byte(vmess.AtypDomainName)
|
||||||
|
addr = make([]byte, len(info.Host)+1)
|
||||||
|
addr[0] = byte(len(info.Host))
|
||||||
|
copy(addr[1:], []byte(info.Host))
|
||||||
|
}
|
||||||
|
|
||||||
|
port, _ := strconv.Atoi(info.Port)
|
||||||
|
return &vmess.DstAddr{
|
||||||
|
AddrType: addrType,
|
||||||
|
Addr: addr,
|
||||||
|
Port: uint(port),
|
||||||
|
}
|
||||||
|
}
|
88
common/simple-obfs/http.go
Normal file
88
common/simple-obfs/http.go
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
package obfs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/base64"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"math/rand"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HTTPObfs is shadowsocks http simple-obfs implementation
|
||||||
|
type HTTPObfs struct {
|
||||||
|
net.Conn
|
||||||
|
host string
|
||||||
|
port string
|
||||||
|
buf []byte
|
||||||
|
offset int
|
||||||
|
firstRequest bool
|
||||||
|
firstResponse bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ho *HTTPObfs) Read(b []byte) (int, error) {
|
||||||
|
if ho.buf != nil {
|
||||||
|
n := copy(b, ho.buf[ho.offset:])
|
||||||
|
ho.offset += n
|
||||||
|
if ho.offset == len(ho.buf) {
|
||||||
|
ho.buf = nil
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if ho.firstResponse {
|
||||||
|
buf := bufPool.Get().([]byte)
|
||||||
|
n, err := ho.Conn.Read(buf)
|
||||||
|
if err != nil {
|
||||||
|
bufPool.Put(buf[:cap(buf)])
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
idx := bytes.Index(buf[:n], []byte("\r\n\r\n"))
|
||||||
|
if idx == -1 {
|
||||||
|
bufPool.Put(buf[:cap(buf)])
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
ho.firstResponse = false
|
||||||
|
length := n - (idx + 4)
|
||||||
|
n = copy(b, buf[idx+4:n])
|
||||||
|
if length > n {
|
||||||
|
ho.buf = buf[:idx+4+length]
|
||||||
|
ho.offset = idx + 4 + n
|
||||||
|
} else {
|
||||||
|
bufPool.Put(buf[:cap(buf)])
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
return ho.Conn.Read(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ho *HTTPObfs) Write(b []byte) (int, error) {
|
||||||
|
if ho.firstRequest {
|
||||||
|
randBytes := make([]byte, 16)
|
||||||
|
rand.Read(randBytes)
|
||||||
|
req, _ := http.NewRequest("GET", fmt.Sprintf("http://%s/", ho.host), bytes.NewBuffer(b[:]))
|
||||||
|
req.Header.Set("User-Agent", fmt.Sprintf("curl/7.%d.%d", rand.Int()%54, rand.Int()%2))
|
||||||
|
req.Header.Set("Upgrade", "websocket")
|
||||||
|
req.Header.Set("Connection", "Upgrade")
|
||||||
|
req.Host = fmt.Sprintf("%s:%s", ho.host, ho.port)
|
||||||
|
req.Header.Set("Sec-WebSocket-Key", base64.URLEncoding.EncodeToString(randBytes))
|
||||||
|
req.ContentLength = int64(len(b))
|
||||||
|
err := req.Write(ho.Conn)
|
||||||
|
ho.firstRequest = false
|
||||||
|
return len(b), err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ho.Conn.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewHTTPObfs return a HTTPObfs
|
||||||
|
func NewHTTPObfs(conn net.Conn, host string, port string) net.Conn {
|
||||||
|
return &HTTPObfs{
|
||||||
|
Conn: conn,
|
||||||
|
firstRequest: true,
|
||||||
|
firstResponse: true,
|
||||||
|
host: host,
|
||||||
|
port: port,
|
||||||
|
}
|
||||||
|
}
|
190
common/simple-obfs/tls.go
Normal file
190
common/simple-obfs/tls.go
Normal file
@ -0,0 +1,190 @@
|
|||||||
|
package obfs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/binary"
|
||||||
|
"io"
|
||||||
|
"math/rand"
|
||||||
|
"net"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rand.Seed(time.Now().Unix())
|
||||||
|
}
|
||||||
|
|
||||||
|
var bufPool = sync.Pool{New: func() interface{} { return make([]byte, 2048) }}
|
||||||
|
|
||||||
|
// TLSObfs is shadowsocks tls simple-obfs implementation
|
||||||
|
type TLSObfs struct {
|
||||||
|
net.Conn
|
||||||
|
server string
|
||||||
|
remain int
|
||||||
|
firstRequest bool
|
||||||
|
firstResponse bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (to *TLSObfs) read(b []byte, discardN int) (int, error) {
|
||||||
|
buf := bufPool.Get().([]byte)
|
||||||
|
_, err := io.ReadFull(to.Conn, buf[:discardN])
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
bufPool.Put(buf[:cap(buf)])
|
||||||
|
|
||||||
|
sizeBuf := make([]byte, 2)
|
||||||
|
_, err = io.ReadFull(to.Conn, sizeBuf)
|
||||||
|
if err != nil {
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
length := int(binary.BigEndian.Uint16(sizeBuf))
|
||||||
|
if length > len(b) {
|
||||||
|
n, err := to.Conn.Read(b)
|
||||||
|
if err != nil {
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
to.remain = length - n
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return io.ReadFull(to.Conn, b[:length])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (to *TLSObfs) Read(b []byte) (int, error) {
|
||||||
|
if to.remain > 0 {
|
||||||
|
length := to.remain
|
||||||
|
if length > len(b) {
|
||||||
|
length = len(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
n, err := io.ReadFull(to.Conn, b[:length])
|
||||||
|
to.remain -= n
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if to.firstResponse {
|
||||||
|
// type + ver + lensize + 91 = 96
|
||||||
|
// type + ver + lensize + 1 = 6
|
||||||
|
// type + ver = 3
|
||||||
|
to.firstResponse = false
|
||||||
|
return to.read(b, 105)
|
||||||
|
}
|
||||||
|
|
||||||
|
// type + ver = 3
|
||||||
|
return to.read(b, 3)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (to *TLSObfs) Write(b []byte) (int, error) {
|
||||||
|
if to.firstRequest {
|
||||||
|
helloMsg := makeClientHelloMsg(b, to.server)
|
||||||
|
_, err := to.Conn.Write(helloMsg)
|
||||||
|
to.firstRequest = false
|
||||||
|
return len(b), err
|
||||||
|
}
|
||||||
|
|
||||||
|
size := bufPool.Get().([]byte)
|
||||||
|
binary.BigEndian.PutUint16(size[:2], uint16(len(b)))
|
||||||
|
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
buf.Write([]byte{0x17, 0x03, 0x03})
|
||||||
|
buf.Write(size[:2])
|
||||||
|
buf.Write(b)
|
||||||
|
_, err := to.Conn.Write(buf.Bytes())
|
||||||
|
bufPool.Put(size[:cap(size)])
|
||||||
|
return len(b), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewTLSObfs return a SimpleObfs
|
||||||
|
func NewTLSObfs(conn net.Conn, server string) net.Conn {
|
||||||
|
return &TLSObfs{
|
||||||
|
Conn: conn,
|
||||||
|
server: server,
|
||||||
|
firstRequest: true,
|
||||||
|
firstResponse: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeClientHelloMsg(data []byte, server string) []byte {
|
||||||
|
random := make([]byte, 32)
|
||||||
|
sessionID := make([]byte, 32)
|
||||||
|
size := make([]byte, 2)
|
||||||
|
rand.Read(random)
|
||||||
|
rand.Read(sessionID)
|
||||||
|
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
|
||||||
|
// handshake, TLS 1.0 version, length
|
||||||
|
buf.WriteByte(22)
|
||||||
|
buf.Write([]byte{0x03, 0x01})
|
||||||
|
length := uint16(212 + len(data) + len(server))
|
||||||
|
buf.WriteByte(byte(length >> 8))
|
||||||
|
buf.WriteByte(byte(length & 0xff))
|
||||||
|
|
||||||
|
// clientHello, length, TLS 1.2 version
|
||||||
|
buf.WriteByte(1)
|
||||||
|
binary.BigEndian.PutUint16(size, uint16(208+len(data)+len(server)))
|
||||||
|
buf.WriteByte(0)
|
||||||
|
buf.Write(size)
|
||||||
|
buf.Write([]byte{0x03, 0x03})
|
||||||
|
|
||||||
|
// random, sid len, sid
|
||||||
|
buf.Write(random)
|
||||||
|
buf.WriteByte(32)
|
||||||
|
buf.Write(sessionID)
|
||||||
|
|
||||||
|
// cipher suites
|
||||||
|
buf.Write([]byte{0x00, 0x38})
|
||||||
|
buf.Write([]byte{
|
||||||
|
0xc0, 0x2c, 0xc0, 0x30, 0x00, 0x9f, 0xcc, 0xa9, 0xcc, 0xa8, 0xcc, 0xaa, 0xc0, 0x2b, 0xc0, 0x2f,
|
||||||
|
0x00, 0x9e, 0xc0, 0x24, 0xc0, 0x28, 0x00, 0x6b, 0xc0, 0x23, 0xc0, 0x27, 0x00, 0x67, 0xc0, 0x0a,
|
||||||
|
0xc0, 0x14, 0x00, 0x39, 0xc0, 0x09, 0xc0, 0x13, 0x00, 0x33, 0x00, 0x9d, 0x00, 0x9c, 0x00, 0x3d,
|
||||||
|
0x00, 0x3c, 0x00, 0x35, 0x00, 0x2f, 0x00, 0xff,
|
||||||
|
})
|
||||||
|
|
||||||
|
// compression
|
||||||
|
buf.Write([]byte{0x01, 0x00})
|
||||||
|
|
||||||
|
// extension length
|
||||||
|
binary.BigEndian.PutUint16(size, uint16(79+len(data)+len(server)))
|
||||||
|
buf.Write(size)
|
||||||
|
|
||||||
|
// session ticket
|
||||||
|
buf.Write([]byte{0x00, 0x23})
|
||||||
|
binary.BigEndian.PutUint16(size, uint16(len(data)))
|
||||||
|
buf.Write(size)
|
||||||
|
buf.Write(data)
|
||||||
|
|
||||||
|
// server name
|
||||||
|
buf.Write([]byte{0x00, 0x00})
|
||||||
|
binary.BigEndian.PutUint16(size, uint16(len(server)+5))
|
||||||
|
buf.Write(size)
|
||||||
|
binary.BigEndian.PutUint16(size, uint16(len(server)+3))
|
||||||
|
buf.Write(size)
|
||||||
|
buf.WriteByte(0)
|
||||||
|
binary.BigEndian.PutUint16(size, uint16(len(server)))
|
||||||
|
buf.Write(size)
|
||||||
|
buf.Write([]byte(server))
|
||||||
|
|
||||||
|
// ec_point
|
||||||
|
buf.Write([]byte{0x00, 0x0b, 0x00, 0x04, 0x03, 0x01, 0x00, 0x02})
|
||||||
|
|
||||||
|
// groups
|
||||||
|
buf.Write([]byte{0x00, 0x0a, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x1d, 0x00, 0x17, 0x00, 0x19, 0x00, 0x18})
|
||||||
|
|
||||||
|
// signature
|
||||||
|
buf.Write([]byte{
|
||||||
|
0x00, 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x01, 0x06, 0x02, 0x06, 0x03, 0x05,
|
||||||
|
0x01, 0x05, 0x02, 0x05, 0x03, 0x04, 0x01, 0x04, 0x02, 0x04, 0x03, 0x03, 0x01,
|
||||||
|
0x03, 0x02, 0x03, 0x03, 0x02, 0x01, 0x02, 0x02, 0x02, 0x03,
|
||||||
|
})
|
||||||
|
|
||||||
|
// encrypt then mac
|
||||||
|
buf.Write([]byte{0x00, 0x16, 0x00, 0x00})
|
||||||
|
|
||||||
|
// extended master secret
|
||||||
|
buf.Write([]byte{0x00, 0x17, 0x00, 0x00})
|
||||||
|
|
||||||
|
return buf.Bytes()
|
||||||
|
}
|
115
common/vmess/aead.go
Normal file
115
common/vmess/aead.go
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
package vmess
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/cipher"
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
type aeadWriter struct {
|
||||||
|
io.Writer
|
||||||
|
cipher.AEAD
|
||||||
|
nonce [32]byte
|
||||||
|
count uint16
|
||||||
|
iv []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func newAEADWriter(w io.Writer, aead cipher.AEAD, iv []byte) *aeadWriter {
|
||||||
|
return &aeadWriter{Writer: w, AEAD: aead, iv: iv}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *aeadWriter) Write(b []byte) (n int, err error) {
|
||||||
|
buf := bufPool.Get().([]byte)
|
||||||
|
defer bufPool.Put(buf[:cap(buf)])
|
||||||
|
length := len(b)
|
||||||
|
for {
|
||||||
|
if length == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
readLen := chunkSize - w.Overhead()
|
||||||
|
if length < readLen {
|
||||||
|
readLen = length
|
||||||
|
}
|
||||||
|
payloadBuf := buf[lenSize : lenSize+chunkSize-w.Overhead()]
|
||||||
|
copy(payloadBuf, b[n:n+readLen])
|
||||||
|
|
||||||
|
binary.BigEndian.PutUint16(buf[:lenSize], uint16(readLen+w.Overhead()))
|
||||||
|
binary.BigEndian.PutUint16(w.nonce[:2], w.count)
|
||||||
|
copy(w.nonce[2:], w.iv[2:12])
|
||||||
|
|
||||||
|
w.Seal(payloadBuf[:0], w.nonce[:w.NonceSize()], payloadBuf[:readLen], nil)
|
||||||
|
w.count++
|
||||||
|
|
||||||
|
_, err = w.Writer.Write(buf[:lenSize+readLen+w.Overhead()])
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
n += readLen
|
||||||
|
length -= readLen
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
type aeadReader struct {
|
||||||
|
io.Reader
|
||||||
|
cipher.AEAD
|
||||||
|
nonce [32]byte
|
||||||
|
buf []byte
|
||||||
|
offset int
|
||||||
|
iv []byte
|
||||||
|
sizeBuf []byte
|
||||||
|
count uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
func newAEADReader(r io.Reader, aead cipher.AEAD, iv []byte) *aeadReader {
|
||||||
|
return &aeadReader{Reader: r, AEAD: aead, iv: iv, sizeBuf: make([]byte, lenSize)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *aeadReader) Read(b []byte) (int, error) {
|
||||||
|
if r.buf != nil {
|
||||||
|
n := copy(b, r.buf[r.offset:])
|
||||||
|
r.offset += n
|
||||||
|
if r.offset == len(r.buf) {
|
||||||
|
bufPool.Put(r.buf[:cap(r.buf)])
|
||||||
|
r.buf = nil
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := io.ReadFull(r.Reader, r.sizeBuf)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
size := int(binary.BigEndian.Uint16(r.sizeBuf))
|
||||||
|
if size > maxSize {
|
||||||
|
return 0, errors.New("Buffer is larger than standard")
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := bufPool.Get().([]byte)
|
||||||
|
_, err = io.ReadFull(r.Reader, buf[:size])
|
||||||
|
if err != nil {
|
||||||
|
bufPool.Put(buf[:cap(buf)])
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
binary.BigEndian.PutUint16(r.nonce[:2], r.count)
|
||||||
|
copy(r.nonce[2:], r.iv[2:12])
|
||||||
|
|
||||||
|
_, err = r.Open(buf[:0], r.nonce[:r.NonceSize()], buf[:size], nil)
|
||||||
|
r.count++
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
realLen := size - r.Overhead()
|
||||||
|
n := copy(b, buf[:realLen])
|
||||||
|
if len(b) >= realLen {
|
||||||
|
bufPool.Put(buf[:cap(buf)])
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r.offset = n
|
||||||
|
r.buf = buf[:realLen]
|
||||||
|
return n, nil
|
||||||
|
}
|
103
common/vmess/chunk.go
Normal file
103
common/vmess/chunk.go
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
package vmess
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
lenSize = 2
|
||||||
|
chunkSize = 1 << 14 // 2 ** 14 == 16 * 1024
|
||||||
|
maxSize = 17 * 1024 // 2 + chunkSize + aead.Overhead()
|
||||||
|
)
|
||||||
|
|
||||||
|
var bufPool = sync.Pool{New: func() interface{} { return make([]byte, maxSize) }}
|
||||||
|
|
||||||
|
type chunkReader struct {
|
||||||
|
io.Reader
|
||||||
|
buf []byte
|
||||||
|
sizeBuf []byte
|
||||||
|
offset int
|
||||||
|
}
|
||||||
|
|
||||||
|
func newChunkReader(reader io.Reader) *chunkReader {
|
||||||
|
return &chunkReader{Reader: reader, sizeBuf: make([]byte, lenSize)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newChunkWriter(writer io.WriteCloser) *chunkWriter {
|
||||||
|
return &chunkWriter{Writer: writer}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cr *chunkReader) Read(b []byte) (int, error) {
|
||||||
|
if cr.buf != nil {
|
||||||
|
n := copy(b, cr.buf[cr.offset:])
|
||||||
|
cr.offset += n
|
||||||
|
if cr.offset == len(cr.buf) {
|
||||||
|
bufPool.Put(cr.buf[:cap(cr.buf)])
|
||||||
|
cr.buf = nil
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := io.ReadFull(cr.Reader, cr.sizeBuf)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
size := int(binary.BigEndian.Uint16(cr.sizeBuf))
|
||||||
|
if size > maxSize {
|
||||||
|
return 0, errors.New("Buffer is larger than standard")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(b) >= size {
|
||||||
|
_, err := io.ReadFull(cr.Reader, b[:size])
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return size, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := bufPool.Get().([]byte)
|
||||||
|
_, err = io.ReadFull(cr.Reader, buf[:size])
|
||||||
|
if err != nil {
|
||||||
|
bufPool.Put(buf[:cap(buf)])
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
n := copy(b, cr.buf[:])
|
||||||
|
cr.offset = n
|
||||||
|
cr.buf = buf[:size]
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type chunkWriter struct {
|
||||||
|
io.Writer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cw *chunkWriter) Write(b []byte) (n int, err error) {
|
||||||
|
buf := bufPool.Get().([]byte)
|
||||||
|
defer bufPool.Put(buf[:cap(buf)])
|
||||||
|
length := len(b)
|
||||||
|
for {
|
||||||
|
if length == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
readLen := chunkSize
|
||||||
|
if length < chunkSize {
|
||||||
|
readLen = length
|
||||||
|
}
|
||||||
|
payloadBuf := buf[lenSize : lenSize+chunkSize]
|
||||||
|
copy(payloadBuf, b[n:n+readLen])
|
||||||
|
|
||||||
|
binary.BigEndian.PutUint16(buf[:lenSize], uint16(readLen))
|
||||||
|
_, err = cw.Writer.Write(buf[:lenSize+readLen])
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
n += readLen
|
||||||
|
length -= readLen
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
213
common/vmess/conn.go
Normal file
213
common/vmess/conn.go
Normal file
@ -0,0 +1,213 @@
|
|||||||
|
package vmess
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
"crypto/hmac"
|
||||||
|
"crypto/md5"
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
"hash/fnv"
|
||||||
|
"io"
|
||||||
|
"math/rand"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/chacha20poly1305"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Conn wrapper a net.Conn with vmess protocol
|
||||||
|
type Conn struct {
|
||||||
|
net.Conn
|
||||||
|
reader io.Reader
|
||||||
|
writer io.Writer
|
||||||
|
dst *DstAddr
|
||||||
|
id *ID
|
||||||
|
reqBodyIV []byte
|
||||||
|
reqBodyKey []byte
|
||||||
|
respBodyIV []byte
|
||||||
|
respBodyKey []byte
|
||||||
|
respV byte
|
||||||
|
security byte
|
||||||
|
|
||||||
|
sent bool
|
||||||
|
received bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (vc *Conn) Write(b []byte) (int, error) {
|
||||||
|
if vc.sent {
|
||||||
|
return vc.writer.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := vc.sendRequest(); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
vc.sent = true
|
||||||
|
return vc.writer.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (vc *Conn) Read(b []byte) (int, error) {
|
||||||
|
if vc.received {
|
||||||
|
return vc.reader.Read(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := vc.recvResponse(); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
vc.received = true
|
||||||
|
return vc.reader.Read(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (vc *Conn) sendRequest() error {
|
||||||
|
timestamp := make([]byte, 8)
|
||||||
|
binary.BigEndian.PutUint64(timestamp, uint64(time.Now().UTC().Unix()))
|
||||||
|
|
||||||
|
h := hmac.New(md5.New, vc.id.UUID.Bytes())
|
||||||
|
h.Write(timestamp)
|
||||||
|
_, err := vc.Conn.Write(h.Sum(nil))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
|
||||||
|
// Ver IV Key V Opt
|
||||||
|
buf.WriteByte(Version)
|
||||||
|
buf.Write(vc.reqBodyIV[:])
|
||||||
|
buf.Write(vc.reqBodyKey[:])
|
||||||
|
buf.WriteByte(vc.respV)
|
||||||
|
buf.WriteByte(OptionChunkStream)
|
||||||
|
|
||||||
|
p := rand.Intn(16)
|
||||||
|
// P Sec Reserve Cmd
|
||||||
|
buf.WriteByte(byte(p<<4) | byte(vc.security))
|
||||||
|
buf.WriteByte(0)
|
||||||
|
buf.WriteByte(CommandTCP)
|
||||||
|
|
||||||
|
// Port AddrType Addr
|
||||||
|
binary.Write(buf, binary.BigEndian, uint16(vc.dst.Port))
|
||||||
|
buf.WriteByte(vc.dst.AddrType)
|
||||||
|
buf.Write(vc.dst.Addr)
|
||||||
|
|
||||||
|
// padding
|
||||||
|
if p > 0 {
|
||||||
|
padding := make([]byte, p)
|
||||||
|
rand.Read(padding)
|
||||||
|
buf.Write(padding)
|
||||||
|
}
|
||||||
|
|
||||||
|
fnv1a := fnv.New32a()
|
||||||
|
fnv1a.Write(buf.Bytes())
|
||||||
|
buf.Write(fnv1a.Sum(nil))
|
||||||
|
|
||||||
|
block, err := aes.NewCipher(vc.id.CmdKey)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
stream := cipher.NewCFBEncrypter(block, hashTimestamp(time.Now().UTC()))
|
||||||
|
stream.XORKeyStream(buf.Bytes(), buf.Bytes())
|
||||||
|
_, err = vc.Conn.Write(buf.Bytes())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (vc *Conn) recvResponse() error {
|
||||||
|
block, err := aes.NewCipher(vc.respBodyKey[:])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
stream := cipher.NewCFBDecrypter(block, vc.respBodyIV[:])
|
||||||
|
buf := make([]byte, 4)
|
||||||
|
_, err = io.ReadFull(vc.Conn, buf)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
stream.XORKeyStream(buf, buf)
|
||||||
|
|
||||||
|
if buf[0] != vc.respV {
|
||||||
|
return errors.New("unexpected response header")
|
||||||
|
}
|
||||||
|
|
||||||
|
if buf[2] != 0 {
|
||||||
|
return errors.New("dynamic port is not supported now")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func hashTimestamp(t time.Time) []byte {
|
||||||
|
md5hash := md5.New()
|
||||||
|
ts := make([]byte, 8)
|
||||||
|
binary.BigEndian.PutUint64(ts, uint64(t.UTC().Unix()))
|
||||||
|
md5hash.Write(ts)
|
||||||
|
md5hash.Write(ts)
|
||||||
|
md5hash.Write(ts)
|
||||||
|
md5hash.Write(ts)
|
||||||
|
return md5hash.Sum(nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// newConn return a Conn instance
|
||||||
|
func newConn(conn net.Conn, id *ID, dst *DstAddr, security Security) *Conn {
|
||||||
|
randBytes := make([]byte, 33)
|
||||||
|
rand.Read(randBytes)
|
||||||
|
reqBodyIV := make([]byte, 16)
|
||||||
|
reqBodyKey := make([]byte, 16)
|
||||||
|
copy(reqBodyIV[:], randBytes[:16])
|
||||||
|
copy(reqBodyKey[:], randBytes[16:32])
|
||||||
|
respV := randBytes[32]
|
||||||
|
|
||||||
|
respBodyKey := md5.Sum(reqBodyKey[:])
|
||||||
|
respBodyIV := md5.Sum(reqBodyIV[:])
|
||||||
|
|
||||||
|
var writer io.Writer
|
||||||
|
var reader io.Reader
|
||||||
|
switch security {
|
||||||
|
case SecurityNone:
|
||||||
|
reader = newChunkReader(conn)
|
||||||
|
writer = newChunkWriter(conn)
|
||||||
|
case SecurityAES128GCM:
|
||||||
|
block, _ := aes.NewCipher(reqBodyKey[:])
|
||||||
|
aead, _ := cipher.NewGCM(block)
|
||||||
|
writer = newAEADWriter(conn, aead, reqBodyIV[:])
|
||||||
|
|
||||||
|
block, _ = aes.NewCipher(respBodyKey[:])
|
||||||
|
aead, _ = cipher.NewGCM(block)
|
||||||
|
reader = newAEADReader(conn, aead, respBodyIV[:])
|
||||||
|
case SecurityCHACHA20POLY1305:
|
||||||
|
key := make([]byte, 32)
|
||||||
|
t := md5.Sum(reqBodyKey[:])
|
||||||
|
copy(key, t[:])
|
||||||
|
t = md5.Sum(key[:16])
|
||||||
|
copy(key[16:], t[:])
|
||||||
|
aead, _ := chacha20poly1305.New(key)
|
||||||
|
writer = newAEADWriter(conn, aead, reqBodyIV[:])
|
||||||
|
|
||||||
|
t = md5.Sum(respBodyKey[:])
|
||||||
|
copy(key, t[:])
|
||||||
|
t = md5.Sum(key[:16])
|
||||||
|
copy(key[16:], t[:])
|
||||||
|
aead, _ = chacha20poly1305.New(key)
|
||||||
|
reader = newAEADReader(conn, aead, respBodyIV[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Conn{
|
||||||
|
Conn: conn,
|
||||||
|
id: id,
|
||||||
|
dst: dst,
|
||||||
|
reqBodyIV: reqBodyIV,
|
||||||
|
reqBodyKey: reqBodyKey,
|
||||||
|
respV: respV,
|
||||||
|
respBodyIV: respBodyIV[:],
|
||||||
|
respBodyKey: respBodyKey[:],
|
||||||
|
reader: reader,
|
||||||
|
writer: writer,
|
||||||
|
security: security,
|
||||||
|
}
|
||||||
|
}
|
54
common/vmess/user.go
Normal file
54
common/vmess/user.go
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package vmess
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/md5"
|
||||||
|
|
||||||
|
"github.com/gofrs/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ID cmdKey length
|
||||||
|
const (
|
||||||
|
IDBytesLen = 16
|
||||||
|
)
|
||||||
|
|
||||||
|
// The ID of en entity, in the form of a UUID.
|
||||||
|
type ID struct {
|
||||||
|
UUID *uuid.UUID
|
||||||
|
CmdKey []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// newID returns an ID with given UUID.
|
||||||
|
func newID(uuid *uuid.UUID) *ID {
|
||||||
|
id := &ID{UUID: uuid, CmdKey: make([]byte, IDBytesLen)}
|
||||||
|
md5hash := md5.New()
|
||||||
|
md5hash.Write(uuid.Bytes())
|
||||||
|
md5hash.Write([]byte("c48619fe-8f02-49e0-b9e9-edf763e17e21"))
|
||||||
|
md5hash.Sum(id.CmdKey[:0])
|
||||||
|
return id
|
||||||
|
}
|
||||||
|
|
||||||
|
func nextID(u *uuid.UUID) *uuid.UUID {
|
||||||
|
md5hash := md5.New()
|
||||||
|
md5hash.Write(u.Bytes())
|
||||||
|
md5hash.Write([]byte("16167dc8-16b6-4e6d-b8bb-65dd68113a81"))
|
||||||
|
var newid uuid.UUID
|
||||||
|
for {
|
||||||
|
md5hash.Sum(newid[:0])
|
||||||
|
if !bytes.Equal(newid.Bytes(), u.Bytes()) {
|
||||||
|
return &newid
|
||||||
|
}
|
||||||
|
md5hash.Write([]byte("533eff8a-4113-4b10-b5ce-0f5d76b98cd2"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newAlterIDs(primary *ID, alterIDCount uint16) []*ID {
|
||||||
|
alterIDs := make([]*ID, alterIDCount)
|
||||||
|
prevID := primary.UUID
|
||||||
|
for idx := range alterIDs {
|
||||||
|
newid := nextID(prevID)
|
||||||
|
alterIDs[idx] = &ID{UUID: newid, CmdKey: primary.CmdKey[:]}
|
||||||
|
prevID = newid
|
||||||
|
}
|
||||||
|
return alterIDs
|
||||||
|
}
|
117
common/vmess/vmess.go
Normal file
117
common/vmess/vmess.go
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
package vmess
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
|
"net"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
|
"github.com/gofrs/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Version of vmess
|
||||||
|
const Version byte = 1
|
||||||
|
|
||||||
|
// Request Options
|
||||||
|
const (
|
||||||
|
OptionChunkStream byte = 1
|
||||||
|
OptionChunkMasking byte = 4
|
||||||
|
)
|
||||||
|
|
||||||
|
// Security type vmess
|
||||||
|
type Security = byte
|
||||||
|
|
||||||
|
// Cipher types
|
||||||
|
const (
|
||||||
|
SecurityAES128GCM Security = 3
|
||||||
|
SecurityCHACHA20POLY1305 Security = 4
|
||||||
|
SecurityNone Security = 5
|
||||||
|
)
|
||||||
|
|
||||||
|
// CipherMapping return
|
||||||
|
var CipherMapping = map[string]byte{
|
||||||
|
"none": SecurityNone,
|
||||||
|
"aes-128-gcm": SecurityAES128GCM,
|
||||||
|
"chacha20-poly1305": SecurityCHACHA20POLY1305,
|
||||||
|
}
|
||||||
|
|
||||||
|
var tlsConfig = &tls.Config{
|
||||||
|
InsecureSkipVerify: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command types
|
||||||
|
const (
|
||||||
|
CommandTCP byte = 1
|
||||||
|
CommandUDP byte = 2
|
||||||
|
)
|
||||||
|
|
||||||
|
// Addr types
|
||||||
|
const (
|
||||||
|
AtypIPv4 byte = 1
|
||||||
|
AtypDomainName byte = 2
|
||||||
|
AtypIPv6 byte = 3
|
||||||
|
)
|
||||||
|
|
||||||
|
// DstAddr store destination address
|
||||||
|
type DstAddr struct {
|
||||||
|
AddrType byte
|
||||||
|
Addr []byte
|
||||||
|
Port uint
|
||||||
|
}
|
||||||
|
|
||||||
|
// Client is vmess connection generator
|
||||||
|
type Client struct {
|
||||||
|
user []*ID
|
||||||
|
uuid *uuid.UUID
|
||||||
|
security Security
|
||||||
|
tls bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Config of vmess
|
||||||
|
type Config struct {
|
||||||
|
UUID string
|
||||||
|
AlterID uint16
|
||||||
|
Security string
|
||||||
|
TLS bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// New return a Conn with net.Conn and DstAddr
|
||||||
|
func (c *Client) New(conn net.Conn, dst *DstAddr) net.Conn {
|
||||||
|
r := rand.Intn(len(c.user))
|
||||||
|
if c.tls {
|
||||||
|
conn = tls.Client(conn, tlsConfig)
|
||||||
|
}
|
||||||
|
return newConn(conn, c.user[r], dst, c.security)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewClient return Client instance
|
||||||
|
func NewClient(config Config) (*Client, error) {
|
||||||
|
uid, err := uuid.FromString(config.UUID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var security Security
|
||||||
|
switch config.Security {
|
||||||
|
case "aes-128-gcm":
|
||||||
|
security = SecurityAES128GCM
|
||||||
|
case "chacha20-poly1305":
|
||||||
|
security = SecurityCHACHA20POLY1305
|
||||||
|
case "none":
|
||||||
|
security = SecurityNone
|
||||||
|
case "auto":
|
||||||
|
security = SecurityCHACHA20POLY1305
|
||||||
|
if runtime.GOARCH == "amd64" || runtime.GOARCH == "s390x" || runtime.GOARCH == "arm64" {
|
||||||
|
security = SecurityAES128GCM
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("Unknown security type: %s", config.Security)
|
||||||
|
}
|
||||||
|
return &Client{
|
||||||
|
user: newAlterIDs(newID(&uid), config.AlterID),
|
||||||
|
uuid: &uid,
|
||||||
|
security: security,
|
||||||
|
tls: config.TLS,
|
||||||
|
}, nil
|
||||||
|
}
|
407
config/config.go
Normal file
407
config/config.go
Normal file
@ -0,0 +1,407 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/Dreamacro/clash/adapters/remote"
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
"github.com/Dreamacro/clash/observable"
|
||||||
|
R "github.com/Dreamacro/clash/rules"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"gopkg.in/ini.v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
config *Config
|
||||||
|
once sync.Once
|
||||||
|
)
|
||||||
|
|
||||||
|
// General config
|
||||||
|
type General struct {
|
||||||
|
Port int
|
||||||
|
SocksPort int
|
||||||
|
RedirPort int
|
||||||
|
AllowLan bool
|
||||||
|
Mode Mode
|
||||||
|
LogLevel C.LogLevel
|
||||||
|
}
|
||||||
|
|
||||||
|
// ProxyConfig is update proxy schema
|
||||||
|
type ProxyConfig struct {
|
||||||
|
Port *int
|
||||||
|
SocksPort *int
|
||||||
|
RedirPort *int
|
||||||
|
AllowLan *bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Config is clash config manager
|
||||||
|
type Config struct {
|
||||||
|
general *General
|
||||||
|
rules []C.Rule
|
||||||
|
proxies map[string]C.Proxy
|
||||||
|
lastUpdate time.Time
|
||||||
|
|
||||||
|
event chan<- interface{}
|
||||||
|
reportCh chan interface{}
|
||||||
|
observable *observable.Observable
|
||||||
|
}
|
||||||
|
|
||||||
|
// Event is event of clash config
|
||||||
|
type Event struct {
|
||||||
|
Type string
|
||||||
|
Payload interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Subscribe config stream
|
||||||
|
func (c *Config) Subscribe() observable.Subscription {
|
||||||
|
sub, _ := c.observable.Subscribe()
|
||||||
|
return sub
|
||||||
|
}
|
||||||
|
|
||||||
|
// Report return a channel for collecting report message
|
||||||
|
func (c *Config) Report() chan<- interface{} {
|
||||||
|
return c.reportCh
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) readConfig() (*ini.File, error) {
|
||||||
|
if _, err := os.Stat(C.ConfigPath); os.IsNotExist(err) {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return ini.LoadSources(
|
||||||
|
ini.LoadOptions{
|
||||||
|
AllowBooleanKeys: true,
|
||||||
|
UnparseableSections: []string{"Rule"},
|
||||||
|
},
|
||||||
|
C.ConfigPath,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse config
|
||||||
|
func (c *Config) Parse() error {
|
||||||
|
cfg, err := c.readConfig()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.parseGeneral(cfg); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.parseProxies(cfg); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.parseRules(cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Proxies return proxies of clash
|
||||||
|
func (c *Config) Proxies() map[string]C.Proxy {
|
||||||
|
return c.proxies
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rules return rules of clash
|
||||||
|
func (c *Config) Rules() []C.Rule {
|
||||||
|
return c.rules
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetMode change mode of clash
|
||||||
|
func (c *Config) SetMode(mode Mode) {
|
||||||
|
c.general.Mode = mode
|
||||||
|
c.event <- &Event{Type: "mode", Payload: mode}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetLogLevel change log level of clash
|
||||||
|
func (c *Config) SetLogLevel(level C.LogLevel) {
|
||||||
|
c.general.LogLevel = level
|
||||||
|
c.event <- &Event{Type: "log-level", Payload: level}
|
||||||
|
}
|
||||||
|
|
||||||
|
// General return clash general config
|
||||||
|
func (c *Config) General() General {
|
||||||
|
return *c.general
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateRules is a function for hot reload rules
|
||||||
|
func (c *Config) UpdateRules() error {
|
||||||
|
cfg, err := c.readConfig()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.parseRules(cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) parseGeneral(cfg *ini.File) error {
|
||||||
|
general := cfg.Section("General")
|
||||||
|
|
||||||
|
port := general.Key("port").RangeInt(0, 1, 65535)
|
||||||
|
socksPort := general.Key("socks-port").RangeInt(0, 1, 65535)
|
||||||
|
redirPort := general.Key("redir-port").RangeInt(0, 1, 65535)
|
||||||
|
allowLan := general.Key("allow-lan").MustBool()
|
||||||
|
logLevelString := general.Key("log-level").MustString(C.INFO.String())
|
||||||
|
modeString := general.Key("mode").MustString(Rule.String())
|
||||||
|
|
||||||
|
mode, exist := ModeMapping[modeString]
|
||||||
|
if !exist {
|
||||||
|
return fmt.Errorf("General.mode value invalid")
|
||||||
|
}
|
||||||
|
|
||||||
|
logLevel, exist := C.LogLevelMapping[logLevelString]
|
||||||
|
if !exist {
|
||||||
|
return fmt.Errorf("General.log-level value invalid")
|
||||||
|
}
|
||||||
|
|
||||||
|
c.general = &General{
|
||||||
|
Port: port,
|
||||||
|
SocksPort: socksPort,
|
||||||
|
RedirPort: redirPort,
|
||||||
|
AllowLan: allowLan,
|
||||||
|
Mode: mode,
|
||||||
|
LogLevel: logLevel,
|
||||||
|
}
|
||||||
|
|
||||||
|
if restAddr := general.Key("external-controller").String(); restAddr != "" {
|
||||||
|
c.event <- &Event{Type: "external-controller", Payload: restAddr}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.UpdateGeneral(*c.general)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateGeneral dispatch update event
|
||||||
|
func (c *Config) UpdateGeneral(general General) {
|
||||||
|
c.UpdateProxy(ProxyConfig{
|
||||||
|
Port: &general.Port,
|
||||||
|
SocksPort: &general.SocksPort,
|
||||||
|
RedirPort: &general.RedirPort,
|
||||||
|
AllowLan: &general.AllowLan,
|
||||||
|
})
|
||||||
|
c.event <- &Event{Type: "mode", Payload: general.Mode}
|
||||||
|
c.event <- &Event{Type: "log-level", Payload: general.LogLevel}
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateProxy dispatch update proxy event
|
||||||
|
func (c *Config) UpdateProxy(pc ProxyConfig) {
|
||||||
|
if pc.AllowLan != nil {
|
||||||
|
c.general.AllowLan = *pc.AllowLan
|
||||||
|
}
|
||||||
|
|
||||||
|
c.general.Port = *or(pc.Port, &c.general.Port)
|
||||||
|
if c.general.Port != 0 && (pc.AllowLan != nil || pc.Port != nil) {
|
||||||
|
c.event <- &Event{Type: "http-addr", Payload: genAddr(c.general.Port, c.general.AllowLan)}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.general.SocksPort = *or(pc.SocksPort, &c.general.SocksPort)
|
||||||
|
if c.general.SocksPort != 0 && (pc.AllowLan != nil || pc.SocksPort != nil) {
|
||||||
|
c.event <- &Event{Type: "socks-addr", Payload: genAddr(c.general.SocksPort, c.general.AllowLan)}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.general.RedirPort = *or(pc.RedirPort, &c.general.RedirPort)
|
||||||
|
if c.general.RedirPort != 0 && (pc.AllowLan != nil || pc.RedirPort != nil) {
|
||||||
|
c.event <- &Event{Type: "redir-addr", Payload: genAddr(c.general.RedirPort, c.general.AllowLan)}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) parseProxies(cfg *ini.File) error {
|
||||||
|
proxies := make(map[string]C.Proxy)
|
||||||
|
proxiesConfig := cfg.Section("Proxy")
|
||||||
|
groupsConfig := cfg.Section("Proxy Group")
|
||||||
|
|
||||||
|
// parse proxy
|
||||||
|
for _, key := range proxiesConfig.Keys() {
|
||||||
|
proxy := key.Strings(",")
|
||||||
|
if len(proxy) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
switch proxy[0] {
|
||||||
|
// ss, server, port, cipter, password
|
||||||
|
case "ss":
|
||||||
|
if len(proxy) < 5 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ssURL := url.URL{
|
||||||
|
Scheme: "ss",
|
||||||
|
User: url.UserPassword(proxy[3], proxy[4]),
|
||||||
|
Host: fmt.Sprintf("%s:%s", proxy[1], proxy[2]),
|
||||||
|
}
|
||||||
|
option := parseOptions(5, proxy...)
|
||||||
|
ss, err := adapters.NewShadowSocks(key.Name(), ssURL.String(), option)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
proxies[key.Name()] = ss
|
||||||
|
// socks5, server, port
|
||||||
|
case "socks5":
|
||||||
|
if len(proxy) < 3 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
addr := fmt.Sprintf("%s:%s", proxy[1], proxy[2])
|
||||||
|
socks5 := adapters.NewSocks5(key.Name(), addr)
|
||||||
|
proxies[key.Name()] = socks5
|
||||||
|
// vmess, server, port, uuid, alterId, security
|
||||||
|
case "vmess":
|
||||||
|
if len(proxy) < 6 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
addr := fmt.Sprintf("%s:%s", proxy[1], proxy[2])
|
||||||
|
alterID, err := strconv.Atoi(proxy[4])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
option := parseOptions(6, proxy...)
|
||||||
|
vmess, err := adapters.NewVmess(key.Name(), addr, proxy[3], uint16(alterID), proxy[5], option)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
proxies[key.Name()] = vmess
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// parse proxy group
|
||||||
|
for _, key := range groupsConfig.Keys() {
|
||||||
|
rule := strings.Split(key.Value(), ",")
|
||||||
|
rule = trimArr(rule)
|
||||||
|
switch rule[0] {
|
||||||
|
case "url-test":
|
||||||
|
if len(rule) < 4 {
|
||||||
|
return fmt.Errorf("URLTest need more than 4 param")
|
||||||
|
}
|
||||||
|
proxyNames := rule[1 : len(rule)-2]
|
||||||
|
delay, _ := strconv.Atoi(rule[len(rule)-1])
|
||||||
|
url := rule[len(rule)-2]
|
||||||
|
var ps []C.Proxy
|
||||||
|
for _, name := range proxyNames {
|
||||||
|
if p, ok := proxies[name]; ok {
|
||||||
|
ps = append(ps, p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
adapter, err := adapters.NewURLTest(key.Name(), ps, url, time.Duration(delay)*time.Second)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Config error: %s", err.Error())
|
||||||
|
}
|
||||||
|
proxies[key.Name()] = adapter
|
||||||
|
case "select":
|
||||||
|
if len(rule) < 2 {
|
||||||
|
return fmt.Errorf("Selector need more than 2 param")
|
||||||
|
}
|
||||||
|
proxyNames := rule[1:]
|
||||||
|
selectProxy := make(map[string]C.Proxy)
|
||||||
|
for _, name := range proxyNames {
|
||||||
|
proxy, exist := proxies[name]
|
||||||
|
if !exist {
|
||||||
|
return fmt.Errorf("Proxy %s not exist", name)
|
||||||
|
}
|
||||||
|
selectProxy[name] = proxy
|
||||||
|
}
|
||||||
|
selector, err := adapters.NewSelector(key.Name(), selectProxy)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Selector create error: %s", err.Error())
|
||||||
|
}
|
||||||
|
proxies[key.Name()] = selector
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// init proxy
|
||||||
|
proxies["GLOBAL"], _ = adapters.NewSelector("GLOBAL", proxies)
|
||||||
|
proxies["DIRECT"] = adapters.NewDirect()
|
||||||
|
proxies["REJECT"] = adapters.NewReject()
|
||||||
|
|
||||||
|
c.proxies = proxies
|
||||||
|
c.event <- &Event{Type: "proxies", Payload: proxies}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) parseRules(cfg *ini.File) error {
|
||||||
|
rules := []C.Rule{}
|
||||||
|
|
||||||
|
rulesConfig := cfg.Section("Rule")
|
||||||
|
// parse rules
|
||||||
|
reader := bufio.NewReader(strings.NewReader(rulesConfig.Body()))
|
||||||
|
for {
|
||||||
|
line, _, err := reader.ReadLine()
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
rule := strings.Split(string(line), ",")
|
||||||
|
if len(rule) < 3 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
rule = trimArr(rule)
|
||||||
|
switch rule[0] {
|
||||||
|
case "DOMAIN":
|
||||||
|
rules = append(rules, R.NewDomain(rule[1], rule[2]))
|
||||||
|
case "DOMAIN-SUFFIX":
|
||||||
|
rules = append(rules, R.NewDomainSuffix(rule[1], rule[2]))
|
||||||
|
case "DOMAIN-KEYWORD":
|
||||||
|
rules = append(rules, R.NewDomainKeyword(rule[1], rule[2]))
|
||||||
|
case "GEOIP":
|
||||||
|
rules = append(rules, R.NewGEOIP(rule[1], rule[2]))
|
||||||
|
case "IP-CIDR", "IP-CIDR6":
|
||||||
|
rules = append(rules, R.NewIPCIDR(rule[1], rule[2]))
|
||||||
|
case "FINAL":
|
||||||
|
rules = append(rules, R.NewFinal(rule[2]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.rules = rules
|
||||||
|
c.event <- &Event{Type: "rules", Payload: rules}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Config) handleResponseMessage() {
|
||||||
|
for elm := range c.reportCh {
|
||||||
|
event := elm.(*Event)
|
||||||
|
switch event.Type {
|
||||||
|
case "http-addr":
|
||||||
|
if event.Payload.(bool) == false {
|
||||||
|
log.Errorf("Listening HTTP proxy at %d error", c.general.Port)
|
||||||
|
c.general.Port = 0
|
||||||
|
}
|
||||||
|
case "socks-addr":
|
||||||
|
if event.Payload.(bool) == false {
|
||||||
|
log.Errorf("Listening SOCKS proxy at %d error", c.general.SocksPort)
|
||||||
|
c.general.SocksPort = 0
|
||||||
|
}
|
||||||
|
case "redir-addr":
|
||||||
|
if event.Payload.(bool) == false {
|
||||||
|
log.Errorf("Listening Redir proxy at %d error", c.general.RedirPort)
|
||||||
|
c.general.RedirPort = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newConfig() *Config {
|
||||||
|
event := make(chan interface{})
|
||||||
|
reportCh := make(chan interface{})
|
||||||
|
config := &Config{
|
||||||
|
general: &General{},
|
||||||
|
proxies: make(map[string]C.Proxy),
|
||||||
|
rules: []C.Rule{},
|
||||||
|
lastUpdate: time.Now(),
|
||||||
|
|
||||||
|
event: event,
|
||||||
|
reportCh: reportCh,
|
||||||
|
observable: observable.NewObservable(event),
|
||||||
|
}
|
||||||
|
go config.handleResponseMessage()
|
||||||
|
return config
|
||||||
|
}
|
||||||
|
|
||||||
|
// Instance return singleton instance of Config
|
||||||
|
func Instance() *Config {
|
||||||
|
once.Do(func() {
|
||||||
|
config = newConfig()
|
||||||
|
})
|
||||||
|
return config
|
||||||
|
}
|
72
config/initial.go
Normal file
72
config/initial.go
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"archive/tar"
|
||||||
|
"compress/gzip"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func downloadMMDB(path string) (err error) {
|
||||||
|
resp, err := http.Get("http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
gr, err := gzip.NewReader(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer gr.Close()
|
||||||
|
|
||||||
|
tr := tar.NewReader(gr)
|
||||||
|
for {
|
||||||
|
h, err := tr.Next()
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
} else if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.HasSuffix(h.Name, "GeoLite2-Country.mmdb") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
_, err = io.Copy(f, tr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init prepare necessary files
|
||||||
|
func Init() {
|
||||||
|
// initial config.ini
|
||||||
|
if _, err := os.Stat(C.ConfigPath); os.IsNotExist(err) {
|
||||||
|
log.Info("Can't find config, create a empty file")
|
||||||
|
os.OpenFile(C.ConfigPath, os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
|
}
|
||||||
|
|
||||||
|
// initial mmdb
|
||||||
|
if _, err := os.Stat(C.MMDBPath); os.IsNotExist(err) {
|
||||||
|
log.Info("Can't find MMDB, start download")
|
||||||
|
err := downloadMMDB(C.MMDBPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Can't download MMDB: %s", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
31
config/mode.go
Normal file
31
config/mode.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
type Mode int
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ModeMapping is a mapping for Mode enum
|
||||||
|
ModeMapping = map[string]Mode{
|
||||||
|
"Global": Global,
|
||||||
|
"Rule": Rule,
|
||||||
|
"Direct": Direct,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Global Mode = iota
|
||||||
|
Rule
|
||||||
|
Direct
|
||||||
|
)
|
||||||
|
|
||||||
|
func (m Mode) String() string {
|
||||||
|
switch m {
|
||||||
|
case Global:
|
||||||
|
return "Global"
|
||||||
|
case Rule:
|
||||||
|
return "Rule"
|
||||||
|
case Direct:
|
||||||
|
return "Direct"
|
||||||
|
default:
|
||||||
|
return "Unknow"
|
||||||
|
}
|
||||||
|
}
|
46
config/utils.go
Normal file
46
config/utils.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func trimArr(arr []string) (r []string) {
|
||||||
|
for _, e := range arr {
|
||||||
|
r = append(r, strings.Trim(e, " "))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func genAddr(port int, allowLan bool) string {
|
||||||
|
if allowLan {
|
||||||
|
return fmt.Sprintf(":%d", port)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("127.0.0.1:%d", port)
|
||||||
|
}
|
||||||
|
|
||||||
|
func or(pointers ...*int) *int {
|
||||||
|
for _, p := range pointers {
|
||||||
|
if p != nil {
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return pointers[len(pointers)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseOptions(startIdx int, params ...string) map[string]string {
|
||||||
|
mapping := make(map[string]string)
|
||||||
|
if len(params) <= startIdx {
|
||||||
|
return mapping
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, option := range params[startIdx:] {
|
||||||
|
pair := strings.SplitN(option, "=", 2)
|
||||||
|
if len(pair) != 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
mapping[strings.Trim(pair[0], " ")] = strings.Trim(pair[1], " ")
|
||||||
|
}
|
||||||
|
return mapping
|
||||||
|
}
|
@ -1,22 +1,56 @@
|
|||||||
package constant
|
package constant
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Adapter Type
|
||||||
|
const (
|
||||||
|
Direct AdapterType = iota
|
||||||
|
Reject
|
||||||
|
Selector
|
||||||
|
Shadowsocks
|
||||||
|
Socks5
|
||||||
|
URLTest
|
||||||
|
Vmess
|
||||||
|
)
|
||||||
|
|
||||||
type ProxyAdapter interface {
|
type ProxyAdapter interface {
|
||||||
ReadWriter() io.ReadWriter
|
|
||||||
Conn() net.Conn
|
Conn() net.Conn
|
||||||
Close()
|
Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerAdapter interface {
|
type ServerAdapter interface {
|
||||||
Addr() *Addr
|
Addr() *Addr
|
||||||
Connect(ProxyAdapter)
|
|
||||||
Close()
|
Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
type Proxy interface {
|
type Proxy interface {
|
||||||
|
Name() string
|
||||||
|
Type() AdapterType
|
||||||
Generator(addr *Addr) (ProxyAdapter, error)
|
Generator(addr *Addr) (ProxyAdapter, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AdapterType is enum of adapter type
|
||||||
|
type AdapterType int
|
||||||
|
|
||||||
|
func (at AdapterType) String() string {
|
||||||
|
switch at {
|
||||||
|
case Direct:
|
||||||
|
return "Direct"
|
||||||
|
case Reject:
|
||||||
|
return "Reject"
|
||||||
|
case Selector:
|
||||||
|
return "Selector"
|
||||||
|
case Shadowsocks:
|
||||||
|
return "Shadowsocks"
|
||||||
|
case Socks5:
|
||||||
|
return "Socks5"
|
||||||
|
case URLTest:
|
||||||
|
return "URLTest"
|
||||||
|
case Vmess:
|
||||||
|
return "Vmess"
|
||||||
|
default:
|
||||||
|
return "Unknow"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -10,8 +10,11 @@ const (
|
|||||||
AtypDomainName = 3
|
AtypDomainName = 3
|
||||||
AtypIPv6 = 4
|
AtypIPv6 = 4
|
||||||
|
|
||||||
TCP = iota
|
TCP NetWork = iota
|
||||||
UDP
|
UDP
|
||||||
|
|
||||||
|
HTTP SourceType = iota
|
||||||
|
SOCKS
|
||||||
)
|
)
|
||||||
|
|
||||||
type NetWork int
|
type NetWork int
|
||||||
@ -23,9 +26,12 @@ func (n *NetWork) String() string {
|
|||||||
return "udp"
|
return "udp"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SourceType int
|
||||||
|
|
||||||
// Addr is used to store connection address
|
// Addr is used to store connection address
|
||||||
type Addr struct {
|
type Addr struct {
|
||||||
NetWork NetWork
|
NetWork NetWork
|
||||||
|
Source SourceType
|
||||||
AddrType int
|
AddrType int
|
||||||
Host string
|
Host string
|
||||||
IP *net.IP
|
IP *net.IP
|
||||||
|
@ -1,23 +1,15 @@
|
|||||||
package constant
|
package constant
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"archive/tar"
|
|
||||||
"compress/gzip"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"gopkg.in/ini.v1"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Name = "clash"
|
Name = "clash"
|
||||||
DefalutHTTPPort = "7890"
|
|
||||||
DefalutSOCKSPort = "7891"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -26,12 +18,26 @@ var (
|
|||||||
MMDBPath string
|
MMDBPath string
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type General struct {
|
||||||
|
Mode *string `json:"mode,omitempty"`
|
||||||
|
AllowLan *bool `json:"allow-lan,omitempty"`
|
||||||
|
Port *int `json:"port,omitempty"`
|
||||||
|
SocksPort *int `json:"socks-port,omitempty"`
|
||||||
|
RedirPort *int `json:"redir-port,omitempty"`
|
||||||
|
LogLevel *string `json:"log-level,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
currentUser, err := user.Current()
|
currentUser, err := user.Current()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Can't get current user: %s", err.Error())
|
dir := os.Getenv("HOME")
|
||||||
|
if dir == "" {
|
||||||
|
log.Fatalf("Can't get current user: %s", err.Error())
|
||||||
|
}
|
||||||
|
HomeDir = dir
|
||||||
|
} else {
|
||||||
|
HomeDir = currentUser.HomeDir
|
||||||
}
|
}
|
||||||
HomeDir = currentUser.HomeDir
|
|
||||||
|
|
||||||
dirPath := path.Join(HomeDir, ".config", Name)
|
dirPath := path.Join(HomeDir, ".config", Name)
|
||||||
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
|
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
|
||||||
@ -41,67 +47,5 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ConfigPath = path.Join(dirPath, "config.ini")
|
ConfigPath = path.Join(dirPath, "config.ini")
|
||||||
if _, err := os.Stat(ConfigPath); os.IsNotExist(err) {
|
|
||||||
log.Info("Can't find config, create a empty file")
|
|
||||||
os.OpenFile(ConfigPath, os.O_CREATE|os.O_WRONLY, 0644)
|
|
||||||
}
|
|
||||||
|
|
||||||
MMDBPath = path.Join(dirPath, "Country.mmdb")
|
MMDBPath = path.Join(dirPath, "Country.mmdb")
|
||||||
if _, err := os.Stat(MMDBPath); os.IsNotExist(err) {
|
|
||||||
log.Info("Can't find MMDB, start download")
|
|
||||||
err := downloadMMDB(MMDBPath)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Can't download MMDB: %s", err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func downloadMMDB(path string) (err error) {
|
|
||||||
resp, err := http.Get("http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz")
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
gr, err := gzip.NewReader(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer gr.Close()
|
|
||||||
|
|
||||||
tr := tar.NewReader(gr)
|
|
||||||
for {
|
|
||||||
h, err := tr.Next()
|
|
||||||
if err == io.EOF {
|
|
||||||
break
|
|
||||||
} else if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if !strings.HasSuffix(h.Name, "GeoLite2-Country.mmdb") {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0644)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
_, err = io.Copy(f, tr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetConfig() (*ini.File, error) {
|
|
||||||
if _, err := os.Stat(ConfigPath); os.IsNotExist(err) {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return ini.LoadSources(
|
|
||||||
ini.LoadOptions{AllowBooleanKeys: true},
|
|
||||||
ConfigPath,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
35
constant/log.go
Normal file
35
constant/log.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package constant
|
||||||
|
|
||||||
|
var (
|
||||||
|
// LogLevelMapping is a mapping for LogLevel enum
|
||||||
|
LogLevelMapping = map[string]LogLevel{
|
||||||
|
"error": ERROR,
|
||||||
|
"warning": WARNING,
|
||||||
|
"info": INFO,
|
||||||
|
"debug": DEBUG,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ERROR LogLevel = iota
|
||||||
|
WARNING
|
||||||
|
INFO
|
||||||
|
DEBUG
|
||||||
|
)
|
||||||
|
|
||||||
|
type LogLevel int
|
||||||
|
|
||||||
|
func (l LogLevel) String() string {
|
||||||
|
switch l {
|
||||||
|
case INFO:
|
||||||
|
return "info"
|
||||||
|
case WARNING:
|
||||||
|
return "warning"
|
||||||
|
case ERROR:
|
||||||
|
return "error"
|
||||||
|
case DEBUG:
|
||||||
|
return "debug"
|
||||||
|
default:
|
||||||
|
return "unknow"
|
||||||
|
}
|
||||||
|
}
|
7
constant/proxy.go
Normal file
7
constant/proxy.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package constant
|
||||||
|
|
||||||
|
// ProxySignal is used to handle graceful shutdown of proxy
|
||||||
|
type ProxySignal struct {
|
||||||
|
Done chan<- struct{}
|
||||||
|
Closed <-chan struct{}
|
||||||
|
}
|
@ -2,7 +2,8 @@ package constant
|
|||||||
|
|
||||||
// Rule Type
|
// Rule Type
|
||||||
const (
|
const (
|
||||||
DomainSuffix RuleType = iota
|
Domain RuleType = iota
|
||||||
|
DomainSuffix
|
||||||
DomainKeyword
|
DomainKeyword
|
||||||
GEOIP
|
GEOIP
|
||||||
IPCIDR
|
IPCIDR
|
||||||
@ -11,8 +12,28 @@ const (
|
|||||||
|
|
||||||
type RuleType int
|
type RuleType int
|
||||||
|
|
||||||
|
func (rt RuleType) String() string {
|
||||||
|
switch rt {
|
||||||
|
case Domain:
|
||||||
|
return "Domain"
|
||||||
|
case DomainSuffix:
|
||||||
|
return "DomainSuffix"
|
||||||
|
case DomainKeyword:
|
||||||
|
return "DomainKeyword"
|
||||||
|
case GEOIP:
|
||||||
|
return "GEOIP"
|
||||||
|
case IPCIDR:
|
||||||
|
return "IPCIDR"
|
||||||
|
case FINAL:
|
||||||
|
return "FINAL"
|
||||||
|
default:
|
||||||
|
return "Unknow"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type Rule interface {
|
type Rule interface {
|
||||||
RuleType() RuleType
|
RuleType() RuleType
|
||||||
IsMatch(addr *Addr) bool
|
IsMatch(addr *Addr) bool
|
||||||
Adapter() string
|
Adapter() string
|
||||||
|
Payload() string
|
||||||
}
|
}
|
||||||
|
55
constant/traffic.go
Normal file
55
constant/traffic.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
package constant
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Traffic struct {
|
||||||
|
up chan int64
|
||||||
|
down chan int64
|
||||||
|
upCount int64
|
||||||
|
downCount int64
|
||||||
|
upTotal int64
|
||||||
|
downTotal int64
|
||||||
|
interval time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Traffic) Up() chan<- int64 {
|
||||||
|
return t.up
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Traffic) Down() chan<- int64 {
|
||||||
|
return t.down
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Traffic) Now() (up int64, down int64) {
|
||||||
|
return t.upTotal, t.downTotal
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Traffic) handle() {
|
||||||
|
go t.handleCh(t.up, &t.upCount, &t.upTotal)
|
||||||
|
go t.handleCh(t.down, &t.downCount, &t.downTotal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Traffic) handleCh(ch <-chan int64, count *int64, total *int64) {
|
||||||
|
ticker := time.NewTicker(t.interval)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case n := <-ch:
|
||||||
|
*count += n
|
||||||
|
case <-ticker.C:
|
||||||
|
*total = *count
|
||||||
|
*count = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTraffic(interval time.Duration) *Traffic {
|
||||||
|
t := &Traffic{
|
||||||
|
up: make(chan int64),
|
||||||
|
down: make(chan int64),
|
||||||
|
interval: interval,
|
||||||
|
}
|
||||||
|
go t.handle()
|
||||||
|
return t
|
||||||
|
}
|
BIN
docs/logo.png
Executable file
BIN
docs/logo.png
Executable file
Binary file not shown.
After Width: | Height: | Size: 14 KiB |
33
hub/common.go
Normal file
33
hub/common.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package hub
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/Dreamacro/clash/config"
|
||||||
|
"github.com/Dreamacro/clash/proxy"
|
||||||
|
T "github.com/Dreamacro/clash/tunnel"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
tunnel = T.Instance()
|
||||||
|
cfg = config.Instance()
|
||||||
|
listener = proxy.Instance()
|
||||||
|
)
|
||||||
|
|
||||||
|
type Error struct {
|
||||||
|
Error string `json:"error"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Errors struct {
|
||||||
|
Errors map[string]string `json:"errors"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatErrors(errorsMap map[string]error) (bool, Errors) {
|
||||||
|
errors := make(map[string]string)
|
||||||
|
hasError := false
|
||||||
|
for key, err := range errorsMap {
|
||||||
|
if err != nil {
|
||||||
|
errors[key] = err.Error()
|
||||||
|
hasError = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return hasError, Errors{Errors: errors}
|
||||||
|
}
|
96
hub/configs.go
Normal file
96
hub/configs.go
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
package hub
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/Dreamacro/clash/config"
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi"
|
||||||
|
"github.com/go-chi/render"
|
||||||
|
)
|
||||||
|
|
||||||
|
func configRouter() http.Handler {
|
||||||
|
r := chi.NewRouter()
|
||||||
|
r.Get("/", getConfigs)
|
||||||
|
r.Put("/", updateConfigs)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
type configSchema struct {
|
||||||
|
Port int `json:"port"`
|
||||||
|
SocksPort int `json:"socket-port"`
|
||||||
|
RedirPort int `json:"redir-port"`
|
||||||
|
AllowLan bool `json:"allow-lan"`
|
||||||
|
Mode string `json:"mode"`
|
||||||
|
LogLevel string `json:"log-level"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func getConfigs(w http.ResponseWriter, r *http.Request) {
|
||||||
|
general := cfg.General()
|
||||||
|
render.JSON(w, r, configSchema{
|
||||||
|
Port: general.Port,
|
||||||
|
SocksPort: general.SocksPort,
|
||||||
|
RedirPort: general.RedirPort,
|
||||||
|
AllowLan: general.AllowLan,
|
||||||
|
Mode: general.Mode.String(),
|
||||||
|
LogLevel: general.LogLevel.String(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateConfigs(w http.ResponseWriter, r *http.Request) {
|
||||||
|
general := &C.General{}
|
||||||
|
err := render.DecodeJSON(r.Body, general)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
render.JSON(w, r, Error{
|
||||||
|
Error: "Format error",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// update errors
|
||||||
|
var modeErr, logLevelErr error
|
||||||
|
|
||||||
|
// update mode
|
||||||
|
if general.Mode != nil {
|
||||||
|
mode, ok := config.ModeMapping[*general.Mode]
|
||||||
|
if !ok {
|
||||||
|
modeErr = fmt.Errorf("Mode error")
|
||||||
|
} else {
|
||||||
|
cfg.SetMode(mode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// update log-level
|
||||||
|
if general.LogLevel != nil {
|
||||||
|
level, ok := C.LogLevelMapping[*general.LogLevel]
|
||||||
|
if !ok {
|
||||||
|
logLevelErr = fmt.Errorf("Log Level error")
|
||||||
|
} else {
|
||||||
|
cfg.SetLogLevel(level)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
hasError, errors := formatErrors(map[string]error{
|
||||||
|
"mode": modeErr,
|
||||||
|
"log-level": logLevelErr,
|
||||||
|
})
|
||||||
|
|
||||||
|
if hasError {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
render.JSON(w, r, errors)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// update proxy
|
||||||
|
cfg.UpdateProxy(config.ProxyConfig{
|
||||||
|
AllowLan: general.AllowLan,
|
||||||
|
Port: general.Port,
|
||||||
|
SocksPort: general.SocksPort,
|
||||||
|
RedirPort: general.RedirPort,
|
||||||
|
})
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
}
|
193
hub/proxies.go
Normal file
193
hub/proxies.go
Normal file
@ -0,0 +1,193 @@
|
|||||||
|
package hub
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
A "github.com/Dreamacro/clash/adapters/remote"
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi"
|
||||||
|
"github.com/go-chi/render"
|
||||||
|
)
|
||||||
|
|
||||||
|
func proxyRouter() http.Handler {
|
||||||
|
r := chi.NewRouter()
|
||||||
|
r.Get("/", getProxies)
|
||||||
|
r.Get("/{name}", getProxy)
|
||||||
|
r.Get("/{name}/delay", getProxyDelay)
|
||||||
|
r.Put("/{name}", updateProxy)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
type SampleProxy struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Selector struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Now string `json:"now"`
|
||||||
|
All []string `json:"all"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type URLTest struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Now string `json:"now"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func transformProxy(proxy C.Proxy) interface{} {
|
||||||
|
t := proxy.Type()
|
||||||
|
switch t {
|
||||||
|
case C.Selector:
|
||||||
|
selector := proxy.(*A.Selector)
|
||||||
|
return Selector{
|
||||||
|
Type: t.String(),
|
||||||
|
Now: selector.Now(),
|
||||||
|
All: selector.All(),
|
||||||
|
}
|
||||||
|
case C.URLTest:
|
||||||
|
return URLTest{
|
||||||
|
Type: t.String(),
|
||||||
|
Now: proxy.(*A.URLTest).Now(),
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return SampleProxy{
|
||||||
|
Type: proxy.Type().String(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetProxiesResponse struct {
|
||||||
|
Proxies map[string]interface{} `json:"proxies"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func getProxies(w http.ResponseWriter, r *http.Request) {
|
||||||
|
rawProxies := cfg.Proxies()
|
||||||
|
proxies := make(map[string]interface{})
|
||||||
|
for name, proxy := range rawProxies {
|
||||||
|
proxies[name] = transformProxy(proxy)
|
||||||
|
}
|
||||||
|
render.JSON(w, r, GetProxiesResponse{Proxies: proxies})
|
||||||
|
}
|
||||||
|
|
||||||
|
func getProxy(w http.ResponseWriter, r *http.Request) {
|
||||||
|
name := chi.URLParam(r, "name")
|
||||||
|
proxies := cfg.Proxies()
|
||||||
|
proxy, exist := proxies[name]
|
||||||
|
if !exist {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
render.JSON(w, r, Error{
|
||||||
|
Error: "Proxy not found",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
render.JSON(w, r, transformProxy(proxy))
|
||||||
|
}
|
||||||
|
|
||||||
|
type UpdateProxyRequest struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateProxy(w http.ResponseWriter, r *http.Request) {
|
||||||
|
req := UpdateProxyRequest{}
|
||||||
|
if err := render.DecodeJSON(r.Body, &req); err != nil {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
render.JSON(w, r, Error{
|
||||||
|
Error: "Format error",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
name := chi.URLParam(r, "name")
|
||||||
|
proxies := cfg.Proxies()
|
||||||
|
proxy, exist := proxies[name]
|
||||||
|
if !exist {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
render.JSON(w, r, Error{
|
||||||
|
Error: "Proxy not found",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
selector, ok := proxy.(*A.Selector)
|
||||||
|
if !ok {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
render.JSON(w, r, Error{
|
||||||
|
Error: "Proxy can't update",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := selector.Set(req.Name); err != nil {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
render.JSON(w, r, Error{
|
||||||
|
Error: fmt.Sprintf("Selector update error: %s", err.Error()),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetProxyDelayRequest struct {
|
||||||
|
URL string `json:"url"`
|
||||||
|
Timeout int16 `json:"timeout"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetProxyDelayResponse struct {
|
||||||
|
Delay int16 `json:"delay"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func getProxyDelay(w http.ResponseWriter, r *http.Request) {
|
||||||
|
query := r.URL.Query()
|
||||||
|
url := query.Get("url")
|
||||||
|
timeout, err := strconv.ParseInt(query.Get("timeout"), 10, 16)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
render.JSON(w, r, Error{
|
||||||
|
Error: "Format error",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
name := chi.URLParam(r, "name")
|
||||||
|
proxies := cfg.Proxies()
|
||||||
|
proxy, exist := proxies[name]
|
||||||
|
if !exist {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
render.JSON(w, r, Error{
|
||||||
|
Error: "Proxy not found",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sigCh := make(chan int16)
|
||||||
|
go func() {
|
||||||
|
t, err := A.DelayTest(proxy, url)
|
||||||
|
if err != nil {
|
||||||
|
sigCh <- 0
|
||||||
|
}
|
||||||
|
sigCh <- t
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-time.After(time.Millisecond * time.Duration(timeout)):
|
||||||
|
w.WriteHeader(http.StatusRequestTimeout)
|
||||||
|
render.JSON(w, r, Error{
|
||||||
|
Error: "Proxy delay test timeout",
|
||||||
|
})
|
||||||
|
case t := <-sigCh:
|
||||||
|
if t == 0 {
|
||||||
|
w.WriteHeader(http.StatusServiceUnavailable)
|
||||||
|
render.JSON(w, r, Error{
|
||||||
|
Error: "An error occurred in the delay test",
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
render.JSON(w, r, GetProxyDelayResponse{
|
||||||
|
Delay: t,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
53
hub/rules.go
Normal file
53
hub/rules.go
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package hub
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi"
|
||||||
|
"github.com/go-chi/render"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ruleRouter() http.Handler {
|
||||||
|
r := chi.NewRouter()
|
||||||
|
r.Get("/", getRules)
|
||||||
|
r.Put("/", updateRules)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
type Rule struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Payload string `json:"type"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetRulesResponse struct {
|
||||||
|
Rules []Rule `json:"rules"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRules(w http.ResponseWriter, r *http.Request) {
|
||||||
|
rawRules := cfg.Rules()
|
||||||
|
|
||||||
|
var rules []Rule
|
||||||
|
for _, rule := range rawRules {
|
||||||
|
rules = append(rules, Rule{
|
||||||
|
Name: rule.RuleType().String(),
|
||||||
|
Payload: rule.Payload(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
render.JSON(w, r, GetRulesResponse{
|
||||||
|
Rules: rules,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateRules(w http.ResponseWriter, r *http.Request) {
|
||||||
|
err := cfg.UpdateRules()
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
render.JSON(w, r, Error{
|
||||||
|
Error: err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
}
|
137
hub/server.go
Normal file
137
hub/server.go
Normal file
@ -0,0 +1,137 @@
|
|||||||
|
package hub
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/Dreamacro/clash/config"
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
T "github.com/Dreamacro/clash/tunnel"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi"
|
||||||
|
"github.com/go-chi/cors"
|
||||||
|
"github.com/go-chi/render"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Traffic struct {
|
||||||
|
Up int64 `json:"up"`
|
||||||
|
Down int64 `json:"down"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func newHub(signal chan struct{}) {
|
||||||
|
var addr string
|
||||||
|
ch := config.Instance().Subscribe()
|
||||||
|
signal <- struct{}{}
|
||||||
|
for {
|
||||||
|
elm := <-ch
|
||||||
|
event := elm.(*config.Event)
|
||||||
|
if event.Type == "external-controller" {
|
||||||
|
addr = event.Payload.(string)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
r := chi.NewRouter()
|
||||||
|
|
||||||
|
cors := cors.New(cors.Options{
|
||||||
|
AllowedOrigins: []string{"*"},
|
||||||
|
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
||||||
|
AllowedHeaders: []string{"Content-Type"},
|
||||||
|
MaxAge: 300,
|
||||||
|
})
|
||||||
|
|
||||||
|
r.Use(cors.Handler)
|
||||||
|
|
||||||
|
r.With(jsonContentType).Get("/traffic", traffic)
|
||||||
|
r.With(jsonContentType).Get("/logs", getLogs)
|
||||||
|
r.Mount("/configs", configRouter())
|
||||||
|
r.Mount("/proxies", proxyRouter())
|
||||||
|
r.Mount("/rules", ruleRouter())
|
||||||
|
|
||||||
|
log.Infof("RESTful API listening at: %s", addr)
|
||||||
|
err := http.ListenAndServe(addr, r)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("External controller error: %s", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func jsonContentType(next http.Handler) http.Handler {
|
||||||
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
return http.HandlerFunc(fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func traffic(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
|
tick := time.NewTicker(time.Second)
|
||||||
|
t := tunnel.Traffic()
|
||||||
|
for range tick.C {
|
||||||
|
up, down := t.Now()
|
||||||
|
if err := json.NewEncoder(w).Encode(Traffic{
|
||||||
|
Up: up,
|
||||||
|
Down: down,
|
||||||
|
}); err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
w.(http.Flusher).Flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Log struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Payload string `json:"payload"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func getLogs(w http.ResponseWriter, r *http.Request) {
|
||||||
|
levelText := r.URL.Query().Get("level")
|
||||||
|
if levelText == "" {
|
||||||
|
levelText = "info"
|
||||||
|
}
|
||||||
|
|
||||||
|
level, ok := C.LogLevelMapping[levelText]
|
||||||
|
if !ok {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
render.JSON(w, r, Error{
|
||||||
|
Error: "Level error",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
src := tunnel.Log()
|
||||||
|
sub, err := src.Subscribe()
|
||||||
|
defer src.UnSubscribe(sub)
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
render.JSON(w, r, Error{
|
||||||
|
Error: err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
render.Status(r, http.StatusOK)
|
||||||
|
for elm := range sub {
|
||||||
|
log := elm.(T.Log)
|
||||||
|
if log.LogLevel > level {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.NewEncoder(w).Encode(Log{
|
||||||
|
Type: log.Type(),
|
||||||
|
Payload: log.Payload,
|
||||||
|
}); err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
w.(http.Flusher).Flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run initial hub
|
||||||
|
func Run() {
|
||||||
|
signal := make(chan struct{})
|
||||||
|
go newHub(signal)
|
||||||
|
<-signal
|
||||||
|
}
|
29
main.go
29
main.go
@ -5,38 +5,25 @@ import (
|
|||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
C "github.com/Dreamacro/clash/constant"
|
"github.com/Dreamacro/clash/config"
|
||||||
"github.com/Dreamacro/clash/proxy/http"
|
"github.com/Dreamacro/clash/hub"
|
||||||
"github.com/Dreamacro/clash/proxy/socks"
|
"github.com/Dreamacro/clash/proxy"
|
||||||
"github.com/Dreamacro/clash/tunnel"
|
"github.com/Dreamacro/clash/tunnel"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cfg, err := C.GetConfig()
|
tunnel.Instance().Run()
|
||||||
if err != nil {
|
proxy.Instance().Run()
|
||||||
log.Fatalf("Read config error: %s", err.Error())
|
hub.Run()
|
||||||
}
|
|
||||||
|
|
||||||
port, socksPort := C.DefalutHTTPPort, C.DefalutSOCKSPort
|
config.Init()
|
||||||
section := cfg.Section("General")
|
err := config.Instance().Parse()
|
||||||
if key, err := section.GetKey("port"); err == nil {
|
|
||||||
port = key.Value()
|
|
||||||
}
|
|
||||||
|
|
||||||
if key, err := section.GetKey("socks-port"); err == nil {
|
|
||||||
socksPort = key.Value()
|
|
||||||
}
|
|
||||||
|
|
||||||
err = tunnel.GetInstance().UpdateConfig()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Parse config error: %s", err.Error())
|
log.Fatalf("Parse config error: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
go http.NewHttpProxy(port)
|
|
||||||
go socks.NewSocksProxy(socksPort)
|
|
||||||
|
|
||||||
sigCh := make(chan os.Signal, 1)
|
sigCh := make(chan os.Signal, 1)
|
||||||
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
|
||||||
<-sigCh
|
<-sigCh
|
||||||
|
@ -1,18 +1,3 @@
|
|||||||
package observable
|
package observable
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Iterable <-chan interface{}
|
type Iterable <-chan interface{}
|
||||||
|
|
||||||
func NewIterable(any interface{}) (Iterable, error) {
|
|
||||||
switch any := any.(type) {
|
|
||||||
case chan interface{}:
|
|
||||||
return Iterable(any), nil
|
|
||||||
case <-chan interface{}:
|
|
||||||
return Iterable(any), nil
|
|
||||||
default:
|
|
||||||
return nil, errors.New("type error")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -50,7 +50,6 @@ func (o *Observable) Subscribe() (Subscription, error) {
|
|||||||
func (o *Observable) UnSubscribe(sub Subscription) {
|
func (o *Observable) UnSubscribe(sub Subscription) {
|
||||||
elm, exist := o.listener.Load(sub)
|
elm, exist := o.listener.Load(sub)
|
||||||
if !exist {
|
if !exist {
|
||||||
println("not exist")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
subscriber := elm.(*Subscriber)
|
subscriber := elm.(*Subscriber)
|
||||||
|
@ -27,11 +27,7 @@ func TestObservable(t *testing.T) {
|
|||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
count := 0
|
count := 0
|
||||||
for {
|
for range data {
|
||||||
_, open := <-data
|
|
||||||
if !open {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
count = count + 1
|
count = count + 1
|
||||||
}
|
}
|
||||||
if count != 5 {
|
if count != 5 {
|
||||||
@ -49,11 +45,7 @@ func TestObservable_MutilSubscribe(t *testing.T) {
|
|||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
wg.Add(2)
|
wg.Add(2)
|
||||||
waitCh := func(ch <-chan interface{}) {
|
waitCh := func(ch <-chan interface{}) {
|
||||||
for {
|
for range ch {
|
||||||
_, open := <-ch
|
|
||||||
if !open {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
count = count + 1
|
count = count + 1
|
||||||
}
|
}
|
||||||
wg.Done()
|
wg.Done()
|
||||||
@ -80,6 +72,25 @@ func TestObservable_UnSubscribe(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestObservable_SubscribeClosedSource(t *testing.T) {
|
||||||
|
iter := iterator([]interface{}{1})
|
||||||
|
src := NewObservable(iter)
|
||||||
|
data, _ := src.Subscribe()
|
||||||
|
<-data
|
||||||
|
|
||||||
|
_, closed := src.Subscribe()
|
||||||
|
if closed == nil {
|
||||||
|
t.Error("Observable should be closed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestObservable_UnSubscribeWithNotExistSubscription(t *testing.T) {
|
||||||
|
sub := Subscription(make(chan interface{}))
|
||||||
|
iter := iterator([]interface{}{1})
|
||||||
|
src := NewObservable(iter)
|
||||||
|
src.UnSubscribe(sub)
|
||||||
|
}
|
||||||
|
|
||||||
func TestObservable_SubscribeGoroutineLeak(t *testing.T) {
|
func TestObservable_SubscribeGoroutineLeak(t *testing.T) {
|
||||||
// waiting for other goroutine recycle
|
// waiting for other goroutine recycle
|
||||||
time.Sleep(120 * time.Millisecond)
|
time.Sleep(120 * time.Millisecond)
|
||||||
@ -97,11 +108,7 @@ func TestObservable_SubscribeGoroutineLeak(t *testing.T) {
|
|||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
wg.Add(max)
|
wg.Add(max)
|
||||||
waitCh := func(ch <-chan interface{}) {
|
waitCh := func(ch <-chan interface{}) {
|
||||||
for {
|
for range ch {
|
||||||
_, open := <-ch
|
|
||||||
if !open {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}
|
}
|
||||||
|
@ -1,15 +0,0 @@
|
|||||||
package observable
|
|
||||||
|
|
||||||
func mergeWithBytes(ch <-chan interface{}, buf []byte) chan interface{} {
|
|
||||||
out := make(chan interface{})
|
|
||||||
go func() {
|
|
||||||
defer close(out)
|
|
||||||
if len(buf) != 0 {
|
|
||||||
out <- buf
|
|
||||||
}
|
|
||||||
for elm := range ch {
|
|
||||||
out <- elm
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
return out
|
|
||||||
}
|
|
@ -1,62 +0,0 @@
|
|||||||
package http
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
C "github.com/Dreamacro/clash/constant"
|
|
||||||
)
|
|
||||||
|
|
||||||
type HttpAdapter struct {
|
|
||||||
addr *C.Addr
|
|
||||||
r *http.Request
|
|
||||||
w http.ResponseWriter
|
|
||||||
done chan struct{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *HttpAdapter) Close() {
|
|
||||||
h.done <- struct{}{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *HttpAdapter) Addr() *C.Addr {
|
|
||||||
return h.addr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *HttpAdapter) Connect(proxy C.ProxyAdapter) {
|
|
||||||
req := http.Transport{
|
|
||||||
Dial: func(string, string) (net.Conn, error) {
|
|
||||||
return proxy.Conn(), nil
|
|
||||||
},
|
|
||||||
// from http.DefaultTransport
|
|
||||||
MaxIdleConns: 100,
|
|
||||||
IdleConnTimeout: 90 * time.Second,
|
|
||||||
TLSHandshakeTimeout: 10 * time.Second,
|
|
||||||
ExpectContinueTimeout: 1 * time.Second,
|
|
||||||
}
|
|
||||||
resp, err := req.RoundTrip(h.r)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
header := h.w.Header()
|
|
||||||
for k, vv := range resp.Header {
|
|
||||||
for _, v := range vv {
|
|
||||||
header.Add(k, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
h.w.WriteHeader(resp.StatusCode)
|
|
||||||
io.Copy(h.w, resp.Body)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewHttp(host string, w http.ResponseWriter, r *http.Request) (*HttpAdapter, chan struct{}) {
|
|
||||||
done := make(chan struct{})
|
|
||||||
return &HttpAdapter{
|
|
||||||
addr: parseHttpAddr(host),
|
|
||||||
r: r,
|
|
||||||
w: w,
|
|
||||||
done: done,
|
|
||||||
}, done
|
|
||||||
}
|
|
@ -1,35 +0,0 @@
|
|||||||
package http
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"io"
|
|
||||||
"net"
|
|
||||||
|
|
||||||
C "github.com/Dreamacro/clash/constant"
|
|
||||||
)
|
|
||||||
|
|
||||||
type HttpsAdapter struct {
|
|
||||||
addr *C.Addr
|
|
||||||
conn net.Conn
|
|
||||||
rw *bufio.ReadWriter
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *HttpsAdapter) Close() {
|
|
||||||
h.conn.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *HttpsAdapter) Addr() *C.Addr {
|
|
||||||
return h.addr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *HttpsAdapter) Connect(proxy C.ProxyAdapter) {
|
|
||||||
go io.Copy(h.conn, proxy.ReadWriter())
|
|
||||||
io.Copy(proxy.ReadWriter(), h.conn)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewHttps(host string, conn net.Conn) *HttpsAdapter {
|
|
||||||
return &HttpsAdapter{
|
|
||||||
addr: parseHttpAddr(host),
|
|
||||||
conn: conn,
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,86 +1,74 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"bufio"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
|
"github.com/Dreamacro/clash/adapters/local"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/tunnel"
|
"github.com/Dreamacro/clash/tunnel"
|
||||||
|
|
||||||
"github.com/riobard/go-shadowsocks2/socks"
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
tun = tunnel.GetInstance()
|
tun = tunnel.Instance()
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewHttpProxy(port string) {
|
func NewHttpProxy(addr string) (*C.ProxySignal, error) {
|
||||||
server := &http.Server{
|
l, err := net.Listen("tcp", addr)
|
||||||
Addr: fmt.Sprintf(":%s", port),
|
|
||||||
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if r.Method == http.MethodConnect {
|
|
||||||
handleTunneling(w, r)
|
|
||||||
} else {
|
|
||||||
handleHTTP(w, r)
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
log.Infof("HTTP proxy :%s", port)
|
|
||||||
server.ListenAndServe()
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleHTTP(w http.ResponseWriter, r *http.Request) {
|
|
||||||
addr := r.Host
|
|
||||||
// padding default port
|
|
||||||
if !strings.Contains(addr, ":") {
|
|
||||||
addr += ":80"
|
|
||||||
}
|
|
||||||
req, done := NewHttp(addr, w, r)
|
|
||||||
tun.Add(req)
|
|
||||||
<-done
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleTunneling(w http.ResponseWriter, r *http.Request) {
|
|
||||||
hijacker, ok := w.(http.Hijacker)
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
conn, _, err := hijacker.Hijack()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
done := make(chan struct{})
|
||||||
|
closed := make(chan struct{})
|
||||||
|
signal := &C.ProxySignal{
|
||||||
|
Done: done,
|
||||||
|
Closed: closed,
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
log.Infof("HTTP proxy listening at: %s", addr)
|
||||||
|
for {
|
||||||
|
c, err := l.Accept()
|
||||||
|
if err != nil {
|
||||||
|
if _, open := <-done; !open {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
go handleConn(c)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
<-done
|
||||||
|
close(done)
|
||||||
|
l.Close()
|
||||||
|
closed <- struct{}{}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return signal, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleConn(conn net.Conn) {
|
||||||
|
br := bufio.NewReader(conn)
|
||||||
|
request, err := http.ReadRequest(br)
|
||||||
|
if err != nil {
|
||||||
|
conn.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// w.WriteHeader(http.StatusOK) doesn't works in Safari
|
|
||||||
conn.Write([]byte("HTTP/1.1 200 OK\r\n\r\n"))
|
if request.Method == http.MethodConnect {
|
||||||
tun.Add(NewHttps(r.Host, conn))
|
_, err := conn.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n"))
|
||||||
}
|
if err != nil {
|
||||||
|
return
|
||||||
func parseHttpAddr(target string) *C.Addr {
|
}
|
||||||
host, port, _ := net.SplitHostPort(target)
|
tun.Add(adapters.NewHTTPS(request, conn))
|
||||||
ipAddr, err := net.ResolveIPAddr("ip", host)
|
return
|
||||||
var resolveIP *net.IP
|
}
|
||||||
if err == nil {
|
|
||||||
resolveIP = &ipAddr.IP
|
tun.Add(adapters.NewHTTP(request, conn))
|
||||||
}
|
|
||||||
|
|
||||||
var addType int
|
|
||||||
ip := net.ParseIP(host)
|
|
||||||
switch {
|
|
||||||
case ip == nil:
|
|
||||||
addType = socks.AtypDomainName
|
|
||||||
case ip.To4() == nil:
|
|
||||||
addType = socks.AtypIPv6
|
|
||||||
default:
|
|
||||||
addType = socks.AtypIPv4
|
|
||||||
}
|
|
||||||
|
|
||||||
return &C.Addr{
|
|
||||||
NetWork: C.TCP,
|
|
||||||
AddrType: addType,
|
|
||||||
Host: host,
|
|
||||||
IP: resolveIP,
|
|
||||||
Port: port,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
116
proxy/listener.go
Normal file
116
proxy/listener.go
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
package proxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/Dreamacro/clash/config"
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
"github.com/Dreamacro/clash/proxy/http"
|
||||||
|
"github.com/Dreamacro/clash/proxy/redir"
|
||||||
|
"github.com/Dreamacro/clash/proxy/socks"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
listener *Listener
|
||||||
|
once sync.Once
|
||||||
|
)
|
||||||
|
|
||||||
|
type Listener struct {
|
||||||
|
// signal for update
|
||||||
|
httpSignal *C.ProxySignal
|
||||||
|
socksSignal *C.ProxySignal
|
||||||
|
redirSignal *C.ProxySignal
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Listener) updateHTTP(addr string) error {
|
||||||
|
if l.httpSignal != nil {
|
||||||
|
signal := l.httpSignal
|
||||||
|
signal.Done <- struct{}{}
|
||||||
|
<-signal.Closed
|
||||||
|
l.httpSignal = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
signal, err := http.NewHttpProxy(addr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
l.httpSignal = signal
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Listener) updateSocks(addr string) error {
|
||||||
|
if l.socksSignal != nil {
|
||||||
|
signal := l.socksSignal
|
||||||
|
signal.Done <- struct{}{}
|
||||||
|
<-signal.Closed
|
||||||
|
l.socksSignal = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
signal, err := socks.NewSocksProxy(addr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
l.socksSignal = signal
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Listener) updateRedir(addr string) error {
|
||||||
|
if l.redirSignal != nil {
|
||||||
|
signal := l.redirSignal
|
||||||
|
signal.Done <- struct{}{}
|
||||||
|
<-signal.Closed
|
||||||
|
l.redirSignal = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
signal, err := redir.NewRedirProxy(addr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
l.redirSignal = signal
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Listener) process(signal chan<- struct{}) {
|
||||||
|
sub := config.Instance().Subscribe()
|
||||||
|
signal <- struct{}{}
|
||||||
|
reportCH := config.Instance().Report()
|
||||||
|
for elm := range sub {
|
||||||
|
event := elm.(*config.Event)
|
||||||
|
switch event.Type {
|
||||||
|
case "http-addr":
|
||||||
|
addr := event.Payload.(string)
|
||||||
|
err := l.updateHTTP(addr)
|
||||||
|
reportCH <- &config.Event{Type: "http-addr", Payload: err == nil}
|
||||||
|
case "socks-addr":
|
||||||
|
addr := event.Payload.(string)
|
||||||
|
err := l.updateSocks(addr)
|
||||||
|
reportCH <- &config.Event{Type: "socks-addr", Payload: err == nil}
|
||||||
|
case "redir-addr":
|
||||||
|
addr := event.Payload.(string)
|
||||||
|
err := l.updateRedir(addr)
|
||||||
|
reportCH <- &config.Event{Type: "redir-addr", Payload: err == nil}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run ensure config monitoring
|
||||||
|
func (l *Listener) Run() {
|
||||||
|
signal := make(chan struct{})
|
||||||
|
go l.process(signal)
|
||||||
|
<-signal
|
||||||
|
}
|
||||||
|
|
||||||
|
func newListener() *Listener {
|
||||||
|
return &Listener{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Instance return singleton instance of Listener
|
||||||
|
func Instance() *Listener {
|
||||||
|
once.Do(func() {
|
||||||
|
listener = newListener()
|
||||||
|
})
|
||||||
|
return listener
|
||||||
|
}
|
62
proxy/redir/tcp.go
Normal file
62
proxy/redir/tcp.go
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
package redir
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/Dreamacro/clash/adapters/local"
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
"github.com/Dreamacro/clash/tunnel"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
tun = tunnel.Instance()
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewRedirProxy(addr string) (*C.ProxySignal, error) {
|
||||||
|
l, err := net.Listen("tcp", addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
done := make(chan struct{})
|
||||||
|
closed := make(chan struct{})
|
||||||
|
signal := &C.ProxySignal{
|
||||||
|
Done: done,
|
||||||
|
Closed: closed,
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
log.Infof("Redir proxy listening at: %s", addr)
|
||||||
|
for {
|
||||||
|
c, err := l.Accept()
|
||||||
|
if err != nil {
|
||||||
|
if _, open := <-done; !open {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
go handleRedir(c)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
<-done
|
||||||
|
close(done)
|
||||||
|
l.Close()
|
||||||
|
closed <- struct{}{}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return signal, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleRedir(conn net.Conn) {
|
||||||
|
target, err := parserPacket(conn)
|
||||||
|
if err != nil {
|
||||||
|
conn.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
conn.(*net.TCPConn).SetKeepAlive(true)
|
||||||
|
tun.Add(adapters.NewSocket(target, conn))
|
||||||
|
}
|
58
proxy/redir/tcp_darwin.go
Normal file
58
proxy/redir/tcp_darwin.go
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package redir
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"syscall"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/Dreamacro/go-shadowsocks2/socks"
|
||||||
|
)
|
||||||
|
|
||||||
|
func parserPacket(c net.Conn) (socks.Addr, error) {
|
||||||
|
const (
|
||||||
|
PfInout = 0
|
||||||
|
PfIn = 1
|
||||||
|
PfOut = 2
|
||||||
|
IOCOut = 0x40000000
|
||||||
|
IOCIn = 0x80000000
|
||||||
|
IOCInOut = IOCIn | IOCOut
|
||||||
|
IOCPARMMask = 0x1FFF
|
||||||
|
LEN = 4*16 + 4*4 + 4*1
|
||||||
|
// #define _IOC(inout,group,num,len) (inout | ((len & IOCPARMMask) << 16) | ((group) << 8) | (num))
|
||||||
|
// #define _IOWR(g,n,t) _IOC(IOCInOut, (g), (n), sizeof(t))
|
||||||
|
// #define DIOCNATLOOK _IOWR('D', 23, struct pfioc_natlook)
|
||||||
|
DIOCNATLOOK = IOCInOut | ((LEN & IOCPARMMask) << 16) | ('D' << 8) | 23
|
||||||
|
)
|
||||||
|
|
||||||
|
fd, err := syscall.Open("/dev/pf", 0, syscall.O_RDONLY)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer syscall.Close(fd)
|
||||||
|
|
||||||
|
nl := struct { // struct pfioc_natlook
|
||||||
|
saddr, daddr, rsaddr, rdaddr [16]byte
|
||||||
|
sxport, dxport, rsxport, rdxport [4]byte
|
||||||
|
af, proto, protoVariant, direction uint8
|
||||||
|
}{
|
||||||
|
af: syscall.AF_INET,
|
||||||
|
proto: syscall.IPPROTO_TCP,
|
||||||
|
direction: PfOut,
|
||||||
|
}
|
||||||
|
saddr := c.RemoteAddr().(*net.TCPAddr)
|
||||||
|
daddr := c.LocalAddr().(*net.TCPAddr)
|
||||||
|
copy(nl.saddr[:], saddr.IP)
|
||||||
|
copy(nl.daddr[:], daddr.IP)
|
||||||
|
nl.sxport[0], nl.sxport[1] = byte(saddr.Port>>8), byte(saddr.Port)
|
||||||
|
nl.dxport[0], nl.dxport[1] = byte(daddr.Port>>8), byte(daddr.Port)
|
||||||
|
|
||||||
|
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), DIOCNATLOOK, uintptr(unsafe.Pointer(&nl))); errno != 0 {
|
||||||
|
return nil, errno
|
||||||
|
}
|
||||||
|
|
||||||
|
addr := make([]byte, 1+net.IPv4len+2)
|
||||||
|
addr[0] = socks.AtypIPv4
|
||||||
|
copy(addr[1:1+net.IPv4len], nl.rdaddr[:4])
|
||||||
|
copy(addr[1+net.IPv4len:], nl.rdxport[:2])
|
||||||
|
return addr, nil
|
||||||
|
}
|
51
proxy/redir/tcp_linux.go
Normal file
51
proxy/redir/tcp_linux.go
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
package redir
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net"
|
||||||
|
"syscall"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/Dreamacro/go-shadowsocks2/socks"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
SO_ORIGINAL_DST = 80 // from linux/include/uapi/linux/netfilter_ipv4.h
|
||||||
|
IP6T_SO_ORIGINAL_DST = 80 // from linux/include/uapi/linux/netfilter_ipv6/ip6_tables.h
|
||||||
|
)
|
||||||
|
|
||||||
|
func parserPacket(conn net.Conn) (socks.Addr, error) {
|
||||||
|
c, ok := conn.(*net.TCPConn)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("only work with TCP connection")
|
||||||
|
}
|
||||||
|
|
||||||
|
rc, err := c.SyscallConn()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var addr socks.Addr
|
||||||
|
|
||||||
|
rc.Control(func(fd uintptr) {
|
||||||
|
addr, err = getorigdst(fd)
|
||||||
|
})
|
||||||
|
|
||||||
|
return addr, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call getorigdst() from linux/net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c
|
||||||
|
func getorigdst(fd uintptr) (socks.Addr, error) {
|
||||||
|
raw := syscall.RawSockaddrInet4{}
|
||||||
|
siz := unsafe.Sizeof(raw)
|
||||||
|
if err := socketcall(GETSOCKOPT, fd, syscall.IPPROTO_IP, SO_ORIGINAL_DST, uintptr(unsafe.Pointer(&raw)), uintptr(unsafe.Pointer(&siz)), 0); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
addr := make([]byte, 1+net.IPv4len+2)
|
||||||
|
addr[0] = socks.AtypIPv4
|
||||||
|
copy(addr[1:1+net.IPv4len], raw.Addr[:])
|
||||||
|
port := (*[2]byte)(unsafe.Pointer(&raw.Port)) // big-endian
|
||||||
|
addr[1+net.IPv4len], addr[1+net.IPv4len+1] = port[0], port[1]
|
||||||
|
return addr, nil
|
||||||
|
}
|
17
proxy/redir/tcp_linux_386.go
Normal file
17
proxy/redir/tcp_linux_386.go
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package redir
|
||||||
|
|
||||||
|
import (
|
||||||
|
"syscall"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
const GETSOCKOPT = 15 // https://golang.org/src/syscall/syscall_linux_386.go#L183
|
||||||
|
|
||||||
|
func socketcall(call, a0, a1, a2, a3, a4, a5 uintptr) error {
|
||||||
|
var a [6]uintptr
|
||||||
|
a[0], a[1], a[2], a[3], a[4], a[5] = a0, a1, a2, a3, a4, a5
|
||||||
|
if _, _, errno := syscall.Syscall6(syscall.SYS_SOCKETCALL, call, uintptr(unsafe.Pointer(&a)), 0, 0, 0, 0); errno != 0 {
|
||||||
|
return errno
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
14
proxy/redir/tcp_linux_other.go
Normal file
14
proxy/redir/tcp_linux_other.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
// +build linux,!386
|
||||||
|
|
||||||
|
package redir
|
||||||
|
|
||||||
|
import "syscall"
|
||||||
|
|
||||||
|
const GETSOCKOPT = syscall.SYS_GETSOCKOPT
|
||||||
|
|
||||||
|
func socketcall(call, a0, a1, a2, a3, a4, a5 uintptr) error {
|
||||||
|
if _, _, errno := syscall.Syscall6(call, a0, a1, a2, a3, a4, a5); errno != 0 {
|
||||||
|
return errno
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
12
proxy/redir/tcp_windows.go
Normal file
12
proxy/redir/tcp_windows.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package redir
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/Dreamacro/go-shadowsocks2/socks"
|
||||||
|
)
|
||||||
|
|
||||||
|
func parserPacket(conn net.Conn) (socks.Addr, error) {
|
||||||
|
return nil, errors.New("Windows not support yet")
|
||||||
|
}
|
@ -1,36 +1,55 @@
|
|||||||
package socks
|
package socks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
|
"github.com/Dreamacro/clash/adapters/local"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/tunnel"
|
"github.com/Dreamacro/clash/tunnel"
|
||||||
|
|
||||||
"github.com/riobard/go-shadowsocks2/socks"
|
"github.com/Dreamacro/go-shadowsocks2/socks"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
tun = tunnel.GetInstance()
|
tun = tunnel.Instance()
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewSocksProxy(port string) {
|
func NewSocksProxy(addr string) (*C.ProxySignal, error) {
|
||||||
l, err := net.Listen("tcp", fmt.Sprintf(":%s", port))
|
l, err := net.Listen("tcp", addr)
|
||||||
defer l.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return nil, err
|
||||||
}
|
}
|
||||||
log.Infof("SOCKS proxy :%s", port)
|
|
||||||
for {
|
done := make(chan struct{})
|
||||||
c, err := l.Accept()
|
closed := make(chan struct{})
|
||||||
if err != nil {
|
signal := &C.ProxySignal{
|
||||||
continue
|
Done: done,
|
||||||
|
Closed: closed,
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
log.Infof("SOCKS proxy listening at: %s", addr)
|
||||||
|
for {
|
||||||
|
c, err := l.Accept()
|
||||||
|
if err != nil {
|
||||||
|
if _, open := <-done; !open {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
go handleSocks(c)
|
||||||
}
|
}
|
||||||
go handleSocks(c)
|
}()
|
||||||
}
|
|
||||||
|
go func() {
|
||||||
|
<-done
|
||||||
|
close(done)
|
||||||
|
l.Close()
|
||||||
|
closed <- struct{}{}
|
||||||
|
}()
|
||||||
|
|
||||||
|
return signal, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleSocks(conn net.Conn) {
|
func handleSocks(conn net.Conn) {
|
||||||
@ -40,59 +59,5 @@ func handleSocks(conn net.Conn) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
conn.(*net.TCPConn).SetKeepAlive(true)
|
conn.(*net.TCPConn).SetKeepAlive(true)
|
||||||
tun.Add(NewSocks(target, conn))
|
tun.Add(adapters.NewSocket(target, conn))
|
||||||
}
|
|
||||||
|
|
||||||
type SocksAdapter struct {
|
|
||||||
conn net.Conn
|
|
||||||
addr *C.Addr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SocksAdapter) Close() {
|
|
||||||
s.conn.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SocksAdapter) Addr() *C.Addr {
|
|
||||||
return s.addr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SocksAdapter) Connect(proxy C.ProxyAdapter) {
|
|
||||||
go io.Copy(s.conn, proxy.ReadWriter())
|
|
||||||
io.Copy(proxy.ReadWriter(), s.conn)
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseSocksAddr(target socks.Addr) *C.Addr {
|
|
||||||
var host, port string
|
|
||||||
var ip net.IP
|
|
||||||
|
|
||||||
switch target[0] {
|
|
||||||
case socks.AtypDomainName:
|
|
||||||
host = string(target[2 : 2+target[1]])
|
|
||||||
port = strconv.Itoa((int(target[2+target[1]]) << 8) | int(target[2+target[1]+1]))
|
|
||||||
ipAddr, err := net.ResolveIPAddr("ip", host)
|
|
||||||
if err == nil {
|
|
||||||
ip = ipAddr.IP
|
|
||||||
}
|
|
||||||
case socks.AtypIPv4:
|
|
||||||
ip = net.IP(target[1 : 1+net.IPv4len])
|
|
||||||
port = strconv.Itoa((int(target[1+net.IPv4len]) << 8) | int(target[1+net.IPv4len+1]))
|
|
||||||
case socks.AtypIPv6:
|
|
||||||
ip = net.IP(target[1 : 1+net.IPv6len])
|
|
||||||
port = strconv.Itoa((int(target[1+net.IPv6len]) << 8) | int(target[1+net.IPv6len+1]))
|
|
||||||
}
|
|
||||||
|
|
||||||
return &C.Addr{
|
|
||||||
NetWork: C.TCP,
|
|
||||||
AddrType: int(target[0]),
|
|
||||||
Host: host,
|
|
||||||
IP: &ip,
|
|
||||||
Port: port,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSocks(target socks.Addr, conn net.Conn) *SocksAdapter {
|
|
||||||
return &SocksAdapter{
|
|
||||||
conn: conn,
|
|
||||||
addr: parseSocksAddr(target),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,10 @@ func (dk *DomainKeyword) Adapter() string {
|
|||||||
return dk.adapter
|
return dk.adapter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (dk *DomainKeyword) Payload() string {
|
||||||
|
return dk.keyword
|
||||||
|
}
|
||||||
|
|
||||||
func NewDomainKeyword(keyword string, adapter string) *DomainKeyword {
|
func NewDomainKeyword(keyword string, adapter string) *DomainKeyword {
|
||||||
return &DomainKeyword{
|
return &DomainKeyword{
|
||||||
keyword: keyword,
|
keyword: keyword,
|
||||||
|
@ -27,6 +27,10 @@ func (ds *DomainSuffix) Adapter() string {
|
|||||||
return ds.adapter
|
return ds.adapter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ds *DomainSuffix) Payload() string {
|
||||||
|
return ds.suffix
|
||||||
|
}
|
||||||
|
|
||||||
func NewDomainSuffix(suffix string, adapter string) *DomainSuffix {
|
func NewDomainSuffix(suffix string, adapter string) *DomainSuffix {
|
||||||
return &DomainSuffix{
|
return &DomainSuffix{
|
||||||
suffix: suffix,
|
suffix: suffix,
|
||||||
|
36
rules/domian.go
Normal file
36
rules/domian.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package rules
|
||||||
|
|
||||||
|
import (
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Domain struct {
|
||||||
|
domain string
|
||||||
|
adapter string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Domain) RuleType() C.RuleType {
|
||||||
|
return C.Domain
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Domain) IsMatch(addr *C.Addr) bool {
|
||||||
|
if addr.AddrType != C.AtypDomainName {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return addr.Host == d.domain
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Domain) Adapter() string {
|
||||||
|
return d.adapter
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Domain) Payload() string {
|
||||||
|
return d.domain
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDomain(domain string, adapter string) *Domain {
|
||||||
|
return &Domain{
|
||||||
|
domain: domain,
|
||||||
|
adapter: adapter,
|
||||||
|
}
|
||||||
|
}
|
@ -20,6 +20,10 @@ func (f *Final) Adapter() string {
|
|||||||
return f.adapter
|
return f.adapter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *Final) Payload() string {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func NewFinal(adapter string) *Final {
|
func NewFinal(adapter string) *Final {
|
||||||
return &Final{
|
return &Final{
|
||||||
adapter: adapter,
|
adapter: adapter,
|
||||||
|
@ -1,21 +1,18 @@
|
|||||||
package rules
|
package rules
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync"
|
||||||
|
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
|
||||||
"github.com/oschwald/geoip2-golang"
|
"github.com/oschwald/geoip2-golang"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
var mmdb *geoip2.Reader
|
var (
|
||||||
|
mmdb *geoip2.Reader
|
||||||
func init() {
|
once sync.Once
|
||||||
var err error
|
)
|
||||||
mmdb, err = geoip2.Open(C.MMDBPath)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Can't load mmdb: %s", err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type GEOIP struct {
|
type GEOIP struct {
|
||||||
country string
|
country string
|
||||||
@ -38,7 +35,18 @@ func (g *GEOIP) Adapter() string {
|
|||||||
return g.adapter
|
return g.adapter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *GEOIP) Payload() string {
|
||||||
|
return g.country
|
||||||
|
}
|
||||||
|
|
||||||
func NewGEOIP(country string, adapter string) *GEOIP {
|
func NewGEOIP(country string, adapter string) *GEOIP {
|
||||||
|
once.Do(func() {
|
||||||
|
var err error
|
||||||
|
mmdb, err = geoip2.Open(C.MMDBPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Can't load mmdb: %s", err.Error())
|
||||||
|
}
|
||||||
|
})
|
||||||
return &GEOIP{
|
return &GEOIP{
|
||||||
country: country,
|
country: country,
|
||||||
adapter: adapter,
|
adapter: adapter,
|
||||||
|
@ -23,8 +23,12 @@ func (i *IPCIDR) IsMatch(addr *C.Addr) bool {
|
|||||||
return i.ipnet.Contains(*addr.IP)
|
return i.ipnet.Contains(*addr.IP)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *IPCIDR) Adapter() string {
|
func (i *IPCIDR) Adapter() string {
|
||||||
return g.adapter
|
return i.adapter
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *IPCIDR) Payload() string {
|
||||||
|
return i.ipnet.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewIPCIDR(s string, adapter string) *IPCIDR {
|
func NewIPCIDR(s string, adapter string) *IPCIDR {
|
||||||
|
77
tunnel/connection.go
Normal file
77
tunnel/connection.go
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
package tunnel
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/Dreamacro/clash/adapters/local"
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (t *Tunnel) handleHTTP(request *adapters.HTTPAdapter, proxy C.ProxyAdapter) {
|
||||||
|
conn := newTrafficTrack(proxy.Conn(), t.traffic)
|
||||||
|
req := request.R
|
||||||
|
host := req.Host
|
||||||
|
|
||||||
|
for {
|
||||||
|
req.Header.Set("Connection", "close")
|
||||||
|
req.RequestURI = ""
|
||||||
|
adapters.RemoveHopByHopHeaders(req.Header)
|
||||||
|
err := req.Write(conn)
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
br := bufio.NewReader(conn)
|
||||||
|
resp, err := http.ReadResponse(br, req)
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
adapters.RemoveHopByHopHeaders(resp.Header)
|
||||||
|
if resp.ContentLength >= 0 {
|
||||||
|
resp.Header.Set("Proxy-Connection", "keep-alive")
|
||||||
|
resp.Header.Set("Connection", "keep-alive")
|
||||||
|
resp.Header.Set("Keep-Alive", "timeout=4")
|
||||||
|
resp.Close = false
|
||||||
|
} else {
|
||||||
|
resp.Close = true
|
||||||
|
}
|
||||||
|
err = resp.Write(request.Conn())
|
||||||
|
if err != nil || resp.Close {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err = http.ReadRequest(bufio.NewReader(request.Conn()))
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sometimes firefox just open a socket to process multiple domains in HTTP
|
||||||
|
// The temporary solution is close connection when encountering different HOST
|
||||||
|
if req.Host != host {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Tunnel) handleSOCKS(request *adapters.SocketAdapter, proxy C.ProxyAdapter) {
|
||||||
|
conn := newTrafficTrack(proxy.Conn(), t.traffic)
|
||||||
|
relay(request.Conn(), conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// relay copies between left and right bidirectionally.
|
||||||
|
func relay(leftConn, rightConn net.Conn) {
|
||||||
|
ch := make(chan error)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
_, err := io.Copy(leftConn, rightConn)
|
||||||
|
leftConn.SetReadDeadline(time.Now())
|
||||||
|
ch <- err
|
||||||
|
}()
|
||||||
|
|
||||||
|
io.Copy(rightConn, leftConn)
|
||||||
|
rightConn.SetReadDeadline(time.Now())
|
||||||
|
<-ch
|
||||||
|
}
|
@ -3,32 +3,29 @@ package tunnel
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
INFO LogType = iota
|
|
||||||
WARNING
|
|
||||||
ERROR
|
|
||||||
DEBUG
|
|
||||||
)
|
|
||||||
|
|
||||||
type LogType int
|
|
||||||
|
|
||||||
type Log struct {
|
type Log struct {
|
||||||
LogType LogType
|
LogLevel C.LogLevel
|
||||||
Payload string
|
Payload string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Log) Type() string {
|
||||||
|
return l.LogLevel.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func print(data Log) {
|
func print(data Log) {
|
||||||
switch data.LogType {
|
switch data.LogLevel {
|
||||||
case INFO:
|
case C.INFO:
|
||||||
log.Infoln(data.Payload)
|
log.Infoln(data.Payload)
|
||||||
case WARNING:
|
case C.WARNING:
|
||||||
log.Warnln(data.Payload)
|
log.Warnln(data.Payload)
|
||||||
case ERROR:
|
case C.ERROR:
|
||||||
log.Errorln(data.Payload)
|
log.Errorln(data.Payload)
|
||||||
case DEBUG:
|
case C.DEBUG:
|
||||||
log.Debugln(data.Payload)
|
log.Debugln(data.Payload)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -40,13 +37,15 @@ func (t *Tunnel) subscribeLogs() {
|
|||||||
}
|
}
|
||||||
for elm := range sub {
|
for elm := range sub {
|
||||||
data := elm.(Log)
|
data := elm.(Log)
|
||||||
print(data)
|
if data.LogLevel <= t.logLevel {
|
||||||
|
print(data)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newLog(logType LogType, format string, v ...interface{}) Log {
|
func newLog(logLevel C.LogLevel, format string, v ...interface{}) Log {
|
||||||
return Log{
|
return Log{
|
||||||
LogType: logType,
|
LogLevel: logLevel,
|
||||||
Payload: fmt.Sprintf(format, v...),
|
Payload: fmt.Sprintf(format, v...),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
157
tunnel/tunnel.go
157
tunnel/tunnel.go
@ -1,14 +1,13 @@
|
|||||||
package tunnel
|
package tunnel
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/Dreamacro/clash/adapters"
|
LocalAdapter "github.com/Dreamacro/clash/adapters/local"
|
||||||
|
cfg "github.com/Dreamacro/clash/config"
|
||||||
C "github.com/Dreamacro/clash/constant"
|
C "github.com/Dreamacro/clash/constant"
|
||||||
"github.com/Dreamacro/clash/observable"
|
"github.com/Dreamacro/clash/observable"
|
||||||
R "github.com/Dreamacro/clash/rules"
|
|
||||||
|
|
||||||
"gopkg.in/eapache/channels.v1"
|
"gopkg.in/eapache/channels.v1"
|
||||||
)
|
)
|
||||||
@ -18,79 +17,60 @@ var (
|
|||||||
once sync.Once
|
once sync.Once
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Tunnel handle proxy socket and HTTP/SOCKS socket
|
||||||
type Tunnel struct {
|
type Tunnel struct {
|
||||||
queue *channels.InfiniteChannel
|
queue *channels.InfiniteChannel
|
||||||
rules []C.Rule
|
rules []C.Rule
|
||||||
proxys map[string]C.Proxy
|
proxies map[string]C.Proxy
|
||||||
observable *observable.Observable
|
|
||||||
logCh chan interface{}
|
|
||||||
configLock *sync.RWMutex
|
configLock *sync.RWMutex
|
||||||
|
traffic *C.Traffic
|
||||||
|
|
||||||
|
// Outbound Rule
|
||||||
|
mode cfg.Mode
|
||||||
|
|
||||||
|
// Log
|
||||||
|
logCh chan interface{}
|
||||||
|
observable *observable.Observable
|
||||||
|
logLevel C.LogLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add request to queue
|
||||||
func (t *Tunnel) Add(req C.ServerAdapter) {
|
func (t *Tunnel) Add(req C.ServerAdapter) {
|
||||||
t.queue.In() <- req
|
t.queue.In() <- req
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tunnel) UpdateConfig() (err error) {
|
// Traffic return traffic of all connections
|
||||||
t.configLock.Lock()
|
func (t *Tunnel) Traffic() *C.Traffic {
|
||||||
defer t.configLock.Unlock()
|
return t.traffic
|
||||||
|
}
|
||||||
|
|
||||||
cfg, err := C.GetConfig()
|
// Log return clash log stream
|
||||||
if err != nil {
|
func (t *Tunnel) Log() *observable.Observable {
|
||||||
return
|
return t.observable
|
||||||
}
|
}
|
||||||
|
|
||||||
// clear proxys and rules
|
func (t *Tunnel) configMonitor(signal chan<- struct{}) {
|
||||||
t.proxys = make(map[string]C.Proxy)
|
sub := cfg.Instance().Subscribe()
|
||||||
t.rules = []C.Rule{}
|
signal <- struct{}{}
|
||||||
|
for elm := range sub {
|
||||||
proxys := cfg.Section("Proxy")
|
event := elm.(*cfg.Event)
|
||||||
rules := cfg.Section("Rule")
|
switch event.Type {
|
||||||
|
case "proxies":
|
||||||
// parse proxy
|
proxies := event.Payload.(map[string]C.Proxy)
|
||||||
for _, key := range proxys.Keys() {
|
t.configLock.Lock()
|
||||||
proxy := strings.Split(key.Value(), ",")
|
t.proxies = proxies
|
||||||
if len(proxy) == 0 {
|
t.configLock.Unlock()
|
||||||
continue
|
case "rules":
|
||||||
}
|
rules := event.Payload.([]C.Rule)
|
||||||
proxy = trimArr(proxy)
|
t.configLock.Lock()
|
||||||
switch proxy[0] {
|
t.rules = rules
|
||||||
// ss, server, port, cipter, password
|
t.configLock.Unlock()
|
||||||
case "ss":
|
case "mode":
|
||||||
if len(proxy) < 5 {
|
t.mode = event.Payload.(cfg.Mode)
|
||||||
continue
|
case "log-level":
|
||||||
}
|
t.logLevel = event.Payload.(C.LogLevel)
|
||||||
ssURL := fmt.Sprintf("ss://%s:%s@%s:%s", proxy[3], proxy[4], proxy[1], proxy[2])
|
|
||||||
t.proxys[key.Name()] = adapters.NewShadowSocks(ssURL)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// init proxy
|
|
||||||
t.proxys["DIRECT"] = adapters.NewDirect()
|
|
||||||
t.proxys["REJECT"] = adapters.NewReject()
|
|
||||||
|
|
||||||
// parse rules
|
|
||||||
for _, key := range rules.Keys() {
|
|
||||||
rule := strings.Split(key.Name(), ",")
|
|
||||||
if len(rule) < 3 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
rule = trimArr(rule)
|
|
||||||
switch rule[0] {
|
|
||||||
case "DOMAIN-SUFFIX":
|
|
||||||
t.rules = append(t.rules, R.NewDomainSuffix(rule[1], rule[2]))
|
|
||||||
case "DOMAIN-KEYWORD":
|
|
||||||
t.rules = append(t.rules, R.NewDomainKeyword(rule[1], rule[2]))
|
|
||||||
case "GEOIP":
|
|
||||||
t.rules = append(t.rules, R.NewGEOIP(rule[1], rule[2]))
|
|
||||||
case "IP-CIDR", "IP-CIDR6":
|
|
||||||
t.rules = append(t.rules, R.NewIPCIDR(rule[1], rule[2]))
|
|
||||||
case "FINAL":
|
|
||||||
t.rules = append(t.rules, R.NewFinal(rule[2]))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tunnel) process() {
|
func (t *Tunnel) process() {
|
||||||
@ -105,15 +85,30 @@ func (t *Tunnel) process() {
|
|||||||
func (t *Tunnel) handleConn(localConn C.ServerAdapter) {
|
func (t *Tunnel) handleConn(localConn C.ServerAdapter) {
|
||||||
defer localConn.Close()
|
defer localConn.Close()
|
||||||
addr := localConn.Addr()
|
addr := localConn.Addr()
|
||||||
proxy := t.match(addr)
|
|
||||||
|
var proxy C.Proxy
|
||||||
|
switch t.mode {
|
||||||
|
case cfg.Direct:
|
||||||
|
proxy = t.proxies["DIRECT"]
|
||||||
|
case cfg.Global:
|
||||||
|
proxy = t.proxies["GLOBAL"]
|
||||||
|
// Rule
|
||||||
|
default:
|
||||||
|
proxy = t.match(addr)
|
||||||
|
}
|
||||||
remoConn, err := proxy.Generator(addr)
|
remoConn, err := proxy.Generator(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.logCh <- newLog(WARNING, "Proxy connect error: %s", err.Error())
|
t.logCh <- newLog(C.WARNING, "Proxy connect error: %s", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer remoConn.Close()
|
defer remoConn.Close()
|
||||||
|
|
||||||
localConn.Connect(remoConn)
|
switch adapter := localConn.(type) {
|
||||||
|
case *LocalAdapter.HTTPAdapter:
|
||||||
|
t.handleHTTP(adapter, remoConn)
|
||||||
|
case *LocalAdapter.SocketAdapter:
|
||||||
|
t.handleSOCKS(adapter, remoConn)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tunnel) match(addr *C.Addr) C.Proxy {
|
func (t *Tunnel) match(addr *C.Addr) C.Proxy {
|
||||||
@ -122,33 +117,43 @@ func (t *Tunnel) match(addr *C.Addr) C.Proxy {
|
|||||||
|
|
||||||
for _, rule := range t.rules {
|
for _, rule := range t.rules {
|
||||||
if rule.IsMatch(addr) {
|
if rule.IsMatch(addr) {
|
||||||
a, ok := t.proxys[rule.Adapter()]
|
a, ok := t.proxies[rule.Adapter()]
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
t.logCh <- newLog(INFO, "%v match %d using %s", addr.String(), rule.RuleType(), rule.Adapter())
|
t.logCh <- newLog(C.INFO, "%v match %s using %s", addr.String(), rule.RuleType().String(), rule.Adapter())
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
t.logCh <- newLog(INFO, "don't find, direct")
|
t.logCh <- newLog(C.INFO, "%v doesn't match any rule using DIRECT", addr.String())
|
||||||
return t.proxys["DIRECT"]
|
return t.proxies["DIRECT"]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run initial task
|
||||||
|
func (t *Tunnel) Run() {
|
||||||
|
go t.process()
|
||||||
|
go t.subscribeLogs()
|
||||||
|
signal := make(chan struct{})
|
||||||
|
go t.configMonitor(signal)
|
||||||
|
<-signal
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTunnel() *Tunnel {
|
func newTunnel() *Tunnel {
|
||||||
logCh := make(chan interface{})
|
logCh := make(chan interface{})
|
||||||
tunnel := &Tunnel{
|
return &Tunnel{
|
||||||
queue: channels.NewInfiniteChannel(),
|
queue: channels.NewInfiniteChannel(),
|
||||||
proxys: make(map[string]C.Proxy),
|
proxies: make(map[string]C.Proxy),
|
||||||
observable: observable.NewObservable(logCh),
|
observable: observable.NewObservable(logCh),
|
||||||
logCh: logCh,
|
logCh: logCh,
|
||||||
configLock: &sync.RWMutex{},
|
configLock: &sync.RWMutex{},
|
||||||
|
traffic: C.NewTraffic(time.Second),
|
||||||
|
mode: cfg.Rule,
|
||||||
|
logLevel: C.INFO,
|
||||||
}
|
}
|
||||||
go tunnel.process()
|
|
||||||
go tunnel.subscribeLogs()
|
|
||||||
return tunnel
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetInstance() *Tunnel {
|
// Instance return singleton instance of Tunnel
|
||||||
|
func Instance() *Tunnel {
|
||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
tunnel = newTunnel()
|
tunnel = newTunnel()
|
||||||
})
|
})
|
||||||
|
29
tunnel/util.go
Normal file
29
tunnel/util.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package tunnel
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
|
||||||
|
C "github.com/Dreamacro/clash/constant"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TrafficTrack record traffic of net.Conn
|
||||||
|
type TrafficTrack struct {
|
||||||
|
net.Conn
|
||||||
|
traffic *C.Traffic
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tt *TrafficTrack) Read(b []byte) (int, error) {
|
||||||
|
n, err := tt.Conn.Read(b)
|
||||||
|
tt.traffic.Down() <- int64(n)
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tt *TrafficTrack) Write(b []byte) (int, error) {
|
||||||
|
n, err := tt.Conn.Write(b)
|
||||||
|
tt.traffic.Up() <- int64(n)
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTrafficTrack(conn net.Conn, traffic *C.Traffic) *TrafficTrack {
|
||||||
|
return &TrafficTrack{traffic: traffic, Conn: conn}
|
||||||
|
}
|
@ -1,12 +0,0 @@
|
|||||||
package tunnel
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func trimArr(arr []string) (r []string) {
|
|
||||||
for _, e := range arr {
|
|
||||||
r = append(r, strings.Trim(e, " "))
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
Reference in New Issue
Block a user