mirror of
https://github.com/WJQSERVER-STUDIO/ghproxy.git
synced 2026-02-03 08:11:11 +08:00
add embed.FS and debug
This commit is contained in:
parent
8371f9564f
commit
83e6b78a93
6 changed files with 31 additions and 7 deletions
2
.github/workflows/build-dev.yml
vendored
2
.github/workflows/build-dev.yml
vendored
|
|
@ -37,7 +37,7 @@ jobs:
|
||||||
GOOS: ${{ matrix.goos }}
|
GOOS: ${{ matrix.goos }}
|
||||||
GOARCH: ${{ matrix.goarch }}
|
GOARCH: ${{ matrix.goarch }}
|
||||||
run: |
|
run: |
|
||||||
CGO_ENABLED=0 go build -ldflags "-X main.version=${{ env.VERSION }}" -o ${{ env.OUTPUT_BINARY }}-${{matrix.goos}}-${{matrix.goarch}} ./main.go
|
CGO_ENABLED=0 go build -ldflags "-X main.version=${{ env.VERSION }} -X main.dev=true" -o ${{ env.OUTPUT_BINARY }}-${{matrix.goos}}-${{matrix.goarch}} ./main.go
|
||||||
- name: 打包
|
- name: 打包
|
||||||
run: |
|
run: |
|
||||||
mkdir ghproxyd
|
mkdir ghproxyd
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ type ServerConfig struct {
|
||||||
Host string `toml:"host"`
|
Host string `toml:"host"`
|
||||||
SizeLimit int `toml:"sizeLimit"`
|
SizeLimit int `toml:"sizeLimit"`
|
||||||
EnableH2C string `toml:"enableH2C"`
|
EnableH2C string `toml:"enableH2C"`
|
||||||
|
Debug bool `toml:"debug"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PagesConfig struct {
|
type PagesConfig struct {
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ host = "127.0.0.1"
|
||||||
port = 8080
|
port = 8080
|
||||||
sizeLimit = 125 # MB
|
sizeLimit = 125 # MB
|
||||||
enableH2C = "on" # "on" or "off"
|
enableH2C = "on" # "on" or "off"
|
||||||
|
debug = false
|
||||||
|
|
||||||
[pages]
|
[pages]
|
||||||
enabled = false
|
enabled = false
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ host = "127.0.0.1"
|
||||||
port = 8080
|
port = 8080
|
||||||
sizeLimit = 125 # MB
|
sizeLimit = 125 # MB
|
||||||
enableH2C = false
|
enableH2C = false
|
||||||
|
debug = false
|
||||||
|
|
||||||
[pages]
|
[pages]
|
||||||
enabled = true
|
enabled = true
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ host = "0.0.0.0"
|
||||||
port = 80 #修改此配置会导致容器异常
|
port = 80 #修改此配置会导致容器异常
|
||||||
sizeLimit = 125 # MB
|
sizeLimit = 125 # MB
|
||||||
enableH2C = "off" # on / off
|
enableH2C = "off" # on / off
|
||||||
|
debug = false
|
||||||
|
|
||||||
[pages]
|
[pages]
|
||||||
enabled = true
|
enabled = true
|
||||||
|
|
|
||||||
30
main.go
30
main.go
|
|
@ -1,8 +1,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"embed"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -24,10 +26,17 @@ var (
|
||||||
configfile = "/data/ghproxy/config/config.toml"
|
configfile = "/data/ghproxy/config/config.toml"
|
||||||
cfgfile string
|
cfgfile string
|
||||||
version string
|
version string
|
||||||
|
dev bool
|
||||||
|
runMode string
|
||||||
limiter *rate.RateLimiter
|
limiter *rate.RateLimiter
|
||||||
iplimiter *rate.IPRateLimiter
|
iplimiter *rate.IPRateLimiter
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
//go:embed pages/*
|
||||||
|
pagesFS embed.FS
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
logw = logger.Logw
|
logw = logger.Logw
|
||||||
logInfo = logger.LogInfo
|
logInfo = logger.LogInfo
|
||||||
|
|
@ -89,7 +98,16 @@ func init() {
|
||||||
loadlist(cfg)
|
loadlist(cfg)
|
||||||
setupRateLimit(cfg)
|
setupRateLimit(cfg)
|
||||||
|
|
||||||
|
if cfg.Server.Debug {
|
||||||
|
dev = true
|
||||||
|
}
|
||||||
|
if dev {
|
||||||
|
gin.SetMode(gin.DebugMode)
|
||||||
|
runMode = "dev"
|
||||||
|
} else {
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
|
runMode = "release"
|
||||||
|
}
|
||||||
|
|
||||||
router = gin.Default()
|
router = gin.Default()
|
||||||
//H2C默认值为true,而后遵循cfg.Server.EnableH2C的设置
|
//H2C默认值为true,而后遵循cfg.Server.EnableH2C的设置
|
||||||
|
|
@ -112,14 +130,16 @@ func init() {
|
||||||
})
|
})
|
||||||
router.StaticFile("/favicon.ico", faviconPath)
|
router.StaticFile("/favicon.ico", faviconPath)
|
||||||
} else if !cfg.Pages.Enabled {
|
} else if !cfg.Pages.Enabled {
|
||||||
router.GET("/", func(c *gin.Context) {
|
pages, err := fs.Sub(pagesFS, "pages")
|
||||||
c.String(http.StatusForbidden, "403 Forbidden Access")
|
if err != nil {
|
||||||
logWarning("403 > Path:/ IP:%s UA:%s METHOD:%s HTTPv:%s", c.ClientIP(), c.Request.UserAgent(), c.Request.Method, c.Request.Proto)
|
log.Fatalf("Failed when processing pages: %s", err)
|
||||||
})
|
}
|
||||||
|
router.GET("/", gin.WrapH(http.FileServer(http.FS(pages))))
|
||||||
|
router.GET("/favicon.ico", gin.WrapH(http.FileServer(http.FS(pages))))
|
||||||
}
|
}
|
||||||
|
|
||||||
router.NoRoute(func(c *gin.Context) {
|
router.NoRoute(func(c *gin.Context) {
|
||||||
proxy.NoRouteHandler(cfg, limiter, iplimiter)(c)
|
proxy.NoRouteHandler(cfg, limiter, iplimiter, runMode)(c)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue