优化项目结构

This commit is contained in:
2025-02-27 18:07:00 +08:00
parent a541a7bd3a
commit 38d5341e84
9 changed files with 176 additions and 36 deletions

57
server/fwd/analysis.go Normal file
View File

@@ -0,0 +1,57 @@
package fwd
import (
"bufio"
"io"
"log/slog"
"proxy-server/pkg/utils"
)
func analysis(reader io.Reader) {
buf := bufio.NewReader(reader)
first, err := buf.Peek(8)
if err != nil {
slog.Error("analysis peek error", "err", err)
} else {
switch {
case first[0] == 0x16:
analysisHttps(reader)
case
string(first[:4]) == "GET ",
// string(first[:4]) == "PUT ",
string(first[:5]) == "POST ":
// string(first[:4]) == "HEAD ",
// string(first[:4]) == "TRACE ",
// string(first[:4]) == "PATCH ",
// string(first[:4]) == "DELETE ",
// string(first[:4]) == "CONNECT ",
// string(first[:4]) == "OPTIONS ":
analysisHttp(reader)
}
}
discord(reader)
}
func analysisHttp(reader io.Reader) {
}
func analysisHttps(reader io.Reader) {
head, err := utils.ReadBuffer(reader, 5)
if err != nil {
slog.Error("analysis https err", "err", err)
return
}
if head[1] == 0x03 && head[2] == 0x03 {
// tls1.2
}
}
func discord(reader io.Reader) {
_, err := io.Copy(io.Discard, reader)
if err != nil {
slog.Error("analysis discord err", "err", err)
}
}

View File

@@ -30,27 +30,19 @@ type Service struct {
}
func New(config *Config) *Service {
_config := config
if _config == nil {
_config = &Config{}
if config == nil {
config = &Config{}
}
return &Service{
Config: _config,
Config: config,
connMap: make(map[string]socks.ProxyConn),
ctrlConnWg: utils.CountWaitGroup{},
dataConnWg: utils.CountWaitGroup{},
}
}
func (s *Service) Run(ctx context.Context, errCh chan error) {
defer func() {
err := recover()
if err != nil {
slog.Error("服务由于意外的 panic 导致退出", err)
}
}()
func (s *Service) Run(ctx context.Context) {
slog.Info("启动 fwd 服务")
// 启动工作协程
@@ -61,7 +53,7 @@ func (s *Service) Run(ctx context.Context, errCh chan error) {
subErrCh := make(chan error, goNum)
defer close(subErrCh)
go s.startCtrlTun(subCtx, subErrCh)
go s.startCtrlTun(subCtx)
go s.startDataTun(subCtx, subErrCh)
// 等待结束
@@ -80,18 +72,20 @@ func (s *Service) Run(ctx context.Context, errCh chan error) {
}
slog.Info("fwd 服务已结束")
errCh <- firstSubErr
}
func (s *Service) startCtrlTun(ctx context.Context, errCh chan error) {
func (s *Service) Close() {
}
func (s *Service) startCtrlTun(ctx context.Context) error {
ctrlPort := env.AppCtrlPort
slog.Debug("监听控制通道", slog.Uint64("port", uint64(ctrlPort)))
// 监听端口
ls, err := net.Listen("tcp", ":"+strconv.Itoa(int(ctrlPort)))
if err != nil {
slog.Error("监听控制通道失败", "err", err)
return
return errors.Wrap(err, "监听控制通道失败")
}
defer utils.Close(ls)
@@ -120,9 +114,10 @@ func (s *Service) startCtrlTun(ctx context.Context, errCh chan error) {
}
// 等待子协程结束 todo 可配置等待时间
s.Close()
timeout, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
procCh := utils.ChanWgWait(timeout, &s.ctrlConnWg)
defer close(procCh)
@@ -134,7 +129,7 @@ func (s *Service) startCtrlTun(ctx context.Context, errCh chan error) {
}
slog.Debug("关闭控制通道")
errCh <- nil
return nil
}
func (s *Service) processCtrlConn(controller net.Conn) {
@@ -283,6 +278,11 @@ func (s *Service) processDataConn(client net.Conn) {
// 数据转发
slog.Info("开始数据转发 " + client.RemoteAddr().String() + " <-> " + data.Dest)
// userPipeReader, userPipeWriter := io.Pipe()
// defer utils.Close(userPipeWriter)
// teeUser := io.TeeReader(user, userPipeWriter)
errCh := make(chan error)
go func() {
_, err := io.Copy(client, user)
@@ -291,6 +291,8 @@ func (s *Service) processDataConn(client net.Conn) {
}
errCh <- err
}()
// go analysis(userPipeReader)
go func() {
_, err := io.Copy(user, client)
if err != nil {
@@ -298,6 +300,7 @@ func (s *Service) processDataConn(client net.Conn) {
}
errCh <- err
}()
<-errCh
slog.Info("数据转发结束 " + client.RemoteAddr().String() + " <-> " + data.Dest)
}

View File

@@ -1,5 +0,0 @@
package logs
func Write(str ...string) {
}

View File

@@ -61,6 +61,10 @@ type Server struct {
// New 创建服务器
func New(conf *Config) (*Server, error) {
if conf == nil {
conf = &Config{}
}
if len(conf.AuthMethods) == 0 {
return nil, ConfigError("认证方法不能为空")
}