代码清理

This commit is contained in:
2025-02-25 15:56:33 +08:00
parent 9a8680a221
commit 884faae5ba
9 changed files with 32 additions and 59 deletions

View File

@@ -5,6 +5,7 @@ WORKDIR /app
COPY ./bin/proxy_server_linux_amd64 /app/proxy
RUN chmod +x /app/proxy
EXPOSE $PORT
EXPOSE $APP_CTRL_PORT
EXPOSE $APP_PROXY_PORT
CMD ["/app/proxy"]

View File

@@ -12,7 +12,9 @@
实现一个 socks context 以在子组件中获取 socks 相关信息
fwd 使用自定义 context 实现在一个上下文中控制 cancelerrch 和其他自定义数据
fwd 使用自定义 context 实现在一个上下文中控制 cancelerrCh 和其他自定义数据
网关根据代理节点对目标服务连接的反馈,决定向用户返回的 socks 响应
### 长期

View File

@@ -1,29 +1,22 @@
package main
import (
"proxy-server/server/pkg/env"
"proxy-server/server/pkg/orm"
"gorm.io/gen"
)
const (
Host = "localhost"
Port = "5432"
Username = "gorm"
Password = "gorm"
Database = "gorm"
Timezone = "Asia/Shanghai"
)
func main() {
env.Init()
orm.Init()
g := gen.NewGenerator(gen.Config{
OutPath: "../../temp-out",
Mode: gen.WithoutContext | gen.WithDefaultQuery | gen.WithQueryInterface,
})
orm.Init()
g.UseDB(orm.DB)
g.ApplyBasic(
g.GenerateAllTable()...,
)

View File

@@ -7,12 +7,12 @@ import (
type CountWaitGroup struct {
wg sync.WaitGroup
num atomic.Uint64
num atomic.Int64
}
func (c *CountWaitGroup) Add(delta uint64) {
func (c *CountWaitGroup) Add(delta uint) {
c.wg.Add(int(delta))
c.num.Add(delta)
c.num.Add(int64(delta))
}
func (c *CountWaitGroup) Done() {
@@ -25,5 +25,5 @@ func (c *CountWaitGroup) Wait() {
}
func (c *CountWaitGroup) Count() uint64 {
return c.num.Load()
return uint64(c.num.Load())
}

View File

@@ -3,7 +3,7 @@ package orm
import (
"fmt"
"log/slog"
"os"
"proxy-server/server/pkg/env"
"github.com/pkg/errors"
"gorm.io/driver/postgres"
@@ -14,16 +14,9 @@ import (
var DB *gorm.DB
func Init() {
Host := os.Getenv("DB_HOST")
Port := os.Getenv("DB_PORT")
Database := os.Getenv("DB_DATABASE")
Username := os.Getenv("DB_USERNAME")
Password := os.Getenv("DB_PASSWORD")
Timezone := os.Getenv("DB_TIMEZONE")
dsn := fmt.Sprintf(
"host=%s port=%s user=%s password=%s dbname=%s sslmode=disable TimeZone=%s",
Host, Port, Username, Password, Database, Timezone,
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable TimeZone=%s",
env.DbHost, env.DbPort, env.DbUsername, env.DbPassword, env.DbDatabase, env.DbTimezone,
)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{

View File

@@ -34,7 +34,7 @@ const (
)
var (
unrecognizedAddrType = fmt.Errorf("Unrecognized address type")
unrecognizedAddrType = fmt.Errorf("unrecognized address type")
)
// AddressRewriter is used to rewrite a destination transparently
@@ -156,9 +156,9 @@ func (server *Server) parseTarget(reader io.Reader, writer io.Writer) (*AddrSpec
if err != nil {
err := sendReply(writer, hostUnreachable, nil)
if err != nil {
return nil, fmt.Errorf("Failed to send reply: %v", err)
return nil, fmt.Errorf("failed to send reply: %v", err)
}
return nil, fmt.Errorf("Failed to resolve destination '%v': %v", dest.FQDN, err)
return nil, fmt.Errorf("failed to resolve destination '%v': %v", dest.FQDN, err)
}
dest.IP = addr
@@ -308,16 +308,16 @@ func (server *Server) handleBind(ctx context.Context, conn net.Conn, req *Reques
// Check if this is allowed
if ctx_, ok := server.config.Rules.Allow(ctx, req); !ok {
if err := sendReply(conn, ruleFailure, nil); err != nil {
return fmt.Errorf("Failed to send reply: %v", err)
return fmt.Errorf("failed to send reply: %v", err)
}
return fmt.Errorf("Bind to %v blocked by rules", req.DestAddr)
return fmt.Errorf("bind to %v blocked by rules", req.DestAddr)
} else {
ctx = ctx_
}
// TODO: Support bind
if err := sendReply(conn, commandNotSupported, nil); err != nil {
return fmt.Errorf("Failed to send reply: %v", err)
return fmt.Errorf("failed to send reply: %v", err)
}
return nil
}
@@ -326,16 +326,16 @@ func (server *Server) handleAssociate(ctx context.Context, conn net.Conn, req *R
// Check if this is allowed
if ctx_, ok := server.config.Rules.Allow(ctx, req); !ok {
if err := sendReply(conn, ruleFailure, nil); err != nil {
return fmt.Errorf("Failed to send reply: %v", err)
return fmt.Errorf("failed to send reply: %v", err)
}
return fmt.Errorf("Associate to %v blocked by rules", req.DestAddr)
return fmt.Errorf("associate to %v blocked by rules", req.DestAddr)
} else {
ctx = ctx_
}
// TODO: Support associate
if err := sendReply(conn, commandNotSupported, nil); err != nil {
return fmt.Errorf("Failed to send reply: %v", err)
return fmt.Errorf("failed to send reply: %v", err)
}
return nil
}

View File

@@ -96,7 +96,7 @@ func (server *Server) Run() error {
if err != nil {
return err
}
defer closeListener(listener)
defer utils.Close(listener)
slog.Info("代理服务已启动,正在监听端口 " + addr)
@@ -126,7 +126,7 @@ func (server *Server) serve(conn net.Conn) error {
slog.Debug("开始认证流程")
authContext, err := server.authenticate(reader, conn)
if err != nil {
conn.Close()
utils.Close(conn)
slog.Error("认证失败", err)
return err
} else {
@@ -179,21 +179,3 @@ func checkVersion(reader io.Reader) error {
return nil
}
// closeListener 关闭监听并处理可能的错误
func closeListener(listener net.Listener) {
err := listener.Close()
if err != nil {
slog.Info("结束监听端口")
}
}
// closeConnection 关闭连接并处理可能的错误
func closeConnection(conn net.Conn) {
err := conn.Close()
if err != nil {
slog.Error("连接异常关闭", err)
} else {
slog.Info("已关闭来自" + conn.RemoteAddr().String() + "的连接")
}
}

View File

@@ -1,8 +1,9 @@
package models
import (
"gorm.io/gorm"
"time"
"gorm.io/gorm"
)
// Channel 连接认证模型

View File

@@ -1,8 +1,9 @@
package router
import (
"github.com/gin-gonic/gin"
"proxy-server/server/web/app/handlers"
"github.com/gin-gonic/gin"
)
func Apply(r *gin.Engine) {