watch config file

This commit is contained in:
tursom 2024-10-03 19:48:24 +08:00
parent 7435cf3823
commit 7a8eaa1feb
4 changed files with 67 additions and 4 deletions

2
README.md Normal file
View File

@ -0,0 +1,2 @@
# mc-gateway
This project is a Minecraft game server gateway. It allows you to access multiple game servers through different hostnames, using the same port.

View File

@ -7,6 +7,9 @@ import (
"net"
"os"
"sync"
"github.com/fsnotify/fsnotify"
"github.com/rs/zerolog/log"
)
type Config struct {
@ -32,24 +35,51 @@ func loadConfig() error {
return json.Unmarshal(byteValue, &config)
}
func watchConfig() *fsnotify.Watcher {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal().Err(err).Msg("Failed to create config watcher")
}
go func() {
for {
_, ok := <-watcher.Events
if !ok {
return
}
loadConfig()
}
}()
err = watcher.Add("config.json")
if err != nil {
log.Fatal().Err(err).Msg("Failed to watch config file")
}
return watcher
}
func main() {
if err := loadConfig(); err != nil {
panic(err)
}
watcher := watchConfig()
defer watcher.Close()
// 监听TCP端口
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", config.Port))
if err != nil {
panic(err)
}
defer listener.Close()
fmt.Println("Listening on :25565")
log.Info().
Int("port", config.Port).
Msg("Listening")
for {
// 接受传入的连接
conn, err := listener.Accept()
if err != nil {
fmt.Println("Error accepting:", err.Error())
log.Err(err).Msg("Error accepting")
continue
}
// 处理连接
@ -64,7 +94,7 @@ func handleRequest(conn net.Conn) {
buf := make([]byte, 1024)
n, err := conn.Read(buf)
if err != nil {
fmt.Println("Error reading:", err.Error())
log.Err(err).Msg("Error reading hostname")
return
}
mc_host := GetMcHost(buf[:n])
@ -75,7 +105,7 @@ func handleRequest(conn net.Conn) {
client, err := net.Dial("tcp", host)
if err != nil {
fmt.Println("Error dialing:", err.Error())
log.Err(err).Msg("Error dialing")
return
}
defer client.Close()

12
go.mod
View File

@ -1,3 +1,15 @@
module github.com/tursom/mc-gateway
go 1.23.1
require github.com/fsnotify/fsnotify v1.7.0
require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
)
require (
github.com/rs/zerolog v1.33.0
golang.org/x/sys v0.12.0 // indirect
)

19
go.sum Normal file
View File

@ -0,0 +1,19 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=