Add gamestats package

This commit is contained in:
mkwcat 2024-02-05 20:03:30 -05:00
parent 4f9916e6b3
commit c9bd3ec2a8
No known key found for this signature in database
GPG Key ID: 7A505679CE9E7AA9
6 changed files with 4839 additions and 4730 deletions

View File

@ -9,10 +9,12 @@ import (
)
type GameInfo struct {
GameID int
Name string
SecretKey string
Description string
GameID int
Name string
SecretKey string
GameStatsVersion int
GameStatsKey string
Description string
}
var (
@ -71,11 +73,22 @@ func ReadGameList() {
}
}
gameStatsVer := -1
if entry[4] != "" {
gameStatsVer, err = strconv.Atoi(entry[4])
if err != nil {
panic(err)
}
}
gameList = append(gameList, GameInfo{
GameID: gameId,
Name: entry[1],
SecretKey: entry[3],
Description: entry[0],
GameID: gameId,
Name: entry[1],
SecretKey: entry[3],
GameStatsVersion: gameStatsVer,
GameStatsKey: entry[5],
Description: entry[0],
})
// Create lookup tables

4
common/misc.go Normal file
View File

@ -0,0 +1,4 @@
package common
func UNUSED(v ...interface{}) {
}

File diff suppressed because it is too large Load Diff

95
gamestats/http.go Normal file
View File

@ -0,0 +1,95 @@
package gamestats
import (
"crypto/sha1"
"encoding/base64"
"encoding/hex"
"net/http"
"net/url"
"strconv"
"strings"
"wwfc/common"
"wwfc/logging"
"github.com/logrusorgru/aurora/v3"
)
func HandleHTTPRequest(w http.ResponseWriter, r *http.Request) {
logging.Info("GSTATS", aurora.Yellow(r.Method), aurora.Cyan(r.URL), "via", aurora.Cyan(r.Host), "from", aurora.BrightCyan(r.RemoteAddr))
u, err := url.Parse(r.URL.String())
if err != nil {
replyHTTPError(w, http.StatusBadRequest, "400 Bad Request")
return
}
query, err := url.ParseQuery(u.RawQuery)
if err != nil {
replyHTTPError(w, http.StatusBadRequest, "400 Bad Request")
return
}
path := u.Path
if strings.HasPrefix(path, "/") {
path = path[1:]
}
gameName := path
subPath := ""
slashIndex := strings.Index(gameName, "/")
if slashIndex != -1 {
gameName = gameName[:slashIndex]
subPath = path[slashIndex:]
}
game := common.GetGameInfoByName(gameName)
if game == nil {
replyHTTPError(w, http.StatusNotFound, "404 Not Found")
return
}
hash := query.Get("hash")
var data string
if hash == "" {
// No hash, just return token
data = common.RandomString(32)
} else {
// TODO: Handle subPath to get data here
common.UNUSED(subPath)
data = ""
if game.GameStatsVersion > 1 {
// SHA-1 hash GamestatsKey + base64(data) + GameStatsKey
hashData := game.GameStatsKey + base64.URLEncoding.EncodeToString([]byte(data)) + game.GameStatsKey
hasher := sha1.New()
hasher.Write([]byte(hashData))
// Append the hash sum as a hex string
data += hex.EncodeToString(hasher.Sum(nil))
}
}
w.Header().Set("Content-type", "text/html")
w.Header().Set("Server", "Microsoft-IIS/6.0")
w.Header().Add("Server", "GSTPRDSTATSWEB2")
w.Header().Set("X-Powered-By", "ASP.NET")
w.Header().Set("Content-Length", strconv.Itoa(len(data)))
w.WriteHeader(200)
w.Write([]byte(data))
}
func replyHTTPError(w http.ResponseWriter, errorCode int, errorString string) {
response := "<html>\n" +
"<head><title>" + errorString + "</title></head>\n" +
"<body>\n" +
"<center><h1>" + errorString + "</h1></center>\n" +
"<hr><center>" + serverName + "</center>\n" +
"</body>\n" +
"</html>\n"
w.Header().Set("Content-Type", "text/html")
w.Header().Set("Content-Length", strconv.Itoa(len(response)))
w.Header().Set("Connection", "close")
w.WriteHeader(errorCode)
w.Write([]byte(response))
}

View File

@ -3,23 +3,24 @@ package gamestats
import (
"context"
"fmt"
"net/http"
"wwfc/common"
"wwfc/logging"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/logrusorgru/aurora/v3"
)
var (
ctx = context.Background()
pool *pgxpool.Pool
serverName string
)
func StartServer() {
// Get config
config := common.GetConfig()
serverName = config.ServerName
common.ReadGameList()
// Start SQL
@ -34,7 +35,3 @@ func StartServer() {
panic(err)
}
}
func HandleRequest(w http.ResponseWriter, r *http.Request) {
logging.Info("GSTATS", aurora.Yellow(r.Method), aurora.Cyan(r.URL), "via", aurora.Cyan(r.Host), "from", aurora.BrightCyan(r.RemoteAddr))
}

View File

@ -68,7 +68,7 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
// Check for *.gamestats(2).gs.* or gamestats(2).gs.*
if regexGamestatsHost.MatchString(r.Host) {
// Redirect to the gamestats server
gamestats.HandleRequest(w, r)
gamestats.HandleHTTPRequest(w, r)
return
}