重构连接监听与处理代码逻辑,连接信息存储于全局通过接口展示
This commit is contained in:
@@ -27,7 +27,7 @@ const (
|
||||
CtrlCmdProxy
|
||||
)
|
||||
|
||||
func (s *Service) listenCtrl() error {
|
||||
func ListenCtrl(ctx context.Context) error {
|
||||
ctrlPort := env.AppCtrlPort
|
||||
|
||||
// 监听端口
|
||||
@@ -53,7 +53,7 @@ func (s *Service) listenCtrl() error {
|
||||
}
|
||||
select {
|
||||
case connCh <- conn:
|
||||
case <-s.ctx.Done():
|
||||
case <-ctx.Done():
|
||||
utils.Close(conn)
|
||||
return
|
||||
}
|
||||
@@ -63,14 +63,14 @@ func (s *Service) listenCtrl() error {
|
||||
err = nil
|
||||
for {
|
||||
select {
|
||||
case <-s.ctx.Done():
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
case conn := <-connCh:
|
||||
s.ctrlConnWg.Add(1)
|
||||
app.CtrlConnWg.Add(1)
|
||||
go func() {
|
||||
defer s.ctrlConnWg.Done()
|
||||
defer app.CtrlConnWg.Done()
|
||||
defer utils.Close(conn)
|
||||
err := s.processCtrlConn(s.ctx, conn)
|
||||
err := processCtrlConn(ctx, conn)
|
||||
if err != nil {
|
||||
slog.Error("处理控制通道连接失败", "err", err)
|
||||
}
|
||||
@@ -79,7 +79,7 @@ func (s *Service) listenCtrl() error {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) processCtrlConn(_ctx context.Context, conn net.Conn) (err error) {
|
||||
func processCtrlConn(_ctx context.Context, conn net.Conn) (err error) {
|
||||
// 通道上下文
|
||||
ctx, cancel := context.WithCancel(_ctx)
|
||||
|
||||
@@ -126,21 +126,21 @@ func (s *Service) processCtrlConn(_ctx context.Context, conn net.Conn) (err erro
|
||||
return fmt.Errorf("读取节点 ID 失败: %w", err)
|
||||
}
|
||||
var client = int32(binary.BigEndian.Uint32(recv))
|
||||
fwdPort, err = s.onOpen(ctx, conn, client)
|
||||
fwdPort, err = onOpen(ctx, conn, client)
|
||||
if err != nil {
|
||||
return fmt.Errorf("处理连接建立命令失败: %w", err)
|
||||
}
|
||||
|
||||
// 心跳命令
|
||||
case CtrlCmdPing:
|
||||
err = s.onPing(conn)
|
||||
err = onPing(conn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("处理心跳命令失败: %w", err)
|
||||
}
|
||||
|
||||
// 连接关闭命令
|
||||
case CtrlCmdClose:
|
||||
err = s.onClose(conn)
|
||||
err = onClose(conn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("处理关闭命令失败: %w", err)
|
||||
}
|
||||
@@ -153,7 +153,7 @@ func (s *Service) processCtrlConn(_ctx context.Context, conn net.Conn) (err erro
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) onOpen(ctx context.Context, writer io.Writer, edge int32) (port uint16, err error) {
|
||||
func onOpen(ctx context.Context, writer io.Writer, edge int32) (port uint16, err error) {
|
||||
// open 命令全局只执行一次
|
||||
_, ok := app.Edges.Load(edge)
|
||||
if ok {
|
||||
@@ -181,16 +181,16 @@ func (s *Service) onOpen(ctx context.Context, writer io.Writer, edge int32) (por
|
||||
}
|
||||
|
||||
// 响应节点
|
||||
if err = s.sendPong(writer); err != nil {
|
||||
if err = sendPong(writer); err != nil {
|
||||
return 0, fmt.Errorf("响应节点失败: %w", err)
|
||||
}
|
||||
|
||||
// 启动转发服务
|
||||
s.fwdLesWg.Add(1)
|
||||
app.FwdLesWg.Add(1)
|
||||
go func() {
|
||||
defer s.fwdLesWg.Done()
|
||||
defer app.FwdLesWg.Done()
|
||||
slog.Info("监听转发端口", "port", port, "edge", edge)
|
||||
err = s.listenUser(ctx, port, writer)
|
||||
err = ListenUser(ctx, port, writer)
|
||||
if err != nil {
|
||||
slog.Error("监听转发端口失败", "port", port, "edge", edge, "err", err)
|
||||
}
|
||||
@@ -199,15 +199,15 @@ func (s *Service) onOpen(ctx context.Context, writer io.Writer, edge int32) (por
|
||||
return port, nil
|
||||
}
|
||||
|
||||
func (s *Service) onPing(writer io.Writer) (err error) {
|
||||
return s.sendPong(writer)
|
||||
func onPing(writer io.Writer) (err error) {
|
||||
return sendPong(writer)
|
||||
}
|
||||
|
||||
func (s *Service) onClose(writer io.Writer) (err error) {
|
||||
return s.sendPong(writer)
|
||||
func onClose(writer io.Writer) (err error) {
|
||||
return sendPong(writer)
|
||||
}
|
||||
|
||||
func (s *Service) sendPong(writer io.Writer) (err error) {
|
||||
func sendPong(writer io.Writer) (err error) {
|
||||
_, err = writer.Write([]byte{byte(CtrlCmdPong)})
|
||||
if err != nil {
|
||||
return fmt.Errorf("响应节点失败: %w", err)
|
||||
@@ -215,7 +215,7 @@ func (s *Service) sendPong(writer io.Writer) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) sendProxy(writer io.Writer, tag [16]byte, addr string) (err error) {
|
||||
func sendProxy(writer io.Writer, tag [16]byte, addr string) (err error) {
|
||||
if len(addr) > 65535 {
|
||||
return fmt.Errorf("代理地址过长: %s", addr)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user