Files
Foundry-Scrapping-API/internal/foundry/requests/config.go

88 lines
2.1 KiB
Go

package requests
import (
"errors"
"fmt"
"net/http"
"strings"
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/models/db"
)
var (
ErrorSidWrongFormat = errors.New("Session id wrong format")
ErrorSidNotFound = errors.New("Session id didn't find in response header")
)
const (
AuthPath = "/auth"
LicensePath = "/license"
JoinPath = "/join"
PlayersPath = "/players"
SetupPath = "/setup"
UpdatePath = "/update"
SocketPath = "/socket.io"
)
var WsTypeData = map[string]string{
AuthPath: "getAuthData",
LicensePath: "getAuthData",
JoinPath: "getJoinData",
PlayersPath: "getPlayersData",
SetupPath: "getSetupData",
UpdatePath: "getUpdateData",
}
var PathToSetupState = map[string]db.StateType{
SetupPath: db.SetupState,
// AuthPath: db.AuthState,
// UpdatePath: db.UpdateState,
// LicensePath: db.LicenseState,
}
var PathToWorldState = map[string]db.StateType{
JoinPath: db.JoinState,
// PlayersPath: db.PlayersState,
}
type FoundryHttpRequest struct {
Host string
Password string
SessionID string
}
func (conf *FoundryHttpRequest) SetSessionTokenFromHeader(resp http.Header) (bool, error) {
setCookieHeader := resp.Get("Set-Cookie")
if setCookieHeader == "" {
return false, nil
}
for value := range strings.SplitSeq(setCookieHeader, ";") {
if strings.Contains(value, "session") {
sessionCookie := strings.Split(value, "=")
if len(sessionCookie) != 2 {
return false, ErrorSidWrongFormat
}
conf.SessionID = sessionCookie[1]
return true, nil
}
}
return false, nil
}
func (conf *FoundryHttpRequest) GetRequestHeader() *http.Header {
header := http.Header{}
header.Set("Cookie", fmt.Sprintf("session=%s", conf.SessionID))
header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
header.Set("Connection", "keep-alive")
header.Set("Content-Type", "application/x-www-form-urlencoded")
header.Set("Host", conf.Host)
header.Set("Origin", fmt.Sprintf("http://%s", conf.Host))
header.Set("Referer", fmt.Sprintf("http://%s%s", conf.Host, AuthPath))
return &header
}