mirror of
https://github.com/yusing/godoxy.git
synced 2025-07-27 06:03:16 +02:00
merge feat/oidc-middleware (#49)
* api: enrich provider statistifcs * fix: docker monitor now uses container status * Feat/auto schemas (#48) * use auto generated schemas * go version bump and dependencies upgrade * clarify some error messages --------- Co-authored-by: yusing <yusing@6uo.me> * cleanup some loadbalancer code * api: cleanup websocket code * api: add /v1/health/ws for health bubbles on dashboard * feat: experimental memory logger and logs api for WebUI --------- Co-authored-by: yusing <yusing@6uo.me>
This commit is contained in:
parent
abbe4ffceb
commit
bde3eee232
64 changed files with 5522 additions and 1078 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -4,6 +4,7 @@ compose.yml
|
||||||
config
|
config
|
||||||
certs
|
certs
|
||||||
config*/
|
config*/
|
||||||
|
!schemas/**
|
||||||
certs*/
|
certs*/
|
||||||
bin/
|
bin/
|
||||||
error_pages/
|
error_pages/
|
||||||
|
|
4
.vscode/settings.example.json
vendored
4
.vscode/settings.example.json
vendored
|
@ -1,10 +1,10 @@
|
||||||
{
|
{
|
||||||
"yaml.schemas": {
|
"yaml.schemas": {
|
||||||
"https://github.com/yusing/go-proxy/raw/v0.8/schema/config.schema.json": [
|
"https://github.com/yusing/go-proxy/raw/v0.8/schemas/config.schema.json": [
|
||||||
"config.example.yml",
|
"config.example.yml",
|
||||||
"config.yml"
|
"config.yml"
|
||||||
],
|
],
|
||||||
"https://github.com/yusing/go-proxy/raw/v0.8/schema/providers.schema.json": [
|
"https://github.com/yusing/go-proxy/raw/v0.8/schemas/routes.schema.json": [
|
||||||
"providers.example.yml"
|
"providers.example.yml"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
# Stage 1: Builder
|
# Stage 1: Builder
|
||||||
FROM golang:1.23.4-alpine AS builder
|
FROM golang:1.23.5-alpine AS builder
|
||||||
HEALTHCHECK NONE
|
HEALTHCHECK NONE
|
||||||
|
|
||||||
# package version does not matter
|
# package version does not matter
|
||||||
|
@ -51,7 +51,7 @@ COPY config.example.yml /app/config/config.yml
|
||||||
COPY --from=builder /etc/ssl/certs /etc/ssl/certs
|
COPY --from=builder /etc/ssl/certs /etc/ssl/certs
|
||||||
|
|
||||||
# copy schema
|
# copy schema
|
||||||
COPY schema /app/schema
|
COPY schemas/config.schema.json schemas/routes.schema.json schemas/middleware_compose.schema.json /app/schemas/
|
||||||
|
|
||||||
ENV DOCKER_HOST=unix:///var/run/docker.sock
|
ENV DOCKER_HOST=unix:///var/run/docker.sock
|
||||||
ENV GODOXY_DEBUG=0
|
ENV GODOXY_DEBUG=0
|
||||||
|
|
24
Makefile
24
Makefile
|
@ -71,3 +71,27 @@ push-docker-io:
|
||||||
build-docker:
|
build-docker:
|
||||||
docker build -t godoxy-nightly \
|
docker build -t godoxy-nightly \
|
||||||
--build-arg VERSION="${VERSION}-nightly-${BUILD_DATE}" .
|
--build-arg VERSION="${VERSION}-nightly-${BUILD_DATE}" .
|
||||||
|
|
||||||
|
gen-schema-single:
|
||||||
|
typescript-json-schema --noExtraProps --required --skipLibCheck --tsNodeRegister=true -o schemas/${OUT} schemas/${IN} ${CLASS}
|
||||||
|
|
||||||
|
gen-schema:
|
||||||
|
make IN=config/config.ts \
|
||||||
|
CLASS=Config \
|
||||||
|
OUT=config.schema.json \
|
||||||
|
gen-schema-single
|
||||||
|
make IN=providers/routes.ts \
|
||||||
|
CLASS=Routes \
|
||||||
|
OUT=routes.schema.json \
|
||||||
|
gen-schema-single
|
||||||
|
make IN=middlewares/middleware_compose.ts \
|
||||||
|
CLASS=MiddlewareCompose \
|
||||||
|
OUT=middleware_compose.schema.json \
|
||||||
|
gen-schema-single
|
||||||
|
make IN=docker.ts \
|
||||||
|
CLASS=DockerRoutes \
|
||||||
|
OUT=docker_routes.schema.json \
|
||||||
|
gen-schema-single
|
||||||
|
|
||||||
|
push-github:
|
||||||
|
git push origin $(shell git rev-parse --abbrev-ref HEAD)
|
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
@ -9,6 +10,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/yusing/go-proxy/internal"
|
"github.com/yusing/go-proxy/internal"
|
||||||
|
v1 "github.com/yusing/go-proxy/internal/api/v1"
|
||||||
"github.com/yusing/go-proxy/internal/api/v1/auth"
|
"github.com/yusing/go-proxy/internal/api/v1/auth"
|
||||||
"github.com/yusing/go-proxy/internal/api/v1/query"
|
"github.com/yusing/go-proxy/internal/api/v1/query"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
|
@ -24,6 +26,12 @@ import (
|
||||||
var rawLogger = log.New(os.Stdout, "", 0)
|
var rawLogger = log.New(os.Stdout, "", 0)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
var out io.Writer = os.Stdout
|
||||||
|
if common.EnableLogStreaming {
|
||||||
|
out = io.MultiWriter(out, v1.MemLogger())
|
||||||
|
}
|
||||||
|
logging.InitLogger(out)
|
||||||
|
|
||||||
args := common.GetArgs()
|
args := common.GetArgs()
|
||||||
|
|
||||||
switch args.Command {
|
switch args.Command {
|
||||||
|
|
22
go.mod
22
go.mod
|
@ -1,16 +1,16 @@
|
||||||
module github.com/yusing/go-proxy
|
module github.com/yusing/go-proxy
|
||||||
|
|
||||||
go 1.23.4
|
go 1.23.5
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/PuerkitoBio/goquery v1.10.1
|
github.com/PuerkitoBio/goquery v1.10.1
|
||||||
github.com/coder/websocket v1.8.12
|
github.com/coder/websocket v1.8.12
|
||||||
github.com/coreos/go-oidc/v3 v3.12.0
|
github.com/coreos/go-oidc/v3 v3.12.0
|
||||||
github.com/docker/cli v27.4.1+incompatible
|
github.com/docker/cli v27.5.0+incompatible
|
||||||
github.com/docker/docker v27.4.1+incompatible
|
github.com/docker/docker v27.5.0+incompatible
|
||||||
github.com/fsnotify/fsnotify v1.8.0
|
github.com/fsnotify/fsnotify v1.8.0
|
||||||
github.com/go-acme/lego/v4 v4.21.0
|
github.com/go-acme/lego/v4 v4.21.0
|
||||||
github.com/go-playground/validator/v10 v10.23.0
|
github.com/go-playground/validator/v10 v10.24.0
|
||||||
github.com/gobwas/glob v0.2.3
|
github.com/gobwas/glob v0.2.3
|
||||||
github.com/golang-jwt/jwt/v5 v5.2.1
|
github.com/golang-jwt/jwt/v5 v5.2.1
|
||||||
github.com/gotify/server/v2 v2.6.1
|
github.com/gotify/server/v2 v2.6.1
|
||||||
|
@ -32,7 +32,7 @@ require (
|
||||||
github.com/beorn7/perks v1.0.1 // indirect
|
github.com/beorn7/perks v1.0.1 // indirect
|
||||||
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
|
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
|
||||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||||
github.com/cloudflare/cloudflare-go v0.113.0 // indirect
|
github.com/cloudflare/cloudflare-go v0.114.0 // indirect
|
||||||
github.com/containerd/log v0.1.0 // indirect
|
github.com/containerd/log v0.1.0 // indirect
|
||||||
github.com/distribution/reference v0.6.0 // indirect
|
github.com/distribution/reference v0.6.0 // indirect
|
||||||
github.com/docker/go-connections v0.5.0 // indirect
|
github.com/docker/go-connections v0.5.0 // indirect
|
||||||
|
@ -61,21 +61,21 @@ require (
|
||||||
github.com/ovh/go-ovh v1.6.0 // indirect
|
github.com/ovh/go-ovh v1.6.0 // indirect
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
github.com/prometheus/client_model v0.6.1 // indirect
|
github.com/prometheus/client_model v0.6.1 // indirect
|
||||||
github.com/prometheus/common v0.61.0 // indirect
|
github.com/prometheus/common v0.62.0 // indirect
|
||||||
github.com/prometheus/procfs v0.15.1 // indirect
|
github.com/prometheus/procfs v0.15.1 // indirect
|
||||||
github.com/sirupsen/logrus v1.9.3 // indirect
|
github.com/sirupsen/logrus v1.9.3 // indirect
|
||||||
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
|
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
|
||||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.58.0 // indirect
|
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.59.0 // indirect
|
||||||
go.opentelemetry.io/otel v1.33.0 // indirect
|
go.opentelemetry.io/otel v1.34.0 // indirect
|
||||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.30.0 // indirect
|
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.30.0 // indirect
|
||||||
go.opentelemetry.io/otel/metric v1.33.0 // indirect
|
go.opentelemetry.io/otel/metric v1.34.0 // indirect
|
||||||
go.opentelemetry.io/otel/sdk v1.30.0 // indirect
|
go.opentelemetry.io/otel/sdk v1.30.0 // indirect
|
||||||
go.opentelemetry.io/otel/trace v1.33.0 // indirect
|
go.opentelemetry.io/otel/trace v1.34.0 // indirect
|
||||||
golang.org/x/mod v0.22.0 // indirect
|
golang.org/x/mod v0.22.0 // indirect
|
||||||
golang.org/x/sync v0.10.0 // indirect
|
golang.org/x/sync v0.10.0 // indirect
|
||||||
golang.org/x/sys v0.29.0 // indirect
|
golang.org/x/sys v0.29.0 // indirect
|
||||||
golang.org/x/tools v0.29.0 // indirect
|
golang.org/x/tools v0.29.0 // indirect
|
||||||
google.golang.org/protobuf v1.36.2 // indirect
|
google.golang.org/protobuf v1.36.3 // indirect
|
||||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||||
gotest.tools/v3 v3.5.1 // indirect
|
gotest.tools/v3 v3.5.1 // indirect
|
||||||
)
|
)
|
||||||
|
|
40
go.sum
40
go.sum
|
@ -12,8 +12,8 @@ github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK3
|
||||||
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
|
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
|
||||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
github.com/cloudflare/cloudflare-go v0.113.0 h1:qnOXmA6RbgZ4rg5gNBK5QGk0Pzbv8pnUYV3C4+8CU6w=
|
github.com/cloudflare/cloudflare-go v0.114.0 h1:ucoti4/7Exo0XQ+rzpn1H+IfVVe++zgiM+tyKtf0HUA=
|
||||||
github.com/cloudflare/cloudflare-go v0.113.0/go.mod h1:Dlm4BAnycHc0i8yLxQZb9b+OlMwYOAoDJsUOEFgpVvo=
|
github.com/cloudflare/cloudflare-go v0.114.0/go.mod h1:O7fYfFfA6wKqKFn2QIR9lhj7FDw6VQCGOY6hd2TBtd0=
|
||||||
github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NAo=
|
github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NAo=
|
||||||
github.com/coder/websocket v1.8.12/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs=
|
github.com/coder/websocket v1.8.12/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs=
|
||||||
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
|
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
|
||||||
|
@ -27,10 +27,10 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
|
||||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
|
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/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
|
||||||
github.com/docker/cli v27.4.1+incompatible h1:VzPiUlRJ/xh+otB75gva3r05isHMo5wXDfPRi5/b4hI=
|
github.com/docker/cli v27.5.0+incompatible h1:aMphQkcGtpHixwwhAXJT1rrK/detk2JIvDaFkLctbGM=
|
||||||
github.com/docker/cli v27.4.1+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
|
github.com/docker/cli v27.5.0+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
|
||||||
github.com/docker/docker v27.4.1+incompatible h1:ZJvcY7gfwHn1JF48PfbyXg7Jyt9ZCWDW+GGXOIxEwp4=
|
github.com/docker/docker v27.5.0+incompatible h1:um++2NcQtGRTz5eEgO6aJimo6/JxrTXC941hd05JO6U=
|
||||||
github.com/docker/docker v27.4.1+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
|
github.com/docker/docker v27.5.0+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
|
||||||
github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj1Br63c=
|
github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj1Br63c=
|
||||||
github.com/docker/go-connections v0.5.0/go.mod h1:ov60Kzw0kKElRwhNs9UlUHAE/F9Fe6GLaXnqyDdmEXc=
|
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 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
|
||||||
|
@ -56,8 +56,8 @@ github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/o
|
||||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||||
github.com/go-playground/validator/v10 v10.23.0 h1:/PwmTwZhS0dPkav3cdK9kV1FsAmrL8sThn8IHr/sO+o=
|
github.com/go-playground/validator/v10 v10.24.0 h1:KHQckvo8G6hlWnrPX4NJJ+aBfWNAE/HH+qdL2cBpCmg=
|
||||||
github.com/go-playground/validator/v10 v10.23.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
github.com/go-playground/validator/v10 v10.24.0/go.mod h1:GGzBIJMuE98Ic/kJsBXbz1x/7cByt++cQ+YOuDM5wus=
|
||||||
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
|
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
|
||||||
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
|
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
|
||||||
github.com/goccy/go-json v0.10.4 h1:JSwxQzIqKfmFX1swYPpUThQZp/Ka4wzJdK0LWVytLPM=
|
github.com/goccy/go-json v0.10.4 h1:JSwxQzIqKfmFX1swYPpUThQZp/Ka4wzJdK0LWVytLPM=
|
||||||
|
@ -126,8 +126,8 @@ github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+
|
||||||
github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
|
github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
|
||||||
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
|
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
|
||||||
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
|
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
|
||||||
github.com/prometheus/common v0.61.0 h1:3gv/GThfX0cV2lpO7gkTUwZru38mxevy90Bj8YFSRQQ=
|
github.com/prometheus/common v0.62.0 h1:xasJaQlnWAeyHdUBeGjXmutelfJHWMRr+Fg4QszZ2Io=
|
||||||
github.com/prometheus/common v0.61.0/go.mod h1:zr29OCN/2BsJRaFwG8QOBr41D6kkchKbpeNH7pAjb/s=
|
github.com/prometheus/common v0.62.0/go.mod h1:vyBcEuLSvWos9B1+CyL7JZ2up+uFzXhkqml0W5zIY1I=
|
||||||
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
|
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
|
||||||
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
|
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
|
||||||
github.com/puzpuzpuz/xsync/v3 v3.4.0 h1:DuVBAdXuGFHv8adVXjWWZ63pJq+NRXOWVXlKDBZ+mJ4=
|
github.com/puzpuzpuz/xsync/v3 v3.4.0 h1:DuVBAdXuGFHv8adVXjWWZ63pJq+NRXOWVXlKDBZ+mJ4=
|
||||||
|
@ -150,20 +150,20 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
|
||||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
|
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
|
||||||
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
|
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
|
||||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.58.0 h1:yd02MEjBdJkG3uabWP9apV+OuWRIXGDuJEUJbOHmCFU=
|
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.59.0 h1:CV7UdSGJt/Ao6Gp4CXckLxVRRsRgDHoI8XjbL3PDl8s=
|
||||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.58.0/go.mod h1:umTcuxiv1n/s/S6/c2AT/g2CQ7u5C59sHDNmfSwgz7Q=
|
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.59.0/go.mod h1:FRmFuRJfag1IZ2dPkHnEoSFVgTVPUd2qf5Vi69hLb8I=
|
||||||
go.opentelemetry.io/otel v1.33.0 h1:/FerN9bax5LoK51X/sI0SVYrjSE0/yUL7DpxW4K3FWw=
|
go.opentelemetry.io/otel v1.34.0 h1:zRLXxLCgL1WyKsPVrgbSdMN4c0FMkDAskSTQP+0hdUY=
|
||||||
go.opentelemetry.io/otel v1.33.0/go.mod h1:SUUkR6csvUQl+yjReHu5uM3EtVV7MBm5FHKRlNx4I8I=
|
go.opentelemetry.io/otel v1.34.0/go.mod h1:OWFPOQ+h4G8xpyjgqo4SxJYdDQ/qmRH+wivy7zzx9oI=
|
||||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.30.0 h1:lsInsfvhVIfOI6qHVyysXMNDnjO9Npvl7tlDPJFBVd4=
|
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.30.0 h1:lsInsfvhVIfOI6qHVyysXMNDnjO9Npvl7tlDPJFBVd4=
|
||||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.30.0/go.mod h1:KQsVNh4OjgjTG0G6EiNi1jVpnaeeKsKMRwbLN+f1+8M=
|
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.30.0/go.mod h1:KQsVNh4OjgjTG0G6EiNi1jVpnaeeKsKMRwbLN+f1+8M=
|
||||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.30.0 h1:umZgi92IyxfXd/l4kaDhnKgY8rnN/cZcF1LKc6I8OQ8=
|
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.30.0 h1:umZgi92IyxfXd/l4kaDhnKgY8rnN/cZcF1LKc6I8OQ8=
|
||||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.30.0/go.mod h1:4lVs6obhSVRb1EW5FhOuBTyiQhtRtAnnva9vD3yRfq8=
|
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.30.0/go.mod h1:4lVs6obhSVRb1EW5FhOuBTyiQhtRtAnnva9vD3yRfq8=
|
||||||
go.opentelemetry.io/otel/metric v1.33.0 h1:r+JOocAyeRVXD8lZpjdQjzMadVZp2M4WmQ+5WtEnklQ=
|
go.opentelemetry.io/otel/metric v1.34.0 h1:+eTR3U0MyfWjRDhmFMxe2SsW64QrZ84AOhvqS7Y+PoQ=
|
||||||
go.opentelemetry.io/otel/metric v1.33.0/go.mod h1:L9+Fyctbp6HFTddIxClbQkjtubW6O9QS3Ann/M82u6M=
|
go.opentelemetry.io/otel/metric v1.34.0/go.mod h1:CEDrp0fy2D0MvkXE+dPV7cMi8tWZwX3dmaIhwPOaqHE=
|
||||||
go.opentelemetry.io/otel/sdk v1.30.0 h1:cHdik6irO49R5IysVhdn8oaiR9m8XluDaJAs4DfOrYE=
|
go.opentelemetry.io/otel/sdk v1.30.0 h1:cHdik6irO49R5IysVhdn8oaiR9m8XluDaJAs4DfOrYE=
|
||||||
go.opentelemetry.io/otel/sdk v1.30.0/go.mod h1:p14X4Ok8S+sygzblytT1nqG98QG2KYKv++HE0LY/mhg=
|
go.opentelemetry.io/otel/sdk v1.30.0/go.mod h1:p14X4Ok8S+sygzblytT1nqG98QG2KYKv++HE0LY/mhg=
|
||||||
go.opentelemetry.io/otel/trace v1.33.0 h1:cCJuF7LRjUFso9LPnEAHJDB2pqzp+hbO8eu1qqW2d/s=
|
go.opentelemetry.io/otel/trace v1.34.0 h1:+ouXS2V8Rd4hp4580a8q23bg0azF2nI8cqLYnC8mh/k=
|
||||||
go.opentelemetry.io/otel/trace v1.33.0/go.mod h1:uIcdVUZMpTAmz0tI1z04GoVSezK37CbGV4fr1f2nBck=
|
go.opentelemetry.io/otel/trace v1.34.0/go.mod h1:Svm7lSjQD7kG7KJ/MUHPVXSDGz2OX4h0M2jHBhmSfRE=
|
||||||
go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeXrui0=
|
go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeXrui0=
|
||||||
go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8=
|
go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
@ -271,8 +271,8 @@ google.golang.org/genproto/googleapis/rpc v0.0.0-20241209162323-e6fa225c2576 h1:
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20241209162323-e6fa225c2576/go.mod h1:5uTbfoYQed2U9p3KIj2/Zzm02PYhndfdmML0qC3q3FU=
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20241209162323-e6fa225c2576/go.mod h1:5uTbfoYQed2U9p3KIj2/Zzm02PYhndfdmML0qC3q3FU=
|
||||||
google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E=
|
google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E=
|
||||||
google.golang.org/grpc v1.67.1/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA=
|
google.golang.org/grpc v1.67.1/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA=
|
||||||
google.golang.org/protobuf v1.36.2 h1:R8FeyR1/eLmkutZOM5CWghmo5itiG9z0ktFlTVLuTmU=
|
google.golang.org/protobuf v1.36.3 h1:82DV7MYdb8anAVi3qge1wSnMDrnKK7ebr+I0hHRN1BU=
|
||||||
google.golang.org/protobuf v1.36.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
google.golang.org/protobuf v1.36.3/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
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 h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||||
|
|
|
@ -31,6 +31,8 @@ func NewHandler(cfg config.ConfigInstance) http.Handler {
|
||||||
mux.HandleFunc("GET", "/v1/schema/{filename...}", v1.GetSchemaFile)
|
mux.HandleFunc("GET", "/v1/schema/{filename...}", v1.GetSchemaFile)
|
||||||
mux.HandleFunc("GET", "/v1/stats", useCfg(cfg, v1.Stats))
|
mux.HandleFunc("GET", "/v1/stats", useCfg(cfg, v1.Stats))
|
||||||
mux.HandleFunc("GET", "/v1/stats/ws", useCfg(cfg, v1.StatsWS))
|
mux.HandleFunc("GET", "/v1/stats/ws", useCfg(cfg, v1.StatsWS))
|
||||||
|
mux.HandleFunc("GET", "/v1/health/ws", useCfg(cfg, v1.HealthWS))
|
||||||
|
mux.HandleFunc("GET", "/v1/logs/ws", useCfg(cfg, v1.LogsWS()))
|
||||||
mux.HandleFunc("GET", "/v1/favicon/{alias}", auth.RequireAuth(favicon.GetFavIcon))
|
mux.HandleFunc("GET", "/v1/favicon/{alias}", auth.RequireAuth(favicon.GetFavIcon))
|
||||||
|
|
||||||
defaultAuth := auth.GetDefaultAuth()
|
defaultAuth := auth.GetDefaultAuth()
|
||||||
|
|
18
internal/api/v1/health.go
Normal file
18
internal/api/v1/health.go
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
package v1
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/coder/websocket"
|
||||||
|
"github.com/coder/websocket/wsjson"
|
||||||
|
U "github.com/yusing/go-proxy/internal/api/v1/utils"
|
||||||
|
config "github.com/yusing/go-proxy/internal/config/types"
|
||||||
|
"github.com/yusing/go-proxy/internal/route/routes"
|
||||||
|
)
|
||||||
|
|
||||||
|
func HealthWS(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Request) {
|
||||||
|
U.PeriodicWS(cfg, w, r, 1*time.Second, func(conn *websocket.Conn) error {
|
||||||
|
return wsjson.Write(r.Context(), conn, routes.HealthMap())
|
||||||
|
})
|
||||||
|
}
|
161
internal/api/v1/mem_logger.go
Normal file
161
internal/api/v1/mem_logger.go
Normal file
|
@ -0,0 +1,161 @@
|
||||||
|
package v1
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/coder/websocket"
|
||||||
|
"github.com/yusing/go-proxy/internal/api/v1/utils"
|
||||||
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
|
config "github.com/yusing/go-proxy/internal/config/types"
|
||||||
|
"github.com/yusing/go-proxy/internal/logging"
|
||||||
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
|
F "github.com/yusing/go-proxy/internal/utils/functional"
|
||||||
|
)
|
||||||
|
|
||||||
|
type logEntryRange struct {
|
||||||
|
Start, End int
|
||||||
|
}
|
||||||
|
|
||||||
|
type memLogger struct {
|
||||||
|
bytes.Buffer
|
||||||
|
sync.Mutex
|
||||||
|
connChans F.Map[chan *logEntryRange, struct{}]
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
maxMemLogSize = 16 * 1024
|
||||||
|
truncateSize = maxMemLogSize / 2
|
||||||
|
initialWriteChunkSize = 4 * 1024
|
||||||
|
)
|
||||||
|
|
||||||
|
var memLoggerInstance = &memLogger{
|
||||||
|
connChans: F.NewMapOf[chan *logEntryRange, struct{}](),
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
if !common.EnableLogStreaming {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
memLoggerInstance.Grow(maxMemLogSize)
|
||||||
|
|
||||||
|
if common.DebugMemLogger {
|
||||||
|
ticker := time.NewTicker(1 * time.Second)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-task.RootContextCanceled():
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
logging.Info().Msgf("mem logger size: %d, active conns: %d",
|
||||||
|
memLoggerInstance.Len(),
|
||||||
|
memLoggerInstance.connChans.Size())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func LogsWS() func(config config.ConfigInstance, w http.ResponseWriter, r *http.Request) {
|
||||||
|
return memLoggerInstance.ServeHTTP
|
||||||
|
}
|
||||||
|
|
||||||
|
func MemLogger() io.Writer {
|
||||||
|
return memLoggerInstance
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *memLogger) Write(p []byte) (n int, err error) {
|
||||||
|
m.Lock()
|
||||||
|
|
||||||
|
if m.Len() > maxMemLogSize {
|
||||||
|
m.Truncate(truncateSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
pos := m.Buffer.Len()
|
||||||
|
n = len(p)
|
||||||
|
_, err = m.Buffer.Write(p)
|
||||||
|
if err != nil {
|
||||||
|
m.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if m.connChans.Size() > 0 {
|
||||||
|
m.Unlock()
|
||||||
|
timeout := time.NewTimer(1 * time.Second)
|
||||||
|
defer timeout.Stop()
|
||||||
|
|
||||||
|
m.connChans.Range(func(ch chan *logEntryRange, _ struct{}) bool {
|
||||||
|
select {
|
||||||
|
case ch <- &logEntryRange{pos, pos + n}:
|
||||||
|
return true
|
||||||
|
case <-timeout.C:
|
||||||
|
logging.Warn().Msg("mem logger: timeout logging to channel")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
m.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *memLogger) ServeHTTP(config config.ConfigInstance, w http.ResponseWriter, r *http.Request) {
|
||||||
|
conn, err := utils.InitiateWS(config, w, r)
|
||||||
|
if err != nil {
|
||||||
|
utils.HandleErr(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logCh := make(chan *logEntryRange)
|
||||||
|
m.connChans.Store(logCh, struct{}{})
|
||||||
|
|
||||||
|
/* trunk-ignore(golangci-lint/errcheck) */
|
||||||
|
defer func() {
|
||||||
|
_ = conn.CloseNow()
|
||||||
|
m.connChans.Delete(logCh)
|
||||||
|
close(logCh)
|
||||||
|
}()
|
||||||
|
|
||||||
|
if err := m.wsInitial(r.Context(), conn); err != nil {
|
||||||
|
utils.HandleErr(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
m.wsStreamLog(r.Context(), conn, logCh)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *memLogger) writeBytes(ctx context.Context, conn *websocket.Conn, b []byte) error {
|
||||||
|
return conn.Write(ctx, websocket.MessageText, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *memLogger) wsInitial(ctx context.Context, conn *websocket.Conn) error {
|
||||||
|
m.Lock()
|
||||||
|
defer m.Unlock()
|
||||||
|
|
||||||
|
return m.writeBytes(ctx, conn, m.Buffer.Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *memLogger) wsStreamLog(ctx context.Context, conn *websocket.Conn, ch <-chan *logEntryRange) {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
case logRange := <-ch:
|
||||||
|
m.Lock()
|
||||||
|
msg := m.Buffer.Bytes()[logRange.Start:logRange.End]
|
||||||
|
err := m.writeBytes(ctx, conn, msg)
|
||||||
|
m.Unlock()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -14,7 +14,7 @@ func GetSchemaFile(w http.ResponseWriter, r *http.Request) {
|
||||||
if filename == "" {
|
if filename == "" {
|
||||||
U.RespondError(w, U.ErrMissingKey("filename"), http.StatusBadRequest)
|
U.RespondError(w, U.ErrMissingKey("filename"), http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
content, err := os.ReadFile(path.Join(common.SchemaBasePath, filename))
|
content, err := os.ReadFile(path.Join(common.SchemasBasePath, filename))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
U.HandleErr(w, r, err)
|
U.HandleErr(w, r, err)
|
||||||
return
|
return
|
||||||
|
|
|
@ -1,14 +1,12 @@
|
||||||
package v1
|
package v1
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/coder/websocket"
|
"github.com/coder/websocket"
|
||||||
"github.com/coder/websocket/wsjson"
|
"github.com/coder/websocket/wsjson"
|
||||||
U "github.com/yusing/go-proxy/internal/api/v1/utils"
|
U "github.com/yusing/go-proxy/internal/api/v1/utils"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
|
||||||
config "github.com/yusing/go-proxy/internal/config/types"
|
config "github.com/yusing/go-proxy/internal/config/types"
|
||||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||||
)
|
)
|
||||||
|
@ -18,46 +16,9 @@ func Stats(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func StatsWS(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Request) {
|
func StatsWS(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Request) {
|
||||||
var originPats []string
|
U.PeriodicWS(cfg, w, r, 1*time.Second, func(conn *websocket.Conn) error {
|
||||||
|
return wsjson.Write(r.Context(), conn, getStats(cfg))
|
||||||
localAddresses := []string{"127.0.0.1", "10.0.*.*", "172.16.*.*", "192.168.*.*"}
|
|
||||||
|
|
||||||
if len(cfg.Value().MatchDomains) == 0 {
|
|
||||||
U.LogWarn(r).Msg("no match domains configured, accepting websocket API request from all origins")
|
|
||||||
originPats = []string{"*"}
|
|
||||||
} else {
|
|
||||||
originPats = make([]string, len(cfg.Value().MatchDomains))
|
|
||||||
for i, domain := range cfg.Value().MatchDomains {
|
|
||||||
originPats[i] = "*" + domain
|
|
||||||
}
|
|
||||||
originPats = append(originPats, localAddresses...)
|
|
||||||
}
|
|
||||||
if common.IsDebug {
|
|
||||||
originPats = []string{"*"}
|
|
||||||
}
|
|
||||||
conn, err := websocket.Accept(w, r, &websocket.AcceptOptions{
|
|
||||||
OriginPatterns: originPats,
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
U.LogError(r).Err(err).Msg("failed to upgrade websocket")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
/* trunk-ignore(golangci-lint/errcheck) */
|
|
||||||
defer conn.CloseNow()
|
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
ticker := time.NewTicker(1 * time.Second)
|
|
||||||
defer ticker.Stop()
|
|
||||||
|
|
||||||
for range ticker.C {
|
|
||||||
stats := getStats(cfg)
|
|
||||||
if err := wsjson.Write(ctx, conn, stats); err != nil {
|
|
||||||
U.LogError(r).Msg("failed to write JSON")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var startTime = time.Now()
|
var startTime = time.Now()
|
||||||
|
|
68
internal/api/v1/utils/ws.go
Normal file
68
internal/api/v1/utils/ws.go
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/coder/websocket"
|
||||||
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
|
config "github.com/yusing/go-proxy/internal/config/types"
|
||||||
|
"github.com/yusing/go-proxy/internal/logging"
|
||||||
|
)
|
||||||
|
|
||||||
|
func warnNoMatchDomains() {
|
||||||
|
logging.Warn().Msg("no match domains configured, accepting websocket API request from all origins")
|
||||||
|
}
|
||||||
|
|
||||||
|
var warnNoMatchDomainOnce sync.Once
|
||||||
|
|
||||||
|
func InitiateWS(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Request) (*websocket.Conn, error) {
|
||||||
|
var originPats []string
|
||||||
|
|
||||||
|
localAddresses := []string{"127.0.0.1", "10.0.*.*", "172.16.*.*", "192.168.*.*"}
|
||||||
|
|
||||||
|
if len(cfg.Value().MatchDomains) == 0 {
|
||||||
|
warnNoMatchDomainOnce.Do(warnNoMatchDomains)
|
||||||
|
originPats = []string{"*"}
|
||||||
|
} else {
|
||||||
|
originPats = make([]string, len(cfg.Value().MatchDomains))
|
||||||
|
for i, domain := range cfg.Value().MatchDomains {
|
||||||
|
originPats[i] = "*" + domain
|
||||||
|
}
|
||||||
|
originPats = append(originPats, localAddresses...)
|
||||||
|
}
|
||||||
|
if common.IsDebug {
|
||||||
|
originPats = []string{"*"}
|
||||||
|
}
|
||||||
|
return websocket.Accept(w, r, &websocket.AcceptOptions{
|
||||||
|
OriginPatterns: originPats,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func PeriodicWS(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Request, interval time.Duration, do func(conn *websocket.Conn) error) {
|
||||||
|
conn, err := InitiateWS(cfg, w, r)
|
||||||
|
if err != nil {
|
||||||
|
HandleErr(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
/* trunk-ignore(golangci-lint/errcheck) */
|
||||||
|
defer conn.CloseNow()
|
||||||
|
|
||||||
|
ticker := time.NewTicker(interval)
|
||||||
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-cfg.Context().Done():
|
||||||
|
return
|
||||||
|
case <-r.Context().Done():
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
if err := do(conn); err != nil {
|
||||||
|
HandleErr(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -25,9 +25,9 @@ const (
|
||||||
|
|
||||||
MiddlewareComposeBasePath = ConfigBasePath + "/middlewares"
|
MiddlewareComposeBasePath = ConfigBasePath + "/middlewares"
|
||||||
|
|
||||||
SchemaBasePath = "schema"
|
SchemasBasePath = "schemas"
|
||||||
ConfigSchemaPath = SchemaBasePath + "/config.schema.json"
|
ConfigSchemaPath = SchemasBasePath + "/config.schema.json"
|
||||||
FileProviderSchemaPath = SchemaBasePath + "/providers.schema.json"
|
FileProviderSchemaPath = SchemasBasePath + "/providers.schema.json"
|
||||||
|
|
||||||
ComposeFileName = "compose.yml"
|
ComposeFileName = "compose.yml"
|
||||||
ComposeExampleFileName = "compose.example.yml"
|
ComposeExampleFileName = "compose.example.yml"
|
||||||
|
@ -37,7 +37,7 @@ const (
|
||||||
|
|
||||||
var RequiredDirectories = []string{
|
var RequiredDirectories = []string{
|
||||||
ConfigBasePath,
|
ConfigBasePath,
|
||||||
SchemaBasePath,
|
SchemasBasePath,
|
||||||
ErrorPagesBasePath,
|
ErrorPagesBasePath,
|
||||||
MiddlewareComposeBasePath,
|
MiddlewareComposeBasePath,
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,7 @@ const (
|
||||||
HealthCheckTimeoutDefault = 5 * time.Second
|
HealthCheckTimeoutDefault = 5 * time.Second
|
||||||
|
|
||||||
WakeTimeoutDefault = "30s"
|
WakeTimeoutDefault = "30s"
|
||||||
StopTimeoutDefault = "10s"
|
StopTimeoutDefault = "30s"
|
||||||
StopMethodDefault = "stop"
|
StopMethodDefault = "stop"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,9 @@ var (
|
||||||
IsTrace = GetEnvBool("TRACE", false) && IsDebug
|
IsTrace = GetEnvBool("TRACE", false) && IsDebug
|
||||||
IsProduction = !IsTest && !IsDebug
|
IsProduction = !IsTest && !IsDebug
|
||||||
|
|
||||||
|
EnableLogStreaming = GetEnvBool("LOG_STREAMING", true)
|
||||||
|
DebugMemLogger = GetEnvBool("DEBUG_MEM_LOGGER", false) && EnableLogStreaming
|
||||||
|
|
||||||
ProxyHTTPAddr,
|
ProxyHTTPAddr,
|
||||||
ProxyHTTPHost,
|
ProxyHTTPHost,
|
||||||
ProxyHTTPPort,
|
ProxyHTTPPort,
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -146,6 +147,10 @@ func (cfg *Config) Task() *task.Task {
|
||||||
return cfg.task
|
return cfg.task
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cfg *Config) Context() context.Context {
|
||||||
|
return cfg.task.Context()
|
||||||
|
}
|
||||||
|
|
||||||
func (cfg *Config) Start() {
|
func (cfg *Config) Start() {
|
||||||
cfg.StartAutoCert()
|
cfg.StartAutoCert()
|
||||||
cfg.StartProxyProviders()
|
cfg.StartProxyProviders()
|
||||||
|
|
|
@ -25,21 +25,22 @@ func (cfg *Config) DumpProviders() map[string]*provider.Provider {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cfg *Config) Statistics() map[string]any {
|
func (cfg *Config) Statistics() map[string]any {
|
||||||
nTotalStreams := 0
|
var rps, streams provider.RouteStats
|
||||||
nTotalRPs := 0
|
var total uint16
|
||||||
providerStats := make(map[string]provider.ProviderStats)
|
providerStats := make(map[string]provider.ProviderStats)
|
||||||
|
|
||||||
cfg.providers.RangeAll(func(_ string, p *provider.Provider) {
|
cfg.providers.RangeAll(func(_ string, p *provider.Provider) {
|
||||||
stats := p.Statistics()
|
stats := p.Statistics()
|
||||||
providerStats[p.ShortName()] = stats
|
providerStats[p.ShortName()] = stats
|
||||||
|
rps.AddOther(stats.RPs)
|
||||||
nTotalRPs += stats.NumRPs
|
streams.AddOther(stats.Streams)
|
||||||
nTotalStreams += stats.NumStreams
|
total += stats.RPs.Total + stats.Streams.Total
|
||||||
})
|
})
|
||||||
|
|
||||||
return map[string]any{
|
return map[string]any{
|
||||||
"num_total_streams": nTotalStreams,
|
"total": total,
|
||||||
"num_total_reverse_proxies": nTotalRPs,
|
"reverse_proxies": rps,
|
||||||
"providers": providerStats,
|
"streams": streams,
|
||||||
|
"providers": providerStats,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
"github.com/yusing/go-proxy/internal/net/http/accesslog"
|
"github.com/yusing/go-proxy/internal/net/http/accesslog"
|
||||||
"github.com/yusing/go-proxy/internal/utils"
|
"github.com/yusing/go-proxy/internal/utils"
|
||||||
|
|
||||||
|
@ -31,6 +33,7 @@ type (
|
||||||
Value() *Config
|
Value() *Config
|
||||||
Reload() E.Error
|
Reload() E.Error
|
||||||
Statistics() map[string]any
|
Statistics() map[string]any
|
||||||
|
Context() context.Context
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
43
internal/docker/container_test.go
Normal file
43
internal/docker/container_test.go
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
package docker
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/docker/docker/api/types"
|
||||||
|
. "github.com/yusing/go-proxy/internal/utils/testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestContainerExplicit(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
labels map[string]string
|
||||||
|
isExplicit bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "explicit",
|
||||||
|
labels: map[string]string{
|
||||||
|
"proxy.aliases": "foo",
|
||||||
|
},
|
||||||
|
isExplicit: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "explicit2",
|
||||||
|
labels: map[string]string{
|
||||||
|
"proxy.idle_timeout": "1s",
|
||||||
|
},
|
||||||
|
isExplicit: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "not explicit",
|
||||||
|
labels: map[string]string{},
|
||||||
|
isExplicit: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
c := FromDocker(&types.Container{Names: []string{"test"}, State: "test", Labels: tt.labels}, "")
|
||||||
|
ExpectEqual(t, c.IsExplicit, tt.isExplicit)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -117,6 +117,11 @@ func (w *Watcher) Uptime() time.Duration {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Latency implements health.HealthMonitor.
|
||||||
|
func (w *Watcher) Latency() time.Duration {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
// Status implements health.HealthMonitor.
|
// Status implements health.HealthMonitor.
|
||||||
func (w *Watcher) Status() health.Status {
|
func (w *Watcher) Status() health.Status {
|
||||||
status := w.getStatusUpdateReady()
|
status := w.getStatusUpdateReady()
|
||||||
|
|
|
@ -294,6 +294,9 @@ func (w *Watcher) watchUntilDestroy() (returnCause error) {
|
||||||
case errors.Is(err, context.Canceled):
|
case errors.Is(err, context.Canceled):
|
||||||
continue
|
continue
|
||||||
case err != nil:
|
case err != nil:
|
||||||
|
if errors.Is(err, context.DeadlineExceeded) {
|
||||||
|
err = errors.New("timeout waiting for container to stop, please set a higher value for `stop_timeout`")
|
||||||
|
}
|
||||||
w.Err(err).Msgf("container stop with method %q failed", w.StopMethod)
|
w.Err(err).Msgf("container stop with method %q failed", w.StopMethod)
|
||||||
default:
|
default:
|
||||||
w.LogReason("container stopped", "idle timeout")
|
w.LogReason("container stopped", "idle timeout")
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
package logging
|
package logging
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
@ -12,7 +12,7 @@ import (
|
||||||
|
|
||||||
var logger zerolog.Logger
|
var logger zerolog.Logger
|
||||||
|
|
||||||
func init() {
|
func InitLogger(out io.Writer) {
|
||||||
var timeFmt string
|
var timeFmt string
|
||||||
var level zerolog.Level
|
var level zerolog.Level
|
||||||
var exclude []string
|
var exclude []string
|
||||||
|
@ -35,7 +35,7 @@ func init() {
|
||||||
|
|
||||||
logger = zerolog.New(
|
logger = zerolog.New(
|
||||||
zerolog.ConsoleWriter{
|
zerolog.ConsoleWriter{
|
||||||
Out: os.Stderr,
|
Out: out,
|
||||||
TimeFormat: timeFmt,
|
TimeFormat: timeFmt,
|
||||||
FieldsExclude: exclude,
|
FieldsExclude: exclude,
|
||||||
FormatMessage: func(msgI interface{}) string { // pad spaces for each line
|
FormatMessage: func(msgI interface{}) string { // pad spaces for each line
|
||||||
|
|
|
@ -31,7 +31,7 @@ func (lb *LoadBalancer) newIPHash() impl {
|
||||||
return impl
|
return impl
|
||||||
}
|
}
|
||||||
|
|
||||||
func (impl *ipHash) OnAddServer(srv *Server) {
|
func (impl *ipHash) OnAddServer(srv Server) {
|
||||||
impl.mu.Lock()
|
impl.mu.Lock()
|
||||||
defer impl.mu.Unlock()
|
defer impl.mu.Unlock()
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ func (impl *ipHash) OnAddServer(srv *Server) {
|
||||||
impl.pool = append(impl.pool, srv)
|
impl.pool = append(impl.pool, srv)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (impl *ipHash) OnRemoveServer(srv *Server) {
|
func (impl *ipHash) OnRemoveServer(srv Server) {
|
||||||
impl.mu.Lock()
|
impl.mu.Lock()
|
||||||
defer impl.mu.Unlock()
|
defer impl.mu.Unlock()
|
||||||
|
|
||||||
|
|
|
@ -9,21 +9,21 @@ import (
|
||||||
|
|
||||||
type leastConn struct {
|
type leastConn struct {
|
||||||
*LoadBalancer
|
*LoadBalancer
|
||||||
nConn F.Map[*Server, *atomic.Int64]
|
nConn F.Map[Server, *atomic.Int64]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lb *LoadBalancer) newLeastConn() impl {
|
func (lb *LoadBalancer) newLeastConn() impl {
|
||||||
return &leastConn{
|
return &leastConn{
|
||||||
LoadBalancer: lb,
|
LoadBalancer: lb,
|
||||||
nConn: F.NewMapOf[*Server, *atomic.Int64](),
|
nConn: F.NewMapOf[Server, *atomic.Int64](),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (impl *leastConn) OnAddServer(srv *Server) {
|
func (impl *leastConn) OnAddServer(srv Server) {
|
||||||
impl.nConn.Store(srv, new(atomic.Int64))
|
impl.nConn.Store(srv, new(atomic.Int64))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (impl *leastConn) OnRemoveServer(srv *Server) {
|
func (impl *leastConn) OnRemoveServer(srv Server) {
|
||||||
impl.nConn.Delete(srv)
|
impl.nConn.Delete(srv)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,14 +31,14 @@ func (impl *leastConn) ServeHTTP(srvs Servers, rw http.ResponseWriter, r *http.R
|
||||||
srv := srvs[0]
|
srv := srvs[0]
|
||||||
minConn, ok := impl.nConn.Load(srv)
|
minConn, ok := impl.nConn.Load(srv)
|
||||||
if !ok {
|
if !ok {
|
||||||
impl.l.Error().Msgf("[BUG] server %s not found", srv.Name)
|
impl.l.Error().Msgf("[BUG] server %s not found", srv.Name())
|
||||||
http.Error(rw, "Internal error", http.StatusInternalServerError)
|
http.Error(rw, "Internal error", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 1; i < len(srvs); i++ {
|
for i := 1; i < len(srvs); i++ {
|
||||||
nConn, ok := impl.nConn.Load(srvs[i])
|
nConn, ok := impl.nConn.Load(srvs[i])
|
||||||
if !ok {
|
if !ok {
|
||||||
impl.l.Error().Msgf("[BUG] server %s not found", srv.Name)
|
impl.l.Error().Msgf("[BUG] server %s not found", srv.Name())
|
||||||
http.Error(rw, "Internal error", http.StatusInternalServerError)
|
http.Error(rw, "Internal error", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
if nConn.Load() < minConn.Load() {
|
if nConn.Load() < minConn.Load() {
|
||||||
|
|
|
@ -20,8 +20,8 @@ import (
|
||||||
type (
|
type (
|
||||||
impl interface {
|
impl interface {
|
||||||
ServeHTTP(srvs Servers, rw http.ResponseWriter, r *http.Request)
|
ServeHTTP(srvs Servers, rw http.ResponseWriter, r *http.Request)
|
||||||
OnAddServer(srv *Server)
|
OnAddServer(srv Server)
|
||||||
OnRemoveServer(srv *Server)
|
OnRemoveServer(srv Server)
|
||||||
}
|
}
|
||||||
|
|
||||||
LoadBalancer struct {
|
LoadBalancer struct {
|
||||||
|
@ -61,7 +61,7 @@ func (lb *LoadBalancer) Start(parent task.Parent) E.Error {
|
||||||
})
|
})
|
||||||
lb.task.OnFinished("cleanup", func() {
|
lb.task.OnFinished("cleanup", func() {
|
||||||
if lb.impl != nil {
|
if lb.impl != nil {
|
||||||
lb.pool.RangeAll(func(k string, v *Server) {
|
lb.pool.RangeAll(func(k string, v Server) {
|
||||||
lb.impl.OnRemoveServer(v)
|
lb.impl.OnRemoveServer(v)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -90,7 +90,7 @@ func (lb *LoadBalancer) updateImpl() {
|
||||||
default: // should happen in test only
|
default: // should happen in test only
|
||||||
lb.impl = lb.newRoundRobin()
|
lb.impl = lb.newRoundRobin()
|
||||||
}
|
}
|
||||||
lb.pool.RangeAll(func(_ string, srv *Server) {
|
lb.pool.RangeAll(func(_ string, srv Server) {
|
||||||
lb.impl.OnAddServer(srv)
|
lb.impl.OnAddServer(srv)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -120,44 +120,44 @@ func (lb *LoadBalancer) UpdateConfigIfNeeded(cfg *Config) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lb *LoadBalancer) AddServer(srv *Server) {
|
func (lb *LoadBalancer) AddServer(srv Server) {
|
||||||
lb.poolMu.Lock()
|
lb.poolMu.Lock()
|
||||||
defer lb.poolMu.Unlock()
|
defer lb.poolMu.Unlock()
|
||||||
|
|
||||||
if lb.pool.Has(srv.Name) {
|
if lb.pool.Has(srv.Name()) {
|
||||||
old, _ := lb.pool.Load(srv.Name)
|
old, _ := lb.pool.Load(srv.Name())
|
||||||
lb.sumWeight -= old.Weight
|
lb.sumWeight -= old.Weight()
|
||||||
lb.impl.OnRemoveServer(old)
|
lb.impl.OnRemoveServer(old)
|
||||||
}
|
}
|
||||||
lb.pool.Store(srv.Name, srv)
|
lb.pool.Store(srv.Name(), srv)
|
||||||
lb.sumWeight += srv.Weight
|
lb.sumWeight += srv.Weight()
|
||||||
|
|
||||||
lb.rebalance()
|
lb.rebalance()
|
||||||
lb.impl.OnAddServer(srv)
|
lb.impl.OnAddServer(srv)
|
||||||
|
|
||||||
lb.l.Debug().
|
lb.l.Debug().
|
||||||
Str("action", "add").
|
Str("action", "add").
|
||||||
Str("server", srv.Name).
|
Str("server", srv.Name()).
|
||||||
Msgf("%d servers available", lb.pool.Size())
|
Msgf("%d servers available", lb.pool.Size())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lb *LoadBalancer) RemoveServer(srv *Server) {
|
func (lb *LoadBalancer) RemoveServer(srv Server) {
|
||||||
lb.poolMu.Lock()
|
lb.poolMu.Lock()
|
||||||
defer lb.poolMu.Unlock()
|
defer lb.poolMu.Unlock()
|
||||||
|
|
||||||
if !lb.pool.Has(srv.Name) {
|
if !lb.pool.Has(srv.Name()) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
lb.pool.Delete(srv.Name)
|
lb.pool.Delete(srv.Name())
|
||||||
|
|
||||||
lb.sumWeight -= srv.Weight
|
lb.sumWeight -= srv.Weight()
|
||||||
lb.rebalance()
|
lb.rebalance()
|
||||||
lb.impl.OnRemoveServer(srv)
|
lb.impl.OnRemoveServer(srv)
|
||||||
|
|
||||||
lb.l.Debug().
|
lb.l.Debug().
|
||||||
Str("action", "remove").
|
Str("action", "remove").
|
||||||
Str("server", srv.Name).
|
Str("server", srv.Name()).
|
||||||
Msgf("%d servers left", lb.pool.Size())
|
Msgf("%d servers left", lb.pool.Size())
|
||||||
|
|
||||||
if lb.pool.Size() == 0 {
|
if lb.pool.Size() == 0 {
|
||||||
|
@ -178,13 +178,14 @@ func (lb *LoadBalancer) rebalance() {
|
||||||
if lb.sumWeight == 0 { // distribute evenly
|
if lb.sumWeight == 0 { // distribute evenly
|
||||||
weightEach := maxWeight / Weight(poolSize)
|
weightEach := maxWeight / Weight(poolSize)
|
||||||
remainder := maxWeight % Weight(poolSize)
|
remainder := maxWeight % Weight(poolSize)
|
||||||
lb.pool.RangeAll(func(_ string, s *Server) {
|
lb.pool.RangeAll(func(_ string, s Server) {
|
||||||
s.Weight = weightEach
|
w := weightEach
|
||||||
lb.sumWeight += weightEach
|
lb.sumWeight += weightEach
|
||||||
if remainder > 0 {
|
if remainder > 0 {
|
||||||
s.Weight++
|
w++
|
||||||
remainder--
|
remainder--
|
||||||
}
|
}
|
||||||
|
s.SetWeight(w)
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -193,25 +194,25 @@ func (lb *LoadBalancer) rebalance() {
|
||||||
scaleFactor := float64(maxWeight) / float64(lb.sumWeight)
|
scaleFactor := float64(maxWeight) / float64(lb.sumWeight)
|
||||||
lb.sumWeight = 0
|
lb.sumWeight = 0
|
||||||
|
|
||||||
lb.pool.RangeAll(func(_ string, s *Server) {
|
lb.pool.RangeAll(func(_ string, s Server) {
|
||||||
s.Weight = Weight(float64(s.Weight) * scaleFactor)
|
s.SetWeight(Weight(float64(s.Weight()) * scaleFactor))
|
||||||
lb.sumWeight += s.Weight
|
lb.sumWeight += s.Weight()
|
||||||
})
|
})
|
||||||
|
|
||||||
delta := maxWeight - lb.sumWeight
|
delta := maxWeight - lb.sumWeight
|
||||||
if delta == 0 {
|
if delta == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
lb.pool.Range(func(_ string, s *Server) bool {
|
lb.pool.Range(func(_ string, s Server) bool {
|
||||||
if delta == 0 {
|
if delta == 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if delta > 0 {
|
if delta > 0 {
|
||||||
s.Weight++
|
s.SetWeight(s.Weight() + 1)
|
||||||
lb.sumWeight++
|
lb.sumWeight++
|
||||||
delta--
|
delta--
|
||||||
} else {
|
} else {
|
||||||
s.Weight--
|
s.SetWeight(s.Weight() - 1)
|
||||||
lb.sumWeight--
|
lb.sumWeight--
|
||||||
delta++
|
delta++
|
||||||
}
|
}
|
||||||
|
@ -229,22 +230,20 @@ func (lb *LoadBalancer) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||||
// wake all servers
|
// wake all servers
|
||||||
for _, srv := range srvs {
|
for _, srv := range srvs {
|
||||||
if err := srv.TryWake(); err != nil {
|
if err := srv.TryWake(); err != nil {
|
||||||
lb.l.Warn().Err(err).Str("server", srv.Name).Msg("failed to wake server")
|
lb.l.Warn().Err(err).
|
||||||
|
Str("server", srv.Name()).
|
||||||
|
Msg("failed to wake server")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lb.impl.ServeHTTP(srvs, rw, r)
|
lb.impl.ServeHTTP(srvs, rw, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lb *LoadBalancer) Uptime() time.Duration {
|
|
||||||
return time.Since(lb.startTime)
|
|
||||||
}
|
|
||||||
|
|
||||||
// MarshalJSON implements health.HealthMonitor.
|
// MarshalJSON implements health.HealthMonitor.
|
||||||
func (lb *LoadBalancer) MarshalJSON() ([]byte, error) {
|
func (lb *LoadBalancer) MarshalJSON() ([]byte, error) {
|
||||||
extra := make(map[string]any)
|
extra := make(map[string]any)
|
||||||
lb.pool.RangeAll(func(k string, v *Server) {
|
lb.pool.RangeAll(func(k string, v Server) {
|
||||||
extra[v.Name] = v.HealthMonitor()
|
extra[v.Name()] = v
|
||||||
})
|
})
|
||||||
|
|
||||||
return (&monitor.JSONRepresentation{
|
return (&monitor.JSONRepresentation{
|
||||||
|
@ -269,20 +268,43 @@ func (lb *LoadBalancer) Status() health.Status {
|
||||||
if lb.pool.Size() == 0 {
|
if lb.pool.Size() == 0 {
|
||||||
return health.StatusUnknown
|
return health.StatusUnknown
|
||||||
}
|
}
|
||||||
if len(lb.availServers()) == 0 {
|
|
||||||
|
isHealthy := true
|
||||||
|
lb.pool.Range(func(_ string, srv Server) bool {
|
||||||
|
if srv.Status().Bad() {
|
||||||
|
isHealthy = false
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
if !isHealthy {
|
||||||
return health.StatusUnhealthy
|
return health.StatusUnhealthy
|
||||||
}
|
}
|
||||||
return health.StatusHealthy
|
return health.StatusHealthy
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Uptime implements health.HealthMonitor.
|
||||||
|
func (lb *LoadBalancer) Uptime() time.Duration {
|
||||||
|
return time.Since(lb.startTime)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Latency implements health.HealthMonitor.
|
||||||
|
func (lb *LoadBalancer) Latency() time.Duration {
|
||||||
|
var sum time.Duration
|
||||||
|
lb.pool.RangeAll(func(_ string, srv Server) {
|
||||||
|
sum += srv.Latency()
|
||||||
|
})
|
||||||
|
return sum
|
||||||
|
}
|
||||||
|
|
||||||
// String implements health.HealthMonitor.
|
// String implements health.HealthMonitor.
|
||||||
func (lb *LoadBalancer) String() string {
|
func (lb *LoadBalancer) String() string {
|
||||||
return lb.Name()
|
return lb.Name()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lb *LoadBalancer) availServers() []*Server {
|
func (lb *LoadBalancer) availServers() []Server {
|
||||||
avail := make([]*Server, 0, lb.pool.Size())
|
avail := make([]Server, 0, lb.pool.Size())
|
||||||
lb.pool.RangeAll(func(_ string, srv *Server) {
|
lb.pool.RangeAll(func(_ string, srv Server) {
|
||||||
if srv.Status().Good() {
|
if srv.Status().Good() {
|
||||||
avail = append(avail, srv)
|
avail = append(avail, srv)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package loadbalancer
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/yusing/go-proxy/internal/net/http/loadbalancer/types"
|
||||||
loadbalance "github.com/yusing/go-proxy/internal/net/http/loadbalancer/types"
|
loadbalance "github.com/yusing/go-proxy/internal/net/http/loadbalancer/types"
|
||||||
. "github.com/yusing/go-proxy/internal/utils/testing"
|
. "github.com/yusing/go-proxy/internal/utils/testing"
|
||||||
)
|
)
|
||||||
|
@ -12,31 +13,31 @@ func TestRebalance(t *testing.T) {
|
||||||
t.Run("zero", func(t *testing.T) {
|
t.Run("zero", func(t *testing.T) {
|
||||||
lb := New(new(loadbalance.Config))
|
lb := New(new(loadbalance.Config))
|
||||||
for range 10 {
|
for range 10 {
|
||||||
lb.AddServer(&Server{})
|
lb.AddServer(types.TestNewServer(0))
|
||||||
}
|
}
|
||||||
lb.rebalance()
|
lb.rebalance()
|
||||||
ExpectEqual(t, lb.sumWeight, maxWeight)
|
ExpectEqual(t, lb.sumWeight, maxWeight)
|
||||||
})
|
})
|
||||||
t.Run("less", func(t *testing.T) {
|
t.Run("less", func(t *testing.T) {
|
||||||
lb := New(new(loadbalance.Config))
|
lb := New(new(loadbalance.Config))
|
||||||
lb.AddServer(&Server{Weight: loadbalance.Weight(float64(maxWeight) * .1)})
|
lb.AddServer(types.TestNewServer(float64(maxWeight) * .1))
|
||||||
lb.AddServer(&Server{Weight: loadbalance.Weight(float64(maxWeight) * .2)})
|
lb.AddServer(types.TestNewServer(float64(maxWeight) * .2))
|
||||||
lb.AddServer(&Server{Weight: loadbalance.Weight(float64(maxWeight) * .3)})
|
lb.AddServer(types.TestNewServer(float64(maxWeight) * .3))
|
||||||
lb.AddServer(&Server{Weight: loadbalance.Weight(float64(maxWeight) * .2)})
|
lb.AddServer(types.TestNewServer(float64(maxWeight) * .2))
|
||||||
lb.AddServer(&Server{Weight: loadbalance.Weight(float64(maxWeight) * .1)})
|
lb.AddServer(types.TestNewServer(float64(maxWeight) * .1))
|
||||||
lb.rebalance()
|
lb.rebalance()
|
||||||
// t.Logf("%s", U.Must(json.MarshalIndent(lb.pool, "", " ")))
|
// t.Logf("%s", U.Must(json.MarshalIndent(lb.pool, "", " ")))
|
||||||
ExpectEqual(t, lb.sumWeight, maxWeight)
|
ExpectEqual(t, lb.sumWeight, maxWeight)
|
||||||
})
|
})
|
||||||
t.Run("more", func(t *testing.T) {
|
t.Run("more", func(t *testing.T) {
|
||||||
lb := New(new(loadbalance.Config))
|
lb := New(new(loadbalance.Config))
|
||||||
lb.AddServer(&Server{Weight: loadbalance.Weight(float64(maxWeight) * .1)})
|
lb.AddServer(types.TestNewServer(float64(maxWeight) * .1))
|
||||||
lb.AddServer(&Server{Weight: loadbalance.Weight(float64(maxWeight) * .2)})
|
lb.AddServer(types.TestNewServer(float64(maxWeight) * .2))
|
||||||
lb.AddServer(&Server{Weight: loadbalance.Weight(float64(maxWeight) * .3)})
|
lb.AddServer(types.TestNewServer(float64(maxWeight) * .3))
|
||||||
lb.AddServer(&Server{Weight: loadbalance.Weight(float64(maxWeight) * .4)})
|
lb.AddServer(types.TestNewServer(float64(maxWeight) * .4))
|
||||||
lb.AddServer(&Server{Weight: loadbalance.Weight(float64(maxWeight) * .3)})
|
lb.AddServer(types.TestNewServer(float64(maxWeight) * .3))
|
||||||
lb.AddServer(&Server{Weight: loadbalance.Weight(float64(maxWeight) * .2)})
|
lb.AddServer(types.TestNewServer(float64(maxWeight) * .2))
|
||||||
lb.AddServer(&Server{Weight: loadbalance.Weight(float64(maxWeight) * .1)})
|
lb.AddServer(types.TestNewServer(float64(maxWeight) * .1))
|
||||||
lb.rebalance()
|
lb.rebalance()
|
||||||
// t.Logf("%s", U.Must(json.MarshalIndent(lb.pool, "", " ")))
|
// t.Logf("%s", U.Must(json.MarshalIndent(lb.pool, "", " ")))
|
||||||
ExpectEqual(t, lb.sumWeight, maxWeight)
|
ExpectEqual(t, lb.sumWeight, maxWeight)
|
||||||
|
|
|
@ -9,9 +9,9 @@ type roundRobin struct {
|
||||||
index atomic.Uint32
|
index atomic.Uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*LoadBalancer) newRoundRobin() impl { return &roundRobin{} }
|
func (*LoadBalancer) newRoundRobin() impl { return &roundRobin{} }
|
||||||
func (lb *roundRobin) OnAddServer(srv *Server) {}
|
func (lb *roundRobin) OnAddServer(srv Server) {}
|
||||||
func (lb *roundRobin) OnRemoveServer(srv *Server) {}
|
func (lb *roundRobin) OnRemoveServer(srv Server) {}
|
||||||
|
|
||||||
func (lb *roundRobin) ServeHTTP(srvs Servers, rw http.ResponseWriter, r *http.Request) {
|
func (lb *roundRobin) ServeHTTP(srvs Servers, rw http.ResponseWriter, r *http.Request) {
|
||||||
index := lb.index.Add(1) % uint32(len(srvs))
|
index := lb.index.Add(1) % uint32(len(srvs))
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
|
|
||||||
type (
|
type (
|
||||||
Server = types.Server
|
Server = types.Server
|
||||||
Servers = types.Servers
|
Servers = []types.Server
|
||||||
Pool = types.Pool
|
Pool = types.Pool
|
||||||
Weight = types.Weight
|
Weight = types.Weight
|
||||||
Config = types.Config
|
Config = types.Config
|
||||||
|
|
|
@ -2,7 +2,6 @@ package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
|
|
||||||
idlewatcher "github.com/yusing/go-proxy/internal/docker/idlewatcher/types"
|
idlewatcher "github.com/yusing/go-proxy/internal/docker/idlewatcher/types"
|
||||||
"github.com/yusing/go-proxy/internal/net/types"
|
"github.com/yusing/go-proxy/internal/net/types"
|
||||||
|
@ -12,51 +11,72 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
Server struct {
|
server struct {
|
||||||
_ U.NoCopy
|
_ U.NoCopy
|
||||||
|
|
||||||
Name string
|
name string
|
||||||
URL types.URL
|
url types.URL
|
||||||
Weight Weight
|
weight Weight
|
||||||
|
|
||||||
handler http.Handler
|
http.Handler `json:"-"`
|
||||||
healthMon health.HealthMonitor
|
health.HealthMonitor
|
||||||
}
|
}
|
||||||
Servers = []*Server
|
|
||||||
Pool = F.Map[string, *Server]
|
Server interface {
|
||||||
|
http.Handler
|
||||||
|
health.HealthMonitor
|
||||||
|
Name() string
|
||||||
|
URL() types.URL
|
||||||
|
Weight() Weight
|
||||||
|
SetWeight(weight Weight)
|
||||||
|
TryWake() error
|
||||||
|
}
|
||||||
|
|
||||||
|
Pool = F.Map[string, Server]
|
||||||
)
|
)
|
||||||
|
|
||||||
var NewServerPool = F.NewMap[Pool]
|
var NewServerPool = F.NewMap[Pool]
|
||||||
|
|
||||||
func NewServer(name string, url types.URL, weight Weight, handler http.Handler, healthMon health.HealthMonitor) *Server {
|
func NewServer(name string, url types.URL, weight Weight, handler http.Handler, healthMon health.HealthMonitor) Server {
|
||||||
srv := &Server{
|
srv := &server{
|
||||||
Name: name,
|
name: name,
|
||||||
URL: url,
|
url: url,
|
||||||
Weight: weight,
|
weight: weight,
|
||||||
handler: handler,
|
Handler: handler,
|
||||||
healthMon: healthMon,
|
HealthMonitor: healthMon,
|
||||||
}
|
}
|
||||||
return srv
|
return srv
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *Server) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
func TestNewServer[T ~int | ~float32 | ~float64](weight T) Server {
|
||||||
srv.handler.ServeHTTP(rw, r)
|
srv := &server{
|
||||||
|
weight: Weight(weight),
|
||||||
|
}
|
||||||
|
return srv
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *Server) String() string {
|
func (srv *server) Name() string {
|
||||||
return srv.Name
|
return srv.name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *Server) Status() health.Status {
|
func (srv *server) URL() types.URL {
|
||||||
return srv.healthMon.Status()
|
return srv.url
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *Server) Uptime() time.Duration {
|
func (srv *server) Weight() Weight {
|
||||||
return srv.healthMon.Uptime()
|
return srv.weight
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *Server) TryWake() error {
|
func (srv *server) SetWeight(weight Weight) {
|
||||||
waker, ok := srv.handler.(idlewatcher.Waker)
|
srv.weight = weight
|
||||||
|
}
|
||||||
|
|
||||||
|
func (srv *server) String() string {
|
||||||
|
return srv.name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (srv *server) TryWake() error {
|
||||||
|
waker, ok := srv.Handler.(idlewatcher.Waker)
|
||||||
if ok {
|
if ok {
|
||||||
if err := waker.Wake(); err != nil {
|
if err := waker.Wake(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -64,7 +84,3 @@ func (srv *Server) TryWake() error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *Server) HealthMonitor() health.HealthMonitor {
|
|
||||||
return srv.healthMon
|
|
||||||
}
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ package reverseproxy
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
@ -207,13 +208,25 @@ func copyHeader(dst, src http.Header) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ReverseProxy) errorHandler(rw http.ResponseWriter, r *http.Request, err error, writeHeader bool) {
|
func (p *ReverseProxy) errorHandler(rw http.ResponseWriter, r *http.Request, err error, writeHeader bool) {
|
||||||
|
reqURL := r.Host + r.RequestURI
|
||||||
switch {
|
switch {
|
||||||
case errors.Is(err, context.Canceled),
|
case errors.Is(err, context.Canceled),
|
||||||
errors.Is(err, io.EOF):
|
errors.Is(err, io.EOF),
|
||||||
logger.Debug().Err(err).Str("url", r.URL.String()).Msg("http proxy error")
|
errors.Is(err, context.DeadlineExceeded):
|
||||||
|
logger.Debug().Err(err).Str("url", reqURL).Msg("http proxy error")
|
||||||
default:
|
default:
|
||||||
logger.Err(err).Str("url", r.URL.String()).Msg("http proxy error")
|
var recordErr tls.RecordHeaderError
|
||||||
|
if errors.As(err, &recordErr) {
|
||||||
|
logger.Error().
|
||||||
|
Str("url", reqURL).
|
||||||
|
Msgf(`scheme was likely misconfigured as https,
|
||||||
|
try setting "proxy.%s.scheme" back to "http"`, p.TargetName)
|
||||||
|
logging.Err(err).Msg("underlying error")
|
||||||
|
} else {
|
||||||
|
logger.Err(err).Str("url", reqURL).Msg("http proxy error")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if writeHeader {
|
if writeHeader {
|
||||||
rw.WriteHeader(http.StatusInternalServerError)
|
rw.WriteHeader(http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ type (
|
||||||
HealthMon health.HealthMonitor `json:"health,omitempty"`
|
HealthMon health.HealthMonitor `json:"health,omitempty"`
|
||||||
|
|
||||||
loadBalancer *loadbalancer.LoadBalancer
|
loadBalancer *loadbalancer.LoadBalancer
|
||||||
server *loadbalancer.Server
|
server loadbalancer.Server
|
||||||
handler http.Handler
|
handler http.Handler
|
||||||
rp *reverseproxy.ReverseProxy
|
rp *reverseproxy.ReverseProxy
|
||||||
|
|
||||||
|
@ -180,6 +180,10 @@ func (r *HTTPRoute) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
r.handler.ServeHTTP(w, req)
|
r.handler.ServeHTTP(w, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *HTTPRoute) HealthMonitor() health.HealthMonitor {
|
||||||
|
return r.HealthMon
|
||||||
|
}
|
||||||
|
|
||||||
func (r *HTTPRoute) addToLoadBalancer(parent task.Parent) {
|
func (r *HTTPRoute) addToLoadBalancer(parent task.Parent) {
|
||||||
var lb *loadbalancer.LoadBalancer
|
var lb *loadbalancer.LoadBalancer
|
||||||
cfg := r.Raw.LoadBalance
|
cfg := r.Raw.LoadBalance
|
||||||
|
|
|
@ -2,6 +2,7 @@ example: # matching `example.y.z`
|
||||||
scheme: http
|
scheme: http
|
||||||
host: 10.0.0.254
|
host: 10.0.0.254
|
||||||
port: 80
|
port: 80
|
||||||
|
no_tls_verify: true
|
||||||
path_patterns: # Check https://pkg.go.dev/net/http#hdr-Patterns-ServeMux for syntax
|
path_patterns: # Check https://pkg.go.dev/net/http#hdr-Patterns-ServeMux for syntax
|
||||||
- GET / # accept any GET request
|
- GET / # accept any GET request
|
||||||
- POST /auth # for /auth and /auth/* accept only POST
|
- POST /auth # for /auth and /auth/* accept only POST
|
||||||
|
|
|
@ -10,7 +10,6 @@ import (
|
||||||
E "github.com/yusing/go-proxy/internal/error"
|
E "github.com/yusing/go-proxy/internal/error"
|
||||||
R "github.com/yusing/go-proxy/internal/route"
|
R "github.com/yusing/go-proxy/internal/route"
|
||||||
"github.com/yusing/go-proxy/internal/route/provider/types"
|
"github.com/yusing/go-proxy/internal/route/provider/types"
|
||||||
route "github.com/yusing/go-proxy/internal/route/types"
|
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
W "github.com/yusing/go-proxy/internal/watcher"
|
W "github.com/yusing/go-proxy/internal/watcher"
|
||||||
"github.com/yusing/go-proxy/internal/watcher/events"
|
"github.com/yusing/go-proxy/internal/watcher/events"
|
||||||
|
@ -33,11 +32,6 @@ type (
|
||||||
NewWatcher() W.Watcher
|
NewWatcher() W.Watcher
|
||||||
Logger() *zerolog.Logger
|
Logger() *zerolog.Logger
|
||||||
}
|
}
|
||||||
ProviderStats struct {
|
|
||||||
NumRPs int `json:"num_reverse_proxies"`
|
|
||||||
NumStreams int `json:"num_streams"`
|
|
||||||
Type types.ProviderType `json:"type"`
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -154,21 +148,3 @@ func (p *Provider) LoadRoutes() E.Error {
|
||||||
func (p *Provider) NumRoutes() int {
|
func (p *Provider) NumRoutes() int {
|
||||||
return p.routes.Size()
|
return p.routes.Size()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Provider) Statistics() ProviderStats {
|
|
||||||
numRPs := 0
|
|
||||||
numStreams := 0
|
|
||||||
p.routes.RangeAll(func(_ string, r *R.Route) {
|
|
||||||
switch r.Type {
|
|
||||||
case route.RouteTypeReverseProxy:
|
|
||||||
numRPs++
|
|
||||||
case route.RouteTypeStream:
|
|
||||||
numStreams++
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return ProviderStats{
|
|
||||||
NumRPs: numRPs,
|
|
||||||
NumStreams: numStreams,
|
|
||||||
Type: p.t,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
73
internal/route/provider/stats.go
Normal file
73
internal/route/provider/stats.go
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
package provider
|
||||||
|
|
||||||
|
import (
|
||||||
|
R "github.com/yusing/go-proxy/internal/route"
|
||||||
|
"github.com/yusing/go-proxy/internal/route/provider/types"
|
||||||
|
route "github.com/yusing/go-proxy/internal/route/types"
|
||||||
|
"github.com/yusing/go-proxy/internal/watcher/health"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
RouteStats struct {
|
||||||
|
Total uint16 `json:"total"`
|
||||||
|
NumHealthy uint16 `json:"healthy"`
|
||||||
|
NumUnhealthy uint16 `json:"unhealthy"`
|
||||||
|
NumNapping uint16 `json:"napping"`
|
||||||
|
NumError uint16 `json:"error"`
|
||||||
|
NumUnknown uint16 `json:"unknown"`
|
||||||
|
}
|
||||||
|
ProviderStats struct {
|
||||||
|
Total uint16 `json:"total"`
|
||||||
|
RPs RouteStats `json:"reverse_proxies"`
|
||||||
|
Streams RouteStats `json:"streams"`
|
||||||
|
Type types.ProviderType `json:"type"`
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func (stats *RouteStats) Add(r *R.Route) {
|
||||||
|
stats.Total++
|
||||||
|
mon := r.HealthMonitor()
|
||||||
|
if mon == nil {
|
||||||
|
stats.NumUnknown++
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch mon.Status() {
|
||||||
|
case health.StatusHealthy:
|
||||||
|
stats.NumHealthy++
|
||||||
|
case health.StatusUnhealthy:
|
||||||
|
stats.NumUnhealthy++
|
||||||
|
case health.StatusNapping:
|
||||||
|
stats.NumNapping++
|
||||||
|
case health.StatusError:
|
||||||
|
stats.NumError++
|
||||||
|
default:
|
||||||
|
stats.NumUnknown++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (stats *RouteStats) AddOther(other RouteStats) {
|
||||||
|
stats.Total += other.Total
|
||||||
|
stats.NumHealthy += other.NumHealthy
|
||||||
|
stats.NumUnhealthy += other.NumUnhealthy
|
||||||
|
stats.NumNapping += other.NumNapping
|
||||||
|
stats.NumError += other.NumError
|
||||||
|
stats.NumUnknown += other.NumUnknown
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Provider) Statistics() ProviderStats {
|
||||||
|
var rps, streams RouteStats
|
||||||
|
p.routes.RangeAll(func(_ string, r *R.Route) {
|
||||||
|
switch r.Type {
|
||||||
|
case route.RouteTypeReverseProxy:
|
||||||
|
rps.Add(r)
|
||||||
|
case route.RouteTypeStream:
|
||||||
|
streams.Add(r)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return ProviderStats{
|
||||||
|
Total: rps.Total + streams.Total,
|
||||||
|
RPs: rps,
|
||||||
|
Streams: streams,
|
||||||
|
Type: p.t,
|
||||||
|
}
|
||||||
|
}
|
|
@ -23,7 +23,7 @@ type (
|
||||||
Routes = F.Map[string, *Route]
|
Routes = F.Map[string, *Route]
|
||||||
|
|
||||||
impl interface {
|
impl interface {
|
||||||
entry.Entry
|
types.Route
|
||||||
task.TaskStarter
|
task.TaskStarter
|
||||||
task.TaskFinisher
|
task.TaskFinisher
|
||||||
String() string
|
String() string
|
||||||
|
|
|
@ -2,6 +2,7 @@ package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/yusing/go-proxy/internal/homepage"
|
"github.com/yusing/go-proxy/internal/homepage"
|
||||||
"github.com/yusing/go-proxy/internal/route/entry"
|
"github.com/yusing/go-proxy/internal/route/entry"
|
||||||
|
@ -10,6 +11,33 @@ import (
|
||||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func getHealthInfo(r route.Route) map[string]string {
|
||||||
|
mon := r.HealthMonitor()
|
||||||
|
if mon == nil {
|
||||||
|
return map[string]string{
|
||||||
|
"status": "unknown",
|
||||||
|
"uptime": "n/a",
|
||||||
|
"latency": "n/a",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return map[string]string{
|
||||||
|
"status": mon.Status().String(),
|
||||||
|
"uptime": mon.Uptime().Round(time.Second).String(),
|
||||||
|
"latency": mon.Latency().Round(time.Microsecond).String(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func HealthMap() map[string]map[string]string {
|
||||||
|
healthMap := make(map[string]map[string]string)
|
||||||
|
httpRoutes.RangeAll(func(alias string, r route.HTTPRoute) {
|
||||||
|
healthMap[alias] = getHealthInfo(r)
|
||||||
|
})
|
||||||
|
streamRoutes.RangeAll(func(alias string, r route.StreamRoute) {
|
||||||
|
healthMap[alias] = getHealthInfo(r)
|
||||||
|
})
|
||||||
|
return healthMap
|
||||||
|
}
|
||||||
|
|
||||||
func HomepageConfig(useDefaultCategories bool) homepage.Config {
|
func HomepageConfig(useDefaultCategories bool) homepage.Config {
|
||||||
hpCfg := homepage.NewHomePageConfig()
|
hpCfg := homepage.NewHomePageConfig()
|
||||||
GetHTTPRoutes().RangeAll(func(alias string, r route.HTTPRoute) {
|
GetHTTPRoutes().RangeAll(func(alias string, r route.HTTPRoute) {
|
||||||
|
@ -77,8 +105,8 @@ func HomepageConfig(useDefaultCategories bool) homepage.Config {
|
||||||
return hpCfg
|
return hpCfg
|
||||||
}
|
}
|
||||||
|
|
||||||
func RoutesByAlias(typeFilter ...route.RouteType) map[string]any {
|
func RoutesByAlias(typeFilter ...route.RouteType) map[string]route.Route {
|
||||||
rts := make(map[string]any)
|
rts := make(map[string]route.Route)
|
||||||
if len(typeFilter) == 0 || typeFilter[0] == "" {
|
if len(typeFilter) == 0 || typeFilter[0] == "" {
|
||||||
typeFilter = []route.RouteType{route.RouteTypeReverseProxy, route.RouteTypeStream}
|
typeFilter = []route.RouteType{route.RouteTypeReverseProxy, route.RouteTypeStream}
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,6 +116,10 @@ func (r *StreamRoute) Finish(reason any) {
|
||||||
r.task.Finish(reason)
|
r.task.Finish(reason)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *StreamRoute) HealthMonitor() health.HealthMonitor {
|
||||||
|
return r.HealthMon
|
||||||
|
}
|
||||||
|
|
||||||
func (r *StreamRoute) acceptConnections() {
|
func (r *StreamRoute) acceptConnections() {
|
||||||
defer r.task.Finish("listener closed")
|
defer r.task.Finish("listener closed")
|
||||||
|
|
||||||
|
|
|
@ -4,15 +4,20 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
net "github.com/yusing/go-proxy/internal/net/types"
|
net "github.com/yusing/go-proxy/internal/net/types"
|
||||||
|
"github.com/yusing/go-proxy/internal/watcher/health"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
HTTPRoute interface {
|
Route interface {
|
||||||
Entry
|
Entry
|
||||||
|
HealthMonitor() health.HealthMonitor
|
||||||
|
}
|
||||||
|
HTTPRoute interface {
|
||||||
|
Route
|
||||||
http.Handler
|
http.Handler
|
||||||
}
|
}
|
||||||
StreamRoute interface {
|
StreamRoute interface {
|
||||||
Entry
|
Route
|
||||||
net.Stream
|
net.Stream
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
|
@ -15,13 +15,17 @@ type (
|
||||||
Detail string
|
Detail string
|
||||||
Latency time.Duration
|
Latency time.Duration
|
||||||
}
|
}
|
||||||
|
WithHealthInfo interface {
|
||||||
|
Status() Status
|
||||||
|
Uptime() time.Duration
|
||||||
|
Latency() time.Duration
|
||||||
|
}
|
||||||
HealthMonitor interface {
|
HealthMonitor interface {
|
||||||
task.TaskStarter
|
task.TaskStarter
|
||||||
task.TaskFinisher
|
task.TaskFinisher
|
||||||
fmt.Stringer
|
fmt.Stringer
|
||||||
json.Marshaler
|
json.Marshaler
|
||||||
Status() Status
|
WithHealthInfo
|
||||||
Uptime() time.Duration
|
|
||||||
Name() string
|
Name() string
|
||||||
}
|
}
|
||||||
HealthChecker interface {
|
HealthChecker interface {
|
||||||
|
|
|
@ -29,6 +29,19 @@ func (mon *DockerHealthMonitor) CheckHealth() (result *health.HealthCheckResult,
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return mon.fallback.CheckHealth()
|
return mon.fallback.CheckHealth()
|
||||||
}
|
}
|
||||||
|
status := cont.State.Status
|
||||||
|
switch status {
|
||||||
|
case "dead", "exited", "paused", "restarting", "removing":
|
||||||
|
return &health.HealthCheckResult{
|
||||||
|
Healthy: false,
|
||||||
|
Detail: "container is " + status,
|
||||||
|
}, nil
|
||||||
|
case "created":
|
||||||
|
return &health.HealthCheckResult{
|
||||||
|
Healthy: false,
|
||||||
|
Detail: "container is not started",
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
if cont.State.Health == nil {
|
if cont.State.Health == nil {
|
||||||
return mon.fallback.CheckHealth()
|
return mon.fallback.CheckHealth()
|
||||||
}
|
}
|
||||||
|
|
|
@ -142,6 +142,14 @@ func (mon *monitor) Uptime() time.Duration {
|
||||||
return time.Since(mon.startTime)
|
return time.Since(mon.startTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Latency implements HealthMonitor.
|
||||||
|
func (mon *monitor) Latency() time.Duration {
|
||||||
|
if mon.lastResult == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return mon.lastResult.Latency
|
||||||
|
}
|
||||||
|
|
||||||
// Name implements HealthMonitor.
|
// Name implements HealthMonitor.
|
||||||
func (mon *monitor) Name() string {
|
func (mon *monitor) Name() string {
|
||||||
parts := strutils.SplitRune(mon.service, '/')
|
parts := strutils.SplitRune(mon.service, '/')
|
||||||
|
|
|
@ -1,103 +0,0 @@
|
||||||
{
|
|
||||||
"$id": "https://github.com/yusing/go-proxy/raw/v0.8/schema/access_log.json",
|
|
||||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
||||||
"title": "Access log configuration",
|
|
||||||
"type": "object",
|
|
||||||
"additionalProperties": false,
|
|
||||||
"properties": {
|
|
||||||
"path": {
|
|
||||||
"title": "Access log path",
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"format": {
|
|
||||||
"title": "Access log format",
|
|
||||||
"type": "string",
|
|
||||||
"enum": [
|
|
||||||
"common",
|
|
||||||
"combined",
|
|
||||||
"json"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"buffer_size": {
|
|
||||||
"title": "Access log buffer size in bytes",
|
|
||||||
"type": "integer",
|
|
||||||
"minimum": 1
|
|
||||||
},
|
|
||||||
"filters": {
|
|
||||||
"title": "Access log filters",
|
|
||||||
"type": "object",
|
|
||||||
"additionalProperties": false,
|
|
||||||
"properties": {
|
|
||||||
"cidr": {
|
|
||||||
"title": "CIDR filter",
|
|
||||||
"$ref": "#/$defs/access_log_filters"
|
|
||||||
},
|
|
||||||
"status_codes": {
|
|
||||||
"title": "Status code filter",
|
|
||||||
"$ref": "#/$defs/access_log_filters"
|
|
||||||
},
|
|
||||||
"method": {
|
|
||||||
"title": "Method filter",
|
|
||||||
"$ref": "#/$defs/access_log_filters"
|
|
||||||
},
|
|
||||||
"headers": {
|
|
||||||
"title": "Header filter",
|
|
||||||
"$ref": "#/$defs/access_log_filters"
|
|
||||||
},
|
|
||||||
"host": {
|
|
||||||
"title": "Host filter",
|
|
||||||
"$ref": "#/$defs/access_log_filters"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"fields": {
|
|
||||||
"title": "Access log fields",
|
|
||||||
"type": "object",
|
|
||||||
"additionalProperties": false,
|
|
||||||
"properties": {
|
|
||||||
"headers": {
|
|
||||||
"title": "Headers field",
|
|
||||||
"$ref": "#/$defs/access_log_fields"
|
|
||||||
},
|
|
||||||
"query": {
|
|
||||||
"title": "Query field",
|
|
||||||
"$ref": "#/$defs/access_log_fields"
|
|
||||||
},
|
|
||||||
"cookies": {
|
|
||||||
"title": "Cookies field",
|
|
||||||
"$ref": "#/$defs/access_log_fields"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"$defs": {
|
|
||||||
"access_log_filters": {
|
|
||||||
"type": "object",
|
|
||||||
"additionalProperties": false,
|
|
||||||
"properties": {
|
|
||||||
"negative": {
|
|
||||||
"type": "boolean"
|
|
||||||
},
|
|
||||||
"values": {
|
|
||||||
"type": "array"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"access_log_fields": {
|
|
||||||
"type": "object",
|
|
||||||
"additionalProperties": false,
|
|
||||||
"properties": {
|
|
||||||
"default": {
|
|
||||||
"enum": [
|
|
||||||
"keep",
|
|
||||||
"redact",
|
|
||||||
"drop"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"config": {
|
|
||||||
"type": "object"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,464 +0,0 @@
|
||||||
{
|
|
||||||
"$id": "https://github.com/yusing/go-proxy/raw/v0.8/schema/config.schema.json",
|
|
||||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
||||||
"type": "object",
|
|
||||||
"title": "GoDoxy config file",
|
|
||||||
"properties": {
|
|
||||||
"autocert": {
|
|
||||||
"title": "Autocert configuration",
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"email": {
|
|
||||||
"title": "ACME Email",
|
|
||||||
"type": "string",
|
|
||||||
"format": "email"
|
|
||||||
},
|
|
||||||
"domains": {
|
|
||||||
"title": "Cert Domains",
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"minItems": 1
|
|
||||||
},
|
|
||||||
"cert_path": {
|
|
||||||
"title": "path of cert file to load/store",
|
|
||||||
"default": "certs/cert.crt",
|
|
||||||
"markdownDescription": "default: `certs/cert.crt`,",
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"key_path": {
|
|
||||||
"title": "path of key file to load/store",
|
|
||||||
"default": "certs/priv.key",
|
|
||||||
"markdownDescription": "default: `certs/priv.key`",
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"acme_key_path": {
|
|
||||||
"title": "path of acme key file to load/store",
|
|
||||||
"default": "certs/acme.key",
|
|
||||||
"markdownDescription": "default: `certs/acme.key`",
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"provider": {
|
|
||||||
"title": "DNS Challenge Provider",
|
|
||||||
"default": "local",
|
|
||||||
"type": "string",
|
|
||||||
"enum": [
|
|
||||||
"local",
|
|
||||||
"cloudflare",
|
|
||||||
"clouddns",
|
|
||||||
"duckdns",
|
|
||||||
"ovh"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"options": {
|
|
||||||
"title": "Provider specific options",
|
|
||||||
"type": "object"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"allOf": [
|
|
||||||
{
|
|
||||||
"if": {
|
|
||||||
"not": {
|
|
||||||
"properties": {
|
|
||||||
"provider": {
|
|
||||||
"const": "local"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"then": {
|
|
||||||
"required": [
|
|
||||||
"email",
|
|
||||||
"domains",
|
|
||||||
"provider",
|
|
||||||
"options"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"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"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"if": {
|
|
||||||
"properties": {
|
|
||||||
"provider": {
|
|
||||||
"const": "ovh"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"then": {
|
|
||||||
"properties": {
|
|
||||||
"options": {
|
|
||||||
"required": [
|
|
||||||
"application_secret",
|
|
||||||
"consumer_key"
|
|
||||||
],
|
|
||||||
"additionalProperties": false,
|
|
||||||
"oneOf": [
|
|
||||||
{
|
|
||||||
"required": [
|
|
||||||
"application_key"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"required": [
|
|
||||||
"oauth2_config"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"properties": {
|
|
||||||
"api_endpoint": {
|
|
||||||
"description": "OVH API endpoint",
|
|
||||||
"default": "ovh-eu",
|
|
||||||
"anyOf": [
|
|
||||||
{
|
|
||||||
"enum": [
|
|
||||||
"ovh-eu",
|
|
||||||
"ovh-ca",
|
|
||||||
"ovh-us",
|
|
||||||
"kimsufi-eu",
|
|
||||||
"kimsufi-ca",
|
|
||||||
"soyoustart-eu",
|
|
||||||
"soyoustart-ca"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "string",
|
|
||||||
"format": "uri"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"application_secret": {
|
|
||||||
"description": "OVH Application Secret",
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"consumer_key": {
|
|
||||||
"description": "OVH Consumer Key",
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"application_key": {
|
|
||||||
"description": "OVH Application Key",
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"oauth2_config": {
|
|
||||||
"description": "OVH OAuth2 config",
|
|
||||||
"type": "object",
|
|
||||||
"additionalProperties": false,
|
|
||||||
"properties": {
|
|
||||||
"client_id": {
|
|
||||||
"description": "OVH Client ID",
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"client_secret": {
|
|
||||||
"description": "OVH Client Secret",
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": [
|
|
||||||
"client_id",
|
|
||||||
"client_secret"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"providers": {
|
|
||||||
"title": "Proxy providers configuration",
|
|
||||||
"type": "object",
|
|
||||||
"additionalProperties": false,
|
|
||||||
"properties": {
|
|
||||||
"include": {
|
|
||||||
"title": "Proxy providers configuration files",
|
|
||||||
"description": "relative path to 'config'",
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"type": "string",
|
|
||||||
"pattern": "^[a-zA-Z0-9_-]+\\.(yml|yaml)$",
|
|
||||||
"patternErrorMessage": "Invalid file name"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"docker": {
|
|
||||||
"title": "Docker provider configuration",
|
|
||||||
"description": "docker clients (name-address pairs)",
|
|
||||||
"type": "object",
|
|
||||||
"patternProperties": {
|
|
||||||
"^[a-zA-Z0-9-_]+$": {
|
|
||||||
"type": "string",
|
|
||||||
"examples": [
|
|
||||||
"unix:///var/run/docker.sock",
|
|
||||||
"tcp://127.0.0.1:2375",
|
|
||||||
"ssh://user@host:port"
|
|
||||||
],
|
|
||||||
"oneOf": [
|
|
||||||
{
|
|
||||||
"const": "$DOCKER_HOST",
|
|
||||||
"description": "Use DOCKER_HOST environment variable"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"pattern": "^unix://.+$",
|
|
||||||
"description": "A Unix socket for local Docker communication."
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"pattern": "^ssh://.+$",
|
|
||||||
"description": "An SSH connection to a remote Docker host."
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"pattern": "^fd://.+$",
|
|
||||||
"description": "A file descriptor for Docker communication."
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"pattern": "^tcp://.+$",
|
|
||||||
"description": "A TCP connection to a remote Docker host."
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"notification": {
|
|
||||||
"description": "Notification provider configuration",
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"type": "object",
|
|
||||||
"required": [
|
|
||||||
"name",
|
|
||||||
"provider"
|
|
||||||
],
|
|
||||||
"properties": {
|
|
||||||
"name": {
|
|
||||||
"type": "string",
|
|
||||||
"description": "Notifier name"
|
|
||||||
},
|
|
||||||
"provider": {
|
|
||||||
"description": "Notifier provider",
|
|
||||||
"type": "string",
|
|
||||||
"enum": [
|
|
||||||
"gotify",
|
|
||||||
"webhook"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"oneOf": [
|
|
||||||
{
|
|
||||||
"description": "Gotify configuration",
|
|
||||||
"additionalProperties": false,
|
|
||||||
"properties": {
|
|
||||||
"name": {},
|
|
||||||
"provider": {
|
|
||||||
"const": "gotify"
|
|
||||||
},
|
|
||||||
"url": {
|
|
||||||
"description": "Gotify URL",
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"token": {
|
|
||||||
"description": "Gotify token",
|
|
||||||
"type": "string"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": [
|
|
||||||
"url",
|
|
||||||
"token"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"description": "Webhook configuration",
|
|
||||||
"additionalProperties": false,
|
|
||||||
"properties": {
|
|
||||||
"name": {},
|
|
||||||
"provider": {
|
|
||||||
"const": "webhook"
|
|
||||||
},
|
|
||||||
"url": {
|
|
||||||
"description": "Webhook URL",
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"token": {
|
|
||||||
"description": "Webhook bearer token",
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"template": {
|
|
||||||
"description": "Webhook template",
|
|
||||||
"type": "string",
|
|
||||||
"enum": [
|
|
||||||
"discord"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"payload": {
|
|
||||||
"description": "Webhook payload",
|
|
||||||
"type": "string",
|
|
||||||
"format": "json"
|
|
||||||
},
|
|
||||||
"method": {
|
|
||||||
"description": "Webhook request method",
|
|
||||||
"type": "string",
|
|
||||||
"enum": [
|
|
||||||
"GET",
|
|
||||||
"POST",
|
|
||||||
"PUT"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"mime_type": {
|
|
||||||
"description": "Webhook NIME type",
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"color_mode": {
|
|
||||||
"description": "Webhook color mode",
|
|
||||||
"type": "string",
|
|
||||||
"enum": [
|
|
||||||
"hex",
|
|
||||||
"dec"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": [
|
|
||||||
"url"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"match_domains": {
|
|
||||||
"title": "Domains to match",
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"minItems": 1
|
|
||||||
},
|
|
||||||
"homepage": {
|
|
||||||
"title": "Homepage configuration",
|
|
||||||
"type": "object",
|
|
||||||
"additionalProperties": false,
|
|
||||||
"properties": {
|
|
||||||
"use_default_categories": {
|
|
||||||
"title": "Use default categories",
|
|
||||||
"type": "boolean"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"entrypoint": {
|
|
||||||
"title": "Entrypoint configuration",
|
|
||||||
"type": "object",
|
|
||||||
"additionalProperties": false,
|
|
||||||
"properties": {
|
|
||||||
"middlewares": {
|
|
||||||
"title": "Entrypoint middlewares",
|
|
||||||
"type": "array",
|
|
||||||
"items": {
|
|
||||||
"type": "object",
|
|
||||||
"required": [
|
|
||||||
"use"
|
|
||||||
],
|
|
||||||
"properties": {
|
|
||||||
"use": {
|
|
||||||
"type": "string",
|
|
||||||
"description": "Middleware to use"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"access_log": {
|
|
||||||
"$ref": "https://github.com/yusing/go-proxy/raw/v0.8/schema/access_log.json"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"timeout_shutdown": {
|
|
||||||
"title": "Shutdown timeout (in seconds)",
|
|
||||||
"type": "integer",
|
|
||||||
"minimum": 0
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"additionalProperties": false,
|
|
||||||
"required": [
|
|
||||||
"providers"
|
|
||||||
]
|
|
||||||
}
|
|
|
@ -1,290 +0,0 @@
|
||||||
{
|
|
||||||
"$id": "https://github.com/yusing/go-proxy/raw/v0.8/schema/providers.schema.json",
|
|
||||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
||||||
"title": "GoDoxy standalone include file",
|
|
||||||
"oneOf": [
|
|
||||||
{
|
|
||||||
"type": "object"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "null"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"patternProperties": {
|
|
||||||
".+": {
|
|
||||||
"title": "Proxy entry",
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"scheme": {
|
|
||||||
"title": "Proxy scheme",
|
|
||||||
"oneOf": [
|
|
||||||
{
|
|
||||||
"type": "string",
|
|
||||||
"enum": [
|
|
||||||
"http",
|
|
||||||
"https",
|
|
||||||
"tcp",
|
|
||||||
"udp",
|
|
||||||
"tcp:tcp",
|
|
||||||
"udp:udp",
|
|
||||||
"tcp:udp",
|
|
||||||
"udp:tcp"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "null",
|
|
||||||
"description": "Auto detect base on port format"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"host": {
|
|
||||||
"default": "localhost",
|
|
||||||
"anyOf": [
|
|
||||||
{
|
|
||||||
"type": "null",
|
|
||||||
"title": "localhost (default)"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "string",
|
|
||||||
"format": "ipv4",
|
|
||||||
"title": "ipv4 address"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "string",
|
|
||||||
"format": "ipv6",
|
|
||||||
"title": "ipv6 address"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "string",
|
|
||||||
"format": "hostname",
|
|
||||||
"title": "hostname"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"title": "Proxy host (ipv4/6 / hostname)"
|
|
||||||
},
|
|
||||||
"port": {},
|
|
||||||
"no_tls_verify": {},
|
|
||||||
"path_patterns": {},
|
|
||||||
"middlewares": {},
|
|
||||||
"homepage": {
|
|
||||||
"title": "Dashboard config",
|
|
||||||
"type": "object",
|
|
||||||
"additionalProperties": false,
|
|
||||||
"properties": {
|
|
||||||
"show": {
|
|
||||||
"title": "Show on dashboard",
|
|
||||||
"type": "boolean",
|
|
||||||
"default": true
|
|
||||||
},
|
|
||||||
"name": {
|
|
||||||
"title": "Display name",
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"icon": {
|
|
||||||
"title": "Display icon",
|
|
||||||
"type": "string",
|
|
||||||
"oneOf": [
|
|
||||||
{
|
|
||||||
"pattern": "^(png|svg|webp)\\/[\\w\\d\\-_]+\\.\\1$",
|
|
||||||
"title": "Icon from walkxcode/dashboard-icons"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"pattern": "^https?://",
|
|
||||||
"title": "Absolute URI",
|
|
||||||
"format": "uri"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"pattern": "^@target/",
|
|
||||||
"title": "Relative URI to target"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"url": {
|
|
||||||
"title": "App URL override",
|
|
||||||
"type": "string",
|
|
||||||
"format": "uri",
|
|
||||||
"pattern": "^https?://"
|
|
||||||
},
|
|
||||||
"category": {
|
|
||||||
"title": "Category",
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"description": {
|
|
||||||
"title": "Description",
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"widget_config": {
|
|
||||||
"title": "Widget config",
|
|
||||||
"type": "object"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"load_balance": {
|
|
||||||
"type": "object",
|
|
||||||
"additionalProperties": false,
|
|
||||||
"properties": {
|
|
||||||
"link": {
|
|
||||||
"type": "string",
|
|
||||||
"title": "Name and subdomain of load-balancer"
|
|
||||||
},
|
|
||||||
"mode": {
|
|
||||||
"enum": [
|
|
||||||
"round_robin",
|
|
||||||
"least_conn",
|
|
||||||
"ip_hash"
|
|
||||||
],
|
|
||||||
"title": "Load-balance mode",
|
|
||||||
"default": "roundrobin"
|
|
||||||
},
|
|
||||||
"weight": {
|
|
||||||
"type": "integer",
|
|
||||||
"title": "Reserved for future use",
|
|
||||||
"minimum": 0,
|
|
||||||
"maximum": 100
|
|
||||||
},
|
|
||||||
"options": {
|
|
||||||
"type": "object",
|
|
||||||
"title": "load-balance mode specific options"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"healthcheck": {
|
|
||||||
"type": "object",
|
|
||||||
"additionalProperties": false,
|
|
||||||
"properties": {
|
|
||||||
"disable": {
|
|
||||||
"type": "boolean",
|
|
||||||
"default": false,
|
|
||||||
"title": "Disable healthcheck"
|
|
||||||
},
|
|
||||||
"path": {
|
|
||||||
"type": "string",
|
|
||||||
"title": "Healthcheck path",
|
|
||||||
"default": "/",
|
|
||||||
"format": "uri-reference",
|
|
||||||
"description": "should start with `/`"
|
|
||||||
},
|
|
||||||
"use_get": {
|
|
||||||
"type": "boolean",
|
|
||||||
"title": "Use GET instead of HEAD",
|
|
||||||
"default": false
|
|
||||||
},
|
|
||||||
"interval": {
|
|
||||||
"type": "string",
|
|
||||||
"title": "healthcheck Interval",
|
|
||||||
"pattern": "^([0-9]+(ms|s|m|h))+$",
|
|
||||||
"default": "5s",
|
|
||||||
"description": "e.g. 5s, 1m, 2h, 3m30s"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"access_log": {
|
|
||||||
"$ref": "https://github.com/yusing/go-proxy/raw/v0.8/schema/access_log.json"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"additionalProperties": false,
|
|
||||||
"allOf": [
|
|
||||||
{
|
|
||||||
"if": {
|
|
||||||
"properties": {
|
|
||||||
"scheme": {
|
|
||||||
"anyOf": [
|
|
||||||
{
|
|
||||||
"enum": [
|
|
||||||
"http",
|
|
||||||
"https"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "null"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"then": {
|
|
||||||
"properties": {
|
|
||||||
"port": {
|
|
||||||
"title": "Proxy port",
|
|
||||||
"markdownDescription": "From **0** to **65535**",
|
|
||||||
"oneOf": [
|
|
||||||
{
|
|
||||||
"type": "string",
|
|
||||||
"pattern": "^\\d{1,5}$",
|
|
||||||
"patternErrorMessage": "`port` must be a number"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "integer",
|
|
||||||
"minimum": 0,
|
|
||||||
"maximum": 65535
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"path_patterns": {
|
|
||||||
"title": "Path patterns",
|
|
||||||
"type": "array",
|
|
||||||
"markdownDescription": "See https://pkg.go.dev/net/http#hdr-Patterns-ServeMux",
|
|
||||||
"items": {
|
|
||||||
"type": "string",
|
|
||||||
"pattern": "^(?:([A-Z]+) )?(?:([a-zA-Z0-9.-]+)\\/)?(\\/[^\\s]*)$",
|
|
||||||
"patternErrorMessage": "invalid path pattern"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"middlewares": {
|
|
||||||
"type": "object"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"else": {
|
|
||||||
"properties": {
|
|
||||||
"port": {
|
|
||||||
"markdownDescription": "`listening port:proxy port` or `listening port:service name`",
|
|
||||||
"type": "string",
|
|
||||||
"pattern": "^[0-9]+:[0-9a-z]+$",
|
|
||||||
"patternErrorMessage": "invalid syntax"
|
|
||||||
},
|
|
||||||
"no_tls_verify": {
|
|
||||||
"not": true
|
|
||||||
},
|
|
||||||
"path_patterns": {
|
|
||||||
"not": true
|
|
||||||
},
|
|
||||||
"middlewares": {
|
|
||||||
"not": true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": [
|
|
||||||
"port"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"if": {
|
|
||||||
"properties": {
|
|
||||||
"scheme": {
|
|
||||||
"const": "https"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"then": {
|
|
||||||
"properties": {
|
|
||||||
"no_tls_verify": {
|
|
||||||
"title": "Disable TLS verification for https proxy",
|
|
||||||
"type": "boolean",
|
|
||||||
"default": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"else": {
|
|
||||||
"properties": {
|
|
||||||
"no_tls_verify": {
|
|
||||||
"not": true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"additionalProperties": false
|
|
||||||
}
|
|
1228
schemas/config.schema.json
Normal file
1228
schemas/config.schema.json
Normal file
File diff suppressed because it is too large
Load diff
66
schemas/config/access_log.ts
Normal file
66
schemas/config/access_log.ts
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
import { CIDR, HTTPHeader, HTTPMethod, StatusCodeRange, URI } from "../types";
|
||||||
|
|
||||||
|
export const ACCESS_LOG_FORMATS = ["combined", "common", "json"] as const;
|
||||||
|
|
||||||
|
export type AccessLogFormat = (typeof ACCESS_LOG_FORMATS)[number];
|
||||||
|
|
||||||
|
export type AccessLogConfig = {
|
||||||
|
/**
|
||||||
|
* The size of the buffer.
|
||||||
|
*
|
||||||
|
* @minimum 0
|
||||||
|
* @default 65536
|
||||||
|
* @TJS-type integer
|
||||||
|
*/
|
||||||
|
buffer_size?: number;
|
||||||
|
/** The format of the access log.
|
||||||
|
*
|
||||||
|
* @default "combined"
|
||||||
|
*/
|
||||||
|
format?: AccessLogFormat;
|
||||||
|
/* The path to the access log file. */
|
||||||
|
path: URI;
|
||||||
|
/* The access log filters. */
|
||||||
|
filters?: AccessLogFilters;
|
||||||
|
/* The access log fields. */
|
||||||
|
fields?: AccessLogFields;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type AccessLogFilter<T> = {
|
||||||
|
/** Whether the filter is negative.
|
||||||
|
*
|
||||||
|
* @default false
|
||||||
|
*/
|
||||||
|
negative?: boolean;
|
||||||
|
/* The values to filter. */
|
||||||
|
values: T[];
|
||||||
|
};
|
||||||
|
|
||||||
|
export type AccessLogFilters = {
|
||||||
|
/* Status code filter. */
|
||||||
|
status_code?: AccessLogFilter<StatusCodeRange>;
|
||||||
|
/* Method filter. */
|
||||||
|
method?: AccessLogFilter<HTTPMethod>;
|
||||||
|
/* Host filter. */
|
||||||
|
host?: AccessLogFilter<string>;
|
||||||
|
/* Header filter. */
|
||||||
|
headers?: AccessLogFilter<HTTPHeader>;
|
||||||
|
/* CIDR filter. */
|
||||||
|
cidr?: AccessLogFilter<CIDR>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const ACCESS_LOG_FIELD_MODES = ["keep", "drop", "redact"] as const;
|
||||||
|
export type AccessLogFieldMode = (typeof ACCESS_LOG_FIELD_MODES)[number];
|
||||||
|
|
||||||
|
export type AccessLogField = {
|
||||||
|
default?: AccessLogFieldMode;
|
||||||
|
config: {
|
||||||
|
[key: string]: AccessLogFieldMode;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export type AccessLogFields = {
|
||||||
|
header?: AccessLogField;
|
||||||
|
query?: AccessLogField;
|
||||||
|
cookie?: AccessLogField;
|
||||||
|
};
|
91
schemas/config/autocert.ts
Normal file
91
schemas/config/autocert.ts
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
import { DomainOrWildcards as DomainsOrWildcards, Email } from "../types";
|
||||||
|
|
||||||
|
export const AUTOCERT_PROVIDERS = [
|
||||||
|
"local",
|
||||||
|
"cloudflare",
|
||||||
|
"clouddns",
|
||||||
|
"duckdns",
|
||||||
|
"ovh",
|
||||||
|
] as const;
|
||||||
|
|
||||||
|
export type AutocertProvider = (typeof AUTOCERT_PROVIDERS)[number];
|
||||||
|
|
||||||
|
export type AutocertConfig =
|
||||||
|
| LocalOptions
|
||||||
|
| CloudflareOptions
|
||||||
|
| CloudDNSOptions
|
||||||
|
| DuckDNSOptions
|
||||||
|
| OVHOptionsWithAppKey
|
||||||
|
| OVHOptionsWithOAuth2Config;
|
||||||
|
|
||||||
|
export interface AutocertConfigBase {
|
||||||
|
/* ACME email */
|
||||||
|
email: Email;
|
||||||
|
/* ACME domains */
|
||||||
|
domains: DomainsOrWildcards;
|
||||||
|
/* ACME certificate path */
|
||||||
|
cert_path?: string;
|
||||||
|
/* ACME key path */
|
||||||
|
key_path?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface LocalOptions extends AutocertConfigBase {
|
||||||
|
provider: "local";
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CloudflareOptions extends AutocertConfigBase {
|
||||||
|
provider: "cloudflare";
|
||||||
|
options: { auth_token: string };
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CloudDNSOptions extends AutocertConfigBase {
|
||||||
|
provider: "clouddns";
|
||||||
|
options: {
|
||||||
|
client_id: string;
|
||||||
|
email: Email;
|
||||||
|
password: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export interface DuckDNSOptions extends AutocertConfigBase {
|
||||||
|
provider: "duckdns";
|
||||||
|
options: {
|
||||||
|
token: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const OVH_ENDPOINTS = [
|
||||||
|
"ovh-eu",
|
||||||
|
"ovh-ca",
|
||||||
|
"ovh-us",
|
||||||
|
"kimsufi-eu",
|
||||||
|
"kimsufi-ca",
|
||||||
|
"soyoustart-eu",
|
||||||
|
"soyoustart-ca",
|
||||||
|
] as const;
|
||||||
|
|
||||||
|
export type OVHEndpoint = (typeof OVH_ENDPOINTS)[number];
|
||||||
|
|
||||||
|
export interface OVHOptionsWithAppKey extends AutocertConfigBase {
|
||||||
|
provider: "ovh";
|
||||||
|
options: {
|
||||||
|
application_secret: string;
|
||||||
|
consumer_key: string;
|
||||||
|
api_endpoint?: OVHEndpoint;
|
||||||
|
application_key: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface OVHOptionsWithOAuth2Config extends AutocertConfigBase {
|
||||||
|
provider: "ovh";
|
||||||
|
options: {
|
||||||
|
application_secret: string;
|
||||||
|
consumer_key: string;
|
||||||
|
api_endpoint?: OVHEndpoint;
|
||||||
|
oauth2_config: {
|
||||||
|
client_id: string;
|
||||||
|
client_secret: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
52
schemas/config/config.ts
Normal file
52
schemas/config/config.ts
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
import { DomainNames } from "../types";
|
||||||
|
import { AutocertConfig } from "./autocert";
|
||||||
|
import { EntrypointConfig } from "./entrypoint";
|
||||||
|
import { HomepageConfig } from "./homepage";
|
||||||
|
import { Providers } from "./providers";
|
||||||
|
|
||||||
|
export type Config = {
|
||||||
|
/** Optional autocert configuration
|
||||||
|
*
|
||||||
|
* @examples require(".").autocertExamples
|
||||||
|
*/
|
||||||
|
autocert?: AutocertConfig;
|
||||||
|
/* Optional entrypoint configuration */
|
||||||
|
entrypoint?: EntrypointConfig;
|
||||||
|
/* Providers configuration (include file, docker, notification) */
|
||||||
|
providers: Providers;
|
||||||
|
/** Optional list of domains to match
|
||||||
|
*
|
||||||
|
* @minItems 1
|
||||||
|
* @examples require(".").matchDomainsExamples
|
||||||
|
*/
|
||||||
|
match_domains?: DomainNames;
|
||||||
|
/* Optional homepage configuration */
|
||||||
|
homepage?: HomepageConfig;
|
||||||
|
/**
|
||||||
|
* Optional timeout before shutdown
|
||||||
|
* @default 3
|
||||||
|
* @minimum 1
|
||||||
|
*/
|
||||||
|
timeout_shutdown?: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const autocertExamples = [
|
||||||
|
{ provider: "local" },
|
||||||
|
{
|
||||||
|
provider: "cloudflare",
|
||||||
|
email: "abc@gmail",
|
||||||
|
domains: ["example.com"],
|
||||||
|
options: { auth_token: "c1234565789-abcdefghijklmnopqrst" },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
provider: "clouddns",
|
||||||
|
email: "abc@gmail",
|
||||||
|
domains: ["example.com"],
|
||||||
|
options: {
|
||||||
|
client_id: "c1234565789",
|
||||||
|
email: "abc@gmail",
|
||||||
|
password: "password",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
export const matchDomainsExamples = ["example.com", "*.example.com"] as const;
|
47
schemas/config/entrypoint.ts
Normal file
47
schemas/config/entrypoint.ts
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
import { MiddlewareCompose } from "../middlewares/middleware_compose";
|
||||||
|
import { AccessLogConfig } from "./access_log";
|
||||||
|
|
||||||
|
export type EntrypointConfig = {
|
||||||
|
/** Entrypoint middleware configuration
|
||||||
|
*
|
||||||
|
* @examples require(".").middlewaresExamples
|
||||||
|
*/
|
||||||
|
middlewares: MiddlewareCompose;
|
||||||
|
/** Entrypoint access log configuration
|
||||||
|
*
|
||||||
|
* @examples require(".").accessLogExamples
|
||||||
|
*/
|
||||||
|
access_log?: AccessLogConfig;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const accessLogExamples = [
|
||||||
|
{
|
||||||
|
path: "/var/log/access.log",
|
||||||
|
format: "combined",
|
||||||
|
filters: {
|
||||||
|
status_codes: {
|
||||||
|
values: ["200-299"],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
fields: {
|
||||||
|
headers: {
|
||||||
|
default: "keep",
|
||||||
|
config: {
|
||||||
|
foo: "redact",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
] as const;
|
||||||
|
|
||||||
|
export const middlewaresExamples = [
|
||||||
|
{
|
||||||
|
use: "RedirectHTTP",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
use: "CIDRWhitelist",
|
||||||
|
allow: ["127.0.0.1", "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"],
|
||||||
|
status: 403,
|
||||||
|
message: "Forbidden",
|
||||||
|
},
|
||||||
|
] as const;
|
7
schemas/config/homepage.ts
Normal file
7
schemas/config/homepage.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
export type HomepageConfig = {
|
||||||
|
/**
|
||||||
|
* Use default app categories (uses docker image name)
|
||||||
|
* @default true
|
||||||
|
*/
|
||||||
|
use_default_categories: boolean;
|
||||||
|
};
|
67
schemas/config/notification.ts
Normal file
67
schemas/config/notification.ts
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
import { URL } from "../types";
|
||||||
|
|
||||||
|
export const NOTIFICATION_PROVIDERS = ["webhook", "gotify"] as const;
|
||||||
|
|
||||||
|
export type NotificationProvider = (typeof NOTIFICATION_PROVIDERS)[number];
|
||||||
|
|
||||||
|
export type NotificationConfig = {
|
||||||
|
/* Name of the notification provider */
|
||||||
|
name: string;
|
||||||
|
/* URL of the notification provider */
|
||||||
|
url: URL;
|
||||||
|
};
|
||||||
|
|
||||||
|
export interface GotifyConfig extends NotificationConfig {
|
||||||
|
provider: "gotify";
|
||||||
|
/* Gotify token */
|
||||||
|
token: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const WEBHOOK_TEMPLATES = ["discord"] as const;
|
||||||
|
export const WEBHOOK_METHODS = ["POST", "GET", "PUT"] as const;
|
||||||
|
export const WEBHOOK_MIME_TYPES = [
|
||||||
|
"application/json",
|
||||||
|
"application/x-www-form-urlencoded",
|
||||||
|
"text/plain",
|
||||||
|
] as const;
|
||||||
|
export const WEBHOOK_COLOR_MODES = ["hex", "dec"] as const;
|
||||||
|
|
||||||
|
export type WebhookTemplate = (typeof WEBHOOK_TEMPLATES)[number];
|
||||||
|
export type WebhookMethod = (typeof WEBHOOK_METHODS)[number];
|
||||||
|
export type WebhookMimeType = (typeof WEBHOOK_MIME_TYPES)[number];
|
||||||
|
export type WebhookColorMode = (typeof WEBHOOK_COLOR_MODES)[number];
|
||||||
|
|
||||||
|
export interface WebhookConfig extends NotificationConfig {
|
||||||
|
provider: "webhook";
|
||||||
|
/**
|
||||||
|
* Webhook template
|
||||||
|
*
|
||||||
|
* @default "discord"
|
||||||
|
*/
|
||||||
|
template?: WebhookTemplate;
|
||||||
|
/* Webhook token */
|
||||||
|
token?: string;
|
||||||
|
/**
|
||||||
|
* Webhook message (usally JSON),
|
||||||
|
* required when template is not defined
|
||||||
|
*/
|
||||||
|
payload?: string;
|
||||||
|
/**
|
||||||
|
* Webhook method
|
||||||
|
*
|
||||||
|
* @default "POST"
|
||||||
|
*/
|
||||||
|
method?: WebhookMethod;
|
||||||
|
/**
|
||||||
|
* Webhook mime type
|
||||||
|
*
|
||||||
|
* @default "application/json"
|
||||||
|
*/
|
||||||
|
mime_type?: WebhookMimeType;
|
||||||
|
/**
|
||||||
|
* Webhook color mode
|
||||||
|
*
|
||||||
|
* @default "hex"
|
||||||
|
*/
|
||||||
|
color_mode?: WebhookColorMode;
|
||||||
|
}
|
46
schemas/config/providers.ts
Normal file
46
schemas/config/providers.ts
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
import { URI, URL } from "../types";
|
||||||
|
import { GotifyConfig, WebhookConfig } from "./notification";
|
||||||
|
|
||||||
|
export type Providers = {
|
||||||
|
/** List of route definition files to include
|
||||||
|
*
|
||||||
|
* @minItems 1
|
||||||
|
* @examples require(".").includeExamples
|
||||||
|
* @items.pattern ^[\w\d\-_]+\.(yaml|yml)$
|
||||||
|
*/
|
||||||
|
include?: URI[];
|
||||||
|
/** Name-value mapping of docker hosts to retrieve routes from
|
||||||
|
*
|
||||||
|
* @minProperties 1
|
||||||
|
* @examples require(".").dockerExamples
|
||||||
|
* @items.pattern ^((\w+://)[^\s]+)|\$DOCKER_HOST$
|
||||||
|
*/
|
||||||
|
docker?: { [name: string]: URL };
|
||||||
|
/** List of notification providers
|
||||||
|
*
|
||||||
|
* @minItems 1
|
||||||
|
* @examples require(".").notificationExamples
|
||||||
|
*/
|
||||||
|
notification?: (WebhookConfig | GotifyConfig)[];
|
||||||
|
};
|
||||||
|
|
||||||
|
export const includeExamples = ["file1.yml", "file2.yml"] as const;
|
||||||
|
export const dockerExamples = [
|
||||||
|
{ local: "$DOCKER_HOST" },
|
||||||
|
{ remote: "tcp://10.0.2.1:2375" },
|
||||||
|
{ remote2: "ssh://root:1234@10.0.2.2" },
|
||||||
|
] as const;
|
||||||
|
export const notificationExamples = [
|
||||||
|
{
|
||||||
|
name: "gotify",
|
||||||
|
provider: "gotify",
|
||||||
|
url: "https://gotify.domain.tld",
|
||||||
|
token: "abcd",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "discord",
|
||||||
|
provider: "webhook",
|
||||||
|
template: "discord",
|
||||||
|
url: "https://discord.com/api/webhooks/1234/abcd",
|
||||||
|
},
|
||||||
|
] as const;
|
7
schemas/docker.ts
Normal file
7
schemas/docker.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import { IdleWatcherConfig } from "./providers/idlewatcher";
|
||||||
|
import { Route } from "./providers/routes";
|
||||||
|
|
||||||
|
//FIXME: fix this
|
||||||
|
export type DockerRoutes = {
|
||||||
|
[key: string]: Route & IdleWatcherConfig;
|
||||||
|
};
|
1198
schemas/docker_routes.schema.json
Normal file
1198
schemas/docker_routes.schema.json
Normal file
File diff suppressed because it is too large
Load diff
364
schemas/middleware_compose.schema.json
Normal file
364
schemas/middleware_compose.schema.json
Normal file
|
@ -0,0 +1,364 @@
|
||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||||
|
"definitions": {
|
||||||
|
"CIDR": {
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"pattern": "^[0-9]*\\.[0-9]*\\.[0-9]*\\.[0-9]*$",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"pattern": "^.*:.*:.*:.*:.*:.*:.*:.*$",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"pattern": "^[0-9]*\\.[0-9]*\\.[0-9]*\\.[0-9]*/[0-9]*$",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"pattern": "^::[0-9]*$",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"pattern": "^.*::/[0-9]*$",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"pattern": "^.*:.*::/[0-9]*$",
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"MiddlewareComposeMap": {
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"additionalProperties": false,
|
||||||
|
"properties": {
|
||||||
|
"use": {
|
||||||
|
"enum": [
|
||||||
|
"CustomErrorPage",
|
||||||
|
"ErrorPage",
|
||||||
|
"customErrorPage",
|
||||||
|
"custom_error_page",
|
||||||
|
"errorPage",
|
||||||
|
"error_page"
|
||||||
|
],
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"use"
|
||||||
|
],
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"additionalProperties": false,
|
||||||
|
"properties": {
|
||||||
|
"use": {
|
||||||
|
"enum": [
|
||||||
|
"RedirectHTTP",
|
||||||
|
"redirectHTTP",
|
||||||
|
"redirect_http"
|
||||||
|
],
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"use"
|
||||||
|
],
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"additionalProperties": false,
|
||||||
|
"properties": {
|
||||||
|
"use": {
|
||||||
|
"enum": [
|
||||||
|
"SetXForwarded",
|
||||||
|
"setXForwarded",
|
||||||
|
"set_x_forwarded"
|
||||||
|
],
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"use"
|
||||||
|
],
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"additionalProperties": false,
|
||||||
|
"properties": {
|
||||||
|
"use": {
|
||||||
|
"enum": [
|
||||||
|
"HideXForwarded",
|
||||||
|
"hideXForwarded",
|
||||||
|
"hide_x_forwarded"
|
||||||
|
],
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"use"
|
||||||
|
],
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"additionalProperties": false,
|
||||||
|
"properties": {
|
||||||
|
"allow": {
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/definitions/CIDR"
|
||||||
|
},
|
||||||
|
"type": "array"
|
||||||
|
},
|
||||||
|
"message": {
|
||||||
|
"default": "IP not allowed",
|
||||||
|
"description": "Error message when blocked",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"status": {
|
||||||
|
"$ref": "#/definitions/StatusCode",
|
||||||
|
"default": 403,
|
||||||
|
"description": "HTTP status code when blocked (alias of status_code)"
|
||||||
|
},
|
||||||
|
"status_code": {
|
||||||
|
"$ref": "#/definitions/StatusCode",
|
||||||
|
"default": 403,
|
||||||
|
"description": "HTTP status code when blocked"
|
||||||
|
},
|
||||||
|
"use": {
|
||||||
|
"enum": [
|
||||||
|
"CIDRWhitelist",
|
||||||
|
"cidrWhitelist",
|
||||||
|
"cidr_whitelist"
|
||||||
|
],
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"allow",
|
||||||
|
"use"
|
||||||
|
],
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"additionalProperties": false,
|
||||||
|
"properties": {
|
||||||
|
"recursive": {
|
||||||
|
"default": false,
|
||||||
|
"description": "Recursively resolve the IP",
|
||||||
|
"type": "boolean"
|
||||||
|
},
|
||||||
|
"use": {
|
||||||
|
"enum": [
|
||||||
|
"cloudflareRealIp",
|
||||||
|
"cloudflare_real_ip"
|
||||||
|
],
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"use"
|
||||||
|
],
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"additionalProperties": false,
|
||||||
|
"properties": {
|
||||||
|
"add_headers": {
|
||||||
|
"additionalProperties": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"description": "Add HTTP headers",
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
"hide_headers": {
|
||||||
|
"description": "Hide HTTP headers",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"type": "array"
|
||||||
|
},
|
||||||
|
"set_headers": {
|
||||||
|
"additionalProperties": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"description": "Set HTTP headers",
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
"use": {
|
||||||
|
"enum": [
|
||||||
|
"ModifyRequest",
|
||||||
|
"Request",
|
||||||
|
"modifyRequest",
|
||||||
|
"modify_request",
|
||||||
|
"request"
|
||||||
|
],
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"use"
|
||||||
|
],
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"additionalProperties": false,
|
||||||
|
"properties": {
|
||||||
|
"add_headers": {
|
||||||
|
"additionalProperties": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"description": "Add HTTP headers",
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
"hide_headers": {
|
||||||
|
"description": "Hide HTTP headers",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"type": "array"
|
||||||
|
},
|
||||||
|
"set_headers": {
|
||||||
|
"additionalProperties": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"description": "Set HTTP headers",
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
"use": {
|
||||||
|
"enum": [
|
||||||
|
"ModifyResponse",
|
||||||
|
"Response",
|
||||||
|
"modifyResponse",
|
||||||
|
"modify_response",
|
||||||
|
"response"
|
||||||
|
],
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"use"
|
||||||
|
],
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"additionalProperties": false,
|
||||||
|
"properties": {
|
||||||
|
"allowed_groups": {
|
||||||
|
"description": "Allowed groups",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"minItems": 1,
|
||||||
|
"type": "array"
|
||||||
|
},
|
||||||
|
"allowed_users": {
|
||||||
|
"description": "Allowed users",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"minItems": 1,
|
||||||
|
"type": "array"
|
||||||
|
},
|
||||||
|
"use": {
|
||||||
|
"enum": [
|
||||||
|
"OIDC",
|
||||||
|
"oidc"
|
||||||
|
],
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"use"
|
||||||
|
],
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"additionalProperties": false,
|
||||||
|
"properties": {
|
||||||
|
"average": {
|
||||||
|
"description": "Average number of requests allowed in a period",
|
||||||
|
"type": "number"
|
||||||
|
},
|
||||||
|
"burst": {
|
||||||
|
"description": "Maximum number of requests allowed in a period",
|
||||||
|
"type": "number"
|
||||||
|
},
|
||||||
|
"period": {
|
||||||
|
"default": "1s",
|
||||||
|
"description": "Duration of the rate limit",
|
||||||
|
"pattern": "^([0-9]+(ms|s|m|h))+$",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"use": {
|
||||||
|
"enum": [
|
||||||
|
"RateLimit",
|
||||||
|
"rateLimit",
|
||||||
|
"rate_limit"
|
||||||
|
],
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"average",
|
||||||
|
"burst",
|
||||||
|
"use"
|
||||||
|
],
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"additionalProperties": false,
|
||||||
|
"properties": {
|
||||||
|
"from": {
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/definitions/CIDR"
|
||||||
|
},
|
||||||
|
"type": "array"
|
||||||
|
},
|
||||||
|
"header": {
|
||||||
|
"default": "X-Real-IP",
|
||||||
|
"description": "Header to get the client IP from",
|
||||||
|
"pattern": "^[a-zA-Z0-9\\-]+$",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"recursive": {
|
||||||
|
"default": false,
|
||||||
|
"description": "Recursive resolve the IP",
|
||||||
|
"type": "boolean"
|
||||||
|
},
|
||||||
|
"use": {
|
||||||
|
"enum": [
|
||||||
|
"RealIP",
|
||||||
|
"realIP",
|
||||||
|
"real_ip"
|
||||||
|
],
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"from",
|
||||||
|
"use"
|
||||||
|
],
|
||||||
|
"type": "object"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"StatusCode": {
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"pattern": "^[0-9]*$",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "number"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/definitions/MiddlewareComposeMap"
|
||||||
|
},
|
||||||
|
"type": "array"
|
||||||
|
}
|
||||||
|
|
3
schemas/middlewares/middleware_compose.ts
Normal file
3
schemas/middlewares/middleware_compose.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
import { MiddlewareComposeMap } from "./middlewares";
|
||||||
|
|
||||||
|
export type MiddlewareCompose = MiddlewareComposeMap[];
|
149
schemas/middlewares/middlewares.ts
Normal file
149
schemas/middlewares/middlewares.ts
Normal file
|
@ -0,0 +1,149 @@
|
||||||
|
import * as types from "../types";
|
||||||
|
|
||||||
|
export type MiddlewareComposeObjectRef = `${string}@file`;
|
||||||
|
|
||||||
|
export type KeyOptMapping<T extends { use: string }> = {
|
||||||
|
[key in T["use"]]: Omit<T, "use">;
|
||||||
|
} | { use: MiddlewareComposeObjectRef };
|
||||||
|
|
||||||
|
export type MiddlewaresMap = (
|
||||||
|
| KeyOptMapping<CustomErrorPage>
|
||||||
|
| KeyOptMapping<RedirectHTTP>
|
||||||
|
| KeyOptMapping<SetXForwarded>
|
||||||
|
| KeyOptMapping<HideXForwarded>
|
||||||
|
| KeyOptMapping<CIDRWhitelist>
|
||||||
|
| KeyOptMapping<CloudflareRealIP>
|
||||||
|
| KeyOptMapping<ModifyRequest>
|
||||||
|
| KeyOptMapping<ModifyResponse>
|
||||||
|
| KeyOptMapping<OIDC>
|
||||||
|
| KeyOptMapping<RateLimit>
|
||||||
|
| KeyOptMapping<RealIP>
|
||||||
|
| { [key in MiddlewareComposeObjectRef]: types.NullOrEmptyMap }
|
||||||
|
);
|
||||||
|
|
||||||
|
export type MiddlewareComposeMap = (
|
||||||
|
| CustomErrorPage
|
||||||
|
| RedirectHTTP
|
||||||
|
| SetXForwarded
|
||||||
|
| HideXForwarded
|
||||||
|
| CIDRWhitelist
|
||||||
|
| CloudflareRealIP
|
||||||
|
| ModifyRequest
|
||||||
|
| ModifyResponse
|
||||||
|
| OIDC
|
||||||
|
| RateLimit
|
||||||
|
| RealIP
|
||||||
|
);
|
||||||
|
|
||||||
|
export type CustomErrorPage = {
|
||||||
|
use: "error_page" | "errorPage" | "ErrorPage" | "custom_error_page" | "customErrorPage" | "CustomErrorPage";
|
||||||
|
};
|
||||||
|
|
||||||
|
export type RedirectHTTP = {
|
||||||
|
use: "redirect_http" | "redirectHTTP" | "RedirectHTTP";
|
||||||
|
};
|
||||||
|
|
||||||
|
export type SetXForwarded = {
|
||||||
|
use: "set_x_forwarded" | "setXForwarded" | "SetXForwarded";
|
||||||
|
};
|
||||||
|
export type HideXForwarded = {
|
||||||
|
use: "hide_x_forwarded" | "hideXForwarded" | "HideXForwarded";
|
||||||
|
};
|
||||||
|
|
||||||
|
export type CIDRWhitelist = {
|
||||||
|
use: "cidr_whitelist" | "cidrWhitelist" | "CIDRWhitelist";
|
||||||
|
/* Allowed CIDRs/IPs */
|
||||||
|
allow: types.CIDR[];
|
||||||
|
/** HTTP status code when blocked
|
||||||
|
*
|
||||||
|
* @default 403
|
||||||
|
*/
|
||||||
|
status_code?: types.StatusCode;
|
||||||
|
/** HTTP status code when blocked (alias of status_code)
|
||||||
|
*
|
||||||
|
* @default 403
|
||||||
|
*/
|
||||||
|
status?: types.StatusCode;
|
||||||
|
/** Error message when blocked
|
||||||
|
*
|
||||||
|
* @default "IP not allowed"
|
||||||
|
*/
|
||||||
|
message?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type CloudflareRealIP = {
|
||||||
|
use: "cloudflare_real_ip" | "cloudflareRealIp" | "cloudflare_real_ip";
|
||||||
|
/** Recursively resolve the IP
|
||||||
|
*
|
||||||
|
* @default false
|
||||||
|
*/
|
||||||
|
recursive?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ModifyRequest = {
|
||||||
|
use: "request" | "Request" | "modify_request" | "modifyRequest" | "ModifyRequest";
|
||||||
|
/** Set HTTP headers */
|
||||||
|
set_headers?: { [key: types.HTTPHeader]: string };
|
||||||
|
/** Add HTTP headers */
|
||||||
|
add_headers?: { [key: types.HTTPHeader]: string };
|
||||||
|
/** Hide HTTP headers */
|
||||||
|
hide_headers?: types.HTTPHeader[];
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ModifyResponse = {
|
||||||
|
use: "response" | "Response" | "modify_response" | "modifyResponse" | "ModifyResponse";
|
||||||
|
/** Set HTTP headers */
|
||||||
|
set_headers?: { [key: types.HTTPHeader]: string };
|
||||||
|
/** Add HTTP headers */
|
||||||
|
add_headers?: { [key: types.HTTPHeader]: string };
|
||||||
|
/** Hide HTTP headers */
|
||||||
|
hide_headers?: types.HTTPHeader[];
|
||||||
|
};
|
||||||
|
|
||||||
|
export type OIDC = {
|
||||||
|
use: "oidc" | "OIDC";
|
||||||
|
/** Allowed users
|
||||||
|
*
|
||||||
|
* @minItems 1
|
||||||
|
*/
|
||||||
|
allowed_users?: string[];
|
||||||
|
/** Allowed groups
|
||||||
|
*
|
||||||
|
* @minItems 1
|
||||||
|
*/
|
||||||
|
allowed_groups?: string[];
|
||||||
|
};
|
||||||
|
|
||||||
|
export type RateLimit = {
|
||||||
|
use: "rate_limit" | "rateLimit" | "RateLimit";
|
||||||
|
/** Average number of requests allowed in a period
|
||||||
|
*
|
||||||
|
* @min 1
|
||||||
|
*/
|
||||||
|
average: number;
|
||||||
|
/** Maximum number of requests allowed in a period
|
||||||
|
*
|
||||||
|
* @min 1
|
||||||
|
*/
|
||||||
|
burst: number;
|
||||||
|
/** Duration of the rate limit
|
||||||
|
*
|
||||||
|
* @default 1s
|
||||||
|
*/
|
||||||
|
period?: types.Duration;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type RealIP = {
|
||||||
|
use: "real_ip" | "realIP" | "RealIP";
|
||||||
|
/** Header to get the client IP from
|
||||||
|
*
|
||||||
|
* @default "X-Real-IP"
|
||||||
|
*/
|
||||||
|
header?: types.HTTPHeader;
|
||||||
|
from: types.CIDR[];
|
||||||
|
/** Recursive resolve the IP
|
||||||
|
*
|
||||||
|
* @default false
|
||||||
|
*/
|
||||||
|
recursive?: boolean;
|
||||||
|
};
|
33
schemas/providers/healthcheck.ts
Normal file
33
schemas/providers/healthcheck.ts
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
import { Duration, URI } from "../types";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @additionalProperties false
|
||||||
|
*/
|
||||||
|
export type HealthcheckConfig = {
|
||||||
|
/** Disable healthcheck
|
||||||
|
*
|
||||||
|
* @default false
|
||||||
|
*/
|
||||||
|
disable?: boolean;
|
||||||
|
/** Healthcheck path
|
||||||
|
*
|
||||||
|
* @default /
|
||||||
|
*/
|
||||||
|
path?: URI;
|
||||||
|
/**
|
||||||
|
* Use GET instead of HEAD
|
||||||
|
*
|
||||||
|
* @default false
|
||||||
|
*/
|
||||||
|
use_get?: boolean;
|
||||||
|
/** Healthcheck interval
|
||||||
|
*
|
||||||
|
* @default 5s
|
||||||
|
*/
|
||||||
|
interval?: Duration;
|
||||||
|
/** Healthcheck timeout
|
||||||
|
*
|
||||||
|
* @default 5s
|
||||||
|
*/
|
||||||
|
timeout?: Duration;
|
||||||
|
};
|
36
schemas/providers/homepage.ts
Normal file
36
schemas/providers/homepage.ts
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
import { URL } from "../types";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @additionalProperties false
|
||||||
|
*/
|
||||||
|
export type HomepageConfig = {
|
||||||
|
/** Whether show in dashboard
|
||||||
|
*
|
||||||
|
* @default true
|
||||||
|
*/
|
||||||
|
show?: boolean;
|
||||||
|
/* Display name on dashboard */
|
||||||
|
name?: string;
|
||||||
|
/* Display icon on dashboard */
|
||||||
|
icon?: URL | WalkxcodeIcon | TargetRelativeIconPath;
|
||||||
|
/* App description */
|
||||||
|
description?: string;
|
||||||
|
/* Override url */
|
||||||
|
url?: URL;
|
||||||
|
/* App category */
|
||||||
|
category?: string;
|
||||||
|
/* Widget config */
|
||||||
|
widget_config?: {
|
||||||
|
[key: string]: any;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @pattern ^(png|svg|webp)\\/[\\w\\d\\-_]+\\.\\1$
|
||||||
|
*/
|
||||||
|
export type WalkxcodeIcon = string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @pattern ^@target/.+$
|
||||||
|
*/
|
||||||
|
export type TargetRelativeIconPath = string;
|
41
schemas/providers/idlewatcher.ts
Normal file
41
schemas/providers/idlewatcher.ts
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
import { Duration, URI } from "../types";
|
||||||
|
|
||||||
|
export const STOP_METHODS = ["pause", "stop", "kill"] as const;
|
||||||
|
export type StopMethod = (typeof STOP_METHODS)[number];
|
||||||
|
|
||||||
|
export const STOP_SIGNALS = [
|
||||||
|
"",
|
||||||
|
"SIGINT",
|
||||||
|
"SIGTERM",
|
||||||
|
"SIGHUP",
|
||||||
|
"SIGQUIT",
|
||||||
|
"INT",
|
||||||
|
"TERM",
|
||||||
|
"HUP",
|
||||||
|
"QUIT",
|
||||||
|
] as const;
|
||||||
|
export type Signal = (typeof STOP_SIGNALS)[number];
|
||||||
|
|
||||||
|
export type IdleWatcherConfig = {
|
||||||
|
/* Idle timeout */
|
||||||
|
idle_timeout?: Duration;
|
||||||
|
/** Wake timeout
|
||||||
|
*
|
||||||
|
* @default 30s
|
||||||
|
*/
|
||||||
|
wake_timeout?: Duration;
|
||||||
|
/** Stop timeout
|
||||||
|
*
|
||||||
|
* @default 10s
|
||||||
|
*/
|
||||||
|
stop_timeout?: Duration;
|
||||||
|
/** Stop method
|
||||||
|
*
|
||||||
|
* @default stop
|
||||||
|
*/
|
||||||
|
stop_method?: StopMethod;
|
||||||
|
/* Stop signal */
|
||||||
|
stop_signal?: Signal;
|
||||||
|
/* Start endpoint (any path can wake the container if not specified) */
|
||||||
|
start_endpoint?: URI;
|
||||||
|
};
|
44
schemas/providers/loadbalance.ts
Normal file
44
schemas/providers/loadbalance.ts
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
import { RealIP } from "../middlewares/middlewares";
|
||||||
|
|
||||||
|
export const LOAD_BALANCE_MODES = [
|
||||||
|
"round_robin",
|
||||||
|
"least_conn",
|
||||||
|
"ip_hash",
|
||||||
|
] as const;
|
||||||
|
export type LoadBalanceMode = (typeof LOAD_BALANCE_MODES)[number];
|
||||||
|
|
||||||
|
export type LoadBalanceConfigBase = {
|
||||||
|
/** Alias (subdomain or FDN) of load-balancer
|
||||||
|
*
|
||||||
|
* @minLength 1
|
||||||
|
*/
|
||||||
|
link: string;
|
||||||
|
/** Load-balance weight (reserved for future use)
|
||||||
|
*
|
||||||
|
* @minimum 0
|
||||||
|
* @maximum 100
|
||||||
|
*/
|
||||||
|
weight?: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type LoadBalanceConfig = LoadBalanceConfigBase &
|
||||||
|
(
|
||||||
|
| {} // linking other routes
|
||||||
|
| RoundRobinLoadBalanceConfig
|
||||||
|
| LeastConnLoadBalanceConfig
|
||||||
|
| IPHashLoadBalanceConfig
|
||||||
|
);
|
||||||
|
|
||||||
|
export type IPHashLoadBalanceConfig = {
|
||||||
|
mode: "ip_hash";
|
||||||
|
/** Real IP config, header to get client IP from */
|
||||||
|
config: RealIP;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type LeastConnLoadBalanceConfig = {
|
||||||
|
mode: "least_conn";
|
||||||
|
};
|
||||||
|
|
||||||
|
export type RoundRobinLoadBalanceConfig = {
|
||||||
|
mode: "round_robin";
|
||||||
|
};
|
114
schemas/providers/routes.ts
Normal file
114
schemas/providers/routes.ts
Normal file
|
@ -0,0 +1,114 @@
|
||||||
|
import { AccessLogConfig } from "../config/access_log";
|
||||||
|
import { accessLogExamples } from "../config/entrypoint";
|
||||||
|
import { MiddlewaresMap } from "../middlewares/middlewares";
|
||||||
|
import { Hostname, IPv4, IPv6, PathPattern, Port, StreamPort } from "../types";
|
||||||
|
import { HealthcheckConfig } from "./healthcheck";
|
||||||
|
import { HomepageConfig } from "./homepage";
|
||||||
|
import { LoadBalanceConfig } from "./loadbalance";
|
||||||
|
export const PROXY_SCHEMES = ["http", "https"] as const;
|
||||||
|
export const STREAM_SCHEMES = ["tcp", "udp"] as const;
|
||||||
|
|
||||||
|
export type ProxyScheme = (typeof PROXY_SCHEMES)[number];
|
||||||
|
export type StreamScheme = (typeof STREAM_SCHEMES)[number];
|
||||||
|
|
||||||
|
export type Route = ReverseProxyRoute | StreamRoute;
|
||||||
|
export type Routes = {
|
||||||
|
[key: string]: Route;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ReverseProxyRoute = {
|
||||||
|
/** Alias (subdomain or FDN)
|
||||||
|
* @minLength 1
|
||||||
|
*/
|
||||||
|
alias?: string;
|
||||||
|
/** Proxy scheme
|
||||||
|
*
|
||||||
|
* @default http
|
||||||
|
*/
|
||||||
|
scheme?: ProxyScheme;
|
||||||
|
/** Proxy host
|
||||||
|
*
|
||||||
|
* @default localhost
|
||||||
|
*/
|
||||||
|
host?: Hostname | IPv4 | IPv6;
|
||||||
|
/** Proxy port
|
||||||
|
*
|
||||||
|
* @default 80
|
||||||
|
*/
|
||||||
|
port?: Port;
|
||||||
|
/** Skip TLS verification
|
||||||
|
*
|
||||||
|
* @default false
|
||||||
|
*/
|
||||||
|
no_tls_verify?: boolean;
|
||||||
|
/** Path patterns (only patterns that match will be proxied).
|
||||||
|
*
|
||||||
|
* See https://pkg.go.dev/net/http#hdr-Patterns-ServeMux
|
||||||
|
*/
|
||||||
|
path_patterns?: PathPattern[];
|
||||||
|
/** Healthcheck config */
|
||||||
|
healthcheck?: HealthcheckConfig;
|
||||||
|
/** Load balance config */
|
||||||
|
load_balance?: LoadBalanceConfig;
|
||||||
|
/** Middlewares */
|
||||||
|
middlewares?: MiddlewaresMap;
|
||||||
|
/** Homepage config
|
||||||
|
*
|
||||||
|
* @examples require(".").homepageExamples
|
||||||
|
*/
|
||||||
|
homepage?: HomepageConfig;
|
||||||
|
/** Access log config
|
||||||
|
*
|
||||||
|
* @examples require(".").accessLogExamples
|
||||||
|
*/
|
||||||
|
access_log?: AccessLogConfig;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type StreamRoute = {
|
||||||
|
/** Alias (subdomain or FDN)
|
||||||
|
* @minLength 1
|
||||||
|
*/
|
||||||
|
alias?: string;
|
||||||
|
/** Stream scheme
|
||||||
|
*
|
||||||
|
* @default tcp
|
||||||
|
*/
|
||||||
|
scheme: StreamScheme;
|
||||||
|
/** Stream host
|
||||||
|
*
|
||||||
|
* @default localhost
|
||||||
|
*/
|
||||||
|
host?: Hostname | IPv4 | IPv6;
|
||||||
|
/* Stream port */
|
||||||
|
port: StreamPort;
|
||||||
|
/** Healthcheck config */
|
||||||
|
healthcheck?: HealthcheckConfig;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const homepageExamples = [
|
||||||
|
{
|
||||||
|
name: "Sonarr",
|
||||||
|
icon: "png/sonarr.png",
|
||||||
|
category: "Arr suite",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "App",
|
||||||
|
icon: "@target/favicon.ico",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
export const loadBalanceExamples = [
|
||||||
|
{
|
||||||
|
link: "flaresolverr",
|
||||||
|
mode: "round_robin",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
link: "service.domain.com",
|
||||||
|
mode: "ip_hash",
|
||||||
|
config: {
|
||||||
|
header: "X-Real-IP",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
export { accessLogExamples };
|
1123
schemas/routes.schema.json
Normal file
1123
schemas/routes.schema.json
Normal file
File diff suppressed because it is too large
Load diff
111
schemas/types.ts
Normal file
111
schemas/types.ts
Normal file
|
@ -0,0 +1,111 @@
|
||||||
|
/**
|
||||||
|
* @type "null"
|
||||||
|
*/
|
||||||
|
export interface Null {}
|
||||||
|
export type Nullable<T> = T | Null;
|
||||||
|
export type NullOrEmptyMap = {} | Null;
|
||||||
|
|
||||||
|
export const HTTP_METHODS = [
|
||||||
|
"GET",
|
||||||
|
"POST",
|
||||||
|
"PUT",
|
||||||
|
"PATCH",
|
||||||
|
"DELETE",
|
||||||
|
"CONNECT",
|
||||||
|
"HEAD",
|
||||||
|
"OPTIONS",
|
||||||
|
"TRACE",
|
||||||
|
] as const;
|
||||||
|
|
||||||
|
export type HTTPMethod = (typeof HTTP_METHODS)[number];
|
||||||
|
/**
|
||||||
|
* HTTP Header
|
||||||
|
* @pattern ^[a-zA-Z0-9\-]+$
|
||||||
|
*/
|
||||||
|
export type HTTPHeader = string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HTTP Query
|
||||||
|
* @pattern ^[a-zA-Z0-9\-_]+$
|
||||||
|
*/
|
||||||
|
export type HTTPQuery = string;
|
||||||
|
/**
|
||||||
|
* HTTP Cookie
|
||||||
|
* @pattern ^[a-zA-Z0-9\-_]+$
|
||||||
|
*/
|
||||||
|
export type HTTPCookie = string;
|
||||||
|
|
||||||
|
export type StatusCode = number | `${number}`;
|
||||||
|
export type StatusCodeRange = number | `${number}` | `${number}-${number}`;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @items.pattern ^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$
|
||||||
|
*/
|
||||||
|
export type DomainNames = string[];
|
||||||
|
/**
|
||||||
|
* @items.pattern ^(\*\.)?(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$
|
||||||
|
*/
|
||||||
|
export type DomainOrWildcards = string[];
|
||||||
|
/**
|
||||||
|
* @format hostname
|
||||||
|
*/
|
||||||
|
export type Hostname = string;
|
||||||
|
/**
|
||||||
|
* @format ipv4
|
||||||
|
*/
|
||||||
|
export type IPv4 = string;
|
||||||
|
/**
|
||||||
|
* @format ipv6
|
||||||
|
*/
|
||||||
|
export type IPv6 = string;
|
||||||
|
|
||||||
|
/* CIDR / IPv4 / IPv6 */
|
||||||
|
export type CIDR =
|
||||||
|
| `${number}.${number}.${number}.${number}`
|
||||||
|
| `${string}:${string}:${string}:${string}:${string}:${string}:${string}:${string}`
|
||||||
|
| `${number}.${number}.${number}.${number}/${number}`
|
||||||
|
| `::${number}`
|
||||||
|
| `${string}::/${number}`
|
||||||
|
| `${string}:${string}::/${number}`;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type integer
|
||||||
|
* @minimum 0
|
||||||
|
* @maximum 65535
|
||||||
|
*/
|
||||||
|
export type Port = number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @pattern ^\d+:\d+$
|
||||||
|
*/
|
||||||
|
export type StreamPort = string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @format email
|
||||||
|
*/
|
||||||
|
export type Email = string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @format uri
|
||||||
|
*/
|
||||||
|
export type URL = string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @format uri-reference
|
||||||
|
*/
|
||||||
|
export type URI = string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @pattern ^(?:([A-Z]+) )?(?:([a-zA-Z0-9.-]+)\\/)?(\\/[^\\s]*)$
|
||||||
|
*/
|
||||||
|
export type PathPattern = string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @pattern ^([0-9]+(ms|s|m|h))+$
|
||||||
|
*/
|
||||||
|
export type Duration = string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @format date-time
|
||||||
|
*/
|
||||||
|
export type DateTime = string;
|
Loading…
Add table
Reference in a new issue