Skip to content

Commit

Permalink
changed env GOPROXY_*_PORT to GOPROXY_*_ADDR, changed api server defa…
Browse files Browse the repository at this point in the history
…ult to listen on localhost only, readme update
  • Loading branch information
yusing committed Sep 21, 2024
1 parent d9fd399 commit 96bce79
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 37 deletions.
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
15 changes: 8 additions & 7 deletions README_CHT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)的反向代理和端口轉發工具

## 目錄

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -119,8 +122,6 @@ providers:
## 已知問題
- 證書“更新”實際上是獲取新證書而不是更新現有證書
- `autocert` 配置不能熱重載

[🔼 返回頂部](#目錄)
Expand Down
2 changes: 1 addition & 1 deletion src/api/v1/utils/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions src/common/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 3 additions & 6 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/http"
"os"
"os/signal"
"runtime"
"sync"
"syscall"
"time"
Expand All @@ -27,8 +26,6 @@ import (
)

func main() {
runtime.GOMAXPROCS(runtime.NumCPU())

args := common.GetArgs()
l := logrus.WithField("module", "main")

Expand Down Expand Up @@ -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,
})
Expand Down
20 changes: 9 additions & 11 deletions src/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 96bce79

Please sign in to comment.