Files
mc-gateway/cmd/gateway/main.go
tursom 8530df6cdf
Some checks failed
Go / build (.exe, 386, windows, windows-386) (push) Has been cancelled
Go / build (.exe, amd64, windows, windows-amd64) (push) Has been cancelled
Go / build (.exe, arm64, windows, windows-arm64) (push) Has been cancelled
Go / build (386, freebsd, freebsd-386) (push) Has been cancelled
Go / build (386, linux, linux-386) (push) Has been cancelled
Go / build (386, netbsd, netbsd-386) (push) Has been cancelled
Go / build (386, openbsd, openbsd-386) (push) Has been cancelled
Go / build (386, plan9, plan9-386) (push) Has been cancelled
Go / build (amd64, darwin, darwin-amd64) (push) Has been cancelled
Go / build (amd64, dragonfly, dragonfly-amd64) (push) Has been cancelled
Go / build (amd64, freebsd, freebsd-amd64) (push) Has been cancelled
Go / build (amd64, illumos, illumos-amd64) (push) Has been cancelled
Go / build (amd64, linux, linux-amd64) (push) Has been cancelled
Go / build (amd64, netbsd, netbsd-amd64) (push) Has been cancelled
Go / build (amd64, openbsd, openbsd-amd64) (push) Has been cancelled
Go / build (amd64, plan9, plan9-amd64) (push) Has been cancelled
Go / build (amd64, solaris, solaris-amd64) (push) Has been cancelled
Go / build (arm, 6, linux, linux-armv6) (push) Has been cancelled
Go / build (arm, 7, linux, linux-armv7) (push) Has been cancelled
Go / build (arm, freebsd, freebsd-arm) (push) Has been cancelled
Go / build (arm, netbsd, netbsd-arm) (push) Has been cancelled
Go / build (arm, openbsd, openbsd-arm) (push) Has been cancelled
Go / build (arm, plan9, plan9-arm) (push) Has been cancelled
Go / build (arm64, darwin, darwin-arm64) (push) Has been cancelled
Go / build (arm64, freebsd, freebsd-arm64) (push) Has been cancelled
Go / build (arm64, linux, linux-arm64) (push) Has been cancelled
Go / build (arm64, netbsd, netbsd-arm64) (push) Has been cancelled
Go / build (arm64, openbsd, openbsd-arm64) (push) Has been cancelled
Go / build (loong64, linux, linux-loong64) (push) Has been cancelled
Go / build (mips, linux, linux-mips) (push) Has been cancelled
Go / build (mips64, linux, linux-mips64) (push) Has been cancelled
Go / build (mips64le, linux, linux-mips64le) (push) Has been cancelled
Go / build (mipsle, linux, linux-mipsle) (push) Has been cancelled
Go / build (ppc64, aix, aix-ppc64) (push) Has been cancelled
Go / build (ppc64, linux, linux-ppc64) (push) Has been cancelled
Go / build (ppc64, openbsd, openbsd-ppc64) (push) Has been cancelled
Go / build (ppc64le, linux, linux-ppc64le) (push) Has been cancelled
Go / build (riscv64, freebsd, freebsd-riscv64) (push) Has been cancelled
Go / build (riscv64, linux, linux-riscv64) (push) Has been cancelled
Go / build (riscv64, openbsd, openbsd-riscv64) (push) Has been cancelled
Go / build (s390x, linux, linux-s390x) (push) Has been cancelled
Go / merge-artifacts (push) Has been cancelled
支持 KCP 协议
2025-07-01 08:02:04 +08:00

186 lines
3.3 KiB
Go

package main
import (
"fmt"
"io"
"net"
"sync"
"time"
"github.com/rs/zerolog/log"
"github.com/tursom/mc-gateway/protocol"
)
func main() {
if err := writePIDFile(); err != nil {
panic(fmt.Sprintf("Failed to write PID file: %v", err))
}
defer removePIDFile()
if err := loadConfig(); err != nil {
panic(err)
}
watcher := watchConfig()
defer watcher.Close()
go handleLogRotate()
var wg sync.WaitGroup
defer wg.Wait()
// 启动QUIC服务
if config.Quic.Enable {
wg.Add(1)
go runQuic(&wg)
}
if config.Kcp.Enable {
wg.Add(1)
go runKcp(&wg)
}
// 监听TCP端口
if config.Tcp.Enable {
wg.Add(1)
go runTcp(&wg)
}
}
func runTcp(wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", config.Tcp.Port))
if err != nil {
log.Fatal().Err(err).
Int("port", config.Tcp.Port).
Msg("Failed to listen on port")
}
defer listener.Close()
log.Info().
Int("port", config.Tcp.Port).
Msg("Listening for TCP connections")
for {
// 接受传入的连接
conn, err := listener.Accept()
if err != nil {
log.Err(err).Msg("Error accepting")
continue
}
// 处理连接
go handleRequest(conn)
}
}
func handleRequest(conn net.Conn) {
defer func() {
rec := recover()
if rec == nil {
return
}
if err, ok := rec.(error); ok {
log.Err(err).
Str("client", conn.RemoteAddr().String()).
Msg("Panic on handle request")
} else {
log.Error().Any("err", rec).
Str("client", conn.RemoteAddr().String()).
Msg("Panic on handle request")
}
}()
// 确保连接关闭
defer conn.Close()
setSocketOptions(conn)
buf := make([]byte, 1024)
n, err := conn.Read(buf)
if err != nil {
log.Err(err).
Str("client", conn.RemoteAddr().String()).
Msg("Error reading hostname")
return
}
if n == 0 {
log.Err(errEmptyBuffer).
Str("client", conn.RemoteAddr().String()).
Msg("Error: buffer is empty")
return
}
mc_host := protocol.GetMcHost(buf[:n])
host, ok := config.Hosts[mc_host]
if !ok {
host = config.Hosts["default"]
}
if host == "" {
log.Err(errEmptyBuffer).
Str("client", conn.RemoteAddr().String()).
Str("host", mc_host).
Msg("failed to route host")
return
}
log.Info().
Str("client", conn.RemoteAddr().String()).
Str("host", mc_host).
Str("mc", host).
Msg("map to host")
client, err := net.Dial("tcp", host)
if err != nil {
log.Err(err).Msg("Error dialing")
return
}
defer client.Close()
setSocketOptions(client)
client.Write(buf[:n])
// 不需要 buf 了,释放掉
buf = nil
var wg sync.WaitGroup
wg.Add(1)
go handleRead(client, conn, &wg)
handleWrite(client, conn, nil)
// 等待所有读写操作完成
// 不放在 defer 中,以防报错时无法关闭连接
wg.Wait()
}
func handleRead(srv, cli net.Conn, wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
_, err := io.Copy(srv, cli)
if err != nil && err != io.EOF {
log.Err(err).Msg("Error copying data")
}
}
func handleWrite(srv, cli net.Conn, wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
_, err := io.Copy(cli, srv)
if err != nil && err != io.EOF {
log.Err(err).Msg("Error copying data")
}
}
func setSocketOptions(conn net.Conn) {
if tcpConn, ok := conn.(*net.TCPConn); ok {
tcpConn.SetNoDelay(true) // 禁用 Nagle 算法
tcpConn.SetKeepAlive(true)
tcpConn.SetKeepAlivePeriod(30 * time.Second)
}
}