forked from realDragonium/Ultraviolet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (57 loc) · 1.25 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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/cloudflare/tableflip"
"github.com/realDragonium/Ultraviolet/conn"
"github.com/realDragonium/Ultraviolet/proxy"
)
func main() {
fmt.Println("Starting up")
var (
pidFile = flag.String("pid-file", "/run/ultraviolet.pid", "`Path` to pid file")
)
flag.Parse()
log.SetPrefix(fmt.Sprintf("%d ", os.Getpid()))
upg, err := tableflip.New(tableflip.Options{
// UpgradeTimeout: time.Duration(24 * time.Hour),
PIDFile: *pidFile,
})
if err != nil {
panic(err)
}
defer upg.Stop()
go func() {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGHUP)
for range sig {
err := upg.Upgrade()
if err != nil {
log.Println("upgrade failed:", err)
}
}
}()
ln, err := upg.Listen("tcp", ":25565")
if err != nil {
log.Fatalln("Can't listen:", err)
}
defer ln.Close()
reqCh := make(chan conn.ConnRequest)
go conn.Serve(ln, reqCh)
p := proxy.NewProxy(reqCh)
p.Serve()
log.Printf("ready")
if err := upg.Ready(); err != nil {
panic(err)
}
log.Println("Upgrade in process...")
<-upg.Exit()
p.ShouldNotifyCh <- struct{}{}
log.Println("Waiting for all open connections to close before shutting down")
<-p.NotifyCh
log.Println("Shutting down")
}