代码清理
This commit is contained in:
@@ -5,6 +5,7 @@ WORKDIR /app
|
|||||||
COPY ./bin/proxy_server_linux_amd64 /app/proxy
|
COPY ./bin/proxy_server_linux_amd64 /app/proxy
|
||||||
RUN chmod +x /app/proxy
|
RUN chmod +x /app/proxy
|
||||||
|
|
||||||
EXPOSE $PORT
|
EXPOSE $APP_CTRL_PORT
|
||||||
|
EXPOSE $APP_PROXY_PORT
|
||||||
|
|
||||||
CMD ["/app/proxy"]
|
CMD ["/app/proxy"]
|
||||||
@@ -12,7 +12,9 @@
|
|||||||
|
|
||||||
实现一个 socks context 以在子组件中获取 socks 相关信息
|
实现一个 socks context 以在子组件中获取 socks 相关信息
|
||||||
|
|
||||||
fwd 使用自定义 context 实现在一个上下文中控制 cancel,errch 和其他自定义数据
|
fwd 使用自定义 context 实现在一个上下文中控制 cancel,errCh 和其他自定义数据
|
||||||
|
|
||||||
|
网关根据代理节点对目标服务连接的反馈,决定向用户返回的 socks 响应
|
||||||
|
|
||||||
### 长期
|
### 长期
|
||||||
|
|
||||||
|
|||||||
@@ -1,29 +1,22 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"proxy-server/server/pkg/env"
|
||||||
"proxy-server/server/pkg/orm"
|
"proxy-server/server/pkg/orm"
|
||||||
|
|
||||||
"gorm.io/gen"
|
"gorm.io/gen"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
Host = "localhost"
|
|
||||||
Port = "5432"
|
|
||||||
Username = "gorm"
|
|
||||||
Password = "gorm"
|
|
||||||
Database = "gorm"
|
|
||||||
Timezone = "Asia/Shanghai"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
env.Init()
|
||||||
|
orm.Init()
|
||||||
|
|
||||||
g := gen.NewGenerator(gen.Config{
|
g := gen.NewGenerator(gen.Config{
|
||||||
OutPath: "../../temp-out",
|
OutPath: "../../temp-out",
|
||||||
Mode: gen.WithoutContext | gen.WithDefaultQuery | gen.WithQueryInterface,
|
Mode: gen.WithoutContext | gen.WithDefaultQuery | gen.WithQueryInterface,
|
||||||
})
|
})
|
||||||
|
|
||||||
orm.Init()
|
|
||||||
g.UseDB(orm.DB)
|
g.UseDB(orm.DB)
|
||||||
|
|
||||||
g.ApplyBasic(
|
g.ApplyBasic(
|
||||||
g.GenerateAllTable()...,
|
g.GenerateAllTable()...,
|
||||||
)
|
)
|
||||||
@@ -7,12 +7,12 @@ import (
|
|||||||
|
|
||||||
type CountWaitGroup struct {
|
type CountWaitGroup struct {
|
||||||
wg sync.WaitGroup
|
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.wg.Add(int(delta))
|
||||||
c.num.Add(delta)
|
c.num.Add(int64(delta))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CountWaitGroup) Done() {
|
func (c *CountWaitGroup) Done() {
|
||||||
@@ -25,5 +25,5 @@ func (c *CountWaitGroup) Wait() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *CountWaitGroup) Count() uint64 {
|
func (c *CountWaitGroup) Count() uint64 {
|
||||||
return c.num.Load()
|
return uint64(c.num.Load())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package orm
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"proxy-server/server/pkg/env"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"gorm.io/driver/postgres"
|
"gorm.io/driver/postgres"
|
||||||
@@ -14,16 +14,9 @@ import (
|
|||||||
var DB *gorm.DB
|
var DB *gorm.DB
|
||||||
|
|
||||||
func Init() {
|
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(
|
dsn := fmt.Sprintf(
|
||||||
"host=%s port=%s user=%s password=%s dbname=%s sslmode=disable TimeZone=%s",
|
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable TimeZone=%s",
|
||||||
Host, Port, Username, Password, Database, Timezone,
|
env.DbHost, env.DbPort, env.DbUsername, env.DbPassword, env.DbDatabase, env.DbTimezone,
|
||||||
)
|
)
|
||||||
|
|
||||||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
|
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
unrecognizedAddrType = fmt.Errorf("Unrecognized address type")
|
unrecognizedAddrType = fmt.Errorf("unrecognized address type")
|
||||||
)
|
)
|
||||||
|
|
||||||
// AddressRewriter is used to rewrite a destination transparently
|
// 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 {
|
if err != nil {
|
||||||
err := sendReply(writer, hostUnreachable, nil)
|
err := sendReply(writer, hostUnreachable, nil)
|
||||||
if err != 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
|
dest.IP = addr
|
||||||
|
|
||||||
@@ -308,16 +308,16 @@ func (server *Server) handleBind(ctx context.Context, conn net.Conn, req *Reques
|
|||||||
// Check if this is allowed
|
// Check if this is allowed
|
||||||
if ctx_, ok := server.config.Rules.Allow(ctx, req); !ok {
|
if ctx_, ok := server.config.Rules.Allow(ctx, req); !ok {
|
||||||
if err := sendReply(conn, ruleFailure, nil); err != nil {
|
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 {
|
} else {
|
||||||
ctx = ctx_
|
ctx = ctx_
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Support bind
|
// TODO: Support bind
|
||||||
if err := sendReply(conn, commandNotSupported, nil); err != nil {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
@@ -326,16 +326,16 @@ func (server *Server) handleAssociate(ctx context.Context, conn net.Conn, req *R
|
|||||||
// Check if this is allowed
|
// Check if this is allowed
|
||||||
if ctx_, ok := server.config.Rules.Allow(ctx, req); !ok {
|
if ctx_, ok := server.config.Rules.Allow(ctx, req); !ok {
|
||||||
if err := sendReply(conn, ruleFailure, nil); err != nil {
|
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 {
|
} else {
|
||||||
ctx = ctx_
|
ctx = ctx_
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Support associate
|
// TODO: Support associate
|
||||||
if err := sendReply(conn, commandNotSupported, nil); err != nil {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ func (server *Server) Run() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer closeListener(listener)
|
defer utils.Close(listener)
|
||||||
|
|
||||||
slog.Info("代理服务已启动,正在监听端口 " + addr)
|
slog.Info("代理服务已启动,正在监听端口 " + addr)
|
||||||
|
|
||||||
@@ -126,7 +126,7 @@ func (server *Server) serve(conn net.Conn) error {
|
|||||||
slog.Debug("开始认证流程")
|
slog.Debug("开始认证流程")
|
||||||
authContext, err := server.authenticate(reader, conn)
|
authContext, err := server.authenticate(reader, conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
conn.Close()
|
utils.Close(conn)
|
||||||
slog.Error("认证失败", err)
|
slog.Error("认证失败", err)
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
@@ -179,21 +179,3 @@ func checkVersion(reader io.Reader) error {
|
|||||||
|
|
||||||
return nil
|
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() + "的连接")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"gorm.io/gorm"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Channel 连接认证模型
|
// Channel 连接认证模型
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package router
|
package router
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"proxy-server/server/web/app/handlers"
|
"proxy-server/server/web/app/handlers"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Apply(r *gin.Engine) {
|
func Apply(r *gin.Engine) {
|
||||||
|
|||||||
Reference in New Issue
Block a user