Files
GoTunnel/pkg/proxy/server.go
Flik e0d88e9ad7
Some checks failed
Build Multi-Platform Binaries / build-binaries (amd64, darwin, server, false) (push) Has been cancelled
Build Multi-Platform Binaries / build-binaries (amd64, linux, client, true) (push) Has been cancelled
Build Multi-Platform Binaries / build-binaries (amd64, linux, server, true) (push) Has been cancelled
Build Multi-Platform Binaries / build-binaries (amd64, windows, client, true) (push) Has been cancelled
Build Multi-Platform Binaries / build-binaries (amd64, windows, server, true) (push) Has been cancelled
Build Multi-Platform Binaries / build-binaries (arm, 7, linux, client, true) (push) Has been cancelled
Build Multi-Platform Binaries / build-binaries (arm, 7, linux, server, true) (push) Has been cancelled
Build Multi-Platform Binaries / build-binaries (arm64, darwin, server, false) (push) Has been cancelled
Build Multi-Platform Binaries / build-binaries (arm64, linux, client, true) (push) Has been cancelled
Build Multi-Platform Binaries / build-binaries (arm64, linux, server, true) (push) Has been cancelled
Build Multi-Platform Binaries / build-binaries (arm64, windows, server, false) (push) Has been cancelled
Build Multi-Platform Binaries / build-frontend (push) Has been cancelled
feat: Implement core tunnel server with client management, authentication, and SOCKS5/HTTP proxy capabilities.
2026-02-03 21:59:55 +08:00

63 lines
1.1 KiB
Go

package proxy
import (
"log"
"net"
)
// Server 代理服务器
type Server struct {
socks5 *SOCKS5Server
http *HTTPServer
listener net.Listener
typ string
}
// NewServer 创建代理服务器
func NewServer(typ string, dialer Dialer, onStats func(in, out int64)) *Server {
return &Server{
socks5: NewSOCKS5Server(dialer, onStats),
http: NewHTTPServer(dialer, onStats),
typ: typ,
}
}
// Run 启动代理服务
func (s *Server) Run(addr string) error {
ln, err := net.Listen("tcp", addr)
if err != nil {
return err
}
s.listener = ln
log.Printf("[Proxy] %s listening on %s", s.typ, addr)
for {
conn, err := ln.Accept()
if err != nil {
return err
}
go s.HandleConn(conn)
}
}
func (s *Server) HandleConn(conn net.Conn) {
var err error
switch s.typ {
case "socks5":
err = s.socks5.HandleConn(conn)
case "http", "https":
err = s.http.HandleConn(conn)
}
if err != nil {
log.Printf("[Proxy] Error: %v", err)
}
}
// Close 关闭服务
func (s *Server) Close() error {
if s.listener != nil {
return s.listener.Close()
}
return nil
}