Compare commits
36 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6b83c00c46 | ||
|
|
0e6333194a | ||
|
|
bc77be08cc | ||
|
|
b887606007 | ||
|
|
ab4a5212d9 | ||
|
|
b592741872 | ||
|
|
b035e2b7dd | ||
|
|
b2546b3219 | ||
|
|
9c5b2622af | ||
|
|
2ac9fad93e | ||
|
|
0b72e1dded | ||
|
|
8f05071a81 | ||
|
|
e102c35f85 | ||
|
|
704fc1dbe5 | ||
|
|
02a56cad13 | ||
|
|
d238fef2e9 | ||
|
|
be8865eeb1 | ||
|
|
6e3c3a25f3 | ||
|
|
afcb393464 | ||
|
|
f637b0f916 | ||
|
|
06d425be3a | ||
|
|
17b31c5fc7 | ||
|
|
eee0bfc80a | ||
|
|
04dd90b25b | ||
|
|
2b116ffad6 | ||
|
|
6ab6551686 | ||
|
|
6bdeffe3f2 | ||
|
|
9224e79eea | ||
|
|
d1711b02b6 | ||
|
|
2fb865da94 | ||
|
|
00cb3dd99e | ||
|
|
0048d87cad | ||
|
|
564dfe2c79 | ||
|
|
f93e047f64 | ||
|
|
863440f936 | ||
|
|
e38b0155dd |
6
.dockerignore
Normal file
6
.dockerignore
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
.dockerignore
|
||||||
|
.github
|
||||||
|
.gitignore
|
||||||
|
Dockerfile
|
||||||
|
LICENSE
|
||||||
|
README.md
|
||||||
3
.github/FUNDING.yml
vendored
Normal file
3
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# These are supported funding model platforms
|
||||||
|
|
||||||
|
liberapay: octeep
|
||||||
14
.github/workflows/build.yml
vendored
14
.github/workflows/build.yml
vendored
@@ -16,7 +16,7 @@ jobs:
|
|||||||
- name: Setting up Go
|
- name: Setting up Go
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v2
|
||||||
with:
|
with:
|
||||||
go-version: 1.17
|
go-version: 1.19
|
||||||
- name: Building Windows amd64 Version
|
- name: Building Windows amd64 Version
|
||||||
run: |
|
run: |
|
||||||
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o WireProxy_amd64.exe -v ./cmd/wireproxy
|
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o WireProxy_amd64.exe -v ./cmd/wireproxy
|
||||||
@@ -36,7 +36,7 @@ jobs:
|
|||||||
- name: Setting up Go
|
- name: Setting up Go
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v2
|
||||||
with:
|
with:
|
||||||
go-version: 1.17
|
go-version: 1.19
|
||||||
- name: Building Windows arm64 Version
|
- name: Building Windows arm64 Version
|
||||||
run: |
|
run: |
|
||||||
CGO_ENABLED=0 GOOS=windows GOARCH=arm64 go build -o WireProxy_arm64.exe -v ./cmd/wireproxy
|
CGO_ENABLED=0 GOOS=windows GOARCH=arm64 go build -o WireProxy_arm64.exe -v ./cmd/wireproxy
|
||||||
@@ -56,7 +56,7 @@ jobs:
|
|||||||
- name: Setting up Go
|
- name: Setting up Go
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v2
|
||||||
with:
|
with:
|
||||||
go-version: 1.17
|
go-version: 1.19
|
||||||
- name: Building Linux amd64 Version
|
- name: Building Linux amd64 Version
|
||||||
run: |
|
run: |
|
||||||
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o WireProxy_amd64 -v ./cmd/wireproxy
|
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o WireProxy_amd64 -v ./cmd/wireproxy
|
||||||
@@ -76,7 +76,7 @@ jobs:
|
|||||||
- name: Setting up Go
|
- name: Setting up Go
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v2
|
||||||
with:
|
with:
|
||||||
go-version: 1.17
|
go-version: 1.19
|
||||||
- name: Building Linux arm64 Version
|
- name: Building Linux arm64 Version
|
||||||
run: |
|
run: |
|
||||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o WireProxy_arm64 -v ./cmd/wireproxy
|
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o WireProxy_arm64 -v ./cmd/wireproxy
|
||||||
@@ -96,7 +96,7 @@ jobs:
|
|||||||
- name: Setting up Go
|
- name: Setting up Go
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v2
|
||||||
with:
|
with:
|
||||||
go-version: 1.17
|
go-version: 1.19
|
||||||
- name: Building Linux s390x Version
|
- name: Building Linux s390x Version
|
||||||
run: |
|
run: |
|
||||||
CGO_ENABLED=0 GOOS=linux GOARCH=s390x go build -o WireProxy_s390x -v ./cmd/wireproxy
|
CGO_ENABLED=0 GOOS=linux GOARCH=s390x go build -o WireProxy_s390x -v ./cmd/wireproxy
|
||||||
@@ -116,7 +116,7 @@ jobs:
|
|||||||
- name: Setting up Go
|
- name: Setting up Go
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v2
|
||||||
with:
|
with:
|
||||||
go-version: 1.17
|
go-version: 1.19
|
||||||
- name: Building Darwin amd64 Version
|
- name: Building Darwin amd64 Version
|
||||||
run: |
|
run: |
|
||||||
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o WireProxy_amd64 -v ./cmd/wireproxy
|
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o WireProxy_amd64 -v ./cmd/wireproxy
|
||||||
@@ -136,7 +136,7 @@ jobs:
|
|||||||
- name: Setting up Go
|
- name: Setting up Go
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v2
|
||||||
with:
|
with:
|
||||||
go-version: 1.17
|
go-version: 1.19
|
||||||
- name: Building Darwin arm64 Version
|
- name: Building Darwin arm64 Version
|
||||||
run: |
|
run: |
|
||||||
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -o WireProxy_arm64 -v ./cmd/wireproxy
|
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -o WireProxy_arm64 -v ./cmd/wireproxy
|
||||||
|
|||||||
71
.github/workflows/container.yml
vendored
Normal file
71
.github/workflows/container.yml
vendored
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
name: Build container
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
# Allow for manually running
|
||||||
|
workflow_dispatch:
|
||||||
|
inputs:
|
||||||
|
container_tag:
|
||||||
|
description: Tag for container
|
||||||
|
default: "latest"
|
||||||
|
required: true
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
packages: write
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
container:
|
||||||
|
runs-on: ubuntu-20.04
|
||||||
|
|
||||||
|
env:
|
||||||
|
CONTAINER_NAME: ghcr.io/${{ github.repository }}
|
||||||
|
BUILD_PLATFORMS: linux/amd64,linux/arm,linux/arm64,linux/ppc64le,linux/s390x
|
||||||
|
RAW_CONTAINER_TAG: ${{ github.event.inputs.container_tag || github.event.pull_request.head.ref || 'latest' }}
|
||||||
|
RAW_REF_NAME: ${{ github.event.pull_request.head.ref || github.ref }}
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Set up QEMU
|
||||||
|
uses: docker/setup-qemu-action@v2
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
id: buildx
|
||||||
|
uses: docker/setup-buildx-action@v2.0.0
|
||||||
|
|
||||||
|
- name: Login to GitHub Container Registry
|
||||||
|
uses: docker/login-action@v2
|
||||||
|
with:
|
||||||
|
registry: ghcr.io
|
||||||
|
username: ${{ github.actor }}
|
||||||
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
with:
|
||||||
|
submodules: recursive
|
||||||
|
|
||||||
|
# Needed for buildx gha cache to work
|
||||||
|
- name: Expose GitHub Runtime
|
||||||
|
uses: crazy-max/ghaction-github-runtime@v2
|
||||||
|
|
||||||
|
- name: Build container
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
run: |
|
||||||
|
CONTAINER_TAG=$(echo "$RAW_CONTAINER_TAG" | sed 's/[^a-zA-Z0-9]\+/-/')
|
||||||
|
REF_NAME=$(echo "$RAW_REF_NAME" | sed -r 's#^refs/(heads|tags)/##')
|
||||||
|
|
||||||
|
docker buildx build \
|
||||||
|
--platform "$BUILD_PLATFORMS" \
|
||||||
|
--tag "$CONTAINER_NAME:$CONTAINER_TAG" \
|
||||||
|
--label "org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}" \
|
||||||
|
--label "org.opencontainers.image.documentation=${{ github.server_url }}/${{ github.repository }}" \
|
||||||
|
--label "org.opencontainers.image.url=${{ github.server_url }}/${{ github.repository }}/packages" \
|
||||||
|
--label "org.opencontainers.image.ref.name=$REF_NAME" \
|
||||||
|
--label "org.opencontainers.image.revision=${{ github.sha }}" \
|
||||||
|
--label "org.opencontainers.image.vendor=${{ github.repository_owner }}" \
|
||||||
|
--label "org.opencontainers.image.created=$(date -u --rfc-3339=seconds)" \
|
||||||
|
--cache-from type=gha \
|
||||||
|
--cache-to type=gha,mode=max \
|
||||||
|
--pull ${{ github.event_name == 'push' && '--push' || '' }} .
|
||||||
6
.github/workflows/golangci-lint.yml
vendored
6
.github/workflows/golangci-lint.yml
vendored
@@ -14,6 +14,8 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/setup-go@v2
|
- uses: actions/setup-go@v2
|
||||||
- uses: actions/checkout@v2
|
with:
|
||||||
|
go-version: '1.19'
|
||||||
|
- uses: actions/checkout@v3
|
||||||
- name: golangci-lint
|
- name: golangci-lint
|
||||||
uses: golangci/golangci-lint-action@v2
|
uses: golangci/golangci-lint-action@v3
|
||||||
|
|||||||
19
.github/workflows/wireproxy.yml
vendored
19
.github/workflows/wireproxy.yml
vendored
@@ -1,17 +1,20 @@
|
|||||||
name: Cross compile WirePorxy
|
name: Cross compile WireProxy
|
||||||
|
|
||||||
on:
|
on:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
|
create:
|
||||||
|
tags:
|
||||||
|
- v*
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
WirePorxy:
|
WireProxy:
|
||||||
|
|
||||||
name: Cross compile WirePorxy
|
name: Cross compile WireProxy
|
||||||
|
|
||||||
runs-on: ubuntu-20.04
|
runs-on: ubuntu-20.04
|
||||||
|
|
||||||
env:
|
env:
|
||||||
workdir: ./WirePorxy
|
workdir: ./WireProxy
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
@@ -19,7 +22,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
- name: Git clone WirePorxy
|
- name: Git clone WireProxy
|
||||||
run: |
|
run: |
|
||||||
git clone https://github.com/octeep/wireproxy.git ${{ env.workdir }}
|
git clone https://github.com/octeep/wireproxy.git ${{ env.workdir }}
|
||||||
cp ./.github/wireproxy-releaser.yml ${{ env.workdir }}/.goreleaser.yml
|
cp ./.github/wireproxy-releaser.yml ${{ env.workdir }}/.goreleaser.yml
|
||||||
@@ -27,7 +30,7 @@ jobs:
|
|||||||
- name: Set up GoReleaser
|
- name: Set up GoReleaser
|
||||||
uses: actions/setup-go@v2
|
uses: actions/setup-go@v2
|
||||||
with:
|
with:
|
||||||
go-version: "1.17"
|
go-version: "1.19"
|
||||||
|
|
||||||
- name: Run GoReleaser
|
- name: Run GoReleaser
|
||||||
uses: goreleaser/goreleaser-action@v2
|
uses: goreleaser/goreleaser-action@v2
|
||||||
@@ -36,6 +39,8 @@ jobs:
|
|||||||
workdir: ${{ env.workdir }}
|
workdir: ${{ env.workdir }}
|
||||||
version: latest
|
version: latest
|
||||||
args: release --rm-dist
|
args: release --rm-dist
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
- name: Release binaries
|
- name: Release binaries
|
||||||
uses: softprops/action-gh-release@v1
|
uses: softprops/action-gh-release@v1
|
||||||
@@ -43,4 +48,4 @@ jobs:
|
|||||||
tag_name: wireproxy
|
tag_name: wireproxy
|
||||||
files: ${{ env.workdir }}/dist/*.tar.gz
|
files: ${{ env.workdir }}/dist/*.tar.gz
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|||||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,5 @@
|
|||||||
/main
|
/main
|
||||||
/wireproxy
|
/wireproxy
|
||||||
*.sw?
|
*.sw?
|
||||||
|
/.idea
|
||||||
|
.goreleaser.yml
|
||||||
|
|||||||
19
Dockerfile
Normal file
19
Dockerfile
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
# Start by building the application.
|
||||||
|
FROM golang:1.18 as build
|
||||||
|
|
||||||
|
WORKDIR /usr/src/wireproxy
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
RUN CGO_ENABLED=0 go build ./cmd/wireproxy
|
||||||
|
|
||||||
|
# Now copy it into our base image.
|
||||||
|
FROM gcr.io/distroless/static-debian11:nonroot
|
||||||
|
COPY --from=build /usr/src/wireproxy/wireproxy /usr/bin/wireproxy
|
||||||
|
|
||||||
|
VOLUME [ "/etc/wireproxy"]
|
||||||
|
ENTRYPOINT [ "/usr/bin/wireproxy" ]
|
||||||
|
CMD [ "--config", "/etc/wireproxy/config" ]
|
||||||
|
|
||||||
|
LABEL org.opencontainers.image.title wireproxy
|
||||||
|
LABEL org.opencontainers.image.description "Wireguard client that exposes itself as a socks5 proxy"
|
||||||
|
LABEL org.opencontainers.image.licenses ISC
|
||||||
13
Makefile
Normal file
13
Makefile
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
export GO ?= go
|
||||||
|
|
||||||
|
.PHONY: all
|
||||||
|
all: wireproxy
|
||||||
|
|
||||||
|
.PHONY: wireproxy
|
||||||
|
wireproxy:
|
||||||
|
tag="$$(git describe --tag 2>/dev/null)" && \
|
||||||
|
${GO} build -ldflags "-X 'main.version=$$tag'" ./cmd/wireproxy
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
${RM} wireproxy
|
||||||
46
README.md
46
README.md
@@ -1,4 +1,8 @@
|
|||||||
# wireproxy
|
# wireproxy
|
||||||
|
[](./LICENSE)
|
||||||
|
[](https://github.com/octeep/wireproxy/actions)
|
||||||
|
[](https://pkg.go.dev/github.com/octeep/wireproxy)
|
||||||
|
|
||||||
A wireguard client that exposes itself as a socks5 proxy or tunnels.
|
A wireguard client that exposes itself as a socks5 proxy or tunnels.
|
||||||
|
|
||||||
# What is this
|
# What is this
|
||||||
@@ -20,6 +24,10 @@ anything.
|
|||||||
- TCP static routing for client and server
|
- TCP static routing for client and server
|
||||||
- SOCKS5 proxy (currently only CONNECT is supported)
|
- SOCKS5 proxy (currently only CONNECT is supported)
|
||||||
|
|
||||||
|
# TODO
|
||||||
|
- UDP Support in SOCKS5
|
||||||
|
- UDP static routing
|
||||||
|
|
||||||
# Usage
|
# Usage
|
||||||
```
|
```
|
||||||
./wireproxy -c [path to config]
|
./wireproxy -c [path to config]
|
||||||
@@ -44,7 +52,7 @@ Arguments:
|
|||||||
```
|
```
|
||||||
git clone https://github.com/octeep/wireproxy
|
git clone https://github.com/octeep/wireproxy
|
||||||
cd wireproxy
|
cd wireproxy
|
||||||
go build ./cmd/wireproxy
|
make
|
||||||
```
|
```
|
||||||
|
|
||||||
# Sample config file
|
# Sample config file
|
||||||
@@ -108,6 +116,42 @@ WGConfig = <path to the wireguard config>
|
|||||||
...
|
...
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Having multiple peers is also supported. `AllowedIPs` would need to be specified
|
||||||
|
such that wireproxy would know which peer to forward to.
|
||||||
|
```
|
||||||
|
[Interface]
|
||||||
|
Address = 10.254.254.40/32
|
||||||
|
PrivateKey = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX=
|
||||||
|
|
||||||
|
[Peer]
|
||||||
|
Endpoint = 192.168.0.204:51820
|
||||||
|
PublicKey = YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY=
|
||||||
|
AllowedIPs = 10.254.254.100/32
|
||||||
|
PersistentKeepalive = 25
|
||||||
|
|
||||||
|
[Peer]
|
||||||
|
PublicKey = ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ=
|
||||||
|
AllowedIPs = 10.254.254.1/32, fdee:1337:c000:d00d::1/128
|
||||||
|
Endpoint = 172.16.0.185:44044
|
||||||
|
PersistentKeepalive = 25
|
||||||
|
|
||||||
|
|
||||||
|
[TCPServerTunnel]
|
||||||
|
ListenPort = 5000
|
||||||
|
Target = service-one.servicenet:5000
|
||||||
|
|
||||||
|
[TCPServerTunnel]
|
||||||
|
ListenPort = 5001
|
||||||
|
Target = service-two.servicenet:5001
|
||||||
|
|
||||||
|
[TCPServerTunnel]
|
||||||
|
ListenPort = 5080
|
||||||
|
Target = service-three.servicenet:80
|
||||||
|
```
|
||||||
|
|
||||||
|
## Donation
|
||||||
|
<noscript><a href="https://liberapay.com/octeep/donate"><img alt="Donate using Liberapay" src="https://liberapay.com/assets/widgets/donate.svg"></a></noscript>
|
||||||
|
|
||||||
|
|
||||||
## Stargazers over time
|
## Stargazers over time
|
||||||
|
|
||||||
|
|||||||
@@ -8,22 +8,63 @@ import (
|
|||||||
|
|
||||||
"github.com/akamensky/argparse"
|
"github.com/akamensky/argparse"
|
||||||
"github.com/octeep/wireproxy"
|
"github.com/octeep/wireproxy"
|
||||||
|
"suah.dev/protect"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// an argument to denote that this process was spawned by -d
|
||||||
const daemonProcess = "daemon-process"
|
const daemonProcess = "daemon-process"
|
||||||
|
|
||||||
|
var version = "1.0.5-dev"
|
||||||
|
|
||||||
|
// attempts to pledge and panic if it fails
|
||||||
|
// this does nothing on non-OpenBSD systems
|
||||||
|
func pledgeOrPanic(promises string) {
|
||||||
|
err := protect.Pledge(promises)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// attempts to unveil and panic if it fails
|
||||||
|
// this does nothing on non-OpenBSD systems
|
||||||
|
func unveilOrPanic(path string, flags string) {
|
||||||
|
err := protect.Unveil(path, flags)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the executable path via syscalls or infer it from argv
|
||||||
|
func executablePath() string {
|
||||||
|
programPath, err := os.Executable()
|
||||||
|
if err != nil {
|
||||||
|
return os.Args[0]
|
||||||
|
}
|
||||||
|
return programPath
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
exePath := executablePath()
|
||||||
|
unveilOrPanic("/", "r")
|
||||||
|
unveilOrPanic(exePath, "x")
|
||||||
|
|
||||||
|
// only allow standard stdio operation, file reading, networking, and exec
|
||||||
|
// also remove unveil permission to lock unveil
|
||||||
|
pledgeOrPanic("stdio rpath inet dns proc exec")
|
||||||
|
|
||||||
isDaemonProcess := len(os.Args) > 1 && os.Args[1] == daemonProcess
|
isDaemonProcess := len(os.Args) > 1 && os.Args[1] == daemonProcess
|
||||||
args := os.Args
|
args := os.Args
|
||||||
if isDaemonProcess {
|
if isDaemonProcess {
|
||||||
|
// remove proc and exec if they are not needed
|
||||||
|
pledgeOrPanic("stdio rpath inet dns")
|
||||||
args = []string{args[0]}
|
args = []string{args[0]}
|
||||||
args = append(args, os.Args[2:]...)
|
args = append(args, os.Args[2:]...)
|
||||||
}
|
}
|
||||||
|
|
||||||
parser := argparse.NewParser("wireproxy", "Userspace wireguard client for proxying")
|
parser := argparse.NewParser("wireproxy", "Userspace wireguard client for proxying")
|
||||||
|
|
||||||
config := parser.String("c", "config", &argparse.Options{Required: true, Help: "Path of configuration file"})
|
config := parser.String("c", "config", &argparse.Options{Help: "Path of configuration file"})
|
||||||
daemon := parser.Flag("d", "daemon", &argparse.Options{Help: "Make wireproxy run in background"})
|
daemon := parser.Flag("d", "daemon", &argparse.Options{Help: "Make wireproxy run in background"})
|
||||||
|
printVerison := parser.Flag("v", "version", &argparse.Options{Help: "Print version"})
|
||||||
configTest := parser.Flag("n", "configtest", &argparse.Options{Help: "Configtest mode. Only check the configuration file for validity."})
|
configTest := parser.Flag("n", "configtest", &argparse.Options{Help: "Configtest mode. Only check the configuration file for validity."})
|
||||||
|
|
||||||
err := parser.Parse(args)
|
err := parser.Parse(args)
|
||||||
@@ -32,9 +73,24 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if *printVerison {
|
||||||
|
fmt.Printf("wireproxy, version %s\n", version)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if *config == "" {
|
||||||
|
fmt.Println("configuration path is required")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !*daemon {
|
||||||
|
// remove proc and exec if they are not needed
|
||||||
|
pledgeOrPanic("stdio rpath inet dns")
|
||||||
|
}
|
||||||
|
|
||||||
conf, err := wireproxy.ParseConfig(*config)
|
conf, err := wireproxy.ParseConfig(*config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if *configTest {
|
if *configTest {
|
||||||
@@ -49,14 +105,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if *daemon {
|
if *daemon {
|
||||||
programPath, err := os.Executable()
|
args[0] = daemonProcess
|
||||||
if err != nil {
|
cmd := exec.Command(exePath, args...)
|
||||||
programPath = args[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
newArgs := []string{daemonProcess}
|
|
||||||
newArgs = append(newArgs, args[1:]...)
|
|
||||||
cmd := exec.Command(programPath, newArgs...)
|
|
||||||
err = cmd.Start()
|
err = cmd.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
@@ -64,9 +114,12 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// no file access is allowed from now on, only networking
|
||||||
|
pledgeOrPanic("stdio inet dns")
|
||||||
|
|
||||||
tnet, err := wireproxy.StartWireguard(conf.Device)
|
tnet, err := wireproxy.StartWireguard(conf.Device)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, spawner := range conf.Routines {
|
for _, spawner := range conf.Routines {
|
||||||
|
|||||||
132
config.go
132
config.go
@@ -9,18 +9,24 @@ import (
|
|||||||
|
|
||||||
"github.com/go-ini/ini"
|
"github.com/go-ini/ini"
|
||||||
|
|
||||||
"golang.zx2c4.com/go118/netip"
|
"net/netip"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type PeerConfig struct {
|
||||||
|
PublicKey string
|
||||||
|
PreSharedKey string
|
||||||
|
Endpoint string
|
||||||
|
KeepAlive int
|
||||||
|
AllowedIPs []netip.Prefix
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeviceConfig contains the information to initiate a wireguard connection
|
||||||
type DeviceConfig struct {
|
type DeviceConfig struct {
|
||||||
SelfSecretKey string
|
SecretKey string
|
||||||
SelfEndpoint []netip.Addr
|
Endpoint []netip.Addr
|
||||||
PeerPublicKey string
|
Peers []PeerConfig
|
||||||
PeerEndpoint string
|
DNS []netip.Addr
|
||||||
DNS []netip.Addr
|
MTU int
|
||||||
KeepAlive int
|
|
||||||
PreSharedKey string
|
|
||||||
MTU int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type TCPClientTunnelConfig struct {
|
type TCPClientTunnelConfig struct {
|
||||||
@@ -108,7 +114,7 @@ func parseNetIP(section *ini.Section, keyName string) ([]netip.Addr, error) {
|
|||||||
return []netip.Addr{}, nil
|
return []netip.Addr{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ips := []netip.Addr{}
|
var ips []netip.Addr
|
||||||
for _, str := range key.StringsWithShadows(",") {
|
for _, str := range key.StringsWithShadows(",") {
|
||||||
str = strings.TrimSpace(str)
|
str = strings.TrimSpace(str)
|
||||||
ip, err := netip.ParseAddr(str)
|
ip, err := netip.ParseAddr(str)
|
||||||
@@ -126,7 +132,7 @@ func parseCIDRNetIP(section *ini.Section, keyName string) ([]netip.Addr, error)
|
|||||||
return []netip.Addr{}, nil
|
return []netip.Addr{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ips := []netip.Addr{}
|
var ips []netip.Addr
|
||||||
for _, str := range key.StringsWithShadows(",") {
|
for _, str := range key.StringsWithShadows(",") {
|
||||||
prefix, err := netip.ParsePrefix(str)
|
prefix, err := netip.ParsePrefix(str)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -143,6 +149,24 @@ func parseCIDRNetIP(section *ini.Section, keyName string) ([]netip.Addr, error)
|
|||||||
return ips, nil
|
return ips, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseAllowedIPs(section *ini.Section) ([]netip.Prefix, error) {
|
||||||
|
key := section.Key("AllowedIPs")
|
||||||
|
if key == nil {
|
||||||
|
return []netip.Prefix{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var ips []netip.Prefix
|
||||||
|
for _, str := range key.StringsWithShadows(",") {
|
||||||
|
prefix, err := netip.ParsePrefix(str)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ips = append(ips, prefix)
|
||||||
|
}
|
||||||
|
return ips, nil
|
||||||
|
}
|
||||||
|
|
||||||
func resolveIP(ip string) (*net.IPAddr, error) {
|
func resolveIP(ip string) (*net.IPAddr, error) {
|
||||||
return net.ResolveIPAddr("ip", ip)
|
return net.ResolveIPAddr("ip", ip)
|
||||||
}
|
}
|
||||||
@@ -160,6 +184,7 @@ func resolveIPPAndPort(addr string) (string, error) {
|
|||||||
return net.JoinHostPort(ip.String(), port), nil
|
return net.JoinHostPort(ip.String(), port), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ParseInterface parses the [Interface] section and extract the information into `device`
|
||||||
func ParseInterface(cfg *ini.File, device *DeviceConfig) error {
|
func ParseInterface(cfg *ini.File, device *DeviceConfig) error {
|
||||||
sections, err := cfg.SectionsByName("Interface")
|
sections, err := cfg.SectionsByName("Interface")
|
||||||
if len(sections) != 1 || err != nil {
|
if len(sections) != 1 || err != nil {
|
||||||
@@ -172,13 +197,13 @@ func ParseInterface(cfg *ini.File, device *DeviceConfig) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
device.SelfEndpoint = address
|
device.Endpoint = address
|
||||||
|
|
||||||
privKey, err := parseBase64KeyToHex(section, "PrivateKey")
|
privKey, err := parseBase64KeyToHex(section, "PrivateKey")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
device.SelfSecretKey = privKey
|
device.SecretKey = privKey
|
||||||
|
|
||||||
dns, err := parseNetIP(section, "DNS")
|
dns, err := parseNetIP(section, "DNS")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -197,45 +222,58 @@ func ParseInterface(cfg *ini.File, device *DeviceConfig) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParsePeer(cfg *ini.File, device *DeviceConfig) error {
|
// ParsePeer parses the [Peer] section and extract the information into `peers`
|
||||||
|
func ParsePeers(cfg *ini.File, peers *[]PeerConfig) error {
|
||||||
sections, err := cfg.SectionsByName("Peer")
|
sections, err := cfg.SectionsByName("Peer")
|
||||||
if len(sections) != 1 || err != nil {
|
if len(sections) < 1 || err != nil {
|
||||||
return errors.New("one and only one [Peer] is expected")
|
return errors.New("at least one [Peer] is expected")
|
||||||
}
|
}
|
||||||
section := sections[0]
|
|
||||||
|
|
||||||
decoded, err := parseBase64KeyToHex(section, "PublicKey")
|
for _, section := range sections {
|
||||||
if err != nil {
|
peer := PeerConfig{
|
||||||
return err
|
PreSharedKey: "0000000000000000000000000000000000000000000000000000000000000000",
|
||||||
}
|
KeepAlive: 0,
|
||||||
device.PeerPublicKey = decoded
|
}
|
||||||
|
|
||||||
if sectionKey, err := section.GetKey("PreSharedKey"); err == nil {
|
decoded, err := parseBase64KeyToHex(section, "PublicKey")
|
||||||
value, err := encodeBase64ToHex(sectionKey.String())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
device.PreSharedKey = value
|
peer.PublicKey = decoded
|
||||||
}
|
|
||||||
|
|
||||||
decoded, err = parseString(section, "Endpoint")
|
if sectionKey, err := section.GetKey("PreSharedKey"); err == nil {
|
||||||
if err != nil {
|
value, err := encodeBase64ToHex(sectionKey.String())
|
||||||
return err
|
if err != nil {
|
||||||
}
|
return err
|
||||||
decoded, err = resolveIPPAndPort(decoded)
|
}
|
||||||
if err != nil {
|
peer.PreSharedKey = value
|
||||||
return err
|
}
|
||||||
}
|
|
||||||
device.PeerEndpoint = decoded
|
|
||||||
|
|
||||||
if sectionKey, err := section.GetKey("PersistentKeepalive"); err == nil {
|
decoded, err = parseString(section, "Endpoint")
|
||||||
value, err := sectionKey.Int()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
device.KeepAlive = value
|
decoded, err = resolveIPPAndPort(decoded)
|
||||||
}
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
peer.Endpoint = decoded
|
||||||
|
|
||||||
|
if sectionKey, err := section.GetKey("PersistentKeepalive"); err == nil {
|
||||||
|
value, err := sectionKey.Int()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
peer.KeepAlive = value
|
||||||
|
}
|
||||||
|
|
||||||
|
peer.AllowedIPs, err = parseAllowedIPs(section)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
*peers = append(*peers, peer)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -292,6 +330,8 @@ func parseSocks5Config(section *ini.Section) (RoutineSpawner, error) {
|
|||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Takes a function that parses an individual section into a config, and apply it on all
|
||||||
|
// specified sections
|
||||||
func parseRoutinesConfig(routines *[]RoutineSpawner, cfg *ini.File, sectionName string, f func(*ini.Section) (RoutineSpawner, error)) error {
|
func parseRoutinesConfig(routines *[]RoutineSpawner, cfg *ini.File, sectionName string, f func(*ini.Section) (RoutineSpawner, error)) error {
|
||||||
sections, err := cfg.SectionsByName(sectionName)
|
sections, err := cfg.SectionsByName(sectionName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -310,10 +350,12 @@ func parseRoutinesConfig(routines *[]RoutineSpawner, cfg *ini.File, sectionName
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ParseConfig takes the path of a configuration file and parses it into Configuration
|
||||||
func ParseConfig(path string) (*Configuration, error) {
|
func ParseConfig(path string) (*Configuration, error) {
|
||||||
iniOpt := ini.LoadOptions{
|
iniOpt := ini.LoadOptions{
|
||||||
Insensitive: true,
|
Insensitive: true,
|
||||||
AllowShadows: true,
|
AllowShadows: true,
|
||||||
|
AllowNonUniqueSections: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg, err := ini.LoadSources(iniOpt, path)
|
cfg, err := ini.LoadSources(iniOpt, path)
|
||||||
@@ -322,9 +364,7 @@ func ParseConfig(path string) (*Configuration, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
device := &DeviceConfig{
|
device := &DeviceConfig{
|
||||||
PreSharedKey: "0000000000000000000000000000000000000000000000000000000000000000",
|
MTU: 1420,
|
||||||
KeepAlive: 0,
|
|
||||||
MTU: 1420,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
root := cfg.Section("")
|
root := cfg.Section("")
|
||||||
@@ -342,12 +382,12 @@ func ParseConfig(path string) (*Configuration, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ParsePeer(wgCfg, device)
|
err = ParsePeers(wgCfg, &device.Peers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
routinesSpawners := []RoutineSpawner{}
|
var routinesSpawners []RoutineSpawner
|
||||||
|
|
||||||
err = parseRoutinesConfig(&routinesSpawners, cfg, "TCPClientTunnel", parseTCPClientTunnelConfig)
|
err = parseRoutinesConfig(&routinesSpawners, cfg, "TCPClientTunnel", parseTCPClientTunnelConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
FROM golang:alpine AS go-build
|
|
||||||
|
|
||||||
RUN apk --no-cache add --update git
|
|
||||||
RUN git clone https://github.com/octeep/wireproxy.git
|
|
||||||
RUN cd ./wireproxy && go build ./cmd/wireproxy
|
|
||||||
|
|
||||||
|
|
||||||
FROM alpine:latest
|
|
||||||
|
|
||||||
RUN apk upgrade
|
|
||||||
COPY --from=go-build /go/wireproxy/wireproxy /usr/bin/
|
|
||||||
|
|
||||||
VOLUME [ "/etc/wireproxy"]
|
|
||||||
ENTRYPOINT [ "/usr/bin/wireproxy", "/etc/wireproxy/config" ]
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
build:
|
|
||||||
docker build -t wireproxy .
|
|
||||||
|
|
||||||
run:
|
|
||||||
docker run \
|
|
||||||
--rm --tty --interactive \
|
|
||||||
--name=wireproxy \
|
|
||||||
--publish 2534:2534 \
|
|
||||||
--volume "${PWD}/config:/etc/wireproxy/config:ro" \
|
|
||||||
wireproxy
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
[Interface]
|
|
||||||
Address = ###Interface - Address###
|
|
||||||
PrivateKey = ###Interface - PrivateKey###
|
|
||||||
DNS = ###Interface - DNS###
|
|
||||||
|
|
||||||
[Peer]
|
|
||||||
PublicKey = ###Peer - PublicKey###
|
|
||||||
Endpoint = ###Peer - Endpoint###
|
|
||||||
|
|
||||||
# Socks5 create a socks5 proxy on your LAN, and any traffic would be routed via wireguard
|
|
||||||
[Socks5]
|
|
||||||
BindAddress = 0.0.0.0:2534
|
|
||||||
21
go.mod
21
go.mod
@@ -1,24 +1,23 @@
|
|||||||
module github.com/octeep/wireproxy
|
module github.com/octeep/wireproxy
|
||||||
|
|
||||||
go 1.17
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/MakeNowJust/heredoc/v2 v2.0.1
|
||||||
|
github.com/akamensky/argparse v1.3.1
|
||||||
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5
|
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5
|
||||||
github.com/go-ini/ini v1.66.4
|
github.com/go-ini/ini v1.66.4
|
||||||
golang.org/x/net v0.0.0-20220225172249-27dd8689420f
|
golang.zx2c4.com/wireguard v0.0.0-20220829161405-d1d08426b27b
|
||||||
golang.zx2c4.com/go118/netip v0.0.0-20211111135330-a4a02eeacf9d
|
suah.dev/protect v1.2.0
|
||||||
golang.zx2c4.com/wireguard v0.0.0-20220202223031-3b95c81cc178
|
|
||||||
golang.zx2c4.com/wireguard/tun/netstack v0.0.0-20220310012736-ae6bc4dd64e1
|
|
||||||
gvisor.dev/gvisor v0.0.0-20211020211948-f76a604701b6
|
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/akamensky/argparse v1.3.1 // indirect
|
|
||||||
github.com/google/btree v1.0.1 // indirect
|
github.com/google/btree v1.0.1 // indirect
|
||||||
github.com/inconshreveable/mousetrap v1.0.0 // indirect
|
github.com/stretchr/testify v1.8.0 // indirect
|
||||||
github.com/spf13/pflag v1.0.5 // indirect
|
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect
|
||||||
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa // indirect
|
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
|
||||||
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect
|
golang.org/x/sys v0.0.0-20220315194320-039c03cc5b86 // indirect
|
||||||
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
|
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
|
||||||
golang.zx2c4.com/wintun v0.0.0-20211104114900-415007cec224 // indirect
|
golang.zx2c4.com/wintun v0.0.0-20211104114900-415007cec224 // indirect
|
||||||
|
gvisor.dev/gvisor v0.0.0-20220817001344-846276b3dbc5 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
10
net.go
10
net.go
@@ -3,8 +3,8 @@
|
|||||||
package wireproxy
|
package wireproxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"golang.zx2c4.com/go118/netip"
|
|
||||||
"net"
|
"net"
|
||||||
|
"net/netip"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TCPAddrFromAddrPort(addr netip.AddrPort) *net.TCPAddr {
|
func TCPAddrFromAddrPort(addr netip.AddrPort) *net.TCPAddr {
|
||||||
@@ -14,11 +14,3 @@ func TCPAddrFromAddrPort(addr netip.AddrPort) *net.TCPAddr {
|
|||||||
Port: int(addr.Port()),
|
Port: int(addr.Port()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func UDPAddrFromAddrPort(addr netip.AddrPort) *net.UDPAddr {
|
|
||||||
return &net.UDPAddr{
|
|
||||||
IP: addr.Addr().AsSlice(),
|
|
||||||
Zone: addr.Addr().Zone(),
|
|
||||||
Port: int(addr.Port()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
163
routine.go
163
routine.go
@@ -4,33 +4,46 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"crypto/subtle"
|
"crypto/subtle"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/armon/go-socks5"
|
"github.com/armon/go-socks5"
|
||||||
|
|
||||||
"golang.zx2c4.com/go118/netip"
|
|
||||||
"golang.zx2c4.com/wireguard/tun/netstack"
|
"golang.zx2c4.com/wireguard/tun/netstack"
|
||||||
|
"net/netip"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// errorLogger is the logger to print error message
|
||||||
|
var errorLogger = log.New(os.Stderr, "ERROR: ", log.LstdFlags)
|
||||||
|
|
||||||
|
// CredentialValidator stores the authentication data of a socks5 proxy
|
||||||
type CredentialValidator struct {
|
type CredentialValidator struct {
|
||||||
username string
|
username string
|
||||||
password string
|
password string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// VirtualTun stores a reference to netstack network and DNS configuration
|
||||||
type VirtualTun struct {
|
type VirtualTun struct {
|
||||||
tnet *netstack.Net
|
tnet *netstack.Net
|
||||||
systemDNS bool
|
systemDNS bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RoutineSpawner spawns a routine (e.g. socks5, tcp static routes) after the configuration is parsed
|
||||||
type RoutineSpawner interface {
|
type RoutineSpawner interface {
|
||||||
SpawnRoutine(vt *VirtualTun)
|
SpawnRoutine(vt *VirtualTun)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type addressPort struct {
|
||||||
|
address string
|
||||||
|
port uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
// LookupAddr lookups a hostname.
|
||||||
|
// DNS traffic may or may not be routed depending on VirtualTun's setting
|
||||||
func (d VirtualTun) LookupAddr(ctx context.Context, name string) ([]string, error) {
|
func (d VirtualTun) LookupAddr(ctx context.Context, name string) ([]string, error) {
|
||||||
if d.systemDNS {
|
if d.systemDNS {
|
||||||
return net.DefaultResolver.LookupHost(ctx, name)
|
return net.DefaultResolver.LookupHost(ctx, name)
|
||||||
@@ -39,13 +52,10 @@ func (d VirtualTun) LookupAddr(ctx context.Context, name string) ([]string, erro
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d VirtualTun) ResolveAddrPort(saddr string) (*netip.AddrPort, error) {
|
// ResolveAddrWithContext resolves a hostname and returns an AddrPort.
|
||||||
name, sport, err := net.SplitHostPort(saddr)
|
// DNS traffic may or may not be routed depending on VirtualTun's setting
|
||||||
if err != nil {
|
func (d VirtualTun) ResolveAddrWithContext(ctx context.Context, name string) (*netip.Addr, error) {
|
||||||
return nil, err
|
addrs, err := d.LookupAddr(ctx, name)
|
||||||
}
|
|
||||||
|
|
||||||
addrs, err := d.LookupAddr(context.Background(), name)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -55,7 +65,38 @@ func (d VirtualTun) ResolveAddrPort(saddr string) (*netip.AddrPort, error) {
|
|||||||
return nil, errors.New("no address found for: " + name)
|
return nil, errors.New("no address found for: " + name)
|
||||||
}
|
}
|
||||||
|
|
||||||
addr, err := netip.ParseAddr(addrs[rand.Intn(size)])
|
rand.Shuffle(size, func(i, j int) {
|
||||||
|
addrs[i], addrs[j] = addrs[j], addrs[i]
|
||||||
|
})
|
||||||
|
|
||||||
|
var addr netip.Addr
|
||||||
|
for _, saddr := range addrs {
|
||||||
|
addr, err = netip.ParseAddr(saddr)
|
||||||
|
if err == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &addr, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resolve resolves a hostname and returns an IP.
|
||||||
|
// DNS traffic may or may not be routed depending on VirtualTun's setting
|
||||||
|
func (d VirtualTun) Resolve(ctx context.Context, name string) (context.Context, net.IP, error) {
|
||||||
|
addr, err := d.ResolveAddrWithContext(ctx, name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctx, addr.AsSlice(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseAddressPort(endpoint string) (*addressPort, error) {
|
||||||
|
name, sport, err := net.SplitHostPort(endpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -65,34 +106,20 @@ func (d VirtualTun) ResolveAddrPort(saddr string) (*netip.AddrPort, error) {
|
|||||||
return nil, &net.OpError{Op: "dial", Err: errors.New("port must be numeric")}
|
return nil, &net.OpError{Op: "dial", Err: errors.New("port must be numeric")}
|
||||||
}
|
}
|
||||||
|
|
||||||
addrPort := netip.AddrPortFrom(addr, uint16(port))
|
return &addressPort{address: name, port: uint16(port)}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d VirtualTun) resolveToAddrPort(endpoint *addressPort) (*netip.AddrPort, error) {
|
||||||
|
addr, err := d.ResolveAddrWithContext(context.Background(), endpoint.address)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
addrPort := netip.AddrPortFrom(*addr, endpoint.port)
|
||||||
return &addrPort, nil
|
return &addrPort, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d VirtualTun) Resolve(ctx context.Context, name string) (context.Context, net.IP, error) {
|
// SpawnRoutine spawns a socks5 server.
|
||||||
var addrs []string
|
|
||||||
var err error
|
|
||||||
|
|
||||||
addrs, err = d.LookupAddr(ctx, name)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return ctx, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
size := len(addrs)
|
|
||||||
if size == 0 {
|
|
||||||
return ctx, nil, errors.New("no address found for: " + name)
|
|
||||||
}
|
|
||||||
|
|
||||||
addr := addrs[rand.Intn(size)]
|
|
||||||
ip := net.ParseIP(addr)
|
|
||||||
if ip == nil {
|
|
||||||
return ctx, nil, errors.New("invalid address: " + addr)
|
|
||||||
}
|
|
||||||
|
|
||||||
return ctx, ip, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (config *Socks5Config) SpawnRoutine(vt *VirtualTun) {
|
func (config *Socks5Config) SpawnRoutine(vt *VirtualTun) {
|
||||||
conf := &socks5.Config{Dial: vt.tnet.DialContext, Resolver: vt}
|
conf := &socks5.Config{Dial: vt.tnet.DialContext, Resolver: vt}
|
||||||
if username := config.Username; username != "" {
|
if username := config.Username; username != "" {
|
||||||
@@ -102,33 +129,46 @@ func (config *Socks5Config) SpawnRoutine(vt *VirtualTun) {
|
|||||||
}
|
}
|
||||||
server, err := socks5.New(conf)
|
server, err := socks5.New(conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := server.ListenAndServe("tcp", config.BindAddress); err != nil {
|
if err := server.ListenAndServe("tcp", config.BindAddress); err != nil {
|
||||||
log.Panic(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Valid checks the authentication data in CredentialValidator and compare them
|
||||||
|
// to username and password in constant time.
|
||||||
func (c CredentialValidator) Valid(username, password string) bool {
|
func (c CredentialValidator) Valid(username, password string) bool {
|
||||||
u := subtle.ConstantTimeCompare([]byte(c.username), []byte(username))
|
u := subtle.ConstantTimeCompare([]byte(c.username), []byte(username))
|
||||||
p := subtle.ConstantTimeCompare([]byte(c.password), []byte(password))
|
p := subtle.ConstantTimeCompare([]byte(c.password), []byte(password))
|
||||||
return u&p == 1
|
return u&p == 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// connForward copy data from `from` to `to`, then close both stream.
|
||||||
func connForward(bufSize int, from io.ReadWriteCloser, to io.ReadWriteCloser) {
|
func connForward(bufSize int, from io.ReadWriteCloser, to io.ReadWriteCloser) {
|
||||||
buf := make([]byte, bufSize)
|
buf := make([]byte, bufSize)
|
||||||
_, err := io.CopyBuffer(to, from, buf)
|
_, err := io.CopyBuffer(to, from, buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
to.Close()
|
errorLogger.Printf("Cannot forward traffic: %s\n", err.Error())
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
_ = from.Close()
|
||||||
|
_ = to.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func tcpClientForward(tnet *netstack.Net, target *net.TCPAddr, conn net.Conn) {
|
// tcpClientForward starts a new connection via wireguard and forward traffic from `conn`
|
||||||
sconn, err := tnet.DialTCP(target)
|
func tcpClientForward(vt *VirtualTun, raddr *addressPort, conn net.Conn) {
|
||||||
|
target, err := vt.resolveToAddrPort(raddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("[ERROR] TCP Client Tunnel to %s: %s\n", target, err.Error())
|
errorLogger.Printf("TCP Server Tunnel to %s: %s\n", target, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
tcpAddr := TCPAddrFromAddrPort(*target)
|
||||||
|
|
||||||
|
sconn, err := vt.tnet.DialTCP(tcpAddr)
|
||||||
|
if err != nil {
|
||||||
|
errorLogger.Printf("TCP Client Tunnel to %s: %s\n", target, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,31 +176,40 @@ func tcpClientForward(tnet *netstack.Net, target *net.TCPAddr, conn net.Conn) {
|
|||||||
go connForward(1024, conn, sconn)
|
go connForward(1024, conn, sconn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SpawnRoutine spawns a local TCP server which acts as a proxy to the specified target
|
||||||
func (conf *TCPClientTunnelConfig) SpawnRoutine(vt *VirtualTun) {
|
func (conf *TCPClientTunnelConfig) SpawnRoutine(vt *VirtualTun) {
|
||||||
raddr, err := vt.ResolveAddrPort(conf.Target)
|
raddr, err := parseAddressPort(conf.Target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
tcpAddr := TCPAddrFromAddrPort(*raddr)
|
|
||||||
|
|
||||||
server, err := net.ListenTCP("tcp", conf.BindAddress)
|
server, err := net.ListenTCP("tcp", conf.BindAddress)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
conn, err := server.Accept()
|
conn, err := server.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
go tcpClientForward(vt.tnet, tcpAddr, conn)
|
go tcpClientForward(vt, raddr, conn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func tcpServerForward(target *net.TCPAddr, conn net.Conn) {
|
// tcpServerForward starts a new connection locally and forward traffic from `conn`
|
||||||
sconn, err := net.DialTCP("tcp", nil, target)
|
func tcpServerForward(vt *VirtualTun, raddr *addressPort, conn net.Conn) {
|
||||||
|
target, err := vt.resolveToAddrPort(raddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("[ERROR] TCP Server Tunnel to %s: %s\n", target, err.Error())
|
errorLogger.Printf("TCP Server Tunnel to %s: %s\n", target, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
tcpAddr := TCPAddrFromAddrPort(*target)
|
||||||
|
|
||||||
|
sconn, err := net.DialTCP("tcp", nil, tcpAddr)
|
||||||
|
if err != nil {
|
||||||
|
errorLogger.Printf("TCP Server Tunnel to %s: %s\n", target, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,24 +217,24 @@ func tcpServerForward(target *net.TCPAddr, conn net.Conn) {
|
|||||||
go connForward(1024, conn, sconn)
|
go connForward(1024, conn, sconn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SpawnRoutine spawns a TCP server on wireguard which acts as a proxy to the specified target
|
||||||
func (conf *TCPServerTunnelConfig) SpawnRoutine(vt *VirtualTun) {
|
func (conf *TCPServerTunnelConfig) SpawnRoutine(vt *VirtualTun) {
|
||||||
raddr, err := vt.ResolveAddrPort(conf.Target)
|
raddr, err := parseAddressPort(conf.Target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
tcpAddr := TCPAddrFromAddrPort(*raddr)
|
|
||||||
|
|
||||||
addr := &net.TCPAddr{Port: conf.ListenPort}
|
addr := &net.TCPAddr{Port: conf.ListenPort}
|
||||||
server, err := vt.tnet.ListenTCP(addr)
|
server, err := vt.tnet.ListenTCP(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
conn, err := server.Accept()
|
conn, err := server.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
go tcpServerForward(tcpAddr, conn)
|
go tcpServerForward(vt, raddr, conn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
41
wireguard.go
41
wireguard.go
@@ -1,14 +1,18 @@
|
|||||||
package wireproxy
|
package wireproxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"golang.zx2c4.com/go118/netip"
|
"net/netip"
|
||||||
|
|
||||||
|
"github.com/MakeNowJust/heredoc/v2"
|
||||||
"golang.zx2c4.com/wireguard/conn"
|
"golang.zx2c4.com/wireguard/conn"
|
||||||
"golang.zx2c4.com/wireguard/device"
|
"golang.zx2c4.com/wireguard/device"
|
||||||
"golang.zx2c4.com/wireguard/tun/netstack"
|
"golang.zx2c4.com/wireguard/tun/netstack"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// DeviceSetting contains the parameters for setting up a tun interface
|
||||||
type DeviceSetting struct {
|
type DeviceSetting struct {
|
||||||
ipcRequest string
|
ipcRequest string
|
||||||
dns []netip.Addr
|
dns []netip.Addr
|
||||||
@@ -16,18 +20,39 @@ type DeviceSetting struct {
|
|||||||
mtu int
|
mtu int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// serialize the config into an IPC request and DeviceSetting
|
||||||
func createIPCRequest(conf *DeviceConfig) (*DeviceSetting, error) {
|
func createIPCRequest(conf *DeviceConfig) (*DeviceSetting, error) {
|
||||||
request := fmt.Sprintf(`private_key=%s
|
var request bytes.Buffer
|
||||||
public_key=%s
|
|
||||||
endpoint=%s
|
|
||||||
persistent_keepalive_interval=%d
|
|
||||||
preshared_key=%s
|
|
||||||
allowed_ip=0.0.0.0/0`, conf.SelfSecretKey, conf.PeerPublicKey, conf.PeerEndpoint, conf.KeepAlive, conf.PreSharedKey)
|
|
||||||
|
|
||||||
setting := &DeviceSetting{ipcRequest: request, dns: conf.DNS, deviceAddr: conf.SelfEndpoint, mtu: conf.MTU}
|
request.WriteString(fmt.Sprintf("private_key=%s\n", conf.SecretKey))
|
||||||
|
|
||||||
|
for _, peer := range conf.Peers {
|
||||||
|
request.WriteString(fmt.Sprintf(heredoc.Doc(`
|
||||||
|
public_key=%s
|
||||||
|
endpoint=%s
|
||||||
|
persistent_keepalive_interval=%d
|
||||||
|
preshared_key=%s
|
||||||
|
`),
|
||||||
|
peer.PublicKey, peer.Endpoint, peer.KeepAlive, peer.PreSharedKey,
|
||||||
|
))
|
||||||
|
|
||||||
|
if len(peer.AllowedIPs) > 0 {
|
||||||
|
for _, ip := range peer.AllowedIPs {
|
||||||
|
request.WriteString(fmt.Sprintf("allowed_ip=%s\n", ip.String()))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
request.WriteString(heredoc.Doc(`
|
||||||
|
allowed_ip=0.0.0.0/0
|
||||||
|
allowed_ip=::0/0
|
||||||
|
`))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setting := &DeviceSetting{ipcRequest: request.String(), dns: conf.DNS, deviceAddr: conf.Endpoint, mtu: conf.MTU}
|
||||||
return setting, nil
|
return setting, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StartWireguard creates a tun interface on netstack given a configuration
|
||||||
func StartWireguard(conf *DeviceConfig) (*VirtualTun, error) {
|
func StartWireguard(conf *DeviceConfig) (*VirtualTun, error) {
|
||||||
setting, err := createIPCRequest(conf)
|
setting, err := createIPCRequest(conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user