-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
134 lines (107 loc) · 3.06 KB
/
main.go
File metadata and controls
134 lines (107 loc) · 3.06 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"crypto/tls"
"flag"
"fmt"
"goFactServView/cwlog"
"html/template"
"net/http"
"os"
"time"
)
const (
Version = "0.3.2"
VDate = "09-09-2025-0231p"
ProgName = "goFactServView"
UserAgent = ProgName + "-" + Version
VString = ProgName + "v" + Version + " (" + VDate + ") "
//How long to wait for list server
ReqTimeout = time.Second * 3
//How often we can make a request, including on error.
ReqThrottle = time.Second * 30
//How often we can refresh when new requests come in
RefreshInterval = time.Minute * 5
//Timeout before our http(s) servers time out
ServerTimeout = 10 * time.Second
//How often to refresh if there are no requests
BGFetchInterval = time.Hour * 3
//If we get less results than this, assume the data is incomplete or corrupt
MinValidCount = 25
//Servers per page
ItemsPerPage = 25
)
var (
sParam ServerStateData
tmpl *template.Template
bindIP *string
bindPortHTTPS *int
bindPortHTTP *int
fileServer http.Handler
)
func main() {
//Parse parameters
sParam = ServerStateData{UserAgent: UserAgent}
sParam.URL = flag.String("url", "multiplayer.factorio.com", "domain name to query")
sParam.Token = flag.String("token", "", "Matchmaking API token")
sParam.Username = flag.String("username", "", "Matchmaking API username")
bindIP = flag.String("ip", "", "IP to bind to")
bindPortHTTPS = flag.Int("httpsPort", 443, "port to bind to for HTTPS")
bindPortHTTP = flag.Int("httpPort", 80, "port to bind to")
flag.Parse()
//Require token/username
if *sParam.Token == "" || *sParam.Username == "" {
cwlog.DoLog(false, "You must supply a username and token. -h for help.")
os.Exit(1)
return
}
//Defer to give log time to write on close
defer time.Sleep(time.Second * 2)
cwlog.StartLog()
cwlog.LogDaemon()
//Pretty time formatting
setupDurafmt()
//Read cache.json
ReadServerCache()
if err := fetchServerList(); err != nil {
cwlog.DoLog(true, "Initial fetch failed: %v", err)
}
//Parse template.html
parseTemplate()
//HTTP(s) fileserver
fileServer = http.FileServer(http.Dir("data/www"))
go backgroundUpdateList()
//HTTP listen
go func() {
buf := fmt.Sprintf("%v:%v", *bindIP, *bindPortHTTP)
if err := http.ListenAndServe(buf, http.HandlerFunc(reqHandle)); err != nil {
cwlog.DoLog(true, "ListenAndServe error: %v", err)
}
}()
http.HandleFunc("/", reqHandle)
if err := loadCerts(); err != nil {
cwlog.DoLog(true, "%v", err)
return
}
config := &tls.Config{
GetCertificate: getCertificate,
InsecureSkipVerify: false,
}
server := &http.Server{
Addr: fmt.Sprintf("%v:%v", *bindIP, *bindPortHTTPS),
Handler: http.DefaultServeMux,
TLSConfig: config,
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler), 0),
ReadTimeout: ServerTimeout,
WriteTimeout: ServerTimeout,
IdleTimeout: ServerTimeout,
}
go autoUpdateCert()
//https listen
cwlog.DoLog(true, "Server started.")
err := server.ListenAndServeTLS("", "")
if err != nil {
cwlog.DoLog(true, "ListenAndServeTLS: %v", err)
return
}
cwlog.DoLog(true, "Goodbye.")
}