mirror of
https://github.com/yusing/godoxy.git
synced 2026-01-11 21:10:30 +01:00
Compare commits
57 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
830d0bdadd | ||
|
|
e12b356d0d | ||
|
|
52549b6446 | ||
|
|
8694987ef9 | ||
|
|
b125b14bf6 | ||
|
|
c782f365f9 | ||
|
|
72418a2056 | ||
|
|
03bf425a38 | ||
|
|
5fafa619ee | ||
|
|
bebf99ed6c | ||
|
|
8483263d01 | ||
|
|
351bf84559 | ||
|
|
cbe23d2ed1 | ||
|
|
6e45f3683c | ||
|
|
581894c05b | ||
|
|
2657b1f726 | ||
|
|
3505e8ff7e | ||
|
|
2314e39291 | ||
|
|
bd19f443d4 | ||
|
|
ce433f0c51 | ||
|
|
47877e5119 | ||
|
|
486122f3d8 | ||
|
|
a0be1f11d3 | ||
|
|
662190e09e | ||
|
|
ce1e5da72e | ||
|
|
eb7e744a75 | ||
|
|
ac26baf97f | ||
|
|
5a8c11de16 | ||
|
|
a8ecafcd09 | ||
|
|
af37d1f29e | ||
|
|
8cfd24e6bd | ||
|
|
7bf5784016 | ||
|
|
25930a1a73 | ||
|
|
f20a1ff523 | ||
|
|
ba51796a64 | ||
|
|
c445d50221 | ||
|
|
73dfc17a82 | ||
|
|
fdab026a3b | ||
|
|
c789c69c86 | ||
|
|
2b298aa7fa | ||
|
|
d20e4d435a | ||
|
|
15d9436d52 | ||
|
|
ca98b31458 | ||
|
|
77f957c7a8 | ||
|
|
51493c9fdd | ||
|
|
9b34dc994d | ||
|
|
6bc4c1c49a | ||
|
|
443dd99b5b | ||
|
|
db6f857aaf | ||
|
|
6a54fc85ac | ||
|
|
90f4aac946 | ||
|
|
539ef911de | ||
|
|
fff790b527 | ||
|
|
094f75ef46 | ||
|
|
43ecd80687 | ||
|
|
e7f6abf027 | ||
|
|
22f911c30f |
14
.github/workflows/docker-image.yml
vendored
Normal file
14
.github/workflows/docker-image.yml
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
name: Docker Image CI
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- "*"
|
||||
jobs:
|
||||
build_and_push:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Build and Push Container to ghcr.io
|
||||
uses: GlueOps/github-actions-build-push-containers@v0.3.7
|
||||
with:
|
||||
tags: latest,${{ github.ref_name }}
|
||||
30
.github/workflows/go.yml
vendored
Normal file
30
.github/workflows/go.yml
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
# This workflow will build a golang project
|
||||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
|
||||
|
||||
name: Go
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- "*"
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v4
|
||||
with:
|
||||
go-version: "1.22.1"
|
||||
|
||||
- name: Build
|
||||
run: make build
|
||||
|
||||
- name: Release
|
||||
uses: softprops/action-gh-release@v2
|
||||
with:
|
||||
files: bin/go-proxy
|
||||
#- name: Test
|
||||
# run: go test -v ./...
|
||||
13
.gitignore
vendored
13
.gitignore
vendored
@@ -1,7 +1,10 @@
|
||||
compose.yml
|
||||
go-proxy.yml
|
||||
config.yml
|
||||
providers.yml
|
||||
bin/go-proxy.bak
|
||||
|
||||
config/
|
||||
certs/
|
||||
bin/
|
||||
templates/codemirror/
|
||||
|
||||
logs/
|
||||
log/
|
||||
log/
|
||||
.vscode/settings.json
|
||||
15
.gitlab-ci.yml
Normal file
15
.gitlab-ci.yml
Normal file
@@ -0,0 +1,15 @@
|
||||
build-image:
|
||||
image: docker
|
||||
rules:
|
||||
- if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
|
||||
variables:
|
||||
CI_REGISTRY_IMAGE: $CI_REGISTRY_IMAGE:latest
|
||||
- if: $CI_COMMIT_REF_NAME != $CI_DEFAULT_BRANCH
|
||||
variables:
|
||||
CI_REGISTRY_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_BRANCH
|
||||
before_script:
|
||||
- echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER $CI_REGISTRY --password-stdin
|
||||
script:
|
||||
- echo building $CI_REGISTRY_IMAGE
|
||||
- docker build --pull -t $CI_REGISTRY_IMAGE .
|
||||
- docker push $CI_REGISTRY_IMAGE
|
||||
0
.gitmodules
vendored
Normal file
0
.gitmodules
vendored
Normal file
12
.vscode/settings.example.json
vendored
Normal file
12
.vscode/settings.example.json
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"yaml.schemas": {
|
||||
"https://github.com/yusing/go-proxy/raw/main/schema/config.schema.json": [
|
||||
"config.example.yml",
|
||||
"config.yml"
|
||||
],
|
||||
"https://github.com/yusing/go-proxy/raw/main/schema/providers.schema.json": [
|
||||
"providers.example.yml",
|
||||
"*.providers.yml"
|
||||
]
|
||||
}
|
||||
}
|
||||
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"go.inferGopath": false
|
||||
}
|
||||
31
Dockerfile
31
Dockerfile
@@ -1,17 +1,34 @@
|
||||
FROM alpine:latest AS codemirror
|
||||
RUN apk add --no-cache unzip wget make
|
||||
COPY Makefile .
|
||||
RUN make setup-codemirror
|
||||
|
||||
FROM golang:1.22.2-alpine as builder
|
||||
COPY src/ /src
|
||||
COPY go.mod go.sum /src/go-proxy
|
||||
WORKDIR /src/go-proxy
|
||||
RUN --mount=type=cache,target="/go/pkg/mod" \
|
||||
go mod download
|
||||
|
||||
ENV GOCACHE=/root/.cache/go-build
|
||||
RUN --mount=type=cache,target="/go/pkg/mod" \
|
||||
--mount=type=cache,target="/root/.cache/go-build" \
|
||||
CGO_ENABLED=0 GOOS=linux go build -pgo=auto -o go-proxy
|
||||
|
||||
FROM alpine:latest
|
||||
|
||||
LABEL maintainer="yusing@6uo.me"
|
||||
|
||||
RUN apk add --no-cache bash tzdata
|
||||
RUN mkdir /app
|
||||
COPY bin/go-proxy entrypoint.sh /app/
|
||||
RUN apk add --no-cache tzdata
|
||||
RUN mkdir -p /app/templates
|
||||
COPY --from=codemirror templates/codemirror/ /app/templates/codemirror
|
||||
COPY templates/ /app/templates
|
||||
COPY config.default.yml /app/config.yml
|
||||
COPY schema/ /app/schema
|
||||
COPY --from=builder /src/go-proxy /app/
|
||||
|
||||
RUN chmod +x /app/go-proxy /app/entrypoint.sh
|
||||
RUN chmod +x /app/go-proxy
|
||||
ENV DOCKER_HOST unix:///var/run/docker.sock
|
||||
ENV GOPROXY_DEBUG 0
|
||||
ENV GOPROXY_REDIRECT_HTTP 1
|
||||
|
||||
EXPOSE 80
|
||||
EXPOSE 8080
|
||||
@@ -19,4 +36,4 @@ EXPOSE 443
|
||||
EXPOSE 8443
|
||||
|
||||
WORKDIR /app
|
||||
ENTRYPOINT /app/entrypoint.sh
|
||||
CMD ["/app/go-proxy"]
|
||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 [fullname]
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
34
Makefile
34
Makefile
@@ -2,22 +2,30 @@
|
||||
|
||||
all: build quick-restart logs
|
||||
|
||||
setup:
|
||||
mkdir -p config certs
|
||||
[ -f config/config.yml ] || cp config.example.yml config/config.yml
|
||||
[ -f config/providers.yml ] || touch config/providers.yml
|
||||
|
||||
setup-codemirror:
|
||||
wget https://codemirror.net/5/codemirror.zip
|
||||
unzip codemirror.zip
|
||||
rm codemirror.zip
|
||||
mkdir -p templates
|
||||
mv codemirror-* templates/codemirror
|
||||
|
||||
build:
|
||||
mkdir -p bin
|
||||
CGO_ENABLED=0 GOOS=linux go build -pgo=auto -o bin/go-proxy src/go-proxy/*.go
|
||||
|
||||
up:
|
||||
docker compose up -d --build go-proxy
|
||||
test:
|
||||
go test src/go-proxy/*.go
|
||||
|
||||
quick-restart: # quick restart without restarting the container
|
||||
docker cp bin/go-proxy go-proxy:/app/go-proxy
|
||||
docker cp templates/* go-proxy:/app/templates
|
||||
docker cp entrypoint.sh go-proxy:/app/entrypoint.sh
|
||||
docker exec -d go-proxy bash /app/entrypoint.sh restart
|
||||
up:
|
||||
docker compose up -d
|
||||
|
||||
restart:
|
||||
docker kill go-proxy
|
||||
docker compose up -d go-proxy
|
||||
docker compose restart -t 0
|
||||
|
||||
logs:
|
||||
tail -f log/go-proxy.log
|
||||
@@ -25,11 +33,17 @@ logs:
|
||||
get:
|
||||
go get -d -u ./src/go-proxy
|
||||
|
||||
repush:
|
||||
git reset --soft HEAD^
|
||||
git add -A
|
||||
git commit -m "repush"
|
||||
git push gitlab dev --force
|
||||
|
||||
udp-server:
|
||||
docker run -it --rm \
|
||||
-p 9999:9999/udp \
|
||||
--label proxy.test-udp.scheme=udp \
|
||||
--label proxy.test-udp.port=20003:9999 \
|
||||
--network data_default \
|
||||
--network host \
|
||||
--name test-udp \
|
||||
$$(docker build -q -f udp-test-server.Dockerfile .)
|
||||
|
||||
337
README.md
337
README.md
@@ -6,204 +6,203 @@ In the examples domain `x.y.z` is used, replace them with your domain
|
||||
|
||||
## Table of content
|
||||
|
||||
<!-- TOC -->
|
||||
- [Table of content](#table-of-content)
|
||||
- [Key Points](#key-points)
|
||||
- [How to use](#how-to-use)
|
||||
- [Binary](#binary)
|
||||
- [Docker](#docker)
|
||||
- [Configuration](#configuration)
|
||||
- [Single Port Configuration](#single-port-configuration-example)
|
||||
- [Multiple Ports Configuration](#multiple-ports-configuration-example)
|
||||
- [TCP/UDP Configuration](#tcpudp-configuration-example)
|
||||
- [Load balancing Configuration](#load-balancing-configuration-example)
|
||||
- [Tested Services](#tested-services)
|
||||
- [HTTP/HTTPs Reverse Proxy](#httphttps-reverse-proxy)
|
||||
- [TCP Proxy](#tcp-proxy)
|
||||
- [UDP Proxy](#udp-proxy)
|
||||
- [Command-line args](#command-line-args)
|
||||
- [Commands](#commands)
|
||||
- [Use JSON Schema in VSCode](#use-json-schema-in-vscode)
|
||||
- [Environment variables](#environment-variables)
|
||||
- [Config File](#config-file)
|
||||
- [Fields](#fields)
|
||||
- [Provider Kinds](#provider-kinds)
|
||||
- [Provider File](#provider-file)
|
||||
- [Supported DNS Challenge Providers](#supported-dns-challenge-providers)
|
||||
- [Troubleshooting](#troubleshooting)
|
||||
- [Benchmarks](#benchmarks)
|
||||
- [Known issues](#known-issues)
|
||||
- [Memory usage](#memory-usage)
|
||||
- [Build it yourself](#build-it-yourself)
|
||||
- [Getting SSL certs](#getting-ssl-certs)
|
||||
<!-- /TOC -->
|
||||
|
||||
## Key Points
|
||||
|
||||
- fast, nearly no performance penalty for end users when comparing to direct IP connections (See [benchmarks](#benchmarks))
|
||||
- auto detect reverse proxies from docker
|
||||
- additional reverse proxies from provider yaml file
|
||||
- allow multiple docker / file providers by custom `config.yml` file
|
||||
- subdomain matching **(domain name doesn't matter)**
|
||||
- path matching
|
||||
- HTTP proxy
|
||||
- TCP/UDP Proxy
|
||||
- HTTP round robin load balance support (same subdomain and path across different hosts)
|
||||
- Auto hot-reload on container start / die / stop or config changes.
|
||||
- Simple panel to see all reverse proxies and health (visit port [panel port] of go-proxy `https://*.y.z:[panel port]`)
|
||||
- Fast (See [benchmarks](#benchmarks))
|
||||
- Auto certificate obtaining and renewal (See [Config File](#config-file) and [Supported DNS Challenge Providers](#supported-dns-challenge-providers))
|
||||
- Auto detect reverse proxies from docker
|
||||
- Auto hot-reload on container `start` / `die` / `stop` or config file changes
|
||||
- Custom proxy entries with `config.yml` and additional provider files
|
||||
- Subdomain matching + Path matching **(domain name doesn't matter)**
|
||||
- HTTP(s) reverse proxy + TCP/UDP Proxy
|
||||
- HTTP(s) round robin load balance support (same subdomain and path across different hosts)
|
||||
- Web UI on port 8080 (http) and port 8443 (https)
|
||||
|
||||

|
||||
- a simple panel to see all reverse proxies and health
|
||||
|
||||

|
||||
|
||||
- a config editor to edit config and provider files with validation
|
||||
|
||||
**Validate and save file with Ctrl+S**
|
||||
|
||||

|
||||
|
||||
[🔼Back to top](#table-of-content)
|
||||
|
||||
## How to use
|
||||
|
||||
1. Download and extract the latest release (or clone the repository if you want to try out experimental features)
|
||||
1. Setup DNS Records to your machine's IP address
|
||||
|
||||
2. Copy `config.example.yml` to `config.yml` and modify the content to fit your needs
|
||||
- A Record: `*.y.z` -> `10.0.10.1`
|
||||
- AAAA Record: `*.y.z` -> `::ffff:a00:a01`
|
||||
|
||||
3. Do the same for `providers.example.yml`
|
||||
2. Start `go-proxy` by
|
||||
|
||||
4. See [Binary](#binary) or [docker](#docker)
|
||||
- [Running from binary or as a system service](docs/binary.md)
|
||||
- [Running as a docker container](docs/docker.md)
|
||||
|
||||
### Binary
|
||||
3. Start editing config files
|
||||
- with text editor (i.e. Visual Studio Code)
|
||||
- or with web config editor by navigate to `http://ip:8080`
|
||||
|
||||
1. (Optional) Prepare your certificates in `certs/` to enable https. See [Getting SSL Certs](#getting-ssl-certs)
|
||||
[🔼Back to top](#table-of-content)
|
||||
|
||||
## Tested Services
|
||||
|
||||
- cert / chain / fullchain: `./certs/cert.crt`
|
||||
- private key: `./certs/priv.key`
|
||||
### HTTP/HTTPs Reverse Proxy
|
||||
|
||||
2. run the binary `bin/go-proxy`
|
||||
- Nginx
|
||||
- Minio
|
||||
- AdguardHome Dashboard
|
||||
- etc.
|
||||
|
||||
3. enjoy
|
||||
### TCP Proxy
|
||||
|
||||
### Docker
|
||||
- Minecraft server
|
||||
- PostgreSQL
|
||||
- MariaDB
|
||||
|
||||
1. Copy content from [compose.example.yml](compose.example.yml) and create your own `compose.yml`
|
||||
### UDP Proxy
|
||||
|
||||
2. Add networks to make sure it is in the same network with other containers, or make sure `proxy.<alias>.host` is reachable
|
||||
- Adguardhome DNS
|
||||
- Palworld Dedicated Server
|
||||
|
||||
3. (Optional) Mount your SSL certs to enable https. See [Getting SSL Certs](#getting-ssl-certs)
|
||||
[🔼Back to top](#table-of-content)
|
||||
|
||||
## Command-line args
|
||||
|
||||
- cert / chain / fullchain -> `/app/certs/cert.crt`
|
||||
- private key -> `/app/certs/priv.key`
|
||||
`go-proxy [command]`
|
||||
|
||||
4. Start `go-proxy` with `docker compose up -d` or `make up`.
|
||||
### Commands
|
||||
|
||||
5. (Optional) If you are using ufw with vpn that drop all inbound traffic except vpn, run below to allow docker containers to connect to `go-proxy`
|
||||
- empty: start proxy server
|
||||
- validate: validate config and exit
|
||||
- reload: trigger a force reload of config
|
||||
|
||||
Examples:
|
||||
|
||||
In case the network of your container is in subnet `172.16.0.0/16` (bridge),
|
||||
and vpn network is under `100.64.0.0/10` (i.e. tailscale)
|
||||
- Binary: `go-proxy reload`
|
||||
- Docker: `docker exec -it go-proxy /app/go-proxy reload`
|
||||
|
||||
`sudo ufw allow from 172.16.0.0/16 to 100.64.0.0/10`
|
||||
[🔼Back to top](#table-of-content)
|
||||
|
||||
You can also list CIDRs of all docker bridge networks by:
|
||||
## Use JSON Schema in VSCode
|
||||
|
||||
`docker network inspect $(docker network ls | awk '$3 == "bridge" { print $1}') | jq -r '.[] | .Name + " " + .IPAM.Config[0].Subnet' -`
|
||||
Copy [`.vscode/settings.example.json`](.vscode/settings.example.json) to `.vscode/settings.json` and modify to fit your needs
|
||||
|
||||
6. start your docker app, and visit <container_name>.y.z
|
||||
|
||||
7. check the logs with `docker compose logs` or `make logs` to see if there is any error, check panel at [panel port] for active proxies
|
||||
|
||||
## Known issues
|
||||
|
||||
None
|
||||
|
||||
## Configuration
|
||||
|
||||
With container name, no label needs to be added.
|
||||
|
||||
However, there are some labels you can manipulate with:
|
||||
|
||||
- `proxy.aliases`: comma separated aliases for subdomain matching
|
||||
- defaults to `container_name`
|
||||
- `proxy.*.<field>`: wildcard config for all aliases
|
||||
- `proxy.<alias>.scheme`: container port protocol (`http` or `https`)
|
||||
- defaults to `http`
|
||||
- `proxy.<alias>.host`: proxy host
|
||||
- defaults to `container_name`
|
||||
- `proxy.<alias>.port`: proxy port
|
||||
- http/https: defaults to first expose port (declared in `Dockerfile` or `docker-compose.yml`)
|
||||
- tcp/udp: is in format of `[<listeningPort>:]<targetPort>`
|
||||
- when `listeningPort` is omitted (not suggested), a free port will be used automatically.
|
||||
- `targetPort` must be a number, or the predefined names (see [stream.go](src/go-proxy/stream.go#L28))
|
||||
- `no_tls_verify`: whether skip tls verify when scheme is https
|
||||
- defaults to false
|
||||
- `proxy.<alias>.path`: path matching (for http proxy only)
|
||||
- defaults to empty
|
||||
- `proxy.<alias>.path_mode`: mode for path handling
|
||||
|
||||
- defaults to empty
|
||||
- allowed: \<empty>, forward, sub
|
||||
- empty: remove path prefix from URL when proxying
|
||||
1. apps.y.z/webdav -> webdav:80
|
||||
2. apps.y.z./webdav/path/to/file -> webdav:80/path/to/file
|
||||
- forward: path remain unchanged
|
||||
1. apps.y.z/webdav -> webdav:80/webdav
|
||||
2. apps.y.z./webdav/path/to/file -> webdav:80/webdav/path/to/file
|
||||
- sub: (experimental) remove path prefix from URL and also append path to HTML link attributes (`src`, `href` and `action`) and Javascript `fetch(url)` by response body substitution
|
||||
e.g. apps.y.z/app1 -> webdav:80, `href="/path/to/file"` -> `href="/app1/path/to/file"`
|
||||
|
||||
- `proxy.<alias>.load_balance`: enable load balance
|
||||
- allowed: `1`, `true`
|
||||
|
||||
### Single port configuration example
|
||||
|
||||
```yaml
|
||||
# (default) https://<container_name>.y.z
|
||||
whoami:
|
||||
image: traefik/whoami
|
||||
container_name: whoami # => whoami.y.z
|
||||
|
||||
# enable both subdomain and path matching:
|
||||
whoami:
|
||||
image: traefik/whoami
|
||||
container_name: whoami
|
||||
labels:
|
||||
- proxy.aliases=whoami,apps
|
||||
- proxy.apps.path=/whoami
|
||||
# 1. visit https://whoami.y.z
|
||||
# 2. visit https://apps.y.z/whoami
|
||||
```json
|
||||
{
|
||||
"yaml.schemas": {
|
||||
"https://github.com/yusing/go-proxy/raw/main/schema/config.schema.json": [
|
||||
"config.example.yml",
|
||||
"config.yml"
|
||||
],
|
||||
"https://github.com/yusing/go-proxy/raw/main/schema/providers.schema.json": [
|
||||
"providers.example.yml",
|
||||
"*.providers.yml"
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Multiple ports configuration example
|
||||
[🔼Back to top](#table-of-content)
|
||||
|
||||
```yaml
|
||||
minio:
|
||||
image: quay.io/minio/minio
|
||||
container_name: minio
|
||||
...
|
||||
labels:
|
||||
- proxy.aliases=minio,minio-console
|
||||
- proxy.minio.port=9000
|
||||
- proxy.minio-console.port=9001
|
||||
## Environment variables
|
||||
|
||||
# visit https://minio.y.z to access minio
|
||||
# visit https://minio-console.y.z/whoami to access minio console
|
||||
```
|
||||
- `GOPROXY_DEBUG`: set to `1` or `true` to enable debug behaviors (i.e. output, etc.)
|
||||
- `GOPROXY_HOST_NETWORK`: _(Docker only)_ set to `1` when `network_mode: host`
|
||||
- `GOPROXY_NO_SCHEMA_VALIDATION`: disable schema validation on config load / reload **(for testing new DNS Challenge providers)**
|
||||
|
||||
### TCP/UDP configuration example
|
||||
[🔼Back to top](#table-of-content)
|
||||
|
||||
```yaml
|
||||
# In the app
|
||||
app-db:
|
||||
image: postgres:15
|
||||
container_name: app-db
|
||||
...
|
||||
labels:
|
||||
# Optional (postgres is in the known image map)
|
||||
- proxy.app-db.scheme=tcp
|
||||
## Config File
|
||||
|
||||
# Optional (first free port will be used for listening port)
|
||||
- proxy.app-db.port=20000:postgres
|
||||
See [config.example.yml](config.example.yml) for more
|
||||
|
||||
# In go-proxy
|
||||
go-proxy:
|
||||
...
|
||||
ports:
|
||||
- 80:80
|
||||
...
|
||||
- 20000:20000/tcp
|
||||
# or 20000-20010:20000-20010/tcp to declare large range at once
|
||||
### Fields
|
||||
|
||||
# access app-db via <*>.y.z:20000
|
||||
```
|
||||
- `autocert`: autocert configuration
|
||||
|
||||
## Load balancing Configuration Example
|
||||
- `email`: ACME Email
|
||||
- `domains`: a list of domains for cert registration
|
||||
- `provider`: DNS Challenge provider, see [Supported DNS Challenge Providers](#supported-dns-challenge-providers)
|
||||
- `options`: [provider specific options](#supported-dns-challenge-providers)
|
||||
|
||||
```yaml
|
||||
nginx:
|
||||
...
|
||||
deploy:
|
||||
mode: replicated
|
||||
replicas: 3
|
||||
labels:
|
||||
- proxy.nginx.load_balance=1 # allowed: [1, true]
|
||||
```
|
||||
- `providers`: reverse proxy providers configuration
|
||||
- `kind`: provider kind (string), see [Provider Kinds](#provider-kinds)
|
||||
- `value`: provider specific value
|
||||
|
||||
[🔼Back to top](#table-of-content)
|
||||
|
||||
### Provider Kinds
|
||||
|
||||
- `docker`: load reverse proxies from docker
|
||||
|
||||
values:
|
||||
|
||||
- `FROM_ENV`: value from environment (`DOCKER_HOST`)
|
||||
- full url to docker host (i.e. `tcp://host:2375`)
|
||||
|
||||
- `file`: load reverse proxies from provider file
|
||||
|
||||
value: relative path of file to `config/`
|
||||
|
||||
[🔼Back to top](#table-of-content)
|
||||
|
||||
### Provider File
|
||||
|
||||
Fields are same as [docker labels](docs/docker.md#labels) starting from `scheme`
|
||||
|
||||
See [providers.example.yml](providers.example.yml) for examples
|
||||
|
||||
[🔼Back to top](#table-of-content)
|
||||
|
||||
### Supported DNS Challenge Providers
|
||||
|
||||
- Cloudflare
|
||||
|
||||
- `auth_token`: your zone API token
|
||||
|
||||
Follow [this guide](https://cloudkul.com/blog/automcatic-renew-and-generate-ssl-on-your-website-using-lego-client/) to create a new token with `Zone.DNS` read and edit permissions
|
||||
|
||||
- CloudDNS
|
||||
|
||||
- `client_id`
|
||||
- `email`
|
||||
- `password`
|
||||
|
||||
- DuckDNS (thanks [earvingad](https://github.com/earvingad))
|
||||
|
||||
- `token`: DuckDNS Token
|
||||
|
||||
To add more provider support, see [this](docs/add_dns_provider.md)
|
||||
|
||||
[🔼Back to top](#table-of-content)
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
@@ -211,6 +210,8 @@ Q: How to fix when it shows "no matching route for subdomain \<subdomain>"?
|
||||
|
||||
A: Make sure the container is running, and \<subdomain> matches any container name / alias
|
||||
|
||||
[🔼Back to top](#table-of-content)
|
||||
|
||||
## Benchmarks
|
||||
|
||||
Benchmarked with `wrk` connecting `traefik/whoami`'s `/bench` endpoint
|
||||
@@ -261,7 +262,7 @@ Local benchmark (client running wrk and `go-proxy` server are under same proxmox
|
||||
|
||||
- Direct connection
|
||||
|
||||
```
|
||||
```shell
|
||||
root@http-benchmark-client:~# wrk -t 10 -c 200 -d 10s --latency http://10.0.100.1/bench
|
||||
Running 10s test @ http://10.0.100.1/bench
|
||||
10 threads and 200 connections
|
||||
@@ -279,7 +280,8 @@ Local benchmark (client running wrk and `go-proxy` server are under same proxmox
|
||||
```
|
||||
|
||||
- With `go-proxy` reverse proxy
|
||||
```
|
||||
|
||||
```shell
|
||||
root@http-benchmark-client:~# wrk -t 10 -c 200 -d 10s -H "Host: bench.6uo.me" --latency http://10.0.1.7/bench
|
||||
Running 10s test @ http://10.0.1.7/bench
|
||||
10 threads and 200 connections
|
||||
@@ -297,7 +299,8 @@ Local benchmark (client running wrk and `go-proxy` server are under same proxmox
|
||||
```
|
||||
|
||||
- With `traefik-v3`
|
||||
```
|
||||
|
||||
```shell
|
||||
root@traefik-benchmark:~# wrk -t10 -c200 -d10s -H "Host: benchmark.whoami" --latency http://127.0.0.1:8000/bench
|
||||
Running 10s test @ http://127.0.0.1:8000/bench
|
||||
10 threads and 200 connections
|
||||
@@ -314,22 +317,30 @@ Local benchmark (client running wrk and `go-proxy` server are under same proxmox
|
||||
Transfer/sec: 10.94MB
|
||||
```
|
||||
|
||||
[🔼Back to top](#table-of-content)
|
||||
|
||||
## Known issues
|
||||
|
||||
- Cert "renewal" is actually obtaining a new cert instead of renewing the existing one
|
||||
|
||||
[🔼Back to top](#table-of-content)
|
||||
|
||||
## Memory usage
|
||||
|
||||
It takes ~30 MB for 50 proxy entries
|
||||
It takes ~15 MB for 50 proxy entries
|
||||
|
||||
[🔼Back to top](#table-of-content)
|
||||
|
||||
## Build it yourself
|
||||
|
||||
1. Install [go](https://go.dev/doc/install) and `make` if not already
|
||||
1. Install / Upgrade [go (>=1.22)](https://go.dev/doc/install) and `make` if not already
|
||||
|
||||
2. get dependencies with `make get`
|
||||
2. Clear cache if you have built this before (go < 1.22) with `go clean -cache`
|
||||
|
||||
3. build binary with `make build`
|
||||
3. get dependencies with `make get`
|
||||
|
||||
4. start your container with `docker compose up -d`
|
||||
4. build binary with `make build`
|
||||
|
||||
## Getting SSL certs
|
||||
5. start your container with `make up` (docker) or `bin/go-proxy` (binary)
|
||||
|
||||
I personally use `nginx-proxy-manager` to get SSL certs with auto renewal by Cloudflare DNS challenge. You may symlink the certs from `nginx-proxy-manager` to `certs/` folder relative to project root. (For docker) mount them to `go-proxy`'s `/app/certs`
|
||||
|
||||
[panel port]: 8443
|
||||
[🔼Back to top](#table-of-content)
|
||||
|
||||
BIN
bin/go-proxy
BIN
bin/go-proxy
Binary file not shown.
@@ -1,47 +1,40 @@
|
||||
version: '3'
|
||||
services:
|
||||
app:
|
||||
build: .
|
||||
image: ghcr.io/yusing/go-proxy:latest
|
||||
container_name: go-proxy
|
||||
restart: always
|
||||
networks: # ^also add here
|
||||
- default
|
||||
# environment:
|
||||
# - GOPROXY_DEBUG=1 # (optional, enable only for debug)
|
||||
# - GOPROXY_REDIRECT_HTTP=0 # (optional, uncomment to disable http redirect (http -> https))
|
||||
ports:
|
||||
- 80:80 # http
|
||||
# - 443:443 # optional, https
|
||||
- 80:80 # http proxy
|
||||
- 8080:8080 # http panel
|
||||
# - 443:443 # optional, https proxy
|
||||
# - 8443:8443 # optional, https panel
|
||||
|
||||
# optional, if you declared any tcp/udp proxy, set a range you want to use
|
||||
# - 20000:20100/tcp
|
||||
# - 20000:20100/udp
|
||||
volumes:
|
||||
# if you want https
|
||||
- ./config:/app/config
|
||||
|
||||
# if local docker provider is used
|
||||
- /var/run/docker.sock:/var/run/docker.sock:ro
|
||||
|
||||
# use existing certificate
|
||||
# - /path/to/cert.pem:/app/certs/cert.crt:ro
|
||||
# - /path/to/privkey.pem:/app/certs/priv.key:ro
|
||||
|
||||
# path to logs
|
||||
- ./log:/app/log
|
||||
# store autocert obtained cert
|
||||
# - ./certs:/app/certs
|
||||
|
||||
# workaround for "lookup: no such host"
|
||||
# dns:
|
||||
# - 127.0.0.1
|
||||
|
||||
# if you use default config, or declared local docker provider
|
||||
# otherwise comment this line
|
||||
- /var/run/docker.sock:/var/run/docker.sock:ro
|
||||
|
||||
# to use custom config
|
||||
# - path/to/config.yml:/app/config.yml
|
||||
|
||||
# mount file provider yaml files
|
||||
# - path/to/provider1.yml:/app/provider1.yml
|
||||
# - path/to/provider2.yml:/app/provider2.yml
|
||||
# etc.
|
||||
dns:
|
||||
- 127.0.0.1 # workaround for "lookup: no such host"
|
||||
extra_hosts:
|
||||
# required if you use local docker provider and have containers in `host` network_mode
|
||||
- host.docker.internal:host-gateway
|
||||
# if you have container running in "host" network mode
|
||||
# extra_hosts:
|
||||
# - host.docker.internal:host-gateway
|
||||
logging:
|
||||
driver: 'json-file'
|
||||
options:
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
providers:
|
||||
local:
|
||||
kind: docker
|
||||
# for value format, see https://docs.docker.com/reference/cli/dockerd/
|
||||
value: FROM_ENV
|
||||
remote1:
|
||||
kind: docker
|
||||
value: ssh://user@10.0.1.1
|
||||
remote2:
|
||||
kind: docker
|
||||
value: tcp://10.0.1.1:2375
|
||||
# provider1:
|
||||
# kind: file
|
||||
# value: provider1.yml
|
||||
# provider2:
|
||||
# kind: file
|
||||
# value: provider2.yml
|
||||
21
config.example.yml
Normal file
21
config.example.yml
Normal file
@@ -0,0 +1,21 @@
|
||||
# Autocert (uncomment to enable)
|
||||
# autocert: # (optional, if you need autocert feature)
|
||||
# email: "user@domain.com" # (required) email for acme certificate
|
||||
# domains: # (required)
|
||||
# - "*.y.z" # domain for acme certificate, use wild card to allow all subdomains
|
||||
# provider: cloudflare # (required) dns challenge provider (string)
|
||||
# options: # provider specific options
|
||||
# auth_token: "YOUR_ZONE_API_TOKEN"
|
||||
providers:
|
||||
local:
|
||||
kind: docker
|
||||
# for value format, see https://docs.docker.com/reference/cli/dockerd/
|
||||
# i.e. FROM_ENV, ssh://user@10.0.1.1:22, tcp://10.0.2.1:2375
|
||||
value: FROM_ENV
|
||||
providers:
|
||||
kind: file
|
||||
value: providers.yml
|
||||
|
||||
# Fixed options (optional, non hot-reloadable)
|
||||
# timeout_shutdown: 5
|
||||
# redirect_to_https: false
|
||||
41
docs/add_dns_provider.md
Normal file
41
docs/add_dns_provider.md
Normal file
@@ -0,0 +1,41 @@
|
||||
# Adding provider support
|
||||
|
||||
## **CloudDNS** as an example
|
||||
|
||||
1. Fork this repo, modify [autocert.go](../src/go-proxy/autocert.go#L305)
|
||||
|
||||
```go
|
||||
var providersGenMap = map[string]ProviderGenerator{
|
||||
"cloudflare": providerGenerator(cloudflare.NewDefaultConfig, cloudflare.NewDNSProviderConfig),
|
||||
// add here, i.e.
|
||||
"clouddns": providerGenerator(clouddns.NewDefaultConfig, clouddns.NewDNSProviderConfig),
|
||||
}
|
||||
```
|
||||
|
||||
2. Go to [https://go-acme.github.io/lego/dns/clouddns](https://go-acme.github.io/lego/dns/clouddns/) and check for required config
|
||||
|
||||
3. Build `go-proxy` with `make build`
|
||||
|
||||
4. Set required config in `config.yml` `autocert` -> `options` section
|
||||
|
||||
```shell
|
||||
# From https://go-acme.github.io/lego/dns/clouddns/
|
||||
CLOUDDNS_CLIENT_ID=bLsdFAks23429841238feb177a572aX \
|
||||
CLOUDDNS_EMAIL=you@example.com \
|
||||
CLOUDDNS_PASSWORD=b9841238feb177a84330f \
|
||||
lego --email you@example.com --dns clouddns --domains my.example.org run
|
||||
```
|
||||
|
||||
Should turn into:
|
||||
|
||||
```yaml
|
||||
autocert:
|
||||
...
|
||||
options:
|
||||
client_id: bLsdFAks23429841238feb177a572aX
|
||||
email: you@example.com
|
||||
password: b9841238feb177a84330f
|
||||
```
|
||||
|
||||
5. Run with `GOPROXY_NO_SCHEMA_VALIDATION=1` and test if it works
|
||||
6. Commit and create pull request
|
||||
59
docs/binary.md
Normal file
59
docs/binary.md
Normal file
@@ -0,0 +1,59 @@
|
||||
# Getting started with `go-proxy` (binary)
|
||||
|
||||
## Setup
|
||||
|
||||
1. Install `bash`, `make` and `wget` if not already
|
||||
|
||||
2. Run setup script
|
||||
|
||||
To specitfy a version _(optional)_
|
||||
|
||||
```shell
|
||||
export VERSION=latest # will be resolved into real version number
|
||||
export VERSION=<version>
|
||||
```
|
||||
|
||||
If you don't need web config editor
|
||||
|
||||
```shell
|
||||
export SETUP_CODEMIRROR=0
|
||||
```
|
||||
|
||||
Setup
|
||||
|
||||
```shell
|
||||
wget -qO- https://6uo.me/go-proxy-setup-binary | sudo bash
|
||||
```
|
||||
|
||||
What it does:
|
||||
|
||||
- Download source file and binary into /opt/go-proxy/$VERSION
|
||||
- Setup `config.yml` and `providers.yml`
|
||||
- Setup `template/codemirror` which is a dependency for web config editor
|
||||
- Create a systemd service (if available) in `/etc/systemd/system/go-proxy.service`
|
||||
- Enable and start `go-proxy` service
|
||||
|
||||
3. Start editing config files in `http://<ip>:8080`
|
||||
|
||||
4. Check logs / status with `systemctl status go-proxy`
|
||||
|
||||
## Setup (alternative method)
|
||||
|
||||
1. Download the latest release and extract somewhere
|
||||
|
||||
2. Run `make setup` and _(optional) `make setup-codemirror`_
|
||||
|
||||
3. Enable HTTPS _(optional)_
|
||||
|
||||
- To use autocert feature
|
||||
|
||||
complete `autocert` in `config/config.yml`
|
||||
|
||||
- To use existing certificate
|
||||
|
||||
Prepare your wildcard (`*.y.z`) SSL cert in `certs/`
|
||||
|
||||
- cert / chain / fullchain: `certs/cert.crt`
|
||||
- private key: `certs/priv.key`
|
||||
|
||||
4. Run the binary `bin/go-proxy`
|
||||
371
docs/docker.md
Normal file
371
docs/docker.md
Normal file
@@ -0,0 +1,371 @@
|
||||
# Docker container guide
|
||||
|
||||
## Table of content
|
||||
|
||||
<!-- TOC -->
|
||||
|
||||
- [Table of content](#table-of-content)
|
||||
- [Setup](#setup)
|
||||
- [Labels](#labels)
|
||||
- [Labels (docker specific)](#labels-docker-specific)
|
||||
- [Troubleshooting](#troubleshooting)
|
||||
- [Docker compose examples](#docker-compose-examples)
|
||||
- [Local docker provider in bridge network](#local-docker-provider-in-bridge-network)
|
||||
- [Remote docker provider](#remote-docker-provider)
|
||||
- [Explaination](#explaination)
|
||||
- [Remote setup](#remote-setup)
|
||||
- [Proxy setup](#proxy-setup)
|
||||
- [Local docker provider in host network](#local-docker-provider-in-host-network)
|
||||
- [Proxy setup](#proxy-setup)
|
||||
- [Services URLs for above examples](#services-urls-for-above-examples)
|
||||
<!-- /TOC -->
|
||||
|
||||
## Setup
|
||||
|
||||
1. Install `wget` if not already
|
||||
|
||||
2. Run setup script
|
||||
|
||||
`bash <(wget -qO- https://6uo.me/go-proxy-setup-docker)`
|
||||
|
||||
What it does:
|
||||
|
||||
- Create required directories
|
||||
- Setup `config.yml` and `compose.yml`
|
||||
|
||||
3. Verify folder structure and then `cd go-proxy`
|
||||
|
||||
```plain
|
||||
go-proxy
|
||||
├── certs
|
||||
├── compose.yml
|
||||
└── config
|
||||
├── config.yml
|
||||
└── providers.yml
|
||||
```
|
||||
|
||||
4. Enable HTTPs _(optional)_
|
||||
|
||||
- To use autocert feature
|
||||
|
||||
- completing `autocert` section in `config/config.yml`
|
||||
- mount `certs/` to `/app/certs` to store obtained certs
|
||||
|
||||
- To use existing certificate
|
||||
|
||||
mount your wildcard (`*.y.z`) SSL cert
|
||||
|
||||
- cert / chain / fullchain -> `/app/certs/cert.crt`
|
||||
- private key -> `/app/certs/priv.key`
|
||||
|
||||
5. Modify `compose.yml` fit your needs
|
||||
|
||||
Add networks to make sure it is in the same network with other containers, or make sure `proxy.<alias>.host` is reachable
|
||||
|
||||
6. Run `docker compose up -d` to start the container
|
||||
|
||||
7. Start editing config files in `http://<ip>:8080`
|
||||
|
||||
[🔼Back to top](#table-of-content)
|
||||
|
||||
## Labels
|
||||
|
||||
- `proxy.aliases`: comma separated aliases for subdomain matching
|
||||
|
||||
- default: container name
|
||||
|
||||
- `proxy.*.<field>`: wildcard label for all aliases
|
||||
|
||||
Below labels has a **`proxy.<alias>.`** prefix (i.e. `proxy.nginx.scheme: http`)
|
||||
|
||||
- `scheme`: proxy protocol
|
||||
- default: `http`
|
||||
- allowed: `http`, `https`, `tcp`, `udp`
|
||||
- `host`: proxy host
|
||||
- default: `container_name`
|
||||
- `port`: proxy port
|
||||
- default: first expose port (declared in `Dockerfile` or `docker-compose.yml`)
|
||||
- `http(s)`: number in range og `0 - 65535`
|
||||
- `tcp/udp`: `[<listeningPort>:]<targetPort>`
|
||||
- `listeningPort`: number, when it is omitted (not suggested), a free port starting from 20000 will be used.
|
||||
- `targetPort`: number, or predefined names (see [constants.go:14](src/go-proxy/constants.go#L14))
|
||||
- `no_tls_verify`: whether skip tls verify when scheme is https
|
||||
- default: `false`
|
||||
- `path`: proxy path _(http(s) proxy only)_
|
||||
- default: empty
|
||||
- `path_mode`: mode for path handling
|
||||
|
||||
- default: empty
|
||||
- allowed: empty, `forward`, `sub`
|
||||
|
||||
- `empty`: remove path prefix from URL when proxying
|
||||
1. apps.y.z/webdav -> webdav:80
|
||||
2. apps.y.z./webdav/path/to/file -> webdav:80/path/to/file
|
||||
- `forward`: path remain unchanged
|
||||
1. apps.y.z/webdav -> webdav:80/webdav
|
||||
2. apps.y.z./webdav/path/to/file -> webdav:80/webdav/path/to/file
|
||||
- `sub`: **(experimental)** remove path prefix from URL and also append path to HTML link attributes (`src`, `href` and `action`) and Javascript `fetch(url)` by response body substitution
|
||||
e.g. apps.y.z/app1 -> webdav:80, `href="/app1/path/to/file"` -> `href="/path/to/file"`
|
||||
|
||||
- `set_headers`: a list of header to set, (key:value, one by line)
|
||||
|
||||
Duplicated keys will be treated as multiple-value headers
|
||||
|
||||
```yaml
|
||||
labels:
|
||||
- |
|
||||
proxy.app.set_headers=
|
||||
X-Custom-Header1: value1
|
||||
X-Custom-Header1: value2
|
||||
X-Custom-Header2: value2
|
||||
```
|
||||
|
||||
- `hide_headers`: comma seperated list of headers to hide
|
||||
|
||||
[🔼Back to top](#table-of-content)
|
||||
|
||||
## Labels (docker specific)
|
||||
|
||||
Below labels has a **`proxy.<alias>.`** prefix (i.e. `proxy.app.headers.hide: X-Powered-By,X-Custom-Header`)
|
||||
|
||||
- `headers.set.<header>`: value of header to set
|
||||
|
||||
- `headers.hide`: comma seperated list of headers to hide
|
||||
|
||||
- `load_balance`: enable load balance
|
||||
- allowed: `1`, `true`
|
||||
|
||||
[🔼Back to top](#table-of-content)
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
- Firewall issues
|
||||
|
||||
If you are using `ufw` with vpn that drop all inbound traffic except vpn, run below:
|
||||
|
||||
`sudo ufw allow from 172.16.0.0/16 to 100.64.0.0/10`
|
||||
|
||||
Explaination:
|
||||
|
||||
Docker network is usually `172.16.0.0/16`
|
||||
|
||||
Tailscale is used as an example, `100.64.0.0/10` will be the CIDR
|
||||
|
||||
You can also list CIDRs of all docker bridge networks by:
|
||||
|
||||
`docker network inspect $(docker network ls | awk '$3 == "bridge" { print $1}') | jq -r '.[] | .Name + " " + .IPAM.Config[0].Subnet' -`
|
||||
|
||||
[🔼Back to top](#table-of-content)
|
||||
|
||||
## Docker compose examples
|
||||
|
||||
### Local docker provider in bridge network
|
||||
|
||||
```yaml
|
||||
volumes:
|
||||
adg-work:
|
||||
adg-conf:
|
||||
mc-data:
|
||||
palworld:
|
||||
nginx:
|
||||
services:
|
||||
adg:
|
||||
image: adguard/adguardhome
|
||||
restart: unless-stopped
|
||||
labels:
|
||||
- proxy.aliases=adg,adg-dns,adg-setup
|
||||
- proxy.adg.port=80
|
||||
- proxy.adg-setup.port=3000
|
||||
- proxy.adg-dns.scheme=udp
|
||||
- proxy.adg-dns.port=20000:dns
|
||||
volumes:
|
||||
- adg-work:/opt/adguardhome/work
|
||||
- adg-conf:/opt/adguardhome/conf
|
||||
mc:
|
||||
image: itzg/minecraft-server
|
||||
tty: true
|
||||
stdin_open: true
|
||||
container_name: mc
|
||||
restart: unless-stopped
|
||||
labels:
|
||||
- proxy.mc.scheme=tcp
|
||||
- proxy.mc.port=20001:25565
|
||||
environment:
|
||||
- EULA=TRUE
|
||||
volumes:
|
||||
- mc-data:/data
|
||||
palworld:
|
||||
image: thijsvanloef/palworld-server-docker:latest
|
||||
restart: unless-stopped
|
||||
container_name: pal
|
||||
stop_grace_period: 30s
|
||||
labels:
|
||||
- proxy.aliases=pal1,pal2
|
||||
- proxy.*.scheme=udp
|
||||
- proxy.pal1.port=20002:8211
|
||||
- proxy.pal2.port=20003:27015
|
||||
environment: ...
|
||||
volumes:
|
||||
- palworld:/palworld
|
||||
nginx:
|
||||
image: nginx
|
||||
container_name: nginx
|
||||
volumes:
|
||||
- nginx:/usr/share/nginx/html
|
||||
go-proxy:
|
||||
image: ghcr.io/yusing/go-proxy
|
||||
container_name: go-proxy
|
||||
restart: always
|
||||
ports:
|
||||
- 80:80 # http
|
||||
- 443:443 # optional, https
|
||||
- 8080:8080 # http panel
|
||||
- 8443:8443 # optional, https panel
|
||||
|
||||
- 53:20000/udp # adguardhome
|
||||
- 25565:20001/tcp # minecraft
|
||||
- 8211:20002/udp # palworld
|
||||
- 27015:20003/udp # palworld
|
||||
volumes:
|
||||
- ./config:/app/config
|
||||
- /var/run/docker.sock:/var/run/docker.sock:ro
|
||||
labels:
|
||||
- proxy.aliases=gp
|
||||
- proxy.gp.port=8080
|
||||
```
|
||||
|
||||
[🔼Back to top](#table-of-content)
|
||||
|
||||
### Remote docker provider
|
||||
|
||||
#### Explaination
|
||||
|
||||
- Expose container ports to random port in remote host
|
||||
- Use container port with an asterisk sign **(\*)** before to find remote port automatically
|
||||
|
||||
#### Remote setup
|
||||
|
||||
```yaml
|
||||
volumes:
|
||||
adg-work:
|
||||
adg-conf:
|
||||
mc-data:
|
||||
palworld:
|
||||
nginx:
|
||||
services:
|
||||
adg:
|
||||
image: adguard/adguardhome
|
||||
restart: unless-stopped
|
||||
ports: # map container ports
|
||||
- 80
|
||||
- 3000
|
||||
- 53/udp
|
||||
- 53/tcp
|
||||
labels:
|
||||
- proxy.aliases=adg,adg-dns,adg-setup
|
||||
# add an asterisk (*) before to find host port automatically
|
||||
- proxy.adg.port=*80
|
||||
- proxy.adg-setup.port=*3000
|
||||
- proxy.adg-dns.scheme=udp
|
||||
- proxy.adg-dns.port=*53
|
||||
volumes:
|
||||
- adg-work:/opt/adguardhome/work
|
||||
- adg-conf:/opt/adguardhome/conf
|
||||
mc:
|
||||
image: itzg/minecraft-server
|
||||
tty: true
|
||||
stdin_open: true
|
||||
container_name: mc
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- 25565
|
||||
labels:
|
||||
- proxy.mc.scheme=tcp
|
||||
- proxy.mc.port=*25565
|
||||
environment:
|
||||
- EULA=TRUE
|
||||
volumes:
|
||||
- mc-data:/data
|
||||
palworld:
|
||||
image: thijsvanloef/palworld-server-docker:latest
|
||||
restart: unless-stopped
|
||||
container_name: pal
|
||||
stop_grace_period: 30s
|
||||
ports:
|
||||
- 8211/udp
|
||||
- 27015/udp
|
||||
labels:
|
||||
- proxy.aliases=pal1,pal2
|
||||
- proxy.*.scheme=udp
|
||||
- proxy.pal1.port=*8211
|
||||
- proxy.pal2.port=*27015
|
||||
environment: ...
|
||||
volumes:
|
||||
- palworld:/palworld
|
||||
nginx:
|
||||
image: nginx
|
||||
container_name: nginx
|
||||
# for single port container, host port will be found automatically
|
||||
ports:
|
||||
- 80
|
||||
volumes:
|
||||
- nginx:/usr/share/nginx/html
|
||||
```
|
||||
|
||||
[🔼Back to top](#table-of-content)
|
||||
|
||||
#### Proxy setup
|
||||
|
||||
```yaml
|
||||
go-proxy:
|
||||
image: ghcr.io/yusing/go-proxy
|
||||
container_name: go-proxy
|
||||
restart: always
|
||||
network_mode: host
|
||||
volumes:
|
||||
- ./config:/app/config
|
||||
- /var/run/docker.sock:/var/run/docker.sock:ro
|
||||
labels:
|
||||
- proxy.aliases=gp
|
||||
- proxy.gp.port=8080
|
||||
```
|
||||
|
||||
[🔼Back to top](#table-of-content)
|
||||
|
||||
### Local docker provider in host network
|
||||
|
||||
Mostly as remote docker setup, see [remote setup](#remote-setup)
|
||||
|
||||
With `GOPROXY_HOST_NETWORK=1` to treat it as remote docker provider
|
||||
|
||||
#### Proxy setup
|
||||
|
||||
```yaml
|
||||
go-proxy:
|
||||
image: ghcr.io/yusing/go-proxy
|
||||
container_name: go-proxy
|
||||
restart: always
|
||||
network_mode: host
|
||||
environment: # this part is needed for local docker in host mode
|
||||
- GOPROXY_HOST_NETWORK=1
|
||||
volumes:
|
||||
- ./config:/app/config
|
||||
- /var/run/docker.sock:/var/run/docker.sock:ro
|
||||
labels:
|
||||
- proxy.aliases=gp
|
||||
- proxy.gp.port=8080
|
||||
```
|
||||
|
||||
[🔼Back to top](#table-of-content)
|
||||
|
||||
### Services URLs for above examples
|
||||
|
||||
- `gp.yourdomain.com`: go-proxy web panel
|
||||
- `adg-setup.yourdomain.com`: adguard setup (first time setup)
|
||||
- `adg.yourdomain.com`: adguard dashboard
|
||||
- `nginx.yourdomain.com`: nginx
|
||||
- `yourdomain.com:53`: adguard dns
|
||||
- `yourdomain.com:25565`: minecraft server
|
||||
- `yourdomain.com:8211`: palworld server
|
||||
|
||||
[🔼Back to top](#table-of-content)
|
||||
@@ -1,11 +0,0 @@
|
||||
#!/bin/bash
|
||||
if [ "$1" == "restart" ]; then
|
||||
echo "restarting"
|
||||
killall go-proxy
|
||||
fi
|
||||
if [ "$DEBUG" == "1" ]; then
|
||||
/app/go-proxy 2> log/go-proxy.log &
|
||||
tail -f /dev/null
|
||||
else
|
||||
/app/go-proxy
|
||||
fi
|
||||
32
go.mod
32
go.mod
@@ -1,42 +1,54 @@
|
||||
module github.com/yusing/go-proxy
|
||||
|
||||
go 1.21.7
|
||||
go 1.22
|
||||
|
||||
require (
|
||||
github.com/docker/cli v26.0.0+incompatible
|
||||
github.com/docker/docker v26.0.0+incompatible
|
||||
github.com/fsnotify/fsnotify v1.7.0
|
||||
github.com/go-acme/lego/v4 v4.16.1
|
||||
github.com/santhosh-tekuri/jsonschema v1.2.4
|
||||
github.com/sirupsen/logrus v1.9.3
|
||||
golang.org/x/net v0.22.0
|
||||
golang.org/x/net v0.24.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/Microsoft/go-winio v0.6.1 // indirect
|
||||
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
|
||||
github.com/cloudflare/cloudflare-go v0.92.0 // indirect
|
||||
github.com/containerd/log v0.1.0 // indirect
|
||||
github.com/distribution/reference v0.5.0 // indirect
|
||||
github.com/distribution/reference v0.6.0 // indirect
|
||||
github.com/docker/go-connections v0.5.0 // indirect
|
||||
github.com/docker/go-units v0.5.0 // indirect
|
||||
github.com/felixge/httpsnoop v1.0.4 // indirect
|
||||
github.com/go-jose/go-jose/v4 v4.0.1 // indirect
|
||||
github.com/go-logr/logr v1.4.1 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/gogo/protobuf v1.3.2 // indirect
|
||||
github.com/google/go-querystring v1.1.0 // indirect
|
||||
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
|
||||
github.com/hashicorp/go-retryablehttp v0.7.5 // indirect
|
||||
github.com/miekg/dns v1.1.58 // indirect
|
||||
github.com/moby/docker-image-spec v1.3.1 // indirect
|
||||
github.com/moby/term v0.5.0 // indirect
|
||||
github.com/morikuni/aec v1.0.0 // indirect
|
||||
github.com/opencontainers/go-digest v1.0.0 // indirect
|
||||
github.com/opencontainers/image-spec v1.1.0 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
|
||||
go.opentelemetry.io/otel v1.24.0 // indirect
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.50.0 // indirect
|
||||
go.opentelemetry.io/otel v1.25.0 // indirect
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.24.0 // indirect
|
||||
go.opentelemetry.io/otel/metric v1.24.0 // indirect
|
||||
go.opentelemetry.io/otel/metric v1.25.0 // indirect
|
||||
go.opentelemetry.io/otel/sdk v1.24.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.24.0 // indirect
|
||||
golang.org/x/mod v0.16.0 // indirect
|
||||
golang.org/x/sys v0.18.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.25.0 // indirect
|
||||
golang.org/x/crypto v0.22.0 // indirect
|
||||
golang.org/x/mod v0.17.0 // indirect
|
||||
golang.org/x/sync v0.7.0 // indirect
|
||||
golang.org/x/sys v0.19.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
golang.org/x/time v0.5.0 // indirect
|
||||
golang.org/x/tools v0.19.0 // indirect
|
||||
golang.org/x/tools v0.20.0 // indirect
|
||||
gotest.tools/v3 v3.5.1 // indirect
|
||||
)
|
||||
|
||||
89
go.sum
89
go.sum
@@ -2,15 +2,17 @@ github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOEl
|
||||
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
|
||||
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
|
||||
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
|
||||
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
|
||||
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
|
||||
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
|
||||
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
|
||||
github.com/cloudflare/cloudflare-go v0.92.0 h1:ltJvGvqZ4G6Fm2hHOYZ5RWpJQcrM0oDrsjjZydZhFJQ=
|
||||
github.com/cloudflare/cloudflare-go v0.92.0/go.mod h1:nUqvBUUDRxNzsDSQjbqUNWHEIYAoUlgRmcAzMKlFdKs=
|
||||
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
|
||||
github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
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/distribution/reference v0.5.0 h1:/FUIFXtfc/x2gpa5/VGfiGLuOIdYa1t65IKK2OFGvA0=
|
||||
github.com/distribution/reference v0.5.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
|
||||
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
|
||||
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
|
||||
github.com/docker/cli v26.0.0+incompatible h1:90BKrx1a1HKYpSnnBFR6AgDq/FqkHxwlUyzJVPxD30I=
|
||||
github.com/docker/cli v26.0.0+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
|
||||
github.com/docker/docker v26.0.0+incompatible h1:Ng2qi+gdKADUa/VM+6b6YaY2nlZhk/lVJiKR/2bMudU=
|
||||
@@ -19,25 +21,53 @@ github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj
|
||||
github.com/docker/go-connections v0.5.0/go.mod h1:ov60Kzw0kKElRwhNs9UlUHAE/F9Fe6GLaXnqyDdmEXc=
|
||||
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
|
||||
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
|
||||
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
|
||||
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
|
||||
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
|
||||
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
|
||||
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
|
||||
github.com/go-acme/lego/v4 v4.16.1 h1:JxZ93s4KG0jL27rZ30UsIgxap6VGzKuREsSkkyzeoCQ=
|
||||
github.com/go-acme/lego/v4 v4.16.1/go.mod h1:AVvwdPned/IWpD/ihHhMsKnveF7HHYAz/CmtXi7OZoE=
|
||||
github.com/go-jose/go-jose/v4 v4.0.1 h1:QVEPDE3OluqXBQZDcnNvQrInro2h0e4eqNbnZSWqS6U=
|
||||
github.com/go-jose/go-jose/v4 v4.0.1/go.mod h1:WVf9LFMHh/QVrmqrOfqun0C45tMe3RoiKJMPvgWwLfY=
|
||||
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
|
||||
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
|
||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
|
||||
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
||||
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
|
||||
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
|
||||
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
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-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
|
||||
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0 h1:Wqo399gCIufwto+VfwCSvsnfGpF/w5E9CNxSwbpD6No=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0/go.mod h1:qmOFXW2epJhM0qSnUUYpldc7gVz2KMQwJ/QYCDIa7XU=
|
||||
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
|
||||
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
|
||||
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
|
||||
github.com/hashicorp/go-hclog v1.2.0 h1:La19f8d7WIlm4ogzNHB0JGqs5AUDAZ2UfCY4sJXcJdM=
|
||||
github.com/hashicorp/go-hclog v1.2.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
|
||||
github.com/hashicorp/go-retryablehttp v0.7.5 h1:bJj+Pj19UZMIweq/iie+1u5YCdGrnxCT9yvm0e+Nd5M=
|
||||
github.com/hashicorp/go-retryablehttp v0.7.5/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8=
|
||||
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
|
||||
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
|
||||
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
|
||||
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/miekg/dns v1.1.58 h1:ca2Hdkz+cDg/7eNF6V56jjzuZ4aCAE+DbVkILdQWG/4=
|
||||
github.com/miekg/dns v1.1.58/go.mod h1:Ypv+3b/KadlvW9vJfXOTf300O4UqaHFzFCuHz+rPkBY=
|
||||
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
|
||||
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
|
||||
github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0=
|
||||
@@ -52,54 +82,61 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
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/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg=
|
||||
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
|
||||
github.com/santhosh-tekuri/jsonschema v1.2.4 h1:hNhW8e7t+H1vgY+1QeEQpveR6D4+OwKPXCfD2aieJis=
|
||||
github.com/santhosh-tekuri/jsonschema v1.2.4/go.mod h1:TEAUOeZSmIxTTuHatJzrvARHiuO9LYd+cIxzgEHCQI4=
|
||||
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 h1:jq9TW8u3so/bN+JPT166wjOI6/vQPF6Xe7nMNIltagk=
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0/go.mod h1:p8pYQP+m5XfbZm9fxtSKAbM6oIllS7s2AfxrChvc7iw=
|
||||
go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo=
|
||||
go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo=
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.50.0 h1:cEPbyTSEHlQR89XVlyo78gqluF8Y3oMeBkXGWzQsfXY=
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.50.0/go.mod h1:DKdbWcT4GH1D0Y3Sqt/PFXt2naRKDWtU+eE6oLdFNA8=
|
||||
go.opentelemetry.io/otel v1.25.0 h1:gldB5FfhRl7OJQbUHt/8s0a7cE8fbsPAtdpRaApKy4k=
|
||||
go.opentelemetry.io/otel v1.25.0/go.mod h1:Wa2ds5NOXEMkCmUou1WA7ZBfLTHWIsp034OVD7AO+Vg=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.24.0 h1:t6wl9SPayj+c7lEIFgm4ooDBZVb01IhLB4InpomhRw8=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.24.0/go.mod h1:iSDOcsnSA5INXzZtwaBPrKp/lWu/V14Dd+llD0oI2EA=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.24.0 h1:Xw8U6u2f8DK2XAkGRFV7BBLENgnTGX9i4rQRxJf+/vs=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.24.0/go.mod h1:6KW1Fm6R/s6Z3PGXwSJN2K4eT6wQB3vXX6CVnYX9NmM=
|
||||
go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI=
|
||||
go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco=
|
||||
go.opentelemetry.io/otel/metric v1.25.0 h1:LUKbS7ArpFL/I2jJHdJcqMGxkRdxpPHE0VU/D4NuEwA=
|
||||
go.opentelemetry.io/otel/metric v1.25.0/go.mod h1:rkDLUSd2lC5lq2dFNrX9LGAbINP5B7WBkC78RXCpH5s=
|
||||
go.opentelemetry.io/otel/sdk v1.24.0 h1:YMPPDNymmQN3ZgczicBY3B6sf9n62Dlj9pWD3ucgoDw=
|
||||
go.opentelemetry.io/otel/sdk v1.24.0/go.mod h1:KVrIYw6tEubO9E96HQpcmpTKDVn9gdv35HoYiQWGDFg=
|
||||
go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI=
|
||||
go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU=
|
||||
go.opentelemetry.io/otel/trace v1.25.0 h1:tqukZGLwQYRIFtSQM2u2+yfMVTgGVeqRLPUYx1Dq6RM=
|
||||
go.opentelemetry.io/otel/trace v1.25.0/go.mod h1:hCCs70XM/ljO+BeQkyFnbK28SBIJ/Emuha+ccrCRT7I=
|
||||
go.opentelemetry.io/proto/otlp v1.1.0 h1:2Di21piLrCqJ3U3eXGCTPHE9R8Nh+0uglSnOyxikMeI=
|
||||
go.opentelemetry.io/proto/otlp v1.1.0/go.mod h1:GpBHCBWiqvVLDqmHZsoMM3C5ySeKTC7ej/RNTae6MdY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
|
||||
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
|
||||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
|
||||
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
|
||||
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
|
||||
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
|
||||
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
|
||||
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
|
||||
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
|
||||
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
|
||||
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
|
||||
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
@@ -110,12 +147,13 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
|
||||
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||
golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw=
|
||||
golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc=
|
||||
golang.org/x/tools v0.20.0 h1:hz/CVckiOxybQvFw6h7b/q80NTr9IUQb4s1IIzW7KNY=
|
||||
golang.org/x/tools v0.20.0/go.mod h1:WvitBU7JJf6A4jOdg4S1tviW9bhUxkgeCui/0JHctQg=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/genproto v0.0.0-20230530153820-e85fd2cbaebc h1:8DyZCyvI8mE1IdLy/60bS+52xfymkE72wv1asokgtao=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20240102182953-50ed04b92917 h1:rcS6EyEaoCO52hQDupoSfrxI3R6C2Tq741is7X8OvnM=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20240102182953-50ed04b92917/go.mod h1:CmlNWB9lSezaYELKS5Ym1r44VrrbPUa7JTvw+6MbpJ0=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917 h1:6G8oQ016D88m1xAKljMlBOOGWDZkes4kMhgGFlf8WcQ=
|
||||
@@ -124,8 +162,9 @@ google.golang.org/grpc v1.61.1 h1:kLAiWrZs7YeDM6MumDe7m3y4aM6wacLzM1Y/wiLP9XY=
|
||||
google.golang.org/grpc v1.61.1/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs=
|
||||
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
|
||||
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
@@ -1,15 +1,38 @@
|
||||
app: # matching `app.y.z`
|
||||
# optional
|
||||
example: # matching `app.y.z`
|
||||
# optional, defaults to http
|
||||
scheme: http
|
||||
# required, proxy target
|
||||
host: 10.0.0.1
|
||||
# optional
|
||||
port: 80
|
||||
# optional, defaults to 80 for http, 443 for https
|
||||
port: "80"
|
||||
# optional, defaults to empty
|
||||
path:
|
||||
# optional
|
||||
# optional, defaults to empty
|
||||
path_mode:
|
||||
# optional
|
||||
notlsverify: false
|
||||
# app2:
|
||||
# ...
|
||||
# optional (https only)
|
||||
# no_tls_verify: false
|
||||
# optional headers to set / override (http(s) only)
|
||||
set_headers:
|
||||
HEADER_A:
|
||||
- VALUE_1
|
||||
- VALUE_2
|
||||
HEADER_B: [VALUE_3]
|
||||
# optional headers to hide (http(s) only)
|
||||
hide_headers:
|
||||
- HEADER_C
|
||||
- HEADER_D
|
||||
app1: # matching `app1.y.z` -> http://x.y.z
|
||||
host: x.y.z
|
||||
app2: # `app2` has no effect for tcp / udp, but still has to be unique across files
|
||||
scheme: tcp
|
||||
host: 10.0.0.2
|
||||
port: 20000:tcp
|
||||
app3: # matching `app3.y.z` -> https://10.0.0.1/app3
|
||||
scheme: https
|
||||
host: 10.0.0.1
|
||||
path: /app3
|
||||
path_mode: forward
|
||||
no_tls_verify: false
|
||||
set_headers:
|
||||
X-Forwarded-Proto: [https]
|
||||
X-Forwarded-Host: [app3.y.z]
|
||||
|
||||
194
schema/config.schema.json
Normal file
194
schema/config.schema.json
Normal file
@@ -0,0 +1,194 @@
|
||||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"type": "object",
|
||||
"title": "go-proxy config file",
|
||||
"properties": {
|
||||
"autocert": {
|
||||
"title": "Autocert configuration",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"email": {
|
||||
"description": "ACME Email",
|
||||
"type": "string",
|
||||
"pattern": "^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$",
|
||||
"patternErrorMessage": "Invalid email"
|
||||
},
|
||||
"domains": {
|
||||
"description": "Cert Domains",
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"minItems": 1
|
||||
},
|
||||
"provider": {
|
||||
"description": "DNS Challenge Provider",
|
||||
"type": "string",
|
||||
"enum": ["cloudflare", "clouddns", "duckdns"]
|
||||
},
|
||||
"options": {
|
||||
"description": "Provider specific options",
|
||||
"type": "object"
|
||||
}
|
||||
},
|
||||
"required": ["email", "domains", "provider", "options"],
|
||||
"allOf": [
|
||||
{
|
||||
"if": {
|
||||
"properties": {
|
||||
"provider": {
|
||||
"const": "cloudflare"
|
||||
}
|
||||
}
|
||||
},
|
||||
"then": {
|
||||
"properties": {
|
||||
"options": {
|
||||
"required": ["auth_token"],
|
||||
"additionalProperties": false,
|
||||
"properties": {
|
||||
"auth_token": {
|
||||
"description": "Cloudflare API Token with Zone Scope",
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"if": {
|
||||
"properties": {
|
||||
"provider": {
|
||||
"const": "clouddns"
|
||||
}
|
||||
}
|
||||
},
|
||||
"then": {
|
||||
"properties": {
|
||||
"options": {
|
||||
"required": ["client_id", "email", "password"],
|
||||
"additionalProperties": false,
|
||||
"properties": {
|
||||
"client_id": {
|
||||
"description": "CloudDNS Client ID",
|
||||
"type": "string"
|
||||
},
|
||||
"email": {
|
||||
"description": "CloudDNS Email",
|
||||
"type": "string"
|
||||
},
|
||||
"password": {
|
||||
"description": "CloudDNS Password",
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"if": {
|
||||
"properties": {
|
||||
"provider": {
|
||||
"const": "duckdns"
|
||||
}
|
||||
}
|
||||
},
|
||||
"then": {
|
||||
"properties": {
|
||||
"options": {
|
||||
"required": ["token"],
|
||||
"additionalProperties": false,
|
||||
"properties": {
|
||||
"token": {
|
||||
"description": "DuckDNS Token",
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"providers": {
|
||||
"title": "Proxy providers configuration",
|
||||
"type": "object",
|
||||
"patternProperties": {
|
||||
"^[a-zA-Z0-9_-]+$": {
|
||||
"description": "Proxy provider",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"kind": {
|
||||
"description": "Proxy provider kind",
|
||||
"type": "string",
|
||||
"enum": ["docker", "file"]
|
||||
},
|
||||
"value": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": ["kind", "value"],
|
||||
"allOf": [
|
||||
{
|
||||
"if": {
|
||||
"properties": {
|
||||
"kind": {
|
||||
"const": "docker"
|
||||
}
|
||||
}
|
||||
},
|
||||
"then": {
|
||||
"if": {
|
||||
"properties": {
|
||||
"value": {
|
||||
"const": "FROM_ENV"
|
||||
}
|
||||
}
|
||||
},
|
||||
"then": {
|
||||
"properties": {
|
||||
"value": {
|
||||
"description": "use docker client from environment"
|
||||
}
|
||||
}
|
||||
},
|
||||
"else": {
|
||||
"properties": {
|
||||
"value": {
|
||||
"description": "docker client URL",
|
||||
"examples": [
|
||||
"unix:///var/run/docker.sock",
|
||||
"tcp://127.0.0.1:2375",
|
||||
"ssh://user@host:port"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"else": {
|
||||
"properties": {
|
||||
"value": {
|
||||
"description": "file path"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"timeout_shutdown": {
|
||||
"title": "Shutdown timeout (in seconds)",
|
||||
"type": "integer",
|
||||
"minimum": 0
|
||||
},
|
||||
"redirect_to_https": {
|
||||
"title": "Redirect to HTTPS",
|
||||
"type": "boolean"
|
||||
}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
"required": ["providers"]
|
||||
}
|
||||
200
schema/providers.schema.json
Normal file
200
schema/providers.schema.json
Normal file
@@ -0,0 +1,200 @@
|
||||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "go-proxy providers file",
|
||||
"anyOf": [
|
||||
{
|
||||
"type":"object"
|
||||
},
|
||||
{
|
||||
"type":"null"
|
||||
}
|
||||
],
|
||||
"patternProperties": {
|
||||
"^[a-zA-Z0-9_-]+$": {
|
||||
"title": "Proxy entry",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"scheme": {
|
||||
"title": "Proxy scheme (http, https, tcp, udp)",
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "string",
|
||||
"enum": ["http", "https", "tcp", "udp"]
|
||||
},
|
||||
{
|
||||
"type": "null",
|
||||
"description": "HTTP proxy"
|
||||
}
|
||||
]
|
||||
},
|
||||
"host": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "string",
|
||||
"format": "ipv4",
|
||||
"description": "Proxy to ipv4 address"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"format": "ipv6",
|
||||
"description": "Proxy to ipv6 address"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"format": "hostname",
|
||||
"description": "Proxy to hostname"
|
||||
}
|
||||
],
|
||||
"title": "Proxy host (ipv4 / ipv6 / hostname)"
|
||||
},
|
||||
"port": {
|
||||
"title": "Proxy port"
|
||||
},
|
||||
"path": {},
|
||||
"path_mode": {},
|
||||
"no_tls_verify": {
|
||||
"description": "Disable TLS verification for https proxy",
|
||||
"type": "boolean"
|
||||
},
|
||||
"set_headers": {},
|
||||
"hide_headers": {}
|
||||
},
|
||||
"required": ["host"],
|
||||
"additionalProperties": false,
|
||||
"allOf": [
|
||||
{
|
||||
"if": {
|
||||
"anyOf": [
|
||||
{
|
||||
"properties": {
|
||||
"scheme": {
|
||||
"enum": ["http", "https"]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"properties": {
|
||||
"scheme": {
|
||||
"not": true
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"properties": {
|
||||
"scheme": {
|
||||
"type": "null"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"then": {
|
||||
"properties": {
|
||||
"port": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "string",
|
||||
"pattern": "^[0-9]{1,5}$",
|
||||
"minimum": 1,
|
||||
"maximum": 65535,
|
||||
"markdownDescription": "Proxy port from **1** to **65535**",
|
||||
"patternErrorMessage": "'port' must be a number"
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"minimum": 1,
|
||||
"maximum": 65535
|
||||
}
|
||||
]
|
||||
},
|
||||
"path": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "Proxy path"
|
||||
},
|
||||
{
|
||||
"type": "null",
|
||||
"description": "No proxy path"
|
||||
}
|
||||
]
|
||||
},
|
||||
"path_mode": {
|
||||
"anyOf": [
|
||||
{
|
||||
"description": "Proxy path mode (forward, sub, empty)",
|
||||
"type": "string",
|
||||
"enum": ["", "forward", "sub"]
|
||||
},
|
||||
{
|
||||
"description": "Default proxy path mode (sub)",
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
},
|
||||
"set_headers": {
|
||||
"type": "object",
|
||||
"description": "Proxy headers to set",
|
||||
"additionalProperties": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
"hide_headers": {
|
||||
"type":"array",
|
||||
"description": "Proxy headers to hide",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"else": {
|
||||
"properties": {
|
||||
"port": {
|
||||
"markdownDescription": "`listening port`:`target port | service type`",
|
||||
"type": "string",
|
||||
"pattern": "^[0-9]+\\:[0-9a-z]+$",
|
||||
"patternErrorMessage": "'port' must be in the format of '<listening port>:<target port | service type>'"
|
||||
},
|
||||
"path": {
|
||||
"not": true
|
||||
},
|
||||
"path_mode": {
|
||||
"not": true
|
||||
},
|
||||
"set_headers": {
|
||||
"not": true
|
||||
},
|
||||
"hide_headers": {
|
||||
"not": true
|
||||
}
|
||||
},
|
||||
"required": ["port"]
|
||||
}
|
||||
},
|
||||
{
|
||||
"if": {
|
||||
"not": {
|
||||
"properties": {
|
||||
"scheme": {
|
||||
"const": "https"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"then": {
|
||||
"properties": {
|
||||
"no_tls_verify": {
|
||||
"not": true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
}
|
||||
BIN
screenshots/config_editor.png
Normal file
BIN
screenshots/config_editor.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 191 KiB |
BIN
screenshots/panel.png
Executable file → Normal file
BIN
screenshots/panel.png
Executable file → Normal file
Binary file not shown.
|
Before Width: | Height: | Size: 149 KiB After Width: | Height: | Size: 304 KiB |
114
setup-binary.sh
Normal file
114
setup-binary.sh
Normal file
@@ -0,0 +1,114 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
REPO_URL=https://github.com/yusing/go-proxy
|
||||
BIN_URL="${REPO_URL}/releases/download/${VERSION}/go-proxy"
|
||||
SRC_URL="${REPO_URL}/archive/refs/tags/${VERSION}.tar.gz"
|
||||
APP_ROOT="/opt/go-proxy/${VERSION}"
|
||||
LOG_FILE="/tmp/go-proxy-setup.log"
|
||||
|
||||
if [ -z "$VERSION" ] || [ "$VERSION" = "latest" ]; then
|
||||
VERSION_URL="${REPO_URL}/raw/main/version.txt"
|
||||
VERSION=$(wget -qO- "$VERSION_URL")
|
||||
fi
|
||||
|
||||
if [ -d "$APP_ROOT" ]; then
|
||||
echo "$APP_ROOT already exists"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# check if wget exists
|
||||
if ! [ -x "$(command -v wget)" ]; then
|
||||
echo "wget is not installed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# check if make exists
|
||||
if ! [ -x "$(command -v make)" ]; then
|
||||
echo "make is not installed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
dl_source() {
|
||||
cd /tmp
|
||||
echo "Downloading go-proxy source ${VERSION}"
|
||||
wget -c "${SRC_URL}" -O go-proxy.tar.gz &> $LOG_FILE
|
||||
if [ $? -gt 0 ]; then
|
||||
echo "Source download failed, check your internet connection and version number"
|
||||
exit 1
|
||||
fi
|
||||
echo "Done"
|
||||
echo "Extracting go-proxy source ${VERSION}"
|
||||
tar xzf go-proxy.tar.gz &> $LOG_FILE
|
||||
if [ $? -gt 0 ]; then
|
||||
echo "failed to untar go-proxy.tar.gz"
|
||||
exit 1
|
||||
fi
|
||||
rm go-proxy.tar.gz
|
||||
mkdir -p "$(dirname "${APP_ROOT}")"
|
||||
mv "go-proxy-${VERSION}" "$APP_ROOT"
|
||||
cd "$APP_ROOT"
|
||||
echo "Done"
|
||||
}
|
||||
dl_binary() {
|
||||
mkdir -p bin
|
||||
echo "Downloading go-proxy binary ${VERSION}"
|
||||
wget -c "${BIN_URL}" -O bin/go-proxy &> $LOG_FILE
|
||||
if [ $? -gt 0 ]; then
|
||||
echo "Binary download failed, check your internet connection and version number"
|
||||
exit 1
|
||||
fi
|
||||
chmod +x bin/go-proxy
|
||||
echo "Done"
|
||||
}
|
||||
setup() {
|
||||
make setup &> $LOG_FILE
|
||||
if [ $? -gt 0 ]; then
|
||||
echo "make setup failed"
|
||||
exit 1
|
||||
fi
|
||||
# SETUP_CODEMIRROR = 1
|
||||
if [ "$SETUP_CODEMIRROR" != "0" ]; then
|
||||
make setup-codemirror &> $LOG_FILE || echo "make setup-codemirror failed, ignored"
|
||||
fi
|
||||
}
|
||||
|
||||
dl_source
|
||||
dl_binary
|
||||
setup
|
||||
|
||||
# setup systemd
|
||||
|
||||
# check if systemctl exists
|
||||
if ! command -v systemctl is-system-running > /dev/null 2>&1; then
|
||||
echo "systemctl not found, skipping systemd setup"
|
||||
exit 0
|
||||
fi
|
||||
systemctl_failed() {
|
||||
echo "Failed to enable and start go-proxy"
|
||||
systemctl status go-proxy
|
||||
exit 1
|
||||
}
|
||||
echo "Setting up systemd service"
|
||||
cat <<EOF > /etc/systemd/system/go-proxy.service
|
||||
[Unit]
|
||||
Description=go-proxy reverse proxy
|
||||
After=network-online.target
|
||||
Wants=network-online.target systemd-networkd-wait-online.service
|
||||
[Service]
|
||||
Type=simple
|
||||
ExecStart=${APP_ROOT}/bin/go-proxy
|
||||
WorkingDirectory=${APP_ROOT}
|
||||
Environment="GOPROXY_IS_SYSTEMD=1"
|
||||
Restart=on-failure
|
||||
RestartSec=1s
|
||||
KillMode=process
|
||||
KillSignal=SIGINT
|
||||
TimeoutStartSec=5s
|
||||
TimeoutStopSec=5s
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
systemctl daemon-reload &>$LOG_FILE || systemctl_failed
|
||||
systemctl enable --now go-proxy &>$LOG_FILE || systemctl_failed
|
||||
echo "Done"
|
||||
echo "Setup complete"
|
||||
14
setup-docker.sh
Normal file
14
setup-docker.sh
Normal file
@@ -0,0 +1,14 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -e
|
||||
if [ -z "$BRANCH" ]; then
|
||||
BRANCH="main"
|
||||
fi
|
||||
BASE_URL="https://github.com/yusing/go-proxy/raw/${BRANCH}"
|
||||
mkdir -p go-proxy
|
||||
cd go-proxy
|
||||
mkdir -p config
|
||||
mkdir -p certs
|
||||
[ -f compose.yml ] || wget -cO - ${BASE_URL}/compose.example.yml > compose.yml
|
||||
[ -f config/config.yml ] || wget -cO - ${BASE_URL}/config.example.yml > config/config.yml
|
||||
[ -f config/providers.yml ] || touch config/providers.yml
|
||||
38
src/go-proxy/args.go
Normal file
38
src/go-proxy/args.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type Args struct {
|
||||
Command string
|
||||
}
|
||||
|
||||
const (
|
||||
CommandStart = ""
|
||||
CommandValidate = "validate"
|
||||
CommandReload = "reload"
|
||||
)
|
||||
|
||||
var ValidCommands = []string{CommandStart, CommandValidate, CommandReload}
|
||||
|
||||
func getArgs() Args {
|
||||
var args Args
|
||||
flag.Parse()
|
||||
args.Command = flag.Arg(0)
|
||||
if err := validateArgs(args.Command, ValidCommands); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
return args
|
||||
}
|
||||
|
||||
func validateArgs[T comparable](arg T, validArgs []T) error {
|
||||
for _, v := range validArgs {
|
||||
if arg == v {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return NewNestedError("invalid argument").Subjectf("%v", arg)
|
||||
}
|
||||
331
src/go-proxy/autocert.go
Normal file
331
src/go-proxy/autocert.go
Normal file
@@ -0,0 +1,331 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto"
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"os"
|
||||
"path"
|
||||
"slices"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-acme/lego/v4/certcrypto"
|
||||
"github.com/go-acme/lego/v4/certificate"
|
||||
"github.com/go-acme/lego/v4/challenge"
|
||||
"github.com/go-acme/lego/v4/lego"
|
||||
"github.com/go-acme/lego/v4/providers/dns/clouddns"
|
||||
"github.com/go-acme/lego/v4/providers/dns/cloudflare"
|
||||
"github.com/go-acme/lego/v4/providers/dns/duckdns"
|
||||
"github.com/go-acme/lego/v4/registration"
|
||||
)
|
||||
|
||||
type ProviderOptions map[string]string
|
||||
type ProviderGenerator func(ProviderOptions) (challenge.Provider, error)
|
||||
type CertExpiries map[string]time.Time
|
||||
|
||||
type AutoCertConfig struct {
|
||||
Email string `json:"email"`
|
||||
Domains []string `yaml:",flow" json:"domains"`
|
||||
Provider string `json:"provider"`
|
||||
Options ProviderOptions `yaml:",flow" json:"options"`
|
||||
}
|
||||
|
||||
type AutoCertUser struct {
|
||||
Email string
|
||||
Registration *registration.Resource
|
||||
key crypto.PrivateKey
|
||||
}
|
||||
|
||||
func (u *AutoCertUser) GetEmail() string {
|
||||
return u.Email
|
||||
}
|
||||
func (u *AutoCertUser) GetRegistration() *registration.Resource {
|
||||
return u.Registration
|
||||
}
|
||||
func (u *AutoCertUser) GetPrivateKey() crypto.PrivateKey {
|
||||
return u.key
|
||||
}
|
||||
|
||||
type AutoCertProvider interface {
|
||||
GetCert(*tls.ClientHelloInfo) (*tls.Certificate, error)
|
||||
GetName() string
|
||||
GetExpiries() CertExpiries
|
||||
LoadCert() bool
|
||||
ObtainCert() NestedErrorLike
|
||||
ShouldRenewOn() time.Time
|
||||
ScheduleRenewal()
|
||||
}
|
||||
|
||||
func (cfg AutoCertConfig) GetProvider() (AutoCertProvider, error) {
|
||||
ne := NewNestedError("invalid autocert config")
|
||||
|
||||
if len(cfg.Domains) == 0 {
|
||||
ne.Extra("no domains specified")
|
||||
}
|
||||
if cfg.Provider == "" {
|
||||
ne.Extra("no provider specified")
|
||||
}
|
||||
if cfg.Email == "" {
|
||||
ne.Extra("no email specified")
|
||||
}
|
||||
gen, ok := providersGenMap[cfg.Provider]
|
||||
if !ok {
|
||||
ne.Extraf("unknown provider: %q", cfg.Provider)
|
||||
}
|
||||
if ne.HasExtras() {
|
||||
return nil, ne
|
||||
}
|
||||
|
||||
ne = NewNestedError("unable to create provider")
|
||||
privKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if err != nil {
|
||||
return nil, ne.With(NewNestedError("unable to generate private key").With(err))
|
||||
}
|
||||
user := &AutoCertUser{
|
||||
Email: cfg.Email,
|
||||
key: privKey,
|
||||
}
|
||||
legoCfg := lego.NewConfig(user)
|
||||
legoCfg.Certificate.KeyType = certcrypto.RSA2048
|
||||
legoClient, err := lego.NewClient(legoCfg)
|
||||
if err != nil {
|
||||
return nil, ne.With(NewNestedError("unable to create lego client").With(err))
|
||||
}
|
||||
base := &autoCertProvider{
|
||||
name: cfg.Provider,
|
||||
cfg: cfg,
|
||||
user: user,
|
||||
legoCfg: legoCfg,
|
||||
client: legoClient,
|
||||
}
|
||||
legoProvider, err := gen(cfg.Options)
|
||||
if err != nil {
|
||||
return nil, ne.With(err)
|
||||
}
|
||||
err = legoClient.Challenge.SetDNS01Provider(legoProvider)
|
||||
if err != nil {
|
||||
return nil, ne.With(NewNestedError("unable to set challenge provider").With(err))
|
||||
}
|
||||
return base, nil
|
||||
}
|
||||
|
||||
type autoCertProvider struct {
|
||||
name string
|
||||
cfg AutoCertConfig
|
||||
user *AutoCertUser
|
||||
legoCfg *lego.Config
|
||||
client *lego.Client
|
||||
|
||||
tlsCert *tls.Certificate
|
||||
certExpiries CertExpiries
|
||||
mutex sync.Mutex
|
||||
}
|
||||
|
||||
func (p *autoCertProvider) GetCert(_ *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
||||
if p.tlsCert == nil {
|
||||
return nil, NewNestedError("no certificate available")
|
||||
}
|
||||
return p.tlsCert, nil
|
||||
}
|
||||
|
||||
func (p *autoCertProvider) GetName() string {
|
||||
return p.name
|
||||
}
|
||||
|
||||
func (p *autoCertProvider) GetExpiries() CertExpiries {
|
||||
return p.certExpiries
|
||||
}
|
||||
|
||||
func (p *autoCertProvider) ObtainCert() NestedErrorLike {
|
||||
ne := NewNestedError("failed to obtain certificate")
|
||||
|
||||
client := p.client
|
||||
if p.user.Registration == nil {
|
||||
reg, err := client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
|
||||
if err != nil {
|
||||
return ne.With(NewNestedError("failed to register account").With(err))
|
||||
}
|
||||
p.user.Registration = reg
|
||||
}
|
||||
req := certificate.ObtainRequest{
|
||||
Domains: p.cfg.Domains,
|
||||
Bundle: true,
|
||||
}
|
||||
cert, err := client.Certificate.Obtain(req)
|
||||
if err != nil {
|
||||
return ne.With(err)
|
||||
}
|
||||
err = p.saveCert(cert)
|
||||
if err != nil {
|
||||
return ne.With(NewNestedError("failed to save certificate").With(err))
|
||||
}
|
||||
tlsCert, err := tls.X509KeyPair(cert.Certificate, cert.PrivateKey)
|
||||
if err != nil {
|
||||
return ne.With(NewNestedError("failed to parse obtained certificate").With(err))
|
||||
}
|
||||
expiries, err := getCertExpiries(&tlsCert)
|
||||
if err != nil {
|
||||
return ne.With(NewNestedError("failed to get certificate expiry").With(err))
|
||||
}
|
||||
p.tlsCert = &tlsCert
|
||||
p.certExpiries = expiries
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *autoCertProvider) LoadCert() bool {
|
||||
cert, err := tls.LoadX509KeyPair(certFileDefault, keyFileDefault)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
expiries, err := getCertExpiries(&cert)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
p.tlsCert = &cert
|
||||
p.certExpiries = expiries
|
||||
p.renewIfNeeded()
|
||||
return true
|
||||
}
|
||||
|
||||
func (p *autoCertProvider) ShouldRenewOn() time.Time {
|
||||
for _, expiry := range p.certExpiries {
|
||||
return expiry.AddDate(0, -1, 0)
|
||||
}
|
||||
// this line should never be reached
|
||||
panic("no certificate available")
|
||||
}
|
||||
|
||||
func (p *autoCertProvider) ScheduleRenewal() {
|
||||
for {
|
||||
t := time.Until(p.ShouldRenewOn())
|
||||
aclog.Infof("next renewal in %v", t.Round(time.Second))
|
||||
time.Sleep(t)
|
||||
err := p.renewIfNeeded()
|
||||
if err != nil {
|
||||
aclog.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *autoCertProvider) saveCert(cert *certificate.Resource) NestedErrorLike {
|
||||
err := os.MkdirAll(path.Dir(certFileDefault), 0644)
|
||||
if err != nil {
|
||||
return NewNestedError("unable to create cert directory").With(err)
|
||||
}
|
||||
err = os.WriteFile(keyFileDefault, cert.PrivateKey, 0600) // -rw-------
|
||||
if err != nil {
|
||||
return NewNestedError("unable to write key file").With(err)
|
||||
}
|
||||
err = os.WriteFile(certFileDefault, cert.Certificate, 0644) // -rw-r--r--
|
||||
if err != nil {
|
||||
return NewNestedError("unable to write cert file").With(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *autoCertProvider) needRenewal() bool {
|
||||
expired := time.Now().After(p.ShouldRenewOn())
|
||||
if expired {
|
||||
return true
|
||||
}
|
||||
if len(p.cfg.Domains) != len(p.certExpiries) {
|
||||
return true
|
||||
}
|
||||
wantedDomains := make([]string, len(p.cfg.Domains))
|
||||
certDomains := make([]string, len(p.certExpiries))
|
||||
copy(wantedDomains, p.cfg.Domains)
|
||||
i := 0
|
||||
for domain := range p.certExpiries {
|
||||
certDomains[i] = domain
|
||||
i++
|
||||
}
|
||||
slices.Sort(wantedDomains)
|
||||
slices.Sort(certDomains)
|
||||
for i, domain := range certDomains {
|
||||
if domain != wantedDomains[i] {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (p *autoCertProvider) renewIfNeeded() NestedErrorLike {
|
||||
if !p.needRenewal() {
|
||||
return nil
|
||||
}
|
||||
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
|
||||
if !p.needRenewal() {
|
||||
return nil
|
||||
}
|
||||
|
||||
trials := 0
|
||||
for {
|
||||
err := p.ObtainCert()
|
||||
if err == nil {
|
||||
aclog.Info("renewed certificate")
|
||||
return nil
|
||||
}
|
||||
trials++
|
||||
if trials > 3 {
|
||||
return NewNestedError("failed to renew certificate after 3 trials").With(err)
|
||||
}
|
||||
aclog.Errorf("failed to renew certificate: %v, trying again in 5 seconds", err)
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func providerGenerator[CT any, PT challenge.Provider](
|
||||
defaultCfg func() *CT,
|
||||
newProvider func(*CT) (PT, error),
|
||||
) ProviderGenerator {
|
||||
return func(opt ProviderOptions) (challenge.Provider, error) {
|
||||
cfg := defaultCfg()
|
||||
err := setOptions(cfg, opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p, err := newProvider(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
}
|
||||
|
||||
func getCertExpiries(cert *tls.Certificate) (CertExpiries, error) {
|
||||
r := make(CertExpiries, len(cert.Certificate))
|
||||
for _, cert := range cert.Certificate {
|
||||
x509Cert, err := x509.ParseCertificate(cert)
|
||||
if err != nil {
|
||||
return nil, NewNestedError("unable to parse certificate").With(err)
|
||||
}
|
||||
if x509Cert.IsCA {
|
||||
continue
|
||||
}
|
||||
r[x509Cert.Subject.CommonName] = x509Cert.NotAfter
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func setOptions[T interface{}](cfg *T, opt ProviderOptions) error {
|
||||
for k, v := range opt {
|
||||
err := setFieldFromSnake(cfg, k, v)
|
||||
if err != nil {
|
||||
return NewNestedError("unable to set option").Subject(k).With(err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var providersGenMap = map[string]ProviderGenerator{
|
||||
"cloudflare": providerGenerator(cloudflare.NewDefaultConfig, cloudflare.NewDNSProviderConfig),
|
||||
"clouddns": providerGenerator(clouddns.NewDefaultConfig, clouddns.NewDNSProviderConfig),
|
||||
"duckdns": providerGenerator(duckdns.NewDefaultConfig, duckdns.NewDNSProviderConfig),
|
||||
}
|
||||
@@ -1,102 +1,200 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// commented out if unused
|
||||
type Config interface {
|
||||
Value() configModel
|
||||
// Load() error
|
||||
MustLoad()
|
||||
GetAutoCertProvider() (AutoCertProvider, error)
|
||||
// MustReload()
|
||||
// Reload() error
|
||||
Reload() error
|
||||
StartProviders()
|
||||
StopProviders()
|
||||
WatchChanges()
|
||||
StopWatching()
|
||||
}
|
||||
|
||||
func NewConfig() Config {
|
||||
cfg := &config{}
|
||||
cfg.watcher = NewFileWatcher(
|
||||
configPath,
|
||||
cfg.MustReload, // OnChange
|
||||
func() { os.Exit(1) }, // OnDelete
|
||||
)
|
||||
func NewConfig(path string) Config {
|
||||
cfg := &config{
|
||||
reader: &FileReader{Path: path},
|
||||
l: cfgl,
|
||||
}
|
||||
// must init fields above before creating watcher
|
||||
cfg.watcher = cfg.NewFileWatcher()
|
||||
return cfg
|
||||
}
|
||||
|
||||
func ValidateConfig(data []byte) error {
|
||||
cfg := &config{reader: &ByteReader{data}}
|
||||
return cfg.Load()
|
||||
}
|
||||
|
||||
func (cfg *config) Value() configModel {
|
||||
return *cfg.m
|
||||
}
|
||||
|
||||
func (cfg *config) Load() error {
|
||||
if cfg.reader == nil {
|
||||
panic("config reader not set")
|
||||
}
|
||||
|
||||
data, err := cfg.reader.Read()
|
||||
if err != nil {
|
||||
return NewNestedError("unable to read config file").With(err)
|
||||
}
|
||||
|
||||
model := defaultConfig()
|
||||
if err := yaml.Unmarshal(data, model); err != nil {
|
||||
return NewNestedError("unable to parse config file").With(err)
|
||||
}
|
||||
|
||||
ne := NewNestedError("invalid config")
|
||||
|
||||
err = validateYaml(configSchema, data)
|
||||
if err != nil {
|
||||
ne.With(err)
|
||||
}
|
||||
|
||||
pErrs := NewNestedError("these providers have errors")
|
||||
|
||||
for name, p := range model.Providers {
|
||||
if p.Kind != ProviderKind_File {
|
||||
continue
|
||||
}
|
||||
_, err := p.ValidateFile()
|
||||
if err != nil {
|
||||
pErrs.ExtraError(
|
||||
NewNestedError("provider file validation error").
|
||||
Subject(name).
|
||||
With(err),
|
||||
)
|
||||
}
|
||||
}
|
||||
if pErrs.HasExtras() {
|
||||
ne.With(pErrs)
|
||||
}
|
||||
if ne.HasInner() {
|
||||
return ne
|
||||
}
|
||||
|
||||
cfg.mutex.Lock()
|
||||
defer cfg.mutex.Unlock()
|
||||
|
||||
// unload if any
|
||||
cfg.StopProviders()
|
||||
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to read config file: %v", err)
|
||||
}
|
||||
|
||||
cfg.Providers = make(map[string]*Provider)
|
||||
if err = yaml.Unmarshal(data, &cfg); err != nil {
|
||||
return fmt.Errorf("unable to parse config file: %v", err)
|
||||
}
|
||||
|
||||
for name, p := range cfg.Providers {
|
||||
err := p.Init(name)
|
||||
if err != nil {
|
||||
cfgl.Errorf("failed to initialize provider %q %v", name, err)
|
||||
cfg.Providers[name] = nil
|
||||
}
|
||||
}
|
||||
|
||||
cfg.m = model
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cfg *config) MustLoad() {
|
||||
if err := cfg.Load(); err != nil {
|
||||
cfgl.Fatal(err)
|
||||
cfg.l.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (cfg *config) GetAutoCertProvider() (AutoCertProvider, error) {
|
||||
return cfg.m.AutoCert.GetProvider()
|
||||
}
|
||||
|
||||
func (cfg *config) Reload() error {
|
||||
return cfg.Load()
|
||||
cfg.StopProviders()
|
||||
if err := cfg.Load(); err != nil {
|
||||
return err
|
||||
}
|
||||
cfg.StartProviders()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cfg *config) MustReload() {
|
||||
cfg.MustLoad()
|
||||
if err := cfg.Reload(); err != nil {
|
||||
cfg.l.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (cfg *config) StartProviders() {
|
||||
if cfg.Providers == nil {
|
||||
cfgl.Fatal("providers not loaded")
|
||||
if cfg.providerInitialized {
|
||||
return
|
||||
}
|
||||
|
||||
cfg.mutex.Lock()
|
||||
defer cfg.mutex.Unlock()
|
||||
if cfg.providerInitialized {
|
||||
return
|
||||
}
|
||||
|
||||
pErrs := NewNestedError("failed to start these providers")
|
||||
|
||||
ParallelForEachKeyValue(cfg.m.Providers, func(name string, p *Provider) {
|
||||
err := p.Init(name)
|
||||
if err != nil {
|
||||
pErrs.ExtraError(NewNestedErrorFrom(err).Subjectf("%s providers %q", p.Kind, name))
|
||||
delete(cfg.m.Providers, name)
|
||||
}
|
||||
p.StartAllRoutes()
|
||||
})
|
||||
|
||||
cfg.providerInitialized = true
|
||||
|
||||
if pErrs.HasExtras() {
|
||||
cfg.l.Error(pErrs)
|
||||
}
|
||||
// Providers have their own mutex, no lock needed
|
||||
ParallelForEachValue(cfg.Providers, (*Provider).StartAllRoutes)
|
||||
}
|
||||
|
||||
func (cfg *config) StopProviders() {
|
||||
if cfg.Providers != nil {
|
||||
// Providers have their own mutex, no lock needed
|
||||
ParallelForEachValue(cfg.Providers, (*Provider).StopAllRoutes)
|
||||
if !cfg.providerInitialized {
|
||||
return
|
||||
}
|
||||
|
||||
cfg.mutex.Lock()
|
||||
defer cfg.mutex.Unlock()
|
||||
if !cfg.providerInitialized {
|
||||
return
|
||||
}
|
||||
ParallelForEachValue(cfg.m.Providers, (*Provider).StopAllRoutes)
|
||||
cfg.m.Providers = make(map[string]*Provider)
|
||||
cfg.providerInitialized = false
|
||||
}
|
||||
|
||||
func (cfg *config) WatchChanges() {
|
||||
if cfg.watcher == nil {
|
||||
return
|
||||
}
|
||||
cfg.watcher.Start()
|
||||
}
|
||||
|
||||
func (cfg *config) StopWatching() {
|
||||
if cfg.watcher == nil {
|
||||
return
|
||||
}
|
||||
cfg.watcher.Stop()
|
||||
}
|
||||
|
||||
type config struct {
|
||||
Providers map[string]*Provider `yaml:",flow"`
|
||||
watcher Watcher
|
||||
mutex sync.Mutex
|
||||
type configModel struct {
|
||||
Providers map[string]*Provider `yaml:",flow" json:"providers"`
|
||||
AutoCert AutoCertConfig `yaml:",flow" json:"autocert"`
|
||||
TimeoutShutdown time.Duration `yaml:"timeout_shutdown" json:"timeout_shutdown"`
|
||||
RedirectToHTTPS bool `yaml:"redirect_to_https" json:"redirect_to_https"`
|
||||
}
|
||||
|
||||
func defaultConfig() *configModel {
|
||||
return &configModel{
|
||||
TimeoutShutdown: 3 * time.Second,
|
||||
RedirectToHTTPS: false,
|
||||
}
|
||||
}
|
||||
|
||||
type config struct {
|
||||
m *configModel
|
||||
|
||||
l logrus.FieldLogger
|
||||
reader Reader
|
||||
watcher Watcher
|
||||
mutex sync.Mutex
|
||||
providerInitialized bool
|
||||
}
|
||||
|
||||
@@ -7,11 +7,12 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/santhosh-tekuri/jsonschema"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var (
|
||||
ImageNamePortMap = map[string]string{
|
||||
ImageNamePortMapTCP = map[string]string{
|
||||
"postgres": "5432",
|
||||
"mysql": "3306",
|
||||
"mariadb": "3306",
|
||||
@@ -21,7 +22,7 @@ var (
|
||||
"rabbitmq": "5672",
|
||||
"mongo": "27017",
|
||||
}
|
||||
ExtraNamePortMap = map[string]string{
|
||||
ExtraNamePortMapTCP = map[string]string{
|
||||
"dns": "53",
|
||||
"ssh": "22",
|
||||
"ftp": "21",
|
||||
@@ -29,18 +30,44 @@ var (
|
||||
"pop3": "110",
|
||||
"imap": "143",
|
||||
}
|
||||
NamePortMap = func() map[string]string {
|
||||
NamePortMapTCP = func() map[string]string {
|
||||
m := make(map[string]string)
|
||||
for k, v := range ImageNamePortMap {
|
||||
for k, v := range ImageNamePortMapTCP {
|
||||
m[k] = v
|
||||
}
|
||||
for k, v := range ExtraNamePortMap {
|
||||
for k, v := range ExtraNamePortMapTCP {
|
||||
m[k] = v
|
||||
}
|
||||
return m
|
||||
}()
|
||||
)
|
||||
|
||||
var ImageNamePortMapHTTP = map[string]uint16{
|
||||
"nginx": 80,
|
||||
"httpd": 80,
|
||||
"adguardhome": 3000,
|
||||
"gogs": 3000,
|
||||
"gitea": 3000,
|
||||
"portainer": 9000,
|
||||
"portainer-ce": 9000,
|
||||
"home-assistant": 8123,
|
||||
"homebridge": 8581,
|
||||
"uptime-kuma": 3001,
|
||||
"changedetection.io": 3000,
|
||||
"prometheus": 9090,
|
||||
"grafana": 3000,
|
||||
"dockge": 5001,
|
||||
"nginx-proxy-manager": 81,
|
||||
}
|
||||
|
||||
var wellKnownHTTPPorts = map[uint16]bool{
|
||||
80: true,
|
||||
8000: true,
|
||||
8008: true,
|
||||
8080: true,
|
||||
3000: true,
|
||||
}
|
||||
|
||||
var (
|
||||
StreamSchemes = []string{StreamType_TCP, StreamType_UDP} // TODO: support "tcp:udp", "udp:tcp"
|
||||
HTTPSchemes = []string{"http", "https"}
|
||||
@@ -63,11 +90,6 @@ const (
|
||||
ProviderKind_File = "file"
|
||||
)
|
||||
|
||||
const (
|
||||
certPath = "certs/cert.crt"
|
||||
keyPath = "certs/priv.key"
|
||||
)
|
||||
|
||||
// TODO: default + per proxy
|
||||
var (
|
||||
transport = &http.Transport{
|
||||
@@ -85,27 +107,85 @@ var (
|
||||
clone.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||||
return clone
|
||||
}()
|
||||
|
||||
healthCheckHttpClient = &http.Client{
|
||||
Timeout: 5 * time.Second,
|
||||
Transport: &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DisableKeepAlives: true,
|
||||
ForceAttemptHTTP2: true,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 5 * time.Second,
|
||||
KeepAlive: 5 * time.Second,
|
||||
}).DialContext,
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
const wildcardLabelPrefix = "proxy.*."
|
||||
const wildcardAlias = "*"
|
||||
|
||||
const clientUrlFromEnv = "FROM_ENV"
|
||||
|
||||
const (
|
||||
configPath = "config.yml"
|
||||
templatePath = "templates/panel.html"
|
||||
certBasePath = "certs/"
|
||||
certFileDefault = certBasePath + "cert.crt"
|
||||
keyFileDefault = certBasePath + "priv.key"
|
||||
|
||||
configBasePath = "config/"
|
||||
configPath = configBasePath + "config.yml"
|
||||
|
||||
templatesBasePath = "templates/"
|
||||
panelTemplatePath = templatesBasePath + "panel/index.html"
|
||||
configEditorTemplatePath = templatesBasePath + "config_editor/index.html"
|
||||
|
||||
schemaBasePath = "schema/"
|
||||
configSchemaPath = schemaBasePath + "config.schema.json"
|
||||
providersSchemaPath = schemaBasePath + "providers.schema.json"
|
||||
)
|
||||
|
||||
const StreamStopListenTimeout = 2 * time.Second
|
||||
var (
|
||||
configSchema *jsonschema.Schema
|
||||
providersSchema *jsonschema.Schema
|
||||
)
|
||||
|
||||
const (
|
||||
streamStopListenTimeout = 1 * time.Second
|
||||
streamDialTimeout = 3 * time.Second
|
||||
)
|
||||
|
||||
const udpBufferSize = 1500
|
||||
|
||||
var isHostNetworkMode = getEnvBool("GOPROXY_HOST_NETWORK")
|
||||
|
||||
var logLevel = func() logrus.Level {
|
||||
switch os.Getenv("GOPROXY_DEBUG") {
|
||||
case "1", "true":
|
||||
if getEnvBool("GOPROXY_DEBUG") {
|
||||
logrus.SetLevel(logrus.DebugLevel)
|
||||
}
|
||||
return logrus.GetLevel()
|
||||
}()
|
||||
|
||||
var redirectHTTP = os.Getenv("GOPROXY_REDIRECT_HTTP") != "0" && os.Getenv("GOPROXY_REDIRECT_HTTP") != "false"
|
||||
var isRunningAsService = getEnvBool("IS_SYSTEMD") || getEnvBool("GOPROXY_IS_SYSTEMD") // IS_SYSTEMD is deprecated
|
||||
|
||||
var noSchemaValidation = getEnvBool("GOPROXY_NO_SCHEMA_VALIDATION")
|
||||
|
||||
func getEnvBool(key string) bool {
|
||||
v := os.Getenv(key)
|
||||
return v == "1" || v == "true"
|
||||
}
|
||||
|
||||
func initSchema() {
|
||||
if noSchemaValidation {
|
||||
return
|
||||
}
|
||||
|
||||
c := jsonschema.NewCompiler()
|
||||
c.Draft = jsonschema.Draft7
|
||||
var err error
|
||||
if configSchema, err = c.Compile(configSchemaPath); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if providersSchema, err = c.Compile(providersSchemaPath); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -14,68 +15,82 @@ import (
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
func (p *Provider) setConfigField(c *ProxyConfig, label string, value string, prefix string) error {
|
||||
if strings.HasPrefix(label, prefix) {
|
||||
field := strings.TrimPrefix(label, prefix)
|
||||
field = utils.snakeToCamel(field)
|
||||
prop := reflect.ValueOf(c).Elem().FieldByName(field)
|
||||
if prop.Kind() == 0 {
|
||||
return fmt.Errorf("ignoring unknown field %s", field)
|
||||
}
|
||||
prop.Set(reflect.ValueOf(value))
|
||||
}
|
||||
return nil
|
||||
func setConfigField(pl *ProxyLabel, c *ProxyConfig) error {
|
||||
return setFieldFromSnake(c, pl.Field, pl.Value)
|
||||
}
|
||||
|
||||
func (p *Provider) getContainerProxyConfigs(container types.Container, clientIP string) []*ProxyConfig {
|
||||
func (p *Provider) getContainerProxyConfigs(container *types.Container, clientIP string) (ProxyConfigSlice, error) {
|
||||
var aliases []string
|
||||
|
||||
cfgs := make([]*ProxyConfig, 0)
|
||||
cfgs := make(ProxyConfigSlice, 0)
|
||||
cfgMap := make(map[string]*ProxyConfig)
|
||||
|
||||
container_name := strings.TrimPrefix(container.Names[0], "/")
|
||||
aliases_label, ok := container.Labels["proxy.aliases"]
|
||||
containerName := strings.TrimPrefix(container.Names[0], "/")
|
||||
aliasesLabel, ok := container.Labels["proxy.aliases"]
|
||||
|
||||
if !ok {
|
||||
aliases = []string{container_name}
|
||||
aliases = []string{containerName}
|
||||
} else {
|
||||
aliases = strings.Split(aliases_label, ",")
|
||||
v, _ := commaSepParser(aliasesLabel)
|
||||
aliases = v.([]string)
|
||||
}
|
||||
|
||||
if clientIP == "" && isHostNetworkMode {
|
||||
clientIP = "127.0.0.1"
|
||||
}
|
||||
isRemote := clientIP != ""
|
||||
|
||||
for _, alias := range aliases {
|
||||
l := p.l.WithField("container", container_name).WithField("alias", alias)
|
||||
config := NewProxyConfig(p)
|
||||
prefix := fmt.Sprintf("proxy.%s.", alias)
|
||||
for label, value := range container.Labels {
|
||||
err := p.setConfigField(&config, label, value, prefix)
|
||||
if err != nil {
|
||||
l.Error(err)
|
||||
}
|
||||
err = p.setConfigField(&config, label, value, wildcardLabelPrefix)
|
||||
if err != nil {
|
||||
l.Error(err)
|
||||
cfgMap[alias] = &ProxyConfig{}
|
||||
}
|
||||
|
||||
ne := NewNestedError("these labels have errors").Subject(containerName)
|
||||
|
||||
for label, value := range container.Labels {
|
||||
pl, err := parseProxyLabel(label, value)
|
||||
if err != nil {
|
||||
if !errors.Is(err, errNotProxyLabel) {
|
||||
ne.ExtraError(NewNestedErrorFrom(err).Subject(label))
|
||||
}
|
||||
continue
|
||||
}
|
||||
if pl.Alias == wildcardAlias {
|
||||
for alias := range cfgMap {
|
||||
pl.Alias = alias
|
||||
err = setConfigField(pl, cfgMap[alias])
|
||||
if err != nil {
|
||||
ne.ExtraError(NewNestedErrorFrom(err).Subject(pl.Alias))
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
config, ok := cfgMap[pl.Alias]
|
||||
if !ok {
|
||||
ne.ExtraError(NewNestedError("unknown alias").Subject(pl.Alias))
|
||||
continue
|
||||
}
|
||||
err = setConfigField(pl, config)
|
||||
if err != nil {
|
||||
ne.ExtraError(NewNestedErrorFrom(err).Subject(pl.Alias))
|
||||
}
|
||||
}
|
||||
|
||||
for alias, config := range cfgMap {
|
||||
l := p.l.WithField("alias", alias)
|
||||
if config.Port == "" {
|
||||
config.Port = fmt.Sprintf("%d", selectPort(container))
|
||||
config.Port = fmt.Sprintf("%d", selectPort(container, isRemote))
|
||||
}
|
||||
if config.Port == "0" {
|
||||
// no ports exposed or specified
|
||||
l.Debugf("no ports exposed, ignored")
|
||||
l.Infof("no ports exposed, ignored")
|
||||
continue
|
||||
}
|
||||
if config.Scheme == "" {
|
||||
switch {
|
||||
case strings.HasSuffix(config.Port, "443"):
|
||||
config.Scheme = "https"
|
||||
case strings.HasPrefix(container.Image, "sha256:"):
|
||||
config.Scheme = "http"
|
||||
default:
|
||||
imageSplit := strings.Split(container.Image, "/")
|
||||
imageSplit = strings.Split(imageSplit[len(imageSplit)-1], ":")
|
||||
imageName := imageSplit[0]
|
||||
_, isKnownImage := ImageNamePortMap[imageName]
|
||||
imageName := getImageName(container)
|
||||
_, isKnownImage := ImageNamePortMapTCP[imageName]
|
||||
if isKnownImage {
|
||||
config.Scheme = "tcp"
|
||||
} else {
|
||||
@@ -84,9 +99,22 @@ func (p *Provider) getContainerProxyConfigs(container types.Container, clientIP
|
||||
}
|
||||
}
|
||||
if !isValidScheme(config.Scheme) {
|
||||
l.Warnf("unsupported scheme: %s, using http", config.Scheme)
|
||||
config.Scheme = "http"
|
||||
ne.Extra("unsupported scheme").Subject(config.Scheme)
|
||||
}
|
||||
|
||||
if isRemote && strings.HasPrefix(config.Port, "*") {
|
||||
var err error
|
||||
// find matching port
|
||||
srcPort := config.Port[1:]
|
||||
config.Port, err = findMatchingContainerPort(container, srcPort)
|
||||
if err != nil {
|
||||
ne.ExtraError(NewNestedErrorFrom(err).Subjectf("alias %s", alias))
|
||||
}
|
||||
if isStreamScheme(config.Scheme) {
|
||||
config.Port = fmt.Sprintf("%s:%s", srcPort, config.Port)
|
||||
}
|
||||
}
|
||||
|
||||
if config.Host == "" {
|
||||
switch {
|
||||
case isRemote:
|
||||
@@ -108,13 +136,20 @@ func (p *Provider) getContainerProxyConfigs(container types.Container, clientIP
|
||||
}
|
||||
}
|
||||
if config.Host == "" {
|
||||
config.Host = container_name
|
||||
config.Host = containerName
|
||||
}
|
||||
config.Alias = alias
|
||||
|
||||
cfgs = append(cfgs, &config)
|
||||
if ne.HasExtras() {
|
||||
continue
|
||||
}
|
||||
cfgs = append(cfgs, *config)
|
||||
}
|
||||
return cfgs
|
||||
|
||||
if ne.HasExtras() {
|
||||
return nil, ne
|
||||
}
|
||||
return cfgs, nil
|
||||
}
|
||||
|
||||
func (p *Provider) getDockerClient() (*client.Client, error) {
|
||||
@@ -151,7 +186,7 @@ func (p *Provider) getDockerClient() (*client.Client, error) {
|
||||
return client.NewClientWithOpts(dockerOpts...)
|
||||
}
|
||||
|
||||
func (p *Provider) getDockerProxyConfigs() ([]*ProxyConfig, error) {
|
||||
func (p *Provider) getDockerProxyConfigs() (ProxyConfigSlice, error) {
|
||||
var clientIP string
|
||||
|
||||
if p.Value == clientUrlFromEnv {
|
||||
@@ -159,7 +194,7 @@ func (p *Provider) getDockerProxyConfigs() ([]*ProxyConfig, error) {
|
||||
} else {
|
||||
url, err := client.ParseHostURL(p.Value)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to parse docker host url: %v", err)
|
||||
return nil, NewNestedError("invalid host url").Subject(p.Value).With(err)
|
||||
}
|
||||
clientIP = strings.Split(url.Host, ":")[0]
|
||||
}
|
||||
@@ -167,38 +202,85 @@ func (p *Provider) getDockerProxyConfigs() ([]*ProxyConfig, error) {
|
||||
dockerClient, err := p.getDockerClient()
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to create docker client: %v", err)
|
||||
return nil, NewNestedError("unable to create docker client").With(err)
|
||||
}
|
||||
|
||||
ctx, _ := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
containerSlice, err := dockerClient.ContainerList(ctx, container.ListOptions{All: true})
|
||||
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to list containers: %v", err)
|
||||
return nil, NewNestedError("unable to list containers").With(err)
|
||||
}
|
||||
|
||||
cfgs := make([]*ProxyConfig, 0)
|
||||
cfgs := make(ProxyConfigSlice, 0)
|
||||
|
||||
ne := NewNestedError("these containers have errors")
|
||||
for _, container := range containerSlice {
|
||||
cfgs = append(cfgs, p.getContainerProxyConfigs(container, clientIP)...)
|
||||
ccfgs, err := p.getContainerProxyConfigs(&container, clientIP)
|
||||
if err != nil {
|
||||
ne.ExtraError(err)
|
||||
continue
|
||||
}
|
||||
cfgs = append(cfgs, ccfgs...)
|
||||
}
|
||||
|
||||
if ne.HasExtras() {
|
||||
// print but ignore
|
||||
p.l.Error(ne)
|
||||
}
|
||||
|
||||
return cfgs, nil
|
||||
}
|
||||
|
||||
// var dockerUrlRegex = regexp.MustCompile(`^(?P<scheme>\w+)://(?P<host>[^:]+)(?P<port>:\d+)?(?P<path>/.*)?$`)
|
||||
func getImageName(c *types.Container) string {
|
||||
imageSplit := strings.Split(c.Image, "/")
|
||||
imageSplit = strings.Split(imageSplit[len(imageSplit)-1], ":")
|
||||
return imageSplit[0]
|
||||
}
|
||||
|
||||
func getPublicPort(p types.Port) uint16 { return p.PublicPort }
|
||||
func getPrivatePort(p types.Port) uint16 { return p.PrivatePort }
|
||||
|
||||
func selectPort(c types.Container) uint16 {
|
||||
if c.HostConfig.NetworkMode == "host" {
|
||||
return selectPortInternal(c, getPrivatePort)
|
||||
func selectPort(c *types.Container, isRemote bool) uint16 {
|
||||
if isRemote || c.HostConfig.NetworkMode == "host" {
|
||||
return selectPortInternal(c, getPublicPort)
|
||||
}
|
||||
return selectPortInternal(c, getPublicPort)
|
||||
return selectPortInternal(c, getPrivatePort)
|
||||
}
|
||||
|
||||
func selectPortInternal(c types.Container, getPort func(types.Port) uint16) uint16 {
|
||||
// used when isRemote is true
|
||||
func findMatchingContainerPort(c *types.Container, ps string) (string, error) {
|
||||
p, err := strconv.Atoi(ps)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
pWant := uint16(p)
|
||||
for _, pGot := range c.Ports {
|
||||
if pGot.PrivatePort == pWant {
|
||||
return fmt.Sprintf("%d", pGot.PublicPort), nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("port %d not found", p)
|
||||
}
|
||||
|
||||
func selectPortInternal(c *types.Container, getPort func(types.Port) uint16) uint16 {
|
||||
imageName := getImageName(c)
|
||||
// if is known image -> use known port
|
||||
if port, isKnown := ImageNamePortMapHTTP[imageName]; isKnown {
|
||||
for _, p := range c.Ports {
|
||||
if p.PrivatePort == port {
|
||||
return getPort(p)
|
||||
}
|
||||
}
|
||||
}
|
||||
// if it has known http port -> use it
|
||||
for _, p := range c.Ports {
|
||||
if isWellKnownHTTPPort(p.PrivatePort) {
|
||||
return getPort(p)
|
||||
}
|
||||
}
|
||||
// if it has any port -> use it
|
||||
for _, p := range c.Ports {
|
||||
if port := getPort(p); port != 0 {
|
||||
return port
|
||||
@@ -206,3 +288,8 @@ func selectPortInternal(c types.Container, getPort func(types.Port) uint16) uint
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func isWellKnownHTTPPort(port uint16) bool {
|
||||
_, ok := wellKnownHTTPPorts[port]
|
||||
return ok
|
||||
}
|
||||
|
||||
195
src/go-proxy/error.go
Normal file
195
src/go-proxy/error.go
Normal file
@@ -0,0 +1,195 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type NestedError struct {
|
||||
subject string
|
||||
message string
|
||||
extras []string
|
||||
inner *NestedError
|
||||
level int
|
||||
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
type NestedErrorLike interface {
|
||||
Error() string
|
||||
Inner() NestedErrorLike
|
||||
Level() int
|
||||
HasInner() bool
|
||||
HasExtras() bool
|
||||
|
||||
Extra(string) NestedErrorLike
|
||||
Extraf(string, ...any) NestedErrorLike
|
||||
ExtraError(error) NestedErrorLike
|
||||
Subject(string) NestedErrorLike
|
||||
Subjectf(string, ...any) NestedErrorLike
|
||||
With(error) NestedErrorLike
|
||||
|
||||
addLevel(int) NestedErrorLike
|
||||
copy() *NestedError
|
||||
}
|
||||
|
||||
func NewNestedError(message string) NestedErrorLike {
|
||||
return &NestedError{message: message, extras: make([]string, 0)}
|
||||
}
|
||||
|
||||
func NewNestedErrorf(format string, args ...any) NestedErrorLike {
|
||||
return NewNestedError(fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
func NewNestedErrorFrom(err error) NestedErrorLike {
|
||||
if err == nil {
|
||||
panic("cannot convert nil error to NestedError")
|
||||
}
|
||||
errUnwrap := errors.Unwrap(err)
|
||||
if errUnwrap != nil {
|
||||
return NewNestedErrorFrom(errUnwrap)
|
||||
}
|
||||
return NewNestedError(err.Error())
|
||||
}
|
||||
|
||||
func (ne *NestedError) Extra(s string) NestedErrorLike {
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
return ne
|
||||
}
|
||||
ne.Lock()
|
||||
defer ne.Unlock()
|
||||
ne.extras = append(ne.extras, s)
|
||||
return ne
|
||||
}
|
||||
|
||||
func (ne *NestedError) Extraf(format string, args ...any) NestedErrorLike {
|
||||
return ne.Extra(fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
func (ne *NestedError) ExtraError(e error) NestedErrorLike {
|
||||
switch t := e.(type) {
|
||||
case NestedErrorLike:
|
||||
extra := t.copy()
|
||||
extra.addLevel(ne.Level() + 1)
|
||||
e = extra
|
||||
}
|
||||
return ne.Extra(e.Error())
|
||||
}
|
||||
|
||||
func (ne *NestedError) Subject(s string) NestedErrorLike {
|
||||
ne.subject = s
|
||||
return ne
|
||||
}
|
||||
|
||||
func (ne *NestedError) Subjectf(format string, args ...any) NestedErrorLike {
|
||||
ne.subject = fmt.Sprintf(format, args...)
|
||||
return ne
|
||||
}
|
||||
|
||||
func (ne *NestedError) Inner() NestedErrorLike {
|
||||
return ne.inner
|
||||
}
|
||||
|
||||
func (ne *NestedError) Level() int {
|
||||
return ne.level
|
||||
}
|
||||
|
||||
func (ne *NestedError) Error() string {
|
||||
var buf strings.Builder
|
||||
ne.writeToSB(&buf, ne.level, "")
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func (ne *NestedError) HasInner() bool {
|
||||
return ne.inner != nil
|
||||
}
|
||||
|
||||
func (ne *NestedError) HasExtras() bool {
|
||||
return len(ne.extras) > 0
|
||||
}
|
||||
|
||||
func (ne *NestedError) With(inner error) NestedErrorLike {
|
||||
ne.Lock()
|
||||
defer ne.Unlock()
|
||||
|
||||
var in *NestedError
|
||||
|
||||
switch t := inner.(type) {
|
||||
case NestedErrorLike:
|
||||
in = t.copy()
|
||||
default:
|
||||
in = &NestedError{message: t.Error()}
|
||||
}
|
||||
if ne.inner == nil {
|
||||
ne.inner = in
|
||||
} else {
|
||||
ne.inner.ExtraError(in)
|
||||
}
|
||||
root := ne
|
||||
for root.inner != nil {
|
||||
root.inner.level = root.level + 1
|
||||
root = root.inner
|
||||
}
|
||||
return ne
|
||||
}
|
||||
|
||||
func (ne *NestedError) addLevel(level int) NestedErrorLike {
|
||||
ne.level += level
|
||||
if ne.inner != nil {
|
||||
ne.inner.addLevel(level)
|
||||
}
|
||||
return ne
|
||||
}
|
||||
|
||||
func (ne *NestedError) copy() *NestedError {
|
||||
var inner *NestedError
|
||||
if ne.inner != nil {
|
||||
inner = ne.inner.copy()
|
||||
}
|
||||
return &NestedError{
|
||||
subject: ne.subject,
|
||||
message: ne.message,
|
||||
extras: ne.extras,
|
||||
inner: inner,
|
||||
}
|
||||
}
|
||||
|
||||
func (ne *NestedError) writeIndents(sb *strings.Builder, level int) {
|
||||
for i := 0; i < level; i++ {
|
||||
sb.WriteString(" ")
|
||||
}
|
||||
}
|
||||
|
||||
func (ne *NestedError) writeToSB(sb *strings.Builder, level int, prefix string) {
|
||||
ne.writeIndents(sb, level)
|
||||
sb.WriteString(prefix)
|
||||
|
||||
if ne.subject != "" {
|
||||
sb.WriteString(ne.subject)
|
||||
if ne.message != "" {
|
||||
sb.WriteString(": ")
|
||||
}
|
||||
}
|
||||
if ne.message != "" {
|
||||
sb.WriteString(ne.message)
|
||||
}
|
||||
if ne.HasExtras() || ne.HasInner() {
|
||||
sb.WriteString(":\n")
|
||||
}
|
||||
level += 1
|
||||
for _, l := range ne.extras {
|
||||
if l == "" {
|
||||
continue
|
||||
}
|
||||
ne.writeIndents(sb, level)
|
||||
sb.WriteString("- ")
|
||||
sb.WriteString(l)
|
||||
sb.WriteRune('\n')
|
||||
}
|
||||
if ne.inner != nil {
|
||||
ne.inner.writeToSB(sb, level, "- ")
|
||||
}
|
||||
}
|
||||
66
src/go-proxy/error_test.go
Normal file
66
src/go-proxy/error_test.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func AssertEq(t *testing.T, got, want string) {
|
||||
t.Helper()
|
||||
if got != want {
|
||||
t.Errorf("expected %q, got %q", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorSimple(t *testing.T) {
|
||||
ne := NewNestedError("foo bar")
|
||||
AssertEq(t, ne.Error(), "foo bar")
|
||||
ne.Subject("baz")
|
||||
AssertEq(t, ne.Error(), "baz: foo bar")
|
||||
}
|
||||
|
||||
func TestErrorSubjectOnly(t *testing.T) {
|
||||
ne := NewNestedError("").Subject("bar")
|
||||
AssertEq(t, ne.Error(), "bar")
|
||||
}
|
||||
|
||||
func TestErrorExtra(t *testing.T) {
|
||||
ne := NewNestedError("foo").Extra("bar").Extra("baz")
|
||||
AssertEq(t, ne.Error(), "foo:\n - bar\n - baz\n")
|
||||
}
|
||||
|
||||
func TestErrorNested(t *testing.T) {
|
||||
inner := NewNestedError("inner").
|
||||
Extra("123").
|
||||
Extra("456")
|
||||
inner2 := NewNestedError("inner").
|
||||
Subject("2").
|
||||
Extra("456").
|
||||
Extra("789")
|
||||
inner3 := NewNestedError("inner").
|
||||
Subject("3").
|
||||
Extra("456").
|
||||
Extra("789")
|
||||
ne := NewNestedError("foo").
|
||||
Extra("bar").
|
||||
Extra("baz").
|
||||
ExtraError(inner).
|
||||
With(inner.With(inner2.With(inner3)))
|
||||
want :=
|
||||
`foo:
|
||||
- bar
|
||||
- baz
|
||||
- inner:
|
||||
- 123
|
||||
- 456
|
||||
- inner:
|
||||
- 123
|
||||
- 456
|
||||
- 2: inner:
|
||||
- 456
|
||||
- 789
|
||||
- 3: inner:
|
||||
- 456
|
||||
- 789
|
||||
`
|
||||
AssertEq(t, ne.Error(), want)
|
||||
}
|
||||
@@ -1,39 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func (p *Provider) getFileProxyConfigs() ([]*ProxyConfig, error) {
|
||||
path := p.Value
|
||||
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to read config file %q: %v", path, err)
|
||||
}
|
||||
configMap := make(map[string]ProxyConfig, 0)
|
||||
configs := make([]*ProxyConfig, 0)
|
||||
err = yaml.Unmarshal(data, &configMap)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to parse config file %q: %v", path, err)
|
||||
}
|
||||
|
||||
for alias, cfg := range configMap {
|
||||
cfg.Alias = alias
|
||||
err = cfg.SetDefaults()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
configs = append(configs, &cfg)
|
||||
}
|
||||
return configs, nil
|
||||
} else if !os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("file not found: %s", path)
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
func (p *Provider) GetFilePath() string {
|
||||
return path.Join(configBasePath, p.Value)
|
||||
}
|
||||
|
||||
func (p *Provider) ValidateFile() (ProxyConfigSlice, error) {
|
||||
path := p.GetFilePath()
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, NewNestedError("unable to read providers file").Subject(path).With(err)
|
||||
}
|
||||
result, err := ValidateFileContent(data)
|
||||
if err != nil {
|
||||
return nil, NewNestedError(err.Error()).Subject(path)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func ValidateFileContent(data []byte) (ProxyConfigSlice, error) {
|
||||
configMap := make(ProxyConfigMap, 0)
|
||||
if err := yaml.Unmarshal(data, &configMap); err != nil {
|
||||
return nil, NewNestedError("invalid yaml").With(err)
|
||||
}
|
||||
|
||||
ne := NewNestedError("errors in providers")
|
||||
|
||||
configs := make(ProxyConfigSlice, len(configMap))
|
||||
i := 0
|
||||
for alias, cfg := range configMap {
|
||||
cfg.Alias = alias
|
||||
if err := cfg.SetDefaults(); err != nil {
|
||||
ne.ExtraError(err)
|
||||
} else {
|
||||
configs[i] = cfg
|
||||
}
|
||||
i++
|
||||
}
|
||||
|
||||
if err := validateYaml(providersSchema, data); err != nil {
|
||||
ne.ExtraError(err)
|
||||
}
|
||||
if ne.HasExtras() {
|
||||
return nil, ne
|
||||
}
|
||||
return configs, nil
|
||||
}
|
||||
|
||||
@@ -21,9 +21,10 @@ type HTTPRoute struct {
|
||||
}
|
||||
|
||||
func NewHTTPRoute(config *ProxyConfig) (*HTTPRoute, error) {
|
||||
url, err := url.Parse(fmt.Sprintf("%s://%s:%s", config.Scheme, config.Host, config.Port))
|
||||
u := fmt.Sprintf("%s://%s:%s", config.Scheme, config.Host, config.Port)
|
||||
url, err := url.Parse(u)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, NewNestedErrorf("invalid url").Subject(u).With(err)
|
||||
}
|
||||
|
||||
var tr *http.Transport
|
||||
@@ -33,11 +34,7 @@ func NewHTTPRoute(config *ProxyConfig) (*HTTPRoute, error) {
|
||||
tr = transport
|
||||
}
|
||||
|
||||
proxy := NewSingleHostReverseProxy(url, tr)
|
||||
|
||||
if !isValidProxyPathMode(config.PathMode) {
|
||||
return nil, fmt.Errorf("invalid path mode: %s", config.PathMode)
|
||||
}
|
||||
proxy := NewReverseProxy(url, tr, config)
|
||||
|
||||
route := &HTTPRoute{
|
||||
Alias: config.Alias,
|
||||
@@ -45,58 +42,34 @@ func NewHTTPRoute(config *ProxyConfig) (*HTTPRoute, error) {
|
||||
Path: config.Path,
|
||||
Proxy: proxy,
|
||||
PathMode: config.PathMode,
|
||||
l: hrlog.WithFields(logrus.Fields{
|
||||
"alias": config.Alias,
|
||||
"path": config.Path,
|
||||
"path_mode": config.PathMode,
|
||||
}),
|
||||
l: logrus.WithField("alias", config.Alias),
|
||||
}
|
||||
|
||||
var rewriteBegin = proxy.Rewrite
|
||||
var rewrite func(*ProxyRequest)
|
||||
var modifyResponse func(*http.Response) error
|
||||
|
||||
switch {
|
||||
case config.Path == "", config.PathMode == ProxyPathMode_Forward:
|
||||
// no path or forward path
|
||||
if config.Path == "" || config.PathMode == ProxyPathMode_Forward {
|
||||
rewrite = rewriteBegin
|
||||
case config.PathMode == ProxyPathMode_Sub:
|
||||
rewrite = func(pr *ProxyRequest) {
|
||||
rewriteBegin(pr)
|
||||
// disable compression
|
||||
pr.Out.Header.Set("Accept-Encoding", "identity")
|
||||
// remove path prefix
|
||||
pr.Out.URL.Path = strings.TrimPrefix(pr.Out.URL.Path, config.Path)
|
||||
}
|
||||
modifyResponse = func(r *http.Response) error {
|
||||
contentType, ok := r.Header["Content-Type"]
|
||||
if !ok || len(contentType) == 0 {
|
||||
route.l.Debug("unknown content type for ", r.Request.URL.String())
|
||||
return nil
|
||||
} else {
|
||||
switch config.PathMode {
|
||||
case ProxyPathMode_RemovedPath:
|
||||
rewrite = func(pr *ProxyRequest) {
|
||||
rewriteBegin(pr)
|
||||
pr.Out.URL.Path = strings.TrimPrefix(pr.Out.URL.Path, config.Path)
|
||||
}
|
||||
// disable cache
|
||||
r.Header.Set("Cache-Control", "no-store")
|
||||
|
||||
var err error = nil
|
||||
switch {
|
||||
case strings.HasPrefix(contentType[0], "text/html"):
|
||||
err = utils.respHTMLSubPath(r, config.Path)
|
||||
case strings.HasPrefix(contentType[0], "application/javascript"):
|
||||
err = utils.respJSSubPath(r, config.Path)
|
||||
default:
|
||||
route.l.Debug("unknown content type(s): ", contentType)
|
||||
case ProxyPathMode_Sub:
|
||||
rewrite = func(pr *ProxyRequest) {
|
||||
rewriteBegin(pr)
|
||||
// disable compression
|
||||
pr.Out.Header.Set("Accept-Encoding", "identity")
|
||||
// remove path prefix
|
||||
pr.Out.URL.Path = strings.TrimPrefix(pr.Out.URL.Path, config.Path)
|
||||
}
|
||||
if err != nil {
|
||||
err = fmt.Errorf("failed to remove path prefix %s: %v", config.Path, err)
|
||||
route.l.WithField("action", "path_sub").Error(err)
|
||||
r.Status = err.Error()
|
||||
r.StatusCode = http.StatusInternalServerError
|
||||
}
|
||||
return err
|
||||
}
|
||||
default:
|
||||
rewrite = func(pr *ProxyRequest) {
|
||||
rewriteBegin(pr)
|
||||
pr.Out.URL.Path = strings.TrimPrefix(pr.Out.URL.Path, config.Path)
|
||||
modifyResponse = config.pathSubModResp
|
||||
default:
|
||||
return nil, NewNestedError("invalid path mode").Subject(config.PathMode)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,21 +94,15 @@ func NewHTTPRoute(config *ProxyConfig) (*HTTPRoute, error) {
|
||||
return route, nil
|
||||
}
|
||||
|
||||
func (r *HTTPRoute) Start() {}
|
||||
func (r *HTTPRoute) Start() {
|
||||
httpRoutes.Get(r.Alias).Add(r.Path, r)
|
||||
}
|
||||
|
||||
func (r *HTTPRoute) Stop() {
|
||||
httpRoutes.Delete(r.Alias)
|
||||
}
|
||||
|
||||
func isValidProxyPathMode(mode string) bool {
|
||||
switch mode {
|
||||
case ProxyPathMode_Forward, ProxyPathMode_Sub, ProxyPathMode_RemovedPath:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func redirectToTLS(w http.ResponseWriter, r *http.Request) {
|
||||
func redirectToTLSHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// Redirect to the same host but with HTTPS
|
||||
var redirectCode int
|
||||
if r.Method == http.MethodGet {
|
||||
@@ -152,26 +119,44 @@ func findHTTPRoute(host string, path string) (*HTTPRoute, error) {
|
||||
if ok {
|
||||
return routeMap.FindMatch(path)
|
||||
}
|
||||
return nil, fmt.Errorf("no matching route for subdomain %s", subdomain)
|
||||
return nil, NewNestedError("no matching route for subdomain").Subject(subdomain)
|
||||
}
|
||||
|
||||
func httpProxyHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func proxyHandler(w http.ResponseWriter, r *http.Request) {
|
||||
route, err := findHTTPRoute(r.Host, r.URL.Path)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("request failed %s %s%s, error: %v",
|
||||
r.Method,
|
||||
r.Host,
|
||||
r.URL.Path,
|
||||
err,
|
||||
)
|
||||
http.Error(w, "404 Not Found", http.StatusNotFound)
|
||||
err = NewNestedError("request failed").
|
||||
Subjectf("%s %s%s", r.Method, r.Host, r.URL.Path).
|
||||
With(err)
|
||||
logrus.Error(err)
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
route.Proxy.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// alias -> (path -> routes)
|
||||
type HTTPRoutes = SafeMap[string, *pathPoolMap]
|
||||
func (config *ProxyConfig) pathSubModResp(r *http.Response) error {
|
||||
contentType, ok := r.Header["Content-Type"]
|
||||
if !ok || len(contentType) == 0 {
|
||||
return nil
|
||||
}
|
||||
// disable cache
|
||||
r.Header.Set("Cache-Control", "no-store")
|
||||
|
||||
var httpRoutes HTTPRoutes = NewSafeMap[string](newPathPoolMap)
|
||||
var err error = nil
|
||||
switch {
|
||||
case strings.HasPrefix(contentType[0], "text/html"):
|
||||
err = utils.respHTMLSubPath(r, config.Path)
|
||||
case strings.HasPrefix(contentType[0], "application/javascript"):
|
||||
err = utils.respJSSubPath(r, config.Path)
|
||||
}
|
||||
if err != nil {
|
||||
err = NewNestedError("failed to remove path prefix").Subject(config.Path).With(err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// alias -> (path -> routes)
|
||||
type HTTPRoutes SafeMap[string, pathPoolMap]
|
||||
|
||||
var httpRoutes HTTPRoutes = NewSafeMapOf[HTTPRoutes](newPathPoolMap)
|
||||
|
||||
127
src/go-proxy/io.go
Normal file
127
src/go-proxy/io.go
Normal file
@@ -0,0 +1,127 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
type Reader interface {
|
||||
Read() ([]byte, error)
|
||||
}
|
||||
|
||||
type FileReader struct {
|
||||
Path string
|
||||
}
|
||||
|
||||
func (r *FileReader) Read() ([]byte, error) {
|
||||
return os.ReadFile(r.Path)
|
||||
}
|
||||
|
||||
type ByteReader struct {
|
||||
Data []byte
|
||||
}
|
||||
|
||||
func (r *ByteReader) Read() ([]byte, error) {
|
||||
return r.Data, nil
|
||||
}
|
||||
|
||||
type ReadCloser struct {
|
||||
ctx context.Context
|
||||
r io.ReadCloser
|
||||
closed atomic.Bool
|
||||
}
|
||||
|
||||
func (r *ReadCloser) Read(p []byte) (int, error) {
|
||||
select {
|
||||
case <-r.ctx.Done():
|
||||
return 0, r.ctx.Err()
|
||||
default:
|
||||
return r.r.Read(p)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ReadCloser) Close() error {
|
||||
if r.closed.Load() {
|
||||
return nil
|
||||
}
|
||||
r.closed.Store(true)
|
||||
return r.r.Close()
|
||||
}
|
||||
|
||||
type Pipe struct {
|
||||
r ReadCloser
|
||||
w io.WriteCloser
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func NewPipe(ctx context.Context, r io.ReadCloser, w io.WriteCloser) *Pipe {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
return &Pipe{
|
||||
r: ReadCloser{ctx: ctx, r: r},
|
||||
w: w,
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Pipe) Start() error {
|
||||
return Copy(p.ctx, p.w, &p.r)
|
||||
}
|
||||
|
||||
func (p *Pipe) Stop() error {
|
||||
p.cancel()
|
||||
return errors.Join(fmt.Errorf("read: %w", p.r.Close()), fmt.Errorf("write: %w", p.w.Close()))
|
||||
}
|
||||
|
||||
func (p *Pipe) Write(b []byte) (int, error) {
|
||||
return p.w.Write(b)
|
||||
}
|
||||
|
||||
type BidirectionalPipe struct {
|
||||
pSrcDst Pipe
|
||||
pDstSrc Pipe
|
||||
}
|
||||
|
||||
func NewBidirectionalPipe(ctx context.Context, rw1 io.ReadWriteCloser, rw2 io.ReadWriteCloser) *BidirectionalPipe {
|
||||
return &BidirectionalPipe{
|
||||
pSrcDst: *NewPipe(ctx, rw1, rw2),
|
||||
pDstSrc: *NewPipe(ctx, rw2, rw1),
|
||||
}
|
||||
}
|
||||
|
||||
func NewBidirectionalPipeIntermediate(ctx context.Context, listener io.ReadCloser, client io.ReadWriteCloser, target io.ReadWriteCloser) *BidirectionalPipe {
|
||||
return &BidirectionalPipe{
|
||||
pSrcDst: *NewPipe(ctx, listener, client),
|
||||
pDstSrc: *NewPipe(ctx, client, target),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *BidirectionalPipe) Start() error {
|
||||
errCh := make(chan error, 2)
|
||||
go func() {
|
||||
errCh <- p.pSrcDst.Start()
|
||||
}()
|
||||
go func() {
|
||||
errCh <- p.pDstSrc.Start()
|
||||
}()
|
||||
for err := range errCh {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *BidirectionalPipe) Stop() error {
|
||||
return errors.Join(p.pSrcDst.Stop(), p.pDstSrc.Stop())
|
||||
}
|
||||
|
||||
func Copy(ctx context.Context, dst io.WriteCloser, src io.ReadCloser) error {
|
||||
_, err := io.Copy(dst, &ReadCloser{ctx: ctx, r: src})
|
||||
return err
|
||||
}
|
||||
@@ -2,9 +2,9 @@ package main
|
||||
|
||||
import "github.com/sirupsen/logrus"
|
||||
|
||||
var palog = logrus.WithField("component", "panel")
|
||||
var prlog = logrus.WithField("component", "provider")
|
||||
var cfgl = logrus.WithField("component", "config")
|
||||
var hrlog = logrus.WithField("component", "http_proxy")
|
||||
var srlog = logrus.WithField("component", "stream")
|
||||
var wlog = logrus.WithField("component", "watcher")
|
||||
var palog = logrus.WithField("?", "panel")
|
||||
var cfgl = logrus.WithField("?", "config")
|
||||
var hrlog = logrus.WithField("?", "http")
|
||||
var srlog = logrus.WithField("?", "stream")
|
||||
var wlog = logrus.WithField("?", "watcher")
|
||||
var aclog = logrus.WithField("?", "autocert")
|
||||
@@ -5,76 +5,133 @@ import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var err error
|
||||
var cfg Config
|
||||
|
||||
// flag.Parse()
|
||||
func main() {
|
||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||
|
||||
log.SetFormatter(&log.TextFormatter{
|
||||
ForceColors: true,
|
||||
DisableColors: false,
|
||||
FullTimestamp: true,
|
||||
})
|
||||
InitFSWatcher()
|
||||
InitDockerWatcher()
|
||||
args := getArgs()
|
||||
|
||||
cfg := NewConfig()
|
||||
if isRunningAsService {
|
||||
logrus.SetFormatter(&logrus.TextFormatter{
|
||||
DisableColors: true,
|
||||
DisableTimestamp: true,
|
||||
DisableSorting: true,
|
||||
})
|
||||
} else {
|
||||
logrus.SetFormatter(&logrus.TextFormatter{
|
||||
ForceColors: true,
|
||||
DisableColors: false,
|
||||
DisableSorting: true,
|
||||
FullTimestamp: true,
|
||||
TimestampFormat: "01-02 15:04:05",
|
||||
})
|
||||
}
|
||||
|
||||
if args.Command == CommandReload {
|
||||
err := utils.reloadServer()
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
initSchema()
|
||||
|
||||
cfg = NewConfig(configPath)
|
||||
cfg.MustLoad()
|
||||
|
||||
if args.Command == CommandValidate {
|
||||
logrus.Printf("config OK")
|
||||
return
|
||||
}
|
||||
|
||||
autoCertProvider, err := cfg.GetAutoCertProvider()
|
||||
|
||||
if err != nil {
|
||||
aclog.Warn(err)
|
||||
autoCertProvider = nil // TODO: remove, it is expected to be nil if error is not nil, but it is not for now
|
||||
}
|
||||
|
||||
var proxyServer *Server
|
||||
var panelServer *Server
|
||||
|
||||
if autoCertProvider != nil {
|
||||
ok := autoCertProvider.LoadCert()
|
||||
if !ok {
|
||||
if ne := autoCertProvider.ObtainCert(); ne != nil {
|
||||
aclog.Fatal(ne)
|
||||
}
|
||||
}
|
||||
for name, expiry := range autoCertProvider.GetExpiries() {
|
||||
aclog.Infof("certificate %q: expire on %v", name, expiry)
|
||||
}
|
||||
go autoCertProvider.ScheduleRenewal()
|
||||
}
|
||||
proxyServer = NewServer(ServerOptions{
|
||||
Name: "proxy",
|
||||
CertProvider: autoCertProvider,
|
||||
HTTPAddr: ":80",
|
||||
HTTPSAddr: ":443",
|
||||
Handler: http.HandlerFunc(proxyHandler),
|
||||
RedirectToHTTPS: cfg.Value().RedirectToHTTPS,
|
||||
})
|
||||
panelServer = NewServer(ServerOptions{
|
||||
Name: "panel",
|
||||
CertProvider: autoCertProvider,
|
||||
HTTPAddr: ":8080",
|
||||
HTTPSAddr: ":8443",
|
||||
Handler: panelHandler,
|
||||
RedirectToHTTPS: cfg.Value().RedirectToHTTPS,
|
||||
})
|
||||
|
||||
proxyServer.Start()
|
||||
panelServer.Start()
|
||||
|
||||
InitFSWatcher()
|
||||
|
||||
cfg.StartProviders()
|
||||
cfg.WatchChanges()
|
||||
|
||||
var certAvailable = utils.fileOK(certPath) && utils.fileOK(keyPath)
|
||||
|
||||
go func() {
|
||||
log.Info("starting http server on port 80")
|
||||
if certAvailable && redirectHTTP {
|
||||
err = http.ListenAndServe(":80", http.HandlerFunc(redirectToTLS))
|
||||
} else {
|
||||
err = http.ListenAndServe(":80", http.HandlerFunc(httpProxyHandler))
|
||||
}
|
||||
if err != nil {
|
||||
log.Fatal("http server error: ", err)
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
log.Infof("starting http panel on port 8080")
|
||||
err = http.ListenAndServe(":8080", http.HandlerFunc(panelHandler))
|
||||
if err != nil {
|
||||
log.Warning("http panel error: ", err)
|
||||
}
|
||||
}()
|
||||
|
||||
if certAvailable {
|
||||
go func() {
|
||||
log.Info("starting https server on port 443")
|
||||
err = http.ListenAndServeTLS(":443", certPath, keyPath, http.HandlerFunc(httpProxyHandler))
|
||||
if err != nil {
|
||||
log.Fatal("https server error: ", err)
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
log.Info("starting https panel on port 8443")
|
||||
err := http.ListenAndServeTLS(":8443", certPath, keyPath, http.HandlerFunc(panelHandler))
|
||||
if err != nil {
|
||||
log.Warning("http panel error: ", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
sig := make(chan os.Signal, 1)
|
||||
signal.Notify(sig, syscall.SIGINT)
|
||||
signal.Notify(sig, syscall.SIGTERM)
|
||||
signal.Notify(sig, syscall.SIGHUP)
|
||||
|
||||
<-sig
|
||||
cfg.StopWatching()
|
||||
cfg.StopProviders()
|
||||
StopFSWatcher()
|
||||
StopDockerWatcher()
|
||||
logrus.Info("shutting down")
|
||||
done := make(chan struct{}, 1)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(3)
|
||||
|
||||
go func() {
|
||||
StopFSWatcher()
|
||||
StopDockerWatcher()
|
||||
cfg.StopProviders()
|
||||
wg.Done()
|
||||
}()
|
||||
go func() {
|
||||
panelServer.Stop()
|
||||
proxyServer.Stop()
|
||||
wg.Done()
|
||||
}()
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(done)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
logrus.Info("shutdown complete")
|
||||
case <-time.After(cfg.Value().TimeoutShutdown * time.Second):
|
||||
logrus.Info("timeout waiting for shutdown")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ import "sync"
|
||||
type safeMap[KT comparable, VT interface{}] struct {
|
||||
SafeMap[KT, VT]
|
||||
m map[KT]VT
|
||||
mutex sync.Mutex
|
||||
defaultFactory func() VT
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
type SafeMap[KT comparable, VT interface{}] interface {
|
||||
@@ -22,7 +22,7 @@ type SafeMap[KT comparable, VT interface{}] interface {
|
||||
Iterator() map[KT]VT
|
||||
}
|
||||
|
||||
func NewSafeMap[KT comparable, VT interface{}](df ...func() VT) SafeMap[KT, VT] {
|
||||
func NewSafeMapOf[T SafeMap[KT, VT], KT comparable, VT interface{}](df ...func() VT) SafeMap[KT, VT] {
|
||||
if len(df) == 0 {
|
||||
return &safeMap[KT, VT]{
|
||||
m: make(map[KT]VT),
|
||||
@@ -35,23 +35,23 @@ func NewSafeMap[KT comparable, VT interface{}](df ...func() VT) SafeMap[KT, VT]
|
||||
}
|
||||
|
||||
func (m *safeMap[KT, VT]) Set(key KT, value VT) {
|
||||
m.mutex.Lock()
|
||||
m.Lock()
|
||||
m.m[key] = value
|
||||
m.mutex.Unlock()
|
||||
m.Unlock()
|
||||
}
|
||||
|
||||
func (m *safeMap[KT, VT]) Ensure(key KT) {
|
||||
m.mutex.Lock()
|
||||
m.Lock()
|
||||
if _, ok := m.m[key]; !ok {
|
||||
m.m[key] = m.defaultFactory()
|
||||
}
|
||||
m.mutex.Unlock()
|
||||
m.Unlock()
|
||||
}
|
||||
|
||||
func (m *safeMap[KT, VT]) Get(key KT) VT {
|
||||
m.mutex.Lock()
|
||||
m.RLock()
|
||||
value := m.m[key]
|
||||
m.mutex.Unlock()
|
||||
m.RUnlock()
|
||||
return value
|
||||
}
|
||||
|
||||
@@ -61,37 +61,36 @@ func (m *safeMap[KT, VT]) UnsafeGet(key KT) (VT, bool) {
|
||||
}
|
||||
|
||||
func (m *safeMap[KT, VT]) Delete(key KT) {
|
||||
m.mutex.Lock()
|
||||
m.Lock()
|
||||
delete(m.m, key)
|
||||
m.mutex.Unlock()
|
||||
m.Unlock()
|
||||
}
|
||||
|
||||
func (m *safeMap[KT, VT]) Clear() {
|
||||
m.mutex.Lock()
|
||||
m.Lock()
|
||||
m.m = make(map[KT]VT)
|
||||
m.mutex.Unlock()
|
||||
m.Unlock()
|
||||
}
|
||||
|
||||
func (m *safeMap[KT, VT]) Size() int {
|
||||
m.mutex.Lock()
|
||||
size := len(m.m)
|
||||
m.mutex.Unlock()
|
||||
return size
|
||||
m.RLock()
|
||||
defer m.RUnlock()
|
||||
return len(m.m)
|
||||
}
|
||||
|
||||
func (m *safeMap[KT, VT]) Contains(key KT) bool {
|
||||
m.mutex.Lock()
|
||||
m.RLock()
|
||||
_, ok := m.m[key]
|
||||
m.mutex.Unlock()
|
||||
m.RUnlock()
|
||||
return ok
|
||||
}
|
||||
|
||||
func (m *safeMap[KT, VT]) ForEach(fn func(key KT, value VT)) {
|
||||
m.mutex.Lock()
|
||||
m.RLock()
|
||||
for k, v := range m.m {
|
||||
fn(k, v)
|
||||
}
|
||||
m.mutex.Unlock()
|
||||
m.RUnlock()
|
||||
}
|
||||
|
||||
func (m *safeMap[KT, VT]) Iterator() map[KT]VT {
|
||||
|
||||
@@ -1,87 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
"os"
|
||||
"path"
|
||||
)
|
||||
|
||||
var healthCheckHttpClient = &http.Client{
|
||||
Timeout: 5 * time.Second,
|
||||
Transport: &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DisableKeepAlives: true,
|
||||
ForceAttemptHTTP2: true,
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: 5 * time.Second,
|
||||
KeepAlive: 5 * time.Second,
|
||||
}).DialContext,
|
||||
},
|
||||
var panelHandler = panelRouter()
|
||||
|
||||
func panelRouter() *http.ServeMux {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /{$}", panelServeFile)
|
||||
mux.HandleFunc("GET /{file}", panelServeFile)
|
||||
mux.HandleFunc("GET /panel/", panelPage)
|
||||
mux.HandleFunc("GET /panel/{file}", panelServeFile)
|
||||
mux.HandleFunc("HEAD /checkhealth", panelCheckTargetHealth)
|
||||
mux.HandleFunc("GET /config_editor/", panelConfigEditor)
|
||||
mux.HandleFunc("GET /config_editor/{file}", panelServeFile)
|
||||
mux.HandleFunc("GET /config/{file}", panelConfigGet)
|
||||
mux.HandleFunc("PUT /config/{file}", panelConfigUpdate)
|
||||
mux.HandleFunc("POST /reload", configReload)
|
||||
mux.HandleFunc("GET /codemirror/", panelServeFile)
|
||||
return mux
|
||||
}
|
||||
|
||||
func panelHandler(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/":
|
||||
panelIndex(w, r)
|
||||
return
|
||||
case "/checkhealth":
|
||||
panelCheckTargetHealth(w, r)
|
||||
return
|
||||
default:
|
||||
palog.Errorf("%s not found", r.URL.Path)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func panelIndex(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles(templatePath)
|
||||
|
||||
if err != nil {
|
||||
palog.Error(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
type allRoutes struct {
|
||||
func panelPage(w http.ResponseWriter, r *http.Request) {
|
||||
resp := struct {
|
||||
HTTPRoutes HTTPRoutes
|
||||
StreamRoutes StreamRoutes
|
||||
}
|
||||
}{httpRoutes, streamRoutes}
|
||||
|
||||
err = tmpl.Execute(w, allRoutes{
|
||||
HTTPRoutes: httpRoutes,
|
||||
StreamRoutes: streamRoutes,
|
||||
})
|
||||
if err != nil {
|
||||
palog.Error(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
panelRenderFile(w, r, panelTemplatePath, resp)
|
||||
}
|
||||
|
||||
func panelCheckTargetHealth(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodHead {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
targetUrl := r.URL.Query().Get("target")
|
||||
|
||||
if targetUrl == "" {
|
||||
http.Error(w, "target is required", http.StatusBadRequest)
|
||||
panelHandleErr(w, r, errors.New("target is required"), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
url, err := url.Parse(targetUrl)
|
||||
if err != nil {
|
||||
palog.Infof("failed to parse url %q, error: %v", targetUrl, err)
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
err = NewNestedError("failed to parse url").Subject(targetUrl).With(err)
|
||||
panelHandleErr(w, r, err, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
scheme := url.Scheme
|
||||
@@ -98,3 +65,89 @@ func panelCheckTargetHealth(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
func panelConfigEditor(w http.ResponseWriter, r *http.Request) {
|
||||
cfgFiles := make([]string, 0)
|
||||
cfgFiles = append(cfgFiles, path.Base(configPath))
|
||||
for _, p := range cfg.Value().Providers {
|
||||
if p.Kind != ProviderKind_File {
|
||||
continue
|
||||
}
|
||||
cfgFiles = append(cfgFiles, p.Value)
|
||||
}
|
||||
|
||||
panelRenderFile(w, r, configEditorTemplatePath, cfgFiles)
|
||||
}
|
||||
|
||||
func panelConfigGet(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, path.Join(configBasePath, r.PathValue("file")))
|
||||
}
|
||||
|
||||
func panelConfigUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
p := r.PathValue("file")
|
||||
content := make([]byte, r.ContentLength)
|
||||
_, err := r.Body.Read(content)
|
||||
if err != nil {
|
||||
panelHandleErr(w, r, NewNestedError("unable to read request body").Subject(p).With(err))
|
||||
return
|
||||
}
|
||||
if p == path.Base(configPath) {
|
||||
err = ValidateConfig(content)
|
||||
} else {
|
||||
_, err = ValidateFileContent(content)
|
||||
}
|
||||
if err != nil {
|
||||
panelHandleErr(w, r, err)
|
||||
return
|
||||
}
|
||||
p = path.Join(configBasePath, p)
|
||||
_, err = os.Stat(p)
|
||||
exists := !errors.Is(err, os.ErrNotExist)
|
||||
err = os.WriteFile(p, content, 0644)
|
||||
if err != nil {
|
||||
panelHandleErr(w, r, NewNestedError("unable to write config file").With(err))
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if !exists {
|
||||
w.Write([]byte(fmt.Sprintf("Config file %s created, remember to add it to config.yml!", p)))
|
||||
return
|
||||
}
|
||||
w.Write([]byte(fmt.Sprintf("Config file %s updated", p)))
|
||||
}
|
||||
|
||||
func panelServeFile(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, path.Join(templatesBasePath, r.URL.Path))
|
||||
}
|
||||
|
||||
func panelRenderFile(w http.ResponseWriter, r *http.Request, f string, data any) {
|
||||
tmpl, err := template.ParseFiles(f)
|
||||
if err != nil {
|
||||
panelHandleErr(w, r, NewNestedError("unable to parse template").With(err))
|
||||
return
|
||||
}
|
||||
|
||||
err = tmpl.Execute(w, data)
|
||||
if err != nil {
|
||||
panelHandleErr(w, r, NewNestedError("unable to render template").With(err))
|
||||
}
|
||||
}
|
||||
|
||||
func configReload(w http.ResponseWriter, r *http.Request) {
|
||||
err := cfg.Reload()
|
||||
if err != nil {
|
||||
panelHandleErr(w, r, err)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func panelHandleErr(w http.ResponseWriter, r *http.Request, err error, code ...int) {
|
||||
err = NewNestedErrorFrom(err).Subjectf("%s %s", r.Method, r.URL)
|
||||
palog.Error(err)
|
||||
if len(code) > 0 {
|
||||
http.Error(w, err.Error(), code[0])
|
||||
return
|
||||
}
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -9,10 +8,8 @@ type pathPoolMap struct {
|
||||
SafeMap[string, *httpLoadBalancePool]
|
||||
}
|
||||
|
||||
func newPathPoolMap() *pathPoolMap {
|
||||
return &pathPoolMap{
|
||||
NewSafeMap[string](NewHTTPLoadBalancePool),
|
||||
}
|
||||
func newPathPoolMap() pathPoolMap {
|
||||
return pathPoolMap{NewSafeMapOf[pathPoolMap](NewHTTPLoadBalancePool)}
|
||||
}
|
||||
|
||||
func (m pathPoolMap) Add(path string, route *HTTPRoute) {
|
||||
@@ -20,11 +17,11 @@ func (m pathPoolMap) Add(path string, route *HTTPRoute) {
|
||||
m.Get(path).Add(route)
|
||||
}
|
||||
|
||||
func (m pathPoolMap) FindMatch(pathGot string) (*HTTPRoute, error) {
|
||||
func (m pathPoolMap) FindMatch(pathGot string) (*HTTPRoute, NestedErrorLike) {
|
||||
for pathWant, v := range m.Iterator() {
|
||||
if strings.HasPrefix(pathGot, pathWant) {
|
||||
return v.Pick(), nil
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("no matching route for path %s", pathGot)
|
||||
return nil, NewNestedError("no matching path").Subject(pathGot)
|
||||
}
|
||||
|
||||
@@ -1,31 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type Provider struct {
|
||||
Kind string // docker, file
|
||||
Value string
|
||||
Kind string `json:"kind"` // docker, file
|
||||
Value string `json:"value"`
|
||||
|
||||
watcher Watcher
|
||||
routes map[string]Route // id -> Route
|
||||
mutex sync.Mutex
|
||||
l logrus.FieldLogger
|
||||
watcher Watcher
|
||||
routes map[string]Route // id -> Route
|
||||
l logrus.FieldLogger
|
||||
reloadReqCh chan struct{}
|
||||
}
|
||||
|
||||
// Init is called after LoadProxyConfig
|
||||
func (p *Provider) Init(name string) error {
|
||||
p.l = prlog.WithFields(logrus.Fields{"kind": p.Kind, "name": name})
|
||||
p.l = logrus.WithField("provider", name)
|
||||
p.reloadReqCh = make(chan struct{}, 1)
|
||||
|
||||
defer p.initWatcher()
|
||||
|
||||
if err := p.loadProxyConfig(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.initWatcher()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -37,34 +36,41 @@ func (p *Provider) StartAllRoutes() {
|
||||
func (p *Provider) StopAllRoutes() {
|
||||
p.watcher.Stop()
|
||||
ParallelForEachValue(p.routes, Route.Stop)
|
||||
p.routes = make(map[string]Route)
|
||||
p.routes = nil
|
||||
}
|
||||
|
||||
func (p *Provider) ReloadRoutes() {
|
||||
p.mutex.Lock()
|
||||
defer p.mutex.Unlock()
|
||||
select {
|
||||
case p.reloadReqCh <- struct{}{}:
|
||||
defer func() {
|
||||
<-p.reloadReqCh
|
||||
}()
|
||||
|
||||
p.StopAllRoutes()
|
||||
err := p.loadProxyConfig()
|
||||
if err != nil {
|
||||
p.l.Error("failed to reload routes: ", err)
|
||||
p.StopAllRoutes()
|
||||
err := p.loadProxyConfig()
|
||||
if err != nil {
|
||||
p.l.Error("failed to reload routes: ", err)
|
||||
return
|
||||
}
|
||||
p.StartAllRoutes()
|
||||
default:
|
||||
p.l.Info("reload request already in progress")
|
||||
return
|
||||
}
|
||||
p.StartAllRoutes()
|
||||
}
|
||||
|
||||
func (p *Provider) loadProxyConfig() error {
|
||||
var cfgs []*ProxyConfig
|
||||
var cfgs ProxyConfigSlice
|
||||
var err error
|
||||
|
||||
switch p.Kind {
|
||||
case ProviderKind_Docker:
|
||||
cfgs, err = p.getDockerProxyConfigs()
|
||||
case ProviderKind_File:
|
||||
cfgs, err = p.getFileProxyConfigs()
|
||||
cfgs, err = p.ValidateFile()
|
||||
default:
|
||||
// this line should never be reached
|
||||
return fmt.Errorf("unknown provider kind")
|
||||
return NewNestedError("unknown provider kind")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
@@ -73,29 +79,34 @@ func (p *Provider) loadProxyConfig() error {
|
||||
p.l.Infof("loaded %d proxy configurations", len(cfgs))
|
||||
|
||||
p.routes = make(map[string]Route, len(cfgs))
|
||||
pErrs := NewNestedError("failed to create these routes")
|
||||
|
||||
for _, cfg := range cfgs {
|
||||
r, err := NewRoute(cfg)
|
||||
r, err := NewRoute(&cfg)
|
||||
if err != nil {
|
||||
p.l.Errorf("error creating route %s: %v", cfg.Alias, err)
|
||||
pErrs.ExtraError(NewNestedErrorFrom(err).Subject(cfg.Alias))
|
||||
continue
|
||||
}
|
||||
p.routes[cfg.GetID()] = r
|
||||
}
|
||||
|
||||
if pErrs.HasExtras() {
|
||||
p.routes = nil
|
||||
return pErrs
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Provider) initWatcher() error {
|
||||
switch p.Kind {
|
||||
case ProviderKind_Docker:
|
||||
var err error
|
||||
dockerClient, err := p.getDockerClient()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to create docker client: %v", err)
|
||||
return NewNestedError("unable to create docker client").With(err)
|
||||
}
|
||||
p.watcher = NewDockerWatcher(dockerClient, p.ReloadRoutes)
|
||||
p.watcher = p.NewDockerWatcher(dockerClient)
|
||||
case ProviderKind_File:
|
||||
p.watcher = NewFileWatcher(p.Value, p.ReloadRoutes, p.StopAllRoutes)
|
||||
p.watcher = p.NewFileWatcher()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,43 +1,55 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ProxyConfig struct {
|
||||
Alias string
|
||||
Scheme string
|
||||
Host string
|
||||
Port string
|
||||
LoadBalance string // docker provider only
|
||||
NoTLSVerify bool // http proxy only
|
||||
Path string // http proxy only
|
||||
PathMode string `yaml:"path_mode"` // http proxy only
|
||||
|
||||
provider *Provider
|
||||
Alias string `yaml:"-" json:"-"`
|
||||
Scheme string `yaml:"scheme" json:"scheme"`
|
||||
Host string `yaml:"host" json:"host"`
|
||||
Port string `yaml:"port" json:"port"`
|
||||
LoadBalance string `yaml:"-" json:"-"` // docker provider only
|
||||
NoTLSVerify bool `yaml:"no_tls_verify" json:"no_tls_verify"` // http proxy only
|
||||
Path string `yaml:"path" json:"path"` // http proxy only
|
||||
PathMode string `yaml:"path_mode" json:"path_mode"` // http proxy only
|
||||
SetHeaders http.Header `yaml:"set_headers" json:"set_headers"` // http proxy only
|
||||
HideHeaders []string `yaml:"hide_headers" json:"hide_headers"` // http proxy only
|
||||
}
|
||||
|
||||
func NewProxyConfig(provider *Provider) ProxyConfig {
|
||||
return ProxyConfig{
|
||||
provider: provider,
|
||||
}
|
||||
}
|
||||
type ProxyConfigMap map[string]ProxyConfig
|
||||
type ProxyConfigSlice []ProxyConfig
|
||||
|
||||
// used by `GetFileProxyConfigs`
|
||||
func (cfg *ProxyConfig) SetDefaults() error {
|
||||
err := NewNestedError("invalid proxy config").Subject(cfg.Alias)
|
||||
|
||||
if cfg.Alias == "" {
|
||||
return fmt.Errorf("alias is required")
|
||||
err.Extra("alias is required")
|
||||
}
|
||||
if cfg.Scheme == "" {
|
||||
cfg.Scheme = "http"
|
||||
}
|
||||
if cfg.Host == "" {
|
||||
return fmt.Errorf("host is required for %q", cfg.Alias)
|
||||
err.Extra("host is required")
|
||||
}
|
||||
if cfg.Port == "" {
|
||||
cfg.Port = "80"
|
||||
switch cfg.Scheme {
|
||||
case "http":
|
||||
cfg.Port = "80"
|
||||
case "https":
|
||||
cfg.Port = "443"
|
||||
default:
|
||||
err.Extraf("port is required for %s scheme", cfg.Scheme)
|
||||
}
|
||||
}
|
||||
if err.HasExtras() {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cfg *ProxyConfig) GetID() string {
|
||||
return fmt.Sprintf("%s-%s-%s-%s-%s", cfg.Alias, cfg.Scheme, cfg.Host, cfg.Port, cfg.Path)
|
||||
}
|
||||
}
|
||||
92
src/go-proxy/proxy_label.go
Normal file
92
src/go-proxy/proxy_label.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ProxyLabel struct {
|
||||
Alias string
|
||||
Field string
|
||||
Value any
|
||||
}
|
||||
|
||||
var errNotProxyLabel = errors.New("not a proxy label")
|
||||
var errInvalidSetHeaderLine = errors.New("invalid set header line")
|
||||
var errInvalidBoolean = errors.New("invalid boolean")
|
||||
|
||||
const proxyLabelNamespace = "proxy"
|
||||
|
||||
func parseProxyLabel(label string, value string) (*ProxyLabel, error) {
|
||||
ns := strings.Split(label, ".")
|
||||
var v any = value
|
||||
|
||||
if len(ns) != 3 {
|
||||
return nil, errNotProxyLabel
|
||||
}
|
||||
|
||||
if ns[0] != proxyLabelNamespace {
|
||||
return nil, errNotProxyLabel
|
||||
}
|
||||
|
||||
field := ns[2]
|
||||
|
||||
var err error
|
||||
parser, ok := valueParser[field]
|
||||
|
||||
if ok {
|
||||
v, err = parser(v.(string))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &ProxyLabel{
|
||||
Alias: ns[1],
|
||||
Field: field,
|
||||
Value: v,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func setHeadersParser(value string) (any, error) {
|
||||
value = strings.TrimSpace(value)
|
||||
lines := strings.Split(value, "\n")
|
||||
h := make(http.Header)
|
||||
for _, line := range lines {
|
||||
parts := strings.SplitN(line, ":", 2)
|
||||
if len(parts) != 2 {
|
||||
return nil, fmt.Errorf("%w: %q", errInvalidSetHeaderLine, line)
|
||||
}
|
||||
key := strings.TrimSpace(parts[0])
|
||||
val := strings.TrimSpace(parts[1])
|
||||
h.Add(key, val)
|
||||
}
|
||||
return h, nil
|
||||
}
|
||||
|
||||
func commaSepParser(value string) (any, error) {
|
||||
v := strings.Split(value, ",")
|
||||
for i := range v {
|
||||
v[i] = strings.TrimSpace(v[i])
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func boolParser(value string) (any, error) {
|
||||
switch strings.ToLower(value) {
|
||||
case "true", "yes", "1":
|
||||
return true, nil
|
||||
case "false", "no", "0":
|
||||
return false, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("%w: %q", errInvalidBoolean, value)
|
||||
}
|
||||
}
|
||||
|
||||
var valueParser = map[string]func(string) (any, error){
|
||||
"set_headers": setHeadersParser,
|
||||
"hide_headers": commaSepParser,
|
||||
"no_tls_verify": boolParser,
|
||||
}
|
||||
186
src/go-proxy/proxy_label_test.go
Normal file
186
src/go-proxy/proxy_label_test.go
Normal file
@@ -0,0 +1,186 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func makeLabel(alias string, field string) string {
|
||||
return fmt.Sprintf("proxy.%s.%s", alias, field)
|
||||
}
|
||||
|
||||
func TestNotProxyLabel(t *testing.T) {
|
||||
pl, err := parseProxyLabel("foo.bar", "1234")
|
||||
if !errors.Is(err, errNotProxyLabel) {
|
||||
t.Errorf("expected err NotProxyLabel, got %v", err)
|
||||
}
|
||||
if pl != nil {
|
||||
t.Errorf("expected nil, got %v", pl)
|
||||
}
|
||||
_, err = parseProxyLabel("proxy.foo", "bar")
|
||||
if !errors.Is(err, errNotProxyLabel) {
|
||||
t.Errorf("expected err InvalidProxyLabel, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringProxyLabel(t *testing.T) {
|
||||
alias := "foo"
|
||||
field := "ip"
|
||||
v := "bar"
|
||||
pl, err := parseProxyLabel(makeLabel(alias, field), v)
|
||||
if err != nil {
|
||||
t.Errorf("expected err=nil, got %v", err)
|
||||
}
|
||||
if pl.Alias != alias {
|
||||
t.Errorf("expected alias=%s, got %s", alias, pl.Alias)
|
||||
}
|
||||
if pl.Field != field {
|
||||
t.Errorf("expected field=%s, got %s", field, pl.Field)
|
||||
}
|
||||
if pl.Value != v {
|
||||
t.Errorf("expected value=%q, got %s", v, pl.Value)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBoolProxyLabelValid(t *testing.T) {
|
||||
alias := "foo"
|
||||
field := "no_tls_verify"
|
||||
tests := map[string]bool{
|
||||
"true": true,
|
||||
"TRUE": true,
|
||||
"yes": true,
|
||||
"1": true,
|
||||
"false": false,
|
||||
"FALSE": false,
|
||||
"no": false,
|
||||
"0": false,
|
||||
}
|
||||
|
||||
for k, v := range tests {
|
||||
pl, err := parseProxyLabel(makeLabel(alias, field), k)
|
||||
if err != nil {
|
||||
t.Errorf("expected err=nil, got %v", err)
|
||||
}
|
||||
if pl.Alias != alias {
|
||||
t.Errorf("expected alias=%s, got %s", alias, pl.Alias)
|
||||
}
|
||||
if pl.Field != field {
|
||||
t.Errorf("expected field=%s, got %s", field, pl.Field)
|
||||
}
|
||||
if pl.Value != v {
|
||||
t.Errorf("expected value=%v, got %v", v, pl.Value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBoolProxyLabelInvalid(t *testing.T) {
|
||||
alias := "foo"
|
||||
field := "no_tls_verify"
|
||||
_, err := parseProxyLabel(makeLabel(alias, field), "invalid")
|
||||
if !errors.Is(err, errInvalidBoolean) {
|
||||
t.Errorf("expected err InvalidProxyLabel, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHeaderProxyLabelValid(t *testing.T) {
|
||||
alias := "foo"
|
||||
field := "set_headers"
|
||||
v := `
|
||||
X-Custom-Header1: foo
|
||||
X-Custom-Header1: bar
|
||||
X-Custom-Header2: baz
|
||||
`
|
||||
h := make(http.Header, 0)
|
||||
h.Set("X-Custom-Header1", "foo")
|
||||
h.Add("X-Custom-Header1", "bar")
|
||||
h.Set("X-Custom-Header2", "baz")
|
||||
|
||||
pl, err := parseProxyLabel(makeLabel(alias, field), v)
|
||||
if err != nil {
|
||||
t.Errorf("expected err=nil, got %v", err)
|
||||
}
|
||||
if pl.Alias != alias {
|
||||
t.Errorf("expected alias=%s, got %s", alias, pl.Alias)
|
||||
}
|
||||
if pl.Field != field {
|
||||
t.Errorf("expected field=%s, got %s", field, pl.Field)
|
||||
}
|
||||
hGot, ok := pl.Value.(http.Header)
|
||||
if !ok {
|
||||
t.Error("value is not http.Header")
|
||||
return
|
||||
}
|
||||
for k, vWant := range h {
|
||||
vGot := hGot[k]
|
||||
if !reflect.DeepEqual(vGot, vWant) {
|
||||
t.Errorf("expected %s=%q, got %q", k, vWant, vGot)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHeaderProxyLabelInvalid(t *testing.T) {
|
||||
alias := "foo"
|
||||
field := "set_headers"
|
||||
tests := []string{
|
||||
"X-Custom-Header1 = bar",
|
||||
"X-Custom-Header1",
|
||||
}
|
||||
|
||||
for _, v := range tests {
|
||||
_, err := parseProxyLabel(makeLabel(alias, field), v)
|
||||
if !errors.Is(err, errInvalidSetHeaderLine) {
|
||||
t.Errorf("expected err InvalidProxyLabel for %q, got %v", v, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommaSepProxyLabelSingle(t *testing.T) {
|
||||
alias := "foo"
|
||||
field := "hide_headers"
|
||||
v := "X-Custom-Header1"
|
||||
pl, err := parseProxyLabel(makeLabel(alias, field), v)
|
||||
if err != nil {
|
||||
t.Errorf("expected err=nil, got %v", err)
|
||||
}
|
||||
if pl.Alias != alias {
|
||||
t.Errorf("expected alias=%s, got %s", alias, pl.Alias)
|
||||
}
|
||||
if pl.Field != field {
|
||||
t.Errorf("expected field=%s, got %s", field, pl.Field)
|
||||
}
|
||||
sGot, ok := pl.Value.([]string)
|
||||
sWant := []string{"X-Custom-Header1"}
|
||||
if !ok {
|
||||
t.Error("value is not []string")
|
||||
}
|
||||
if !reflect.DeepEqual(sGot, sWant) {
|
||||
t.Errorf("expected %q, got %q", sWant, sGot)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommaSepProxyLabelMulti(t *testing.T) {
|
||||
alias := "foo"
|
||||
field := "hide_headers"
|
||||
v := "X-Custom-Header1, X-Custom-Header2,X-Custom-Header3"
|
||||
pl, err := parseProxyLabel(makeLabel(alias, field), v)
|
||||
if err != nil {
|
||||
t.Errorf("expected err=nil, got %v", err)
|
||||
}
|
||||
if pl.Alias != alias {
|
||||
t.Errorf("expected alias=%s, got %s", alias, pl.Alias)
|
||||
}
|
||||
if pl.Field != field {
|
||||
t.Errorf("expected field=%s, got %s", field, pl.Field)
|
||||
}
|
||||
sGot, ok := pl.Value.([]string)
|
||||
sWant := []string{"X-Custom-Header1", "X-Custom-Header2", "X-Custom-Header3"}
|
||||
if !ok {
|
||||
t.Error("value is not []string")
|
||||
}
|
||||
if !reflect.DeepEqual(sGot, sWant) {
|
||||
t.Errorf("expected %q, got %q", sWant, sGot)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package main
|
||||
|
||||
// A small mod on net/http/httputils
|
||||
// A small mod on net/http/httputil/reverseproxy.go
|
||||
// that doubled the performance
|
||||
|
||||
import (
|
||||
@@ -8,14 +8,12 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptrace"
|
||||
"net/textproto"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/http/httpguts"
|
||||
)
|
||||
@@ -39,16 +37,16 @@ type ProxyRequest struct {
|
||||
//
|
||||
// SetURL rewrites the outbound Host header to match the target's host.
|
||||
// To preserve the inbound request's Host header (the default behavior
|
||||
// of [NewSingleHostReverseProxy]):
|
||||
// of [NewReverseProxy]):
|
||||
//
|
||||
// rewriteFunc := func(r *httputil.ProxyRequest) {
|
||||
// r.SetURL(url)
|
||||
// r.Out.Host = r.In.Host
|
||||
// }
|
||||
func (r *ProxyRequest) SetURL(target *url.URL) {
|
||||
rewriteRequestURL(r.Out, target)
|
||||
r.Out.Host = ""
|
||||
}
|
||||
// func (r *ProxyRequest) SetURL(target *url.URL) {
|
||||
// rewriteRequestURL(r.Out, target)
|
||||
// r.Out.Host = ""
|
||||
// }
|
||||
|
||||
// SetXForwarded sets the X-Forwarded-For, X-Forwarded-Host, and
|
||||
// X-Forwarded-Proto headers of the outbound request.
|
||||
@@ -132,17 +130,17 @@ type ReverseProxy struct {
|
||||
// recognizes a response as a streaming response, or
|
||||
// if its ContentLength is -1; for such responses, writes
|
||||
// are flushed to the client immediately.
|
||||
FlushInterval time.Duration
|
||||
// FlushInterval time.Duration
|
||||
|
||||
// ErrorLog specifies an optional logger for errors
|
||||
// that occur when attempting to proxy the request.
|
||||
// If nil, logging is done via the log package's standard logger.
|
||||
ErrorLog *log.Logger
|
||||
// ErrorLog *log.Logger
|
||||
|
||||
// BufferPool optionally specifies a buffer pool to
|
||||
// get byte slices for use by io.CopyBuffer when
|
||||
// copying HTTP response bodies.
|
||||
BufferPool BufferPool
|
||||
// BufferPool BufferPool
|
||||
|
||||
// ModifyResponse is an optional function that modifies the
|
||||
// Response from the backend. It is called if the backend
|
||||
@@ -203,18 +201,18 @@ func joinURLPath(a, b *url.URL) (path, rawpath string) {
|
||||
return a.Path + b.Path, apath + bpath
|
||||
}
|
||||
|
||||
// NewSingleHostReverseProxy returns a new [ReverseProxy] that routes
|
||||
// NewReverseProxy returns a new [ReverseProxy] that routes
|
||||
// URLs to the scheme, host, and base path provided in target. If the
|
||||
// target's path is "/base" and the incoming request was for "/dir",
|
||||
// the target request will be for /base/dir.
|
||||
//
|
||||
// NewSingleHostReverseProxy does not rewrite the Host header.
|
||||
// NewReverseProxy does not rewrite the Host header.
|
||||
//
|
||||
// To customize the ReverseProxy behavior beyond what
|
||||
// NewSingleHostReverseProxy provides, use ReverseProxy directly
|
||||
// NewReverseProxy provides, use ReverseProxy directly
|
||||
// with a Rewrite function. The ProxyRequest SetURL method
|
||||
// may be used to route the outbound request. (Note that SetURL,
|
||||
// unlike NewSingleHostReverseProxy, rewrites the Host header
|
||||
// unlike NewReverseProxy, rewrites the Host header
|
||||
// of the outbound request by default.)
|
||||
//
|
||||
// proxy := &ReverseProxy{
|
||||
@@ -223,9 +221,34 @@ func joinURLPath(a, b *url.URL) (path, rawpath string) {
|
||||
// r.Out.Host = r.In.Host // if desired
|
||||
// },
|
||||
// }
|
||||
func NewSingleHostReverseProxy(target *url.URL, transport *http.Transport) *ReverseProxy {
|
||||
func NewReverseProxy(target *url.URL, transport *http.Transport, config *ProxyConfig) *ReverseProxy {
|
||||
// check on init rather than on request
|
||||
var setHeaders = func(r *http.Request) {}
|
||||
var hideHeaders = func(r *http.Request) {}
|
||||
if len(config.SetHeaders) > 0 {
|
||||
setHeaders = func(r *http.Request) {
|
||||
h := config.SetHeaders.Clone()
|
||||
for k, vv := range h {
|
||||
if k == "Host" {
|
||||
r.Host = vv[0]
|
||||
} else {
|
||||
r.Header[k] = vv
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(config.HideHeaders) > 0 {
|
||||
hideHeaders = func(r *http.Request) {
|
||||
for _, k := range config.HideHeaders {
|
||||
r.Header.Del(k)
|
||||
}
|
||||
}
|
||||
}
|
||||
return &ReverseProxy{Rewrite: func(pr *ProxyRequest) {
|
||||
rewriteRequestURL(pr.Out, target)
|
||||
pr.SetXForwarded()
|
||||
setHeaders(pr.Out)
|
||||
hideHeaders(pr.Out)
|
||||
}, Transport: transport}
|
||||
}
|
||||
|
||||
@@ -380,7 +403,7 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
// Strip client-provided forwarding headers.
|
||||
// The Rewrite func may use SetXForwarded to set new values
|
||||
// for these or copy the previous values from the inbound request.
|
||||
// outreq.Header.Del("Forwarded")
|
||||
outreq.Header.Del("Forwarded")
|
||||
// outreq.Header.Del("X-Forwarded-For")
|
||||
// outreq.Header.Del("X-Forwarded-Host")
|
||||
// outreq.Header.Del("X-Forwarded-Proto")
|
||||
@@ -388,29 +411,27 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
// NOTE: removed
|
||||
// Remove unparsable query parameters from the outbound request.
|
||||
// outreq.URL.RawQuery = cleanQueryParams(outreq.URL.RawQuery)
|
||||
|
||||
pr := &ProxyRequest{
|
||||
In: req,
|
||||
Out: outreq,
|
||||
}
|
||||
pr.SetXForwarded() // NOTE: added
|
||||
p.Rewrite(pr)
|
||||
outreq = pr.Out
|
||||
// NOTE: removed
|
||||
// } else {
|
||||
// if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
|
||||
// // If we aren't the first proxy retain prior
|
||||
// // X-Forwarded-For information as a comma+space
|
||||
// // separated list and fold multiple headers into one.
|
||||
// prior, ok := outreq.Header["X-Forwarded-For"]
|
||||
// omit := ok && prior == nil // Issue 38079: nil now means don't populate the header
|
||||
// if len(prior) > 0 {
|
||||
// clientIP = strings.Join(prior, ", ") + ", " + clientIP
|
||||
// }
|
||||
// if !omit {
|
||||
// outreq.Header.Set("X-Forwarded-For", clientIP)
|
||||
// }
|
||||
// if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
|
||||
// // If we aren't the first proxy retain prior
|
||||
// // X-Forwarded-For information as a comma+space
|
||||
// // separated list and fold multiple headers into one.
|
||||
// prior, ok := outreq.Header["X-Forwarded-For"]
|
||||
// omit := ok && prior == nil // Issue 38079: nil now means don't populate the header
|
||||
// if len(prior) > 0 {
|
||||
// clientIP = strings.Join(prior, ", ") + ", " + clientIP
|
||||
// }
|
||||
// if !omit {
|
||||
// outreq.Header.Set("X-Forwarded-For", clientIP)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
if _, ok := outreq.Header["User-Agent"]; !ok {
|
||||
@@ -474,7 +495,7 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
|
||||
rw.WriteHeader(res.StatusCode)
|
||||
|
||||
// NOTE: changing this line extremely improve throughput
|
||||
// NOTE: changing this line extremely improve throughput
|
||||
// err = p.copyResponse(rw, res.Body, p.flushInterval(res))
|
||||
_, err = io.Copy(rw, res.Body)
|
||||
if err != nil {
|
||||
@@ -637,11 +658,11 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
// }
|
||||
|
||||
func (p *ReverseProxy) logf(format string, args ...any) {
|
||||
if p.ErrorLog != nil {
|
||||
p.ErrorLog.Printf(format, args...)
|
||||
} else {
|
||||
hrlog.Printf(format, args...)
|
||||
}
|
||||
// if p.ErrorLog != nil {
|
||||
// p.ErrorLog.Printf(format, args...)
|
||||
// } else {
|
||||
hrlog.Errorf(format, args...)
|
||||
// }
|
||||
}
|
||||
|
||||
// NOTE: removed
|
||||
102
src/go-proxy/reverse_proxy_mod_test.go
Normal file
102
src/go-proxy/reverse_proxy_mod_test.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var proxyCfg ProxyConfig
|
||||
var proxyUrl, _ = url.Parse("http://127.0.0.1:8181")
|
||||
var proxyServer = NewServer(ServerOptions{
|
||||
Name: "proxy",
|
||||
HTTPAddr: ":8080",
|
||||
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
NewReverseProxy(proxyUrl, &http.Transport{}, &proxyCfg).ServeHTTP(w, r)
|
||||
}),
|
||||
})
|
||||
|
||||
var testServer = NewServer(ServerOptions{
|
||||
Name: "test",
|
||||
HTTPAddr: ":8181",
|
||||
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
h := r.Header
|
||||
for k, vv := range h {
|
||||
for _, v := range vv {
|
||||
w.Header().Add(k, v)
|
||||
}
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}),
|
||||
})
|
||||
|
||||
var httpClient = http.DefaultClient
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
proxyServer.Start()
|
||||
testServer.Start()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
code := m.Run()
|
||||
proxyServer.Stop()
|
||||
testServer.Stop()
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
func TestSetHeader(t *testing.T) {
|
||||
hWant := http.Header{"X-Test": []string{"foo", "bar"}, "X-Test2": []string{"baz"}}
|
||||
proxyCfg = ProxyConfig{
|
||||
Alias: "test",
|
||||
Scheme: "http",
|
||||
Host: "127.0.0.1",
|
||||
Port: "8181",
|
||||
SetHeaders: hWant,
|
||||
}
|
||||
req, err := http.NewRequest("HEAD", "http://127.0.0.1:8080", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
hGot := resp.Header
|
||||
t.Log("headers: ", hGot)
|
||||
for k, v := range hWant {
|
||||
if !reflect.DeepEqual(hGot[k], v) {
|
||||
t.Errorf("header %s: expected %v, got %v", k, v, hGot[k])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHideHeader(t *testing.T) {
|
||||
hHide := []string{"X-Test", "X-Test2"}
|
||||
proxyCfg = ProxyConfig{
|
||||
Alias: "test",
|
||||
Scheme: "http",
|
||||
Host: "127.0.0.1",
|
||||
Port: "8181",
|
||||
HideHeaders: hHide,
|
||||
}
|
||||
req, err := http.NewRequest("HEAD", "http://127.0.0.1:8080", nil)
|
||||
for _, k := range hHide {
|
||||
req.Header.Set(k, "foo")
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
hGot := resp.Header
|
||||
t.Log("headers: ", hGot)
|
||||
for _, v := range hHide {
|
||||
_, ok := hGot[v]
|
||||
if ok {
|
||||
t.Errorf("header %s: expected hidden, got %v", v, hGot[v])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,5 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Route interface {
|
||||
Start()
|
||||
Stop()
|
||||
@@ -13,21 +9,19 @@ func NewRoute(cfg *ProxyConfig) (Route, error) {
|
||||
if isStreamScheme(cfg.Scheme) {
|
||||
id := cfg.GetID()
|
||||
if streamRoutes.Contains(id) {
|
||||
return nil, fmt.Errorf("duplicated %s stream %s, ignoring", cfg.Scheme, id)
|
||||
return nil, NewNestedError("duplicated stream").Subject(cfg.Alias)
|
||||
}
|
||||
route, err := NewStreamRoute(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, NewNestedErrorFrom(err).Subject(cfg.Alias)
|
||||
}
|
||||
streamRoutes.Set(id, route)
|
||||
return route, nil
|
||||
} else {
|
||||
httpRoutes.Ensure(cfg.Alias)
|
||||
route, err := NewHTTPRoute(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, NewNestedErrorFrom(err).Subject(cfg.Alias)
|
||||
}
|
||||
httpRoutes.Get(cfg.Alias).Add(cfg.Path, route)
|
||||
return route, nil
|
||||
}
|
||||
}
|
||||
@@ -49,8 +43,3 @@ func isStreamScheme(s string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// id -> target
|
||||
type StreamRoutes = SafeMap[string, StreamRoute]
|
||||
|
||||
var streamRoutes = NewSafeMap[string, StreamRoute]()
|
||||
|
||||
140
src/go-proxy/server.go
Normal file
140
src/go-proxy/server.go
Normal file
@@ -0,0 +1,140 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
Name string
|
||||
KeyFile string
|
||||
CertFile string
|
||||
CertProvider AutoCertProvider
|
||||
http *http.Server
|
||||
https *http.Server
|
||||
httpStarted bool
|
||||
httpsStarted bool
|
||||
}
|
||||
|
||||
type ServerOptions struct {
|
||||
Name string
|
||||
HTTPAddr string
|
||||
HTTPSAddr string
|
||||
CertProvider AutoCertProvider
|
||||
RedirectToHTTPS bool
|
||||
Handler http.Handler
|
||||
}
|
||||
|
||||
type LogrusWrapper struct {
|
||||
*logrus.Entry
|
||||
}
|
||||
|
||||
func (l LogrusWrapper) Write(b []byte) (int, error) {
|
||||
return l.Logger.WriterLevel(logrus.ErrorLevel).Write(b)
|
||||
}
|
||||
|
||||
func NewServer(opt ServerOptions) *Server {
|
||||
var httpHandler http.Handler
|
||||
var s *Server
|
||||
if opt.RedirectToHTTPS {
|
||||
httpHandler = http.HandlerFunc(redirectToTLSHandler)
|
||||
} else {
|
||||
httpHandler = opt.Handler
|
||||
}
|
||||
logger := log.Default()
|
||||
logger.SetOutput(LogrusWrapper{
|
||||
logrus.WithFields(logrus.Fields{"component": "server", "name": opt.Name}),
|
||||
})
|
||||
if opt.CertProvider != nil {
|
||||
s = &Server{
|
||||
Name: opt.Name,
|
||||
CertProvider: opt.CertProvider,
|
||||
http: &http.Server{
|
||||
Addr: opt.HTTPAddr,
|
||||
Handler: httpHandler,
|
||||
ErrorLog: logger,
|
||||
},
|
||||
https: &http.Server{
|
||||
Addr: opt.HTTPSAddr,
|
||||
Handler: opt.Handler,
|
||||
ErrorLog: logger,
|
||||
TLSConfig: &tls.Config{
|
||||
GetCertificate: opt.CertProvider.GetCert,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
s = &Server{
|
||||
Name: opt.Name,
|
||||
KeyFile: keyFileDefault,
|
||||
CertFile: certFileDefault,
|
||||
http: &http.Server{
|
||||
Addr: opt.HTTPAddr,
|
||||
Handler: httpHandler,
|
||||
ErrorLog: logger,
|
||||
},
|
||||
https: &http.Server{
|
||||
Addr: opt.HTTPSAddr,
|
||||
Handler: opt.Handler,
|
||||
ErrorLog: logger,
|
||||
},
|
||||
}
|
||||
if !s.certsOK() {
|
||||
s.http.Handler = opt.Handler
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Server) Start() {
|
||||
if s.http != nil {
|
||||
s.httpStarted = true
|
||||
logrus.Printf("starting http %s server on %s", s.Name, s.http.Addr)
|
||||
go func() {
|
||||
err := s.http.ListenAndServe()
|
||||
s.handleErr("http", err)
|
||||
}()
|
||||
}
|
||||
|
||||
if s.https != nil && (s.CertProvider != nil || s.certsOK()) {
|
||||
s.httpsStarted = true
|
||||
logrus.Printf("starting https %s server on %s", s.Name, s.https.Addr)
|
||||
go func() {
|
||||
err := s.https.ListenAndServeTLS(s.CertFile, s.KeyFile)
|
||||
s.handleErr("https", err)
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) Stop() {
|
||||
ctx, _ := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
|
||||
if s.httpStarted {
|
||||
errHTTP := s.http.Shutdown(ctx)
|
||||
s.handleErr("http", errHTTP)
|
||||
s.httpStarted = false
|
||||
}
|
||||
|
||||
if s.httpsStarted {
|
||||
errHTTPS := s.https.Shutdown(ctx)
|
||||
s.handleErr("https", errHTTPS)
|
||||
s.httpsStarted = false
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) handleErr(scheme string, err error) {
|
||||
switch err {
|
||||
case nil, http.ErrServerClosed:
|
||||
return
|
||||
default:
|
||||
logrus.Fatalf("failed to start %s %s server: %v", scheme, s.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) certsOK() bool {
|
||||
return utils.fileOK(s.CertFile) && utils.fileOK(s.KeyFile)
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -11,16 +10,18 @@ import (
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type StreamImpl interface {
|
||||
Setup() error
|
||||
Accept() (interface{}, error)
|
||||
Handle(interface{}) error
|
||||
CloseListeners()
|
||||
}
|
||||
|
||||
type StreamRoute interface {
|
||||
Route
|
||||
ListeningUrl() string
|
||||
TargetUrl() string
|
||||
Logger() logrus.FieldLogger
|
||||
|
||||
closeListeners()
|
||||
closeChannel()
|
||||
unmarkPort()
|
||||
wait()
|
||||
}
|
||||
|
||||
type StreamRouteBase struct {
|
||||
@@ -32,59 +33,68 @@ type StreamRouteBase struct {
|
||||
TargetHost string
|
||||
TargetPort int
|
||||
|
||||
id string
|
||||
wg sync.WaitGroup
|
||||
stopChann chan struct{}
|
||||
l logrus.FieldLogger
|
||||
id string
|
||||
wg sync.WaitGroup
|
||||
stopCh chan struct{}
|
||||
connCh chan interface{}
|
||||
started bool
|
||||
l logrus.FieldLogger
|
||||
|
||||
StreamImpl
|
||||
}
|
||||
|
||||
func newStreamRouteBase(config *ProxyConfig) (*StreamRouteBase, error) {
|
||||
var streamType string = StreamType_TCP
|
||||
var srcPort string
|
||||
var dstPort string
|
||||
var srcScheme string
|
||||
var dstScheme string
|
||||
var srcPort, dstPort string
|
||||
var srcScheme, dstScheme string
|
||||
|
||||
port_split := strings.Split(config.Port, ":")
|
||||
if len(port_split) != 2 {
|
||||
cfgl.Warnf("invalid port %s, assuming it is target port", config.Port)
|
||||
srcPort = "0"
|
||||
l := srlog.WithFields(logrus.Fields{
|
||||
"alias": config.Alias,
|
||||
})
|
||||
portSplit := strings.Split(config.Port, ":")
|
||||
if len(portSplit) != 2 {
|
||||
l.Warnf(
|
||||
`%s: invalid port %s,
|
||||
assuming it is target port`,
|
||||
config.Alias,
|
||||
config.Port,
|
||||
)
|
||||
srcPort = "0" // will assign later
|
||||
dstPort = config.Port
|
||||
} else {
|
||||
srcPort = port_split[0]
|
||||
dstPort = port_split[1]
|
||||
srcPort = portSplit[0]
|
||||
dstPort = portSplit[1]
|
||||
}
|
||||
|
||||
if port, hasName := NamePortMap[dstPort]; hasName {
|
||||
if port, hasName := NamePortMapTCP[dstPort]; hasName {
|
||||
dstPort = port
|
||||
}
|
||||
|
||||
srcPortInt, err := strconv.Atoi(srcPort)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"invalid stream source port %s, ignoring", srcPort,
|
||||
)
|
||||
return nil, NewNestedError("invalid stream source port").Subject(srcPort)
|
||||
}
|
||||
|
||||
utils.markPortInUse(srcPortInt)
|
||||
|
||||
dstPortInt, err := strconv.Atoi(dstPort)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"invalid stream target port %s, ignoring", dstPort,
|
||||
)
|
||||
return nil, NewNestedError("invalid stream target port").Subject(dstPort)
|
||||
}
|
||||
|
||||
scheme_split := strings.Split(config.Scheme, ":")
|
||||
|
||||
if len(scheme_split) == 2 {
|
||||
srcScheme = scheme_split[0]
|
||||
dstScheme = scheme_split[1]
|
||||
schemeSplit := strings.Split(config.Scheme, ":")
|
||||
if len(schemeSplit) == 2 {
|
||||
srcScheme = schemeSplit[0]
|
||||
dstScheme = schemeSplit[1]
|
||||
} else {
|
||||
srcScheme = config.Scheme
|
||||
dstScheme = config.Scheme
|
||||
}
|
||||
|
||||
if srcScheme != dstScheme {
|
||||
return nil, NewNestedError("unsupported").Subjectf("%v -> %v", srcScheme, dstScheme)
|
||||
}
|
||||
|
||||
return &StreamRouteBase{
|
||||
Alias: config.Alias,
|
||||
Type: streamType,
|
||||
@@ -94,26 +104,29 @@ func newStreamRouteBase(config *ProxyConfig) (*StreamRouteBase, error) {
|
||||
TargetHost: config.Host,
|
||||
TargetPort: dstPortInt,
|
||||
|
||||
id: config.GetID(),
|
||||
wg: sync.WaitGroup{},
|
||||
stopChann: make(chan struct{}, 1),
|
||||
l: srlog.WithFields(logrus.Fields{
|
||||
"alias": config.Alias,
|
||||
"src": fmt.Sprintf("%s://:%d", srcScheme, srcPortInt),
|
||||
"dst": fmt.Sprintf("%s://%s:%d", dstScheme, config.Host, dstPortInt),
|
||||
}),
|
||||
id: config.GetID(),
|
||||
wg: sync.WaitGroup{},
|
||||
stopCh: make(chan struct{}, 1),
|
||||
connCh: make(chan interface{}),
|
||||
started: false,
|
||||
l: l,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func NewStreamRoute(config *ProxyConfig) (StreamRoute, error) {
|
||||
base, err := newStreamRouteBase(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch config.Scheme {
|
||||
case StreamType_TCP:
|
||||
return NewTCPRoute(config)
|
||||
base.StreamImpl = NewTCPRoute(base)
|
||||
case StreamType_UDP:
|
||||
return NewUDPRoute(config)
|
||||
base.StreamImpl = NewUDPRoute(base)
|
||||
default:
|
||||
return nil, errors.New("unknown stream type")
|
||||
return nil, NewNestedError("invalid stream type").Subject(config.Scheme)
|
||||
}
|
||||
return base, nil
|
||||
}
|
||||
|
||||
func (route *StreamRouteBase) ListeningUrl() string {
|
||||
@@ -128,7 +141,47 @@ func (route *StreamRouteBase) Logger() logrus.FieldLogger {
|
||||
return route.l
|
||||
}
|
||||
|
||||
func (route *StreamRouteBase) setupListen() {
|
||||
func (route *StreamRouteBase) Start() {
|
||||
route.wg.Wait()
|
||||
route.ensurePort()
|
||||
if err := route.Setup(); err != nil {
|
||||
route.l.Errorf("failed to setup: %v", err)
|
||||
return
|
||||
}
|
||||
route.started = true
|
||||
streamRoutes.Set(route.id, route)
|
||||
route.wg.Add(2)
|
||||
go route.grAcceptConnections()
|
||||
go route.grHandleConnections()
|
||||
}
|
||||
|
||||
func (route *StreamRouteBase) Stop() {
|
||||
if !route.started {
|
||||
return
|
||||
}
|
||||
l := route.Logger()
|
||||
l.Debug("stopping listening")
|
||||
close(route.stopCh)
|
||||
route.CloseListeners()
|
||||
|
||||
done := make(chan struct{}, 1)
|
||||
go func() {
|
||||
route.wg.Wait()
|
||||
close(done)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
l.Info("stopped listening")
|
||||
case <-time.After(streamStopListenTimeout):
|
||||
l.Error("timed out waiting for connections")
|
||||
}
|
||||
|
||||
utils.unmarkPortInUse(route.ListeningPort)
|
||||
streamRoutes.Delete(route.id)
|
||||
}
|
||||
|
||||
func (route *StreamRouteBase) ensurePort() {
|
||||
if route.ListeningPort == 0 {
|
||||
freePort, err := utils.findUseFreePort(20000)
|
||||
if err != nil {
|
||||
@@ -142,40 +195,48 @@ func (route *StreamRouteBase) setupListen() {
|
||||
route.l.Info("listening on ", route.ListeningUrl())
|
||||
}
|
||||
|
||||
func (route *StreamRouteBase) wait() {
|
||||
route.wg.Wait()
|
||||
}
|
||||
func (route *StreamRouteBase) grAcceptConnections() {
|
||||
defer route.wg.Done()
|
||||
|
||||
func (route *StreamRouteBase) closeChannel() {
|
||||
close(route.stopChann)
|
||||
}
|
||||
|
||||
func (route *StreamRouteBase) unmarkPort() {
|
||||
utils.unmarkPortInUse(route.ListeningPort)
|
||||
}
|
||||
|
||||
func stopListening(route StreamRoute) {
|
||||
l := route.Logger()
|
||||
l.Debug("stopping listening")
|
||||
|
||||
// close channel -> wait -> close listeners
|
||||
|
||||
route.closeChannel()
|
||||
|
||||
done := make(chan struct{})
|
||||
|
||||
go func() {
|
||||
route.wait()
|
||||
close(done)
|
||||
route.unmarkPort()
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
l.Info("stopped listening")
|
||||
case <-time.After(StreamStopListenTimeout):
|
||||
l.Error("timed out waiting for connections")
|
||||
for {
|
||||
select {
|
||||
case <-route.stopCh:
|
||||
return
|
||||
default:
|
||||
conn, err := route.Accept()
|
||||
if err != nil {
|
||||
select {
|
||||
case <-route.stopCh:
|
||||
return
|
||||
default:
|
||||
route.l.Error(err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
route.connCh <- conn
|
||||
}
|
||||
}
|
||||
|
||||
route.closeListeners()
|
||||
}
|
||||
|
||||
func (route *StreamRouteBase) grHandleConnections() {
|
||||
defer route.wg.Done()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-route.stopCh:
|
||||
return
|
||||
case conn := <-route.connCh:
|
||||
go func() {
|
||||
err := route.Handle(conn)
|
||||
if err != nil {
|
||||
route.l.Error(err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// id -> target
|
||||
type StreamRoutes SafeMap[string, StreamRoute]
|
||||
|
||||
var streamRoutes StreamRoutes = NewSafeMapOf[StreamRoutes]()
|
||||
|
||||
@@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -11,122 +10,74 @@ import (
|
||||
|
||||
const tcpDialTimeout = 5 * time.Second
|
||||
|
||||
type Pipes []*BidirectionalPipe
|
||||
|
||||
type TCPRoute struct {
|
||||
*StreamRouteBase
|
||||
listener net.Listener
|
||||
connChan chan net.Conn
|
||||
pipe Pipes
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func NewTCPRoute(config *ProxyConfig) (StreamRoute, error) {
|
||||
base, err := newStreamRouteBase(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if base.TargetScheme != StreamType_TCP {
|
||||
return nil, fmt.Errorf("tcp to %s not yet supported", base.TargetScheme)
|
||||
}
|
||||
func NewTCPRoute(base *StreamRouteBase) StreamImpl {
|
||||
return &TCPRoute{
|
||||
StreamRouteBase: base,
|
||||
listener: nil,
|
||||
connChan: make(chan net.Conn),
|
||||
}, nil
|
||||
pipe: make(Pipes, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (route *TCPRoute) Start() {
|
||||
route.setupListen()
|
||||
func (route *TCPRoute) Setup() error {
|
||||
in, err := net.Listen("tcp", fmt.Sprintf(":%v", route.ListeningPort))
|
||||
if err != nil {
|
||||
route.l.Error(err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
route.listener = in
|
||||
route.wg.Add(2)
|
||||
go route.grAcceptConnections()
|
||||
go route.grHandleConnections()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (route *TCPRoute) Stop() {
|
||||
stopListening(route)
|
||||
streamRoutes.Delete(route.id)
|
||||
func (route *TCPRoute) Accept() (interface{}, error) {
|
||||
return route.listener.Accept()
|
||||
}
|
||||
|
||||
func (route *TCPRoute) closeListeners() {
|
||||
if route.listener == nil {
|
||||
return
|
||||
}
|
||||
route.listener.Close()
|
||||
route.listener = nil
|
||||
}
|
||||
func (route *TCPRoute) Handle(c interface{}) error {
|
||||
clientConn := c.(net.Conn)
|
||||
|
||||
func (route *TCPRoute) grAcceptConnections() {
|
||||
defer route.wg.Done()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-route.stopChann:
|
||||
return
|
||||
default:
|
||||
conn, err := route.listener.Accept()
|
||||
if err != nil {
|
||||
route.l.Error(err)
|
||||
continue
|
||||
}
|
||||
route.connChan <- conn
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (route *TCPRoute) grHandleConnections() {
|
||||
defer route.wg.Done()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-route.stopChann:
|
||||
return
|
||||
case conn := <-route.connChan:
|
||||
route.wg.Add(1)
|
||||
go route.grHandleConnection(conn)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (route *TCPRoute) grHandleConnection(clientConn net.Conn) {
|
||||
defer clientConn.Close()
|
||||
defer route.wg.Done()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), tcpDialTimeout)
|
||||
defer cancel()
|
||||
|
||||
serverAddr := fmt.Sprintf("%s:%v", route.TargetHost, route.TargetPort)
|
||||
dialer := &net.Dialer{}
|
||||
|
||||
serverConn, err := dialer.DialContext(ctx, route.TargetScheme, serverAddr)
|
||||
if err != nil {
|
||||
route.l.WithField("stage", "dial").Infof("%v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
pipeCtx, pipeCancel := context.WithCancel(context.Background())
|
||||
go func() {
|
||||
<-route.stopCh
|
||||
pipeCancel()
|
||||
}()
|
||||
|
||||
route.mu.Lock()
|
||||
pipe := NewBidirectionalPipe(pipeCtx, clientConn, serverConn)
|
||||
route.pipe = append(route.pipe, pipe)
|
||||
route.mu.Unlock()
|
||||
return pipe.Start()
|
||||
}
|
||||
|
||||
func (route *TCPRoute) CloseListeners() {
|
||||
if route.listener == nil {
|
||||
return
|
||||
}
|
||||
route.tcpPipe(clientConn, serverConn)
|
||||
}
|
||||
|
||||
func (route *TCPRoute) tcpPipe(src net.Conn, dest net.Conn) {
|
||||
close := func() {
|
||||
src.Close()
|
||||
dest.Close()
|
||||
route.listener.Close()
|
||||
route.listener = nil
|
||||
for _, pipe := range route.pipe {
|
||||
if err := pipe.Stop(); err != nil {
|
||||
route.l.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(2) // Number of goroutines
|
||||
|
||||
go func() {
|
||||
_, err := io.Copy(src, dest)
|
||||
route.l.Error(err)
|
||||
close()
|
||||
wg.Done()
|
||||
}()
|
||||
go func() {
|
||||
_, err := io.Copy(dest, src)
|
||||
route.l.Error(err)
|
||||
close()
|
||||
wg.Done()
|
||||
}()
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
@@ -1,188 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"sync"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type UDPRoute struct {
|
||||
*StreamRouteBase
|
||||
|
||||
connMap map[net.Addr]net.Conn
|
||||
connMap UDPConnMap
|
||||
connMapMutex sync.Mutex
|
||||
|
||||
listeningConn *net.UDPConn
|
||||
targetConn *net.UDPConn
|
||||
|
||||
connChan chan *UDPConn
|
||||
targetAddr *net.UDPAddr
|
||||
}
|
||||
|
||||
type UDPConn struct {
|
||||
remoteAddr net.Addr
|
||||
buffer []byte
|
||||
bytesReceived []byte
|
||||
nReceived int
|
||||
src *net.UDPConn
|
||||
dst *net.UDPConn
|
||||
*BidirectionalPipe
|
||||
}
|
||||
|
||||
func NewUDPRoute(config *ProxyConfig) (StreamRoute, error) {
|
||||
base, err := newStreamRouteBase(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if base.TargetScheme != StreamType_UDP {
|
||||
return nil, fmt.Errorf("udp to %s not yet supported", base.TargetScheme)
|
||||
}
|
||||
type UDPConnMap map[string]*UDPConn
|
||||
|
||||
func NewUDPRoute(base *StreamRouteBase) StreamImpl {
|
||||
return &UDPRoute{
|
||||
StreamRouteBase: base,
|
||||
connMap: make(map[net.Addr]net.Conn),
|
||||
connChan: make(chan *UDPConn),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (route *UDPRoute) Start() {
|
||||
route.setupListen()
|
||||
|
||||
source, err := net.ListenPacket(route.ListeningScheme, fmt.Sprintf(":%v", route.ListeningPort))
|
||||
if err != nil {
|
||||
route.l.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
target, err := net.Dial(route.TargetScheme, fmt.Sprintf("%s:%v", route.TargetHost, route.TargetPort))
|
||||
if err != nil {
|
||||
route.l.Error(err)
|
||||
source.Close()
|
||||
return
|
||||
}
|
||||
|
||||
route.listeningConn = source.(*net.UDPConn)
|
||||
route.targetConn = target.(*net.UDPConn)
|
||||
|
||||
route.wg.Add(2)
|
||||
go route.grAcceptConnections()
|
||||
go route.grHandleConnections()
|
||||
}
|
||||
|
||||
func (route *UDPRoute) Stop() {
|
||||
stopListening(route)
|
||||
streamRoutes.Delete(route.id)
|
||||
}
|
||||
|
||||
func (route *UDPRoute) closeListeners() {
|
||||
if route.listeningConn != nil {
|
||||
route.listeningConn.Close()
|
||||
route.listeningConn = nil
|
||||
}
|
||||
if route.targetConn != nil {
|
||||
route.targetConn.Close()
|
||||
route.targetConn = nil
|
||||
}
|
||||
for _, conn := range route.connMap {
|
||||
conn.(*net.UDPConn).Close() // TODO: change on non udp target
|
||||
}
|
||||
route.connMap = make(map[net.Addr]net.Conn)
|
||||
}
|
||||
|
||||
func (route *UDPRoute) grAcceptConnections() {
|
||||
defer route.wg.Done()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-route.stopChann:
|
||||
return
|
||||
default:
|
||||
conn, err := route.accept()
|
||||
if err != nil {
|
||||
route.l.Error(err)
|
||||
continue
|
||||
}
|
||||
route.connChan <- conn
|
||||
}
|
||||
connMap: make(UDPConnMap),
|
||||
}
|
||||
}
|
||||
|
||||
func (route *UDPRoute) grHandleConnections() {
|
||||
defer route.wg.Done()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-route.stopChann:
|
||||
return
|
||||
case conn := <-route.connChan:
|
||||
go func() {
|
||||
err := route.handleConnection(conn)
|
||||
if err != nil {
|
||||
route.l.Error(err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (route *UDPRoute) handleConnection(conn *UDPConn) error {
|
||||
var err error
|
||||
|
||||
srcConn, ok := route.connMap[conn.remoteAddr]
|
||||
if !ok {
|
||||
route.connMapMutex.Lock()
|
||||
srcConn, err = net.DialUDP("udp", nil, conn.remoteAddr.(*net.UDPAddr))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
route.connMap[conn.remoteAddr] = srcConn
|
||||
route.connMapMutex.Unlock()
|
||||
}
|
||||
|
||||
var forwarder func(*UDPConn, net.Conn) error
|
||||
|
||||
if logLevel == logrus.DebugLevel {
|
||||
forwarder = route.forwardReceivedDebug
|
||||
} else {
|
||||
forwarder = route.forwardReceivedReal
|
||||
}
|
||||
|
||||
// initiate connection to target
|
||||
err = forwarder(conn, route.targetConn)
|
||||
func (route *UDPRoute) Setup() error {
|
||||
laddr, err := net.ResolveUDPAddr(route.ListeningScheme, fmt.Sprintf(":%v", route.ListeningPort))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-route.stopChann:
|
||||
return nil
|
||||
default:
|
||||
// receive from target
|
||||
conn, err = route.readFrom(route.targetConn, conn.buffer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// forward to source
|
||||
err = forwarder(conn, srcConn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// read from source
|
||||
conn, err = route.readFrom(srcConn, conn.buffer)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
// forward to target
|
||||
err = forwarder(conn, route.targetConn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
source, err := net.ListenUDP(route.ListeningScheme, laddr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
raddr, err := net.ResolveUDPAddr(route.TargetScheme, fmt.Sprintf("%s:%v", route.TargetHost, route.TargetPort))
|
||||
if err != nil {
|
||||
source.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
route.listeningConn = source
|
||||
route.targetAddr = raddr
|
||||
return nil
|
||||
}
|
||||
|
||||
func (route *UDPRoute) accept() (*UDPConn, error) {
|
||||
func (route *UDPRoute) Accept() (interface{}, error) {
|
||||
in := route.listeningConn
|
||||
|
||||
buffer := make([]byte, udpBufferSize)
|
||||
@@ -196,48 +67,65 @@ func (route *UDPRoute) accept() (*UDPConn, error) {
|
||||
return nil, io.ErrShortBuffer
|
||||
}
|
||||
|
||||
return &UDPConn{
|
||||
remoteAddr: srcAddr,
|
||||
buffer: buffer,
|
||||
bytesReceived: buffer[:nRead],
|
||||
nReceived: nRead},
|
||||
nil
|
||||
}
|
||||
key := srcAddr.String()
|
||||
conn, ok := route.connMap[key]
|
||||
|
||||
func (route *UDPRoute) readFrom(src net.Conn, buffer []byte) (*UDPConn, error) {
|
||||
nRead, err := src.Read(buffer)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if !ok {
|
||||
route.connMapMutex.Lock()
|
||||
if conn, ok = route.connMap[key]; !ok {
|
||||
srcConn, err := net.DialUDP("udp", nil, srcAddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dstConn, err := net.DialUDP("udp", nil, route.targetAddr)
|
||||
if err != nil {
|
||||
srcConn.Close()
|
||||
return nil, err
|
||||
}
|
||||
pipeCtx, pipeCancel := context.WithCancel(context.Background())
|
||||
go func() {
|
||||
<-route.stopCh
|
||||
pipeCancel()
|
||||
}()
|
||||
conn = &UDPConn{
|
||||
srcConn,
|
||||
dstConn,
|
||||
NewBidirectionalPipe(pipeCtx, sourceRWCloser{in, dstConn}, sourceRWCloser{in, srcConn}),
|
||||
}
|
||||
route.connMap[key] = conn
|
||||
}
|
||||
route.connMapMutex.Unlock()
|
||||
}
|
||||
|
||||
if nRead == 0 {
|
||||
return nil, io.ErrShortBuffer
|
||||
_, err = conn.dst.Write(buffer[:nRead])
|
||||
return conn, err
|
||||
}
|
||||
|
||||
func (route *UDPRoute) Handle(c interface{}) error {
|
||||
return c.(*UDPConn).Start()
|
||||
}
|
||||
|
||||
func (route *UDPRoute) CloseListeners() {
|
||||
if route.listeningConn != nil {
|
||||
route.listeningConn.Close()
|
||||
route.listeningConn = nil
|
||||
}
|
||||
|
||||
return &UDPConn{
|
||||
remoteAddr: src.RemoteAddr(),
|
||||
buffer: buffer,
|
||||
bytesReceived: buffer[:nRead],
|
||||
nReceived: nRead,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (route *UDPRoute) forwardReceivedReal(receivedConn *UDPConn, dest net.Conn) error {
|
||||
nWritten, err := dest.Write(receivedConn.bytesReceived)
|
||||
|
||||
if nWritten != receivedConn.nReceived {
|
||||
err = io.ErrShortWrite
|
||||
for _, conn := range route.connMap {
|
||||
if err := conn.src.Close(); err != nil {
|
||||
route.l.Errorf("error closing src conn: %w", err)
|
||||
}
|
||||
if err := conn.dst.Close(); err != nil {
|
||||
route.l.Error("error closing dst conn: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
route.connMap = make(UDPConnMap)
|
||||
}
|
||||
|
||||
func (route *UDPRoute) forwardReceivedDebug(receivedConn *UDPConn, dest net.Conn) error {
|
||||
route.l.WithField("size", receivedConn.nReceived).Debugf(
|
||||
"forwarding from %s to %s",
|
||||
receivedConn.remoteAddr.String(),
|
||||
dest.RemoteAddr().String(),
|
||||
)
|
||||
return route.forwardReceivedReal(receivedConn, dest)
|
||||
type sourceRWCloser struct {
|
||||
server *net.UDPConn
|
||||
*net.UDPConn
|
||||
}
|
||||
|
||||
func (w sourceRWCloser) Write(p []byte) (int, error) {
|
||||
return w.server.WriteToUDP(p, w.RemoteAddr().(*net.UDPAddr)) // TODO: support non udp
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
@@ -9,13 +11,15 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/santhosh-tekuri/jsonschema"
|
||||
"github.com/sirupsen/logrus"
|
||||
xhtml "golang.org/x/net/html"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type Utils struct {
|
||||
@@ -51,7 +55,7 @@ func (u *Utils) findUseFreePort(startingPort int) (int, error) {
|
||||
l.Close()
|
||||
return port, nil
|
||||
}
|
||||
return -1, fmt.Errorf("unable to find free port: %v", err)
|
||||
return -1, NewNestedError("unable to find free port").With(err)
|
||||
}
|
||||
|
||||
func (u *Utils) markPortInUse(port int) {
|
||||
@@ -83,7 +87,7 @@ func (*Utils) healthCheckHttp(targetUrl string) error {
|
||||
}
|
||||
|
||||
func (*Utils) healthCheckStream(scheme, host string) error {
|
||||
conn, err := net.DialTimeout(scheme, host, 5*time.Second)
|
||||
conn, err := net.DialTimeout(scheme, host, streamDialTimeout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -91,7 +95,19 @@ func (*Utils) healthCheckStream(scheme, host string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*Utils) snakeToCamel(s string) string {
|
||||
func (*Utils) reloadServer() error {
|
||||
resp, err := healthCheckHttpClient.Post("http://localhost:8080/reload", "", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return NewNestedError("server reload failed").Subjectf("%d", resp.StatusCode)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*Utils) snakeToPascal(s string) string {
|
||||
toHyphenCamel := http.CanonicalHeaderKey(strings.ReplaceAll(s, "_", "-"))
|
||||
return strings.ReplaceAll(toHyphenCamel, "-", "")
|
||||
}
|
||||
@@ -192,3 +208,42 @@ func (*Utils) fileOK(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func setFieldFromSnake[T interface{}, VT interface{}](obj *T, field string, value VT) error {
|
||||
field = utils.snakeToPascal(field)
|
||||
prop := reflect.ValueOf(obj).Elem().FieldByName(field)
|
||||
if prop.Kind() == 0 {
|
||||
return errors.New("unknown field")
|
||||
}
|
||||
prop.Set(reflect.ValueOf(value))
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateYaml(schema *jsonschema.Schema, data []byte) error {
|
||||
if noSchemaValidation {
|
||||
return nil
|
||||
}
|
||||
|
||||
var i interface{}
|
||||
|
||||
err := yaml.Unmarshal(data, &i)
|
||||
if err != nil {
|
||||
return NewNestedError("unable to unmarshal yaml").With(err)
|
||||
}
|
||||
|
||||
m, err := json.Marshal(i)
|
||||
if err != nil {
|
||||
return NewNestedError("unable to marshal json").With(err)
|
||||
}
|
||||
|
||||
err = schema.Validate(bytes.NewReader(m))
|
||||
if err != nil {
|
||||
valErr := err.(*jsonschema.ValidationError)
|
||||
ne := NewNestedError("validation error")
|
||||
for _, e := range valErr.Causes {
|
||||
ne.ExtraError(e)
|
||||
}
|
||||
return ne
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"path"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -22,10 +22,9 @@ type Watcher interface {
|
||||
}
|
||||
|
||||
type watcherBase struct {
|
||||
name string // for log / error output
|
||||
kind string // for log / error output
|
||||
onChange func()
|
||||
l logrus.FieldLogger
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
type fileWatcher struct {
|
||||
@@ -41,31 +40,47 @@ type dockerWatcher struct {
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
func newWatcher(kind string, name string, onChange func()) *watcherBase {
|
||||
func (p *Provider) newWatcher() *watcherBase {
|
||||
return &watcherBase{
|
||||
kind: kind,
|
||||
name: name,
|
||||
onChange: onChange,
|
||||
l: wlog.WithFields(logrus.Fields{"kind": kind, "name": name}),
|
||||
}
|
||||
}
|
||||
func NewFileWatcher(p string, onChange func(), onDelete func()) Watcher {
|
||||
return &fileWatcher{
|
||||
watcherBase: newWatcher("File", path.Base(p), onChange),
|
||||
path: p,
|
||||
onDelete: onDelete,
|
||||
onChange: p.ReloadRoutes,
|
||||
l: p.l,
|
||||
}
|
||||
}
|
||||
|
||||
func NewDockerWatcher(c *client.Client, onChange func()) Watcher {
|
||||
func (p *Provider) NewFileWatcher() Watcher {
|
||||
return &fileWatcher{
|
||||
watcherBase: p.newWatcher(),
|
||||
path: p.GetFilePath(),
|
||||
onDelete: p.StopAllRoutes,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Provider) NewDockerWatcher(c *client.Client) Watcher {
|
||||
return &dockerWatcher{
|
||||
watcherBase: newWatcher("Docker", c.DaemonHost(), onChange),
|
||||
watcherBase: p.newWatcher(),
|
||||
client: c,
|
||||
stopCh: make(chan struct{}, 1),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *config) newWatcher() *watcherBase {
|
||||
return &watcherBase{
|
||||
onChange: c.MustReload,
|
||||
l: c.l,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *config) NewFileWatcher() Watcher {
|
||||
return &fileWatcher{
|
||||
watcherBase: c.newWatcher(),
|
||||
path: c.reader.(*FileReader).Path,
|
||||
onDelete: func() { c.l.Fatal("config file deleted") },
|
||||
}
|
||||
}
|
||||
|
||||
func (w *fileWatcher) Start() {
|
||||
w.Lock()
|
||||
defer w.Unlock()
|
||||
if fsWatcher == nil {
|
||||
return
|
||||
}
|
||||
@@ -78,13 +93,15 @@ func (w *fileWatcher) Start() {
|
||||
}
|
||||
|
||||
func (w *fileWatcher) Stop() {
|
||||
w.Lock()
|
||||
defer w.Unlock()
|
||||
if fsWatcher == nil {
|
||||
return
|
||||
}
|
||||
fileWatchMap.Delete(w.path)
|
||||
err := fsWatcher.Remove(w.path)
|
||||
if err != nil {
|
||||
w.l.WithField("action", "stop").Error(err)
|
||||
w.l.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,19 +110,23 @@ func (w *fileWatcher) Dispose() {
|
||||
}
|
||||
|
||||
func (w *dockerWatcher) Start() {
|
||||
dockerWatchMap.Set(w.name, w)
|
||||
w.Lock()
|
||||
defer w.Unlock()
|
||||
dockerWatchMap.Set(w.client.DaemonHost(), w)
|
||||
w.wg.Add(1)
|
||||
go w.watch()
|
||||
}
|
||||
|
||||
func (w *dockerWatcher) Stop() {
|
||||
w.Lock()
|
||||
defer w.Unlock()
|
||||
if w.stopCh == nil {
|
||||
return
|
||||
}
|
||||
close(w.stopCh)
|
||||
w.wg.Wait()
|
||||
w.stopCh = nil
|
||||
dockerWatchMap.Delete(w.name)
|
||||
dockerWatchMap.Delete(w.client.DaemonHost())
|
||||
}
|
||||
|
||||
func (w *dockerWatcher) Dispose() {
|
||||
@@ -124,31 +145,22 @@ func InitFSWatcher() {
|
||||
go watchFiles()
|
||||
}
|
||||
|
||||
func InitDockerWatcher() {
|
||||
// stop all docker client on watcher stop
|
||||
go func() {
|
||||
defer dockerWatcherWg.Done()
|
||||
<-dockerWatcherStop
|
||||
ParallelForEachValue(
|
||||
dockerWatchMap.Iterator(),
|
||||
(*dockerWatcher).Dispose,
|
||||
)
|
||||
}()
|
||||
}
|
||||
|
||||
func StopFSWatcher() {
|
||||
close(fsWatcherStop)
|
||||
fsWatcherWg.Wait()
|
||||
}
|
||||
|
||||
func StopDockerWatcher() {
|
||||
close(dockerWatcherStop)
|
||||
dockerWatcherWg.Wait()
|
||||
ParallelForEachValue(
|
||||
dockerWatchMap.Iterator(),
|
||||
(*dockerWatcher).Dispose,
|
||||
)
|
||||
}
|
||||
|
||||
func watchFiles() {
|
||||
defer fsWatcher.Close()
|
||||
defer fsWatcherWg.Done()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-fsWatcherStop:
|
||||
@@ -164,10 +176,10 @@ func watchFiles() {
|
||||
}
|
||||
switch {
|
||||
case event.Has(fsnotify.Write):
|
||||
w.l.Info("file changed")
|
||||
w.l.Info("file changed: ", event.Name)
|
||||
go w.onChange()
|
||||
case event.Has(fsnotify.Remove), event.Has(fsnotify.Rename):
|
||||
w.l.Info("file renamed / deleted")
|
||||
w.l.Info("file renamed / deleted: ", event.Name)
|
||||
go w.onDelete()
|
||||
}
|
||||
case err := <-fsWatcher.Errors:
|
||||
@@ -194,26 +206,40 @@ func (w *dockerWatcher) watch() {
|
||||
case <-w.stopCh:
|
||||
return
|
||||
case msg := <-msgChan:
|
||||
w.l.Infof("container %s %s", msg.Actor.Attributes["name"], msg.Action)
|
||||
containerName := msg.Actor.Attributes["name"]
|
||||
if strings.HasPrefix(containerName, "buildx_buildkit_builder-") {
|
||||
continue
|
||||
}
|
||||
w.l.Infof("container %s %s", containerName, msg.Action)
|
||||
go w.onChange()
|
||||
case err := <-errChan:
|
||||
w.l.Errorf("%s, retrying in 1s", err)
|
||||
switch {
|
||||
case client.IsErrConnectionFailed(err):
|
||||
w.l.Error("watcher: connection failed")
|
||||
case client.IsErrNotFound(err):
|
||||
w.l.Error("watcher: endpoint not found")
|
||||
default:
|
||||
w.l.Errorf("watcher: %v", err)
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
msgChan, errChan = listen()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type (
|
||||
FileWatcherMap = SafeMap[string, *fileWatcher]
|
||||
DockerWatcherMap = SafeMap[string, *dockerWatcher]
|
||||
)
|
||||
|
||||
var fsWatcher *fsnotify.Watcher
|
||||
var (
|
||||
fileWatchMap = NewSafeMap[string, *fileWatcher]()
|
||||
dockerWatchMap = NewSafeMap[string, *dockerWatcher]()
|
||||
fileWatchMap FileWatcherMap = NewSafeMapOf[FileWatcherMap]()
|
||||
dockerWatchMap DockerWatcherMap = NewSafeMapOf[DockerWatcherMap]()
|
||||
)
|
||||
var (
|
||||
fsWatcherStop = make(chan struct{}, 1)
|
||||
dockerWatcherStop = make(chan struct{}, 1)
|
||||
fsWatcherStop = make(chan struct{}, 1)
|
||||
)
|
||||
var (
|
||||
fsWatcherWg sync.WaitGroup
|
||||
dockerWatcherWg sync.WaitGroup
|
||||
fsWatcherWg sync.WaitGroup
|
||||
)
|
||||
|
||||
35
templates/config_editor/index.html
Normal file
35
templates/config_editor/index.html
Normal file
@@ -0,0 +1,35 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link href="/codemirror/lib/codemirror.css" rel="stylesheet" />
|
||||
<link href="/codemirror/theme/dracula.css" rel="stylesheet" />
|
||||
<link href="style.css" rel="stylesheet" />
|
||||
<title>Config Editor</title>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="file-navigation">
|
||||
<h3 class="navigation-header">Config Files</h3>
|
||||
<ul id="file-list">
|
||||
{{- range $_, $cfgFile := .}}
|
||||
<li id="file-{{$cfgFile}}">
|
||||
<a class="unselectable">{{$cfgFile}}</a>
|
||||
</li>
|
||||
{{- end}}
|
||||
<li id="new-file">
|
||||
<a class="unselectable">+</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div id="config-editor"></div>
|
||||
</div>
|
||||
|
||||
<script src="/codemirror/lib/codemirror.js"></script>
|
||||
<script src="/codemirror/mode/yaml/yaml.js"></script>
|
||||
<script src="/codemirror/keymap/sublime.js"></script>
|
||||
<script src="/codemirror/addon/comment/comment.js"></script>
|
||||
<script src="index.js" onload="onLoad()"></script>
|
||||
</body>
|
||||
</html>
|
||||
114
templates/config_editor/index.js
Normal file
114
templates/config_editor/index.js
Normal file
@@ -0,0 +1,114 @@
|
||||
let currentFile = "config.yml";
|
||||
let editorElement = document.getElementById("config-editor");
|
||||
let fileListElement = document.getElementById("file-list");
|
||||
let editor = CodeMirror(editorElement, {
|
||||
lineNumbers: true,
|
||||
mode: "yaml",
|
||||
theme: "dracula",
|
||||
autofocus: true,
|
||||
lineWiseCopyCut: true,
|
||||
keyMap: "sublime",
|
||||
tabSize: 2
|
||||
});
|
||||
|
||||
function setCurrentFile(filename) {
|
||||
let old_nav_item = document.getElementById(`file-${currentFile}`);
|
||||
if (old_nav_item !== null) {
|
||||
old_nav_item.classList.remove("active");
|
||||
}
|
||||
currentFile = filename;
|
||||
document.title = `${currentFile} - Config Editor`;
|
||||
let new_nav_item = document.getElementById(`file-${currentFile}`);
|
||||
if (new_nav_item === null) {
|
||||
new_file_btn = document.getElementById("new-file");
|
||||
file_list = document.getElementById("file-list");
|
||||
new_nav_item = document.createElement("li");
|
||||
new_nav_item.id = `file-${currentFile}`;
|
||||
new_nav_item.innerHTML = `<a class="unselectable">${currentFile}</a>`;
|
||||
file_list.insertBefore(new_nav_item, new_file_btn);
|
||||
}
|
||||
new_nav_item.classList.add("active");
|
||||
}
|
||||
|
||||
function loadFile(filename) {
|
||||
if (filename === undefined) {
|
||||
return;
|
||||
}
|
||||
if (filename === '+') {
|
||||
newFile();
|
||||
return;
|
||||
}
|
||||
let req = new XMLHttpRequest();
|
||||
req.open("GET", `/config/${filename}`, true);
|
||||
req.onreadystatechange = function () {
|
||||
if (req.readyState == 4) {
|
||||
if (req.status == 200) {
|
||||
editor.setValue(req.responseText);
|
||||
setCurrentFile(filename);
|
||||
console.log(`loaded ${currentFile}`);
|
||||
} else {
|
||||
let msg = `Failed to load ${filename}: ` + req.responseText;
|
||||
alert(msg);
|
||||
console.log(msg);
|
||||
}
|
||||
}
|
||||
};
|
||||
req.send();
|
||||
}
|
||||
|
||||
function saveFile(filename, content) {
|
||||
let req = new XMLHttpRequest();
|
||||
req.open("PUT", `/config/${filename}`, true);
|
||||
req.setRequestHeader("Content-Type", "text/plain");
|
||||
req.send(content);
|
||||
req.onreadystatechange = function () {
|
||||
if (req.readyState == 4) {
|
||||
if (req.status == 200) {
|
||||
alert(req.responseText);
|
||||
} else {
|
||||
alert("Error:\n" + req.responseText);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function newFile() {
|
||||
let filename = prompt("Enter filename:");
|
||||
if (filename === undefined || filename === "") {
|
||||
alert("File name cannot be empty");
|
||||
return;
|
||||
}
|
||||
if (!filename.endsWith(".yml") && !filename.endsWith(".yaml")) {
|
||||
alert("File name must end with .yml or .yaml");
|
||||
return;
|
||||
}
|
||||
let files = document.getElementById("file-list").children;
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
if (files[i].id === `file-${filename}`) {
|
||||
alert("File already exists");
|
||||
return;
|
||||
}
|
||||
}
|
||||
editor.setValue("");
|
||||
setCurrentFile(filename);
|
||||
}
|
||||
|
||||
editor.setSize("100wh", "100vh");
|
||||
editor.setOption("extraKeys", {
|
||||
Tab: function (cm) {
|
||||
const spaces = Array(cm.getOption("indentUnit") + 1).join(" ");
|
||||
cm.replaceSelection(spaces);
|
||||
},
|
||||
"Ctrl-S": function (cm) {
|
||||
saveFile(currentFile, cm.getValue());
|
||||
},
|
||||
});
|
||||
fileListElement.addEventListener("click", function (e) {
|
||||
if (e.target === null) {
|
||||
return;
|
||||
}
|
||||
loadFile(e.target.text);
|
||||
});
|
||||
function onLoad() {
|
||||
loadFile(currentFile);
|
||||
}
|
||||
64
templates/config_editor/style.css
Normal file
64
templates/config_editor/style.css
Normal file
@@ -0,0 +1,64 @@
|
||||
html,
|
||||
body {
|
||||
height: 100%;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
font: 14px !important;
|
||||
font-family: monospace !important;
|
||||
}
|
||||
.container {
|
||||
display: flex;
|
||||
}
|
||||
.navigation-header {
|
||||
color: #f8f8f2 !important;
|
||||
padding-left: 2em;
|
||||
display: block;
|
||||
}
|
||||
.file-navigation {
|
||||
width: 250px;
|
||||
height: auto;
|
||||
overflow-y: auto;
|
||||
background: #282a36 !important;
|
||||
}
|
||||
.file-navigation ul {
|
||||
list-style: none;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
}
|
||||
.file-navigation li {
|
||||
padding-top: 8px;
|
||||
padding-bottom: 8px;
|
||||
}
|
||||
.file-navigation a {
|
||||
color: #f8f8f2 !important;
|
||||
text-decoration: none;
|
||||
padding-left: 4em;
|
||||
padding-right: 4em;
|
||||
display: block;
|
||||
}
|
||||
#new-file {
|
||||
color: #f8f8f2 !important;
|
||||
font-weight: bold;
|
||||
}
|
||||
.active {
|
||||
font-weight: bold;
|
||||
background: rgba(255, 255, 255, 0.1);
|
||||
}
|
||||
.unselectable {
|
||||
-webkit-touch-callout: none;
|
||||
-webkit-user-select: none;
|
||||
-khtml-user-select: none;
|
||||
-moz-user-select: none;
|
||||
-ms-user-select: none;
|
||||
user-select: none;
|
||||
}
|
||||
.CodeMirror * {
|
||||
font-size: 14px !important;
|
||||
}
|
||||
.CodeMirror pre {
|
||||
padding-top: 3px;
|
||||
padding-bottom: 3px;
|
||||
}
|
||||
#config-editor {
|
||||
flex-grow: 1;
|
||||
}
|
||||
23
templates/index.html
Normal file
23
templates/index.html
Normal file
@@ -0,0 +1,23 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link href="style.css" rel="stylesheet" />
|
||||
<title>go-proxy</title>
|
||||
</head>
|
||||
<body>
|
||||
<script src="main.js"></script>
|
||||
<div id="sidenav" class="sidenav">
|
||||
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()"
|
||||
>×</a
|
||||
>
|
||||
<a href="#" onClick='setContent("/panel")'>Panel</a>
|
||||
<a href="#" onClick='setContent("/config_editor")'>Config Editor</a>
|
||||
</div>
|
||||
<a class="openbtn" id="openbtn" onclick="openNav()">≡</a>
|
||||
<div id="main">
|
||||
<iframe id="content" src="/config_editor" title="panel"></iframe>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
27
templates/main.js
Normal file
27
templates/main.js
Normal file
@@ -0,0 +1,27 @@
|
||||
function contentIFrame() {
|
||||
return document.getElementById("content");
|
||||
}
|
||||
|
||||
function openNavBtn() {
|
||||
return document.getElementById("openbtn");
|
||||
}
|
||||
|
||||
function sideNav() {
|
||||
return document.getElementById("sidenav");
|
||||
}
|
||||
|
||||
function setContent(path) {
|
||||
contentIFrame().attributes.src.value = path;
|
||||
}
|
||||
|
||||
function openNav() {
|
||||
sideNav().style.width = "250px";
|
||||
contentIFrame().style.marginLeft = "250px";
|
||||
openNavBtn().style.display = "none";
|
||||
}
|
||||
|
||||
function closeNav() {
|
||||
sideNav().style.width = "0";
|
||||
contentIFrame().style.marginLeft = "0px";
|
||||
openNavBtn().style.display = "inline-block";
|
||||
}
|
||||
@@ -1,156 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<style>
|
||||
body {
|
||||
background-color: #131516;
|
||||
color: #ffffff;
|
||||
}
|
||||
|
||||
table {
|
||||
border-collapse: collapse;
|
||||
border-spacing: 0;
|
||||
}
|
||||
|
||||
tr {
|
||||
border-radius: 10px;
|
||||
}
|
||||
|
||||
table th:first-child {
|
||||
border-radius: 10px 0 0 10px;
|
||||
}
|
||||
|
||||
table th:last-child {
|
||||
border-radius: 0 10px 10px 0;
|
||||
}
|
||||
|
||||
table td:first-of-type {
|
||||
border-top-left-radius: 10px;
|
||||
border-bottom-left-radius: 10px;
|
||||
}
|
||||
|
||||
table td:last-of-type {
|
||||
border-top-right-radius: 10px;
|
||||
border-bottom-right-radius: 10px;
|
||||
}
|
||||
|
||||
table caption {
|
||||
color: antiquewhite;
|
||||
}
|
||||
|
||||
.health-circle {
|
||||
height: 15px;
|
||||
width: 15px;
|
||||
background-color: #28a745;
|
||||
border-radius: 50%;
|
||||
margin: auto;
|
||||
}
|
||||
</style>
|
||||
<title>Route Panel</title>
|
||||
<script>
|
||||
function checkHealth(url, cell) {
|
||||
var xhttp = new XMLHttpRequest();
|
||||
xhttp.onreadystatechange = function () {
|
||||
if (this.readyState != 4) {
|
||||
return
|
||||
}
|
||||
if (this.status === 200) {
|
||||
cell.innerHTML = '<div class="health-circle"></div>'; // Green circle for healthy
|
||||
} else {
|
||||
cell.innerHTML = '<div class="health-circle" style="background-color: #dc3545;"></div>'; // Red circle for unhealthy
|
||||
}
|
||||
};
|
||||
url = window.location.origin + '/checkhealth?target=' + encodeURIComponent(url);
|
||||
xhttp.open("HEAD", url, true);
|
||||
xhttp.send();
|
||||
}
|
||||
|
||||
function updateHealthStatus() {
|
||||
let rows = document.querySelectorAll('tbody tr');
|
||||
rows.forEach(row => {
|
||||
let url = row.querySelector('#url-cell').textContent;
|
||||
let cell = row.querySelector('#health-cell'); // Health column cell
|
||||
checkHealth(url, cell);
|
||||
});
|
||||
}
|
||||
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
updateHealthStatus();
|
||||
|
||||
// Update health status every 5 seconds
|
||||
setInterval(updateHealthStatus, 5000);
|
||||
})
|
||||
</script>
|
||||
</head>
|
||||
|
||||
<body class="m-3">
|
||||
<div class="container">
|
||||
<h1 class="text-success">
|
||||
Route Panel
|
||||
</h1>
|
||||
<div class="row">
|
||||
<div class="table-responsive col-md-6">
|
||||
<table class="table table-striped table-dark caption-top w-auto">
|
||||
<caption>HTTP Proxies</caption>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Alias</th>
|
||||
<th>Path</th>
|
||||
<th>Path Mode</th>
|
||||
<th>URL</th>
|
||||
<th>Health</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range $alias, $pathPoolMap := .HTTPRoutes.Iterator}}
|
||||
{{range $path, $lbPool := $pathPoolMap.Iterator}}
|
||||
{{range $_, $route := $lbPool.Iterator}}
|
||||
<tr>
|
||||
<td>{{$alias}}</td>
|
||||
<td>{{$path}}</td>
|
||||
<td>{{$route.PathMode}}</td>
|
||||
<td id="url-cell">{{$route.Url.String}}</td>
|
||||
<td class="align-middle" id="health-cell">
|
||||
<div class="health-circle"></div>
|
||||
</td> <!-- Health column -->
|
||||
</tr>
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div class="table-responsive col-md-6">
|
||||
<table class="table table-striped table-dark caption-top w-auto">
|
||||
<caption>Streams</caption>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Alias</th>
|
||||
<th>Source</th>
|
||||
<th>Target</th>
|
||||
<th>Health</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range $_, $route := .StreamRoutes.Iterator}}
|
||||
<tr>
|
||||
<td>{{$route.Alias}}</td>
|
||||
<td>{{$route.ListeningUrl}}</td>
|
||||
<td id="url-cell">{{$route.TargetUrl}}</td>
|
||||
<td class="align-middle" id="health-cell">
|
||||
<div class="health-circle"></div>
|
||||
</td> <!-- Health column -->
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
6
templates/panel/bootstrap.min.css
vendored
Normal file
6
templates/panel/bootstrap.min.css
vendored
Normal file
File diff suppressed because one or more lines are too long
79
templates/panel/index.html
Executable file
79
templates/panel/index.html
Executable file
@@ -0,0 +1,79 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link href="bootstrap.min.css" rel="stylesheet" />
|
||||
<link href="style.css" rel="stylesheet" />
|
||||
<title>Route Panel</title>
|
||||
</head>
|
||||
|
||||
<body class="m-3">
|
||||
<script src="index.js" defer></script>
|
||||
<div class="container">
|
||||
<h1 class="text-success">Route Panel</h1>
|
||||
<div class="row">
|
||||
<div class="table-responsive col-md-auto flex-shrink-1">
|
||||
<table class="table table-striped table-dark caption-top">
|
||||
<caption>
|
||||
HTTP Proxies
|
||||
</caption>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Alias</th>
|
||||
<th>Path</th>
|
||||
<th>Path Mode</th>
|
||||
<th>URL</th>
|
||||
<th>Health</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range $alias, $pathPoolMap := .HTTPRoutes.Iterator}} {{range
|
||||
$path, $lbPool := $pathPoolMap.Iterator}} {{range $_, $route :=
|
||||
$lbPool.Iterator}}
|
||||
<tr>
|
||||
<td>{{$alias}}</td>
|
||||
<td>{{$path}}</td>
|
||||
<td>{{$route.PathMode}}</td>
|
||||
<td id="url-cell">{{$route.Url.String}}</td>
|
||||
<td class="align-middle" id="health-cell">
|
||||
<div class="health-circle"></div>
|
||||
</td>
|
||||
<!-- Health column -->
|
||||
</tr>
|
||||
{{end}} {{end}} {{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div class="table-responsive col-md">
|
||||
<table class="table table-striped table-dark caption-top w-auto">
|
||||
<caption>
|
||||
Streams
|
||||
</caption>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Alias</th>
|
||||
<th>Source</th>
|
||||
<th>Target</th>
|
||||
<th>Health</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range $_, $route := .StreamRoutes.Iterator}}
|
||||
<tr>
|
||||
<td>{{$route.Alias}}</td>
|
||||
<td>{{$route.ListeningUrl}}</td>
|
||||
<td id="url-cell">{{$route.TargetUrl}}</td>
|
||||
<td class="align-middle" id="health-cell">
|
||||
<div class="health-circle"></div>
|
||||
</td>
|
||||
<!-- Health column -->
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
34
templates/panel/index.js
Normal file
34
templates/panel/index.js
Normal file
@@ -0,0 +1,34 @@
|
||||
function checkHealth(url, cell) {
|
||||
var xhttp = new XMLHttpRequest();
|
||||
xhttp.onreadystatechange = function () {
|
||||
if (this.readyState != 4) {
|
||||
return;
|
||||
}
|
||||
if (this.status === 200) {
|
||||
cell.innerHTML = '<div class="health-circle"></div>'; // Green circle for healthy
|
||||
} else {
|
||||
cell.innerHTML =
|
||||
'<div class="health-circle" style="background-color: #dc3545;"></div>'; // Red circle for unhealthy
|
||||
}
|
||||
};
|
||||
url =
|
||||
window.location.origin + "/checkhealth?target=" + encodeURIComponent(url);
|
||||
xhttp.open("HEAD", url, true);
|
||||
xhttp.send();
|
||||
}
|
||||
|
||||
function updateHealthStatus() {
|
||||
let rows = document.querySelectorAll("tbody tr");
|
||||
rows.forEach((row) => {
|
||||
let url = row.querySelector("#url-cell").textContent;
|
||||
let cell = row.querySelector("#health-cell"); // Health column cell
|
||||
checkHealth(url, cell);
|
||||
});
|
||||
}
|
||||
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
updateHealthStatus();
|
||||
|
||||
// Update health status every 5 seconds
|
||||
setInterval(updateHealthStatus, 5000);
|
||||
});
|
||||
43
templates/panel/style.css
Normal file
43
templates/panel/style.css
Normal file
@@ -0,0 +1,43 @@
|
||||
body {
|
||||
background-color: #131516;
|
||||
color: #ffffff;
|
||||
}
|
||||
|
||||
table {
|
||||
border-collapse: collapse;
|
||||
border-spacing: 0;
|
||||
}
|
||||
|
||||
tr {
|
||||
border-radius: 10px;
|
||||
}
|
||||
|
||||
table th:first-child {
|
||||
border-radius: 10px 0 0 10px;
|
||||
}
|
||||
|
||||
table th:last-child {
|
||||
border-radius: 0 10px 10px 0;
|
||||
}
|
||||
|
||||
table td:first-of-type {
|
||||
border-top-left-radius: 10px;
|
||||
border-bottom-left-radius: 10px;
|
||||
}
|
||||
|
||||
table td:last-of-type {
|
||||
border-top-right-radius: 10px;
|
||||
border-bottom-right-radius: 10px;
|
||||
}
|
||||
|
||||
table caption {
|
||||
color: antiquewhite;
|
||||
}
|
||||
|
||||
.health-circle {
|
||||
height: 15px;
|
||||
width: 15px;
|
||||
background-color: #28a745;
|
||||
border-radius: 50%;
|
||||
margin: auto;
|
||||
}
|
||||
68
templates/style.css
Normal file
68
templates/style.css
Normal file
@@ -0,0 +1,68 @@
|
||||
html,
|
||||
body {
|
||||
font-family: monospace !important;
|
||||
}
|
||||
|
||||
.sidenav {
|
||||
height: 100%;
|
||||
width: 0;
|
||||
position: fixed;
|
||||
z-index: 1;
|
||||
top: 0;
|
||||
left: 0;
|
||||
background-color: #111;
|
||||
overflow-x: hidden;
|
||||
padding-top: 32px;
|
||||
transition: 0.3s;
|
||||
}
|
||||
|
||||
.sidenav a {
|
||||
padding: 8px 8px 8px 24px;
|
||||
text-decoration: none;
|
||||
font-size: 24px;
|
||||
font-weight: bold;
|
||||
color: #818181;
|
||||
display: block;
|
||||
}
|
||||
.sidenav a:hover {
|
||||
color: #f1f1f1;
|
||||
}
|
||||
|
||||
.sidenav .closebtn {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
right: 24px;
|
||||
font-size: 24px;
|
||||
margin-left: 42px;
|
||||
}
|
||||
|
||||
.openbtn {
|
||||
z-index: 1;
|
||||
position: absolute;
|
||||
top: 16;
|
||||
left: 16;
|
||||
font: 24px bold monospace;
|
||||
color: #f8f8f2 !important;
|
||||
}
|
||||
|
||||
#main {
|
||||
transition: margin-left 0.3s;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
#content {
|
||||
transition: margin-left 0.3s;
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
bottom: 0;
|
||||
right: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
border: none;
|
||||
margin: 0;
|
||||
margin-left: 0px;
|
||||
padding: 0;
|
||||
overflow: hidden;
|
||||
z-index: 0;
|
||||
}
|
||||
1
version.txt
Normal file
1
version.txt
Normal file
@@ -0,0 +1 @@
|
||||
0.4.8
|
||||
Reference in New Issue
Block a user