-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (94 loc) · 2.33 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"strings"
"syscall"
"time"
proxyPkg "golem/proxy"
serverPkg "golem/server"
"golem/server/process"
)
func main() {
var proxyAddr string
var serverAddr string
var serverStart string
var serverDirectory string
var stopTimeout int
var versionName string
var versionProtocol int
var playersMax int
var debug bool
// Define flags
flag.StringVar(&proxyAddr, "proxyAddr", ":25565",
"Proxy server address")
flag.StringVar(&serverAddr, "serverAddr", ":25566",
"Minecraft server address")
flag.StringVar(&serverStart, "serverStart", "",
"Minecraft start command. Empty disables autostart/stop")
flag.StringVar(&serverDirectory, "serverDirectory", "",
"Minecraft server working directory")
flag.IntVar(&stopTimeout, "stopTimeout", 60,
"Wait period to stop server after last disconnect (seconds)")
flag.StringVar(&versionName, "versionName", "1.17.1",
"Minecraft version name")
flag.IntVar(&versionProtocol, "versionProtocol", 756,
"Minecraft protocol version")
flag.IntVar(&playersMax, "playersMax", 20,
"Maximum number of players (to display in status message)")
flag.BoolVar(&debug, "debug", false,
"Log all traffic")
flag.Parse()
// Create server depending on if server start command was given
var server serverPkg.Server
var timeDuration *time.Duration
if serverStart == "" {
server = serverPkg.NewBasicServer()
} else {
server = process.NewProcessServer(
newLogger("[server] "),
strings.Fields(serverStart),
serverDirectory,
)
// Make time duration reference
d := time.Duration(stopTimeout) * time.Second
timeDuration = &d
}
// Make optional packet logger
var protocolLogger *log.Logger
if debug {
protocolLogger = newLogger("[protocol] ")
}
// Make proxy
proxy := proxyPkg.NewProxy(
newLogger("[proxy] "),
proxyAddr,
serverAddr,
timeDuration,
server,
protocolLogger,
versionName,
versionProtocol,
playersMax,
)
// Listen for SIGINT or SIGTERM and safely exit
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
server.Stop()
os.Exit(1)
}()
// Run proxy
err := proxy.Run()
if err != nil {
fmt.Printf("error starting proxy: %s\n", err)
}
}
// newLogger makes a new logger to stdout.
func newLogger(prefix string) *log.Logger {
return log.New(os.Stdout, prefix, 0)
}