-
Notifications
You must be signed in to change notification settings - Fork 18
/
session.go
282 lines (242 loc) · 7.1 KB
/
session.go
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package main
import (
"errors"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"gorm.io/gorm"
"log"
"net/http"
"net/url"
"strings"
)
type sessionJoinRequest struct {
AccessToken string `json:"accessToken"`
SelectedProfile string `json:"selectedProfile"`
ServerID string `json:"serverId"`
}
// /session/minecraft/join
// https://wiki.vg/Protocol_Encryption#Client
func SessionJoin(app *App) func(c echo.Context) error {
return func(c echo.Context) error {
req := new(sessionJoinRequest)
if err := c.Bind(req); err != nil {
return err
}
client := app.GetClient(req.AccessToken, StalePolicyDeny)
if client == nil {
return c.JSONBlob(http.StatusForbidden, invalidAccessTokenBlob)
}
user := client.User
user.ServerID = MakeNullString(&req.ServerID)
result := app.DB.Save(&user)
if result.Error != nil {
return result.Error
}
return c.NoContent(http.StatusNoContent)
}
}
// /game/joinserver.jsp
func SessionJoinServer(app *App) func(c echo.Context) error {
return func(c echo.Context) error {
username := c.QueryParam("user")
sessionID := c.QueryParam("sessionId")
serverID := c.QueryParam("serverId")
// If any parameters are missing, return NO
if username == "" || sessionID == "" || serverID == "" {
return c.String(http.StatusOK, "Bad login")
}
// Parse sessionId. It has the form:
// token:<accessToken>:<player UUID>
split := strings.Split(sessionID, ":")
if len(split) != 3 || split[0] != "token" {
return c.String(http.StatusOK, "Bad login")
}
accessToken := split[1]
id := split[2]
// Is the accessToken valid?
client := app.GetClient(accessToken, StalePolicyDeny)
if client == nil {
return c.String(http.StatusOK, "Bad login")
}
// If the player name corresponding to the access token doesn't match
// the `user` param from the request, return NO
user := client.User
if user.PlayerName != username {
return c.String(http.StatusOK, "Bad login")
}
// If the player's UUID doesn't match the UUID in the sessionId, return
// NO
userID, err := UUIDToID(user.UUID)
if err != nil {
return err
}
if userID != id {
return c.String(http.StatusOK, "Bad login")
}
user.ServerID = MakeNullString(&serverID)
result := app.DB.Save(&user)
if result.Error != nil {
return result.Error
}
return c.String(http.StatusOK, "OK")
}
}
func fullProfile(app *App, user *User, uuid string, sign bool) (SessionProfileResponse, error) {
id, err := UUIDToID(uuid)
if err != nil {
return SessionProfileResponse{}, err
}
texturesProperty, err := app.GetSkinTexturesProperty(user, sign)
if err != nil {
return SessionProfileResponse{}, err
}
return SessionProfileResponse{
ID: id,
Name: user.PlayerName,
Properties: []SessionProfileProperty{texturesProperty},
}, nil
}
func (app *App) hasJoined(c *echo.Context, playerName string, serverID string, legacy bool) error {
var user User
result := app.DB.First(&user, "player_name = ?", playerName)
// If the error isn't "not found", throw.
if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) {
return result.Error
}
if result.Error != nil || !user.ServerID.Valid || serverID != user.ServerID.String {
for _, fallbackAPIServer := range app.Config.FallbackAPIServers {
if fallbackAPIServer.DenyUnknownUsers && result.Error != nil {
// If DenyUnknownUsers is enabled and the player name is
// not known, don't query the fallback server.
continue
}
base, err := url.Parse(fallbackAPIServer.SessionURL)
if err != nil {
log.Println(err)
continue
}
base.Path += "/session/minecraft/hasJoined"
params := url.Values{}
params.Add("username", playerName)
params.Add("serverId", serverID)
base.RawQuery = params.Encode()
res, err := MakeHTTPClient().Get(base.String())
if err != nil {
log.Printf("Received invalid response from fallback API server at %s\n", base.String())
continue
}
defer res.Body.Close()
if res.StatusCode == http.StatusOK {
if legacy {
return (*c).String(http.StatusOK, "YES")
} else {
return (*c).Stream(http.StatusOK, res.Header.Get("Content-Type"), res.Body)
}
}
}
if legacy {
return (*c).String(http.StatusMethodNotAllowed, "NO")
} else {
return (*c).NoContent(http.StatusForbidden)
}
}
if legacy {
return (*c).String(http.StatusOK, "YES")
}
profile, err := fullProfile(app, &user, user.UUID, true)
if err != nil {
return err
}
return (*c).JSON(http.StatusOK, profile)
}
// /session/minecraft/hasJoined
// https://c4k3.github.io/wiki.vg/Protocol_Encryption.html#Server
func SessionHasJoined(app *App) func(c echo.Context) error {
return func(c echo.Context) error {
playerName := c.QueryParam("username")
serverID := c.QueryParam("serverId")
return app.hasJoined(&c, playerName, serverID, false)
}
}
// /game/checkserver.jsp
func SessionCheckServer(app *App) func(c echo.Context) error {
return func(c echo.Context) error {
playerName := c.QueryParam("user")
serverID := c.QueryParam("serverId")
return app.hasJoined(&c, playerName, serverID, true)
}
}
// /session/minecraft/profile/:id
// https://wiki.vg/Mojang_API#UUID_to_Profile_and_Skin.2FCape
func SessionProfile(app *App) func(c echo.Context) error {
return func(c echo.Context) error {
id := c.Param("id")
var uuid_ string
uuid_, err := IDToUUID(id)
if err != nil {
_, err = uuid.Parse(id)
if err != nil {
return c.JSON(http.StatusBadRequest, ErrorResponse{
ErrorMessage: Ptr("Not a valid UUID: " + c.Param("id")),
})
}
uuid_ = id
}
findUser := func() (*User, error) {
var user User
result := app.DB.First(&user, "uuid = ?", uuid_)
if result.Error == nil {
return &user, nil
}
if !errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, err
}
// Could be an offline UUID
if app.Config.OfflineSkins {
result = app.DB.First(&user, "offline_uuid = ?", uuid_)
if result.Error == nil {
return &user, nil
}
if !errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, err
}
}
return nil, nil
}
user, err := findUser()
if err != nil {
return err
}
if user == nil {
for _, fallbackAPIServer := range app.Config.FallbackAPIServers {
reqURL, err := url.JoinPath(fallbackAPIServer.SessionURL, "session/minecraft/profile", id)
if err != nil {
log.Println(err)
continue
}
res, err := app.CachedGet(reqURL+"?unsigned=false", fallbackAPIServer.CacheTTLSeconds)
if err != nil {
log.Printf("Couldn't access fallback API server at %s: %s\n", reqURL, err)
continue
}
if res.StatusCode == http.StatusOK {
return c.Blob(http.StatusOK, "application/json", res.BodyBytes)
}
}
return c.NoContent(http.StatusNoContent)
}
sign := c.QueryParam("unsigned") == "false"
profile, err := fullProfile(app, user, uuid_, sign)
if err != nil {
return err
}
return c.JSON(http.StatusOK, profile)
}
}
// /blockedservers
// https://wiki.vg/Mojang_API#Blocked_Servers
func SessionBlockedServers(app *App) func(c echo.Context) error {
return func(c echo.Context) error {
return c.NoContent(http.StatusOK)
}
}