Compare commits

...

17 Commits

Author SHA1 Message Date
WJQSERVER
d7949f625a 1.6.0 2024-10-20 20:14:39 +08:00
WJQSERVER
ddd12729a4 24w18f 2024-10-20 18:56:37 +08:00
WJQSERVER
91b7cfe533 24w18e 2024-10-20 18:16:30 +08:00
WJQSERVER
aee81ba4fd 24w18e 2024-10-20 17:48:31 +08:00
WJQSERVER
e5d941414e 24w18e 2024-10-20 17:39:27 +08:00
WJQSERVER
c50f23c399 24w18d 2024-10-19 16:05:18 +08:00
WJQSERVER
47d062a1c4 hotfix 2024-10-17 18:01:15 +08:00
WJQSERVER
57ba06e01e 24w18c 2024-10-17 17:45:29 +08:00
WJQSERVER
52fdaf5f81 24w18b 2024-10-17 10:43:36 +08:00
WJQSERVER
d6b8f2b812 hotfix 2024-10-17 09:57:14 +08:00
WJQSERVER
00d2113904 hotfix 2024-10-17 00:04:29 +08:00
WJQSERVER
670bca31ca 24w18a 2024-10-16 23:52:07 +08:00
WJQSERVER
f77de0d37a 24w18a 2024-10-16 23:32:25 +08:00
WJQSERVER
1c18ccc363 24w18a 2024-10-16 21:51:24 +08:00
WJQSERVER
a386304d42 update 2024-10-16 17:45:51 +08:00
WJQSERVER
a1fdd0f477 1.5.2 2024-10-16 15:19:10 +08:00
WJQSERVER
e94a98668e 24w17b 2024-10-16 13:08:58 +08:00
20 changed files with 264 additions and 136 deletions

View File

@@ -1,5 +1,63 @@
# 更新日志 # 更新日志
v1.6.0
---
- CHANGE: 优化代码结构,提升性能
- CHANGE: 引入H2C支持,支持无加密HTTP/2请求,一定程度上提升传输性能
- ADD: 在核心程序内加入静态页面支持,支持不通过caddy等web server提供前端页面
- CHANGE: 优化日志记录,带来更多的可观测性
- CHANGE: 改进前端界面,优化用户体验; 对原有Alert提示进行优化改为ShowToast提示
- CHANGE: 规范化部分函数命名,提升可读性; 同时对config.toml内的参数命名进行规范化(部分参数名称已过时,请注意更新)
- CHANGE: 修改日志检查周期,降低检查频率,避免不必要的资源浪费
- ADD: 增加CORS状态API
24w18f
---
- PRE-RELEASE: 此版本是v1.6.0的预发布版本,请勿在生产环境中使用
- CHANGE: 修正前端页面的部分样式问题
- FIX: 修正部分问题
24w18e
---
- PRE-RELEASE: 此版本是预发布版本,请勿在生产环境中使用
- CHANGE: 引入H2C协议支持,支持无加密HTTP/2请求
- ADD: 尝试在核心程序内加入静态页面支持
- CHANGE: 优化日志记录
- CHANGE: 去除部分无用/重复配置
- CHANGE: 规范化部分函数命名
24w18d
---
- PRE-RELEASE: 此版本是预发布版本,请勿在生产环境中使用
- CHANGE: 更新相关依赖库
- ADD: 增加CORS状态API
- CHANGE: 优化部分函数执行顺序
- CHANGE: 优化前端界面
24w18c
---
- PRE-RELEASE: 此版本是预发布版本,请勿在生产环境中使用
- CHANGE: 修正配置命名,改为驼峰式命名
- CHANGE: 修正函数命名
24w18b
---
- PRE-RELEASE: 此版本是预发布版本,请勿在生产环境中使用
- CHANGE: 经团队考量,移除 Docker 代理功能,若造成了不便敬请谅解
- CHANGE: 修改日志检查周期
24w18a
---
- PRE-RELEASE: 此版本是预发布版本,请勿在生产环境中使用
- CHANGE: 改进Docker 代理
- CHANGE: 改进前端页面的copy提示,弃用alert提示
v1.5.2
---
- FIX: 修正flag传入问题
- CHANGE: 去除/路径重定向,改为返回403,并记录对应请求日志
- CHANGE: 优化Proxy模块的日志记录,记录请求详细信息
24w17b 24w17b
--- ---
- PRE-RELEASE: 此版本是v1.5.2的预发布版本,请勿在生产环境中使用 - PRE-RELEASE: 此版本是v1.5.2的预发布版本,请勿在生产环境中使用

View File

@@ -1 +1 @@
24w17b 24w18f

View File

