mirror of
https://github.com/fosrl/gerbil.git
synced 2026-02-08 05:56:40 +00:00
Compare commits
66 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c3e73d0189 | ||
|
|
df2fbdf160 | ||
|
|
cb4ac8199d | ||
|
|
dd4b86b3e5 | ||
|
|
bad290aa4e | ||
|
|
8c27d5e3bf | ||
|
|
7e7a37d49c | ||
|
|
d44aa97f32 | ||
|
|
b57ad74589 | ||
|
|
82256a3f6f | ||
|
|
9e140a94db | ||
|
|
d0c9ea5a57 | ||
|
|
c88810ef24 | ||
|
|
463a4eea79 | ||
|
|
4576a2e8a7 | ||
|
|
69c13adcdb | ||
|
|
3886c1a8c1 | ||
|
|
06eb4d4310 | ||
|
|
247c47b27f | ||
|
|
060038c29b | ||
|
|
5414d21dcd | ||
|
|
364fa020aa | ||
|
|
b96ee16fbf | ||
|
|
467d69aa7c | ||
|
|
7c7762ebc5 | ||
|
|
526f9c8b4e | ||
|
|
905983cf61 | ||
|
|
a0879114e2 | ||
|
|
0d54a07973 | ||
|
|
4cb2fde961 | ||
|
|
9602599565 | ||
|
|
11f858b341 | ||
|
|
29b2cb33a2 | ||
|
|
34290ffe09 | ||
|
|
1013d0591e | ||
|
|
2f6d62ab45 | ||
|
|
8d6ba79408 | ||
|
|
208b434cb7 | ||
|
|
39ce0ac407 | ||
|
|
72bee56412 | ||
|
|
b32da3a714 | ||
|
|
971452e5d3 | ||
|
|
bba4345b0f | ||
|
|
b2392fb250 | ||
|
|
697f4131e7 | ||
|
|
e282715251 | ||
|
|
709df6db3e | ||
|
|
cf2b436470 | ||
|
|
2a29021572 | ||
|
|
a3f9a89079 | ||
|
|
ee27bf3153 | ||
|
|
a90f681957 | ||
|
|
3afc82ef9a | ||
|
|
d3a16f4c59 | ||
|
|
2a1911a66f | ||
|
|
08341b2385 | ||
|
|
6cde07d479 | ||
|
|
06b1e84f99 | ||
|
|
2b7e93ec92 | ||
|
|
ca23ae7a30 | ||
|
|
661fd86305 | ||
|
|
594a499b95 | ||
|
|
44aed84827 | ||
|
|
bf038eb4a2 | ||
|
|
6da3129b4e | ||
|
|
ac0f9b6a82 |
179
.github/workflows/cicd.yml
vendored
179
.github/workflows/cicd.yml
vendored
@@ -1,52 +1,161 @@
|
|||||||
name: CI/CD Pipeline
|
name: CI/CD Pipeline
|
||||||
|
|
||||||
|
# CI/CD workflow for building, publishing, mirroring, signing container images and building release binaries.
|
||||||
|
# Actions are pinned to specific SHAs to reduce supply-chain risk. This workflow triggers on tag push events.
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
packages: write # for GHCR push
|
||||||
|
id-token: write # for Cosign Keyless (OIDC) Signing
|
||||||
|
|
||||||
|
# Required secrets:
|
||||||
|
# - DOCKER_HUB_USERNAME / DOCKER_HUB_ACCESS_TOKEN: push to Docker Hub
|
||||||
|
# - GITHUB_TOKEN: used for GHCR login and OIDC keyless signing
|
||||||
|
# - COSIGN_PRIVATE_KEY / COSIGN_PASSWORD / COSIGN_PUBLIC_KEY: for key-based signing
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
tags:
|
tags:
|
||||||
- "*"
|
- "[0-9]+.[0-9]+.[0-9]+"
|
||||||
|
- "[0-9]+.[0-9]+.[0-9]+.rc.[0-9]+"
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: ${{ github.ref }}
|
||||||
|
cancel-in-progress: true
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
release:
|
release:
|
||||||
name: Build and Release
|
name: Build and Release
|
||||||
runs-on: ubuntu-latest
|
runs-on: amd64-runner
|
||||||
|
# Job-level timeout to avoid runaway or stuck runs
|
||||||
|
timeout-minutes: 120
|
||||||
|
env:
|
||||||
|
# Target images
|
||||||
|
DOCKERHUB_IMAGE: docker.io/fosrl/${{ github.event.repository.name }}
|
||||||
|
GHCR_IMAGE: ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v5
|
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
|
||||||
|
|
||||||
- name: Set up QEMU
|
- name: Set up QEMU
|
||||||
uses: docker/setup-qemu-action@v3
|
uses: docker/setup-qemu-action@c7c53464625b32c7a7e944ae62b3e17d2b600130 # v3.7.0
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0
|
||||||
|
|
||||||
- name: Log in to Docker Hub
|
- name: Log in to Docker Hub
|
||||||
uses: docker/login-action@v3
|
uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3.6.0
|
||||||
with:
|
with:
|
||||||
username: ${{ secrets.DOCKER_HUB_USERNAME }}
|
registry: docker.io
|
||||||
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
|
username: ${{ secrets.DOCKER_HUB_USERNAME }}
|
||||||
|
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
|
||||||
|
|
||||||
- name: Extract tag name
|
- name: Extract tag name
|
||||||
id: get-tag
|
id: get-tag
|
||||||
run: echo "TAG=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
|
run: echo "TAG=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
|
||||||
|
shell: bash
|
||||||
|
|
||||||
- name: Install Go
|
- name: Install Go
|
||||||
uses: actions/setup-go@v6
|
uses: actions/setup-go@7a3fe6cf4cb3a834922a1244abfce67bcef6a0c5 # v6.2.0
|
||||||
with:
|
with:
|
||||||
go-version: 1.25
|
go-version: 1.25
|
||||||
|
|
||||||
- name: Build and push Docker images
|
- name: Update version in main.go
|
||||||
run: |
|
run: |
|
||||||
TAG=${{ env.TAG }}
|
TAG=${{ env.TAG }}
|
||||||
make docker-build-release tag=$TAG
|
if [ -f main.go ]; then
|
||||||
|
sed -i 's/version_replaceme/'"$TAG"'/' main.go
|
||||||
|
echo "Updated main.go with version $TAG"
|
||||||
|
else
|
||||||
|
echo "main.go not found"
|
||||||
|
fi
|
||||||
|
shell: bash
|
||||||
|
|
||||||
- name: Build binaries
|
- name: Build and push Docker images (Docker Hub)
|
||||||
run: |
|
run: |
|
||||||
make go-build-release
|
TAG=${{ env.TAG }}
|
||||||
|
make docker-build-release tag=$TAG
|
||||||
|
echo "Built & pushed to: ${{ env.DOCKERHUB_IMAGE }}:${TAG}"
|
||||||
|
shell: bash
|
||||||
|
|
||||||
- name: Upload artifacts from /bin
|
- name: Login in to GHCR
|
||||||
uses: actions/upload-artifact@v4
|
uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3.6.0
|
||||||
with:
|
with:
|
||||||
name: binaries
|
registry: ghcr.io
|
||||||
path: bin/
|
username: ${{ github.actor }}
|
||||||
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Install skopeo + jq
|
||||||
|
# skopeo: copy/inspect images between registries
|
||||||
|
# jq: JSON parsing tool used to extract digest values
|
||||||
|
run: |
|
||||||
|
sudo apt-get update -y
|
||||||
|
sudo apt-get install -y skopeo jq
|
||||||
|
skopeo --version
|
||||||
|
shell: bash
|
||||||
|
|
||||||
|
- name: Copy tag from Docker Hub to GHCR
|
||||||
|
# Mirror the already-built image (all architectures) to GHCR so we can sign it
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
TAG=${{ env.TAG }}
|
||||||
|
echo "Copying ${{ env.DOCKERHUB_IMAGE }}:${TAG} -> ${{ env.GHCR_IMAGE }}:${TAG}"
|
||||||
|
skopeo copy --all --retry-times 3 \
|
||||||
|
docker://$DOCKERHUB_IMAGE:$TAG \
|
||||||
|
docker://$GHCR_IMAGE:$TAG
|
||||||
|
shell: bash
|
||||||
|
|
||||||
|
- name: Install cosign
|
||||||
|
# cosign is used to sign and verify container images (key and keyless)
|
||||||
|
uses: sigstore/cosign-installer@faadad0cce49287aee09b3a48701e75088a2c6ad # v4.0.0
|
||||||
|
|
||||||
|
- name: Dual-sign and verify (GHCR & Docker Hub)
|
||||||
|
# Sign each image by digest using keyless (OIDC) and key-based signing,
|
||||||
|
# then verify both the public key signature and the keyless OIDC signature.
|
||||||
|
env:
|
||||||
|
TAG: ${{ env.TAG }}
|
||||||
|
COSIGN_PRIVATE_KEY: ${{ secrets.COSIGN_PRIVATE_KEY }}
|
||||||
|
COSIGN_PASSWORD: ${{ secrets.COSIGN_PASSWORD }}
|
||||||
|
COSIGN_PUBLIC_KEY: ${{ secrets.COSIGN_PUBLIC_KEY }}
|
||||||
|
COSIGN_YES: "true"
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
issuer="https://token.actions.githubusercontent.com"
|
||||||
|
id_regex="^https://github.com/${{ github.repository }}/.+" # accept this repo (all workflows/refs)
|
||||||
|
|
||||||
|
for IMAGE in "${GHCR_IMAGE}" "${DOCKERHUB_IMAGE}"; do
|
||||||
|
echo "Processing ${IMAGE}:${TAG}"
|
||||||
|
|
||||||
|
DIGEST="$(skopeo inspect --retry-times 3 docker://${IMAGE}:${TAG} | jq -r '.Digest')"
|
||||||
|
REF="${IMAGE}@${DIGEST}"
|
||||||
|
echo "Resolved digest: ${REF}"
|
||||||
|
|
||||||
|
echo "==> cosign sign (keyless) --recursive ${REF}"
|
||||||
|
cosign sign --recursive "${REF}"
|
||||||
|
|
||||||
|
echo "==> cosign sign (key) --recursive ${REF}"
|
||||||
|
cosign sign --key env://COSIGN_PRIVATE_KEY --recursive "${REF}"
|
||||||
|
|
||||||
|
echo "==> cosign verify (public key) ${REF}"
|
||||||
|
cosign verify --key env://COSIGN_PUBLIC_KEY "${REF}" -o text
|
||||||
|
|
||||||
|
echo "==> cosign verify (keyless policy) ${REF}"
|
||||||
|
cosign verify \
|
||||||
|
--certificate-oidc-issuer "${issuer}" \
|
||||||
|
--certificate-identity-regexp "${id_regex}" \
|
||||||
|
"${REF}" -o text
|
||||||
|
done
|
||||||
|
shell: bash
|
||||||
|
|
||||||
|
- name: Build binaries
|
||||||
|
run: |
|
||||||
|
make go-build-release
|
||||||
|
shell: bash
|
||||||
|
|
||||||
|
- name: Upload artifacts from /bin
|
||||||
|
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
|
||||||
|
with:
|
||||||
|
name: binaries
|
||||||
|
path: bin/
|
||||||
|
|||||||
132
.github/workflows/mirror.yaml
vendored
Normal file
132
.github/workflows/mirror.yaml
vendored
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
name: Mirror & Sign (Docker Hub to GHCR)
|
||||||
|
|
||||||
|
on:
|
||||||
|
workflow_dispatch: {}
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
packages: write
|
||||||
|
id-token: write # for keyless OIDC
|
||||||
|
|
||||||
|
env:
|
||||||
|
SOURCE_IMAGE: docker.io/fosrl/gerbil
|
||||||
|
DEST_IMAGE: ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
mirror-and-dual-sign:
|
||||||
|
runs-on: amd64-runner
|
||||||
|
steps:
|
||||||
|
- name: Install skopeo + jq
|
||||||
|
run: |
|
||||||
|
sudo apt-get update -y
|
||||||
|
sudo apt-get install -y skopeo jq
|
||||||
|
skopeo --version
|
||||||
|
|
||||||
|
- name: Install cosign
|
||||||
|
uses: sigstore/cosign-installer@faadad0cce49287aee09b3a48701e75088a2c6ad # v4.0.0
|
||||||
|
|
||||||
|
- name: Input check
|
||||||
|
run: |
|
||||||
|
test -n "${SOURCE_IMAGE}" || (echo "SOURCE_IMAGE is empty" && exit 1)
|
||||||
|
echo "Source : ${SOURCE_IMAGE}"
|
||||||
|
echo "Target : ${DEST_IMAGE}"
|
||||||
|
|
||||||
|
# Auth for skopeo (containers-auth)
|
||||||
|
- name: Skopeo login to GHCR
|
||||||
|
run: |
|
||||||
|
skopeo login ghcr.io -u "${{ github.actor }}" -p "${{ secrets.GITHUB_TOKEN }}"
|
||||||
|
|
||||||
|
# Auth for cosign (docker-config)
|
||||||
|
- name: Docker login to GHCR (for cosign)
|
||||||
|
run: |
|
||||||
|
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u "${{ github.actor }}" --password-stdin
|
||||||
|
|
||||||
|
- name: List source tags
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
skopeo list-tags --retry-times 3 docker://"${SOURCE_IMAGE}" \
|
||||||
|
| jq -r '.Tags[]' | sort -u > src-tags.txt
|
||||||
|
echo "Found source tags: $(wc -l < src-tags.txt)"
|
||||||
|
head -n 20 src-tags.txt || true
|
||||||
|
|
||||||
|
- name: List destination tags (skip existing)
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
if skopeo list-tags --retry-times 3 docker://"${DEST_IMAGE}" >/tmp/dst.json 2>/dev/null; then
|
||||||
|
jq -r '.Tags[]' /tmp/dst.json | sort -u > dst-tags.txt
|
||||||
|
else
|
||||||
|
: > dst-tags.txt
|
||||||
|
fi
|
||||||
|
echo "Existing destination tags: $(wc -l < dst-tags.txt)"
|
||||||
|
|
||||||
|
- name: Mirror, dual-sign, and verify
|
||||||
|
env:
|
||||||
|
# keyless
|
||||||
|
COSIGN_YES: "true"
|
||||||
|
# key-based
|
||||||
|
COSIGN_PRIVATE_KEY: ${{ secrets.COSIGN_PRIVATE_KEY }}
|
||||||
|
COSIGN_PASSWORD: ${{ secrets.COSIGN_PASSWORD }}
|
||||||
|
# verify
|
||||||
|
COSIGN_PUBLIC_KEY: ${{ secrets.COSIGN_PUBLIC_KEY }}
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
copied=0; skipped=0; v_ok=0; errs=0
|
||||||
|
|
||||||
|
issuer="https://token.actions.githubusercontent.com"
|
||||||
|
id_regex="^https://github.com/${{ github.repository }}/.+"
|
||||||
|
|
||||||
|
while read -r tag; do
|
||||||
|
[ -z "$tag" ] && continue
|
||||||
|
|
||||||
|
if grep -Fxq "$tag" dst-tags.txt; then
|
||||||
|
echo "::notice ::Skip (exists) ${DEST_IMAGE}:${tag}"
|
||||||
|
skipped=$((skipped+1))
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "==> Copy ${SOURCE_IMAGE}:${tag} → ${DEST_IMAGE}:${tag}"
|
||||||
|
if ! skopeo copy --all --retry-times 3 \
|
||||||
|
docker://"${SOURCE_IMAGE}:${tag}" docker://"${DEST_IMAGE}:${tag}"; then
|
||||||
|
echo "::warning title=Copy failed::${SOURCE_IMAGE}:${tag}"
|
||||||
|
errs=$((errs+1)); continue
|
||||||
|
fi
|
||||||
|
copied=$((copied+1))
|
||||||
|
|
||||||
|
digest="$(skopeo inspect --retry-times 3 docker://"${DEST_IMAGE}:${tag}" | jq -r '.Digest')"
|
||||||
|
ref="${DEST_IMAGE}@${digest}"
|
||||||
|
|
||||||
|
echo "==> cosign sign (keyless) --recursive ${ref}"
|
||||||
|
if ! cosign sign --recursive "${ref}"; then
|
||||||
|
echo "::warning title=Keyless sign failed::${ref}"
|
||||||
|
errs=$((errs+1))
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "==> cosign sign (key) --recursive ${ref}"
|
||||||
|
if ! cosign sign --key env://COSIGN_PRIVATE_KEY --recursive "${ref}"; then
|
||||||
|
echo "::warning title=Key sign failed::${ref}"
|
||||||
|
errs=$((errs+1))
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "==> cosign verify (public key) ${ref}"
|
||||||
|
if ! cosign verify --key env://COSIGN_PUBLIC_KEY "${ref}" -o text; then
|
||||||
|
echo "::warning title=Verify(pubkey) failed::${ref}"
|
||||||
|
errs=$((errs+1))
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "==> cosign verify (keyless policy) ${ref}"
|
||||||
|
if ! cosign verify \
|
||||||
|
--certificate-oidc-issuer "${issuer}" \
|
||||||
|
--certificate-identity-regexp "${id_regex}" \
|
||||||
|
"${ref}" -o text; then
|
||||||
|
echo "::warning title=Verify(keyless) failed::${ref}"
|
||||||
|
errs=$((errs+1))
|
||||||
|
else
|
||||||
|
v_ok=$((v_ok+1))
|
||||||
|
fi
|
||||||
|
done < src-tags.txt
|
||||||
|
|
||||||
|
echo "---- Summary ----"
|
||||||
|
echo "Copied : $copied"
|
||||||
|
echo "Skipped : $skipped"
|
||||||
|
echo "Verified OK : $v_ok"
|
||||||
|
echo "Errors : $errs"
|
||||||
11
.github/workflows/test.yml
vendored
11
.github/workflows/test.yml
vendored
@@ -1,5 +1,8 @@
|
|||||||
name: Run Tests
|
name: Run Tests
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
|
||||||
on:
|
on:
|
||||||
pull_request:
|
pull_request:
|
||||||
branches:
|
branches:
|
||||||
@@ -8,15 +11,15 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
runs-on: ubuntu-latest
|
runs-on: amd64-runner
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v5
|
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
|
||||||
|
|
||||||
- name: Set up Go
|
- name: Set up Go
|
||||||
uses: actions/setup-go@v6
|
uses: actions/setup-go@7a3fe6cf4cb3a834922a1244abfce67bcef6a0c5 # v6.2.0
|
||||||
with:
|
with:
|
||||||
go-version: '1.25'
|
go-version: 1.25
|
||||||
|
|
||||||
- name: Build go
|
- name: Build go
|
||||||
run: go build
|
run: go build
|
||||||
|
|||||||
@@ -4,11 +4,7 @@ Contributions are welcome!
|
|||||||
|
|
||||||
Please see the contribution and local development guide on the docs page before getting started:
|
Please see the contribution and local development guide on the docs page before getting started:
|
||||||
|
|
||||||
https://docs.fossorial.io/development
|
https://docs.pangolin.net/development/contributing
|
||||||
|
|
||||||
For ideas about what features to work on and our future plans, please see the roadmap:
|
|
||||||
|
|
||||||
https://docs.fossorial.io/roadmap
|
|
||||||
|
|
||||||
### Licensing Considerations
|
### Licensing Considerations
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ COPY . .
|
|||||||
RUN CGO_ENABLED=0 GOOS=linux go build -o /gerbil
|
RUN CGO_ENABLED=0 GOOS=linux go build -o /gerbil
|
||||||
|
|
||||||
# Start a new stage from scratch
|
# Start a new stage from scratch
|
||||||
FROM alpine:3.22 AS runner
|
FROM alpine:3.23 AS runner
|
||||||
|
|
||||||
RUN apk add --no-cache iptables iproute2
|
RUN apk add --no-cache iptables iproute2
|
||||||
|
|
||||||
|
|||||||
12
README.md
12
README.md
@@ -6,7 +6,7 @@ Gerbil is a simple [WireGuard](https://www.wireguard.com/) interface management
|
|||||||
|
|
||||||
Gerbil works with Pangolin, Newt, and Olm as part of the larger system. See documentation below:
|
Gerbil works with Pangolin, Newt, and Olm as part of the larger system. See documentation below:
|
||||||
|
|
||||||
- [Full Documentation](https://docs.fossorial.io)
|
- [Full Documentation](https://docs.pangolin.net)
|
||||||
|
|
||||||
## Key Functions
|
## Key Functions
|
||||||
|
|
||||||
@@ -42,13 +42,12 @@ In single node (self hosted) Pangolin deployments this can be bypassed by using
|
|||||||
|
|
||||||
## CLI Args
|
## CLI Args
|
||||||
|
|
||||||
|
Important:
|
||||||
- `reachableAt`: How should the remote server reach Gerbil's API?
|
- `reachableAt`: How should the remote server reach Gerbil's API?
|
||||||
- `generateAndSaveKeyTo`: Where to save the generated WireGuard private key to persist across restarts.
|
- `generateAndSaveKeyTo`: Where to save the generated WireGuard private key to persist across restarts.
|
||||||
- `remoteConfig` (optional): Remote config location to HTTP get the JSON based config from. See `example_config.json`
|
- `remoteConfig`: Remote config location to HTTP get the JSON based config from.
|
||||||
- `config` (optional): Local JSON file path to load config. Used if remote config is not supplied. See `example_config.json`
|
|
||||||
|
|
||||||
Note: You must use either `config` or `remoteConfig` to configure WireGuard.
|
|
||||||
|
|
||||||
|
Others:
|
||||||
- `reportBandwidthTo` (optional): **DEPRECATED** - Use `remoteConfig` instead. Remote HTTP endpoint to send peer bandwidth data
|
- `reportBandwidthTo` (optional): **DEPRECATED** - Use `remoteConfig` instead. Remote HTTP endpoint to send peer bandwidth data
|
||||||
- `interface` (optional): Name of the WireGuard interface created by Gerbil. Default: `wg0`
|
- `interface` (optional): Name of the WireGuard interface created by Gerbil. Default: `wg0`
|
||||||
- `listen` (optional): Port to listen on for HTTP server. Default: `:3004`
|
- `listen` (optional): Port to listen on for HTTP server. Default: `:3004`
|
||||||
@@ -66,7 +65,6 @@ Note: You must use either `config` or `remoteConfig` to configure WireGuard.
|
|||||||
All CLI arguments can also be provided via environment variables:
|
All CLI arguments can also be provided via environment variables:
|
||||||
|
|
||||||
- `INTERFACE`: Name of the WireGuard interface
|
- `INTERFACE`: Name of the WireGuard interface
|
||||||
- `CONFIG`: Path to local configuration file
|
|
||||||
- `REMOTE_CONFIG`: URL of the remote config server
|
- `REMOTE_CONFIG`: URL of the remote config server
|
||||||
- `LISTEN`: Address to listen on for HTTP server
|
- `LISTEN`: Address to listen on for HTTP server
|
||||||
- `GENERATE_AND_SAVE_KEY_TO`: Path to save generated private key
|
- `GENERATE_AND_SAVE_KEY_TO`: Path to save generated private key
|
||||||
@@ -96,7 +94,7 @@ services:
|
|||||||
container_name: gerbil
|
container_name: gerbil
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
command:
|
command:
|
||||||
- --reachableAt=http://gerbil:3003
|
- --reachableAt=http://gerbil:3004
|
||||||
- --generateAndSaveKeyTo=/var/config/key
|
- --generateAndSaveKeyTo=/var/config/key
|
||||||
- --remoteConfig=http://pangolin:3001/api/v1/
|
- --remoteConfig=http://pangolin:3001/api/v1/
|
||||||
volumes:
|
volumes:
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
If you discover a security vulnerability, please follow the steps below to responsibly disclose it to us:
|
If you discover a security vulnerability, please follow the steps below to responsibly disclose it to us:
|
||||||
|
|
||||||
1. **Do not create a public GitHub issue or discussion post.** This could put the security of other users at risk.
|
1. **Do not create a public GitHub issue or discussion post.** This could put the security of other users at risk.
|
||||||
2. Send a detailed report to [security@fossorial.io](mailto:security@fossorial.io) or send a **private** message to a maintainer on [Discord](https://discord.gg/HCJR8Xhme4). Include:
|
2. Send a detailed report to [security@pangolin.net](mailto:security@pangolin.net) or send a **private** message to a maintainer on [Discord](https://discord.gg/HCJR8Xhme4). Include:
|
||||||
|
|
||||||
- Description and location of the vulnerability.
|
- Description and location of the vulnerability.
|
||||||
- Potential impact of the vulnerability.
|
- Potential impact of the vulnerability.
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
{
|
|
||||||
"privateKey": "kBGTgk7c+zncEEoSnMl+jsLjVh5ZVoL/HwBSQem+d1M=",
|
|
||||||
"listenPort": 51820,
|
|
||||||
"ipAddress": "10.0.0.1/24",
|
|
||||||
"peers": [
|
|
||||||
{
|
|
||||||
"publicKey": "5UzzoeveFVSzuqK3nTMS5bA1jIMs1fQffVQzJ8MXUQM=",
|
|
||||||
"allowedIps": ["10.0.0.0/28"]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"publicKey": "kYrZpuO2NsrFoBh1GMNgkhd1i9Rgtu1rAjbJ7qsfngU=",
|
|
||||||
"allowedIps": ["10.0.0.16/28"]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"publicKey": "1YfPUVr9ZF4zehkbI2BQhCxaRLz+Vtwa4vJwH+mpK0A=",
|
|
||||||
"allowedIps": ["10.0.0.32/28"]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"publicKey": "2/U4oyZ+sai336Dal/yExCphL8AxyqvIxMk4qsUy4iI=",
|
|
||||||
"allowedIps": ["10.0.0.48/28"]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
8
go.mod
8
go.mod
@@ -5,7 +5,8 @@ go 1.25
|
|||||||
require (
|
require (
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible
|
github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||||
github.com/vishvananda/netlink v1.3.1
|
github.com/vishvananda/netlink v1.3.1
|
||||||
golang.org/x/crypto v0.43.0
|
golang.org/x/crypto v0.46.0
|
||||||
|
golang.org/x/sync v0.1.0
|
||||||
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20230429144221-925a1e7659e6
|
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20230429144221-925a1e7659e6
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,8 +17,7 @@ require (
|
|||||||
github.com/mdlayher/netlink v1.7.2 // indirect
|
github.com/mdlayher/netlink v1.7.2 // indirect
|
||||||
github.com/mdlayher/socket v0.4.1 // indirect
|
github.com/mdlayher/socket v0.4.1 // indirect
|
||||||
github.com/vishvananda/netns v0.0.5 // indirect
|
github.com/vishvananda/netns v0.0.5 // indirect
|
||||||
golang.org/x/net v0.45.0 // indirect
|
golang.org/x/net v0.47.0 // indirect
|
||||||
golang.org/x/sync v0.1.0 // indirect
|
golang.org/x/sys v0.39.0 // indirect
|
||||||
golang.org/x/sys v0.37.0 // indirect
|
|
||||||
golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b // indirect
|
golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
12
go.sum
12
go.sum
@@ -16,16 +16,16 @@ github.com/vishvananda/netlink v1.3.1 h1:3AEMt62VKqz90r0tmNhog0r/PpWKmrEShJU0wJW
|
|||||||
github.com/vishvananda/netlink v1.3.1/go.mod h1:ARtKouGSTGchR8aMwmkzC0qiNPrrWO5JS/XMVl45+b4=
|
github.com/vishvananda/netlink v1.3.1/go.mod h1:ARtKouGSTGchR8aMwmkzC0qiNPrrWO5JS/XMVl45+b4=
|
||||||
github.com/vishvananda/netns v0.0.5 h1:DfiHV+j8bA32MFM7bfEunvT8IAqQ/NzSJHtcmW5zdEY=
|
github.com/vishvananda/netns v0.0.5 h1:DfiHV+j8bA32MFM7bfEunvT8IAqQ/NzSJHtcmW5zdEY=
|
||||||
github.com/vishvananda/netns v0.0.5/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
|
github.com/vishvananda/netns v0.0.5/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
|
||||||
golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04=
|
golang.org/x/crypto v0.46.0 h1:cKRW/pmt1pKAfetfu+RCEvjvZkA9RimPbh7bhFjGVBU=
|
||||||
golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0=
|
golang.org/x/crypto v0.46.0/go.mod h1:Evb/oLKmMraqjZ2iQTwDwvCtJkczlDuTmdJXoZVzqU0=
|
||||||
golang.org/x/net v0.45.0 h1:RLBg5JKixCy82FtLJpeNlVM0nrSqpCRYzVU1n8kj0tM=
|
golang.org/x/net v0.47.0 h1:Mx+4dIFzqraBXUugkia1OOvlD6LemFo1ALMHjrXDOhY=
|
||||||
golang.org/x/net v0.45.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY=
|
golang.org/x/net v0.47.0/go.mod h1:/jNxtkgq5yWUGYkaZGqo27cfGZ1c5Nen03aYrrKpVRU=
|
||||||
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
|
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
|
||||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
|
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
|
||||||
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||||
golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b h1:J1CaxgLerRR5lgx3wnr6L04cJFbWoceSK9JWBdglINo=
|
golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b h1:J1CaxgLerRR5lgx3wnr6L04cJFbWoceSK9JWBdglINo=
|
||||||
golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b/go.mod h1:tqur9LnfstdR9ep2LaJT4lFUl0EjlHtge+gAjmsHUG4=
|
golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b/go.mod h1:tqur9LnfstdR9ep2LaJT4lFUl0EjlHtge+gAjmsHUG4=
|
||||||
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20230429144221-925a1e7659e6 h1:CawjfCvYQH2OU3/TnxLx97WDSUDRABfT18pCOYwc2GE=
|
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20230429144221-925a1e7659e6 h1:CawjfCvYQH2OU3/TnxLx97WDSUDRABfT18pCOYwc2GE=
|
||||||
|
|||||||
243
main.go
243
main.go
@@ -2,15 +2,21 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
_ "net/http/pprof"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"runtime"
|
||||||
|
"runtime/pprof"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -21,6 +27,7 @@ import (
|
|||||||
"github.com/fosrl/gerbil/proxy"
|
"github.com/fosrl/gerbil/proxy"
|
||||||
"github.com/fosrl/gerbil/relay"
|
"github.com/fosrl/gerbil/relay"
|
||||||
"github.com/vishvananda/netlink"
|
"github.com/vishvananda/netlink"
|
||||||
|
"golang.org/x/sync/errgroup"
|
||||||
"golang.zx2c4.com/wireguard/wgctrl"
|
"golang.zx2c4.com/wireguard/wgctrl"
|
||||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||||
)
|
)
|
||||||
@@ -40,6 +47,7 @@ var (
|
|||||||
type WgConfig struct {
|
type WgConfig struct {
|
||||||
PrivateKey string `json:"privateKey"`
|
PrivateKey string `json:"privateKey"`
|
||||||
ListenPort int `json:"listenPort"`
|
ListenPort int `json:"listenPort"`
|
||||||
|
RelayPort int `json:"relayPort"`
|
||||||
IpAddress string `json:"ipAddress"`
|
IpAddress string `json:"ipAddress"`
|
||||||
Peers []Peer `json:"peers"`
|
Peers []Peer `json:"peers"`
|
||||||
}
|
}
|
||||||
@@ -108,6 +116,8 @@ func parseLogLevel(level string) logger.LogLevel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
go monitorMemory(1024 * 1024 * 512) // trigger if memory usage exceeds 512MB
|
||||||
|
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
wgconfig WgConfig
|
wgconfig WgConfig
|
||||||
@@ -217,6 +227,10 @@ func main() {
|
|||||||
logger.Init()
|
logger.Init()
|
||||||
logger.GetLogger().SetLevel(parseLogLevel(logLevel))
|
logger.GetLogger().SetLevel(parseLogLevel(logLevel))
|
||||||
|
|
||||||
|
// Base context for the application; cancel on SIGINT/SIGTERM
|
||||||
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||||
|
defer stop()
|
||||||
|
|
||||||
// try to parse as http://host:port and set the listenAddr to the :port from this reachableAt.
|
// try to parse as http://host:port and set the listenAddr to the :port from this reachableAt.
|
||||||
if reachableAt != "" && listenAddr == "" {
|
if reachableAt != "" && listenAddr == "" {
|
||||||
if strings.HasPrefix(reachableAt, "http://") || strings.HasPrefix(reachableAt, "https://") {
|
if strings.HasPrefix(reachableAt, "http://") || strings.HasPrefix(reachableAt, "https://") {
|
||||||
@@ -324,10 +338,20 @@ func main() {
|
|||||||
// Ensure the WireGuard peers exist
|
// Ensure the WireGuard peers exist
|
||||||
ensureWireguardPeers(wgconfig.Peers)
|
ensureWireguardPeers(wgconfig.Peers)
|
||||||
|
|
||||||
go periodicBandwidthCheck(remoteConfigURL + "/gerbil/receive-bandwidth")
|
// Child error group derived from base context
|
||||||
|
group, groupCtx := errgroup.WithContext(ctx)
|
||||||
|
|
||||||
|
// Periodic bandwidth reporting
|
||||||
|
group.Go(func() error {
|
||||||
|
return periodicBandwidthCheck(groupCtx, remoteConfigURL+"/gerbil/receive-bandwidth")
|
||||||
|
})
|
||||||
|
|
||||||
// Start the UDP proxy server
|
// Start the UDP proxy server
|
||||||
proxyRelay = relay.NewUDPProxyServer(":21820", remoteConfigURL, key, reachableAt)
|
relayPort := wgconfig.RelayPort
|
||||||
|
if relayPort == 0 {
|
||||||
|
relayPort = 21820 // in case there is no relay port set, use 21820
|
||||||
|
}
|
||||||
|
proxyRelay = relay.NewUDPProxyServer(groupCtx, fmt.Sprintf(":%d", relayPort), remoteConfigURL, key, reachableAt)
|
||||||
err = proxyRelay.Start()
|
err = proxyRelay.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal("Failed to start UDP proxy server: %v", err)
|
logger.Fatal("Failed to start UDP proxy server: %v", err)
|
||||||
@@ -369,20 +393,42 @@ func main() {
|
|||||||
http.HandleFunc("/update-proxy-mapping", handleUpdateProxyMapping)
|
http.HandleFunc("/update-proxy-mapping", handleUpdateProxyMapping)
|
||||||
http.HandleFunc("/update-destinations", handleUpdateDestinations)
|
http.HandleFunc("/update-destinations", handleUpdateDestinations)
|
||||||
http.HandleFunc("/update-local-snis", handleUpdateLocalSNIs)
|
http.HandleFunc("/update-local-snis", handleUpdateLocalSNIs)
|
||||||
|
http.HandleFunc("/healthz", handleHealthz)
|
||||||
logger.Info("Starting HTTP server on %s", listenAddr)
|
logger.Info("Starting HTTP server on %s", listenAddr)
|
||||||
|
|
||||||
// Run HTTP server in a goroutine
|
// HTTP server with graceful shutdown on context cancel
|
||||||
go func() {
|
server := &http.Server{
|
||||||
if err := http.ListenAndServe(listenAddr, nil); err != nil {
|
Addr: listenAddr,
|
||||||
logger.Error("HTTP server failed: %v", err)
|
Handler: nil,
|
||||||
|
}
|
||||||
|
group.Go(func() error {
|
||||||
|
// http.ErrServerClosed is returned on graceful shutdown; not an error for us
|
||||||
|
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
}()
|
return nil
|
||||||
|
})
|
||||||
|
group.Go(func() error {
|
||||||
|
<-groupCtx.Done()
|
||||||
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
_ = server.Shutdown(shutdownCtx)
|
||||||
|
// Stop background components as the context is canceled
|
||||||
|
if proxySNI != nil {
|
||||||
|
_ = proxySNI.Stop()
|
||||||
|
}
|
||||||
|
if proxyRelay != nil {
|
||||||
|
proxyRelay.Stop()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
// Keep the main goroutine running
|
// Wait for all goroutines to finish
|
||||||
sigCh := make(chan os.Signal, 1)
|
if err := group.Wait(); err != nil && !errors.Is(err, context.Canceled) {
|
||||||
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
|
logger.Error("Service exited with error: %v", err)
|
||||||
<-sigCh
|
} else if errors.Is(err, context.Canceled) {
|
||||||
logger.Info("Shutting down servers...")
|
logger.Info("Context cancelled, shutting down")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadRemoteConfig(url string, key wgtypes.Key, reachableAt string) (WgConfig, error) {
|
func loadRemoteConfig(url string, key wgtypes.Key, reachableAt string) (WgConfig, error) {
|
||||||
@@ -509,6 +555,10 @@ func ensureWireguardInterface(wgconfig WgConfig) error {
|
|||||||
logger.Warn("Failed to ensure MSS clamping: %v", err)
|
logger.Warn("Failed to ensure MSS clamping: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := ensureWireguardFirewall(); err != nil {
|
||||||
|
logger.Warn("Failed to ensure WireGuard firewall rules: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
logger.Info("WireGuard interface %s created and configured", interfaceName)
|
logger.Info("WireGuard interface %s created and configured", interfaceName)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -639,7 +689,7 @@ func ensureMSSClamping() error {
|
|||||||
if out, err := addCmd.CombinedOutput(); err != nil {
|
if out, err := addCmd.CombinedOutput(); err != nil {
|
||||||
errMsg := fmt.Sprintf("Failed to add MSS clamping rule for chain %s: %v (output: %s)",
|
errMsg := fmt.Sprintf("Failed to add MSS clamping rule for chain %s: %v (output: %s)",
|
||||||
chain, err, string(out))
|
chain, err, string(out))
|
||||||
logger.Error(errMsg)
|
logger.Error("%s", errMsg)
|
||||||
errors = append(errors, fmt.Errorf("%s", errMsg))
|
errors = append(errors, fmt.Errorf("%s", errMsg))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -656,7 +706,7 @@ func ensureMSSClamping() error {
|
|||||||
if out, err := checkCmd.CombinedOutput(); err != nil {
|
if out, err := checkCmd.CombinedOutput(); err != nil {
|
||||||
errMsg := fmt.Sprintf("Rule verification failed for chain %s: %v (output: %s)",
|
errMsg := fmt.Sprintf("Rule verification failed for chain %s: %v (output: %s)",
|
||||||
chain, err, string(out))
|
chain, err, string(out))
|
||||||
logger.Error(errMsg)
|
logger.Error("%s", errMsg)
|
||||||
errors = append(errors, fmt.Errorf("%s", errMsg))
|
errors = append(errors, fmt.Errorf("%s", errMsg))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -677,6 +727,113 @@ func ensureMSSClamping() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ensureWireguardFirewall() error {
|
||||||
|
// Rules to enforce:
|
||||||
|
// 1. Allow established/related connections (responses to our outbound traffic)
|
||||||
|
// 2. Allow ICMP ping packets
|
||||||
|
// 3. Drop all other inbound traffic from peers
|
||||||
|
|
||||||
|
// Define the rules we want to ensure exist
|
||||||
|
rules := [][]string{
|
||||||
|
// Allow established and related connections (responses to outbound traffic)
|
||||||
|
{
|
||||||
|
"-A", "INPUT",
|
||||||
|
"-i", interfaceName,
|
||||||
|
"-m", "conntrack",
|
||||||
|
"--ctstate", "ESTABLISHED,RELATED",
|
||||||
|
"-j", "ACCEPT",
|
||||||
|
},
|
||||||
|
// Allow ICMP ping requests
|
||||||
|
{
|
||||||
|
"-A", "INPUT",
|
||||||
|
"-i", interfaceName,
|
||||||
|
"-p", "icmp",
|
||||||
|
"--icmp-type", "8",
|
||||||
|
"-j", "ACCEPT",
|
||||||
|
},
|
||||||
|
// Drop all other inbound traffic from WireGuard interface
|
||||||
|
{
|
||||||
|
"-A", "INPUT",
|
||||||
|
"-i", interfaceName,
|
||||||
|
"-j", "DROP",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// First, try to delete any existing rules for this interface
|
||||||
|
for _, rule := range rules {
|
||||||
|
deleteArgs := make([]string, len(rule))
|
||||||
|
copy(deleteArgs, rule)
|
||||||
|
// Change -A to -D for deletion
|
||||||
|
for i, arg := range deleteArgs {
|
||||||
|
if arg == "-A" {
|
||||||
|
deleteArgs[i] = "-D"
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteCmd := exec.Command("/usr/sbin/iptables", deleteArgs...)
|
||||||
|
logger.Debug("Attempting to delete existing firewall rule: %v", deleteArgs)
|
||||||
|
|
||||||
|
// Try deletion multiple times to handle multiple existing rules
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
out, err := deleteCmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
if exitErr, ok := err.(*exec.ExitError); ok {
|
||||||
|
logger.Debug("Deletion stopped: %v (output: %s)", exitErr.String(), string(out))
|
||||||
|
}
|
||||||
|
break // No more rules to delete
|
||||||
|
}
|
||||||
|
logger.Info("Deleted existing firewall rule (attempt %d)", i+1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now add the rules
|
||||||
|
var errors []error
|
||||||
|
for i, rule := range rules {
|
||||||
|
addCmd := exec.Command("/usr/sbin/iptables", rule...)
|
||||||
|
logger.Info("Adding WireGuard firewall rule %d: %v", i+1, rule)
|
||||||
|
|
||||||
|
if out, err := addCmd.CombinedOutput(); err != nil {
|
||||||
|
errMsg := fmt.Sprintf("Failed to add firewall rule %d: %v (output: %s)", i+1, err, string(out))
|
||||||
|
logger.Error("%s", errMsg)
|
||||||
|
errors = append(errors, fmt.Errorf("%s", errMsg))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify the rule was added by checking
|
||||||
|
checkArgs := make([]string, len(rule))
|
||||||
|
copy(checkArgs, rule)
|
||||||
|
// Change -A to -C for check
|
||||||
|
for j, arg := range checkArgs {
|
||||||
|
if arg == "-A" {
|
||||||
|
checkArgs[j] = "-C"
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
checkCmd := exec.Command("/usr/sbin/iptables", checkArgs...)
|
||||||
|
if out, err := checkCmd.CombinedOutput(); err != nil {
|
||||||
|
errMsg := fmt.Sprintf("Rule verification failed for rule %d: %v (output: %s)", i+1, err, string(out))
|
||||||
|
logger.Error("%s", errMsg)
|
||||||
|
errors = append(errors, fmt.Errorf("%s", errMsg))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Info("Successfully added and verified WireGuard firewall rule %d", i+1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(errors) > 0 {
|
||||||
|
var errMsgs []string
|
||||||
|
for _, err := range errors {
|
||||||
|
errMsgs = append(errMsgs, err.Error())
|
||||||
|
}
|
||||||
|
return fmt.Errorf("WireGuard firewall setup encountered errors:\n%s", strings.Join(errMsgs, "\n"))
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Info("WireGuard firewall rules successfully configured for interface %s", interfaceName)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func handlePeer(w http.ResponseWriter, r *http.Request) {
|
func handlePeer(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodPost:
|
case http.MethodPost:
|
||||||
@@ -688,6 +845,15 @@ func handlePeer(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleHealthz(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodGet {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
_, _ = w.Write([]byte("ok"))
|
||||||
|
}
|
||||||
|
|
||||||
func handleAddPeer(w http.ResponseWriter, r *http.Request) {
|
func handleAddPeer(w http.ResponseWriter, r *http.Request) {
|
||||||
var peer Peer
|
var peer Peer
|
||||||
if err := json.NewDecoder(r.Body).Decode(&peer); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&peer); err != nil {
|
||||||
@@ -977,13 +1143,18 @@ func handleUpdateLocalSNIs(w http.ResponseWriter, r *http.Request) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func periodicBandwidthCheck(endpoint string) {
|
func periodicBandwidthCheck(ctx context.Context, endpoint string) error {
|
||||||
ticker := time.NewTicker(10 * time.Second)
|
ticker := time.NewTicker(10 * time.Second)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
for range ticker.C {
|
for {
|
||||||
if err := reportPeerBandwidth(endpoint); err != nil {
|
select {
|
||||||
logger.Info("Failed to report peer bandwidth: %v", err)
|
case <-ticker.C:
|
||||||
|
if err := reportPeerBandwidth(endpoint); err != nil {
|
||||||
|
logger.Info("Failed to report peer bandwidth: %v", err)
|
||||||
|
}
|
||||||
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1003,8 +1174,13 @@ func calculatePeerBandwidth() ([]PeerBandwidth, error) {
|
|||||||
mu.Lock()
|
mu.Lock()
|
||||||
defer mu.Unlock()
|
defer mu.Unlock()
|
||||||
|
|
||||||
|
// Track the set of peers currently present on the device to prune stale readings efficiently
|
||||||
|
currentPeerKeys := make(map[string]struct{}, len(device.Peers))
|
||||||
|
|
||||||
for _, peer := range device.Peers {
|
for _, peer := range device.Peers {
|
||||||
publicKey := peer.PublicKey.String()
|
publicKey := peer.PublicKey.String()
|
||||||
|
currentPeerKeys[publicKey] = struct{}{}
|
||||||
|
|
||||||
currentReading := PeerReading{
|
currentReading := PeerReading{
|
||||||
BytesReceived: peer.ReceiveBytes,
|
BytesReceived: peer.ReceiveBytes,
|
||||||
BytesTransmitted: peer.TransmitBytes,
|
BytesTransmitted: peer.TransmitBytes,
|
||||||
@@ -1061,14 +1237,7 @@ func calculatePeerBandwidth() ([]PeerBandwidth, error) {
|
|||||||
|
|
||||||
// Clean up old peers
|
// Clean up old peers
|
||||||
for publicKey := range lastReadings {
|
for publicKey := range lastReadings {
|
||||||
found := false
|
if _, exists := currentPeerKeys[publicKey]; !exists {
|
||||||
for _, peer := range device.Peers {
|
|
||||||
if peer.PublicKey.String() == publicKey {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
delete(lastReadings, publicKey)
|
delete(lastReadings, publicKey)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1124,3 +1293,25 @@ func notifyPeerChange(action, publicKey string) {
|
|||||||
logger.Warn("Notify server returned non-OK: %s", resp.Status)
|
logger.Warn("Notify server returned non-OK: %s", resp.Status)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func monitorMemory(limit uint64) {
|
||||||
|
var m runtime.MemStats
|
||||||
|
for {
|
||||||
|
runtime.ReadMemStats(&m)
|
||||||
|
if m.Alloc > limit {
|
||||||
|
fmt.Printf("Memory spike detected (%d bytes). Dumping profile...\n", m.Alloc)
|
||||||
|
|
||||||
|
f, err := os.Create(fmt.Sprintf("/var/config/heap/heap-spike-%d.pprof", time.Now().Unix()))
|
||||||
|
if err != nil {
|
||||||
|
log.Println("could not create profile:", err)
|
||||||
|
} else {
|
||||||
|
pprof.WriteHeapProfile(f)
|
||||||
|
f.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait a while before checking again to avoid spamming profiles
|
||||||
|
time.Sleep(5 * time.Minute)
|
||||||
|
}
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -758,14 +758,20 @@ func (p *SNIProxy) pipe(clientConn, targetConn net.Conn, clientReader io.Reader)
|
|||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
wg.Add(2)
|
wg.Add(2)
|
||||||
|
|
||||||
|
// closeOnce ensures we only close connections once
|
||||||
|
var closeOnce sync.Once
|
||||||
|
closeConns := func() {
|
||||||
|
closeOnce.Do(func() {
|
||||||
|
// Close both connections to unblock any pending reads
|
||||||
|
clientConn.Close()
|
||||||
|
targetConn.Close()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// Copy data from client to target (using the buffered reader)
|
// Copy data from client to target (using the buffered reader)
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
defer func() {
|
defer closeConns()
|
||||||
if tcpConn, ok := targetConn.(*net.TCPConn); ok {
|
|
||||||
tcpConn.CloseWrite()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Use a large buffer for better performance
|
// Use a large buffer for better performance
|
||||||
buf := make([]byte, 32*1024)
|
buf := make([]byte, 32*1024)
|
||||||
@@ -778,11 +784,7 @@ func (p *SNIProxy) pipe(clientConn, targetConn net.Conn, clientReader io.Reader)
|
|||||||
// Copy data from target to client
|
// Copy data from target to client
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
defer func() {
|
defer closeConns()
|
||||||
if tcpConn, ok := clientConn.(*net.TCPConn); ok {
|
|
||||||
tcpConn.CloseWrite()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Use a large buffer for better performance
|
// Use a large buffer for better performance
|
||||||
buf := make([]byte, 32*1024)
|
buf := make([]byte, 32*1024)
|
||||||
|
|||||||
267
relay/relay.go
267
relay/relay.go
@@ -2,6 +2,7 @@ package relay
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -24,20 +25,22 @@ type EncryptedHolePunchMessage struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type HolePunchMessage struct {
|
type HolePunchMessage struct {
|
||||||
OlmID string `json:"olmId"`
|
OlmID string `json:"olmId"`
|
||||||
NewtID string `json:"newtId"`
|
NewtID string `json:"newtId"`
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
|
PublicKey string `json:"publicKey"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClientEndpoint struct {
|
type ClientEndpoint struct {
|
||||||
OlmID string `json:"olmId"`
|
OlmID string `json:"olmId"`
|
||||||
NewtID string `json:"newtId"`
|
NewtID string `json:"newtId"`
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
IP string `json:"ip"`
|
IP string `json:"ip"`
|
||||||
Port int `json:"port"`
|
Port int `json:"port"`
|
||||||
Timestamp int64 `json:"timestamp"`
|
Timestamp int64 `json:"timestamp"`
|
||||||
ReachableAt string `json:"reachableAt"`
|
ReachableAt string `json:"reachableAt"`
|
||||||
PublicKey string `json:"publicKey"`
|
ExitNodePublicKey string `json:"exitNodePublicKey"`
|
||||||
|
ClientPublicKey string `json:"publicKey"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Updated to support multiple destination peers
|
// Updated to support multiple destination peers
|
||||||
@@ -58,12 +61,41 @@ type DestinationConn struct {
|
|||||||
|
|
||||||
// Type for storing WireGuard handshake information
|
// Type for storing WireGuard handshake information
|
||||||
type WireGuardSession struct {
|
type WireGuardSession struct {
|
||||||
|
mu sync.RWMutex
|
||||||
ReceiverIndex uint32
|
ReceiverIndex uint32
|
||||||
SenderIndex uint32
|
SenderIndex uint32
|
||||||
DestAddr *net.UDPAddr
|
DestAddr *net.UDPAddr
|
||||||
LastSeen time.Time
|
LastSeen time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetSenderIndex returns the SenderIndex in a thread-safe manner
|
||||||
|
func (s *WireGuardSession) GetSenderIndex() uint32 {
|
||||||
|
s.mu.RLock()
|
||||||
|
defer s.mu.RUnlock()
|
||||||
|
return s.SenderIndex
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDestAddr returns the DestAddr in a thread-safe manner
|
||||||
|
func (s *WireGuardSession) GetDestAddr() *net.UDPAddr {
|
||||||
|
s.mu.RLock()
|
||||||
|
defer s.mu.RUnlock()
|
||||||
|
return s.DestAddr
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLastSeen returns the LastSeen timestamp in a thread-safe manner
|
||||||
|
func (s *WireGuardSession) GetLastSeen() time.Time {
|
||||||
|
s.mu.RLock()
|
||||||
|
defer s.mu.RUnlock()
|
||||||
|
return s.LastSeen
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateLastSeen updates the LastSeen timestamp in a thread-safe manner
|
||||||
|
func (s *WireGuardSession) UpdateLastSeen() {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
s.LastSeen = time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
// Type for tracking bidirectional communication patterns to rebuild sessions
|
// Type for tracking bidirectional communication patterns to rebuild sessions
|
||||||
type CommunicationPattern struct {
|
type CommunicationPattern struct {
|
||||||
FromClient *net.UDPAddr // The client address
|
FromClient *net.UDPAddr // The client address
|
||||||
@@ -112,6 +144,8 @@ type UDPProxyServer struct {
|
|||||||
connections sync.Map // map[string]*DestinationConn where key is destination "ip:port"
|
connections sync.Map // map[string]*DestinationConn where key is destination "ip:port"
|
||||||
privateKey wgtypes.Key
|
privateKey wgtypes.Key
|
||||||
packetChan chan Packet
|
packetChan chan Packet
|
||||||
|
ctx context.Context
|
||||||
|
cancel context.CancelFunc
|
||||||
|
|
||||||
// Session tracking for WireGuard peers
|
// Session tracking for WireGuard peers
|
||||||
// Key format: "senderIndex:receiverIndex"
|
// Key format: "senderIndex:receiverIndex"
|
||||||
@@ -123,14 +157,17 @@ type UDPProxyServer struct {
|
|||||||
ReachableAt string
|
ReachableAt string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewUDPProxyServer initializes the server with a buffered packet channel.
|
// NewUDPProxyServer initializes the server with a buffered packet channel and derived context.
|
||||||
func NewUDPProxyServer(addr, serverURL string, privateKey wgtypes.Key, reachableAt string) *UDPProxyServer {
|
func NewUDPProxyServer(parentCtx context.Context, addr, serverURL string, privateKey wgtypes.Key, reachableAt string) *UDPProxyServer {
|
||||||
|
ctx, cancel := context.WithCancel(parentCtx)
|
||||||
return &UDPProxyServer{
|
return &UDPProxyServer{
|
||||||
addr: addr,
|
addr: addr,
|
||||||
serverURL: serverURL,
|
serverURL: serverURL,
|
||||||
privateKey: privateKey,
|
privateKey: privateKey,
|
||||||
packetChan: make(chan Packet, 1000),
|
packetChan: make(chan Packet, 1000),
|
||||||
ReachableAt: reachableAt,
|
ReachableAt: reachableAt,
|
||||||
|
ctx: ctx,
|
||||||
|
cancel: cancel,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -177,17 +214,51 @@ func (s *UDPProxyServer) Start() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *UDPProxyServer) Stop() {
|
func (s *UDPProxyServer) Stop() {
|
||||||
s.conn.Close()
|
// Signal all background goroutines to stop
|
||||||
|
if s.cancel != nil {
|
||||||
|
s.cancel()
|
||||||
|
}
|
||||||
|
// Close listener to unblock reads
|
||||||
|
if s.conn != nil {
|
||||||
|
_ = s.conn.Close()
|
||||||
|
}
|
||||||
|
// Close all downstream UDP connections
|
||||||
|
s.connections.Range(func(key, value interface{}) bool {
|
||||||
|
if dc, ok := value.(*DestinationConn); ok && dc.conn != nil {
|
||||||
|
_ = dc.conn.Close()
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
// Close packet channel to stop workers
|
||||||
|
select {
|
||||||
|
case <-s.ctx.Done():
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
close(s.packetChan)
|
||||||
}
|
}
|
||||||
|
|
||||||
// readPackets continuously reads from the UDP socket and pushes packets into the channel.
|
// readPackets continuously reads from the UDP socket and pushes packets into the channel.
|
||||||
func (s *UDPProxyServer) readPackets() {
|
func (s *UDPProxyServer) readPackets() {
|
||||||
for {
|
for {
|
||||||
|
// Exit promptly if context is canceled
|
||||||
|
select {
|
||||||
|
case <-s.ctx.Done():
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
}
|
||||||
buf := bufferPool.Get().([]byte)
|
buf := bufferPool.Get().([]byte)
|
||||||
n, remoteAddr, err := s.conn.ReadFromUDP(buf)
|
n, remoteAddr, err := s.conn.ReadFromUDP(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Error reading UDP packet: %v", err)
|
// If we're shutting down, exit
|
||||||
continue
|
select {
|
||||||
|
case <-s.ctx.Done():
|
||||||
|
bufferPool.Put(buf[:1500])
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
logger.Error("Error reading UDP packet: %v", err)
|
||||||
|
bufferPool.Put(buf[:1500])
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
s.packetChan <- Packet{data: buf[:n], remoteAddr: remoteAddr, n: n}
|
s.packetChan <- Packet{data: buf[:n], remoteAddr: remoteAddr, n: n}
|
||||||
}
|
}
|
||||||
@@ -236,14 +307,15 @@ func (s *UDPProxyServer) packetWorker() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
endpoint := ClientEndpoint{
|
endpoint := ClientEndpoint{
|
||||||
NewtID: msg.NewtID,
|
NewtID: msg.NewtID,
|
||||||
OlmID: msg.OlmID,
|
OlmID: msg.OlmID,
|
||||||
Token: msg.Token,
|
Token: msg.Token,
|
||||||
IP: packet.remoteAddr.IP.String(),
|
IP: packet.remoteAddr.IP.String(),
|
||||||
Port: packet.remoteAddr.Port,
|
Port: packet.remoteAddr.Port,
|
||||||
Timestamp: time.Now().Unix(),
|
Timestamp: time.Now().Unix(),
|
||||||
ReachableAt: s.ReachableAt,
|
ReachableAt: s.ReachableAt,
|
||||||
PublicKey: s.privateKey.PublicKey().String(),
|
ExitNodePublicKey: s.privateKey.PublicKey().String(),
|
||||||
|
ClientPublicKey: msg.PublicKey,
|
||||||
}
|
}
|
||||||
logger.Debug("Created endpoint from packet remoteAddr %s: IP=%s, Port=%d", packet.remoteAddr.String(), endpoint.IP, endpoint.Port)
|
logger.Debug("Created endpoint from packet remoteAddr %s: IP=%s, Port=%d", packet.remoteAddr.String(), endpoint.IP, endpoint.Port)
|
||||||
s.notifyServer(endpoint)
|
s.notifyServer(endpoint)
|
||||||
@@ -392,7 +464,7 @@ func (s *UDPProxyServer) handleWireGuardPacket(packet []byte, remoteAddr *net.UD
|
|||||||
|
|
||||||
_, err = conn.Write(packet)
|
_, err = conn.Write(packet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Failed to forward handshake initiation: %v", err)
|
logger.Debug("Failed to forward handshake initiation: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -442,13 +514,11 @@ func (s *UDPProxyServer) handleWireGuardPacket(packet []byte, remoteAddr *net.UD
|
|||||||
// First check for existing sessions to see if we know where to send this packet
|
// First check for existing sessions to see if we know where to send this packet
|
||||||
s.wgSessions.Range(func(k, v interface{}) bool {
|
s.wgSessions.Range(func(k, v interface{}) bool {
|
||||||
session := v.(*WireGuardSession)
|
session := v.(*WireGuardSession)
|
||||||
if session.SenderIndex == receiverIndex {
|
// Check if session matches (read lock for check)
|
||||||
// Found matching session
|
if session.GetSenderIndex() == receiverIndex {
|
||||||
destAddr = session.DestAddr
|
// Found matching session - get dest addr and update last seen
|
||||||
|
destAddr = session.GetDestAddr()
|
||||||
// Update last seen time
|
session.UpdateLastSeen()
|
||||||
session.LastSeen = time.Now()
|
|
||||||
s.wgSessions.Store(k, session)
|
|
||||||
return false // stop iteration
|
return false // stop iteration
|
||||||
}
|
}
|
||||||
return true // continue iteration
|
return true // continue iteration
|
||||||
@@ -588,49 +658,69 @@ func (s *UDPProxyServer) handleResponses(conn *net.UDPConn, destAddr *net.UDPAdd
|
|||||||
// Add a cleanup method to periodically remove idle connections
|
// Add a cleanup method to periodically remove idle connections
|
||||||
func (s *UDPProxyServer) cleanupIdleConnections() {
|
func (s *UDPProxyServer) cleanupIdleConnections() {
|
||||||
ticker := time.NewTicker(5 * time.Minute)
|
ticker := time.NewTicker(5 * time.Minute)
|
||||||
for range ticker.C {
|
defer ticker.Stop()
|
||||||
now := time.Now()
|
for {
|
||||||
s.connections.Range(func(key, value interface{}) bool {
|
select {
|
||||||
destConn := value.(*DestinationConn)
|
case <-ticker.C:
|
||||||
if now.Sub(destConn.lastUsed) > 10*time.Minute {
|
now := time.Now()
|
||||||
destConn.conn.Close()
|
s.connections.Range(func(key, value interface{}) bool {
|
||||||
s.connections.Delete(key)
|
destConn := value.(*DestinationConn)
|
||||||
}
|
if now.Sub(destConn.lastUsed) > 10*time.Minute {
|
||||||
return true
|
destConn.conn.Close()
|
||||||
})
|
s.connections.Delete(key)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
case <-s.ctx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// New method to periodically remove idle sessions
|
// New method to periodically remove idle sessions
|
||||||
func (s *UDPProxyServer) cleanupIdleSessions() {
|
func (s *UDPProxyServer) cleanupIdleSessions() {
|
||||||
ticker := time.NewTicker(5 * time.Minute)
|
ticker := time.NewTicker(5 * time.Minute)
|
||||||
for range ticker.C {
|
|
||||||
now := time.Now()
|
defer ticker.Stop()
|
||||||
s.wgSessions.Range(func(key, value interface{}) bool {
|
for {
|
||||||
session := value.(*WireGuardSession)
|
select {
|
||||||
if now.Sub(session.LastSeen) > 15*time.Minute {
|
case <-ticker.C:
|
||||||
s.wgSessions.Delete(key)
|
now := time.Now()
|
||||||
logger.Debug("Removed idle session: %s", key)
|
s.wgSessions.Range(func(key, value interface{}) bool {
|
||||||
}
|
session := value.(*WireGuardSession)
|
||||||
return true
|
// Use thread-safe method to read LastSeen
|
||||||
})
|
if now.Sub(session.GetLastSeen()) > 15*time.Minute {
|
||||||
|
s.wgSessions.Delete(key)
|
||||||
|
logger.Debug("Removed idle session: %s", key)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
case <-s.ctx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// New method to periodically remove idle proxy mappings
|
// New method to periodically remove idle proxy mappings
|
||||||
func (s *UDPProxyServer) cleanupIdleProxyMappings() {
|
func (s *UDPProxyServer) cleanupIdleProxyMappings() {
|
||||||
ticker := time.NewTicker(10 * time.Minute)
|
ticker := time.NewTicker(10 * time.Minute)
|
||||||
for range ticker.C {
|
defer ticker.Stop()
|
||||||
now := time.Now()
|
for {
|
||||||
s.proxyMappings.Range(func(key, value interface{}) bool {
|
select {
|
||||||
mapping := value.(ProxyMapping)
|
case <-ticker.C:
|
||||||
// Remove mappings that haven't been used in 30 minutes
|
now := time.Now()
|
||||||
if now.Sub(mapping.LastUsed) > 30*time.Minute {
|
s.proxyMappings.Range(func(key, value interface{}) bool {
|
||||||
s.proxyMappings.Delete(key)
|
mapping := value.(ProxyMapping)
|
||||||
logger.Debug("Removed idle proxy mapping: %s", key)
|
// Remove mappings that haven't been used in 30 minutes
|
||||||
}
|
if now.Sub(mapping.LastUsed) > 30*time.Minute {
|
||||||
return true
|
s.proxyMappings.Delete(key)
|
||||||
})
|
logger.Debug("Removed idle proxy mapping: %s", key)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
case <-s.ctx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -735,8 +825,9 @@ func (s *UDPProxyServer) clearSessionsForIP(ip string) {
|
|||||||
keyStr := key.(string)
|
keyStr := key.(string)
|
||||||
session := value.(*WireGuardSession)
|
session := value.(*WireGuardSession)
|
||||||
|
|
||||||
// Check if the session's destination address contains the WG IP
|
// Check if the session's destination address contains the WG IP (thread-safe)
|
||||||
if session.DestAddr != nil && session.DestAddr.IP.String() == ip {
|
destAddr := session.GetDestAddr()
|
||||||
|
if destAddr != nil && destAddr.IP.String() == ip {
|
||||||
keysToDelete = append(keysToDelete, keyStr)
|
keysToDelete = append(keysToDelete, keyStr)
|
||||||
logger.Debug("Marking session for deletion for WG IP %s: %s", ip, keyStr)
|
logger.Debug("Marking session for deletion for WG IP %s: %s", ip, keyStr)
|
||||||
}
|
}
|
||||||
@@ -748,7 +839,7 @@ func (s *UDPProxyServer) clearSessionsForIP(ip string) {
|
|||||||
s.wgSessions.Delete(key)
|
s.wgSessions.Delete(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Info("Cleared %d sessions for WG IP: %s", len(keysToDelete), ip)
|
logger.Debug("Cleared %d sessions for WG IP: %s", len(keysToDelete), ip)
|
||||||
}
|
}
|
||||||
|
|
||||||
// // clearProxyMappingsForWGIP removes all proxy mappings that have destinations pointing to a specific WireGuard IP
|
// // clearProxyMappingsForWGIP removes all proxy mappings that have destinations pointing to a specific WireGuard IP
|
||||||
@@ -926,14 +1017,12 @@ func (s *UDPProxyServer) tryRebuildSession(pattern *CommunicationPattern) {
|
|||||||
|
|
||||||
// Check if we already have this session
|
// Check if we already have this session
|
||||||
if _, exists := s.wgSessions.Load(sessionKey); !exists {
|
if _, exists := s.wgSessions.Load(sessionKey); !exists {
|
||||||
session := &WireGuardSession{
|
s.wgSessions.Store(sessionKey, &WireGuardSession{
|
||||||
ReceiverIndex: pattern.DestIndex,
|
ReceiverIndex: pattern.DestIndex,
|
||||||
SenderIndex: pattern.ClientIndex,
|
SenderIndex: pattern.ClientIndex,
|
||||||
DestAddr: pattern.ToDestination,
|
DestAddr: pattern.ToDestination,
|
||||||
LastSeen: time.Now(),
|
LastSeen: time.Now(),
|
||||||
}
|
})
|
||||||
|
|
||||||
s.wgSessions.Store(sessionKey, session)
|
|
||||||
logger.Info("Rebuilt WireGuard session from communication pattern: %s -> %s (packets: %d)",
|
logger.Info("Rebuilt WireGuard session from communication pattern: %s -> %s (packets: %d)",
|
||||||
sessionKey, pattern.ToDestination.String(), pattern.PacketCount)
|
sessionKey, pattern.ToDestination.String(), pattern.PacketCount)
|
||||||
}
|
}
|
||||||
@@ -943,23 +1032,29 @@ func (s *UDPProxyServer) tryRebuildSession(pattern *CommunicationPattern) {
|
|||||||
// cleanupIdleCommunicationPatterns periodically removes idle communication patterns
|
// cleanupIdleCommunicationPatterns periodically removes idle communication patterns
|
||||||
func (s *UDPProxyServer) cleanupIdleCommunicationPatterns() {
|
func (s *UDPProxyServer) cleanupIdleCommunicationPatterns() {
|
||||||
ticker := time.NewTicker(10 * time.Minute)
|
ticker := time.NewTicker(10 * time.Minute)
|
||||||
for range ticker.C {
|
defer ticker.Stop()
|
||||||
now := time.Now()
|
for {
|
||||||
s.commPatterns.Range(func(key, value interface{}) bool {
|
select {
|
||||||
pattern := value.(*CommunicationPattern)
|
case <-ticker.C:
|
||||||
|
now := time.Now()
|
||||||
|
s.commPatterns.Range(func(key, value interface{}) bool {
|
||||||
|
pattern := value.(*CommunicationPattern)
|
||||||
|
|
||||||
// Get the most recent activity
|
// Get the most recent activity
|
||||||
lastActivity := pattern.LastFromClient
|
lastActivity := pattern.LastFromClient
|
||||||
if pattern.LastFromDest.After(lastActivity) {
|
if pattern.LastFromDest.After(lastActivity) {
|
||||||
lastActivity = pattern.LastFromDest
|
lastActivity = pattern.LastFromDest
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove patterns that haven't had activity in 20 minutes
|
// Remove patterns that haven't had activity in 20 minutes
|
||||||
if now.Sub(lastActivity) > 20*time.Minute {
|
if now.Sub(lastActivity) > 20*time.Minute {
|
||||||
s.commPatterns.Delete(key)
|
s.commPatterns.Delete(key)
|
||||||
logger.Debug("Removed idle communication pattern: %s", key)
|
logger.Debug("Removed idle communication pattern: %s", key)
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
case <-s.ctx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user