add reconnect to websocket, add get world json object

This commit is contained in:
lbenedar
2026-04-13 17:40:59 +03:00
parent cd204ebcf5
commit 5bb592ea8a
14 changed files with 321 additions and 103 deletions

View File

@@ -4,7 +4,8 @@ import (
"errors"
"fmt"
"net/http"
"strings"
"net/http/cookiejar"
"net/url"
"gitea.local.lab/Lbenedar/foundry_helper_service/internal/foundry/models/db"
)
@@ -21,6 +22,7 @@ const (
PlayersPath = "/players"
SetupPath = "/setup"
UpdatePath = "/update"
GamePath = "/game"
SocketPath = "/socket.io"
)
@@ -49,23 +51,20 @@ var PathToWorldState = map[string]db.StateType{
type FoundryHttpRequest struct {
Host string
Password string
SessionID string
SessionID *string
Jar *cookiejar.Jar
}
func (conf *FoundryHttpRequest) SetSessionTokenFromHeader(resp http.Header) (bool, error) {
setCookieHeader := resp.Get("Set-Cookie")
if setCookieHeader == "" {
return false, nil
func (conf *FoundryHttpRequest) SetSessionTokenFromHeader() (bool, error) {
u, err := url.Parse(fmt.Sprintf("http://%s", conf.Host))
if err != nil {
return false, err
}
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]
cookies := conf.Jar.Cookies(u)
for i := range cookies {
if cookies[i].Name == "session" {
conf.SessionID = &cookies[i].Value
return true, nil
}
}
@@ -73,15 +72,30 @@ func (conf *FoundryHttpRequest) SetSessionTokenFromHeader(resp http.Header) (boo
return false, nil
}
func (conf *FoundryHttpRequest) GetRequestHeader() *http.Header {
func (conf *FoundryHttpRequest) GetRequestHeader(path string) *http.Header {
header := http.Header{}
header.Set("Cookie", fmt.Sprintf("session=%s", conf.SessionID))
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))
header.Set("Referer", fmt.Sprintf("http://%s%s", conf.Host, path))
return &header
}
func (conf *FoundryHttpRequest) GetRequestHeaderJson(path string) *http.Header {
header := http.Header{}
header.Set("Accept", "*/*")
header.Set("Accept-Encoding", "gzip, deflate, br, zstd")
header.Set("Connection", "keep-alive")
header.Set("Content-Type", "application/json")
header.Set("Cookie", fmt.Sprintf("session=%s", *conf.SessionID))
header.Set("Host", conf.Host)
header.Set("Origin", fmt.Sprintf("http://%s", conf.Host))
header.Set("Priority", "u=0")
header.Set("Referer", fmt.Sprintf("http://%s%s", conf.Host, path))
return &header
}