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
}

View File

@@ -11,41 +11,57 @@ import (
)
func (conf *FoundryHttpRequest) GetSessionId() error {
getResp, err := http.Get(fmt.Sprintf("http://%s%s", conf.Host, AuthPath))
path := fmt.Sprintf("http://%s%s", conf.Host, AuthPath)
req, err := http.NewRequest("GET", path, nil)
if err != nil {
return err
}
_, err = conf.SetSessionTokenFromHeader(getResp.Header)
client := &http.Client{Jar: conf.Jar}
_, err = client.Do(req)
if err != nil {
return err
}
fmt.Printf("%v\n", conf.Jar)
_, err = conf.SetSessionTokenFromHeader()
return err
}
func (conf *FoundryHttpRequest) GetSessionToken() (bool, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s%s", conf.Host, AuthPath), nil)
if err != nil {
return false, err
}
// func (conf *FoundryHttpRequest) GetSessionToken() (bool, error) {
// req, err := http.NewRequest("GET", fmt.Sprintf("http://%s%s", conf.Host, AuthPath), nil)
// if err != nil {
// return false, err
// }
header := http.Header{}
header.Set("Cookie", fmt.Sprintf("session=%s", conf.SessionID))
header.Set("Connection", "keep-alive")
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 := http.Header{}
// header.Set("Cookie", fmt.Sprintf("session=%s", conf.SessionID))
// header.Set("Connection", "keep-alive")
// 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))
req.Header = header
client := &http.Client{}
// req.Header = header
// client := &http.Client{Jar: conf.Jar}
resp, err := client.Do(req)
if err != nil {
return false, err
}
// resp, err := client.Do(req)
// if err != nil {
// return false, err
// }
return conf.SetSessionTokenFromHeader(resp.Header)
}
// return conf.SetSessionTokenFromHeader(resp.Header)
// }
func (conf *FoundryHttpRequest) GetStatus() (*json_model.Status, error) {
getResp, err := http.Get(fmt.Sprintf("http://%s/api/status", conf.Host))
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/api/status", conf.Host), nil)
if err != nil {
return nil, err
}
client := &http.Client{Jar: conf.Jar}
getResp, err := client.Do(req)
if err != nil {
return nil, err
}
@@ -64,3 +80,22 @@ func (conf *FoundryHttpRequest) GetStatus() (*json_model.Status, error) {
}
return &status, nil
}
func (conf *FoundryHttpRequest) GetGame() (bool, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s%s", conf.Host, GamePath), nil)
if err != nil {
return false, err
}
header := conf.GetRequestHeader(GamePath)
req.Header = *header
client := &http.Client{Jar: conf.Jar}
_, err = client.Do(req)
if err != nil {
return false, err
}
return true, nil
}

View File

@@ -2,6 +2,7 @@ package requests
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
@@ -17,12 +18,48 @@ func (conf *FoundryHttpRequest) PostAuthenticationData() (*http.Response, error)
return nil, err
}
header := conf.GetRequestHeader()
header := conf.GetRequestHeader(AuthPath)
header.Set("Content-Length", fmt.Sprintf("%d", len(authData.Encode())))
req.Header = header.Clone()
client := &http.Client{}
client := &http.Client{Jar: conf.Jar}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}
func (conf *FoundryHttpRequest) PostJoinWorld(userId, password string) (*http.Response, error) {
data := struct {
UserId string `json:"userid"`
Password string `json:"password"`
Action string `json:"action"`
Session string `json:"session"`
}{
UserId: userId,
Password: password,
Action: "join",
Session: *conf.SessionID,
}
byteData, err := json.Marshal(data)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", fmt.Sprintf("http://%s%s", conf.Host, JoinPath), bytes.NewBuffer(byteData))
if err != nil {
return nil, err
}
header := conf.GetRequestHeaderJson(JoinPath)
header.Set("Content-Length", fmt.Sprintf("%d", len(byteData)))
req.Header = header.Clone()
client := &http.Client{Jar: conf.Jar}
resp, err := client.Do(req)
if err != nil {
return nil, err
@@ -40,12 +77,12 @@ func (conf *FoundryHttpRequest) PostLaunchWorld(world string) (*http.Response, e
return nil, err
}
header := conf.GetRequestHeader()
header := conf.GetRequestHeader(SetupPath)
header.Set("Content-Length", fmt.Sprintf("%d", len(launchData.Encode())))
req.Header = header.Clone()
client := &http.Client{}
client := &http.Client{Jar: conf.Jar}
resp, err := client.Do(req)
if err != nil {
return nil, err
@@ -62,12 +99,12 @@ func (conf *FoundryHttpRequest) PostReturnToSetup() (*http.Response, error) {
return nil, err
}
header := conf.GetRequestHeader()
header := conf.GetRequestHeader(JoinPath)
header.Set("Content-Length", fmt.Sprintf("%d", len(launchData.Encode())))
req.Header = header.Clone()
client := &http.Client{}
client := &http.Client{Jar: conf.Jar}
resp, err := client.Do(req)
if err != nil {
return nil, err