forked from JustaPenguin/assetto-server-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_manager_wrapper.go
680 lines (517 loc) · 17.2 KB
/
content_manager_wrapper.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
package servermanager
import (
"bytes"
"context"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"regexp"
"strconv"
"strings"
"sync"
"text/template"
"time"
"github.com/JustaPenguin/assetto-server-manager/pkg/udp"
"github.com/google/uuid"
"github.com/jaytaylor/html2text"
"github.com/sirupsen/logrus"
)
const (
// contentManagerWrapperSeparator is a special character used by Content Manager to determine which port
// the content manager wrapper is running on. The server name shows "<server name> <separator><port>",
// which Content Manager uses to split the string and find the port.
contentManagerWrapperSeparator = 'ℹ'
ContentManagerJoinLinkBase string = "https://acstuff.ru/s/q:race/online/join"
)
type ContentManagerWrapperData struct {
ACHTTPSessionInfo
Players ACHTTPPlayers `json:"players"`
Description string `json:"description"`
AmbientTemperature uint8 `json:"ambientTemperature"`
RoadTemperature uint8 `json:"roadTemperature"`
WindDirection int `json:"windDirection"`
WindSpeed int `json:"windSpeed"`
CurrentWeatherID string `json:"currentWeatherId"`
Grip int `json:"grip"`
GripTransfer int `json:"gripTransfer"`
Assists CMAssists `json:"assists"`
MaxContactsPerKM int `json:"maxContactsPerKm"`
City string `json:"city"`
PasswordChecksum [2]string `json:"passwordChecksum"`
WrappedPort int `json:"wrappedPort"`
Content *CMContent `json:"content"`
Frequency int `json:"frequency"`
Until int64 `json:"until"`
}
type CMAssists struct {
ABSState FactoryAssist `json:"absState"`
AllowedTyresOut int `json:"allowedTyresOut"`
AutoClutchAllowed bool `json:"autoclutchAllowed"`
DamageMultiplier int `json:"damageMultiplier"`
ForceVirtualMirror bool `json:"forceVirtualMirror"`
FuelRate int `json:"fuelRate"`
StabilityAllowed bool `json:"stabilityAllowed"`
TractionControlState FactoryAssist `json:"tcState"`
TyreBlanketsAllowed bool `json:"tyreBlanketsAllowed"`
TyreWearRate int `json:"tyreWearRate"`
}
type CMContent struct {
Cars map[string]ContentURL `json:"cars"`
Track ContentURL `json:"track"`
// @TODO not functional
Password bool `json:"password"`
}
type ContentURL struct {
URL string `json:"url"`
}
type ACHTTPPlayers struct {
Cars []*CMCar `json:"Cars"`
}
type CMCar struct {
DriverName string `json:"DriverName"`
DriverNation string `json:"DriverNation"`
DriverTeam string `json:"DriverTeam"`
ID string `json:"ID"`
IsConnected bool `json:"IsConnected"`
IsEntryList bool `json:"IsEntryList"`
IsRequestedGUID bool `json:"IsRequestedGUID"`
Model string `json:"Model"`
Skin string `json:"Skin"`
}
type ContentManagerWrapper struct {
store Store
carManager *CarManager
trackManager *TrackManager
sessionInfo udp.SessionInfo
logger cmwSessionLogger
reverseProxy *httputil.ReverseProxy
serverConfig ServerConfig
entryList EntryList
event RaceEvent
srv *http.Server
description string
mutex sync.Mutex
}
func NewContentManagerWrapper(store Store, carManager *CarManager, trackManager *TrackManager) *ContentManagerWrapper {
return &ContentManagerWrapper{
store: store,
carManager: carManager,
trackManager: trackManager,
}
}
func (cmw *ContentManagerWrapper) NewCMContent(cars []string, trackName string, requirePassword bool) (*CMContent, error) {
carsMap := make(map[string]ContentURL)
var trackDownload string
for _, carName := range cars {
car, err := cmw.carManager.LoadCar(carName, nil)
if err != nil {
logrus.WithError(err).Errorf("Couldn't load car for CM Wrapper: %s", carName)
continue
}
carsMap[car.Name] = ContentURL{URL: car.Details.DownloadURL}
}
trackMeta, err := LoadTrackMetaDataFromName(trackName)
if err != nil {
logrus.WithError(err).Errorf("Couldn't load track for CM Wrapper: %s", trackName)
} else {
trackDownload = trackMeta.DownloadURL
}
return &CMContent{
Cars: carsMap,
Track: ContentURL{
URL: trackDownload,
},
Password: requirePassword,
}, nil
}
func (cmw *ContentManagerWrapper) UDPCallback(message udp.Message) {
cmw.mutex.Lock()
defer cmw.mutex.Unlock()
if m, ok := message.(udp.SessionInfo); ok {
cmw.sessionInfo = m
}
}
type contentManagerDescriptionTemplateOpts struct {
GlobalServerConfig
CurrentRaceConfig
RaceEvent
EventDescription string
ChampionshipPoints string
CarDownloads string
TrackDownload string
ServerName string
}
func (cmw *ContentManagerWrapper) setDescriptionText(event RaceEvent) error {
text, err := html2text.FromString(cmw.serverConfig.GlobalServerConfig.ContentManagerWelcomeMessage)
if err != nil {
return err
}
eventDescriptionAsText, err := html2text.FromString(event.EventDescription(), html2text.Options{PrettyTables: true})
if err != nil {
return err
}
var champPoints string
if champ, ok := cmw.event.(*ActiveChampionship); ok {
if u := champ.GetURL(); u != "" {
champPoints = fmt.Sprintf("\n\nView the Championship points here: %s", u)
}
}
if raceWeekend, ok := cmw.event.(*ActiveRaceWeekend); ok {
if u := raceWeekend.GetURL(); u != "" && raceWeekend.ChampionshipID != uuid.Nil {
champPoints = fmt.Sprintf("\n\nView the Championship points here: %s", u)
}
}
var carDownloads string
for _, carName := range strings.Split(cmw.serverConfig.CurrentRaceConfig.Cars, ";") {
car, err := cmw.carManager.LoadCar(carName, nil)
if err != nil {
logrus.WithError(err).Warnf("Could not load car details for: %s, skipping attaching download URLs to Content Manager Wrapper", carName)
continue
}
if car.Details.DownloadURL == "" {
continue
}
carDownloads += fmt.Sprintf("\n* %s Download: %s", car.Details.Name, car.Details.DownloadURL)
}
var trackDownload string
track, err := cmw.trackManager.GetTrackFromName(cmw.serverConfig.CurrentRaceConfig.Track)
if err != nil {
logrus.WithError(err).Warnf("Could not load track: %s, skipping attaching download URL to Content Manager Wrapper", cmw.serverConfig.CurrentRaceConfig.Track)
} else {
err := track.LoadMetaData()
if err != nil {
logrus.WithError(err).Debugf("Could not load meta data for: %s, skipping attaching download URL to Content Manager Wrapper", cmw.serverConfig.CurrentRaceConfig.Track)
} else if track.MetaData.DownloadURL != "" {
trackDownload = fmt.Sprintf("\n* %s Download: %s", track.Name, track.MetaData.DownloadURL)
}
}
// interpret the custom template
t, err := template.New("cmDescription").Parse(text)
if err != nil {
return err
}
out := new(bytes.Buffer)
err = t.Execute(out, contentManagerDescriptionTemplateOpts{
EventDescription: eventDescriptionAsText,
ChampionshipPoints: champPoints,
CarDownloads: carDownloads,
TrackDownload: trackDownload,
CurrentRaceConfig: cmw.serverConfig.CurrentRaceConfig,
GlobalServerConfig: cmw.serverConfig.GlobalServerConfig,
RaceEvent: event,
ServerName: cmw.serverConfig.GlobalServerConfig.Name,
})
if err != nil {
return err
}
cmw.description = out.String()
return nil
}
type cmwSessionLogger interface {
Logs() string
}
func (cmw *ContentManagerWrapper) Start(servePort int, event RaceEvent, logger cmwSessionLogger) error {
cmw.mutex.Lock()
cmw.logger = logger
logrus.Infof("Starting content manager wrapper server on port %d", servePort)
serverOptions, err := cmw.store.LoadServerOptions()
if err != nil {
cmw.mutex.Unlock()
return err
}
u, err := url.Parse(fmt.Sprintf("http://127.0.0.1:%d", serverOptions.HTTPPort))
if err != nil {
cmw.mutex.Unlock()
return err
}
cmw.serverConfig = ServerConfig{GlobalServerConfig: *serverOptions, CurrentRaceConfig: event.GetRaceConfig()}
cmw.entryList = event.GetEntryList()
cmw.event = event
cmw.reverseProxy = httputil.NewSingleHostReverseProxy(u)
if err := cmw.setDescriptionText(event); err != nil {
logrus.WithError(err).Warn("could not set description text")
}
cmw.srv = &http.Server{Addr: fmt.Sprintf(":%d", servePort)}
cmw.srv.Handler = cmw
cmw.mutex.Unlock()
if err := cmw.srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
return err
}
return nil
}
func (cmw *ContentManagerWrapper) Stop() {
cmw.mutex.Lock()
defer cmw.mutex.Unlock()
if cmw.srv == nil {
return
}
logrus.Infof("Shutting down content manager wrapper server")
err := cmw.srv.Shutdown(context.Background())
if err != nil {
logrus.WithError(err).Error("Could not shutdown content manager wrapper server")
}
}
func (cmw *ContentManagerWrapper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/details" {
cmw.reverseProxy.ServeHTTP(w, r)
return
}
details, err := cmw.buildContentManagerDetails(r.URL.Query().Get("guid"))
if err != nil {
logrus.WithError(err).Error("could not build content manager details")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
enc := json.NewEncoder(w)
if Debug {
enc.SetIndent("", " ")
}
_ = enc.Encode(details)
}
type ACHTTPSessionInfo struct {
Cars []string `json:"cars"`
Clients int64 `json:"clients"`
Country []string `json:"country"`
HTTPPort int64 `json:"cport"`
Durations []int64 `json:"durations"`
Extra bool `json:"extra"`
Inverted int64 `json:"inverted"`
IP string `json:"ip"`
JSON interface{} `json:"json"`
L bool `json:"l"`
MaxClients int64 `json:"maxclients"`
Name string `json:"name"`
Pass bool `json:"pass"`
Pickup bool `json:"pickup"`
Pit bool `json:"pit"`
Port int64 `json:"port"`
Session int64 `json:"session"`
Sessiontypes []int64 `json:"sessiontypes"`
Timed bool `json:"timed"`
Timeleft int64 `json:"timeleft"`
Timeofday int64 `json:"timeofday"`
Timestamp int64 `json:"timestamp"`
TCPPort int64 `json:"tport"`
Track string `json:"track"`
}
func (cmw *ContentManagerWrapper) getSessionInfo() (*ACHTTPSessionInfo, error) {
resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d/INFO", cmw.serverConfig.GlobalServerConfig.HTTPPort))
if err != nil {
return nil, err
}
defer resp.Body.Close()
var sessionInfo *ACHTTPSessionInfo
if err := json.NewDecoder(resp.Body).Decode(&sessionInfo); err != nil {
return nil, err
}
return sessionInfo, nil
}
func (cmw *ContentManagerWrapper) getPlayers(guid string) (*ACHTTPPlayers, error) {
resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d/JSON|%s", cmw.serverConfig.GlobalServerConfig.HTTPPort, guid))
if err != nil {
return nil, err
}
defer resp.Body.Close()
var clients *ACHTTPPlayers
if err := json.NewDecoder(resp.Body).Decode(&clients); err != nil {
return nil, err
}
return clients, nil
}
func (cmw *ContentManagerWrapper) buildContentManagerDetails(guid string) (*ContentManagerWrapperData, error) {
cmw.mutex.Lock()
defer cmw.mutex.Unlock()
race := cmw.serverConfig.CurrentRaceConfig
global := cmw.serverConfig.GlobalServerConfig
live := cmw.sessionInfo
sessionInfo, err := cmw.getSessionInfo()
if err != nil {
return nil, err
}
for index, duration := range sessionInfo.Durations {
// convert durations to seconds
if !(race.HasSession(SessionTypeRace) && index == len(sessionInfo.Durations)-1 && race.Sessions[SessionTypeRace].Laps > 0) {
sessionInfo.Durations[index] = duration * 60
}
}
players, err := cmw.getPlayers(guid)
if err != nil {
return nil, err
}
for entrantNum, entrant := range cmw.entryList.AsSlice() {
if entrantNum < len(players.Cars) {
players.Cars[entrantNum].ID, err = contentManagerIDChecksum(entrant.GUID)
if err != nil {
return nil, err
}
}
}
var passwordChecksum [2]string
if global.Password != "" {
passwordChecksum[0], err = contentManagerPasswordChecksum(global.Name, global.Password)
if err != nil {
return nil, err
}
passwordChecksum[1], err = contentManagerPasswordChecksum(global.Name, global.AdminPassword)
if err != nil {
return nil, err
}
}
geoInfo, err := geoIP()
if err != nil {
logrus.WithError(err).Warn("could not get geo IP data")
geoInfo = &GeoIP{}
}
serverNameSplit := strings.Split(sessionInfo.Name, fmt.Sprintf(" %c", contentManagerWrapperSeparator))
if len(serverNameSplit) > 0 {
sessionInfo.Name = serverNameSplit[0]
}
sessionInfo.IP = geoInfo.IP
sessionInfo.Country = []string{geoInfo.CountryName, geoInfo.CountryCode}
var description string
var championshipID uuid.UUID
if champ, ok := cmw.event.(*ActiveChampionship); ok {
championshipID = champ.ChampionshipID
}
if raceWeekend, ok := cmw.event.(*ActiveRaceWeekend); ok {
championshipID = raceWeekend.ChampionshipID
}
if championshipID != uuid.Nil {
championship, err := cmw.store.LoadChampionship(championshipID.String())
if err == nil {
description = championship.GetPlayerSummary(guid) + "\n\n"
} else {
logrus.WithError(err).Warn("can't load championship info")
}
}
description += cmw.description
// @TODO ContentManagerWrapperContentRequiresPassword from config_ini.go
cmContent, err := cmw.NewCMContent(sessionInfo.Cars, race.Track, false)
if err != nil {
logrus.Errorf("Couldn't attach content download URL(s) through CM Wrapper!")
cmContent = &CMContent{}
}
windSpeed, windDirection := cmw.getWindDetailsFromSessionLogs()
return &ContentManagerWrapperData{
ACHTTPSessionInfo: *sessionInfo,
Players: *players,
Description: description,
AmbientTemperature: live.AmbientTemp,
RoadTemperature: live.RoadTemp,
WindDirection: windDirection,
WindSpeed: windSpeed,
CurrentWeatherID: getSolWeatherPrettyName(live.WeatherGraphics),
Grip: race.DynamicTrack.SessionStart,
GripTransfer: race.DynamicTrack.SessionTransfer,
// rules
Assists: CMAssists{
ABSState: race.ABSAllowed,
AllowedTyresOut: race.AllowedTyresOut,
AutoClutchAllowed: race.AutoClutchAllowed == 1,
DamageMultiplier: race.DamageMultiplier,
ForceVirtualMirror: race.ForceVirtualMirror == 1,
FuelRate: race.FuelRate,
StabilityAllowed: race.StabilityControlAllowed == 1,
TractionControlState: race.TractionControlAllowed,
TyreBlanketsAllowed: race.TyreBlanketsAllowed == 1,
TyreWearRate: race.TyreWearRate,
},
MaxContactsPerKM: race.MaxContactsPerKilometer,
// server info
PasswordChecksum: passwordChecksum,
WrappedPort: global.ContentManagerWrapperPort,
Content: cmContent,
Frequency: global.ClientSendIntervalInHertz,
Until: time.Now().Add(time.Second * time.Duration(sessionInfo.Timeleft)).Unix(),
}, nil
}
var serverLogsWindRegex = regexp.MustCompile(`Wind update\. Speed: (\d+) Direction: (\d+)`)
func (cmw *ContentManagerWrapper) getWindDetailsFromSessionLogs() (speed int, direction int) {
if cmw.logger == nil {
return 0, 0
}
logs := cmw.logger.Logs()
matches := serverLogsWindRegex.FindAllStringSubmatch(logs, -1)
if len(matches) < 1 {
return 0, 0
}
match := matches[len(matches)-1]
if len(match) < 3 {
return 0, 0
}
return formValueAsInt(match[1]), formValueAsInt(match[2])
}
func getSolWeatherPrettyName(weatherName string) string {
if !strings.HasPrefix(weatherName, "sol_") {
return weatherName
}
parts := strings.Split(weatherName, "_type")
if len(parts) == 0 {
return weatherName
}
// remove sol_ prefix and transform to lower case
solName := strings.ToLower(strings.TrimPrefix(parts[0], "sol_"))
// remove underscores, convert to title case
solName = strings.Title(strings.Replace(solName, "_", " ", -1))
return "Sol: " + solName
}
func contentManagerPasswordChecksum(serverName, password string) (string, error) {
h := sha1.New()
_, err := h.Write([]byte("apatosaur" + serverName + password))
if err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
func contentManagerIDChecksum(guid string) (string, error) {
h := sha1.New()
_, err := h.Write([]byte("antarcticfurseal" + guid))
if err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
var geoIPData *GeoIP
const geoIPURL = "https://geoip.cj.workers.dev"
type GeoIP struct {
CountryCode string `json:"country_code"`
CountryName string `json:"country_name"`
IP string `json:"ip"`
}
func geoIP() (*GeoIP, error) {
if geoIPData != nil {
return geoIPData, nil
}
resp, err := http.Get(geoIPURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&geoIPData); err != nil {
return nil, err
}
return geoIPData, nil
}
func getContentManagerJoinLink(config GlobalServerConfig) (*url.URL, error) {
geoIP, err := geoIP()
if err != nil {
return nil, err
}
cmURL, err := url.Parse(ContentManagerJoinLinkBase)
if err != nil {
return nil, err
}
queryString := cmURL.Query()
if config.ContentManagerIPOverride != "" {
queryString.Set("ip", config.ContentManagerIPOverride)
} else {
queryString.Set("ip", geoIP.IP)
}
queryString.Set("httpPort", strconv.Itoa(config.HTTPPort))
cmURL.RawQuery = queryString.Encode()
return cmURL, nil
}