Compare commits

..

6 Commits

Author SHA1 Message Date
wjqserver
ca9a638369 make it work 2025-09-14 08:44:19 +08:00
google-labs-jules[bot]
86a4ad881a feat: 添加后台统计页面
为项目增加了一个后台页面, 用于显示IP代理的使用情况统计.

主要包括:
- 新增 `backend` 目录, 包含 `index.html` 和 `script.js` 文件, 用于展示统计数据.
- 在 `main.go` 中增加了 `setBackendRoute` 函数, 用于提供后台页面的路由.
- 将后台页面路由设置为 `/admin`.

注意: 当前代码存在编译错误, 因为无法确定 `ipfilter.NewIPFilter` 的正确返回类型. 错误信息为 `undefined: ipfilter.IPFilter`. 提交此代码是为了让用户能够检查问题.
2025-09-13 23:56:26 +00:00
wjqserver
e3f84f4c17 fix retrun, change to false 2025-09-10 03:36:15 +08:00
wjqserver
4a7ad2ec75 4.3.3 2025-09-10 03:21:14 +08:00
wjqserver
a285777217 4.3.2 2025-08-20 15:53:09 +08:00
wjqserver
44cc5d5677 fix if cfg.Pages.StaticDir is "" issue 2025-08-20 15:48:00 +08:00
13 changed files with 259 additions and 34 deletions

View File

