diff --git a/README.md b/README.md index 205d93fc..98d673ad 100755 --- a/README.md +++ b/README.md @@ -76,13 +76,13 @@ A lightweight, easy-to-use, and [performant](docs/benchmark_result.md) reverse p ### Environment variables -| Environment Variable | Description | Default | Values | -| ------------------------------ | ----------------------------- | ------- | ------- | -| `GOPROXY_NO_SCHEMA_VALIDATION` | disable schema validation | `false` | boolean | -| `GOPROXY_DEBUG` | enable debug behaviors | `false` | boolean | -| `GOPROXY_HTTP_PORT` | http server port | `80` | integer | -| `GOPROXY_HTTPS_PORT` | http server port (if enabled) | `443` | integer | -| `GOPROXY_API_PORT` | api server port | `8888` | integer | +| Environment Variable | Description | Default | Values | +| ------------------------------ | ------------------------------------------- | ---------------- | ------------- | +| `GOPROXY_NO_SCHEMA_VALIDATION` | disable schema validation | `false` | boolean | +| `GOPROXY_DEBUG` | enable debug behaviors | `false` | boolean | +| `GOPROXY_HTTP_ADDR` | http server listening address | `:80` | `[host]:port` | +| `GOPROXY_HTTPS_ADDR` | https server listening address (if enabled) | `:443` | `[host]:port` | +| `GOPROXY_API_ADDR` | api server listening address | `127.0.0.1:8888` | `[host]:port` | ### Use JSON Schema in VSCode @@ -126,8 +126,6 @@ See [providers.example.yml](providers.example.yml) for examples ## Known issues -- Cert "renewal" is actually obtaining a new cert instead of renewing the existing one - - `autocert` config is not hot-reloadable [🔼Back to top](#table-of-content) diff --git a/README_CHT.md b/README_CHT.md index 1ef0f641..cb50f1ad 100644 --- a/README_CHT.md +++ b/README_CHT.md @@ -6,7 +6,7 @@ [![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=yusing_go-proxy&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=yusing_go-proxy) [![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=yusing_go-proxy&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=yusing_go-proxy) -一個輕量化、易用且[高效](docs/benchmark_result.md)的反向代理工具 +一個輕量化、易用且[高效](docs/benchmark_result.md)的反向代理和端口轉發工具 ## 目錄 @@ -72,10 +72,13 @@ ### 環境變量 -| 環境變量 | 描述 | 默認 | 值 | -| ------------------------------ | ---------------- | ------- | ------- | -| `GOPROXY_NO_SCHEMA_VALIDATION` | 禁用 schema 驗證 | `false` | boolean | -| `GOPROXY_DEBUG` | 啟用調試輸出 | `false` | boolean | +| 環境變量 | 描述 | 默認 | 格式 | +| ------------------------------ | ---------------- | ---------------- | ------------- | +| `GOPROXY_NO_SCHEMA_VALIDATION` | 禁用 schema 驗證 | `false` | boolean | +| `GOPROXY_DEBUG` | 啟用調試輸出 | `false` | boolean | +| `GOPROXY_HTTP_ADDR` | http 收聽地址 | `:80` | `[host]:port` | +| `GOPROXY_HTTPS_ADDR` | https 收聽地址 | `:443` | `[host]:port` | +| `GOPROXY_API_ADDR` | api 收聽地址 | `127.0.0.1:8888` | `[host]:port` | ### VSCode 中使用 JSON Schema @@ -119,8 +122,6 @@ providers: ## 已知問題 -- 證書“更新”實際上是獲取新證書而不是更新現有證書 - - `autocert` 配置不能熱重載 [🔼 返回頂部](#目錄) diff --git a/src/api/v1/utils/net.go b/src/api/v1/utils/net.go index b4da5c6d..a9c7743d 100644 --- a/src/api/v1/utils/net.go +++ b/src/api/v1/utils/net.go @@ -36,7 +36,7 @@ func IsStreamHealthy(scheme, address string) bool { } func ReloadServer() E.NestedError { - resp, err := HttpClient.Post(fmt.Sprintf("http://localhost%v/reload", common.APIHTTPPort), "", nil) + resp, err := HttpClient.Post(fmt.Sprintf("http://localhost%v/reload", common.APIHTTPAddr), "", nil) if err != nil { return E.From(err) } diff --git a/src/common/env.go b/src/common/env.go index 9f793b26..9eec6a92 100644 --- a/src/common/env.go +++ b/src/common/env.go @@ -9,9 +9,9 @@ import ( var ( NoSchemaValidation = getEnvBool("GOPROXY_NO_SCHEMA_VALIDATION") IsDebug = getEnvBool("GOPROXY_DEBUG") - ProxyHTTPPort = ":" + getEnv("GOPROXY_HTTP_PORT", "80") - ProxyHTTPSPort = ":" + getEnv("GOPROXY_HTTPS_PORT", "443") - APIHTTPPort = ":" + getEnv("GOPROXY_API_PORT", "8888") + ProxyHTTPAddr = getEnv("GOPROXY_HTTP_ADDR", ":80") + ProxyHTTPSAddr = getEnv("GOPROXY_HTTPS_ADDR", ":443") + APIHTTPAddr = getEnv("GOPROXY_API_ADDR", "127.0.0.1:8888") ) func getEnvBool(key string) bool { diff --git a/src/main.go b/src/main.go index 0a62db09..7bcb06a2 100755 --- a/src/main.go +++ b/src/main.go @@ -8,7 +8,6 @@ import ( "net/http" "os" "os/signal" - "runtime" "sync" "syscall" "time" @@ -27,8 +26,6 @@ import ( ) func main() { - runtime.GOMAXPROCS(runtime.NumCPU()) - args := common.GetArgs() l := logrus.WithField("module", "main") @@ -136,15 +133,15 @@ func main() { proxyServer := server.InitProxyServer(server.Options{ Name: "proxy", CertProvider: autocert, - HTTPPort: common.ProxyHTTPPort, - HTTPSPort: common.ProxyHTTPSPort, + HTTPAddr: common.ProxyHTTPAddr, + HTTPSAddr: common.ProxyHTTPSAddr, Handler: http.HandlerFunc(R.ProxyHandler), RedirectToHTTPS: cfg.Value().RedirectToHTTPS, }) apiServer := server.InitAPIServer(server.Options{ Name: "api", CertProvider: autocert, - HTTPPort: common.APIHTTPPort, + HTTPAddr: common.APIHTTPAddr, Handler: api.NewHandler(cfg), RedirectToHTTPS: cfg.Value().RedirectToHTTPS, }) diff --git a/src/server/server.go b/src/server/server.go index e99b901c..d054fe54 100644 --- a/src/server/server.go +++ b/src/server/server.go @@ -22,11 +22,9 @@ type server struct { } type Options struct { - Name string - // port (with leading colon) - HTTPPort string - // port (with leading colon) - HTTPSPort string + Name string + HTTPAddr string + HTTPSAddr string CertProvider *autocert.Provider RedirectToHTTPS bool Handler http.Handler @@ -55,22 +53,22 @@ func NewServer(opt Options) (s *server) { certAvailable = err == nil } - if certAvailable && opt.RedirectToHTTPS && opt.HTTPSPort != "" { - httpHandler = redirectToTLSHandler(opt.HTTPSPort) + if certAvailable && opt.RedirectToHTTPS && opt.HTTPSAddr != "" { + httpHandler = redirectToTLSHandler(opt.HTTPSAddr) } else { httpHandler = opt.Handler } - if opt.HTTPPort != "" { + if opt.HTTPAddr != "" { httpSer = &http.Server{ - Addr: opt.HTTPPort, + Addr: opt.HTTPAddr, Handler: httpHandler, ErrorLog: logger, } } - if certAvailable && opt.HTTPSPort != "" { + if certAvailable && opt.HTTPSAddr != "" { httpsSer = &http.Server{ - Addr: opt.HTTPSPort, + Addr: opt.HTTPSAddr, Handler: opt.Handler, ErrorLog: logger, TLSConfig: &tls.Config{