@@ -7,6 +7,8 @@
[DEMO](https://ghproxy.1888866.xyz) [DEMO](https://ghproxy.1888866.xyz)
[TG讨论群组](https://t.me/ghproxy_go)
## 项目说明 ## 项目说明
### 项目特点 ### 项目特点
@@ -75,28 +77,32 @@ wget -O install.sh https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/ma
```toml ```toml
[server] [server]
host = "127.0.0.1" # 监听地址(小白请勿修改) host = "127.0.0.1" # 监听地址
port = 8080 #监听端口(小白请勿修改) port = 8080 # 监听端口
sizelimit = 131072000 # 125MB sizeLimit = 131072000 # 125MB
[pages]
enabled = true # 是否开启内置静态页面
staticPath = "/data/www" # 静态页面文件路径
[log] [log]
logfilepath = "/data/ghproxy/log/ghproxy.log" # 日志文件路径(小白请勿修改) logFilePath = "/data/ghproxy/log/ghproxy.log" # 日志文件路径
maxlogsize = 5 # MB maxLogSize = 5 # MB 日志文件最大大小
[cors] [cors]
enabled = true # 是否开启CORS enabled = true # 是否开启跨域
[auth] [auth]
authtoken = "test" # 鉴权Token authToken = "token" # 用户鉴权Token
enabled = false # 是否开启鉴权 enabled = false # 是否开启用户鉴权
[blacklist] [blacklist]
blacklistfile = "/data/ghproxy/config/blacklist.json" # 黑名单文件路径 blacklistFile = "/data/ghproxy/config/blacklist.json" # 黑名单文件路径
enabled = false # 是否开启黑名单 enabled = false # 是否开启黑名单
[whitelist] [whitelist]
enabled = false # 是否开启白名单 enabled = false # 是否开启白名单
whitelistfile = "/data/ghproxy/config/whitelist.json" # 白名单文件路径 whitelistFile = "/data/ghproxy/config/whitelist.json" # 白名单文件路径
``` ```

View File

@@ -16,6 +16,8 @@
使用本项目,请遵循 **[WSL (WJQSERVER-STUDIO LICENSE)](https://wjqserver-studio.github.io/LICENSE/LICENSE.html)** 协议。 使用本项目,请遵循 **[WSL (WJQSERVER-STUDIO LICENSE)](https://wjqserver-studio.github.io/LICENSE/LICENSE.html)** 协议。
本项目所有文件均受到 WSL (WJQSERVER-STUDIO LICENSE) 协议保护,任何人不得在任何情况下以非 WSL (WJQSERVER-STUDIO LICENSE) 协议内规定的方式使用,复制,修改,编译,发布,分发,再许可,或者出售本项目的任何部分。
## 报告漏洞 ## 报告漏洞
如果您发现本项目存在安全漏洞,请通过发送ISSUES或尝试联系项目维护者来报告。请在您的报告中包含以下信息: 如果您发现本项目存在安全漏洞,请通过发送ISSUES或尝试联系项目维护者来报告。请在您的报告中包含以下信息:

View File

@@ -1 +1 @@
1.5.1 1.6.0

View File

@@ -17,7 +17,7 @@ var (
var ( var (
logw = logger.Logw logw = logger.Logw
logInfo = logger.LogInfo logInfo = logger.LogInfo
LogWarning = logger.LogWarning logWarning = logger.LogWarning
logError = logger.LogError logError = logger.LogError
) )
@@ -34,6 +34,9 @@ func InitHandleRouter(cfg *config.Config, router *gin.Engine) {
apiRouter.GET("/blacklist/status", func(c *gin.Context) { apiRouter.GET("/blacklist/status", func(c *gin.Context) {
BlackListStatusHandler(c, cfg) BlackListStatusHandler(c, cfg)
}) })
apiRouter.GET("/cors/status", func(c *gin.Context) {
CorsStatusHandler(c, cfg)
})
apiRouter.GET("/healthcheck", func(c *gin.Context) { apiRouter.GET("/healthcheck", func(c *gin.Context) {
HealthcheckHandler(c) HealthcheckHandler(c)
}) })
@@ -42,9 +45,7 @@ func InitHandleRouter(cfg *config.Config, router *gin.Engine) {
} }
func SizeLimitHandler(cfg *config.Config, c *gin.Context) { func SizeLimitHandler(cfg *config.Config, c *gin.Context) {
// 转换为MB
sizeLimit := cfg.Server.SizeLimit / 1024 / 1024 sizeLimit := cfg.Server.SizeLimit / 1024 / 1024
// 设置响应头
c.Writer.Header().Set("Content-Type", "application/json") c.Writer.Header().Set("Content-Type", "application/json")
json.NewEncoder(c.Writer).Encode(map[string]interface{}{ json.NewEncoder(c.Writer).Encode(map[string]interface{}{
"MaxResponseBodySize": sizeLimit, "MaxResponseBodySize": sizeLimit,
@@ -52,7 +53,6 @@ func SizeLimitHandler(cfg *config.Config, c *gin.Context) {
} }
func WhiteListStatusHandler(c *gin.Context, cfg *config.Config) { func WhiteListStatusHandler(c *gin.Context, cfg *config.Config) {
// 设置响应头
c.Writer.Header().Set("Content-Type", "application/json") c.Writer.Header().Set("Content-Type", "application/json")
json.NewEncoder(c.Writer).Encode(map[string]interface{}{ json.NewEncoder(c.Writer).Encode(map[string]interface{}{
"Whitelist": cfg.Whitelist.Enabled, "Whitelist": cfg.Whitelist.Enabled,
@@ -60,15 +60,20 @@ func WhiteListStatusHandler(c *gin.Context, cfg *config.Config) {
} }
func BlackListStatusHandler(c *gin.Context, cfg *config.Config) { func BlackListStatusHandler(c *gin.Context, cfg *config.Config) {
// 设置响应头
c.Writer.Header().Set("Content-Type", "application/json") c.Writer.Header().Set("Content-Type", "application/json")
json.NewEncoder(c.Writer).Encode(map[string]interface{}{ json.NewEncoder(c.Writer).Encode(map[string]interface{}{
"Blacklist": cfg.Blacklist.Enabled, "Blacklist": cfg.Blacklist.Enabled,
}) })
} }
func CorsStatusHandler(c *gin.Context, cfg *config.Config) {
c.Writer.Header().Set("Content-Type", "application/json")
json.NewEncoder(c.Writer).Encode(map[string]interface{}{
"Cors": cfg.CORS.Enabled,
})
}
func HealthcheckHandler(c *gin.Context) { func HealthcheckHandler(c *gin.Context) {
// 设置响应头
c.Writer.Header().Set("Content-Type", "application/json") c.Writer.Header().Set("Content-Type", "application/json")
json.NewEncoder(c.Writer).Encode(map[string]interface{}{ json.NewEncoder(c.Writer).Encode(map[string]interface{}{
"Status": "OK", "Status": "OK",

View File

@@ -11,7 +11,7 @@ import (
var ( var (
logw = logger.Logw logw = logger.Logw
logInfo = logger.LogInfo logInfo = logger.LogInfo
LogWarning = logger.LogWarning logWarning = logger.LogWarning
logError = logger.LogError logError = logger.LogError
) )
@@ -38,13 +38,13 @@ func AuthHandler(c *gin.Context, cfg *config.Config) bool {
// 验证 token // 验证 token
if authToken == "" { if authToken == "" {
LogWarning("auth FAILED: no auth_token provided") logWarning("auth FAILED: no auth_token provided")
return false return false
} }
isValid := authToken == cfg.Auth.AuthToken isValid := authToken == cfg.Auth.AuthToken
if !isValid { if !isValid {
LogWarning("auth FAILED: invalid auth_token: %s", authToken) logWarning("auth FAILED: invalid auth_token: %s", authToken)
} }
logInfo("auth SUCCESS: %t", isValid) logInfo("auth SUCCESS: %t", isValid)

View File

@@ -68,7 +68,7 @@
:80 { :80 {
reverse_proxy { reverse_proxy {
to 127.0.0.1:8080 to h2c://127.0.0.1:8080
import header_realip import header_realip
} }
import log ghproxy import log ghproxy
@@ -76,8 +76,6 @@
import error_page import error_page
import encode import encode
import rate_limit 60 import rate_limit 60
header Age 10
header Cache-Control "max-age=300"
route / { route / {
root /data/www root /data/www
file_server file_server
@@ -88,22 +86,11 @@
file_server file_server
import cache 0s 24h import cache 0s 24h
} }
handle_errors {
@redirects `{err.status_code} in [301, 302, 307]`
reverse_proxy @redirects {
header_up Location {http.response.header.Location}
}
}
route /v2* { route /api* {
reverse_proxy https://registry-1.docker.io { rate_limit {remote.ip} 15r/m 10000 429
header_up Host registry-1.docker.io import cache 0s 6h
header_up X-Real-IP {remote} }
header_up X-Forwarded-For {http.request.header.X-Forwarded-For}
header_up X-Forwarded-Proto {scheme}
header_up Authorization {http.request.header.Authorization}
}
}
} }
import /data/caddy/config.d/* import /data/caddy/config.d/*

View File

@@ -68,16 +68,14 @@
:80 { :80 {
reverse_proxy { reverse_proxy {
to 127.0.0.1:8080 to h2c://127.0.0.1:8080
import header_realip import header_realip
} }
import log ghproxy import log ghproxy
import cache 0s 300s import cache 0s 300s
import error_page import error_page
import encode import encode
route /* { import rate_limit 60
rate_limit {remote.ip} 60r/m 10000 429
}
route / { route / {
root /data/www root /data/www
file_server file_server
@@ -87,6 +85,12 @@
root /data/www root /data/www
file_server file_server
import cache 0s 24h import cache 0s 24h
}
route /api* {
rate_limit {remote.ip} 15r/m 10000 429
import cache 0s 6h
} }
} }

View File

@@ -6,6 +6,7 @@ import (
type Config struct { type Config struct {
Server ServerConfig Server ServerConfig
Pages PagesConfig
Log LogConfig Log LogConfig
CORS CORSConfig CORS CORSConfig
Auth AuthConfig Auth AuthConfig
@@ -16,12 +17,17 @@ type Config struct {
type ServerConfig struct { type ServerConfig struct {
Port int `toml:"port"` Port int `toml:"port"`
Host string `toml:"host"` Host string `toml:"host"`
SizeLimit int `toml:"sizelimit"` SizeLimit int `toml:"sizeLimit"`
}
type PagesConfig struct {
Enabled bool `toml:"enabled"`
StaticDir string `toml:"staticDir"`
} }
type LogConfig struct { type LogConfig struct {
LogFilePath string `toml:"logfilepath"` LogFilePath string `toml:"logFilePath"`
MaxLogSize int `toml:"maxlogsize"` MaxLogSize int `toml:"maxLogSize"`
} }
type CORSConfig struct { type CORSConfig struct {
@@ -30,17 +36,17 @@ type CORSConfig struct {
type AuthConfig struct { type AuthConfig struct {
Enabled bool `toml:"enabled"` Enabled bool `toml:"enabled"`
AuthToken string `toml:"authtoken"` AuthToken string `toml:"authToken"`
} }
type BlacklistConfig struct { type BlacklistConfig struct {
Enabled bool `toml:"enabled"` Enabled bool `toml:"enabled"`
BlacklistFile string `toml:"blacklistfile"` BlacklistFile string `toml:"blacklistFile"`
} }
type WhitelistConfig struct { type WhitelistConfig struct {
Enabled bool `toml:"enabled"` Enabled bool `toml:"enabled"`
WhitelistFile string `toml:"whitelistfile"` WhitelistFile string `toml:"whitelistFile"`
} }
// LoadConfig 从 TOML 配置文件加载配置 // LoadConfig 从 TOML 配置文件加载配置

View File

@@ -1,23 +1,27 @@
[server] [server]
host = "127.0.0.1" host = "127.0.0.1"
port = 8080 port = 8080
sizelimit = 131072000 # 125MB sizeLimit = 131072000 # 125MB
[pages]
enabled = false
staticDir = "/data/www"
[log] [log]
logfilepath = "/data/ghproxy/log/ghproxy.log" logFilePath = "/data/ghproxy/log/ghproxy.log"
maxlogsize = 5 # MB maxLogSize = 5 # MB
[cors] [cors]
enabled = true enabled = true
[auth] [auth]
authtoken = "test" authToken = "token"
enabled = false enabled = false
[blacklist] [blacklist]
blacklistfile = "/data/ghproxy/config/blacklist.json" blacklistFile = "/data/ghproxy/config/blacklist.json"
enabled = false enabled = false
[whitelist] [whitelist]
enabled = false enabled = false
whitelistfile = "/data/ghproxy/config/whitelist.json" whitelistFile = "/data/ghproxy/config/whitelist.json"

View File

@@ -1,23 +1,27 @@
[server] [server]
host = "127.0.0.1" host = "127.0.0.1"
port = 8080 port = 8080
sizelimit = 131072000 # 125MB sizeLimit = 131072000 # 125MB
[pages]
enabled = true
staticDir = "/root/data/ghproxy/pages"
[log] [log]
logfilepath = "/root/data/ghproxy/log/ghproxy.log" logFilePath = "/root/data/ghproxy/log/ghproxy.log"
maxlogsize = 5 # MB maxLogSize = 5 # MB
[cors] [cors]
enabled = true enabled = true
[auth] [auth]
authtoken = "test" authToken = "token"
enabled = false enabled = false
[blacklist] [blacklist]
blacklistfile = "/root/data/ghproxy/config/blacklist.json" blacklistFile = "/root/data/ghproxy/config/blacklist.json"
enabled = false enabled = false
[whitelist] [whitelist]
enabled = false enabled = false
whitelistfile = "/root/data/ghproxy/config/whitelist.json" whitelistFile = "/root/data/ghproxy/config/whitelist.json"

View File

@@ -53,27 +53,35 @@ fi
mkdir -p /root/data/ghproxy mkdir -p /root/data/ghproxy
mkdir -p /root/data/ghproxy/config mkdir -p /root/data/ghproxy/config
mkdir -p /root/data/ghproxy/log mkdir -p /root/data/ghproxy/log
mkdir -p /root/data/ghproxy/pages
# 获取最新版本号 # 获取最新版本号
VERSION=$(curl -s https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/DEV-VERSION) VERSION=$(curl -s https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/DEV-VERSION)
wget -O /root/data/ghproxy/VERSION https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/DEV-VERSION wget -q -O /root/data/ghproxy/VERSION https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/DEV-VERSION
# 下载ghproxy # 下载ghproxy
wget -O /root/data/ghproxy/ghproxy https://github.com/WJQSERVER-STUDIO/ghproxy/releases/download/$VERSION/ghproxy-linux-$ARCH wget -q -O /root/data/ghproxy/ghproxy https://github.com/WJQSERVER-STUDIO/ghproxy/releases/download/$VERSION/ghproxy-linux-$ARCH
chmod +x /root/data/ghproxy/ghproxy chmod +x /root/data/ghproxy/ghproxy
# 下载pages
wget -q -O /root/data/ghproxy/pages/index.html https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/pages/index.html
wget -q -O /root/data/ghproxy/pages/favicon.ico https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/pages/favicon.ico
# 下载配置文件 # 下载配置文件
if [ -f /root/data/ghproxy/config/config.toml ]; then if [ -f /root/data/ghproxy/config/config.toml ]; then
echo "配置文件已存在, 跳过下载" echo "配置文件已存在, 跳过下载"
echo "[WARNING] 请检查配置文件是否正确DEV版本升级时请注意配置文件兼容性"
sleep 2
else else
wget -O /root/data/ghproxy/config/config.toml https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/config.toml wget -q -O /root/data/ghproxy/config/config.toml https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/deploy/config.toml
fi fi
# 替换 port = 8080 # 替换 port = 8080
sed -i "s/port = 8080/port = $PORT/g" /root/data/ghproxy/config/config.toml sed -i "s/port = 8080/port = $PORT/g" /root/data/ghproxy/config/config.toml
# 下载systemd服务文件 # 下载systemd服务文件
wget -O /etc/systemd/system/ghproxy.service https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/deploy/ghproxy.service wget -q -O /etc/systemd/system/ghproxy.service https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/deploy/ghproxy.service
# 启动ghproxy # 启动ghproxy
systemctl daemon-reload systemctl daemon-reload

View File

@@ -28,7 +28,7 @@ install() {
} }
# 安装依赖包 # 安装依赖包
install curl wget sed install curl wget -q sed
# 查看当前架构是否为linux/amd64或linux/arm64 # 查看当前架构是否为linux/amd64或linux/arm64
ARCH=$(uname -m) ARCH=$(uname -m)
@@ -56,24 +56,26 @@ mkdir -p /root/data/ghproxy/log
# 获取最新版本号 # 获取最新版本号
VERSION=$(curl -s https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/VERSION) VERSION=$(curl -s https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/VERSION)
wget -O /root/data/ghproxy/VERSION https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/VERSION wget -q -O /root/data/ghproxy/VERSION https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/VERSION
# 下载ghproxy # 下载ghproxy
wget -O /root/data/ghproxy/ghproxy https://github.com/WJQSERVER-STUDIO/ghproxy/releases/download/$VERSION/ghproxy-linux-$ARCH wget -q -O /root/data/ghproxy/ghproxy https://github.com/WJQSERVER-STUDIO/ghproxy/releases/download/$VERSION/ghproxy-linux-$ARCH
chmod +x /root/data/ghproxy/ghproxy chmod +x /root/data/ghproxy/ghproxy
# 下载配置文件 # 下载配置文件
if [ -f /root/data/ghproxy/config/config.toml ]; then if [ -f /root/data/ghproxy/config/config.toml ]; then
echo "配置文件已存在, 跳过下载" echo "配置文件已存在, 跳过下载"
echo "请检查配置文件是否正确,跨大版本升级时请注意配置文件兼容性"
sleep 2
else else
wget -O /root/data/ghproxy/config/config.toml https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/config.toml wget -q -O /root/data/ghproxy/config/config.toml https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/deploy/config.toml
fi fi
# 替换 port = 8080 # 替换 port = 8080
sed -i "s/port = 8080/port = $PORT/g" /root/data/ghproxy/config/config.toml sed -i "s/port = 8080/port = $PORT/g" /root/data/ghproxy/config/config.toml
# 下载systemd服务文件 # 下载systemd服务文件
wget -O /etc/systemd/system/ghproxy.service https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/deploy/ghproxy.service wget -q -O /etc/systemd/system/ghproxy.service https://raw.githubusercontent.com/WJQSERVER-STUDIO/ghproxy/main/deploy/ghproxy.service
# 启动ghproxy # 启动ghproxy
systemctl daemon-reload systemctl daemon-reload

8
go.mod
View File

@@ -15,14 +15,14 @@ require (
github.com/cloudflare/circl v1.5.0 // indirect github.com/cloudflare/circl v1.5.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect github.com/cloudwego/iasm v0.2.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.5 // indirect github.com/gabriel-vasile/mimetype v1.4.6 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.22.1 // indirect github.com/go-playground/validator/v10 v10.22.1 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/goccy/go-json v0.10.3 // indirect github.com/goccy/go-json v0.10.3 // indirect
github.com/google/pprof v0.0.0-20241009165004-a3522334989c // indirect github.com/google/pprof v0.0.0-20241017200806-017d972448fc // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect github.com/json-iterator/go v1.1.12 // indirect
@@ -35,11 +35,11 @@ require (
github.com/onsi/ginkgo/v2 v2.20.2 // indirect github.com/onsi/ginkgo/v2 v2.20.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/quic-go/qpack v0.5.1 // indirect github.com/quic-go/qpack v0.5.1 // indirect
github.com/quic-go/quic-go v0.47.0 // indirect github.com/quic-go/quic-go v0.48.0 // indirect
github.com/refraction-networking/utls v1.6.7 // indirect github.com/refraction-networking/utls v1.6.7 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect github.com/ugorji/go/codec v1.2.12 // indirect
go.uber.org/mock v0.4.0 // indirect go.uber.org/mock v0.5.0 // indirect
golang.org/x/arch v0.11.0 // indirect golang.org/x/arch v0.11.0 // indirect
golang.org/x/crypto v0.28.0 // indirect golang.org/x/crypto v0.28.0 // indirect
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect

9
go.sum
View File

@@ -18,6 +18,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v1.4.5 h1:J7wGKdGu33ocBOhGy0z653k/lFKLFDPJMG8Gql0kxn4= github.com/gabriel-vasile/mimetype v1.4.5 h1:J7wGKdGu33ocBOhGy0z653k/lFKLFDPJMG8Gql0kxn4=
github.com/gabriel-vasile/mimetype v1.4.5/go.mod h1:ibHel+/kbxn9x2407k1izTA1S81ku1z/DlgOW2QE0M4= github.com/gabriel-vasile/mimetype v1.4.5/go.mod h1:ibHel+/kbxn9x2407k1izTA1S81ku1z/DlgOW2QE0M4=
github.com/gabriel-vasile/mimetype v1.4.6 h1:3+PzJTKLkvgjeTbts6msPJt4DixhT4YtFNf1gtGe3zc=
github.com/gabriel-vasile/mimetype v1.4.6/go.mod h1:JX1qVKqZd40hUPpAfiNTe0Sne7hdfKSbOqqmkq8GCXc=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
@@ -32,6 +34,7 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
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.22.1 h1:40JcKH+bBNGFczGuoBYgX4I6m/i27HYW8P9FDk5PbgA= github.com/go-playground/validator/v10 v10.22.1 h1:40JcKH+bBNGFczGuoBYgX4I6m/i27HYW8P9FDk5PbgA=
github.com/go-playground/validator/v10 v10.22.1/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= github.com/go-playground/validator/v10 v10.22.1/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI= github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8= github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA= github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
@@ -41,6 +44,8 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/pprof v0.0.0-20241009165004-a3522334989c h1:NDovD0SMpBYXlE1zJmS1q55vWB/fUQBcPAqAboZSccA= github.com/google/pprof v0.0.0-20241009165004-a3522334989c h1:NDovD0SMpBYXlE1zJmS1q55vWB/fUQBcPAqAboZSccA=
github.com/google/pprof v0.0.0-20241009165004-a3522334989c/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144= github.com/google/pprof v0.0.0-20241009165004-a3522334989c/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
github.com/google/pprof v0.0.0-20241017200806-017d972448fc h1:NGyrhhFhwvRAZg02jnYVg3GBQy0qGBKmFQJwaPmpmxs=
github.com/google/pprof v0.0.0-20241017200806-017d972448fc/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I= github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
@@ -77,6 +82,8 @@ github.com/quic-go/qpack v0.5.1 h1:giqksBPnT/HDtZ6VhtFKgoLOWmlyo9Ei6u9PqzIMbhI=
github.com/quic-go/qpack v0.5.1/go.mod h1:+PC4XFrEskIVkcLzpEkbLqq1uCoxPhQuvK5rH1ZgaEg= github.com/quic-go/qpack v0.5.1/go.mod h1:+PC4XFrEskIVkcLzpEkbLqq1uCoxPhQuvK5rH1ZgaEg=
github.com/quic-go/quic-go v0.47.0 h1:yXs3v7r2bm1wmPTYNLKAAJTHMYkPEsfYJmTazXrCZ7Y= github.com/quic-go/quic-go v0.47.0 h1:yXs3v7r2bm1wmPTYNLKAAJTHMYkPEsfYJmTazXrCZ7Y=
github.com/quic-go/quic-go v0.47.0/go.mod h1:3bCapYsJvXGZcipOHuu7plYtaV6tnF+z7wIFsU0WK9E= github.com/quic-go/quic-go v0.47.0/go.mod h1:3bCapYsJvXGZcipOHuu7plYtaV6tnF+z7wIFsU0WK9E=
github.com/quic-go/quic-go v0.48.0 h1:2TCyvBrMu1Z25rvIAlnp2dPT4lgh/uTqLqiXVpp5AeU=
github.com/quic-go/quic-go v0.48.0/go.mod h1:yBgs3rWBOADpga7F+jJsb6Ybg1LSYiQvwWlLX+/6HMs=
github.com/refraction-networking/utls v1.6.7 h1:zVJ7sP1dJx/WtVuITug3qYUq034cDq9B2MR1K67ULZM= github.com/refraction-networking/utls v1.6.7 h1:zVJ7sP1dJx/WtVuITug3qYUq034cDq9B2MR1K67ULZM=
github.com/refraction-networking/utls v1.6.7/go.mod h1:BC3O4vQzye5hqpmDTWUqi4P5DDhzJfkV1tdqtawQIH0= github.com/refraction-networking/utls v1.6.7/go.mod h1:BC3O4vQzye5hqpmDTWUqi4P5DDhzJfkV1tdqtawQIH0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@@ -97,6 +104,8 @@ github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZ
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E= github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU= go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc= go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU=
go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM=
golang.org/x/arch v0.11.0 h1:KXV8WWKCXm6tRpLirl2szsO5j/oOODwZf4hATmGVNs4= golang.org/x/arch v0.11.0 h1:KXV8WWKCXm6tRpLirl2szsO5j/oOODwZf4hATmGVNs4=
golang.org/x/arch v0.11.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/arch v0.11.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw= golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=

View File

@@ -18,11 +18,11 @@ var (
logger *log.Logger logger *log.Logger
logChannel = make(chan string, 100) logChannel = make(chan string, 100)
quitChannel = make(chan struct{}) quitChannel = make(chan struct{})
logFileMutex sync.Mutex // 保护 logFile 的互斥锁 logFileMutex sync.Mutex
logFilePath = "/data/ghproxy/log/ghproxy.log" logFilePath = "/data/ghproxy/log/ghproxy.log"
) )
// Init 初始化日志记录器,接受日志文件路径作为参数 // 初始化,接受日志文件路径作为参数
func Init(logFilePath_input string, maxLogsize int) error { func Init(logFilePath_input string, maxLogsize int) error {
logFileMutex.Lock() logFileMutex.Lock()
defer logFileMutex.Unlock() defer logFileMutex.Unlock()
@@ -40,7 +40,6 @@ func Init(logFilePath_input string, maxLogsize int) error {
return nil return nil
} }
// logWorker 处理日志记录
func logWorker() { func logWorker() {
for { for {
select { select {
@@ -53,38 +52,37 @@ func logWorker() {
} }
} }
// Log 直接记录日志的函数
func Log(customMessage string) { func Log(customMessage string) {
logChannel <- customMessage logChannel <- customMessage
} }
// Logw 用于格式化日志记录 // 格式化日志记录
func Logw(format string, args ...interface{}) { func Logw(format string, args ...interface{}) {
message := fmt.Sprintf(format, args...) message := fmt.Sprintf(format, args...)
Log(message) Log(message)
} }
// 日志等级INFO // INFO
func LogInfo(format string, args ...interface{}) { func LogInfo(format string, args ...interface{}) {
message := fmt.Sprintf(format, args...) message := fmt.Sprintf(format, args...)
output := fmt.Sprintf("[INFO] %s", message) output := fmt.Sprintf("[INFO] %s", message)
Log(output) Log(output)
} }
// 日志等级WARNING // WARNING
func LogWarning(format string, args ...interface{}) { func LogWarning(format string, args ...interface{}) {
message := fmt.Sprintf(format, args...) message := fmt.Sprintf(format, args...)
output := fmt.Sprintf("[WARNING] %s", message) output := fmt.Sprintf("[WARNING] %s", message)
Log(output) Log(output)
} }
// 日志等级ERROR // ERROR
func LogError(format string, args ...interface{}) { func LogError(format string, args ...interface{}) {
message := fmt.Sprintf(format, args...) message := fmt.Sprintf(format, args...)
Log(message) Log(message)
} }
// Close 关闭日志文件 // 关闭日志文件
func Close() { func Close() {
logFileMutex.Lock() logFileMutex.Lock()
defer logFileMutex.Unlock() defer logFileMutex.Unlock()
@@ -100,7 +98,7 @@ func Close() {
func monitorLogSize(logFilePath string, maxLogsize int) { func monitorLogSize(logFilePath string, maxLogsize int) {
var maxLogsizeBytes int64 = int64(maxLogsize) * 1024 * 1024 // 最大日志文件大小单位为MB var maxLogsizeBytes int64 = int64(maxLogsize) * 1024 * 1024 // 最大日志文件大小单位为MB
for { for {
time.Sleep(600 * time.Second) // 每10分钟检查一次 time.Sleep(120 * time.Minute) // 每120分钟检查一次日志文件大小
logFileMutex.Lock() logFileMutex.Lock()
info, err := logFile.Stat() info, err := logFile.Stat()
logFileMutex.Unlock() logFileMutex.Unlock()

38
main.go
View File

@@ -26,11 +26,11 @@ var (
var ( var (
logw = logger.Logw logw = logger.Logw
logInfo = logger.LogInfo logInfo = logger.LogInfo
LogWarning = logger.LogWarning logWarning = logger.LogWarning
logError = logger.LogError logError = logger.LogError
) )
func ReadFlag() { func readFlag() {
flag.StringVar(&cfgfile, "cfg", configfile, "config file path") flag.StringVar(&cfgfile, "cfg", configfile, "config file path")
} }
@@ -57,37 +57,45 @@ func setupLogger(cfg *config.Config) {
logInfo("Init Completed") logInfo("Init Completed")
} }
func Loadlist(cfg *config.Config) { func loadlist(cfg *config.Config) {
auth.Init(cfg) auth.Init(cfg)
} }
func setupApi(cfg *config.Config, router *gin.Engine) { func setupApi(cfg *config.Config, router *gin.Engine) {
// 注册 API 接口
api.InitHandleRouter(cfg, router) api.InitHandleRouter(cfg, router)
} }
func init() { func init() {
ReadFlag() readFlag()
flag.Parse() flag.Parse()
loadConfig() loadConfig()
setupLogger(cfg) setupLogger(cfg)
Loadlist(cfg) loadlist(cfg)
// 设置 Gin 模式
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
// 初始化路由
router = gin.Default() router = gin.Default()
router.UseH2C = true
setupApi(cfg, router) setupApi(cfg, router)
// 定义路由 if cfg.Pages.Enabled {
router.GET("/", func(c *gin.Context) { indexPagePath := fmt.Sprintf("%s/index.html", cfg.Pages.StaticDir)
// 返回403错误 faviconPath := fmt.Sprintf("%s/favicon.ico", cfg.Pages.StaticDir)
c.String(http.StatusForbidden, "403 Forbidden This route is not allowed to access.") // 静态index页
// 记录访问者IP UA METHOD //router.StaticFile("/", indexPagePath)
LogWarning("Forbidden: IP:%s UA:%s METHOD:%s", c.ClientIP(), c.Request.UserAgent(), c.Request.Method) router.GET("/", func(c *gin.Context) {
}) c.File(indexPagePath)
logInfo("IP:%s UA:%s METHOD:%s HTTPv:%s", c.ClientIP(), c.Request.UserAgent(), c.Request.Method, c.Request.Proto)
})
// 静态favicon.ico
router.StaticFile("/favicon.ico", faviconPath)
} else if !cfg.Pages.Enabled {
router.GET("/", func(c *gin.Context) {
c.String(http.StatusForbidden, "403 Forbidden This route is not allowed to access.")
logWarning("Forbidden: IP:%s UA:%s METHOD:%s HTTPv:%s", c.ClientIP(), c.Request.UserAgent(), c.Request.Method, c.Request.Proto)
})
}
// 未匹配路由处理 // 未匹配路由处理
router.NoRoute(func(c *gin.Context) { router.NoRoute(func(c *gin.Context) {

View File

@@ -10,7 +10,7 @@
<link rel="stylesheet" href="https://font.sec.miui.com/font/css?family=MiSans:400,700:MiSans"> <link rel="stylesheet" href="https://font.sec.miui.com/font/css?family=MiSans:400,700:MiSans">
<style> <style>
body { body {
background-color: #f8f9fac5; background-color: #ecececf3;
font-family: 'Misans', Arial, sans-serif; font-family: 'Misans', Arial, sans-serif;
padding: 30px; padding: 30px;
display: flex; display: flex;
@@ -30,15 +30,15 @@
h1 { h1 {
font-weight: bold; font-weight: bold;
margin-bottom: 75px; margin-bottom: 85px;
} }
.rounded-button { .rounded-button {
border-radius: 6px; border-radius: 6px;
transition: background-color 0.3s, transform 0.2s; transition: background-color 0.3s, transform 0.2s;
padding: 10px 30px; padding: 10px 30px;
background-color: #39c5bb; background-color: #555c5c;
color: white; color: rgb(255, 255, 255);
border: none; border: none;
margin-bottom: 10px; margin-bottom: 10px;
} }
@@ -55,6 +55,7 @@
} }
footer { footer {
line-height: 1.25;
position: absolute; position: absolute;
bottom: 0; bottom: 0;
left: 0; left: 0;
@@ -63,8 +64,8 @@
} }
pre { pre {
background: #2d2d2d; background: #012333;
color: #f8f8f2; color: #39c5bc;
padding: 20px 20px; padding: 20px 20px;
margin: 10px 0; margin: 10px 0;
border-radius: 8px; border-radius: 8px;
@@ -80,15 +81,14 @@
left: 10px; left: 10px;
width: 12px; width: 12px;
height: 12px; height: 12px;
background: #ff5f56; background: #bd3c35;
border-radius: 50%; border-radius: 50%;
box-shadow: 20px 0 0 #ffbd2e, 40px 0 0 #27c93f; box-shadow: 20px 0 0 #d69f27, 40px 0 0 #39c5bb;
} }
code { code {
font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
font-size: 0.875em; font-size: 1em;
} }
.status-container { .status-container {
@@ -112,7 +112,7 @@
position: absolute; position: absolute;
top: 5px; top: 5px;
right: 10px; right: 10px;
background: rgba(118, 119, 121, 0.7); background: rgba(0, 217, 224, 0.822);
color: white; color: white;
border: none; border: none;
padding: 5px 10px; padding: 5px 10px;
@@ -129,7 +129,20 @@
#visitor-info { #visitor-info {
margin-top: 10px; margin-top: 10px;
text-align: center; text-align: center;
line-height: 0.5; line-height: 0;
}
#toast {
position: fixed;
top: 10%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #39c5bcde;
color: white;
padding: 15px 20px;
border-radius: 10px;
font-size: 16px;
z-index: 1000;
} }
</style> </style>
</head> </head>
@@ -138,12 +151,12 @@
<div class="container"> <div class="container">
<h1>Github文件加速</h1> <h1>Github文件加速</h1>
<div class="form-group"> <div class="form-group">
<input type="text" class="form-control" id="githubLinkInput" placeholder="键入Github链接"> <input type="text" class="form-control" id="githubLinkInput" placeholder="键入需要代理Github链接">
</div> </div>
<button class="btn rounded-button" id="formatButton">获取文件链接</button> <button class="btn rounded-button" id="formatButton">获取文件链接</button>
<div class="code" id="outputBlock"> <div class="code" id="outputBlock">
<button class="copy-button" id="copyButton" onclick="copyCode(this)">Copy</button> <button class="copy-button" id="copyButton">复制</button>
<pre id="formattedLinkOutput"></pre> <pre id="formattedLinkOutput"></pre>
</div> </div>
<div class="tips"> <div class="tips">
@@ -155,6 +168,10 @@
</div> </div>
</div> </div>
</div> </div>
<div id="toast" style="display:none;">
链接已复制到剪贴板
</div>
<script> <script>
function formatGithubLink() { function formatGithubLink() {
@@ -171,7 +188,8 @@
} else if (githubLinkInput.value.startsWith("raw.githubusercontent.com/")) { } else if (githubLinkInput.value.startsWith("raw.githubusercontent.com/")) {
formattedLink = "https://" + currentHost + "/" + githubLinkInput.value; formattedLink = "https://" + currentHost + "/" + githubLinkInput.value;
} else if (!githubLinkInput.value.trim()) { } else if (!githubLinkInput.value.trim()) {
alert('请输入有效的GitHub链接'); //alert('请输入有效的GitHub链接');
showToast('请输入有效的GitHub链接');
} }
var formattedLinkOutput = document.getElementById('formattedLinkOutput'); var formattedLinkOutput = document.getElementById('formattedLinkOutput');
formattedLinkOutput.textContent = formattedLink; formattedLinkOutput.textContent = formattedLink;
@@ -186,9 +204,20 @@
window.getSelection().addRange(range); window.getSelection().addRange(range);
document.execCommand('copy'); document.execCommand('copy');
window.getSelection().removeAllRanges(); window.getSelection().removeAllRanges();
alert('链接已复制到剪贴板'); //alert('链接已复制到剪贴板');
showToast('链接已复制到剪贴板');
}); });
function showToast(message) {
const toast = document.getElementById('toast');
toast.textContent = message;
toast.style.display = 'block';
setTimeout(() => {
toast.style.display = 'none';
}, 3000); // 3秒后隐藏
}
function fetchSizeLimit() { function fetchSizeLimit() {
fetch(window.location.origin + '/api/size_limit') fetch(window.location.origin + '/api/size_limit')
.then(response => response.json()) .then(response => response.json())
@@ -246,10 +275,8 @@
<footer> <footer>
<p> <p>
Copyright &copy; 2024 WJQSERVER-STUDIO Copyright &copy; 2024 WJQSERVER-STUDIO<br>
</p> GitHub仓库地址<a href="https://github.com/WJQSERVER-STUDIO/ghproxy">https://github.com/WJQSERVER-STUDIO/ghproxy</a> <br><a href="https://t.me/ghproxy_go">Telegram交流群</a>
<p>
GitHub仓库地址<a href="https://github.com/WJQSERVER-STUDIO/ghproxy">https://github.com/WJQSERVER-STUDIO/ghproxy</a>
</p> </p>
<div id="visitor-info" style="text-align: center; margin-top: 15px;"> <div id="visitor-info" style="text-align: center; margin-top: 15px;">
<p>您的IP地址: <span id="visitor-ip"></span></p> <p>您的IP地址: <span id="visitor-ip"></span></p>

View File

@@ -21,7 +21,7 @@ import (
var ( var (
logw = logger.Logw logw = logger.Logw
logInfo = logger.LogInfo logInfo = logger.LogInfo
LogWarning = logger.LogWarning logWarning = logger.LogWarning
logError = logger.LogError logError = logger.LogError
) )
@@ -40,7 +40,7 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc {
matches := re.FindStringSubmatch(rawPath) matches := re.FindStringSubmatch(rawPath)
if len(matches) < 3 { if len(matches) < 3 {
LogWarning("Invalid URL: %s", rawPath) logWarning("Invalid URL: %s", rawPath)
c.String(http.StatusForbidden, "Invalid URL.") c.String(http.StatusForbidden, "Invalid URL.")
return return
} }
@@ -51,14 +51,14 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc {
pathmatches := regexp.MustCompile(`^([^/]+)/([^/]+)/([^/]+)/.*`) pathmatches := regexp.MustCompile(`^([^/]+)/([^/]+)/([^/]+)/.*`)
pathParts := pathmatches.FindStringSubmatch(matches[2]) pathParts := pathmatches.FindStringSubmatch(matches[2])
if len(pathParts) < 4 { if len(pathParts) < 4 {
LogWarning("Invalid path: %s", rawPath) logWarning("Invalid path: %s", rawPath)
c.String(http.StatusForbidden, "Invalid path; expected username/repo.") c.String(http.StatusForbidden, "Invalid path; expected username/repo.")
return return
} }
username := pathParts[2] username := pathParts[2]
repo := pathParts[3] repo := pathParts[3]
LogWarning("Blacklist Check > Username: %s, Repo: %s", username, repo) logWarning("Blacklist Check > Username: %s, Repo: %s", username, repo)
fullrepo := fmt.Sprintf("%s/%s", username, repo) fullrepo := fmt.Sprintf("%s/%s", username, repo)
// 白名单检查 // 白名单检查
@@ -67,7 +67,7 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc {
if !whitelistpass { if !whitelistpass {
errMsg := fmt.Sprintf("Whitelist Blocked repo: %s", fullrepo) errMsg := fmt.Sprintf("Whitelist Blocked repo: %s", fullrepo)
c.JSON(http.StatusForbidden, gin.H{"error": errMsg}) c.JSON(http.StatusForbidden, gin.H{"error": errMsg})
LogWarning(errMsg) logWarning(errMsg)
return return
} }
} }
@@ -78,7 +78,7 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc {
if blacklistpass { if blacklistpass {
errMsg := fmt.Sprintf("Blacklist Blocked repo: %s", fullrepo) errMsg := fmt.Sprintf("Blacklist Blocked repo: %s", fullrepo)
c.JSON(http.StatusForbidden, gin.H{"error": errMsg}) c.JSON(http.StatusForbidden, gin.H{"error": errMsg})
LogWarning(errMsg) logWarning(errMsg)
return return
} }
} }
@@ -95,7 +95,7 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc {
if !auth.AuthHandler(c, cfg) { if !auth.AuthHandler(c, cfg) {
c.AbortWithStatusJSON(401, gin.H{"error": "Unauthorized"}) c.AbortWithStatusJSON(401, gin.H{"error": "Unauthorized"})
LogWarning("Unauthorized request: %s", rawPath) logWarning("Unauthorized request: %s", rawPath)
return return
} }
@@ -117,8 +117,8 @@ func NoRouteHandler(cfg *config.Config) gin.HandlerFunc {
func ProxyRequest(c *gin.Context, u string, cfg *config.Config, mode string) { func ProxyRequest(c *gin.Context, u string, cfg *config.Config, mode string) {
method := c.Request.Method method := c.Request.Method
// 记录 IP Method URL UA // 记录日志 IP 地址、请求方法、请求 URL、请求头 User-Agent 、HTTP版本
logInfo("%s %s %s %s", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent")) logInfo("%s %s %s %s %s", c.ClientIP(), method, u, c.Request.Header.Get("User-Agent"), c.Request.Proto)
client := createHTTPClient(mode) client := createHTTPClient(mode)
@@ -139,7 +139,7 @@ func ProxyRequest(c *gin.Context, u string, cfg *config.Config, mode string) {
defer resp.Body.Close() defer resp.Body.Close()
if err := HandleResponseSize(resp, cfg, c); err != nil { if err := HandleResponseSize(resp, cfg, c); err != nil {
LogWarning("Error handling response size: %v", err) logWarning("Error handling response size: %v", err)
return return
} }
@@ -150,12 +150,11 @@ func ProxyRequest(c *gin.Context, u string, cfg *config.Config, mode string) {
} }
} }
// createHTTPClient 创建并配置 HTTP 客户端
func createHTTPClient(mode string) *req.Client { func createHTTPClient(mode string) *req.Client {
client := req.C() client := req.C()
switch mode { switch mode {
case "chrome": case "chrome":
client.SetUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"). client.SetUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36").
SetTLSFingerprintChrome(). SetTLSFingerprintChrome().
ImpersonateChrome() ImpersonateChrome()
case "git": case "git":
@@ -212,7 +211,7 @@ func HandleResponseSize(resp *req.Response, cfg *config.Config, c *gin.Context)
if err == nil && size > cfg.Server.SizeLimit { if err == nil && size > cfg.Server.SizeLimit {
finalURL := resp.Request.URL.String() finalURL := resp.Request.URL.String()
c.Redirect(http.StatusMovedPermanently, finalURL) c.Redirect(http.StatusMovedPermanently, finalURL)
LogWarning("Size limit exceeded: %s, Size: %d", finalURL, size) logWarning("Size limit exceeded: %s, Size: %d", finalURL, size)
return fmt.Errorf("size limit exceeded: %d", size) return fmt.Errorf("size limit exceeded: %d", size)
} }
} }
@@ -220,10 +219,11 @@ func HandleResponseSize(resp *req.Response, cfg *config.Config, c *gin.Context)
} }
func CopyResponseHeaders(resp *req.Response, c *gin.Context, cfg *config.Config) { func CopyResponseHeaders(resp *req.Response, c *gin.Context, cfg *config.Config) {
removeHeaders(resp)
copyHeaders(resp, c) copyHeaders(resp, c)
removeHeaders(resp)
setCORSHeaders(c, cfg) setCORSHeaders(c, cfg)
setDefaultHeaders(c) setDefaultHeaders(c)
@@ -268,7 +268,7 @@ func setDefaultHeaders(c *gin.Context) {
func HandleError(c *gin.Context, message string) { func HandleError(c *gin.Context, message string) {
c.String(http.StatusInternalServerError, fmt.Sprintf("server error %v", message)) c.String(http.StatusInternalServerError, fmt.Sprintf("server error %v", message))
LogWarning(message) logWarning(message)
} }
func CheckURL(u string) []string { func CheckURL(u string) []string {
@@ -279,6 +279,6 @@ func CheckURL(u string) []string {
} }
} }
errMsg := fmt.Sprintf("Invalid URL: %s", u) errMsg := fmt.Sprintf("Invalid URL: %s", u)
LogWarning(errMsg) logWarning(errMsg)
return nil return nil
} }