@@ -1,5 +1,14 @@
# 更新日志
4.3.3 - 2025-09-10
---
- CHANGE: 增强对[wanf](https://github.com/WJQSERVER/wanf)的支持
- CHANGE: 更新包括Touka框架在内的各个依赖版本
4.3.2 - 2025-08-20
---
- FIX: 修正`cfg.Pages.StaticDir`为空时的处置
4.3.1 - 2025-08-13
---
- CHANGE: 更新至[Go 1.25](https://tip.golang.org/doc/go1.25)

View File

@@ -1 +1 @@
4.3.1
4.3.3

View File

@@ -3,6 +3,7 @@ package api
import (
"ghproxy/config"
"ghproxy/middleware/nocache"
"ghproxy/stats"
"github.com/infinite-iroha/touka"
)
@@ -46,9 +47,17 @@ func InitHandleRouter(cfg *config.Config, r *touka.Engine, version string) {
apiRouter.GET("/oci_proxy/status", func(c *touka.Context) {
ociProxyStatusHandler(cfg, c)
})
apiRouter.GET("/stats", func(c *touka.Context) {
StatsHandler(c)
})
}
}
func StatsHandler(c *touka.Context) {
c.SetHeader("Content-Type", "application/json")
c.JSON(200, stats.GetStats())
}
func SizeLimitHandler(cfg *config.Config, c *touka.Context) {
sizeLimit := cfg.Server.SizeLimit
c.SetHeader("Content-Type", "application/json")

28
backend/index.html Normal file
View File

@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>后台统计</title>
<link rel="stylesheet" href="/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
<h1>IP 代理使用情况统计</h1>
<table class="table table-striped table-bordered mt-4">
<thead>
<tr>
<th>IP 地址</th>
<th>调用次数</th>
<th>总流量 (bytes)</th>
<th>最后调用时间</th>
</tr>
</thead>
<tbody id="stats-table-body">
<!-- 数据将由 script.js 动态填充 -->
</tbody>
</table>
</div>
<script src="script.js"></script>
</body>
</html>

36
backend/script.js Normal file
View File

@@ -0,0 +1,36 @@
document.addEventListener('DOMContentLoaded', function() {
fetch('/api/stats')
.then(response => response.json())
.then(data => {
const tableBody = document.getElementById('stats-table-body');
tableBody.innerHTML = ''; // 清空现有内容
for (const ip in data) {
const stats = data[ip];
const row = document.createElement('tr');
const ipCell = document.createElement('td');
ipCell.textContent = stats.ip;
row.appendChild(ipCell);
const callCountCell = document.createElement('td');
callCountCell.textContent = stats.call_count;
row.appendChild(callCountCell);
const transferredCell = document.createElement('td');
transferredCell.textContent = stats.total_transferred;
row.appendChild(transferredCell);
const lastCalledCell = document.createElement('td');
lastCalledCell.textContent = new Date(stats.last_called).toLocaleString();
row.appendChild(lastCalledCell);
tableBody.appendChild(row);
}
})
.catch(error => {
console.error('获取统计数据时出错:', error);
const tableBody = document.getElementById('stats-table-body');
tableBody.innerHTML = '<tr><td colspan="4" class="text-center">加载统计数据失败</td></tr>';
});
});

View File

@@ -1,8 +1,10 @@
package config
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/BurntSushi/toml"
@@ -212,7 +214,8 @@ type DockerConfig struct {
// LoadConfig 从配置文件加载配置
func LoadConfig(filePath string) (*Config, error) {
if !FileExists(filePath) {
exist, filePath2read := FileExists(filePath)
if !exist {
// 楔入配置文件
err := DefaultConfig().WriteConfig(filePath)
if err != nil {
@@ -221,15 +224,15 @@ func LoadConfig(filePath string) (*Config, error) {
return DefaultConfig(), nil
}
var config Config
ext := filepath.Ext(filePath)
ext := filepath.Ext(filePath2read)
if ext == ".wanf" {
if err := wanf.DecodeFile(filePath, &config); err != nil {
if err := wanf.DecodeFile(filePath2read, &config); err != nil {
return nil, err
}
return &config, nil
}
if _, err := toml.DecodeFile(filePath, &config); err != nil {
if _, err := toml.DecodeFile(filePath2read, &config); err != nil {
return nil, err
}
return &config, nil
@@ -257,9 +260,37 @@ func (c *Config) WriteConfig(filePath string) error {
}
// FileExists 检测文件是否存在
func FileExists(filename string) bool {
func FileExists(filename string) (bool, string) {
_, err := os.Stat(filename)
return !os.IsNotExist(err)
if err == nil {
return true, filename
}
if os.IsNotExist(err) {
// 获取文件名(不包含路径)
base := filepath.Base(filename)
dir := filepath.Dir(filename)
// 获取扩展名
fileNameBody := strings.TrimSuffix(base, filepath.Ext(base))
// 重新组合路径, 扩展名改为.wanf, 确认是否存在
wanfFilename := filepath.Join(dir, fileNameBody+".wanf")
_, err = os.Stat(wanfFilename)
if err == nil {
// .wanf 文件存在
fmt.Printf("\n Found .wanf file: %s\n", wanfFilename)
return true, wanfFilename
} else if os.IsNotExist(err) {
// .wanf 文件不存在
return false, ""
} else {
// 其他错误
return false, ""
}
} else {
return false, filename
}
}
// DefaultConfig 返回默认配置结构体

View File

@@ -25,10 +25,10 @@ rewriteAPI = false
[pages]
mode = "internal" # "internal" or "external"
theme = "bootstrap" # "bootstrap" or "nebula"
staticDir = "/data/www"
staticDir = "pages"
[log]
logFilePath = "/data/ghproxy/log/ghproxy.log"
logFilePath = "ghproxy.log"
maxLogSize = 5 # MB
level = "info" # debug, info, warn, error, none
@@ -42,18 +42,18 @@ ForceAllowApi = false
ForceAllowApiPassList = false
[blacklist]
blacklistFile = "/data/ghproxy/config/blacklist.json"
blacklistFile = "blacklist.json"
enabled = false
[whitelist]
enabled = false
whitelistFile = "/data/ghproxy/config/whitelist.json"
whitelistFile = "whitelist.json"
[ipFilter]
enabled = false
enableAllowList = false
enableBlockList = false
ipFilterFile = "/data/ghproxy/config/ipfilter.json"
ipFilterFile = "ipfilter.json"
[rateLimit]
enabled = false

10
go.mod
View File

@@ -1,12 +1,12 @@
module ghproxy
go 1.25
go 1.25.1
require (
github.com/BurntSushi/toml v1.5.0
github.com/WJQSERVER-STUDIO/httpc v0.8.2
golang.org/x/net v0.43.0
golang.org/x/time v0.12.0
golang.org/x/net v0.44.0
golang.org/x/time v0.13.0
)
require (
@@ -18,9 +18,9 @@ require (
github.com/fenthope/ipfilter v0.0.1
github.com/fenthope/reco v0.0.4
github.com/fenthope/record v0.0.4
github.com/go-json-experiment/json v0.0.0-20250813024750-ebf49471dced
github.com/go-json-experiment/json v0.0.0-20250813233538-9b1f9ea2e11b
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/infinite-iroha/touka v0.3.6
github.com/infinite-iroha/touka v0.3.8
github.com/wjqserver/modembed v0.0.1
)

16
go.sum
View File

@@ -18,17 +18,17 @@ github.com/fenthope/reco v0.0.4 h1:yo2g3aWwdoMpaZWZX4SdZOW7mCK82RQIU/YI8ZUQThM=
github.com/fenthope/reco v0.0.4/go.mod h1:eMyS8HpdMVdJ/2WJt6Cvt8P1EH9Igzj5lSJrgc+0jeg=
github.com/fenthope/record v0.0.4 h1:/1JHNCxiXGLL/qCh4LEGaAvhj4CcKsb6siTxjLmjdO4=
github.com/fenthope/record v0.0.4/go.mod h1:G0a6KCiCDyX2SsC3nfzSN651fJKxH482AyJvzlnvAJU=
github.com/go-json-experiment/json v0.0.0-20250813024750-ebf49471dced h1:Q311OHjMh/u5E2TITc++WlTP5We0xNseRMkHDyvhW7I=
github.com/go-json-experiment/json v0.0.0-20250813024750-ebf49471dced/go.mod h1:TiCD2a1pcmjd7YnhGH0f/zKNcCD06B029pHhzV23c2M=
github.com/go-json-experiment/json v0.0.0-20250813233538-9b1f9ea2e11b h1:6Q4zRHXS/YLOl9Ng1b1OOOBWMidAQZR3Gel0UKPC/KU=
github.com/go-json-experiment/json v0.0.0-20250813233538-9b1f9ea2e11b/go.mod h1:TiCD2a1pcmjd7YnhGH0f/zKNcCD06B029pHhzV23c2M=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/infinite-iroha/touka v0.3.6 h1:SkpM/VFGCWOFQP3RRuoWdX/Q4zafPngG1VMwkrLwtkw=
github.com/infinite-iroha/touka v0.3.6/go.mod h1:XW7a3fpLAjJfylSmdNuDQ8wGKkKmLVi9V/89sT1d7uw=
github.com/infinite-iroha/touka v0.3.8 h1:BK7+hwk5s5SpRFjFKIPe5CzZNzjP36kLHkM/HX6SU38=
github.com/infinite-iroha/touka v0.3.8/go.mod h1:uwkF1gTrNEgQ4P/Gwtk6WLbERehq3lzB8x1FMedyrfE=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/wjqserver/modembed v0.0.1 h1:8ZDz7t9M5DLrUFlYgBUUmrMzxWsZPmHvOazkr/T2jEs=
github.com/wjqserver/modembed v0.0.1/go.mod h1:sYbQJMAjSBsdYQrUsuHY380XXE1CuRh8g9yyCztTXOQ=
golang.org/x/net v0.43.0 h1:lat02VYK2j4aLzMzecihNvTlJNQUq316m2Mr9rnM6YE=
golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg=
golang.org/x/time v0.12.0 h1:ScB/8o8olJvc+CQPWrK3fPZNfh7qgwCrY0zJmoEQLSE=
golang.org/x/time v0.12.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
golang.org/x/net v0.44.0 h1:evd8IRDyfNBMBTTY5XRF1vaZlD+EmWx6x8PkhR04H/I=
golang.org/x/net v0.44.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY=
golang.org/x/time v0.13.0 h1:eUlYslOIt32DgYD6utsuUeHs4d7AsEYLuIAdg7FlYgI=
golang.org/x/time v0.13.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4=

36
main.go
View File

@@ -47,6 +47,8 @@ var (
var (
//go:embed pages/*
pagesFS embed.FS
//go:embed backend/*
backendFS embed.FS
)
var (
@@ -234,8 +236,18 @@ func setupPages(cfg *config.Config, r *touka.Engine) {
}
case "external":
r.SetUnMatchFS(http.Dir(cfg.Pages.StaticDir))
if cfg.Pages.StaticDir == "" {
logger.Errorf("Pages Mode is 'external' but StaticDir is empty. Using embedded pages instead.")
err := setInternalRoute(cfg, r)
if err != nil {
logger.Errorf("Failed to load embedded pages: %s", err)
fmt.Printf("Failed to load embedded pages: %s", err)
os.Exit(1)
}
} else {
extPageFS := os.DirFS(cfg.Pages.StaticDir)
r.SetUnMatchFS(http.FS(extPageFS))
}
default:
// 处理无效的Pages Mode
logger.Warnf("Invalid Pages Mode: %s, using default embedded theme", cfg.Pages.Mode)
@@ -332,6 +344,7 @@ func main() {
}
r := touka.Default()
var err error
r.SetProtocols(&touka.ProtocolsConfig{
Http1: true,
Http2_Cleartext: true,
@@ -370,8 +383,8 @@ func main() {
}
if cfg.IPFilter.Enabled {
var err error
ipAllowList, ipBlockList, err := auth.ReadIPFilterList(cfg)
var ipAllowList, ipBlockList []string
ipAllowList, ipBlockList, err = auth.ReadIPFilterList(cfg)
if err != nil {
fmt.Printf("Failed to read IP filter list: %v\n", err)
logger.Errorf("Failed to read IP filter list: %v", err)
@@ -393,6 +406,7 @@ func main() {
}
setupApi(cfg, r, version)
setupPages(cfg, r)
setBackendRoute(r)
r.SetRedirectTrailingSlash(false)
r.GET("/github.com/:user/:repo/releases/*filepath", func(c *touka.Context) {
@@ -507,7 +521,7 @@ func main() {
defer logger.Close()
addr := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port)
err := r.RunShutdown(addr)
err = r.RunShutdown(addr)
if err != nil {
logger.Errorf("Server Run Error: %v", err)
fmt.Printf("Server Run Error: %v\n", err)
@@ -515,3 +529,15 @@ func main() {
fmt.Println("Program Exit")
}
func setBackendRoute(r *touka.Engine) {
backend, err := fs.Sub(backendFS, "backend")
if err != nil {
logger.Errorf("Failed to load embedded backend pages: %s", err)
fmt.Printf("Failed to load embedded backend pages: %s", err)
os.Exit(1)
}
r.StaticFS("/backend", http.FS(backend))
}

View File

@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"ghproxy/config"
"ghproxy/stats"
"io"
"net/http"
"strconv"
@@ -124,7 +125,11 @@ func ChunkedProxyRequest(ctx context.Context, c *touka.Context, u string, cfg *c
bodyReader = limitreader.NewRateLimitedReader(bodyReader, bandwidthLimit, int(bandwidthBurst), ctx)
}
defer bodyReader.Close()
countingReader := NewCountingReader(bodyReader)
defer countingReader.Close()
defer func() {
stats.Record(c.ClientIP(), countingReader.BytesRead())
}()
if MatcherShell(u) && matchString(matcher) && cfg.Shell.Editor {
// 判断body是不是gzip
@@ -138,7 +143,7 @@ func ChunkedProxyRequest(ctx context.Context, c *touka.Context, u string, cfg *c
var reader io.Reader
reader, _, err = processLinks(bodyReader, compress, c.Request.Host, cfg, c)
reader, _, err = processLinks(countingReader, compress, c.Request.Host, cfg, c)
c.WriteStream(reader)
if err != nil {
c.Errorf("%s %s %s %s %s Failed to copy response body: %v", c.ClientIP(), c.Request.Method, u, c.UserAgent(), c.Request.Proto, err)
@@ -149,10 +154,10 @@ func ChunkedProxyRequest(ctx context.Context, c *touka.Context, u string, cfg *c
if contentLength != "" {
c.SetHeader("Content-Length", contentLength)
c.WriteStream(bodyReader)
c.WriteStream(countingReader)
return
}
c.WriteStream(bodyReader)
c.WriteStream(countingReader)
}
}

View File

@@ -4,10 +4,47 @@ import (
"fmt"
"ghproxy/auth"
"ghproxy/config"
"io"
"github.com/infinite-iroha/touka"
)
// CountingReader is a reader that counts the number of bytes read.
// CountingReader 是一个计算已读字节数的读取器.
type CountingReader struct {
reader io.Reader
bytesRead int64
}
// NewCountingReader creates a new CountingReader.
// NewCountingReader 创建一个新的 CountingReader.
func NewCountingReader(reader io.Reader) *CountingReader {
return &CountingReader{
reader: reader,
}
}
func (cr *CountingReader) Read(p []byte) (n int, err error) {
n, err = cr.reader.Read(p)
cr.bytesRead += int64(n)
return n, err
}
// BytesRead returns the number of bytes read.
// BytesRead 返回已读字节数.
func (cr *CountingReader) BytesRead() int64 {
return cr.bytesRead
}
// Close closes the underlying reader if it implements io.Closer.
// 如果底层读取器实现了 io.Closer, 则关闭它.
func (cr *CountingReader) Close() error {
if closer, ok := cr.reader.(io.Closer); ok {
return closer.Close()
}
return nil
}
func listCheck(cfg *config.Config, c *touka.Context, user string, repo string, rawPath string) bool {
if cfg.Auth.ForceAllowApi && cfg.Auth.ForceAllowApiPassList {
return false

44
stats/stats.go Normal file
View File

@@ -0,0 +1,44 @@
package stats
import (
"sync"
"time"
)
// ProxyStats store one ip's proxy stats
// ProxyStats 存储一个IP的代理统计信息
type ProxyStats struct {
IP string `json:"ip"`
LastCalled time.Time `json:"last_called"`
CallCount int64 `json:"call_count"`
TotalTransferred int64 `json:"total_transferred"`
}
var (
statsMap = &sync.Map{}
)
// Record update a ip's proxy stats
// Record 更新一个IP的代理统计信息
func Record(ip string, transferred int64) {
s, _ := statsMap.LoadOrStore(ip, &ProxyStats{
IP: ip,
})
ps := s.(*ProxyStats)
ps.LastCalled = time.Now()
ps.CallCount++
ps.TotalTransferred += transferred
statsMap.Store(ip, ps)
}
// GetStats return all proxy stats
// GetStats 返回所有的代理统计信息
func GetStats() map[string]*ProxyStats {
data := make(map[string]*ProxyStats)
statsMap.Range(func(key, value interface{}) bool {
data[key.(string)] = value.(*ProxyStats)
return true
})
return data
}