Compare commits
49 Commits
5c6826fe89
...
v1.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 878664e0ac | |||
| 20a88df330 | |||
| 075dd7ecad | |||
| ab34b34765 | |||
| 514c4f9de1 | |||
| d8330c684f | |||
| fbf182025b | |||
| 1038c0861e | |||
| 64e0d5805e | |||
| 85f21e7698 | |||
| 08565d845f | |||
| a7d9b2ab8a | |||
| bc8c5127a6 | |||
| a49b53e56f | |||
| e5b5cc3ae5 | |||
| b0b00764cf | |||
| 8b6cdef2e9 | |||
| 653517f5be | |||
| f11a92fb3b | |||
| ac283626d3 | |||
| ad7c5985b1 | |||
| 2644b4521c | |||
| d23ed27a4a | |||
| b5862bd7a0 | |||
| bf7f7bd8da | |||
| c3a469be64 | |||
| eee04daf80 | |||
| 1d918ef2aa | |||
| a2676a4f30 | |||
| 83657d3206 | |||
| 6710aec4bf | |||
| 0ca6285ef5 | |||
| 28cc069fdb | |||
| fe58e35e91 | |||
| 6cac64412c | |||
| 318003ac9f | |||
| 14fa237027 | |||
| 9a2a373eb3 | |||
| 1b248a2957 | |||
| 7348bdafb7 | |||
| cb8529f13e | |||
| fa6b097d66 | |||
| e3c4f59a77 | |||
| c69cd68820 | |||
| 76d1202b8e | |||
| 6dff735216 | |||
| 7bc5a01ba7 | |||
| 2a43f1441c | |||
| 6451304ed7 |
32
.dockerignore
Normal file
32
.dockerignore
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
.git/
|
||||||
|
.github/
|
||||||
|
.gitea/
|
||||||
|
.gitignore
|
||||||
|
|
||||||
|
.idea/
|
||||||
|
*.swp
|
||||||
|
*.swo
|
||||||
|
*~
|
||||||
|
|
||||||
|
tmp/
|
||||||
|
*.log
|
||||||
|
|
||||||
|
.env
|
||||||
|
.env.*
|
||||||
|
|
||||||
|
certs/
|
||||||
|
id_rsa*
|
||||||
|
*.pub
|
||||||
|
|
||||||
|
README.md
|
||||||
|
LICENSE.md
|
||||||
|
*.md
|
||||||
|
|
||||||
|
renovate.json
|
||||||
|
renovate-config.js
|
||||||
|
|
||||||
|
*_test.go
|
||||||
|
testdata/
|
||||||
|
|
||||||
|
app
|
||||||
|
|
||||||
@@ -5,14 +5,25 @@ on:
|
|||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
- staging
|
- staging
|
||||||
|
tags:
|
||||||
|
- 'v*'
|
||||||
|
paths:
|
||||||
|
- '**.go'
|
||||||
|
- 'go.mod'
|
||||||
|
- 'go.sum'
|
||||||
|
- 'Dockerfile'
|
||||||
|
- 'Dockerfile.*'
|
||||||
|
- '.dockerignore'
|
||||||
|
- '.gitea/workflows/build.yml'
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-and-push:
|
build-and-push-branches:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
if: github.ref_type == 'branch'
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v6
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@v3
|
||||||
@@ -24,6 +35,17 @@ jobs:
|
|||||||
username: ${{ secrets.DOCKER_USERNAME }}
|
username: ${{ secrets.DOCKER_USERNAME }}
|
||||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||||
|
|
||||||
|
- name: Set version variables
|
||||||
|
id: vars
|
||||||
|
run: |
|
||||||
|
if [ "${{ github.ref }}" == "refs/heads/main" ]; then
|
||||||
|
echo "VERSION=dev-main" >> $GITHUB_OUTPUT
|
||||||
|
else
|
||||||
|
echo "VERSION=dev-staging" >> $GITHUB_OUTPUT
|
||||||
|
fi
|
||||||
|
echo "BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
|
||||||
|
echo "COMMIT=${{ github.sha }}" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
- name: Build and push Docker image for main
|
- name: Build and push Docker image for main
|
||||||
uses: docker/build-push-action@v6
|
uses: docker/build-push-action@v6
|
||||||
with:
|
with:
|
||||||
@@ -32,6 +54,10 @@ jobs:
|
|||||||
tags: |
|
tags: |
|
||||||
git.fossy.my.id/${{ secrets.DOCKER_USERNAME }}/tunnel-please:latest
|
git.fossy.my.id/${{ secrets.DOCKER_USERNAME }}/tunnel-please:latest
|
||||||
platforms: linux/amd64,linux/arm64
|
platforms: linux/amd64,linux/arm64
|
||||||
|
build-args: |
|
||||||
|
VERSION=${{ steps.vars.outputs.VERSION }}
|
||||||
|
BUILD_DATE=${{ steps.vars.outputs.BUILD_DATE }}
|
||||||
|
COMMIT=${{ steps.vars.outputs.COMMIT }}
|
||||||
if: github.ref == 'refs/heads/main'
|
if: github.ref == 'refs/heads/main'
|
||||||
|
|
||||||
- name: Build and push Docker image for staging
|
- name: Build and push Docker image for staging
|
||||||
@@ -42,4 +68,85 @@ jobs:
|
|||||||
tags: |
|
tags: |
|
||||||
git.fossy.my.id/${{ secrets.DOCKER_USERNAME }}/tunnel-please:staging
|
git.fossy.my.id/${{ secrets.DOCKER_USERNAME }}/tunnel-please:staging
|
||||||
platforms: linux/amd64,linux/arm64
|
platforms: linux/amd64,linux/arm64
|
||||||
|
build-args: |
|
||||||
|
VERSION=${{ steps.vars.outputs.VERSION }}
|
||||||
|
BUILD_DATE=${{ steps.vars.outputs.BUILD_DATE }}
|
||||||
|
COMMIT=${{ steps.vars.outputs.COMMIT }}
|
||||||
if: github.ref == 'refs/heads/staging'
|
if: github.ref == 'refs/heads/staging'
|
||||||
|
|
||||||
|
build-and-push-tags:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
if: github.ref_type == 'tag' && startsWith(github.ref, 'refs/tags/v')
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v6
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
|
- name: Log in to Docker Hub
|
||||||
|
uses: docker/login-action@v3
|
||||||
|
with:
|
||||||
|
registry: git.fossy.my.id
|
||||||
|
username: ${{ secrets.DOCKER_USERNAME }}
|
||||||
|
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||||
|
|
||||||
|
- name: Extract version and determine release type
|
||||||
|
id: version
|
||||||
|
run: |
|
||||||
|
VERSION=${GITHUB_REF#refs/tags/v}
|
||||||
|
echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
|
||||||
|
echo "BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
|
||||||
|
echo "COMMIT=${{ github.sha }}" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
if echo "$VERSION" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.]+)?$'; then
|
||||||
|
MAJOR=$(echo "$VERSION" | cut -d. -f1)
|
||||||
|
MINOR=$(echo "$VERSION" | cut -d. -f2)
|
||||||
|
|
||||||
|
echo "MAJOR=$MAJOR" >> $GITHUB_OUTPUT
|
||||||
|
echo "MINOR=$MINOR" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
if echo "$VERSION" | grep -q '-'; then
|
||||||
|
echo "IS_PRERELEASE=true" >> $GITHUB_OUTPUT
|
||||||
|
echo "ADDITIONAL_TAG=staging" >> $GITHUB_OUTPUT
|
||||||
|
else
|
||||||
|
echo "IS_PRERELEASE=false" >> $GITHUB_OUTPUT
|
||||||
|
echo "ADDITIONAL_TAG=latest" >> $GITHUB_OUTPUT
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "Invalid version format: $VERSION"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Build and push Docker image for release
|
||||||
|
uses: docker/build-push-action@v6
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
push: true
|
||||||
|
tags: |
|
||||||
|
git.fossy.my.id/${{ secrets.DOCKER_USERNAME }}/tunnel-please:v${{ steps.version.outputs.VERSION }}
|
||||||
|
git.fossy.my.id/${{ secrets.DOCKER_USERNAME }}/tunnel-please:v${{ steps.version.outputs.MAJOR }}.${{ steps.version.outputs.MINOR }}
|
||||||
|
git.fossy.my.id/${{ secrets.DOCKER_USERNAME }}/tunnel-please:v${{ steps.version.outputs.MAJOR }}
|
||||||
|
git.fossy.my.id/${{ secrets.DOCKER_USERNAME }}/tunnel-please:${{ steps.version.outputs.ADDITIONAL_TAG }}
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
|
build-args: |
|
||||||
|
VERSION=${{ steps.version.outputs.VERSION }}
|
||||||
|
BUILD_DATE=${{ steps.version.outputs.BUILD_DATE }}
|
||||||
|
COMMIT=${{ steps.version.outputs.COMMIT }}
|
||||||
|
if: steps.version.outputs.IS_PRERELEASE == 'false'
|
||||||
|
|
||||||
|
- name: Build and push Docker image for pre-release
|
||||||
|
uses: docker/build-push-action@v6
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
push: true
|
||||||
|
tags: |
|
||||||
|
git.fossy.my.id/${{ secrets.DOCKER_USERNAME }}/tunnel-please:v${{ steps.version.outputs.VERSION }}
|
||||||
|
git.fossy.my.id/${{ secrets.DOCKER_USERNAME }}/tunnel-please:${{ steps.version.outputs.ADDITIONAL_TAG }}
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
|
build-args: |
|
||||||
|
VERSION=${{ steps.version.outputs.VERSION }}
|
||||||
|
BUILD_DATE=${{ steps.version.outputs.BUILD_DATE }}
|
||||||
|
COMMIT=${{ steps.version.outputs.COMMIT }}
|
||||||
|
if: steps.version.outputs.IS_PRERELEASE == 'true'
|
||||||
|
|||||||
21
.gitea/workflows/renovate.yml
Normal file
21
.gitea/workflows/renovate.yml
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
name: renovate
|
||||||
|
|
||||||
|
on:
|
||||||
|
schedule:
|
||||||
|
- cron: "0 0 * * *"
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- staging
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
renovate:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container: git.fossy.my.id/renovate-clanker/renovate:latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v6
|
||||||
|
- run: renovate
|
||||||
|
env:
|
||||||
|
RENOVATE_CONFIG_FILE: ${{ gitea.workspace }}/renovate-config.js
|
||||||
|
LOG_LEVEL: "debug"
|
||||||
|
RENOVATE_TOKEN: ${{ secrets.RENOVATE_TOKEN }}
|
||||||
|
GITHUB_COM_TOKEN: ${{ secrets.COM_TOKEN }}
|
||||||
27
.github/workflows/sync-to-gitea.yml
vendored
Normal file
27
.github/workflows/sync-to-gitea.yml
vendored
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
name: Sync to Gitea
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- '**'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
sync:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
if: |
|
||||||
|
github.repository == 'fossyy/tunnel-please' &&
|
||||||
|
!contains(github.event.head_commit.message, '[skip-gitea]')
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v6
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Push to Gitea
|
||||||
|
env:
|
||||||
|
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||||
|
GITEA_URL: git.fossy.my.id
|
||||||
|
run: |
|
||||||
|
git remote add gitea https://${{ secrets.GITEA_USERNAME }}:${GITEA_TOKEN}@${GITEA_URL}/bagas/tunnel-please.git
|
||||||
|
git push gitea ${{ github.ref }} --force
|
||||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -3,3 +3,5 @@ id_rsa*
|
|||||||
.idea
|
.idea
|
||||||
.env
|
.env
|
||||||
tmp
|
tmp
|
||||||
|
certs
|
||||||
|
app
|
||||||
55
Dockerfile
55
Dockerfile
@@ -1,20 +1,59 @@
|
|||||||
FROM golang:1.24.4-alpine AS go_builder
|
FROM golang:1.25.5-alpine AS go_builder
|
||||||
|
|
||||||
|
ARG VERSION=dev
|
||||||
|
ARG BUILD_DATE=unknown
|
||||||
|
ARG COMMIT=unknown
|
||||||
|
|
||||||
|
RUN apk update && apk upgrade && \
|
||||||
|
apk add --no-cache ca-certificates tzdata git && \
|
||||||
|
update-ca-certificates
|
||||||
|
|
||||||
WORKDIR /src
|
WORKDIR /src
|
||||||
|
|
||||||
|
COPY go.mod go.sum ./
|
||||||
|
|
||||||
|
RUN --mount=type=cache,target=/go/pkg/mod \
|
||||||
|
--mount=type=cache,target=/root/.cache/go-build \
|
||||||
|
go mod download && go mod verify
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
RUN apk update && apk upgrade && apk add --no-cache ca-certificates tzdata
|
RUN --mount=type=cache,target=/go/pkg/mod \
|
||||||
RUN update-ca-certificates
|
--mount=type=cache,target=/root/.cache/go-build \
|
||||||
RUN go build -o ./tmp/main
|
CGO_ENABLED=0 GOOS=linux \
|
||||||
|
go build -trimpath \
|
||||||
|
-ldflags="-w -s -X tunnel_pls/version.Version=${VERSION} -X tunnel_pls/version.BuildDate=${BUILD_DATE} -X tunnel_pls/version.Commit=${COMMIT}" \
|
||||||
|
-o /app/tunnel_pls \
|
||||||
|
.
|
||||||
|
|
||||||
|
RUN adduser -D -u 10001 -g '' appuser && \
|
||||||
|
mkdir -p /app/certs/ssh /app/certs/tls && \
|
||||||
|
chown -R appuser:appuser /app
|
||||||
|
|
||||||
FROM scratch
|
FROM scratch
|
||||||
|
|
||||||
WORKDIR /src
|
ARG VERSION=dev
|
||||||
|
ARG BUILD_DATE=unknown
|
||||||
|
ARG COMMIT=unknown
|
||||||
|
|
||||||
COPY --from=go_builder /usr/share/zoneinfo /usr/share/zoneinfo
|
COPY --from=go_builder /usr/share/zoneinfo /usr/share/zoneinfo
|
||||||
COPY --from=go_builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
|
COPY --from=go_builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
|
||||||
COPY --from=go_builder /src/tmp/main /src
|
COPY --from=go_builder /etc/passwd /etc/passwd
|
||||||
|
COPY --from=go_builder /etc/group /etc/group
|
||||||
|
COPY --from=go_builder --chown=appuser:appuser /app /app
|
||||||
|
|
||||||
ENV TZ Asia/Jakarta
|
WORKDIR /app
|
||||||
|
|
||||||
ENTRYPOINT ["./main"]
|
USER appuser
|
||||||
|
|
||||||
|
ENV TZ=Asia/Jakarta
|
||||||
|
|
||||||
|
EXPOSE 2200 8080 8443
|
||||||
|
|
||||||
|
LABEL org.opencontainers.image.title="Tunnel Please" \
|
||||||
|
org.opencontainers.image.description="SSH-based tunnel server" \
|
||||||
|
org.opencontainers.image.version="${VERSION}" \
|
||||||
|
org.opencontainers.image.revision="${COMMIT}" \
|
||||||
|
org.opencontainers.image.created="${BUILD_DATE}"
|
||||||
|
|
||||||
|
ENTRYPOINT ["/app/tunnel_pls"]
|
||||||
|
|||||||
207
README.md
207
README.md
@@ -6,7 +6,6 @@ A lightweight SSH-based tunnel server written in Go that enables secure TCP and
|
|||||||
|
|
||||||
- SSH interactive session with real-time command handling
|
- SSH interactive session with real-time command handling
|
||||||
- Custom subdomain management for HTTP tunnels
|
- Custom subdomain management for HTTP tunnels
|
||||||
- Active connection control with drop functionality
|
|
||||||
- Dual protocol support: HTTP and TCP tunnels
|
- Dual protocol support: HTTP and TCP tunnels
|
||||||
- Real-time connection monitoring
|
- Real-time connection monitoring
|
||||||
## Requirements
|
## Requirements
|
||||||
@@ -14,6 +13,212 @@ A lightweight SSH-based tunnel server written in Go that enables secure TCP and
|
|||||||
- Go 1.18 or higher
|
- Go 1.18 or higher
|
||||||
- Valid domain name for subdomain routing
|
- Valid domain name for subdomain routing
|
||||||
|
|
||||||
|
## Environment Variables
|
||||||
|
|
||||||
|
The following environment variables can be configured in the `.env` file:
|
||||||
|
|
||||||
|
| Variable | Description | Default | Required |
|
||||||
|
|----------|-------------|---------|----------|
|
||||||
|
| `DOMAIN` | Domain name for subdomain routing | `localhost` | No |
|
||||||
|
| `PORT` | SSH server port | `2200` | No |
|
||||||
|
| `HTTP_PORT` | HTTP server port | `8080` | No |
|
||||||
|
| `HTTPS_PORT` | HTTPS server port | `8443` | No |
|
||||||
|
| `TLS_ENABLED` | Enable TLS/HTTPS | `false` | No |
|
||||||
|
| `TLS_REDIRECT` | Redirect HTTP to HTTPS | `false` | No |
|
||||||
|
| `ACME_EMAIL` | Email for Let's Encrypt registration | `admin@<DOMAIN>` | No |
|
||||||
|
| `CF_API_TOKEN` | Cloudflare API token for DNS-01 challenge | - | Yes (if auto-cert) |
|
||||||
|
| `ACME_STAGING` | Use Let's Encrypt staging server | `false` | No |
|
||||||
|
| `CORS_LIST` | Comma-separated list of allowed CORS origins | - | No |
|
||||||
|
| `ALLOWED_PORTS` | Port range for TCP tunnels (e.g., 40000-41000) | `40000-41000` | No |
|
||||||
|
| `BUFFER_SIZE` | Buffer size for io.Copy operations in bytes (4096-1048576) | `32768` | No |
|
||||||
|
| `PPROF_ENABLED` | Enable pprof profiling server | `false` | No |
|
||||||
|
| `PPROF_PORT` | Port for pprof server | `6060` | No |
|
||||||
|
|
||||||
|
**Note:** All environment variables now use UPPERCASE naming. The application includes sensible defaults for all variables, so you can run it without a `.env` file for basic functionality.
|
||||||
|
|
||||||
|
### Automatic TLS Certificate Management
|
||||||
|
|
||||||
|
The server supports automatic TLS certificate generation and renewal using [CertMagic](https://github.com/caddyserver/certmagic) with Cloudflare DNS-01 challenge. This is required for wildcard certificate support (`*.yourdomain.com`).
|
||||||
|
|
||||||
|
**Certificate Storage:**
|
||||||
|
- TLS certificates are stored in `certs/tls/` (relative to application directory)
|
||||||
|
- User-provided certificates: `certs/tls/cert.pem` and `certs/tls/privkey.pem`
|
||||||
|
- CertMagic automatic certificates: `certs/tls/certmagic/`
|
||||||
|
- SSH keys are stored separately in `certs/ssh/`
|
||||||
|
|
||||||
|
**How it works:**
|
||||||
|
1. If user-provided certificates exist at `certs/tls/cert.pem` and `certs/tls/privkey.pem` and cover both `DOMAIN` and `*.DOMAIN`, they will be used
|
||||||
|
2. If certificates are missing, expired, expiring within 30 days, or don't cover the required domains, CertMagic will automatically obtain new certificates from Let's Encrypt
|
||||||
|
3. Certificates are automatically renewed before expiration
|
||||||
|
4. User-provided certificates support hot-reload (changes detected every 30 seconds)
|
||||||
|
|
||||||
|
**Cloudflare API Token Setup:**
|
||||||
|
|
||||||
|
To use automatic certificate generation, you need a Cloudflare API token with the following permissions:
|
||||||
|
|
||||||
|
1. Go to [Cloudflare Dashboard](https://dash.cloudflare.com/profile/api-tokens)
|
||||||
|
2. Click "Create Token"
|
||||||
|
3. Use "Create Custom Token" with these permissions:
|
||||||
|
- **Zone → Zone → Read** (for all zones or specific zone)
|
||||||
|
- **Zone → DNS → Edit** (for all zones or specific zone)
|
||||||
|
4. Copy the token and set it as `CF_API_TOKEN` environment variable
|
||||||
|
|
||||||
|
**Example configuration for automatic certificates:**
|
||||||
|
```env
|
||||||
|
DOMAIN=example.com
|
||||||
|
TLS_ENABLED=true
|
||||||
|
CF_API_TOKEN=your_cloudflare_api_token_here
|
||||||
|
ACME_EMAIL=admin@example.com
|
||||||
|
# ACME_STAGING=true # Uncomment for testing to avoid rate limits
|
||||||
|
```
|
||||||
|
|
||||||
|
### SSH Key Auto-Generation
|
||||||
|
|
||||||
|
The application will automatically generate a new 4096-bit RSA key pair at `certs/ssh/id_rsa` if it doesn't exist. This makes it easier to get started without manually creating SSH keys. SSH keys are stored separately from TLS certificates.
|
||||||
|
|
||||||
|
### Memory Optimization
|
||||||
|
|
||||||
|
The application uses a buffer pool with controlled buffer sizes to prevent excessive memory usage under high concurrent loads. The `BUFFER_SIZE` environment variable controls the size of buffers used for io.Copy operations:
|
||||||
|
|
||||||
|
- **Default:** 32768 bytes (32 KB) - Good balance for most scenarios
|
||||||
|
- **Minimum:** 4096 bytes (4 KB) - Lower memory usage, more CPU overhead
|
||||||
|
- **Maximum:** 1048576 bytes (1 MB) - Higher throughput, more memory usage
|
||||||
|
|
||||||
|
**Recommended settings based on load:**
|
||||||
|
- **Low traffic (<100 concurrent):** `BUFFER_SIZE=32768` (default)
|
||||||
|
- **High traffic (>100 concurrent):** `BUFFER_SIZE=16384` or `BUFFER_SIZE=8192`
|
||||||
|
- **Very high traffic (>1000 concurrent):** `BUFFER_SIZE=8192` or `BUFFER_SIZE=4096`
|
||||||
|
|
||||||
|
The buffer pool reuses buffers across connections, preventing memory fragmentation and reducing garbage collection pressure.
|
||||||
|
|
||||||
|
### Profiling with pprof
|
||||||
|
|
||||||
|
To enable profiling for performance analysis:
|
||||||
|
|
||||||
|
1. Set `PPROF_ENABLED=true` in your `.env` file
|
||||||
|
2. Optionally set `PPROF_PORT` to your desired port (default: 6060)
|
||||||
|
3. Access profiling data at `http://localhost:6060/debug/pprof/`
|
||||||
|
|
||||||
|
Common pprof endpoints:
|
||||||
|
- `/debug/pprof/` - Index page with available profiles
|
||||||
|
- `/debug/pprof/heap` - Memory allocation profile
|
||||||
|
- `/debug/pprof/goroutine` - Stack traces of all current goroutines
|
||||||
|
- `/debug/pprof/profile` - CPU profile (30-second sample by default)
|
||||||
|
- `/debug/pprof/trace` - Execution trace
|
||||||
|
|
||||||
|
Example usage with `go tool pprof`:
|
||||||
|
```bash
|
||||||
|
# Analyze CPU profile
|
||||||
|
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
|
||||||
|
|
||||||
|
# Analyze memory heap
|
||||||
|
go tool pprof http://localhost:6060/debug/pprof/heap
|
||||||
|
```
|
||||||
|
|
||||||
|
## Docker Deployment
|
||||||
|
|
||||||
|
Three Docker Compose configurations are available for different deployment scenarios. Each configuration uses the image `git.fossy.my.id/bagas/tunnel-please:latest`.
|
||||||
|
|
||||||
|
### Configuration Options
|
||||||
|
|
||||||
|
#### 1. Root with Host Networking (RECOMMENDED)
|
||||||
|
|
||||||
|
**File:** `docker-compose.root.yml`
|
||||||
|
|
||||||
|
**Advantages:**
|
||||||
|
- Full TCP port forwarding support (ports 40000-41000)
|
||||||
|
- Direct binding to privileged ports (80, 443, 2200)
|
||||||
|
- Best performance with no NAT overhead
|
||||||
|
- Maximum flexibility for all tunnel types
|
||||||
|
- No port mapping limitations
|
||||||
|
|
||||||
|
**Use Case:** Production deployments where you need unrestricted TCP forwarding and maximum performance.
|
||||||
|
|
||||||
|
**Deploy:**
|
||||||
|
```bash
|
||||||
|
docker-compose -f docker-compose.root.yml up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 2. Standard (HTTP/HTTPS Only)
|
||||||
|
|
||||||
|
**File:** `docker-compose.standard.yml`
|
||||||
|
|
||||||
|
**Advantages:**
|
||||||
|
- Runs with unprivileged user (more secure)
|
||||||
|
- Standard port mappings (2200, 80, 443)
|
||||||
|
- Simple and predictable networking
|
||||||
|
- TCP port forwarding disabled (`ALLOWED_PORTS=none`)
|
||||||
|
|
||||||
|
**Use Case:** Deployments where you only need HTTP/HTTPS tunneling without custom TCP port forwarding.
|
||||||
|
|
||||||
|
**Deploy:**
|
||||||
|
```bash
|
||||||
|
docker-compose -f docker-compose.standard.yml up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 3. Limited TCP Forwarding
|
||||||
|
|
||||||
|
**File:** `docker-compose.tcp.yml`
|
||||||
|
|
||||||
|
**Advantages:**
|
||||||
|
- Runs with unprivileged user (more secure)
|
||||||
|
- Standard port mappings (2200, 80, 443)
|
||||||
|
- Limited TCP forwarding (ports 30000-31000)
|
||||||
|
- Controlled port range exposure
|
||||||
|
|
||||||
|
**Use Case:** Deployments where you need both HTTP/HTTPS tunneling and limited TCP forwarding within a specific port range.
|
||||||
|
|
||||||
|
**Deploy:**
|
||||||
|
```bash
|
||||||
|
docker-compose -f docker-compose.tcp.yml up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
### Quick Start
|
||||||
|
|
||||||
|
1. **Choose your configuration** based on your requirements
|
||||||
|
2. **Edit the environment variables** in the chosen compose file:
|
||||||
|
- `DOMAIN`: Your domain name (e.g., `example.com`)
|
||||||
|
- `ACME_EMAIL`: Your email for Let's Encrypt
|
||||||
|
- `CF_API_TOKEN`: Your Cloudflare API token (if using automatic TLS)
|
||||||
|
3. **Deploy:**
|
||||||
|
```bash
|
||||||
|
docker-compose -f docker-compose.root.yml up -d
|
||||||
|
```
|
||||||
|
4. **Check logs:**
|
||||||
|
```bash
|
||||||
|
docker-compose -f docker-compose.root.yml logs -f
|
||||||
|
```
|
||||||
|
5. **Stop the service:**
|
||||||
|
```bash
|
||||||
|
docker-compose -f docker-compose.root.yml down
|
||||||
|
```
|
||||||
|
|
||||||
|
### Volume Management
|
||||||
|
|
||||||
|
All configurations use a named volume `certs` for persistent storage:
|
||||||
|
- SSH keys: `/app/certs/ssh/`
|
||||||
|
- TLS certificates: `/app/certs/tls/`
|
||||||
|
|
||||||
|
To backup certificates:
|
||||||
|
```bash
|
||||||
|
docker run --rm -v tunnel_pls_certs:/data -v $(pwd):/backup alpine tar czf /backup/certs-backup.tar.gz -C /data .
|
||||||
|
```
|
||||||
|
|
||||||
|
To restore certificates:
|
||||||
|
```bash
|
||||||
|
docker run --rm -v tunnel_pls_certs:/data -v $(pwd):/backup alpine tar xzf /backup/certs-backup.tar.gz -C /data
|
||||||
|
```
|
||||||
|
|
||||||
|
### Recommendation
|
||||||
|
|
||||||
|
**Use `docker-compose.root.yml`** for production deployments if you need:
|
||||||
|
- Full TCP port forwarding capabilities
|
||||||
|
- Any port range configuration
|
||||||
|
- Direct port binding without mapping overhead
|
||||||
|
- Maximum performance and flexibility
|
||||||
|
|
||||||
|
This is the recommended configuration for most use cases as it provides the complete feature set without limitations.
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
Contributions are welcome!
|
Contributions are welcome!
|
||||||
|
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
-----BEGIN CERTIFICATE-----
|
|
||||||
MIIEWzCCA0OgAwIBAgIUDYTdEDHwVznxV/qnn0/WVlHNMZAwDQYJKoZIhvcNAQEL
|
|
||||||
BQAwgZYxCzAJBgNVBAYTAklEMQ8wDQYDVQQIDAZCYW50ZW4xGjAYBgNVBAcMEVRh
|
|
||||||
bmdlcmFuZyBTZWxhdGFuMRIwEAYDVQQKDAlGb3NzeSBMVFMxDjAMBgNVBAsMBUZv
|
|
||||||
c3N5MRQwEgYDVQQDDAtmb3NzeS5teS5pZDEgMB4GCSqGSIb3DQEJARYRYmFnYXNA
|
|
||||||
Zm9zc3kubXkuaWQwHhcNMjUwMjA3MTYyMTU1WhcNMjYwMjA3MTYyMTU1WjCBljEL
|
|
||||||
MAkGA1UEBhMCSUQxDzANBgNVBAgMBkJhbnRlbjEaMBgGA1UEBwwRVGFuZ2VyYW5n
|
|
||||||
IFNlbGF0YW4xEjAQBgNVBAoMCUZvc3N5IExUUzEOMAwGA1UECwwFRm9zc3kxFDAS
|
|
||||||
BgNVBAMMC2Zvc3N5Lm15LmlkMSAwHgYJKoZIhvcNAQkBFhFiYWdhc0Bmb3NzeS5t
|
|
||||||
eS5pZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMU3pCA0eP+VR2CA
|
|
||||||
+p3p0BgKw33xCKQmQx52jnqNbvwW4NMMDc3mS6a+wb6QB6v0WLGMI1g22TtJPatx
|
|
||||||
7dcy4PCSOCV9xJ2Yfq5HlQgDHYoyE+juy4/pGlMjo45thJ0yI8zOSzaz2esosP22
|
|
||||||
XkfFwj7oVMXXPIY6UovcAlGU+DFtwVrNa76/esUwJs+7M3tBubjkpcal0wXR+SIX
|
|
||||||
3fmw5v0YzKV8qLGMGOvX6+OyLQCx4r9gB0d+WOrT6EfrPfAzo07NKjzWG9aWl1rk
|
|
||||||
Q+h0i38hke2MTFxId7za57L+NlXRjLo3ESbBF0hjYquTQOG3jx/UvWs+I1NEfsdu
|
|
||||||
vj8beiMCAwEAAaOBnjCBmzAfBgNVHSMEGDAWgBT6nj69+I+GSdgScjfOqnVFzX7G
|
|
||||||
aTAJBgNVHRMEAjAAMAsGA1UdDwQEAwIFoDATBgNVHSUEDDAKBggrBgEFBQcDATAs
|
|
||||||
BgNVHREEJTAjgglsb2NhbGhvc3SCCTEyNy4wLjAuMYILKi5sb2NhbGhvc3QwHQYD
|
|
||||||
VR0OBBYEFD9ci20pAUTRLVWxvVXjfE2GKwSAMA0GCSqGSIb3DQEBCwUAA4IBAQAT
|
|
||||||
rkjU+GzQy9B3/nd/79N7ozK80ygzmnRnj3ou/bbqUHOIYQQgKM1cuN6zh57ovRh/
|
|
||||||
u45s6pZUOUVN59POFUCvqUiOgsDkY/auXGbKtzzqzoZABuvm85ySd6zurOOx8tA5
|
|
||||||
e7ArX1ppy3LgzSb+cXANvzYC9bCwp70w2YylMFhwHBAp5TXRVqsqG6jujD8GMLoS
|
|
||||||
zDSDx8M6o8gqWmPQve7Saim9mgLJUvvCYBzvowuNjzZrJfAeGoIfLV127xCQiylm
|
|
||||||
fzUQ1Ac6udldWm9scA32nteSQMWg2d1nW3RG1nRondp13WUkgGQ490/c97D3MKLt
|
|
||||||
D1HB5dLIYkRVHVhCKHT1
|
|
||||||
-----END CERTIFICATE-----
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
-----BEGIN CERTIFICATE-----
|
|
||||||
MIIDjDCCAnSgAwIBAgIUPpuvw4ZdpnDBbwOZBt2ALfZykuYwDQYJKoZIhvcNAQEL
|
|
||||||
BQAwPDELMAkGA1UEBhMCVUsxDzANBgNVBAgMBkxvbmRvbjEPMA0GA1UEBwwGTG9u
|
|
||||||
ZG9uMQswCQYDVQQLDAJIUTAeFw0yNDExMTkxNjQxNTNaFw0zNDExMTcxNjQxNTNa
|
|
||||||
MDwxCzAJBgNVBAYTAlVLMQ8wDQYDVQQIDAZMb25kb24xDzANBgNVBAcMBkxvbmRv
|
|
||||||
bjELMAkGA1UECwwCSFEwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDD
|
|
||||||
L/aeaBzgkYxDNiQq7+nt6tKDfGnPDfBXunJlr1xbZfIVpJeDqwrNLWaZ0gtHci5E
|
|
||||||
sHptIpu5/uTBFaFyZVH604etY/YHIsff7BT2y32OobJYiKy2lvAI3/IDR4TGeDgA
|
|
||||||
HKryOwMcB5DheBVdeggxj36m8OjxhVADaiKp7BNXE2eqUqk8f2QpwqLQYe9UaU9r
|
|
||||||
WSJllNHOFk+RH17YBDiyyQ8CD1Vf5HcVSmPItWOMQytHcSgy0DHVXQCde86mky8t
|
|
||||||
8Ik74GeNrM6f+vWR4OfQ8dU2WSyTUE4c7czagkToheMX5fbbzWJkJcd2SD9wvyIk
|
|
||||||
tOot0YiZGQAoOedtGSEnAgMBAAGjgYUwgYIwCQYDVR0TBAIwADALBgNVHQ8EBAMC
|
|
||||||
BeAwSQYDVR0RBEIwQIIQbG9jYWxob3N0LmRpcmVjdIISKi5sb2NhbGhvc3QuZGly
|
|
||||||
ZWN0ghhTUy5jZXJ0LmxvY2FsaG9zdC5kaXJlY3QwHQYDVR0OBBYEFKBVeirQGZ4D
|
|
||||||
4AKVPd7LGfCF1wEZMA0GCSqGSIb3DQEBCwUAA4IBAQCRpvsc5DrQBo8yATmUS0OK
|
|
||||||
xfUXfZR28u3xYY+qMHi+ngIVT2TKJ1yoBJezV6WwQLkcGdWacULvPYt3jCFUtaP7
|
|
||||||
+hzfs5y1FssFsXDx+r3pQxYyE6BX3BhlrJPJhLRyG1siTTgN439Qu40/TsTzNgAT
|
|
||||||
A9GbAro+W6+qA4H92mBlyfQEOBossID/Kk95uvDnQguUOp0ZBLgFNRfE6Ra9+yC+
|
|
||||||
ufAOksYDrisPE6kZId0Ra4Ln/GmrIXKjjmLCitq2q2REC/70JSCnaJcBYeThSJLZ
|
|
||||||
AZ24AF+JteakSJ8FEgRGxvSu0wdZfnMobNoelsjai1p5l5mCTiD8GH9sQCkslnp3
|
|
||||||
-----END CERTIFICATE-----
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
-----BEGIN PRIVATE KEY-----
|
|
||||||
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDDL/aeaBzgkYxD
|
|
||||||
NiQq7+nt6tKDfGnPDfBXunJlr1xbZfIVpJeDqwrNLWaZ0gtHci5EsHptIpu5/uTB
|
|
||||||
FaFyZVH604etY/YHIsff7BT2y32OobJYiKy2lvAI3/IDR4TGeDgAHKryOwMcB5Dh
|
|
||||||
eBVdeggxj36m8OjxhVADaiKp7BNXE2eqUqk8f2QpwqLQYe9UaU9rWSJllNHOFk+R
|
|
||||||
H17YBDiyyQ8CD1Vf5HcVSmPItWOMQytHcSgy0DHVXQCde86mky8t8Ik74GeNrM6f
|
|
||||||
+vWR4OfQ8dU2WSyTUE4c7czagkToheMX5fbbzWJkJcd2SD9wvyIktOot0YiZGQAo
|
|
||||||
OedtGSEnAgMBAAECggEASXibb2MnQ4zl7ELL+HGYb5sNpLrHJU5M4ujmuMn6jNjh
|
|
||||||
+C2dbs2KYlMtpMcAweMD8Y0weDYnwiplNx0KSYJECpNnJehTqrn33J0EAyXz3CWX
|
|
||||||
eWXxBUXpkp2hfoSEQSTth3VDD60Q7ZMXgRdvi2EtBmLKPNLADHGu/aoM5ENdwE9/
|
|
||||||
E80X68E+MT2czOY/sEI/w2Tf/S2ZVOHRvFOsmvTLFlbiElWG8pmguajpJwdae7uX
|
|
||||||
c5VD87b0oYicSUvaHe6xOCCyyeBVq06sWk+vh6Tzrw6K/B+q0SYvzXdrdsJxbzUD
|
|
||||||
PvhVi9rf9AC1Ncb6lFOP2ZjGfqYQvfgGXKqaVxXWQQKBgQD8frATynMSeR15oERa
|
|
||||||
+Maa7r3GlwWV3tkUblX7/FxBo9UhAsivnWZprccZR43YPowbWNeU3AQppf/YDxmx
|
|
||||||
70/RVTCMjXPGyspSS2iFtcp+K2KnIZA4BuAG/s1EBrAUW2KrtaTaXgYu9usgb+Fq
|
|
||||||
dJUEBDWrL59XXtQUwSM1laH0BwKBgQDF5Z31VY96xOS7flmolq8Ag+frSMH0G4np
|
|
||||||
3nUnTlUkgpFXE5FkmUccbYZv9QialAVriBBUNANPDBQH4PRrNnX8Z6B2HJbpAy30
|
|
||||||
II9jPMTNKnXT3RKFcJCamNkOQhT0sBAwm4gTsx+7gbpdZxinxH0Cr/08sfU4lbee
|
|
||||||
EtMV/J1h4QKBgC4MLLBvS20jCW0U/WJZ3F6FC7cb87jRW2WOeb/q1ihiaIwMpezh
|
|
||||||
F7xOJPFHS2cUgRi7qxVKyreNvor4tgbtTfEvSBtZ8LNgaGV5uyYncTZxUxyH0nVl
|
|
||||||
S5X7AhRV4+bSg7ws9FOesiH+hgL0ZHe1qzeATQlbNgQJF0RxtKohD9ghAoGAcM0N
|
|
||||||
WIZInoYUivreSEZ7wiNt0qNKSsZXukLfLGRuC72Q8r1opprn+cBEXRSirtmorT6F
|
|
||||||
cDmlmS0dTdBgAaytXA4FXM23B2KUkw7sLHi7BOcq+nSM1hrvke+F6aapI0AoOkyt
|
|
||||||
J+12LP8pJ4xYdWh+iUWfZzVYvcQ5QZUhVOsFGoECgYA9llKmc/cFaXrCr97g7ls8
|
|
||||||
ZWW1kQKLawAv6+90dwECJl+zpyQN/TURyEfz6oFJDbNEL0xAAcm9thDah177Tq95
|
|
||||||
pcHbVn/pAI4h4CLzM2ExSe8Ybvy6iPZaBiCVXq3ms1PK0EDyYLH1p3FZqm+UStZh
|
|
||||||
/6fYspyivrQEivRK3jGuWA==
|
|
||||||
-----END PRIVATE KEY-----
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
-----BEGIN PRIVATE KEY-----
|
|
||||||
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDFN6QgNHj/lUdg
|
|
||||||
gPqd6dAYCsN98QikJkMedo56jW78FuDTDA3N5kumvsG+kAer9FixjCNYNtk7ST2r
|
|
||||||
ce3XMuDwkjglfcSdmH6uR5UIAx2KMhPo7suP6RpTI6OObYSdMiPMzks2s9nrKLD9
|
|
||||||
tl5HxcI+6FTF1zyGOlKL3AJRlPgxbcFazWu+v3rFMCbPuzN7Qbm45KXGpdMF0fki
|
|
||||||
F935sOb9GMylfKixjBjr1+vjsi0AseK/YAdHfljq0+hH6z3wM6NOzSo81hvWlpda
|
|
||||||
5EPodIt/IZHtjExcSHe82uey/jZV0Yy6NxEmwRdIY2Krk0Dht48f1L1rPiNTRH7H
|
|
||||||
br4/G3ojAgMBAAECggEAATiJ23ZhS1++squ87jsgAfR+TYP8Kpv408u/43Ig5KgC
|
|
||||||
ZnwPUWqvP2e0TLwyhMKwXxHMt2nITxQlSnyCXU/5nk07BYxkqhiwEni4Xo86YK+H
|
|
||||||
rQXEnKFaSHdF6dNNIo+VZiark4adS9XgJp0fsn0LnON7GhBt72MvPW6auxH1HJIC
|
|
||||||
rcjnzJu/KzTVrId9QNsEDl9cNRlHXuPSfohdq2o39PBKGDeDEDTvP9wHtasEF6oe
|
|
||||||
uj1OH5fAxiXptT0Ln0Y8QeFe8R8Odul5mUXCgMOkKmHZPDxTq9ldCuPjrz15JfnS
|
|
||||||
T3MecaWdvChpIP2WybjLstJy9qXTl0fhRkpJWpaVbQKBgQDu9puq64vZn0jak7ns
|
|
||||||
bFbKPemFw7tiiwwnqyzqCTRddw1vETJN/MgpRWvc7d1Cvzs2+Z2kn0Cgpdw/30Ej
|
|
||||||
VRHiE/d1rj+u33F6vGBUBccRue1t0UdFMFF/YnlNDRRnljQgx4N3mezzNE2RikR8
|
|
||||||
jVp+jvWTSTgf3y4Hip1ffCSNtQKBgQDTRx1huSzUOed/36YLdnxP+AlV3L+c/EKU
|
|
||||||
GItKZk2bqRlqgs6wcsNlVjveb9o9j6M5pg/lh5HaqJyhj8DYVnEdmXfcQJiUfIPh
|
|
||||||
5802asIXQVBmL9rTMR34wsr/lzzK3k0KORqFcUdf2fDI/UQNpVUJvWQQY8fl2sVo
|
|
||||||
zmp88JAPdwKBgQCLC6fsvn5ztMF5nffTX/7oUzosgYXpgyshcfMCgzSbJgkFFaaF
|
|
||||||
xo7ZpPFsbmQO0KMuC/T0s02xrJEKAWgvnPJ48FFPgoK/yHiJiE8s1OfOordK7Tlh
|
|
||||||
QwpI6w3WDcRPuhC++hi/YSuFIGv6QdA0ATQk7B5tA2/K69wmuztzMhM6+QKBgH5E
|
|
||||||
LwQbRfZj0L20bKjHHA4y32loLz/j5upZLM2/DDyuN9lW6a28OJiUi90pHdXSxSsL
|
|
||||||
2s5DUmDKiiloH0lrh9i3wlFobYe4Tp0xCoyuCucZCrK3gODcptvnlqhfu15GsuYc
|
|
||||||
MIR1qcFYH7YO3qAFIiha/rVo3Ku7LmWvjyayInaLAoGAZ/dELEAcyhImf1HvtmBT
|
|
||||||
qNg4uI6t/2fvHdoAeQkkGjEDWFTGEaMp0cJEbETPD68TwNh5dL/xUJibTwMbK+m5
|
|
||||||
rdIA2oTZMkFtWubIvMCrD/J6E+8Pzl+eK+0C0axO/I29S4veORNGWvCtqdFICDgZ
|
|
||||||
CluJ0NZFeMH8g8tgfI9HnHc=
|
|
||||||
-----END PRIVATE KEY-----
|
|
||||||
37
docker-compose.root.yml
Normal file
37
docker-compose.root.yml
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
version: '3.8'
|
||||||
|
|
||||||
|
services:
|
||||||
|
tunnel-please:
|
||||||
|
image: git.fossy.my.id/bagas/tunnel-please:latest
|
||||||
|
container_name: tunnel-please-root
|
||||||
|
user: root
|
||||||
|
network_mode: host
|
||||||
|
restart: unless-stopped
|
||||||
|
volumes:
|
||||||
|
- certs:/app/certs
|
||||||
|
environment:
|
||||||
|
DOMAIN: example.com
|
||||||
|
PORT: 2200
|
||||||
|
HTTP_PORT: 8080
|
||||||
|
HTTPS_PORT: 8443
|
||||||
|
TLS_ENABLED: "true"
|
||||||
|
TLS_REDIRECT: "true"
|
||||||
|
ACME_EMAIL: admin@example.com
|
||||||
|
CF_API_TOKEN: your_cloudflare_api_token_here
|
||||||
|
ACME_STAGING: "false"
|
||||||
|
CORS_LIST: http://localhost:3000,https://example.com
|
||||||
|
ALLOWED_PORTS: 40000-41000
|
||||||
|
BUFFER_SIZE: 32768
|
||||||
|
PPROF_ENABLED: "false"
|
||||||
|
PPROF_PORT: 6060
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "/bin/sh", "-c", "netstat -tln | grep -q :2200"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 10s
|
||||||
|
retries: 3
|
||||||
|
start_period: 10s
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
certs:
|
||||||
|
driver: local
|
||||||
|
|
||||||
39
docker-compose.standard.yml
Normal file
39
docker-compose.standard.yml
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
version: '3.8'
|
||||||
|
|
||||||
|
services:
|
||||||
|
tunnel-please:
|
||||||
|
image: git.fossy.my.id/bagas/tunnel-please:latest
|
||||||
|
container_name: tunnel-please-standard
|
||||||
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- "2200:2200"
|
||||||
|
- "80:8080"
|
||||||
|
- "443:8443"
|
||||||
|
volumes:
|
||||||
|
- certs:/app/certs
|
||||||
|
environment:
|
||||||
|
DOMAIN: example.com
|
||||||
|
PORT: 2200
|
||||||
|
HTTP_PORT: 8080
|
||||||
|
HTTPS_PORT: 8443
|
||||||
|
TLS_ENABLED: "true"
|
||||||
|
TLS_REDIRECT: "true"
|
||||||
|
ACME_EMAIL: admin@example.com
|
||||||
|
CF_API_TOKEN: your_cloudflare_api_token_here
|
||||||
|
ACME_STAGING: "false"
|
||||||
|
CORS_LIST: http://localhost:3000,https://example.com
|
||||||
|
ALLOWED_PORTS: none
|
||||||
|
BUFFER_SIZE: 32768
|
||||||
|
PPROF_ENABLED: "false"
|
||||||
|
PPROF_PORT: 6060
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "/bin/sh", "-c", "netstat -tln | grep -q :2200"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 10s
|
||||||
|
retries: 3
|
||||||
|
start_period: 10s
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
certs:
|
||||||
|
driver: local
|
||||||
|
|
||||||
40
docker-compose.tcp.yml
Normal file
40
docker-compose.tcp.yml
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
version: '3.8'
|
||||||
|
|
||||||
|
services:
|
||||||
|
tunnel-please:
|
||||||
|
image: git.fossy.my.id/bagas/tunnel-please:latest
|
||||||
|
container_name: tunnel-please-tcp
|
||||||
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- "2200:2200"
|
||||||
|
- "80:8080"
|
||||||
|
- "443:8443"
|
||||||
|
- "30000-31000:30000-31000"
|
||||||
|
volumes:
|
||||||
|
- certs:/app/certs
|
||||||
|
environment:
|
||||||
|
DOMAIN: example.com
|
||||||
|
PORT: 2200
|
||||||
|
HTTP_PORT: 8080
|
||||||
|
HTTPS_PORT: 8443
|
||||||
|
TLS_ENABLED: "true"
|
||||||
|
TLS_REDIRECT: "true"
|
||||||
|
ACME_EMAIL: admin@example.com
|
||||||
|
CF_API_TOKEN: your_cloudflare_api_token_here
|
||||||
|
ACME_STAGING: "false"
|
||||||
|
CORS_LIST: http://localhost:3000,https://example.com
|
||||||
|
ALLOWED_PORTS: 30000-31000
|
||||||
|
BUFFER_SIZE: 32768
|
||||||
|
PPROF_ENABLED: "false"
|
||||||
|
PPROF_PORT: 6060
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "/bin/sh", "-c", "netstat -tln | grep -q :2200"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 10s
|
||||||
|
retries: 3
|
||||||
|
start_period: 10s
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
certs:
|
||||||
|
driver: local
|
||||||
|
|
||||||
42
go.mod
42
go.mod
@@ -3,8 +3,46 @@ module tunnel_pls
|
|||||||
go 1.24.4
|
go 1.24.4
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/caddyserver/certmagic v0.25.0
|
||||||
|
github.com/charmbracelet/bubbles v0.21.0
|
||||||
|
github.com/charmbracelet/bubbletea v1.3.10
|
||||||
github.com/joho/godotenv v1.5.1
|
github.com/joho/godotenv v1.5.1
|
||||||
golang.org/x/crypto v0.45.0
|
github.com/libdns/cloudflare v0.2.2
|
||||||
|
golang.org/x/crypto v0.46.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require golang.org/x/sys v0.38.0 // indirect
|
require (
|
||||||
|
github.com/atotto/clipboard v0.1.4 // indirect
|
||||||
|
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
||||||
|
github.com/caddyserver/zerossl v0.1.3 // indirect
|
||||||
|
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc // indirect
|
||||||
|
github.com/charmbracelet/lipgloss v1.1.0 // indirect
|
||||||
|
github.com/charmbracelet/x/ansi v0.10.1 // indirect
|
||||||
|
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect
|
||||||
|
github.com/charmbracelet/x/term v0.2.1 // indirect
|
||||||
|
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
|
||||||
|
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
|
||||||
|
github.com/libdns/libdns v1.1.1 // indirect
|
||||||
|
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
github.com/mattn/go-localereader v0.0.1 // indirect
|
||||||
|
github.com/mattn/go-runewidth v0.0.16 // indirect
|
||||||
|
github.com/mholt/acmez/v3 v3.1.3 // indirect
|
||||||
|
github.com/miekg/dns v1.1.68 // indirect
|
||||||
|
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
|
||||||
|
github.com/muesli/cancelreader v0.2.2 // indirect
|
||||||
|
github.com/muesli/termenv v0.16.0 // indirect
|
||||||
|
github.com/rivo/uniseg v0.4.7 // indirect
|
||||||
|
github.com/sahilm/fuzzy v0.1.1 // indirect
|
||||||
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
||||||
|
github.com/zeebo/blake3 v0.2.4 // indirect
|
||||||
|
go.uber.org/multierr v1.11.0 // indirect
|
||||||
|
go.uber.org/zap v1.27.0 // indirect
|
||||||
|
go.uber.org/zap/exp v0.3.0 // indirect
|
||||||
|
golang.org/x/mod v0.30.0 // indirect
|
||||||
|
golang.org/x/net v0.47.0 // indirect
|
||||||
|
golang.org/x/sync v0.19.0 // indirect
|
||||||
|
golang.org/x/sys v0.39.0 // indirect
|
||||||
|
golang.org/x/text v0.32.0 // indirect
|
||||||
|
golang.org/x/tools v0.39.0 // indirect
|
||||||
|
)
|
||||||
|
|||||||
116
go.sum
116
go.sum
@@ -1,13 +1,107 @@
|
|||||||
|
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
|
||||||
|
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
|
||||||
|
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
|
||||||
|
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
|
||||||
|
github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWpi6yML8=
|
||||||
|
github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA=
|
||||||
|
github.com/caddyserver/certmagic v0.25.0 h1:VMleO/XA48gEWes5l+Fh6tRWo9bHkhwAEhx63i+F5ic=
|
||||||
|
github.com/caddyserver/certmagic v0.25.0/go.mod h1:m9yB7Mud24OQbPHOiipAoyKPn9pKHhpSJxXR1jydBxA=
|
||||||
|
github.com/caddyserver/zerossl v0.1.3 h1:onS+pxp3M8HnHpN5MMbOMyNjmTheJyWRaZYwn+YTAyA=
|
||||||
|
github.com/caddyserver/zerossl v0.1.3/go.mod h1:CxA0acn7oEGO6//4rtrRjYgEoa4MFw/XofZnrYwGqG4=
|
||||||
|
github.com/charmbracelet/bubbles v0.21.0 h1:9TdC97SdRVg/1aaXNVWfFH3nnLAwOXr8Fn6u6mfQdFs=
|
||||||
|
github.com/charmbracelet/bubbles v0.21.0/go.mod h1:HF+v6QUR4HkEpz62dx7ym2xc71/KBHg+zKwJtMw+qtg=
|
||||||
|
github.com/charmbracelet/bubbletea v1.3.10 h1:otUDHWMMzQSB0Pkc87rm691KZ3SWa4KUlvF9nRvCICw=
|
||||||
|
github.com/charmbracelet/bubbletea v1.3.10/go.mod h1:ORQfo0fk8U+po9VaNvnV95UPWA1BitP1E0N6xJPlHr4=
|
||||||
|
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc h1:4pZI35227imm7yK2bGPcfpFEmuY1gc2YSTShr4iJBfs=
|
||||||
|
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc/go.mod h1:X4/0JoqgTIPSFcRA/P6INZzIuyqdFY5rm8tb41s9okk=
|
||||||
|
github.com/charmbracelet/lipgloss v1.1.0 h1:vYXsiLHVkK7fp74RkV7b2kq9+zDLoEU4MZoFqR/noCY=
|
||||||
|
github.com/charmbracelet/lipgloss v1.1.0/go.mod h1:/6Q8FR2o+kj8rz4Dq0zQc3vYf7X+B0binUUBwA0aL30=
|
||||||
|
github.com/charmbracelet/x/ansi v0.10.1 h1:rL3Koar5XvX0pHGfovN03f5cxLbCF2YvLeyz7D2jVDQ=
|
||||||
|
github.com/charmbracelet/x/ansi v0.10.1/go.mod h1:3RQDQ6lDnROptfpWuUVIUG64bD2g2BgntdxH0Ya5TeE=
|
||||||
|
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd h1:vy0GVL4jeHEwG5YOXDmi86oYw2yuYUGqz6a8sLwg0X8=
|
||||||
|
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd/go.mod h1:xe0nKWGd3eJgtqZRaN9RjMtK7xUYchjzPr7q6kcvCCs=
|
||||||
|
github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91 h1:payRxjMjKgx2PaCWLZ4p3ro9y97+TVLZNaRZgJwSVDQ=
|
||||||
|
github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=
|
||||||
|
github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ=
|
||||||
|
github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
|
||||||
|
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
|
||||||
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||||
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
|
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
|
||||||
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
|
github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
||||||
golang.org/x/crypto v0.45.0 h1:jMBrvKuj23MTlT0bQEOBcAE0mjg8mK9RXFhRH6nyF3Q=
|
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||||
golang.org/x/crypto v0.45.0/go.mod h1:XTGrrkGJve7CYK7J8PEww4aY7gM3qMCElcJQ8n8JdX4=
|
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||||
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
|
github.com/libdns/cloudflare v0.2.2 h1:XWHv+C1dDcApqazlh08Q6pjytYLgR2a+Y3xrXFu0vsI=
|
||||||
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
github.com/libdns/cloudflare v0.2.2/go.mod h1:w9uTmRCDlAoafAsTPnn2nJ0XHK/eaUMh86DUk8BWi60=
|
||||||
golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
|
github.com/libdns/libdns v1.1.1 h1:wPrHrXILoSHKWJKGd0EiAVmiJbFShguILTg9leS/P/U=
|
||||||
golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
github.com/libdns/libdns v1.1.1/go.mod h1:4Bj9+5CQiNMVGf87wjX4CY3HQJypUHRuLvlsfsZqLWQ=
|
||||||
golang.org/x/term v0.28.0 h1:/Ts8HFuMR2E6IP/jlo7QVLZHggjKQbhu/7H0LJFr3Gg=
|
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||||
golang.org/x/term v0.28.0/go.mod h1:Sw/lC2IAUZ92udQNf3WodGtn4k/XoLyZoh8v/8uiwek=
|
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||||
golang.org/x/term v0.37.0 h1:8EGAD0qCmHYZg6J17DvsMy9/wJ7/D/4pV/wfnld5lTU=
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
|
||||||
|
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
|
||||||
|
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
|
||||||
|
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||||
|
github.com/mholt/acmez/v3 v3.1.3 h1:gUl789rjbJSuM5hYzOFnNaGgWPV1xVfnOs59o0dZEcc=
|
||||||
|
github.com/mholt/acmez/v3 v3.1.3/go.mod h1:L1wOU06KKvq7tswuMDwKdcHeKpFFgkppZy/y0DFxagQ=
|
||||||
|
github.com/miekg/dns v1.1.68 h1:jsSRkNozw7G/mnmXULynzMNIsgY2dHC8LO6U6Ij2JEA=
|
||||||
|
github.com/miekg/dns v1.1.68/go.mod h1:fujopn7TB3Pu3JM69XaawiU0wqjpL9/8xGop5UrTPps=
|
||||||
|
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=
|
||||||
|
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=
|
||||||
|
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
|
||||||
|
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
|
||||||
|
github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
|
||||||
|
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
|
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||||
|
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||||
|
github.com/sahilm/fuzzy v0.1.1 h1:ceu5RHF8DGgoi+/dR5PsECjCDH1BE3Fnmpo7aVXOdRA=
|
||||||
|
github.com/sahilm/fuzzy v0.1.1/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
|
||||||
|
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||||
|
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||||
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
|
||||||
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
|
||||||
|
github.com/zeebo/assert v1.1.0 h1:hU1L1vLTHsnO8x8c9KAR5GmM5QscxHg5RNU5z5qbUWY=
|
||||||
|
github.com/zeebo/assert v1.1.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
|
||||||
|
github.com/zeebo/blake3 v0.2.4 h1:KYQPkhpRtcqh0ssGYcKLG1JYvddkEA8QwCM/yBqhaZI=
|
||||||
|
github.com/zeebo/blake3 v0.2.4/go.mod h1:7eeQ6d2iXWRGF6npfaxl2CU+xy2Fjo2gxeyZGCRUjcE=
|
||||||
|
github.com/zeebo/pcg v1.0.1 h1:lyqfGeWiv4ahac6ttHs+I5hwtH/+1mrhlCtVNQM2kHo=
|
||||||
|
github.com/zeebo/pcg v1.0.1/go.mod h1:09F0S9iiKrwn9rlI5yjLkmrug154/YRW6KnnXVDM/l4=
|
||||||
|
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||||
|
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||||
|
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
|
||||||
|
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||||
|
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
|
||||||
|
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
|
||||||
|
go.uber.org/zap/exp v0.3.0 h1:6JYzdifzYkGmTdRR59oYH+Ng7k49H9qVpWwNSsGJj3U=
|
||||||
|
go.uber.org/zap/exp v0.3.0/go.mod h1:5I384qq7XGxYyByIhHm6jg5CHkGY0nsTfbDLgDDlgJQ=
|
||||||
|
golang.org/x/crypto v0.46.0 h1:cKRW/pmt1pKAfetfu+RCEvjvZkA9RimPbh7bhFjGVBU=
|
||||||
|
golang.org/x/crypto v0.46.0/go.mod h1:Evb/oLKmMraqjZ2iQTwDwvCtJkczlDuTmdJXoZVzqU0=
|
||||||
|
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
|
||||||
|
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
|
||||||
|
golang.org/x/mod v0.30.0 h1:fDEXFVZ/fmCKProc/yAXXUijritrDzahmwwefnjoPFk=
|
||||||
|
golang.org/x/mod v0.30.0/go.mod h1:lAsf5O2EvJeSFMiBxXDki7sCgAxEUcZHXoXMKT4GJKc=
|
||||||
|
golang.org/x/net v0.47.0 h1:Mx+4dIFzqraBXUugkia1OOvlD6LemFo1ALMHjrXDOhY=
|
||||||
|
golang.org/x/net v0.47.0/go.mod h1:/jNxtkgq5yWUGYkaZGqo27cfGZ1c5Nen03aYrrKpVRU=
|
||||||
|
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
|
||||||
|
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
||||||
|
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
|
||||||
|
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||||
|
golang.org/x/term v0.38.0 h1:PQ5pkm/rLO6HnxFR7N2lJHOZX6Kez5Y1gDSJla6jo7Q=
|
||||||
|
golang.org/x/term v0.38.0/go.mod h1:bSEAKrOT1W+VSu9TSCMtoGEOUcKxOKgl3LE5QEF/xVg=
|
||||||
|
golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU=
|
||||||
|
golang.org/x/text v0.32.0/go.mod h1:o/rUWzghvpD5TXrTIBuJU77MTaN0ljMWE47kxGJQ7jY=
|
||||||
|
golang.org/x/tools v0.39.0 h1:ik4ho21kwuQln40uelmciQPp9SipgNDdrafrYA4TmQQ=
|
||||||
|
golang.org/x/tools v0.39.0/go.mod h1:JnefbkDPyD8UU2kI5fuf8ZX4/yUeh9W877ZeBONxUqQ=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
@@ -9,36 +9,47 @@ import (
|
|||||||
"tunnel_pls/utils"
|
"tunnel_pls/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PortManager struct {
|
type Manager interface {
|
||||||
|
AddPortRange(startPort, endPort uint16) error
|
||||||
|
GetUnassignedPort() (uint16, bool)
|
||||||
|
SetPortStatus(port uint16, assigned bool) error
|
||||||
|
GetPortStatus(port uint16) (bool, bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
type manager struct {
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
ports map[uint16]bool
|
ports map[uint16]bool
|
||||||
sortedPorts []uint16
|
sortedPorts []uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
var Manager = PortManager{
|
var Default Manager = &manager{
|
||||||
ports: make(map[uint16]bool),
|
ports: make(map[uint16]bool),
|
||||||
sortedPorts: []uint16{},
|
sortedPorts: []uint16{},
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rawRange := utils.Getenv("ALLOWED_PORTS")
|
rawRange := utils.Getenv("ALLOWED_PORTS", "")
|
||||||
|
if rawRange == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
splitRange := strings.Split(rawRange, "-")
|
splitRange := strings.Split(rawRange, "-")
|
||||||
if len(splitRange) != 2 {
|
if len(splitRange) != 2 {
|
||||||
Manager.AddPortRange(30000, 31000)
|
return
|
||||||
} else {
|
|
||||||
start, err := strconv.ParseUint(splitRange[0], 10, 16)
|
|
||||||
if err != nil {
|
|
||||||
start = 30000
|
|
||||||
}
|
|
||||||
end, err := strconv.ParseUint(splitRange[1], 10, 16)
|
|
||||||
if err != nil {
|
|
||||||
end = 31000
|
|
||||||
}
|
|
||||||
Manager.AddPortRange(uint16(start), uint16(end))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
start, err := strconv.ParseUint(splitRange[0], 10, 16)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
end, err := strconv.ParseUint(splitRange[1], 10, 16)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = Default.AddPortRange(uint16(start), uint16(end))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pm *PortManager) AddPortRange(startPort, endPort uint16) error {
|
func (pm *manager) AddPortRange(startPort, endPort uint16) error {
|
||||||
pm.mu.Lock()
|
pm.mu.Lock()
|
||||||
defer pm.mu.Unlock()
|
defer pm.mu.Unlock()
|
||||||
|
|
||||||
@@ -57,7 +68,7 @@ func (pm *PortManager) AddPortRange(startPort, endPort uint16) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pm *PortManager) GetUnassignedPort() (uint16, bool) {
|
func (pm *manager) GetUnassignedPort() (uint16, bool) {
|
||||||
pm.mu.Lock()
|
pm.mu.Lock()
|
||||||
defer pm.mu.Unlock()
|
defer pm.mu.Unlock()
|
||||||
|
|
||||||
@@ -70,7 +81,7 @@ func (pm *PortManager) GetUnassignedPort() (uint16, bool) {
|
|||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pm *PortManager) SetPortStatus(port uint16, assigned bool) error {
|
func (pm *manager) SetPortStatus(port uint16, assigned bool) error {
|
||||||
pm.mu.Lock()
|
pm.mu.Lock()
|
||||||
defer pm.mu.Unlock()
|
defer pm.mu.Unlock()
|
||||||
|
|
||||||
@@ -78,7 +89,7 @@ func (pm *PortManager) SetPortStatus(port uint16, assigned bool) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pm *PortManager) GetPortStatus(port uint16) (bool, bool) {
|
func (pm *manager) GetPortStatus(port uint16) (bool, bool) {
|
||||||
pm.mu.RLock()
|
pm.mu.RLock()
|
||||||
defer pm.mu.RUnlock()
|
defer pm.mu.RUnlock()
|
||||||
|
|
||||||
|
|||||||
36
main.go
36
main.go
@@ -1,24 +1,52 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
|
_ "net/http/pprof"
|
||||||
"os"
|
"os"
|
||||||
"tunnel_pls/server"
|
"tunnel_pls/server"
|
||||||
"tunnel_pls/utils"
|
"tunnel_pls/utils"
|
||||||
|
"tunnel_pls/version"
|
||||||
|
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
if len(os.Args) > 1 && (os.Args[1] == "--version" || os.Args[1] == "-v") {
|
||||||
|
fmt.Println(version.GetVersion())
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
log.SetOutput(os.Stdout)
|
log.SetOutput(os.Stdout)
|
||||||
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||||
|
|
||||||
sshConfig := &ssh.ServerConfig{
|
log.Printf("Starting %s", version.GetVersion())
|
||||||
NoClientAuth: true,
|
|
||||||
ServerVersion: "SSH-2.0-TunnlPls-1.0",
|
pprofEnabled := utils.Getenv("PPROF_ENABLED", "false")
|
||||||
|
if pprofEnabled == "true" {
|
||||||
|
pprofPort := utils.Getenv("PPROF_PORT", "6060")
|
||||||
|
go func() {
|
||||||
|
pprofAddr := fmt.Sprintf("localhost:%s", pprofPort)
|
||||||
|
log.Printf("Starting pprof server on http://%s/debug/pprof/", pprofAddr)
|
||||||
|
if err := http.ListenAndServe(pprofAddr, nil); err != nil {
|
||||||
|
log.Printf("pprof server error: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
privateBytes, err := os.ReadFile(utils.Getenv("ssh_private_key"))
|
sshConfig := &ssh.ServerConfig{
|
||||||
|
NoClientAuth: true,
|
||||||
|
ServerVersion: fmt.Sprintf("SSH-2.0-TunnlPls-%s", version.GetShortVersion()),
|
||||||
|
}
|
||||||
|
|
||||||
|
sshKeyPath := "certs/ssh/id_rsa"
|
||||||
|
if err := utils.GenerateSSHKeyIfNotExist(sshKeyPath); err != nil {
|
||||||
|
log.Fatalf("Failed to generate SSH key: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
privateBytes, err := os.ReadFile(sshKeyPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to load private key: %s", err)
|
log.Fatalf("Failed to load private key: %s", err)
|
||||||
}
|
}
|
||||||
|
|||||||
8
renovate-config.js
Normal file
8
renovate-config.js
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
module.exports = {
|
||||||
|
"endpoint": "https://git.fossy.my.id/api/v1",
|
||||||
|
"gitAuthor": "Renovate-Clanker <renovate-bot@fossy.my.id>",
|
||||||
|
"platform": "gitea",
|
||||||
|
"onboardingConfigFileName": "renovate.json",
|
||||||
|
"autodiscover": true,
|
||||||
|
"optimizeForDisabled": true,
|
||||||
|
};
|
||||||
19
renovate.json
Normal file
19
renovate.json
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"extends": [
|
||||||
|
"config:recommended"
|
||||||
|
],
|
||||||
|
"packageRules": [
|
||||||
|
{
|
||||||
|
"matchUpdateTypes": [
|
||||||
|
"minor",
|
||||||
|
"patch",
|
||||||
|
"pin",
|
||||||
|
"digest"
|
||||||
|
],
|
||||||
|
"automerge": true,
|
||||||
|
"baseBranchPatterns": [
|
||||||
|
"staging"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (s *Server) handleConnection(conn net.Conn) {
|
func (s *Server) handleConnection(conn net.Conn) {
|
||||||
sshConn, chans, forwardingReqs, err := ssh.NewServerConn(conn, s.Config)
|
sshConn, chans, forwardingReqs, err := ssh.NewServerConn(conn, s.config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to establish SSH connection: %v", err)
|
log.Printf("failed to establish SSH connection: %v", err)
|
||||||
err := conn.Close()
|
err := conn.Close()
|
||||||
|
|||||||
@@ -14,21 +14,38 @@ type HeaderManager interface {
|
|||||||
Finalize() []byte
|
Finalize() []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResponseHeaderFactory struct {
|
type ResponseHeaderManager interface {
|
||||||
|
Get(key string) string
|
||||||
|
Set(key string, value string)
|
||||||
|
Remove(key string)
|
||||||
|
Finalize() []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
type RequestHeaderManager interface {
|
||||||
|
Get(key string) string
|
||||||
|
Set(key string, value string)
|
||||||
|
Remove(key string)
|
||||||
|
Finalize() []byte
|
||||||
|
GetMethod() string
|
||||||
|
GetPath() string
|
||||||
|
GetVersion() string
|
||||||
|
}
|
||||||
|
|
||||||
|
type responseHeaderFactory struct {
|
||||||
startLine []byte
|
startLine []byte
|
||||||
headers map[string]string
|
headers map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
type RequestHeaderFactory struct {
|
type requestHeaderFactory struct {
|
||||||
Method string
|
method string
|
||||||
Path string
|
path string
|
||||||
Version string
|
version string
|
||||||
startLine []byte
|
startLine []byte
|
||||||
headers map[string]string
|
headers map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRequestHeaderFactory(br *bufio.Reader) (*RequestHeaderFactory, error) {
|
func NewRequestHeaderFactory(br *bufio.Reader) (RequestHeaderManager, error) {
|
||||||
header := &RequestHeaderFactory{
|
header := &requestHeaderFactory{
|
||||||
headers: make(map[string]string),
|
headers: make(map[string]string),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,9 +61,9 @@ func NewRequestHeaderFactory(br *bufio.Reader) (*RequestHeaderFactory, error) {
|
|||||||
return nil, fmt.Errorf("invalid request line")
|
return nil, fmt.Errorf("invalid request line")
|
||||||
}
|
}
|
||||||
|
|
||||||
header.Method = parts[0]
|
header.method = parts[0]
|
||||||
header.Path = parts[1]
|
header.path = parts[1]
|
||||||
header.Version = parts[2]
|
header.version = parts[2]
|
||||||
|
|
||||||
for {
|
for {
|
||||||
line, err := br.ReadString('\n')
|
line, err := br.ReadString('\n')
|
||||||
@@ -69,8 +86,8 @@ func NewRequestHeaderFactory(br *bufio.Reader) (*RequestHeaderFactory, error) {
|
|||||||
return header, nil
|
return header, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewResponseHeaderFactory(startLine []byte) *ResponseHeaderFactory {
|
func NewResponseHeaderFactory(startLine []byte) ResponseHeaderManager {
|
||||||
header := &ResponseHeaderFactory{
|
header := &responseHeaderFactory{
|
||||||
startLine: nil,
|
startLine: nil,
|
||||||
headers: make(map[string]string),
|
headers: make(map[string]string),
|
||||||
}
|
}
|
||||||
@@ -96,19 +113,19 @@ func NewResponseHeaderFactory(startLine []byte) *ResponseHeaderFactory {
|
|||||||
return header
|
return header
|
||||||
}
|
}
|
||||||
|
|
||||||
func (resp *ResponseHeaderFactory) Get(key string) string {
|
func (resp *responseHeaderFactory) Get(key string) string {
|
||||||
return resp.headers[key]
|
return resp.headers[key]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (resp *ResponseHeaderFactory) Set(key string, value string) {
|
func (resp *responseHeaderFactory) Set(key string, value string) {
|
||||||
resp.headers[key] = value
|
resp.headers[key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (resp *ResponseHeaderFactory) Remove(key string) {
|
func (resp *responseHeaderFactory) Remove(key string) {
|
||||||
delete(resp.headers, key)
|
delete(resp.headers, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (resp *ResponseHeaderFactory) Finalize() []byte {
|
func (resp *responseHeaderFactory) Finalize() []byte {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
|
||||||
buf.Write(resp.startLine)
|
buf.Write(resp.startLine)
|
||||||
@@ -125,7 +142,7 @@ func (resp *ResponseHeaderFactory) Finalize() []byte {
|
|||||||
return buf.Bytes()
|
return buf.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (req *RequestHeaderFactory) Get(key string) string {
|
func (req *requestHeaderFactory) Get(key string) string {
|
||||||
val, ok := req.headers[key]
|
val, ok := req.headers[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
return ""
|
return ""
|
||||||
@@ -133,15 +150,27 @@ func (req *RequestHeaderFactory) Get(key string) string {
|
|||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
func (req *RequestHeaderFactory) Set(key string, value string) {
|
func (req *requestHeaderFactory) Set(key string, value string) {
|
||||||
req.headers[key] = value
|
req.headers[key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (req *RequestHeaderFactory) Remove(key string) {
|
func (req *requestHeaderFactory) Remove(key string) {
|
||||||
delete(req.headers, key)
|
delete(req.headers, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (req *RequestHeaderFactory) Finalize() []byte {
|
func (req *requestHeaderFactory) GetMethod() string {
|
||||||
|
return req.method
|
||||||
|
}
|
||||||
|
|
||||||
|
func (req *requestHeaderFactory) GetPath() string {
|
||||||
|
return req.path
|
||||||
|
}
|
||||||
|
|
||||||
|
func (req *requestHeaderFactory) GetVersion() string {
|
||||||
|
return req.version
|
||||||
|
}
|
||||||
|
|
||||||
|
func (req *requestHeaderFactory) Finalize() []byte {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
|
||||||
buf.Write(req.startLine)
|
buf.Write(req.startLine)
|
||||||
|
|||||||
134
server/http.go
134
server/http.go
@@ -10,32 +10,73 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
"tunnel_pls/session"
|
"tunnel_pls/session"
|
||||||
"tunnel_pls/utils"
|
"tunnel_pls/utils"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Interaction interface {
|
type Interaction interface {
|
||||||
SendMessage(message string)
|
SendMessage(message string)
|
||||||
}
|
}
|
||||||
type CustomWriter struct {
|
|
||||||
RemoteAddr net.Addr
|
type HTTPWriter interface {
|
||||||
|
io.Reader
|
||||||
|
io.Writer
|
||||||
|
SetInteraction(interaction Interaction)
|
||||||
|
AddInteraction(interaction Interaction)
|
||||||
|
GetRemoteAddr() net.Addr
|
||||||
|
GetWriter() io.Writer
|
||||||
|
AddResponseMiddleware(mw ResponseMiddleware)
|
||||||
|
AddRequestStartMiddleware(mw RequestMiddleware)
|
||||||
|
SetRequestHeader(header RequestHeaderManager)
|
||||||
|
GetRequestStartMiddleware() []RequestMiddleware
|
||||||
|
}
|
||||||
|
|
||||||
|
type customWriter struct {
|
||||||
|
remoteAddr net.Addr
|
||||||
writer io.Writer
|
writer io.Writer
|
||||||
reader io.Reader
|
reader io.Reader
|
||||||
headerBuf []byte
|
headerBuf []byte
|
||||||
buf []byte
|
buf []byte
|
||||||
respHeader *ResponseHeaderFactory
|
respHeader ResponseHeaderManager
|
||||||
reqHeader *RequestHeaderFactory
|
reqHeader RequestHeaderManager
|
||||||
interaction Interaction
|
interaction Interaction
|
||||||
respMW []ResponseMiddleware
|
respMW []ResponseMiddleware
|
||||||
reqStartMW []RequestMiddleware
|
reqStartMW []RequestMiddleware
|
||||||
reqEndMW []RequestMiddleware
|
reqEndMW []RequestMiddleware
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cw *CustomWriter) SetInteraction(interaction Interaction) {
|
func (cw *customWriter) SetInteraction(interaction Interaction) {
|
||||||
cw.interaction = interaction
|
cw.interaction = interaction
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cw *CustomWriter) Read(p []byte) (int, error) {
|
func (cw *customWriter) GetRemoteAddr() net.Addr {
|
||||||
|
return cw.remoteAddr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cw *customWriter) GetWriter() io.Writer {
|
||||||
|
return cw.writer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cw *customWriter) AddResponseMiddleware(mw ResponseMiddleware) {
|
||||||
|
cw.respMW = append(cw.respMW, mw)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cw *customWriter) AddRequestStartMiddleware(mw RequestMiddleware) {
|
||||||
|
cw.reqStartMW = append(cw.reqStartMW, mw)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cw *customWriter) SetRequestHeader(header RequestHeaderManager) {
|
||||||
|
cw.reqHeader = header
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cw *customWriter) GetRequestStartMiddleware() []RequestMiddleware {
|
||||||
|
return cw.reqStartMW
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cw *customWriter) Read(p []byte) (int, error) {
|
||||||
tmp := make([]byte, len(p))
|
tmp := make([]byte, len(p))
|
||||||
read, err := cw.reader.Read(tmp)
|
read, err := cw.reader.Read(tmp)
|
||||||
if read == 0 && err != nil {
|
if read == 0 && err != nil {
|
||||||
@@ -92,9 +133,9 @@ func (cw *CustomWriter) Read(p []byte) (int, error) {
|
|||||||
return n, nil
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCustomWriter(writer io.Writer, reader io.Reader, remoteAddr net.Addr) *CustomWriter {
|
func NewCustomWriter(writer io.Writer, reader io.Reader, remoteAddr net.Addr) HTTPWriter {
|
||||||
return &CustomWriter{
|
return &customWriter{
|
||||||
RemoteAddr: remoteAddr,
|
remoteAddr: remoteAddr,
|
||||||
writer: writer,
|
writer: writer,
|
||||||
reader: reader,
|
reader: reader,
|
||||||
buf: make([]byte, 0, 4096),
|
buf: make([]byte, 0, 4096),
|
||||||
@@ -126,7 +167,7 @@ func isHTTPHeader(buf []byte) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cw *CustomWriter) Write(p []byte) (int, error) {
|
func (cw *customWriter) Write(p []byte) (int, error) {
|
||||||
if cw.respHeader != nil && len(cw.buf) == 0 && len(p) >= 5 && string(p[0:5]) == "HTTP/" {
|
if cw.respHeader != nil && len(cw.buf) == 0 && len(p) >= 5 && string(p[0:5]) == "HTTP/" {
|
||||||
cw.respHeader = nil
|
cw.respHeader = nil
|
||||||
}
|
}
|
||||||
@@ -183,18 +224,19 @@ func (cw *CustomWriter) Write(p []byte) (int, error) {
|
|||||||
return len(p), nil
|
return len(p), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cw *CustomWriter) AddInteraction(interaction Interaction) {
|
func (cw *customWriter) AddInteraction(interaction Interaction) {
|
||||||
cw.interaction = interaction
|
cw.interaction = interaction
|
||||||
}
|
}
|
||||||
|
|
||||||
var redirectTLS = false
|
var redirectTLS = false
|
||||||
|
|
||||||
func NewHTTPServer() error {
|
func NewHTTPServer() error {
|
||||||
listener, err := net.Listen("tcp", ":80")
|
httpPort := utils.Getenv("HTTP_PORT", "8080")
|
||||||
|
listener, err := net.Listen("tcp", ":"+httpPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("Error listening: " + err.Error())
|
return errors.New("Error listening: " + err.Error())
|
||||||
}
|
}
|
||||||
if utils.Getenv("tls_enabled") == "true" && utils.Getenv("tls_redirect") == "true" {
|
if utils.Getenv("TLS_ENABLED", "false") == "true" && utils.Getenv("TLS_REDIRECT", "false") == "true" {
|
||||||
redirectTLS = true
|
redirectTLS = true
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
@@ -246,7 +288,7 @@ func Handler(conn net.Conn) {
|
|||||||
|
|
||||||
if redirectTLS {
|
if redirectTLS {
|
||||||
_, err = conn.Write([]byte("HTTP/1.1 301 Moved Permanently\r\n" +
|
_, err = conn.Write([]byte("HTTP/1.1 301 Moved Permanently\r\n" +
|
||||||
fmt.Sprintf("Location: https://%s.%s/\r\n", slug, utils.Getenv("domain")) +
|
fmt.Sprintf("Location: https://%s.%s/\r\n", slug, utils.Getenv("DOMAIN", "localhost")) +
|
||||||
"Content-Length: 0\r\n" +
|
"Content-Length: 0\r\n" +
|
||||||
"Connection: close\r\n" +
|
"Connection: close\r\n" +
|
||||||
"\r\n"))
|
"\r\n"))
|
||||||
@@ -288,51 +330,65 @@ func Handler(conn net.Conn) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
cw := NewCustomWriter(conn, dstReader, conn.RemoteAddr())
|
cw := NewCustomWriter(conn, dstReader, conn.RemoteAddr())
|
||||||
cw.SetInteraction(sshSession.Interaction)
|
|
||||||
forwardRequest(cw, reqhf, sshSession)
|
forwardRequest(cw, reqhf, sshSession)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func forwardRequest(cw *CustomWriter, initialRequest *RequestHeaderFactory, sshSession *session.SSHSession) {
|
func forwardRequest(cw HTTPWriter, initialRequest RequestHeaderManager, sshSession *session.SSHSession) {
|
||||||
payload := sshSession.Forwarder.CreateForwardedTCPIPPayload(cw.RemoteAddr)
|
payload := sshSession.GetForwarder().CreateForwardedTCPIPPayload(cw.GetRemoteAddr())
|
||||||
channel, reqs, err := sshSession.Lifecycle.GetConnection().OpenChannel("forwarded-tcpip", payload)
|
|
||||||
if err != nil {
|
type channelResult struct {
|
||||||
log.Printf("Failed to open forwarded-tcpip channel: %v", err)
|
channel ssh.Channel
|
||||||
|
reqs <-chan *ssh.Request
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
resultChan := make(chan channelResult, 1)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
channel, reqs, err := sshSession.GetLifecycle().GetConnection().OpenChannel("forwarded-tcpip", payload)
|
||||||
|
resultChan <- channelResult{channel, reqs, err}
|
||||||
|
}()
|
||||||
|
|
||||||
|
var channel ssh.Channel
|
||||||
|
var reqs <-chan *ssh.Request
|
||||||
|
|
||||||
|
select {
|
||||||
|
case result := <-resultChan:
|
||||||
|
if result.err != nil {
|
||||||
|
log.Printf("Failed to open forwarded-tcpip channel: %v", result.err)
|
||||||
|
sshSession.GetForwarder().WriteBadGatewayResponse(cw.GetWriter())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
channel = result.channel
|
||||||
|
reqs = result.reqs
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
log.Printf("Timeout opening forwarded-tcpip channel")
|
||||||
|
sshSession.GetForwarder().WriteBadGatewayResponse(cw.GetWriter())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go ssh.DiscardRequests(reqs)
|
||||||
for req := range reqs {
|
|
||||||
err := req.Reply(false, nil)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to reply to request: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
fingerprintMiddleware := NewTunnelFingerprint()
|
fingerprintMiddleware := NewTunnelFingerprint()
|
||||||
forwardedForMiddleware := NewForwardedFor(cw.RemoteAddr)
|
forwardedForMiddleware := NewForwardedFor(cw.GetRemoteAddr())
|
||||||
|
|
||||||
cw.respMW = append(cw.respMW, fingerprintMiddleware)
|
cw.AddResponseMiddleware(fingerprintMiddleware)
|
||||||
cw.reqStartMW = append(cw.reqStartMW, forwardedForMiddleware)
|
cw.AddRequestStartMiddleware(forwardedForMiddleware)
|
||||||
cw.reqEndMW = nil
|
cw.SetRequestHeader(initialRequest)
|
||||||
cw.reqHeader = initialRequest
|
|
||||||
|
|
||||||
for _, m := range cw.reqStartMW {
|
for _, m := range cw.GetRequestStartMiddleware() {
|
||||||
err = m.HandleRequest(cw.reqHeader)
|
if err := m.HandleRequest(initialRequest); err != nil {
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error handling request: %v", err)
|
log.Printf("Error handling request: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = channel.Write(initialRequest.Finalize())
|
_, err := channel.Write(initialRequest.Finalize())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to forward request: %v", err)
|
log.Printf("Failed to forward request: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sshSession.Forwarder.HandleConnection(cw, channel, cw.RemoteAddr)
|
sshSession.GetForwarder().HandleConnection(cw, channel, cw.GetRemoteAddr())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,13 +13,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func NewHTTPSServer() error {
|
func NewHTTPSServer() error {
|
||||||
cert, err := tls.LoadX509KeyPair(utils.Getenv("cert_loc"), utils.Getenv("key_loc"))
|
domain := utils.Getenv("DOMAIN", "localhost")
|
||||||
|
httpsPort := utils.Getenv("HTTPS_PORT", "8443")
|
||||||
|
|
||||||
|
tlsConfig, err := NewTLSConfig(domain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to initialize TLS config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
config := &tls.Config{Certificates: []tls.Certificate{cert}}
|
ln, err := tls.Listen("tcp", ":"+httpsPort, tlsConfig)
|
||||||
ln, err := tls.Listen("tcp", ":443", config)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -102,7 +104,6 @@ func HandlerTLS(conn net.Conn) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
cw := NewCustomWriter(conn, dstReader, conn.RemoteAddr())
|
cw := NewCustomWriter(conn, dstReader, conn.RemoteAddr())
|
||||||
cw.SetInteraction(sshSession.Interaction)
|
|
||||||
forwardRequest(cw, reqhf, sshSession)
|
forwardRequest(cw, reqhf, sshSession)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,11 +5,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type RequestMiddleware interface {
|
type RequestMiddleware interface {
|
||||||
HandleRequest(header *RequestHeaderFactory) error
|
HandleRequest(header RequestHeaderManager) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResponseMiddleware interface {
|
type ResponseMiddleware interface {
|
||||||
HandleResponse(header *ResponseHeaderFactory, body []byte) error
|
HandleResponse(header ResponseHeaderManager, body []byte) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type TunnelFingerprint struct{}
|
type TunnelFingerprint struct{}
|
||||||
@@ -18,16 +18,11 @@ func NewTunnelFingerprint() *TunnelFingerprint {
|
|||||||
return &TunnelFingerprint{}
|
return &TunnelFingerprint{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *TunnelFingerprint) HandleResponse(header *ResponseHeaderFactory, body []byte) error {
|
func (h *TunnelFingerprint) HandleResponse(header ResponseHeaderManager, body []byte) error {
|
||||||
header.Set("Server", "Tunnel Please")
|
header.Set("Server", "Tunnel Please")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type RequestLogger struct {
|
|
||||||
interaction Interaction
|
|
||||||
remoteAddr net.Addr
|
|
||||||
}
|
|
||||||
|
|
||||||
type ForwardedFor struct {
|
type ForwardedFor struct {
|
||||||
addr net.Addr
|
addr net.Addr
|
||||||
}
|
}
|
||||||
@@ -36,7 +31,7 @@ func NewForwardedFor(addr net.Addr) *ForwardedFor {
|
|||||||
return &ForwardedFor{addr: addr}
|
return &ForwardedFor{addr: addr}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ff *ForwardedFor) HandleRequest(header *RequestHeaderFactory) error {
|
func (ff *ForwardedFor) HandleRequest(header RequestHeaderManager) error {
|
||||||
host, _, err := net.SplitHostPort(ff.addr.String())
|
host, _, err := net.SplitHostPort(ff.addr.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -44,119 +39,3 @@ func (ff *ForwardedFor) HandleRequest(header *RequestHeaderFactory) error {
|
|||||||
header.Set("X-Forwarded-For", host)
|
header.Set("X-Forwarded-For", host)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: Implement caching atau enggak
|
|
||||||
//const maxCacheSize = 50 * 1024 * 1024
|
|
||||||
//
|
|
||||||
//type DiskCacheMiddleware struct {
|
|
||||||
// dir string
|
|
||||||
// mu sync.Mutex
|
|
||||||
// file *os.File
|
|
||||||
// path string
|
|
||||||
// cacheable bool
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func NewDiskCacheMiddleware() *DiskCacheMiddleware {
|
|
||||||
// return &DiskCacheMiddleware{dir: "cache"}
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *DiskCacheMiddleware) ensureDir() error {
|
|
||||||
// return os.MkdirAll(c.dir, 0755)
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *DiskCacheMiddleware) cacheKey(method, path string) string {
|
|
||||||
// return fmt.Sprintf("%s_%s.cache", method, base64.URLEncoding.EncodeToString([]byte(path)))
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *DiskCacheMiddleware) filePath(method, path string) string {
|
|
||||||
// return filepath.Join(c.dir, c.cacheKey(method, path))
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func fileExists(path string) bool {
|
|
||||||
// _, err := os.Stat(path)
|
|
||||||
// if err == nil {
|
|
||||||
// return true
|
|
||||||
// }
|
|
||||||
// if os.IsNotExist(err) {
|
|
||||||
// return false
|
|
||||||
// }
|
|
||||||
// return false
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func canCacheRequest(header *RequestHeaderFactory) bool {
|
|
||||||
// if header.Method != "GET" {
|
|
||||||
// return false
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// if cacheControl := header.Get("Cache-Control"); cacheControl != "" {
|
|
||||||
// if strings.Contains(cacheControl, "no-store") || strings.Contains(cacheControl, "private") || strings.Contains(cacheControl, "no-cache") || strings.Contains(cacheControl, "max-age=0") {
|
|
||||||
// return false
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// if header.Get("Authorization") != "" {
|
|
||||||
// return false
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// if header.Get("Cookie") != "" {
|
|
||||||
// return false
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// return true
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *DiskCacheMiddleware) HandleRequest(header *RequestHeaderFactory) error {
|
|
||||||
// if !canCacheRequest(header) {
|
|
||||||
// c.cacheable = false
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// c.cacheable = true
|
|
||||||
// _ = c.ensureDir()
|
|
||||||
// path := c.filePath(header.Method, header.Path)
|
|
||||||
//
|
|
||||||
// if fileExists(path + ".finish") {
|
|
||||||
// c.file = nil
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// if c.file != nil {
|
|
||||||
// err := c.file.Close()
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// err = os.Rename(c.path, c.path+".finish")
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// c.path = path
|
|
||||||
// f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// c.file = f
|
|
||||||
//
|
|
||||||
// return nil
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *DiskCacheMiddleware) HandleResponse(header *ResponseHeaderFactory, body []byte) error {
|
|
||||||
// if !c.cacheable {
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// if c.file == nil {
|
|
||||||
// header.Set("X-Cache", "HIT")
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// _, err := c.file.Write(body)
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// header.Set("X-Cache", "MISS")
|
|
||||||
// return nil
|
|
||||||
//}
|
|
||||||
|
|||||||
@@ -11,42 +11,49 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
Conn *net.Listener
|
conn *net.Listener
|
||||||
Config *ssh.ServerConfig
|
config *ssh.ServerConfig
|
||||||
HttpServer *http.Server
|
httpServer *http.Server
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) GetConn() *net.Listener {
|
||||||
|
return s.conn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) GetConfig() *ssh.ServerConfig {
|
||||||
|
return s.config
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) GetHttpServer() *http.Server {
|
||||||
|
return s.httpServer
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer(config *ssh.ServerConfig) *Server {
|
func NewServer(config *ssh.ServerConfig) *Server {
|
||||||
listener, err := net.Listen("tcp", fmt.Sprintf(":%s", utils.Getenv("port")))
|
listener, err := net.Listen("tcp", fmt.Sprintf(":%s", utils.Getenv("PORT", "2200")))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to listen on port 2200: %v", err)
|
log.Fatalf("failed to listen on port 2200: %v", err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if utils.Getenv("tls_enabled") == "true" {
|
if utils.Getenv("TLS_ENABLED", "false") == "true" {
|
||||||
go func() {
|
err = NewHTTPSServer()
|
||||||
err = NewHTTPSServer()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("failed to start https server: %v", err)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
go func() {
|
|
||||||
err = NewHTTPServer()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to start http server: %v", err)
|
log.Fatalf("failed to start https server: %v", err)
|
||||||
}
|
}
|
||||||
}()
|
}
|
||||||
|
err = NewHTTPServer()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed to start http server: %v", err)
|
||||||
|
}
|
||||||
return &Server{
|
return &Server{
|
||||||
Conn: &listener,
|
conn: &listener,
|
||||||
Config: config,
|
config: config,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Start() {
|
func (s *Server) Start() {
|
||||||
log.Println("SSH server is starting on port 2200...")
|
log.Println("SSH server is starting on port 2200...")
|
||||||
for {
|
for {
|
||||||
conn, err := (*s.Conn).Accept()
|
conn, err := (*s.conn).Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to accept connection: %v", err)
|
log.Printf("failed to accept connection: %v", err)
|
||||||
continue
|
continue
|
||||||
|
|||||||
321
server/tls.go
Normal file
321
server/tls.go
Normal file
@@ -0,0 +1,321 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/pem"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
"tunnel_pls/utils"
|
||||||
|
|
||||||
|
"github.com/caddyserver/certmagic"
|
||||||
|
"github.com/libdns/cloudflare"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TLSManager interface {
|
||||||
|
userCertsExistAndValid() bool
|
||||||
|
loadUserCerts() error
|
||||||
|
startCertWatcher()
|
||||||
|
initCertMagic() error
|
||||||
|
getTLSConfig() *tls.Config
|
||||||
|
getCertificate(hello *tls.ClientHelloInfo) (*tls.Certificate, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type tlsManager struct {
|
||||||
|
domain string
|
||||||
|
certPath string
|
||||||
|
keyPath string
|
||||||
|
storagePath string
|
||||||
|
|
||||||
|
userCert *tls.Certificate
|
||||||
|
userCertMu sync.RWMutex
|
||||||
|
|
||||||
|
magic *certmagic.Config
|
||||||
|
|
||||||
|
useCertMagic bool
|
||||||
|
}
|
||||||
|
|
||||||
|
var globalTLSManager TLSManager
|
||||||
|
var tlsManagerOnce sync.Once
|
||||||
|
|
||||||
|
func NewTLSConfig(domain string) (*tls.Config, error) {
|
||||||
|
var initErr error
|
||||||
|
|
||||||
|
tlsManagerOnce.Do(func() {
|
||||||
|
certPath := "certs/tls/cert.pem"
|
||||||
|
keyPath := "certs/tls/privkey.pem"
|
||||||
|
storagePath := "certs/tls/certmagic"
|
||||||
|
|
||||||
|
tm := &tlsManager{
|
||||||
|
domain: domain,
|
||||||
|
certPath: certPath,
|
||||||
|
keyPath: keyPath,
|
||||||
|
storagePath: storagePath,
|
||||||
|
}
|
||||||
|
|
||||||
|
if tm.userCertsExistAndValid() {
|
||||||
|
log.Printf("Using user-provided certificates from %s and %s", certPath, keyPath)
|
||||||
|
if err := tm.loadUserCerts(); err != nil {
|
||||||
|
initErr = fmt.Errorf("failed to load user certificates: %w", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tm.useCertMagic = false
|
||||||
|
tm.startCertWatcher()
|
||||||
|
} else {
|
||||||
|
if !isACMEConfigComplete() {
|
||||||
|
log.Printf("User certificates missing or invalid, and ACME configuration is incomplete")
|
||||||
|
log.Printf("To enable automatic certificate generation, set CF_API_TOKEN environment variable")
|
||||||
|
initErr = fmt.Errorf("no valid certificates found and ACME configuration is incomplete (CF_API_TOKEN is required)")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("User certificates missing or don't cover %s and *.%s, using CertMagic", domain, domain)
|
||||||
|
if err := tm.initCertMagic(); err != nil {
|
||||||
|
initErr = fmt.Errorf("failed to initialize CertMagic: %w", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tm.useCertMagic = true
|
||||||
|
}
|
||||||
|
|
||||||
|
globalTLSManager = tm
|
||||||
|
})
|
||||||
|
|
||||||
|
if initErr != nil {
|
||||||
|
return nil, initErr
|
||||||
|
}
|
||||||
|
|
||||||
|
return globalTLSManager.getTLSConfig(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func isACMEConfigComplete() bool {
|
||||||
|
cfAPIToken := utils.Getenv("CF_API_TOKEN", "")
|
||||||
|
return cfAPIToken != ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tm *tlsManager) userCertsExistAndValid() bool {
|
||||||
|
if _, err := os.Stat(tm.certPath); os.IsNotExist(err) {
|
||||||
|
log.Printf("Certificate file not found: %s", tm.certPath)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(tm.keyPath); os.IsNotExist(err) {
|
||||||
|
log.Printf("Key file not found: %s", tm.keyPath)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return ValidateCertDomains(tm.certPath, tm.domain)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ValidateCertDomains(certPath, domain string) bool {
|
||||||
|
certPEM, err := os.ReadFile(certPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to read certificate: %v", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
block, _ := pem.Decode(certPEM)
|
||||||
|
if block == nil {
|
||||||
|
log.Printf("Failed to decode PEM block from certificate")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
cert, err := x509.ParseCertificate(block.Bytes)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to parse certificate: %v", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if time.Now().After(cert.NotAfter) {
|
||||||
|
log.Printf("Certificate has expired (NotAfter: %v)", cert.NotAfter)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if time.Now().Add(30 * 24 * time.Hour).After(cert.NotAfter) {
|
||||||
|
log.Printf("Certificate expiring soon (NotAfter: %v), will use CertMagic for renewal", cert.NotAfter)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
var certDomains []string
|
||||||
|
if cert.Subject.CommonName != "" {
|
||||||
|
certDomains = append(certDomains, cert.Subject.CommonName)
|
||||||
|
}
|
||||||
|
certDomains = append(certDomains, cert.DNSNames...)
|
||||||
|
|
||||||
|
hasBase := false
|
||||||
|
hasWildcard := false
|
||||||
|
wildcardDomain := "*." + domain
|
||||||
|
|
||||||
|
for _, d := range certDomains {
|
||||||
|
if d == domain {
|
||||||
|
hasBase = true
|
||||||
|
}
|
||||||
|
if d == wildcardDomain {
|
||||||
|
hasWildcard = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !hasBase {
|
||||||
|
log.Printf("Certificate does not cover base domain: %s", domain)
|
||||||
|
}
|
||||||
|
if !hasWildcard {
|
||||||
|
log.Printf("Certificate does not cover wildcard domain: %s", wildcardDomain)
|
||||||
|
}
|
||||||
|
|
||||||
|
return hasBase && hasWildcard
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tm *tlsManager) loadUserCerts() error {
|
||||||
|
cert, err := tls.LoadX509KeyPair(tm.certPath, tm.keyPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
tm.userCertMu.Lock()
|
||||||
|
tm.userCert = &cert
|
||||||
|
tm.userCertMu.Unlock()
|
||||||
|
|
||||||
|
log.Printf("Loaded user certificates successfully")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tm *tlsManager) startCertWatcher() {
|
||||||
|
go func() {
|
||||||
|
var lastCertMod, lastKeyMod time.Time
|
||||||
|
|
||||||
|
if info, err := os.Stat(tm.certPath); err == nil {
|
||||||
|
lastCertMod = info.ModTime()
|
||||||
|
}
|
||||||
|
if info, err := os.Stat(tm.keyPath); err == nil {
|
||||||
|
lastKeyMod = info.ModTime()
|
||||||
|
}
|
||||||
|
|
||||||
|
ticker := time.NewTicker(30 * time.Second)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for range ticker.C {
|
||||||
|
certInfo, certErr := os.Stat(tm.certPath)
|
||||||
|
keyInfo, keyErr := os.Stat(tm.keyPath)
|
||||||
|
|
||||||
|
if certErr != nil || keyErr != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if certInfo.ModTime().After(lastCertMod) || keyInfo.ModTime().After(lastKeyMod) {
|
||||||
|
log.Printf("Certificate files changed, reloading...")
|
||||||
|
|
||||||
|
if !ValidateCertDomains(tm.certPath, tm.domain) {
|
||||||
|
log.Printf("New certificates don't cover required domains")
|
||||||
|
|
||||||
|
if !isACMEConfigComplete() {
|
||||||
|
log.Printf("Cannot switch to CertMagic: ACME configuration is incomplete (CF_API_TOKEN is required)")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Switching to CertMagic for automatic certificate management")
|
||||||
|
if err := tm.initCertMagic(); err != nil {
|
||||||
|
log.Printf("Failed to initialize CertMagic: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
tm.useCertMagic = true
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tm.loadUserCerts(); err != nil {
|
||||||
|
log.Printf("Failed to reload certificates: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
lastCertMod = certInfo.ModTime()
|
||||||
|
lastKeyMod = keyInfo.ModTime()
|
||||||
|
log.Printf("Certificates reloaded successfully")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tm *tlsManager) initCertMagic() error {
|
||||||
|
if err := os.MkdirAll(tm.storagePath, 0700); err != nil {
|
||||||
|
return fmt.Errorf("failed to create cert storage directory: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
acmeEmail := utils.Getenv("ACME_EMAIL", "admin@"+tm.domain)
|
||||||
|
cfAPIToken := utils.Getenv("CF_API_TOKEN", "")
|
||||||
|
acmeStaging := utils.Getenv("ACME_STAGING", "false") == "true"
|
||||||
|
|
||||||
|
if cfAPIToken == "" {
|
||||||
|
return fmt.Errorf("CF_API_TOKEN environment variable is required for automatic certificate generation")
|
||||||
|
}
|
||||||
|
|
||||||
|
cfProvider := &cloudflare.Provider{
|
||||||
|
APIToken: cfAPIToken,
|
||||||
|
}
|
||||||
|
|
||||||
|
storage := &certmagic.FileStorage{Path: tm.storagePath}
|
||||||
|
|
||||||
|
cache := certmagic.NewCache(certmagic.CacheOptions{
|
||||||
|
GetConfigForCert: func(cert certmagic.Certificate) (*certmagic.Config, error) {
|
||||||
|
return tm.magic, nil
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
magic := certmagic.New(cache, certmagic.Config{
|
||||||
|
Storage: storage,
|
||||||
|
})
|
||||||
|
|
||||||
|
acmeIssuer := certmagic.NewACMEIssuer(magic, certmagic.ACMEIssuer{
|
||||||
|
Email: acmeEmail,
|
||||||
|
Agreed: true,
|
||||||
|
DNS01Solver: &certmagic.DNS01Solver{
|
||||||
|
DNSManager: certmagic.DNSManager{
|
||||||
|
DNSProvider: cfProvider,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if acmeStaging {
|
||||||
|
acmeIssuer.CA = certmagic.LetsEncryptStagingCA
|
||||||
|
log.Printf("Using Let's Encrypt staging server")
|
||||||
|
} else {
|
||||||
|
acmeIssuer.CA = certmagic.LetsEncryptProductionCA
|
||||||
|
log.Printf("Using Let's Encrypt production server")
|
||||||
|
}
|
||||||
|
|
||||||
|
magic.Issuers = []certmagic.Issuer{acmeIssuer}
|
||||||
|
tm.magic = magic
|
||||||
|
|
||||||
|
domains := []string{tm.domain, "*." + tm.domain}
|
||||||
|
log.Printf("Requesting certificates for: %v", domains)
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
if err := magic.ManageSync(ctx, domains); err != nil {
|
||||||
|
return fmt.Errorf("failed to obtain certificates: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Certificates obtained successfully for %v", domains)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tm *tlsManager) getTLSConfig() *tls.Config {
|
||||||
|
return &tls.Config{
|
||||||
|
GetCertificate: tm.getCertificate,
|
||||||
|
MinVersion: tls.VersionTLS12,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tm *tlsManager) getCertificate(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
||||||
|
if tm.useCertMagic {
|
||||||
|
return tm.magic.GetCertificate(hello)
|
||||||
|
}
|
||||||
|
|
||||||
|
tm.userCertMu.RLock()
|
||||||
|
defer tm.userCertMu.RUnlock()
|
||||||
|
|
||||||
|
if tm.userCert == nil {
|
||||||
|
return nil, fmt.Errorf("no certificate available")
|
||||||
|
}
|
||||||
|
|
||||||
|
return tm.userCert, nil
|
||||||
|
}
|
||||||
@@ -4,24 +4,48 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
"tunnel_pls/session/slug"
|
"tunnel_pls/session/slug"
|
||||||
"tunnel_pls/types"
|
"tunnel_pls/types"
|
||||||
|
"tunnel_pls/utils"
|
||||||
|
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var bufferPool = sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
bufSize := utils.GetBufferSize()
|
||||||
|
return make([]byte, bufSize)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func copyWithBuffer(dst io.Writer, src io.Reader) (written int64, err error) {
|
||||||
|
buf := bufferPool.Get().([]byte)
|
||||||
|
defer bufferPool.Put(buf)
|
||||||
|
return io.CopyBuffer(dst, src, buf)
|
||||||
|
}
|
||||||
|
|
||||||
type Forwarder struct {
|
type Forwarder struct {
|
||||||
Listener net.Listener
|
listener net.Listener
|
||||||
TunnelType types.TunnelType
|
tunnelType types.TunnelType
|
||||||
ForwardedPort uint16
|
forwardedPort uint16
|
||||||
SlugManager slug.Manager
|
slugManager slug.Manager
|
||||||
Lifecycle Lifecycle
|
lifecycle Lifecycle
|
||||||
ActiveForwarder []chan struct{}
|
}
|
||||||
|
|
||||||
|
func NewForwarder(slugManager slug.Manager) *Forwarder {
|
||||||
|
return &Forwarder{
|
||||||
|
listener: nil,
|
||||||
|
tunnelType: "",
|
||||||
|
forwardedPort: 0,
|
||||||
|
slugManager: slugManager,
|
||||||
|
lifecycle: nil,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Lifecycle interface {
|
type Lifecycle interface {
|
||||||
@@ -41,31 +65,10 @@ type ForwardingController interface {
|
|||||||
SetLifecycle(lifecycle Lifecycle)
|
SetLifecycle(lifecycle Lifecycle)
|
||||||
CreateForwardedTCPIPPayload(origin net.Addr) []byte
|
CreateForwardedTCPIPPayload(origin net.Addr) []byte
|
||||||
WriteBadGatewayResponse(dst io.Writer)
|
WriteBadGatewayResponse(dst io.Writer)
|
||||||
AddActiveForwarder(drop chan struct{})
|
|
||||||
DropAllForwarder() int
|
|
||||||
GetForwarderCount() int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Forwarder) AddActiveForwarder(drop chan struct{}) {
|
|
||||||
f.ActiveForwarder = append(f.ActiveForwarder, drop)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Forwarder) DropAllForwarder() int {
|
|
||||||
total := 0
|
|
||||||
for _, d := range f.ActiveForwarder {
|
|
||||||
close(d)
|
|
||||||
total += 1
|
|
||||||
}
|
|
||||||
f.ActiveForwarder = nil
|
|
||||||
return total
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Forwarder) GetForwarderCount() int {
|
|
||||||
return len(f.ActiveForwarder)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Forwarder) SetLifecycle(lifecycle Lifecycle) {
|
func (f *Forwarder) SetLifecycle(lifecycle Lifecycle) {
|
||||||
f.Lifecycle = lifecycle
|
f.lifecycle = lifecycle
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Forwarder) AcceptTCPConnections() {
|
func (f *Forwarder) AcceptTCPConnections() {
|
||||||
@@ -78,29 +81,57 @@ func (f *Forwarder) AcceptTCPConnections() {
|
|||||||
log.Printf("Error accepting connection: %v", err)
|
log.Printf("Error accepting connection: %v", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
payload := f.CreateForwardedTCPIPPayload(conn.RemoteAddr())
|
|
||||||
channel, reqs, err := f.Lifecycle.GetConnection().OpenChannel("forwarded-tcpip", payload)
|
if err := conn.SetDeadline(time.Now().Add(5 * time.Second)); err != nil {
|
||||||
if err != nil {
|
log.Printf("Failed to set connection deadline: %v", err)
|
||||||
log.Printf("Failed to open forwarded-tcpip channel: %v", err)
|
if closeErr := conn.Close(); closeErr != nil {
|
||||||
return
|
log.Printf("Failed to close connection: %v", closeErr)
|
||||||
|
}
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
payload := f.CreateForwardedTCPIPPayload(conn.RemoteAddr())
|
||||||
|
|
||||||
|
type channelResult struct {
|
||||||
|
channel ssh.Channel
|
||||||
|
reqs <-chan *ssh.Request
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
resultChan := make(chan channelResult, 1)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for req := range reqs {
|
channel, reqs, err := f.lifecycle.GetConnection().OpenChannel("forwarded-tcpip", payload)
|
||||||
err := req.Reply(false, nil)
|
resultChan <- channelResult{channel, reqs, err}
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to reply to request: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
go f.HandleConnection(conn, channel, conn.RemoteAddr())
|
|
||||||
|
select {
|
||||||
|
case result := <-resultChan:
|
||||||
|
if result.err != nil {
|
||||||
|
log.Printf("Failed to open forwarded-tcpip channel: %v", result.err)
|
||||||
|
if closeErr := conn.Close(); closeErr != nil {
|
||||||
|
log.Printf("Failed to close connection: %v", closeErr)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := conn.SetDeadline(time.Time{}); err != nil {
|
||||||
|
log.Printf("Failed to clear connection deadline: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
go ssh.DiscardRequests(result.reqs)
|
||||||
|
go f.HandleConnection(conn, result.channel, conn.RemoteAddr())
|
||||||
|
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
log.Printf("Timeout opening forwarded-tcpip channel")
|
||||||
|
if closeErr := conn.Close(); closeErr != nil {
|
||||||
|
log.Printf("Failed to close connection: %v", closeErr)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Forwarder) HandleConnection(dst io.ReadWriter, src ssh.Channel, remoteAddr net.Addr) {
|
func (f *Forwarder) HandleConnection(dst io.ReadWriter, src ssh.Channel, remoteAddr net.Addr) {
|
||||||
drop := make(chan struct{})
|
defer func() {
|
||||||
defer func(src ssh.Channel) {
|
|
||||||
_, err := io.Copy(io.Discard, src)
|
_, err := io.Copy(io.Discard, src)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to discard connection: %v", err)
|
log.Printf("Failed to discard connection: %v", err)
|
||||||
@@ -108,58 +139,62 @@ func (f *Forwarder) HandleConnection(dst io.ReadWriter, src ssh.Channel, remoteA
|
|||||||
|
|
||||||
err = src.Close()
|
err = src.Close()
|
||||||
if err != nil && !errors.Is(err, io.EOF) {
|
if err != nil && !errors.Is(err, io.EOF) {
|
||||||
log.Printf("Error closing connection: %v", err)
|
log.Printf("Error closing source channel: %v", err)
|
||||||
}
|
}
|
||||||
}(src)
|
|
||||||
|
if closer, ok := dst.(io.Closer); ok {
|
||||||
|
err = closer.Close()
|
||||||
|
if err != nil && !errors.Is(err, io.EOF) {
|
||||||
|
log.Printf("Error closing destination connection: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
log.Printf("Handling new forwarded connection from %s", remoteAddr)
|
log.Printf("Handling new forwarded connection from %s", remoteAddr)
|
||||||
|
|
||||||
|
done := make(chan struct{}, 2)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
_, err := io.Copy(src, dst)
|
_, err := copyWithBuffer(src, dst)
|
||||||
if err != nil && !errors.Is(err, io.EOF) && !errors.Is(err, net.ErrClosed) {
|
if err != nil && !errors.Is(err, io.EOF) && !errors.Is(err, net.ErrClosed) {
|
||||||
log.Printf("Error copying from conn.Reader to channel: %v", err)
|
log.Printf("Error copying from conn.Reader to channel: %v", err)
|
||||||
}
|
}
|
||||||
|
done <- struct{}{}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
select {
|
_, err := copyWithBuffer(dst, src)
|
||||||
case <-drop:
|
if err != nil && !errors.Is(err, io.EOF) && !errors.Is(err, net.ErrClosed) {
|
||||||
fmt.Println("Closinggggg")
|
log.Printf("Error copying from channel to conn.Writer: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
done <- struct{}{}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
f.AddActiveForwarder(drop)
|
<-done
|
||||||
|
|
||||||
_, err := io.Copy(dst, src)
|
|
||||||
|
|
||||||
if err != nil && !errors.Is(err, io.EOF) {
|
|
||||||
log.Printf("Error copying from channel to conn.Writer: %v", err)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Forwarder) SetType(tunnelType types.TunnelType) {
|
func (f *Forwarder) SetType(tunnelType types.TunnelType) {
|
||||||
f.TunnelType = tunnelType
|
f.tunnelType = tunnelType
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Forwarder) GetTunnelType() types.TunnelType {
|
func (f *Forwarder) GetTunnelType() types.TunnelType {
|
||||||
return f.TunnelType
|
return f.tunnelType
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Forwarder) GetForwardedPort() uint16 {
|
func (f *Forwarder) GetForwardedPort() uint16 {
|
||||||
return f.ForwardedPort
|
return f.forwardedPort
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Forwarder) SetForwardedPort(port uint16) {
|
func (f *Forwarder) SetForwardedPort(port uint16) {
|
||||||
f.ForwardedPort = port
|
f.forwardedPort = port
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Forwarder) SetListener(listener net.Listener) {
|
func (f *Forwarder) SetListener(listener net.Listener) {
|
||||||
f.Listener = listener
|
f.listener = listener
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Forwarder) GetListener() net.Listener {
|
func (f *Forwarder) GetListener() net.Listener {
|
||||||
return f.Listener
|
return f.listener
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Forwarder) WriteBadGatewayResponse(dst io.Writer) {
|
func (f *Forwarder) WriteBadGatewayResponse(dst io.Writer) {
|
||||||
@@ -172,7 +207,7 @@ func (f *Forwarder) WriteBadGatewayResponse(dst io.Writer) {
|
|||||||
|
|
||||||
func (f *Forwarder) Close() error {
|
func (f *Forwarder) Close() error {
|
||||||
if f.GetListener() != nil {
|
if f.GetListener() != nil {
|
||||||
return f.Listener.Close()
|
return f.listener.Close()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,10 +19,28 @@ var blockedReservedPorts = []uint16{1080, 1433, 1521, 1900, 2049, 3306, 3389, 54
|
|||||||
func (s *SSHSession) HandleGlobalRequest(GlobalRequest <-chan *ssh.Request) {
|
func (s *SSHSession) HandleGlobalRequest(GlobalRequest <-chan *ssh.Request) {
|
||||||
for req := range GlobalRequest {
|
for req := range GlobalRequest {
|
||||||
switch req.Type {
|
switch req.Type {
|
||||||
case "tcpip-forward":
|
case "shell", "pty-req":
|
||||||
s.HandleTCPIPForward(req)
|
err := req.Reply(true, nil)
|
||||||
return
|
if err != nil {
|
||||||
case "shell", "pty-req", "window-change":
|
log.Println("Failed to reply to request:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case "window-change":
|
||||||
|
p := req.Payload
|
||||||
|
if len(p) < 16 {
|
||||||
|
log.Println("invalid window-change payload")
|
||||||
|
err := req.Reply(false, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Failed to reply to request:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cols := binary.BigEndian.Uint32(p[0:4])
|
||||||
|
rows := binary.BigEndian.Uint32(p[4:8])
|
||||||
|
|
||||||
|
s.interaction.SetWH(int(cols), int(rows))
|
||||||
|
|
||||||
err := req.Reply(true, nil)
|
err := req.Reply(true, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to reply to request:", err)
|
log.Println("Failed to reply to request:", err)
|
||||||
@@ -52,7 +70,7 @@ func (s *SSHSession) HandleTCPIPForward(req *ssh.Request) {
|
|||||||
log.Println("Failed to reply to request:", err)
|
log.Println("Failed to reply to request:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = s.Lifecycle.Close()
|
err = s.lifecycle.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to close session: %v", err)
|
log.Printf("failed to close session: %v", err)
|
||||||
}
|
}
|
||||||
@@ -62,13 +80,12 @@ func (s *SSHSession) HandleTCPIPForward(req *ssh.Request) {
|
|||||||
var rawPortToBind uint32
|
var rawPortToBind uint32
|
||||||
if err := binary.Read(reader, binary.BigEndian, &rawPortToBind); err != nil {
|
if err := binary.Read(reader, binary.BigEndian, &rawPortToBind); err != nil {
|
||||||
log.Println("Failed to read port from payload:", err)
|
log.Println("Failed to read port from payload:", err)
|
||||||
s.Interaction.SendMessage(fmt.Sprintf("Port %d is already in use or restricted. Please choose a different port. (02) \r\n", rawPortToBind))
|
|
||||||
err := req.Reply(false, nil)
|
err := req.Reply(false, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to reply to request:", err)
|
log.Println("Failed to reply to request:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = s.Lifecycle.Close()
|
err = s.lifecycle.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to close session: %v", err)
|
log.Printf("failed to close session: %v", err)
|
||||||
}
|
}
|
||||||
@@ -76,13 +93,13 @@ func (s *SSHSession) HandleTCPIPForward(req *ssh.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if rawPortToBind > 65535 {
|
if rawPortToBind > 65535 {
|
||||||
s.Interaction.SendMessage(fmt.Sprintf("Port %d is larger then allowed port of 65535. (02)\r\n", rawPortToBind))
|
log.Printf("Port %d is larger than allowed port of 65535", rawPortToBind)
|
||||||
err := req.Reply(false, nil)
|
err := req.Reply(false, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to reply to request:", err)
|
log.Println("Failed to reply to request:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = s.Lifecycle.Close()
|
err = s.lifecycle.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to close session: %v", err)
|
log.Printf("failed to close session: %v", err)
|
||||||
}
|
}
|
||||||
@@ -92,13 +109,13 @@ func (s *SSHSession) HandleTCPIPForward(req *ssh.Request) {
|
|||||||
portToBind := uint16(rawPortToBind)
|
portToBind := uint16(rawPortToBind)
|
||||||
|
|
||||||
if isBlockedPort(portToBind) {
|
if isBlockedPort(portToBind) {
|
||||||
s.Interaction.SendMessage(fmt.Sprintf("Port %d is already in use or restricted. Please choose a different port. (02)\r\n", portToBind))
|
log.Printf("Port %d is blocked or restricted", portToBind)
|
||||||
err := req.Reply(false, nil)
|
err := req.Reply(false, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to reply to request:", err)
|
log.Println("Failed to reply to request:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = s.Lifecycle.Close()
|
err = s.lifecycle.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to close session: %v", err)
|
log.Printf("failed to close session: %v", err)
|
||||||
}
|
}
|
||||||
@@ -108,42 +125,42 @@ func (s *SSHSession) HandleTCPIPForward(req *ssh.Request) {
|
|||||||
if portToBind == 80 || portToBind == 443 {
|
if portToBind == 80 || portToBind == 443 {
|
||||||
s.HandleHTTPForward(req, portToBind)
|
s.HandleHTTPForward(req, portToBind)
|
||||||
return
|
return
|
||||||
} else {
|
}
|
||||||
if portToBind == 0 {
|
if portToBind == 0 {
|
||||||
unassign, success := portUtil.Manager.GetUnassignedPort()
|
unassign, success := portUtil.Default.GetUnassignedPort()
|
||||||
portToBind = unassign
|
portToBind = unassign
|
||||||
if !success {
|
if !success {
|
||||||
s.Interaction.SendMessage("No available port\r\n")
|
log.Println("No available port")
|
||||||
err := req.Reply(false, nil)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Failed to reply to request:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = s.Lifecycle.Close()
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("failed to close session: %v", err)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
} else if isUse, isExist := portUtil.Manager.GetPortStatus(portToBind); isExist && isUse {
|
|
||||||
s.Interaction.SendMessage(fmt.Sprintf("Port %d is already in use or restricted. Please choose a different port. (03)\r\n", portToBind))
|
|
||||||
err := req.Reply(false, nil)
|
err := req.Reply(false, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to reply to request:", err)
|
log.Println("Failed to reply to request:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = s.Lifecycle.Close()
|
err = s.lifecycle.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to close session: %v", err)
|
log.Printf("failed to close session: %v", err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err := portUtil.Manager.SetPortStatus(portToBind, true)
|
} else if isUse, isExist := portUtil.Default.GetPortStatus(portToBind); isExist && isUse {
|
||||||
|
log.Printf("Port %d is already in use or restricted", portToBind)
|
||||||
|
err := req.Reply(false, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to set port status:", err)
|
log.Println("Failed to reply to request:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
err = s.lifecycle.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("failed to close session: %v", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
err = portUtil.Default.SetPortStatus(portToBind, true)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Failed to set port status:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
s.HandleTCPForward(req, addr, portToBind)
|
s.HandleTCPForward(req, addr, portToBind)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,12 +196,6 @@ func (s *SSHSession) HandleHTTPForward(req *ssh.Request, portToBind uint16) {
|
|||||||
}
|
}
|
||||||
log.Printf("HTTP forwarding approved on port: %d", portToBind)
|
log.Printf("HTTP forwarding approved on port: %d", portToBind)
|
||||||
|
|
||||||
domain := utils.Getenv("domain")
|
|
||||||
protocol := "http"
|
|
||||||
if utils.Getenv("tls_enabled") == "true" {
|
|
||||||
protocol = "https"
|
|
||||||
}
|
|
||||||
|
|
||||||
err = req.Reply(true, buf.Bytes())
|
err = req.Reply(true, buf.Bytes())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to reply to request:", err)
|
log.Println("Failed to reply to request:", err)
|
||||||
@@ -196,27 +207,27 @@ func (s *SSHSession) HandleHTTPForward(req *ssh.Request, portToBind uint16) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Forwarder.SetType(types.HTTP)
|
s.forwarder.SetType(types.HTTP)
|
||||||
s.Forwarder.SetForwardedPort(portToBind)
|
s.forwarder.SetForwardedPort(portToBind)
|
||||||
s.SlugManager.Set(slug)
|
s.slugManager.Set(slug)
|
||||||
s.Interaction.SendMessage("\033[H\033[2J")
|
s.lifecycle.SetStatus(types.RUNNING)
|
||||||
s.Interaction.ShowWelcomeMessage()
|
s.interaction.Start()
|
||||||
s.Interaction.SendMessage(fmt.Sprintf("Forwarding your traffic to %s://%s.%s\r\n", protocol, slug, domain))
|
|
||||||
s.Lifecycle.SetStatus(types.RUNNING)
|
|
||||||
s.Interaction.HandleUserInput()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SSHSession) HandleTCPForward(req *ssh.Request, addr string, portToBind uint16) {
|
func (s *SSHSession) HandleTCPForward(req *ssh.Request, addr string, portToBind uint16) {
|
||||||
log.Printf("Requested forwarding on %s:%d", addr, portToBind)
|
log.Printf("Requested forwarding on %s:%d", addr, portToBind)
|
||||||
listener, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", portToBind))
|
listener, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", portToBind))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.Interaction.SendMessage(fmt.Sprintf("Port %d is already in use or restricted. Please choose a different port.\r\n", portToBind))
|
log.Printf("Port %d is already in use or restricted", portToBind)
|
||||||
|
if setErr := portUtil.Default.SetPortStatus(portToBind, false); setErr != nil {
|
||||||
|
log.Printf("Failed to reset port status: %v", setErr)
|
||||||
|
}
|
||||||
err = req.Reply(false, nil)
|
err = req.Reply(false, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to reply to request:", err)
|
log.Println("Failed to reply to request:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = s.Lifecycle.Close()
|
err = s.lifecycle.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to close session: %v", err)
|
log.Printf("failed to close session: %v", err)
|
||||||
}
|
}
|
||||||
@@ -227,6 +238,9 @@ func (s *SSHSession) HandleTCPForward(req *ssh.Request, addr string, portToBind
|
|||||||
err = binary.Write(buf, binary.BigEndian, uint32(portToBind))
|
err = binary.Write(buf, binary.BigEndian, uint32(portToBind))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to write port to buffer:", err)
|
log.Println("Failed to write port to buffer:", err)
|
||||||
|
if setErr := portUtil.Default.SetPortStatus(portToBind, false); setErr != nil {
|
||||||
|
log.Printf("Failed to reset port status: %v", setErr)
|
||||||
|
}
|
||||||
err = listener.Close()
|
err = listener.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to close listener: %s", err)
|
log.Printf("Failed to close listener: %s", err)
|
||||||
@@ -239,6 +253,9 @@ func (s *SSHSession) HandleTCPForward(req *ssh.Request, addr string, portToBind
|
|||||||
err = req.Reply(true, buf.Bytes())
|
err = req.Reply(true, buf.Bytes())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to reply to request:", err)
|
log.Println("Failed to reply to request:", err)
|
||||||
|
if setErr := portUtil.Default.SetPortStatus(portToBind, false); setErr != nil {
|
||||||
|
log.Printf("Failed to reset port status: %v", setErr)
|
||||||
|
}
|
||||||
err = listener.Close()
|
err = listener.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to close listener: %s", err)
|
log.Printf("Failed to close listener: %s", err)
|
||||||
@@ -247,15 +264,12 @@ func (s *SSHSession) HandleTCPForward(req *ssh.Request, addr string, portToBind
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Forwarder.SetType(types.TCP)
|
s.forwarder.SetType(types.TCP)
|
||||||
s.Forwarder.SetListener(listener)
|
s.forwarder.SetListener(listener)
|
||||||
s.Forwarder.SetForwardedPort(portToBind)
|
s.forwarder.SetForwardedPort(portToBind)
|
||||||
s.Interaction.SendMessage("\033[H\033[2J")
|
s.lifecycle.SetStatus(types.RUNNING)
|
||||||
s.Interaction.ShowWelcomeMessage()
|
go s.forwarder.AcceptTCPConnections()
|
||||||
s.Interaction.SendMessage(fmt.Sprintf("Forwarding your traffic to tcp://%s:%d \r\n", utils.Getenv("domain"), s.Forwarder.GetForwardedPort()))
|
s.interaction.Start()
|
||||||
s.Lifecycle.SetStatus(types.RUNNING)
|
|
||||||
go s.Forwarder.AcceptTCPConnections()
|
|
||||||
s.Interaction.HandleUserInput()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateUniqueSlug() string {
|
func generateUniqueSlug() string {
|
||||||
|
|||||||
@@ -22,6 +22,131 @@ const (
|
|||||||
paddingRight = 4
|
paddingRight = 4
|
||||||
)
|
)
|
||||||
|
|
||||||
var forbiddenSlugs = []string{
|
var forbiddenSlugs = map[string]struct{}{
|
||||||
"ping",
|
"ping": {},
|
||||||
|
"staging": {},
|
||||||
|
"admin": {},
|
||||||
|
"root": {},
|
||||||
|
"api": {},
|
||||||
|
"www": {},
|
||||||
|
"support": {},
|
||||||
|
"help": {},
|
||||||
|
"status": {},
|
||||||
|
"health": {},
|
||||||
|
"login": {},
|
||||||
|
"logout": {},
|
||||||
|
"signup": {},
|
||||||
|
"register": {},
|
||||||
|
"settings": {},
|
||||||
|
"config": {},
|
||||||
|
"null": {},
|
||||||
|
"undefined": {},
|
||||||
|
"example": {},
|
||||||
|
"test": {},
|
||||||
|
"dev": {},
|
||||||
|
"system": {},
|
||||||
|
"administrator": {},
|
||||||
|
"dashboard": {},
|
||||||
|
"account": {},
|
||||||
|
"profile": {},
|
||||||
|
"user": {},
|
||||||
|
"users": {},
|
||||||
|
"auth": {},
|
||||||
|
"oauth": {},
|
||||||
|
"callback": {},
|
||||||
|
"webhook": {},
|
||||||
|
"webhooks": {},
|
||||||
|
"static": {},
|
||||||
|
"assets": {},
|
||||||
|
"cdn": {},
|
||||||
|
"mail": {},
|
||||||
|
"email": {},
|
||||||
|
"ftp": {},
|
||||||
|
"ssh": {},
|
||||||
|
"git": {},
|
||||||
|
"svn": {},
|
||||||
|
"blog": {},
|
||||||
|
"news": {},
|
||||||
|
"about": {},
|
||||||
|
"contact": {},
|
||||||
|
"terms": {},
|
||||||
|
"privacy": {},
|
||||||
|
"legal": {},
|
||||||
|
"billing": {},
|
||||||
|
"payment": {},
|
||||||
|
"checkout": {},
|
||||||
|
"cart": {},
|
||||||
|
"shop": {},
|
||||||
|
"store": {},
|
||||||
|
"download": {},
|
||||||
|
"uploads": {},
|
||||||
|
"images": {},
|
||||||
|
"img": {},
|
||||||
|
"css": {},
|
||||||
|
"js": {},
|
||||||
|
"fonts": {},
|
||||||
|
"public": {},
|
||||||
|
"private": {},
|
||||||
|
"internal": {},
|
||||||
|
"external": {},
|
||||||
|
"proxy": {},
|
||||||
|
"cache": {},
|
||||||
|
"debug": {},
|
||||||
|
"metrics": {},
|
||||||
|
"monitoring": {},
|
||||||
|
"graphql": {},
|
||||||
|
"rest": {},
|
||||||
|
"rpc": {},
|
||||||
|
"socket": {},
|
||||||
|
"ws": {},
|
||||||
|
"wss": {},
|
||||||
|
"app": {},
|
||||||
|
"apps": {},
|
||||||
|
"mobile": {},
|
||||||
|
"desktop": {},
|
||||||
|
"embed": {},
|
||||||
|
"widget": {},
|
||||||
|
"docs": {},
|
||||||
|
"documentation": {},
|
||||||
|
"wiki": {},
|
||||||
|
"forum": {},
|
||||||
|
"community": {},
|
||||||
|
"feedback": {},
|
||||||
|
"report": {},
|
||||||
|
"abuse": {},
|
||||||
|
"spam": {},
|
||||||
|
"security": {},
|
||||||
|
"verify": {},
|
||||||
|
"confirm": {},
|
||||||
|
"reset": {},
|
||||||
|
"password": {},
|
||||||
|
"recovery": {},
|
||||||
|
"unsubscribe": {},
|
||||||
|
"subscribe": {},
|
||||||
|
"notifications": {},
|
||||||
|
"alerts": {},
|
||||||
|
"messages": {},
|
||||||
|
"inbox": {},
|
||||||
|
"outbox": {},
|
||||||
|
"sent": {},
|
||||||
|
"draft": {},
|
||||||
|
"trash": {},
|
||||||
|
"archive": {},
|
||||||
|
"search": {},
|
||||||
|
"explore": {},
|
||||||
|
"discover": {},
|
||||||
|
"trending": {},
|
||||||
|
"popular": {},
|
||||||
|
"featured": {},
|
||||||
|
"new": {},
|
||||||
|
"latest": {},
|
||||||
|
"top": {},
|
||||||
|
"best": {},
|
||||||
|
"hot": {},
|
||||||
|
"random": {},
|
||||||
|
"all": {},
|
||||||
|
"any": {},
|
||||||
|
"none": {},
|
||||||
|
"true": {},
|
||||||
|
"false": {},
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -2,11 +2,8 @@ package lifecycle
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
"net"
|
"net"
|
||||||
"time"
|
|
||||||
portUtil "tunnel_pls/internal/port"
|
portUtil "tunnel_pls/internal/port"
|
||||||
"tunnel_pls/session/slug"
|
"tunnel_pls/session/slug"
|
||||||
"tunnel_pls/types"
|
"tunnel_pls/types"
|
||||||
@@ -14,10 +11,6 @@ import (
|
|||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Interaction interface {
|
|
||||||
SendMessage(string)
|
|
||||||
}
|
|
||||||
|
|
||||||
type Forwarder interface {
|
type Forwarder interface {
|
||||||
Close() error
|
Close() error
|
||||||
GetTunnelType() types.TunnelType
|
GetTunnelType() types.TunnelType
|
||||||
@@ -25,23 +18,31 @@ type Forwarder interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Lifecycle struct {
|
type Lifecycle struct {
|
||||||
Status types.Status
|
status types.Status
|
||||||
Conn ssh.Conn
|
conn ssh.Conn
|
||||||
Channel ssh.Channel
|
channel ssh.Channel
|
||||||
|
forwarder Forwarder
|
||||||
Interaction Interaction
|
slugManager slug.Manager
|
||||||
Forwarder Forwarder
|
|
||||||
SlugManager slug.Manager
|
|
||||||
unregisterClient func(slug string)
|
unregisterClient func(slug string)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewLifecycle(conn ssh.Conn, forwarder Forwarder, slugManager slug.Manager) *Lifecycle {
|
||||||
|
return &Lifecycle{
|
||||||
|
status: "",
|
||||||
|
conn: conn,
|
||||||
|
channel: nil,
|
||||||
|
forwarder: forwarder,
|
||||||
|
slugManager: slugManager,
|
||||||
|
unregisterClient: nil,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (l *Lifecycle) SetUnregisterClient(unregisterClient func(slug string)) {
|
func (l *Lifecycle) SetUnregisterClient(unregisterClient func(slug string)) {
|
||||||
l.unregisterClient = unregisterClient
|
l.unregisterClient = unregisterClient
|
||||||
}
|
}
|
||||||
|
|
||||||
type SessionLifecycle interface {
|
type SessionLifecycle interface {
|
||||||
Close() error
|
Close() error
|
||||||
WaitForRunningStatus()
|
|
||||||
SetStatus(status types.Status)
|
SetStatus(status types.Status)
|
||||||
GetConnection() ssh.Conn
|
GetConnection() ssh.Conn
|
||||||
GetChannel() ssh.Channel
|
GetChannel() ssh.Channel
|
||||||
@@ -50,73 +51,46 @@ type SessionLifecycle interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *Lifecycle) GetChannel() ssh.Channel {
|
func (l *Lifecycle) GetChannel() ssh.Channel {
|
||||||
return l.Channel
|
return l.channel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Lifecycle) SetChannel(channel ssh.Channel) {
|
func (l *Lifecycle) SetChannel(channel ssh.Channel) {
|
||||||
l.Channel = channel
|
l.channel = channel
|
||||||
}
|
}
|
||||||
func (l *Lifecycle) GetConnection() ssh.Conn {
|
func (l *Lifecycle) GetConnection() ssh.Conn {
|
||||||
return l.Conn
|
return l.conn
|
||||||
}
|
}
|
||||||
func (l *Lifecycle) SetStatus(status types.Status) {
|
func (l *Lifecycle) SetStatus(status types.Status) {
|
||||||
l.Status = status
|
l.status = status
|
||||||
}
|
|
||||||
func (l *Lifecycle) WaitForRunningStatus() {
|
|
||||||
timeout := time.After(3 * time.Second)
|
|
||||||
ticker := time.NewTicker(150 * time.Millisecond)
|
|
||||||
defer ticker.Stop()
|
|
||||||
frames := []string{"-", "\\", "|", "/"}
|
|
||||||
i := 0
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ticker.C:
|
|
||||||
l.Interaction.SendMessage(fmt.Sprintf("\rLoading %s", frames[i]))
|
|
||||||
i = (i + 1) % len(frames)
|
|
||||||
if l.Status == types.RUNNING {
|
|
||||||
l.Interaction.SendMessage("\r\033[K")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
case <-timeout:
|
|
||||||
l.Interaction.SendMessage("\r\033[K")
|
|
||||||
l.Interaction.SendMessage("TCP/IP request not received in time.\r\nCheck your internet connection and confirm the server responds within 3000ms.\r\nEnsure you ran the correct command. For more details, visit https://tunnl.live.\r\n\r\n")
|
|
||||||
err := l.Close()
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("failed to close session: %v", err)
|
|
||||||
}
|
|
||||||
log.Println("Timeout waiting for session to start running")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Lifecycle) Close() error {
|
func (l *Lifecycle) Close() error {
|
||||||
err := l.Forwarder.Close()
|
err := l.forwarder.Close()
|
||||||
if err != nil && !errors.Is(err, net.ErrClosed) {
|
if err != nil && !errors.Is(err, net.ErrClosed) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if l.Channel != nil {
|
if l.channel != nil {
|
||||||
err := l.Channel.Close()
|
err := l.channel.Close()
|
||||||
if err != nil && !errors.Is(err, io.EOF) {
|
if err != nil && !errors.Is(err, io.EOF) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if l.Conn != nil {
|
if l.conn != nil {
|
||||||
err := l.Conn.Close()
|
err := l.conn.Close()
|
||||||
if err != nil && !errors.Is(err, net.ErrClosed) {
|
if err != nil && !errors.Is(err, net.ErrClosed) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
clientSlug := l.SlugManager.Get()
|
clientSlug := l.slugManager.Get()
|
||||||
if clientSlug != "" {
|
if clientSlug != "" {
|
||||||
l.unregisterClient(clientSlug)
|
l.unregisterClient(clientSlug)
|
||||||
}
|
}
|
||||||
|
|
||||||
if l.Forwarder.GetTunnelType() == types.TCP {
|
if l.forwarder.GetTunnelType() == types.TCP {
|
||||||
err := portUtil.Manager.SetPortStatus(l.Forwarder.GetForwardedPort(), false)
|
err := portUtil.Default.SetPortStatus(l.forwarder.GetForwardedPort(), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
package session
|
package session
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"log"
|
"log"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
"tunnel_pls/session/forwarder"
|
"tunnel_pls/session/forwarder"
|
||||||
"tunnel_pls/session/interaction"
|
"tunnel_pls/session/interaction"
|
||||||
"tunnel_pls/session/lifecycle"
|
"tunnel_pls/session/lifecycle"
|
||||||
"tunnel_pls/session/slug"
|
"tunnel_pls/session/slug"
|
||||||
"tunnel_pls/types"
|
"tunnel_pls/utils"
|
||||||
|
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
@@ -26,38 +26,33 @@ type Session interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type SSHSession struct {
|
type SSHSession struct {
|
||||||
Lifecycle lifecycle.SessionLifecycle
|
lifecycle lifecycle.SessionLifecycle
|
||||||
Interaction interaction.Controller
|
interaction interaction.Controller
|
||||||
Forwarder forwarder.ForwardingController
|
forwarder forwarder.ForwardingController
|
||||||
SlugManager slug.Manager
|
slugManager slug.Manager
|
||||||
|
}
|
||||||
|
|
||||||
channelOnce sync.Once
|
func (s *SSHSession) GetLifecycle() lifecycle.SessionLifecycle {
|
||||||
|
return s.lifecycle
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SSHSession) GetInteraction() interaction.Controller {
|
||||||
|
return s.interaction
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SSHSession) GetForwarder() forwarder.ForwardingController {
|
||||||
|
return s.forwarder
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SSHSession) GetSlugManager() slug.Manager {
|
||||||
|
return s.slugManager
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(conn *ssh.ServerConn, forwardingReq <-chan *ssh.Request, sshChan <-chan ssh.NewChannel) {
|
func New(conn *ssh.ServerConn, forwardingReq <-chan *ssh.Request, sshChan <-chan ssh.NewChannel) {
|
||||||
slugManager := slug.NewManager()
|
slugManager := slug.NewManager()
|
||||||
forwarderManager := &forwarder.Forwarder{
|
forwarderManager := forwarder.NewForwarder(slugManager)
|
||||||
Listener: nil,
|
interactionManager := interaction.NewInteraction(slugManager, forwarderManager)
|
||||||
TunnelType: "",
|
lifecycleManager := lifecycle.NewLifecycle(conn, forwarderManager, slugManager)
|
||||||
ForwardedPort: 0,
|
|
||||||
SlugManager: slugManager,
|
|
||||||
}
|
|
||||||
interactionManager := &interaction.Interaction{
|
|
||||||
CommandBuffer: bytes.NewBuffer(make([]byte, 0, 20)),
|
|
||||||
InteractiveMode: false,
|
|
||||||
EditSlug: "",
|
|
||||||
SlugManager: slugManager,
|
|
||||||
Forwarder: forwarderManager,
|
|
||||||
Lifecycle: nil,
|
|
||||||
}
|
|
||||||
lifecycleManager := &lifecycle.Lifecycle{
|
|
||||||
Status: "",
|
|
||||||
Conn: conn,
|
|
||||||
Channel: nil,
|
|
||||||
Interaction: interactionManager,
|
|
||||||
Forwarder: forwarderManager,
|
|
||||||
SlugManager: slugManager,
|
|
||||||
}
|
|
||||||
|
|
||||||
interactionManager.SetLifecycle(lifecycleManager)
|
interactionManager.SetLifecycle(lifecycleManager)
|
||||||
interactionManager.SetSlugModificator(updateClientSlug)
|
interactionManager.SetSlugModificator(updateClientSlug)
|
||||||
@@ -65,33 +60,61 @@ func New(conn *ssh.ServerConn, forwardingReq <-chan *ssh.Request, sshChan <-chan
|
|||||||
lifecycleManager.SetUnregisterClient(unregisterClient)
|
lifecycleManager.SetUnregisterClient(unregisterClient)
|
||||||
|
|
||||||
session := &SSHSession{
|
session := &SSHSession{
|
||||||
Lifecycle: lifecycleManager,
|
lifecycle: lifecycleManager,
|
||||||
Interaction: interactionManager,
|
interaction: interactionManager,
|
||||||
Forwarder: forwarderManager,
|
forwarder: forwarderManager,
|
||||||
SlugManager: slugManager,
|
slugManager: slugManager,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var once sync.Once
|
||||||
for channel := range sshChan {
|
for channel := range sshChan {
|
||||||
ch, reqs, err := channel.Accept()
|
ch, reqs, err := channel.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to accept channel: %v", err)
|
log.Printf("failed to accept channel: %v", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
session.channelOnce.Do(func() {
|
once.Do(func() {
|
||||||
session.Lifecycle.SetChannel(ch)
|
session.lifecycle.SetChannel(ch)
|
||||||
session.Interaction.SetChannel(ch)
|
session.interaction.SetChannel(ch)
|
||||||
session.Lifecycle.SetStatus(types.SETUP)
|
|
||||||
go session.HandleGlobalRequest(forwardingReq)
|
|
||||||
session.Lifecycle.WaitForRunningStatus()
|
|
||||||
})
|
|
||||||
|
|
||||||
go session.HandleGlobalRequest(reqs)
|
tcpipReq := session.waitForTCPIPForward(forwardingReq)
|
||||||
|
if tcpipReq == nil {
|
||||||
|
log.Printf("Port forwarding request not received. Ensure you ran the correct command with -R flag. Example: ssh %s -p %s -R 80:localhost:3000", utils.Getenv("DOMAIN", "localhost"), utils.Getenv("PORT", "2200"))
|
||||||
|
if err := session.lifecycle.Close(); err != nil {
|
||||||
|
log.Printf("failed to close session: %v", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
go session.HandleTCPIPForward(tcpipReq)
|
||||||
|
})
|
||||||
|
session.HandleGlobalRequest(reqs)
|
||||||
}
|
}
|
||||||
if err := session.Lifecycle.Close(); err != nil {
|
if err := session.lifecycle.Close(); err != nil {
|
||||||
log.Printf("failed to close session: %v", err)
|
log.Printf("failed to close session: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SSHSession) waitForTCPIPForward(forwardingReq <-chan *ssh.Request) *ssh.Request {
|
||||||
|
select {
|
||||||
|
case req, ok := <-forwardingReq:
|
||||||
|
if !ok {
|
||||||
|
log.Println("Forwarding request channel closed")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if req.Type == "tcpip-forward" {
|
||||||
|
return req
|
||||||
|
}
|
||||||
|
if err := req.Reply(false, nil); err != nil {
|
||||||
|
log.Printf("Failed to reply to request: %v", err)
|
||||||
|
}
|
||||||
|
log.Printf("Expected tcpip-forward request, got: %s", req.Type)
|
||||||
|
return nil
|
||||||
|
case <-time.After(500 * time.Millisecond):
|
||||||
|
log.Println("No forwarding request received")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func updateClientSlug(oldSlug, newSlug string) bool {
|
func updateClientSlug(oldSlug, newSlug string) bool {
|
||||||
clientsMutex.Lock()
|
clientsMutex.Lock()
|
||||||
defer clientsMutex.Unlock()
|
defer clientsMutex.Unlock()
|
||||||
@@ -106,7 +129,7 @@ func updateClientSlug(oldSlug, newSlug string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
delete(Clients, oldSlug)
|
delete(Clients, oldSlug)
|
||||||
client.SlugManager.Set(newSlug)
|
client.slugManager.Set(newSlug)
|
||||||
Clients[newSlug] = client
|
Clients[newSlug] = client
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,13 +15,6 @@ const (
|
|||||||
TCP TunnelType = "TCP"
|
TCP TunnelType = "TCP"
|
||||||
)
|
)
|
||||||
|
|
||||||
type InteractionType string
|
|
||||||
|
|
||||||
const (
|
|
||||||
Slug InteractionType = "SLUG"
|
|
||||||
Drop InteractionType = "DROP"
|
|
||||||
)
|
|
||||||
|
|
||||||
var BadGatewayResponse = []byte("HTTP/1.1 502 Bad Gateway\r\n" +
|
var BadGatewayResponse = []byte("HTTP/1.1 502 Bad Gateway\r\n" +
|
||||||
"Content-Length: 11\r\n" +
|
"Content-Length: 11\r\n" +
|
||||||
"Content-Type: text/plain\r\n\r\n" +
|
"Content-Type: text/plain\r\n\r\n" +
|
||||||
|
|||||||
107
utils/utils.go
107
utils/utils.go
@@ -1,30 +1,33 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/rsa"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/pem"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
mathrand "math/rand"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
|
"golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Env struct {
|
|
||||||
value map[string]string
|
|
||||||
mu sync.Mutex
|
|
||||||
}
|
|
||||||
|
|
||||||
var env *Env
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
env = &Env{value: map[string]string{}}
|
if _, err := os.Stat(".env"); err == nil {
|
||||||
|
if err := godotenv.Load(".env"); err != nil {
|
||||||
|
log.Printf("Warning: Failed to load .env file: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GenerateRandomString(length int) string {
|
func GenerateRandomString(length int) string {
|
||||||
const charset = "abcdefghijklmnopqrstuvwxyz"
|
const charset = "abcdefghijklmnopqrstuvwxyz"
|
||||||
seededRand := rand.New(rand.NewSource(time.Now().UnixNano() + int64(rand.Intn(9999))))
|
seededRand := mathrand.New(mathrand.NewSource(time.Now().UnixNano() + int64(mathrand.Intn(9999))))
|
||||||
var result strings.Builder
|
var result strings.Builder
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
randomIndex := seededRand.Intn(len(charset))
|
randomIndex := seededRand.Intn(len(charset))
|
||||||
@@ -33,26 +36,74 @@ func GenerateRandomString(length int) string {
|
|||||||
return result.String()
|
return result.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func Getenv(key string) string {
|
func Getenv(key, defaultValue string) string {
|
||||||
env.mu.Lock()
|
|
||||||
defer env.mu.Unlock()
|
|
||||||
if val, ok := env.value[key]; ok {
|
|
||||||
return val
|
|
||||||
}
|
|
||||||
|
|
||||||
if os.Getenv("HOSTNAME") == "" {
|
|
||||||
err := godotenv.Load(".env")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Error loading .env file: %s", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val := os.Getenv(key)
|
val := os.Getenv(key)
|
||||||
env.value[key] = val
|
|
||||||
|
|
||||||
if val == "" {
|
if val == "" {
|
||||||
panic("Asking for env: " + key + " but got nothing, please set your environment first")
|
val = defaultValue
|
||||||
}
|
}
|
||||||
|
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetBufferSize() int {
|
||||||
|
sizeStr := Getenv("BUFFER_SIZE", "32768")
|
||||||
|
size, err := strconv.Atoi(sizeStr)
|
||||||
|
if err != nil || size < 4096 || size > 1048576 {
|
||||||
|
return 32768
|
||||||
|
}
|
||||||
|
return size
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateSSHKeyIfNotExist(keyPath string) error {
|
||||||
|
if _, err := os.Stat(keyPath); err == nil {
|
||||||
|
log.Printf("SSH key already exists at %s", keyPath)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("SSH key not found at %s, generating new key pair...", keyPath)
|
||||||
|
|
||||||
|
privateKey, err := rsa.GenerateKey(rand.Reader, 4096)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
privateKeyPEM := &pem.Block{
|
||||||
|
Type: "RSA PRIVATE KEY",
|
||||||
|
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
|
||||||
|
}
|
||||||
|
|
||||||
|
dir := filepath.Dir(keyPath)
|
||||||
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
privateKeyFile, err := os.OpenFile(keyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer privateKeyFile.Close()
|
||||||
|
|
||||||
|
if err := pem.Encode(privateKeyFile, privateKeyPEM); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
publicKey, err := ssh.NewPublicKey(&privateKey.PublicKey)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
pubKeyPath := keyPath + ".pub"
|
||||||
|
pubKeyFile, err := os.OpenFile(pubKeyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer pubKeyFile.Close()
|
||||||
|
|
||||||
|
_, err = pubKeyFile.Write(ssh.MarshalAuthorizedKey(publicKey))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("SSH key pair generated successfully at %s and %s", keyPath, pubKeyPath)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
17
version/version.go
Normal file
17
version/version.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package version
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
var (
|
||||||
|
Version = "dev"
|
||||||
|
BuildDate = "unknown"
|
||||||
|
Commit = "unknown"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetVersion() string {
|
||||||
|
return fmt.Sprintf("tunnel_pls %s (commit: %s, built: %s)", Version, Commit, BuildDate)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetShortVersion() string {
|
||||||
|
return Version
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user