chore: 初始提交 Fonu 1.0.2
- 公网反代:Nginx 状态卡片(版本 / 运行状态 / 配置校验 / 端口监听) - 部署设置第一、二梯队(HTTPS 开关、安全头、限流、连接数、仅中国大陆 IP) - 隧道检测兼容 frps v0.68 / v0.71(V2 与旧版端点) - 恢复被清空的 internal/frp/store.go、internal/api/ddns_handler.go、web/src/views/DdnsView.vue
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package traffic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Store struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewStore(db *sql.DB) *Store {
|
||||
return &Store{db: db}
|
||||
}
|
||||
|
||||
func (s *Store) LoadTotals(ctx context.Context) (map[string]hostTotals, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `SELECT host, upload_bytes, download_bytes FROM proxy_traffic`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
out := map[string]hostTotals{}
|
||||
for rows.Next() {
|
||||
var host string
|
||||
var up, down int64
|
||||
if err := rows.Scan(&host, &up, &down); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
key := strings.ToLower(strings.TrimSpace(host))
|
||||
out[key] = hostTotals{Upload: up, Download: down}
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) SaveTotals(ctx context.Context, hosts map[string]hostTotals) error {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for host, totals := range hosts {
|
||||
_, err := tx.ExecContext(ctx, `
|
||||
INSERT INTO proxy_traffic(host, upload_bytes, download_bytes, updated_at)
|
||||
VALUES (?, ?, ?, ?)
|
||||
ON CONFLICT(host) DO UPDATE SET
|
||||
upload_bytes = excluded.upload_bytes,
|
||||
download_bytes = excluded.download_bytes,
|
||||
updated_at = excluded.updated_at
|
||||
`, host, totals.Upload, totals.Download, time.Now().UTC().Format(time.RFC3339))
|
||||
if err != nil {
|
||||
_ = tx.Rollback()
|
||||
return err
|
||||
}
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
type hostTotals struct {
|
||||
Upload int64
|
||||
Download int64
|
||||
}
|
||||
Reference in New Issue
Block a user