mirror of
https://github.com/netbirdio/netbird.git
synced 2026-05-12 03:39:55 +00:00
Compare commits
2 Commits
test/proxy
...
trigger-pr
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bfeb60fbb5 | ||
|
|
ea41cf2d2c |
@@ -1,6 +0,0 @@
|
|||||||
.env
|
|
||||||
.env.*
|
|
||||||
*.pem
|
|
||||||
*.key
|
|
||||||
*.crt
|
|
||||||
*.p12
|
|
||||||
14
.github/ISSUE_TEMPLATE/config.yml
vendored
14
.github/ISSUE_TEMPLATE/config.yml
vendored
@@ -1,14 +0,0 @@
|
|||||||
blank_issues_enabled: true
|
|
||||||
contact_links:
|
|
||||||
- name: Community Support
|
|
||||||
url: https://forum.netbird.io/
|
|
||||||
about: Community support forum
|
|
||||||
- name: Cloud Support
|
|
||||||
url: https://docs.netbird.io/help/report-bug-issues
|
|
||||||
about: Contact us for support
|
|
||||||
- name: Client/Connection Troubleshooting
|
|
||||||
url: https://docs.netbird.io/help/troubleshooting-client
|
|
||||||
about: See our client troubleshooting guide for help addressing common issues
|
|
||||||
- name: Self-host Troubleshooting
|
|
||||||
url: https://docs.netbird.io/selfhosted/troubleshooting
|
|
||||||
about: See our self-host troubleshooting guide for help addressing common issues
|
|
||||||
10
.github/workflows/check-license-dependencies.yml
vendored
10
.github/workflows/check-license-dependencies.yml
vendored
@@ -23,7 +23,7 @@ jobs:
|
|||||||
|
|
||||||
- name: Check for problematic license dependencies
|
- name: Check for problematic license dependencies
|
||||||
run: |
|
run: |
|
||||||
echo "Checking for dependencies on management/, signal/, relay/, and proxy/ packages..."
|
echo "Checking for dependencies on management/, signal/, and relay/ packages..."
|
||||||
echo ""
|
echo ""
|
||||||
|
|
||||||
# Find all directories except the problematic ones and system dirs
|
# Find all directories except the problematic ones and system dirs
|
||||||
@@ -31,7 +31,7 @@ jobs:
|
|||||||
while IFS= read -r dir; do
|
while IFS= read -r dir; do
|
||||||
echo "=== Checking $dir ==="
|
echo "=== Checking $dir ==="
|
||||||
# Search for problematic imports, excluding test files
|
# Search for problematic imports, excluding test files
|
||||||
RESULTS=$(grep -r "github.com/netbirdio/netbird/\(management\|signal\|relay\|proxy\)" "$dir" --include="*.go" 2>/dev/null | grep -v "_test.go" | grep -v "test_" | grep -v "/test/" || true)
|
RESULTS=$(grep -r "github.com/netbirdio/netbird/\(management\|signal\|relay\)" "$dir" --include="*.go" 2>/dev/null | grep -v "_test.go" | grep -v "test_" | grep -v "/test/" || true)
|
||||||
if [ -n "$RESULTS" ]; then
|
if [ -n "$RESULTS" ]; then
|
||||||
echo "❌ Found problematic dependencies:"
|
echo "❌ Found problematic dependencies:"
|
||||||
echo "$RESULTS"
|
echo "$RESULTS"
|
||||||
@@ -39,11 +39,11 @@ jobs:
|
|||||||
else
|
else
|
||||||
echo "✓ No problematic dependencies found"
|
echo "✓ No problematic dependencies found"
|
||||||
fi
|
fi
|
||||||
done < <(find . -maxdepth 1 -type d -not -name "." -not -name "management" -not -name "signal" -not -name "relay" -not -name "proxy" -not -name "combined" -not -name ".git*" | sort)
|
done < <(find . -maxdepth 1 -type d -not -name "." -not -name "management" -not -name "signal" -not -name "relay" -not -name ".git*" | sort)
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
if [ $FOUND_ISSUES -eq 1 ]; then
|
if [ $FOUND_ISSUES -eq 1 ]; then
|
||||||
echo "❌ Found dependencies on management/, signal/, relay/, or proxy/ packages"
|
echo "❌ Found dependencies on management/, signal/, or relay/ packages"
|
||||||
echo "These packages are licensed under AGPLv3 and must not be imported by BSD-licensed code"
|
echo "These packages are licensed under AGPLv3 and must not be imported by BSD-licensed code"
|
||||||
exit 1
|
exit 1
|
||||||
else
|
else
|
||||||
@@ -88,7 +88,7 @@ jobs:
|
|||||||
IMPORTERS=$(go list -json -deps ./... 2>/dev/null | jq -r "select(.Imports[]? == \"$package\") | .ImportPath")
|
IMPORTERS=$(go list -json -deps ./... 2>/dev/null | jq -r "select(.Imports[]? == \"$package\") | .ImportPath")
|
||||||
|
|
||||||
# Check if any importer is NOT in management/signal/relay
|
# Check if any importer is NOT in management/signal/relay
|
||||||
BSD_IMPORTER=$(echo "$IMPORTERS" | grep -v "github.com/netbirdio/netbird/\(management\|signal\|relay\|proxy\|combined\)" | head -1)
|
BSD_IMPORTER=$(echo "$IMPORTERS" | grep -v "github.com/netbirdio/netbird/\(management\|signal\|relay\)" | head -1)
|
||||||
|
|
||||||
if [ -n "$BSD_IMPORTER" ]; then
|
if [ -n "$BSD_IMPORTER" ]; then
|
||||||
echo "❌ $package ($license) is imported by BSD-licensed code: $BSD_IMPORTER"
|
echo "❌ $package ($license) is imported by BSD-licensed code: $BSD_IMPORTER"
|
||||||
|
|||||||
2
.github/workflows/golang-test-darwin.yml
vendored
2
.github/workflows/golang-test-darwin.yml
vendored
@@ -43,5 +43,5 @@ jobs:
|
|||||||
run: git --no-pager diff --exit-code
|
run: git --no-pager diff --exit-code
|
||||||
|
|
||||||
- name: Test
|
- name: Test
|
||||||
run: NETBIRD_STORE_ENGINE=${{ matrix.store }} CI=true go test -tags=devcert -exec 'sudo --preserve-env=CI,NETBIRD_STORE_ENGINE' -timeout 5m -p 1 $(go list ./... | grep -v -e /management -e /signal -e /relay -e /proxy -e /combined)
|
run: NETBIRD_STORE_ENGINE=${{ matrix.store }} CI=true go test -tags=devcert -exec 'sudo --preserve-env=CI,NETBIRD_STORE_ENGINE' -timeout 5m -p 1 $(go list ./... | grep -v /management)
|
||||||
|
|
||||||
|
|||||||
1
.github/workflows/golang-test-freebsd.yml
vendored
1
.github/workflows/golang-test-freebsd.yml
vendored
@@ -46,5 +46,6 @@ jobs:
|
|||||||
time go test -timeout 1m -failfast ./client/iface/...
|
time go test -timeout 1m -failfast ./client/iface/...
|
||||||
time go test -timeout 1m -failfast ./route/...
|
time go test -timeout 1m -failfast ./route/...
|
||||||
time go test -timeout 1m -failfast ./sharedsock/...
|
time go test -timeout 1m -failfast ./sharedsock/...
|
||||||
|
time go test -timeout 1m -failfast ./signal/...
|
||||||
time go test -timeout 1m -failfast ./util/...
|
time go test -timeout 1m -failfast ./util/...
|
||||||
time go test -timeout 1m -failfast ./version/...
|
time go test -timeout 1m -failfast ./version/...
|
||||||
|
|||||||
98
.github/workflows/golang-test-linux.yml
vendored
98
.github/workflows/golang-test-linux.yml
vendored
@@ -97,16 +97,6 @@ jobs:
|
|||||||
working-directory: relay
|
working-directory: relay
|
||||||
run: CGO_ENABLED=1 GOARCH=386 go build -o relay-386 .
|
run: CGO_ENABLED=1 GOARCH=386 go build -o relay-386 .
|
||||||
|
|
||||||
- name: Build combined
|
|
||||||
if: steps.cache.outputs.cache-hit != 'true'
|
|
||||||
working-directory: combined
|
|
||||||
run: CGO_ENABLED=1 go build .
|
|
||||||
|
|
||||||
- name: Build combined 386
|
|
||||||
if: steps.cache.outputs.cache-hit != 'true'
|
|
||||||
working-directory: combined
|
|
||||||
run: CGO_ENABLED=1 GOARCH=386 go build -o combined-386 .
|
|
||||||
|
|
||||||
test:
|
test:
|
||||||
name: "Client / Unit"
|
name: "Client / Unit"
|
||||||
needs: [build-cache]
|
needs: [build-cache]
|
||||||
@@ -154,7 +144,7 @@ jobs:
|
|||||||
run: git --no-pager diff --exit-code
|
run: git --no-pager diff --exit-code
|
||||||
|
|
||||||
- name: Test
|
- name: Test
|
||||||
run: CGO_ENABLED=1 GOARCH=${{ matrix.arch }} CI=true go test -tags devcert -exec 'sudo' -timeout 10m -p 1 $(go list ./... | grep -v -e /management -e /signal -e /relay -e /proxy -e /combined)
|
run: CGO_ENABLED=1 GOARCH=${{ matrix.arch }} CI=true go test -tags devcert -exec 'sudo' -timeout 10m -p 1 $(go list ./... | grep -v -e /management -e /signal -e /relay)
|
||||||
|
|
||||||
test_client_on_docker:
|
test_client_on_docker:
|
||||||
name: "Client (Docker) / Unit"
|
name: "Client (Docker) / Unit"
|
||||||
@@ -214,7 +204,7 @@ jobs:
|
|||||||
sh -c ' \
|
sh -c ' \
|
||||||
apk update; apk add --no-cache \
|
apk update; apk add --no-cache \
|
||||||
ca-certificates iptables ip6tables dbus dbus-dev libpcap-dev build-base; \
|
ca-certificates iptables ip6tables dbus dbus-dev libpcap-dev build-base; \
|
||||||
go test -buildvcs=false -tags devcert -v -timeout 10m -p 1 $(go list -buildvcs=false ./... | grep -v -e /management -e /signal -e /relay -e /proxy -e /combined -e /client/ui -e /upload-server)
|
go test -buildvcs=false -tags devcert -v -timeout 10m -p 1 $(go list -buildvcs=false ./... | grep -v -e /management -e /signal -e /relay -e /client/ui -e /upload-server)
|
||||||
'
|
'
|
||||||
|
|
||||||
test_relay:
|
test_relay:
|
||||||
@@ -271,53 +261,6 @@ jobs:
|
|||||||
-exec 'sudo' \
|
-exec 'sudo' \
|
||||||
-timeout 10m -p 1 ./relay/... ./shared/relay/...
|
-timeout 10m -p 1 ./relay/... ./shared/relay/...
|
||||||
|
|
||||||
test_proxy:
|
|
||||||
name: "Proxy / Unit"
|
|
||||||
needs: [build-cache]
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
|
||||||
arch: [ '386','amd64' ]
|
|
||||||
runs-on: ubuntu-22.04
|
|
||||||
steps:
|
|
||||||
- name: Checkout code
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Install Go
|
|
||||||
uses: actions/setup-go@v5
|
|
||||||
with:
|
|
||||||
go-version-file: "go.mod"
|
|
||||||
cache: false
|
|
||||||
|
|
||||||
- name: Install dependencies
|
|
||||||
run: sudo apt update && sudo apt install -y gcc-multilib g++-multilib libc6-dev-i386
|
|
||||||
|
|
||||||
- name: Get Go environment
|
|
||||||
run: |
|
|
||||||
echo "cache=$(go env GOCACHE)" >> $GITHUB_ENV
|
|
||||||
echo "modcache=$(go env GOMODCACHE)" >> $GITHUB_ENV
|
|
||||||
|
|
||||||
- name: Cache Go modules
|
|
||||||
uses: actions/cache/restore@v4
|
|
||||||
with:
|
|
||||||
path: |
|
|
||||||
${{ env.cache }}
|
|
||||||
${{ env.modcache }}
|
|
||||||
key: ${{ runner.os }}-gotest-cache-${{ hashFiles('**/go.sum') }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-gotest-cache-
|
|
||||||
|
|
||||||
- name: Install modules
|
|
||||||
run: go mod tidy
|
|
||||||
|
|
||||||
- name: check git status
|
|
||||||
run: git --no-pager diff --exit-code
|
|
||||||
|
|
||||||
- name: Test
|
|
||||||
run: |
|
|
||||||
CGO_ENABLED=1 GOARCH=${{ matrix.arch }} \
|
|
||||||
go test -timeout 10m -p 1 ./proxy/...
|
|
||||||
|
|
||||||
test_signal:
|
test_signal:
|
||||||
name: "Signal / Unit"
|
name: "Signal / Unit"
|
||||||
needs: [build-cache]
|
needs: [build-cache]
|
||||||
@@ -409,19 +352,12 @@ jobs:
|
|||||||
run: git --no-pager diff --exit-code
|
run: git --no-pager diff --exit-code
|
||||||
|
|
||||||
- name: Login to Docker hub
|
- name: Login to Docker hub
|
||||||
if: github.event.pull_request && github.event.pull_request.head.repo && github.event.pull_request.head.repo.full_name == '' || github.repository == github.event.pull_request.head.repo.full_name || !github.head_ref
|
if: matrix.store == 'mysql' && (github.repository == github.head.repo.full_name || !github.head_ref)
|
||||||
uses: docker/login-action@v3
|
uses: docker/login-action@v1
|
||||||
with:
|
with:
|
||||||
username: ${{ secrets.DOCKER_USER }}
|
username: ${{ secrets.DOCKER_USER }}
|
||||||
password: ${{ secrets.DOCKER_TOKEN }}
|
password: ${{ secrets.DOCKER_TOKEN }}
|
||||||
|
|
||||||
- name: docker login for root user
|
|
||||||
if: github.event.pull_request && github.event.pull_request.head.repo && github.event.pull_request.head.repo.full_name == '' || github.repository == github.event.pull_request.head.repo.full_name || !github.head_ref
|
|
||||||
env:
|
|
||||||
DOCKER_USER: ${{ secrets.DOCKER_USER }}
|
|
||||||
DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }}
|
|
||||||
run: echo "$DOCKER_TOKEN" | sudo docker login --username "$DOCKER_USER" --password-stdin
|
|
||||||
|
|
||||||
- name: download mysql image
|
- name: download mysql image
|
||||||
if: matrix.store == 'mysql'
|
if: matrix.store == 'mysql'
|
||||||
run: docker pull mlsmaycon/warmed-mysql:8
|
run: docker pull mlsmaycon/warmed-mysql:8
|
||||||
@@ -504,18 +440,15 @@ jobs:
|
|||||||
run: git --no-pager diff --exit-code
|
run: git --no-pager diff --exit-code
|
||||||
|
|
||||||
- name: Login to Docker hub
|
- name: Login to Docker hub
|
||||||
if: github.event.pull_request && github.event.pull_request.head.repo && github.event.pull_request.head.repo.full_name == '' || github.repository == github.event.pull_request.head.repo.full_name || !github.head_ref
|
if: matrix.store == 'mysql' && (github.repository == github.head.repo.full_name || !github.head_ref)
|
||||||
uses: docker/login-action@v3
|
uses: docker/login-action@v1
|
||||||
with:
|
with:
|
||||||
username: ${{ secrets.DOCKER_USER }}
|
username: ${{ secrets.DOCKER_USER }}
|
||||||
password: ${{ secrets.DOCKER_TOKEN }}
|
password: ${{ secrets.DOCKER_TOKEN }}
|
||||||
|
|
||||||
- name: docker login for root user
|
- name: download mysql image
|
||||||
if: github.event.pull_request && github.event.pull_request.head.repo && github.event.pull_request.head.repo.full_name == '' || github.repository == github.event.pull_request.head.repo.full_name || !github.head_ref
|
if: matrix.store == 'mysql'
|
||||||
env:
|
run: docker pull mlsmaycon/warmed-mysql:8
|
||||||
DOCKER_USER: ${{ secrets.DOCKER_USER }}
|
|
||||||
DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }}
|
|
||||||
run: echo "$DOCKER_TOKEN" | sudo docker login --username "$DOCKER_USER" --password-stdin
|
|
||||||
|
|
||||||
- name: Test
|
- name: Test
|
||||||
run: |
|
run: |
|
||||||
@@ -596,18 +529,15 @@ jobs:
|
|||||||
run: git --no-pager diff --exit-code
|
run: git --no-pager diff --exit-code
|
||||||
|
|
||||||
- name: Login to Docker hub
|
- name: Login to Docker hub
|
||||||
if: github.event.pull_request && github.event.pull_request.head.repo && github.event.pull_request.head.repo.full_name == '' || github.repository == github.event.pull_request.head.repo.full_name || !github.head_ref
|
if: matrix.store == 'mysql' && (github.repository == github.head.repo.full_name || !github.head_ref)
|
||||||
uses: docker/login-action@v3
|
uses: docker/login-action@v1
|
||||||
with:
|
with:
|
||||||
username: ${{ secrets.DOCKER_USER }}
|
username: ${{ secrets.DOCKER_USER }}
|
||||||
password: ${{ secrets.DOCKER_TOKEN }}
|
password: ${{ secrets.DOCKER_TOKEN }}
|
||||||
|
|
||||||
- name: docker login for root user
|
- name: download mysql image
|
||||||
if: github.event.pull_request && github.event.pull_request.head.repo && github.event.pull_request.head.repo.full_name == '' || github.repository == github.event.pull_request.head.repo.full_name || !github.head_ref
|
if: matrix.store == 'mysql'
|
||||||
env:
|
run: docker pull mlsmaycon/warmed-mysql:8
|
||||||
DOCKER_USER: ${{ secrets.DOCKER_USER }}
|
|
||||||
DOCKER_TOKEN: ${{ secrets.DOCKER_TOKEN }}
|
|
||||||
run: echo "$DOCKER_TOKEN" | sudo docker login --username "$DOCKER_USER" --password-stdin
|
|
||||||
|
|
||||||
- name: Test
|
- name: Test
|
||||||
run: |
|
run: |
|
||||||
|
|||||||
2
.github/workflows/golang-test-windows.yml
vendored
2
.github/workflows/golang-test-windows.yml
vendored
@@ -63,7 +63,7 @@ jobs:
|
|||||||
- run: PsExec64 -s -w ${{ github.workspace }} C:\hostedtoolcache\windows\go\${{ steps.go.outputs.go-version }}\x64\bin\go.exe env -w GOMODCACHE=${{ env.cache }}
|
- run: PsExec64 -s -w ${{ github.workspace }} C:\hostedtoolcache\windows\go\${{ steps.go.outputs.go-version }}\x64\bin\go.exe env -w GOMODCACHE=${{ env.cache }}
|
||||||
- run: PsExec64 -s -w ${{ github.workspace }} C:\hostedtoolcache\windows\go\${{ steps.go.outputs.go-version }}\x64\bin\go.exe env -w GOCACHE=${{ env.modcache }}
|
- run: PsExec64 -s -w ${{ github.workspace }} C:\hostedtoolcache\windows\go\${{ steps.go.outputs.go-version }}\x64\bin\go.exe env -w GOCACHE=${{ env.modcache }}
|
||||||
- run: PsExec64 -s -w ${{ github.workspace }} C:\hostedtoolcache\windows\go\${{ steps.go.outputs.go-version }}\x64\bin\go.exe mod tidy
|
- run: PsExec64 -s -w ${{ github.workspace }} C:\hostedtoolcache\windows\go\${{ steps.go.outputs.go-version }}\x64\bin\go.exe mod tidy
|
||||||
- run: echo "files=$(go list ./... | ForEach-Object { $_ } | Where-Object { $_ -notmatch '/management' } | Where-Object { $_ -notmatch '/relay' } | Where-Object { $_ -notmatch '/signal' } | Where-Object { $_ -notmatch '/proxy' } | Where-Object { $_ -notmatch '/combined' })" >> $env:GITHUB_ENV
|
- run: echo "files=$(go list ./... | ForEach-Object { $_ } | Where-Object { $_ -notmatch '/management' } | Where-Object { $_ -notmatch '/relay' } | Where-Object { $_ -notmatch '/signal' })" >> $env:GITHUB_ENV
|
||||||
|
|
||||||
- name: test
|
- name: test
|
||||||
run: PsExec64 -s -w ${{ github.workspace }} cmd.exe /c "C:\hostedtoolcache\windows\go\${{ steps.go.outputs.go-version }}\x64\bin\go.exe test -tags=devcert -timeout 10m -p 1 ${{ env.files }} > test-out.txt 2>&1"
|
run: PsExec64 -s -w ${{ github.workspace }} cmd.exe /c "C:\hostedtoolcache\windows\go\${{ steps.go.outputs.go-version }}\x64\bin\go.exe test -tags=devcert -timeout 10m -p 1 ${{ env.files }} > test-out.txt 2>&1"
|
||||||
|
|||||||
4
.github/workflows/golangci-lint.yml
vendored
4
.github/workflows/golangci-lint.yml
vendored
@@ -19,8 +19,8 @@ jobs:
|
|||||||
- name: codespell
|
- name: codespell
|
||||||
uses: codespell-project/actions-codespell@v2
|
uses: codespell-project/actions-codespell@v2
|
||||||
with:
|
with:
|
||||||
ignore_words_list: erro,clienta,hastable,iif,groupd,testin,groupe,cros,ans,deriver,te
|
ignore_words_list: erro,clienta,hastable,iif,groupd,testin,groupe,cros,ans
|
||||||
skip: go.mod,go.sum,**/proxy/web/**
|
skip: go.mod,go.sum
|
||||||
golangci:
|
golangci:
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
|
|||||||
51
.github/workflows/pr-title-check.yml
vendored
51
.github/workflows/pr-title-check.yml
vendored
@@ -1,51 +0,0 @@
|
|||||||
name: PR Title Check
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
types: [opened, edited, synchronize, reopened]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
check-title:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Validate PR title prefix
|
|
||||||
uses: actions/github-script@v7
|
|
||||||
with:
|
|
||||||
script: |
|
|
||||||
const title = context.payload.pull_request.title;
|
|
||||||
const allowedTags = [
|
|
||||||
'management',
|
|
||||||
'client',
|
|
||||||
'signal',
|
|
||||||
'proxy',
|
|
||||||
'relay',
|
|
||||||
'misc',
|
|
||||||
'infrastructure',
|
|
||||||
'self-hosted',
|
|
||||||
'doc',
|
|
||||||
];
|
|
||||||
|
|
||||||
const pattern = /^\[([^\]]+)\]\s+.+/;
|
|
||||||
const match = title.match(pattern);
|
|
||||||
|
|
||||||
if (!match) {
|
|
||||||
core.setFailed(
|
|
||||||
`PR title must start with a tag in brackets.\n` +
|
|
||||||
`Example: [client] fix something\n` +
|
|
||||||
`Allowed tags: ${allowedTags.join(', ')}`
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const tags = match[1].split(',').map(t => t.trim().toLowerCase());
|
|
||||||
|
|
||||||
const invalid = tags.filter(t => !allowedTags.includes(t));
|
|
||||||
if (invalid.length > 0) {
|
|
||||||
core.setFailed(
|
|
||||||
`Invalid tag(s): ${invalid.join(', ')}\n` +
|
|
||||||
`Allowed tags: ${allowedTags.join(', ')}`
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(`Valid PR title tags: [${tags.join(', ')}]`);
|
|
||||||
18
.github/workflows/release.yml
vendored
18
.github/workflows/release.yml
vendored
@@ -9,7 +9,7 @@ on:
|
|||||||
pull_request:
|
pull_request:
|
||||||
|
|
||||||
env:
|
env:
|
||||||
SIGN_PIPE_VER: "v0.1.1"
|
SIGN_PIPE_VER: "v0.1.0"
|
||||||
GORELEASER_VER: "v2.3.2"
|
GORELEASER_VER: "v2.3.2"
|
||||||
PRODUCT_NAME: "NetBird"
|
PRODUCT_NAME: "NetBird"
|
||||||
COPYRIGHT: "NetBird GmbH"
|
COPYRIGHT: "NetBird GmbH"
|
||||||
@@ -160,7 +160,7 @@ jobs:
|
|||||||
username: ${{ secrets.DOCKER_USER }}
|
username: ${{ secrets.DOCKER_USER }}
|
||||||
password: ${{ secrets.DOCKER_TOKEN }}
|
password: ${{ secrets.DOCKER_TOKEN }}
|
||||||
- name: Log in to the GitHub container registry
|
- name: Log in to the GitHub container registry
|
||||||
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
|
if: github.event_name != 'pull_request'
|
||||||
uses: docker/login-action@v3
|
uses: docker/login-action@v3
|
||||||
with:
|
with:
|
||||||
registry: ghcr.io
|
registry: ghcr.io
|
||||||
@@ -176,7 +176,6 @@ jobs:
|
|||||||
- name: Generate windows syso arm64
|
- name: Generate windows syso arm64
|
||||||
run: goversioninfo -arm -64 -icon client/ui/assets/netbird.ico -manifest client/manifest.xml -product-name ${{ env.PRODUCT_NAME }} -copyright "${{ env.COPYRIGHT }}" -ver-major ${{ steps.semver_parser.outputs.major }} -ver-minor ${{ steps.semver_parser.outputs.minor }} -ver-patch ${{ steps.semver_parser.outputs.patch }} -ver-build 0 -file-version ${{ steps.semver_parser.outputs.fullversion }}.0 -product-version ${{ steps.semver_parser.outputs.fullversion }}.0 -o client/resources_windows_arm64.syso
|
run: goversioninfo -arm -64 -icon client/ui/assets/netbird.ico -manifest client/manifest.xml -product-name ${{ env.PRODUCT_NAME }} -copyright "${{ env.COPYRIGHT }}" -ver-major ${{ steps.semver_parser.outputs.major }} -ver-minor ${{ steps.semver_parser.outputs.minor }} -ver-patch ${{ steps.semver_parser.outputs.patch }} -ver-build 0 -file-version ${{ steps.semver_parser.outputs.fullversion }}.0 -product-version ${{ steps.semver_parser.outputs.fullversion }}.0 -o client/resources_windows_arm64.syso
|
||||||
- name: Run GoReleaser
|
- name: Run GoReleaser
|
||||||
id: goreleaser
|
|
||||||
uses: goreleaser/goreleaser-action@v4
|
uses: goreleaser/goreleaser-action@v4
|
||||||
with:
|
with:
|
||||||
version: ${{ env.GORELEASER_VER }}
|
version: ${{ env.GORELEASER_VER }}
|
||||||
@@ -186,19 +185,6 @@ jobs:
|
|||||||
HOMEBREW_TAP_GITHUB_TOKEN: ${{ secrets.HOMEBREW_TAP_GITHUB_TOKEN }}
|
HOMEBREW_TAP_GITHUB_TOKEN: ${{ secrets.HOMEBREW_TAP_GITHUB_TOKEN }}
|
||||||
UPLOAD_DEBIAN_SECRET: ${{ secrets.PKG_UPLOAD_SECRET }}
|
UPLOAD_DEBIAN_SECRET: ${{ secrets.PKG_UPLOAD_SECRET }}
|
||||||
UPLOAD_YUM_SECRET: ${{ secrets.PKG_UPLOAD_SECRET }}
|
UPLOAD_YUM_SECRET: ${{ secrets.PKG_UPLOAD_SECRET }}
|
||||||
- name: Tag and push PR images (amd64 only)
|
|
||||||
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
|
|
||||||
run: |
|
|
||||||
PR_TAG="pr-${{ github.event.pull_request.number }}"
|
|
||||||
echo '${{ steps.goreleaser.outputs.artifacts }}' | \
|
|
||||||
jq -r '.[] | select(.type == "Docker Image") | select(.goarch == "amd64") | .name' | \
|
|
||||||
grep '^ghcr.io/' | while read -r SRC; do
|
|
||||||
IMG_NAME="${SRC%%:*}"
|
|
||||||
DST="${IMG_NAME}:${PR_TAG}"
|
|
||||||
echo "Tagging ${SRC} -> ${DST}"
|
|
||||||
docker tag "$SRC" "$DST"
|
|
||||||
docker push "$DST"
|
|
||||||
done
|
|
||||||
- name: upload non tags for debug purposes
|
- name: upload non tags for debug purposes
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,7 +2,6 @@
|
|||||||
.run
|
.run
|
||||||
*.iml
|
*.iml
|
||||||
dist/
|
dist/
|
||||||
!proxy/web/dist/
|
|
||||||
bin/
|
bin/
|
||||||
.env
|
.env
|
||||||
conf.json
|
conf.json
|
||||||
|
|||||||
181
.goreleaser.yaml
181
.goreleaser.yaml
@@ -106,26 +106,6 @@ builds:
|
|||||||
- -s -w -X github.com/netbirdio/netbird/version.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.CommitDate}} -X main.builtBy=goreleaser
|
- -s -w -X github.com/netbirdio/netbird/version.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.CommitDate}} -X main.builtBy=goreleaser
|
||||||
mod_timestamp: "{{ .CommitTimestamp }}"
|
mod_timestamp: "{{ .CommitTimestamp }}"
|
||||||
|
|
||||||
- id: netbird-server
|
|
||||||
dir: combined
|
|
||||||
env:
|
|
||||||
- CGO_ENABLED=1
|
|
||||||
- >-
|
|
||||||
{{- if eq .Runtime.Goos "linux" }}
|
|
||||||
{{- if eq .Arch "arm64"}}CC=aarch64-linux-gnu-gcc{{- end }}
|
|
||||||
{{- if eq .Arch "arm"}}CC=arm-linux-gnueabihf-gcc{{- end }}
|
|
||||||
{{- end }}
|
|
||||||
binary: netbird-server
|
|
||||||
goos:
|
|
||||||
- linux
|
|
||||||
goarch:
|
|
||||||
- amd64
|
|
||||||
- arm64
|
|
||||||
- arm
|
|
||||||
ldflags:
|
|
||||||
- -s -w -X github.com/netbirdio/netbird/version.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.CommitDate}} -X main.builtBy=goreleaser
|
|
||||||
mod_timestamp: "{{ .CommitTimestamp }}"
|
|
||||||
|
|
||||||
- id: netbird-upload
|
- id: netbird-upload
|
||||||
dir: upload-server
|
dir: upload-server
|
||||||
env: [CGO_ENABLED=0]
|
env: [CGO_ENABLED=0]
|
||||||
@@ -140,20 +120,6 @@ builds:
|
|||||||
- -s -w -X github.com/netbirdio/netbird/version.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.CommitDate}} -X main.builtBy=goreleaser
|
- -s -w -X github.com/netbirdio/netbird/version.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.CommitDate}} -X main.builtBy=goreleaser
|
||||||
mod_timestamp: "{{ .CommitTimestamp }}"
|
mod_timestamp: "{{ .CommitTimestamp }}"
|
||||||
|
|
||||||
- id: netbird-proxy
|
|
||||||
dir: proxy/cmd/proxy
|
|
||||||
env: [CGO_ENABLED=0]
|
|
||||||
binary: netbird-proxy
|
|
||||||
goos:
|
|
||||||
- linux
|
|
||||||
goarch:
|
|
||||||
- amd64
|
|
||||||
- arm64
|
|
||||||
- arm
|
|
||||||
ldflags:
|
|
||||||
- -s -w -X main.Version={{.Version}} -X main.Commit={{.Commit}} -X main.BuildDate={{.CommitDate}}
|
|
||||||
mod_timestamp: "{{ .CommitTimestamp }}"
|
|
||||||
|
|
||||||
universal_binaries:
|
universal_binaries:
|
||||||
- id: netbird
|
- id: netbird
|
||||||
|
|
||||||
@@ -554,104 +520,6 @@ dockers:
|
|||||||
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
|
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
|
||||||
- "--label=org.opencontainers.image.source=https://github.com/netbirdio/{{.ProjectName}}"
|
- "--label=org.opencontainers.image.source=https://github.com/netbirdio/{{.ProjectName}}"
|
||||||
- "--label=maintainer=dev@netbird.io"
|
- "--label=maintainer=dev@netbird.io"
|
||||||
- image_templates:
|
|
||||||
- netbirdio/netbird-server:{{ .Version }}-amd64
|
|
||||||
- ghcr.io/netbirdio/netbird-server:{{ .Version }}-amd64
|
|
||||||
ids:
|
|
||||||
- netbird-server
|
|
||||||
goarch: amd64
|
|
||||||
use: buildx
|
|
||||||
dockerfile: combined/Dockerfile
|
|
||||||
build_flag_templates:
|
|
||||||
- "--platform=linux/amd64"
|
|
||||||
- "--label=org.opencontainers.image.created={{.Date}}"
|
|
||||||
- "--label=org.opencontainers.image.title={{.ProjectName}}"
|
|
||||||
- "--label=org.opencontainers.image.version={{.Version}}"
|
|
||||||
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
|
|
||||||
- "--label=org.opencontainers.image.source=https://github.com/netbirdio/{{.ProjectName}}"
|
|
||||||
- "--label=maintainer=dev@netbird.io"
|
|
||||||
- image_templates:
|
|
||||||
- netbirdio/netbird-server:{{ .Version }}-arm64v8
|
|
||||||
- ghcr.io/netbirdio/netbird-server:{{ .Version }}-arm64v8
|
|
||||||
ids:
|
|
||||||
- netbird-server
|
|
||||||
goarch: arm64
|
|
||||||
use: buildx
|
|
||||||
dockerfile: combined/Dockerfile
|
|
||||||
build_flag_templates:
|
|
||||||
- "--platform=linux/arm64"
|
|
||||||
- "--label=org.opencontainers.image.created={{.Date}}"
|
|
||||||
- "--label=org.opencontainers.image.title={{.ProjectName}}"
|
|
||||||
- "--label=org.opencontainers.image.version={{.Version}}"
|
|
||||||
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
|
|
||||||
- "--label=org.opencontainers.image.source=https://github.com/netbirdio/{{.ProjectName}}"
|
|
||||||
- "--label=maintainer=dev@netbird.io"
|
|
||||||
- image_templates:
|
|
||||||
- netbirdio/netbird-server:{{ .Version }}-arm
|
|
||||||
- ghcr.io/netbirdio/netbird-server:{{ .Version }}-arm
|
|
||||||
ids:
|
|
||||||
- netbird-server
|
|
||||||
goarch: arm
|
|
||||||
goarm: 6
|
|
||||||
use: buildx
|
|
||||||
dockerfile: combined/Dockerfile
|
|
||||||
build_flag_templates:
|
|
||||||
- "--platform=linux/arm"
|
|
||||||
- "--label=org.opencontainers.image.created={{.Date}}"
|
|
||||||
- "--label=org.opencontainers.image.title={{.ProjectName}}"
|
|
||||||
- "--label=org.opencontainers.image.version={{.Version}}"
|
|
||||||
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
|
|
||||||
- "--label=org.opencontainers.image.source=https://github.com/netbirdio/{{.ProjectName}}"
|
|
||||||
- "--label=maintainer=dev@netbird.io"
|
|
||||||
- image_templates:
|
|
||||||
- netbirdio/reverse-proxy:{{ .Version }}-amd64
|
|
||||||
- ghcr.io/netbirdio/reverse-proxy:{{ .Version }}-amd64
|
|
||||||
ids:
|
|
||||||
- netbird-proxy
|
|
||||||
goarch: amd64
|
|
||||||
use: buildx
|
|
||||||
dockerfile: proxy/Dockerfile
|
|
||||||
build_flag_templates:
|
|
||||||
- "--platform=linux/amd64"
|
|
||||||
- "--label=org.opencontainers.image.created={{.Date}}"
|
|
||||||
- "--label=org.opencontainers.image.title={{.ProjectName}}"
|
|
||||||
- "--label=org.opencontainers.image.version={{.Version}}"
|
|
||||||
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
|
|
||||||
- "--label=org.opencontainers.image.source=https://github.com/netbirdio/{{.ProjectName}}"
|
|
||||||
- "--label=maintainer=dev@netbird.io"
|
|
||||||
- image_templates:
|
|
||||||
- netbirdio/reverse-proxy:{{ .Version }}-arm64v8
|
|
||||||
- ghcr.io/netbirdio/reverse-proxy:{{ .Version }}-arm64v8
|
|
||||||
ids:
|
|
||||||
- netbird-proxy
|
|
||||||
goarch: arm64
|
|
||||||
use: buildx
|
|
||||||
dockerfile: proxy/Dockerfile
|
|
||||||
build_flag_templates:
|
|
||||||
- "--platform=linux/arm64"
|
|
||||||
- "--label=org.opencontainers.image.created={{.Date}}"
|
|
||||||
- "--label=org.opencontainers.image.title={{.ProjectName}}"
|
|
||||||
- "--label=org.opencontainers.image.version={{.Version}}"
|
|
||||||
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
|
|
||||||
- "--label=org.opencontainers.image.source=https://github.com/netbirdio/{{.ProjectName}}"
|
|
||||||
- "--label=maintainer=dev@netbird.io"
|
|
||||||
- image_templates:
|
|
||||||
- netbirdio/reverse-proxy:{{ .Version }}-arm
|
|
||||||
- ghcr.io/netbirdio/reverse-proxy:{{ .Version }}-arm
|
|
||||||
ids:
|
|
||||||
- netbird-proxy
|
|
||||||
goarch: arm
|
|
||||||
goarm: 6
|
|
||||||
use: buildx
|
|
||||||
dockerfile: proxy/Dockerfile
|
|
||||||
build_flag_templates:
|
|
||||||
- "--platform=linux/arm"
|
|
||||||
- "--label=org.opencontainers.image.created={{.Date}}"
|
|
||||||
- "--label=org.opencontainers.image.title={{.ProjectName}}"
|
|
||||||
- "--label=org.opencontainers.image.version={{.Version}}"
|
|
||||||
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
|
|
||||||
- "--label=org.opencontainers.image.source=https://github.com/netbirdio/{{.ProjectName}}"
|
|
||||||
- "--label=maintainer=dev@netbird.io"
|
|
||||||
docker_manifests:
|
docker_manifests:
|
||||||
- name_template: netbirdio/netbird:{{ .Version }}
|
- name_template: netbirdio/netbird:{{ .Version }}
|
||||||
image_templates:
|
image_templates:
|
||||||
@@ -730,18 +598,6 @@ docker_manifests:
|
|||||||
- netbirdio/upload:{{ .Version }}-arm
|
- netbirdio/upload:{{ .Version }}-arm
|
||||||
- netbirdio/upload:{{ .Version }}-amd64
|
- netbirdio/upload:{{ .Version }}-amd64
|
||||||
|
|
||||||
- name_template: netbirdio/netbird-server:{{ .Version }}
|
|
||||||
image_templates:
|
|
||||||
- netbirdio/netbird-server:{{ .Version }}-arm64v8
|
|
||||||
- netbirdio/netbird-server:{{ .Version }}-arm
|
|
||||||
- netbirdio/netbird-server:{{ .Version }}-amd64
|
|
||||||
|
|
||||||
- name_template: netbirdio/netbird-server:latest
|
|
||||||
image_templates:
|
|
||||||
- netbirdio/netbird-server:{{ .Version }}-arm64v8
|
|
||||||
- netbirdio/netbird-server:{{ .Version }}-arm
|
|
||||||
- netbirdio/netbird-server:{{ .Version }}-amd64
|
|
||||||
|
|
||||||
- name_template: ghcr.io/netbirdio/netbird:{{ .Version }}
|
- name_template: ghcr.io/netbirdio/netbird:{{ .Version }}
|
||||||
image_templates:
|
image_templates:
|
||||||
- ghcr.io/netbirdio/netbird:{{ .Version }}-arm64v8
|
- ghcr.io/netbirdio/netbird:{{ .Version }}-arm64v8
|
||||||
@@ -819,43 +675,6 @@ docker_manifests:
|
|||||||
- ghcr.io/netbirdio/upload:{{ .Version }}-arm64v8
|
- ghcr.io/netbirdio/upload:{{ .Version }}-arm64v8
|
||||||
- ghcr.io/netbirdio/upload:{{ .Version }}-arm
|
- ghcr.io/netbirdio/upload:{{ .Version }}-arm
|
||||||
- ghcr.io/netbirdio/upload:{{ .Version }}-amd64
|
- ghcr.io/netbirdio/upload:{{ .Version }}-amd64
|
||||||
|
|
||||||
- name_template: ghcr.io/netbirdio/netbird-server:{{ .Version }}
|
|
||||||
image_templates:
|
|
||||||
- ghcr.io/netbirdio/netbird-server:{{ .Version }}-arm64v8
|
|
||||||
- ghcr.io/netbirdio/netbird-server:{{ .Version }}-arm
|
|
||||||
- ghcr.io/netbirdio/netbird-server:{{ .Version }}-amd64
|
|
||||||
|
|
||||||
- name_template: ghcr.io/netbirdio/netbird-server:latest
|
|
||||||
image_templates:
|
|
||||||
- ghcr.io/netbirdio/netbird-server:{{ .Version }}-arm64v8
|
|
||||||
- ghcr.io/netbirdio/netbird-server:{{ .Version }}-arm
|
|
||||||
- ghcr.io/netbirdio/netbird-server:{{ .Version }}-amd64
|
|
||||||
|
|
||||||
- name_template: netbirdio/reverse-proxy:{{ .Version }}
|
|
||||||
image_templates:
|
|
||||||
- netbirdio/reverse-proxy:{{ .Version }}-arm64v8
|
|
||||||
- netbirdio/reverse-proxy:{{ .Version }}-arm
|
|
||||||
- netbirdio/reverse-proxy:{{ .Version }}-amd64
|
|
||||||
|
|
||||||
- name_template: netbirdio/reverse-proxy:latest
|
|
||||||
image_templates:
|
|
||||||
- netbirdio/reverse-proxy:{{ .Version }}-arm64v8
|
|
||||||
- netbirdio/reverse-proxy:{{ .Version }}-arm
|
|
||||||
- netbirdio/reverse-proxy:{{ .Version }}-amd64
|
|
||||||
|
|
||||||
- name_template: ghcr.io/netbirdio/reverse-proxy:{{ .Version }}
|
|
||||||
image_templates:
|
|
||||||
- ghcr.io/netbirdio/reverse-proxy:{{ .Version }}-arm64v8
|
|
||||||
- ghcr.io/netbirdio/reverse-proxy:{{ .Version }}-arm
|
|
||||||
- ghcr.io/netbirdio/reverse-proxy:{{ .Version }}-amd64
|
|
||||||
|
|
||||||
- name_template: ghcr.io/netbirdio/reverse-proxy:latest
|
|
||||||
image_templates:
|
|
||||||
- ghcr.io/netbirdio/reverse-proxy:{{ .Version }}-arm64v8
|
|
||||||
- ghcr.io/netbirdio/reverse-proxy:{{ .Version }}-arm
|
|
||||||
- ghcr.io/netbirdio/reverse-proxy:{{ .Version }}-amd64
|
|
||||||
|
|
||||||
brews:
|
brews:
|
||||||
- ids:
|
- ids:
|
||||||
- default
|
- default
|
||||||
|
|||||||
2
LICENSE
2
LICENSE
@@ -1,4 +1,4 @@
|
|||||||
This BSD‑3‑Clause license applies to all parts of the repository except for the directories management/, signal/, relay/ and combined/.
|
This BSD‑3‑Clause license applies to all parts of the repository except for the directories management/, signal/ and relay/.
|
||||||
Those directories are licensed under the GNU Affero General Public License version 3.0 (AGPLv3). See the respective LICENSE files inside each directory.
|
Those directories are licensed under the GNU Affero General Public License version 3.0 (AGPLv3). See the respective LICENSE files inside each directory.
|
||||||
|
|
||||||
BSD 3-Clause License
|
BSD 3-Clause License
|
||||||
|
|||||||
@@ -60,8 +60,8 @@
|
|||||||
|
|
||||||
https://github.com/user-attachments/assets/10cec749-bb56-4ab3-97af-4e38850108d2
|
https://github.com/user-attachments/assets/10cec749-bb56-4ab3-97af-4e38850108d2
|
||||||
|
|
||||||
### Self-Host NetBird (Video)
|
### NetBird on Lawrence Systems (Video)
|
||||||
[](https://youtu.be/bZAgpT6nzaQ)
|
[](https://www.youtube.com/watch?v=Kwrff6h0rEw)
|
||||||
|
|
||||||
### Key features
|
### Key features
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
# sudo podman build -t localhost/netbird:latest -f client/Dockerfile --ignorefile .dockerignore-client .
|
# sudo podman build -t localhost/netbird:latest -f client/Dockerfile --ignorefile .dockerignore-client .
|
||||||
# sudo podman run --rm -it --cap-add={BPF,NET_ADMIN,NET_RAW} localhost/netbird:latest
|
# sudo podman run --rm -it --cap-add={BPF,NET_ADMIN,NET_RAW} localhost/netbird:latest
|
||||||
|
|
||||||
FROM alpine:3.23.3
|
FROM alpine:3.23.2
|
||||||
# iproute2: busybox doesn't display ip rules properly
|
# iproute2: busybox doesn't display ip rules properly
|
||||||
RUN apk add --no-cache \
|
RUN apk add --no-cache \
|
||||||
bash \
|
bash \
|
||||||
|
|||||||
@@ -1,19 +1,10 @@
|
|||||||
package android
|
package android
|
||||||
|
|
||||||
import (
|
import "github.com/netbirdio/netbird/client/internal/peer"
|
||||||
"github.com/netbirdio/netbird/client/internal/lazyconn"
|
|
||||||
"github.com/netbirdio/netbird/client/internal/peer"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// EnvKeyNBForceRelay Exported for Android java client to force relay connections
|
// EnvKeyNBForceRelay Exported for Android java client
|
||||||
EnvKeyNBForceRelay = peer.EnvKeyNBForceRelay
|
EnvKeyNBForceRelay = peer.EnvKeyNBForceRelay
|
||||||
|
|
||||||
// EnvKeyNBLazyConn Exported for Android java client to configure lazy connection
|
|
||||||
EnvKeyNBLazyConn = lazyconn.EnvEnableLazyConn
|
|
||||||
|
|
||||||
// EnvKeyNBInactivityThreshold Exported for Android java client to configure connection inactivity threshold
|
|
||||||
EnvKeyNBInactivityThreshold = lazyconn.EnvInactivityThreshold
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// EnvList wraps a Go map for export to Java
|
// EnvList wraps a Go map for export to Java
|
||||||
|
|||||||
@@ -1,194 +0,0 @@
|
|||||||
package cmd
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"os/signal"
|
|
||||||
"regexp"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"syscall"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
|
|
||||||
"github.com/netbirdio/netbird/client/proto"
|
|
||||||
"github.com/netbirdio/netbird/util"
|
|
||||||
)
|
|
||||||
|
|
||||||
var pinRegexp = regexp.MustCompile(`^\d{6}$`)
|
|
||||||
|
|
||||||
var (
|
|
||||||
exposePin string
|
|
||||||
exposePassword string
|
|
||||||
exposeUserGroups []string
|
|
||||||
exposeDomain string
|
|
||||||
exposeNamePrefix string
|
|
||||||
exposeProtocol string
|
|
||||||
)
|
|
||||||
|
|
||||||
var exposeCmd = &cobra.Command{
|
|
||||||
Use: "expose <port>",
|
|
||||||
Short: "Expose a local port via the NetBird reverse proxy",
|
|
||||||
Args: cobra.ExactArgs(1),
|
|
||||||
Example: "netbird expose --with-password safe-pass 8080",
|
|
||||||
RunE: exposeFn,
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
exposeCmd.Flags().StringVar(&exposePin, "with-pin", "", "Protect the exposed service with a 6-digit PIN (e.g. --with-pin 123456)")
|
|
||||||
exposeCmd.Flags().StringVar(&exposePassword, "with-password", "", "Protect the exposed service with a password (e.g. --with-password my-secret)")
|
|
||||||
exposeCmd.Flags().StringSliceVar(&exposeUserGroups, "with-user-groups", nil, "Restrict access to specific user groups with SSO (e.g. --with-user-groups devops,Backend)")
|
|
||||||
exposeCmd.Flags().StringVar(&exposeDomain, "with-custom-domain", "", "Custom domain for the exposed service, must be configured to your account (e.g. --with-custom-domain myapp.example.com)")
|
|
||||||
exposeCmd.Flags().StringVar(&exposeNamePrefix, "with-name-prefix", "", "Prefix for the generated service name (e.g. --with-name-prefix my-app)")
|
|
||||||
exposeCmd.Flags().StringVar(&exposeProtocol, "protocol", "http", "Protocol to use, http/https is supported (e.g. --protocol http)")
|
|
||||||
}
|
|
||||||
|
|
||||||
func validateExposeFlags(cmd *cobra.Command, portStr string) (uint64, error) {
|
|
||||||
port, err := strconv.ParseUint(portStr, 10, 32)
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("invalid port number: %s", portStr)
|
|
||||||
}
|
|
||||||
if port == 0 || port > 65535 {
|
|
||||||
return 0, fmt.Errorf("invalid port number: must be between 1 and 65535")
|
|
||||||
}
|
|
||||||
|
|
||||||
if !isProtocolValid(exposeProtocol) {
|
|
||||||
return 0, fmt.Errorf("unsupported protocol %q: only 'http' or 'https' are supported", exposeProtocol)
|
|
||||||
}
|
|
||||||
|
|
||||||
if exposePin != "" && !pinRegexp.MatchString(exposePin) {
|
|
||||||
return 0, fmt.Errorf("invalid pin: must be exactly 6 digits")
|
|
||||||
}
|
|
||||||
|
|
||||||
if cmd.Flags().Changed("with-password") && exposePassword == "" {
|
|
||||||
return 0, fmt.Errorf("password cannot be empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
if cmd.Flags().Changed("with-user-groups") && len(exposeUserGroups) == 0 {
|
|
||||||
return 0, fmt.Errorf("user groups cannot be empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
return port, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func isProtocolValid(exposeProtocol string) bool {
|
|
||||||
return strings.ToLower(exposeProtocol) == "http" || strings.ToLower(exposeProtocol) == "https"
|
|
||||||
}
|
|
||||||
|
|
||||||
func exposeFn(cmd *cobra.Command, args []string) error {
|
|
||||||
SetFlagsFromEnvVars(rootCmd)
|
|
||||||
|
|
||||||
if err := util.InitLog(logLevel, util.LogConsole); err != nil {
|
|
||||||
log.Errorf("failed initializing log %v", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd.Root().SilenceUsage = false
|
|
||||||
|
|
||||||
port, err := validateExposeFlags(cmd, args[0])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd.Root().SilenceUsage = true
|
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(cmd.Context())
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
sigCh := make(chan os.Signal, 1)
|
|
||||||
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
|
|
||||||
go func() {
|
|
||||||
<-sigCh
|
|
||||||
cancel()
|
|
||||||
}()
|
|
||||||
|
|
||||||
conn, err := DialClientGRPCServer(ctx, daemonAddr)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("connect to daemon: %w", err)
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
if err := conn.Close(); err != nil {
|
|
||||||
log.Debugf("failed to close daemon connection: %v", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
client := proto.NewDaemonServiceClient(conn)
|
|
||||||
|
|
||||||
protocol, err := toExposeProtocol(exposeProtocol)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
stream, err := client.ExposeService(ctx, &proto.ExposeServiceRequest{
|
|
||||||
Port: uint32(port),
|
|
||||||
Protocol: protocol,
|
|
||||||
Pin: exposePin,
|
|
||||||
Password: exposePassword,
|
|
||||||
UserGroups: exposeUserGroups,
|
|
||||||
Domain: exposeDomain,
|
|
||||||
NamePrefix: exposeNamePrefix,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("expose service: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := handleExposeReady(cmd, stream, port); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return waitForExposeEvents(cmd, ctx, stream)
|
|
||||||
}
|
|
||||||
|
|
||||||
func toExposeProtocol(exposeProtocol string) (proto.ExposeProtocol, error) {
|
|
||||||
switch strings.ToLower(exposeProtocol) {
|
|
||||||
case "http":
|
|
||||||
return proto.ExposeProtocol_EXPOSE_HTTP, nil
|
|
||||||
case "https":
|
|
||||||
return proto.ExposeProtocol_EXPOSE_HTTPS, nil
|
|
||||||
default:
|
|
||||||
return 0, fmt.Errorf("unsupported protocol %q: only 'http' or 'https' are supported", exposeProtocol)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleExposeReady(cmd *cobra.Command, stream proto.DaemonService_ExposeServiceClient, port uint64) error {
|
|
||||||
event, err := stream.Recv()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("receive expose event: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch e := event.Event.(type) {
|
|
||||||
case *proto.ExposeServiceEvent_Ready:
|
|
||||||
cmd.Println("Service exposed successfully!")
|
|
||||||
cmd.Printf(" Name: %s\n", e.Ready.ServiceName)
|
|
||||||
cmd.Printf(" URL: %s\n", e.Ready.ServiceUrl)
|
|
||||||
cmd.Printf(" Domain: %s\n", e.Ready.Domain)
|
|
||||||
cmd.Printf(" Protocol: %s\n", exposeProtocol)
|
|
||||||
cmd.Printf(" Port: %d\n", port)
|
|
||||||
cmd.Println()
|
|
||||||
cmd.Println("Press Ctrl+C to stop exposing.")
|
|
||||||
return nil
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("unexpected expose event: %T", event.Event)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func waitForExposeEvents(cmd *cobra.Command, ctx context.Context, stream proto.DaemonService_ExposeServiceClient) error {
|
|
||||||
for {
|
|
||||||
_, err := stream.Recv()
|
|
||||||
if err != nil {
|
|
||||||
if ctx.Err() != nil {
|
|
||||||
cmd.Println("\nService stopped.")
|
|
||||||
//nolint:nilerr
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if errors.Is(err, io.EOF) {
|
|
||||||
return fmt.Errorf("connection to daemon closed unexpectedly")
|
|
||||||
}
|
|
||||||
return fmt.Errorf("stream error: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -282,9 +282,13 @@ func foregroundLogin(ctx context.Context, cmd *cobra.Command, config *profileman
|
|||||||
}
|
}
|
||||||
defer authClient.Close()
|
defer authClient.Close()
|
||||||
|
|
||||||
needsLogin, err := authClient.IsLoginRequired(ctx)
|
needsLogin := false
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("check login required: %v", err)
|
err, isAuthError := authClient.Login(ctx, "", "")
|
||||||
|
if isAuthError {
|
||||||
|
needsLogin = true
|
||||||
|
} else if err != nil {
|
||||||
|
return fmt.Errorf("login check failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
jwtToken := ""
|
jwtToken := ""
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ import (
|
|||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/credentials/insecure"
|
"google.golang.org/grpc/credentials/insecure"
|
||||||
|
|
||||||
daddr "github.com/netbirdio/netbird/client/internal/daemonaddr"
|
|
||||||
"github.com/netbirdio/netbird/client/internal/profilemanager"
|
"github.com/netbirdio/netbird/client/internal/profilemanager"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -81,15 +80,6 @@ var (
|
|||||||
Short: "",
|
Short: "",
|
||||||
Long: "",
|
Long: "",
|
||||||
SilenceUsage: true,
|
SilenceUsage: true,
|
||||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
||||||
SetFlagsFromEnvVars(cmd.Root())
|
|
||||||
|
|
||||||
// Don't resolve for service commands — they create the socket, not connect to it.
|
|
||||||
if !isServiceCmd(cmd) {
|
|
||||||
daemonAddr = daddr.ResolveUnixDaemonAddr(daemonAddr)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -154,7 +144,6 @@ func init() {
|
|||||||
rootCmd.AddCommand(forwardingRulesCmd)
|
rootCmd.AddCommand(forwardingRulesCmd)
|
||||||
rootCmd.AddCommand(debugCmd)
|
rootCmd.AddCommand(debugCmd)
|
||||||
rootCmd.AddCommand(profileCmd)
|
rootCmd.AddCommand(profileCmd)
|
||||||
rootCmd.AddCommand(exposeCmd)
|
|
||||||
|
|
||||||
networksCMD.AddCommand(routesListCmd)
|
networksCMD.AddCommand(routesListCmd)
|
||||||
networksCMD.AddCommand(routesSelectCmd, routesDeselectCmd)
|
networksCMD.AddCommand(routesSelectCmd, routesDeselectCmd)
|
||||||
@@ -396,6 +385,7 @@ func migrateToNetbird(oldPath, newPath string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getClient(cmd *cobra.Command) (*grpc.ClientConn, error) {
|
func getClient(cmd *cobra.Command) (*grpc.ClientConn, error) {
|
||||||
|
SetFlagsFromEnvVars(rootCmd)
|
||||||
cmd.SetOut(cmd.OutOrStdout())
|
cmd.SetOut(cmd.OutOrStdout())
|
||||||
|
|
||||||
conn, err := DialClientGRPCServer(cmd.Context(), daemonAddr)
|
conn, err := DialClientGRPCServer(cmd.Context(), daemonAddr)
|
||||||
@@ -408,13 +398,3 @@ func getClient(cmd *cobra.Command) (*grpc.ClientConn, error) {
|
|||||||
|
|
||||||
return conn, nil
|
return conn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// isServiceCmd returns true if cmd is the "service" command or a child of it.
|
|
||||||
func isServiceCmd(cmd *cobra.Command) bool {
|
|
||||||
for c := cmd; c != nil; c = c.Parent() {
|
|
||||||
if c.Name() == "service" {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -31,14 +31,6 @@ var (
|
|||||||
ErrConfigNotInitialized = errors.New("config not initialized")
|
ErrConfigNotInitialized = errors.New("config not initialized")
|
||||||
)
|
)
|
||||||
|
|
||||||
// PeerConnStatus is a peer's connection status.
|
|
||||||
type PeerConnStatus = peer.ConnStatus
|
|
||||||
|
|
||||||
const (
|
|
||||||
// PeerStatusConnected indicates the peer is in connected state.
|
|
||||||
PeerStatusConnected = peer.StatusConnected
|
|
||||||
)
|
|
||||||
|
|
||||||
// Client manages a netbird embedded client instance.
|
// Client manages a netbird embedded client instance.
|
||||||
type Client struct {
|
type Client struct {
|
||||||
deviceName string
|
deviceName string
|
||||||
@@ -79,8 +71,6 @@ type Options struct {
|
|||||||
DisableClientRoutes bool
|
DisableClientRoutes bool
|
||||||
// BlockInbound blocks all inbound connections from peers
|
// BlockInbound blocks all inbound connections from peers
|
||||||
BlockInbound bool
|
BlockInbound bool
|
||||||
// WireguardPort is the port for the WireGuard interface. Use 0 for a random port.
|
|
||||||
WireguardPort *int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// validateCredentials checks that exactly one credential type is provided
|
// validateCredentials checks that exactly one credential type is provided
|
||||||
@@ -150,7 +140,6 @@ func New(opts Options) (*Client, error) {
|
|||||||
DisableServerRoutes: &t,
|
DisableServerRoutes: &t,
|
||||||
DisableClientRoutes: &opts.DisableClientRoutes,
|
DisableClientRoutes: &opts.DisableClientRoutes,
|
||||||
BlockInbound: &opts.BlockInbound,
|
BlockInbound: &opts.BlockInbound,
|
||||||
WireguardPort: opts.WireguardPort,
|
|
||||||
}
|
}
|
||||||
if opts.ConfigPath != "" {
|
if opts.ConfigPath != "" {
|
||||||
config, err = profilemanager.UpdateOrCreateConfig(input)
|
config, err = profilemanager.UpdateOrCreateConfig(input)
|
||||||
@@ -170,7 +159,6 @@ func New(opts Options) (*Client, error) {
|
|||||||
setupKey: opts.SetupKey,
|
setupKey: opts.SetupKey,
|
||||||
jwtToken: opts.JWTToken,
|
jwtToken: opts.JWTToken,
|
||||||
config: config,
|
config: config,
|
||||||
recorder: peer.NewRecorder(config.ManagementURL.String()),
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,7 +180,6 @@ func (c *Client) Start(startCtx context.Context) error {
|
|||||||
|
|
||||||
// nolint:staticcheck
|
// nolint:staticcheck
|
||||||
ctx = context.WithValue(ctx, system.DeviceNameCtxKey, c.deviceName)
|
ctx = context.WithValue(ctx, system.DeviceNameCtxKey, c.deviceName)
|
||||||
|
|
||||||
authClient, err := auth.NewAuth(ctx, c.config.PrivateKey, c.config.ManagementURL, c.config)
|
authClient, err := auth.NewAuth(ctx, c.config.PrivateKey, c.config.ManagementURL, c.config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("create auth client: %w", err)
|
return fmt.Errorf("create auth client: %w", err)
|
||||||
@@ -202,7 +189,10 @@ func (c *Client) Start(startCtx context.Context) error {
|
|||||||
if err, _ := authClient.Login(ctx, c.setupKey, c.jwtToken); err != nil {
|
if err, _ := authClient.Login(ctx, c.setupKey, c.jwtToken); err != nil {
|
||||||
return fmt.Errorf("login: %w", err)
|
return fmt.Errorf("login: %w", err)
|
||||||
}
|
}
|
||||||
client := internal.NewConnectClient(ctx, c.config, c.recorder, false)
|
|
||||||
|
recorder := peer.NewRecorder(c.config.ManagementURL.String())
|
||||||
|
c.recorder = recorder
|
||||||
|
client := internal.NewConnectClient(ctx, c.config, recorder, false)
|
||||||
client.SetSyncResponsePersistence(true)
|
client.SetSyncResponsePersistence(true)
|
||||||
|
|
||||||
// either startup error (permanent backoff err) or nil err (successful engine up)
|
// either startup error (permanent backoff err) or nil err (successful engine up)
|
||||||
@@ -355,9 +345,14 @@ func (c *Client) NewHTTPClient() *http.Client {
|
|||||||
// Status returns the current status of the client.
|
// Status returns the current status of the client.
|
||||||
func (c *Client) Status() (peer.FullStatus, error) {
|
func (c *Client) Status() (peer.FullStatus, error) {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
|
recorder := c.recorder
|
||||||
connect := c.connect
|
connect := c.connect
|
||||||
c.mu.Unlock()
|
c.mu.Unlock()
|
||||||
|
|
||||||
|
if recorder == nil {
|
||||||
|
return peer.FullStatus{}, errors.New("client not started")
|
||||||
|
}
|
||||||
|
|
||||||
if connect != nil {
|
if connect != nil {
|
||||||
engine := connect.Engine()
|
engine := connect.Engine()
|
||||||
if engine != nil {
|
if engine != nil {
|
||||||
@@ -365,7 +360,7 @@ func (c *Client) Status() (peer.FullStatus, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.recorder.GetFullStatus(), nil
|
return recorder.GetFullStatus(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLatestSyncResponse returns the latest sync response from the management server.
|
// GetLatestSyncResponse returns the latest sync response from the management server.
|
||||||
|
|||||||
@@ -483,12 +483,7 @@ func (r *router) DeleteRouteRule(rule firewall.Rule) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if nftRule.Handle == 0 {
|
if nftRule.Handle == 0 {
|
||||||
log.Warnf("route rule %s has no handle, removing stale entry", ruleKey)
|
return fmt.Errorf("route rule %s has no handle", ruleKey)
|
||||||
if err := r.decrementSetCounter(nftRule); err != nil {
|
|
||||||
log.Warnf("decrement set counter for stale rule %s: %v", ruleKey, err)
|
|
||||||
}
|
|
||||||
delete(r.rules, ruleKey)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := r.deleteNftRule(nftRule, ruleKey); err != nil {
|
if err := r.deleteNftRule(nftRule, ruleKey); err != nil {
|
||||||
@@ -665,32 +660,13 @@ func (r *router) AddNatRule(pair firewall.RouterPair) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := r.conn.Flush(); err != nil {
|
if err := r.conn.Flush(); err != nil {
|
||||||
r.rollbackRules(pair)
|
// TODO: rollback ipset counter
|
||||||
return fmt.Errorf("insert rules for %s: %w", pair.Destination, err)
|
return fmt.Errorf("insert rules for %s: %v", pair.Destination, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// rollbackRules cleans up unflushed rules and their set counters after a flush failure.
|
|
||||||
func (r *router) rollbackRules(pair firewall.RouterPair) {
|
|
||||||
keys := []string{
|
|
||||||
firewall.GenKey(firewall.ForwardingFormat, pair),
|
|
||||||
firewall.GenKey(firewall.PreroutingFormat, pair),
|
|
||||||
firewall.GenKey(firewall.PreroutingFormat, firewall.GetInversePair(pair)),
|
|
||||||
}
|
|
||||||
for _, key := range keys {
|
|
||||||
rule, ok := r.rules[key]
|
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err := r.decrementSetCounter(rule); err != nil {
|
|
||||||
log.Warnf("rollback set counter for %s: %v", key, err)
|
|
||||||
}
|
|
||||||
delete(r.rules, key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// addNatRule inserts a nftables rule to the conn client flush queue
|
// addNatRule inserts a nftables rule to the conn client flush queue
|
||||||
func (r *router) addNatRule(pair firewall.RouterPair) error {
|
func (r *router) addNatRule(pair firewall.RouterPair) error {
|
||||||
sourceExp, err := r.applyNetwork(pair.Source, nil, true)
|
sourceExp, err := r.applyNetwork(pair.Source, nil, true)
|
||||||
@@ -952,22 +928,9 @@ func (r *router) addLegacyRouteRule(pair firewall.RouterPair) error {
|
|||||||
func (r *router) removeLegacyRouteRule(pair firewall.RouterPair) error {
|
func (r *router) removeLegacyRouteRule(pair firewall.RouterPair) error {
|
||||||
ruleKey := firewall.GenKey(firewall.ForwardingFormat, pair)
|
ruleKey := firewall.GenKey(firewall.ForwardingFormat, pair)
|
||||||
|
|
||||||
rule, exists := r.rules[ruleKey]
|
if rule, exists := r.rules[ruleKey]; exists {
|
||||||
if !exists {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if rule.Handle == 0 {
|
|
||||||
log.Warnf("legacy forwarding rule %s has no handle, removing stale entry", ruleKey)
|
|
||||||
if err := r.decrementSetCounter(rule); err != nil {
|
|
||||||
log.Warnf("decrement set counter for stale rule %s: %v", ruleKey, err)
|
|
||||||
}
|
|
||||||
delete(r.rules, ruleKey)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := r.conn.DelRule(rule); err != nil {
|
if err := r.conn.DelRule(rule); err != nil {
|
||||||
return fmt.Errorf("remove legacy forwarding rule %s -> %s: %w", pair.Source, pair.Destination, err)
|
return fmt.Errorf("remove legacy forwarding rule %s -> %s: %v", pair.Source, pair.Destination, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("removed legacy forwarding rule %s -> %s", pair.Source, pair.Destination)
|
log.Debugf("removed legacy forwarding rule %s -> %s", pair.Source, pair.Destination)
|
||||||
@@ -977,6 +940,7 @@ func (r *router) removeLegacyRouteRule(pair firewall.RouterPair) error {
|
|||||||
if err := r.decrementSetCounter(rule); err != nil {
|
if err := r.decrementSetCounter(rule); err != nil {
|
||||||
return fmt.Errorf("decrement set counter: %w", err)
|
return fmt.Errorf("decrement set counter: %w", err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -1365,51 +1329,34 @@ func (r *router) RemoveNatRule(pair firewall.RouterPair) error {
|
|||||||
return fmt.Errorf(refreshRulesMapError, err)
|
return fmt.Errorf(refreshRulesMapError, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var merr *multierror.Error
|
|
||||||
|
|
||||||
if pair.Masquerade {
|
if pair.Masquerade {
|
||||||
if err := r.removeNatRule(pair); err != nil {
|
if err := r.removeNatRule(pair); err != nil {
|
||||||
merr = multierror.Append(merr, fmt.Errorf("remove prerouting rule: %w", err))
|
return fmt.Errorf("remove prerouting rule: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := r.removeNatRule(firewall.GetInversePair(pair)); err != nil {
|
if err := r.removeNatRule(firewall.GetInversePair(pair)); err != nil {
|
||||||
merr = multierror.Append(merr, fmt.Errorf("remove inverse prerouting rule: %w", err))
|
return fmt.Errorf("remove inverse prerouting rule: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := r.removeLegacyRouteRule(pair); err != nil {
|
if err := r.removeLegacyRouteRule(pair); err != nil {
|
||||||
merr = multierror.Append(merr, fmt.Errorf("remove legacy routing rule: %w", err))
|
return fmt.Errorf("remove legacy routing rule: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set counters are decremented in the sub-methods above before flush. If flush fails,
|
|
||||||
// counters will be off until the next successful removal or refresh cycle.
|
|
||||||
if err := r.conn.Flush(); err != nil {
|
if err := r.conn.Flush(); err != nil {
|
||||||
merr = multierror.Append(merr, fmt.Errorf("flush remove nat rules %s: %w", pair.Destination, err))
|
// TODO: rollback set counter
|
||||||
|
return fmt.Errorf("remove nat rules rule %s: %v", pair.Destination, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nberrors.FormatErrorOrNil(merr)
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *router) removeNatRule(pair firewall.RouterPair) error {
|
func (r *router) removeNatRule(pair firewall.RouterPair) error {
|
||||||
ruleKey := firewall.GenKey(firewall.PreroutingFormat, pair)
|
ruleKey := firewall.GenKey(firewall.PreroutingFormat, pair)
|
||||||
|
|
||||||
rule, exists := r.rules[ruleKey]
|
if rule, exists := r.rules[ruleKey]; exists {
|
||||||
if !exists {
|
|
||||||
log.Debugf("prerouting rule %s not found", ruleKey)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if rule.Handle == 0 {
|
|
||||||
log.Warnf("prerouting rule %s has no handle, removing stale entry", ruleKey)
|
|
||||||
if err := r.decrementSetCounter(rule); err != nil {
|
|
||||||
log.Warnf("decrement set counter for stale rule %s: %v", ruleKey, err)
|
|
||||||
}
|
|
||||||
delete(r.rules, ruleKey)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := r.conn.DelRule(rule); err != nil {
|
if err := r.conn.DelRule(rule); err != nil {
|
||||||
return fmt.Errorf("remove prerouting rule %s -> %s: %w", pair.Source, pair.Destination, err)
|
return fmt.Errorf("remove prerouting rule %s -> %s: %v", pair.Source, pair.Destination, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("removed prerouting rule %s -> %s", pair.Source, pair.Destination)
|
log.Debugf("removed prerouting rule %s -> %s", pair.Source, pair.Destination)
|
||||||
@@ -1419,35 +1366,28 @@ func (r *router) removeNatRule(pair firewall.RouterPair) error {
|
|||||||
if err := r.decrementSetCounter(rule); err != nil {
|
if err := r.decrementSetCounter(rule); err != nil {
|
||||||
return fmt.Errorf("decrement set counter: %w", err)
|
return fmt.Errorf("decrement set counter: %w", err)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
log.Debugf("prerouting rule %s not found", ruleKey)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// refreshRulesMap rebuilds the rule map from the kernel. This removes stale entries
|
// refreshRulesMap refreshes the rule map with the latest rules. this is useful to avoid
|
||||||
// (e.g. from failed flushes) and updates handles for all existing rules.
|
// duplicates and to get missing attributes that we don't have when adding new rules
|
||||||
func (r *router) refreshRulesMap() error {
|
func (r *router) refreshRulesMap() error {
|
||||||
var merr *multierror.Error
|
|
||||||
newRules := make(map[string]*nftables.Rule)
|
|
||||||
for _, chain := range r.chains {
|
for _, chain := range r.chains {
|
||||||
rules, err := r.conn.GetRules(chain.Table, chain)
|
rules, err := r.conn.GetRules(chain.Table, chain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
merr = multierror.Append(merr, fmt.Errorf("list rules for chain %s: %w", chain.Name, err))
|
return fmt.Errorf("list rules: %w", err)
|
||||||
// preserve existing entries for this chain since we can't verify their state
|
|
||||||
for k, v := range r.rules {
|
|
||||||
if v.Chain != nil && v.Chain.Name == chain.Name {
|
|
||||||
newRules[k] = v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
for _, rule := range rules {
|
for _, rule := range rules {
|
||||||
if len(rule.UserData) > 0 {
|
if len(rule.UserData) > 0 {
|
||||||
newRules[string(rule.UserData)] = rule
|
r.rules[string(rule.UserData)] = rule
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
r.rules = newRules
|
return nil
|
||||||
return nberrors.FormatErrorOrNil(merr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *router) AddDNATRule(rule firewall.ForwardRule) (firewall.Rule, error) {
|
func (r *router) AddDNATRule(rule firewall.ForwardRule) (firewall.Rule, error) {
|
||||||
@@ -1689,35 +1629,21 @@ func (r *router) DeleteDNATRule(rule firewall.Rule) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var merr *multierror.Error
|
var merr *multierror.Error
|
||||||
var needsFlush bool
|
|
||||||
|
|
||||||
if dnatRule, exists := r.rules[ruleKey+dnatSuffix]; exists {
|
if dnatRule, exists := r.rules[ruleKey+dnatSuffix]; exists {
|
||||||
if dnatRule.Handle == 0 {
|
if err := r.conn.DelRule(dnatRule); err != nil {
|
||||||
log.Warnf("dnat rule %s has no handle, removing stale entry", ruleKey+dnatSuffix)
|
|
||||||
delete(r.rules, ruleKey+dnatSuffix)
|
|
||||||
} else if err := r.conn.DelRule(dnatRule); err != nil {
|
|
||||||
merr = multierror.Append(merr, fmt.Errorf("delete dnat rule: %w", err))
|
merr = multierror.Append(merr, fmt.Errorf("delete dnat rule: %w", err))
|
||||||
} else {
|
|
||||||
needsFlush = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if masqRule, exists := r.rules[ruleKey+snatSuffix]; exists {
|
if masqRule, exists := r.rules[ruleKey+snatSuffix]; exists {
|
||||||
if masqRule.Handle == 0 {
|
if err := r.conn.DelRule(masqRule); err != nil {
|
||||||
log.Warnf("snat rule %s has no handle, removing stale entry", ruleKey+snatSuffix)
|
|
||||||
delete(r.rules, ruleKey+snatSuffix)
|
|
||||||
} else if err := r.conn.DelRule(masqRule); err != nil {
|
|
||||||
merr = multierror.Append(merr, fmt.Errorf("delete snat rule: %w", err))
|
merr = multierror.Append(merr, fmt.Errorf("delete snat rule: %w", err))
|
||||||
} else {
|
|
||||||
needsFlush = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if needsFlush {
|
|
||||||
if err := r.conn.Flush(); err != nil {
|
if err := r.conn.Flush(); err != nil {
|
||||||
merr = multierror.Append(merr, fmt.Errorf(flushError, err))
|
merr = multierror.Append(merr, fmt.Errorf(flushError, err))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if merr == nil {
|
if merr == nil {
|
||||||
delete(r.rules, ruleKey+dnatSuffix)
|
delete(r.rules, ruleKey+dnatSuffix)
|
||||||
@@ -1831,17 +1757,7 @@ func (r *router) RemoveInboundDNAT(localAddr netip.Addr, protocol firewall.Proto
|
|||||||
|
|
||||||
ruleID := fmt.Sprintf("inbound-dnat-%s-%s-%d-%d", localAddr.String(), protocol, sourcePort, targetPort)
|
ruleID := fmt.Sprintf("inbound-dnat-%s-%s-%d-%d", localAddr.String(), protocol, sourcePort, targetPort)
|
||||||
|
|
||||||
rule, exists := r.rules[ruleID]
|
if rule, exists := r.rules[ruleID]; exists {
|
||||||
if !exists {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if rule.Handle == 0 {
|
|
||||||
log.Warnf("inbound DNAT rule %s has no handle, removing stale entry", ruleID)
|
|
||||||
delete(r.rules, ruleID)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := r.conn.DelRule(rule); err != nil {
|
if err := r.conn.DelRule(rule); err != nil {
|
||||||
return fmt.Errorf("delete inbound DNAT rule %s: %w", ruleID, err)
|
return fmt.Errorf("delete inbound DNAT rule %s: %w", ruleID, err)
|
||||||
}
|
}
|
||||||
@@ -1849,6 +1765,7 @@ func (r *router) RemoveInboundDNAT(localAddr netip.Addr, protocol firewall.Proto
|
|||||||
return fmt.Errorf("flush delete inbound DNAT rule: %w", err)
|
return fmt.Errorf("flush delete inbound DNAT rule: %w", err)
|
||||||
}
|
}
|
||||||
delete(r.rules, ruleID)
|
delete(r.rules, ruleID)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ import (
|
|||||||
firewall "github.com/netbirdio/netbird/client/firewall/manager"
|
firewall "github.com/netbirdio/netbird/client/firewall/manager"
|
||||||
"github.com/netbirdio/netbird/client/firewall/test"
|
"github.com/netbirdio/netbird/client/firewall/test"
|
||||||
"github.com/netbirdio/netbird/client/iface"
|
"github.com/netbirdio/netbird/client/iface"
|
||||||
"github.com/netbirdio/netbird/client/internal/acl/id"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -720,137 +719,3 @@ func deleteWorkTable() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRouter_RefreshRulesMap_RemovesStaleEntries(t *testing.T) {
|
|
||||||
if check() != NFTABLES {
|
|
||||||
t.Skip("nftables not supported on this system")
|
|
||||||
}
|
|
||||||
|
|
||||||
workTable, err := createWorkTable()
|
|
||||||
require.NoError(t, err)
|
|
||||||
defer deleteWorkTable()
|
|
||||||
|
|
||||||
r, err := newRouter(workTable, ifaceMock, iface.DefaultMTU)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NoError(t, r.init(workTable))
|
|
||||||
defer func() { require.NoError(t, r.Reset()) }()
|
|
||||||
|
|
||||||
// Add a real rule to the kernel
|
|
||||||
ruleKey, err := r.AddRouteFiltering(
|
|
||||||
nil,
|
|
||||||
[]netip.Prefix{netip.MustParsePrefix("192.168.1.0/24")},
|
|
||||||
firewall.Network{Prefix: netip.MustParsePrefix("10.0.0.0/24")},
|
|
||||||
firewall.ProtocolTCP,
|
|
||||||
nil,
|
|
||||||
&firewall.Port{Values: []uint16{80}},
|
|
||||||
firewall.ActionAccept,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
|
||||||
t.Cleanup(func() {
|
|
||||||
require.NoError(t, r.DeleteRouteRule(ruleKey))
|
|
||||||
})
|
|
||||||
|
|
||||||
// Inject a stale entry with Handle=0 (simulates store-before-flush failure)
|
|
||||||
staleKey := "stale-rule-that-does-not-exist"
|
|
||||||
r.rules[staleKey] = &nftables.Rule{
|
|
||||||
Table: r.workTable,
|
|
||||||
Chain: r.chains[chainNameRoutingFw],
|
|
||||||
Handle: 0,
|
|
||||||
UserData: []byte(staleKey),
|
|
||||||
}
|
|
||||||
|
|
||||||
require.Contains(t, r.rules, staleKey, "stale entry should be in map before refresh")
|
|
||||||
|
|
||||||
err = r.refreshRulesMap()
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
assert.NotContains(t, r.rules, staleKey, "stale entry should be removed after refresh")
|
|
||||||
|
|
||||||
realRule, ok := r.rules[ruleKey.ID()]
|
|
||||||
assert.True(t, ok, "real rule should still exist after refresh")
|
|
||||||
assert.NotZero(t, realRule.Handle, "real rule should have a valid handle")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRouter_DeleteRouteRule_StaleHandle(t *testing.T) {
|
|
||||||
if check() != NFTABLES {
|
|
||||||
t.Skip("nftables not supported on this system")
|
|
||||||
}
|
|
||||||
|
|
||||||
workTable, err := createWorkTable()
|
|
||||||
require.NoError(t, err)
|
|
||||||
defer deleteWorkTable()
|
|
||||||
|
|
||||||
r, err := newRouter(workTable, ifaceMock, iface.DefaultMTU)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NoError(t, r.init(workTable))
|
|
||||||
defer func() { require.NoError(t, r.Reset()) }()
|
|
||||||
|
|
||||||
// Inject a stale entry with Handle=0
|
|
||||||
staleKey := "stale-route-rule"
|
|
||||||
r.rules[staleKey] = &nftables.Rule{
|
|
||||||
Table: r.workTable,
|
|
||||||
Chain: r.chains[chainNameRoutingFw],
|
|
||||||
Handle: 0,
|
|
||||||
UserData: []byte(staleKey),
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeleteRouteRule should not return an error for stale handles
|
|
||||||
err = r.DeleteRouteRule(id.RuleID(staleKey))
|
|
||||||
assert.NoError(t, err, "deleting a stale rule should not error")
|
|
||||||
assert.NotContains(t, r.rules, staleKey, "stale entry should be cleaned up")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRouter_AddNatRule_WithStaleEntry(t *testing.T) {
|
|
||||||
if check() != NFTABLES {
|
|
||||||
t.Skip("nftables not supported on this system")
|
|
||||||
}
|
|
||||||
|
|
||||||
manager, err := Create(ifaceMock, iface.DefaultMTU)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NoError(t, manager.Init(nil))
|
|
||||||
t.Cleanup(func() {
|
|
||||||
require.NoError(t, manager.Close(nil))
|
|
||||||
})
|
|
||||||
|
|
||||||
pair := firewall.RouterPair{
|
|
||||||
ID: "staletest",
|
|
||||||
Source: firewall.Network{Prefix: netip.MustParsePrefix("100.100.100.1/32")},
|
|
||||||
Destination: firewall.Network{Prefix: netip.MustParsePrefix("100.100.200.0/24")},
|
|
||||||
Masquerade: true,
|
|
||||||
}
|
|
||||||
|
|
||||||
rtr := manager.router
|
|
||||||
|
|
||||||
// First add succeeds
|
|
||||||
err = rtr.AddNatRule(pair)
|
|
||||||
require.NoError(t, err)
|
|
||||||
t.Cleanup(func() {
|
|
||||||
require.NoError(t, rtr.RemoveNatRule(pair))
|
|
||||||
})
|
|
||||||
|
|
||||||
// Corrupt the handle to simulate stale state
|
|
||||||
natRuleKey := firewall.GenKey(firewall.PreroutingFormat, pair)
|
|
||||||
if rule, exists := rtr.rules[natRuleKey]; exists {
|
|
||||||
rule.Handle = 0
|
|
||||||
}
|
|
||||||
inverseKey := firewall.GenKey(firewall.PreroutingFormat, firewall.GetInversePair(pair))
|
|
||||||
if rule, exists := rtr.rules[inverseKey]; exists {
|
|
||||||
rule.Handle = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// Adding the same rule again should succeed despite stale handles
|
|
||||||
err = rtr.AddNatRule(pair)
|
|
||||||
assert.NoError(t, err, "AddNatRule should succeed even with stale entries")
|
|
||||||
|
|
||||||
// Verify rules exist in kernel
|
|
||||||
rules, err := rtr.conn.GetRules(rtr.workTable, rtr.chains[chainNameManglePrerouting])
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
found := 0
|
|
||||||
for _, rule := range rules {
|
|
||||||
if len(rule.UserData) > 0 && string(rule.UserData) == natRuleKey {
|
|
||||||
found++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assert.Equal(t, 1, found, "NAT rule should exist in kernel")
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -3,6 +3,12 @@
|
|||||||
package uspfilter
|
package uspfilter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"net/netip"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/netbirdio/netbird/client/internal/statemanager"
|
"github.com/netbirdio/netbird/client/internal/statemanager"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -11,7 +17,33 @@ func (m *Manager) Close(stateManager *statemanager.Manager) error {
|
|||||||
m.mutex.Lock()
|
m.mutex.Lock()
|
||||||
defer m.mutex.Unlock()
|
defer m.mutex.Unlock()
|
||||||
|
|
||||||
m.resetState()
|
m.outgoingRules = make(map[netip.Addr]RuleSet)
|
||||||
|
m.incomingDenyRules = make(map[netip.Addr]RuleSet)
|
||||||
|
m.incomingRules = make(map[netip.Addr]RuleSet)
|
||||||
|
|
||||||
|
if m.udpTracker != nil {
|
||||||
|
m.udpTracker.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.icmpTracker != nil {
|
||||||
|
m.icmpTracker.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.tcpTracker != nil {
|
||||||
|
m.tcpTracker.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
if fwder := m.forwarder.Load(); fwder != nil {
|
||||||
|
fwder.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.logger != nil {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if err := m.logger.Stop(ctx); err != nil {
|
||||||
|
log.Errorf("failed to shutdown logger: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if m.nativeFirewall != nil {
|
if m.nativeFirewall != nil {
|
||||||
return m.nativeFirewall.Close(stateManager)
|
return m.nativeFirewall.Close(stateManager)
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
package uspfilter
|
package uspfilter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/netip"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
@@ -23,7 +26,33 @@ func (m *Manager) Close(*statemanager.Manager) error {
|
|||||||
m.mutex.Lock()
|
m.mutex.Lock()
|
||||||
defer m.mutex.Unlock()
|
defer m.mutex.Unlock()
|
||||||
|
|
||||||
m.resetState()
|
m.outgoingRules = make(map[netip.Addr]RuleSet)
|
||||||
|
m.incomingDenyRules = make(map[netip.Addr]RuleSet)
|
||||||
|
m.incomingRules = make(map[netip.Addr]RuleSet)
|
||||||
|
|
||||||
|
if m.udpTracker != nil {
|
||||||
|
m.udpTracker.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.icmpTracker != nil {
|
||||||
|
m.icmpTracker.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.tcpTracker != nil {
|
||||||
|
m.tcpTracker.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
if fwder := m.forwarder.Load(); fwder != nil {
|
||||||
|
fwder.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.logger != nil {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if err := m.logger.Stop(ctx); err != nil {
|
||||||
|
log.Errorf("failed to shutdown logger: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if !isWindowsFirewallReachable() {
|
if !isWindowsFirewallReachable() {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -115,17 +115,6 @@ func (t *TCPConnTrack) IsTombstone() bool {
|
|||||||
return t.tombstone.Load()
|
return t.tombstone.Load()
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsSupersededBy returns true if this connection should be replaced by a new one
|
|
||||||
// carrying the given flags. Tombstoned connections are always superseded; TIME-WAIT
|
|
||||||
// connections are superseded by a pure SYN (a new connection attempt for the same
|
|
||||||
// four-tuple, as contemplated by RFC 1122 §4.2.2.13 and RFC 6191).
|
|
||||||
func (t *TCPConnTrack) IsSupersededBy(flags uint8) bool {
|
|
||||||
if t.tombstone.Load() {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return flags&TCPSyn != 0 && flags&TCPAck == 0 && TCPState(t.state.Load()) == TCPStateTimeWait
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetTombstone safely marks the connection for deletion
|
// SetTombstone safely marks the connection for deletion
|
||||||
func (t *TCPConnTrack) SetTombstone() {
|
func (t *TCPConnTrack) SetTombstone() {
|
||||||
t.tombstone.Store(true)
|
t.tombstone.Store(true)
|
||||||
@@ -180,7 +169,7 @@ func (t *TCPTracker) updateIfExists(srcIP, dstIP netip.Addr, srcPort, dstPort ui
|
|||||||
conn, exists := t.connections[key]
|
conn, exists := t.connections[key]
|
||||||
t.mutex.RUnlock()
|
t.mutex.RUnlock()
|
||||||
|
|
||||||
if exists && !conn.IsSupersededBy(flags) {
|
if exists {
|
||||||
t.updateState(key, conn, flags, direction, size)
|
t.updateState(key, conn, flags, direction, size)
|
||||||
return key, uint16(conn.DNATOrigPort.Load()), true
|
return key, uint16(conn.DNATOrigPort.Load()), true
|
||||||
}
|
}
|
||||||
@@ -252,7 +241,7 @@ func (t *TCPTracker) IsValidInbound(srcIP, dstIP netip.Addr, srcPort, dstPort ui
|
|||||||
conn, exists := t.connections[key]
|
conn, exists := t.connections[key]
|
||||||
t.mutex.RUnlock()
|
t.mutex.RUnlock()
|
||||||
|
|
||||||
if !exists || conn.IsSupersededBy(flags) {
|
if !exists || conn.IsTombstone() {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -485,261 +485,6 @@ func TestTCPAbnormalSequences(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestTCPPortReuseTombstone verifies that a new connection on a port with a
|
|
||||||
// tombstoned (closed) conntrack entry is properly tracked. Without the fix,
|
|
||||||
// updateIfExists treats tombstoned entries as live, causing track() to skip
|
|
||||||
// creating a new connection. The subsequent SYN-ACK then fails IsValidInbound
|
|
||||||
// because the entry is tombstoned, and the response packet gets dropped by ACL.
|
|
||||||
func TestTCPPortReuseTombstone(t *testing.T) {
|
|
||||||
srcIP := netip.MustParseAddr("100.64.0.1")
|
|
||||||
dstIP := netip.MustParseAddr("100.64.0.2")
|
|
||||||
srcPort := uint16(12345)
|
|
||||||
dstPort := uint16(80)
|
|
||||||
|
|
||||||
t.Run("Outbound port reuse after graceful close", func(t *testing.T) {
|
|
||||||
tracker := NewTCPTracker(DefaultTCPTimeout, logger, flowLogger)
|
|
||||||
defer tracker.Close()
|
|
||||||
|
|
||||||
key := ConnKey{SrcIP: srcIP, DstIP: dstIP, SrcPort: srcPort, DstPort: dstPort}
|
|
||||||
|
|
||||||
// Establish and gracefully close a connection (server-initiated close)
|
|
||||||
establishConnection(t, tracker, srcIP, dstIP, srcPort, dstPort)
|
|
||||||
|
|
||||||
// Server sends FIN
|
|
||||||
valid := tracker.IsValidInbound(dstIP, srcIP, dstPort, srcPort, TCPFin|TCPAck, 0)
|
|
||||||
require.True(t, valid)
|
|
||||||
|
|
||||||
// Client sends FIN-ACK
|
|
||||||
tracker.TrackOutbound(srcIP, dstIP, srcPort, dstPort, TCPFin|TCPAck, 0)
|
|
||||||
|
|
||||||
// Server sends final ACK
|
|
||||||
valid = tracker.IsValidInbound(dstIP, srcIP, dstPort, srcPort, TCPAck, 0)
|
|
||||||
require.True(t, valid)
|
|
||||||
|
|
||||||
// Connection should be tombstoned
|
|
||||||
conn := tracker.connections[key]
|
|
||||||
require.NotNil(t, conn, "old connection should still be in map")
|
|
||||||
require.True(t, conn.IsTombstone(), "old connection should be tombstoned")
|
|
||||||
|
|
||||||
// Now reuse the same port for a new connection
|
|
||||||
tracker.TrackOutbound(srcIP, dstIP, srcPort, dstPort, TCPSyn, 100)
|
|
||||||
|
|
||||||
// The old tombstoned entry should be replaced with a new one
|
|
||||||
newConn := tracker.connections[key]
|
|
||||||
require.NotNil(t, newConn, "new connection should exist")
|
|
||||||
require.False(t, newConn.IsTombstone(), "new connection should not be tombstoned")
|
|
||||||
require.Equal(t, TCPStateSynSent, newConn.GetState())
|
|
||||||
|
|
||||||
// SYN-ACK for the new connection should be valid
|
|
||||||
valid = tracker.IsValidInbound(dstIP, srcIP, dstPort, srcPort, TCPSyn|TCPAck, 100)
|
|
||||||
require.True(t, valid, "SYN-ACK for new connection on reused port should be accepted")
|
|
||||||
require.Equal(t, TCPStateEstablished, newConn.GetState())
|
|
||||||
|
|
||||||
// Data transfer should work
|
|
||||||
tracker.TrackOutbound(srcIP, dstIP, srcPort, dstPort, TCPAck, 100)
|
|
||||||
valid = tracker.IsValidInbound(dstIP, srcIP, dstPort, srcPort, TCPPush|TCPAck, 500)
|
|
||||||
require.True(t, valid, "data should be allowed on new connection")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("Outbound port reuse after RST", func(t *testing.T) {
|
|
||||||
tracker := NewTCPTracker(DefaultTCPTimeout, logger, flowLogger)
|
|
||||||
defer tracker.Close()
|
|
||||||
|
|
||||||
key := ConnKey{SrcIP: srcIP, DstIP: dstIP, SrcPort: srcPort, DstPort: dstPort}
|
|
||||||
|
|
||||||
// Establish and RST a connection
|
|
||||||
establishConnection(t, tracker, srcIP, dstIP, srcPort, dstPort)
|
|
||||||
valid := tracker.IsValidInbound(dstIP, srcIP, dstPort, srcPort, TCPRst|TCPAck, 0)
|
|
||||||
require.True(t, valid)
|
|
||||||
|
|
||||||
conn := tracker.connections[key]
|
|
||||||
require.True(t, conn.IsTombstone(), "RST connection should be tombstoned")
|
|
||||||
|
|
||||||
// Reuse the same port
|
|
||||||
tracker.TrackOutbound(srcIP, dstIP, srcPort, dstPort, TCPSyn, 100)
|
|
||||||
|
|
||||||
newConn := tracker.connections[key]
|
|
||||||
require.NotNil(t, newConn)
|
|
||||||
require.False(t, newConn.IsTombstone())
|
|
||||||
require.Equal(t, TCPStateSynSent, newConn.GetState())
|
|
||||||
|
|
||||||
valid = tracker.IsValidInbound(dstIP, srcIP, dstPort, srcPort, TCPSyn|TCPAck, 100)
|
|
||||||
require.True(t, valid, "SYN-ACK should be accepted after RST tombstone")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("Inbound port reuse after close", func(t *testing.T) {
|
|
||||||
tracker := NewTCPTracker(DefaultTCPTimeout, logger, flowLogger)
|
|
||||||
defer tracker.Close()
|
|
||||||
|
|
||||||
clientIP := srcIP
|
|
||||||
serverIP := dstIP
|
|
||||||
clientPort := srcPort
|
|
||||||
serverPort := dstPort
|
|
||||||
key := ConnKey{SrcIP: clientIP, DstIP: serverIP, SrcPort: clientPort, DstPort: serverPort}
|
|
||||||
|
|
||||||
// Inbound connection: client SYN → server SYN-ACK → client ACK
|
|
||||||
tracker.TrackInbound(clientIP, serverIP, clientPort, serverPort, TCPSyn, nil, 100, 0)
|
|
||||||
tracker.TrackOutbound(serverIP, clientIP, serverPort, clientPort, TCPSyn|TCPAck, 100)
|
|
||||||
tracker.TrackInbound(clientIP, serverIP, clientPort, serverPort, TCPAck, nil, 100, 0)
|
|
||||||
|
|
||||||
conn := tracker.connections[key]
|
|
||||||
require.Equal(t, TCPStateEstablished, conn.GetState())
|
|
||||||
|
|
||||||
// Server-initiated close to reach Closed/tombstoned:
|
|
||||||
// Server FIN (opposite dir) → CloseWait
|
|
||||||
tracker.TrackOutbound(serverIP, clientIP, serverPort, clientPort, TCPFin|TCPAck, 100)
|
|
||||||
require.Equal(t, TCPStateCloseWait, conn.GetState())
|
|
||||||
// Client FIN-ACK (same dir as conn) → LastAck
|
|
||||||
tracker.TrackInbound(clientIP, serverIP, clientPort, serverPort, TCPFin|TCPAck, nil, 100, 0)
|
|
||||||
require.Equal(t, TCPStateLastAck, conn.GetState())
|
|
||||||
// Server final ACK (opposite dir) → Closed → tombstoned
|
|
||||||
tracker.TrackOutbound(serverIP, clientIP, serverPort, clientPort, TCPAck, 100)
|
|
||||||
|
|
||||||
require.True(t, conn.IsTombstone())
|
|
||||||
|
|
||||||
// New inbound connection on same ports
|
|
||||||
tracker.TrackInbound(clientIP, serverIP, clientPort, serverPort, TCPSyn, nil, 100, 0)
|
|
||||||
|
|
||||||
newConn := tracker.connections[key]
|
|
||||||
require.NotNil(t, newConn)
|
|
||||||
require.False(t, newConn.IsTombstone())
|
|
||||||
require.Equal(t, TCPStateSynReceived, newConn.GetState())
|
|
||||||
|
|
||||||
// Complete handshake: server SYN-ACK, then client ACK
|
|
||||||
tracker.TrackOutbound(serverIP, clientIP, serverPort, clientPort, TCPSyn|TCPAck, 100)
|
|
||||||
tracker.TrackInbound(clientIP, serverIP, clientPort, serverPort, TCPAck, nil, 100, 0)
|
|
||||||
require.Equal(t, TCPStateEstablished, newConn.GetState())
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("Late ACK on tombstoned connection is harmless", func(t *testing.T) {
|
|
||||||
tracker := NewTCPTracker(DefaultTCPTimeout, logger, flowLogger)
|
|
||||||
defer tracker.Close()
|
|
||||||
|
|
||||||
key := ConnKey{SrcIP: srcIP, DstIP: dstIP, SrcPort: srcPort, DstPort: dstPort}
|
|
||||||
|
|
||||||
// Establish and close via passive close (server-initiated FIN → Closed → tombstoned)
|
|
||||||
establishConnection(t, tracker, srcIP, dstIP, srcPort, dstPort)
|
|
||||||
tracker.IsValidInbound(dstIP, srcIP, dstPort, srcPort, TCPFin|TCPAck, 0) // CloseWait
|
|
||||||
tracker.TrackOutbound(srcIP, dstIP, srcPort, dstPort, TCPFin|TCPAck, 0) // LastAck
|
|
||||||
tracker.IsValidInbound(dstIP, srcIP, dstPort, srcPort, TCPAck, 0) // Closed
|
|
||||||
|
|
||||||
conn := tracker.connections[key]
|
|
||||||
require.True(t, conn.IsTombstone())
|
|
||||||
|
|
||||||
// Late ACK should be rejected (tombstoned)
|
|
||||||
valid := tracker.IsValidInbound(dstIP, srcIP, dstPort, srcPort, TCPAck, 0)
|
|
||||||
require.False(t, valid, "late ACK on tombstoned connection should be rejected")
|
|
||||||
|
|
||||||
// Late outbound ACK should not create a new connection (not a SYN)
|
|
||||||
tracker.TrackOutbound(srcIP, dstIP, srcPort, dstPort, TCPAck, 0)
|
|
||||||
require.True(t, tracker.connections[key].IsTombstone(), "late outbound ACK should not replace tombstoned entry")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTCPPortReuseTimeWait(t *testing.T) {
|
|
||||||
srcIP := netip.MustParseAddr("100.64.0.1")
|
|
||||||
dstIP := netip.MustParseAddr("100.64.0.2")
|
|
||||||
srcPort := uint16(12345)
|
|
||||||
dstPort := uint16(80)
|
|
||||||
|
|
||||||
t.Run("Outbound port reuse during TIME-WAIT (active close)", func(t *testing.T) {
|
|
||||||
tracker := NewTCPTracker(DefaultTCPTimeout, logger, flowLogger)
|
|
||||||
defer tracker.Close()
|
|
||||||
|
|
||||||
key := ConnKey{SrcIP: srcIP, DstIP: dstIP, SrcPort: srcPort, DstPort: dstPort}
|
|
||||||
|
|
||||||
// Establish connection
|
|
||||||
establishConnection(t, tracker, srcIP, dstIP, srcPort, dstPort)
|
|
||||||
|
|
||||||
// Active close: client (outbound initiator) sends FIN first
|
|
||||||
tracker.TrackOutbound(srcIP, dstIP, srcPort, dstPort, TCPFin|TCPAck, 0)
|
|
||||||
conn := tracker.connections[key]
|
|
||||||
require.Equal(t, TCPStateFinWait1, conn.GetState())
|
|
||||||
|
|
||||||
// Server ACKs the FIN
|
|
||||||
valid := tracker.IsValidInbound(dstIP, srcIP, dstPort, srcPort, TCPAck, 0)
|
|
||||||
require.True(t, valid)
|
|
||||||
require.Equal(t, TCPStateFinWait2, conn.GetState())
|
|
||||||
|
|
||||||
// Server sends its own FIN
|
|
||||||
valid = tracker.IsValidInbound(dstIP, srcIP, dstPort, srcPort, TCPFin|TCPAck, 0)
|
|
||||||
require.True(t, valid)
|
|
||||||
require.Equal(t, TCPStateTimeWait, conn.GetState())
|
|
||||||
|
|
||||||
// Client sends final ACK (TIME-WAIT stays, not tombstoned)
|
|
||||||
tracker.TrackOutbound(srcIP, dstIP, srcPort, dstPort, TCPAck, 0)
|
|
||||||
require.False(t, conn.IsTombstone(), "TIME-WAIT should not be tombstoned")
|
|
||||||
|
|
||||||
// New outbound SYN on the same port (port reuse during TIME-WAIT)
|
|
||||||
tracker.TrackOutbound(srcIP, dstIP, srcPort, dstPort, TCPSyn, 100)
|
|
||||||
|
|
||||||
// Per RFC 1122/6191, new SYN during TIME-WAIT should start a new connection
|
|
||||||
newConn := tracker.connections[key]
|
|
||||||
require.NotNil(t, newConn, "new connection should exist")
|
|
||||||
require.False(t, newConn.IsTombstone(), "new connection should not be tombstoned")
|
|
||||||
require.Equal(t, TCPStateSynSent, newConn.GetState(), "new connection should be in SYN-SENT")
|
|
||||||
|
|
||||||
// SYN-ACK for new connection should be valid
|
|
||||||
valid = tracker.IsValidInbound(dstIP, srcIP, dstPort, srcPort, TCPSyn|TCPAck, 100)
|
|
||||||
require.True(t, valid, "SYN-ACK for new connection should be accepted")
|
|
||||||
require.Equal(t, TCPStateEstablished, newConn.GetState())
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("Inbound SYN during TIME-WAIT falls through to normal tracking", func(t *testing.T) {
|
|
||||||
tracker := NewTCPTracker(DefaultTCPTimeout, logger, flowLogger)
|
|
||||||
defer tracker.Close()
|
|
||||||
|
|
||||||
key := ConnKey{SrcIP: srcIP, DstIP: dstIP, SrcPort: srcPort, DstPort: dstPort}
|
|
||||||
|
|
||||||
// Establish outbound connection and close via active close → TIME-WAIT
|
|
||||||
establishConnection(t, tracker, srcIP, dstIP, srcPort, dstPort)
|
|
||||||
tracker.TrackOutbound(srcIP, dstIP, srcPort, dstPort, TCPFin|TCPAck, 0)
|
|
||||||
tracker.IsValidInbound(dstIP, srcIP, dstPort, srcPort, TCPAck, 0)
|
|
||||||
tracker.IsValidInbound(dstIP, srcIP, dstPort, srcPort, TCPFin|TCPAck, 0)
|
|
||||||
tracker.TrackOutbound(srcIP, dstIP, srcPort, dstPort, TCPAck, 0)
|
|
||||||
|
|
||||||
conn := tracker.connections[key]
|
|
||||||
require.Equal(t, TCPStateTimeWait, conn.GetState())
|
|
||||||
|
|
||||||
// Inbound SYN on same ports during TIME-WAIT: IsValidInbound returns false
|
|
||||||
// so the filter falls through to ACL check + TrackInbound (which creates
|
|
||||||
// a new connection via track() → updateIfExists skips TIME-WAIT for SYN)
|
|
||||||
valid := tracker.IsValidInbound(dstIP, srcIP, dstPort, srcPort, TCPSyn, 0)
|
|
||||||
require.False(t, valid, "inbound SYN during TIME-WAIT should fail conntrack validation")
|
|
||||||
|
|
||||||
// Simulate what the filter does next: TrackInbound via the normal path
|
|
||||||
tracker.TrackInbound(dstIP, srcIP, dstPort, srcPort, TCPSyn, nil, 100, 0)
|
|
||||||
|
|
||||||
// The new inbound connection uses the inverted key (dst→src becomes src→dst in track)
|
|
||||||
invertedKey := ConnKey{SrcIP: dstIP, DstIP: srcIP, SrcPort: dstPort, DstPort: srcPort}
|
|
||||||
newConn := tracker.connections[invertedKey]
|
|
||||||
require.NotNil(t, newConn, "new inbound connection should be tracked")
|
|
||||||
require.Equal(t, TCPStateSynReceived, newConn.GetState())
|
|
||||||
require.False(t, newConn.IsTombstone())
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("Late retransmit during TIME-WAIT still allowed", func(t *testing.T) {
|
|
||||||
tracker := NewTCPTracker(DefaultTCPTimeout, logger, flowLogger)
|
|
||||||
defer tracker.Close()
|
|
||||||
|
|
||||||
key := ConnKey{SrcIP: srcIP, DstIP: dstIP, SrcPort: srcPort, DstPort: dstPort}
|
|
||||||
|
|
||||||
// Establish and active close → TIME-WAIT
|
|
||||||
establishConnection(t, tracker, srcIP, dstIP, srcPort, dstPort)
|
|
||||||
tracker.TrackOutbound(srcIP, dstIP, srcPort, dstPort, TCPFin|TCPAck, 0)
|
|
||||||
tracker.IsValidInbound(dstIP, srcIP, dstPort, srcPort, TCPAck, 0)
|
|
||||||
tracker.IsValidInbound(dstIP, srcIP, dstPort, srcPort, TCPFin|TCPAck, 0)
|
|
||||||
tracker.TrackOutbound(srcIP, dstIP, srcPort, dstPort, TCPAck, 0)
|
|
||||||
|
|
||||||
conn := tracker.connections[key]
|
|
||||||
require.Equal(t, TCPStateTimeWait, conn.GetState())
|
|
||||||
|
|
||||||
// Late ACK retransmits during TIME-WAIT should still be accepted
|
|
||||||
valid := tracker.IsValidInbound(dstIP, srcIP, dstPort, srcPort, TCPAck, 0)
|
|
||||||
require.True(t, valid, "retransmitted ACK during TIME-WAIT should be accepted")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTCPTimeoutHandling(t *testing.T) {
|
func TestTCPTimeoutHandling(t *testing.T) {
|
||||||
// Create tracker with a very short timeout for testing
|
// Create tracker with a very short timeout for testing
|
||||||
shortTimeout := 100 * time.Millisecond
|
shortTimeout := 100 * time.Millisecond
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package uspfilter
|
package uspfilter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -13,13 +12,11 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/google/gopacket"
|
"github.com/google/gopacket"
|
||||||
"github.com/google/gopacket/layers"
|
"github.com/google/gopacket/layers"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"golang.org/x/exp/maps"
|
|
||||||
|
|
||||||
firewall "github.com/netbirdio/netbird/client/firewall/manager"
|
firewall "github.com/netbirdio/netbird/client/firewall/manager"
|
||||||
"github.com/netbirdio/netbird/client/firewall/uspfilter/common"
|
"github.com/netbirdio/netbird/client/firewall/uspfilter/common"
|
||||||
@@ -27,7 +24,6 @@ import (
|
|||||||
"github.com/netbirdio/netbird/client/firewall/uspfilter/forwarder"
|
"github.com/netbirdio/netbird/client/firewall/uspfilter/forwarder"
|
||||||
nblog "github.com/netbirdio/netbird/client/firewall/uspfilter/log"
|
nblog "github.com/netbirdio/netbird/client/firewall/uspfilter/log"
|
||||||
"github.com/netbirdio/netbird/client/iface/netstack"
|
"github.com/netbirdio/netbird/client/iface/netstack"
|
||||||
nbid "github.com/netbirdio/netbird/client/internal/acl/id"
|
|
||||||
nftypes "github.com/netbirdio/netbird/client/internal/netflow/types"
|
nftypes "github.com/netbirdio/netbird/client/internal/netflow/types"
|
||||||
"github.com/netbirdio/netbird/client/internal/statemanager"
|
"github.com/netbirdio/netbird/client/internal/statemanager"
|
||||||
)
|
)
|
||||||
@@ -93,7 +89,6 @@ type Manager struct {
|
|||||||
incomingDenyRules map[netip.Addr]RuleSet
|
incomingDenyRules map[netip.Addr]RuleSet
|
||||||
incomingRules map[netip.Addr]RuleSet
|
incomingRules map[netip.Addr]RuleSet
|
||||||
routeRules RouteRules
|
routeRules RouteRules
|
||||||
routeRulesMap map[nbid.RuleID]*RouteRule
|
|
||||||
decoders sync.Pool
|
decoders sync.Pool
|
||||||
wgIface common.IFaceMapper
|
wgIface common.IFaceMapper
|
||||||
nativeFirewall firewall.Manager
|
nativeFirewall firewall.Manager
|
||||||
@@ -234,7 +229,6 @@ func create(iface common.IFaceMapper, nativeFirewall firewall.Manager, disableSe
|
|||||||
flowLogger: flowLogger,
|
flowLogger: flowLogger,
|
||||||
netstack: netstack.IsEnabled(),
|
netstack: netstack.IsEnabled(),
|
||||||
localForwarding: enableLocalForwarding,
|
localForwarding: enableLocalForwarding,
|
||||||
routeRulesMap: make(map[nbid.RuleID]*RouteRule),
|
|
||||||
dnatMappings: make(map[netip.Addr]netip.Addr),
|
dnatMappings: make(map[netip.Addr]netip.Addr),
|
||||||
portDNATRules: []portDNATRule{},
|
portDNATRules: []portDNATRule{},
|
||||||
netstackServices: make(map[serviceKey]struct{}),
|
netstackServices: make(map[serviceKey]struct{}),
|
||||||
@@ -486,15 +480,11 @@ func (m *Manager) addRouteFiltering(
|
|||||||
return m.nativeFirewall.AddRouteFiltering(id, sources, destination, proto, sPort, dPort, action)
|
return m.nativeFirewall.AddRouteFiltering(id, sources, destination, proto, sPort, dPort, action)
|
||||||
}
|
}
|
||||||
|
|
||||||
ruleKey := nbid.GenerateRouteRuleKey(sources, destination, proto, sPort, dPort, action)
|
ruleID := uuid.New().String()
|
||||||
|
|
||||||
if existingRule, ok := m.routeRulesMap[ruleKey]; ok {
|
|
||||||
return existingRule, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
rule := RouteRule{
|
rule := RouteRule{
|
||||||
// TODO: consolidate these IDs
|
// TODO: consolidate these IDs
|
||||||
id: string(ruleKey),
|
id: ruleID,
|
||||||
mgmtId: id,
|
mgmtId: id,
|
||||||
sources: sources,
|
sources: sources,
|
||||||
dstSet: destination.Set,
|
dstSet: destination.Set,
|
||||||
@@ -509,7 +499,6 @@ func (m *Manager) addRouteFiltering(
|
|||||||
|
|
||||||
m.routeRules = append(m.routeRules, &rule)
|
m.routeRules = append(m.routeRules, &rule)
|
||||||
m.routeRules.Sort()
|
m.routeRules.Sort()
|
||||||
m.routeRulesMap[ruleKey] = &rule
|
|
||||||
|
|
||||||
return &rule, nil
|
return &rule, nil
|
||||||
}
|
}
|
||||||
@@ -526,20 +515,15 @@ func (m *Manager) deleteRouteRule(rule firewall.Rule) error {
|
|||||||
return m.nativeFirewall.DeleteRouteRule(rule)
|
return m.nativeFirewall.DeleteRouteRule(rule)
|
||||||
}
|
}
|
||||||
|
|
||||||
ruleKey := nbid.RuleID(rule.ID())
|
ruleID := rule.ID()
|
||||||
if _, ok := m.routeRulesMap[ruleKey]; !ok {
|
|
||||||
return fmt.Errorf("route rule not found: %s", ruleKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
idx := slices.IndexFunc(m.routeRules, func(r *RouteRule) bool {
|
idx := slices.IndexFunc(m.routeRules, func(r *RouteRule) bool {
|
||||||
return r.id == string(ruleKey)
|
return r.id == ruleID
|
||||||
})
|
})
|
||||||
if idx < 0 {
|
if idx < 0 {
|
||||||
return fmt.Errorf("route rule not found in slice: %s", ruleKey)
|
return fmt.Errorf("route rule not found: %s", ruleID)
|
||||||
}
|
}
|
||||||
|
|
||||||
m.routeRules = slices.Delete(m.routeRules, idx, idx+1)
|
m.routeRules = slices.Delete(m.routeRules, idx, idx+1)
|
||||||
delete(m.routeRulesMap, ruleKey)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -586,40 +570,6 @@ func (m *Manager) SetLegacyManagement(isLegacy bool) error {
|
|||||||
// Flush doesn't need to be implemented for this manager
|
// Flush doesn't need to be implemented for this manager
|
||||||
func (m *Manager) Flush() error { return nil }
|
func (m *Manager) Flush() error { return nil }
|
||||||
|
|
||||||
// resetState clears all firewall rules and closes connection trackers.
|
|
||||||
// Must be called with m.mutex held.
|
|
||||||
func (m *Manager) resetState() {
|
|
||||||
maps.Clear(m.outgoingRules)
|
|
||||||
maps.Clear(m.incomingDenyRules)
|
|
||||||
maps.Clear(m.incomingRules)
|
|
||||||
maps.Clear(m.routeRulesMap)
|
|
||||||
m.routeRules = m.routeRules[:0]
|
|
||||||
|
|
||||||
if m.udpTracker != nil {
|
|
||||||
m.udpTracker.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
if m.icmpTracker != nil {
|
|
||||||
m.icmpTracker.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
if m.tcpTracker != nil {
|
|
||||||
m.tcpTracker.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
if fwder := m.forwarder.Load(); fwder != nil {
|
|
||||||
fwder.Stop()
|
|
||||||
}
|
|
||||||
|
|
||||||
if m.logger != nil {
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
if err := m.logger.Stop(ctx); err != nil {
|
|
||||||
log.Errorf("failed to shutdown logger: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetupEBPFProxyNoTrack creates notrack rules for eBPF proxy loopback traffic.
|
// SetupEBPFProxyNoTrack creates notrack rules for eBPF proxy loopback traffic.
|
||||||
func (m *Manager) SetupEBPFProxyNoTrack(proxyPort, wgPort uint16) error {
|
func (m *Manager) SetupEBPFProxyNoTrack(proxyPort, wgPort uint16) error {
|
||||||
if m.nativeFirewall == nil {
|
if m.nativeFirewall == nil {
|
||||||
|
|||||||
@@ -1,376 +0,0 @@
|
|||||||
package uspfilter
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/netip"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/golang/mock/gomock"
|
|
||||||
"github.com/google/gopacket/layers"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
wgdevice "golang.zx2c4.com/wireguard/device"
|
|
||||||
|
|
||||||
fw "github.com/netbirdio/netbird/client/firewall/manager"
|
|
||||||
"github.com/netbirdio/netbird/client/iface"
|
|
||||||
"github.com/netbirdio/netbird/client/iface/device"
|
|
||||||
"github.com/netbirdio/netbird/client/iface/mocks"
|
|
||||||
"github.com/netbirdio/netbird/client/iface/wgaddr"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TestAddRouteFilteringReturnsExistingRule verifies that adding the same route
|
|
||||||
// filtering rule twice returns the same rule ID (idempotent behavior).
|
|
||||||
func TestAddRouteFilteringReturnsExistingRule(t *testing.T) {
|
|
||||||
manager := setupTestManager(t)
|
|
||||||
|
|
||||||
sources := []netip.Prefix{
|
|
||||||
netip.MustParsePrefix("100.64.1.0/24"),
|
|
||||||
netip.MustParsePrefix("100.64.2.0/24"),
|
|
||||||
}
|
|
||||||
destination := fw.Network{Prefix: netip.MustParsePrefix("192.168.1.0/24")}
|
|
||||||
|
|
||||||
// Add rule first time
|
|
||||||
rule1, err := manager.AddRouteFiltering(
|
|
||||||
[]byte("policy-1"),
|
|
||||||
sources,
|
|
||||||
destination,
|
|
||||||
fw.ProtocolTCP,
|
|
||||||
nil,
|
|
||||||
&fw.Port{Values: []uint16{443}},
|
|
||||||
fw.ActionAccept,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotNil(t, rule1)
|
|
||||||
|
|
||||||
// Add the same rule again
|
|
||||||
rule2, err := manager.AddRouteFiltering(
|
|
||||||
[]byte("policy-1"),
|
|
||||||
sources,
|
|
||||||
destination,
|
|
||||||
fw.ProtocolTCP,
|
|
||||||
nil,
|
|
||||||
&fw.Port{Values: []uint16{443}},
|
|
||||||
fw.ActionAccept,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotNil(t, rule2)
|
|
||||||
|
|
||||||
// These should be the same (idempotent) like nftables/iptables implementations
|
|
||||||
assert.Equal(t, rule1.ID(), rule2.ID(),
|
|
||||||
"Adding the same rule twice should return the same rule ID (idempotent)")
|
|
||||||
|
|
||||||
manager.mutex.RLock()
|
|
||||||
ruleCount := len(manager.routeRules)
|
|
||||||
manager.mutex.RUnlock()
|
|
||||||
|
|
||||||
assert.Equal(t, 2, ruleCount,
|
|
||||||
"Should have exactly 2 rules (1 user rule + 1 block rule)")
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestAddRouteFilteringDifferentRulesGetDifferentIDs verifies that rules with
|
|
||||||
// different parameters get distinct IDs.
|
|
||||||
func TestAddRouteFilteringDifferentRulesGetDifferentIDs(t *testing.T) {
|
|
||||||
manager := setupTestManager(t)
|
|
||||||
|
|
||||||
sources := []netip.Prefix{netip.MustParsePrefix("100.64.1.0/24")}
|
|
||||||
|
|
||||||
// Add first rule
|
|
||||||
rule1, err := manager.AddRouteFiltering(
|
|
||||||
[]byte("policy-1"),
|
|
||||||
sources,
|
|
||||||
fw.Network{Prefix: netip.MustParsePrefix("192.168.1.0/24")},
|
|
||||||
fw.ProtocolTCP,
|
|
||||||
nil,
|
|
||||||
&fw.Port{Values: []uint16{443}},
|
|
||||||
fw.ActionAccept,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
// Add different rule (different destination)
|
|
||||||
rule2, err := manager.AddRouteFiltering(
|
|
||||||
[]byte("policy-2"),
|
|
||||||
sources,
|
|
||||||
fw.Network{Prefix: netip.MustParsePrefix("192.168.2.0/24")}, // Different!
|
|
||||||
fw.ProtocolTCP,
|
|
||||||
nil,
|
|
||||||
&fw.Port{Values: []uint16{443}},
|
|
||||||
fw.ActionAccept,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
assert.NotEqual(t, rule1.ID(), rule2.ID(),
|
|
||||||
"Different rules should have different IDs")
|
|
||||||
|
|
||||||
manager.mutex.RLock()
|
|
||||||
ruleCount := len(manager.routeRules)
|
|
||||||
manager.mutex.RUnlock()
|
|
||||||
|
|
||||||
assert.Equal(t, 3, ruleCount, "Should have 3 rules (2 user rules + 1 block rule)")
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestRouteRuleUpdateDoesNotCauseGap verifies that re-adding the same route
|
|
||||||
// rule during a network map update does not disrupt existing traffic.
|
|
||||||
func TestRouteRuleUpdateDoesNotCauseGap(t *testing.T) {
|
|
||||||
manager := setupTestManager(t)
|
|
||||||
|
|
||||||
sources := []netip.Prefix{netip.MustParsePrefix("100.64.1.0/24")}
|
|
||||||
destination := fw.Network{Prefix: netip.MustParsePrefix("192.168.1.0/24")}
|
|
||||||
|
|
||||||
rule1, err := manager.AddRouteFiltering(
|
|
||||||
[]byte("policy-1"),
|
|
||||||
sources,
|
|
||||||
destination,
|
|
||||||
fw.ProtocolTCP,
|
|
||||||
nil,
|
|
||||||
nil,
|
|
||||||
fw.ActionAccept,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
srcIP := netip.MustParseAddr("100.64.1.5")
|
|
||||||
dstIP := netip.MustParseAddr("192.168.1.10")
|
|
||||||
_, pass := manager.routeACLsPass(srcIP, dstIP, layers.LayerTypeTCP, 12345, 443)
|
|
||||||
require.True(t, pass, "Traffic should pass with rule in place")
|
|
||||||
|
|
||||||
// Re-add same rule (simulates network map update)
|
|
||||||
rule2, err := manager.AddRouteFiltering(
|
|
||||||
[]byte("policy-1"),
|
|
||||||
sources,
|
|
||||||
destination,
|
|
||||||
fw.ProtocolTCP,
|
|
||||||
nil,
|
|
||||||
nil,
|
|
||||||
fw.ActionAccept,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
// Idempotent IDs mean rule1.ID() == rule2.ID(), so the ACL manager
|
|
||||||
// won't delete rule1 during cleanup. If IDs differed, deleting rule1
|
|
||||||
// would remove the only matching rule and cause a traffic gap.
|
|
||||||
if rule1.ID() != rule2.ID() {
|
|
||||||
err = manager.DeleteRouteRule(rule1)
|
|
||||||
require.NoError(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, passAfter := manager.routeACLsPass(srcIP, dstIP, layers.LayerTypeTCP, 12345, 443)
|
|
||||||
assert.True(t, passAfter,
|
|
||||||
"Traffic should still pass after rule update - no gap should occur")
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestBlockInvalidRoutedIdempotent verifies that blockInvalidRouted creates
|
|
||||||
// exactly one drop rule for the WireGuard network prefix, and calling it again
|
|
||||||
// returns the same rule without duplicating.
|
|
||||||
func TestBlockInvalidRoutedIdempotent(t *testing.T) {
|
|
||||||
ctrl := gomock.NewController(t)
|
|
||||||
dev := mocks.NewMockDevice(ctrl)
|
|
||||||
dev.EXPECT().MTU().Return(1500, nil).AnyTimes()
|
|
||||||
|
|
||||||
wgNet := netip.MustParsePrefix("100.64.0.1/16")
|
|
||||||
|
|
||||||
ifaceMock := &IFaceMock{
|
|
||||||
SetFilterFunc: func(device.PacketFilter) error { return nil },
|
|
||||||
AddressFunc: func() wgaddr.Address {
|
|
||||||
return wgaddr.Address{
|
|
||||||
IP: wgNet.Addr(),
|
|
||||||
Network: wgNet,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
GetDeviceFunc: func() *device.FilteredDevice {
|
|
||||||
return &device.FilteredDevice{Device: dev}
|
|
||||||
},
|
|
||||||
GetWGDeviceFunc: func() *wgdevice.Device {
|
|
||||||
return &wgdevice.Device{}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
manager, err := Create(ifaceMock, false, flowLogger, iface.DefaultMTU)
|
|
||||||
require.NoError(t, err)
|
|
||||||
t.Cleanup(func() {
|
|
||||||
require.NoError(t, manager.Close(nil))
|
|
||||||
})
|
|
||||||
|
|
||||||
// Call blockInvalidRouted directly multiple times
|
|
||||||
rule1, err := manager.blockInvalidRouted(ifaceMock)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotNil(t, rule1)
|
|
||||||
|
|
||||||
rule2, err := manager.blockInvalidRouted(ifaceMock)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotNil(t, rule2)
|
|
||||||
|
|
||||||
rule3, err := manager.blockInvalidRouted(ifaceMock)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotNil(t, rule3)
|
|
||||||
|
|
||||||
// All should return the same rule
|
|
||||||
assert.Equal(t, rule1.ID(), rule2.ID(), "Second call should return same rule")
|
|
||||||
assert.Equal(t, rule2.ID(), rule3.ID(), "Third call should return same rule")
|
|
||||||
|
|
||||||
// Should have exactly 1 route rule
|
|
||||||
manager.mutex.RLock()
|
|
||||||
ruleCount := len(manager.routeRules)
|
|
||||||
manager.mutex.RUnlock()
|
|
||||||
|
|
||||||
assert.Equal(t, 1, ruleCount, "Should have exactly 1 block rule after 3 calls")
|
|
||||||
|
|
||||||
// Verify the rule blocks traffic to the WG network
|
|
||||||
srcIP := netip.MustParseAddr("10.0.0.1")
|
|
||||||
dstIP := netip.MustParseAddr("100.64.0.50")
|
|
||||||
_, pass := manager.routeACLsPass(srcIP, dstIP, layers.LayerTypeTCP, 12345, 80)
|
|
||||||
assert.False(t, pass, "Block rule should deny traffic to WG prefix")
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestBlockRuleNotAccumulatedOnRepeatedEnableRouting verifies that calling
|
|
||||||
// EnableRouting multiple times (as happens on each route update) does not
|
|
||||||
// accumulate duplicate block rules in the routeRules slice.
|
|
||||||
func TestBlockRuleNotAccumulatedOnRepeatedEnableRouting(t *testing.T) {
|
|
||||||
ctrl := gomock.NewController(t)
|
|
||||||
dev := mocks.NewMockDevice(ctrl)
|
|
||||||
dev.EXPECT().MTU().Return(1500, nil).AnyTimes()
|
|
||||||
|
|
||||||
wgNet := netip.MustParsePrefix("100.64.0.1/16")
|
|
||||||
|
|
||||||
ifaceMock := &IFaceMock{
|
|
||||||
SetFilterFunc: func(device.PacketFilter) error { return nil },
|
|
||||||
AddressFunc: func() wgaddr.Address {
|
|
||||||
return wgaddr.Address{
|
|
||||||
IP: wgNet.Addr(),
|
|
||||||
Network: wgNet,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
GetDeviceFunc: func() *device.FilteredDevice {
|
|
||||||
return &device.FilteredDevice{Device: dev}
|
|
||||||
},
|
|
||||||
GetWGDeviceFunc: func() *wgdevice.Device {
|
|
||||||
return &wgdevice.Device{}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
manager, err := Create(ifaceMock, false, flowLogger, iface.DefaultMTU)
|
|
||||||
require.NoError(t, err)
|
|
||||||
t.Cleanup(func() {
|
|
||||||
require.NoError(t, manager.Close(nil))
|
|
||||||
})
|
|
||||||
|
|
||||||
// Call EnableRouting multiple times (simulating repeated route updates)
|
|
||||||
for i := 0; i < 5; i++ {
|
|
||||||
require.NoError(t, manager.EnableRouting())
|
|
||||||
}
|
|
||||||
|
|
||||||
manager.mutex.RLock()
|
|
||||||
ruleCount := len(manager.routeRules)
|
|
||||||
manager.mutex.RUnlock()
|
|
||||||
|
|
||||||
assert.Equal(t, 1, ruleCount,
|
|
||||||
"Repeated EnableRouting should not accumulate block rules")
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestRouteRuleCountStableAcrossUpdates verifies that adding the same route
|
|
||||||
// rule multiple times does not create duplicate entries.
|
|
||||||
func TestRouteRuleCountStableAcrossUpdates(t *testing.T) {
|
|
||||||
manager := setupTestManager(t)
|
|
||||||
|
|
||||||
sources := []netip.Prefix{netip.MustParsePrefix("100.64.1.0/24")}
|
|
||||||
destination := fw.Network{Prefix: netip.MustParsePrefix("192.168.1.0/24")}
|
|
||||||
|
|
||||||
// Simulate 5 network map updates with the same route rule
|
|
||||||
for i := 0; i < 5; i++ {
|
|
||||||
rule, err := manager.AddRouteFiltering(
|
|
||||||
[]byte("policy-1"),
|
|
||||||
sources,
|
|
||||||
destination,
|
|
||||||
fw.ProtocolTCP,
|
|
||||||
nil,
|
|
||||||
&fw.Port{Values: []uint16{443}},
|
|
||||||
fw.ActionAccept,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NotNil(t, rule)
|
|
||||||
}
|
|
||||||
|
|
||||||
manager.mutex.RLock()
|
|
||||||
ruleCount := len(manager.routeRules)
|
|
||||||
manager.mutex.RUnlock()
|
|
||||||
|
|
||||||
assert.Equal(t, 2, ruleCount,
|
|
||||||
"Should have exactly 2 rules (1 user rule + 1 block rule) after 5 updates")
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestDeleteRouteRuleAfterIdempotentAdd verifies that deleting a route rule
|
|
||||||
// after adding it multiple times works correctly.
|
|
||||||
func TestDeleteRouteRuleAfterIdempotentAdd(t *testing.T) {
|
|
||||||
manager := setupTestManager(t)
|
|
||||||
|
|
||||||
sources := []netip.Prefix{netip.MustParsePrefix("100.64.1.0/24")}
|
|
||||||
destination := fw.Network{Prefix: netip.MustParsePrefix("192.168.1.0/24")}
|
|
||||||
|
|
||||||
// Add same rule twice
|
|
||||||
rule1, err := manager.AddRouteFiltering(
|
|
||||||
[]byte("policy-1"),
|
|
||||||
sources,
|
|
||||||
destination,
|
|
||||||
fw.ProtocolTCP,
|
|
||||||
nil,
|
|
||||||
nil,
|
|
||||||
fw.ActionAccept,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
rule2, err := manager.AddRouteFiltering(
|
|
||||||
[]byte("policy-1"),
|
|
||||||
sources,
|
|
||||||
destination,
|
|
||||||
fw.ProtocolTCP,
|
|
||||||
nil,
|
|
||||||
nil,
|
|
||||||
fw.ActionAccept,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
require.Equal(t, rule1.ID(), rule2.ID(), "Should return same rule ID")
|
|
||||||
|
|
||||||
// Delete using first reference
|
|
||||||
err = manager.DeleteRouteRule(rule1)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
// Verify traffic no longer passes
|
|
||||||
srcIP := netip.MustParseAddr("100.64.1.5")
|
|
||||||
dstIP := netip.MustParseAddr("192.168.1.10")
|
|
||||||
_, pass := manager.routeACLsPass(srcIP, dstIP, layers.LayerTypeTCP, 12345, 443)
|
|
||||||
assert.False(t, pass, "Traffic should not pass after rule deletion")
|
|
||||||
}
|
|
||||||
|
|
||||||
func setupTestManager(t *testing.T) *Manager {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
ctrl := gomock.NewController(t)
|
|
||||||
dev := mocks.NewMockDevice(ctrl)
|
|
||||||
dev.EXPECT().MTU().Return(1500, nil).AnyTimes()
|
|
||||||
|
|
||||||
wgNet := netip.MustParsePrefix("100.64.0.1/16")
|
|
||||||
|
|
||||||
ifaceMock := &IFaceMock{
|
|
||||||
SetFilterFunc: func(device.PacketFilter) error { return nil },
|
|
||||||
AddressFunc: func() wgaddr.Address {
|
|
||||||
return wgaddr.Address{
|
|
||||||
IP: wgNet.Addr(),
|
|
||||||
Network: wgNet,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
GetDeviceFunc: func() *device.FilteredDevice {
|
|
||||||
return &device.FilteredDevice{Device: dev}
|
|
||||||
},
|
|
||||||
GetWGDeviceFunc: func() *wgdevice.Device {
|
|
||||||
return &wgdevice.Device{}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
manager, err := Create(ifaceMock, false, flowLogger, iface.DefaultMTU)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NoError(t, manager.EnableRouting())
|
|
||||||
|
|
||||||
t.Cleanup(func() {
|
|
||||||
require.NoError(t, manager.Close(nil))
|
|
||||||
})
|
|
||||||
|
|
||||||
return manager
|
|
||||||
}
|
|
||||||
@@ -263,158 +263,6 @@ func TestAddUDPPacketHook(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestPeerRuleLifecycleDenyRules verifies that deny rules are correctly added
|
|
||||||
// to the deny map and can be cleanly deleted without leaving orphans.
|
|
||||||
func TestPeerRuleLifecycleDenyRules(t *testing.T) {
|
|
||||||
ifaceMock := &IFaceMock{
|
|
||||||
SetFilterFunc: func(device.PacketFilter) error { return nil },
|
|
||||||
}
|
|
||||||
|
|
||||||
m, err := Create(ifaceMock, false, flowLogger, nbiface.DefaultMTU)
|
|
||||||
require.NoError(t, err)
|
|
||||||
defer func() {
|
|
||||||
require.NoError(t, m.Close(nil))
|
|
||||||
}()
|
|
||||||
|
|
||||||
ip := net.ParseIP("192.168.1.1")
|
|
||||||
addr := netip.MustParseAddr("192.168.1.1")
|
|
||||||
|
|
||||||
// Add multiple deny rules for different ports
|
|
||||||
rule1, err := m.AddPeerFiltering(nil, ip, fw.ProtocolTCP, nil,
|
|
||||||
&fw.Port{Values: []uint16{22}}, fw.ActionDrop, "")
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
rule2, err := m.AddPeerFiltering(nil, ip, fw.ProtocolTCP, nil,
|
|
||||||
&fw.Port{Values: []uint16{80}}, fw.ActionDrop, "")
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
m.mutex.RLock()
|
|
||||||
denyCount := len(m.incomingDenyRules[addr])
|
|
||||||
m.mutex.RUnlock()
|
|
||||||
require.Equal(t, 2, denyCount, "Should have exactly 2 deny rules")
|
|
||||||
|
|
||||||
// Delete the first deny rule
|
|
||||||
err = m.DeletePeerRule(rule1[0])
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
m.mutex.RLock()
|
|
||||||
denyCount = len(m.incomingDenyRules[addr])
|
|
||||||
m.mutex.RUnlock()
|
|
||||||
require.Equal(t, 1, denyCount, "Should have 1 deny rule after deleting first")
|
|
||||||
|
|
||||||
// Delete the second deny rule
|
|
||||||
err = m.DeletePeerRule(rule2[0])
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
m.mutex.RLock()
|
|
||||||
_, exists := m.incomingDenyRules[addr]
|
|
||||||
m.mutex.RUnlock()
|
|
||||||
require.False(t, exists, "Deny rules IP entry should be cleaned up when empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestPeerRuleAddAndDeleteDontLeak verifies that repeatedly adding and deleting
|
|
||||||
// peer rules (simulating network map updates) does not leak rules in the maps.
|
|
||||||
func TestPeerRuleAddAndDeleteDontLeak(t *testing.T) {
|
|
||||||
ifaceMock := &IFaceMock{
|
|
||||||
SetFilterFunc: func(device.PacketFilter) error { return nil },
|
|
||||||
}
|
|
||||||
|
|
||||||
m, err := Create(ifaceMock, false, flowLogger, nbiface.DefaultMTU)
|
|
||||||
require.NoError(t, err)
|
|
||||||
defer func() {
|
|
||||||
require.NoError(t, m.Close(nil))
|
|
||||||
}()
|
|
||||||
|
|
||||||
ip := net.ParseIP("192.168.1.1")
|
|
||||||
addr := netip.MustParseAddr("192.168.1.1")
|
|
||||||
|
|
||||||
// Simulate 10 network map updates: add rule, delete old, add new
|
|
||||||
for i := 0; i < 10; i++ {
|
|
||||||
// Add a deny rule
|
|
||||||
rules, err := m.AddPeerFiltering(nil, ip, fw.ProtocolTCP, nil,
|
|
||||||
&fw.Port{Values: []uint16{22}}, fw.ActionDrop, "")
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
// Add an allow rule
|
|
||||||
allowRules, err := m.AddPeerFiltering(nil, ip, fw.ProtocolTCP, nil,
|
|
||||||
&fw.Port{Values: []uint16{80}}, fw.ActionAccept, "")
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
// Delete them (simulating ACL manager cleanup)
|
|
||||||
for _, r := range rules {
|
|
||||||
require.NoError(t, m.DeletePeerRule(r))
|
|
||||||
}
|
|
||||||
for _, r := range allowRules {
|
|
||||||
require.NoError(t, m.DeletePeerRule(r))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
m.mutex.RLock()
|
|
||||||
denyCount := len(m.incomingDenyRules[addr])
|
|
||||||
allowCount := len(m.incomingRules[addr])
|
|
||||||
m.mutex.RUnlock()
|
|
||||||
|
|
||||||
require.Equal(t, 0, denyCount, "No deny rules should remain after cleanup")
|
|
||||||
require.Equal(t, 0, allowCount, "No allow rules should remain after cleanup")
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestMixedAllowDenyRulesSameIP verifies that allow and deny rules for the same
|
|
||||||
// IP are stored in separate maps and don't interfere with each other.
|
|
||||||
func TestMixedAllowDenyRulesSameIP(t *testing.T) {
|
|
||||||
ifaceMock := &IFaceMock{
|
|
||||||
SetFilterFunc: func(device.PacketFilter) error { return nil },
|
|
||||||
}
|
|
||||||
|
|
||||||
m, err := Create(ifaceMock, false, flowLogger, nbiface.DefaultMTU)
|
|
||||||
require.NoError(t, err)
|
|
||||||
defer func() {
|
|
||||||
require.NoError(t, m.Close(nil))
|
|
||||||
}()
|
|
||||||
|
|
||||||
ip := net.ParseIP("192.168.1.1")
|
|
||||||
|
|
||||||
// Add allow rule for port 80
|
|
||||||
allowRule, err := m.AddPeerFiltering(nil, ip, fw.ProtocolTCP, nil,
|
|
||||||
&fw.Port{Values: []uint16{80}}, fw.ActionAccept, "")
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
// Add deny rule for port 22
|
|
||||||
denyRule, err := m.AddPeerFiltering(nil, ip, fw.ProtocolTCP, nil,
|
|
||||||
&fw.Port{Values: []uint16{22}}, fw.ActionDrop, "")
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
addr := netip.MustParseAddr("192.168.1.1")
|
|
||||||
m.mutex.RLock()
|
|
||||||
allowCount := len(m.incomingRules[addr])
|
|
||||||
denyCount := len(m.incomingDenyRules[addr])
|
|
||||||
m.mutex.RUnlock()
|
|
||||||
|
|
||||||
require.Equal(t, 1, allowCount, "Should have 1 allow rule")
|
|
||||||
require.Equal(t, 1, denyCount, "Should have 1 deny rule")
|
|
||||||
|
|
||||||
// Delete allow rule should not affect deny rule
|
|
||||||
err = m.DeletePeerRule(allowRule[0])
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
m.mutex.RLock()
|
|
||||||
denyCountAfter := len(m.incomingDenyRules[addr])
|
|
||||||
m.mutex.RUnlock()
|
|
||||||
|
|
||||||
require.Equal(t, 1, denyCountAfter, "Deny rule should still exist after deleting allow rule")
|
|
||||||
|
|
||||||
// Delete deny rule
|
|
||||||
err = m.DeletePeerRule(denyRule[0])
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
m.mutex.RLock()
|
|
||||||
_, denyExists := m.incomingDenyRules[addr]
|
|
||||||
_, allowExists := m.incomingRules[addr]
|
|
||||||
m.mutex.RUnlock()
|
|
||||||
|
|
||||||
require.False(t, denyExists, "Deny rules should be empty")
|
|
||||||
require.False(t, allowExists, "Allow rules should be empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestManagerReset(t *testing.T) {
|
func TestManagerReset(t *testing.T) {
|
||||||
ifaceMock := &IFaceMock{
|
ifaceMock := &IFaceMock{
|
||||||
SetFilterFunc: func(device.PacketFilter) error { return nil },
|
SetFilterFunc: func(device.PacketFilter) error { return nil },
|
||||||
|
|||||||
@@ -5,8 +5,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
@@ -18,18 +16,9 @@ const (
|
|||||||
maxBatchSize = 1024 * 16
|
maxBatchSize = 1024 * 16
|
||||||
maxMessageSize = 1024 * 2
|
maxMessageSize = 1024 * 2
|
||||||
defaultFlushInterval = 2 * time.Second
|
defaultFlushInterval = 2 * time.Second
|
||||||
defaultLogChanSize = 1000
|
logChannelSize = 1000
|
||||||
)
|
)
|
||||||
|
|
||||||
func getLogChannelSize() int {
|
|
||||||
if v := os.Getenv("NB_USPFILTER_LOG_BUFFER"); v != "" {
|
|
||||||
if n, err := strconv.Atoi(v); err == nil && n > 0 {
|
|
||||||
return n
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return defaultLogChanSize
|
|
||||||
}
|
|
||||||
|
|
||||||
type Level uint32
|
type Level uint32
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -80,7 +69,7 @@ type Logger struct {
|
|||||||
func NewFromLogrus(logrusLogger *log.Logger) *Logger {
|
func NewFromLogrus(logrusLogger *log.Logger) *Logger {
|
||||||
l := &Logger{
|
l := &Logger{
|
||||||
output: logrusLogger.Out,
|
output: logrusLogger.Out,
|
||||||
msgChannel: make(chan logMessage, getLogChannelSize()),
|
msgChannel: make(chan logMessage, logChannelSize),
|
||||||
shutdown: make(chan struct{}),
|
shutdown: make(chan struct{}),
|
||||||
bufPool: sync.Pool{
|
bufPool: sync.Pool{
|
||||||
New: func() any {
|
New: func() any {
|
||||||
|
|||||||
@@ -358,9 +358,9 @@ func incrementalUpdate(oldChecksum uint16, oldBytes, newBytes []byte) uint16 {
|
|||||||
// Fast path for IPv4 addresses (4 bytes) - most common case
|
// Fast path for IPv4 addresses (4 bytes) - most common case
|
||||||
if len(oldBytes) == 4 && len(newBytes) == 4 {
|
if len(oldBytes) == 4 && len(newBytes) == 4 {
|
||||||
sum += uint32(^binary.BigEndian.Uint16(oldBytes[0:2]))
|
sum += uint32(^binary.BigEndian.Uint16(oldBytes[0:2]))
|
||||||
sum += uint32(^binary.BigEndian.Uint16(oldBytes[2:4])) //nolint:gosec // length checked above
|
sum += uint32(^binary.BigEndian.Uint16(oldBytes[2:4]))
|
||||||
sum += uint32(binary.BigEndian.Uint16(newBytes[0:2]))
|
sum += uint32(binary.BigEndian.Uint16(newBytes[0:2]))
|
||||||
sum += uint32(binary.BigEndian.Uint16(newBytes[2:4])) //nolint:gosec // length checked above
|
sum += uint32(binary.BigEndian.Uint16(newBytes[2:4]))
|
||||||
} else {
|
} else {
|
||||||
// Fallback for other lengths
|
// Fallback for other lengths
|
||||||
for i := 0; i < len(oldBytes)-1; i += 2 {
|
for i := 0; i < len(oldBytes)-1; i += 2 {
|
||||||
|
|||||||
@@ -5,18 +5,20 @@ package configurer
|
|||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
"golang.zx2c4.com/wireguard/ipc"
|
"golang.zx2c4.com/wireguard/ipc"
|
||||||
)
|
)
|
||||||
|
|
||||||
func openUAPI(deviceName string) (net.Listener, error) {
|
func openUAPI(deviceName string) (net.Listener, error) {
|
||||||
uapiSock, err := ipc.UAPIOpen(deviceName)
|
uapiSock, err := ipc.UAPIOpen(deviceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Errorf("failed to open uapi socket: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
listener, err := ipc.UAPIListen(deviceName, uapiSock)
|
listener, err := ipc.UAPIListen(deviceName, uapiSock)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = uapiSock.Close()
|
log.Errorf("failed to listen on uapi socket: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -54,14 +54,6 @@ func NewUSPConfigurer(device *device.Device, deviceName string, activityRecorder
|
|||||||
return wgCfg
|
return wgCfg
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUSPConfigurerNoUAPI(device *device.Device, deviceName string, activityRecorder *bind.ActivityRecorder) *WGUSPConfigurer {
|
|
||||||
return &WGUSPConfigurer{
|
|
||||||
device: device,
|
|
||||||
deviceName: deviceName,
|
|
||||||
activityRecorder: activityRecorder,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *WGUSPConfigurer) ConfigureInterface(privateKey string, port int) error {
|
func (c *WGUSPConfigurer) ConfigureInterface(privateKey string, port int) error {
|
||||||
log.Debugf("adding Wireguard private key")
|
log.Debugf("adding Wireguard private key")
|
||||||
key, err := wgtypes.ParseKey(privateKey)
|
key, err := wgtypes.ParseKey(privateKey)
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ type FilteredDevice struct {
|
|||||||
|
|
||||||
filter PacketFilter
|
filter PacketFilter
|
||||||
mutex sync.RWMutex
|
mutex sync.RWMutex
|
||||||
closeOnce sync.Once
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// newDeviceFilter constructor function
|
// newDeviceFilter constructor function
|
||||||
@@ -41,20 +40,6 @@ func newDeviceFilter(device tun.Device) *FilteredDevice {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close closes the underlying tun device exactly once.
|
|
||||||
// wireguard-go's netTun.Close() panics on double-close due to a bare close(channel),
|
|
||||||
// and multiple code paths can trigger Close on the same device.
|
|
||||||
func (d *FilteredDevice) Close() error {
|
|
||||||
var err error
|
|
||||||
d.closeOnce.Do(func() {
|
|
||||||
err = d.Device.Close()
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read wraps read method with filtering feature
|
// Read wraps read method with filtering feature
|
||||||
func (d *FilteredDevice) Read(bufs [][]byte, sizes []int, offset int) (n int, err error) {
|
func (d *FilteredDevice) Read(bufs [][]byte, sizes []int, offset int) (n int, err error) {
|
||||||
if n, err = d.Device.Read(bufs, sizes, offset); err != nil {
|
if n, err = d.Device.Read(bufs, sizes, offset); err != nil {
|
||||||
|
|||||||
@@ -79,12 +79,10 @@ func (t *TunNetstackDevice) create() (WGConfigurer, error) {
|
|||||||
device.NewLogger(wgLogLevel(), "[netbird] "),
|
device.NewLogger(wgLogLevel(), "[netbird] "),
|
||||||
)
|
)
|
||||||
|
|
||||||
t.configurer = configurer.NewUSPConfigurerNoUAPI(t.device, t.name, t.bind.ActivityRecorder())
|
t.configurer = configurer.NewUSPConfigurer(t.device, t.name, t.bind.ActivityRecorder())
|
||||||
err = t.configurer.ConfigureInterface(t.key, t.port)
|
err = t.configurer.ConfigureInterface(t.key, t.port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if cErr := tunIface.Close(); cErr != nil {
|
_ = tunIface.Close()
|
||||||
log.Debugf("failed to close tun device: %v", cErr)
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("error configuring interface: %s", err)
|
return nil, fmt.Errorf("error configuring interface: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ import (
|
|||||||
"github.com/netbirdio/netbird/client/errors"
|
"github.com/netbirdio/netbird/client/errors"
|
||||||
"github.com/netbirdio/netbird/client/iface/configurer"
|
"github.com/netbirdio/netbird/client/iface/configurer"
|
||||||
"github.com/netbirdio/netbird/client/iface/device"
|
"github.com/netbirdio/netbird/client/iface/device"
|
||||||
nbnetstack "github.com/netbirdio/netbird/client/iface/netstack"
|
|
||||||
"github.com/netbirdio/netbird/client/iface/udpmux"
|
"github.com/netbirdio/netbird/client/iface/udpmux"
|
||||||
"github.com/netbirdio/netbird/client/iface/wgaddr"
|
"github.com/netbirdio/netbird/client/iface/wgaddr"
|
||||||
"github.com/netbirdio/netbird/client/iface/wgproxy"
|
"github.com/netbirdio/netbird/client/iface/wgproxy"
|
||||||
@@ -229,10 +228,6 @@ func (w *WGIface) Close() error {
|
|||||||
result = multierror.Append(result, fmt.Errorf("failed to close wireguard interface %s: %w", w.Name(), err))
|
result = multierror.Append(result, fmt.Errorf("failed to close wireguard interface %s: %w", w.Name(), err))
|
||||||
}
|
}
|
||||||
|
|
||||||
if nbnetstack.IsEnabled() {
|
|
||||||
return errors.FormatErrorOrNil(result)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := w.waitUntilRemoved(); err != nil {
|
if err := w.waitUntilRemoved(); err != nil {
|
||||||
log.Warnf("failed to remove WireGuard interface %s: %v", w.Name(), err)
|
log.Warnf("failed to remove WireGuard interface %s: %v", w.Name(), err)
|
||||||
if err := w.Destroy(); err != nil {
|
if err := w.Destroy(); err != nil {
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ func (t *NetStackTun) Create() (tun.Device, *netstack.Net, error) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return t.tundev, tunNet, nil
|
return nsTunDev, tunNet, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *NetStackTun) Close() error {
|
func (t *NetStackTun) Close() error {
|
||||||
|
|||||||
@@ -189,212 +189,6 @@ func TestDefaultManagerStateless(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestDenyRulesNotAccumulatedOnRepeatedApply verifies that applying the same
|
|
||||||
// deny rules repeatedly does not accumulate duplicate rules in the uspfilter.
|
|
||||||
// This tests the full ACL manager -> uspfilter integration.
|
|
||||||
func TestDenyRulesNotAccumulatedOnRepeatedApply(t *testing.T) {
|
|
||||||
t.Setenv("NB_WG_KERNEL_DISABLED", "true")
|
|
||||||
|
|
||||||
networkMap := &mgmProto.NetworkMap{
|
|
||||||
FirewallRules: []*mgmProto.FirewallRule{
|
|
||||||
{
|
|
||||||
PeerIP: "10.93.0.1",
|
|
||||||
Direction: mgmProto.RuleDirection_IN,
|
|
||||||
Action: mgmProto.RuleAction_DROP,
|
|
||||||
Protocol: mgmProto.RuleProtocol_TCP,
|
|
||||||
Port: "22",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
PeerIP: "10.93.0.2",
|
|
||||||
Direction: mgmProto.RuleDirection_IN,
|
|
||||||
Action: mgmProto.RuleAction_DROP,
|
|
||||||
Protocol: mgmProto.RuleProtocol_TCP,
|
|
||||||
Port: "80",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
PeerIP: "10.93.0.3",
|
|
||||||
Direction: mgmProto.RuleDirection_IN,
|
|
||||||
Action: mgmProto.RuleAction_ACCEPT,
|
|
||||||
Protocol: mgmProto.RuleProtocol_TCP,
|
|
||||||
Port: "443",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
FirewallRulesIsEmpty: false,
|
|
||||||
}
|
|
||||||
|
|
||||||
ctrl := gomock.NewController(t)
|
|
||||||
defer ctrl.Finish()
|
|
||||||
|
|
||||||
ifaceMock := mocks.NewMockIFaceMapper(ctrl)
|
|
||||||
ifaceMock.EXPECT().IsUserspaceBind().Return(true).AnyTimes()
|
|
||||||
ifaceMock.EXPECT().SetFilter(gomock.Any())
|
|
||||||
network := netip.MustParsePrefix("172.0.0.1/32")
|
|
||||||
ifaceMock.EXPECT().Name().Return("lo").AnyTimes()
|
|
||||||
ifaceMock.EXPECT().Address().Return(wgaddr.Address{
|
|
||||||
IP: network.Addr(),
|
|
||||||
Network: network,
|
|
||||||
}).AnyTimes()
|
|
||||||
ifaceMock.EXPECT().GetWGDevice().Return(nil).AnyTimes()
|
|
||||||
|
|
||||||
fw, err := firewall.NewFirewall(ifaceMock, nil, flowLogger, false, iface.DefaultMTU)
|
|
||||||
require.NoError(t, err)
|
|
||||||
defer func() {
|
|
||||||
require.NoError(t, fw.Close(nil))
|
|
||||||
}()
|
|
||||||
|
|
||||||
acl := NewDefaultManager(fw)
|
|
||||||
|
|
||||||
// Apply the same rules 5 times (simulating repeated network map updates)
|
|
||||||
for i := 0; i < 5; i++ {
|
|
||||||
acl.ApplyFiltering(networkMap, false)
|
|
||||||
}
|
|
||||||
|
|
||||||
// The ACL manager should track exactly 3 rule pairs (2 deny + 1 accept inbound)
|
|
||||||
assert.Equal(t, 3, len(acl.peerRulesPairs),
|
|
||||||
"Should have exactly 3 rule pairs after 5 identical updates")
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestDenyRulesCleanedUpOnRemoval verifies that deny rules are properly cleaned
|
|
||||||
// up when they're removed from the network map in a subsequent update.
|
|
||||||
func TestDenyRulesCleanedUpOnRemoval(t *testing.T) {
|
|
||||||
t.Setenv("NB_WG_KERNEL_DISABLED", "true")
|
|
||||||
|
|
||||||
ctrl := gomock.NewController(t)
|
|
||||||
defer ctrl.Finish()
|
|
||||||
|
|
||||||
ifaceMock := mocks.NewMockIFaceMapper(ctrl)
|
|
||||||
ifaceMock.EXPECT().IsUserspaceBind().Return(true).AnyTimes()
|
|
||||||
ifaceMock.EXPECT().SetFilter(gomock.Any())
|
|
||||||
network := netip.MustParsePrefix("172.0.0.1/32")
|
|
||||||
ifaceMock.EXPECT().Name().Return("lo").AnyTimes()
|
|
||||||
ifaceMock.EXPECT().Address().Return(wgaddr.Address{
|
|
||||||
IP: network.Addr(),
|
|
||||||
Network: network,
|
|
||||||
}).AnyTimes()
|
|
||||||
ifaceMock.EXPECT().GetWGDevice().Return(nil).AnyTimes()
|
|
||||||
|
|
||||||
fw, err := firewall.NewFirewall(ifaceMock, nil, flowLogger, false, iface.DefaultMTU)
|
|
||||||
require.NoError(t, err)
|
|
||||||
defer func() {
|
|
||||||
require.NoError(t, fw.Close(nil))
|
|
||||||
}()
|
|
||||||
|
|
||||||
acl := NewDefaultManager(fw)
|
|
||||||
|
|
||||||
// First update: add deny and accept rules
|
|
||||||
networkMap1 := &mgmProto.NetworkMap{
|
|
||||||
FirewallRules: []*mgmProto.FirewallRule{
|
|
||||||
{
|
|
||||||
PeerIP: "10.93.0.1",
|
|
||||||
Direction: mgmProto.RuleDirection_IN,
|
|
||||||
Action: mgmProto.RuleAction_DROP,
|
|
||||||
Protocol: mgmProto.RuleProtocol_TCP,
|
|
||||||
Port: "22",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
PeerIP: "10.93.0.2",
|
|
||||||
Direction: mgmProto.RuleDirection_IN,
|
|
||||||
Action: mgmProto.RuleAction_ACCEPT,
|
|
||||||
Protocol: mgmProto.RuleProtocol_TCP,
|
|
||||||
Port: "443",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
FirewallRulesIsEmpty: false,
|
|
||||||
}
|
|
||||||
|
|
||||||
acl.ApplyFiltering(networkMap1, false)
|
|
||||||
assert.Equal(t, 2, len(acl.peerRulesPairs), "Should have 2 rules after first update")
|
|
||||||
|
|
||||||
// Second update: remove the deny rule, keep only accept
|
|
||||||
networkMap2 := &mgmProto.NetworkMap{
|
|
||||||
FirewallRules: []*mgmProto.FirewallRule{
|
|
||||||
{
|
|
||||||
PeerIP: "10.93.0.2",
|
|
||||||
Direction: mgmProto.RuleDirection_IN,
|
|
||||||
Action: mgmProto.RuleAction_ACCEPT,
|
|
||||||
Protocol: mgmProto.RuleProtocol_TCP,
|
|
||||||
Port: "443",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
FirewallRulesIsEmpty: false,
|
|
||||||
}
|
|
||||||
|
|
||||||
acl.ApplyFiltering(networkMap2, false)
|
|
||||||
assert.Equal(t, 1, len(acl.peerRulesPairs),
|
|
||||||
"Should have 1 rule after removing deny rule")
|
|
||||||
|
|
||||||
// Third update: remove all rules
|
|
||||||
networkMap3 := &mgmProto.NetworkMap{
|
|
||||||
FirewallRules: []*mgmProto.FirewallRule{},
|
|
||||||
FirewallRulesIsEmpty: true,
|
|
||||||
}
|
|
||||||
|
|
||||||
acl.ApplyFiltering(networkMap3, false)
|
|
||||||
assert.Equal(t, 0, len(acl.peerRulesPairs),
|
|
||||||
"Should have 0 rules after removing all rules")
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestRuleUpdateChangingAction verifies that when a rule's action changes from
|
|
||||||
// accept to deny (or vice versa), the old rule is properly removed and the new
|
|
||||||
// one added without leaking.
|
|
||||||
func TestRuleUpdateChangingAction(t *testing.T) {
|
|
||||||
t.Setenv("NB_WG_KERNEL_DISABLED", "true")
|
|
||||||
|
|
||||||
ctrl := gomock.NewController(t)
|
|
||||||
defer ctrl.Finish()
|
|
||||||
|
|
||||||
ifaceMock := mocks.NewMockIFaceMapper(ctrl)
|
|
||||||
ifaceMock.EXPECT().IsUserspaceBind().Return(true).AnyTimes()
|
|
||||||
ifaceMock.EXPECT().SetFilter(gomock.Any())
|
|
||||||
network := netip.MustParsePrefix("172.0.0.1/32")
|
|
||||||
ifaceMock.EXPECT().Name().Return("lo").AnyTimes()
|
|
||||||
ifaceMock.EXPECT().Address().Return(wgaddr.Address{
|
|
||||||
IP: network.Addr(),
|
|
||||||
Network: network,
|
|
||||||
}).AnyTimes()
|
|
||||||
ifaceMock.EXPECT().GetWGDevice().Return(nil).AnyTimes()
|
|
||||||
|
|
||||||
fw, err := firewall.NewFirewall(ifaceMock, nil, flowLogger, false, iface.DefaultMTU)
|
|
||||||
require.NoError(t, err)
|
|
||||||
defer func() {
|
|
||||||
require.NoError(t, fw.Close(nil))
|
|
||||||
}()
|
|
||||||
|
|
||||||
acl := NewDefaultManager(fw)
|
|
||||||
|
|
||||||
// First update: accept rule
|
|
||||||
networkMap := &mgmProto.NetworkMap{
|
|
||||||
FirewallRules: []*mgmProto.FirewallRule{
|
|
||||||
{
|
|
||||||
PeerIP: "10.93.0.1",
|
|
||||||
Direction: mgmProto.RuleDirection_IN,
|
|
||||||
Action: mgmProto.RuleAction_ACCEPT,
|
|
||||||
Protocol: mgmProto.RuleProtocol_TCP,
|
|
||||||
Port: "22",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
FirewallRulesIsEmpty: false,
|
|
||||||
}
|
|
||||||
acl.ApplyFiltering(networkMap, false)
|
|
||||||
assert.Equal(t, 1, len(acl.peerRulesPairs))
|
|
||||||
|
|
||||||
// Second update: change to deny (same IP/port/proto, different action)
|
|
||||||
networkMap.FirewallRules = []*mgmProto.FirewallRule{
|
|
||||||
{
|
|
||||||
PeerIP: "10.93.0.1",
|
|
||||||
Direction: mgmProto.RuleDirection_IN,
|
|
||||||
Action: mgmProto.RuleAction_DROP,
|
|
||||||
Protocol: mgmProto.RuleProtocol_TCP,
|
|
||||||
Port: "22",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
acl.ApplyFiltering(networkMap, false)
|
|
||||||
|
|
||||||
// Should still have exactly 1 rule (the old accept removed, new deny added)
|
|
||||||
assert.Equal(t, 1, len(acl.peerRulesPairs),
|
|
||||||
"Changing action should result in exactly 1 rule, not 2")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPortInfoEmpty(t *testing.T) {
|
func TestPortInfoEmpty(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ import (
|
|||||||
|
|
||||||
"github.com/netbirdio/netbird/client/iface"
|
"github.com/netbirdio/netbird/client/iface"
|
||||||
"github.com/netbirdio/netbird/client/iface/device"
|
"github.com/netbirdio/netbird/client/iface/device"
|
||||||
"github.com/netbirdio/netbird/client/iface/netstack"
|
|
||||||
"github.com/netbirdio/netbird/client/internal/dns"
|
"github.com/netbirdio/netbird/client/internal/dns"
|
||||||
"github.com/netbirdio/netbird/client/internal/listener"
|
"github.com/netbirdio/netbird/client/internal/listener"
|
||||||
"github.com/netbirdio/netbird/client/internal/peer"
|
"github.com/netbirdio/netbird/client/internal/peer"
|
||||||
@@ -245,7 +244,7 @@ func (c *ConnectClient) run(mobileDependency MobileDependency, runningChan chan
|
|||||||
localPeerState := peer.LocalPeerState{
|
localPeerState := peer.LocalPeerState{
|
||||||
IP: loginResp.GetPeerConfig().GetAddress(),
|
IP: loginResp.GetPeerConfig().GetAddress(),
|
||||||
PubKey: myPrivateKey.PublicKey().String(),
|
PubKey: myPrivateKey.PublicKey().String(),
|
||||||
KernelInterface: device.WireGuardModuleIsLoaded() && !netstack.IsEnabled(),
|
KernelInterface: device.WireGuardModuleIsLoaded(),
|
||||||
FQDN: loginResp.GetPeerConfig().GetFqdn(),
|
FQDN: loginResp.GetPeerConfig().GetFqdn(),
|
||||||
}
|
}
|
||||||
c.statusRecorder.UpdateLocalPeerState(localPeerState)
|
c.statusRecorder.UpdateLocalPeerState(localPeerState)
|
||||||
@@ -331,11 +330,8 @@ func (c *ConnectClient) run(mobileDependency MobileDependency, runningChan chan
|
|||||||
state.Set(StatusConnected)
|
state.Set(StatusConnected)
|
||||||
|
|
||||||
if runningChan != nil {
|
if runningChan != nil {
|
||||||
select {
|
|
||||||
case <-runningChan:
|
|
||||||
default:
|
|
||||||
close(runningChan)
|
close(runningChan)
|
||||||
}
|
runningChan = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
<-engineCtx.Done()
|
<-engineCtx.Done()
|
||||||
|
|||||||
@@ -1,60 +0,0 @@
|
|||||||
//go:build !windows && !ios && !android
|
|
||||||
|
|
||||||
package daemonaddr
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
var scanDir = "/var/run/netbird"
|
|
||||||
|
|
||||||
// setScanDir overrides the scan directory (used by tests).
|
|
||||||
func setScanDir(dir string) {
|
|
||||||
scanDir = dir
|
|
||||||
}
|
|
||||||
|
|
||||||
// ResolveUnixDaemonAddr checks whether the default Unix socket exists and, if not,
|
|
||||||
// scans /var/run/netbird/ for a single .sock file to use instead. This handles the
|
|
||||||
// mismatch between the netbird@.service template (which places the socket under
|
|
||||||
// /var/run/netbird/<instance>.sock) and the CLI default (/var/run/netbird.sock).
|
|
||||||
func ResolveUnixDaemonAddr(addr string) string {
|
|
||||||
if !strings.HasPrefix(addr, "unix://") {
|
|
||||||
return addr
|
|
||||||
}
|
|
||||||
|
|
||||||
sockPath := strings.TrimPrefix(addr, "unix://")
|
|
||||||
if _, err := os.Stat(sockPath); err == nil {
|
|
||||||
return addr
|
|
||||||
}
|
|
||||||
|
|
||||||
entries, err := os.ReadDir(scanDir)
|
|
||||||
if err != nil {
|
|
||||||
return addr
|
|
||||||
}
|
|
||||||
|
|
||||||
var found []string
|
|
||||||
for _, e := range entries {
|
|
||||||
if e.IsDir() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if strings.HasSuffix(e.Name(), ".sock") {
|
|
||||||
found = append(found, filepath.Join(scanDir, e.Name()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
switch len(found) {
|
|
||||||
case 1:
|
|
||||||
resolved := "unix://" + found[0]
|
|
||||||
log.Debugf("Default daemon socket not found, using discovered socket: %s", resolved)
|
|
||||||
return resolved
|
|
||||||
case 0:
|
|
||||||
return addr
|
|
||||||
default:
|
|
||||||
log.Warnf("Default daemon socket not found and multiple sockets discovered in %s; pass --daemon-addr explicitly", scanDir)
|
|
||||||
return addr
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
//go:build windows || ios || android
|
|
||||||
|
|
||||||
package daemonaddr
|
|
||||||
|
|
||||||
// ResolveUnixDaemonAddr is a no-op on platforms that don't use Unix sockets.
|
|
||||||
func ResolveUnixDaemonAddr(addr string) string {
|
|
||||||
return addr
|
|
||||||
}
|
|
||||||
@@ -1,121 +0,0 @@
|
|||||||
//go:build !windows && !ios && !android
|
|
||||||
|
|
||||||
package daemonaddr
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
// createSockFile creates a regular file with a .sock extension.
|
|
||||||
// ResolveUnixDaemonAddr uses os.Stat (not net.Dial), so a regular file is
|
|
||||||
// sufficient and avoids Unix socket path-length limits on macOS.
|
|
||||||
func createSockFile(t *testing.T, path string) {
|
|
||||||
t.Helper()
|
|
||||||
if err := os.WriteFile(path, nil, 0o600); err != nil {
|
|
||||||
t.Fatalf("failed to create test sock file at %s: %v", path, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestResolveUnixDaemonAddr_DefaultExists(t *testing.T) {
|
|
||||||
tmp := t.TempDir()
|
|
||||||
sock := filepath.Join(tmp, "netbird.sock")
|
|
||||||
createSockFile(t, sock)
|
|
||||||
|
|
||||||
addr := "unix://" + sock
|
|
||||||
got := ResolveUnixDaemonAddr(addr)
|
|
||||||
if got != addr {
|
|
||||||
t.Errorf("expected %s, got %s", addr, got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestResolveUnixDaemonAddr_SingleDiscovered(t *testing.T) {
|
|
||||||
tmp := t.TempDir()
|
|
||||||
|
|
||||||
// Default socket does not exist
|
|
||||||
defaultAddr := "unix://" + filepath.Join(tmp, "netbird.sock")
|
|
||||||
|
|
||||||
// Create a scan dir with one socket
|
|
||||||
sd := filepath.Join(tmp, "netbird")
|
|
||||||
if err := os.MkdirAll(sd, 0o755); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
instanceSock := filepath.Join(sd, "main.sock")
|
|
||||||
createSockFile(t, instanceSock)
|
|
||||||
|
|
||||||
origScanDir := scanDir
|
|
||||||
setScanDir(sd)
|
|
||||||
t.Cleanup(func() { setScanDir(origScanDir) })
|
|
||||||
|
|
||||||
got := ResolveUnixDaemonAddr(defaultAddr)
|
|
||||||
expected := "unix://" + instanceSock
|
|
||||||
if got != expected {
|
|
||||||
t.Errorf("expected %s, got %s", expected, got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestResolveUnixDaemonAddr_MultipleDiscovered(t *testing.T) {
|
|
||||||
tmp := t.TempDir()
|
|
||||||
|
|
||||||
defaultAddr := "unix://" + filepath.Join(tmp, "netbird.sock")
|
|
||||||
|
|
||||||
sd := filepath.Join(tmp, "netbird")
|
|
||||||
if err := os.MkdirAll(sd, 0o755); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
createSockFile(t, filepath.Join(sd, "main.sock"))
|
|
||||||
createSockFile(t, filepath.Join(sd, "other.sock"))
|
|
||||||
|
|
||||||
origScanDir := scanDir
|
|
||||||
setScanDir(sd)
|
|
||||||
t.Cleanup(func() { setScanDir(origScanDir) })
|
|
||||||
|
|
||||||
got := ResolveUnixDaemonAddr(defaultAddr)
|
|
||||||
if got != defaultAddr {
|
|
||||||
t.Errorf("expected original %s, got %s", defaultAddr, got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestResolveUnixDaemonAddr_NoSocketsFound(t *testing.T) {
|
|
||||||
tmp := t.TempDir()
|
|
||||||
|
|
||||||
defaultAddr := "unix://" + filepath.Join(tmp, "netbird.sock")
|
|
||||||
|
|
||||||
sd := filepath.Join(tmp, "netbird")
|
|
||||||
if err := os.MkdirAll(sd, 0o755); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
origScanDir := scanDir
|
|
||||||
setScanDir(sd)
|
|
||||||
t.Cleanup(func() { setScanDir(origScanDir) })
|
|
||||||
|
|
||||||
got := ResolveUnixDaemonAddr(defaultAddr)
|
|
||||||
if got != defaultAddr {
|
|
||||||
t.Errorf("expected original %s, got %s", defaultAddr, got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestResolveUnixDaemonAddr_NonUnixAddr(t *testing.T) {
|
|
||||||
addr := "tcp://127.0.0.1:41731"
|
|
||||||
got := ResolveUnixDaemonAddr(addr)
|
|
||||||
if got != addr {
|
|
||||||
t.Errorf("expected %s, got %s", addr, got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestResolveUnixDaemonAddr_ScanDirMissing(t *testing.T) {
|
|
||||||
tmp := t.TempDir()
|
|
||||||
|
|
||||||
defaultAddr := "unix://" + filepath.Join(tmp, "netbird.sock")
|
|
||||||
|
|
||||||
origScanDir := scanDir
|
|
||||||
setScanDir(filepath.Join(tmp, "nonexistent"))
|
|
||||||
t.Cleanup(func() { setScanDir(origScanDir) })
|
|
||||||
|
|
||||||
got := ResolveUnixDaemonAddr(defaultAddr)
|
|
||||||
if got != defaultAddr {
|
|
||||||
t.Errorf("expected original %s, got %s", defaultAddr, got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -14,8 +14,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/hashicorp/go-multierror"
|
|
||||||
nberrors "github.com/netbirdio/netbird/client/errors"
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"golang.org/x/exp/maps"
|
"golang.org/x/exp/maps"
|
||||||
|
|
||||||
@@ -24,7 +22,6 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
netbirdDNSStateKeyFormat = "State:/Network/Service/NetBird-%s/DNS"
|
netbirdDNSStateKeyFormat = "State:/Network/Service/NetBird-%s/DNS"
|
||||||
netbirdDNSStateKeyIndexedFormat = "State:/Network/Service/NetBird-%s-%d/DNS"
|
|
||||||
globalIPv4State = "State:/Network/Global/IPv4"
|
globalIPv4State = "State:/Network/Global/IPv4"
|
||||||
primaryServiceStateKeyFormat = "State:/Network/Service/%s/DNS"
|
primaryServiceStateKeyFormat = "State:/Network/Service/%s/DNS"
|
||||||
keySupplementalMatchDomains = "SupplementalMatchDomains"
|
keySupplementalMatchDomains = "SupplementalMatchDomains"
|
||||||
@@ -38,14 +35,6 @@ const (
|
|||||||
searchSuffix = "Search"
|
searchSuffix = "Search"
|
||||||
matchSuffix = "Match"
|
matchSuffix = "Match"
|
||||||
localSuffix = "Local"
|
localSuffix = "Local"
|
||||||
|
|
||||||
// maxDomainsPerResolverEntry is the max number of domains per scutil resolver key.
|
|
||||||
// scutil's d.add has maxArgs=101 (key + * + 99 values), so 99 is the hard cap.
|
|
||||||
maxDomainsPerResolverEntry = 50
|
|
||||||
|
|
||||||
// maxDomainBytesPerResolverEntry is the max total bytes of domain strings per key.
|
|
||||||
// scutil has an undocumented ~2048 byte value buffer; we stay well under it.
|
|
||||||
maxDomainBytesPerResolverEntry = 1500
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type systemConfigurator struct {
|
type systemConfigurator struct {
|
||||||
@@ -95,23 +84,28 @@ func (s *systemConfigurator) applyDNSConfig(config HostDNSConfig, stateManager *
|
|||||||
searchDomains = append(searchDomains, strings.TrimSuffix(""+dConf.Domain, "."))
|
searchDomains = append(searchDomains, strings.TrimSuffix(""+dConf.Domain, "."))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.removeKeysContaining(matchSuffix); err != nil {
|
matchKey := getKeyWithInput(netbirdDNSStateKeyFormat, matchSuffix)
|
||||||
log.Warnf("failed to remove old match keys: %v", err)
|
var err error
|
||||||
}
|
|
||||||
if len(matchDomains) != 0 {
|
if len(matchDomains) != 0 {
|
||||||
if err := s.addBatchedDomains(matchSuffix, matchDomains, config.ServerIP, config.ServerPort, false); err != nil {
|
err = s.addMatchDomains(matchKey, strings.Join(matchDomains, " "), config.ServerIP, config.ServerPort)
|
||||||
return fmt.Errorf("add match domains: %w", err)
|
} else {
|
||||||
|
log.Infof("removing match domains from the system")
|
||||||
|
err = s.removeKeyFromSystemConfig(matchKey)
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("add match domains: %w", err)
|
||||||
}
|
}
|
||||||
s.updateState(stateManager)
|
s.updateState(stateManager)
|
||||||
|
|
||||||
if err := s.removeKeysContaining(searchSuffix); err != nil {
|
searchKey := getKeyWithInput(netbirdDNSStateKeyFormat, searchSuffix)
|
||||||
log.Warnf("failed to remove old search keys: %v", err)
|
|
||||||
}
|
|
||||||
if len(searchDomains) != 0 {
|
if len(searchDomains) != 0 {
|
||||||
if err := s.addBatchedDomains(searchSuffix, searchDomains, config.ServerIP, config.ServerPort, true); err != nil {
|
err = s.addSearchDomains(searchKey, strings.Join(searchDomains, " "), config.ServerIP, config.ServerPort)
|
||||||
return fmt.Errorf("add search domains: %w", err)
|
} else {
|
||||||
|
log.Infof("removing search domains from the system")
|
||||||
|
err = s.removeKeyFromSystemConfig(searchKey)
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("add search domains: %w", err)
|
||||||
}
|
}
|
||||||
s.updateState(stateManager)
|
s.updateState(stateManager)
|
||||||
|
|
||||||
@@ -155,7 +149,8 @@ func (s *systemConfigurator) restoreHostDNS() error {
|
|||||||
|
|
||||||
func (s *systemConfigurator) getRemovableKeysWithDefaults() []string {
|
func (s *systemConfigurator) getRemovableKeysWithDefaults() []string {
|
||||||
if len(s.createdKeys) == 0 {
|
if len(s.createdKeys) == 0 {
|
||||||
return s.discoverExistingKeys()
|
// return defaults for startup calls
|
||||||
|
return []string{getKeyWithInput(netbirdDNSStateKeyFormat, searchSuffix), getKeyWithInput(netbirdDNSStateKeyFormat, matchSuffix)}
|
||||||
}
|
}
|
||||||
|
|
||||||
keys := make([]string, 0, len(s.createdKeys))
|
keys := make([]string, 0, len(s.createdKeys))
|
||||||
@@ -165,47 +160,6 @@ func (s *systemConfigurator) getRemovableKeysWithDefaults() []string {
|
|||||||
return keys
|
return keys
|
||||||
}
|
}
|
||||||
|
|
||||||
// discoverExistingKeys probes scutil for all NetBird DNS keys that may exist.
|
|
||||||
// This handles the case where createdKeys is empty (e.g., state file lost after unclean shutdown).
|
|
||||||
func (s *systemConfigurator) discoverExistingKeys() []string {
|
|
||||||
dnsKeys, err := getSystemDNSKeys()
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("failed to get system DNS keys: %v", err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var keys []string
|
|
||||||
|
|
||||||
for _, suffix := range []string{searchSuffix, matchSuffix, localSuffix} {
|
|
||||||
key := getKeyWithInput(netbirdDNSStateKeyFormat, suffix)
|
|
||||||
if strings.Contains(dnsKeys, key) {
|
|
||||||
keys = append(keys, key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, suffix := range []string{searchSuffix, matchSuffix} {
|
|
||||||
for i := 0; ; i++ {
|
|
||||||
key := fmt.Sprintf(netbirdDNSStateKeyIndexedFormat, suffix, i)
|
|
||||||
if !strings.Contains(dnsKeys, key) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
keys = append(keys, key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return keys
|
|
||||||
}
|
|
||||||
|
|
||||||
// getSystemDNSKeys gets all DNS keys
|
|
||||||
func getSystemDNSKeys() (string, error) {
|
|
||||||
command := "list .*DNS\nquit\n"
|
|
||||||
out, err := runSystemConfigCommand(command)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return string(out), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *systemConfigurator) removeKeyFromSystemConfig(key string) error {
|
func (s *systemConfigurator) removeKeyFromSystemConfig(key string) error {
|
||||||
line := buildRemoveKeyOperation(key)
|
line := buildRemoveKeyOperation(key)
|
||||||
_, err := runSystemConfigCommand(wrapCommand(line))
|
_, err := runSystemConfigCommand(wrapCommand(line))
|
||||||
@@ -230,11 +184,12 @@ func (s *systemConfigurator) addLocalDNS() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
domainsStr := strings.Join(s.systemDNSSettings.Domains, " ")
|
if err := s.addSearchDomains(
|
||||||
if err := s.addDNSState(localKey, domainsStr, s.systemDNSSettings.ServerIP, s.systemDNSSettings.ServerPort, true); err != nil {
|
localKey,
|
||||||
return fmt.Errorf("add local dns state: %w", err)
|
strings.Join(s.systemDNSSettings.Domains, " "), s.systemDNSSettings.ServerIP, s.systemDNSSettings.ServerPort,
|
||||||
|
); err != nil {
|
||||||
|
return fmt.Errorf("add search domains: %w", err)
|
||||||
}
|
}
|
||||||
s.createdKeys[localKey] = struct{}{}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -325,77 +280,28 @@ func (s *systemConfigurator) getOriginalNameservers() []netip.Addr {
|
|||||||
return slices.Clone(s.origNameservers)
|
return slices.Clone(s.origNameservers)
|
||||||
}
|
}
|
||||||
|
|
||||||
// splitDomainsIntoBatches splits domains into batches respecting both element count and byte size limits.
|
func (s *systemConfigurator) addSearchDomains(key, domains string, ip netip.Addr, port int) error {
|
||||||
func splitDomainsIntoBatches(domains []string) [][]string {
|
err := s.addDNSState(key, domains, ip, port, true)
|
||||||
if len(domains) == 0 {
|
if err != nil {
|
||||||
|
return fmt.Errorf("add dns state: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Infof("added %d search domains to the state. Domain list: %s", len(strings.Split(domains, " ")), domains)
|
||||||
|
|
||||||
|
s.createdKeys[key] = struct{}{}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var batches [][]string
|
func (s *systemConfigurator) addMatchDomains(key, domains string, dnsServer netip.Addr, port int) error {
|
||||||
var current []string
|
err := s.addDNSState(key, domains, dnsServer, port, false)
|
||||||
currentBytes := 0
|
if err != nil {
|
||||||
|
return fmt.Errorf("add dns state: %w", err)
|
||||||
for _, d := range domains {
|
|
||||||
domainLen := len(d)
|
|
||||||
newBytes := currentBytes + domainLen
|
|
||||||
if currentBytes > 0 {
|
|
||||||
newBytes++ // space separator
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(current) > 0 && (len(current) >= maxDomainsPerResolverEntry || newBytes > maxDomainBytesPerResolverEntry) {
|
log.Infof("added %d match domains to the state. Domain list: %s", len(strings.Split(domains, " ")), domains)
|
||||||
batches = append(batches, current)
|
|
||||||
current = nil
|
|
||||||
currentBytes = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
current = append(current, d)
|
|
||||||
if currentBytes > 0 {
|
|
||||||
currentBytes += 1 + domainLen
|
|
||||||
} else {
|
|
||||||
currentBytes = domainLen
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(current) > 0 {
|
|
||||||
batches = append(batches, current)
|
|
||||||
}
|
|
||||||
|
|
||||||
return batches
|
|
||||||
}
|
|
||||||
|
|
||||||
// removeKeysContaining removes all created keys that contain the given substring.
|
|
||||||
func (s *systemConfigurator) removeKeysContaining(suffix string) error {
|
|
||||||
var toRemove []string
|
|
||||||
for key := range s.createdKeys {
|
|
||||||
if strings.Contains(key, suffix) {
|
|
||||||
toRemove = append(toRemove, key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var multiErr *multierror.Error
|
|
||||||
for _, key := range toRemove {
|
|
||||||
if err := s.removeKeyFromSystemConfig(key); err != nil {
|
|
||||||
multiErr = multierror.Append(multiErr, fmt.Errorf("couldn't remove key %s: %w", key, err))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nberrors.FormatErrorOrNil(multiErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
// addBatchedDomains splits domains into batches and creates indexed scutil keys for each batch.
|
|
||||||
func (s *systemConfigurator) addBatchedDomains(suffix string, domains []string, ip netip.Addr, port int, enableSearch bool) error {
|
|
||||||
batches := splitDomainsIntoBatches(domains)
|
|
||||||
|
|
||||||
for i, batch := range batches {
|
|
||||||
key := fmt.Sprintf(netbirdDNSStateKeyIndexedFormat, suffix, i)
|
|
||||||
domainsStr := strings.Join(batch, " ")
|
|
||||||
|
|
||||||
if err := s.addDNSState(key, domainsStr, ip, port, enableSearch); err != nil {
|
|
||||||
return fmt.Errorf("add dns state for batch %d: %w", i, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
s.createdKeys[key] = struct{}{}
|
s.createdKeys[key] = struct{}{}
|
||||||
}
|
|
||||||
|
|
||||||
log.Infof("added %d %s domains across %d resolver entries", len(domains), suffix, len(batches))
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -458,6 +364,7 @@ func (s *systemConfigurator) flushDNSCache() error {
|
|||||||
if out, err := cmd.CombinedOutput(); err != nil {
|
if out, err := cmd.CombinedOutput(); err != nil {
|
||||||
return fmt.Errorf("restart mDNSResponder: %w, output: %s", err, out)
|
return fmt.Errorf("restart mDNSResponder: %w, output: %s", err, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info("flushed DNS cache")
|
log.Info("flushed DNS cache")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,10 +3,7 @@
|
|||||||
package dns
|
package dns
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -52,22 +49,17 @@ func TestDarwinDNSUncleanShutdownCleanup(t *testing.T) {
|
|||||||
|
|
||||||
require.NoError(t, sm.PersistState(context.Background()))
|
require.NoError(t, sm.PersistState(context.Background()))
|
||||||
|
|
||||||
|
searchKey := getKeyWithInput(netbirdDNSStateKeyFormat, searchSuffix)
|
||||||
|
matchKey := getKeyWithInput(netbirdDNSStateKeyFormat, matchSuffix)
|
||||||
localKey := getKeyWithInput(netbirdDNSStateKeyFormat, localSuffix)
|
localKey := getKeyWithInput(netbirdDNSStateKeyFormat, localSuffix)
|
||||||
|
|
||||||
// Collect all created keys for cleanup verification
|
|
||||||
createdKeys := make([]string, 0, len(configurator.createdKeys))
|
|
||||||
for key := range configurator.createdKeys {
|
|
||||||
createdKeys = append(createdKeys, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
for _, key := range createdKeys {
|
for _, key := range []string{searchKey, matchKey, localKey} {
|
||||||
_ = removeTestDNSKey(key)
|
_ = removeTestDNSKey(key)
|
||||||
}
|
}
|
||||||
_ = removeTestDNSKey(localKey)
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
for _, key := range createdKeys {
|
for _, key := range []string{searchKey, matchKey, localKey} {
|
||||||
exists, err := checkDNSKeyExists(key)
|
exists, err := checkDNSKeyExists(key)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
if exists {
|
if exists {
|
||||||
@@ -91,223 +83,13 @@ func TestDarwinDNSUncleanShutdownCleanup(t *testing.T) {
|
|||||||
err = shutdownState.Cleanup()
|
err = shutdownState.Cleanup()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
for _, key := range createdKeys {
|
for _, key := range []string{searchKey, matchKey, localKey} {
|
||||||
exists, err := checkDNSKeyExists(key)
|
exists, err := checkDNSKeyExists(key)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.False(t, exists, "Key %s should NOT exist after cleanup", key)
|
assert.False(t, exists, "Key %s should NOT exist after cleanup", key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// generateShortDomains generates domains like a.com, b.com, ..., aa.com, ab.com, etc.
|
|
||||||
func generateShortDomains(count int) []string {
|
|
||||||
domains := make([]string, 0, count)
|
|
||||||
for i := range count {
|
|
||||||
label := ""
|
|
||||||
n := i
|
|
||||||
for {
|
|
||||||
label = string(rune('a'+n%26)) + label
|
|
||||||
n = n/26 - 1
|
|
||||||
if n < 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
domains = append(domains, label+".com")
|
|
||||||
}
|
|
||||||
return domains
|
|
||||||
}
|
|
||||||
|
|
||||||
// generateLongDomains generates domains like subdomain-000.department.organization-name.example.com
|
|
||||||
func generateLongDomains(count int) []string {
|
|
||||||
domains := make([]string, 0, count)
|
|
||||||
for i := range count {
|
|
||||||
domains = append(domains, fmt.Sprintf("subdomain-%03d.department.organization-name.example.com", i))
|
|
||||||
}
|
|
||||||
return domains
|
|
||||||
}
|
|
||||||
|
|
||||||
// readDomainsFromKey reads the SupplementalMatchDomains array back from scutil for a given key.
|
|
||||||
func readDomainsFromKey(t *testing.T, key string) []string {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
cmd := exec.Command(scutilPath)
|
|
||||||
cmd.Stdin = strings.NewReader(fmt.Sprintf("open\nshow %s\nquit\n", key))
|
|
||||||
out, err := cmd.Output()
|
|
||||||
require.NoError(t, err, "scutil show should succeed")
|
|
||||||
|
|
||||||
var domains []string
|
|
||||||
inArray := false
|
|
||||||
scanner := bufio.NewScanner(bytes.NewReader(out))
|
|
||||||
for scanner.Scan() {
|
|
||||||
line := strings.TrimSpace(scanner.Text())
|
|
||||||
if strings.HasPrefix(line, "SupplementalMatchDomains") && strings.Contains(line, "<array>") {
|
|
||||||
inArray = true
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if inArray {
|
|
||||||
if line == "}" {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
// lines look like: "0 : a.com"
|
|
||||||
parts := strings.SplitN(line, " : ", 2)
|
|
||||||
if len(parts) == 2 {
|
|
||||||
domains = append(domains, parts[1])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
require.NoError(t, scanner.Err())
|
|
||||||
return domains
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSplitDomainsIntoBatches(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
domains []string
|
|
||||||
expectedCount int
|
|
||||||
checkAllPresent bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "empty",
|
|
||||||
domains: nil,
|
|
||||||
expectedCount: 0,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "under_limit",
|
|
||||||
domains: generateShortDomains(10),
|
|
||||||
expectedCount: 1,
|
|
||||||
checkAllPresent: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "at_element_limit",
|
|
||||||
domains: generateShortDomains(50),
|
|
||||||
expectedCount: 1,
|
|
||||||
checkAllPresent: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "over_element_limit",
|
|
||||||
domains: generateShortDomains(51),
|
|
||||||
expectedCount: 2,
|
|
||||||
checkAllPresent: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "triple_element_limit",
|
|
||||||
domains: generateShortDomains(150),
|
|
||||||
expectedCount: 3,
|
|
||||||
checkAllPresent: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "long_domains_hit_byte_limit",
|
|
||||||
domains: generateLongDomains(50),
|
|
||||||
checkAllPresent: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "500_short_domains",
|
|
||||||
domains: generateShortDomains(500),
|
|
||||||
expectedCount: 10,
|
|
||||||
checkAllPresent: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "500_long_domains",
|
|
||||||
domains: generateLongDomains(500),
|
|
||||||
checkAllPresent: true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tc := range tests {
|
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
|
||||||
batches := splitDomainsIntoBatches(tc.domains)
|
|
||||||
|
|
||||||
if tc.expectedCount > 0 {
|
|
||||||
assert.Len(t, batches, tc.expectedCount, "expected %d batches", tc.expectedCount)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify each batch respects limits
|
|
||||||
for i, batch := range batches {
|
|
||||||
assert.LessOrEqual(t, len(batch), maxDomainsPerResolverEntry,
|
|
||||||
"batch %d exceeds element limit", i)
|
|
||||||
|
|
||||||
totalBytes := 0
|
|
||||||
for j, d := range batch {
|
|
||||||
if j > 0 {
|
|
||||||
totalBytes++
|
|
||||||
}
|
|
||||||
totalBytes += len(d)
|
|
||||||
}
|
|
||||||
assert.LessOrEqual(t, totalBytes, maxDomainBytesPerResolverEntry,
|
|
||||||
"batch %d exceeds byte limit (%d bytes)", i, totalBytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
if tc.checkAllPresent {
|
|
||||||
var all []string
|
|
||||||
for _, batch := range batches {
|
|
||||||
all = append(all, batch...)
|
|
||||||
}
|
|
||||||
assert.Equal(t, tc.domains, all, "all domains should be present in order")
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestMatchDomainBatching writes increasing numbers of domains via the batching mechanism
|
|
||||||
// and verifies all domains are readable across multiple scutil keys.
|
|
||||||
func TestMatchDomainBatching(t *testing.T) {
|
|
||||||
if testing.Short() {
|
|
||||||
t.Skip("skipping scutil integration test in short mode")
|
|
||||||
}
|
|
||||||
|
|
||||||
testCases := []struct {
|
|
||||||
name string
|
|
||||||
count int
|
|
||||||
generator func(int) []string
|
|
||||||
}{
|
|
||||||
{"short_10", 10, generateShortDomains},
|
|
||||||
{"short_50", 50, generateShortDomains},
|
|
||||||
{"short_100", 100, generateShortDomains},
|
|
||||||
{"short_200", 200, generateShortDomains},
|
|
||||||
{"short_500", 500, generateShortDomains},
|
|
||||||
{"long_10", 10, generateLongDomains},
|
|
||||||
{"long_50", 50, generateLongDomains},
|
|
||||||
{"long_100", 100, generateLongDomains},
|
|
||||||
{"long_200", 200, generateLongDomains},
|
|
||||||
{"long_500", 500, generateLongDomains},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tc := range testCases {
|
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
|
||||||
configurator := &systemConfigurator{
|
|
||||||
createdKeys: make(map[string]struct{}),
|
|
||||||
}
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
for key := range configurator.createdKeys {
|
|
||||||
_ = removeTestDNSKey(key)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
domains := tc.generator(tc.count)
|
|
||||||
err := configurator.addBatchedDomains(matchSuffix, domains, netip.MustParseAddr("100.64.0.1"), 53, false)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
batches := splitDomainsIntoBatches(domains)
|
|
||||||
t.Logf("wrote %d domains across %d batched keys", tc.count, len(batches))
|
|
||||||
|
|
||||||
// Read back all domains from all batched keys
|
|
||||||
var got []string
|
|
||||||
for i := range batches {
|
|
||||||
key := fmt.Sprintf(netbirdDNSStateKeyIndexedFormat, matchSuffix, i)
|
|
||||||
exists, err := checkDNSKeyExists(key)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.True(t, exists, "key %s should exist", key)
|
|
||||||
|
|
||||||
got = append(got, readDomainsFromKey(t, key)...)
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("read back %d/%d domains from %d keys", len(got), tc.count, len(batches))
|
|
||||||
assert.Equal(t, tc.count, len(got), "all domains should be readable")
|
|
||||||
assert.Equal(t, domains, got, "domains should match in order")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkDNSKeyExists(key string) (bool, error) {
|
func checkDNSKeyExists(key string) (bool, error) {
|
||||||
cmd := exec.Command(scutilPath)
|
cmd := exec.Command(scutilPath)
|
||||||
cmd.Stdin = strings.NewReader("show " + key + "\nquit\n")
|
cmd.Stdin = strings.NewReader("show " + key + "\nquit\n")
|
||||||
@@ -376,15 +158,15 @@ func setupTestConfigurator(t *testing.T) (*systemConfigurator, *statemanager.Man
|
|||||||
createdKeys: make(map[string]struct{}),
|
createdKeys: make(map[string]struct{}),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
searchKey := getKeyWithInput(netbirdDNSStateKeyFormat, searchSuffix)
|
||||||
|
matchKey := getKeyWithInput(netbirdDNSStateKeyFormat, matchSuffix)
|
||||||
|
localKey := getKeyWithInput(netbirdDNSStateKeyFormat, localSuffix)
|
||||||
|
|
||||||
cleanup := func() {
|
cleanup := func() {
|
||||||
_ = sm.Stop(context.Background())
|
_ = sm.Stop(context.Background())
|
||||||
for key := range configurator.createdKeys {
|
for _, key := range []string{searchKey, matchKey, localKey} {
|
||||||
_ = removeTestDNSKey(key)
|
_ = removeTestDNSKey(key)
|
||||||
}
|
}
|
||||||
// Also clean up old-format keys and local key in case they exist
|
|
||||||
_ = removeTestDNSKey(getKeyWithInput(netbirdDNSStateKeyFormat, searchSuffix))
|
|
||||||
_ = removeTestDNSKey(getKeyWithInput(netbirdDNSStateKeyFormat, matchSuffix))
|
|
||||||
_ = removeTestDNSKey(getKeyWithInput(netbirdDNSStateKeyFormat, localSuffix))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return configurator, sm, cleanup
|
return configurator, sm, cleanup
|
||||||
|
|||||||
@@ -42,8 +42,6 @@ const (
|
|||||||
dnsPolicyConfigConfigOptionsKey = "ConfigOptions"
|
dnsPolicyConfigConfigOptionsKey = "ConfigOptions"
|
||||||
dnsPolicyConfigConfigOptionsValue = 0x8
|
dnsPolicyConfigConfigOptionsValue = 0x8
|
||||||
|
|
||||||
nrptMaxDomainsPerRule = 50
|
|
||||||
|
|
||||||
interfaceConfigPath = `SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces`
|
interfaceConfigPath = `SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces`
|
||||||
interfaceConfigNameServerKey = "NameServer"
|
interfaceConfigNameServerKey = "NameServer"
|
||||||
interfaceConfigSearchListKey = "SearchList"
|
interfaceConfigSearchListKey = "SearchList"
|
||||||
@@ -200,11 +198,10 @@ func (r *registryConfigurator) applyDNSConfig(config HostDNSConfig, stateManager
|
|||||||
|
|
||||||
if len(matchDomains) != 0 {
|
if len(matchDomains) != 0 {
|
||||||
count, err := r.addDNSMatchPolicy(matchDomains, config.ServerIP)
|
count, err := r.addDNSMatchPolicy(matchDomains, config.ServerIP)
|
||||||
// Update count even on error to ensure cleanup covers partially created rules
|
|
||||||
r.nrptEntryCount = count
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("add dns match policy: %w", err)
|
return fmt.Errorf("add dns match policy: %w", err)
|
||||||
}
|
}
|
||||||
|
r.nrptEntryCount = count
|
||||||
} else {
|
} else {
|
||||||
r.nrptEntryCount = 0
|
r.nrptEntryCount = 0
|
||||||
}
|
}
|
||||||
@@ -242,33 +239,23 @@ func (r *registryConfigurator) addDNSSetupForAll(ip netip.Addr) error {
|
|||||||
func (r *registryConfigurator) addDNSMatchPolicy(domains []string, ip netip.Addr) (int, error) {
|
func (r *registryConfigurator) addDNSMatchPolicy(domains []string, ip netip.Addr) (int, error) {
|
||||||
// if the gpo key is present, we need to put our DNS settings there, otherwise our config might be ignored
|
// if the gpo key is present, we need to put our DNS settings there, otherwise our config might be ignored
|
||||||
// see https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-gpnrpt/8cc31cb9-20cb-4140-9e85-3e08703b4745
|
// see https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-gpnrpt/8cc31cb9-20cb-4140-9e85-3e08703b4745
|
||||||
|
for i, domain := range domains {
|
||||||
|
localPath := fmt.Sprintf("%s-%d", dnsPolicyConfigMatchPath, i)
|
||||||
|
gpoPath := fmt.Sprintf("%s-%d", gpoDnsPolicyConfigMatchPath, i)
|
||||||
|
|
||||||
// We need to batch domains into chunks and create one NRPT rule per batch.
|
singleDomain := []string{domain}
|
||||||
ruleIndex := 0
|
|
||||||
for i := 0; i < len(domains); i += nrptMaxDomainsPerRule {
|
if err := r.configureDNSPolicy(localPath, singleDomain, ip); err != nil {
|
||||||
end := i + nrptMaxDomainsPerRule
|
return i, fmt.Errorf("configure DNS Local policy for domain %s: %w", domain, err)
|
||||||
if end > len(domains) {
|
|
||||||
end = len(domains)
|
|
||||||
}
|
}
|
||||||
batchDomains := domains[i:end]
|
|
||||||
|
|
||||||
localPath := fmt.Sprintf("%s-%d", dnsPolicyConfigMatchPath, ruleIndex)
|
|
||||||
gpoPath := fmt.Sprintf("%s-%d", gpoDnsPolicyConfigMatchPath, ruleIndex)
|
|
||||||
|
|
||||||
if err := r.configureDNSPolicy(localPath, batchDomains, ip); err != nil {
|
|
||||||
return ruleIndex, fmt.Errorf("configure DNS Local policy for rule %d: %w", ruleIndex, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Increment immediately so the caller's cleanup path knows about this rule
|
|
||||||
ruleIndex++
|
|
||||||
|
|
||||||
if r.gpo {
|
if r.gpo {
|
||||||
if err := r.configureDNSPolicy(gpoPath, batchDomains, ip); err != nil {
|
if err := r.configureDNSPolicy(gpoPath, singleDomain, ip); err != nil {
|
||||||
return ruleIndex, fmt.Errorf("configure gpo DNS policy for rule %d: %w", ruleIndex-1, err)
|
return i, fmt.Errorf("configure gpo DNS policy: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("added NRPT rule %d with %d domains", ruleIndex-1, len(batchDomains))
|
log.Debugf("added NRPT entry for domain: %s", domain)
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.gpo {
|
if r.gpo {
|
||||||
@@ -277,8 +264,8 @@ func (r *registryConfigurator) addDNSMatchPolicy(domains []string, ip netip.Addr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("added %d NRPT rules for %d domains", ruleIndex, len(domains))
|
log.Infof("added %d separate NRPT entries. Domain list: %s", len(domains), domains)
|
||||||
return ruleIndex, nil
|
return len(domains), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *registryConfigurator) configureDNSPolicy(policyPath string, domains []string, ip netip.Addr) error {
|
func (r *registryConfigurator) configureDNSPolicy(policyPath string, domains []string, ip netip.Addr) error {
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import (
|
|||||||
|
|
||||||
// TestNRPTEntriesCleanupOnConfigChange tests that old NRPT entries are properly cleaned up
|
// TestNRPTEntriesCleanupOnConfigChange tests that old NRPT entries are properly cleaned up
|
||||||
// when the number of match domains decreases between configuration changes.
|
// when the number of match domains decreases between configuration changes.
|
||||||
// With batching enabled (50 domains per rule), we need enough domains to create multiple rules.
|
|
||||||
func TestNRPTEntriesCleanupOnConfigChange(t *testing.T) {
|
func TestNRPTEntriesCleanupOnConfigChange(t *testing.T) {
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
t.Skip("skipping registry integration test in short mode")
|
t.Skip("skipping registry integration test in short mode")
|
||||||
@@ -38,60 +37,51 @@ func TestNRPTEntriesCleanupOnConfigChange(t *testing.T) {
|
|||||||
gpo: false,
|
gpo: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create 125 domains which will result in 3 NRPT rules (50+50+25)
|
config5 := HostDNSConfig{
|
||||||
domains125 := make([]DomainConfig, 125)
|
|
||||||
for i := 0; i < 125; i++ {
|
|
||||||
domains125[i] = DomainConfig{
|
|
||||||
Domain: fmt.Sprintf("domain%d.com", i+1),
|
|
||||||
MatchOnly: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
config125 := HostDNSConfig{
|
|
||||||
ServerIP: testIP,
|
ServerIP: testIP,
|
||||||
Domains: domains125,
|
Domains: []DomainConfig{
|
||||||
|
{Domain: "domain1.com", MatchOnly: true},
|
||||||
|
{Domain: "domain2.com", MatchOnly: true},
|
||||||
|
{Domain: "domain3.com", MatchOnly: true},
|
||||||
|
{Domain: "domain4.com", MatchOnly: true},
|
||||||
|
{Domain: "domain5.com", MatchOnly: true},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
err = cfg.applyDNSConfig(config125, nil)
|
err = cfg.applyDNSConfig(config5, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Verify 3 NRPT rules exist
|
// Verify all 5 entries exist
|
||||||
assert.Equal(t, 3, cfg.nrptEntryCount, "Should create 3 NRPT rules for 125 domains")
|
for i := 0; i < 5; i++ {
|
||||||
for i := 0; i < 3; i++ {
|
|
||||||
exists, err := registryKeyExists(fmt.Sprintf("%s-%d", dnsPolicyConfigMatchPath, i))
|
exists, err := registryKeyExists(fmt.Sprintf("%s-%d", dnsPolicyConfigMatchPath, i))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.True(t, exists, "NRPT rule %d should exist after first config", i)
|
assert.True(t, exists, "Entry %d should exist after first config", i)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reduce to 75 domains which will result in 2 NRPT rules (50+25)
|
config2 := HostDNSConfig{
|
||||||
domains75 := make([]DomainConfig, 75)
|
|
||||||
for i := 0; i < 75; i++ {
|
|
||||||
domains75[i] = DomainConfig{
|
|
||||||
Domain: fmt.Sprintf("domain%d.com", i+1),
|
|
||||||
MatchOnly: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
config75 := HostDNSConfig{
|
|
||||||
ServerIP: testIP,
|
ServerIP: testIP,
|
||||||
Domains: domains75,
|
Domains: []DomainConfig{
|
||||||
|
{Domain: "domain1.com", MatchOnly: true},
|
||||||
|
{Domain: "domain2.com", MatchOnly: true},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
err = cfg.applyDNSConfig(config75, nil)
|
err = cfg.applyDNSConfig(config2, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Verify first 2 NRPT rules exist
|
// Verify first 2 entries exist
|
||||||
assert.Equal(t, 2, cfg.nrptEntryCount, "Should create 2 NRPT rules for 75 domains")
|
|
||||||
for i := 0; i < 2; i++ {
|
for i := 0; i < 2; i++ {
|
||||||
exists, err := registryKeyExists(fmt.Sprintf("%s-%d", dnsPolicyConfigMatchPath, i))
|
exists, err := registryKeyExists(fmt.Sprintf("%s-%d", dnsPolicyConfigMatchPath, i))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.True(t, exists, "NRPT rule %d should exist after second config", i)
|
assert.True(t, exists, "Entry %d should exist after second config", i)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify rule 2 is cleaned up
|
// Verify entries 2-4 are cleaned up
|
||||||
exists, err := registryKeyExists(fmt.Sprintf("%s-%d", dnsPolicyConfigMatchPath, 2))
|
for i := 2; i < 5; i++ {
|
||||||
|
exists, err := registryKeyExists(fmt.Sprintf("%s-%d", dnsPolicyConfigMatchPath, i))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.False(t, exists, "NRPT rule 2 should NOT exist after reducing to 75 domains")
|
assert.False(t, exists, "Entry %d should NOT exist after reducing to 2 domains", i)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func registryKeyExists(path string) (bool, error) {
|
func registryKeyExists(path string) (bool, error) {
|
||||||
@@ -107,106 +97,6 @@ func registryKeyExists(path string) (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func cleanupRegistryKeys(*testing.T) {
|
func cleanupRegistryKeys(*testing.T) {
|
||||||
// Clean up more entries to account for batching tests with many domains
|
cfg := ®istryConfigurator{nrptEntryCount: 10}
|
||||||
cfg := ®istryConfigurator{nrptEntryCount: 20}
|
|
||||||
_ = cfg.removeDNSMatchPolicies()
|
_ = cfg.removeDNSMatchPolicies()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestNRPTDomainBatching verifies that domains are correctly batched into NRPT rules.
|
|
||||||
func TestNRPTDomainBatching(t *testing.T) {
|
|
||||||
if testing.Short() {
|
|
||||||
t.Skip("skipping registry integration test in short mode")
|
|
||||||
}
|
|
||||||
|
|
||||||
defer cleanupRegistryKeys(t)
|
|
||||||
cleanupRegistryKeys(t)
|
|
||||||
|
|
||||||
testIP := netip.MustParseAddr("100.64.0.1")
|
|
||||||
|
|
||||||
// Create a test interface registry key so updateSearchDomains doesn't fail
|
|
||||||
testGUID := "{12345678-1234-1234-1234-123456789ABC}"
|
|
||||||
interfacePath := `SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\` + testGUID
|
|
||||||
testKey, _, err := registry.CreateKey(registry.LOCAL_MACHINE, interfacePath, registry.SET_VALUE)
|
|
||||||
require.NoError(t, err, "Should create test interface registry key")
|
|
||||||
testKey.Close()
|
|
||||||
defer func() {
|
|
||||||
_ = registry.DeleteKey(registry.LOCAL_MACHINE, interfacePath)
|
|
||||||
}()
|
|
||||||
|
|
||||||
cfg := ®istryConfigurator{
|
|
||||||
guid: testGUID,
|
|
||||||
gpo: false,
|
|
||||||
}
|
|
||||||
|
|
||||||
testCases := []struct {
|
|
||||||
name string
|
|
||||||
domainCount int
|
|
||||||
expectedRuleCount int
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "Less than 50 domains (single rule)",
|
|
||||||
domainCount: 30,
|
|
||||||
expectedRuleCount: 1,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Exactly 50 domains (single rule)",
|
|
||||||
domainCount: 50,
|
|
||||||
expectedRuleCount: 1,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "51 domains (two rules)",
|
|
||||||
domainCount: 51,
|
|
||||||
expectedRuleCount: 2,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "100 domains (two rules)",
|
|
||||||
domainCount: 100,
|
|
||||||
expectedRuleCount: 2,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "125 domains (three rules: 50+50+25)",
|
|
||||||
domainCount: 125,
|
|
||||||
expectedRuleCount: 3,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tc := range testCases {
|
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
|
||||||
// Clean up before each subtest
|
|
||||||
cleanupRegistryKeys(t)
|
|
||||||
|
|
||||||
// Generate domains
|
|
||||||
domains := make([]DomainConfig, tc.domainCount)
|
|
||||||
for i := 0; i < tc.domainCount; i++ {
|
|
||||||
domains[i] = DomainConfig{
|
|
||||||
Domain: fmt.Sprintf("domain%d.com", i+1),
|
|
||||||
MatchOnly: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
config := HostDNSConfig{
|
|
||||||
ServerIP: testIP,
|
|
||||||
Domains: domains,
|
|
||||||
}
|
|
||||||
|
|
||||||
err := cfg.applyDNSConfig(config, nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
// Verify that exactly expectedRuleCount rules were created
|
|
||||||
assert.Equal(t, tc.expectedRuleCount, cfg.nrptEntryCount,
|
|
||||||
"Should create %d NRPT rules for %d domains", tc.expectedRuleCount, tc.domainCount)
|
|
||||||
|
|
||||||
// Verify all expected rules exist
|
|
||||||
for i := 0; i < tc.expectedRuleCount; i++ {
|
|
||||||
exists, err := registryKeyExists(fmt.Sprintf("%s-%d", dnsPolicyConfigMatchPath, i))
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.True(t, exists, "NRPT rule %d should exist", i)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify no extra rules were created
|
|
||||||
exists, err := registryKeyExists(fmt.Sprintf("%s-%d", dnsPolicyConfigMatchPath, tc.expectedRuleCount))
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.False(t, exists, "No NRPT rule should exist at index %d", tc.expectedRuleCount)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -376,9 +376,9 @@ func (m *Resolver) extractDomainsFromServerDomains(serverDomains dnsconfig.Serve
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flow receiver domain is intentionally excluded from caching.
|
if serverDomains.Flow != "" {
|
||||||
// Cloud providers may rotate the IP behind this domain; a stale cached record
|
domains = append(domains, serverDomains.Flow)
|
||||||
// causes TLS certificate verification failures on reconnect.
|
}
|
||||||
|
|
||||||
for _, stun := range serverDomains.Stuns {
|
for _, stun := range serverDomains.Stuns {
|
||||||
if stun != "" {
|
if stun != "" {
|
||||||
|
|||||||
@@ -391,8 +391,7 @@ func TestResolver_PartialUpdateAddsNewTypePreservesExisting(t *testing.T) {
|
|||||||
}
|
}
|
||||||
assert.Len(t, resolver.GetCachedDomains(), 3)
|
assert.Len(t, resolver.GetCachedDomains(), 3)
|
||||||
|
|
||||||
// Update with partial ServerDomains (only flow domain - flow is intentionally excluded from
|
// Update with partial ServerDomains (only flow domain - new type, should preserve all existing)
|
||||||
// caching to prevent TLS failures from stale records, so all existing domains are preserved)
|
|
||||||
partialDomains := dnsconfig.ServerDomains{
|
partialDomains := dnsconfig.ServerDomains{
|
||||||
Flow: "github.com",
|
Flow: "github.com",
|
||||||
}
|
}
|
||||||
@@ -401,10 +400,10 @@ func TestResolver_PartialUpdateAddsNewTypePreservesExisting(t *testing.T) {
|
|||||||
t.Skipf("Skipping test due to DNS resolution failure: %v", err)
|
t.Skipf("Skipping test due to DNS resolution failure: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Len(t, removedDomains, 0, "Should not remove any domains when only flow domain is provided")
|
assert.Len(t, removedDomains, 0, "Should not remove any domains when adding new type")
|
||||||
|
|
||||||
finalDomains := resolver.GetCachedDomains()
|
finalDomains := resolver.GetCachedDomains()
|
||||||
assert.Len(t, finalDomains, 3, "Flow domain is not cached; all original domains should be preserved")
|
assert.Len(t, finalDomains, 4, "Should have all original domains plus new flow domain")
|
||||||
|
|
||||||
domainStrings := make([]string, len(finalDomains))
|
domainStrings := make([]string, len(finalDomains))
|
||||||
for i, d := range finalDomains {
|
for i, d := range finalDomains {
|
||||||
@@ -413,5 +412,5 @@ func TestResolver_PartialUpdateAddsNewTypePreservesExisting(t *testing.T) {
|
|||||||
assert.Contains(t, domainStrings, "example.org")
|
assert.Contains(t, domainStrings, "example.org")
|
||||||
assert.Contains(t, domainStrings, "google.com")
|
assert.Contains(t, domainStrings, "google.com")
|
||||||
assert.Contains(t, domainStrings, "cloudflare.com")
|
assert.Contains(t, domainStrings, "cloudflare.com")
|
||||||
assert.NotContains(t, domainStrings, "github.com")
|
assert.Contains(t, domainStrings, "github.com")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -84,18 +84,3 @@ func (m *MockServer) UpdateServerConfig(domains dnsconfig.ServerDomains) error {
|
|||||||
func (m *MockServer) PopulateManagementDomain(mgmtURL *url.URL) error {
|
func (m *MockServer) PopulateManagementDomain(mgmtURL *url.URL) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// BeginBatch mock implementation of BeginBatch from Server interface
|
|
||||||
func (m *MockServer) BeginBatch() {
|
|
||||||
// Mock implementation - no-op
|
|
||||||
}
|
|
||||||
|
|
||||||
// EndBatch mock implementation of EndBatch from Server interface
|
|
||||||
func (m *MockServer) EndBatch() {
|
|
||||||
// Mock implementation - no-op
|
|
||||||
}
|
|
||||||
|
|
||||||
// CancelBatch mock implementation of CancelBatch from Server interface
|
|
||||||
func (m *MockServer) CancelBatch() {
|
|
||||||
// Mock implementation - no-op
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -6,9 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@@ -29,8 +27,6 @@ import (
|
|||||||
"github.com/netbirdio/netbird/shared/management/domain"
|
"github.com/netbirdio/netbird/shared/management/domain"
|
||||||
)
|
)
|
||||||
|
|
||||||
const envSkipDNSProbe = "NB_SKIP_DNS_PROBE"
|
|
||||||
|
|
||||||
// ReadyListener is a notification mechanism what indicate the server is ready to handle host dns address changes
|
// ReadyListener is a notification mechanism what indicate the server is ready to handle host dns address changes
|
||||||
type ReadyListener interface {
|
type ReadyListener interface {
|
||||||
OnReady()
|
OnReady()
|
||||||
@@ -45,9 +41,6 @@ type IosDnsManager interface {
|
|||||||
type Server interface {
|
type Server interface {
|
||||||
RegisterHandler(domains domain.List, handler dns.Handler, priority int)
|
RegisterHandler(domains domain.List, handler dns.Handler, priority int)
|
||||||
DeregisterHandler(domains domain.List, priority int)
|
DeregisterHandler(domains domain.List, priority int)
|
||||||
BeginBatch()
|
|
||||||
EndBatch()
|
|
||||||
CancelBatch()
|
|
||||||
Initialize() error
|
Initialize() error
|
||||||
Stop()
|
Stop()
|
||||||
DnsIP() netip.Addr
|
DnsIP() netip.Addr
|
||||||
@@ -90,7 +83,6 @@ type DefaultServer struct {
|
|||||||
currentConfigHash uint64
|
currentConfigHash uint64
|
||||||
handlerChain *HandlerChain
|
handlerChain *HandlerChain
|
||||||
extraDomains map[domain.Domain]int
|
extraDomains map[domain.Domain]int
|
||||||
batchMode bool
|
|
||||||
|
|
||||||
mgmtCacheResolver *mgmt.Resolver
|
mgmtCacheResolver *mgmt.Resolver
|
||||||
|
|
||||||
@@ -238,10 +230,8 @@ func (s *DefaultServer) RegisterHandler(domains domain.List, handler dns.Handler
|
|||||||
// convert to zone with simple ref counter
|
// convert to zone with simple ref counter
|
||||||
s.extraDomains[toZone(domain)]++
|
s.extraDomains[toZone(domain)]++
|
||||||
}
|
}
|
||||||
if !s.batchMode {
|
|
||||||
s.applyHostConfig()
|
s.applyHostConfig()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func (s *DefaultServer) registerHandler(domains []string, handler dns.Handler, priority int) {
|
func (s *DefaultServer) registerHandler(domains []string, handler dns.Handler, priority int) {
|
||||||
log.Debugf("registering handler %s with priority %d for %v", handler, priority, domains)
|
log.Debugf("registering handler %s with priority %d for %v", handler, priority, domains)
|
||||||
@@ -269,40 +259,8 @@ func (s *DefaultServer) DeregisterHandler(domains domain.List, priority int) {
|
|||||||
delete(s.extraDomains, zone)
|
delete(s.extraDomains, zone)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !s.batchMode {
|
|
||||||
s.applyHostConfig()
|
s.applyHostConfig()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// BeginBatch starts batch mode for DNS handler registration/deregistration.
|
|
||||||
// In batch mode, applyHostConfig() is not called after each handler operation,
|
|
||||||
// allowing multiple handlers to be registered/deregistered efficiently.
|
|
||||||
// Must be followed by EndBatch() to apply the accumulated changes.
|
|
||||||
func (s *DefaultServer) BeginBatch() {
|
|
||||||
s.mux.Lock()
|
|
||||||
defer s.mux.Unlock()
|
|
||||||
log.Debugf("DNS batch mode enabled")
|
|
||||||
s.batchMode = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// EndBatch ends batch mode and applies all accumulated DNS configuration changes.
|
|
||||||
func (s *DefaultServer) EndBatch() {
|
|
||||||
s.mux.Lock()
|
|
||||||
defer s.mux.Unlock()
|
|
||||||
log.Debugf("DNS batch mode disabled, applying accumulated changes")
|
|
||||||
s.batchMode = false
|
|
||||||
s.applyHostConfig()
|
|
||||||
}
|
|
||||||
|
|
||||||
// CancelBatch cancels batch mode without applying accumulated changes.
|
|
||||||
// This is useful when operations fail partway through and you want to
|
|
||||||
// discard partial state rather than applying it.
|
|
||||||
func (s *DefaultServer) CancelBatch() {
|
|
||||||
s.mux.Lock()
|
|
||||||
defer s.mux.Unlock()
|
|
||||||
log.Debugf("DNS batch mode cancelled, discarding accumulated changes")
|
|
||||||
s.batchMode = false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *DefaultServer) deregisterHandler(domains []string, priority int) {
|
func (s *DefaultServer) deregisterHandler(domains []string, priority int) {
|
||||||
log.Debugf("deregistering handler with priority %d for %v", priority, domains)
|
log.Debugf("deregistering handler with priority %d for %v", priority, domains)
|
||||||
@@ -481,17 +439,6 @@ func (s *DefaultServer) SearchDomains() []string {
|
|||||||
// ProbeAvailability tests each upstream group's servers for availability
|
// ProbeAvailability tests each upstream group's servers for availability
|
||||||
// and deactivates the group if no server responds
|
// and deactivates the group if no server responds
|
||||||
func (s *DefaultServer) ProbeAvailability() {
|
func (s *DefaultServer) ProbeAvailability() {
|
||||||
if val := os.Getenv(envSkipDNSProbe); val != "" {
|
|
||||||
skipProbe, err := strconv.ParseBool(val)
|
|
||||||
if err != nil {
|
|
||||||
log.Warnf("failed to parse %s: %v", envSkipDNSProbe, err)
|
|
||||||
}
|
|
||||||
if skipProbe {
|
|
||||||
log.Infof("skipping DNS probe due to %s", envSkipDNSProbe)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
for _, mux := range s.dnsMuxMap {
|
for _, mux := range s.dnsMuxMap {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
@@ -561,7 +508,6 @@ func (s *DefaultServer) applyConfiguration(update nbdns.Config) error {
|
|||||||
s.currentConfig.RouteAll = false
|
s.currentConfig.RouteAll = false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Always apply host config for management updates, regardless of batch mode
|
|
||||||
s.applyHostConfig()
|
s.applyHostConfig()
|
||||||
|
|
||||||
s.shutdownWg.Add(1)
|
s.shutdownWg.Add(1)
|
||||||
@@ -926,7 +872,6 @@ func (s *DefaultServer) upstreamCallbacks(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Always apply host config when nameserver goes down, regardless of batch mode
|
|
||||||
s.applyHostConfig()
|
s.applyHostConfig()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@@ -962,7 +907,6 @@ func (s *DefaultServer) upstreamCallbacks(
|
|||||||
s.registerHandler([]string{nbdns.RootZone}, handler, priority)
|
s.registerHandler([]string{nbdns.RootZone}, handler, priority)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Always apply host config when nameserver reactivates, regardless of batch mode
|
|
||||||
s.applyHostConfig()
|
s.applyHostConfig()
|
||||||
|
|
||||||
s.updateNSState(nsGroup, nil, true)
|
s.updateNSState(nsGroup, nil, true)
|
||||||
|
|||||||
@@ -18,12 +18,7 @@ func TestGetServerDns(t *testing.T) {
|
|||||||
t.Errorf("invalid dns server instance: %s", err)
|
t.Errorf("invalid dns server instance: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mockSrvB, ok := srvB.(*MockServer)
|
if srvB != srv {
|
||||||
if !ok {
|
|
||||||
t.Errorf("returned server is not a MockServer")
|
|
||||||
}
|
|
||||||
|
|
||||||
if mockSrvB != srv {
|
|
||||||
t.Errorf("mismatch dns instances")
|
t.Errorf("mismatch dns instances")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -351,13 +351,9 @@ func (u *upstreamResolverBase) waitUntilResponse() {
|
|||||||
return fmt.Errorf("upstream check call error")
|
return fmt.Errorf("upstream check call error")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := backoff.Retry(operation, backoff.WithContext(exponentialBackOff, u.ctx))
|
err := backoff.Retry(operation, exponentialBackOff)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, context.Canceled) {
|
log.Warn(err)
|
||||||
log.Debugf("upstream retry loop exited for upstreams %s", u.upstreamServersString())
|
|
||||||
} else {
|
|
||||||
log.Warnf("upstream retry loop exited for upstreams %s: %v", u.upstreamServersString(), err)
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -190,75 +190,50 @@ func (f *DNSForwarder) Close(ctx context.Context) error {
|
|||||||
return nberrors.FormatErrorOrNil(result)
|
return nberrors.FormatErrorOrNil(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *DNSForwarder) handleDNSQuery(logger *log.Entry, w dns.ResponseWriter, query *dns.Msg, startTime time.Time) {
|
func (f *DNSForwarder) handleDNSQuery(logger *log.Entry, w dns.ResponseWriter, query *dns.Msg) *dns.Msg {
|
||||||
if len(query.Question) == 0 {
|
if len(query.Question) == 0 {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
question := query.Question[0]
|
question := query.Question[0]
|
||||||
qname := strings.ToLower(question.Name)
|
logger.Tracef("received DNS request for DNS forwarder: domain=%s type=%s class=%s",
|
||||||
|
question.Name, dns.TypeToString[question.Qtype], dns.ClassToString[question.Qclass])
|
||||||
|
|
||||||
logger.Tracef("question: domain=%s type=%s class=%s",
|
domain := strings.ToLower(question.Name)
|
||||||
qname, dns.TypeToString[question.Qtype], dns.ClassToString[question.Qclass])
|
|
||||||
|
|
||||||
resp := query.SetReply(query)
|
resp := query.SetReply(query)
|
||||||
network := resutil.NetworkForQtype(question.Qtype)
|
network := resutil.NetworkForQtype(question.Qtype)
|
||||||
if network == "" {
|
if network == "" {
|
||||||
resp.Rcode = dns.RcodeNotImplemented
|
resp.Rcode = dns.RcodeNotImplemented
|
||||||
f.writeResponse(logger, w, resp, qname, startTime)
|
if err := w.WriteMsg(resp); err != nil {
|
||||||
return
|
logger.Errorf("failed to write DNS response: %v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
mostSpecificResId, matchingEntries := f.getMatchingEntries(strings.TrimSuffix(qname, "."))
|
mostSpecificResId, matchingEntries := f.getMatchingEntries(strings.TrimSuffix(domain, "."))
|
||||||
|
// query doesn't match any configured domain
|
||||||
if mostSpecificResId == "" {
|
if mostSpecificResId == "" {
|
||||||
resp.Rcode = dns.RcodeRefused
|
resp.Rcode = dns.RcodeRefused
|
||||||
f.writeResponse(logger, w, resp, qname, startTime)
|
if err := w.WriteMsg(resp); err != nil {
|
||||||
return
|
logger.Errorf("failed to write DNS response: %v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), upstreamTimeout)
|
ctx, cancel := context.WithTimeout(context.Background(), upstreamTimeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
result := resutil.LookupIP(ctx, f.resolver, network, qname, question.Qtype)
|
result := resutil.LookupIP(ctx, f.resolver, network, domain, question.Qtype)
|
||||||
if result.Err != nil {
|
if result.Err != nil {
|
||||||
f.handleDNSError(ctx, logger, w, question, resp, qname, result, startTime)
|
f.handleDNSError(ctx, logger, w, question, resp, domain, result)
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
f.updateInternalState(result.IPs, mostSpecificResId, matchingEntries)
|
f.updateInternalState(result.IPs, mostSpecificResId, matchingEntries)
|
||||||
resp.Answer = append(resp.Answer, resutil.IPsToRRs(qname, result.IPs, f.ttl)...)
|
resp.Answer = append(resp.Answer, resutil.IPsToRRs(domain, result.IPs, f.ttl)...)
|
||||||
f.cache.set(qname, question.Qtype, result.IPs)
|
f.cache.set(domain, question.Qtype, result.IPs)
|
||||||
|
|
||||||
f.writeResponse(logger, w, resp, qname, startTime)
|
return resp
|
||||||
}
|
|
||||||
|
|
||||||
func (f *DNSForwarder) writeResponse(logger *log.Entry, w dns.ResponseWriter, resp *dns.Msg, qname string, startTime time.Time) {
|
|
||||||
if err := w.WriteMsg(resp); err != nil {
|
|
||||||
logger.Errorf("failed to write DNS response: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Tracef("response: domain=%s rcode=%s answers=%s took=%s",
|
|
||||||
qname, dns.RcodeToString[resp.Rcode], resutil.FormatAnswers(resp.Answer), time.Since(startTime))
|
|
||||||
}
|
|
||||||
|
|
||||||
// udpResponseWriter wraps a dns.ResponseWriter to handle UDP-specific truncation.
|
|
||||||
type udpResponseWriter struct {
|
|
||||||
dns.ResponseWriter
|
|
||||||
query *dns.Msg
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *udpResponseWriter) WriteMsg(resp *dns.Msg) error {
|
|
||||||
opt := u.query.IsEdns0()
|
|
||||||
maxSize := dns.MinMsgSize
|
|
||||||
if opt != nil {
|
|
||||||
maxSize = int(opt.UDPSize())
|
|
||||||
}
|
|
||||||
|
|
||||||
if resp.Len() > maxSize {
|
|
||||||
resp.Truncate(maxSize)
|
|
||||||
}
|
|
||||||
|
|
||||||
return u.ResponseWriter.WriteMsg(resp)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *DNSForwarder) handleDNSQueryUDP(w dns.ResponseWriter, query *dns.Msg) {
|
func (f *DNSForwarder) handleDNSQueryUDP(w dns.ResponseWriter, query *dns.Msg) {
|
||||||
@@ -268,7 +243,30 @@ func (f *DNSForwarder) handleDNSQueryUDP(w dns.ResponseWriter, query *dns.Msg) {
|
|||||||
"dns_id": fmt.Sprintf("%04x", query.Id),
|
"dns_id": fmt.Sprintf("%04x", query.Id),
|
||||||
})
|
})
|
||||||
|
|
||||||
f.handleDNSQuery(logger, &udpResponseWriter{ResponseWriter: w, query: query}, query, startTime)
|
resp := f.handleDNSQuery(logger, w, query)
|
||||||
|
if resp == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := query.IsEdns0()
|
||||||
|
maxSize := dns.MinMsgSize
|
||||||
|
if opt != nil {
|
||||||
|
// client advertised a larger EDNS0 buffer
|
||||||
|
maxSize = int(opt.UDPSize())
|
||||||
|
}
|
||||||
|
|
||||||
|
// if our response is too big, truncate and set the TC bit
|
||||||
|
if resp.Len() > maxSize {
|
||||||
|
resp.Truncate(maxSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := w.WriteMsg(resp); err != nil {
|
||||||
|
logger.Errorf("failed to write DNS response: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Tracef("response: domain=%s rcode=%s answers=%s took=%s",
|
||||||
|
query.Question[0].Name, dns.RcodeToString[resp.Rcode], resutil.FormatAnswers(resp.Answer), time.Since(startTime))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *DNSForwarder) handleDNSQueryTCP(w dns.ResponseWriter, query *dns.Msg) {
|
func (f *DNSForwarder) handleDNSQueryTCP(w dns.ResponseWriter, query *dns.Msg) {
|
||||||
@@ -278,7 +276,18 @@ func (f *DNSForwarder) handleDNSQueryTCP(w dns.ResponseWriter, query *dns.Msg) {
|
|||||||
"dns_id": fmt.Sprintf("%04x", query.Id),
|
"dns_id": fmt.Sprintf("%04x", query.Id),
|
||||||
})
|
})
|
||||||
|
|
||||||
f.handleDNSQuery(logger, w, query, startTime)
|
resp := f.handleDNSQuery(logger, w, query)
|
||||||
|
if resp == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := w.WriteMsg(resp); err != nil {
|
||||||
|
logger.Errorf("failed to write DNS response: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.Tracef("response: domain=%s rcode=%s answers=%s took=%s",
|
||||||
|
query.Question[0].Name, dns.RcodeToString[resp.Rcode], resutil.FormatAnswers(resp.Answer), time.Since(startTime))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *DNSForwarder) updateInternalState(ips []netip.Addr, mostSpecificResId route.ResID, matchingEntries []*ForwarderEntry) {
|
func (f *DNSForwarder) updateInternalState(ips []netip.Addr, mostSpecificResId route.ResID, matchingEntries []*ForwarderEntry) {
|
||||||
@@ -325,7 +334,6 @@ func (f *DNSForwarder) handleDNSError(
|
|||||||
resp *dns.Msg,
|
resp *dns.Msg,
|
||||||
domain string,
|
domain string,
|
||||||
result resutil.LookupResult,
|
result resutil.LookupResult,
|
||||||
startTime time.Time,
|
|
||||||
) {
|
) {
|
||||||
qType := question.Qtype
|
qType := question.Qtype
|
||||||
qTypeName := dns.TypeToString[qType]
|
qTypeName := dns.TypeToString[qType]
|
||||||
@@ -335,7 +343,9 @@ func (f *DNSForwarder) handleDNSError(
|
|||||||
// NotFound: cache negative result and respond
|
// NotFound: cache negative result and respond
|
||||||
if result.Rcode == dns.RcodeNameError || result.Rcode == dns.RcodeSuccess {
|
if result.Rcode == dns.RcodeNameError || result.Rcode == dns.RcodeSuccess {
|
||||||
f.cache.set(domain, question.Qtype, nil)
|
f.cache.set(domain, question.Qtype, nil)
|
||||||
f.writeResponse(logger, w, resp, domain, startTime)
|
if writeErr := w.WriteMsg(resp); writeErr != nil {
|
||||||
|
logger.Errorf("failed to write failure DNS response: %v", writeErr)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -345,7 +355,9 @@ func (f *DNSForwarder) handleDNSError(
|
|||||||
logger.Debugf("serving cached DNS response after upstream failure: domain=%s type=%s", domain, qTypeName)
|
logger.Debugf("serving cached DNS response after upstream failure: domain=%s type=%s", domain, qTypeName)
|
||||||
resp.Answer = append(resp.Answer, resutil.IPsToRRs(domain, ips, f.ttl)...)
|
resp.Answer = append(resp.Answer, resutil.IPsToRRs(domain, ips, f.ttl)...)
|
||||||
resp.Rcode = dns.RcodeSuccess
|
resp.Rcode = dns.RcodeSuccess
|
||||||
f.writeResponse(logger, w, resp, domain, startTime)
|
if writeErr := w.WriteMsg(resp); writeErr != nil {
|
||||||
|
logger.Errorf("failed to write cached DNS response: %v", writeErr)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -353,7 +365,9 @@ func (f *DNSForwarder) handleDNSError(
|
|||||||
verifyResult := resutil.LookupIP(ctx, f.resolver, resutil.NetworkForQtype(qType), domain, qType)
|
verifyResult := resutil.LookupIP(ctx, f.resolver, resutil.NetworkForQtype(qType), domain, qType)
|
||||||
if verifyResult.Rcode == dns.RcodeNameError || verifyResult.Rcode == dns.RcodeSuccess {
|
if verifyResult.Rcode == dns.RcodeNameError || verifyResult.Rcode == dns.RcodeSuccess {
|
||||||
resp.Rcode = verifyResult.Rcode
|
resp.Rcode = verifyResult.Rcode
|
||||||
f.writeResponse(logger, w, resp, domain, startTime)
|
if writeErr := w.WriteMsg(resp); writeErr != nil {
|
||||||
|
logger.Errorf("failed to write failure DNS response: %v", writeErr)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -361,12 +375,15 @@ func (f *DNSForwarder) handleDNSError(
|
|||||||
// No cache or verification failed. Log with or without the server field for more context.
|
// No cache or verification failed. Log with or without the server field for more context.
|
||||||
var dnsErr *net.DNSError
|
var dnsErr *net.DNSError
|
||||||
if errors.As(result.Err, &dnsErr) && dnsErr.Server != "" {
|
if errors.As(result.Err, &dnsErr) && dnsErr.Server != "" {
|
||||||
logger.Warnf("upstream failure: type=%s domain=%s server=%s: %v", qTypeName, domain, dnsErr.Server, result.Err)
|
logger.Warnf("failed to resolve: type=%s domain=%s server=%s: %v", qTypeName, domain, dnsErr.Server, result.Err)
|
||||||
} else {
|
} else {
|
||||||
logger.Warnf(errResolveFailed, domain, result.Err)
|
logger.Warnf(errResolveFailed, domain, result.Err)
|
||||||
}
|
}
|
||||||
|
|
||||||
f.writeResponse(logger, w, resp, domain, startTime)
|
// Write final failure response.
|
||||||
|
if writeErr := w.WriteMsg(resp); writeErr != nil {
|
||||||
|
logger.Errorf("failed to write failure DNS response: %v", writeErr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// getMatchingEntries retrieves the resource IDs for a given domain.
|
// getMatchingEntries retrieves the resource IDs for a given domain.
|
||||||
|
|||||||
@@ -318,9 +318,8 @@ func TestDNSForwarder_UnauthorizedDomainAccess(t *testing.T) {
|
|||||||
query.SetQuestion(dns.Fqdn(tt.queryDomain), dns.TypeA)
|
query.SetQuestion(dns.Fqdn(tt.queryDomain), dns.TypeA)
|
||||||
|
|
||||||
mockWriter := &test.MockResponseWriter{}
|
mockWriter := &test.MockResponseWriter{}
|
||||||
forwarder.handleDNSQuery(log.NewEntry(log.StandardLogger()), mockWriter, query, time.Now())
|
resp := forwarder.handleDNSQuery(log.NewEntry(log.StandardLogger()), mockWriter, query)
|
||||||
|
|
||||||
resp := mockWriter.GetLastResponse()
|
|
||||||
if tt.shouldResolve {
|
if tt.shouldResolve {
|
||||||
require.NotNil(t, resp, "Expected response for authorized domain")
|
require.NotNil(t, resp, "Expected response for authorized domain")
|
||||||
require.Equal(t, dns.RcodeSuccess, resp.Rcode, "Expected successful response")
|
require.Equal(t, dns.RcodeSuccess, resp.Rcode, "Expected successful response")
|
||||||
@@ -330,9 +329,10 @@ func TestDNSForwarder_UnauthorizedDomainAccess(t *testing.T) {
|
|||||||
mockFirewall.AssertExpectations(t)
|
mockFirewall.AssertExpectations(t)
|
||||||
mockResolver.AssertExpectations(t)
|
mockResolver.AssertExpectations(t)
|
||||||
} else {
|
} else {
|
||||||
require.NotNil(t, resp, "Expected response")
|
if resp != nil {
|
||||||
assert.True(t, len(resp.Answer) == 0 || resp.Rcode != dns.RcodeSuccess,
|
assert.True(t, len(resp.Answer) == 0 || resp.Rcode != dns.RcodeSuccess,
|
||||||
"Unauthorized domain should not return successful answers")
|
"Unauthorized domain should not return successful answers")
|
||||||
|
}
|
||||||
mockFirewall.AssertNotCalled(t, "UpdateSet")
|
mockFirewall.AssertNotCalled(t, "UpdateSet")
|
||||||
mockResolver.AssertNotCalled(t, "LookupNetIP")
|
mockResolver.AssertNotCalled(t, "LookupNetIP")
|
||||||
}
|
}
|
||||||
@@ -466,16 +466,14 @@ func TestDNSForwarder_FirewallSetUpdates(t *testing.T) {
|
|||||||
dnsQuery.SetQuestion(dns.Fqdn(tt.query), dns.TypeA)
|
dnsQuery.SetQuestion(dns.Fqdn(tt.query), dns.TypeA)
|
||||||
|
|
||||||
mockWriter := &test.MockResponseWriter{}
|
mockWriter := &test.MockResponseWriter{}
|
||||||
forwarder.handleDNSQuery(log.NewEntry(log.StandardLogger()), mockWriter, dnsQuery, time.Now())
|
resp := forwarder.handleDNSQuery(log.NewEntry(log.StandardLogger()), mockWriter, dnsQuery)
|
||||||
|
|
||||||
// Verify response
|
// Verify response
|
||||||
resp := mockWriter.GetLastResponse()
|
|
||||||
if tt.shouldResolve {
|
if tt.shouldResolve {
|
||||||
require.NotNil(t, resp, "Expected response for authorized domain")
|
require.NotNil(t, resp, "Expected response for authorized domain")
|
||||||
require.Equal(t, dns.RcodeSuccess, resp.Rcode)
|
require.Equal(t, dns.RcodeSuccess, resp.Rcode)
|
||||||
require.NotEmpty(t, resp.Answer)
|
require.NotEmpty(t, resp.Answer)
|
||||||
} else {
|
} else if resp != nil {
|
||||||
require.NotNil(t, resp, "Expected response")
|
|
||||||
assert.True(t, resp.Rcode == dns.RcodeRefused || len(resp.Answer) == 0,
|
assert.True(t, resp.Rcode == dns.RcodeRefused || len(resp.Answer) == 0,
|
||||||
"Unauthorized domain should be refused or have no answers")
|
"Unauthorized domain should be refused or have no answers")
|
||||||
}
|
}
|
||||||
@@ -530,10 +528,9 @@ func TestDNSForwarder_MultipleIPsInSingleUpdate(t *testing.T) {
|
|||||||
query.SetQuestion("example.com.", dns.TypeA)
|
query.SetQuestion("example.com.", dns.TypeA)
|
||||||
|
|
||||||
mockWriter := &test.MockResponseWriter{}
|
mockWriter := &test.MockResponseWriter{}
|
||||||
forwarder.handleDNSQuery(log.NewEntry(log.StandardLogger()), mockWriter, query, time.Now())
|
resp := forwarder.handleDNSQuery(log.NewEntry(log.StandardLogger()), mockWriter, query)
|
||||||
|
|
||||||
// Verify response contains all IPs
|
// Verify response contains all IPs
|
||||||
resp := mockWriter.GetLastResponse()
|
|
||||||
require.NotNil(t, resp)
|
require.NotNil(t, resp)
|
||||||
require.Equal(t, dns.RcodeSuccess, resp.Rcode)
|
require.Equal(t, dns.RcodeSuccess, resp.Rcode)
|
||||||
require.Len(t, resp.Answer, 3, "Should have 3 answer records")
|
require.Len(t, resp.Answer, 3, "Should have 3 answer records")
|
||||||
@@ -608,7 +605,7 @@ func TestDNSForwarder_ResponseCodes(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
forwarder.handleDNSQuery(log.NewEntry(log.StandardLogger()), mockWriter, query, time.Now())
|
_ = forwarder.handleDNSQuery(log.NewEntry(log.StandardLogger()), mockWriter, query)
|
||||||
|
|
||||||
// Check the response written to the writer
|
// Check the response written to the writer
|
||||||
require.NotNil(t, writtenResp, "Expected response to be written")
|
require.NotNil(t, writtenResp, "Expected response to be written")
|
||||||
@@ -678,8 +675,7 @@ func TestDNSForwarder_ServeFromCacheOnUpstreamFailure(t *testing.T) {
|
|||||||
q1 := &dns.Msg{}
|
q1 := &dns.Msg{}
|
||||||
q1.SetQuestion(dns.Fqdn("example.com"), dns.TypeA)
|
q1.SetQuestion(dns.Fqdn("example.com"), dns.TypeA)
|
||||||
w1 := &test.MockResponseWriter{}
|
w1 := &test.MockResponseWriter{}
|
||||||
forwarder.handleDNSQuery(log.NewEntry(log.StandardLogger()), w1, q1, time.Now())
|
resp1 := forwarder.handleDNSQuery(log.NewEntry(log.StandardLogger()), w1, q1)
|
||||||
resp1 := w1.GetLastResponse()
|
|
||||||
require.NotNil(t, resp1)
|
require.NotNil(t, resp1)
|
||||||
require.Equal(t, dns.RcodeSuccess, resp1.Rcode)
|
require.Equal(t, dns.RcodeSuccess, resp1.Rcode)
|
||||||
require.Len(t, resp1.Answer, 1)
|
require.Len(t, resp1.Answer, 1)
|
||||||
@@ -687,13 +683,13 @@ func TestDNSForwarder_ServeFromCacheOnUpstreamFailure(t *testing.T) {
|
|||||||
// Second query: serve from cache after upstream failure
|
// Second query: serve from cache after upstream failure
|
||||||
q2 := &dns.Msg{}
|
q2 := &dns.Msg{}
|
||||||
q2.SetQuestion(dns.Fqdn("example.com"), dns.TypeA)
|
q2.SetQuestion(dns.Fqdn("example.com"), dns.TypeA)
|
||||||
w2 := &test.MockResponseWriter{}
|
var writtenResp *dns.Msg
|
||||||
forwarder.handleDNSQuery(log.NewEntry(log.StandardLogger()), w2, q2, time.Now())
|
w2 := &test.MockResponseWriter{WriteMsgFunc: func(m *dns.Msg) error { writtenResp = m; return nil }}
|
||||||
|
_ = forwarder.handleDNSQuery(log.NewEntry(log.StandardLogger()), w2, q2)
|
||||||
|
|
||||||
resp2 := w2.GetLastResponse()
|
require.NotNil(t, writtenResp, "expected response to be written")
|
||||||
require.NotNil(t, resp2, "expected response to be written")
|
require.Equal(t, dns.RcodeSuccess, writtenResp.Rcode)
|
||||||
require.Equal(t, dns.RcodeSuccess, resp2.Rcode)
|
require.Len(t, writtenResp.Answer, 1)
|
||||||
require.Len(t, resp2.Answer, 1)
|
|
||||||
|
|
||||||
mockResolver.AssertExpectations(t)
|
mockResolver.AssertExpectations(t)
|
||||||
}
|
}
|
||||||
@@ -719,8 +715,7 @@ func TestDNSForwarder_CacheNormalizationCasingAndDot(t *testing.T) {
|
|||||||
q1 := &dns.Msg{}
|
q1 := &dns.Msg{}
|
||||||
q1.SetQuestion(mixedQuery+".", dns.TypeA)
|
q1.SetQuestion(mixedQuery+".", dns.TypeA)
|
||||||
w1 := &test.MockResponseWriter{}
|
w1 := &test.MockResponseWriter{}
|
||||||
forwarder.handleDNSQuery(log.NewEntry(log.StandardLogger()), w1, q1, time.Now())
|
resp1 := forwarder.handleDNSQuery(log.NewEntry(log.StandardLogger()), w1, q1)
|
||||||
resp1 := w1.GetLastResponse()
|
|
||||||
require.NotNil(t, resp1)
|
require.NotNil(t, resp1)
|
||||||
require.Equal(t, dns.RcodeSuccess, resp1.Rcode)
|
require.Equal(t, dns.RcodeSuccess, resp1.Rcode)
|
||||||
require.Len(t, resp1.Answer, 1)
|
require.Len(t, resp1.Answer, 1)
|
||||||
@@ -732,13 +727,13 @@ func TestDNSForwarder_CacheNormalizationCasingAndDot(t *testing.T) {
|
|||||||
|
|
||||||
q2 := &dns.Msg{}
|
q2 := &dns.Msg{}
|
||||||
q2.SetQuestion("EXAMPLE.COM", dns.TypeA)
|
q2.SetQuestion("EXAMPLE.COM", dns.TypeA)
|
||||||
w2 := &test.MockResponseWriter{}
|
var writtenResp *dns.Msg
|
||||||
forwarder.handleDNSQuery(log.NewEntry(log.StandardLogger()), w2, q2, time.Now())
|
w2 := &test.MockResponseWriter{WriteMsgFunc: func(m *dns.Msg) error { writtenResp = m; return nil }}
|
||||||
|
_ = forwarder.handleDNSQuery(log.NewEntry(log.StandardLogger()), w2, q2)
|
||||||
|
|
||||||
resp2 := w2.GetLastResponse()
|
require.NotNil(t, writtenResp)
|
||||||
require.NotNil(t, resp2)
|
require.Equal(t, dns.RcodeSuccess, writtenResp.Rcode)
|
||||||
require.Equal(t, dns.RcodeSuccess, resp2.Rcode)
|
require.Len(t, writtenResp.Answer, 1)
|
||||||
require.Len(t, resp2.Answer, 1)
|
|
||||||
|
|
||||||
mockResolver.AssertExpectations(t)
|
mockResolver.AssertExpectations(t)
|
||||||
}
|
}
|
||||||
@@ -789,9 +784,8 @@ func TestDNSForwarder_MultipleOverlappingPatterns(t *testing.T) {
|
|||||||
query.SetQuestion("smtp.mail.example.com.", dns.TypeA)
|
query.SetQuestion("smtp.mail.example.com.", dns.TypeA)
|
||||||
|
|
||||||
mockWriter := &test.MockResponseWriter{}
|
mockWriter := &test.MockResponseWriter{}
|
||||||
forwarder.handleDNSQuery(log.NewEntry(log.StandardLogger()), mockWriter, query, time.Now())
|
resp := forwarder.handleDNSQuery(log.NewEntry(log.StandardLogger()), mockWriter, query)
|
||||||
|
|
||||||
resp := mockWriter.GetLastResponse()
|
|
||||||
require.NotNil(t, resp)
|
require.NotNil(t, resp)
|
||||||
assert.Equal(t, dns.RcodeSuccess, resp.Rcode)
|
assert.Equal(t, dns.RcodeSuccess, resp.Rcode)
|
||||||
|
|
||||||
@@ -903,15 +897,26 @@ func TestDNSForwarder_NodataVsNxdomain(t *testing.T) {
|
|||||||
query := &dns.Msg{}
|
query := &dns.Msg{}
|
||||||
query.SetQuestion(dns.Fqdn("example.com"), tt.queryType)
|
query.SetQuestion(dns.Fqdn("example.com"), tt.queryType)
|
||||||
|
|
||||||
mockWriter := &test.MockResponseWriter{}
|
var writtenResp *dns.Msg
|
||||||
forwarder.handleDNSQuery(log.NewEntry(log.StandardLogger()), mockWriter, query, time.Now())
|
mockWriter := &test.MockResponseWriter{
|
||||||
|
WriteMsgFunc: func(m *dns.Msg) error {
|
||||||
|
writtenResp = m
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
resp := mockWriter.GetLastResponse()
|
resp := forwarder.handleDNSQuery(log.NewEntry(log.StandardLogger()), mockWriter, query)
|
||||||
require.NotNil(t, resp, "Expected response to be written")
|
|
||||||
assert.Equal(t, tt.expectedCode, resp.Rcode, tt.description)
|
// If a response was returned, it means it should be written (happens in wrapper functions)
|
||||||
|
if resp != nil && writtenResp == nil {
|
||||||
|
writtenResp = resp
|
||||||
|
}
|
||||||
|
|
||||||
|
require.NotNil(t, writtenResp, "Expected response to be written")
|
||||||
|
assert.Equal(t, tt.expectedCode, writtenResp.Rcode, tt.description)
|
||||||
|
|
||||||
if tt.expectNoAnswer {
|
if tt.expectNoAnswer {
|
||||||
assert.Empty(t, resp.Answer, "Response should have no answer records")
|
assert.Empty(t, writtenResp.Answer, "Response should have no answer records")
|
||||||
}
|
}
|
||||||
|
|
||||||
mockResolver.AssertExpectations(t)
|
mockResolver.AssertExpectations(t)
|
||||||
@@ -926,8 +931,15 @@ func TestDNSForwarder_EmptyQuery(t *testing.T) {
|
|||||||
query := &dns.Msg{}
|
query := &dns.Msg{}
|
||||||
// Don't set any question
|
// Don't set any question
|
||||||
|
|
||||||
mockWriter := &test.MockResponseWriter{}
|
writeCalled := false
|
||||||
forwarder.handleDNSQuery(log.NewEntry(log.StandardLogger()), mockWriter, query, time.Now())
|
mockWriter := &test.MockResponseWriter{
|
||||||
|
WriteMsgFunc: func(m *dns.Msg) error {
|
||||||
assert.Nil(t, mockWriter.GetLastResponse(), "Should not write response for empty query")
|
writeCalled = true
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
resp := forwarder.handleDNSQuery(log.NewEntry(log.StandardLogger()), mockWriter, query)
|
||||||
|
|
||||||
|
assert.Nil(t, resp, "Should return nil for empty query")
|
||||||
|
assert.False(t, writeCalled, "Should not write response for empty query")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,14 +29,12 @@ import (
|
|||||||
firewallManager "github.com/netbirdio/netbird/client/firewall/manager"
|
firewallManager "github.com/netbirdio/netbird/client/firewall/manager"
|
||||||
"github.com/netbirdio/netbird/client/iface"
|
"github.com/netbirdio/netbird/client/iface"
|
||||||
"github.com/netbirdio/netbird/client/iface/device"
|
"github.com/netbirdio/netbird/client/iface/device"
|
||||||
nbnetstack "github.com/netbirdio/netbird/client/iface/netstack"
|
|
||||||
"github.com/netbirdio/netbird/client/iface/udpmux"
|
"github.com/netbirdio/netbird/client/iface/udpmux"
|
||||||
"github.com/netbirdio/netbird/client/internal/acl"
|
"github.com/netbirdio/netbird/client/internal/acl"
|
||||||
"github.com/netbirdio/netbird/client/internal/debug"
|
"github.com/netbirdio/netbird/client/internal/debug"
|
||||||
"github.com/netbirdio/netbird/client/internal/dns"
|
"github.com/netbirdio/netbird/client/internal/dns"
|
||||||
dnsconfig "github.com/netbirdio/netbird/client/internal/dns/config"
|
dnsconfig "github.com/netbirdio/netbird/client/internal/dns/config"
|
||||||
"github.com/netbirdio/netbird/client/internal/dnsfwd"
|
"github.com/netbirdio/netbird/client/internal/dnsfwd"
|
||||||
"github.com/netbirdio/netbird/client/internal/expose"
|
|
||||||
"github.com/netbirdio/netbird/client/internal/ingressgw"
|
"github.com/netbirdio/netbird/client/internal/ingressgw"
|
||||||
"github.com/netbirdio/netbird/client/internal/netflow"
|
"github.com/netbirdio/netbird/client/internal/netflow"
|
||||||
nftypes "github.com/netbirdio/netbird/client/internal/netflow/types"
|
nftypes "github.com/netbirdio/netbird/client/internal/netflow/types"
|
||||||
@@ -46,6 +44,7 @@ import (
|
|||||||
icemaker "github.com/netbirdio/netbird/client/internal/peer/ice"
|
icemaker "github.com/netbirdio/netbird/client/internal/peer/ice"
|
||||||
"github.com/netbirdio/netbird/client/internal/peerstore"
|
"github.com/netbirdio/netbird/client/internal/peerstore"
|
||||||
"github.com/netbirdio/netbird/client/internal/profilemanager"
|
"github.com/netbirdio/netbird/client/internal/profilemanager"
|
||||||
|
"github.com/netbirdio/netbird/client/internal/proxy"
|
||||||
"github.com/netbirdio/netbird/client/internal/relay"
|
"github.com/netbirdio/netbird/client/internal/relay"
|
||||||
"github.com/netbirdio/netbird/client/internal/rosenpass"
|
"github.com/netbirdio/netbird/client/internal/rosenpass"
|
||||||
"github.com/netbirdio/netbird/client/internal/routemanager"
|
"github.com/netbirdio/netbird/client/internal/routemanager"
|
||||||
@@ -54,11 +53,13 @@ import (
|
|||||||
"github.com/netbirdio/netbird/client/internal/updatemanager"
|
"github.com/netbirdio/netbird/client/internal/updatemanager"
|
||||||
"github.com/netbirdio/netbird/client/jobexec"
|
"github.com/netbirdio/netbird/client/jobexec"
|
||||||
cProto "github.com/netbirdio/netbird/client/proto"
|
cProto "github.com/netbirdio/netbird/client/proto"
|
||||||
|
"github.com/netbirdio/netbird/shared/management/domain"
|
||||||
|
semaphoregroup "github.com/netbirdio/netbird/util/semaphore-group"
|
||||||
|
|
||||||
"github.com/netbirdio/netbird/client/system"
|
"github.com/netbirdio/netbird/client/system"
|
||||||
nbdns "github.com/netbirdio/netbird/dns"
|
nbdns "github.com/netbirdio/netbird/dns"
|
||||||
"github.com/netbirdio/netbird/route"
|
"github.com/netbirdio/netbird/route"
|
||||||
mgm "github.com/netbirdio/netbird/shared/management/client"
|
mgm "github.com/netbirdio/netbird/shared/management/client"
|
||||||
"github.com/netbirdio/netbird/shared/management/domain"
|
|
||||||
mgmProto "github.com/netbirdio/netbird/shared/management/proto"
|
mgmProto "github.com/netbirdio/netbird/shared/management/proto"
|
||||||
auth "github.com/netbirdio/netbird/shared/relay/auth/hmac"
|
auth "github.com/netbirdio/netbird/shared/relay/auth/hmac"
|
||||||
relayClient "github.com/netbirdio/netbird/shared/relay/client"
|
relayClient "github.com/netbirdio/netbird/shared/relay/client"
|
||||||
@@ -74,6 +75,7 @@ import (
|
|||||||
const (
|
const (
|
||||||
PeerConnectionTimeoutMax = 45000 // ms
|
PeerConnectionTimeoutMax = 45000 // ms
|
||||||
PeerConnectionTimeoutMin = 30000 // ms
|
PeerConnectionTimeoutMin = 30000 // ms
|
||||||
|
connInitLimit = 200
|
||||||
disableAutoUpdate = "disabled"
|
disableAutoUpdate = "disabled"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -139,6 +141,11 @@ type EngineConfig struct {
|
|||||||
ProfileConfig *profilemanager.Config
|
ProfileConfig *profilemanager.Config
|
||||||
|
|
||||||
LogPath string
|
LogPath string
|
||||||
|
|
||||||
|
// ProxyConfig contains system proxy settings for macOS
|
||||||
|
ProxyEnabled bool
|
||||||
|
ProxyHost string
|
||||||
|
ProxyPort int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Engine is a mechanism responsible for reacting on Signal and Management stream events and managing connections to the remote peers.
|
// Engine is a mechanism responsible for reacting on Signal and Management stream events and managing connections to the remote peers.
|
||||||
@@ -206,6 +213,7 @@ type Engine struct {
|
|||||||
syncRespMux sync.RWMutex
|
syncRespMux sync.RWMutex
|
||||||
persistSyncResponse bool
|
persistSyncResponse bool
|
||||||
latestSyncResponse *mgmProto.SyncResponse
|
latestSyncResponse *mgmProto.SyncResponse
|
||||||
|
connSemaphore *semaphoregroup.SemaphoreGroup
|
||||||
flowManager nftypes.FlowManager
|
flowManager nftypes.FlowManager
|
||||||
|
|
||||||
// auto-update
|
// auto-update
|
||||||
@@ -222,7 +230,8 @@ type Engine struct {
|
|||||||
jobExecutor *jobexec.Executor
|
jobExecutor *jobexec.Executor
|
||||||
jobExecutorWG sync.WaitGroup
|
jobExecutorWG sync.WaitGroup
|
||||||
|
|
||||||
exposeManager *expose.Manager
|
// proxyManager manages system-wide browser proxy settings on macOS
|
||||||
|
proxyManager *proxy.Manager
|
||||||
}
|
}
|
||||||
|
|
||||||
// Peer is an instance of the Connection Peer
|
// Peer is an instance of the Connection Peer
|
||||||
@@ -265,6 +274,7 @@ func NewEngine(
|
|||||||
statusRecorder: statusRecorder,
|
statusRecorder: statusRecorder,
|
||||||
stateManager: stateManager,
|
stateManager: stateManager,
|
||||||
checks: checks,
|
checks: checks,
|
||||||
|
connSemaphore: semaphoregroup.NewSemaphoreGroup(connInitLimit),
|
||||||
probeStunTurn: relay.NewStunTurnProbe(relay.DefaultCacheTTL),
|
probeStunTurn: relay.NewStunTurnProbe(relay.DefaultCacheTTL),
|
||||||
jobExecutor: jobexec.NewExecutor(),
|
jobExecutor: jobexec.NewExecutor(),
|
||||||
}
|
}
|
||||||
@@ -312,6 +322,12 @@ func (e *Engine) Stop() error {
|
|||||||
e.updateManager.Stop()
|
e.updateManager.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if e.proxyManager != nil {
|
||||||
|
if err := e.proxyManager.DisableWebProxy(); err != nil {
|
||||||
|
log.Warnf("failed to disable system proxy: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
log.Info("cleaning up status recorder states")
|
log.Info("cleaning up status recorder states")
|
||||||
e.statusRecorder.ReplaceOfflinePeers([]peer.State{})
|
e.statusRecorder.ReplaceOfflinePeers([]peer.State{})
|
||||||
e.statusRecorder.UpdateDNSStates([]peer.NSGroupState{})
|
e.statusRecorder.UpdateDNSStates([]peer.NSGroupState{})
|
||||||
@@ -417,7 +433,6 @@ func (e *Engine) Start(netbirdConfig *mgmProto.NetbirdConfig, mgmtURL *url.URL)
|
|||||||
e.cancel()
|
e.cancel()
|
||||||
}
|
}
|
||||||
e.ctx, e.cancel = context.WithCancel(e.clientCtx)
|
e.ctx, e.cancel = context.WithCancel(e.clientCtx)
|
||||||
e.exposeManager = expose.NewManager(e.ctx, e.mgmClient)
|
|
||||||
|
|
||||||
wgIface, err := e.newWgIface()
|
wgIface, err := e.newWgIface()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -448,6 +463,10 @@ func (e *Engine) Start(netbirdConfig *mgmProto.NetbirdConfig, mgmtURL *url.URL)
|
|||||||
}
|
}
|
||||||
e.stateManager.Start()
|
e.stateManager.Start()
|
||||||
|
|
||||||
|
// Initialize proxy manager and register state for cleanup
|
||||||
|
proxy.RegisterState(e.stateManager)
|
||||||
|
e.proxyManager = proxy.NewManager(e.stateManager)
|
||||||
|
|
||||||
initialRoutes, dnsConfig, dnsFeatureFlag, err := e.readInitialSettings()
|
initialRoutes, dnsConfig, dnsFeatureFlag, err := e.readInitialSettings()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
e.close()
|
e.close()
|
||||||
@@ -543,12 +562,11 @@ func (e *Engine) Start(netbirdConfig *mgmProto.NetbirdConfig, mgmtURL *url.URL)
|
|||||||
// monitor WireGuard interface lifecycle and restart engine on changes
|
// monitor WireGuard interface lifecycle and restart engine on changes
|
||||||
e.wgIfaceMonitor = NewWGIfaceMonitor()
|
e.wgIfaceMonitor = NewWGIfaceMonitor()
|
||||||
e.shutdownWg.Add(1)
|
e.shutdownWg.Add(1)
|
||||||
wgIfaceName := e.wgInterface.Name()
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer e.shutdownWg.Done()
|
defer e.shutdownWg.Done()
|
||||||
|
|
||||||
if shouldRestart, err := e.wgIfaceMonitor.Start(e.ctx, wgIfaceName); shouldRestart {
|
if shouldRestart, err := e.wgIfaceMonitor.Start(e.ctx, e.wgInterface.Name()); shouldRestart {
|
||||||
log.Infof("WireGuard interface monitor: %s, restarting engine", err)
|
log.Infof("WireGuard interface monitor: %s, restarting engine", err)
|
||||||
e.triggerClientRestart()
|
e.triggerClientRestart()
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
@@ -800,7 +818,7 @@ func (e *Engine) handleAutoUpdateVersion(autoUpdateSettings *mgmProto.AutoUpdate
|
|||||||
|
|
||||||
disabled := autoUpdateSettings.Version == disableAutoUpdate
|
disabled := autoUpdateSettings.Version == disableAutoUpdate
|
||||||
|
|
||||||
// stop and cleanup if disabled
|
// Stop and cleanup if disabled
|
||||||
if e.updateManager != nil && disabled {
|
if e.updateManager != nil && disabled {
|
||||||
log.Infof("auto-update is disabled, stopping update manager")
|
log.Infof("auto-update is disabled, stopping update manager")
|
||||||
e.updateManager.Stop()
|
e.updateManager.Stop()
|
||||||
@@ -829,10 +847,6 @@ func (e *Engine) handleAutoUpdateVersion(autoUpdateSettings *mgmProto.AutoUpdate
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *Engine) handleSync(update *mgmProto.SyncResponse) error {
|
func (e *Engine) handleSync(update *mgmProto.SyncResponse) error {
|
||||||
started := time.Now()
|
|
||||||
defer func() {
|
|
||||||
log.Infof("sync finished in %s", time.Since(started))
|
|
||||||
}()
|
|
||||||
e.syncMsgMux.Lock()
|
e.syncMsgMux.Lock()
|
||||||
defer e.syncMsgMux.Unlock()
|
defer e.syncMsgMux.Unlock()
|
||||||
|
|
||||||
@@ -1022,7 +1036,7 @@ func (e *Engine) updateConfig(conf *mgmProto.PeerConfig) error {
|
|||||||
state := e.statusRecorder.GetLocalPeerState()
|
state := e.statusRecorder.GetLocalPeerState()
|
||||||
state.IP = e.wgInterface.Address().String()
|
state.IP = e.wgInterface.Address().String()
|
||||||
state.PubKey = e.config.WgPrivateKey.PublicKey().String()
|
state.PubKey = e.config.WgPrivateKey.PublicKey().String()
|
||||||
state.KernelInterface = !e.wgInterface.IsUserspaceBind()
|
state.KernelInterface = device.WireGuardModuleIsLoaded()
|
||||||
state.FQDN = conf.GetFqdn()
|
state.FQDN = conf.GetFqdn()
|
||||||
|
|
||||||
e.statusRecorder.UpdateLocalPeerState(state)
|
e.statusRecorder.UpdateLocalPeerState(state)
|
||||||
@@ -1317,6 +1331,9 @@ func (e *Engine) updateNetworkMap(networkMap *mgmProto.NetworkMap) error {
|
|||||||
// If no server of a server group responds this will disable the respective handler and retry later.
|
// If no server of a server group responds this will disable the respective handler and retry later.
|
||||||
e.dnsServer.ProbeAvailability()
|
e.dnsServer.ProbeAvailability()
|
||||||
|
|
||||||
|
// Update system proxy state based on routes after network map is fully applied
|
||||||
|
e.updateSystemProxy(clientRoutes)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1538,6 +1555,7 @@ func (e *Engine) createPeerConn(pubKey string, allowedIPs []netip.Prefix, agentV
|
|||||||
IFaceDiscover: e.mobileDep.IFaceDiscover,
|
IFaceDiscover: e.mobileDep.IFaceDiscover,
|
||||||
RelayManager: e.relayManager,
|
RelayManager: e.relayManager,
|
||||||
SrWatcher: e.srWatcher,
|
SrWatcher: e.srWatcher,
|
||||||
|
Semaphore: e.connSemaphore,
|
||||||
}
|
}
|
||||||
peerConn, err := peer.NewConn(config, serviceDependencies)
|
peerConn, err := peer.NewConn(config, serviceDependencies)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1560,10 +1578,8 @@ func (e *Engine) receiveSignalEvents() {
|
|||||||
defer e.shutdownWg.Done()
|
defer e.shutdownWg.Done()
|
||||||
// connect to a stream of messages coming from the signal server
|
// connect to a stream of messages coming from the signal server
|
||||||
err := e.signal.Receive(e.ctx, func(msg *sProto.Message) error {
|
err := e.signal.Receive(e.ctx, func(msg *sProto.Message) error {
|
||||||
start := time.Now()
|
|
||||||
e.syncMsgMux.Lock()
|
e.syncMsgMux.Lock()
|
||||||
defer e.syncMsgMux.Unlock()
|
defer e.syncMsgMux.Unlock()
|
||||||
gotLock := time.Since(start)
|
|
||||||
|
|
||||||
// Check context INSIDE lock to ensure atomicity with shutdown
|
// Check context INSIDE lock to ensure atomicity with shutdown
|
||||||
if e.ctx.Err() != nil {
|
if e.ctx.Err() != nil {
|
||||||
@@ -1587,8 +1603,6 @@ func (e *Engine) receiveSignalEvents() {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("receiveMSG: took %s to get lock for peer %s with session id %s", gotLock, msg.Key, offerAnswer.SessionID)
|
|
||||||
|
|
||||||
if msg.Body.Type == sProto.Body_OFFER {
|
if msg.Body.Type == sProto.Body_OFFER {
|
||||||
conn.OnRemoteOffer(*offerAnswer)
|
conn.OnRemoteOffer(*offerAnswer)
|
||||||
} else {
|
} else {
|
||||||
@@ -1822,18 +1836,11 @@ func (e *Engine) GetRouteManager() routemanager.Manager {
|
|||||||
return e.routeManager
|
return e.routeManager
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetFirewallManager returns the firewall manager.
|
// GetFirewallManager returns the firewall manager
|
||||||
func (e *Engine) GetFirewallManager() firewallManager.Manager {
|
func (e *Engine) GetFirewallManager() firewallManager.Manager {
|
||||||
return e.firewall
|
return e.firewall
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetExposeManager returns the expose session manager.
|
|
||||||
func (e *Engine) GetExposeManager() *expose.Manager {
|
|
||||||
e.syncMsgMux.Lock()
|
|
||||||
defer e.syncMsgMux.Unlock()
|
|
||||||
return e.exposeManager
|
|
||||||
}
|
|
||||||
|
|
||||||
func findIPFromInterfaceName(ifaceName string) (net.IP, error) {
|
func findIPFromInterfaceName(ifaceName string) (net.IP, error) {
|
||||||
iface, err := net.InterfaceByName(ifaceName)
|
iface, err := net.InterfaceByName(ifaceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1933,7 +1940,7 @@ func (e *Engine) triggerClientRestart() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *Engine) startNetworkMonitor() {
|
func (e *Engine) startNetworkMonitor() {
|
||||||
if !e.config.NetworkMonitor || nbnetstack.IsEnabled() {
|
if !e.config.NetworkMonitor {
|
||||||
log.Infof("Network monitor is disabled, not starting")
|
log.Infof("Network monitor is disabled, not starting")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -2318,6 +2325,26 @@ func createFile(path string) error {
|
|||||||
return file.Close()
|
return file.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// updateSystemProxy triggers a proxy enable/disable cycle after the network map is updated.
|
||||||
|
func (e *Engine) updateSystemProxy(clientRoutes route.HAMap) {
|
||||||
|
if runtime.GOOS != "darwin" || e.proxyManager == nil {
|
||||||
|
log.Errorf("not updating proxy")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := e.proxyManager.EnableWebProxy(e.config.ProxyHost, e.config.ProxyPort); err != nil {
|
||||||
|
log.Errorf("enable system proxy: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Error("system proxy enabled after network map update")
|
||||||
|
|
||||||
|
if err := e.proxyManager.DisableWebProxy(); err != nil {
|
||||||
|
log.Errorf("disable system proxy: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Error("system proxy disabled after network map update")
|
||||||
|
}
|
||||||
|
|
||||||
func convertToOfferAnswer(msg *sProto.Message) (*peer.OfferAnswer, error) {
|
func convertToOfferAnswer(msg *sProto.Message) (*peer.OfferAnswer, error) {
|
||||||
remoteCred, err := signal.UnMarshalCredential(msg)
|
remoteCred, err := signal.UnMarshalCredential(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
firewallManager "github.com/netbirdio/netbird/client/firewall/manager"
|
firewallManager "github.com/netbirdio/netbird/client/firewall/manager"
|
||||||
"github.com/netbirdio/netbird/client/iface/netstack"
|
|
||||||
nftypes "github.com/netbirdio/netbird/client/internal/netflow/types"
|
nftypes "github.com/netbirdio/netbird/client/internal/netflow/types"
|
||||||
sshauth "github.com/netbirdio/netbird/client/ssh/auth"
|
sshauth "github.com/netbirdio/netbird/client/ssh/auth"
|
||||||
sshconfig "github.com/netbirdio/netbird/client/ssh/config"
|
sshconfig "github.com/netbirdio/netbird/client/ssh/config"
|
||||||
@@ -95,10 +94,6 @@ func (e *Engine) updateSSH(sshConf *mgmProto.SSHConfig) error {
|
|||||||
|
|
||||||
// updateSSHClientConfig updates the SSH client configuration with peer information
|
// updateSSHClientConfig updates the SSH client configuration with peer information
|
||||||
func (e *Engine) updateSSHClientConfig(remotePeers []*mgmProto.RemotePeerConfig) error {
|
func (e *Engine) updateSSHClientConfig(remotePeers []*mgmProto.RemotePeerConfig) error {
|
||||||
if netstack.IsEnabled() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
peerInfo := e.extractPeerSSHInfo(remotePeers)
|
peerInfo := e.extractPeerSSHInfo(remotePeers)
|
||||||
if len(peerInfo) == 0 {
|
if len(peerInfo) == 0 {
|
||||||
log.Debug("no SSH-enabled peers found, skipping SSH config update")
|
log.Debug("no SSH-enabled peers found, skipping SSH config update")
|
||||||
@@ -221,10 +216,6 @@ func (e *Engine) GetPeerSSHKey(peerAddress string) ([]byte, bool) {
|
|||||||
|
|
||||||
// cleanupSSHConfig removes NetBird SSH client configuration on shutdown
|
// cleanupSSHConfig removes NetBird SSH client configuration on shutdown
|
||||||
func (e *Engine) cleanupSSHConfig() {
|
func (e *Engine) cleanupSSHConfig() {
|
||||||
if netstack.IsEnabled() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
configMgr := sshconfig.New()
|
configMgr := sshconfig.New()
|
||||||
|
|
||||||
if err := configMgr.RemoveSSHClientConfig(); err != nil {
|
if err := configMgr.RemoveSSHClientConfig(); err != nil {
|
||||||
|
|||||||
@@ -1,95 +0,0 @@
|
|||||||
package expose
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
mgm "github.com/netbirdio/netbird/shared/management/client"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
const renewTimeout = 10 * time.Second
|
|
||||||
|
|
||||||
// Response holds the response from exposing a service.
|
|
||||||
type Response struct {
|
|
||||||
ServiceName string
|
|
||||||
ServiceURL string
|
|
||||||
Domain string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Request struct {
|
|
||||||
NamePrefix string
|
|
||||||
Domain string
|
|
||||||
Port uint16
|
|
||||||
Protocol int
|
|
||||||
Pin string
|
|
||||||
Password string
|
|
||||||
UserGroups []string
|
|
||||||
}
|
|
||||||
|
|
||||||
type ManagementClient interface {
|
|
||||||
CreateExpose(ctx context.Context, req mgm.ExposeRequest) (*mgm.ExposeResponse, error)
|
|
||||||
RenewExpose(ctx context.Context, domain string) error
|
|
||||||
StopExpose(ctx context.Context, domain string) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// Manager handles expose session lifecycle via the management client.
|
|
||||||
type Manager struct {
|
|
||||||
mgmClient ManagementClient
|
|
||||||
ctx context.Context
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewManager creates a new expose Manager using the given management client.
|
|
||||||
func NewManager(ctx context.Context, mgmClient ManagementClient) *Manager {
|
|
||||||
return &Manager{mgmClient: mgmClient, ctx: ctx}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Expose creates a new expose session via the management server.
|
|
||||||
func (m *Manager) Expose(ctx context.Context, req Request) (*Response, error) {
|
|
||||||
log.Infof("exposing service on port %d", req.Port)
|
|
||||||
resp, err := m.mgmClient.CreateExpose(ctx, toClientExposeRequest(req))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Infof("expose session created for %s", resp.Domain)
|
|
||||||
|
|
||||||
return fromClientExposeResponse(resp), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Manager) KeepAlive(ctx context.Context, domain string) error {
|
|
||||||
ticker := time.NewTicker(30 * time.Second)
|
|
||||||
defer ticker.Stop()
|
|
||||||
defer m.stop(domain)
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
log.Infof("context canceled, stopping keep alive for %s", domain)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
case <-ticker.C:
|
|
||||||
if err := m.renew(ctx, domain); err != nil {
|
|
||||||
log.Errorf("renewing expose session for %s: %v", domain, err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// renew extends the TTL of an active expose session.
|
|
||||||
func (m *Manager) renew(ctx context.Context, domain string) error {
|
|
||||||
renewCtx, cancel := context.WithTimeout(ctx, renewTimeout)
|
|
||||||
defer cancel()
|
|
||||||
return m.mgmClient.RenewExpose(renewCtx, domain)
|
|
||||||
}
|
|
||||||
|
|
||||||
// stop terminates an active expose session.
|
|
||||||
func (m *Manager) stop(domain string) {
|
|
||||||
stopCtx, cancel := context.WithTimeout(m.ctx, renewTimeout)
|
|
||||||
defer cancel()
|
|
||||||
err := m.mgmClient.StopExpose(stopCtx, domain)
|
|
||||||
if err != nil {
|
|
||||||
log.Warnf("Failed stopping expose session for %s: %v", domain, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,95 +0,0 @@
|
|||||||
package expose
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
|
|
||||||
daemonProto "github.com/netbirdio/netbird/client/proto"
|
|
||||||
mgm "github.com/netbirdio/netbird/shared/management/client"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestManager_Expose_Success(t *testing.T) {
|
|
||||||
mock := &mgm.MockClient{
|
|
||||||
CreateExposeFunc: func(ctx context.Context, req mgm.ExposeRequest) (*mgm.ExposeResponse, error) {
|
|
||||||
return &mgm.ExposeResponse{
|
|
||||||
ServiceName: "my-service",
|
|
||||||
ServiceURL: "https://my-service.example.com",
|
|
||||||
Domain: "my-service.example.com",
|
|
||||||
}, nil
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
m := NewManager(context.Background(), mock)
|
|
||||||
result, err := m.Expose(context.Background(), Request{Port: 8080})
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, "my-service", result.ServiceName, "service name should match")
|
|
||||||
assert.Equal(t, "https://my-service.example.com", result.ServiceURL, "service URL should match")
|
|
||||||
assert.Equal(t, "my-service.example.com", result.Domain, "domain should match")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestManager_Expose_Error(t *testing.T) {
|
|
||||||
mock := &mgm.MockClient{
|
|
||||||
CreateExposeFunc: func(ctx context.Context, req mgm.ExposeRequest) (*mgm.ExposeResponse, error) {
|
|
||||||
return nil, errors.New("permission denied")
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
m := NewManager(context.Background(), mock)
|
|
||||||
_, err := m.Expose(context.Background(), Request{Port: 8080})
|
|
||||||
require.Error(t, err)
|
|
||||||
assert.Contains(t, err.Error(), "permission denied", "error should propagate")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestManager_Renew_Success(t *testing.T) {
|
|
||||||
mock := &mgm.MockClient{
|
|
||||||
RenewExposeFunc: func(ctx context.Context, domain string) error {
|
|
||||||
assert.Equal(t, "my-service.example.com", domain, "domain should be passed through")
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
m := NewManager(context.Background(), mock)
|
|
||||||
err := m.renew(context.Background(), "my-service.example.com")
|
|
||||||
require.NoError(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestManager_Renew_Timeout(t *testing.T) {
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
cancel()
|
|
||||||
|
|
||||||
mock := &mgm.MockClient{
|
|
||||||
RenewExposeFunc: func(ctx context.Context, domain string) error {
|
|
||||||
return ctx.Err()
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
m := NewManager(ctx, mock)
|
|
||||||
err := m.renew(ctx, "my-service.example.com")
|
|
||||||
require.Error(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNewRequest(t *testing.T) {
|
|
||||||
req := &daemonProto.ExposeServiceRequest{
|
|
||||||
Port: 8080,
|
|
||||||
Protocol: daemonProto.ExposeProtocol_EXPOSE_HTTPS,
|
|
||||||
Pin: "123456",
|
|
||||||
Password: "secret",
|
|
||||||
UserGroups: []string{"group1", "group2"},
|
|
||||||
Domain: "custom.example.com",
|
|
||||||
NamePrefix: "my-prefix",
|
|
||||||
}
|
|
||||||
|
|
||||||
exposeReq := NewRequest(req)
|
|
||||||
|
|
||||||
assert.Equal(t, uint16(8080), exposeReq.Port, "port should match")
|
|
||||||
assert.Equal(t, int(daemonProto.ExposeProtocol_EXPOSE_HTTPS), exposeReq.Protocol, "protocol should match")
|
|
||||||
assert.Equal(t, "123456", exposeReq.Pin, "pin should match")
|
|
||||||
assert.Equal(t, "secret", exposeReq.Password, "password should match")
|
|
||||||
assert.Equal(t, []string{"group1", "group2"}, exposeReq.UserGroups, "user groups should match")
|
|
||||||
assert.Equal(t, "custom.example.com", exposeReq.Domain, "domain should match")
|
|
||||||
assert.Equal(t, "my-prefix", exposeReq.NamePrefix, "name prefix should match")
|
|
||||||
}
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
package expose
|
|
||||||
|
|
||||||
import (
|
|
||||||
daemonProto "github.com/netbirdio/netbird/client/proto"
|
|
||||||
mgm "github.com/netbirdio/netbird/shared/management/client"
|
|
||||||
)
|
|
||||||
|
|
||||||
// NewRequest converts a daemon ExposeServiceRequest to a management ExposeServiceRequest.
|
|
||||||
func NewRequest(req *daemonProto.ExposeServiceRequest) *Request {
|
|
||||||
return &Request{
|
|
||||||
Port: uint16(req.Port),
|
|
||||||
Protocol: int(req.Protocol),
|
|
||||||
Pin: req.Pin,
|
|
||||||
Password: req.Password,
|
|
||||||
UserGroups: req.UserGroups,
|
|
||||||
Domain: req.Domain,
|
|
||||||
NamePrefix: req.NamePrefix,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func toClientExposeRequest(req Request) mgm.ExposeRequest {
|
|
||||||
return mgm.ExposeRequest{
|
|
||||||
NamePrefix: req.NamePrefix,
|
|
||||||
Domain: req.Domain,
|
|
||||||
Port: req.Port,
|
|
||||||
Protocol: req.Protocol,
|
|
||||||
Pin: req.Pin,
|
|
||||||
Password: req.Password,
|
|
||||||
UserGroups: req.UserGroups,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func fromClientExposeResponse(response *mgm.ExposeResponse) *Response {
|
|
||||||
return &Response{
|
|
||||||
ServiceName: response.ServiceName,
|
|
||||||
Domain: response.Domain,
|
|
||||||
ServiceURL: response.ServiceURL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -11,7 +11,6 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||||
|
|
||||||
"github.com/netbirdio/netbird/client/iface/netstack"
|
|
||||||
"github.com/netbirdio/netbird/client/iface/wgaddr"
|
"github.com/netbirdio/netbird/client/iface/wgaddr"
|
||||||
"github.com/netbirdio/netbird/client/internal/lazyconn"
|
"github.com/netbirdio/netbird/client/internal/lazyconn"
|
||||||
peerid "github.com/netbirdio/netbird/client/internal/peer/id"
|
peerid "github.com/netbirdio/netbird/client/internal/peer/id"
|
||||||
@@ -75,13 +74,12 @@ func (m *Manager) createListener(peerCfg lazyconn.PeerConfig) (listener, error)
|
|||||||
return NewUDPListener(m.wgIface, peerCfg)
|
return NewUDPListener(m.wgIface, peerCfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// BindListener is used on Windows, JS, and netstack platforms:
|
// BindListener is only used on Windows and JS platforms:
|
||||||
// - JS: Cannot listen to UDP sockets
|
// - JS: Cannot listen to UDP sockets
|
||||||
// - Windows: IP_UNICAST_IF socket option forces packets out the interface the default
|
// - Windows: IP_UNICAST_IF socket option forces packets out the interface the default
|
||||||
// gateway points to, preventing them from reaching the loopback interface.
|
// gateway points to, preventing them from reaching the loopback interface.
|
||||||
// - Netstack: Allows multiple instances on the same host without port conflicts.
|
// BindListener bypasses this by passing data directly through the bind.
|
||||||
// BindListener bypasses these issues by passing data directly through the bind.
|
if runtime.GOOS != "windows" && runtime.GOOS != "js" {
|
||||||
if runtime.GOOS != "windows" && runtime.GOOS != "js" && !netstack.IsEnabled() {
|
|
||||||
return NewUDPListener(m.wgIface, peerCfg)
|
return NewUDPListener(m.wgIface, peerCfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,24 +22,18 @@ func prepareFd() (int, error) {
|
|||||||
|
|
||||||
func routeCheck(ctx context.Context, fd int, nexthopv4, nexthopv6 systemops.Nexthop) error {
|
func routeCheck(ctx context.Context, fd int, nexthopv4, nexthopv6 systemops.Nexthop) error {
|
||||||
for {
|
for {
|
||||||
// Wait until fd is readable or context is cancelled, to avoid a busy-loop
|
select {
|
||||||
// when the routing socket returns EAGAIN (e.g. immediately after wakeup).
|
case <-ctx.Done():
|
||||||
if err := waitReadable(ctx, fd); err != nil {
|
return ctx.Err()
|
||||||
return err
|
default:
|
||||||
}
|
|
||||||
|
|
||||||
buf := make([]byte, 2048)
|
buf := make([]byte, 2048)
|
||||||
n, err := unix.Read(fd, buf)
|
n, err := unix.Read(fd, buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, unix.EAGAIN) || errors.Is(err, unix.EINTR) {
|
if !errors.Is(err, unix.EBADF) && !errors.Is(err, unix.EINVAL) {
|
||||||
|
log.Warnf("Network monitor: failed to read from routing socket: %v", err)
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if errors.Is(err, unix.EBADF) || errors.Is(err, unix.EINVAL) {
|
|
||||||
return fmt.Errorf("routing socket closed: %w", err)
|
|
||||||
}
|
|
||||||
return fmt.Errorf("read routing socket: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if n < unix.SizeofRtMsghdr {
|
if n < unix.SizeofRtMsghdr {
|
||||||
log.Debugf("Network monitor: read from routing socket returned less than expected: %d bytes", n)
|
log.Debugf("Network monitor: read from routing socket returned less than expected: %d bytes", n)
|
||||||
continue
|
continue
|
||||||
@@ -77,6 +71,7 @@ func routeCheck(ctx context.Context, fd int, nexthopv4, nexthopv6 systemops.Next
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func parseRouteMessage(buf []byte) (*systemops.Route, error) {
|
func parseRouteMessage(buf []byte) (*systemops.Route, error) {
|
||||||
msgs, err := route.ParseRIB(route.RIBTypeRoute, buf)
|
msgs, err := route.ParseRIB(route.RIBTypeRoute, buf)
|
||||||
@@ -95,33 +90,3 @@ func parseRouteMessage(buf []byte) (*systemops.Route, error) {
|
|||||||
|
|
||||||
return systemops.MsgToRoute(msg)
|
return systemops.MsgToRoute(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// waitReadable blocks until fd has data to read, or ctx is cancelled.
|
|
||||||
func waitReadable(ctx context.Context, fd int) error {
|
|
||||||
var fdset unix.FdSet
|
|
||||||
if fd < 0 || fd/unix.NFDBITS >= len(fdset.Bits) {
|
|
||||||
return fmt.Errorf("fd %d out of range for FdSet", fd)
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
|
||||||
if err := ctx.Err(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
fdset = unix.FdSet{}
|
|
||||||
fdset.Set(fd)
|
|
||||||
// Use a 1-second timeout so we can re-check ctx periodically.
|
|
||||||
tv := unix.Timeval{Sec: 1}
|
|
||||||
n, err := unix.Select(fd+1, &fdset, nil, nil, &tv)
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, unix.EINTR) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
return fmt.Errorf("select on routing socket: %w", err)
|
|
||||||
}
|
|
||||||
if n > 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
// timeout — loop back and re-check ctx
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import (
|
|||||||
"github.com/cenkalti/backoff/v4"
|
"github.com/cenkalti/backoff/v4"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/netbirdio/netbird/client/iface/netstack"
|
||||||
"github.com/netbirdio/netbird/client/internal/routemanager/systemops"
|
"github.com/netbirdio/netbird/client/internal/routemanager/systemops"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -37,6 +38,11 @@ func New() *NetworkMonitor {
|
|||||||
|
|
||||||
// Listen begins monitoring network changes. When a change is detected, this function will return without error.
|
// Listen begins monitoring network changes. When a change is detected, this function will return without error.
|
||||||
func (nw *NetworkMonitor) Listen(ctx context.Context) (err error) {
|
func (nw *NetworkMonitor) Listen(ctx context.Context) (err error) {
|
||||||
|
if netstack.IsEnabled() {
|
||||||
|
log.Debugf("Network monitor: skipping in netstack mode")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
nw.mu.Lock()
|
nw.mu.Lock()
|
||||||
if nw.cancel != nil {
|
if nw.cancel != nil {
|
||||||
nw.mu.Unlock()
|
nw.mu.Unlock()
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package peer
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -24,6 +25,7 @@ import (
|
|||||||
"github.com/netbirdio/netbird/client/internal/stdnet"
|
"github.com/netbirdio/netbird/client/internal/stdnet"
|
||||||
"github.com/netbirdio/netbird/route"
|
"github.com/netbirdio/netbird/route"
|
||||||
relayClient "github.com/netbirdio/netbird/shared/relay/client"
|
relayClient "github.com/netbirdio/netbird/shared/relay/client"
|
||||||
|
semaphoregroup "github.com/netbirdio/netbird/util/semaphore-group"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ServiceDependencies struct {
|
type ServiceDependencies struct {
|
||||||
@@ -32,6 +34,7 @@ type ServiceDependencies struct {
|
|||||||
IFaceDiscover stdnet.ExternalIFaceDiscover
|
IFaceDiscover stdnet.ExternalIFaceDiscover
|
||||||
RelayManager *relayClient.Manager
|
RelayManager *relayClient.Manager
|
||||||
SrWatcher *guard.SRWatcher
|
SrWatcher *guard.SRWatcher
|
||||||
|
Semaphore *semaphoregroup.SemaphoreGroup
|
||||||
PeerConnDispatcher *dispatcher.ConnectionDispatcher
|
PeerConnDispatcher *dispatcher.ConnectionDispatcher
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,6 +112,7 @@ type Conn struct {
|
|||||||
handshaker *Handshaker
|
handshaker *Handshaker
|
||||||
|
|
||||||
guard *guard.Guard
|
guard *guard.Guard
|
||||||
|
semaphore *semaphoregroup.SemaphoreGroup
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
|
|
||||||
// debug purpose
|
// debug purpose
|
||||||
@@ -135,6 +139,7 @@ func NewConn(config ConnConfig, services ServiceDependencies) (*Conn, error) {
|
|||||||
iFaceDiscover: services.IFaceDiscover,
|
iFaceDiscover: services.IFaceDiscover,
|
||||||
relayManager: services.RelayManager,
|
relayManager: services.RelayManager,
|
||||||
srWatcher: services.SrWatcher,
|
srWatcher: services.SrWatcher,
|
||||||
|
semaphore: services.Semaphore,
|
||||||
statusRelay: worker.NewAtomicStatus(),
|
statusRelay: worker.NewAtomicStatus(),
|
||||||
statusICE: worker.NewAtomicStatus(),
|
statusICE: worker.NewAtomicStatus(),
|
||||||
dumpState: dumpState,
|
dumpState: dumpState,
|
||||||
@@ -149,10 +154,15 @@ func NewConn(config ConnConfig, services ServiceDependencies) (*Conn, error) {
|
|||||||
// It will try to establish a connection using ICE and in parallel with relay. The higher priority connection type will
|
// It will try to establish a connection using ICE and in parallel with relay. The higher priority connection type will
|
||||||
// be used.
|
// be used.
|
||||||
func (conn *Conn) Open(engineCtx context.Context) error {
|
func (conn *Conn) Open(engineCtx context.Context) error {
|
||||||
|
if err := conn.semaphore.Add(engineCtx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
conn.mu.Lock()
|
conn.mu.Lock()
|
||||||
defer conn.mu.Unlock()
|
defer conn.mu.Unlock()
|
||||||
|
|
||||||
if conn.opened {
|
if conn.opened {
|
||||||
|
conn.semaphore.Done()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,6 +173,7 @@ func (conn *Conn) Open(engineCtx context.Context) error {
|
|||||||
relayIsSupportedLocally := conn.workerRelay.RelayIsSupportedLocally()
|
relayIsSupportedLocally := conn.workerRelay.RelayIsSupportedLocally()
|
||||||
workerICE, err := NewWorkerICE(conn.ctx, conn.Log, conn.config, conn, conn.signaler, conn.iFaceDiscover, conn.statusRecorder, relayIsSupportedLocally)
|
workerICE, err := NewWorkerICE(conn.ctx, conn.Log, conn.config, conn, conn.signaler, conn.iFaceDiscover, conn.statusRecorder, relayIsSupportedLocally)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
conn.semaphore.Done()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
conn.workerICE = workerICE
|
conn.workerICE = workerICE
|
||||||
@@ -196,6 +207,10 @@ func (conn *Conn) Open(engineCtx context.Context) error {
|
|||||||
conn.wg.Add(1)
|
conn.wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer conn.wg.Done()
|
defer conn.wg.Done()
|
||||||
|
|
||||||
|
conn.waitInitialRandomSleepTime(conn.ctx)
|
||||||
|
conn.semaphore.Done()
|
||||||
|
|
||||||
conn.guard.Start(conn.ctx, conn.onGuardEvent)
|
conn.guard.Start(conn.ctx, conn.onGuardEvent)
|
||||||
}()
|
}()
|
||||||
conn.opened = true
|
conn.opened = true
|
||||||
@@ -395,7 +410,7 @@ func (conn *Conn) onICEConnectionIsReady(priority conntype.ConnPriority, iceConn
|
|||||||
conn.doOnConnected(iceConnInfo.RosenpassPubKey, iceConnInfo.RosenpassAddr)
|
conn.doOnConnected(iceConnInfo.RosenpassPubKey, iceConnInfo.RosenpassAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *Conn) onICEStateDisconnected(sessionChanged bool) {
|
func (conn *Conn) onICEStateDisconnected() {
|
||||||
conn.mu.Lock()
|
conn.mu.Lock()
|
||||||
defer conn.mu.Unlock()
|
defer conn.mu.Unlock()
|
||||||
|
|
||||||
@@ -415,18 +430,14 @@ func (conn *Conn) onICEStateDisconnected(sessionChanged bool) {
|
|||||||
if conn.isReadyToUpgrade() {
|
if conn.isReadyToUpgrade() {
|
||||||
conn.Log.Infof("ICE disconnected, set Relay to active connection")
|
conn.Log.Infof("ICE disconnected, set Relay to active connection")
|
||||||
conn.dumpState.SwitchToRelay()
|
conn.dumpState.SwitchToRelay()
|
||||||
if sessionChanged {
|
|
||||||
conn.resetEndpoint()
|
|
||||||
}
|
|
||||||
|
|
||||||
// todo consider to move after the ConfigureWGEndpoint
|
|
||||||
conn.wgProxyRelay.Work()
|
conn.wgProxyRelay.Work()
|
||||||
|
|
||||||
presharedKey := conn.presharedKey(conn.rosenpassRemoteKey)
|
presharedKey := conn.presharedKey(conn.rosenpassRemoteKey)
|
||||||
if err := conn.endpointUpdater.SwitchWGEndpoint(conn.wgProxyRelay.EndpointAddr(), presharedKey); err != nil {
|
if err := conn.endpointUpdater.ConfigureWGEndpoint(conn.wgProxyRelay.EndpointAddr(), presharedKey); err != nil {
|
||||||
conn.Log.Errorf("failed to switch to relay conn: %v", err)
|
conn.Log.Errorf("failed to switch to relay conn: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
conn.wgProxyRelay.Work()
|
||||||
conn.currentConnPriority = conntype.Relay
|
conn.currentConnPriority = conntype.Relay
|
||||||
} else {
|
} else {
|
||||||
conn.Log.Infof("ICE disconnected, do not switch to Relay. Reset priority to: %s", conntype.None.String())
|
conn.Log.Infof("ICE disconnected, do not switch to Relay. Reset priority to: %s", conntype.None.String())
|
||||||
@@ -488,22 +499,20 @@ func (conn *Conn) onRelayConnectionIsReady(rci RelayConnInfo) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
controller := isController(conn.config)
|
|
||||||
|
|
||||||
if controller {
|
|
||||||
wgProxy.Work()
|
wgProxy.Work()
|
||||||
}
|
presharedKey := conn.presharedKey(rci.rosenpassPubKey)
|
||||||
|
|
||||||
conn.enableWgWatcherIfNeeded()
|
conn.enableWgWatcherIfNeeded()
|
||||||
if err := conn.endpointUpdater.ConfigureWGEndpoint(wgProxy.EndpointAddr(), conn.presharedKey(rci.rosenpassPubKey)); err != nil {
|
|
||||||
|
if err := conn.endpointUpdater.ConfigureWGEndpoint(wgProxy.EndpointAddr(), presharedKey); err != nil {
|
||||||
if err := wgProxy.CloseConn(); err != nil {
|
if err := wgProxy.CloseConn(); err != nil {
|
||||||
conn.Log.Warnf("Failed to close relay connection: %v", err)
|
conn.Log.Warnf("Failed to close relay connection: %v", err)
|
||||||
}
|
}
|
||||||
conn.Log.Errorf("Failed to update WireGuard peer configuration: %v", err)
|
conn.Log.Errorf("Failed to update WireGuard peer configuration: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !controller {
|
|
||||||
wgProxy.Work()
|
wgConfigWorkaround()
|
||||||
}
|
|
||||||
conn.rosenpassRemoteKey = rci.rosenpassPubKey
|
conn.rosenpassRemoteKey = rci.rosenpassPubKey
|
||||||
conn.currentConnPriority = conntype.Relay
|
conn.currentConnPriority = conntype.Relay
|
||||||
conn.statusRelay.SetConnected()
|
conn.statusRelay.SetConnected()
|
||||||
@@ -655,6 +664,19 @@ func (conn *Conn) doOnConnected(remoteRosenpassPubKey []byte, remoteRosenpassAdd
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (conn *Conn) waitInitialRandomSleepTime(ctx context.Context) {
|
||||||
|
maxWait := 300
|
||||||
|
duration := time.Duration(rand.Intn(maxWait)) * time.Millisecond
|
||||||
|
|
||||||
|
timeout := time.NewTimer(duration)
|
||||||
|
defer timeout.Stop()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
case <-timeout.C:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (conn *Conn) isRelayed() bool {
|
func (conn *Conn) isRelayed() bool {
|
||||||
switch conn.currentConnPriority {
|
switch conn.currentConnPriority {
|
||||||
case conntype.Relay, conntype.ICETurn:
|
case conntype.Relay, conntype.ICETurn:
|
||||||
@@ -735,17 +757,6 @@ func (conn *Conn) newProxy(remoteConn net.Conn) (wgproxy.Proxy, error) {
|
|||||||
return wgProxy, nil
|
return wgProxy, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (conn *Conn) resetEndpoint() {
|
|
||||||
if !isController(conn.config) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
conn.Log.Infof("reset wg endpoint")
|
|
||||||
conn.wgWatcher.Reset()
|
|
||||||
if err := conn.endpointUpdater.RemoveEndpointAddress(); err != nil {
|
|
||||||
conn.Log.Warnf("failed to remove endpoint address before update: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (conn *Conn) isReadyToUpgrade() bool {
|
func (conn *Conn) isReadyToUpgrade() bool {
|
||||||
return conn.wgProxyRelay != nil && conn.currentConnPriority != conntype.Relay
|
return conn.wgProxyRelay != nil && conn.currentConnPriority != conntype.Relay
|
||||||
}
|
}
|
||||||
@@ -851,3 +862,9 @@ func isController(config ConnConfig) bool {
|
|||||||
func isRosenpassEnabled(remoteRosenpassPubKey []byte) bool {
|
func isRosenpassEnabled(remoteRosenpassPubKey []byte) bool {
|
||||||
return remoteRosenpassPubKey != nil
|
return remoteRosenpassPubKey != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// wgConfigWorkaround is a workaround for the issue with WireGuard configuration update
|
||||||
|
// When update a peer configuration in near to each other time, the second update can be ignored by WireGuard
|
||||||
|
func wgConfigWorkaround() {
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import (
|
|||||||
"github.com/netbirdio/netbird/client/internal/peer/ice"
|
"github.com/netbirdio/netbird/client/internal/peer/ice"
|
||||||
"github.com/netbirdio/netbird/client/internal/stdnet"
|
"github.com/netbirdio/netbird/client/internal/stdnet"
|
||||||
"github.com/netbirdio/netbird/util"
|
"github.com/netbirdio/netbird/util"
|
||||||
|
semaphoregroup "github.com/netbirdio/netbird/util/semaphore-group"
|
||||||
)
|
)
|
||||||
|
|
||||||
var testDispatcher = dispatcher.NewConnectionDispatcher()
|
var testDispatcher = dispatcher.NewConnectionDispatcher()
|
||||||
@@ -52,6 +53,7 @@ func TestConn_GetKey(t *testing.T) {
|
|||||||
|
|
||||||
sd := ServiceDependencies{
|
sd := ServiceDependencies{
|
||||||
SrWatcher: swWatcher,
|
SrWatcher: swWatcher,
|
||||||
|
Semaphore: semaphoregroup.NewSemaphoreGroup(1),
|
||||||
PeerConnDispatcher: testDispatcher,
|
PeerConnDispatcher: testDispatcher,
|
||||||
}
|
}
|
||||||
conn, err := NewConn(connConf, sd)
|
conn, err := NewConn(connConf, sd)
|
||||||
@@ -69,6 +71,7 @@ func TestConn_OnRemoteOffer(t *testing.T) {
|
|||||||
sd := ServiceDependencies{
|
sd := ServiceDependencies{
|
||||||
StatusRecorder: NewRecorder("https://mgm"),
|
StatusRecorder: NewRecorder("https://mgm"),
|
||||||
SrWatcher: swWatcher,
|
SrWatcher: swWatcher,
|
||||||
|
Semaphore: semaphoregroup.NewSemaphoreGroup(1),
|
||||||
PeerConnDispatcher: testDispatcher,
|
PeerConnDispatcher: testDispatcher,
|
||||||
}
|
}
|
||||||
conn, err := NewConn(connConf, sd)
|
conn, err := NewConn(connConf, sd)
|
||||||
@@ -107,6 +110,7 @@ func TestConn_OnRemoteAnswer(t *testing.T) {
|
|||||||
sd := ServiceDependencies{
|
sd := ServiceDependencies{
|
||||||
StatusRecorder: NewRecorder("https://mgm"),
|
StatusRecorder: NewRecorder("https://mgm"),
|
||||||
SrWatcher: swWatcher,
|
SrWatcher: swWatcher,
|
||||||
|
Semaphore: semaphoregroup.NewSemaphoreGroup(1),
|
||||||
PeerConnDispatcher: testDispatcher,
|
PeerConnDispatcher: testDispatcher,
|
||||||
}
|
}
|
||||||
conn, err := NewConn(connConf, sd)
|
conn, err := NewConn(connConf, sd)
|
||||||
|
|||||||
@@ -34,27 +34,28 @@ func NewEndpointUpdater(log *logrus.Entry, wgConfig WgConfig, initiator bool) *E
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ConfigureWGEndpoint sets up the WireGuard endpoint configuration.
|
||||||
|
// The initiator immediately configures the endpoint, while the non-initiator
|
||||||
|
// waits for a fallback period before configuring to avoid handshake congestion.
|
||||||
func (e *EndpointUpdater) ConfigureWGEndpoint(addr *net.UDPAddr, presharedKey *wgtypes.Key) error {
|
func (e *EndpointUpdater) ConfigureWGEndpoint(addr *net.UDPAddr, presharedKey *wgtypes.Key) error {
|
||||||
e.mu.Lock()
|
e.mu.Lock()
|
||||||
defer e.mu.Unlock()
|
defer e.mu.Unlock()
|
||||||
|
|
||||||
if e.initiator {
|
if e.initiator {
|
||||||
e.log.Debugf("configure up WireGuard as initiator")
|
e.log.Debugf("configure up WireGuard as initiatr")
|
||||||
return e.configureAsInitiator(addr, presharedKey)
|
return e.updateWireGuardPeer(addr, presharedKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
e.log.Debugf("configure up WireGuard as responder")
|
|
||||||
return e.configureAsResponder(addr, presharedKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *EndpointUpdater) SwitchWGEndpoint(addr *net.UDPAddr, presharedKey *wgtypes.Key) error {
|
|
||||||
e.mu.Lock()
|
|
||||||
defer e.mu.Unlock()
|
|
||||||
|
|
||||||
// prevent to run new update while cancel the previous update
|
// prevent to run new update while cancel the previous update
|
||||||
e.waitForCloseTheDelayedUpdate()
|
e.waitForCloseTheDelayedUpdate()
|
||||||
|
|
||||||
return e.updateWireGuardPeer(addr, presharedKey)
|
var ctx context.Context
|
||||||
|
ctx, e.cancelFunc = context.WithCancel(context.Background())
|
||||||
|
e.updateWg.Add(1)
|
||||||
|
go e.scheduleDelayedUpdate(ctx, addr, presharedKey)
|
||||||
|
|
||||||
|
e.log.Debugf("configure up WireGuard and wait for handshake")
|
||||||
|
return e.updateWireGuardPeer(nil, presharedKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *EndpointUpdater) RemoveWgPeer() error {
|
func (e *EndpointUpdater) RemoveWgPeer() error {
|
||||||
@@ -65,38 +66,6 @@ func (e *EndpointUpdater) RemoveWgPeer() error {
|
|||||||
return e.wgConfig.WgInterface.RemovePeer(e.wgConfig.RemoteKey)
|
return e.wgConfig.WgInterface.RemovePeer(e.wgConfig.RemoteKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *EndpointUpdater) RemoveEndpointAddress() error {
|
|
||||||
e.mu.Lock()
|
|
||||||
defer e.mu.Unlock()
|
|
||||||
|
|
||||||
e.waitForCloseTheDelayedUpdate()
|
|
||||||
return e.wgConfig.WgInterface.RemoveEndpointAddress(e.wgConfig.RemoteKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *EndpointUpdater) configureAsInitiator(addr *net.UDPAddr, presharedKey *wgtypes.Key) error {
|
|
||||||
if err := e.updateWireGuardPeer(addr, presharedKey); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *EndpointUpdater) configureAsResponder(addr *net.UDPAddr, presharedKey *wgtypes.Key) error {
|
|
||||||
// prevent to run new update while cancel the previous update
|
|
||||||
e.waitForCloseTheDelayedUpdate()
|
|
||||||
|
|
||||||
e.log.Debugf("configure up WireGuard and wait for handshake")
|
|
||||||
var ctx context.Context
|
|
||||||
ctx, e.cancelFunc = context.WithCancel(context.Background())
|
|
||||||
e.updateWg.Add(1)
|
|
||||||
go e.scheduleDelayedUpdate(ctx, addr, presharedKey)
|
|
||||||
|
|
||||||
if err := e.updateWireGuardPeer(nil, presharedKey); err != nil {
|
|
||||||
e.waitForCloseTheDelayedUpdate()
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *EndpointUpdater) waitForCloseTheDelayedUpdate() {
|
func (e *EndpointUpdater) waitForCloseTheDelayedUpdate() {
|
||||||
if e.cancelFunc == nil {
|
if e.cancelFunc == nil {
|
||||||
return
|
return
|
||||||
@@ -132,9 +101,3 @@ func (e *EndpointUpdater) updateWireGuardPeer(endpoint *net.UDPAddr, presharedKe
|
|||||||
presharedKey,
|
presharedKey,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// wgConfigWorkaround is a workaround for the issue with WireGuard configuration update
|
|
||||||
// When update a peer configuration in near to each other time, the second update can be ignored by WireGuard
|
|
||||||
func wgConfigWorkaround() {
|
|
||||||
time.Sleep(100 * time.Millisecond)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package ice
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -33,6 +32,24 @@ type ThreadSafeAgent struct {
|
|||||||
once sync.Once
|
once sync.Once
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *ThreadSafeAgent) Close() error {
|
||||||
|
var err error
|
||||||
|
a.once.Do(func() {
|
||||||
|
done := make(chan error, 1)
|
||||||
|
go func() {
|
||||||
|
done <- a.Agent.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case err = <-done:
|
||||||
|
case <-time.After(iceAgentCloseTimeout):
|
||||||
|
log.Warnf("ICE agent close timed out after %v, proceeding with cleanup", iceAgentCloseTimeout)
|
||||||
|
err = nil
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func NewAgent(ctx context.Context, iFaceDiscover stdnet.ExternalIFaceDiscover, config Config, candidateTypes []ice.CandidateType, ufrag string, pwd string) (*ThreadSafeAgent, error) {
|
func NewAgent(ctx context.Context, iFaceDiscover stdnet.ExternalIFaceDiscover, config Config, candidateTypes []ice.CandidateType, ufrag string, pwd string) (*ThreadSafeAgent, error) {
|
||||||
iceKeepAlive := iceKeepAlive()
|
iceKeepAlive := iceKeepAlive()
|
||||||
iceDisconnectedTimeout := iceDisconnectedTimeout()
|
iceDisconnectedTimeout := iceDisconnectedTimeout()
|
||||||
@@ -76,41 +93,9 @@ func NewAgent(ctx context.Context, iFaceDiscover stdnet.ExternalIFaceDiscover, c
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if agent == nil {
|
|
||||||
return nil, fmt.Errorf("ice.NewAgent returned nil agent without error")
|
|
||||||
}
|
|
||||||
|
|
||||||
return &ThreadSafeAgent{Agent: agent}, nil
|
return &ThreadSafeAgent{Agent: agent}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ThreadSafeAgent) Close() error {
|
|
||||||
var err error
|
|
||||||
a.once.Do(func() {
|
|
||||||
// Defensive check to prevent nil pointer dereference
|
|
||||||
// This can happen during sleep/wake transitions or memory corruption scenarios
|
|
||||||
// github.com/netbirdio/netbird/client/internal/peer/ice.(*ThreadSafeAgent).Close(0x40006883f0?)
|
|
||||||
// [signal 0xc0000005 code=0x0 addr=0x0 pc=0x7ff7e73af83c]
|
|
||||||
agent := a.Agent
|
|
||||||
if agent == nil {
|
|
||||||
log.Warnf("ICE agent is nil during close, skipping")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
done := make(chan error, 1)
|
|
||||||
go func() {
|
|
||||||
done <- agent.Close()
|
|
||||||
}()
|
|
||||||
|
|
||||||
select {
|
|
||||||
case err = <-done:
|
|
||||||
case <-time.After(iceAgentCloseTimeout):
|
|
||||||
log.Warnf("ICE agent close timed out after %v, proceeding with cleanup", iceAgentCloseTimeout)
|
|
||||||
err = nil
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func GenerateICECredentials() (string, string, error) {
|
func GenerateICECredentials() (string, string, error) {
|
||||||
ufrag, err := randutil.GenerateCryptoRandomString(lenUFrag, runesAlpha)
|
ufrag, err := randutil.GenerateCryptoRandomString(lenUFrag, runesAlpha)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -32,8 +32,6 @@ type WGWatcher struct {
|
|||||||
|
|
||||||
enabled bool
|
enabled bool
|
||||||
muEnabled sync.RWMutex
|
muEnabled sync.RWMutex
|
||||||
|
|
||||||
resetCh chan struct{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWGWatcher(log *log.Entry, wgIfaceStater WGInterfaceStater, peerKey string, stateDump *stateDump) *WGWatcher {
|
func NewWGWatcher(log *log.Entry, wgIfaceStater WGInterfaceStater, peerKey string, stateDump *stateDump) *WGWatcher {
|
||||||
@@ -42,7 +40,6 @@ func NewWGWatcher(log *log.Entry, wgIfaceStater WGInterfaceStater, peerKey strin
|
|||||||
wgIfaceStater: wgIfaceStater,
|
wgIfaceStater: wgIfaceStater,
|
||||||
peerKey: peerKey,
|
peerKey: peerKey,
|
||||||
stateDump: stateDump,
|
stateDump: stateDump,
|
||||||
resetCh: make(chan struct{}, 1),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,15 +76,6 @@ func (w *WGWatcher) IsEnabled() bool {
|
|||||||
return w.enabled
|
return w.enabled
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset signals the watcher that the WireGuard peer has been reset and a new
|
|
||||||
// handshake is expected. This restarts the handshake timeout from scratch.
|
|
||||||
func (w *WGWatcher) Reset() {
|
|
||||||
select {
|
|
||||||
case w.resetCh <- struct{}{}:
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// wgStateCheck help to check the state of the WireGuard handshake and relay connection
|
// wgStateCheck help to check the state of the WireGuard handshake and relay connection
|
||||||
func (w *WGWatcher) periodicHandshakeCheck(ctx context.Context, onDisconnectedFn func(), enabledTime time.Time, initialHandshake time.Time) {
|
func (w *WGWatcher) periodicHandshakeCheck(ctx context.Context, onDisconnectedFn func(), enabledTime time.Time, initialHandshake time.Time) {
|
||||||
w.log.Infof("WireGuard watcher started")
|
w.log.Infof("WireGuard watcher started")
|
||||||
@@ -117,12 +105,6 @@ func (w *WGWatcher) periodicHandshakeCheck(ctx context.Context, onDisconnectedFn
|
|||||||
w.stateDump.WGcheckSuccess()
|
w.stateDump.WGcheckSuccess()
|
||||||
|
|
||||||
w.log.Debugf("WireGuard watcher reset timer: %v", resetTime)
|
w.log.Debugf("WireGuard watcher reset timer: %v", resetTime)
|
||||||
case <-w.resetCh:
|
|
||||||
w.log.Infof("WireGuard watcher received peer reset, restarting handshake timeout")
|
|
||||||
lastHandshake = time.Time{}
|
|
||||||
enabledTime = time.Now()
|
|
||||||
timer.Stop()
|
|
||||||
timer.Reset(wgHandshakeOvertime)
|
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
w.log.Infof("WireGuard watcher stopped")
|
w.log.Infof("WireGuard watcher stopped")
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -53,7 +53,6 @@ type WorkerICE struct {
|
|||||||
// with it the remote peer can discard the already deprecated offer/answer
|
// with it the remote peer can discard the already deprecated offer/answer
|
||||||
// Without it the remote peer may recreate a workable ICE connection
|
// Without it the remote peer may recreate a workable ICE connection
|
||||||
sessionID ICESessionID
|
sessionID ICESessionID
|
||||||
remoteSessionChanged bool
|
|
||||||
muxAgent sync.Mutex
|
muxAgent sync.Mutex
|
||||||
|
|
||||||
localUfrag string
|
localUfrag string
|
||||||
@@ -107,13 +106,10 @@ func (w *WorkerICE) OnNewOffer(remoteOfferAnswer *OfferAnswer) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.log.Debugf("agent already exists, recreate the connection")
|
w.log.Debugf("agent already exists, recreate the connection")
|
||||||
w.remoteSessionChanged = true
|
|
||||||
w.agentDialerCancel()
|
w.agentDialerCancel()
|
||||||
if w.agent != nil {
|
|
||||||
if err := w.agent.Close(); err != nil {
|
if err := w.agent.Close(); err != nil {
|
||||||
w.log.Warnf("failed to close ICE agent: %s", err)
|
w.log.Warnf("failed to close ICE agent: %s", err)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
sessionID, err := NewICESessionID()
|
sessionID, err := NewICESessionID()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -308,17 +304,13 @@ func (w *WorkerICE) connect(ctx context.Context, agent *icemaker.ThreadSafeAgent
|
|||||||
w.conn.onICEConnectionIsReady(selectedPriority(pair), ci)
|
w.conn.onICEConnectionIsReady(selectedPriority(pair), ci)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WorkerICE) closeAgent(agent *icemaker.ThreadSafeAgent, cancel context.CancelFunc) bool {
|
func (w *WorkerICE) closeAgent(agent *icemaker.ThreadSafeAgent, cancel context.CancelFunc) {
|
||||||
cancel()
|
cancel()
|
||||||
if err := agent.Close(); err != nil {
|
if err := agent.Close(); err != nil {
|
||||||
w.log.Warnf("failed to close ICE agent: %s", err)
|
w.log.Warnf("failed to close ICE agent: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
w.muxAgent.Lock()
|
w.muxAgent.Lock()
|
||||||
defer w.muxAgent.Unlock()
|
|
||||||
|
|
||||||
sessionChanged := w.remoteSessionChanged
|
|
||||||
w.remoteSessionChanged = false
|
|
||||||
|
|
||||||
if w.agent == agent {
|
if w.agent == agent {
|
||||||
// consider to remove from here and move to the OnNewOffer
|
// consider to remove from here and move to the OnNewOffer
|
||||||
@@ -331,7 +323,7 @@ func (w *WorkerICE) closeAgent(agent *icemaker.ThreadSafeAgent, cancel context.C
|
|||||||
w.agentConnecting = false
|
w.agentConnecting = false
|
||||||
w.remoteSessionID = ""
|
w.remoteSessionID = ""
|
||||||
}
|
}
|
||||||
return sessionChanged
|
w.muxAgent.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WorkerICE) punchRemoteWGPort(pair *ice.CandidatePair, remoteWgPort int) {
|
func (w *WorkerICE) punchRemoteWGPort(pair *ice.CandidatePair, remoteWgPort int) {
|
||||||
@@ -432,11 +424,11 @@ func (w *WorkerICE) onConnectionStateChange(agent *icemaker.ThreadSafeAgent, dia
|
|||||||
// ice.ConnectionStateClosed happens when we recreate the agent. For the P2P to TURN switch important to
|
// ice.ConnectionStateClosed happens when we recreate the agent. For the P2P to TURN switch important to
|
||||||
// notify the conn.onICEStateDisconnected changes to update the current used priority
|
// notify the conn.onICEStateDisconnected changes to update the current used priority
|
||||||
|
|
||||||
sessionChanged := w.closeAgent(agent, dialerCancel)
|
w.closeAgent(agent, dialerCancel)
|
||||||
|
|
||||||
if w.lastKnownState == ice.ConnectionStateConnected {
|
if w.lastKnownState == ice.ConnectionStateConnected {
|
||||||
w.lastKnownState = ice.ConnectionStateDisconnected
|
w.lastKnownState = ice.ConnectionStateDisconnected
|
||||||
w.conn.onICEStateDisconnected(sessionChanged)
|
w.conn.onICEStateDisconnected()
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -198,7 +198,7 @@ func getConfigDirForUser(username string) (string, error) {
|
|||||||
|
|
||||||
configDir := filepath.Join(DefaultConfigPathDir, username)
|
configDir := filepath.Join(DefaultConfigPathDir, username)
|
||||||
if _, err := os.Stat(configDir); os.IsNotExist(err) {
|
if _, err := os.Stat(configDir); os.IsNotExist(err) {
|
||||||
if err := os.MkdirAll(configDir, 0700); err != nil {
|
if err := os.MkdirAll(configDir, 0600); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -206,15 +206,9 @@ func getConfigDirForUser(username string) (string, error) {
|
|||||||
return configDir, nil
|
return configDir, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func fileExists(path string) (bool, error) {
|
func fileExists(path string) bool {
|
||||||
_, err := os.Stat(path)
|
_, err := os.Stat(path)
|
||||||
if err == nil {
|
return !os.IsNotExist(err)
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
return false, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// createNewConfig creates a new config generating a new Wireguard key and saving to file
|
// createNewConfig creates a new config generating a new Wireguard key and saving to file
|
||||||
@@ -258,7 +252,7 @@ func (config *Config) apply(input ConfigInput) (updated bool, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if config.AdminURL == nil {
|
if config.AdminURL == nil {
|
||||||
log.Infof("using default Admin URL %s", DefaultAdminURL)
|
log.Infof("using default Admin URL %s", DefaultManagementURL)
|
||||||
config.AdminURL, err = parseURL("Admin URL", DefaultAdminURL)
|
config.AdminURL, err = parseURL("Admin URL", DefaultAdminURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
@@ -641,11 +635,7 @@ func isPreSharedKeyHidden(preSharedKey *string) bool {
|
|||||||
|
|
||||||
// UpdateConfig update existing configuration according to input configuration and return with the configuration
|
// UpdateConfig update existing configuration according to input configuration and return with the configuration
|
||||||
func UpdateConfig(input ConfigInput) (*Config, error) {
|
func UpdateConfig(input ConfigInput) (*Config, error) {
|
||||||
configExists, err := fileExists(input.ConfigPath)
|
if !fileExists(input.ConfigPath) {
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to check if config file exists: %w", err)
|
|
||||||
}
|
|
||||||
if !configExists {
|
|
||||||
return nil, fmt.Errorf("config file %s does not exist", input.ConfigPath)
|
return nil, fmt.Errorf("config file %s does not exist", input.ConfigPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -654,11 +644,7 @@ func UpdateConfig(input ConfigInput) (*Config, error) {
|
|||||||
|
|
||||||
// UpdateOrCreateConfig reads existing config or generates a new one
|
// UpdateOrCreateConfig reads existing config or generates a new one
|
||||||
func UpdateOrCreateConfig(input ConfigInput) (*Config, error) {
|
func UpdateOrCreateConfig(input ConfigInput) (*Config, error) {
|
||||||
configExists, err := fileExists(input.ConfigPath)
|
if !fileExists(input.ConfigPath) {
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to check if config file exists: %w", err)
|
|
||||||
}
|
|
||||||
if !configExists {
|
|
||||||
log.Infof("generating new config %s", input.ConfigPath)
|
log.Infof("generating new config %s", input.ConfigPath)
|
||||||
cfg, err := createNewConfig(input)
|
cfg, err := createNewConfig(input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -671,7 +657,7 @@ func UpdateOrCreateConfig(input ConfigInput) (*Config, error) {
|
|||||||
if isPreSharedKeyHidden(input.PreSharedKey) {
|
if isPreSharedKeyHidden(input.PreSharedKey) {
|
||||||
input.PreSharedKey = nil
|
input.PreSharedKey = nil
|
||||||
}
|
}
|
||||||
err = util.EnforcePermission(input.ConfigPath)
|
err := util.EnforcePermission(input.ConfigPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("failed to enforce permission on config dir: %v", err)
|
log.Errorf("failed to enforce permission on config dir: %v", err)
|
||||||
}
|
}
|
||||||
@@ -798,12 +784,7 @@ func ReadConfig(configPath string) (*Config, error) {
|
|||||||
|
|
||||||
// ReadConfig read config file and return with Config. If it is not exists create a new with default values
|
// ReadConfig read config file and return with Config. If it is not exists create a new with default values
|
||||||
func readConfig(configPath string, createIfMissing bool) (*Config, error) {
|
func readConfig(configPath string, createIfMissing bool) (*Config, error) {
|
||||||
configExists, err := fileExists(configPath)
|
if fileExists(configPath) {
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to check if config file exists: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if configExists {
|
|
||||||
err := util.EnforcePermission(configPath)
|
err := util.EnforcePermission(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("failed to enforce permission on config dir: %v", err)
|
log.Errorf("failed to enforce permission on config dir: %v", err)
|
||||||
@@ -850,11 +831,7 @@ func DirectWriteOutConfig(path string, config *Config) error {
|
|||||||
// DirectUpdateOrCreateConfig is like UpdateOrCreateConfig but uses direct (non-atomic) writes.
|
// DirectUpdateOrCreateConfig is like UpdateOrCreateConfig but uses direct (non-atomic) writes.
|
||||||
// Use this on platforms where atomic writes are blocked (e.g., tvOS sandbox).
|
// Use this on platforms where atomic writes are blocked (e.g., tvOS sandbox).
|
||||||
func DirectUpdateOrCreateConfig(input ConfigInput) (*Config, error) {
|
func DirectUpdateOrCreateConfig(input ConfigInput) (*Config, error) {
|
||||||
configExists, err := fileExists(input.ConfigPath)
|
if !fileExists(input.ConfigPath) {
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to check if config file exists: %w", err)
|
|
||||||
}
|
|
||||||
if !configExists {
|
|
||||||
log.Infof("generating new config %s", input.ConfigPath)
|
log.Infof("generating new config %s", input.ConfigPath)
|
||||||
cfg, err := createNewConfig(input)
|
cfg, err := createNewConfig(input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -256,11 +256,7 @@ func (s *ServiceManager) AddProfile(profileName, username string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
profPath := filepath.Join(configDir, profileName+".json")
|
profPath := filepath.Join(configDir, profileName+".json")
|
||||||
profileExists, err := fileExists(profPath)
|
if fileExists(profPath) {
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to check if profile exists: %w", err)
|
|
||||||
}
|
|
||||||
if profileExists {
|
|
||||||
return ErrProfileAlreadyExists
|
return ErrProfileAlreadyExists
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -289,11 +285,7 @@ func (s *ServiceManager) RemoveProfile(profileName, username string) error {
|
|||||||
return fmt.Errorf("cannot remove profile with reserved name: %s", defaultProfileName)
|
return fmt.Errorf("cannot remove profile with reserved name: %s", defaultProfileName)
|
||||||
}
|
}
|
||||||
profPath := filepath.Join(configDir, profileName+".json")
|
profPath := filepath.Join(configDir, profileName+".json")
|
||||||
profileExists, err := fileExists(profPath)
|
if !fileExists(profPath) {
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to check if profile exists: %w", err)
|
|
||||||
}
|
|
||||||
if !profileExists {
|
|
||||||
return ErrProfileNotFound
|
return ErrProfileNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,11 +20,7 @@ func (pm *ProfileManager) GetProfileState(profileName string) (*ProfileState, er
|
|||||||
}
|
}
|
||||||
|
|
||||||
stateFile := filepath.Join(configDir, profileName+".state.json")
|
stateFile := filepath.Join(configDir, profileName+".state.json")
|
||||||
stateFileExists, err := fileExists(stateFile)
|
if !fileExists(stateFile) {
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to check if profile state file exists: %w", err)
|
|
||||||
}
|
|
||||||
if !stateFileExists {
|
|
||||||
return nil, errors.New("profile state file does not exist")
|
return nil, errors.New("profile state file does not exist")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
262
client/internal/proxy/manager_darwin.go
Normal file
262
client/internal/proxy/manager_darwin.go
Normal file
@@ -0,0 +1,262 @@
|
|||||||
|
//go:build darwin && !ios
|
||||||
|
|
||||||
|
package proxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/netbirdio/netbird/client/internal/statemanager"
|
||||||
|
)
|
||||||
|
|
||||||
|
const networksetupPath = "/usr/sbin/networksetup"
|
||||||
|
|
||||||
|
// Manager handles system-wide proxy configuration on macOS.
|
||||||
|
type Manager struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
stateManager *statemanager.Manager
|
||||||
|
modifiedServices []string
|
||||||
|
enabled bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewManager creates a new proxy manager.
|
||||||
|
func NewManager(stateManager *statemanager.Manager) *Manager {
|
||||||
|
return &Manager{
|
||||||
|
stateManager: stateManager,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetActiveNetworkServices returns the list of active network services.
|
||||||
|
func GetActiveNetworkServices() ([]string, error) {
|
||||||
|
cmd := exec.Command(networksetupPath, "-listallnetworkservices")
|
||||||
|
out, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("list network services: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
lines := strings.Split(string(out), "\n")
|
||||||
|
var services []string
|
||||||
|
for _, line := range lines {
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
if line == "" || strings.HasPrefix(line, "*") || strings.Contains(line, "asterisk") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
services = append(services, line)
|
||||||
|
}
|
||||||
|
return services, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnableWebProxy enables web proxy for all active network services.
|
||||||
|
func (m *Manager) EnableWebProxy(host string, port int) error {
|
||||||
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
|
if m.enabled {
|
||||||
|
log.Debug("web proxy already enabled")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
services, err := GetActiveNetworkServices()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var modifiedServices []string
|
||||||
|
for _, service := range services {
|
||||||
|
if err := m.enableProxyForService(service, host, port); err != nil {
|
||||||
|
log.Warnf("enable proxy for %s: %v", service, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
modifiedServices = append(modifiedServices, service)
|
||||||
|
}
|
||||||
|
|
||||||
|
m.modifiedServices = modifiedServices
|
||||||
|
m.enabled = true
|
||||||
|
m.updateState()
|
||||||
|
|
||||||
|
log.Infof("enabled web proxy on %d services -> %s:%d", len(modifiedServices), host, port)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Manager) enableProxyForService(service, host string, port int) error {
|
||||||
|
portStr := fmt.Sprintf("%d", port)
|
||||||
|
|
||||||
|
// Set web proxy (HTTP)
|
||||||
|
cmd := exec.Command(networksetupPath, "-setwebproxy", service, host, portStr)
|
||||||
|
if out, err := cmd.CombinedOutput(); err != nil {
|
||||||
|
return fmt.Errorf("set web proxy: %w, output: %s", err, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enable web proxy
|
||||||
|
cmd = exec.Command(networksetupPath, "-setwebproxystate", service, "on")
|
||||||
|
if out, err := cmd.CombinedOutput(); err != nil {
|
||||||
|
return fmt.Errorf("enable web proxy state: %w, output: %s", err, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set secure web proxy (HTTPS)
|
||||||
|
cmd = exec.Command(networksetupPath, "-setsecurewebproxy", service, host, portStr)
|
||||||
|
if out, err := cmd.CombinedOutput(); err != nil {
|
||||||
|
return fmt.Errorf("set secure web proxy: %w, output: %s", err, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enable secure web proxy
|
||||||
|
cmd = exec.Command(networksetupPath, "-setsecurewebproxystate", service, "on")
|
||||||
|
if out, err := cmd.CombinedOutput(); err != nil {
|
||||||
|
return fmt.Errorf("enable secure web proxy state: %w, output: %s", err, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debugf("enabled proxy for service %s", service)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisableWebProxy disables web proxy for all modified network services.
|
||||||
|
func (m *Manager) DisableWebProxy() error {
|
||||||
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
|
if !m.enabled {
|
||||||
|
log.Debug("web proxy already disabled")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
services := m.modifiedServices
|
||||||
|
if len(services) == 0 {
|
||||||
|
services, _ = GetActiveNetworkServices()
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, service := range services {
|
||||||
|
if err := m.disableProxyForService(service); err != nil {
|
||||||
|
log.Warnf("disable proxy for %s: %v", service, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m.modifiedServices = nil
|
||||||
|
m.enabled = false
|
||||||
|
m.updateState()
|
||||||
|
|
||||||
|
log.Info("disabled web proxy")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Manager) disableProxyForService(service string) error {
|
||||||
|
// Disable web proxy (HTTP)
|
||||||
|
cmd := exec.Command(networksetupPath, "-setwebproxystate", service, "off")
|
||||||
|
if out, err := cmd.CombinedOutput(); err != nil {
|
||||||
|
return fmt.Errorf("disable web proxy: %w, output: %s", err, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disable secure web proxy (HTTPS)
|
||||||
|
cmd = exec.Command(networksetupPath, "-setsecurewebproxystate", service, "off")
|
||||||
|
if out, err := cmd.CombinedOutput(); err != nil {
|
||||||
|
return fmt.Errorf("disable secure web proxy: %w, output: %s", err, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debugf("disabled proxy for service %s", service)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetAutoproxyURL sets the automatic proxy configuration URL (PAC file).
|
||||||
|
func (m *Manager) SetAutoproxyURL(pacURL string) error {
|
||||||
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
|
services, err := GetActiveNetworkServices()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var modifiedServices []string
|
||||||
|
for _, service := range services {
|
||||||
|
cmd := exec.Command(networksetupPath, "-setautoproxyurl", service, pacURL)
|
||||||
|
if out, err := cmd.CombinedOutput(); err != nil {
|
||||||
|
log.Warnf("set autoproxy for %s: %v, output: %s", service, err, out)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd = exec.Command(networksetupPath, "-setautoproxystate", service, "on")
|
||||||
|
if out, err := cmd.CombinedOutput(); err != nil {
|
||||||
|
log.Warnf("enable autoproxy for %s: %v, output: %s", service, err, out)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
modifiedServices = append(modifiedServices, service)
|
||||||
|
log.Debugf("set autoproxy URL for %s -> %s", service, pacURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
m.modifiedServices = modifiedServices
|
||||||
|
m.enabled = true
|
||||||
|
m.updateState()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisableAutoproxy disables automatic proxy configuration.
|
||||||
|
func (m *Manager) DisableAutoproxy() error {
|
||||||
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
|
services := m.modifiedServices
|
||||||
|
if len(services) == 0 {
|
||||||
|
services, _ = GetActiveNetworkServices()
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, service := range services {
|
||||||
|
cmd := exec.Command(networksetupPath, "-setautoproxystate", service, "off")
|
||||||
|
if out, err := cmd.CombinedOutput(); err != nil {
|
||||||
|
log.Warnf("disable autoproxy for %s: %v, output: %s", service, err, out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m.modifiedServices = nil
|
||||||
|
m.enabled = false
|
||||||
|
m.updateState()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsEnabled returns whether the proxy is currently enabled.
|
||||||
|
func (m *Manager) IsEnabled() bool {
|
||||||
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
return m.enabled
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restore restores proxy settings from a previous state.
|
||||||
|
func (m *Manager) Restore(services []string) error {
|
||||||
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
|
for _, service := range services {
|
||||||
|
if err := m.disableProxyForService(service); err != nil {
|
||||||
|
log.Warnf("restore proxy for %s: %v", service, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m.modifiedServices = nil
|
||||||
|
m.enabled = false
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Manager) updateState() {
|
||||||
|
if m.stateManager == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.enabled && len(m.modifiedServices) > 0 {
|
||||||
|
state := &ShutdownState{
|
||||||
|
ModifiedServices: m.modifiedServices,
|
||||||
|
}
|
||||||
|
if err := m.stateManager.UpdateState(state); err != nil {
|
||||||
|
log.Errorf("update proxy state: %v", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if err := m.stateManager.DeleteState(&ShutdownState{}); err != nil {
|
||||||
|
log.Debugf("delete proxy state: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
45
client/internal/proxy/manager_other.go
Normal file
45
client/internal/proxy/manager_other.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
//go:build !darwin || ios
|
||||||
|
|
||||||
|
package proxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/netbirdio/netbird/client/internal/statemanager"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Manager is a no-op proxy manager for non-macOS platforms.
|
||||||
|
type Manager struct{}
|
||||||
|
|
||||||
|
// NewManager creates a new proxy manager (no-op on non-macOS).
|
||||||
|
func NewManager(_ *statemanager.Manager) *Manager {
|
||||||
|
return &Manager{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// EnableWebProxy is a no-op on non-macOS platforms.
|
||||||
|
func (m *Manager) EnableWebProxy(host string, port int) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisableWebProxy is a no-op on non-macOS platforms.
|
||||||
|
func (m *Manager) DisableWebProxy() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetAutoproxyURL is a no-op on non-macOS platforms.
|
||||||
|
func (m *Manager) SetAutoproxyURL(pacURL string) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisableAutoproxy is a no-op on non-macOS platforms.
|
||||||
|
func (m *Manager) DisableAutoproxy() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsEnabled always returns false on non-macOS platforms.
|
||||||
|
func (m *Manager) IsEnabled() bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restore is a no-op on non-macOS platforms.
|
||||||
|
func (m *Manager) Restore(services []string) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
88
client/internal/proxy/manager_test.go
Normal file
88
client/internal/proxy/manager_test.go
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
//go:build darwin && !ios
|
||||||
|
|
||||||
|
package proxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetActiveNetworkServices(t *testing.T) {
|
||||||
|
services, err := GetActiveNetworkServices()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotEmpty(t, services, "should have at least one network service")
|
||||||
|
|
||||||
|
// Check that services don't contain invalid entries
|
||||||
|
for _, service := range services {
|
||||||
|
assert.NotEmpty(t, service)
|
||||||
|
assert.NotContains(t, service, "*")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestManager_EnableDisableWebProxy(t *testing.T) {
|
||||||
|
// Skip this test in CI as it requires admin privileges
|
||||||
|
if testing.Short() {
|
||||||
|
t.Skip("skipping proxy test in short mode")
|
||||||
|
}
|
||||||
|
|
||||||
|
m := NewManager(nil)
|
||||||
|
assert.NotNil(t, m)
|
||||||
|
assert.False(t, m.IsEnabled())
|
||||||
|
|
||||||
|
// This test would require admin privileges to actually enable the proxy
|
||||||
|
// So we just test the basic state management
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestShutdownState_Name(t *testing.T) {
|
||||||
|
state := &ShutdownState{}
|
||||||
|
assert.Equal(t, "proxy_state", state.Name())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestShutdownState_Cleanup_EmptyServices(t *testing.T) {
|
||||||
|
state := &ShutdownState{
|
||||||
|
ModifiedServices: []string{},
|
||||||
|
}
|
||||||
|
err := state.Cleanup()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContains(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
s string
|
||||||
|
substr string
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{"Enabled: Yes", "Enabled: Yes", true},
|
||||||
|
{"Enabled: No", "Enabled: Yes", false},
|
||||||
|
{"Server: 127.0.0.1\nEnabled: Yes\nPort: 8080", "Enabled: Yes", true},
|
||||||
|
{"", "Enabled: Yes", false},
|
||||||
|
{"Enabled: Yes", "", true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.s+"_"+tt.substr, func(t *testing.T) {
|
||||||
|
got := contains(tt.s, tt.substr)
|
||||||
|
assert.Equal(t, tt.want, got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsProxyEnabled(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
output string
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{"Enabled: Yes\nServer: 127.0.0.1\nPort: 8080", true},
|
||||||
|
{"Enabled: No\nServer: \nPort: 0", false},
|
||||||
|
{"Server: 127.0.0.1\nEnabled: Yes\nPort: 8080", true},
|
||||||
|
{"", false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.output, func(t *testing.T) {
|
||||||
|
got := isProxyEnabled(tt.output)
|
||||||
|
assert.Equal(t, tt.want, got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
105
client/internal/proxy/state_darwin.go
Normal file
105
client/internal/proxy/state_darwin.go
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
//go:build darwin && !ios
|
||||||
|
|
||||||
|
package proxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/netbirdio/netbird/client/internal/statemanager"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ShutdownState stores proxy state for cleanup on unclean shutdown.
|
||||||
|
type ShutdownState struct {
|
||||||
|
ModifiedServices []string `json:"modified_services"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name returns the state name for persistence.
|
||||||
|
func (s *ShutdownState) Name() string {
|
||||||
|
return "proxy_state"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cleanup restores proxy settings after an unclean shutdown.
|
||||||
|
func (s *ShutdownState) Cleanup() error {
|
||||||
|
if len(s.ModifiedServices) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Infof("cleaning up proxy state for %d services", len(s.ModifiedServices))
|
||||||
|
|
||||||
|
for _, service := range s.ModifiedServices {
|
||||||
|
// Disable web proxy (HTTP)
|
||||||
|
cmd := exec.Command(networksetupPath, "-setwebproxystate", service, "off")
|
||||||
|
if out, err := cmd.CombinedOutput(); err != nil {
|
||||||
|
log.Warnf("cleanup web proxy for %s: %v, output: %s", service, err, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disable secure web proxy (HTTPS)
|
||||||
|
cmd = exec.Command(networksetupPath, "-setsecurewebproxystate", service, "off")
|
||||||
|
if out, err := cmd.CombinedOutput(); err != nil {
|
||||||
|
log.Warnf("cleanup secure web proxy for %s: %v, output: %s", service, err, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disable autoproxy
|
||||||
|
cmd = exec.Command(networksetupPath, "-setautoproxystate", service, "off")
|
||||||
|
if out, err := cmd.CombinedOutput(); err != nil {
|
||||||
|
log.Warnf("cleanup autoproxy for %s: %v, output: %s", service, err, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debugf("cleaned up proxy for service %s", service)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RegisterState registers the proxy state with the state manager.
|
||||||
|
func RegisterState(stateManager *statemanager.Manager) {
|
||||||
|
if stateManager == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
stateManager.RegisterState(&ShutdownState{})
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetProxyState returns the current proxy state from the command line.
|
||||||
|
func GetProxyState(service string) (webProxy, secureProxy, autoProxy bool, err error) {
|
||||||
|
// Check web proxy state
|
||||||
|
cmd := exec.Command(networksetupPath, "-getwebproxy", service)
|
||||||
|
out, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
return false, false, false, fmt.Errorf("get web proxy: %w", err)
|
||||||
|
}
|
||||||
|
webProxy = isProxyEnabled(string(out))
|
||||||
|
|
||||||
|
// Check secure web proxy state
|
||||||
|
cmd = exec.Command(networksetupPath, "-getsecurewebproxy", service)
|
||||||
|
out, err = cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
return false, false, false, fmt.Errorf("get secure web proxy: %w", err)
|
||||||
|
}
|
||||||
|
secureProxy = isProxyEnabled(string(out))
|
||||||
|
|
||||||
|
// Check autoproxy state
|
||||||
|
cmd = exec.Command(networksetupPath, "-getautoproxyurl", service)
|
||||||
|
out, err = cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
return false, false, false, fmt.Errorf("get autoproxy: %w", err)
|
||||||
|
}
|
||||||
|
autoProxy = isProxyEnabled(string(out))
|
||||||
|
|
||||||
|
return webProxy, secureProxy, autoProxy, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func isProxyEnabled(output string) bool {
|
||||||
|
return !contains(output, "Enabled: No") && contains(output, "Enabled: Yes")
|
||||||
|
}
|
||||||
|
|
||||||
|
func contains(s, substr string) bool {
|
||||||
|
for i := 0; i <= len(s)-len(substr); i++ {
|
||||||
|
if s[i:i+len(substr)] == substr {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
24
client/internal/proxy/state_other.go
Normal file
24
client/internal/proxy/state_other.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
//go:build !darwin || ios
|
||||||
|
|
||||||
|
package proxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/netbirdio/netbird/client/internal/statemanager"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ShutdownState is a no-op state for non-macOS platforms.
|
||||||
|
type ShutdownState struct{}
|
||||||
|
|
||||||
|
// Name returns the state name.
|
||||||
|
func (s *ShutdownState) Name() string {
|
||||||
|
return "proxy_state"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cleanup is a no-op on non-macOS platforms.
|
||||||
|
func (s *ShutdownState) Cleanup() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RegisterState is a no-op on non-macOS platforms.
|
||||||
|
func RegisterState(stateManager *statemanager.Manager) {
|
||||||
|
}
|
||||||
@@ -263,14 +263,8 @@ func (w *Watcher) watchPeerStatusChanges(ctx context.Context, peerKey string, pe
|
|||||||
case <-closer:
|
case <-closer:
|
||||||
return
|
return
|
||||||
case routerStates := <-subscription.Events():
|
case routerStates := <-subscription.Events():
|
||||||
select {
|
peerStateUpdate <- routerStates
|
||||||
case peerStateUpdate <- routerStates:
|
|
||||||
log.Debugf("triggered route state update for Peer: %s", peerKey)
|
log.Debugf("triggered route state update for Peer: %s", peerKey)
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case <-closer:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -351,11 +351,6 @@ func (d *DnsInterceptor) writeMsg(w dns.ResponseWriter, r *dns.Msg, logger *log.
|
|||||||
logger.Errorf("failed to update domain prefixes: %v", err)
|
logger.Errorf("failed to update domain prefixes: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allow time for route changes to be applied before sending
|
|
||||||
// the DNS response (relevant on iOS where setTunnelNetworkSettings
|
|
||||||
// is asynchronous).
|
|
||||||
waitForRouteSettlement(logger)
|
|
||||||
|
|
||||||
d.replaceIPsInDNSResponse(r, newPrefixes, logger)
|
d.replaceIPsInDNSResponse(r, newPrefixes, logger)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +0,0 @@
|
|||||||
//go:build ios
|
|
||||||
|
|
||||||
package dnsinterceptor
|
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
const routeSettleDelay = 500 * time.Millisecond
|
|
||||||
|
|
||||||
// waitForRouteSettlement introduces a short delay on iOS to allow
|
|
||||||
// setTunnelNetworkSettings to apply route changes before the DNS
|
|
||||||
// response reaches the application. Without this, the first request
|
|
||||||
// to a newly resolved domain may bypass the tunnel.
|
|
||||||
func waitForRouteSettlement(logger *log.Entry) {
|
|
||||||
logger.Tracef("waiting %v for iOS route settlement", routeSettleDelay)
|
|
||||||
time.Sleep(routeSettleDelay)
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
//go:build !ios
|
|
||||||
|
|
||||||
package dnsinterceptor
|
|
||||||
|
|
||||||
import log "github.com/sirupsen/logrus"
|
|
||||||
|
|
||||||
func waitForRouteSettlement(_ *log.Entry) {
|
|
||||||
// No-op on non-iOS platforms: route changes are applied synchronously by
|
|
||||||
// the kernel, so no settlement delay is needed before the DNS response
|
|
||||||
// reaches the application. The delay is only required on iOS where
|
|
||||||
// setTunnelNetworkSettings applies routes asynchronously.
|
|
||||||
}
|
|
||||||
@@ -173,21 +173,12 @@ func (m *DefaultManager) setupAndroidRoutes(config ManagerConfig) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *DefaultManager) setupRefCounters(useNoop bool) {
|
func (m *DefaultManager) setupRefCounters(useNoop bool) {
|
||||||
var once sync.Once
|
|
||||||
var wgIface *net.Interface
|
|
||||||
toInterface := func() *net.Interface {
|
|
||||||
once.Do(func() {
|
|
||||||
wgIface = m.wgInterface.ToInterface()
|
|
||||||
})
|
|
||||||
return wgIface
|
|
||||||
}
|
|
||||||
|
|
||||||
m.routeRefCounter = refcounter.New(
|
m.routeRefCounter = refcounter.New(
|
||||||
func(prefix netip.Prefix, _ struct{}) (struct{}, error) {
|
func(prefix netip.Prefix, _ struct{}) (struct{}, error) {
|
||||||
return struct{}{}, m.sysOps.AddVPNRoute(prefix, toInterface())
|
return struct{}{}, m.sysOps.AddVPNRoute(prefix, m.wgInterface.ToInterface())
|
||||||
},
|
},
|
||||||
func(prefix netip.Prefix, _ struct{}) error {
|
func(prefix netip.Prefix, _ struct{}) error {
|
||||||
return m.sysOps.RemoveVPNRoute(prefix, toInterface())
|
return m.sysOps.RemoveVPNRoute(prefix, m.wgInterface.ToInterface())
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -346,23 +337,6 @@ func (m *DefaultManager) updateSystemRoutes(newRoutes route.HAMap) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var merr *multierror.Error
|
var merr *multierror.Error
|
||||||
|
|
||||||
// Begin batch mode to avoid calling applyHostConfig() after each DNS handler operation
|
|
||||||
batchStarted := false
|
|
||||||
if m.dnsServer != nil {
|
|
||||||
m.dnsServer.BeginBatch()
|
|
||||||
batchStarted = true
|
|
||||||
defer func() {
|
|
||||||
if merr != nil {
|
|
||||||
// On error, cancel batch to discard partial DNS state
|
|
||||||
m.dnsServer.CancelBatch()
|
|
||||||
} else {
|
|
||||||
// On success, apply accumulated DNS changes
|
|
||||||
m.dnsServer.EndBatch()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
for id, handler := range toRemove {
|
for id, handler := range toRemove {
|
||||||
if err := handler.RemoveRoute(); err != nil {
|
if err := handler.RemoveRoute(); err != nil {
|
||||||
merr = multierror.Append(merr, fmt.Errorf("remove route %s: %w", handler.String(), err))
|
merr = multierror.Append(merr, fmt.Errorf("remove route %s: %w", handler.String(), err))
|
||||||
@@ -393,7 +367,6 @@ func (m *DefaultManager) updateSystemRoutes(newRoutes route.HAMap) error {
|
|||||||
m.activeRoutes[id] = handler
|
m.activeRoutes[id] = handler
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = batchStarted // Mark as used
|
|
||||||
return nberrors.FormatErrorOrNil(merr)
|
return nberrors.FormatErrorOrNil(merr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,17 +4,16 @@ package systemops
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// filterRoutesByFlags returns true if the route message should be ignored based on its flags.
|
// filterRoutesByFlags returns true if the route message should be ignored based on its flags.
|
||||||
func filterRoutesByFlags(routeMessageFlags int) bool {
|
func filterRoutesByFlags(routeMessageFlags int) bool {
|
||||||
if routeMessageFlags&unix.RTF_UP == 0 {
|
if routeMessageFlags&syscall.RTF_UP == 0 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
if routeMessageFlags&(unix.RTF_REJECT|unix.RTF_BLACKHOLE|unix.RTF_WASCLONED) != 0 {
|
if routeMessageFlags&(syscall.RTF_REJECT|syscall.RTF_BLACKHOLE|syscall.RTF_WASCLONED) != 0 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,51 +24,42 @@ func filterRoutesByFlags(routeMessageFlags int) bool {
|
|||||||
func formatBSDFlags(flags int) string {
|
func formatBSDFlags(flags int) string {
|
||||||
var flagStrs []string
|
var flagStrs []string
|
||||||
|
|
||||||
if flags&unix.RTF_UP != 0 {
|
if flags&syscall.RTF_UP != 0 {
|
||||||
flagStrs = append(flagStrs, "U")
|
flagStrs = append(flagStrs, "U")
|
||||||
}
|
}
|
||||||
if flags&unix.RTF_GATEWAY != 0 {
|
if flags&syscall.RTF_GATEWAY != 0 {
|
||||||
flagStrs = append(flagStrs, "G")
|
flagStrs = append(flagStrs, "G")
|
||||||
}
|
}
|
||||||
if flags&unix.RTF_HOST != 0 {
|
if flags&syscall.RTF_HOST != 0 {
|
||||||
flagStrs = append(flagStrs, "H")
|
flagStrs = append(flagStrs, "H")
|
||||||
}
|
}
|
||||||
if flags&unix.RTF_REJECT != 0 {
|
if flags&syscall.RTF_REJECT != 0 {
|
||||||
flagStrs = append(flagStrs, "R")
|
flagStrs = append(flagStrs, "R")
|
||||||
}
|
}
|
||||||
if flags&unix.RTF_DYNAMIC != 0 {
|
if flags&syscall.RTF_DYNAMIC != 0 {
|
||||||
flagStrs = append(flagStrs, "D")
|
flagStrs = append(flagStrs, "D")
|
||||||
}
|
}
|
||||||
if flags&unix.RTF_MODIFIED != 0 {
|
if flags&syscall.RTF_MODIFIED != 0 {
|
||||||
flagStrs = append(flagStrs, "M")
|
flagStrs = append(flagStrs, "M")
|
||||||
}
|
}
|
||||||
if flags&unix.RTF_STATIC != 0 {
|
if flags&syscall.RTF_STATIC != 0 {
|
||||||
flagStrs = append(flagStrs, "S")
|
flagStrs = append(flagStrs, "S")
|
||||||
}
|
}
|
||||||
if flags&unix.RTF_LLINFO != 0 {
|
if flags&syscall.RTF_LLINFO != 0 {
|
||||||
flagStrs = append(flagStrs, "L")
|
flagStrs = append(flagStrs, "L")
|
||||||
}
|
}
|
||||||
if flags&unix.RTF_LOCAL != 0 {
|
if flags&syscall.RTF_LOCAL != 0 {
|
||||||
flagStrs = append(flagStrs, "l")
|
flagStrs = append(flagStrs, "l")
|
||||||
}
|
}
|
||||||
if flags&unix.RTF_BLACKHOLE != 0 {
|
if flags&syscall.RTF_BLACKHOLE != 0 {
|
||||||
flagStrs = append(flagStrs, "B")
|
flagStrs = append(flagStrs, "B")
|
||||||
}
|
}
|
||||||
if flags&unix.RTF_CLONING != 0 {
|
if flags&syscall.RTF_CLONING != 0 {
|
||||||
flagStrs = append(flagStrs, "C")
|
flagStrs = append(flagStrs, "C")
|
||||||
}
|
}
|
||||||
if flags&unix.RTF_WASCLONED != 0 {
|
if flags&syscall.RTF_WASCLONED != 0 {
|
||||||
flagStrs = append(flagStrs, "W")
|
flagStrs = append(flagStrs, "W")
|
||||||
}
|
}
|
||||||
if flags&unix.RTF_PROTO1 != 0 {
|
|
||||||
flagStrs = append(flagStrs, "1")
|
|
||||||
}
|
|
||||||
if flags&unix.RTF_PROTO2 != 0 {
|
|
||||||
flagStrs = append(flagStrs, "2")
|
|
||||||
}
|
|
||||||
if flags&unix.RTF_PROTO3 != 0 {
|
|
||||||
flagStrs = append(flagStrs, "3")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(flagStrs) == 0 {
|
if len(flagStrs) == 0 {
|
||||||
return "-"
|
return "-"
|
||||||
|
|||||||
@@ -4,18 +4,17 @@ package systemops
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// filterRoutesByFlags returns true if the route message should be ignored based on its flags.
|
// filterRoutesByFlags returns true if the route message should be ignored based on its flags.
|
||||||
func filterRoutesByFlags(routeMessageFlags int) bool {
|
func filterRoutesByFlags(routeMessageFlags int) bool {
|
||||||
if routeMessageFlags&unix.RTF_UP == 0 {
|
if routeMessageFlags&syscall.RTF_UP == 0 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: RTF_WASCLONED deprecated in FreeBSD 8.0
|
// NOTE: syscall.RTF_WASCLONED deprecated in FreeBSD 8.0
|
||||||
if routeMessageFlags&(unix.RTF_REJECT|unix.RTF_BLACKHOLE) != 0 {
|
if routeMessageFlags&(syscall.RTF_REJECT|syscall.RTF_BLACKHOLE) != 0 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,46 +25,37 @@ func filterRoutesByFlags(routeMessageFlags int) bool {
|
|||||||
func formatBSDFlags(flags int) string {
|
func formatBSDFlags(flags int) string {
|
||||||
var flagStrs []string
|
var flagStrs []string
|
||||||
|
|
||||||
if flags&unix.RTF_UP != 0 {
|
if flags&syscall.RTF_UP != 0 {
|
||||||
flagStrs = append(flagStrs, "U")
|
flagStrs = append(flagStrs, "U")
|
||||||
}
|
}
|
||||||
if flags&unix.RTF_GATEWAY != 0 {
|
if flags&syscall.RTF_GATEWAY != 0 {
|
||||||
flagStrs = append(flagStrs, "G")
|
flagStrs = append(flagStrs, "G")
|
||||||
}
|
}
|
||||||
if flags&unix.RTF_HOST != 0 {
|
if flags&syscall.RTF_HOST != 0 {
|
||||||
flagStrs = append(flagStrs, "H")
|
flagStrs = append(flagStrs, "H")
|
||||||
}
|
}
|
||||||
if flags&unix.RTF_REJECT != 0 {
|
if flags&syscall.RTF_REJECT != 0 {
|
||||||
flagStrs = append(flagStrs, "R")
|
flagStrs = append(flagStrs, "R")
|
||||||
}
|
}
|
||||||
if flags&unix.RTF_DYNAMIC != 0 {
|
if flags&syscall.RTF_DYNAMIC != 0 {
|
||||||
flagStrs = append(flagStrs, "D")
|
flagStrs = append(flagStrs, "D")
|
||||||
}
|
}
|
||||||
if flags&unix.RTF_MODIFIED != 0 {
|
if flags&syscall.RTF_MODIFIED != 0 {
|
||||||
flagStrs = append(flagStrs, "M")
|
flagStrs = append(flagStrs, "M")
|
||||||
}
|
}
|
||||||
if flags&unix.RTF_STATIC != 0 {
|
if flags&syscall.RTF_STATIC != 0 {
|
||||||
flagStrs = append(flagStrs, "S")
|
flagStrs = append(flagStrs, "S")
|
||||||
}
|
}
|
||||||
if flags&unix.RTF_LLINFO != 0 {
|
if flags&syscall.RTF_LLINFO != 0 {
|
||||||
flagStrs = append(flagStrs, "L")
|
flagStrs = append(flagStrs, "L")
|
||||||
}
|
}
|
||||||
if flags&unix.RTF_LOCAL != 0 {
|
if flags&syscall.RTF_LOCAL != 0 {
|
||||||
flagStrs = append(flagStrs, "l")
|
flagStrs = append(flagStrs, "l")
|
||||||
}
|
}
|
||||||
if flags&unix.RTF_BLACKHOLE != 0 {
|
if flags&syscall.RTF_BLACKHOLE != 0 {
|
||||||
flagStrs = append(flagStrs, "B")
|
flagStrs = append(flagStrs, "B")
|
||||||
}
|
}
|
||||||
// Note: RTF_CLONING and RTF_WASCLONED deprecated in FreeBSD 8.0
|
// Note: RTF_CLONING and RTF_WASCLONED deprecated in FreeBSD 8.0
|
||||||
if flags&unix.RTF_PROTO1 != 0 {
|
|
||||||
flagStrs = append(flagStrs, "1")
|
|
||||||
}
|
|
||||||
if flags&unix.RTF_PROTO2 != 0 {
|
|
||||||
flagStrs = append(flagStrs, "2")
|
|
||||||
}
|
|
||||||
if flags&unix.RTF_PROTO3 != 0 {
|
|
||||||
flagStrs = append(flagStrs, "3")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(flagStrs) == 0 {
|
if len(flagStrs) == 0 {
|
||||||
return "-"
|
return "-"
|
||||||
|
|||||||
@@ -1,80 +0,0 @@
|
|||||||
package handler
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
|
|
||||||
"github.com/netbirdio/netbird/client/internal"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Agent interface {
|
|
||||||
Up(ctx context.Context) error
|
|
||||||
Down(ctx context.Context) error
|
|
||||||
Status() (internal.StatusType, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type SleepHandler struct {
|
|
||||||
agent Agent
|
|
||||||
|
|
||||||
mu sync.Mutex
|
|
||||||
// sleepTriggeredDown indicates whether the sleep handler triggered the last client down, to avoid unnecessary up on wake
|
|
||||||
sleepTriggeredDown bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func New(agent Agent) *SleepHandler {
|
|
||||||
return &SleepHandler{
|
|
||||||
agent: agent,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SleepHandler) HandleWakeUp(ctx context.Context) error {
|
|
||||||
s.mu.Lock()
|
|
||||||
defer s.mu.Unlock()
|
|
||||||
|
|
||||||
if !s.sleepTriggeredDown {
|
|
||||||
log.Info("skipping up because wasn't sleep down")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// avoid other wakeup runs if sleep didn't make the computer sleep
|
|
||||||
s.sleepTriggeredDown = false
|
|
||||||
|
|
||||||
log.Info("running up after wake up")
|
|
||||||
err := s.agent.Up(ctx)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("running up failed: %v", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Info("running up command executed successfully")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *SleepHandler) HandleSleep(ctx context.Context) error {
|
|
||||||
s.mu.Lock()
|
|
||||||
defer s.mu.Unlock()
|
|
||||||
|
|
||||||
status, err := s.agent.Status()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if status != internal.StatusConnecting && status != internal.StatusConnected {
|
|
||||||
log.Infof("skipping setting the agent down because status is %s", status)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Info("running down after system started sleeping")
|
|
||||||
|
|
||||||
if err = s.agent.Down(ctx); err != nil {
|
|
||||||
log.Errorf("running down failed: %v", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
s.sleepTriggeredDown = true
|
|
||||||
|
|
||||||
log.Info("running down executed successfully")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,153 +0,0 @@
|
|||||||
package handler
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
|
|
||||||
"github.com/netbirdio/netbird/client/internal"
|
|
||||||
)
|
|
||||||
|
|
||||||
type mockAgent struct {
|
|
||||||
upErr error
|
|
||||||
downErr error
|
|
||||||
statusErr error
|
|
||||||
status internal.StatusType
|
|
||||||
upCalls int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *mockAgent) Up(_ context.Context) error {
|
|
||||||
m.upCalls++
|
|
||||||
return m.upErr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *mockAgent) Down(_ context.Context) error {
|
|
||||||
return m.downErr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *mockAgent) Status() (internal.StatusType, error) {
|
|
||||||
return m.status, m.statusErr
|
|
||||||
}
|
|
||||||
|
|
||||||
func newHandler(status internal.StatusType) (*SleepHandler, *mockAgent) {
|
|
||||||
agent := &mockAgent{status: status}
|
|
||||||
return New(agent), agent
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandleWakeUp_SkipsWhenFlagFalse(t *testing.T) {
|
|
||||||
h, agent := newHandler(internal.StatusIdle)
|
|
||||||
|
|
||||||
err := h.HandleWakeUp(context.Background())
|
|
||||||
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, 0, agent.upCalls, "Up should not be called when flag is false")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandleWakeUp_ResetsFlagBeforeUp(t *testing.T) {
|
|
||||||
h, _ := newHandler(internal.StatusIdle)
|
|
||||||
h.sleepTriggeredDown = true
|
|
||||||
|
|
||||||
// Even if Up fails, flag should be reset
|
|
||||||
_ = h.HandleWakeUp(context.Background())
|
|
||||||
|
|
||||||
assert.False(t, h.sleepTriggeredDown, "flag must be reset before calling Up")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandleWakeUp_CallsUpWhenFlagSet(t *testing.T) {
|
|
||||||
h, agent := newHandler(internal.StatusIdle)
|
|
||||||
h.sleepTriggeredDown = true
|
|
||||||
|
|
||||||
err := h.HandleWakeUp(context.Background())
|
|
||||||
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, 1, agent.upCalls)
|
|
||||||
assert.False(t, h.sleepTriggeredDown)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandleWakeUp_ReturnsErrorFromUp(t *testing.T) {
|
|
||||||
h, agent := newHandler(internal.StatusIdle)
|
|
||||||
h.sleepTriggeredDown = true
|
|
||||||
agent.upErr = errors.New("up failed")
|
|
||||||
|
|
||||||
err := h.HandleWakeUp(context.Background())
|
|
||||||
|
|
||||||
assert.ErrorIs(t, err, agent.upErr)
|
|
||||||
assert.False(t, h.sleepTriggeredDown, "flag should still be reset even when Up fails")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandleWakeUp_SecondCallIsNoOp(t *testing.T) {
|
|
||||||
h, agent := newHandler(internal.StatusIdle)
|
|
||||||
h.sleepTriggeredDown = true
|
|
||||||
|
|
||||||
_ = h.HandleWakeUp(context.Background())
|
|
||||||
err := h.HandleWakeUp(context.Background())
|
|
||||||
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, 1, agent.upCalls, "second wakeup should be no-op")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandleSleep_SkipsForNonActiveStates(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
status internal.StatusType
|
|
||||||
}{
|
|
||||||
{"Idle", internal.StatusIdle},
|
|
||||||
{"NeedsLogin", internal.StatusNeedsLogin},
|
|
||||||
{"LoginFailed", internal.StatusLoginFailed},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
h, _ := newHandler(tt.status)
|
|
||||||
|
|
||||||
err := h.HandleSleep(context.Background())
|
|
||||||
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.False(t, h.sleepTriggeredDown)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandleSleep_ProceedsForActiveStates(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
status internal.StatusType
|
|
||||||
}{
|
|
||||||
{"Connecting", internal.StatusConnecting},
|
|
||||||
{"Connected", internal.StatusConnected},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
h, _ := newHandler(tt.status)
|
|
||||||
|
|
||||||
err := h.HandleSleep(context.Background())
|
|
||||||
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.True(t, h.sleepTriggeredDown)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandleSleep_ReturnsErrorFromStatus(t *testing.T) {
|
|
||||||
agent := &mockAgent{statusErr: errors.New("status error")}
|
|
||||||
h := New(agent)
|
|
||||||
|
|
||||||
err := h.HandleSleep(context.Background())
|
|
||||||
|
|
||||||
assert.ErrorIs(t, err, agent.statusErr)
|
|
||||||
assert.False(t, h.sleepTriggeredDown)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandleSleep_ReturnsErrorFromDown(t *testing.T) {
|
|
||||||
agent := &mockAgent{status: internal.StatusConnected, downErr: errors.New("down failed")}
|
|
||||||
h := New(agent)
|
|
||||||
|
|
||||||
err := h.HandleSleep(context.Background())
|
|
||||||
|
|
||||||
assert.ErrorIs(t, err, agent.downErr)
|
|
||||||
assert.False(t, h.sleepTriggeredDown, "flag should not be set when Down fails")
|
|
||||||
}
|
|
||||||
@@ -2,10 +2,7 @@
|
|||||||
|
|
||||||
package NetBirdSDK
|
package NetBirdSDK
|
||||||
|
|
||||||
import (
|
import "github.com/netbirdio/netbird/client/internal/peer"
|
||||||
"github.com/netbirdio/netbird/client/internal/lazyconn"
|
|
||||||
"github.com/netbirdio/netbird/client/internal/peer"
|
|
||||||
)
|
|
||||||
|
|
||||||
// EnvList is an exported struct to be bound by gomobile
|
// EnvList is an exported struct to be bound by gomobile
|
||||||
type EnvList struct {
|
type EnvList struct {
|
||||||
@@ -35,13 +32,3 @@ func (el *EnvList) AllItems() map[string]string {
|
|||||||
func GetEnvKeyNBForceRelay() string {
|
func GetEnvKeyNBForceRelay() string {
|
||||||
return peer.EnvKeyNBForceRelay
|
return peer.EnvKeyNBForceRelay
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetEnvKeyNBLazyConn Exports the environment variable for the iOS client
|
|
||||||
func GetEnvKeyNBLazyConn() string {
|
|
||||||
return lazyconn.EnvEnableLazyConn
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetEnvKeyNBInactivityThreshold Exports the environment variable for the iOS client
|
|
||||||
func GetEnvKeyNBInactivityThreshold() string {
|
|
||||||
return lazyconn.EnvInactivityThreshold
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||||
// versions:
|
// versions:
|
||||||
// protoc-gen-go v1.36.6
|
// protoc-gen-go v1.36.6
|
||||||
// protoc v6.33.3
|
// protoc v6.32.1
|
||||||
// source: daemon.proto
|
// source: daemon.proto
|
||||||
|
|
||||||
package proto
|
package proto
|
||||||
@@ -88,58 +88,6 @@ func (LogLevel) EnumDescriptor() ([]byte, []int) {
|
|||||||
return file_daemon_proto_rawDescGZIP(), []int{0}
|
return file_daemon_proto_rawDescGZIP(), []int{0}
|
||||||
}
|
}
|
||||||
|
|
||||||
type ExposeProtocol int32
|
|
||||||
|
|
||||||
const (
|
|
||||||
ExposeProtocol_EXPOSE_HTTP ExposeProtocol = 0
|
|
||||||
ExposeProtocol_EXPOSE_HTTPS ExposeProtocol = 1
|
|
||||||
ExposeProtocol_EXPOSE_TCP ExposeProtocol = 2
|
|
||||||
ExposeProtocol_EXPOSE_UDP ExposeProtocol = 3
|
|
||||||
)
|
|
||||||
|
|
||||||
// Enum value maps for ExposeProtocol.
|
|
||||||
var (
|
|
||||||
ExposeProtocol_name = map[int32]string{
|
|
||||||
0: "EXPOSE_HTTP",
|
|
||||||
1: "EXPOSE_HTTPS",
|
|
||||||
2: "EXPOSE_TCP",
|
|
||||||
3: "EXPOSE_UDP",
|
|
||||||
}
|
|
||||||
ExposeProtocol_value = map[string]int32{
|
|
||||||
"EXPOSE_HTTP": 0,
|
|
||||||
"EXPOSE_HTTPS": 1,
|
|
||||||
"EXPOSE_TCP": 2,
|
|
||||||
"EXPOSE_UDP": 3,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
func (x ExposeProtocol) Enum() *ExposeProtocol {
|
|
||||||
p := new(ExposeProtocol)
|
|
||||||
*p = x
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x ExposeProtocol) String() string {
|
|
||||||
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ExposeProtocol) Descriptor() protoreflect.EnumDescriptor {
|
|
||||||
return file_daemon_proto_enumTypes[1].Descriptor()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ExposeProtocol) Type() protoreflect.EnumType {
|
|
||||||
return &file_daemon_proto_enumTypes[1]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x ExposeProtocol) Number() protoreflect.EnumNumber {
|
|
||||||
return protoreflect.EnumNumber(x)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deprecated: Use ExposeProtocol.Descriptor instead.
|
|
||||||
func (ExposeProtocol) EnumDescriptor() ([]byte, []int) {
|
|
||||||
return file_daemon_proto_rawDescGZIP(), []int{1}
|
|
||||||
}
|
|
||||||
|
|
||||||
// avoid collision with loglevel enum
|
// avoid collision with loglevel enum
|
||||||
type OSLifecycleRequest_CycleType int32
|
type OSLifecycleRequest_CycleType int32
|
||||||
|
|
||||||
@@ -174,11 +122,11 @@ func (x OSLifecycleRequest_CycleType) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (OSLifecycleRequest_CycleType) Descriptor() protoreflect.EnumDescriptor {
|
func (OSLifecycleRequest_CycleType) Descriptor() protoreflect.EnumDescriptor {
|
||||||
return file_daemon_proto_enumTypes[2].Descriptor()
|
return file_daemon_proto_enumTypes[1].Descriptor()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (OSLifecycleRequest_CycleType) Type() protoreflect.EnumType {
|
func (OSLifecycleRequest_CycleType) Type() protoreflect.EnumType {
|
||||||
return &file_daemon_proto_enumTypes[2]
|
return &file_daemon_proto_enumTypes[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x OSLifecycleRequest_CycleType) Number() protoreflect.EnumNumber {
|
func (x OSLifecycleRequest_CycleType) Number() protoreflect.EnumNumber {
|
||||||
@@ -226,11 +174,11 @@ func (x SystemEvent_Severity) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (SystemEvent_Severity) Descriptor() protoreflect.EnumDescriptor {
|
func (SystemEvent_Severity) Descriptor() protoreflect.EnumDescriptor {
|
||||||
return file_daemon_proto_enumTypes[3].Descriptor()
|
return file_daemon_proto_enumTypes[2].Descriptor()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (SystemEvent_Severity) Type() protoreflect.EnumType {
|
func (SystemEvent_Severity) Type() protoreflect.EnumType {
|
||||||
return &file_daemon_proto_enumTypes[3]
|
return &file_daemon_proto_enumTypes[2]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x SystemEvent_Severity) Number() protoreflect.EnumNumber {
|
func (x SystemEvent_Severity) Number() protoreflect.EnumNumber {
|
||||||
@@ -281,11 +229,11 @@ func (x SystemEvent_Category) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (SystemEvent_Category) Descriptor() protoreflect.EnumDescriptor {
|
func (SystemEvent_Category) Descriptor() protoreflect.EnumDescriptor {
|
||||||
return file_daemon_proto_enumTypes[4].Descriptor()
|
return file_daemon_proto_enumTypes[3].Descriptor()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (SystemEvent_Category) Type() protoreflect.EnumType {
|
func (SystemEvent_Category) Type() protoreflect.EnumType {
|
||||||
return &file_daemon_proto_enumTypes[4]
|
return &file_daemon_proto_enumTypes[3]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x SystemEvent_Category) Number() protoreflect.EnumNumber {
|
func (x SystemEvent_Category) Number() protoreflect.EnumNumber {
|
||||||
@@ -5652,224 +5600,6 @@ func (x *InstallerResultResponse) GetErrorMsg() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
type ExposeServiceRequest struct {
|
|
||||||
state protoimpl.MessageState `protogen:"open.v1"`
|
|
||||||
Port uint32 `protobuf:"varint,1,opt,name=port,proto3" json:"port,omitempty"`
|
|
||||||
Protocol ExposeProtocol `protobuf:"varint,2,opt,name=protocol,proto3,enum=daemon.ExposeProtocol" json:"protocol,omitempty"`
|
|
||||||
Pin string `protobuf:"bytes,3,opt,name=pin,proto3" json:"pin,omitempty"`
|
|
||||||
Password string `protobuf:"bytes,4,opt,name=password,proto3" json:"password,omitempty"`
|
|
||||||
UserGroups []string `protobuf:"bytes,5,rep,name=user_groups,json=userGroups,proto3" json:"user_groups,omitempty"`
|
|
||||||
Domain string `protobuf:"bytes,6,opt,name=domain,proto3" json:"domain,omitempty"`
|
|
||||||
NamePrefix string `protobuf:"bytes,7,opt,name=name_prefix,json=namePrefix,proto3" json:"name_prefix,omitempty"`
|
|
||||||
unknownFields protoimpl.UnknownFields
|
|
||||||
sizeCache protoimpl.SizeCache
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *ExposeServiceRequest) Reset() {
|
|
||||||
*x = ExposeServiceRequest{}
|
|
||||||
mi := &file_daemon_proto_msgTypes[85]
|
|
||||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
|
||||||
ms.StoreMessageInfo(mi)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *ExposeServiceRequest) String() string {
|
|
||||||
return protoimpl.X.MessageStringOf(x)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*ExposeServiceRequest) ProtoMessage() {}
|
|
||||||
|
|
||||||
func (x *ExposeServiceRequest) ProtoReflect() protoreflect.Message {
|
|
||||||
mi := &file_daemon_proto_msgTypes[85]
|
|
||||||
if x != nil {
|
|
||||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
|
||||||
if ms.LoadMessageInfo() == nil {
|
|
||||||
ms.StoreMessageInfo(mi)
|
|
||||||
}
|
|
||||||
return ms
|
|
||||||
}
|
|
||||||
return mi.MessageOf(x)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deprecated: Use ExposeServiceRequest.ProtoReflect.Descriptor instead.
|
|
||||||
func (*ExposeServiceRequest) Descriptor() ([]byte, []int) {
|
|
||||||
return file_daemon_proto_rawDescGZIP(), []int{85}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *ExposeServiceRequest) GetPort() uint32 {
|
|
||||||
if x != nil {
|
|
||||||
return x.Port
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *ExposeServiceRequest) GetProtocol() ExposeProtocol {
|
|
||||||
if x != nil {
|
|
||||||
return x.Protocol
|
|
||||||
}
|
|
||||||
return ExposeProtocol_EXPOSE_HTTP
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *ExposeServiceRequest) GetPin() string {
|
|
||||||
if x != nil {
|
|
||||||
return x.Pin
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *ExposeServiceRequest) GetPassword() string {
|
|
||||||
if x != nil {
|
|
||||||
return x.Password
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *ExposeServiceRequest) GetUserGroups() []string {
|
|
||||||
if x != nil {
|
|
||||||
return x.UserGroups
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *ExposeServiceRequest) GetDomain() string {
|
|
||||||
if x != nil {
|
|
||||||
return x.Domain
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *ExposeServiceRequest) GetNamePrefix() string {
|
|
||||||
if x != nil {
|
|
||||||
return x.NamePrefix
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
type ExposeServiceEvent struct {
|
|
||||||
state protoimpl.MessageState `protogen:"open.v1"`
|
|
||||||
// Types that are valid to be assigned to Event:
|
|
||||||
//
|
|
||||||
// *ExposeServiceEvent_Ready
|
|
||||||
Event isExposeServiceEvent_Event `protobuf_oneof:"event"`
|
|
||||||
unknownFields protoimpl.UnknownFields
|
|
||||||
sizeCache protoimpl.SizeCache
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *ExposeServiceEvent) Reset() {
|
|
||||||
*x = ExposeServiceEvent{}
|
|
||||||
mi := &file_daemon_proto_msgTypes[86]
|
|
||||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
|
||||||
ms.StoreMessageInfo(mi)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *ExposeServiceEvent) String() string {
|
|
||||||
return protoimpl.X.MessageStringOf(x)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*ExposeServiceEvent) ProtoMessage() {}
|
|
||||||
|
|
||||||
func (x *ExposeServiceEvent) ProtoReflect() protoreflect.Message {
|
|
||||||
mi := &file_daemon_proto_msgTypes[86]
|
|
||||||
if x != nil {
|
|
||||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
|
||||||
if ms.LoadMessageInfo() == nil {
|
|
||||||
ms.StoreMessageInfo(mi)
|
|
||||||
}
|
|
||||||
return ms
|
|
||||||
}
|
|
||||||
return mi.MessageOf(x)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deprecated: Use ExposeServiceEvent.ProtoReflect.Descriptor instead.
|
|
||||||
func (*ExposeServiceEvent) Descriptor() ([]byte, []int) {
|
|
||||||
return file_daemon_proto_rawDescGZIP(), []int{86}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *ExposeServiceEvent) GetEvent() isExposeServiceEvent_Event {
|
|
||||||
if x != nil {
|
|
||||||
return x.Event
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *ExposeServiceEvent) GetReady() *ExposeServiceReady {
|
|
||||||
if x != nil {
|
|
||||||
if x, ok := x.Event.(*ExposeServiceEvent_Ready); ok {
|
|
||||||
return x.Ready
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type isExposeServiceEvent_Event interface {
|
|
||||||
isExposeServiceEvent_Event()
|
|
||||||
}
|
|
||||||
|
|
||||||
type ExposeServiceEvent_Ready struct {
|
|
||||||
Ready *ExposeServiceReady `protobuf:"bytes,1,opt,name=ready,proto3,oneof"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*ExposeServiceEvent_Ready) isExposeServiceEvent_Event() {}
|
|
||||||
|
|
||||||
type ExposeServiceReady struct {
|
|
||||||
state protoimpl.MessageState `protogen:"open.v1"`
|
|
||||||
ServiceName string `protobuf:"bytes,1,opt,name=service_name,json=serviceName,proto3" json:"service_name,omitempty"`
|
|
||||||
ServiceUrl string `protobuf:"bytes,2,opt,name=service_url,json=serviceUrl,proto3" json:"service_url,omitempty"`
|
|
||||||
Domain string `protobuf:"bytes,3,opt,name=domain,proto3" json:"domain,omitempty"`
|
|
||||||
unknownFields protoimpl.UnknownFields
|
|
||||||
sizeCache protoimpl.SizeCache
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *ExposeServiceReady) Reset() {
|
|
||||||
*x = ExposeServiceReady{}
|
|
||||||
mi := &file_daemon_proto_msgTypes[87]
|
|
||||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
|
||||||
ms.StoreMessageInfo(mi)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *ExposeServiceReady) String() string {
|
|
||||||
return protoimpl.X.MessageStringOf(x)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (*ExposeServiceReady) ProtoMessage() {}
|
|
||||||
|
|
||||||
func (x *ExposeServiceReady) ProtoReflect() protoreflect.Message {
|
|
||||||
mi := &file_daemon_proto_msgTypes[87]
|
|
||||||
if x != nil {
|
|
||||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
|
||||||
if ms.LoadMessageInfo() == nil {
|
|
||||||
ms.StoreMessageInfo(mi)
|
|
||||||
}
|
|
||||||
return ms
|
|
||||||
}
|
|
||||||
return mi.MessageOf(x)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deprecated: Use ExposeServiceReady.ProtoReflect.Descriptor instead.
|
|
||||||
func (*ExposeServiceReady) Descriptor() ([]byte, []int) {
|
|
||||||
return file_daemon_proto_rawDescGZIP(), []int{87}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *ExposeServiceReady) GetServiceName() string {
|
|
||||||
if x != nil {
|
|
||||||
return x.ServiceName
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *ExposeServiceReady) GetServiceUrl() string {
|
|
||||||
if x != nil {
|
|
||||||
return x.ServiceUrl
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *ExposeServiceReady) GetDomain() string {
|
|
||||||
if x != nil {
|
|
||||||
return x.Domain
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
type PortInfo_Range struct {
|
type PortInfo_Range struct {
|
||||||
state protoimpl.MessageState `protogen:"open.v1"`
|
state protoimpl.MessageState `protogen:"open.v1"`
|
||||||
Start uint32 `protobuf:"varint,1,opt,name=start,proto3" json:"start,omitempty"`
|
Start uint32 `protobuf:"varint,1,opt,name=start,proto3" json:"start,omitempty"`
|
||||||
@@ -5880,7 +5610,7 @@ type PortInfo_Range struct {
|
|||||||
|
|
||||||
func (x *PortInfo_Range) Reset() {
|
func (x *PortInfo_Range) Reset() {
|
||||||
*x = PortInfo_Range{}
|
*x = PortInfo_Range{}
|
||||||
mi := &file_daemon_proto_msgTypes[89]
|
mi := &file_daemon_proto_msgTypes[86]
|
||||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
ms.StoreMessageInfo(mi)
|
ms.StoreMessageInfo(mi)
|
||||||
}
|
}
|
||||||
@@ -5892,7 +5622,7 @@ func (x *PortInfo_Range) String() string {
|
|||||||
func (*PortInfo_Range) ProtoMessage() {}
|
func (*PortInfo_Range) ProtoMessage() {}
|
||||||
|
|
||||||
func (x *PortInfo_Range) ProtoReflect() protoreflect.Message {
|
func (x *PortInfo_Range) ProtoReflect() protoreflect.Message {
|
||||||
mi := &file_daemon_proto_msgTypes[89]
|
mi := &file_daemon_proto_msgTypes[86]
|
||||||
if x != nil {
|
if x != nil {
|
||||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||||
if ms.LoadMessageInfo() == nil {
|
if ms.LoadMessageInfo() == nil {
|
||||||
@@ -6419,25 +6149,7 @@ const file_daemon_proto_rawDesc = "" +
|
|||||||
"\x16InstallerResultRequest\"O\n" +
|
"\x16InstallerResultRequest\"O\n" +
|
||||||
"\x17InstallerResultResponse\x12\x18\n" +
|
"\x17InstallerResultResponse\x12\x18\n" +
|
||||||
"\asuccess\x18\x01 \x01(\bR\asuccess\x12\x1a\n" +
|
"\asuccess\x18\x01 \x01(\bR\asuccess\x12\x1a\n" +
|
||||||
"\berrorMsg\x18\x02 \x01(\tR\berrorMsg\"\xe6\x01\n" +
|
"\berrorMsg\x18\x02 \x01(\tR\berrorMsg*b\n" +
|
||||||
"\x14ExposeServiceRequest\x12\x12\n" +
|
|
||||||
"\x04port\x18\x01 \x01(\rR\x04port\x122\n" +
|
|
||||||
"\bprotocol\x18\x02 \x01(\x0e2\x16.daemon.ExposeProtocolR\bprotocol\x12\x10\n" +
|
|
||||||
"\x03pin\x18\x03 \x01(\tR\x03pin\x12\x1a\n" +
|
|
||||||
"\bpassword\x18\x04 \x01(\tR\bpassword\x12\x1f\n" +
|
|
||||||
"\vuser_groups\x18\x05 \x03(\tR\n" +
|
|
||||||
"userGroups\x12\x16\n" +
|
|
||||||
"\x06domain\x18\x06 \x01(\tR\x06domain\x12\x1f\n" +
|
|
||||||
"\vname_prefix\x18\a \x01(\tR\n" +
|
|
||||||
"namePrefix\"Q\n" +
|
|
||||||
"\x12ExposeServiceEvent\x122\n" +
|
|
||||||
"\x05ready\x18\x01 \x01(\v2\x1a.daemon.ExposeServiceReadyH\x00R\x05readyB\a\n" +
|
|
||||||
"\x05event\"p\n" +
|
|
||||||
"\x12ExposeServiceReady\x12!\n" +
|
|
||||||
"\fservice_name\x18\x01 \x01(\tR\vserviceName\x12\x1f\n" +
|
|
||||||
"\vservice_url\x18\x02 \x01(\tR\n" +
|
|
||||||
"serviceUrl\x12\x16\n" +
|
|
||||||
"\x06domain\x18\x03 \x01(\tR\x06domain*b\n" +
|
|
||||||
"\bLogLevel\x12\v\n" +
|
"\bLogLevel\x12\v\n" +
|
||||||
"\aUNKNOWN\x10\x00\x12\t\n" +
|
"\aUNKNOWN\x10\x00\x12\t\n" +
|
||||||
"\x05PANIC\x10\x01\x12\t\n" +
|
"\x05PANIC\x10\x01\x12\t\n" +
|
||||||
@@ -6446,14 +6158,7 @@ const file_daemon_proto_rawDesc = "" +
|
|||||||
"\x04WARN\x10\x04\x12\b\n" +
|
"\x04WARN\x10\x04\x12\b\n" +
|
||||||
"\x04INFO\x10\x05\x12\t\n" +
|
"\x04INFO\x10\x05\x12\t\n" +
|
||||||
"\x05DEBUG\x10\x06\x12\t\n" +
|
"\x05DEBUG\x10\x06\x12\t\n" +
|
||||||
"\x05TRACE\x10\a*S\n" +
|
"\x05TRACE\x10\a2\xdd\x14\n" +
|
||||||
"\x0eExposeProtocol\x12\x0f\n" +
|
|
||||||
"\vEXPOSE_HTTP\x10\x00\x12\x10\n" +
|
|
||||||
"\fEXPOSE_HTTPS\x10\x01\x12\x0e\n" +
|
|
||||||
"\n" +
|
|
||||||
"EXPOSE_TCP\x10\x02\x12\x0e\n" +
|
|
||||||
"\n" +
|
|
||||||
"EXPOSE_UDP\x10\x032\xac\x15\n" +
|
|
||||||
"\rDaemonService\x126\n" +
|
"\rDaemonService\x126\n" +
|
||||||
"\x05Login\x12\x14.daemon.LoginRequest\x1a\x15.daemon.LoginResponse\"\x00\x12K\n" +
|
"\x05Login\x12\x14.daemon.LoginRequest\x1a\x15.daemon.LoginResponse\"\x00\x12K\n" +
|
||||||
"\fWaitSSOLogin\x12\x1b.daemon.WaitSSOLoginRequest\x1a\x1c.daemon.WaitSSOLoginResponse\"\x00\x12-\n" +
|
"\fWaitSSOLogin\x12\x1b.daemon.WaitSSOLoginRequest\x1a\x1c.daemon.WaitSSOLoginResponse\"\x00\x12-\n" +
|
||||||
@@ -6492,8 +6197,7 @@ const file_daemon_proto_rawDesc = "" +
|
|||||||
"\x0fStartCPUProfile\x12\x1e.daemon.StartCPUProfileRequest\x1a\x1f.daemon.StartCPUProfileResponse\"\x00\x12Q\n" +
|
"\x0fStartCPUProfile\x12\x1e.daemon.StartCPUProfileRequest\x1a\x1f.daemon.StartCPUProfileResponse\"\x00\x12Q\n" +
|
||||||
"\x0eStopCPUProfile\x12\x1d.daemon.StopCPUProfileRequest\x1a\x1e.daemon.StopCPUProfileResponse\"\x00\x12N\n" +
|
"\x0eStopCPUProfile\x12\x1d.daemon.StopCPUProfileRequest\x1a\x1e.daemon.StopCPUProfileResponse\"\x00\x12N\n" +
|
||||||
"\x11NotifyOSLifecycle\x12\x1a.daemon.OSLifecycleRequest\x1a\x1b.daemon.OSLifecycleResponse\"\x00\x12W\n" +
|
"\x11NotifyOSLifecycle\x12\x1a.daemon.OSLifecycleRequest\x1a\x1b.daemon.OSLifecycleResponse\"\x00\x12W\n" +
|
||||||
"\x12GetInstallerResult\x12\x1e.daemon.InstallerResultRequest\x1a\x1f.daemon.InstallerResultResponse\"\x00\x12M\n" +
|
"\x12GetInstallerResult\x12\x1e.daemon.InstallerResultRequest\x1a\x1f.daemon.InstallerResultResponse\"\x00B\bZ\x06/protob\x06proto3"
|
||||||
"\rExposeService\x12\x1c.daemon.ExposeServiceRequest\x1a\x1a.daemon.ExposeServiceEvent\"\x000\x01B\bZ\x06/protob\x06proto3"
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
file_daemon_proto_rawDescOnce sync.Once
|
file_daemon_proto_rawDescOnce sync.Once
|
||||||
@@ -6507,222 +6211,214 @@ func file_daemon_proto_rawDescGZIP() []byte {
|
|||||||
return file_daemon_proto_rawDescData
|
return file_daemon_proto_rawDescData
|
||||||
}
|
}
|
||||||
|
|
||||||
var file_daemon_proto_enumTypes = make([]protoimpl.EnumInfo, 5)
|
var file_daemon_proto_enumTypes = make([]protoimpl.EnumInfo, 4)
|
||||||
var file_daemon_proto_msgTypes = make([]protoimpl.MessageInfo, 91)
|
var file_daemon_proto_msgTypes = make([]protoimpl.MessageInfo, 88)
|
||||||
var file_daemon_proto_goTypes = []any{
|
var file_daemon_proto_goTypes = []any{
|
||||||
(LogLevel)(0), // 0: daemon.LogLevel
|
(LogLevel)(0), // 0: daemon.LogLevel
|
||||||
(ExposeProtocol)(0), // 1: daemon.ExposeProtocol
|
(OSLifecycleRequest_CycleType)(0), // 1: daemon.OSLifecycleRequest.CycleType
|
||||||
(OSLifecycleRequest_CycleType)(0), // 2: daemon.OSLifecycleRequest.CycleType
|
(SystemEvent_Severity)(0), // 2: daemon.SystemEvent.Severity
|
||||||
(SystemEvent_Severity)(0), // 3: daemon.SystemEvent.Severity
|
(SystemEvent_Category)(0), // 3: daemon.SystemEvent.Category
|
||||||
(SystemEvent_Category)(0), // 4: daemon.SystemEvent.Category
|
(*EmptyRequest)(nil), // 4: daemon.EmptyRequest
|
||||||
(*EmptyRequest)(nil), // 5: daemon.EmptyRequest
|
(*OSLifecycleRequest)(nil), // 5: daemon.OSLifecycleRequest
|
||||||
(*OSLifecycleRequest)(nil), // 6: daemon.OSLifecycleRequest
|
(*OSLifecycleResponse)(nil), // 6: daemon.OSLifecycleResponse
|
||||||
(*OSLifecycleResponse)(nil), // 7: daemon.OSLifecycleResponse
|
(*LoginRequest)(nil), // 7: daemon.LoginRequest
|
||||||
(*LoginRequest)(nil), // 8: daemon.LoginRequest
|
(*LoginResponse)(nil), // 8: daemon.LoginResponse
|
||||||
(*LoginResponse)(nil), // 9: daemon.LoginResponse
|
(*WaitSSOLoginRequest)(nil), // 9: daemon.WaitSSOLoginRequest
|
||||||
(*WaitSSOLoginRequest)(nil), // 10: daemon.WaitSSOLoginRequest
|
(*WaitSSOLoginResponse)(nil), // 10: daemon.WaitSSOLoginResponse
|
||||||
(*WaitSSOLoginResponse)(nil), // 11: daemon.WaitSSOLoginResponse
|
(*UpRequest)(nil), // 11: daemon.UpRequest
|
||||||
(*UpRequest)(nil), // 12: daemon.UpRequest
|
(*UpResponse)(nil), // 12: daemon.UpResponse
|
||||||
(*UpResponse)(nil), // 13: daemon.UpResponse
|
(*StatusRequest)(nil), // 13: daemon.StatusRequest
|
||||||
(*StatusRequest)(nil), // 14: daemon.StatusRequest
|
(*StatusResponse)(nil), // 14: daemon.StatusResponse
|
||||||
(*StatusResponse)(nil), // 15: daemon.StatusResponse
|
(*DownRequest)(nil), // 15: daemon.DownRequest
|
||||||
(*DownRequest)(nil), // 16: daemon.DownRequest
|
(*DownResponse)(nil), // 16: daemon.DownResponse
|
||||||
(*DownResponse)(nil), // 17: daemon.DownResponse
|
(*GetConfigRequest)(nil), // 17: daemon.GetConfigRequest
|
||||||
(*GetConfigRequest)(nil), // 18: daemon.GetConfigRequest
|
(*GetConfigResponse)(nil), // 18: daemon.GetConfigResponse
|
||||||
(*GetConfigResponse)(nil), // 19: daemon.GetConfigResponse
|
(*PeerState)(nil), // 19: daemon.PeerState
|
||||||
(*PeerState)(nil), // 20: daemon.PeerState
|
(*LocalPeerState)(nil), // 20: daemon.LocalPeerState
|
||||||
(*LocalPeerState)(nil), // 21: daemon.LocalPeerState
|
(*SignalState)(nil), // 21: daemon.SignalState
|
||||||
(*SignalState)(nil), // 22: daemon.SignalState
|
(*ManagementState)(nil), // 22: daemon.ManagementState
|
||||||
(*ManagementState)(nil), // 23: daemon.ManagementState
|
(*RelayState)(nil), // 23: daemon.RelayState
|
||||||
(*RelayState)(nil), // 24: daemon.RelayState
|
(*NSGroupState)(nil), // 24: daemon.NSGroupState
|
||||||
(*NSGroupState)(nil), // 25: daemon.NSGroupState
|
(*SSHSessionInfo)(nil), // 25: daemon.SSHSessionInfo
|
||||||
(*SSHSessionInfo)(nil), // 26: daemon.SSHSessionInfo
|
(*SSHServerState)(nil), // 26: daemon.SSHServerState
|
||||||
(*SSHServerState)(nil), // 27: daemon.SSHServerState
|
(*FullStatus)(nil), // 27: daemon.FullStatus
|
||||||
(*FullStatus)(nil), // 28: daemon.FullStatus
|
(*ListNetworksRequest)(nil), // 28: daemon.ListNetworksRequest
|
||||||
(*ListNetworksRequest)(nil), // 29: daemon.ListNetworksRequest
|
(*ListNetworksResponse)(nil), // 29: daemon.ListNetworksResponse
|
||||||
(*ListNetworksResponse)(nil), // 30: daemon.ListNetworksResponse
|
(*SelectNetworksRequest)(nil), // 30: daemon.SelectNetworksRequest
|
||||||
(*SelectNetworksRequest)(nil), // 31: daemon.SelectNetworksRequest
|
(*SelectNetworksResponse)(nil), // 31: daemon.SelectNetworksResponse
|
||||||
(*SelectNetworksResponse)(nil), // 32: daemon.SelectNetworksResponse
|
(*IPList)(nil), // 32: daemon.IPList
|
||||||
(*IPList)(nil), // 33: daemon.IPList
|
(*Network)(nil), // 33: daemon.Network
|
||||||
(*Network)(nil), // 34: daemon.Network
|
(*PortInfo)(nil), // 34: daemon.PortInfo
|
||||||
(*PortInfo)(nil), // 35: daemon.PortInfo
|
(*ForwardingRule)(nil), // 35: daemon.ForwardingRule
|
||||||
(*ForwardingRule)(nil), // 36: daemon.ForwardingRule
|
(*ForwardingRulesResponse)(nil), // 36: daemon.ForwardingRulesResponse
|
||||||
(*ForwardingRulesResponse)(nil), // 37: daemon.ForwardingRulesResponse
|
(*DebugBundleRequest)(nil), // 37: daemon.DebugBundleRequest
|
||||||
(*DebugBundleRequest)(nil), // 38: daemon.DebugBundleRequest
|
(*DebugBundleResponse)(nil), // 38: daemon.DebugBundleResponse
|
||||||
(*DebugBundleResponse)(nil), // 39: daemon.DebugBundleResponse
|
(*GetLogLevelRequest)(nil), // 39: daemon.GetLogLevelRequest
|
||||||
(*GetLogLevelRequest)(nil), // 40: daemon.GetLogLevelRequest
|
(*GetLogLevelResponse)(nil), // 40: daemon.GetLogLevelResponse
|
||||||
(*GetLogLevelResponse)(nil), // 41: daemon.GetLogLevelResponse
|
(*SetLogLevelRequest)(nil), // 41: daemon.SetLogLevelRequest
|
||||||
(*SetLogLevelRequest)(nil), // 42: daemon.SetLogLevelRequest
|
(*SetLogLevelResponse)(nil), // 42: daemon.SetLogLevelResponse
|
||||||
(*SetLogLevelResponse)(nil), // 43: daemon.SetLogLevelResponse
|
(*State)(nil), // 43: daemon.State
|
||||||
(*State)(nil), // 44: daemon.State
|
(*ListStatesRequest)(nil), // 44: daemon.ListStatesRequest
|
||||||
(*ListStatesRequest)(nil), // 45: daemon.ListStatesRequest
|
(*ListStatesResponse)(nil), // 45: daemon.ListStatesResponse
|
||||||
(*ListStatesResponse)(nil), // 46: daemon.ListStatesResponse
|
(*CleanStateRequest)(nil), // 46: daemon.CleanStateRequest
|
||||||
(*CleanStateRequest)(nil), // 47: daemon.CleanStateRequest
|
(*CleanStateResponse)(nil), // 47: daemon.CleanStateResponse
|
||||||
(*CleanStateResponse)(nil), // 48: daemon.CleanStateResponse
|
(*DeleteStateRequest)(nil), // 48: daemon.DeleteStateRequest
|
||||||
(*DeleteStateRequest)(nil), // 49: daemon.DeleteStateRequest
|
(*DeleteStateResponse)(nil), // 49: daemon.DeleteStateResponse
|
||||||
(*DeleteStateResponse)(nil), // 50: daemon.DeleteStateResponse
|
(*SetSyncResponsePersistenceRequest)(nil), // 50: daemon.SetSyncResponsePersistenceRequest
|
||||||
(*SetSyncResponsePersistenceRequest)(nil), // 51: daemon.SetSyncResponsePersistenceRequest
|
(*SetSyncResponsePersistenceResponse)(nil), // 51: daemon.SetSyncResponsePersistenceResponse
|
||||||
(*SetSyncResponsePersistenceResponse)(nil), // 52: daemon.SetSyncResponsePersistenceResponse
|
(*TCPFlags)(nil), // 52: daemon.TCPFlags
|
||||||
(*TCPFlags)(nil), // 53: daemon.TCPFlags
|
(*TracePacketRequest)(nil), // 53: daemon.TracePacketRequest
|
||||||
(*TracePacketRequest)(nil), // 54: daemon.TracePacketRequest
|
(*TraceStage)(nil), // 54: daemon.TraceStage
|
||||||
(*TraceStage)(nil), // 55: daemon.TraceStage
|
(*TracePacketResponse)(nil), // 55: daemon.TracePacketResponse
|
||||||
(*TracePacketResponse)(nil), // 56: daemon.TracePacketResponse
|
(*SubscribeRequest)(nil), // 56: daemon.SubscribeRequest
|
||||||
(*SubscribeRequest)(nil), // 57: daemon.SubscribeRequest
|
(*SystemEvent)(nil), // 57: daemon.SystemEvent
|
||||||
(*SystemEvent)(nil), // 58: daemon.SystemEvent
|
(*GetEventsRequest)(nil), // 58: daemon.GetEventsRequest
|
||||||
(*GetEventsRequest)(nil), // 59: daemon.GetEventsRequest
|
(*GetEventsResponse)(nil), // 59: daemon.GetEventsResponse
|
||||||
(*GetEventsResponse)(nil), // 60: daemon.GetEventsResponse
|
(*SwitchProfileRequest)(nil), // 60: daemon.SwitchProfileRequest
|
||||||
(*SwitchProfileRequest)(nil), // 61: daemon.SwitchProfileRequest
|
(*SwitchProfileResponse)(nil), // 61: daemon.SwitchProfileResponse
|
||||||
(*SwitchProfileResponse)(nil), // 62: daemon.SwitchProfileResponse
|
(*SetConfigRequest)(nil), // 62: daemon.SetConfigRequest
|
||||||
(*SetConfigRequest)(nil), // 63: daemon.SetConfigRequest
|
(*SetConfigResponse)(nil), // 63: daemon.SetConfigResponse
|
||||||
(*SetConfigResponse)(nil), // 64: daemon.SetConfigResponse
|
(*AddProfileRequest)(nil), // 64: daemon.AddProfileRequest
|
||||||
(*AddProfileRequest)(nil), // 65: daemon.AddProfileRequest
|
(*AddProfileResponse)(nil), // 65: daemon.AddProfileResponse
|
||||||
(*AddProfileResponse)(nil), // 66: daemon.AddProfileResponse
|
(*RemoveProfileRequest)(nil), // 66: daemon.RemoveProfileRequest
|
||||||
(*RemoveProfileRequest)(nil), // 67: daemon.RemoveProfileRequest
|
(*RemoveProfileResponse)(nil), // 67: daemon.RemoveProfileResponse
|
||||||
(*RemoveProfileResponse)(nil), // 68: daemon.RemoveProfileResponse
|
(*ListProfilesRequest)(nil), // 68: daemon.ListProfilesRequest
|
||||||
(*ListProfilesRequest)(nil), // 69: daemon.ListProfilesRequest
|
(*ListProfilesResponse)(nil), // 69: daemon.ListProfilesResponse
|
||||||
(*ListProfilesResponse)(nil), // 70: daemon.ListProfilesResponse
|
(*Profile)(nil), // 70: daemon.Profile
|
||||||
(*Profile)(nil), // 71: daemon.Profile
|
(*GetActiveProfileRequest)(nil), // 71: daemon.GetActiveProfileRequest
|
||||||
(*GetActiveProfileRequest)(nil), // 72: daemon.GetActiveProfileRequest
|
(*GetActiveProfileResponse)(nil), // 72: daemon.GetActiveProfileResponse
|
||||||
(*GetActiveProfileResponse)(nil), // 73: daemon.GetActiveProfileResponse
|
(*LogoutRequest)(nil), // 73: daemon.LogoutRequest
|
||||||
(*LogoutRequest)(nil), // 74: daemon.LogoutRequest
|
(*LogoutResponse)(nil), // 74: daemon.LogoutResponse
|
||||||
(*LogoutResponse)(nil), // 75: daemon.LogoutResponse
|
(*GetFeaturesRequest)(nil), // 75: daemon.GetFeaturesRequest
|
||||||
(*GetFeaturesRequest)(nil), // 76: daemon.GetFeaturesRequest
|
(*GetFeaturesResponse)(nil), // 76: daemon.GetFeaturesResponse
|
||||||
(*GetFeaturesResponse)(nil), // 77: daemon.GetFeaturesResponse
|
(*GetPeerSSHHostKeyRequest)(nil), // 77: daemon.GetPeerSSHHostKeyRequest
|
||||||
(*GetPeerSSHHostKeyRequest)(nil), // 78: daemon.GetPeerSSHHostKeyRequest
|
(*GetPeerSSHHostKeyResponse)(nil), // 78: daemon.GetPeerSSHHostKeyResponse
|
||||||
(*GetPeerSSHHostKeyResponse)(nil), // 79: daemon.GetPeerSSHHostKeyResponse
|
(*RequestJWTAuthRequest)(nil), // 79: daemon.RequestJWTAuthRequest
|
||||||
(*RequestJWTAuthRequest)(nil), // 80: daemon.RequestJWTAuthRequest
|
(*RequestJWTAuthResponse)(nil), // 80: daemon.RequestJWTAuthResponse
|
||||||
(*RequestJWTAuthResponse)(nil), // 81: daemon.RequestJWTAuthResponse
|
(*WaitJWTTokenRequest)(nil), // 81: daemon.WaitJWTTokenRequest
|
||||||
(*WaitJWTTokenRequest)(nil), // 82: daemon.WaitJWTTokenRequest
|
(*WaitJWTTokenResponse)(nil), // 82: daemon.WaitJWTTokenResponse
|
||||||
(*WaitJWTTokenResponse)(nil), // 83: daemon.WaitJWTTokenResponse
|
(*StartCPUProfileRequest)(nil), // 83: daemon.StartCPUProfileRequest
|
||||||
(*StartCPUProfileRequest)(nil), // 84: daemon.StartCPUProfileRequest
|
(*StartCPUProfileResponse)(nil), // 84: daemon.StartCPUProfileResponse
|
||||||
(*StartCPUProfileResponse)(nil), // 85: daemon.StartCPUProfileResponse
|
(*StopCPUProfileRequest)(nil), // 85: daemon.StopCPUProfileRequest
|
||||||
(*StopCPUProfileRequest)(nil), // 86: daemon.StopCPUProfileRequest
|
(*StopCPUProfileResponse)(nil), // 86: daemon.StopCPUProfileResponse
|
||||||
(*StopCPUProfileResponse)(nil), // 87: daemon.StopCPUProfileResponse
|
(*InstallerResultRequest)(nil), // 87: daemon.InstallerResultRequest
|
||||||
(*InstallerResultRequest)(nil), // 88: daemon.InstallerResultRequest
|
(*InstallerResultResponse)(nil), // 88: daemon.InstallerResultResponse
|
||||||
(*InstallerResultResponse)(nil), // 89: daemon.InstallerResultResponse
|
nil, // 89: daemon.Network.ResolvedIPsEntry
|
||||||
(*ExposeServiceRequest)(nil), // 90: daemon.ExposeServiceRequest
|
(*PortInfo_Range)(nil), // 90: daemon.PortInfo.Range
|
||||||
(*ExposeServiceEvent)(nil), // 91: daemon.ExposeServiceEvent
|
nil, // 91: daemon.SystemEvent.MetadataEntry
|
||||||
(*ExposeServiceReady)(nil), // 92: daemon.ExposeServiceReady
|
(*durationpb.Duration)(nil), // 92: google.protobuf.Duration
|
||||||
nil, // 93: daemon.Network.ResolvedIPsEntry
|
(*timestamppb.Timestamp)(nil), // 93: google.protobuf.Timestamp
|
||||||
(*PortInfo_Range)(nil), // 94: daemon.PortInfo.Range
|
|
||||||
nil, // 95: daemon.SystemEvent.MetadataEntry
|
|
||||||
(*durationpb.Duration)(nil), // 96: google.protobuf.Duration
|
|
||||||
(*timestamppb.Timestamp)(nil), // 97: google.protobuf.Timestamp
|
|
||||||
}
|
}
|
||||||
var file_daemon_proto_depIdxs = []int32{
|
var file_daemon_proto_depIdxs = []int32{
|
||||||
2, // 0: daemon.OSLifecycleRequest.type:type_name -> daemon.OSLifecycleRequest.CycleType
|
1, // 0: daemon.OSLifecycleRequest.type:type_name -> daemon.OSLifecycleRequest.CycleType
|
||||||
96, // 1: daemon.LoginRequest.dnsRouteInterval:type_name -> google.protobuf.Duration
|
92, // 1: daemon.LoginRequest.dnsRouteInterval:type_name -> google.protobuf.Duration
|
||||||
28, // 2: daemon.StatusResponse.fullStatus:type_name -> daemon.FullStatus
|
27, // 2: daemon.StatusResponse.fullStatus:type_name -> daemon.FullStatus
|
||||||
97, // 3: daemon.PeerState.connStatusUpdate:type_name -> google.protobuf.Timestamp
|
93, // 3: daemon.PeerState.connStatusUpdate:type_name -> google.protobuf.Timestamp
|
||||||
97, // 4: daemon.PeerState.lastWireguardHandshake:type_name -> google.protobuf.Timestamp
|
93, // 4: daemon.PeerState.lastWireguardHandshake:type_name -> google.protobuf.Timestamp
|
||||||
96, // 5: daemon.PeerState.latency:type_name -> google.protobuf.Duration
|
92, // 5: daemon.PeerState.latency:type_name -> google.protobuf.Duration
|
||||||
26, // 6: daemon.SSHServerState.sessions:type_name -> daemon.SSHSessionInfo
|
25, // 6: daemon.SSHServerState.sessions:type_name -> daemon.SSHSessionInfo
|
||||||
23, // 7: daemon.FullStatus.managementState:type_name -> daemon.ManagementState
|
22, // 7: daemon.FullStatus.managementState:type_name -> daemon.ManagementState
|
||||||
22, // 8: daemon.FullStatus.signalState:type_name -> daemon.SignalState
|
21, // 8: daemon.FullStatus.signalState:type_name -> daemon.SignalState
|
||||||
21, // 9: daemon.FullStatus.localPeerState:type_name -> daemon.LocalPeerState
|
20, // 9: daemon.FullStatus.localPeerState:type_name -> daemon.LocalPeerState
|
||||||
20, // 10: daemon.FullStatus.peers:type_name -> daemon.PeerState
|
19, // 10: daemon.FullStatus.peers:type_name -> daemon.PeerState
|
||||||
24, // 11: daemon.FullStatus.relays:type_name -> daemon.RelayState
|
23, // 11: daemon.FullStatus.relays:type_name -> daemon.RelayState
|
||||||
25, // 12: daemon.FullStatus.dns_servers:type_name -> daemon.NSGroupState
|
24, // 12: daemon.FullStatus.dns_servers:type_name -> daemon.NSGroupState
|
||||||
58, // 13: daemon.FullStatus.events:type_name -> daemon.SystemEvent
|
57, // 13: daemon.FullStatus.events:type_name -> daemon.SystemEvent
|
||||||
27, // 14: daemon.FullStatus.sshServerState:type_name -> daemon.SSHServerState
|
26, // 14: daemon.FullStatus.sshServerState:type_name -> daemon.SSHServerState
|
||||||
34, // 15: daemon.ListNetworksResponse.routes:type_name -> daemon.Network
|
33, // 15: daemon.ListNetworksResponse.routes:type_name -> daemon.Network
|
||||||
93, // 16: daemon.Network.resolvedIPs:type_name -> daemon.Network.ResolvedIPsEntry
|
89, // 16: daemon.Network.resolvedIPs:type_name -> daemon.Network.ResolvedIPsEntry
|
||||||
94, // 17: daemon.PortInfo.range:type_name -> daemon.PortInfo.Range
|
90, // 17: daemon.PortInfo.range:type_name -> daemon.PortInfo.Range
|
||||||
35, // 18: daemon.ForwardingRule.destinationPort:type_name -> daemon.PortInfo
|
34, // 18: daemon.ForwardingRule.destinationPort:type_name -> daemon.PortInfo
|
||||||
35, // 19: daemon.ForwardingRule.translatedPort:type_name -> daemon.PortInfo
|
34, // 19: daemon.ForwardingRule.translatedPort:type_name -> daemon.PortInfo
|
||||||
36, // 20: daemon.ForwardingRulesResponse.rules:type_name -> daemon.ForwardingRule
|
35, // 20: daemon.ForwardingRulesResponse.rules:type_name -> daemon.ForwardingRule
|
||||||
0, // 21: daemon.GetLogLevelResponse.level:type_name -> daemon.LogLevel
|
0, // 21: daemon.GetLogLevelResponse.level:type_name -> daemon.LogLevel
|
||||||
0, // 22: daemon.SetLogLevelRequest.level:type_name -> daemon.LogLevel
|
0, // 22: daemon.SetLogLevelRequest.level:type_name -> daemon.LogLevel
|
||||||
44, // 23: daemon.ListStatesResponse.states:type_name -> daemon.State
|
43, // 23: daemon.ListStatesResponse.states:type_name -> daemon.State
|
||||||
53, // 24: daemon.TracePacketRequest.tcp_flags:type_name -> daemon.TCPFlags
|
52, // 24: daemon.TracePacketRequest.tcp_flags:type_name -> daemon.TCPFlags
|
||||||
55, // 25: daemon.TracePacketResponse.stages:type_name -> daemon.TraceStage
|
54, // 25: daemon.TracePacketResponse.stages:type_name -> daemon.TraceStage
|
||||||
3, // 26: daemon.SystemEvent.severity:type_name -> daemon.SystemEvent.Severity
|
2, // 26: daemon.SystemEvent.severity:type_name -> daemon.SystemEvent.Severity
|
||||||
4, // 27: daemon.SystemEvent.category:type_name -> daemon.SystemEvent.Category
|
3, // 27: daemon.SystemEvent.category:type_name -> daemon.SystemEvent.Category
|
||||||
97, // 28: daemon.SystemEvent.timestamp:type_name -> google.protobuf.Timestamp
|
93, // 28: daemon.SystemEvent.timestamp:type_name -> google.protobuf.Timestamp
|
||||||
95, // 29: daemon.SystemEvent.metadata:type_name -> daemon.SystemEvent.MetadataEntry
|
91, // 29: daemon.SystemEvent.metadata:type_name -> daemon.SystemEvent.MetadataEntry
|
||||||
58, // 30: daemon.GetEventsResponse.events:type_name -> daemon.SystemEvent
|
57, // 30: daemon.GetEventsResponse.events:type_name -> daemon.SystemEvent
|
||||||
96, // 31: daemon.SetConfigRequest.dnsRouteInterval:type_name -> google.protobuf.Duration
|
92, // 31: daemon.SetConfigRequest.dnsRouteInterval:type_name -> google.protobuf.Duration
|
||||||
71, // 32: daemon.ListProfilesResponse.profiles:type_name -> daemon.Profile
|
70, // 32: daemon.ListProfilesResponse.profiles:type_name -> daemon.Profile
|
||||||
1, // 33: daemon.ExposeServiceRequest.protocol:type_name -> daemon.ExposeProtocol
|
32, // 33: daemon.Network.ResolvedIPsEntry.value:type_name -> daemon.IPList
|
||||||
92, // 34: daemon.ExposeServiceEvent.ready:type_name -> daemon.ExposeServiceReady
|
7, // 34: daemon.DaemonService.Login:input_type -> daemon.LoginRequest
|
||||||
33, // 35: daemon.Network.ResolvedIPsEntry.value:type_name -> daemon.IPList
|
9, // 35: daemon.DaemonService.WaitSSOLogin:input_type -> daemon.WaitSSOLoginRequest
|
||||||
8, // 36: daemon.DaemonService.Login:input_type -> daemon.LoginRequest
|
11, // 36: daemon.DaemonService.Up:input_type -> daemon.UpRequest
|
||||||
10, // 37: daemon.DaemonService.WaitSSOLogin:input_type -> daemon.WaitSSOLoginRequest
|
13, // 37: daemon.DaemonService.Status:input_type -> daemon.StatusRequest
|
||||||
12, // 38: daemon.DaemonService.Up:input_type -> daemon.UpRequest
|
15, // 38: daemon.DaemonService.Down:input_type -> daemon.DownRequest
|
||||||
14, // 39: daemon.DaemonService.Status:input_type -> daemon.StatusRequest
|
17, // 39: daemon.DaemonService.GetConfig:input_type -> daemon.GetConfigRequest
|
||||||
16, // 40: daemon.DaemonService.Down:input_type -> daemon.DownRequest
|
28, // 40: daemon.DaemonService.ListNetworks:input_type -> daemon.ListNetworksRequest
|
||||||
18, // 41: daemon.DaemonService.GetConfig:input_type -> daemon.GetConfigRequest
|
30, // 41: daemon.DaemonService.SelectNetworks:input_type -> daemon.SelectNetworksRequest
|
||||||
29, // 42: daemon.DaemonService.ListNetworks:input_type -> daemon.ListNetworksRequest
|
30, // 42: daemon.DaemonService.DeselectNetworks:input_type -> daemon.SelectNetworksRequest
|
||||||
31, // 43: daemon.DaemonService.SelectNetworks:input_type -> daemon.SelectNetworksRequest
|
4, // 43: daemon.DaemonService.ForwardingRules:input_type -> daemon.EmptyRequest
|
||||||
31, // 44: daemon.DaemonService.DeselectNetworks:input_type -> daemon.SelectNetworksRequest
|
37, // 44: daemon.DaemonService.DebugBundle:input_type -> daemon.DebugBundleRequest
|
||||||
5, // 45: daemon.DaemonService.ForwardingRules:input_type -> daemon.EmptyRequest
|
39, // 45: daemon.DaemonService.GetLogLevel:input_type -> daemon.GetLogLevelRequest
|
||||||
38, // 46: daemon.DaemonService.DebugBundle:input_type -> daemon.DebugBundleRequest
|
41, // 46: daemon.DaemonService.SetLogLevel:input_type -> daemon.SetLogLevelRequest
|
||||||
40, // 47: daemon.DaemonService.GetLogLevel:input_type -> daemon.GetLogLevelRequest
|
44, // 47: daemon.DaemonService.ListStates:input_type -> daemon.ListStatesRequest
|
||||||
42, // 48: daemon.DaemonService.SetLogLevel:input_type -> daemon.SetLogLevelRequest
|
46, // 48: daemon.DaemonService.CleanState:input_type -> daemon.CleanStateRequest
|
||||||
45, // 49: daemon.DaemonService.ListStates:input_type -> daemon.ListStatesRequest
|
48, // 49: daemon.DaemonService.DeleteState:input_type -> daemon.DeleteStateRequest
|
||||||
47, // 50: daemon.DaemonService.CleanState:input_type -> daemon.CleanStateRequest
|
50, // 50: daemon.DaemonService.SetSyncResponsePersistence:input_type -> daemon.SetSyncResponsePersistenceRequest
|
||||||
49, // 51: daemon.DaemonService.DeleteState:input_type -> daemon.DeleteStateRequest
|
53, // 51: daemon.DaemonService.TracePacket:input_type -> daemon.TracePacketRequest
|
||||||
51, // 52: daemon.DaemonService.SetSyncResponsePersistence:input_type -> daemon.SetSyncResponsePersistenceRequest
|
56, // 52: daemon.DaemonService.SubscribeEvents:input_type -> daemon.SubscribeRequest
|
||||||
54, // 53: daemon.DaemonService.TracePacket:input_type -> daemon.TracePacketRequest
|
58, // 53: daemon.DaemonService.GetEvents:input_type -> daemon.GetEventsRequest
|
||||||
57, // 54: daemon.DaemonService.SubscribeEvents:input_type -> daemon.SubscribeRequest
|
60, // 54: daemon.DaemonService.SwitchProfile:input_type -> daemon.SwitchProfileRequest
|
||||||
59, // 55: daemon.DaemonService.GetEvents:input_type -> daemon.GetEventsRequest
|
62, // 55: daemon.DaemonService.SetConfig:input_type -> daemon.SetConfigRequest
|
||||||
61, // 56: daemon.DaemonService.SwitchProfile:input_type -> daemon.SwitchProfileRequest
|
64, // 56: daemon.DaemonService.AddProfile:input_type -> daemon.AddProfileRequest
|
||||||
63, // 57: daemon.DaemonService.SetConfig:input_type -> daemon.SetConfigRequest
|
66, // 57: daemon.DaemonService.RemoveProfile:input_type -> daemon.RemoveProfileRequest
|
||||||
65, // 58: daemon.DaemonService.AddProfile:input_type -> daemon.AddProfileRequest
|
68, // 58: daemon.DaemonService.ListProfiles:input_type -> daemon.ListProfilesRequest
|
||||||
67, // 59: daemon.DaemonService.RemoveProfile:input_type -> daemon.RemoveProfileRequest
|
71, // 59: daemon.DaemonService.GetActiveProfile:input_type -> daemon.GetActiveProfileRequest
|
||||||
69, // 60: daemon.DaemonService.ListProfiles:input_type -> daemon.ListProfilesRequest
|
73, // 60: daemon.DaemonService.Logout:input_type -> daemon.LogoutRequest
|
||||||
72, // 61: daemon.DaemonService.GetActiveProfile:input_type -> daemon.GetActiveProfileRequest
|
75, // 61: daemon.DaemonService.GetFeatures:input_type -> daemon.GetFeaturesRequest
|
||||||
74, // 62: daemon.DaemonService.Logout:input_type -> daemon.LogoutRequest
|
77, // 62: daemon.DaemonService.GetPeerSSHHostKey:input_type -> daemon.GetPeerSSHHostKeyRequest
|
||||||
76, // 63: daemon.DaemonService.GetFeatures:input_type -> daemon.GetFeaturesRequest
|
79, // 63: daemon.DaemonService.RequestJWTAuth:input_type -> daemon.RequestJWTAuthRequest
|
||||||
78, // 64: daemon.DaemonService.GetPeerSSHHostKey:input_type -> daemon.GetPeerSSHHostKeyRequest
|
81, // 64: daemon.DaemonService.WaitJWTToken:input_type -> daemon.WaitJWTTokenRequest
|
||||||
80, // 65: daemon.DaemonService.RequestJWTAuth:input_type -> daemon.RequestJWTAuthRequest
|
83, // 65: daemon.DaemonService.StartCPUProfile:input_type -> daemon.StartCPUProfileRequest
|
||||||
82, // 66: daemon.DaemonService.WaitJWTToken:input_type -> daemon.WaitJWTTokenRequest
|
85, // 66: daemon.DaemonService.StopCPUProfile:input_type -> daemon.StopCPUProfileRequest
|
||||||
84, // 67: daemon.DaemonService.StartCPUProfile:input_type -> daemon.StartCPUProfileRequest
|
5, // 67: daemon.DaemonService.NotifyOSLifecycle:input_type -> daemon.OSLifecycleRequest
|
||||||
86, // 68: daemon.DaemonService.StopCPUProfile:input_type -> daemon.StopCPUProfileRequest
|
87, // 68: daemon.DaemonService.GetInstallerResult:input_type -> daemon.InstallerResultRequest
|
||||||
6, // 69: daemon.DaemonService.NotifyOSLifecycle:input_type -> daemon.OSLifecycleRequest
|
8, // 69: daemon.DaemonService.Login:output_type -> daemon.LoginResponse
|
||||||
88, // 70: daemon.DaemonService.GetInstallerResult:input_type -> daemon.InstallerResultRequest
|
10, // 70: daemon.DaemonService.WaitSSOLogin:output_type -> daemon.WaitSSOLoginResponse
|
||||||
90, // 71: daemon.DaemonService.ExposeService:input_type -> daemon.ExposeServiceRequest
|
12, // 71: daemon.DaemonService.Up:output_type -> daemon.UpResponse
|
||||||
9, // 72: daemon.DaemonService.Login:output_type -> daemon.LoginResponse
|
14, // 72: daemon.DaemonService.Status:output_type -> daemon.StatusResponse
|
||||||
11, // 73: daemon.DaemonService.WaitSSOLogin:output_type -> daemon.WaitSSOLoginResponse
|
16, // 73: daemon.DaemonService.Down:output_type -> daemon.DownResponse
|
||||||
13, // 74: daemon.DaemonService.Up:output_type -> daemon.UpResponse
|
18, // 74: daemon.DaemonService.GetConfig:output_type -> daemon.GetConfigResponse
|
||||||
15, // 75: daemon.DaemonService.Status:output_type -> daemon.StatusResponse
|
29, // 75: daemon.DaemonService.ListNetworks:output_type -> daemon.ListNetworksResponse
|
||||||
17, // 76: daemon.DaemonService.Down:output_type -> daemon.DownResponse
|
31, // 76: daemon.DaemonService.SelectNetworks:output_type -> daemon.SelectNetworksResponse
|
||||||
19, // 77: daemon.DaemonService.GetConfig:output_type -> daemon.GetConfigResponse
|
31, // 77: daemon.DaemonService.DeselectNetworks:output_type -> daemon.SelectNetworksResponse
|
||||||
30, // 78: daemon.DaemonService.ListNetworks:output_type -> daemon.ListNetworksResponse
|
36, // 78: daemon.DaemonService.ForwardingRules:output_type -> daemon.ForwardingRulesResponse
|
||||||
32, // 79: daemon.DaemonService.SelectNetworks:output_type -> daemon.SelectNetworksResponse
|
38, // 79: daemon.DaemonService.DebugBundle:output_type -> daemon.DebugBundleResponse
|
||||||
32, // 80: daemon.DaemonService.DeselectNetworks:output_type -> daemon.SelectNetworksResponse
|
40, // 80: daemon.DaemonService.GetLogLevel:output_type -> daemon.GetLogLevelResponse
|
||||||
37, // 81: daemon.DaemonService.ForwardingRules:output_type -> daemon.ForwardingRulesResponse
|
42, // 81: daemon.DaemonService.SetLogLevel:output_type -> daemon.SetLogLevelResponse
|
||||||
39, // 82: daemon.DaemonService.DebugBundle:output_type -> daemon.DebugBundleResponse
|
45, // 82: daemon.DaemonService.ListStates:output_type -> daemon.ListStatesResponse
|
||||||
41, // 83: daemon.DaemonService.GetLogLevel:output_type -> daemon.GetLogLevelResponse
|
47, // 83: daemon.DaemonService.CleanState:output_type -> daemon.CleanStateResponse
|
||||||
43, // 84: daemon.DaemonService.SetLogLevel:output_type -> daemon.SetLogLevelResponse
|
49, // 84: daemon.DaemonService.DeleteState:output_type -> daemon.DeleteStateResponse
|
||||||
46, // 85: daemon.DaemonService.ListStates:output_type -> daemon.ListStatesResponse
|
51, // 85: daemon.DaemonService.SetSyncResponsePersistence:output_type -> daemon.SetSyncResponsePersistenceResponse
|
||||||
48, // 86: daemon.DaemonService.CleanState:output_type -> daemon.CleanStateResponse
|
55, // 86: daemon.DaemonService.TracePacket:output_type -> daemon.TracePacketResponse
|
||||||
50, // 87: daemon.DaemonService.DeleteState:output_type -> daemon.DeleteStateResponse
|
57, // 87: daemon.DaemonService.SubscribeEvents:output_type -> daemon.SystemEvent
|
||||||
52, // 88: daemon.DaemonService.SetSyncResponsePersistence:output_type -> daemon.SetSyncResponsePersistenceResponse
|
59, // 88: daemon.DaemonService.GetEvents:output_type -> daemon.GetEventsResponse
|
||||||
56, // 89: daemon.DaemonService.TracePacket:output_type -> daemon.TracePacketResponse
|
61, // 89: daemon.DaemonService.SwitchProfile:output_type -> daemon.SwitchProfileResponse
|
||||||
58, // 90: daemon.DaemonService.SubscribeEvents:output_type -> daemon.SystemEvent
|
63, // 90: daemon.DaemonService.SetConfig:output_type -> daemon.SetConfigResponse
|
||||||
60, // 91: daemon.DaemonService.GetEvents:output_type -> daemon.GetEventsResponse
|
65, // 91: daemon.DaemonService.AddProfile:output_type -> daemon.AddProfileResponse
|
||||||
62, // 92: daemon.DaemonService.SwitchProfile:output_type -> daemon.SwitchProfileResponse
|
67, // 92: daemon.DaemonService.RemoveProfile:output_type -> daemon.RemoveProfileResponse
|
||||||
64, // 93: daemon.DaemonService.SetConfig:output_type -> daemon.SetConfigResponse
|
69, // 93: daemon.DaemonService.ListProfiles:output_type -> daemon.ListProfilesResponse
|
||||||
66, // 94: daemon.DaemonService.AddProfile:output_type -> daemon.AddProfileResponse
|
72, // 94: daemon.DaemonService.GetActiveProfile:output_type -> daemon.GetActiveProfileResponse
|
||||||
68, // 95: daemon.DaemonService.RemoveProfile:output_type -> daemon.RemoveProfileResponse
|
74, // 95: daemon.DaemonService.Logout:output_type -> daemon.LogoutResponse
|
||||||
70, // 96: daemon.DaemonService.ListProfiles:output_type -> daemon.ListProfilesResponse
|
76, // 96: daemon.DaemonService.GetFeatures:output_type -> daemon.GetFeaturesResponse
|
||||||
73, // 97: daemon.DaemonService.GetActiveProfile:output_type -> daemon.GetActiveProfileResponse
|
78, // 97: daemon.DaemonService.GetPeerSSHHostKey:output_type -> daemon.GetPeerSSHHostKeyResponse
|
||||||
75, // 98: daemon.DaemonService.Logout:output_type -> daemon.LogoutResponse
|
80, // 98: daemon.DaemonService.RequestJWTAuth:output_type -> daemon.RequestJWTAuthResponse
|
||||||
77, // 99: daemon.DaemonService.GetFeatures:output_type -> daemon.GetFeaturesResponse
|
82, // 99: daemon.DaemonService.WaitJWTToken:output_type -> daemon.WaitJWTTokenResponse
|
||||||
79, // 100: daemon.DaemonService.GetPeerSSHHostKey:output_type -> daemon.GetPeerSSHHostKeyResponse
|
84, // 100: daemon.DaemonService.StartCPUProfile:output_type -> daemon.StartCPUProfileResponse
|
||||||
81, // 101: daemon.DaemonService.RequestJWTAuth:output_type -> daemon.RequestJWTAuthResponse
|
86, // 101: daemon.DaemonService.StopCPUProfile:output_type -> daemon.StopCPUProfileResponse
|
||||||
83, // 102: daemon.DaemonService.WaitJWTToken:output_type -> daemon.WaitJWTTokenResponse
|
6, // 102: daemon.DaemonService.NotifyOSLifecycle:output_type -> daemon.OSLifecycleResponse
|
||||||
85, // 103: daemon.DaemonService.StartCPUProfile:output_type -> daemon.StartCPUProfileResponse
|
88, // 103: daemon.DaemonService.GetInstallerResult:output_type -> daemon.InstallerResultResponse
|
||||||
87, // 104: daemon.DaemonService.StopCPUProfile:output_type -> daemon.StopCPUProfileResponse
|
69, // [69:104] is the sub-list for method output_type
|
||||||
7, // 105: daemon.DaemonService.NotifyOSLifecycle:output_type -> daemon.OSLifecycleResponse
|
34, // [34:69] is the sub-list for method input_type
|
||||||
89, // 106: daemon.DaemonService.GetInstallerResult:output_type -> daemon.InstallerResultResponse
|
34, // [34:34] is the sub-list for extension type_name
|
||||||
91, // 107: daemon.DaemonService.ExposeService:output_type -> daemon.ExposeServiceEvent
|
34, // [34:34] is the sub-list for extension extendee
|
||||||
72, // [72:108] is the sub-list for method output_type
|
0, // [0:34] is the sub-list for field type_name
|
||||||
36, // [36:72] is the sub-list for method input_type
|
|
||||||
36, // [36:36] is the sub-list for extension type_name
|
|
||||||
36, // [36:36] is the sub-list for extension extendee
|
|
||||||
0, // [0:36] is the sub-list for field type_name
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() { file_daemon_proto_init() }
|
func init() { file_daemon_proto_init() }
|
||||||
@@ -6743,16 +6439,13 @@ func file_daemon_proto_init() {
|
|||||||
file_daemon_proto_msgTypes[58].OneofWrappers = []any{}
|
file_daemon_proto_msgTypes[58].OneofWrappers = []any{}
|
||||||
file_daemon_proto_msgTypes[69].OneofWrappers = []any{}
|
file_daemon_proto_msgTypes[69].OneofWrappers = []any{}
|
||||||
file_daemon_proto_msgTypes[75].OneofWrappers = []any{}
|
file_daemon_proto_msgTypes[75].OneofWrappers = []any{}
|
||||||
file_daemon_proto_msgTypes[86].OneofWrappers = []any{
|
|
||||||
(*ExposeServiceEvent_Ready)(nil),
|
|
||||||
}
|
|
||||||
type x struct{}
|
type x struct{}
|
||||||
out := protoimpl.TypeBuilder{
|
out := protoimpl.TypeBuilder{
|
||||||
File: protoimpl.DescBuilder{
|
File: protoimpl.DescBuilder{
|
||||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||||
RawDescriptor: unsafe.Slice(unsafe.StringData(file_daemon_proto_rawDesc), len(file_daemon_proto_rawDesc)),
|
RawDescriptor: unsafe.Slice(unsafe.StringData(file_daemon_proto_rawDesc), len(file_daemon_proto_rawDesc)),
|
||||||
NumEnums: 5,
|
NumEnums: 4,
|
||||||
NumMessages: 91,
|
NumMessages: 88,
|
||||||
NumExtensions: 0,
|
NumExtensions: 0,
|
||||||
NumServices: 1,
|
NumServices: 1,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -103,9 +103,6 @@ service DaemonService {
|
|||||||
rpc NotifyOSLifecycle(OSLifecycleRequest) returns(OSLifecycleResponse) {}
|
rpc NotifyOSLifecycle(OSLifecycleRequest) returns(OSLifecycleResponse) {}
|
||||||
|
|
||||||
rpc GetInstallerResult(InstallerResultRequest) returns (InstallerResultResponse) {}
|
rpc GetInstallerResult(InstallerResultRequest) returns (InstallerResultResponse) {}
|
||||||
|
|
||||||
// ExposeService exposes a local port via the NetBird reverse proxy
|
|
||||||
rpc ExposeService(ExposeServiceRequest) returns (stream ExposeServiceEvent) {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -804,32 +801,3 @@ message InstallerResultResponse {
|
|||||||
bool success = 1;
|
bool success = 1;
|
||||||
string errorMsg = 2;
|
string errorMsg = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
enum ExposeProtocol {
|
|
||||||
EXPOSE_HTTP = 0;
|
|
||||||
EXPOSE_HTTPS = 1;
|
|
||||||
EXPOSE_TCP = 2;
|
|
||||||
EXPOSE_UDP = 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
message ExposeServiceRequest {
|
|
||||||
uint32 port = 1;
|
|
||||||
ExposeProtocol protocol = 2;
|
|
||||||
string pin = 3;
|
|
||||||
string password = 4;
|
|
||||||
repeated string user_groups = 5;
|
|
||||||
string domain = 6;
|
|
||||||
string name_prefix = 7;
|
|
||||||
}
|
|
||||||
|
|
||||||
message ExposeServiceEvent {
|
|
||||||
oneof event {
|
|
||||||
ExposeServiceReady ready = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
message ExposeServiceReady {
|
|
||||||
string service_name = 1;
|
|
||||||
string service_url = 2;
|
|
||||||
string domain = 3;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -76,8 +76,6 @@ type DaemonServiceClient interface {
|
|||||||
StopCPUProfile(ctx context.Context, in *StopCPUProfileRequest, opts ...grpc.CallOption) (*StopCPUProfileResponse, error)
|
StopCPUProfile(ctx context.Context, in *StopCPUProfileRequest, opts ...grpc.CallOption) (*StopCPUProfileResponse, error)
|
||||||
NotifyOSLifecycle(ctx context.Context, in *OSLifecycleRequest, opts ...grpc.CallOption) (*OSLifecycleResponse, error)
|
NotifyOSLifecycle(ctx context.Context, in *OSLifecycleRequest, opts ...grpc.CallOption) (*OSLifecycleResponse, error)
|
||||||
GetInstallerResult(ctx context.Context, in *InstallerResultRequest, opts ...grpc.CallOption) (*InstallerResultResponse, error)
|
GetInstallerResult(ctx context.Context, in *InstallerResultRequest, opts ...grpc.CallOption) (*InstallerResultResponse, error)
|
||||||
// ExposeService exposes a local port via the NetBird reverse proxy
|
|
||||||
ExposeService(ctx context.Context, in *ExposeServiceRequest, opts ...grpc.CallOption) (DaemonService_ExposeServiceClient, error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type daemonServiceClient struct {
|
type daemonServiceClient struct {
|
||||||
@@ -426,38 +424,6 @@ func (c *daemonServiceClient) GetInstallerResult(ctx context.Context, in *Instal
|
|||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *daemonServiceClient) ExposeService(ctx context.Context, in *ExposeServiceRequest, opts ...grpc.CallOption) (DaemonService_ExposeServiceClient, error) {
|
|
||||||
stream, err := c.cc.NewStream(ctx, &DaemonService_ServiceDesc.Streams[1], "/daemon.DaemonService/ExposeService", opts...)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
x := &daemonServiceExposeServiceClient{stream}
|
|
||||||
if err := x.ClientStream.SendMsg(in); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err := x.ClientStream.CloseSend(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return x, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type DaemonService_ExposeServiceClient interface {
|
|
||||||
Recv() (*ExposeServiceEvent, error)
|
|
||||||
grpc.ClientStream
|
|
||||||
}
|
|
||||||
|
|
||||||
type daemonServiceExposeServiceClient struct {
|
|
||||||
grpc.ClientStream
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *daemonServiceExposeServiceClient) Recv() (*ExposeServiceEvent, error) {
|
|
||||||
m := new(ExposeServiceEvent)
|
|
||||||
if err := x.ClientStream.RecvMsg(m); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return m, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// DaemonServiceServer is the server API for DaemonService service.
|
// DaemonServiceServer is the server API for DaemonService service.
|
||||||
// All implementations must embed UnimplementedDaemonServiceServer
|
// All implementations must embed UnimplementedDaemonServiceServer
|
||||||
// for forward compatibility
|
// for forward compatibility
|
||||||
@@ -520,8 +486,6 @@ type DaemonServiceServer interface {
|
|||||||
StopCPUProfile(context.Context, *StopCPUProfileRequest) (*StopCPUProfileResponse, error)
|
StopCPUProfile(context.Context, *StopCPUProfileRequest) (*StopCPUProfileResponse, error)
|
||||||
NotifyOSLifecycle(context.Context, *OSLifecycleRequest) (*OSLifecycleResponse, error)
|
NotifyOSLifecycle(context.Context, *OSLifecycleRequest) (*OSLifecycleResponse, error)
|
||||||
GetInstallerResult(context.Context, *InstallerResultRequest) (*InstallerResultResponse, error)
|
GetInstallerResult(context.Context, *InstallerResultRequest) (*InstallerResultResponse, error)
|
||||||
// ExposeService exposes a local port via the NetBird reverse proxy
|
|
||||||
ExposeService(*ExposeServiceRequest, DaemonService_ExposeServiceServer) error
|
|
||||||
mustEmbedUnimplementedDaemonServiceServer()
|
mustEmbedUnimplementedDaemonServiceServer()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -634,9 +598,6 @@ func (UnimplementedDaemonServiceServer) NotifyOSLifecycle(context.Context, *OSLi
|
|||||||
func (UnimplementedDaemonServiceServer) GetInstallerResult(context.Context, *InstallerResultRequest) (*InstallerResultResponse, error) {
|
func (UnimplementedDaemonServiceServer) GetInstallerResult(context.Context, *InstallerResultRequest) (*InstallerResultResponse, error) {
|
||||||
return nil, status.Errorf(codes.Unimplemented, "method GetInstallerResult not implemented")
|
return nil, status.Errorf(codes.Unimplemented, "method GetInstallerResult not implemented")
|
||||||
}
|
}
|
||||||
func (UnimplementedDaemonServiceServer) ExposeService(*ExposeServiceRequest, DaemonService_ExposeServiceServer) error {
|
|
||||||
return status.Errorf(codes.Unimplemented, "method ExposeService not implemented")
|
|
||||||
}
|
|
||||||
func (UnimplementedDaemonServiceServer) mustEmbedUnimplementedDaemonServiceServer() {}
|
func (UnimplementedDaemonServiceServer) mustEmbedUnimplementedDaemonServiceServer() {}
|
||||||
|
|
||||||
// UnsafeDaemonServiceServer may be embedded to opt out of forward compatibility for this service.
|
// UnsafeDaemonServiceServer may be embedded to opt out of forward compatibility for this service.
|
||||||
@@ -1283,27 +1244,6 @@ func _DaemonService_GetInstallerResult_Handler(srv interface{}, ctx context.Cont
|
|||||||
return interceptor(ctx, in, info, handler)
|
return interceptor(ctx, in, info, handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
func _DaemonService_ExposeService_Handler(srv interface{}, stream grpc.ServerStream) error {
|
|
||||||
m := new(ExposeServiceRequest)
|
|
||||||
if err := stream.RecvMsg(m); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return srv.(DaemonServiceServer).ExposeService(m, &daemonServiceExposeServiceServer{stream})
|
|
||||||
}
|
|
||||||
|
|
||||||
type DaemonService_ExposeServiceServer interface {
|
|
||||||
Send(*ExposeServiceEvent) error
|
|
||||||
grpc.ServerStream
|
|
||||||
}
|
|
||||||
|
|
||||||
type daemonServiceExposeServiceServer struct {
|
|
||||||
grpc.ServerStream
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *daemonServiceExposeServiceServer) Send(m *ExposeServiceEvent) error {
|
|
||||||
return x.ServerStream.SendMsg(m)
|
|
||||||
}
|
|
||||||
|
|
||||||
// DaemonService_ServiceDesc is the grpc.ServiceDesc for DaemonService service.
|
// DaemonService_ServiceDesc is the grpc.ServiceDesc for DaemonService service.
|
||||||
// It's only intended for direct use with grpc.RegisterService,
|
// It's only intended for direct use with grpc.RegisterService,
|
||||||
// and not to be introspected or modified (even as a copy)
|
// and not to be introspected or modified (even as a copy)
|
||||||
@@ -1454,11 +1394,6 @@ var DaemonService_ServiceDesc = grpc.ServiceDesc{
|
|||||||
Handler: _DaemonService_SubscribeEvents_Handler,
|
Handler: _DaemonService_SubscribeEvents_Handler,
|
||||||
ServerStreams: true,
|
ServerStreams: true,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
StreamName: "ExposeService",
|
|
||||||
Handler: _DaemonService_ExposeService_Handler,
|
|
||||||
ServerStreams: true,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
Metadata: "daemon.proto",
|
Metadata: "daemon.proto",
|
||||||
}
|
}
|
||||||
|
|||||||
77
client/server/lifecycle.go
Normal file
77
client/server/lifecycle.go
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/netbirdio/netbird/client/internal"
|
||||||
|
"github.com/netbirdio/netbird/client/proto"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NotifyOSLifecycle handles operating system lifecycle events by executing appropriate logic based on the request type.
|
||||||
|
func (s *Server) NotifyOSLifecycle(callerCtx context.Context, req *proto.OSLifecycleRequest) (*proto.OSLifecycleResponse, error) {
|
||||||
|
switch req.GetType() {
|
||||||
|
case proto.OSLifecycleRequest_WAKEUP:
|
||||||
|
return s.handleWakeUp(callerCtx)
|
||||||
|
case proto.OSLifecycleRequest_SLEEP:
|
||||||
|
return s.handleSleep(callerCtx)
|
||||||
|
default:
|
||||||
|
log.Errorf("unknown OSLifecycleRequest type: %v", req.GetType())
|
||||||
|
}
|
||||||
|
return &proto.OSLifecycleResponse{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleWakeUp processes a wake-up event by triggering the Up command if the system was previously put to sleep.
|
||||||
|
// It resets the sleep state and logs the process. Returns a response or an error if the Up command fails.
|
||||||
|
func (s *Server) handleWakeUp(callerCtx context.Context) (*proto.OSLifecycleResponse, error) {
|
||||||
|
if !s.sleepTriggeredDown.Load() {
|
||||||
|
log.Info("skipping up because wasn't sleep down")
|
||||||
|
return &proto.OSLifecycleResponse{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// avoid other wakeup runs if sleep didn't make the computer sleep
|
||||||
|
s.sleepTriggeredDown.Store(false)
|
||||||
|
|
||||||
|
log.Info("running up after wake up")
|
||||||
|
_, err := s.Up(callerCtx, &proto.UpRequest{})
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("running up failed: %v", err)
|
||||||
|
return &proto.OSLifecycleResponse{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info("running up command executed successfully")
|
||||||
|
return &proto.OSLifecycleResponse{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleSleep handles the sleep event by initiating a "down" sequence if the system is in a connected or connecting state.
|
||||||
|
func (s *Server) handleSleep(callerCtx context.Context) (*proto.OSLifecycleResponse, error) {
|
||||||
|
s.mutex.Lock()
|
||||||
|
|
||||||
|
state := internal.CtxGetState(s.rootCtx)
|
||||||
|
status, err := state.Status()
|
||||||
|
if err != nil {
|
||||||
|
s.mutex.Unlock()
|
||||||
|
return &proto.OSLifecycleResponse{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if status != internal.StatusConnecting && status != internal.StatusConnected {
|
||||||
|
log.Infof("skipping setting the agent down because status is %s", status)
|
||||||
|
s.mutex.Unlock()
|
||||||
|
return &proto.OSLifecycleResponse{}, nil
|
||||||
|
}
|
||||||
|
s.mutex.Unlock()
|
||||||
|
|
||||||
|
log.Info("running down after system started sleeping")
|
||||||
|
|
||||||
|
_, err = s.Down(callerCtx, &proto.DownRequest{})
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("running down failed: %v", err)
|
||||||
|
return &proto.OSLifecycleResponse{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
s.sleepTriggeredDown.Store(true)
|
||||||
|
|
||||||
|
log.Info("running down executed successfully")
|
||||||
|
return &proto.OSLifecycleResponse{}, nil
|
||||||
|
}
|
||||||
219
client/server/lifecycle_test.go
Normal file
219
client/server/lifecycle_test.go
Normal file
@@ -0,0 +1,219 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/netbirdio/netbird/client/internal"
|
||||||
|
"github.com/netbirdio/netbird/client/internal/peer"
|
||||||
|
"github.com/netbirdio/netbird/client/proto"
|
||||||
|
)
|
||||||
|
|
||||||
|
func newTestServer() *Server {
|
||||||
|
ctx := internal.CtxInitState(context.Background())
|
||||||
|
return &Server{
|
||||||
|
rootCtx: ctx,
|
||||||
|
statusRecorder: peer.NewRecorder(""),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNotifyOSLifecycle_WakeUp_SkipsWhenNotSleepTriggered(t *testing.T) {
|
||||||
|
s := newTestServer()
|
||||||
|
|
||||||
|
// sleepTriggeredDown is false by default
|
||||||
|
assert.False(t, s.sleepTriggeredDown.Load())
|
||||||
|
|
||||||
|
resp, err := s.NotifyOSLifecycle(context.Background(), &proto.OSLifecycleRequest{
|
||||||
|
Type: proto.OSLifecycleRequest_WAKEUP,
|
||||||
|
})
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, resp)
|
||||||
|
assert.False(t, s.sleepTriggeredDown.Load(), "flag should remain false")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNotifyOSLifecycle_Sleep_SkipsWhenStatusIdle(t *testing.T) {
|
||||||
|
s := newTestServer()
|
||||||
|
|
||||||
|
state := internal.CtxGetState(s.rootCtx)
|
||||||
|
state.Set(internal.StatusIdle)
|
||||||
|
|
||||||
|
resp, err := s.NotifyOSLifecycle(context.Background(), &proto.OSLifecycleRequest{
|
||||||
|
Type: proto.OSLifecycleRequest_SLEEP,
|
||||||
|
})
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, resp)
|
||||||
|
assert.False(t, s.sleepTriggeredDown.Load(), "flag should remain false when status is Idle")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNotifyOSLifecycle_Sleep_SkipsWhenStatusNeedsLogin(t *testing.T) {
|
||||||
|
s := newTestServer()
|
||||||
|
|
||||||
|
state := internal.CtxGetState(s.rootCtx)
|
||||||
|
state.Set(internal.StatusNeedsLogin)
|
||||||
|
|
||||||
|
resp, err := s.NotifyOSLifecycle(context.Background(), &proto.OSLifecycleRequest{
|
||||||
|
Type: proto.OSLifecycleRequest_SLEEP,
|
||||||
|
})
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, resp)
|
||||||
|
assert.False(t, s.sleepTriggeredDown.Load(), "flag should remain false when status is NeedsLogin")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNotifyOSLifecycle_Sleep_SetsFlag_WhenConnecting(t *testing.T) {
|
||||||
|
s := newTestServer()
|
||||||
|
|
||||||
|
state := internal.CtxGetState(s.rootCtx)
|
||||||
|
state.Set(internal.StatusConnecting)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
s.actCancel = cancel
|
||||||
|
|
||||||
|
resp, err := s.NotifyOSLifecycle(ctx, &proto.OSLifecycleRequest{
|
||||||
|
Type: proto.OSLifecycleRequest_SLEEP,
|
||||||
|
})
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.NotNil(t, resp, "handleSleep returns not nil response on success")
|
||||||
|
assert.True(t, s.sleepTriggeredDown.Load(), "flag should be set after sleep when connecting")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNotifyOSLifecycle_Sleep_SetsFlag_WhenConnected(t *testing.T) {
|
||||||
|
s := newTestServer()
|
||||||
|
|
||||||
|
state := internal.CtxGetState(s.rootCtx)
|
||||||
|
state.Set(internal.StatusConnected)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
s.actCancel = cancel
|
||||||
|
|
||||||
|
resp, err := s.NotifyOSLifecycle(ctx, &proto.OSLifecycleRequest{
|
||||||
|
Type: proto.OSLifecycleRequest_SLEEP,
|
||||||
|
})
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.NotNil(t, resp, "handleSleep returns not nil response on success")
|
||||||
|
assert.True(t, s.sleepTriggeredDown.Load(), "flag should be set after sleep when connected")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNotifyOSLifecycle_WakeUp_ResetsFlag(t *testing.T) {
|
||||||
|
s := newTestServer()
|
||||||
|
|
||||||
|
// Manually set the flag to simulate prior sleep down
|
||||||
|
s.sleepTriggeredDown.Store(true)
|
||||||
|
|
||||||
|
// WakeUp will try to call Up which fails without proper setup, but flag should reset first
|
||||||
|
_, _ = s.NotifyOSLifecycle(context.Background(), &proto.OSLifecycleRequest{
|
||||||
|
Type: proto.OSLifecycleRequest_WAKEUP,
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.False(t, s.sleepTriggeredDown.Load(), "flag should be reset after WakeUp attempt")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNotifyOSLifecycle_MultipleWakeUpCalls(t *testing.T) {
|
||||||
|
s := newTestServer()
|
||||||
|
|
||||||
|
// First wakeup without prior sleep - should be no-op
|
||||||
|
resp, err := s.NotifyOSLifecycle(context.Background(), &proto.OSLifecycleRequest{
|
||||||
|
Type: proto.OSLifecycleRequest_WAKEUP,
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, resp)
|
||||||
|
assert.False(t, s.sleepTriggeredDown.Load())
|
||||||
|
|
||||||
|
// Simulate prior sleep
|
||||||
|
s.sleepTriggeredDown.Store(true)
|
||||||
|
|
||||||
|
// First wakeup after sleep - should reset flag
|
||||||
|
_, _ = s.NotifyOSLifecycle(context.Background(), &proto.OSLifecycleRequest{
|
||||||
|
Type: proto.OSLifecycleRequest_WAKEUP,
|
||||||
|
})
|
||||||
|
assert.False(t, s.sleepTriggeredDown.Load())
|
||||||
|
|
||||||
|
// Second wakeup - should be no-op
|
||||||
|
resp, err = s.NotifyOSLifecycle(context.Background(), &proto.OSLifecycleRequest{
|
||||||
|
Type: proto.OSLifecycleRequest_WAKEUP,
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, resp)
|
||||||
|
assert.False(t, s.sleepTriggeredDown.Load())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHandleWakeUp_SkipsWhenFlagFalse(t *testing.T) {
|
||||||
|
s := newTestServer()
|
||||||
|
|
||||||
|
resp, err := s.handleWakeUp(context.Background())
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, resp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHandleWakeUp_ResetsFlagBeforeUp(t *testing.T) {
|
||||||
|
s := newTestServer()
|
||||||
|
s.sleepTriggeredDown.Store(true)
|
||||||
|
|
||||||
|
// Even if Up fails, flag should be reset
|
||||||
|
_, _ = s.handleWakeUp(context.Background())
|
||||||
|
|
||||||
|
assert.False(t, s.sleepTriggeredDown.Load(), "flag must be reset before calling Up")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHandleSleep_SkipsForNonActiveStates(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
status internal.StatusType
|
||||||
|
}{
|
||||||
|
{"Idle", internal.StatusIdle},
|
||||||
|
{"NeedsLogin", internal.StatusNeedsLogin},
|
||||||
|
{"LoginFailed", internal.StatusLoginFailed},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
s := newTestServer()
|
||||||
|
state := internal.CtxGetState(s.rootCtx)
|
||||||
|
state.Set(tt.status)
|
||||||
|
|
||||||
|
resp, err := s.handleSleep(context.Background())
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, resp)
|
||||||
|
assert.False(t, s.sleepTriggeredDown.Load())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHandleSleep_ProceedsForActiveStates(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
status internal.StatusType
|
||||||
|
}{
|
||||||
|
{"Connecting", internal.StatusConnecting},
|
||||||
|
{"Connected", internal.StatusConnected},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
s := newTestServer()
|
||||||
|
state := internal.CtxGetState(s.rootCtx)
|
||||||
|
state.Set(tt.status)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
s.actCancel = cancel
|
||||||
|
|
||||||
|
resp, err := s.handleSleep(ctx)
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.NotNil(t, resp)
|
||||||
|
assert.True(t, s.sleepTriggeredDown.Load())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -21,9 +21,7 @@ import (
|
|||||||
gstatus "google.golang.org/grpc/status"
|
gstatus "google.golang.org/grpc/status"
|
||||||
|
|
||||||
"github.com/netbirdio/netbird/client/internal/auth"
|
"github.com/netbirdio/netbird/client/internal/auth"
|
||||||
"github.com/netbirdio/netbird/client/internal/expose"
|
|
||||||
"github.com/netbirdio/netbird/client/internal/profilemanager"
|
"github.com/netbirdio/netbird/client/internal/profilemanager"
|
||||||
sleephandler "github.com/netbirdio/netbird/client/internal/sleep/handler"
|
|
||||||
"github.com/netbirdio/netbird/client/system"
|
"github.com/netbirdio/netbird/client/system"
|
||||||
mgm "github.com/netbirdio/netbird/shared/management/client"
|
mgm "github.com/netbirdio/netbird/shared/management/client"
|
||||||
"github.com/netbirdio/netbird/shared/management/domain"
|
"github.com/netbirdio/netbird/shared/management/domain"
|
||||||
@@ -87,7 +85,8 @@ type Server struct {
|
|||||||
profilesDisabled bool
|
profilesDisabled bool
|
||||||
updateSettingsDisabled bool
|
updateSettingsDisabled bool
|
||||||
|
|
||||||
sleepHandler *sleephandler.SleepHandler
|
// sleepTriggeredDown holds a state indicated if the sleep handler triggered the last client down
|
||||||
|
sleepTriggeredDown atomic.Bool
|
||||||
|
|
||||||
jwtCache *jwtCache
|
jwtCache *jwtCache
|
||||||
}
|
}
|
||||||
@@ -101,7 +100,7 @@ type oauthAuthFlow struct {
|
|||||||
|
|
||||||
// New server instance constructor.
|
// New server instance constructor.
|
||||||
func New(ctx context.Context, logFile string, configFile string, profilesDisabled bool, updateSettingsDisabled bool) *Server {
|
func New(ctx context.Context, logFile string, configFile string, profilesDisabled bool, updateSettingsDisabled bool) *Server {
|
||||||
s := &Server{
|
return &Server{
|
||||||
rootCtx: ctx,
|
rootCtx: ctx,
|
||||||
logFile: logFile,
|
logFile: logFile,
|
||||||
persistSyncResponse: true,
|
persistSyncResponse: true,
|
||||||
@@ -111,10 +110,6 @@ func New(ctx context.Context, logFile string, configFile string, profilesDisable
|
|||||||
updateSettingsDisabled: updateSettingsDisabled,
|
updateSettingsDisabled: updateSettingsDisabled,
|
||||||
jwtCache: newJWTCache(),
|
jwtCache: newJWTCache(),
|
||||||
}
|
}
|
||||||
agent := &serverAgent{s}
|
|
||||||
s.sleepHandler = sleephandler.New(agent)
|
|
||||||
|
|
||||||
return s
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Start() error {
|
func (s *Server) Start() error {
|
||||||
@@ -641,6 +636,8 @@ func (s *Server) Up(callerCtx context.Context, msg *proto.UpRequest) (*proto.UpR
|
|||||||
|
|
||||||
return s.waitForUp(callerCtx)
|
return s.waitForUp(callerCtx)
|
||||||
}
|
}
|
||||||
|
defer s.mutex.Unlock()
|
||||||
|
|
||||||
if err := restoreResidualState(callerCtx, s.profileManager.GetStatePath()); err != nil {
|
if err := restoreResidualState(callerCtx, s.profileManager.GetStatePath()); err != nil {
|
||||||
log.Warnf(errRestoreResidualState, err)
|
log.Warnf(errRestoreResidualState, err)
|
||||||
}
|
}
|
||||||
@@ -652,12 +649,10 @@ func (s *Server) Up(callerCtx context.Context, msg *proto.UpRequest) (*proto.UpR
|
|||||||
// not in the progress or already successfully established connection.
|
// not in the progress or already successfully established connection.
|
||||||
status, err := state.Status()
|
status, err := state.Status()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.mutex.Unlock()
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if status != internal.StatusIdle {
|
if status != internal.StatusIdle {
|
||||||
s.mutex.Unlock()
|
|
||||||
return nil, fmt.Errorf("up already in progress: current status %s", status)
|
return nil, fmt.Errorf("up already in progress: current status %s", status)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -674,20 +669,17 @@ func (s *Server) Up(callerCtx context.Context, msg *proto.UpRequest) (*proto.UpR
|
|||||||
s.actCancel = cancel
|
s.actCancel = cancel
|
||||||
|
|
||||||
if s.config == nil {
|
if s.config == nil {
|
||||||
s.mutex.Unlock()
|
|
||||||
return nil, fmt.Errorf("config is not defined, please call login command first")
|
return nil, fmt.Errorf("config is not defined, please call login command first")
|
||||||
}
|
}
|
||||||
|
|
||||||
activeProf, err := s.profileManager.GetActiveProfileState()
|
activeProf, err := s.profileManager.GetActiveProfileState()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.mutex.Unlock()
|
|
||||||
log.Errorf("failed to get active profile state: %v", err)
|
log.Errorf("failed to get active profile state: %v", err)
|
||||||
return nil, fmt.Errorf("failed to get active profile state: %w", err)
|
return nil, fmt.Errorf("failed to get active profile state: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if msg != nil && msg.ProfileName != nil {
|
if msg != nil && msg.ProfileName != nil {
|
||||||
if err := s.switchProfileIfNeeded(*msg.ProfileName, msg.Username, activeProf); err != nil {
|
if err := s.switchProfileIfNeeded(*msg.ProfileName, msg.Username, activeProf); err != nil {
|
||||||
s.mutex.Unlock()
|
|
||||||
log.Errorf("failed to switch profile: %v", err)
|
log.Errorf("failed to switch profile: %v", err)
|
||||||
return nil, fmt.Errorf("failed to switch profile: %w", err)
|
return nil, fmt.Errorf("failed to switch profile: %w", err)
|
||||||
}
|
}
|
||||||
@@ -695,7 +687,6 @@ func (s *Server) Up(callerCtx context.Context, msg *proto.UpRequest) (*proto.UpR
|
|||||||
|
|
||||||
activeProf, err = s.profileManager.GetActiveProfileState()
|
activeProf, err = s.profileManager.GetActiveProfileState()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.mutex.Unlock()
|
|
||||||
log.Errorf("failed to get active profile state: %v", err)
|
log.Errorf("failed to get active profile state: %v", err)
|
||||||
return nil, fmt.Errorf("failed to get active profile state: %w", err)
|
return nil, fmt.Errorf("failed to get active profile state: %w", err)
|
||||||
}
|
}
|
||||||
@@ -704,7 +695,6 @@ func (s *Server) Up(callerCtx context.Context, msg *proto.UpRequest) (*proto.UpR
|
|||||||
|
|
||||||
config, _, err := s.getConfig(activeProf)
|
config, _, err := s.getConfig(activeProf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.mutex.Unlock()
|
|
||||||
log.Errorf("failed to get active profile config: %v", err)
|
log.Errorf("failed to get active profile config: %v", err)
|
||||||
return nil, fmt.Errorf("failed to get active profile config: %w", err)
|
return nil, fmt.Errorf("failed to get active profile config: %w", err)
|
||||||
}
|
}
|
||||||
@@ -723,7 +713,6 @@ func (s *Server) Up(callerCtx context.Context, msg *proto.UpRequest) (*proto.UpR
|
|||||||
}
|
}
|
||||||
go s.connectWithRetryRuns(ctx, s.config, s.statusRecorder, doAutoUpdate, s.clientRunningChan, s.clientGiveUpChan)
|
go s.connectWithRetryRuns(ctx, s.config, s.statusRecorder, doAutoUpdate, s.clientRunningChan, s.clientGiveUpChan)
|
||||||
|
|
||||||
s.mutex.Unlock()
|
|
||||||
return s.waitForUp(callerCtx)
|
return s.waitForUp(callerCtx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -849,27 +838,15 @@ func (s *Server) cleanupConnection() error {
|
|||||||
if s.actCancel == nil {
|
if s.actCancel == nil {
|
||||||
return ErrServiceNotUp
|
return ErrServiceNotUp
|
||||||
}
|
}
|
||||||
|
|
||||||
// Capture the engine reference before cancelling the context.
|
|
||||||
// After actCancel(), the connectWithRetryRuns goroutine wakes up
|
|
||||||
// and sets connectClient.engine = nil, causing connectClient.Stop()
|
|
||||||
// to skip the engine shutdown entirely.
|
|
||||||
var engine *internal.Engine
|
|
||||||
if s.connectClient != nil {
|
|
||||||
engine = s.connectClient.Engine()
|
|
||||||
}
|
|
||||||
|
|
||||||
s.actCancel()
|
s.actCancel()
|
||||||
|
|
||||||
if s.connectClient == nil {
|
if s.connectClient == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if engine != nil {
|
if err := s.connectClient.Stop(); err != nil {
|
||||||
if err := engine.Stop(); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
s.connectClient = nil
|
s.connectClient = nil
|
||||||
s.isSessionActive.Store(false)
|
s.isSessionActive.Store(false)
|
||||||
@@ -1335,60 +1312,6 @@ func (s *Server) WaitJWTToken(
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExposeService exposes a local port via the NetBird reverse proxy.
|
|
||||||
func (s *Server) ExposeService(req *proto.ExposeServiceRequest, srv proto.DaemonService_ExposeServiceServer) error {
|
|
||||||
s.mutex.Lock()
|
|
||||||
if !s.clientRunning {
|
|
||||||
s.mutex.Unlock()
|
|
||||||
return gstatus.Errorf(codes.FailedPrecondition, "client is not running, run 'netbird up' first")
|
|
||||||
}
|
|
||||||
connectClient := s.connectClient
|
|
||||||
s.mutex.Unlock()
|
|
||||||
|
|
||||||
if connectClient == nil {
|
|
||||||
return gstatus.Errorf(codes.FailedPrecondition, "client not initialized")
|
|
||||||
}
|
|
||||||
|
|
||||||
engine := connectClient.Engine()
|
|
||||||
if engine == nil {
|
|
||||||
return gstatus.Errorf(codes.FailedPrecondition, "engine not initialized")
|
|
||||||
}
|
|
||||||
|
|
||||||
mgr := engine.GetExposeManager()
|
|
||||||
if mgr == nil {
|
|
||||||
return gstatus.Errorf(codes.Internal, "expose manager not available")
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx := srv.Context()
|
|
||||||
|
|
||||||
exposeCtx, exposeCancel := context.WithTimeout(ctx, 30*time.Second)
|
|
||||||
defer exposeCancel()
|
|
||||||
|
|
||||||
mgmReq := expose.NewRequest(req)
|
|
||||||
result, err := mgr.Expose(exposeCtx, *mgmReq)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := srv.Send(&proto.ExposeServiceEvent{
|
|
||||||
Event: &proto.ExposeServiceEvent_Ready{
|
|
||||||
Ready: &proto.ExposeServiceReady{
|
|
||||||
ServiceName: result.ServiceName,
|
|
||||||
ServiceUrl: result.ServiceURL,
|
|
||||||
Domain: result.Domain,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = mgr.KeepAlive(ctx, result.Domain)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func isUnixRunningDesktop() bool {
|
func isUnixRunningDesktop() bool {
|
||||||
if runtime.GOOS != "linux" && runtime.GOOS != "freebsd" {
|
if runtime.GOOS != "linux" && runtime.GOOS != "freebsd" {
|
||||||
return false
|
return false
|
||||||
|
|||||||
@@ -1,46 +0,0 @@
|
|||||||
package server
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
|
|
||||||
"github.com/netbirdio/netbird/client/internal"
|
|
||||||
"github.com/netbirdio/netbird/client/proto"
|
|
||||||
)
|
|
||||||
|
|
||||||
// serverAgent adapts Server to the handler.Agent and handler.StatusChecker interfaces
|
|
||||||
type serverAgent struct {
|
|
||||||
s *Server
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *serverAgent) Up(ctx context.Context) error {
|
|
||||||
_, err := a.s.Up(ctx, &proto.UpRequest{})
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *serverAgent) Down(ctx context.Context) error {
|
|
||||||
_, err := a.s.Down(ctx, &proto.DownRequest{})
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *serverAgent) Status() (internal.StatusType, error) {
|
|
||||||
return internal.CtxGetState(a.s.rootCtx).Status()
|
|
||||||
}
|
|
||||||
|
|
||||||
// NotifyOSLifecycle handles operating system lifecycle events by executing appropriate logic based on the request type.
|
|
||||||
func (s *Server) NotifyOSLifecycle(callerCtx context.Context, req *proto.OSLifecycleRequest) (*proto.OSLifecycleResponse, error) {
|
|
||||||
switch req.GetType() {
|
|
||||||
case proto.OSLifecycleRequest_WAKEUP:
|
|
||||||
if err := s.sleepHandler.HandleWakeUp(callerCtx); err != nil {
|
|
||||||
return &proto.OSLifecycleResponse{}, err
|
|
||||||
}
|
|
||||||
case proto.OSLifecycleRequest_SLEEP:
|
|
||||||
if err := s.sleepHandler.HandleSleep(callerCtx); err != nil {
|
|
||||||
return &proto.OSLifecycleResponse{}, err
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
log.Errorf("unknown OSLifecycleRequest type: %v", req.GetType())
|
|
||||||
}
|
|
||||||
return &proto.OSLifecycleResponse{}, nil
|
|
||||||
}
|
|
||||||
@@ -19,7 +19,6 @@ import (
|
|||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/credentials/insecure"
|
"google.golang.org/grpc/credentials/insecure"
|
||||||
|
|
||||||
"github.com/netbirdio/netbird/client/internal/daemonaddr"
|
|
||||||
"github.com/netbirdio/netbird/client/internal/profilemanager"
|
"github.com/netbirdio/netbird/client/internal/profilemanager"
|
||||||
"github.com/netbirdio/netbird/client/proto"
|
"github.com/netbirdio/netbird/client/proto"
|
||||||
nbssh "github.com/netbirdio/netbird/client/ssh"
|
nbssh "github.com/netbirdio/netbird/client/ssh"
|
||||||
@@ -269,7 +268,7 @@ func getDefaultDaemonAddr() string {
|
|||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
return DefaultDaemonAddrWindows
|
return DefaultDaemonAddrWindows
|
||||||
}
|
}
|
||||||
return daemonaddr.ResolveUnixDaemonAddr(DefaultDaemonAddr)
|
return DefaultDaemonAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialOptions contains options for SSH connections
|
// DialOptions contains options for SSH connections
|
||||||
|
|||||||
@@ -46,10 +46,8 @@ const (
|
|||||||
cmdSFTP = "<sftp>"
|
cmdSFTP = "<sftp>"
|
||||||
cmdNonInteractive = "<idle>"
|
cmdNonInteractive = "<idle>"
|
||||||
|
|
||||||
// DefaultJWTMaxTokenAge is the default maximum age for JWT tokens accepted by the SSH server.
|
// DefaultJWTMaxTokenAge is the default maximum age for JWT tokens accepted by the SSH server
|
||||||
// Set to 10 minutes to accommodate identity providers like Azure Entra ID
|
DefaultJWTMaxTokenAge = 5 * 60
|
||||||
// that backdate the iat claim by up to 5 minutes.
|
|
||||||
DefaultJWTMaxTokenAge = 10 * 60
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
FROM ubuntu:24.04
|
|
||||||
RUN apt update && apt install -y ca-certificates && rm -fr /var/cache/apt
|
|
||||||
ENTRYPOINT [ "/go/bin/netbird-server" ]
|
|
||||||
CMD ["--config", "/etc/netbird/config.yaml"]
|
|
||||||
COPY netbird-server /go/bin/netbird-server
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
FROM golang:1.25-bookworm AS builder
|
|
||||||
WORKDIR /app
|
|
||||||
|
|
||||||
# Install build dependencies
|
|
||||||
RUN apt-get update && apt-get install -y gcc libc6-dev git && rm -rf /var/lib/apt/lists/*
|
|
||||||
|
|
||||||
COPY go.mod go.sum ./
|
|
||||||
RUN go mod download
|
|
||||||
|
|
||||||
COPY . .
|
|
||||||
|
|
||||||
# Build with version info from git (matching goreleaser ldflags)
|
|
||||||
RUN CGO_ENABLED=1 GOOS=linux go build \
|
|
||||||
-ldflags="-s -w \
|
|
||||||
-X github.com/netbirdio/netbird/version.version=$(git describe --tags --always --dirty 2>/dev/null || echo 'dev') \
|
|
||||||
-X main.commit=$(git rev-parse --short HEAD 2>/dev/null || echo 'unknown') \
|
|
||||||
-X main.date=$(date -u +%Y-%m-%dT%H:%M:%SZ) \
|
|
||||||
-X main.builtBy=docker" \
|
|
||||||
-o netbird-server ./combined
|
|
||||||
|
|
||||||
FROM ubuntu:24.04
|
|
||||||
RUN apt update && apt install -y ca-certificates && rm -fr /var/cache/apt
|
|
||||||
ENTRYPOINT [ "/go/bin/netbird-server" ]
|
|
||||||
CMD ["--config", "/etc/netbird/config.yaml"]
|
|
||||||
COPY --from=builder /app/netbird-server /go/bin/netbird-server
|
|
||||||
661
combined/LICENSE
661
combined/LICENSE
@@ -1,661 +0,0 @@
|
|||||||
GNU AFFERO GENERAL PUBLIC LICENSE
|
|
||||||
Version 3, 19 November 2007
|
|
||||||
|
|
||||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
|
||||||
Everyone is permitted to copy and distribute verbatim copies
|
|
||||||
of this license document, but changing it is not allowed.
|
|
||||||
|
|
||||||
Preamble
|
|
||||||
|
|
||||||
The GNU Affero General Public License is a free, copyleft license for
|
|
||||||
software and other kinds of works, specifically designed to ensure
|
|
||||||
cooperation with the community in the case of network server software.
|
|
||||||
|
|
||||||
The licenses for most software and other practical works are designed
|
|
||||||
to take away your freedom to share and change the works. By contrast,
|
|
||||||
our General Public Licenses are intended to guarantee your freedom to
|
|
||||||
share and change all versions of a program--to make sure it remains free
|
|
||||||
software for all its users.
|
|
||||||
|
|
||||||
When we speak of free software, we are referring to freedom, not
|
|
||||||
price. Our General Public Licenses are designed to make sure that you
|
|
||||||
have the freedom to distribute copies of free software (and charge for
|
|
||||||
them if you wish), that you receive source code or can get it if you
|
|
||||||
want it, that you can change the software or use pieces of it in new
|
|
||||||
free programs, and that you know you can do these things.
|
|
||||||
|
|
||||||
Developers that use our General Public Licenses protect your rights
|
|
||||||
with two steps: (1) assert copyright on the software, and (2) offer
|
|
||||||
you this License which gives you legal permission to copy, distribute
|
|
||||||
and/or modify the software.
|
|
||||||
|
|
||||||
A secondary benefit of defending all users' freedom is that
|
|
||||||
improvements made in alternate versions of the program, if they
|
|
||||||
receive widespread use, become available for other developers to
|
|
||||||
incorporate. Many developers of free software are heartened and
|
|
||||||
encouraged by the resulting cooperation. However, in the case of
|
|
||||||
software used on network servers, this result may fail to come about.
|
|
||||||
The GNU General Public License permits making a modified version and
|
|
||||||
letting the public access it on a server without ever releasing its
|
|
||||||
source code to the public.
|
|
||||||
|
|
||||||
The GNU Affero General Public License is designed specifically to
|
|
||||||
ensure that, in such cases, the modified source code becomes available
|
|
||||||
to the community. It requires the operator of a network server to
|
|
||||||
provide the source code of the modified version running there to the
|
|
||||||
users of that server. Therefore, public use of a modified version, on
|
|
||||||
a publicly accessible server, gives the public access to the source
|
|
||||||
code of the modified version.
|
|
||||||
|
|
||||||
An older license, called the Affero General Public License and
|
|
||||||
published by Affero, was designed to accomplish similar goals. This is
|
|
||||||
a different license, not a version of the Affero GPL, but Affero has
|
|
||||||
released a new version of the Affero GPL which permits relicensing under
|
|
||||||
this license.
|
|
||||||
|
|
||||||
The precise terms and conditions for copying, distribution and
|
|
||||||
modification follow.
|
|
||||||
|
|
||||||
TERMS AND CONDITIONS
|
|
||||||
|
|
||||||
0. Definitions.
|
|
||||||
|
|
||||||
"This License" refers to version 3 of the GNU Affero General Public License.
|
|
||||||
|
|
||||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
|
||||||
works, such as semiconductor masks.
|
|
||||||
|
|
||||||
"The Program" refers to any copyrightable work licensed under this
|
|
||||||
License. Each licensee is addressed as "you". "Licensees" and
|
|
||||||
"recipients" may be individuals or organizations.
|
|
||||||
|
|
||||||
To "modify" a work means to copy from or adapt all or part of the work
|
|
||||||
in a fashion requiring copyright permission, other than the making of an
|
|
||||||
exact copy. The resulting work is called a "modified version" of the
|
|
||||||
earlier work or a work "based on" the earlier work.
|
|
||||||
|
|
||||||
A "covered work" means either the unmodified Program or a work based
|
|
||||||
on the Program.
|
|
||||||
|
|
||||||
To "propagate" a work means to do anything with it that, without
|
|
||||||
permission, would make you directly or secondarily liable for
|
|
||||||
infringement under applicable copyright law, except executing it on a
|
|
||||||
computer or modifying a private copy. Propagation includes copying,
|
|
||||||
distribution (with or without modification), making available to the
|
|
||||||
public, and in some countries other activities as well.
|
|
||||||
|
|
||||||
To "convey" a work means any kind of propagation that enables other
|
|
||||||
parties to make or receive copies. Mere interaction with a user through
|
|
||||||
a computer network, with no transfer of a copy, is not conveying.
|
|
||||||
|
|
||||||
An interactive user interface displays "Appropriate Legal Notices"
|
|
||||||
to the extent that it includes a convenient and prominently visible
|
|
||||||
feature that (1) displays an appropriate copyright notice, and (2)
|
|
||||||
tells the user that there is no warranty for the work (except to the
|
|
||||||
extent that warranties are provided), that licensees may convey the
|
|
||||||
work under this License, and how to view a copy of this License. If
|
|
||||||
the interface presents a list of user commands or options, such as a
|
|
||||||
menu, a prominent item in the list meets this criterion.
|
|
||||||
|
|
||||||
1. Source Code.
|
|
||||||
|
|
||||||
The "source code" for a work means the preferred form of the work
|
|
||||||
for making modifications to it. "Object code" means any non-source
|
|
||||||
form of a work.
|
|
||||||
|
|
||||||
A "Standard Interface" means an interface that either is an official
|
|
||||||
standard defined by a recognized standards body, or, in the case of
|
|
||||||
interfaces specified for a particular programming language, one that
|
|
||||||
is widely used among developers working in that language.
|
|
||||||
|
|
||||||
The "System Libraries" of an executable work include anything, other
|
|
||||||
than the work as a whole, that (a) is included in the normal form of
|
|
||||||
packaging a Major Component, but which is not part of that Major
|
|
||||||
Component, and (b) serves only to enable use of the work with that
|
|
||||||
Major Component, or to implement a Standard Interface for which an
|
|
||||||
implementation is available to the public in source code form. A
|
|
||||||
"Major Component", in this context, means a major essential component
|
|
||||||
(kernel, window system, and so on) of the specific operating system
|
|
||||||
(if any) on which the executable work runs, or a compiler used to
|
|
||||||
produce the work, or an object code interpreter used to run it.
|
|
||||||
|
|
||||||
The "Corresponding Source" for a work in object code form means all
|
|
||||||
the source code needed to generate, install, and (for an executable
|
|
||||||
work) run the object code and to modify the work, including scripts to
|
|
||||||
control those activities. However, it does not include the work's
|
|
||||||
System Libraries, or general-purpose tools or generally available free
|
|
||||||
programs which are used unmodified in performing those activities but
|
|
||||||
which are not part of the work. For example, Corresponding Source
|
|
||||||
includes interface definition files associated with source files for
|
|
||||||
the work, and the source code for shared libraries and dynamically
|
|
||||||
linked subprograms that the work is specifically designed to require,
|
|
||||||
such as by intimate data communication or control flow between those
|
|
||||||
subprograms and other parts of the work.
|
|
||||||
|
|
||||||
The Corresponding Source need not include anything that users
|
|
||||||
can regenerate automatically from other parts of the Corresponding
|
|
||||||
Source.
|
|
||||||
|
|
||||||
The Corresponding Source for a work in source code form is that
|
|
||||||
same work.
|
|
||||||
|
|
||||||
2. Basic Permissions.
|
|
||||||
|
|
||||||
All rights granted under this License are granted for the term of
|
|
||||||
copyright on the Program, and are irrevocable provided the stated
|
|
||||||
conditions are met. This License explicitly affirms your unlimited
|
|
||||||
permission to run the unmodified Program. The output from running a
|
|
||||||
covered work is covered by this License only if the output, given its
|
|
||||||
content, constitutes a covered work. This License acknowledges your
|
|
||||||
rights of fair use or other equivalent, as provided by copyright law.
|
|
||||||
|
|
||||||
You may make, run and propagate covered works that you do not
|
|
||||||
convey, without conditions so long as your license otherwise remains
|
|
||||||
in force. You may convey covered works to others for the sole purpose
|
|
||||||
of having them make modifications exclusively for you, or provide you
|
|
||||||
with facilities for running those works, provided that you comply with
|
|
||||||
the terms of this License in conveying all material for which you do
|
|
||||||
not control copyright. Those thus making or running the covered works
|
|
||||||
for you must do so exclusively on your behalf, under your direction
|
|
||||||
and control, on terms that prohibit them from making any copies of
|
|
||||||
your copyrighted material outside their relationship with you.
|
|
||||||
|
|
||||||
Conveying under any other circumstances is permitted solely under
|
|
||||||
the conditions stated below. Sublicensing is not allowed; section 10
|
|
||||||
makes it unnecessary.
|
|
||||||
|
|
||||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
|
||||||
|
|
||||||
No covered work shall be deemed part of an effective technological
|
|
||||||
measure under any applicable law fulfilling obligations under article
|
|
||||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
|
||||||
similar laws prohibiting or restricting circumvention of such
|
|
||||||
measures.
|
|
||||||
|
|
||||||
When you convey a covered work, you waive any legal power to forbid
|
|
||||||
circumvention of technological measures to the extent such circumvention
|
|
||||||
is effected by exercising rights under this License with respect to
|
|
||||||
the covered work, and you disclaim any intention to limit operation or
|
|
||||||
modification of the work as a means of enforcing, against the work's
|
|
||||||
users, your or third parties' legal rights to forbid circumvention of
|
|
||||||
technological measures.
|
|
||||||
|
|
||||||
4. Conveying Verbatim Copies.
|
|
||||||
|
|
||||||
You may convey verbatim copies of the Program's source code as you
|
|
||||||
receive it, in any medium, provided that you conspicuously and
|
|
||||||
appropriately publish on each copy an appropriate copyright notice;
|
|
||||||
keep intact all notices stating that this License and any
|
|
||||||
non-permissive terms added in accord with section 7 apply to the code;
|
|
||||||
keep intact all notices of the absence of any warranty; and give all
|
|
||||||
recipients a copy of this License along with the Program.
|
|
||||||
|
|
||||||
You may charge any price or no price for each copy that you convey,
|
|
||||||
and you may offer support or warranty protection for a fee.
|
|
||||||
|
|
||||||
5. Conveying Modified Source Versions.
|
|
||||||
|
|
||||||
You may convey a work based on the Program, or the modifications to
|
|
||||||
produce it from the Program, in the form of source code under the
|
|
||||||
terms of section 4, provided that you also meet all of these conditions:
|
|
||||||
|
|
||||||
a) The work must carry prominent notices stating that you modified
|
|
||||||
it, and giving a relevant date.
|
|
||||||
|
|
||||||
b) The work must carry prominent notices stating that it is
|
|
||||||
released under this License and any conditions added under section
|
|
||||||
7. This requirement modifies the requirement in section 4 to
|
|
||||||
"keep intact all notices".
|
|
||||||
|
|
||||||
c) You must license the entire work, as a whole, under this
|
|
||||||
License to anyone who comes into possession of a copy. This
|
|
||||||
License will therefore apply, along with any applicable section 7
|
|
||||||
additional terms, to the whole of the work, and all its parts,
|
|
||||||
regardless of how they are packaged. This License gives no
|
|
||||||
permission to license the work in any other way, but it does not
|
|
||||||
invalidate such permission if you have separately received it.
|
|
||||||
|
|
||||||
d) If the work has interactive user interfaces, each must display
|
|
||||||
Appropriate Legal Notices; however, if the Program has interactive
|
|
||||||
interfaces that do not display Appropriate Legal Notices, your
|
|
||||||
work need not make them do so.
|
|
||||||
|
|
||||||
A compilation of a covered work with other separate and independent
|
|
||||||
works, which are not by their nature extensions of the covered work,
|
|
||||||
and which are not combined with it such as to form a larger program,
|
|
||||||
in or on a volume of a storage or distribution medium, is called an
|
|
||||||
"aggregate" if the compilation and its resulting copyright are not
|
|
||||||
used to limit the access or legal rights of the compilation's users
|
|
||||||
beyond what the individual works permit. Inclusion of a covered work
|
|
||||||
in an aggregate does not cause this License to apply to the other
|
|
||||||
parts of the aggregate.
|
|
||||||
|
|
||||||
6. Conveying Non-Source Forms.
|
|
||||||
|
|
||||||
You may convey a covered work in object code form under the terms
|
|
||||||
of sections 4 and 5, provided that you also convey the
|
|
||||||
machine-readable Corresponding Source under the terms of this License,
|
|
||||||
in one of these ways:
|
|
||||||
|
|
||||||
a) Convey the object code in, or embodied in, a physical product
|
|
||||||
(including a physical distribution medium), accompanied by the
|
|
||||||
Corresponding Source fixed on a durable physical medium
|
|
||||||
customarily used for software interchange.
|
|
||||||
|
|
||||||
b) Convey the object code in, or embodied in, a physical product
|
|
||||||
(including a physical distribution medium), accompanied by a
|
|
||||||
written offer, valid for at least three years and valid for as
|
|
||||||
long as you offer spare parts or customer support for that product
|
|
||||||
model, to give anyone who possesses the object code either (1) a
|
|
||||||
copy of the Corresponding Source for all the software in the
|
|
||||||
product that is covered by this License, on a durable physical
|
|
||||||
medium customarily used for software interchange, for a price no
|
|
||||||
more than your reasonable cost of physically performing this
|
|
||||||
conveying of source, or (2) access to copy the
|
|
||||||
Corresponding Source from a network server at no charge.
|
|
||||||
|
|
||||||
c) Convey individual copies of the object code with a copy of the
|
|
||||||
written offer to provide the Corresponding Source. This
|
|
||||||
alternative is allowed only occasionally and noncommercially, and
|
|
||||||
only if you received the object code with such an offer, in accord
|
|
||||||
with subsection 6b.
|
|
||||||
|
|
||||||
d) Convey the object code by offering access from a designated
|
|
||||||
place (gratis or for a charge), and offer equivalent access to the
|
|
||||||
Corresponding Source in the same way through the same place at no
|
|
||||||
further charge. You need not require recipients to copy the
|
|
||||||
Corresponding Source along with the object code. If the place to
|
|
||||||
copy the object code is a network server, the Corresponding Source
|
|
||||||
may be on a different server (operated by you or a third party)
|
|
||||||
that supports equivalent copying facilities, provided you maintain
|
|
||||||
clear directions next to the object code saying where to find the
|
|
||||||
Corresponding Source. Regardless of what server hosts the
|
|
||||||
Corresponding Source, you remain obligated to ensure that it is
|
|
||||||
available for as long as needed to satisfy these requirements.
|
|
||||||
|
|
||||||
e) Convey the object code using peer-to-peer transmission, provided
|
|
||||||
you inform other peers where the object code and Corresponding
|
|
||||||
Source of the work are being offered to the general public at no
|
|
||||||
charge under subsection 6d.
|
|
||||||
|
|
||||||
A separable portion of the object code, whose source code is excluded
|
|
||||||
from the Corresponding Source as a System Library, need not be
|
|
||||||
included in conveying the object code work.
|
|
||||||
|
|
||||||
A "User Product" is either (1) a "consumer product", which means any
|
|
||||||
tangible personal property which is normally used for personal, family,
|
|
||||||
or household purposes, or (2) anything designed or sold for incorporation
|
|
||||||
into a dwelling. In determining whether a product is a consumer product,
|
|
||||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
|
||||||
product received by a particular user, "normally used" refers to a
|
|
||||||
typical or common use of that class of product, regardless of the status
|
|
||||||
of the particular user or of the way in which the particular user
|
|
||||||
actually uses, or expects or is expected to use, the product. A product
|
|
||||||
is a consumer product regardless of whether the product has substantial
|
|
||||||
commercial, industrial or non-consumer uses, unless such uses represent
|
|
||||||
the only significant mode of use of the product.
|
|
||||||
|
|
||||||
"Installation Information" for a User Product means any methods,
|
|
||||||
procedures, authorization keys, or other information required to install
|
|
||||||
and execute modified versions of a covered work in that User Product from
|
|
||||||
a modified version of its Corresponding Source. The information must
|
|
||||||
suffice to ensure that the continued functioning of the modified object
|
|
||||||
code is in no case prevented or interfered with solely because
|
|
||||||
modification has been made.
|
|
||||||
|
|
||||||
If you convey an object code work under this section in, or with, or
|
|
||||||
specifically for use in, a User Product, and the conveying occurs as
|
|
||||||
part of a transaction in which the right of possession and use of the
|
|
||||||
User Product is transferred to the recipient in perpetuity or for a
|
|
||||||
fixed term (regardless of how the transaction is characterized), the
|
|
||||||
Corresponding Source conveyed under this section must be accompanied
|
|
||||||
by the Installation Information. But this requirement does not apply
|
|
||||||
if neither you nor any third party retains the ability to install
|
|
||||||
modified object code on the User Product (for example, the work has
|
|
||||||
been installed in ROM).
|
|
||||||
|
|
||||||
The requirement to provide Installation Information does not include a
|
|
||||||
requirement to continue to provide support service, warranty, or updates
|
|
||||||
for a work that has been modified or installed by the recipient, or for
|
|
||||||
the User Product in which it has been modified or installed. Access to a
|
|
||||||
network may be denied when the modification itself materially and
|
|
||||||
adversely affects the operation of the network or violates the rules and
|
|
||||||
protocols for communication across the network.
|
|
||||||
|
|
||||||
Corresponding Source conveyed, and Installation Information provided,
|
|
||||||
in accord with this section must be in a format that is publicly
|
|
||||||
documented (and with an implementation available to the public in
|
|
||||||
source code form), and must require no special password or key for
|
|
||||||
unpacking, reading or copying.
|
|
||||||
|
|
||||||
7. Additional Terms.
|
|
||||||
|
|
||||||
"Additional permissions" are terms that supplement the terms of this
|
|
||||||
License by making exceptions from one or more of its conditions.
|
|
||||||
Additional permissions that are applicable to the entire Program shall
|
|
||||||
be treated as though they were included in this License, to the extent
|
|
||||||
that they are valid under applicable law. If additional permissions
|
|
||||||
apply only to part of the Program, that part may be used separately
|
|
||||||
under those permissions, but the entire Program remains governed by
|
|
||||||
this License without regard to the additional permissions.
|
|
||||||
|
|
||||||
When you convey a copy of a covered work, you may at your option
|
|
||||||
remove any additional permissions from that copy, or from any part of
|
|
||||||
it. (Additional permissions may be written to require their own
|
|
||||||
removal in certain cases when you modify the work.) You may place
|
|
||||||
additional permissions on material, added by you to a covered work,
|
|
||||||
for which you have or can give appropriate copyright permission.
|
|
||||||
|
|
||||||
Notwithstanding any other provision of this License, for material you
|
|
||||||
add to a covered work, you may (if authorized by the copyright holders of
|
|
||||||
that material) supplement the terms of this License with terms:
|
|
||||||
|
|
||||||
a) Disclaiming warranty or limiting liability differently from the
|
|
||||||
terms of sections 15 and 16 of this License; or
|
|
||||||
|
|
||||||
b) Requiring preservation of specified reasonable legal notices or
|
|
||||||
author attributions in that material or in the Appropriate Legal
|
|
||||||
Notices displayed by works containing it; or
|
|
||||||
|
|
||||||
c) Prohibiting misrepresentation of the origin of that material, or
|
|
||||||
requiring that modified versions of such material be marked in
|
|
||||||
reasonable ways as different from the original version; or
|
|
||||||
|
|
||||||
d) Limiting the use for publicity purposes of names of licensors or
|
|
||||||
authors of the material; or
|
|
||||||
|
|
||||||
e) Declining to grant rights under trademark law for use of some
|
|
||||||
trade names, trademarks, or service marks; or
|
|
||||||
|
|
||||||
f) Requiring indemnification of licensors and authors of that
|
|
||||||
material by anyone who conveys the material (or modified versions of
|
|
||||||
it) with contractual assumptions of liability to the recipient, for
|
|
||||||
any liability that these contractual assumptions directly impose on
|
|
||||||
those licensors and authors.
|
|
||||||
|
|
||||||
All other non-permissive additional terms are considered "further
|
|
||||||
restrictions" within the meaning of section 10. If the Program as you
|
|
||||||
received it, or any part of it, contains a notice stating that it is
|
|
||||||
governed by this License along with a term that is a further
|
|
||||||
restriction, you may remove that term. If a license document contains
|
|
||||||
a further restriction but permits relicensing or conveying under this
|
|
||||||
License, you may add to a covered work material governed by the terms
|
|
||||||
of that license document, provided that the further restriction does
|
|
||||||
not survive such relicensing or conveying.
|
|
||||||
|
|
||||||
If you add terms to a covered work in accord with this section, you
|
|
||||||
must place, in the relevant source files, a statement of the
|
|
||||||
additional terms that apply to those files, or a notice indicating
|
|
||||||
where to find the applicable terms.
|
|
||||||
|
|
||||||
Additional terms, permissive or non-permissive, may be stated in the
|
|
||||||
form of a separately written license, or stated as exceptions;
|
|
||||||
the above requirements apply either way.
|
|
||||||
|
|
||||||
8. Termination.
|
|
||||||
|
|
||||||
You may not propagate or modify a covered work except as expressly
|
|
||||||
provided under this License. Any attempt otherwise to propagate or
|
|
||||||
modify it is void, and will automatically terminate your rights under
|
|
||||||
this License (including any patent licenses granted under the third
|
|
||||||
paragraph of section 11).
|
|
||||||
|
|
||||||
However, if you cease all violation of this License, then your
|
|
||||||
license from a particular copyright holder is reinstated (a)
|
|
||||||
provisionally, unless and until the copyright holder explicitly and
|
|
||||||
finally terminates your license, and (b) permanently, if the copyright
|
|
||||||
holder fails to notify you of the violation by some reasonable means
|
|
||||||
prior to 60 days after the cessation.
|
|
||||||
|
|
||||||
Moreover, your license from a particular copyright holder is
|
|
||||||
reinstated permanently if the copyright holder notifies you of the
|
|
||||||
violation by some reasonable means, this is the first time you have
|
|
||||||
received notice of violation of this License (for any work) from that
|
|
||||||
copyright holder, and you cure the violation prior to 30 days after
|
|
||||||
your receipt of the notice.
|
|
||||||
|
|
||||||
Termination of your rights under this section does not terminate the
|
|
||||||
licenses of parties who have received copies or rights from you under
|
|
||||||
this License. If your rights have been terminated and not permanently
|
|
||||||
reinstated, you do not qualify to receive new licenses for the same
|
|
||||||
material under section 10.
|
|
||||||
|
|
||||||
9. Acceptance Not Required for Having Copies.
|
|
||||||
|
|
||||||
You are not required to accept this License in order to receive or
|
|
||||||
run a copy of the Program. Ancillary propagation of a covered work
|
|
||||||
occurring solely as a consequence of using peer-to-peer transmission
|
|
||||||
to receive a copy likewise does not require acceptance. However,
|
|
||||||
nothing other than this License grants you permission to propagate or
|
|
||||||
modify any covered work. These actions infringe copyright if you do
|
|
||||||
not accept this License. Therefore, by modifying or propagating a
|
|
||||||
covered work, you indicate your acceptance of this License to do so.
|
|
||||||
|
|
||||||
10. Automatic Licensing of Downstream Recipients.
|
|
||||||
|
|
||||||
Each time you convey a covered work, the recipient automatically
|
|
||||||
receives a license from the original licensors, to run, modify and
|
|
||||||
propagate that work, subject to this License. You are not responsible
|
|
||||||
for enforcing compliance by third parties with this License.
|
|
||||||
|
|
||||||
An "entity transaction" is a transaction transferring control of an
|
|
||||||
organization, or substantially all assets of one, or subdividing an
|
|
||||||
organization, or merging organizations. If propagation of a covered
|
|
||||||
work results from an entity transaction, each party to that
|
|
||||||
transaction who receives a copy of the work also receives whatever
|
|
||||||
licenses to the work the party's predecessor in interest had or could
|
|
||||||
give under the previous paragraph, plus a right to possession of the
|
|
||||||
Corresponding Source of the work from the predecessor in interest, if
|
|
||||||
the predecessor has it or can get it with reasonable efforts.
|
|
||||||
|
|
||||||
You may not impose any further restrictions on the exercise of the
|
|
||||||
rights granted or affirmed under this License. For example, you may
|
|
||||||
not impose a license fee, royalty, or other charge for exercise of
|
|
||||||
rights granted under this License, and you may not initiate litigation
|
|
||||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
|
||||||
any patent claim is infringed by making, using, selling, offering for
|
|
||||||
sale, or importing the Program or any portion of it.
|
|
||||||
|
|
||||||
11. Patents.
|
|
||||||
|
|
||||||
A "contributor" is a copyright holder who authorizes use under this
|
|
||||||
License of the Program or a work on which the Program is based. The
|
|
||||||
work thus licensed is called the contributor's "contributor version".
|
|
||||||
|
|
||||||
A contributor's "essential patent claims" are all patent claims
|
|
||||||
owned or controlled by the contributor, whether already acquired or
|
|
||||||
hereafter acquired, that would be infringed by some manner, permitted
|
|
||||||
by this License, of making, using, or selling its contributor version,
|
|
||||||
but do not include claims that would be infringed only as a
|
|
||||||
consequence of further modification of the contributor version. For
|
|
||||||
purposes of this definition, "control" includes the right to grant
|
|
||||||
patent sublicenses in a manner consistent with the requirements of
|
|
||||||
this License.
|
|
||||||
|
|
||||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
|
||||||
patent license under the contributor's essential patent claims, to
|
|
||||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
|
||||||
propagate the contents of its contributor version.
|
|
||||||
|
|
||||||
In the following three paragraphs, a "patent license" is any express
|
|
||||||
agreement or commitment, however denominated, not to enforce a patent
|
|
||||||
(such as an express permission to practice a patent or covenant not to
|
|
||||||
sue for patent infringement). To "grant" such a patent license to a
|
|
||||||
party means to make such an agreement or commitment not to enforce a
|
|
||||||
patent against the party.
|
|
||||||
|
|
||||||
If you convey a covered work, knowingly relying on a patent license,
|
|
||||||
and the Corresponding Source of the work is not available for anyone
|
|
||||||
to copy, free of charge and under the terms of this License, through a
|
|
||||||
publicly available network server or other readily accessible means,
|
|
||||||
then you must either (1) cause the Corresponding Source to be so
|
|
||||||
available, or (2) arrange to deprive yourself of the benefit of the
|
|
||||||
patent license for this particular work, or (3) arrange, in a manner
|
|
||||||
consistent with the requirements of this License, to extend the patent
|
|
||||||
license to downstream recipients. "Knowingly relying" means you have
|
|
||||||
actual knowledge that, but for the patent license, your conveying the
|
|
||||||
covered work in a country, or your recipient's use of the covered work
|
|
||||||
in a country, would infringe one or more identifiable patents in that
|
|
||||||
country that you have reason to believe are valid.
|
|
||||||
|
|
||||||
If, pursuant to or in connection with a single transaction or
|
|
||||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
|
||||||
covered work, and grant a patent license to some of the parties
|
|
||||||
receiving the covered work authorizing them to use, propagate, modify
|
|
||||||
or convey a specific copy of the covered work, then the patent license
|
|
||||||
you grant is automatically extended to all recipients of the covered
|
|
||||||
work and works based on it.
|
|
||||||
|
|
||||||
A patent license is "discriminatory" if it does not include within
|
|
||||||
the scope of its coverage, prohibits the exercise of, or is
|
|
||||||
conditioned on the non-exercise of one or more of the rights that are
|
|
||||||
specifically granted under this License. You may not convey a covered
|
|
||||||
work if you are a party to an arrangement with a third party that is
|
|
||||||
in the business of distributing software, under which you make payment
|
|
||||||
to the third party based on the extent of your activity of conveying
|
|
||||||
the work, and under which the third party grants, to any of the
|
|
||||||
parties who would receive the covered work from you, a discriminatory
|
|
||||||
patent license (a) in connection with copies of the covered work
|
|
||||||
conveyed by you (or copies made from those copies), or (b) primarily
|
|
||||||
for and in connection with specific products or compilations that
|
|
||||||
contain the covered work, unless you entered into that arrangement,
|
|
||||||
or that patent license was granted, prior to 28 March 2007.
|
|
||||||
|
|
||||||
Nothing in this License shall be construed as excluding or limiting
|
|
||||||
any implied license or other defenses to infringement that may
|
|
||||||
otherwise be available to you under applicable patent law.
|
|
||||||
|
|
||||||
12. No Surrender of Others' Freedom.
|
|
||||||
|
|
||||||
If conditions are imposed on you (whether by court order, agreement or
|
|
||||||
otherwise) that contradict the conditions of this License, they do not
|
|
||||||
excuse you from the conditions of this License. If you cannot convey a
|
|
||||||
covered work so as to satisfy simultaneously your obligations under this
|
|
||||||
License and any other pertinent obligations, then as a consequence you may
|
|
||||||
not convey it at all. For example, if you agree to terms that obligate you
|
|
||||||
to collect a royalty for further conveying from those to whom you convey
|
|
||||||
the Program, the only way you could satisfy both those terms and this
|
|
||||||
License would be to refrain entirely from conveying the Program.
|
|
||||||
|
|
||||||
13. Remote Network Interaction; Use with the GNU General Public License.
|
|
||||||
|
|
||||||
Notwithstanding any other provision of this License, if you modify the
|
|
||||||
Program, your modified version must prominently offer all users
|
|
||||||
interacting with it remotely through a computer network (if your version
|
|
||||||
supports such interaction) an opportunity to receive the Corresponding
|
|
||||||
Source of your version by providing access to the Corresponding Source
|
|
||||||
from a network server at no charge, through some standard or customary
|
|
||||||
means of facilitating copying of software. This Corresponding Source
|
|
||||||
shall include the Corresponding Source for any work covered by version 3
|
|
||||||
of the GNU General Public License that is incorporated pursuant to the
|
|
||||||
following paragraph.
|
|
||||||
|
|
||||||
Notwithstanding any other provision of this License, you have
|
|
||||||
permission to link or combine any covered work with a work licensed
|
|
||||||
under version 3 of the GNU General Public License into a single
|
|
||||||
combined work, and to convey the resulting work. The terms of this
|
|
||||||
License will continue to apply to the part which is the covered work,
|
|
||||||
but the work with which it is combined will remain governed by version
|
|
||||||
3 of the GNU General Public License.
|
|
||||||
|
|
||||||
14. Revised Versions of this License.
|
|
||||||
|
|
||||||
The Free Software Foundation may publish revised and/or new versions of
|
|
||||||
the GNU Affero General Public License from time to time. Such new versions
|
|
||||||
will be similar in spirit to the present version, but may differ in detail to
|
|
||||||
address new problems or concerns.
|
|
||||||
|
|
||||||
Each version is given a distinguishing version number. If the
|
|
||||||
Program specifies that a certain numbered version of the GNU Affero General
|
|
||||||
Public License "or any later version" applies to it, you have the
|
|
||||||
option of following the terms and conditions either of that numbered
|
|
||||||
version or of any later version published by the Free Software
|
|
||||||
Foundation. If the Program does not specify a version number of the
|
|
||||||
GNU Affero General Public License, you may choose any version ever published
|
|
||||||
by the Free Software Foundation.
|
|
||||||
|
|
||||||
If the Program specifies that a proxy can decide which future
|
|
||||||
versions of the GNU Affero General Public License can be used, that proxy's
|
|
||||||
public statement of acceptance of a version permanently authorizes you
|
|
||||||
to choose that version for the Program.
|
|
||||||
|
|
||||||
Later license versions may give you additional or different
|
|
||||||
permissions. However, no additional obligations are imposed on any
|
|
||||||
author or copyright holder as a result of your choosing to follow a
|
|
||||||
later version.
|
|
||||||
|
|
||||||
15. Disclaimer of Warranty.
|
|
||||||
|
|
||||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
|
||||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
|
||||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
|
||||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
|
||||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
||||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
|
||||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
|
||||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
|
||||||
|
|
||||||
16. Limitation of Liability.
|
|
||||||
|
|
||||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
|
||||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
|
||||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
|
||||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
|
||||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
|
||||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
|
||||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
|
||||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
|
||||||
SUCH DAMAGES.
|
|
||||||
|
|
||||||
17. Interpretation of Sections 15 and 16.
|
|
||||||
|
|
||||||
If the disclaimer of warranty and limitation of liability provided
|
|
||||||
above cannot be given local legal effect according to their terms,
|
|
||||||
reviewing courts shall apply local law that most closely approximates
|
|
||||||
an absolute waiver of all civil liability in connection with the
|
|
||||||
Program, unless a warranty or assumption of liability accompanies a
|
|
||||||
copy of the Program in return for a fee.
|
|
||||||
|
|
||||||
END OF TERMS AND CONDITIONS
|
|
||||||
|
|
||||||
How to Apply These Terms to Your New Programs
|
|
||||||
|
|
||||||
If you develop a new program, and you want it to be of the greatest
|
|
||||||
possible use to the public, the best way to achieve this is to make it
|
|
||||||
free software which everyone can redistribute and change under these terms.
|
|
||||||
|
|
||||||
To do so, attach the following notices to the program. It is safest
|
|
||||||
to attach them to the start of each source file to most effectively
|
|
||||||
state the exclusion of warranty; and each file should have at least
|
|
||||||
the "copyright" line and a pointer to where the full notice is found.
|
|
||||||
|
|
||||||
<one line to give the program's name and a brief idea of what it does.>
|
|
||||||
Copyright (C) <year> <name of author>
|
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU Affero General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU Affero General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Affero General Public License
|
|
||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
Also add information on how to contact you by electronic and paper mail.
|
|
||||||
|
|
||||||
If your software can interact with users remotely through a computer
|
|
||||||
network, you should also make sure that it provides a way for users to
|
|
||||||
get its source. For example, if your program is a web application, its
|
|
||||||
interface could display a "Source" link that leads users to an archive
|
|
||||||
of the code. There are many ways you could offer source, and different
|
|
||||||
solutions will be better for different programs; see section 13 for the
|
|
||||||
specific requirements.
|
|
||||||
|
|
||||||
You should also get your employer (if you work as a programmer) or school,
|
|
||||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
|
||||||
For more information on this, and how to apply and follow the GNU AGPL, see
|
|
||||||
<https://www.gnu.org/licenses/>.
|
|
||||||
@@ -1,765 +0,0 @@
|
|||||||
package cmd
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
"net/netip"
|
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
|
|
||||||
"github.com/netbirdio/netbird/management/server/idp"
|
|
||||||
"github.com/netbirdio/netbird/management/server/types"
|
|
||||||
"github.com/netbirdio/netbird/util"
|
|
||||||
"github.com/netbirdio/netbird/util/crypt"
|
|
||||||
|
|
||||||
nbconfig "github.com/netbirdio/netbird/management/internals/server/config"
|
|
||||||
)
|
|
||||||
|
|
||||||
// CombinedConfig is the root configuration for the combined server.
|
|
||||||
// The combined server is primarily a Management server with optional embedded
|
|
||||||
// Signal, Relay, and STUN services.
|
|
||||||
//
|
|
||||||
// Architecture:
|
|
||||||
// - Management: Always runs locally (this IS the management server)
|
|
||||||
// - Signal: Runs locally by default; disabled if server.signalUri is set
|
|
||||||
// - Relay: Runs locally by default; disabled if server.relays is set
|
|
||||||
// - STUN: Runs locally on port 3478 by default; disabled if server.stuns is set
|
|
||||||
//
|
|
||||||
// All user-facing settings are under "server". The relay/signal/management
|
|
||||||
// fields are internal and populated automatically from server settings.
|
|
||||||
type CombinedConfig struct {
|
|
||||||
Server ServerConfig `yaml:"server"`
|
|
||||||
|
|
||||||
// Internal configs - populated from Server settings, not user-configurable
|
|
||||||
Relay RelayConfig `yaml:"-"`
|
|
||||||
Signal SignalConfig `yaml:"-"`
|
|
||||||
Management ManagementConfig `yaml:"-"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// ServerConfig contains server-wide settings
|
|
||||||
// In simplified mode, this contains all configuration
|
|
||||||
type ServerConfig struct {
|
|
||||||
ListenAddress string `yaml:"listenAddress"`
|
|
||||||
MetricsPort int `yaml:"metricsPort"`
|
|
||||||
HealthcheckAddress string `yaml:"healthcheckAddress"`
|
|
||||||
LogLevel string `yaml:"logLevel"`
|
|
||||||
LogFile string `yaml:"logFile"`
|
|
||||||
TLS TLSConfig `yaml:"tls"`
|
|
||||||
|
|
||||||
// Simplified config fields (used when relay/signal/management sections are omitted)
|
|
||||||
ExposedAddress string `yaml:"exposedAddress"` // Public address with protocol (e.g., "https://example.com:443")
|
|
||||||
StunPorts []int `yaml:"stunPorts"` // STUN ports (empty to disable local STUN)
|
|
||||||
AuthSecret string `yaml:"authSecret"` // Shared secret for relay authentication
|
|
||||||
DataDir string `yaml:"dataDir"` // Data directory for all services
|
|
||||||
|
|
||||||
// External service overrides (simplified mode)
|
|
||||||
// When these are set, the corresponding local service is NOT started
|
|
||||||
// and these values are used for client configuration instead
|
|
||||||
Stuns []HostConfig `yaml:"stuns"` // External STUN servers (disables local STUN)
|
|
||||||
Relays RelaysConfig `yaml:"relays"` // External relay servers (disables local relay)
|
|
||||||
SignalURI string `yaml:"signalUri"` // External signal server (disables local signal)
|
|
||||||
|
|
||||||
// Management settings (simplified mode)
|
|
||||||
DisableAnonymousMetrics bool `yaml:"disableAnonymousMetrics"`
|
|
||||||
DisableGeoliteUpdate bool `yaml:"disableGeoliteUpdate"`
|
|
||||||
Auth AuthConfig `yaml:"auth"`
|
|
||||||
Store StoreConfig `yaml:"store"`
|
|
||||||
ActivityStore StoreConfig `yaml:"activityStore"`
|
|
||||||
AuthStore StoreConfig `yaml:"authStore"`
|
|
||||||
ReverseProxy ReverseProxyConfig `yaml:"reverseProxy"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// TLSConfig contains TLS/HTTPS settings
|
|
||||||
type TLSConfig struct {
|
|
||||||
CertFile string `yaml:"certFile"`
|
|
||||||
KeyFile string `yaml:"keyFile"`
|
|
||||||
LetsEncrypt LetsEncryptConfig `yaml:"letsencrypt"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// LetsEncryptConfig contains Let's Encrypt settings
|
|
||||||
type LetsEncryptConfig struct {
|
|
||||||
Enabled bool `yaml:"enabled"`
|
|
||||||
DataDir string `yaml:"dataDir"`
|
|
||||||
Domains []string `yaml:"domains"`
|
|
||||||
Email string `yaml:"email"`
|
|
||||||
AWSRoute53 bool `yaml:"awsRoute53"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// RelayConfig contains relay service settings
|
|
||||||
type RelayConfig struct {
|
|
||||||
Enabled bool `yaml:"enabled"`
|
|
||||||
ExposedAddress string `yaml:"exposedAddress"`
|
|
||||||
AuthSecret string `yaml:"authSecret"`
|
|
||||||
LogLevel string `yaml:"logLevel"`
|
|
||||||
Stun StunConfig `yaml:"stun"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// StunConfig contains embedded STUN service settings
|
|
||||||
type StunConfig struct {
|
|
||||||
Enabled bool `yaml:"enabled"`
|
|
||||||
Ports []int `yaml:"ports"`
|
|
||||||
LogLevel string `yaml:"logLevel"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// SignalConfig contains signal service settings
|
|
||||||
type SignalConfig struct {
|
|
||||||
Enabled bool `yaml:"enabled"`
|
|
||||||
LogLevel string `yaml:"logLevel"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// ManagementConfig contains management service settings
|
|
||||||
type ManagementConfig struct {
|
|
||||||
Enabled bool `yaml:"enabled"`
|
|
||||||
LogLevel string `yaml:"logLevel"`
|
|
||||||
DataDir string `yaml:"dataDir"`
|
|
||||||
DnsDomain string `yaml:"dnsDomain"`
|
|
||||||
DisableAnonymousMetrics bool `yaml:"disableAnonymousMetrics"`
|
|
||||||
DisableGeoliteUpdate bool `yaml:"disableGeoliteUpdate"`
|
|
||||||
DisableDefaultPolicy bool `yaml:"disableDefaultPolicy"`
|
|
||||||
Auth AuthConfig `yaml:"auth"`
|
|
||||||
Stuns []HostConfig `yaml:"stuns"`
|
|
||||||
Relays RelaysConfig `yaml:"relays"`
|
|
||||||
SignalURI string `yaml:"signalUri"`
|
|
||||||
Store StoreConfig `yaml:"store"`
|
|
||||||
ReverseProxy ReverseProxyConfig `yaml:"reverseProxy"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// AuthConfig contains authentication/identity provider settings
|
|
||||||
type AuthConfig struct {
|
|
||||||
Issuer string `yaml:"issuer"`
|
|
||||||
LocalAuthDisabled bool `yaml:"localAuthDisabled"`
|
|
||||||
SignKeyRefreshEnabled bool `yaml:"signKeyRefreshEnabled"`
|
|
||||||
Storage AuthStorageConfig `yaml:"storage"`
|
|
||||||
DashboardRedirectURIs []string `yaml:"dashboardRedirectURIs"`
|
|
||||||
CLIRedirectURIs []string `yaml:"cliRedirectURIs"`
|
|
||||||
Owner *AuthOwnerConfig `yaml:"owner,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// AuthStorageConfig contains auth storage settings
|
|
||||||
type AuthStorageConfig struct {
|
|
||||||
Type string `yaml:"type"`
|
|
||||||
File string `yaml:"file"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// AuthOwnerConfig contains initial admin user settings
|
|
||||||
type AuthOwnerConfig struct {
|
|
||||||
Email string `yaml:"email"`
|
|
||||||
Password string `yaml:"password"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// HostConfig represents a STUN/TURN/Signal host
|
|
||||||
type HostConfig struct {
|
|
||||||
URI string `yaml:"uri"`
|
|
||||||
Proto string `yaml:"proto,omitempty"` // udp, dtls, tcp, http, https - defaults based on URI scheme
|
|
||||||
Username string `yaml:"username,omitempty"`
|
|
||||||
Password string `yaml:"password,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// RelaysConfig contains external relay server settings for clients
|
|
||||||
type RelaysConfig struct {
|
|
||||||
Addresses []string `yaml:"addresses"`
|
|
||||||
CredentialsTTL string `yaml:"credentialsTTL"`
|
|
||||||
Secret string `yaml:"secret"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// StoreConfig contains database settings
|
|
||||||
type StoreConfig struct {
|
|
||||||
Engine string `yaml:"engine"`
|
|
||||||
EncryptionKey string `yaml:"encryptionKey"`
|
|
||||||
DSN string `yaml:"dsn"` // Connection string for postgres or mysql engines
|
|
||||||
File string `yaml:"file"` // SQLite database file path (optional, defaults to dataDir)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReverseProxyConfig contains reverse proxy settings
|
|
||||||
type ReverseProxyConfig struct {
|
|
||||||
TrustedHTTPProxies []string `yaml:"trustedHTTPProxies"`
|
|
||||||
TrustedHTTPProxiesCount uint `yaml:"trustedHTTPProxiesCount"`
|
|
||||||
TrustedPeers []string `yaml:"trustedPeers"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// DefaultConfig returns a CombinedConfig with default values
|
|
||||||
func DefaultConfig() *CombinedConfig {
|
|
||||||
return &CombinedConfig{
|
|
||||||
Server: ServerConfig{
|
|
||||||
ListenAddress: ":443",
|
|
||||||
MetricsPort: 9090,
|
|
||||||
HealthcheckAddress: ":9000",
|
|
||||||
LogLevel: "info",
|
|
||||||
LogFile: "console",
|
|
||||||
StunPorts: []int{3478},
|
|
||||||
DataDir: "/var/lib/netbird/",
|
|
||||||
Auth: AuthConfig{
|
|
||||||
Storage: AuthStorageConfig{
|
|
||||||
Type: "sqlite3",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Store: StoreConfig{
|
|
||||||
Engine: "sqlite",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Relay: RelayConfig{
|
|
||||||
// LogLevel inherited from Server.LogLevel via ApplySimplifiedDefaults
|
|
||||||
Stun: StunConfig{
|
|
||||||
Enabled: false,
|
|
||||||
Ports: []int{3478},
|
|
||||||
// LogLevel inherited from Server.LogLevel via ApplySimplifiedDefaults
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Signal: SignalConfig{
|
|
||||||
// LogLevel inherited from Server.LogLevel via ApplySimplifiedDefaults
|
|
||||||
},
|
|
||||||
Management: ManagementConfig{
|
|
||||||
DataDir: "/var/lib/netbird/",
|
|
||||||
Auth: AuthConfig{
|
|
||||||
Storage: AuthStorageConfig{
|
|
||||||
Type: "sqlite3",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Relays: RelaysConfig{
|
|
||||||
CredentialsTTL: "12h",
|
|
||||||
},
|
|
||||||
Store: StoreConfig{
|
|
||||||
Engine: "sqlite",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// hasRequiredSettings returns true if the configuration has the required server settings
|
|
||||||
func (c *CombinedConfig) hasRequiredSettings() bool {
|
|
||||||
return c.Server.ExposedAddress != ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// parseExposedAddress extracts protocol, host, and host:port from the exposed address
|
|
||||||
// Input format: "https://example.com:443" or "http://example.com:8080" or "example.com:443"
|
|
||||||
// Returns: protocol ("https" or "http"), hostname only, and host:port
|
|
||||||
func parseExposedAddress(exposedAddress string) (protocol, hostname, hostPort string) {
|
|
||||||
// Default to https if no protocol specified
|
|
||||||
protocol = "https"
|
|
||||||
hostPort = exposedAddress
|
|
||||||
|
|
||||||
// Check for protocol prefix
|
|
||||||
if strings.HasPrefix(exposedAddress, "https://") {
|
|
||||||
protocol = "https"
|
|
||||||
hostPort = strings.TrimPrefix(exposedAddress, "https://")
|
|
||||||
} else if strings.HasPrefix(exposedAddress, "http://") {
|
|
||||||
protocol = "http"
|
|
||||||
hostPort = strings.TrimPrefix(exposedAddress, "http://")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract hostname (without port)
|
|
||||||
hostname = hostPort
|
|
||||||
if host, _, err := net.SplitHostPort(hostPort); err == nil {
|
|
||||||
hostname = host
|
|
||||||
}
|
|
||||||
|
|
||||||
return protocol, hostname, hostPort
|
|
||||||
}
|
|
||||||
|
|
||||||
// ApplySimplifiedDefaults populates internal relay/signal/management configs from server settings.
|
|
||||||
// Management is always enabled. Signal, Relay, and STUN are enabled unless external
|
|
||||||
// overrides are configured (server.signalUri, server.relays, server.stuns).
|
|
||||||
func (c *CombinedConfig) ApplySimplifiedDefaults() {
|
|
||||||
if !c.hasRequiredSettings() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse exposed address to extract protocol and hostname
|
|
||||||
exposedProto, exposedHost, exposedHostPort := parseExposedAddress(c.Server.ExposedAddress)
|
|
||||||
|
|
||||||
// Check for external service overrides
|
|
||||||
hasExternalRelay := len(c.Server.Relays.Addresses) > 0
|
|
||||||
hasExternalSignal := c.Server.SignalURI != ""
|
|
||||||
hasExternalStuns := len(c.Server.Stuns) > 0
|
|
||||||
|
|
||||||
// Default stunPorts to [3478] if not specified and no external STUN
|
|
||||||
if len(c.Server.StunPorts) == 0 && !hasExternalStuns {
|
|
||||||
c.Server.StunPorts = []int{3478}
|
|
||||||
}
|
|
||||||
|
|
||||||
c.applyRelayDefaults(exposedProto, exposedHostPort, hasExternalRelay, hasExternalStuns)
|
|
||||||
c.applySignalDefaults(hasExternalSignal)
|
|
||||||
c.applyManagementDefaults(exposedHost)
|
|
||||||
|
|
||||||
// Auto-configure client settings (stuns, relays, signalUri)
|
|
||||||
c.autoConfigureClientSettings(exposedProto, exposedHost, exposedHostPort, hasExternalStuns, hasExternalRelay, hasExternalSignal)
|
|
||||||
}
|
|
||||||
|
|
||||||
// applyRelayDefaults configures the relay service if no external relay is configured.
|
|
||||||
func (c *CombinedConfig) applyRelayDefaults(exposedProto, exposedHostPort string, hasExternalRelay, hasExternalStuns bool) {
|
|
||||||
if hasExternalRelay {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Relay.Enabled = true
|
|
||||||
relayProto := "rel"
|
|
||||||
if exposedProto == "https" {
|
|
||||||
relayProto = "rels"
|
|
||||||
}
|
|
||||||
c.Relay.ExposedAddress = fmt.Sprintf("%s://%s", relayProto, exposedHostPort)
|
|
||||||
c.Relay.AuthSecret = c.Server.AuthSecret
|
|
||||||
if c.Relay.LogLevel == "" {
|
|
||||||
c.Relay.LogLevel = c.Server.LogLevel
|
|
||||||
}
|
|
||||||
|
|
||||||
// Enable local STUN only if no external STUN servers and stunPorts are configured
|
|
||||||
if !hasExternalStuns && len(c.Server.StunPorts) > 0 {
|
|
||||||
c.Relay.Stun.Enabled = true
|
|
||||||
c.Relay.Stun.Ports = c.Server.StunPorts
|
|
||||||
if c.Relay.Stun.LogLevel == "" {
|
|
||||||
c.Relay.Stun.LogLevel = c.Server.LogLevel
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// applySignalDefaults configures the signal service if no external signal is configured.
|
|
||||||
func (c *CombinedConfig) applySignalDefaults(hasExternalSignal bool) {
|
|
||||||
if hasExternalSignal {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Signal.Enabled = true
|
|
||||||
if c.Signal.LogLevel == "" {
|
|
||||||
c.Signal.LogLevel = c.Server.LogLevel
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// applyManagementDefaults configures the management service (always enabled).
|
|
||||||
func (c *CombinedConfig) applyManagementDefaults(exposedHost string) {
|
|
||||||
c.Management.Enabled = true
|
|
||||||
if c.Management.LogLevel == "" {
|
|
||||||
c.Management.LogLevel = c.Server.LogLevel
|
|
||||||
}
|
|
||||||
if c.Management.DataDir == "" || c.Management.DataDir == "/var/lib/netbird/" {
|
|
||||||
c.Management.DataDir = c.Server.DataDir
|
|
||||||
}
|
|
||||||
c.Management.DnsDomain = exposedHost
|
|
||||||
c.Management.DisableAnonymousMetrics = c.Server.DisableAnonymousMetrics
|
|
||||||
c.Management.DisableGeoliteUpdate = c.Server.DisableGeoliteUpdate
|
|
||||||
// Copy auth config from server if management auth issuer is not set
|
|
||||||
if c.Management.Auth.Issuer == "" && c.Server.Auth.Issuer != "" {
|
|
||||||
c.Management.Auth = c.Server.Auth
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copy store config from server if not set
|
|
||||||
if c.Management.Store.Engine == "" || c.Management.Store.Engine == "sqlite" {
|
|
||||||
if c.Server.Store.Engine != "" {
|
|
||||||
c.Management.Store = c.Server.Store
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copy reverse proxy config from server
|
|
||||||
if len(c.Server.ReverseProxy.TrustedHTTPProxies) > 0 || c.Server.ReverseProxy.TrustedHTTPProxiesCount > 0 || len(c.Server.ReverseProxy.TrustedPeers) > 0 {
|
|
||||||
c.Management.ReverseProxy = c.Server.ReverseProxy
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// autoConfigureClientSettings sets up STUN/relay/signal URIs for clients
|
|
||||||
// External overrides from server config take precedence over auto-generated values
|
|
||||||
func (c *CombinedConfig) autoConfigureClientSettings(exposedProto, exposedHost, exposedHostPort string, hasExternalStuns, hasExternalRelay, hasExternalSignal bool) {
|
|
||||||
// Determine relay protocol from exposed protocol
|
|
||||||
relayProto := "rel"
|
|
||||||
if exposedProto == "https" {
|
|
||||||
relayProto = "rels"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Configure STUN servers for clients
|
|
||||||
if hasExternalStuns {
|
|
||||||
// Use external STUN servers from server config
|
|
||||||
c.Management.Stuns = c.Server.Stuns
|
|
||||||
} else if len(c.Server.StunPorts) > 0 && len(c.Management.Stuns) == 0 {
|
|
||||||
// Auto-configure local STUN servers for all ports
|
|
||||||
for _, port := range c.Server.StunPorts {
|
|
||||||
c.Management.Stuns = append(c.Management.Stuns, HostConfig{
|
|
||||||
URI: fmt.Sprintf("stun:%s:%d", exposedHost, port),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Configure relay for clients
|
|
||||||
if hasExternalRelay {
|
|
||||||
// Use external relay config from server
|
|
||||||
c.Management.Relays = c.Server.Relays
|
|
||||||
} else if len(c.Management.Relays.Addresses) == 0 {
|
|
||||||
// Auto-configure local relay
|
|
||||||
c.Management.Relays.Addresses = []string{
|
|
||||||
fmt.Sprintf("%s://%s", relayProto, exposedHostPort),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if c.Management.Relays.Secret == "" {
|
|
||||||
c.Management.Relays.Secret = c.Server.AuthSecret
|
|
||||||
}
|
|
||||||
if c.Management.Relays.CredentialsTTL == "" {
|
|
||||||
c.Management.Relays.CredentialsTTL = "12h"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Configure signal for clients
|
|
||||||
if hasExternalSignal {
|
|
||||||
// Use external signal URI from server config
|
|
||||||
c.Management.SignalURI = c.Server.SignalURI
|
|
||||||
} else if c.Management.SignalURI == "" {
|
|
||||||
// Auto-configure local signal
|
|
||||||
c.Management.SignalURI = fmt.Sprintf("%s://%s", exposedProto, exposedHostPort)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// LoadConfig loads configuration from a YAML file
|
|
||||||
func LoadConfig(configPath string) (*CombinedConfig, error) {
|
|
||||||
cfg := DefaultConfig()
|
|
||||||
|
|
||||||
if configPath == "" {
|
|
||||||
return cfg, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
data, err := os.ReadFile(configPath)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to read config file: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := yaml.Unmarshal(data, cfg); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to parse config file: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Populate internal configs from server settings
|
|
||||||
cfg.ApplySimplifiedDefaults()
|
|
||||||
|
|
||||||
return cfg, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate validates the configuration
|
|
||||||
func (c *CombinedConfig) Validate() error {
|
|
||||||
if c.Server.ExposedAddress == "" {
|
|
||||||
return fmt.Errorf("server.exposedAddress is required")
|
|
||||||
}
|
|
||||||
if c.Server.DataDir == "" {
|
|
||||||
return fmt.Errorf("server.dataDir is required")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate STUN ports
|
|
||||||
seen := make(map[int]bool)
|
|
||||||
for _, port := range c.Server.StunPorts {
|
|
||||||
if port <= 0 || port > 65535 {
|
|
||||||
return fmt.Errorf("invalid server.stunPorts value %d: must be between 1 and 65535", port)
|
|
||||||
}
|
|
||||||
if seen[port] {
|
|
||||||
return fmt.Errorf("duplicate STUN port %d in server.stunPorts", port)
|
|
||||||
}
|
|
||||||
seen[port] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
// authSecret is required only if running local relay (no external relay configured)
|
|
||||||
hasExternalRelay := len(c.Server.Relays.Addresses) > 0
|
|
||||||
if !hasExternalRelay && c.Server.AuthSecret == "" {
|
|
||||||
return fmt.Errorf("server.authSecret is required when running local relay")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// HasTLSCert returns true if TLS certificate files are configured
|
|
||||||
func (c *CombinedConfig) HasTLSCert() bool {
|
|
||||||
return c.Server.TLS.CertFile != "" && c.Server.TLS.KeyFile != ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// HasLetsEncrypt returns true if Let's Encrypt is configured
|
|
||||||
func (c *CombinedConfig) HasLetsEncrypt() bool {
|
|
||||||
return c.Server.TLS.LetsEncrypt.Enabled &&
|
|
||||||
c.Server.TLS.LetsEncrypt.DataDir != "" &&
|
|
||||||
len(c.Server.TLS.LetsEncrypt.Domains) > 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// parseExplicitProtocol parses an explicit protocol string to nbconfig.Protocol
|
|
||||||
func parseExplicitProtocol(proto string) (nbconfig.Protocol, bool) {
|
|
||||||
switch strings.ToLower(proto) {
|
|
||||||
case "udp":
|
|
||||||
return nbconfig.UDP, true
|
|
||||||
case "dtls":
|
|
||||||
return nbconfig.DTLS, true
|
|
||||||
case "tcp":
|
|
||||||
return nbconfig.TCP, true
|
|
||||||
case "http":
|
|
||||||
return nbconfig.HTTP, true
|
|
||||||
case "https":
|
|
||||||
return nbconfig.HTTPS, true
|
|
||||||
default:
|
|
||||||
return "", false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// parseStunProtocol determines protocol for STUN/TURN servers.
|
|
||||||
// stun: → UDP, stuns: → DTLS, turn: → UDP, turns: → DTLS
|
|
||||||
// Explicit proto overrides URI scheme. Defaults to UDP.
|
|
||||||
func parseStunProtocol(uri, proto string) nbconfig.Protocol {
|
|
||||||
if proto != "" {
|
|
||||||
if p, ok := parseExplicitProtocol(proto); ok {
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
uri = strings.ToLower(uri)
|
|
||||||
switch {
|
|
||||||
case strings.HasPrefix(uri, "stuns:"):
|
|
||||||
return nbconfig.DTLS
|
|
||||||
case strings.HasPrefix(uri, "turns:"):
|
|
||||||
return nbconfig.DTLS
|
|
||||||
default:
|
|
||||||
// stun:, turn:, or no scheme - default to UDP
|
|
||||||
return nbconfig.UDP
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// parseSignalProtocol determines protocol for Signal servers.
|
|
||||||
// https:// → HTTPS, http:// → HTTP. Defaults to HTTPS.
|
|
||||||
func parseSignalProtocol(uri string) nbconfig.Protocol {
|
|
||||||
uri = strings.ToLower(uri)
|
|
||||||
switch {
|
|
||||||
case strings.HasPrefix(uri, "http://"):
|
|
||||||
return nbconfig.HTTP
|
|
||||||
default:
|
|
||||||
// https:// or no scheme - default to HTTPS
|
|
||||||
return nbconfig.HTTPS
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// stripSignalProtocol removes the protocol prefix from a signal URI.
|
|
||||||
// Returns just the host:port (e.g., "selfhosted2.demo.netbird.io:443").
|
|
||||||
func stripSignalProtocol(uri string) string {
|
|
||||||
uri = strings.TrimPrefix(uri, "https://")
|
|
||||||
uri = strings.TrimPrefix(uri, "http://")
|
|
||||||
return uri
|
|
||||||
}
|
|
||||||
|
|
||||||
func buildRelayConfig(relays RelaysConfig) (*nbconfig.Relay, error) {
|
|
||||||
var ttl time.Duration
|
|
||||||
if relays.CredentialsTTL != "" {
|
|
||||||
var err error
|
|
||||||
ttl, err = time.ParseDuration(relays.CredentialsTTL)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("invalid relay credentials TTL %q: %w", relays.CredentialsTTL, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return &nbconfig.Relay{
|
|
||||||
Addresses: relays.Addresses,
|
|
||||||
CredentialsTTL: util.Duration{Duration: ttl},
|
|
||||||
Secret: relays.Secret,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// buildEmbeddedIdPConfig builds the embedded IdP configuration.
|
|
||||||
// authStore overrides auth.storage when set.
|
|
||||||
func (c *CombinedConfig) buildEmbeddedIdPConfig(mgmt ManagementConfig) (*idp.EmbeddedIdPConfig, error) {
|
|
||||||
authStorageType := mgmt.Auth.Storage.Type
|
|
||||||
authStorageDSN := c.Server.AuthStore.DSN
|
|
||||||
if c.Server.AuthStore.Engine != "" {
|
|
||||||
authStorageType = c.Server.AuthStore.Engine
|
|
||||||
}
|
|
||||||
if authStorageType == "" {
|
|
||||||
authStorageType = "sqlite3"
|
|
||||||
}
|
|
||||||
authStorageFile := ""
|
|
||||||
if authStorageType == "postgres" {
|
|
||||||
if authStorageDSN == "" {
|
|
||||||
return nil, fmt.Errorf("authStore.dsn is required when authStore.engine is postgres")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
authStorageFile = path.Join(mgmt.DataDir, "idp.db")
|
|
||||||
if c.Server.AuthStore.File != "" {
|
|
||||||
authStorageFile = c.Server.AuthStore.File
|
|
||||||
if !filepath.IsAbs(authStorageFile) {
|
|
||||||
authStorageFile = filepath.Join(mgmt.DataDir, authStorageFile)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg := &idp.EmbeddedIdPConfig{
|
|
||||||
Enabled: true,
|
|
||||||
Issuer: mgmt.Auth.Issuer,
|
|
||||||
LocalAuthDisabled: mgmt.Auth.LocalAuthDisabled,
|
|
||||||
SignKeyRefreshEnabled: mgmt.Auth.SignKeyRefreshEnabled,
|
|
||||||
Storage: idp.EmbeddedStorageConfig{
|
|
||||||
Type: authStorageType,
|
|
||||||
Config: idp.EmbeddedStorageTypeConfig{
|
|
||||||
File: authStorageFile,
|
|
||||||
DSN: authStorageDSN,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
DashboardRedirectURIs: mgmt.Auth.DashboardRedirectURIs,
|
|
||||||
CLIRedirectURIs: mgmt.Auth.CLIRedirectURIs,
|
|
||||||
}
|
|
||||||
|
|
||||||
if mgmt.Auth.Owner != nil && mgmt.Auth.Owner.Email != "" {
|
|
||||||
cfg.Owner = &idp.OwnerConfig{
|
|
||||||
Email: mgmt.Auth.Owner.Email,
|
|
||||||
Hash: mgmt.Auth.Owner.Password,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return cfg, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ToManagementConfig converts CombinedConfig to management server config
|
|
||||||
func (c *CombinedConfig) ToManagementConfig() (*nbconfig.Config, error) {
|
|
||||||
mgmt := c.Management
|
|
||||||
|
|
||||||
// Build STUN hosts
|
|
||||||
var stuns []*nbconfig.Host
|
|
||||||
for _, s := range mgmt.Stuns {
|
|
||||||
stuns = append(stuns, &nbconfig.Host{
|
|
||||||
URI: s.URI,
|
|
||||||
Proto: parseStunProtocol(s.URI, s.Proto),
|
|
||||||
Username: s.Username,
|
|
||||||
Password: s.Password,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build relay config
|
|
||||||
var relayConfig *nbconfig.Relay
|
|
||||||
if len(mgmt.Relays.Addresses) > 0 || mgmt.Relays.Secret != "" {
|
|
||||||
relay, err := buildRelayConfig(mgmt.Relays)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
relayConfig = relay
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build signal config
|
|
||||||
var signalConfig *nbconfig.Host
|
|
||||||
if mgmt.SignalURI != "" {
|
|
||||||
signalConfig = &nbconfig.Host{
|
|
||||||
URI: stripSignalProtocol(mgmt.SignalURI),
|
|
||||||
Proto: parseSignalProtocol(mgmt.SignalURI),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build store config
|
|
||||||
storeConfig := nbconfig.StoreConfig{
|
|
||||||
Engine: types.Engine(mgmt.Store.Engine),
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build reverse proxy config
|
|
||||||
reverseProxy := nbconfig.ReverseProxy{
|
|
||||||
TrustedHTTPProxiesCount: mgmt.ReverseProxy.TrustedHTTPProxiesCount,
|
|
||||||
}
|
|
||||||
for _, p := range mgmt.ReverseProxy.TrustedHTTPProxies {
|
|
||||||
if prefix, err := netip.ParsePrefix(p); err == nil {
|
|
||||||
reverseProxy.TrustedHTTPProxies = append(reverseProxy.TrustedHTTPProxies, prefix)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, p := range mgmt.ReverseProxy.TrustedPeers {
|
|
||||||
if prefix, err := netip.ParsePrefix(p); err == nil {
|
|
||||||
reverseProxy.TrustedPeers = append(reverseProxy.TrustedPeers, prefix)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build HTTP config (required, even if empty)
|
|
||||||
httpConfig := &nbconfig.HttpServerConfig{}
|
|
||||||
|
|
||||||
// Build embedded IDP config (always enabled in combined server)
|
|
||||||
embeddedIdP, err := c.buildEmbeddedIdPConfig(mgmt)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set HTTP config fields for embedded IDP
|
|
||||||
httpConfig.AuthIssuer = mgmt.Auth.Issuer
|
|
||||||
httpConfig.AuthAudience = "netbird-dashboard"
|
|
||||||
httpConfig.AuthClientID = httpConfig.AuthAudience
|
|
||||||
httpConfig.CLIAuthAudience = "netbird-cli"
|
|
||||||
httpConfig.AuthUserIDClaim = "sub"
|
|
||||||
httpConfig.AuthKeysLocation = mgmt.Auth.Issuer + "/keys"
|
|
||||||
httpConfig.OIDCConfigEndpoint = mgmt.Auth.Issuer + "/.well-known/openid-configuration"
|
|
||||||
httpConfig.IdpSignKeyRefreshEnabled = mgmt.Auth.SignKeyRefreshEnabled
|
|
||||||
callbackURL := strings.TrimSuffix(httpConfig.AuthIssuer, "/oauth2")
|
|
||||||
httpConfig.AuthCallbackURL = callbackURL + types.ProxyCallbackEndpointFull
|
|
||||||
|
|
||||||
return &nbconfig.Config{
|
|
||||||
Stuns: stuns,
|
|
||||||
Relay: relayConfig,
|
|
||||||
Signal: signalConfig,
|
|
||||||
Datadir: mgmt.DataDir,
|
|
||||||
DataStoreEncryptionKey: mgmt.Store.EncryptionKey,
|
|
||||||
HttpConfig: httpConfig,
|
|
||||||
StoreConfig: storeConfig,
|
|
||||||
ReverseProxy: reverseProxy,
|
|
||||||
DisableDefaultPolicy: mgmt.DisableDefaultPolicy,
|
|
||||||
EmbeddedIdP: embeddedIdP,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ApplyEmbeddedIdPConfig applies embedded IdP configuration to the management config.
|
|
||||||
// This mirrors the logic in management/cmd/management.go ApplyEmbeddedIdPConfig.
|
|
||||||
func ApplyEmbeddedIdPConfig(ctx context.Context, cfg *nbconfig.Config, mgmtPort int, disableSingleAccMode bool) error {
|
|
||||||
if cfg.EmbeddedIdP == nil || !cfg.EmbeddedIdP.Enabled {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Embedded IdP requires single account mode
|
|
||||||
if disableSingleAccMode {
|
|
||||||
return fmt.Errorf("embedded IdP requires single account mode; multiple account mode is not supported with embedded IdP")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set LocalAddress for embedded IdP, used for internal JWT validation
|
|
||||||
cfg.EmbeddedIdP.LocalAddress = fmt.Sprintf("localhost:%d", mgmtPort)
|
|
||||||
|
|
||||||
// Set storage defaults based on Datadir
|
|
||||||
if cfg.EmbeddedIdP.Storage.Type == "" {
|
|
||||||
cfg.EmbeddedIdP.Storage.Type = "sqlite3"
|
|
||||||
}
|
|
||||||
if cfg.EmbeddedIdP.Storage.Config.File == "" && cfg.Datadir != "" {
|
|
||||||
cfg.EmbeddedIdP.Storage.Config.File = path.Join(cfg.Datadir, "idp.db")
|
|
||||||
}
|
|
||||||
|
|
||||||
issuer := cfg.EmbeddedIdP.Issuer
|
|
||||||
|
|
||||||
// Ensure HttpConfig exists
|
|
||||||
if cfg.HttpConfig == nil {
|
|
||||||
cfg.HttpConfig = &nbconfig.HttpServerConfig{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set HttpConfig values from EmbeddedIdP
|
|
||||||
cfg.HttpConfig.AuthIssuer = issuer
|
|
||||||
cfg.HttpConfig.AuthAudience = "netbird-dashboard"
|
|
||||||
cfg.HttpConfig.CLIAuthAudience = "netbird-cli"
|
|
||||||
cfg.HttpConfig.AuthUserIDClaim = "sub"
|
|
||||||
cfg.HttpConfig.AuthKeysLocation = issuer + "/keys"
|
|
||||||
cfg.HttpConfig.OIDCConfigEndpoint = issuer + "/.well-known/openid-configuration"
|
|
||||||
cfg.HttpConfig.IdpSignKeyRefreshEnabled = true
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// EnsureEncryptionKey generates an encryption key if not set.
|
|
||||||
// Unlike management server, we don't write back to the config file.
|
|
||||||
func EnsureEncryptionKey(ctx context.Context, cfg *nbconfig.Config) error {
|
|
||||||
if cfg.DataStoreEncryptionKey != "" {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
log.WithContext(ctx).Infof("DataStoreEncryptionKey is not set, generating a new key")
|
|
||||||
key, err := crypt.GenerateKey()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to generate datastore encryption key: %v", err)
|
|
||||||
}
|
|
||||||
cfg.DataStoreEncryptionKey = key
|
|
||||||
keyPreview := key[:8] + "..."
|
|
||||||
log.WithContext(ctx).Warnf("DataStoreEncryptionKey generated (%s); add it to your config file under 'server.store.encryptionKey' to persist across restarts", keyPreview)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// LogConfigInfo logs informational messages about the loaded configuration
|
|
||||||
func LogConfigInfo(cfg *nbconfig.Config) {
|
|
||||||
if cfg.EmbeddedIdP != nil && cfg.EmbeddedIdP.Enabled {
|
|
||||||
log.Infof("running with the embedded IdP: %v", cfg.EmbeddedIdP.Issuer)
|
|
||||||
}
|
|
||||||
if cfg.Relay != nil {
|
|
||||||
log.Infof("Relay addresses: %v", cfg.Relay.Addresses)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
//go:build pprof
|
|
||||||
// +build pprof
|
|
||||||
|
|
||||||
package cmd
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
_ "net/http/pprof"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
addr := pprofAddr()
|
|
||||||
go pprof(addr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func pprofAddr() string {
|
|
||||||
listenAddr := os.Getenv("NB_PPROF_ADDR")
|
|
||||||
if listenAddr == "" {
|
|
||||||
return "localhost:6969"
|
|
||||||
}
|
|
||||||
|
|
||||||
return listenAddr
|
|
||||||
}
|
|
||||||
|
|
||||||
func pprof(listenAddr string) {
|
|
||||||
log.Infof("listening pprof on: %s\n", listenAddr)
|
|
||||||
if err := http.ListenAndServe(listenAddr, nil); err != nil {
|
|
||||||
log.Fatalf("Failed to start pprof: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user