Compare commits
16 Commits
08565d845f
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 2e8767f17a | |||
| 7716eb7f29 | |||
| b115369913 | |||
| 9276430fae | |||
| f8a6f0bafe | |||
| acd02aadd3 | |||
| 878664e0ac | |||
| 20a88df330 | |||
| 075dd7ecad | |||
| ab34b34765 | |||
| 514c4f9de1 | |||
| d8330c684f | |||
| fbf182025b | |||
| 1038c0861e | |||
| 64e0d5805e | |||
| 85f21e7698 |
@@ -5,6 +5,8 @@ on:
|
|||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
- staging
|
- staging
|
||||||
|
tags:
|
||||||
|
- 'v*'
|
||||||
paths:
|
paths:
|
||||||
- '**.go'
|
- '**.go'
|
||||||
- 'go.mod'
|
- 'go.mod'
|
||||||
@@ -15,8 +17,9 @@ on:
|
|||||||
- '.gitea/workflows/build.yml'
|
- '.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
|
||||||
@@ -32,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:
|
||||||
@@ -40,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
|
||||||
@@ -50,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'
|
||||||
|
|||||||
15
Dockerfile
15
Dockerfile
@@ -1,5 +1,9 @@
|
|||||||
FROM golang:1.25.5-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 && \
|
RUN apk update && apk upgrade && \
|
||||||
apk add --no-cache ca-certificates tzdata git && \
|
apk add --no-cache ca-certificates tzdata git && \
|
||||||
update-ca-certificates
|
update-ca-certificates
|
||||||
@@ -18,7 +22,7 @@ RUN --mount=type=cache,target=/go/pkg/mod \
|
|||||||
--mount=type=cache,target=/root/.cache/go-build \
|
--mount=type=cache,target=/root/.cache/go-build \
|
||||||
CGO_ENABLED=0 GOOS=linux \
|
CGO_ENABLED=0 GOOS=linux \
|
||||||
go build -trimpath \
|
go build -trimpath \
|
||||||
-ldflags="-w -s" \
|
-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 \
|
-o /app/tunnel_pls \
|
||||||
.
|
.
|
||||||
|
|
||||||
@@ -28,6 +32,10 @@ RUN adduser -D -u 10001 -g '' appuser && \
|
|||||||
|
|
||||||
FROM scratch
|
FROM scratch
|
||||||
|
|
||||||
|
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 /etc/passwd /etc/passwd
|
COPY --from=go_builder /etc/passwd /etc/passwd
|
||||||
@@ -43,6 +51,9 @@ ENV TZ=Asia/Jakarta
|
|||||||
EXPOSE 2200 8080 8443
|
EXPOSE 2200 8080 8443
|
||||||
|
|
||||||
LABEL org.opencontainers.image.title="Tunnel Please" \
|
LABEL org.opencontainers.image.title="Tunnel Please" \
|
||||||
org.opencontainers.image.description="SSH-based tunnel server"
|
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"]
|
ENTRYPOINT ["/app/tunnel_pls"]
|
||||||
|
|||||||
20
go.mod
20
go.mod
@@ -4,17 +4,37 @@ go 1.24.4
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/caddyserver/certmagic v0.25.0
|
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
|
||||||
github.com/libdns/cloudflare v0.2.2
|
github.com/libdns/cloudflare v0.2.2
|
||||||
golang.org/x/crypto v0.46.0
|
golang.org/x/crypto v0.46.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
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/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/klauspost/cpuid/v2 v2.3.0 // indirect
|
||||||
github.com/libdns/libdns v1.1.1 // 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/mholt/acmez/v3 v3.1.3 // indirect
|
||||||
github.com/miekg/dns v1.1.68 // 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
|
github.com/zeebo/blake3 v0.2.4 // indirect
|
||||||
go.uber.org/multierr v1.11.0 // indirect
|
go.uber.org/multierr v1.11.0 // indirect
|
||||||
go.uber.org/zap v1.27.0 // indirect
|
go.uber.org/zap v1.27.0 // indirect
|
||||||
|
|||||||
51
go.sum
51
go.sum
@@ -1,27 +1,74 @@
|
|||||||
|
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 h1:VMleO/XA48gEWes5l+Fh6tRWo9bHkhwAEhx63i+F5ic=
|
||||||
github.com/caddyserver/certmagic v0.25.0/go.mod h1:m9yB7Mud24OQbPHOiipAoyKPn9pKHhpSJxXR1jydBxA=
|
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 h1:onS+pxp3M8HnHpN5MMbOMyNjmTheJyWRaZYwn+YTAyA=
|
||||||
github.com/caddyserver/zerossl v0.1.3/go.mod h1:CxA0acn7oEGO6//4rtrRjYgEoa4MFw/XofZnrYwGqG4=
|
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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
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 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
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=
|
||||||
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
|
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
|
||||||
github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
||||||
|
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||||
|
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||||
github.com/libdns/cloudflare v0.2.2 h1:XWHv+C1dDcApqazlh08Q6pjytYLgR2a+Y3xrXFu0vsI=
|
github.com/libdns/cloudflare v0.2.2 h1:XWHv+C1dDcApqazlh08Q6pjytYLgR2a+Y3xrXFu0vsI=
|
||||||
github.com/libdns/cloudflare v0.2.2/go.mod h1:w9uTmRCDlAoafAsTPnn2nJ0XHK/eaUMh86DUk8BWi60=
|
github.com/libdns/cloudflare v0.2.2/go.mod h1:w9uTmRCDlAoafAsTPnn2nJ0XHK/eaUMh86DUk8BWi60=
|
||||||
github.com/libdns/libdns v1.1.1 h1:wPrHrXILoSHKWJKGd0EiAVmiJbFShguILTg9leS/P/U=
|
github.com/libdns/libdns v1.1.1 h1:wPrHrXILoSHKWJKGd0EiAVmiJbFShguILTg9leS/P/U=
|
||||||
github.com/libdns/libdns v1.1.1/go.mod h1:4Bj9+5CQiNMVGf87wjX4CY3HQJypUHRuLvlsfsZqLWQ=
|
github.com/libdns/libdns v1.1.1/go.mod h1:4Bj9+5CQiNMVGf87wjX4CY3HQJypUHRuLvlsfsZqLWQ=
|
||||||
|
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||||
|
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||||
|
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 h1:gUl789rjbJSuM5hYzOFnNaGgWPV1xVfnOs59o0dZEcc=
|
||||||
github.com/mholt/acmez/v3 v3.1.3/go.mod h1:L1wOU06KKvq7tswuMDwKdcHeKpFFgkppZy/y0DFxagQ=
|
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 h1:jsSRkNozw7G/mnmXULynzMNIsgY2dHC8LO6U6Ij2JEA=
|
||||||
github.com/miekg/dns v1.1.68/go.mod h1:fujopn7TB3Pu3JM69XaawiU0wqjpL9/8xGop5UrTPps=
|
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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
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 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
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 h1:hU1L1vLTHsnO8x8c9KAR5GmM5QscxHg5RNU5z5qbUWY=
|
||||||
github.com/zeebo/assert v1.1.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
|
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 h1:KYQPkhpRtcqh0ssGYcKLG1JYvddkEA8QwCM/yBqhaZI=
|
||||||
@@ -38,12 +85,16 @@ go.uber.org/zap/exp v0.3.0 h1:6JYzdifzYkGmTdRR59oYH+Ng7k49H9qVpWwNSsGJj3U=
|
|||||||
go.uber.org/zap/exp v0.3.0/go.mod h1:5I384qq7XGxYyByIhHm6jg5CHkGY0nsTfbDLgDDlgJQ=
|
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 h1:cKRW/pmt1pKAfetfu+RCEvjvZkA9RimPbh7bhFjGVBU=
|
||||||
golang.org/x/crypto v0.46.0/go.mod h1:Evb/oLKmMraqjZ2iQTwDwvCtJkczlDuTmdJXoZVzqU0=
|
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 h1:fDEXFVZ/fmCKProc/yAXXUijritrDzahmwwefnjoPFk=
|
||||||
golang.org/x/mod v0.30.0/go.mod h1:lAsf5O2EvJeSFMiBxXDki7sCgAxEUcZHXoXMKT4GJKc=
|
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 h1:Mx+4dIFzqraBXUugkia1OOvlD6LemFo1ALMHjrXDOhY=
|
||||||
golang.org/x/net v0.47.0/go.mod h1:/jNxtkgq5yWUGYkaZGqo27cfGZ1c5Nen03aYrrKpVRU=
|
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 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
|
||||||
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
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 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
|
||||||
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
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 h1:PQ5pkm/rLO6HnxFR7N2lJHOZX6Kez5Y1gDSJla6jo7Q=
|
||||||
|
|||||||
35
internal/config/config.go
Normal file
35
internal/config/config.go
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/joho/godotenv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
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 Getenv(key, defaultValue string) string {
|
||||||
|
val := os.Getenv(key)
|
||||||
|
if val == "" {
|
||||||
|
val = defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package utils
|
package key
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
@@ -6,54 +6,12 @@ import (
|
|||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"log"
|
"log"
|
||||||
mathrand "math/rand"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
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 {
|
|
||||||
const charset = "abcdefghijklmnopqrstuvwxyz"
|
|
||||||
seededRand := mathrand.New(mathrand.NewSource(time.Now().UnixNano() + int64(mathrand.Intn(9999))))
|
|
||||||
var result strings.Builder
|
|
||||||
for i := 0; i < length; i++ {
|
|
||||||
randomIndex := seededRand.Intn(len(charset))
|
|
||||||
result.WriteString(string(charset[randomIndex]))
|
|
||||||
}
|
|
||||||
return result.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
func Getenv(key, defaultValue string) string {
|
|
||||||
val := os.Getenv(key)
|
|
||||||
if val == "" {
|
|
||||||
val = defaultValue
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
func GenerateSSHKeyIfNotExist(keyPath string) error {
|
||||||
if _, err := os.Stat(keyPath); err == nil {
|
if _, err := os.Stat(keyPath); err == nil {
|
||||||
log.Printf("SSH key already exists at %s", keyPath)
|
log.Printf("SSH key already exists at %s", keyPath)
|
||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"tunnel_pls/utils"
|
"tunnel_pls/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Manager interface {
|
type Manager interface {
|
||||||
@@ -28,7 +28,7 @@ var Default Manager = &manager{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rawRange := utils.Getenv("ALLOWED_PORTS", "")
|
rawRange := config.Getenv("ALLOWED_PORTS", "")
|
||||||
if rawRange == "" {
|
if rawRange == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
18
internal/random/random.go
Normal file
18
internal/random/random.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package random
|
||||||
|
|
||||||
|
import (
|
||||||
|
mathrand "math/rand"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GenerateRandomString(length int) string {
|
||||||
|
const charset = "abcdefghijklmnopqrstuvwxyz"
|
||||||
|
seededRand := mathrand.New(mathrand.NewSource(time.Now().UnixNano() + int64(mathrand.Intn(9999))))
|
||||||
|
var result strings.Builder
|
||||||
|
for i := 0; i < length; i++ {
|
||||||
|
randomIndex := seededRand.Intn(len(charset))
|
||||||
|
result.WriteString(string(charset[randomIndex]))
|
||||||
|
}
|
||||||
|
return result.String()
|
||||||
|
}
|
||||||
27
main.go
27
main.go
@@ -6,19 +6,29 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
_ "net/http/pprof"
|
_ "net/http/pprof"
|
||||||
"os"
|
"os"
|
||||||
|
"tunnel_pls/internal/config"
|
||||||
|
"tunnel_pls/internal/key"
|
||||||
"tunnel_pls/server"
|
"tunnel_pls/server"
|
||||||
"tunnel_pls/utils"
|
"tunnel_pls/session"
|
||||||
|
"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)
|
||||||
|
|
||||||
pprofEnabled := utils.Getenv("PPROF_ENABLED", "false")
|
log.Printf("Starting %s", version.GetVersion())
|
||||||
|
|
||||||
|
pprofEnabled := config.Getenv("PPROF_ENABLED", "false")
|
||||||
if pprofEnabled == "true" {
|
if pprofEnabled == "true" {
|
||||||
pprofPort := utils.Getenv("PPROF_PORT", "6060")
|
pprofPort := config.Getenv("PPROF_PORT", "6060")
|
||||||
go func() {
|
go func() {
|
||||||
pprofAddr := fmt.Sprintf("localhost:%s", pprofPort)
|
pprofAddr := fmt.Sprintf("localhost:%s", pprofPort)
|
||||||
log.Printf("Starting pprof server on http://%s/debug/pprof/", pprofAddr)
|
log.Printf("Starting pprof server on http://%s/debug/pprof/", pprofAddr)
|
||||||
@@ -30,11 +40,11 @@ func main() {
|
|||||||
|
|
||||||
sshConfig := &ssh.ServerConfig{
|
sshConfig := &ssh.ServerConfig{
|
||||||
NoClientAuth: true,
|
NoClientAuth: true,
|
||||||
ServerVersion: "SSH-2.0-TunnlPls-1.0",
|
ServerVersion: fmt.Sprintf("SSH-2.0-TunnlPls-%s", version.GetShortVersion()),
|
||||||
}
|
}
|
||||||
|
|
||||||
sshKeyPath := "certs/ssh/id_rsa"
|
sshKeyPath := "certs/ssh/id_rsa"
|
||||||
if err := utils.GenerateSSHKeyIfNotExist(sshKeyPath); err != nil {
|
if err := key.GenerateSSHKeyIfNotExist(sshKeyPath); err != nil {
|
||||||
log.Fatalf("Failed to generate SSH key: %s", err)
|
log.Fatalf("Failed to generate SSH key: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,6 +59,11 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sshConfig.AddHostKey(private)
|
sshConfig.AddHostKey(private)
|
||||||
app := server.NewServer(sshConfig)
|
sessionRegistry := session.NewRegistry()
|
||||||
|
|
||||||
|
app, err := server.NewServer(sshConfig, sessionRegistry)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to start server: %s", err)
|
||||||
|
}
|
||||||
app.Start()
|
app.Start()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
"digest"
|
"digest"
|
||||||
],
|
],
|
||||||
"automerge": true,
|
"automerge": true,
|
||||||
"baseBranches": [
|
"baseBranchPatterns": [
|
||||||
"staging"
|
"staging"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
package server
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log"
|
|
||||||
"net"
|
|
||||||
"tunnel_pls/session"
|
|
||||||
|
|
||||||
"golang.org/x/crypto/ssh"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (s *Server) handleConnection(conn net.Conn) {
|
|
||||||
sshConn, chans, forwardingReqs, err := ssh.NewServerConn(conn, s.config)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("failed to establish SSH connection: %v", err)
|
|
||||||
err := conn.Close()
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("failed to close SSH connection: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println("SSH connection established:", sshConn.User())
|
|
||||||
|
|
||||||
session.New(sshConn, forwardingReqs, chans)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
130
server/header.go
130
server/header.go
@@ -4,7 +4,6 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type HeaderManager interface {
|
type HeaderManager interface {
|
||||||
@@ -44,43 +43,132 @@ type requestHeaderFactory struct {
|
|||||||
headers map[string]string
|
headers map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRequestHeaderFactory(br *bufio.Reader) (RequestHeaderManager, error) {
|
func NewRequestHeaderFactory(r interface{}) (RequestHeaderManager, error) {
|
||||||
|
switch v := r.(type) {
|
||||||
|
case []byte:
|
||||||
|
return parseHeadersFromBytes(v)
|
||||||
|
case *bufio.Reader:
|
||||||
|
return parseHeadersFromReader(v)
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("unsupported type: %T", r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseHeadersFromBytes(headerData []byte) (RequestHeaderManager, error) {
|
||||||
header := &requestHeaderFactory{
|
header := &requestHeaderFactory{
|
||||||
headers: make(map[string]string),
|
headers: make(map[string]string, 16),
|
||||||
}
|
}
|
||||||
|
|
||||||
startLine, err := br.ReadString('\n')
|
lineEnd := bytes.IndexByte(headerData, '\n')
|
||||||
if err != nil {
|
if lineEnd == -1 {
|
||||||
return nil, err
|
return nil, fmt.Errorf("invalid request: no newline found")
|
||||||
}
|
}
|
||||||
startLine = strings.TrimRight(startLine, "\r\n")
|
|
||||||
header.startLine = []byte(startLine)
|
|
||||||
|
|
||||||
parts := strings.Split(startLine, " ")
|
startLine := bytes.TrimRight(headerData[:lineEnd], "\r\n")
|
||||||
|
header.startLine = make([]byte, len(startLine))
|
||||||
|
copy(header.startLine, startLine)
|
||||||
|
|
||||||
|
parts := bytes.Split(startLine, []byte{' '})
|
||||||
if len(parts) < 3 {
|
if len(parts) < 3 {
|
||||||
return nil, fmt.Errorf("invalid request line")
|
return nil, fmt.Errorf("invalid request line")
|
||||||
}
|
}
|
||||||
|
|
||||||
header.method = parts[0]
|
header.method = string(parts[0])
|
||||||
header.path = parts[1]
|
header.path = string(parts[1])
|
||||||
header.version = parts[2]
|
header.version = string(parts[2])
|
||||||
|
|
||||||
for {
|
remaining := headerData[lineEnd+1:]
|
||||||
line, err := br.ReadString('\n')
|
|
||||||
if err != nil {
|
for len(remaining) > 0 {
|
||||||
return nil, err
|
lineEnd = bytes.IndexByte(remaining, '\n')
|
||||||
|
if lineEnd == -1 {
|
||||||
|
lineEnd = len(remaining)
|
||||||
}
|
}
|
||||||
line = strings.TrimRight(line, "\r\n")
|
|
||||||
|
|
||||||
if line == "" {
|
line := bytes.TrimRight(remaining[:lineEnd], "\r\n")
|
||||||
|
|
||||||
|
if len(line) == 0 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
kv := strings.SplitN(line, ":", 2)
|
colonIdx := bytes.IndexByte(line, ':')
|
||||||
if len(kv) != 2 {
|
if colonIdx != -1 {
|
||||||
|
key := bytes.TrimSpace(line[:colonIdx])
|
||||||
|
value := bytes.TrimSpace(line[colonIdx+1:])
|
||||||
|
header.headers[string(key)] = string(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
if lineEnd == len(remaining) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
remaining = remaining[lineEnd+1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
return header, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseHeadersFromReader(br *bufio.Reader) (RequestHeaderManager, error) {
|
||||||
|
header := &requestHeaderFactory{
|
||||||
|
headers: make(map[string]string, 16),
|
||||||
|
}
|
||||||
|
|
||||||
|
startLineBytes, err := br.ReadSlice('\n')
|
||||||
|
if err != nil {
|
||||||
|
if err == bufio.ErrBufferFull {
|
||||||
|
var startLine string
|
||||||
|
startLine, err = br.ReadString('\n')
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
startLineBytes = []byte(startLine)
|
||||||
|
} else {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
startLineBytes = bytes.TrimRight(startLineBytes, "\r\n")
|
||||||
|
header.startLine = make([]byte, len(startLineBytes))
|
||||||
|
copy(header.startLine, startLineBytes)
|
||||||
|
|
||||||
|
parts := bytes.Split(startLineBytes, []byte{' '})
|
||||||
|
if len(parts) < 3 {
|
||||||
|
return nil, fmt.Errorf("invalid request line")
|
||||||
|
}
|
||||||
|
|
||||||
|
header.method = string(parts[0])
|
||||||
|
header.path = string(parts[1])
|
||||||
|
header.version = string(parts[2])
|
||||||
|
|
||||||
|
for {
|
||||||
|
lineBytes, err := br.ReadSlice('\n')
|
||||||
|
if err != nil {
|
||||||
|
if err == bufio.ErrBufferFull {
|
||||||
|
var line string
|
||||||
|
line, err = br.ReadString('\n')
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
lineBytes = []byte(line)
|
||||||
|
} else {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lineBytes = bytes.TrimRight(lineBytes, "\r\n")
|
||||||
|
|
||||||
|
if len(lineBytes) == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
colonIdx := bytes.IndexByte(lineBytes, ':')
|
||||||
|
if colonIdx == -1 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
header.headers[strings.TrimSpace(kv[0])] = strings.TrimSpace(kv[1])
|
|
||||||
|
key := bytes.TrimSpace(lineBytes[:colonIdx])
|
||||||
|
value := bytes.TrimSpace(lineBytes[colonIdx+1:])
|
||||||
|
|
||||||
|
header.headers[string(key)] = string(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
return header, nil
|
return header, nil
|
||||||
|
|||||||
@@ -11,21 +11,15 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"tunnel_pls/internal/config"
|
||||||
"tunnel_pls/session"
|
"tunnel_pls/session"
|
||||||
"tunnel_pls/utils"
|
|
||||||
|
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Interaction interface {
|
|
||||||
SendMessage(message string)
|
|
||||||
}
|
|
||||||
|
|
||||||
type HTTPWriter interface {
|
type HTTPWriter interface {
|
||||||
io.Reader
|
io.Reader
|
||||||
io.Writer
|
io.Writer
|
||||||
SetInteraction(interaction Interaction)
|
|
||||||
AddInteraction(interaction Interaction)
|
|
||||||
GetRemoteAddr() net.Addr
|
GetRemoteAddr() net.Addr
|
||||||
GetWriter() io.Writer
|
GetWriter() io.Writer
|
||||||
AddResponseMiddleware(mw ResponseMiddleware)
|
AddResponseMiddleware(mw ResponseMiddleware)
|
||||||
@@ -35,21 +29,16 @@ type HTTPWriter interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type customWriter struct {
|
type customWriter struct {
|
||||||
remoteAddr net.Addr
|
remoteAddr net.Addr
|
||||||
writer io.Writer
|
writer io.Writer
|
||||||
reader io.Reader
|
reader io.Reader
|
||||||
headerBuf []byte
|
headerBuf []byte
|
||||||
buf []byte
|
buf []byte
|
||||||
respHeader ResponseHeaderManager
|
respHeader ResponseHeaderManager
|
||||||
reqHeader RequestHeaderManager
|
reqHeader RequestHeaderManager
|
||||||
interaction Interaction
|
respMW []ResponseMiddleware
|
||||||
respMW []ResponseMiddleware
|
reqStartMW []RequestMiddleware
|
||||||
reqStartMW []RequestMiddleware
|
reqEndMW []RequestMiddleware
|
||||||
reqEndMW []RequestMiddleware
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cw *customWriter) SetInteraction(interaction Interaction) {
|
|
||||||
cw.interaction = interaction
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cw *customWriter) GetRemoteAddr() net.Addr {
|
func (cw *customWriter) GetRemoteAddr() net.Addr {
|
||||||
@@ -110,8 +99,7 @@ func (cw *customWriter) Read(p []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
headerReader := bufio.NewReader(bytes.NewReader(header))
|
reqhf, err := NewRequestHeaderFactory(header)
|
||||||
reqhf, err := NewRequestHeaderFactory(headerReader)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
@@ -135,11 +123,10 @@ func (cw *customWriter) Read(p []byte) (int, error) {
|
|||||||
|
|
||||||
func NewCustomWriter(writer io.Writer, reader io.Reader, remoteAddr net.Addr) HTTPWriter {
|
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),
|
||||||
interaction: nil,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,19 +211,29 @@ func (cw *customWriter) Write(p []byte) (int, error) {
|
|||||||
return len(p), nil
|
return len(p), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cw *customWriter) AddInteraction(interaction Interaction) {
|
|
||||||
cw.interaction = interaction
|
|
||||||
}
|
|
||||||
|
|
||||||
var redirectTLS = false
|
var redirectTLS = false
|
||||||
|
|
||||||
func NewHTTPServer() error {
|
type HTTPServer interface {
|
||||||
httpPort := utils.Getenv("HTTP_PORT", "8080")
|
ListenAndServe() error
|
||||||
|
ListenAndServeTLS() error
|
||||||
|
handler(conn net.Conn)
|
||||||
|
handlerTLS(conn net.Conn)
|
||||||
|
}
|
||||||
|
type httpServer struct {
|
||||||
|
sessionRegistry session.Registry
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHTTPServer(sessionRegistry session.Registry) HTTPServer {
|
||||||
|
return &httpServer{sessionRegistry: sessionRegistry}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hs *httpServer) ListenAndServe() error {
|
||||||
|
httpPort := config.Getenv("HTTP_PORT", "8080")
|
||||||
listener, err := net.Listen("tcp", ":"+httpPort)
|
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", "false") == "true" && utils.Getenv("TLS_REDIRECT", "false") == "true" {
|
if config.Getenv("TLS_ENABLED", "false") == "true" && config.Getenv("TLS_REDIRECT", "false") == "true" {
|
||||||
redirectTLS = true
|
redirectTLS = true
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
@@ -251,13 +248,13 @@ func NewHTTPServer() error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
go Handler(conn)
|
go hs.handler(conn)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Handler(conn net.Conn) {
|
func (hs *httpServer) handler(conn net.Conn) {
|
||||||
defer func() {
|
defer func() {
|
||||||
err := conn.Close()
|
err := conn.Close()
|
||||||
if err != nil && !errors.Is(err, net.ErrClosed) {
|
if err != nil && !errors.Is(err, net.ErrClosed) {
|
||||||
@@ -288,7 +285,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", "localhost")) +
|
fmt.Sprintf("Location: https://%s.%s/\r\n", slug, config.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"))
|
||||||
@@ -316,8 +313,8 @@ func Handler(conn net.Conn) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sshSession, ok := session.Clients[slug]
|
sshSession, exist := hs.sessionRegistry.Get(slug)
|
||||||
if !ok {
|
if !exist {
|
||||||
_, 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://tunnl.live/tunnel-not-found?slug=%s\r\n", slug) +
|
fmt.Sprintf("Location: https://tunnl.live/tunnel-not-found?slug=%s\r\n", slug) +
|
||||||
"Content-Length: 0\r\n" +
|
"Content-Length: 0\r\n" +
|
||||||
@@ -330,7 +327,6 @@ func Handler(conn net.Conn) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
cw := NewCustomWriter(conn, dstReader, conn.RemoteAddr())
|
cw := NewCustomWriter(conn, dstReader, conn.RemoteAddr())
|
||||||
cw.SetInteraction(sshSession.GetInteraction())
|
|
||||||
forwardRequest(cw, reqhf, sshSession)
|
forwardRequest(cw, reqhf, sshSession)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,13 +8,12 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
"tunnel_pls/session"
|
"tunnel_pls/internal/config"
|
||||||
"tunnel_pls/utils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewHTTPSServer() error {
|
func (hs *httpServer) ListenAndServeTLS() error {
|
||||||
domain := utils.Getenv("DOMAIN", "localhost")
|
domain := config.Getenv("DOMAIN", "localhost")
|
||||||
httpsPort := utils.Getenv("HTTPS_PORT", "8443")
|
httpsPort := config.Getenv("HTTPS_PORT", "8443")
|
||||||
|
|
||||||
tlsConfig, err := NewTLSConfig(domain)
|
tlsConfig, err := NewTLSConfig(domain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -38,13 +37,13 @@ func NewHTTPSServer() error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
go HandlerTLS(conn)
|
go hs.handlerTLS(conn)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandlerTLS(conn net.Conn) {
|
func (hs *httpServer) handlerTLS(conn net.Conn) {
|
||||||
defer func() {
|
defer func() {
|
||||||
err := conn.Close()
|
err := conn.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -90,8 +89,8 @@ func HandlerTLS(conn net.Conn) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sshSession, ok := session.Clients[slug]
|
sshSession, exist := hs.sessionRegistry.Get(slug)
|
||||||
if !ok {
|
if !exist {
|
||||||
_, 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://tunnl.live/tunnel-not-found?slug=%s\r\n", slug) +
|
fmt.Sprintf("Location: https://tunnl.live/tunnel-not-found?slug=%s\r\n", slug) +
|
||||||
"Content-Length: 0\r\n" +
|
"Content-Length: 0\r\n" +
|
||||||
@@ -104,7 +103,6 @@ func HandlerTLS(conn net.Conn) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
cw := NewCustomWriter(conn, dstReader, conn.RemoteAddr())
|
cw := NewCustomWriter(conn, dstReader, conn.RemoteAddr())
|
||||||
cw.SetInteraction(sshSession.GetInteraction())
|
|
||||||
forwardRequest(cw, reqhf, sshSession)
|
forwardRequest(cw, reqhf, sshSession)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,50 +4,45 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"tunnel_pls/internal/config"
|
||||||
"tunnel_pls/utils"
|
"tunnel_pls/session"
|
||||||
|
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
conn *net.Listener
|
conn *net.Listener
|
||||||
config *ssh.ServerConfig
|
config *ssh.ServerConfig
|
||||||
httpServer *http.Server
|
sessionRegistry session.Registry
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) GetConn() *net.Listener {
|
func NewServer(sshConfig *ssh.ServerConfig, sessionRegistry session.Registry) (*Server, error) {
|
||||||
return s.conn
|
listener, err := net.Listen("tcp", fmt.Sprintf(":%s", config.Getenv("PORT", "2200")))
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) GetConfig() *ssh.ServerConfig {
|
|
||||||
return s.config
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) GetHttpServer() *http.Server {
|
|
||||||
return s.httpServer
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewServer(config *ssh.ServerConfig) *Server {
|
|
||||||
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, err
|
||||||
}
|
}
|
||||||
if utils.Getenv("TLS_ENABLED", "false") == "true" {
|
|
||||||
err = NewHTTPSServer()
|
HttpServer := NewHTTPServer(sessionRegistry)
|
||||||
if err != nil {
|
err = HttpServer.ListenAndServe()
|
||||||
log.Fatalf("failed to start https server: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
err = NewHTTPServer()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to start http server: %v", err)
|
log.Fatalf("failed to start http server: %v", err)
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if config.Getenv("TLS_ENABLED", "false") == "true" {
|
||||||
|
err = HttpServer.ListenAndServeTLS()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed to start https server: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return &Server{
|
return &Server{
|
||||||
conn: &listener,
|
conn: &listener,
|
||||||
config: config,
|
config: sshConfig,
|
||||||
}
|
sessionRegistry: sessionRegistry,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Start() {
|
func (s *Server) Start() {
|
||||||
@@ -62,3 +57,26 @@ func (s *Server) Start() {
|
|||||||
go s.handleConnection(conn)
|
go s.handleConnection(conn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) handleConnection(conn net.Conn) {
|
||||||
|
sshConn, chans, forwardingReqs, err := ssh.NewServerConn(conn, s.config)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("failed to establish SSH connection: %v", err)
|
||||||
|
err := conn.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("failed to close SSH connection: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("SSH connection established:", sshConn.User())
|
||||||
|
|
||||||
|
sshSession := session.New(sshConn, forwardingReqs, chans, s.sessionRegistry)
|
||||||
|
err = sshSession.Start()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("SSH session ended with error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
"tunnel_pls/utils"
|
"tunnel_pls/internal/config"
|
||||||
|
|
||||||
"github.com/caddyserver/certmagic"
|
"github.com/caddyserver/certmagic"
|
||||||
"github.com/libdns/cloudflare"
|
"github.com/libdns/cloudflare"
|
||||||
@@ -92,7 +92,7 @@ func NewTLSConfig(domain string) (*tls.Config, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func isACMEConfigComplete() bool {
|
func isACMEConfigComplete() bool {
|
||||||
cfAPIToken := utils.Getenv("CF_API_TOKEN", "")
|
cfAPIToken := config.Getenv("CF_API_TOKEN", "")
|
||||||
return cfAPIToken != ""
|
return cfAPIToken != ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,9 +241,9 @@ func (tm *tlsManager) initCertMagic() error {
|
|||||||
return fmt.Errorf("failed to create cert storage directory: %w", err)
|
return fmt.Errorf("failed to create cert storage directory: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
acmeEmail := utils.Getenv("ACME_EMAIL", "admin@"+tm.domain)
|
acmeEmail := config.Getenv("ACME_EMAIL", "admin@"+tm.domain)
|
||||||
cfAPIToken := utils.Getenv("CF_API_TOKEN", "")
|
cfAPIToken := config.Getenv("CF_API_TOKEN", "")
|
||||||
acmeStaging := utils.Getenv("ACME_STAGING", "false") == "true"
|
acmeStaging := config.Getenv("ACME_STAGING", "false") == "true"
|
||||||
|
|
||||||
if cfAPIToken == "" {
|
if cfAPIToken == "" {
|
||||||
return fmt.Errorf("CF_API_TOKEN environment variable is required for automatic certificate generation")
|
return fmt.Errorf("CF_API_TOKEN environment variable is required for automatic certificate generation")
|
||||||
@@ -301,7 +301,22 @@ func (tm *tlsManager) initCertMagic() error {
|
|||||||
func (tm *tlsManager) getTLSConfig() *tls.Config {
|
func (tm *tlsManager) getTLSConfig() *tls.Config {
|
||||||
return &tls.Config{
|
return &tls.Config{
|
||||||
GetCertificate: tm.getCertificate,
|
GetCertificate: tm.getCertificate,
|
||||||
MinVersion: tls.VersionTLS12,
|
MinVersion: tls.VersionTLS13,
|
||||||
|
MaxVersion: tls.VersionTLS13,
|
||||||
|
|
||||||
|
SessionTicketsDisabled: false,
|
||||||
|
|
||||||
|
CipherSuites: []uint16{
|
||||||
|
tls.TLS_AES_128_GCM_SHA256,
|
||||||
|
tls.TLS_CHACHA20_POLY1305_SHA256,
|
||||||
|
},
|
||||||
|
|
||||||
|
CurvePreferences: []tls.CurveID{
|
||||||
|
tls.X25519,
|
||||||
|
},
|
||||||
|
|
||||||
|
ClientAuth: tls.NoClientCert,
|
||||||
|
NextProtos: nil,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,16 +10,16 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
"tunnel_pls/internal/config"
|
||||||
"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{
|
var bufferPool = sync.Pool{
|
||||||
New: func() interface{} {
|
New: func() interface{} {
|
||||||
bufSize := utils.GetBufferSize()
|
bufSize := config.GetBufferSize()
|
||||||
return make([]byte, bufSize)
|
return make([]byte, bufSize)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -152,25 +152,26 @@ func (f *Forwarder) HandleConnection(dst io.ReadWriter, src ssh.Channel, remoteA
|
|||||||
|
|
||||||
log.Printf("Handling new forwarded connection from %s", remoteAddr)
|
log.Printf("Handling new forwarded connection from %s", remoteAddr)
|
||||||
|
|
||||||
done := make(chan struct{}, 2)
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(2)
|
||||||
go func() {
|
|
||||||
_, err := copyWithBuffer(src, dst)
|
|
||||||
if err != nil && !errors.Is(err, io.EOF) && !errors.Is(err, net.ErrClosed) {
|
|
||||||
log.Printf("Error copying from conn.Reader to channel: %v", err)
|
|
||||||
}
|
|
||||||
done <- struct{}{}
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
_, err := copyWithBuffer(dst, src)
|
_, err := copyWithBuffer(dst, src)
|
||||||
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 channel to conn.Writer: %v", err)
|
log.Printf("Error copying src→dst: %v", err)
|
||||||
}
|
}
|
||||||
done <- struct{}{}
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
<-done
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
_, err := copyWithBuffer(src, dst)
|
||||||
|
if err != nil && !errors.Is(err, io.EOF) && !errors.Is(err, net.ErrClosed) {
|
||||||
|
log.Printf("Error copying dst→src: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Forwarder) SetType(tunnelType types.TunnelType) {
|
func (f *Forwarder) SetType(tunnelType types.TunnelType) {
|
||||||
|
|||||||
@@ -7,10 +7,9 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
portUtil "tunnel_pls/internal/port"
|
portUtil "tunnel_pls/internal/port"
|
||||||
|
"tunnel_pls/internal/random"
|
||||||
"tunnel_pls/types"
|
"tunnel_pls/types"
|
||||||
|
|
||||||
"tunnel_pls/utils"
|
|
||||||
|
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -19,7 +18,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 "shell", "pty-req", "window-change":
|
case "shell", "pty-req":
|
||||||
|
err := req.Reply(true, nil)
|
||||||
|
if err != nil {
|
||||||
|
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)
|
||||||
@@ -59,7 +79,6 @@ 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)
|
||||||
@@ -73,7 +92,7 @@ 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)
|
||||||
@@ -87,9 +106,8 @@ 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)
|
||||||
@@ -105,25 +123,12 @@ 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.Default.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.Default.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)
|
||||||
@@ -135,26 +140,32 @@ func (s *SSHSession) HandleTCPIPForward(req *ssh.Request) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err := portUtil.Default.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)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SSHSession) HandleHTTPForward(req *ssh.Request, portToBind uint16) {
|
func (s *SSHSession) HandleHTTPForward(req *ssh.Request, portToBind uint16) {
|
||||||
slug := generateUniqueSlug()
|
slug := random.GenerateRandomString(20)
|
||||||
if slug == "" {
|
|
||||||
err := req.Reply(false, nil)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Failed to reply to request:", err)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if !registerClient(slug, s) {
|
if !s.registry.Register(slug, s) {
|
||||||
log.Printf("Failed to register client with slug: %s", slug)
|
log.Printf("Failed to register client with slug: %s", slug)
|
||||||
err := req.Reply(false, nil)
|
err := req.Reply(false, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -167,7 +178,7 @@ func (s *SSHSession) HandleHTTPForward(req *ssh.Request, portToBind uint16) {
|
|||||||
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)
|
||||||
unregisterClient(slug)
|
s.registry.Remove(slug)
|
||||||
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)
|
||||||
@@ -176,16 +187,10 @@ 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", "localhost")
|
|
||||||
protocol := "http"
|
|
||||||
if utils.Getenv("TLS_ENABLED", "false") == "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)
|
||||||
unregisterClient(slug)
|
s.registry.Remove(slug)
|
||||||
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)
|
||||||
@@ -196,18 +201,15 @@ func (s *SSHSession) HandleHTTPForward(req *ssh.Request, portToBind uint16) {
|
|||||||
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.interaction.ShowWelcomeMessage()
|
|
||||||
s.interaction.SendMessage(fmt.Sprintf("Forwarding your traffic to %s://%s.%s\r\n", protocol, slug, domain))
|
|
||||||
s.lifecycle.SetStatus(types.RUNNING)
|
s.lifecycle.SetStatus(types.RUNNING)
|
||||||
s.interaction.HandleUserInput()
|
s.interaction.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
if setErr := portUtil.Default.SetPortStatus(portToBind, false); setErr != nil {
|
||||||
log.Printf("Failed to reset port status: %v", setErr)
|
log.Printf("Failed to reset port status: %v", setErr)
|
||||||
}
|
}
|
||||||
@@ -256,31 +258,9 @@ func (s *SSHSession) HandleTCPForward(req *ssh.Request, addr string, portToBind
|
|||||||
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.interaction.ShowWelcomeMessage()
|
|
||||||
s.interaction.SendMessage(fmt.Sprintf("Forwarding your traffic to tcp://%s:%d \r\n", utils.Getenv("DOMAIN", "localhost"), s.forwarder.GetForwardedPort()))
|
|
||||||
s.lifecycle.SetStatus(types.RUNNING)
|
s.lifecycle.SetStatus(types.RUNNING)
|
||||||
go s.forwarder.AcceptTCPConnections()
|
go s.forwarder.AcceptTCPConnections()
|
||||||
s.interaction.HandleUserInput()
|
s.interaction.Start()
|
||||||
}
|
|
||||||
|
|
||||||
func generateUniqueSlug() string {
|
|
||||||
maxAttempts := 5
|
|
||||||
|
|
||||||
for i := 0; i < maxAttempts; i++ {
|
|
||||||
slug := utils.GenerateRandomString(20)
|
|
||||||
|
|
||||||
clientsMutex.RLock()
|
|
||||||
_, exists := Clients[slug]
|
|
||||||
clientsMutex.RUnlock()
|
|
||||||
|
|
||||||
if !exists {
|
|
||||||
return slug
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println("Failed to generate unique slug after multiple attempts")
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func readSSHString(reader *bytes.Reader) (string, error) {
|
func readSSHString(reader *bytes.Reader) (string, error) {
|
||||||
|
|||||||
@@ -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
@@ -11,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,18 +21,16 @@ type Lifecycle struct {
|
|||||||
status types.Status
|
status types.Status
|
||||||
conn ssh.Conn
|
conn ssh.Conn
|
||||||
channel ssh.Channel
|
channel ssh.Channel
|
||||||
interaction Interaction
|
|
||||||
forwarder Forwarder
|
forwarder Forwarder
|
||||||
slugManager slug.Manager
|
slugManager slug.Manager
|
||||||
unregisterClient func(slug string)
|
unregisterClient func(slug string)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLifecycle(conn ssh.Conn, interaction Interaction, forwarder Forwarder, slugManager slug.Manager) *Lifecycle {
|
func NewLifecycle(conn ssh.Conn, forwarder Forwarder, slugManager slug.Manager) *Lifecycle {
|
||||||
return &Lifecycle{
|
return &Lifecycle{
|
||||||
status: "",
|
status: "",
|
||||||
conn: conn,
|
conn: conn,
|
||||||
channel: nil,
|
channel: nil,
|
||||||
interaction: interaction,
|
|
||||||
forwarder: forwarder,
|
forwarder: forwarder,
|
||||||
slugManager: slugManager,
|
slugManager: slugManager,
|
||||||
unregisterClient: nil,
|
unregisterClient: nil,
|
||||||
|
|||||||
66
session/registry.go
Normal file
66
session/registry.go
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
package session
|
||||||
|
|
||||||
|
import "sync"
|
||||||
|
|
||||||
|
type Registry interface {
|
||||||
|
Get(slug string) (session *SSHSession, exist bool)
|
||||||
|
Update(oldSlug, newSlug string) (success bool)
|
||||||
|
Register(slug string, session *SSHSession) (success bool)
|
||||||
|
Remove(slug string)
|
||||||
|
}
|
||||||
|
type registry struct {
|
||||||
|
mu sync.RWMutex
|
||||||
|
clients map[string]*SSHSession
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRegistry() Registry {
|
||||||
|
return ®istry{
|
||||||
|
clients: make(map[string]*SSHSession),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *registry) Get(slug string) (session *SSHSession, exist bool) {
|
||||||
|
r.mu.RLock()
|
||||||
|
defer r.mu.RUnlock()
|
||||||
|
|
||||||
|
session, exist = r.clients[slug]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *registry) Update(oldSlug, newSlug string) (success bool) {
|
||||||
|
r.mu.Lock()
|
||||||
|
defer r.mu.Unlock()
|
||||||
|
|
||||||
|
if _, exists := r.clients[newSlug]; exists && newSlug != oldSlug {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
client, ok := r.clients[oldSlug]
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
delete(r.clients, oldSlug)
|
||||||
|
client.slugManager.Set(newSlug)
|
||||||
|
r.clients[newSlug] = client
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *registry) Register(slug string, session *SSHSession) (success bool) {
|
||||||
|
r.mu.Lock()
|
||||||
|
defer r.mu.Unlock()
|
||||||
|
|
||||||
|
if _, exists := r.clients[slug]; exists {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
r.clients[slug] = session
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *registry) Remove(slug string) {
|
||||||
|
r.mu.Lock()
|
||||||
|
defer r.mu.Unlock()
|
||||||
|
|
||||||
|
delete(r.clients, slug)
|
||||||
|
}
|
||||||
@@ -3,22 +3,16 @@ package session
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
"tunnel_pls/internal/config"
|
||||||
"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/utils"
|
|
||||||
|
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
clientsMutex sync.RWMutex
|
|
||||||
Clients = make(map[string]*SSHSession)
|
|
||||||
)
|
|
||||||
|
|
||||||
type Session interface {
|
type Session interface {
|
||||||
HandleGlobalRequest(ch <-chan *ssh.Request)
|
HandleGlobalRequest(ch <-chan *ssh.Request)
|
||||||
HandleTCPIPForward(req *ssh.Request)
|
HandleTCPIPForward(req *ssh.Request)
|
||||||
@@ -27,10 +21,13 @@ type Session interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type SSHSession struct {
|
type SSHSession struct {
|
||||||
lifecycle lifecycle.SessionLifecycle
|
initialReq <-chan *ssh.Request
|
||||||
interaction interaction.Controller
|
sshReqChannel <-chan ssh.NewChannel
|
||||||
forwarder forwarder.ForwardingController
|
lifecycle lifecycle.SessionLifecycle
|
||||||
slugManager slug.Manager
|
interaction interaction.Controller
|
||||||
|
forwarder forwarder.ForwardingController
|
||||||
|
slugManager slug.Manager
|
||||||
|
registry Registry
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SSHSession) GetLifecycle() lifecycle.SessionLifecycle {
|
func (s *SSHSession) GetLifecycle() lifecycle.SessionLifecycle {
|
||||||
@@ -49,55 +46,64 @@ func (s *SSHSession) GetSlugManager() slug.Manager {
|
|||||||
return s.slugManager
|
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, sessionRegistry Registry) *SSHSession {
|
||||||
slugManager := slug.NewManager()
|
slugManager := slug.NewManager()
|
||||||
forwarderManager := forwarder.NewForwarder(slugManager)
|
forwarderManager := forwarder.NewForwarder(slugManager)
|
||||||
interactionManager := interaction.NewInteraction(slugManager, forwarderManager)
|
interactionManager := interaction.NewInteraction(slugManager, forwarderManager)
|
||||||
lifecycleManager := lifecycle.NewLifecycle(conn, interactionManager, forwarderManager, slugManager)
|
lifecycleManager := lifecycle.NewLifecycle(conn, forwarderManager, slugManager)
|
||||||
|
|
||||||
interactionManager.SetLifecycle(lifecycleManager)
|
interactionManager.SetLifecycle(lifecycleManager)
|
||||||
interactionManager.SetSlugModificator(updateClientSlug)
|
interactionManager.SetSlugModificator(sessionRegistry.Update)
|
||||||
forwarderManager.SetLifecycle(lifecycleManager)
|
forwarderManager.SetLifecycle(lifecycleManager)
|
||||||
lifecycleManager.SetUnregisterClient(unregisterClient)
|
lifecycleManager.SetUnregisterClient(sessionRegistry.Remove)
|
||||||
|
|
||||||
session := &SSHSession{
|
return &SSHSession{
|
||||||
lifecycle: lifecycleManager,
|
initialReq: forwardingReq,
|
||||||
interaction: interactionManager,
|
sshReqChannel: sshChan,
|
||||||
forwarder: forwarderManager,
|
lifecycle: lifecycleManager,
|
||||||
slugManager: slugManager,
|
interaction: interactionManager,
|
||||||
}
|
forwarder: forwarderManager,
|
||||||
|
slugManager: slugManager,
|
||||||
var once sync.Once
|
registry: sessionRegistry,
|
||||||
for channel := range sshChan {
|
|
||||||
ch, reqs, err := channel.Accept()
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("failed to accept channel: %v", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
once.Do(func() {
|
|
||||||
session.lifecycle.SetChannel(ch)
|
|
||||||
session.interaction.SetChannel(ch)
|
|
||||||
|
|
||||||
tcpipReq := session.waitForTCPIPForward(forwardingReq)
|
|
||||||
if tcpipReq == nil {
|
|
||||||
session.interaction.SendMessage(fmt.Sprintf("Port forwarding request not received.\r\nEnsure you ran the correct command with -R flag.\r\nExample: ssh %s -p %s -R 80:localhost:3000\r\nFor more details, visit https://tunnl.live.\r\n\r\n", utils.Getenv("DOMAIN", "localhost"), utils.Getenv("PORT", "2200")))
|
|
||||||
if err := session.lifecycle.Close(); err != nil {
|
|
||||||
log.Printf("failed to close session: %v", err)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
session.HandleTCPIPForward(tcpipReq)
|
|
||||||
})
|
|
||||||
go session.HandleGlobalRequest(reqs)
|
|
||||||
}
|
|
||||||
if err := session.lifecycle.Close(); err != nil {
|
|
||||||
log.Printf("failed to close session: %v", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SSHSession) waitForTCPIPForward(forwardingReq <-chan *ssh.Request) *ssh.Request {
|
func (s *SSHSession) Start() error {
|
||||||
|
channel := <-s.sshReqChannel
|
||||||
|
ch, reqs, err := channel.Accept()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("failed to accept channel: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
go s.HandleGlobalRequest(reqs)
|
||||||
|
|
||||||
|
tcpipReq := s.waitForTCPIPForward()
|
||||||
|
if tcpipReq == nil {
|
||||||
|
_, err := ch.Write([]byte(fmt.Sprintf("Port forwarding request not received. Ensure you ran the correct command with -R flag. Example: ssh %s -p %s -R 80:localhost:3000", config.Getenv("DOMAIN", "localhost"), config.Getenv("PORT", "2200"))))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := s.lifecycle.Close(); err != nil {
|
||||||
|
log.Printf("failed to close session: %v", err)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("No forwarding Request")
|
||||||
|
}
|
||||||
|
|
||||||
|
s.lifecycle.SetChannel(ch)
|
||||||
|
s.interaction.SetChannel(ch)
|
||||||
|
|
||||||
|
s.HandleTCPIPForward(tcpipReq)
|
||||||
|
|
||||||
|
if err := s.lifecycle.Close(); err != nil {
|
||||||
|
log.Printf("failed to close session: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SSHSession) waitForTCPIPForward() *ssh.Request {
|
||||||
select {
|
select {
|
||||||
case req, ok := <-forwardingReq:
|
case req, ok := <-s.initialReq:
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Println("Forwarding request channel closed")
|
log.Println("Forwarding request channel closed")
|
||||||
return nil
|
return nil
|
||||||
@@ -115,41 +121,3 @@ func (s *SSHSession) waitForTCPIPForward(forwardingReq <-chan *ssh.Request) *ssh
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateClientSlug(oldSlug, newSlug string) bool {
|
|
||||||
clientsMutex.Lock()
|
|
||||||
defer clientsMutex.Unlock()
|
|
||||||
|
|
||||||
if _, exists := Clients[newSlug]; exists && newSlug != oldSlug {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
client, ok := Clients[oldSlug]
|
|
||||||
if !ok {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
delete(Clients, oldSlug)
|
|
||||||
client.slugManager.Set(newSlug)
|
|
||||||
Clients[newSlug] = client
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func registerClient(slug string, session *SSHSession) bool {
|
|
||||||
clientsMutex.Lock()
|
|
||||||
defer clientsMutex.Unlock()
|
|
||||||
|
|
||||||
if _, exists := Clients[slug]; exists {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
Clients[slug] = session
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func unregisterClient(slug string) {
|
|
||||||
clientsMutex.Lock()
|
|
||||||
defer clientsMutex.Unlock()
|
|
||||||
|
|
||||||
delete(Clients, slug)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,32 +1,24 @@
|
|||||||
package slug
|
package slug
|
||||||
|
|
||||||
import "sync"
|
|
||||||
|
|
||||||
type Manager interface {
|
type Manager interface {
|
||||||
Get() string
|
Get() string
|
||||||
Set(slug string)
|
Set(slug string)
|
||||||
}
|
}
|
||||||
|
|
||||||
type manager struct {
|
type manager struct {
|
||||||
slug string
|
slug string
|
||||||
slugMu sync.RWMutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewManager() Manager {
|
func NewManager() Manager {
|
||||||
return &manager{
|
return &manager{
|
||||||
slug: "",
|
slug: "",
|
||||||
slugMu: sync.RWMutex{},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *manager) Get() string {
|
func (s *manager) Get() string {
|
||||||
s.slugMu.RLock()
|
|
||||||
defer s.slugMu.RUnlock()
|
|
||||||
return s.slug
|
return s.slug
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *manager) Set(slug string) {
|
func (s *manager) Set(slug string) {
|
||||||
s.slugMu.Lock()
|
|
||||||
s.slug = slug
|
s.slug = slug
|
||||||
s.slugMu.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,12 +15,6 @@ const (
|
|||||||
TCP TunnelType = "TCP"
|
TCP TunnelType = "TCP"
|
||||||
)
|
)
|
||||||
|
|
||||||
type InteractionType string
|
|
||||||
|
|
||||||
const (
|
|
||||||
Slug InteractionType = "SLUG"
|
|
||||||
)
|
|
||||||
|
|
||||||
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" +
|
||||||
|
|||||||
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