mirror of
https://github.com/fosrl/gerbil.git
synced 2026-02-09 06:26:43 +00:00
Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
16aef10cca | ||
|
|
19031ebdfd | ||
|
|
0eebbc51d5 | ||
|
|
d321a8ba7e | ||
|
|
3ea86222ca | ||
|
|
c3ebe930d9 | ||
|
|
f2b96f2a38 | ||
|
|
9038239bbe | ||
|
|
3e64eb9c4f | ||
|
|
92992b8c14 | ||
|
|
4ee9d77532 | ||
|
|
bd7a5bd4b0 | ||
|
|
1cd49f8ee3 | ||
|
|
7a919d867b | ||
|
|
ce50c627a7 | ||
|
|
691d5f0271 | ||
|
|
56151089e3 | ||
|
|
af7c1caf98 | ||
|
|
dd208ab67c | ||
|
|
8189d41a45 | ||
|
|
ea3477c8ce | ||
|
|
b03f8911a5 |
47
.github/DISCUSSION_TEMPLATE/feature-requests.yml
vendored
Normal file
47
.github/DISCUSSION_TEMPLATE/feature-requests.yml
vendored
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
body:
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: Summary
|
||||||
|
description: A clear and concise summary of the requested feature.
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: Motivation
|
||||||
|
description: |
|
||||||
|
Why is this feature important?
|
||||||
|
Explain the problem this feature would solve or what use case it would enable.
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: Proposed Solution
|
||||||
|
description: |
|
||||||
|
How would you like to see this feature implemented?
|
||||||
|
Provide as much detail as possible about the desired behavior, configuration, or changes.
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: Alternatives Considered
|
||||||
|
description: Describe any alternative solutions or workarounds you've thought about.
|
||||||
|
validations:
|
||||||
|
required: false
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: Additional Context
|
||||||
|
description: Add any other context, mockups, or screenshots about the feature request here.
|
||||||
|
validations:
|
||||||
|
required: false
|
||||||
|
|
||||||
|
- type: markdown
|
||||||
|
attributes:
|
||||||
|
value: |
|
||||||
|
Before submitting, please:
|
||||||
|
- Check if there is an existing issue for this feature.
|
||||||
|
- Clearly explain the benefit and use case.
|
||||||
|
- Be as specific as possible to help contributors evaluate and implement.
|
||||||
51
.github/ISSUE_TEMPLATE/1.bug_report.yml
vendored
Normal file
51
.github/ISSUE_TEMPLATE/1.bug_report.yml
vendored
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
name: Bug Report
|
||||||
|
description: Create a bug report
|
||||||
|
labels: []
|
||||||
|
body:
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: Describe the Bug
|
||||||
|
description: A clear and concise description of what the bug is.
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: Environment
|
||||||
|
description: Please fill out the relevant details below for your environment.
|
||||||
|
value: |
|
||||||
|
- OS Type & Version: (e.g., Ubuntu 22.04)
|
||||||
|
- Pangolin Version:
|
||||||
|
- Gerbil Version:
|
||||||
|
- Traefik Version:
|
||||||
|
- Newt Version:
|
||||||
|
- Olm Version: (if applicable)
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: To Reproduce
|
||||||
|
description: |
|
||||||
|
Steps to reproduce the behavior, please provide a clear description of how to reproduce the issue, based on the linked minimal reproduction. Screenshots can be provided in the issue body below.
|
||||||
|
|
||||||
|
If using code blocks, make sure syntax highlighting is correct and double-check that the rendered preview is not broken.
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: Expected Behavior
|
||||||
|
description: A clear and concise description of what you expected to happen.
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: markdown
|
||||||
|
attributes:
|
||||||
|
value: |
|
||||||
|
Before posting the issue go through the steps you've written down to make sure the steps provided are detailed and clear.
|
||||||
|
|
||||||
|
- type: markdown
|
||||||
|
attributes:
|
||||||
|
value: |
|
||||||
|
Contributors should be able to follow the steps provided in order to reproduce the bug.
|
||||||
8
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
8
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
blank_issues_enabled: false
|
||||||
|
contact_links:
|
||||||
|
- name: Need help or have questions?
|
||||||
|
url: https://github.com/orgs/fosrl/discussions
|
||||||
|
about: Ask questions, get help, and discuss with other community members
|
||||||
|
- name: Request a Feature
|
||||||
|
url: https://github.com/orgs/fosrl/discussions/new?category=feature-requests
|
||||||
|
about: Feature requests should be opened as discussions so others can upvote and comment
|
||||||
4
.github/workflows/cicd.yml
vendored
4
.github/workflows/cicd.yml
vendored
@@ -12,7 +12,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v5
|
||||||
|
|
||||||
- name: Set up QEMU
|
- name: Set up QEMU
|
||||||
uses: docker/setup-qemu-action@v3
|
uses: docker/setup-qemu-action@v3
|
||||||
@@ -31,7 +31,7 @@ jobs:
|
|||||||
run: echo "TAG=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
|
run: echo "TAG=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
|
||||||
|
|
||||||
- name: Install Go
|
- name: Install Go
|
||||||
uses: actions/setup-go@v5
|
uses: actions/setup-go@v6
|
||||||
with:
|
with:
|
||||||
go-version: 1.25
|
go-version: 1.25
|
||||||
|
|
||||||
|
|||||||
4
.github/workflows/test.yml
vendored
4
.github/workflows/test.yml
vendored
@@ -11,10 +11,10 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v5
|
||||||
|
|
||||||
- name: Set up Go
|
- name: Set up Go
|
||||||
uses: actions/setup-go@v5
|
uses: actions/setup-go@v6
|
||||||
with:
|
with:
|
||||||
go-version: '1.25'
|
go-version: '1.25'
|
||||||
|
|
||||||
|
|||||||
@@ -16,18 +16,13 @@ COPY . .
|
|||||||
RUN CGO_ENABLED=0 GOOS=linux go build -o /gerbil
|
RUN CGO_ENABLED=0 GOOS=linux go build -o /gerbil
|
||||||
|
|
||||||
# Start a new stage from scratch
|
# Start a new stage from scratch
|
||||||
FROM ubuntu:24.04 AS runner
|
FROM alpine:3.22 AS runner
|
||||||
|
|
||||||
RUN apt-get update && apt-get install -y iptables iproute2 && rm -rf /var/lib/apt/lists/*
|
RUN apk add --no-cache iptables iproute2
|
||||||
|
|
||||||
# Copy the pre-built binary file from the previous stage and the entrypoint script
|
|
||||||
COPY --from=builder /gerbil /usr/local/bin/
|
COPY --from=builder /gerbil /usr/local/bin/
|
||||||
COPY entrypoint.sh /
|
COPY entrypoint.sh /
|
||||||
|
|
||||||
RUN chmod +x /entrypoint.sh
|
RUN chmod +x /entrypoint.sh
|
||||||
|
|
||||||
# Copy the entrypoint script
|
|
||||||
ENTRYPOINT ["/entrypoint.sh"]
|
ENTRYPOINT ["/entrypoint.sh"]
|
||||||
|
|
||||||
# Command to run the executable
|
|
||||||
CMD ["gerbil"]
|
CMD ["gerbil"]
|
||||||
@@ -20,7 +20,7 @@ Gerbil will create the peers defined in the config on the WireGuard interface. T
|
|||||||
|
|
||||||
### Report Bandwidth
|
### Report Bandwidth
|
||||||
|
|
||||||
Bytes transmitted in and out of each peer are collected every 10 seconds, and incremental usage is reported via the "reportBandwidthTo" endpoint. This can be used to track data usage of each peer on the remote server.
|
Bytes transmitted in and out of each peer are collected every 10 seconds, and incremental usage is reported via the api endpoint. This can be used to track data usage of each peer on the remote server.
|
||||||
|
|
||||||
### Handle client relaying
|
### Handle client relaying
|
||||||
|
|
||||||
@@ -51,7 +51,7 @@ Note: You must use either `config` or `remoteConfig` to configure WireGuard.
|
|||||||
|
|
||||||
- `reportBandwidthTo` (optional): **DEPRECATED** - Use `remoteConfig` instead. Remote HTTP endpoint to send peer bandwidth data
|
- `reportBandwidthTo` (optional): **DEPRECATED** - Use `remoteConfig` instead. Remote HTTP endpoint to send peer bandwidth data
|
||||||
- `interface` (optional): Name of the WireGuard interface created by Gerbil. Default: `wg0`
|
- `interface` (optional): Name of the WireGuard interface created by Gerbil. Default: `wg0`
|
||||||
- `listen` (optional): Port to listen on for HTTP server. Default: `:3003`
|
- `listen` (optional): Port to listen on for HTTP server. Default: `:3004`
|
||||||
- `log-level` (optional): The log level to use (DEBUG, INFO, WARN, ERROR, FATAL). Default: `INFO`
|
- `log-level` (optional): The log level to use (DEBUG, INFO, WARN, ERROR, FATAL). Default: `INFO`
|
||||||
- `mtu` (optional): MTU of the WireGuard interface. Default: `1280`
|
- `mtu` (optional): MTU of the WireGuard interface. Default: `1280`
|
||||||
- `notify` (optional): URL to notify on peer changes
|
- `notify` (optional): URL to notify on peer changes
|
||||||
@@ -84,7 +84,7 @@ Example:
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
./gerbil \
|
./gerbil \
|
||||||
--reachableAt=http://gerbil:3003 \
|
--reachableAt=http://gerbil:3004 \
|
||||||
--generateAndSaveKeyTo=/var/config/key \
|
--generateAndSaveKeyTo=/var/config/key \
|
||||||
--remoteConfig=http://pangolin:3001/api/v1/
|
--remoteConfig=http://pangolin:3001/api/v1/
|
||||||
```
|
```
|
||||||
|
|||||||
8
go.mod
8
go.mod
@@ -3,8 +3,9 @@ module github.com/fosrl/gerbil
|
|||||||
go 1.25
|
go 1.25
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||||
github.com/vishvananda/netlink v1.3.1
|
github.com/vishvananda/netlink v1.3.1
|
||||||
golang.org/x/crypto v0.36.0
|
golang.org/x/crypto v0.43.0
|
||||||
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20230429144221-925a1e7659e6
|
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20230429144221-925a1e7659e6
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -14,10 +15,9 @@ require (
|
|||||||
github.com/mdlayher/genetlink v1.3.2 // indirect
|
github.com/mdlayher/genetlink v1.3.2 // indirect
|
||||||
github.com/mdlayher/netlink v1.7.2 // indirect
|
github.com/mdlayher/netlink v1.7.2 // indirect
|
||||||
github.com/mdlayher/socket v0.4.1 // indirect
|
github.com/mdlayher/socket v0.4.1 // indirect
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
|
|
||||||
github.com/vishvananda/netns v0.0.5 // indirect
|
github.com/vishvananda/netns v0.0.5 // indirect
|
||||||
golang.org/x/net v0.38.0 // indirect
|
golang.org/x/net v0.45.0 // indirect
|
||||||
golang.org/x/sync v0.1.0 // indirect
|
golang.org/x/sync v0.1.0 // indirect
|
||||||
golang.org/x/sys v0.31.0 // indirect
|
golang.org/x/sys v0.37.0 // indirect
|
||||||
golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b // indirect
|
golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
12
go.sum
12
go.sum
@@ -16,16 +16,16 @@ github.com/vishvananda/netlink v1.3.1 h1:3AEMt62VKqz90r0tmNhog0r/PpWKmrEShJU0wJW
|
|||||||
github.com/vishvananda/netlink v1.3.1/go.mod h1:ARtKouGSTGchR8aMwmkzC0qiNPrrWO5JS/XMVl45+b4=
|
github.com/vishvananda/netlink v1.3.1/go.mod h1:ARtKouGSTGchR8aMwmkzC0qiNPrrWO5JS/XMVl45+b4=
|
||||||
github.com/vishvananda/netns v0.0.5 h1:DfiHV+j8bA32MFM7bfEunvT8IAqQ/NzSJHtcmW5zdEY=
|
github.com/vishvananda/netns v0.0.5 h1:DfiHV+j8bA32MFM7bfEunvT8IAqQ/NzSJHtcmW5zdEY=
|
||||||
github.com/vishvananda/netns v0.0.5/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
|
github.com/vishvananda/netns v0.0.5/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
|
||||||
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
|
golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04=
|
||||||
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
|
golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0=
|
||||||
golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
|
golang.org/x/net v0.45.0 h1:RLBg5JKixCy82FtLJpeNlVM0nrSqpCRYzVU1n8kj0tM=
|
||||||
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
|
golang.org/x/net v0.45.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY=
|
||||||
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
|
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
|
||||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
|
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
|
||||||
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||||
golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b h1:J1CaxgLerRR5lgx3wnr6L04cJFbWoceSK9JWBdglINo=
|
golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b h1:J1CaxgLerRR5lgx3wnr6L04cJFbWoceSK9JWBdglINo=
|
||||||
golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b/go.mod h1:tqur9LnfstdR9ep2LaJT4lFUl0EjlHtge+gAjmsHUG4=
|
golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b/go.mod h1:tqur9LnfstdR9ep2LaJT4lFUl0EjlHtge+gAjmsHUG4=
|
||||||
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20230429144221-925a1e7659e6 h1:CawjfCvYQH2OU3/TnxLx97WDSUDRABfT18pCOYwc2GE=
|
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20230429144221-925a1e7659e6 h1:CawjfCvYQH2OU3/TnxLx97WDSUDRABfT18pCOYwc2GE=
|
||||||
|
|||||||
36
main.go
36
main.go
@@ -121,6 +121,7 @@ func main() {
|
|||||||
localProxyAddr string
|
localProxyAddr string
|
||||||
localProxyPort int
|
localProxyPort int
|
||||||
localOverridesStr string
|
localOverridesStr string
|
||||||
|
trustedUpstreamsStr string
|
||||||
proxyProtocol bool
|
proxyProtocol bool
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -138,6 +139,7 @@ func main() {
|
|||||||
localProxyAddr = os.Getenv("LOCAL_PROXY")
|
localProxyAddr = os.Getenv("LOCAL_PROXY")
|
||||||
localProxyPortStr := os.Getenv("LOCAL_PROXY_PORT")
|
localProxyPortStr := os.Getenv("LOCAL_PROXY_PORT")
|
||||||
localOverridesStr = os.Getenv("LOCAL_OVERRIDES")
|
localOverridesStr = os.Getenv("LOCAL_OVERRIDES")
|
||||||
|
trustedUpstreamsStr = os.Getenv("TRUSTED_UPSTREAMS")
|
||||||
proxyProtocolStr := os.Getenv("PROXY_PROTOCOL")
|
proxyProtocolStr := os.Getenv("PROXY_PROTOCOL")
|
||||||
|
|
||||||
if interfaceName == "" {
|
if interfaceName == "" {
|
||||||
@@ -150,7 +152,7 @@ func main() {
|
|||||||
flag.StringVar(&remoteConfigURL, "remoteConfig", "", "URL of the Pangolin server")
|
flag.StringVar(&remoteConfigURL, "remoteConfig", "", "URL of the Pangolin server")
|
||||||
}
|
}
|
||||||
if listenAddr == "" {
|
if listenAddr == "" {
|
||||||
flag.StringVar(&listenAddr, "listen", ":3003", "Address to listen on")
|
flag.StringVar(&listenAddr, "listen", "", "DEPRECATED (overridden by reachableAt): Address to listen on")
|
||||||
}
|
}
|
||||||
// DEPRECATED AND UNSED: reportBandwidthTo
|
// DEPRECATED AND UNSED: reportBandwidthTo
|
||||||
// allow reportBandwidthTo to be passed but dont do anything with it just thow it away
|
// allow reportBandwidthTo to be passed but dont do anything with it just thow it away
|
||||||
@@ -160,9 +162,11 @@ func main() {
|
|||||||
if generateAndSaveKeyTo == "" {
|
if generateAndSaveKeyTo == "" {
|
||||||
flag.StringVar(&generateAndSaveKeyTo, "generateAndSaveKeyTo", "", "Path to save generated private key")
|
flag.StringVar(&generateAndSaveKeyTo, "generateAndSaveKeyTo", "", "Path to save generated private key")
|
||||||
}
|
}
|
||||||
|
|
||||||
if reachableAt == "" {
|
if reachableAt == "" {
|
||||||
flag.StringVar(&reachableAt, "reachableAt", "", "Endpoint of the http server to tell remote config about")
|
flag.StringVar(&reachableAt, "reachableAt", "", "Endpoint of the http server to tell remote config about")
|
||||||
}
|
}
|
||||||
|
|
||||||
if logLevel == "" {
|
if logLevel == "" {
|
||||||
flag.StringVar(&logLevel, "log-level", "INFO", "Log level (DEBUG, INFO, WARN, ERROR, FATAL)")
|
flag.StringVar(&logLevel, "log-level", "INFO", "Log level (DEBUG, INFO, WARN, ERROR, FATAL)")
|
||||||
}
|
}
|
||||||
@@ -197,6 +201,9 @@ func main() {
|
|||||||
if localOverridesStr != "" {
|
if localOverridesStr != "" {
|
||||||
flag.StringVar(&localOverridesStr, "local-overrides", "", "Comma-separated list of local overrides for SNI proxy")
|
flag.StringVar(&localOverridesStr, "local-overrides", "", "Comma-separated list of local overrides for SNI proxy")
|
||||||
}
|
}
|
||||||
|
if trustedUpstreamsStr == "" {
|
||||||
|
flag.StringVar(&trustedUpstreamsStr, "trusted-upstreams", "", "Comma-separated list of trusted upstream proxy domain names/IPs that can send PROXY protocol")
|
||||||
|
}
|
||||||
|
|
||||||
if proxyProtocolStr != "" {
|
if proxyProtocolStr != "" {
|
||||||
proxyProtocol = strings.ToLower(proxyProtocolStr) == "true"
|
proxyProtocol = strings.ToLower(proxyProtocolStr) == "true"
|
||||||
@@ -210,6 +217,22 @@ func main() {
|
|||||||
logger.Init()
|
logger.Init()
|
||||||
logger.GetLogger().SetLevel(parseLogLevel(logLevel))
|
logger.GetLogger().SetLevel(parseLogLevel(logLevel))
|
||||||
|
|
||||||
|
// try to parse as http://host:port and set the listenAddr to the :port from this reachableAt.
|
||||||
|
if reachableAt != "" && listenAddr == "" {
|
||||||
|
if strings.HasPrefix(reachableAt, "http://") || strings.HasPrefix(reachableAt, "https://") {
|
||||||
|
parts := strings.Split(reachableAt, ":")
|
||||||
|
if len(parts) == 3 {
|
||||||
|
port := parts[2]
|
||||||
|
if strings.Contains(port, "/") {
|
||||||
|
port = strings.Split(port, "/")[0]
|
||||||
|
}
|
||||||
|
listenAddr = ":" + port
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if listenAddr == "" {
|
||||||
|
listenAddr = ":3003"
|
||||||
|
}
|
||||||
|
|
||||||
mtuInt, err = strconv.Atoi(mtu)
|
mtuInt, err = strconv.Atoi(mtu)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal("Failed to parse MTU: %v", err)
|
logger.Fatal("Failed to parse MTU: %v", err)
|
||||||
@@ -323,7 +346,16 @@ func main() {
|
|||||||
logger.Info("Local overrides configured: %v", localOverrides)
|
logger.Info("Local overrides configured: %v", localOverrides)
|
||||||
}
|
}
|
||||||
|
|
||||||
proxySNI, err = proxy.NewSNIProxy(sniProxyPort, remoteConfigURL, key.PublicKey().String(), localProxyAddr, localProxyPort, localOverrides, proxyProtocol)
|
var trustedUpstreams []string
|
||||||
|
if trustedUpstreamsStr != "" {
|
||||||
|
trustedUpstreams = strings.Split(trustedUpstreamsStr, ",")
|
||||||
|
for i, upstream := range trustedUpstreams {
|
||||||
|
trustedUpstreams[i] = strings.TrimSpace(upstream)
|
||||||
|
}
|
||||||
|
logger.Info("Trusted upstreams configured: %v", trustedUpstreams)
|
||||||
|
}
|
||||||
|
|
||||||
|
proxySNI, err = proxy.NewSNIProxy(sniProxyPort, remoteConfigURL, key.PublicKey().String(), localProxyAddr, localProxyPort, localOverrides, proxyProtocol, trustedUpstreams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal("Failed to create proxy: %v", err)
|
logger.Fatal("Failed to create proxy: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
298
proxy/proxy.go
298
proxy/proxy.go
@@ -11,6 +11,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -31,6 +32,16 @@ type RouteAPIResponse struct {
|
|||||||
Endpoints []string `json:"endpoints"`
|
Endpoints []string `json:"endpoints"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ProxyProtocolInfo holds information parsed from incoming PROXY protocol header
|
||||||
|
type ProxyProtocolInfo struct {
|
||||||
|
Protocol string // TCP4 or TCP6
|
||||||
|
SrcIP string
|
||||||
|
DestIP string
|
||||||
|
SrcPort int
|
||||||
|
DestPort int
|
||||||
|
OriginalConn net.Conn // The original connection after PROXY protocol parsing
|
||||||
|
}
|
||||||
|
|
||||||
// SNIProxy represents the main proxy server
|
// SNIProxy represents the main proxy server
|
||||||
type SNIProxy struct {
|
type SNIProxy struct {
|
||||||
port int
|
port int
|
||||||
@@ -55,6 +66,9 @@ type SNIProxy struct {
|
|||||||
// Track active tunnels by SNI
|
// Track active tunnels by SNI
|
||||||
activeTunnels map[string]*activeTunnel
|
activeTunnels map[string]*activeTunnel
|
||||||
activeTunnelsLock sync.Mutex
|
activeTunnelsLock sync.Mutex
|
||||||
|
|
||||||
|
// Trusted upstream proxies that can send PROXY protocol
|
||||||
|
trustedUpstreams map[string]struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
type activeTunnel struct {
|
type activeTunnel struct {
|
||||||
@@ -75,6 +89,194 @@ func (conn readOnlyConn) SetDeadline(t time.Time) error { return nil }
|
|||||||
func (conn readOnlyConn) SetReadDeadline(t time.Time) error { return nil }
|
func (conn readOnlyConn) SetReadDeadline(t time.Time) error { return nil }
|
||||||
func (conn readOnlyConn) SetWriteDeadline(t time.Time) error { return nil }
|
func (conn readOnlyConn) SetWriteDeadline(t time.Time) error { return nil }
|
||||||
|
|
||||||
|
// parseProxyProtocolHeader parses a PROXY protocol v1 header from the connection
|
||||||
|
func (p *SNIProxy) parseProxyProtocolHeader(conn net.Conn) (*ProxyProtocolInfo, net.Conn, error) {
|
||||||
|
// Check if the connection comes from a trusted upstream
|
||||||
|
remoteHost, _, err := net.SplitHostPort(conn.RemoteAddr().String())
|
||||||
|
if err != nil {
|
||||||
|
return nil, conn, fmt.Errorf("failed to parse remote address: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resolve the remote IP to hostname to check if it's trusted
|
||||||
|
// For simplicity, we'll check the IP directly in trusted upstreams
|
||||||
|
// In production, you might want to do reverse DNS lookup
|
||||||
|
if _, isTrusted := p.trustedUpstreams[remoteHost]; !isTrusted {
|
||||||
|
// Not from trusted upstream, return original connection
|
||||||
|
return nil, conn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set read timeout for PROXY protocol parsing
|
||||||
|
if err := conn.SetReadDeadline(time.Now().Add(5 * time.Second)); err != nil {
|
||||||
|
return nil, conn, fmt.Errorf("failed to set read deadline: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the first line (PROXY protocol header)
|
||||||
|
buffer := make([]byte, 512) // PROXY protocol header should be much smaller
|
||||||
|
n, err := conn.Read(buffer)
|
||||||
|
if err != nil {
|
||||||
|
// If we can't read from trusted upstream, treat as regular connection
|
||||||
|
logger.Debug("Could not read from trusted upstream %s, treating as regular connection: %v", remoteHost, err)
|
||||||
|
// Clear read timeout before returning
|
||||||
|
if clearErr := conn.SetReadDeadline(time.Time{}); clearErr != nil {
|
||||||
|
logger.Debug("Failed to clear read deadline: %v", clearErr)
|
||||||
|
}
|
||||||
|
return nil, conn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find the end of the first line (CRLF)
|
||||||
|
headerEnd := bytes.Index(buffer[:n], []byte("\r\n"))
|
||||||
|
if headerEnd == -1 {
|
||||||
|
// No PROXY protocol header found, treat as regular TLS connection
|
||||||
|
// Return the connection with the buffered data prepended
|
||||||
|
logger.Debug("No PROXY protocol header from trusted upstream %s, treating as regular TLS connection", remoteHost)
|
||||||
|
|
||||||
|
// Clear read timeout
|
||||||
|
if err := conn.SetReadDeadline(time.Time{}); err != nil {
|
||||||
|
logger.Debug("Failed to clear read deadline: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a reader that includes the buffered data + original connection
|
||||||
|
newReader := io.MultiReader(bytes.NewReader(buffer[:n]), conn)
|
||||||
|
wrappedConn := &proxyProtocolConn{
|
||||||
|
Conn: conn,
|
||||||
|
reader: newReader,
|
||||||
|
}
|
||||||
|
return nil, wrappedConn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
headerLine := string(buffer[:headerEnd])
|
||||||
|
remainingData := buffer[headerEnd+2 : n]
|
||||||
|
|
||||||
|
// Parse PROXY protocol line: "PROXY TCP4/TCP6 srcIP destIP srcPort destPort"
|
||||||
|
parts := strings.Fields(headerLine)
|
||||||
|
if len(parts) != 6 || parts[0] != "PROXY" {
|
||||||
|
// Check for PROXY UNKNOWN
|
||||||
|
if len(parts) == 2 && parts[0] == "PROXY" && parts[1] == "UNKNOWN" {
|
||||||
|
// PROXY UNKNOWN - use original connection info
|
||||||
|
return nil, conn, nil
|
||||||
|
}
|
||||||
|
// Invalid PROXY protocol, but might be regular TLS - treat as such
|
||||||
|
logger.Debug("Invalid PROXY protocol from trusted upstream %s, treating as regular TLS connection: %s", remoteHost, headerLine)
|
||||||
|
|
||||||
|
// Clear read timeout
|
||||||
|
if err := conn.SetReadDeadline(time.Time{}); err != nil {
|
||||||
|
logger.Debug("Failed to clear read deadline: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the connection with all buffered data prepended
|
||||||
|
newReader := io.MultiReader(bytes.NewReader(buffer[:n]), conn)
|
||||||
|
wrappedConn := &proxyProtocolConn{
|
||||||
|
Conn: conn,
|
||||||
|
reader: newReader,
|
||||||
|
}
|
||||||
|
return nil, wrappedConn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
protocol := parts[1]
|
||||||
|
srcIP := parts[2]
|
||||||
|
destIP := parts[3]
|
||||||
|
srcPort, err := strconv.Atoi(parts[4])
|
||||||
|
if err != nil {
|
||||||
|
return nil, conn, fmt.Errorf("invalid source port in PROXY header: %s", parts[4])
|
||||||
|
}
|
||||||
|
destPort, err := strconv.Atoi(parts[5])
|
||||||
|
if err != nil {
|
||||||
|
return nil, conn, fmt.Errorf("invalid destination port in PROXY header: %s", parts[5])
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new reader that includes remaining data + original connection
|
||||||
|
var newReader io.Reader
|
||||||
|
if len(remainingData) > 0 {
|
||||||
|
newReader = io.MultiReader(bytes.NewReader(remainingData), conn)
|
||||||
|
} else {
|
||||||
|
newReader = conn
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a wrapper connection that reads from the combined reader
|
||||||
|
wrappedConn := &proxyProtocolConn{
|
||||||
|
Conn: conn,
|
||||||
|
reader: newReader,
|
||||||
|
}
|
||||||
|
|
||||||
|
proxyInfo := &ProxyProtocolInfo{
|
||||||
|
Protocol: protocol,
|
||||||
|
SrcIP: srcIP,
|
||||||
|
DestIP: destIP,
|
||||||
|
SrcPort: srcPort,
|
||||||
|
DestPort: destPort,
|
||||||
|
OriginalConn: wrappedConn,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear read timeout
|
||||||
|
if err := conn.SetReadDeadline(time.Time{}); err != nil {
|
||||||
|
return nil, conn, fmt.Errorf("failed to clear read deadline: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return proxyInfo, wrappedConn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// proxyProtocolConn wraps a connection to read from a custom reader
|
||||||
|
type proxyProtocolConn struct {
|
||||||
|
net.Conn
|
||||||
|
reader io.Reader
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *proxyProtocolConn) Read(b []byte) (int, error) {
|
||||||
|
return c.reader.Read(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildProxyProtocolHeaderFromInfo creates a PROXY protocol v1 header using ProxyProtocolInfo
|
||||||
|
func (p *SNIProxy) buildProxyProtocolHeaderFromInfo(proxyInfo *ProxyProtocolInfo, targetAddr net.Addr) string {
|
||||||
|
targetTCP, ok := targetAddr.(*net.TCPAddr)
|
||||||
|
if !ok {
|
||||||
|
// Fallback for unknown address types
|
||||||
|
return "PROXY UNKNOWN\r\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use the original client information from the PROXY protocol
|
||||||
|
var targetIP string
|
||||||
|
var protocol string
|
||||||
|
|
||||||
|
// Parse source IP to determine protocol family
|
||||||
|
srcIP := net.ParseIP(proxyInfo.SrcIP)
|
||||||
|
if srcIP == nil {
|
||||||
|
return "PROXY UNKNOWN\r\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
if srcIP.To4() != nil {
|
||||||
|
// Source is IPv4, use TCP4 protocol
|
||||||
|
protocol = "TCP4"
|
||||||
|
if targetTCP.IP.To4() != nil {
|
||||||
|
// Target is also IPv4, use as-is
|
||||||
|
targetIP = targetTCP.IP.String()
|
||||||
|
} else {
|
||||||
|
// Target is IPv6, but we need IPv4 for consistent protocol family
|
||||||
|
if targetTCP.IP.IsLoopback() {
|
||||||
|
targetIP = "127.0.0.1"
|
||||||
|
} else {
|
||||||
|
targetIP = "127.0.0.1" // Safe fallback
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Source is IPv6, use TCP6 protocol
|
||||||
|
protocol = "TCP6"
|
||||||
|
if targetTCP.IP.To4() != nil {
|
||||||
|
// Target is IPv4, convert to IPv6 representation
|
||||||
|
targetIP = "::ffff:" + targetTCP.IP.String()
|
||||||
|
} else {
|
||||||
|
// Target is also IPv6, use as-is
|
||||||
|
targetIP = targetTCP.IP.String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("PROXY %s %s %s %d %d\r\n",
|
||||||
|
protocol,
|
||||||
|
proxyInfo.SrcIP,
|
||||||
|
targetIP,
|
||||||
|
proxyInfo.SrcPort,
|
||||||
|
targetTCP.Port)
|
||||||
|
}
|
||||||
|
|
||||||
// buildProxyProtocolHeader creates a PROXY protocol v1 header
|
// buildProxyProtocolHeader creates a PROXY protocol v1 header
|
||||||
func buildProxyProtocolHeader(clientAddr, targetAddr net.Addr) string {
|
func buildProxyProtocolHeader(clientAddr, targetAddr net.Addr) string {
|
||||||
clientTCP, ok := clientAddr.(*net.TCPAddr)
|
clientTCP, ok := clientAddr.(*net.TCPAddr)
|
||||||
@@ -131,7 +333,7 @@ func buildProxyProtocolHeader(clientAddr, targetAddr net.Addr) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewSNIProxy creates a new SNI proxy instance
|
// NewSNIProxy creates a new SNI proxy instance
|
||||||
func NewSNIProxy(port int, remoteConfigURL, publicKey, localProxyAddr string, localProxyPort int, localOverrides []string, proxyProtocol bool) (*SNIProxy, error) {
|
func NewSNIProxy(port int, remoteConfigURL, publicKey, localProxyAddr string, localProxyPort int, localOverrides []string, proxyProtocol bool, trustedUpstreams []string) (*SNIProxy, error) {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
// Create local overrides map
|
// Create local overrides map
|
||||||
@@ -142,19 +344,36 @@ func NewSNIProxy(port int, remoteConfigURL, publicKey, localProxyAddr string, lo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create trusted upstreams map
|
||||||
|
trustedMap := make(map[string]struct{})
|
||||||
|
for _, upstream := range trustedUpstreams {
|
||||||
|
if upstream != "" {
|
||||||
|
// Add both the domain and potentially resolved IPs
|
||||||
|
trustedMap[upstream] = struct{}{}
|
||||||
|
|
||||||
|
// Try to resolve the domain to IPs and add them too
|
||||||
|
if ips, err := net.LookupIP(upstream); err == nil {
|
||||||
|
for _, ip := range ips {
|
||||||
|
trustedMap[ip.String()] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
proxy := &SNIProxy{
|
proxy := &SNIProxy{
|
||||||
port: port,
|
port: port,
|
||||||
cache: cache.New(3*time.Second, 10*time.Minute),
|
cache: cache.New(3*time.Second, 10*time.Minute),
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
cancel: cancel,
|
cancel: cancel,
|
||||||
localProxyAddr: localProxyAddr,
|
localProxyAddr: localProxyAddr,
|
||||||
localProxyPort: localProxyPort,
|
localProxyPort: localProxyPort,
|
||||||
remoteConfigURL: remoteConfigURL,
|
remoteConfigURL: remoteConfigURL,
|
||||||
publicKey: publicKey,
|
publicKey: publicKey,
|
||||||
proxyProtocol: proxyProtocol,
|
proxyProtocol: proxyProtocol,
|
||||||
localSNIs: make(map[string]struct{}),
|
localSNIs: make(map[string]struct{}),
|
||||||
localOverrides: overridesMap,
|
localOverrides: overridesMap,
|
||||||
activeTunnels: make(map[string]*activeTunnel),
|
activeTunnels: make(map[string]*activeTunnel),
|
||||||
|
trustedUpstreams: trustedMap,
|
||||||
}
|
}
|
||||||
|
|
||||||
return proxy, nil
|
return proxy, nil
|
||||||
@@ -270,14 +489,35 @@ func (p *SNIProxy) handleConnection(clientConn net.Conn) {
|
|||||||
|
|
||||||
logger.Debug("Accepted connection from %s", clientConn.RemoteAddr())
|
logger.Debug("Accepted connection from %s", clientConn.RemoteAddr())
|
||||||
|
|
||||||
|
// Check for PROXY protocol from trusted upstream
|
||||||
|
var proxyInfo *ProxyProtocolInfo
|
||||||
|
var actualClientConn net.Conn = clientConn
|
||||||
|
|
||||||
|
if len(p.trustedUpstreams) > 0 {
|
||||||
|
var err error
|
||||||
|
proxyInfo, actualClientConn, err = p.parseProxyProtocolHeader(clientConn)
|
||||||
|
if err != nil {
|
||||||
|
logger.Debug("Failed to parse PROXY protocol: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if proxyInfo != nil {
|
||||||
|
logger.Debug("Received PROXY protocol from trusted upstream: %s:%d -> %s:%d",
|
||||||
|
proxyInfo.SrcIP, proxyInfo.SrcPort, proxyInfo.DestIP, proxyInfo.DestPort)
|
||||||
|
} else {
|
||||||
|
// No PROXY protocol detected, but connection is from trusted upstream
|
||||||
|
// This is fine - treat as regular connection
|
||||||
|
logger.Debug("No PROXY protocol detected from trusted upstream, treating as regular connection")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Set read timeout for SNI extraction
|
// Set read timeout for SNI extraction
|
||||||
if err := clientConn.SetReadDeadline(time.Now().Add(5 * time.Second)); err != nil {
|
if err := actualClientConn.SetReadDeadline(time.Now().Add(5 * time.Second)); err != nil {
|
||||||
logger.Debug("Failed to set read deadline: %v", err)
|
logger.Debug("Failed to set read deadline: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract SNI hostname
|
// Extract SNI hostname
|
||||||
hostname, clientReader, err := p.extractSNI(clientConn)
|
hostname, clientReader, err := p.extractSNI(actualClientConn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Debug("SNI extraction failed: %v", err)
|
logger.Debug("SNI extraction failed: %v", err)
|
||||||
return
|
return
|
||||||
@@ -291,13 +531,20 @@ func (p *SNIProxy) handleConnection(clientConn net.Conn) {
|
|||||||
logger.Debug("SNI hostname detected: %s", hostname)
|
logger.Debug("SNI hostname detected: %s", hostname)
|
||||||
|
|
||||||
// Remove read timeout for normal operation
|
// Remove read timeout for normal operation
|
||||||
if err := clientConn.SetReadDeadline(time.Time{}); err != nil {
|
if err := actualClientConn.SetReadDeadline(time.Time{}); err != nil {
|
||||||
logger.Debug("Failed to clear read deadline: %v", err)
|
logger.Debug("Failed to clear read deadline: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get routing information
|
// Get routing information - use original client address if available from PROXY protocol
|
||||||
route, err := p.getRoute(hostname, clientConn.RemoteAddr().String())
|
var clientAddrStr string
|
||||||
|
if proxyInfo != nil {
|
||||||
|
clientAddrStr = fmt.Sprintf("%s:%d", proxyInfo.SrcIP, proxyInfo.SrcPort)
|
||||||
|
} else {
|
||||||
|
clientAddrStr = clientConn.RemoteAddr().String()
|
||||||
|
}
|
||||||
|
|
||||||
|
route, err := p.getRoute(hostname, clientAddrStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Debug("Failed to get route for %s: %v", hostname, err)
|
logger.Debug("Failed to get route for %s: %v", hostname, err)
|
||||||
return
|
return
|
||||||
@@ -325,7 +572,14 @@ func (p *SNIProxy) handleConnection(clientConn net.Conn) {
|
|||||||
|
|
||||||
// Send PROXY protocol header if enabled
|
// Send PROXY protocol header if enabled
|
||||||
if p.proxyProtocol {
|
if p.proxyProtocol {
|
||||||
proxyHeader := buildProxyProtocolHeader(clientConn.RemoteAddr(), targetConn.LocalAddr())
|
var proxyHeader string
|
||||||
|
if proxyInfo != nil {
|
||||||
|
// Use original client info from PROXY protocol
|
||||||
|
proxyHeader = p.buildProxyProtocolHeaderFromInfo(proxyInfo, targetConn.LocalAddr())
|
||||||
|
} else {
|
||||||
|
// Use direct client connection info
|
||||||
|
proxyHeader = buildProxyProtocolHeader(clientConn.RemoteAddr(), targetConn.LocalAddr())
|
||||||
|
}
|
||||||
logger.Debug("Sending PROXY protocol header: %s", strings.TrimSpace(proxyHeader))
|
logger.Debug("Sending PROXY protocol header: %s", strings.TrimSpace(proxyHeader))
|
||||||
|
|
||||||
if _, err := targetConn.Write([]byte(proxyHeader)); err != nil {
|
if _, err := targetConn.Write([]byte(proxyHeader)); err != nil {
|
||||||
@@ -341,7 +595,7 @@ func (p *SNIProxy) handleConnection(clientConn net.Conn) {
|
|||||||
tunnel = &activeTunnel{}
|
tunnel = &activeTunnel{}
|
||||||
p.activeTunnels[hostname] = tunnel
|
p.activeTunnels[hostname] = tunnel
|
||||||
}
|
}
|
||||||
tunnel.conns = append(tunnel.conns, clientConn)
|
tunnel.conns = append(tunnel.conns, actualClientConn)
|
||||||
p.activeTunnelsLock.Unlock()
|
p.activeTunnelsLock.Unlock()
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
@@ -350,7 +604,7 @@ func (p *SNIProxy) handleConnection(clientConn net.Conn) {
|
|||||||
if tunnel, ok := p.activeTunnels[hostname]; ok {
|
if tunnel, ok := p.activeTunnels[hostname]; ok {
|
||||||
newConns := make([]net.Conn, 0, len(tunnel.conns))
|
newConns := make([]net.Conn, 0, len(tunnel.conns))
|
||||||
for _, c := range tunnel.conns {
|
for _, c := range tunnel.conns {
|
||||||
if c != clientConn {
|
if c != actualClientConn {
|
||||||
newConns = append(newConns, c)
|
newConns = append(newConns, c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -364,7 +618,7 @@ func (p *SNIProxy) handleConnection(clientConn net.Conn) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// Start bidirectional data transfer
|
// Start bidirectional data transfer
|
||||||
p.pipe(clientConn, targetConn, clientReader)
|
p.pipe(actualClientConn, targetConn, clientReader)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getRoute retrieves routing information for a hostname
|
// getRoute retrieves routing information for a hostname
|
||||||
|
|||||||
@@ -76,3 +76,44 @@ func TestBuildProxyProtocolHeaderUnknownType(t *testing.T) {
|
|||||||
t.Errorf("Expected %q, got %q", expected, result)
|
t.Errorf("Expected %q, got %q", expected, result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBuildProxyProtocolHeaderFromInfo(t *testing.T) {
|
||||||
|
proxy, err := NewSNIProxy(8443, "", "", "127.0.0.1", 443, nil, true, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to create SNI proxy: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test IPv4 case
|
||||||
|
proxyInfo := &ProxyProtocolInfo{
|
||||||
|
Protocol: "TCP4",
|
||||||
|
SrcIP: "10.0.0.1",
|
||||||
|
DestIP: "192.168.1.100",
|
||||||
|
SrcPort: 12345,
|
||||||
|
DestPort: 443,
|
||||||
|
}
|
||||||
|
|
||||||
|
targetAddr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:8080")
|
||||||
|
header := proxy.buildProxyProtocolHeaderFromInfo(proxyInfo, targetAddr)
|
||||||
|
|
||||||
|
expected := "PROXY TCP4 10.0.0.1 127.0.0.1 12345 8080\r\n"
|
||||||
|
if header != expected {
|
||||||
|
t.Errorf("Expected header '%s', got '%s'", expected, header)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test IPv6 case
|
||||||
|
proxyInfo = &ProxyProtocolInfo{
|
||||||
|
Protocol: "TCP6",
|
||||||
|
SrcIP: "2001:db8::1",
|
||||||
|
DestIP: "2001:db8::2",
|
||||||
|
SrcPort: 12345,
|
||||||
|
DestPort: 443,
|
||||||
|
}
|
||||||
|
|
||||||
|
targetAddr, _ = net.ResolveTCPAddr("tcp6", "[::1]:8080")
|
||||||
|
header = proxy.buildProxyProtocolHeaderFromInfo(proxyInfo, targetAddr)
|
||||||
|
|
||||||
|
expected = "PROXY TCP6 2001:db8::1 ::1 12345 8080\r\n"
|
||||||
|
if header != expected {
|
||||||
|
t.Errorf("Expected header '%s', got '%s'", expected, header)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
140
relay/relay.go
140
relay/relay.go
@@ -64,6 +64,17 @@ type WireGuardSession struct {
|
|||||||
LastSeen time.Time
|
LastSeen time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Type for tracking bidirectional communication patterns to rebuild sessions
|
||||||
|
type CommunicationPattern struct {
|
||||||
|
FromClient *net.UDPAddr // The client address
|
||||||
|
ToDestination *net.UDPAddr // The destination address
|
||||||
|
ClientIndex uint32 // The receiver index seen from client
|
||||||
|
DestIndex uint32 // The receiver index seen from destination
|
||||||
|
LastFromClient time.Time // Last packet from client to destination
|
||||||
|
LastFromDest time.Time // Last packet from destination to client
|
||||||
|
PacketCount int // Number of packets observed
|
||||||
|
}
|
||||||
|
|
||||||
type InitialMappings struct {
|
type InitialMappings struct {
|
||||||
Mappings map[string]ProxyMapping `json:"mappings"` // key is "ip:port"
|
Mappings map[string]ProxyMapping `json:"mappings"` // key is "ip:port"
|
||||||
}
|
}
|
||||||
@@ -105,6 +116,9 @@ type UDPProxyServer struct {
|
|||||||
// Session tracking for WireGuard peers
|
// Session tracking for WireGuard peers
|
||||||
// Key format: "senderIndex:receiverIndex"
|
// Key format: "senderIndex:receiverIndex"
|
||||||
wgSessions sync.Map
|
wgSessions sync.Map
|
||||||
|
// Communication pattern tracking for rebuilding sessions
|
||||||
|
// Key format: "clientIP:clientPort-destIP:destPort"
|
||||||
|
commPatterns sync.Map
|
||||||
// ReachableAt is the URL where this server can be reached
|
// ReachableAt is the URL where this server can be reached
|
||||||
ReachableAt string
|
ReachableAt string
|
||||||
}
|
}
|
||||||
@@ -156,6 +170,9 @@ func (s *UDPProxyServer) Start() error {
|
|||||||
// Start the proxy mapping cleanup routine
|
// Start the proxy mapping cleanup routine
|
||||||
go s.cleanupIdleProxyMappings()
|
go s.cleanupIdleProxyMappings()
|
||||||
|
|
||||||
|
// Start the communication pattern cleanup routine
|
||||||
|
go s.cleanupIdleCommunicationPatterns()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -445,6 +462,9 @@ func (s *UDPProxyServer) handleWireGuardPacket(packet []byte, remoteAddr *net.UD
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Track communication pattern for session rebuilding
|
||||||
|
s.trackCommunicationPattern(remoteAddr, destAddr, receiverIndex, true)
|
||||||
|
|
||||||
_, err = conn.Write(packet)
|
_, err = conn.Write(packet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Debug("Failed to forward transport data: %v", err)
|
logger.Debug("Failed to forward transport data: %v", err)
|
||||||
@@ -465,6 +485,9 @@ func (s *UDPProxyServer) handleWireGuardPacket(packet []byte, remoteAddr *net.UD
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Track communication pattern for session rebuilding
|
||||||
|
s.trackCommunicationPattern(remoteAddr, destAddr, receiverIndex, true)
|
||||||
|
|
||||||
_, err = conn.Write(packet)
|
_, err = conn.Write(packet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Debug("Failed to forward transport data: %v", err)
|
logger.Debug("Failed to forward transport data: %v", err)
|
||||||
@@ -548,6 +571,9 @@ func (s *UDPProxyServer) handleResponses(conn *net.UDPConn, destAddr *net.UDPAdd
|
|||||||
LastSeen: time.Now(),
|
LastSeen: time.Now(),
|
||||||
})
|
})
|
||||||
logger.Debug("Stored session mapping: %s -> %s", sessionKey, destAddr.String())
|
logger.Debug("Stored session mapping: %s -> %s", sessionKey, destAddr.String())
|
||||||
|
} else if ok && buffer[0] == WireGuardMessageTypeTransportData {
|
||||||
|
// Track communication pattern for session rebuilding (reverse direction)
|
||||||
|
s.trackCommunicationPattern(destAddr, remoteAddr, receiverIndex, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -823,3 +849,117 @@ func (s *UDPProxyServer) UpdateDestinationInMappings(oldDest, newDest PeerDestin
|
|||||||
|
|
||||||
return updatedCount
|
return updatedCount
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// trackCommunicationPattern tracks bidirectional communication patterns to rebuild sessions
|
||||||
|
func (s *UDPProxyServer) trackCommunicationPattern(fromAddr, toAddr *net.UDPAddr, receiverIndex uint32, fromClient bool) {
|
||||||
|
var clientAddr, destAddr *net.UDPAddr
|
||||||
|
var clientIndex, destIndex uint32
|
||||||
|
|
||||||
|
if fromClient {
|
||||||
|
clientAddr = fromAddr
|
||||||
|
destAddr = toAddr
|
||||||
|
clientIndex = receiverIndex
|
||||||
|
destIndex = 0 // We don't know the destination index yet
|
||||||
|
} else {
|
||||||
|
clientAddr = toAddr
|
||||||
|
destAddr = fromAddr
|
||||||
|
clientIndex = 0 // We don't know the client index yet
|
||||||
|
destIndex = receiverIndex
|
||||||
|
}
|
||||||
|
|
||||||
|
patternKey := fmt.Sprintf("%s-%s", clientAddr.String(), destAddr.String())
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
|
if existingPattern, ok := s.commPatterns.Load(patternKey); ok {
|
||||||
|
pattern := existingPattern.(*CommunicationPattern)
|
||||||
|
|
||||||
|
// Update the pattern
|
||||||
|
if fromClient {
|
||||||
|
pattern.LastFromClient = now
|
||||||
|
if pattern.ClientIndex == 0 {
|
||||||
|
pattern.ClientIndex = clientIndex
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
pattern.LastFromDest = now
|
||||||
|
if pattern.DestIndex == 0 {
|
||||||
|
pattern.DestIndex = destIndex
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pattern.PacketCount++
|
||||||
|
s.commPatterns.Store(patternKey, pattern)
|
||||||
|
|
||||||
|
// Check if we have bidirectional communication and can rebuild a session
|
||||||
|
s.tryRebuildSession(pattern)
|
||||||
|
} else {
|
||||||
|
// Create new pattern
|
||||||
|
pattern := &CommunicationPattern{
|
||||||
|
FromClient: clientAddr,
|
||||||
|
ToDestination: destAddr,
|
||||||
|
ClientIndex: clientIndex,
|
||||||
|
DestIndex: destIndex,
|
||||||
|
PacketCount: 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
if fromClient {
|
||||||
|
pattern.LastFromClient = now
|
||||||
|
} else {
|
||||||
|
pattern.LastFromDest = now
|
||||||
|
}
|
||||||
|
|
||||||
|
s.commPatterns.Store(patternKey, pattern)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// tryRebuildSession attempts to rebuild a WireGuard session from communication patterns
|
||||||
|
func (s *UDPProxyServer) tryRebuildSession(pattern *CommunicationPattern) {
|
||||||
|
// Check if we have bidirectional communication within a reasonable time window
|
||||||
|
timeDiff := pattern.LastFromClient.Sub(pattern.LastFromDest)
|
||||||
|
if timeDiff < 0 {
|
||||||
|
timeDiff = -timeDiff
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only rebuild if we have recent bidirectional communication and both indices
|
||||||
|
if timeDiff < 30*time.Second && pattern.ClientIndex != 0 && pattern.DestIndex != 0 && pattern.PacketCount >= 4 {
|
||||||
|
// Create session mapping: client's index maps to destination
|
||||||
|
sessionKey := fmt.Sprintf("%d:%d", pattern.DestIndex, pattern.ClientIndex)
|
||||||
|
|
||||||
|
// Check if we already have this session
|
||||||
|
if _, exists := s.wgSessions.Load(sessionKey); !exists {
|
||||||
|
session := &WireGuardSession{
|
||||||
|
ReceiverIndex: pattern.DestIndex,
|
||||||
|
SenderIndex: pattern.ClientIndex,
|
||||||
|
DestAddr: pattern.ToDestination,
|
||||||
|
LastSeen: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
s.wgSessions.Store(sessionKey, session)
|
||||||
|
logger.Info("Rebuilt WireGuard session from communication pattern: %s -> %s (packets: %d)",
|
||||||
|
sessionKey, pattern.ToDestination.String(), pattern.PacketCount)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// cleanupIdleCommunicationPatterns periodically removes idle communication patterns
|
||||||
|
func (s *UDPProxyServer) cleanupIdleCommunicationPatterns() {
|
||||||
|
ticker := time.NewTicker(10 * time.Minute)
|
||||||
|
for range ticker.C {
|
||||||
|
now := time.Now()
|
||||||
|
s.commPatterns.Range(func(key, value interface{}) bool {
|
||||||
|
pattern := value.(*CommunicationPattern)
|
||||||
|
|
||||||
|
// Get the most recent activity
|
||||||
|
lastActivity := pattern.LastFromClient
|
||||||
|
if pattern.LastFromDest.After(lastActivity) {
|
||||||
|
lastActivity = pattern.LastFromDest
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove patterns that haven't had activity in 20 minutes
|
||||||
|
if now.Sub(lastActivity) > 20*time.Minute {
|
||||||
|
s.commPatterns.Delete(key)
|
||||||
|
logger.Debug("Removed idle communication pattern: %s", key)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user