重构认证相关结构,更新认证流程,添加日志功能

This commit is contained in:
2025-02-26 17:01:20 +08:00
parent 7ee4ded08c
commit a541a7bd3a
12 changed files with 110 additions and 472 deletions

View File

@@ -49,6 +49,27 @@ type AddrSpec struct {
Port int
}
func (a AddrSpec) Domain() []string {
if a.FQDN != "" {
return []string{a.FQDN}
}
var domain []string
ch := make(chan struct{})
defer close(ch)
go func() {
addr, err := net.LookupAddr(a.IP.String())
if err == nil {
domain = addr
}
ch <- struct{}{}
}()
<-ch
return domain
}
func (a AddrSpec) String() string {
if a.FQDN != "" {
return fmt.Sprintf("%s (%s):%d", a.FQDN, a.IP, a.Port)
@@ -186,8 +207,8 @@ type Request struct {
Version uint8
// Requested command
Command uint8
// AuthContext provided during negotiation
AuthContext *AuthContext
// Authentication provided during negotiation
Authentication *Authentication
// AddrSpec of the network that sent the request
RemoteAddr *AddrSpec
// AddrSpec of the desired destination
@@ -220,7 +241,6 @@ func (s *Server) handle(req *Request, conn net.Conn) error {
}
func (s *Server) handleConnect(ctx context.Context, conn net.Conn, req *Request) error {
// 检查规则集约束
s.config.Logger.Printf("检查约束规则\n")
if ctx_, ok := s.config.Rules.Allow(ctx, req); !ok {
@@ -233,75 +253,8 @@ func (s *Server) handleConnect(ctx context.Context, conn net.Conn, req *Request)
}
slog.Info("需要向 " + req.DestAddr.Address() + " 建立连接")
s.Conn <- ProxyData{conn, req.realDestAddr.Address()}
s.Conn <- ProxyConn{conn, req.realDestAddr.Address()}
return nil
// 与目标服务器建立连接
// s.config.Logger.Printf("与目标服务器建立连接\n")
// dial := s.config.Dial
// target, err := dial("tcp", req.realDestAddr.Address())
// if err != nil {
// msg := err.Error()
// resp := hostUnreachable
// if strings.Contains(msg, "refused") {
// resp = connectionRefused
// } else if strings.Contains(msg, "network is unreachable") {
// resp = networkUnreachable
// }
//
// err := sendReply(Conn, resp, nil)
// if err != nil {
// return fmt.Errorf("failed to send reply: %v", err)
// }
// return fmt.Errorf("request to %v failed: %v", req.DestAddr, err)
// }
// defer closeConnection(target)
//
// // 正常响应
// slog.Info("连接成功,开始代理流量")
//
// local := target.LocalAddr().(*net.TCPAddr)
// bind := AddrSpec{IP: local.IP, Port: local.Port}
// err = sendReply(Conn, successReply, &bind)
// if err != nil {
// return fmt.Errorf("Failed to send reply: %v", err)
// }
//
// // 配置超时时间和行为
// timeout := req.AuthContext.Timeout
// slog.Debug("超时时间", "timeout", timeout)
//
// timeoutCtx, cancel := ctx.WithTimeout(ctx, time.Duration(timeout)*time.Second)
// defer cancel()
//
// // 代理流量
// errChan := make(chan error, 2)
// go func() {
// _, err = io.Copy(target, req.bufConn)
// errChan <- err
// }()
// go func() {
// _, err = io.Copy(Conn, target)
// errChan <- err
// }()
//
// for {
// select {
//
// case <-timeoutCtx.Done():
// slog.Debug("超时断开连接")
// // todo 根据 termination 执行不同的断开行为
// return nil
//
// case err := <-errChan:
// slog.Debug("主动断开连接")
// if err != nil {
// return errors.Wrap(err, "代理流量出现错误")
// }
// return nil
// }
// }
}
func (s *Server) handleBind(ctx context.Context, conn net.Conn, req *Request) error {
@@ -391,15 +344,19 @@ func SendSuccess(user net.Conn, target net.Conn) {
}
}
type ProxyData struct {
type ProxyConn struct {
// 用户连入的连接
Conn net.Conn
// 用户目标地址
Dest string
}
func (d ProxyData) Tag() string {
func (d ProxyConn) Tag() string {
local := d.Conn.LocalAddr()
remote := d.Conn.RemoteAddr()
return fmt.Sprintf("%s-%s", remote, local)
}
func (d ProxyConn) Close() error {
return d.Conn.Close()
}