-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpsynet.go
More file actions
181 lines (151 loc) · 4.85 KB
/
Copy pathpsynet.go
File metadata and controls
181 lines (151 loc) · 4.85 KB
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
package rlapi
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"strconv"
"strings"
"time"
"github.com/gorilla/websocket"
)
const (
baseURL = "https://api.rlpp.psynet.gg/rpc"
gameVersion = "260825.79374.526531"
featureSet = "PrimeUpdate59_1"
psySigKey = "c338bd36fb8c42b1a431d30add939fc7"
pingInterval = 20 * time.Second
pongTimeout = 10 * time.Second
)
type psyNetError struct {
Type string `json:"Type"`
Message string `json:"Message"`
}
func (e psyNetError) Error() string {
return fmt.Sprintf("%s: %s", e.Type, e.Message)
}
// PsyNet represents the HTTP API client, see PsyNetRPC for the WebSocket client.
type PsyNet struct {
client *http.Client
requestID *requestIDCounter
logger *slog.Logger
gameVersion string
featureSet string
buildID string
}
type PsyRequest struct {
Service string `json:"PsyService"`
Sig string `json:"PsySig"`
RequestID string `json:"PsyRequestID"`
Data interface{} `json:"-"`
}
type PsyResponse struct {
ResponseID string `json:"PsyResponseID"`
Result json.RawMessage `json:"Result"`
Error *psyNetError `json:"Error"`
}
func generatePsySig(body []byte) string {
h := hmac.New(sha256.New, []byte(psySigKey))
h.Write([]byte("-"))
h.Write(body)
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
func NewPsyNet() *PsyNet {
return &PsyNet{
client: &http.Client{},
requestID: &requestIDCounter{},
logger: slog.Default(),
gameVersion: gameVersion,
featureSet: featureSet,
buildID: strconv.Itoa(int(decodeBuildID(gameVersion))),
}
}
// Deprecated: Use NewPsyNet and SetLogger instead.
func NewPsyNetWithLogger(logger *slog.Logger) *PsyNet {
return &PsyNet{
client: &http.Client{},
requestID: &requestIDCounter{},
logger: logger,
gameVersion: gameVersion,
featureSet: featureSet,
buildID: strconv.Itoa(int(decodeBuildID(gameVersion))),
}
}
func (p *PsyNet) SetLogger(logger *slog.Logger) {
p.logger = logger
}
// SetVersion overrides the default game version and feature set.
func (p *PsyNet) SetVersion(gameVersion, featureSet string) {
p.gameVersion = gameVersion
p.featureSet = featureSet
p.buildID = strconv.Itoa(int(decodeBuildID(gameVersion)))
}
// GetVersion returns the current game version and feature set.
func (p *PsyNet) GetVersion() (gameVersion, featureSet string) {
return p.gameVersion, p.featureSet
}
func (p *PsyNet) establishSocket(url string, playerID PlayerID, psyToken string, sessionID string) (*PsyNetRPC, error) {
p.logger.Debug("establishing websocket connection", slog.String("url", url))
dialer := websocket.Dialer{}
conn, _, err := dialer.Dial(url, http.Header{
"PsyBuildID": []string{p.buildID},
"User-Agent": []string{fmt.Sprintf("RL Win/%s gzip", p.gameVersion)},
"PsyEnvironment": []string{"Prod"},
"PsyToken": []string{psyToken},
"PsySessionID": []string{sessionID},
})
if err != nil {
return nil, fmt.Errorf("failed to dial websocket: %w", err)
}
return newPsyNetRPC(conn, playerID, p.requestID, p.logger), nil
}
func (p *PsyNet) postJSON(path []string, params interface{}, result interface{}) error {
url := fmt.Sprintf("%s/%s", baseURL, strings.Join(path, "/"))
body, err := json.Marshal(params)
if err != nil {
return fmt.Errorf("failed to marshal params: %w", err)
}
p.logger.Debug("sending http request", slog.String("url", url), slog.String("body", string(body)))
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(body))
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", fmt.Sprintf("RL Win/%s gzip (x86_64-pc-win32) curl-7.67.0 Schannel", p.gameVersion))
req.Header.Set("PsyBuildID", p.buildID)
req.Header.Set("PsyEnvironment", "Prod")
req.Header.Set("PsyRequestID", p.requestID.getID())
req.Header.Set("PsySig", generatePsySig(body))
resp, err := p.client.Do(req)
if err != nil {
return fmt.Errorf("failed to send request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status: %s", resp.Status)
}
respBytes, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response body: %w", err)
}
p.logger.Debug("received http response", slog.String("status", resp.Status), slog.String("body", string(respBytes)))
var wrapper struct {
Result json.RawMessage `json:"Result"`
Error *psyNetError `json:"Error"`
}
if err := json.Unmarshal(respBytes, &wrapper); err != nil {
return fmt.Errorf("failed to unmarshal wrapper: %w", err)
}
if wrapper.Error != nil {
return wrapper.Error
}
if err := json.Unmarshal(wrapper.Result, result); err != nil {
return fmt.Errorf("failed to unmarshal result: %w", err)
}
return nil
}