添加在线调试 api

This commit is contained in:
2025-03-08 10:59:31 +08:00
parent 053041ae34
commit 5786ac9d99
28 changed files with 236 additions and 539 deletions

View File

@@ -3,7 +3,7 @@ package core
import (
"log/slog"
"net"
"proxy-server/server/models"
models2 "proxy-server/server/pkg/models"
"proxy-server/server/pkg/orm"
"time"
@@ -41,12 +41,12 @@ func CheckIp(conn net.Conn) (*AuthContext, error) {
// 查询权限记录
slog.Debug("用户 " + remoteHost + " 请求连接到 " + localPort)
var channels []models.Channel
var channels []models2.Channel
err = orm.DB.
Joins("INNER JOIN public.nodes n ON channels.node_id = n.id AND n.name = ?", localPort).
Joins("INNER JOIN public.users u ON channels.user_id = u.id").
Joins("INNER JOIN public.user_ips ip ON u.id = ip.user_id AND ip.ip_address = ?", remoteHost).
Where(&models.Channel{
Where(&models2.Channel{
AuthIp: true,
}).
Find(&channels).Error
@@ -88,9 +88,9 @@ func CheckPass(conn net.Conn, username, password string) (*AuthContext, error) {
}, nil
// 查询通道配置
var channel models.Channel
var channel models2.Channel
err := orm.DB.
Where(&models.Channel{
Where(&models2.Channel{
Username: username,
AuthPass: true,
}).
@@ -125,7 +125,7 @@ func CheckPass(conn net.Conn, username, password string) (*AuthContext, error) {
var ips int64
err = orm.DB.
Where(&models.UserIp{
Where(&models2.UserIp{
UserId: channel.UserId,
IpAddress: remoteHost,
}).

View File

@@ -8,8 +8,9 @@ import (
"proxy-server/pkg/utils"
"proxy-server/server/fwd/core"
"proxy-server/server/fwd/dispatcher"
"proxy-server/server/models"
"proxy-server/server/fwd/metrics"
"proxy-server/server/pkg/env"
"proxy-server/server/pkg/models"
"proxy-server/server/pkg/orm"
"strconv"
"strings"
@@ -170,6 +171,7 @@ func (s *Service) processCtrlConn(conn net.Conn) error {
return errors.Wrap(err, "客户端意外断开连接")
}
case user := <-proxy.Conn:
metrics.TimerAuth.Store(user.Conn, time.Now())
s.userConnWg.Add(1)
go func() {
defer s.userConnWg.Done()

View File

@@ -5,9 +5,12 @@ import (
"log/slog"
"net"
"proxy-server/pkg/utils"
"proxy-server/server/debug"
"proxy-server/server/fwd/metrics"
"proxy-server/server/pkg/env"
"strconv"
"sync"
"time"
"github.com/pkg/errors"
)
@@ -77,6 +80,7 @@ func (s *Service) processDataConn(client net.Conn) error {
return errors.New("用户连接已关闭tag" + tag)
}
defer utils.Close(user)
data := time.Now()
// 检查状态
if status != 1 {
@@ -116,5 +120,34 @@ func (s *Service) processDataConn(client net.Conn) error {
case <-utils.ChanWgWait(s.ctx, &wg):
}
proxy := time.Now()
start, startOk := metrics.TimerStart.Load(user.Conn)
auth, authOk := metrics.TimerAuth.Load(user.Conn)
var authDuration time.Duration
if startOk && authOk {
authDuration = auth.(time.Time).Sub(start.(time.Time))
}
var dataDuration time.Duration
if authOk {
dataDuration = data.Sub(auth.(time.Time))
}
proxyDuration := proxy.Sub(data)
var totalDuration time.Duration
if startOk {
totalDuration = proxy.Sub(start.(time.Time))
}
debug.ConsumingCh <- debug.Consuming{
Auth: authDuration,
Data: dataDuration,
Proxy: proxyDuration,
Total: totalDuration,
}
return nil
}

View File

@@ -7,6 +7,7 @@ import (
"proxy-server/pkg/utils"
"proxy-server/server/fwd/core"
"proxy-server/server/fwd/http"
"proxy-server/server/fwd/metrics"
"proxy-server/server/fwd/socks"
"strconv"
"strings"
@@ -112,6 +113,8 @@ func (s *Server) acceptHttp(ls net.Listener) error {
return errors.Wrap(err, "dispatcher http accept error")
}
metrics.TimerStart.Store(conn, time.Now())
go func() {
user, err := http.Process(s.ctx, conn)
if err != nil {
@@ -142,6 +145,8 @@ func (s *Server) acceptSocks(ls net.Listener) error {
return errors.Wrap(err, "dispatcher socks accept error")
}
metrics.TimerStart.Store(conn, time.Now())
go func() {
user, err := socks.Process(s.ctx, conn)
if err != nil {

View File

@@ -34,13 +34,6 @@ func New(config *Config) *Service {
Config: config,
ctx: ctx,
cancel: cancel,
userConnMap: core.ConnMap{},
fwdLesWg: utils.CountWaitGroup{},
ctrlConnWg: utils.CountWaitGroup{},
dataConnWg: utils.CountWaitGroup{},
userConnWg: utils.CountWaitGroup{},
}
}

View File

@@ -0,0 +1,6 @@
package metrics
import "sync"
var TimerStart sync.Map
var TimerAuth sync.Map