forked from HeroesAwaken/GoFesl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·262 lines (218 loc) · 7.51 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package main
import (
"flag"
"fmt"
"io/ioutil"
"net/http/httputil"
"os"
"os/signal"
"runtime"
"strings"
"time"
"github.com/HeroesAwaken/GoAwaken/core"
"github.com/HeroesAwaken/GoFesl/GameSpy"
"github.com/HeroesAwaken/GoFesl/fesl"
"github.com/HeroesAwaken/GoFesl/log"
"github.com/HeroesAwaken/GoFesl/matchmaking"
"github.com/HeroesAwaken/GoFesl/theater"
"github.com/go-redis/redis"
"github.com/gorilla/mux"
"net/http"
"net/http/pprof"
)
// Initialize flag-parameters and config
func init() {
flag.StringVar(&configPath, "config", "config.yml", "Path to yml configuration file")
flag.StringVar(&logLevel, "logLevel", "error", "LogLevel [error|warning|note|debug]")
flag.StringVar(&certFileFlag, "cert", "cert.pem", "[HTTPS] Location of your certification file. Env: LOUIS_HTTPS_CERT")
flag.StringVar(&keyFileFlag, "key", "key.pem", "[HTTPS] Location of your private key file. Env: LOUIS_HTTPS_KEY")
flag.BoolVar(&localMode, "localMode", false, "Use in local modus")
flag.Parse()
log.SetLevel(logLevel)
MyConfig.Load(configPath)
if CompileVersion != "0" {
Version = Version + "." + CompileVersion
}
}
var (
configPath string
logLevel string
certFileFlag string
keyFileFlag string
localMode bool
// CompileVersion we are receiving by the build command
CompileVersion = "0"
// Version of the Application
Version = "0.0.7"
// MyConfig Default configuration
MyConfig = Config{
MysqlServer: "localhost:3306",
MysqlUser: "loginserver",
MysqlDb: "loginserver",
MysqlPw: "",
}
mem runtime.MemStats
AppName = "HeroesServer"
Shard string
)
func emtpyHandler(w http.ResponseWriter, r *http.Request) {
log.Noteln("EMTPTY", r.URL.Path)
LogMagmaRequest(r, "requestEmtpy")
fmt.Fprintf(w, "<update><status>Online</status></update>")
}
func relationship(w http.ResponseWriter, r *http.Request) {
log.Noteln("RELATIONSHIP", r.URL.Path)
LogMagmaRequest(r, "requestRelationship")
vars := mux.Vars(r)
fmt.Fprintf(w, "<update><id>1</id><name>Test</name><state>ACTIVE</state><type>server</type><status>Online</status><realid>"+vars["id"]+"</realid></update>")
}
func sessionHandler(w http.ResponseWriter, r *http.Request) {
serverKey := r.Header.Get("X-SERVER-KEY")
if serverKey != "" {
log.Noteln("Server " + serverKey + " authenticating.")
fmt.Fprintf(w, "<success><token>"+serverKey+"</token></success>")
} else {
userKey, err := r.Cookie("magma")
if err != nil {
}
log.Noteln("<success><token code=\"NEW_TOKEN\">" + userKey.Value + "</token></success>")
fmt.Fprintf(w, "<success><token code=\"NEW_TOKEN\">"+userKey.Value+"</token></success>")
}
}
func entitlementsHandler(w http.ResponseWriter, r *http.Request) {
log.Noteln("ENTITLEMENTS", r.URL.Path)
LogMagmaRequest(r, "requestEntitlements")
vars := mux.Vars(r)
fmt.Fprintf(w,
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>"+
"<entitlements>"+
" <entitlement>"+
" <entitlementId>1</entitlementId>"+
" <entitlementTag>WEST_Custom_Item_142</entitlementTag>"+
" <status>ACTIVE</status>"+
" <userId>"+vars["heroID"]+"</userId>"+
" </entitlement>"+
" <entitlement>"+
" <entitlementId>1253</entitlementId>"+
" <entitlementTag>WEST_Custom_Item_142</entitlementTag>"+
" <status>ACTIVE</status>"+
" <userId>"+vars["heroID"]+"</userId>"+
" </entitlement>"+
"</entitlements>")
}
func offersHandler(w http.ResponseWriter, r *http.Request) {
log.Noteln("OFFERS", r.URL.Path)
LogMagmaRequest(r, "requestOffers")
contents, _ := ioutil.ReadFile("api/products.xml")
str := string(contents)
fmt.Fprintf(w, str)
}
func walletsHandler(w http.ResponseWriter, r *http.Request) {
log.Noteln("WALLETS", r.URL.Path)
LogMagmaRequest(r, "requestWallets")
//vars := mux.Vars(r)
fmt.Fprintf(w, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?><billingAccounts><walletAccount><currency>hp</currency><balance>1</balance></billingAccounts>")
}
// LogMagmaRequest log data to a debug file for further analysis
func LogMagmaRequest(r *http.Request, commandType string) {
b, err := httputil.DumpRequest(r, true)
if err != nil {
panic(err)
}
os.MkdirAll("./magma/"+commandType+"", 0777)
err = ioutil.WriteFile("./magma/"+commandType+"/"+strings.Replace(r.URL.Path, "/", "_", -1)+r.URL.RawQuery, b, 0644)
if err != nil {
panic(err)
}
}
func collectGlobalMetrics(iDB *core.InfluxDB) {
runtime.ReadMemStats(&mem)
tags := map[string]string{"metric": "server_metrics", "server": "global"}
fields := map[string]interface{}{
"memAlloc": int(mem.Alloc),
"memTotalAlloc": int(mem.TotalAlloc),
"memHeapAlloc": int(mem.HeapAlloc),
"memHeapSys": int(mem.HeapSys),
}
iDB.AddMetric("server_metrics", tags, fields)
iDB.Flush()
}
func main() {
log.Notef("Starting up v%s", Version)
r := mux.NewRouter()
// Register pprof handlers
r.HandleFunc("/debug/pprof/", pprof.Index)
r.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
r.HandleFunc("/debug/pprof/profile", pprof.Profile)
r.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
r.HandleFunc("/debug/pprof/trace", pprof.Trace)
r.HandleFunc("/nucleus/authToken", sessionHandler)
r.HandleFunc("/relationships/roster/{type}:{id}", relationship)
r.HandleFunc("/nucleus/entitlements/{heroID}", entitlementsHandler)
r.HandleFunc("/nucleus/wallets/{heroID}", walletsHandler)
r.HandleFunc("/ofb/products", offersHandler)
r.HandleFunc("/", emtpyHandler)
if localMode {
go func() {
log.Noteln(http.ListenAndServe("0.0.0.0:80", r))
}()
go func() {
log.Noteln(http.ListenAndServeTLS("0.0.0.0:443", certFileFlag, keyFileFlag, r))
}()
} else {
go func() {
log.Noteln(http.ListenAndServe("0.0.0.0:8080", r))
}()
go func() {
log.Noteln(http.ListenAndServeTLS("0.0.0.0:8443", certFileFlag, keyFileFlag, r))
}()
}
// Startup done
// DB Connection
dbConnection := new(core.DB)
dbSQL, err := dbConnection.New(MyConfig.MysqlServer, MyConfig.MysqlDb, MyConfig.MysqlUser, MyConfig.MysqlPw)
if err != nil {
log.Fatalln("Error connecting to DB:", err)
}
// Redis Connection
redisClient := redis.NewClient(&redis.Options{
Addr: MyConfig.RedisServer,
Password: MyConfig.RedisPassword,
DB: MyConfig.RedisDB,
})
_, err = redisClient.Ping().Result()
if err != nil {
log.Fatalln("Error connecting to redis:", err)
}
// Influx Connection
metricConnection := new(core.InfluxDB)
err = metricConnection.New(MyConfig.InfluxDBHost, MyConfig.InfluxDBDatabase, MyConfig.InfluxDBUser, MyConfig.InfluxDBPassword, AppName, Version)
if err != nil {
log.Fatalln("Error connecting to MetricsDB:", err)
}
globalMetrics := time.NewTicker(time.Second * 10)
go func() {
for range globalMetrics.C {
collectGlobalMetrics(metricConnection)
}
}()
Shard := GameSpy.BF2RandomUnsafe(6)
log.Noteln("Starting up as shard: " + Shard)
matchmaking.Shard = Shard
theater.Shard = Shard
fesl.Shard = Shard
feslManager := new(fesl.FeslManager)
feslManager.New("FM", "18270", certFileFlag, keyFileFlag, false, dbSQL, redisClient, metricConnection, localMode)
serverManager := new(fesl.FeslManager)
serverManager.New("SFM", "18051", certFileFlag, keyFileFlag, true, dbSQL, redisClient, metricConnection, localMode)
theaterManager := new(theater.TheaterManager)
theaterManager.New("TM", "18275", dbSQL, redisClient, metricConnection, localMode)
servertheaterManager := new(theater.TheaterManager)
servertheaterManager.New("STM", "18056", dbSQL, redisClient, metricConnection, localMode)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
for sig := range c {
log.Noteln("Captured" + sig.String() + ". Shutting down.")
os.Exit(0)
}
}