-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
376 lines (345 loc) · 9.06 KB
/
main.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package main
import (
"auth-server/Auth"
"auth-server/GMMAuth"
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/pion/mdns"
"golang.org/x/net/ipv4"
"io"
"log"
"net"
"net/http"
"os"
"regexp"
"strings"
"time"
)
var (
accountsData = map[string][]string{}
authTemp = map[string]Auth.Auth{}
acccountRegex = regexp.MustCompile("(\\w+) (\\S+):(\\S+)")
)
func reloadAccount() {
file, err := os.ReadFile("accounts.txt")
if err != nil {
panic("accounts.txt not found")
}
accounts := strings.Split(strings.ReplaceAll(string(file), "\r\n", "\n"), "\n")
for _, account := range accounts {
match := acccountRegex.FindStringSubmatch(account)
if match != nil {
accountsData[match[1]] = []string{match[2], match[3]}
}
}
}
type ReqBody struct {
User string `json:"user"`
ShareSecret string `json:"shareSecret"`
ServerID string `json:"serverID"`
PublicKey string `json:"publicKey"`
VerifyToken string `json:"verifyToken"`
}
type Error struct {
Err string `json:"error"`
ErrMsg string `json:"errorMessage"`
Cause string `json:"cause"`
}
func (e Error) Error() string {
return e.Err + ": " + e.ErrMsg + ", " + e.Cause
}
// agent is a struct of auth
type agent struct {
Name string `json:"name"`
Version int `json:"version"`
}
type proof struct {
UserName string `json:"username"`
Password string `json:"password"`
}
// Tokens store AccessToken and ClientToken
type Tokens struct {
AccessToken string `json:"accessToken"`
ClientToken string `json:"clientToken"`
}
var defaultAgent = agent{
Name: "Minecraft",
Version: 1,
}
// authPayload is a yggdrasil request struct
type authPayload struct {
Agent agent `json:"agent"`
proof
ClientToken string `json:"clientToken,omitempty"`
RequestUser bool `json:"requestUser"`
}
// authResp is the response from Mojang's auth server
type authResp struct {
Tokens
AvailableProfiles []Profile `json:"availableProfiles"` // only present if the agent field was received
SelectedProfile Profile `json:"selectedProfile"` // only present if the agent field was received
User struct {
// only present if requestUser was true in the request authPayload
ID string `json:"id"` // hexadecimal
Properties []struct {
Name string `json:"name"`
Value string `json:"value"`
}
} `json:"user"`
*Error
}
type Profile struct {
ID string `json:"id"`
Name string `json:"name"`
// Legacy bool `json:"legacy"` // we don't care
}
type refreshPayload struct {
Tokens
SelectedProfile *Profile `json:"selectedProfile,omitempty"`
RequestUser bool `json:"requestUser"`
}
type profile struct {
ID string `json:"id"`
Name string `json:"name"`
}
type request struct {
AccessToken string `json:"accessToken"`
SelectedProfile string `json:"selectedProfile"`
ServerID string `json:"serverId"`
}
func main() {
reloadAccount()
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
as := r.Group("/as")
as.POST("/authenticate", func(c *gin.Context) {
log.Printf("[session auth]\n")
reloadAccount()
var payload authPayload
c.BindJSON(&payload)
ac, ok := accountsData[payload.UserName]
if !ok {
c.AbortWithStatusJSON(403, gin.H{
"error": "ForbiddenOperationException",
"errorMessage": "Invalid credentials. Invalid username or password.",
})
return
}
if auth, ok := authTemp[payload.UserName]; ok {
profile := Profile{
ID: auth.UUID,
Name: auth.Name,
}
c.JSON(200, authResp{
Tokens: Tokens{
AccessToken: payload.UserName,
ClientToken: "",
},
AvailableProfiles: []Profile{profile},
SelectedProfile: profile,
})
}
auth, err := GMMAuth.GetMCcredentialsByPassword(ac[0], ac[1])
if err != nil {
c.AbortWithStatusJSON(400, gin.H{
"error": "ForbiddenOperationException",
"errorMessage": "Forbidden",
})
return
}
authTemp[payload.UserName] = auth
profile := Profile{
ID: auth.UUID,
Name: auth.Name,
}
c.JSON(200, authResp{
Tokens: Tokens{
AccessToken: payload.UserName,
ClientToken: "",
},
AvailableProfiles: []Profile{profile},
SelectedProfile: profile,
})
})
as.POST("/refresh", func(c *gin.Context) {
var re refreshPayload
c.BindJSON(&re)
c.JSON(200, authResp{
Tokens: Tokens{
AccessToken: re.AccessToken,
ClientToken: re.AccessToken,
},
AvailableProfiles: []Profile{
*re.SelectedProfile,
},
SelectedProfile: *re.SelectedProfile,
})
})
as.POST("/validate", func(c *gin.Context) {
c.Status(204)
})
as.POST("/signout", func(c *gin.Context) {
c.Status(200)
})
as.POST("/invalidate", func(c *gin.Context) {
c.Status(200)
})
ss := r.Group("/ss")
ss.POST("/session/minecraft/join", func(c *gin.Context) {
var req request
err := c.BindJSON(&req)
if err != nil {
fmt.Println(err)
}
ac, ok := accountsData[req.AccessToken]
if !ok {
c.AbortWithStatusJSON(403, gin.H{
"error": "ForbiddenOperationException",
"errorMessage": "Invalid credentials. Invalid username or password.",
})
return
}
log.Printf("[session join]: %v\n", req.AccessToken)
if _, ok := authTemp[req.AccessToken]; ok {
err := LoginRemote(request{
AccessToken: authTemp[req.AccessToken].AsTk,
SelectedProfile: strings.ReplaceAll(authTemp[req.AccessToken].UUID, "-", ""),
ServerID: req.ServerID,
})
if err == nil {
c.Status(204)
log.Printf("[login] Code [%v]: %v\n", req.AccessToken, "Successful(cache)")
return
}
}
auth, err := GMMAuth.GetMCcredentialsByPassword(ac[0], ac[1])
if err != nil {
c.AbortWithStatusJSON(400, gin.H{
"error": "ForbiddenOperationException",
"errorMessage": "Forbidden",
})
return
}
authTemp[req.AccessToken] = auth
err = LoginRemote(request{
AccessToken: authTemp[req.AccessToken].AsTk,
SelectedProfile: strings.ReplaceAll(authTemp[req.AccessToken].UUID, "-", ""),
ServerID: req.ServerID,
})
if err != nil {
c.AbortWithStatus(403)
return
}
c.Status(204)
})
r.POST("/getUser", func(c *gin.Context) {
reloadAccount()
buf := new(bytes.Buffer)
buf.ReadFrom(c.Request.Body)
user := buf.String()
ac := accountsData[user]
auth, err := GMMAuth.GetMCcredentialsByPassword(ac[0], ac[1])
if err != nil {
fmt.Println(err)
c.AbortWithStatus(400)
return
}
authTemp[user] = auth
c.Data(200, "text/plain", []byte(auth.Name))
log.Printf("[getUser] Code [%v]: %v\n", user, auth.Name)
})
r.POST("/login", func(c *gin.Context) {
reloadAccount()
result, _ := io.ReadAll(c.Request.Body)
var req ReqBody
err := json.Unmarshal(result, &req)
if err != nil {
log.Printf("[login] Request with bad payload\n")
c.Status(403)
return
}
log.Printf("[login] Request Code [%v] login\n", req.User)
if _, ok := accountsData[req.User]; !ok {
log.Printf("[login] Request Code [%v] not found\n", req.User)
c.Status(403)
return
}
if _, ok := authTemp[req.User]; ok {
err = Auth.LoginAuth(authTemp[req.User], req.ShareSecret, req.ServerID, req.PublicKey, req.VerifyToken)
c.Status(200)
log.Printf("[login] Code [%v]: %v\n", req.User, "Successful(cache)")
delete(authTemp, req.User)
return
}
ac := accountsData[req.User]
auth, err := GMMAuth.GetMCcredentialsByPassword(ac[0], ac[1])
if err != nil {
go failedLogin(ac[0], ac[1])
c.Status(400)
return
}
authTemp[req.User] = auth
err = Auth.LoginAuth(auth, req.ShareSecret, req.ServerID, req.PublicKey, req.VerifyToken)
if err != nil {
c.Status(400)
return
}
c.Status(200)
log.Printf("[login] Code [%v]: %v\n", req.User, "Successful(refresh)")
})
r.Run("127.0.0.1:37565")
}
func LoginRemote(req request) error {
client := http.Client{}
requestPacket, err := json.Marshal(
req,
)
if err != nil {
return fmt.Errorf("create request packet to yggdrasil faile: %v", err)
}
PostRequest, err := http.NewRequest(http.MethodPost, "https://sessionserver.mojang.com/session/minecraft/join",
bytes.NewReader(requestPacket))
if err != nil {
return fmt.Errorf("make request error: %v", err)
}
PostRequest.Header.Set("User-agent", "go-mc")
PostRequest.Header.Set("Connection", "keep-alive")
PostRequest.Header.Set("Content-Type", "application/json")
resp, err := client.Do(PostRequest)
if err != nil {
return fmt.Errorf("post fail: %v", err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("auth fail: %s", string(body))
}
return nil
}
func failedLogin(username, password string) {
addr, err := net.ResolveUDPAddr("udp", mdns.DefaultAddress)
if err != nil {
panic(err)
}
l, err := net.ListenUDP("udp4", addr)
if err != nil {
panic(err)
}
server, err := mdns.Server(ipv4.NewPacketConn(l), &mdns.Config{})
if err != nil {
panic(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
answer, src, err := server.Query(ctx, "auth-server.local")
_ = answer
_ = src
if src == nil {
return
}
fmt.Println("[remote] Got Answer: " + src.String())
http.Post("http://"+src.String()+":37585/failedLogin", "text/plain", strings.NewReader(username+"|"+password))
}