feat: 添加后台统计页面

为项目增加了一个后台页面, 用于显示IP代理的使用情况统计.

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

注意: 当前代码存在编译错误, 因为无法确定 `ipfilter.NewIPFilter` 的正确返回类型. 错误信息为 `undefined: ipfilter.IPFilter`. 提交此代码是为了让用户能够检查问题.
This commit is contained in:
google-labs-jules[bot] 2025-09-13 23:56:26 +00:00
parent e3f84f4c17
commit 86a4ad881a
8 changed files with 188 additions and 13 deletions

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