This repository was archived by the owner on Dec 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.py
More file actions
executable file
·203 lines (164 loc) · 7.93 KB
/
Copy pathproxy.py
File metadata and controls
executable file
·203 lines (164 loc) · 7.93 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
# Set this to True if behind an ip masking proxy (with X-Forwarded-For support) while using prevent-proxy-connections
behind_proxy = True
# Address and port to listen to
bind_to = ('127.0.0.1', 8080)
# Should mojang accounts be allowed to join with this AltAuth proxy
allow_mojang_accounts = True
# Should banned microsoft accounts ("UserBannedException") be allowed to join?
# Should accounts with disabled multiplayer ("InsufficientPrivilegesException") be allowed to join?
allowed_microsoft_accounts = ()
# allowed_microsoft_accounts = ("InsufficientPrivilegesException", "UserBannedException")
from traceback import format_exc
from collections import namedtuple
from typing import Dict
from json import dumps, loads
from base64 import b64decode
from time import time, sleep
from threading import Thread
from urllib.error import HTTPError
from urllib.request import Request, urlopen
from http import HTTPStatus
from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler
def moj_request(url, data=None):
try:
response = urlopen(Request(
url,
headers={
"Content-Type": "application/json"
} if data else {},
data=dumps(data).encode("utf8") if data else None
))
return response.code, response.read()
except HTTPError as response:
return response.code, response.read()
CachedProfile = namedtuple('CachedProfile', ('timestamp', 'use_altauth', 'ip', 'uuid'))
cached_profiles: Dict[str, CachedProfile] = {}
def timeout_cleaner():
global cached_profiles
while True:
timeout = time() - 60
cached_profiles = {
serverId: profile
for serverId, profile in cached_profiles.items()
if profile.timestamp > timeout
}
sleep(1)
class SilentException(Exception):
pass
class AltAuthRequestHandler(BaseHTTPRequestHandler):
def do_POST(self):
try:
self.close_connection = True
if self.path != "/session/minecraft/join":
self.send_response(HTTPStatus.NOT_FOUND)
self.end_headers()
return
content_length = int(self.headers['Content-Length'])
if content_length > 1024:
raise Exception("Unusual large request (malicious actor?)")
request = loads(self.rfile.read(content_length))
access_token = request["accessToken"]
selected_profile = request["selectedProfile"]
server_id = request["serverId"]
token = loads(b64decode(access_token.split(".")[1] + "==")) # Decode the JSON Web Token
now = time()
use_altauth = False
if token["exp"] <= now: # check token expiration date
raise SilentException("Expired token")
if token["iss"] == "Yggdrasil-Auth" and allow_mojang_accounts: # Mojang account
if token["spr"] != selected_profile:
raise Exception("UUIDs don't match (malicious actor?)")
# Valid token (even on other ip): 204
# Invalid token: 403 {"error": "ForbiddenOperationException", "errorMessage": "Invalid token"}
code, data = moj_request("https://authserver.mojang.com/validate", data={
"accessToken": access_token
})
if code != 204:
raise Exception("Token invalid for unknown reasons")
use_altauth = True
else: # Microsoft account
# Valid token: 204
# According to wiki.vg Xbox multiplayer disabled: InsufficientPrivilegesException
# According to wiki.vg Multiplayer banned: UserBannedException
# Mojang account: 403 {"error":"ForbiddenOperationException","path":"/session/minecraft/join"}
# Invalid token: 403 {"error":"ForbiddenOperationException","path":"/session/minecraft/join"}
code, data = moj_request("https://sessionserver.mojang.com/session/minecraft/join", data={
"accessToken": access_token,
"selectedProfile": selected_profile,
"serverId": server_id
})
if code == 403:
if loads(data)["error"] in allowed_microsoft_accounts:
use_altauth = True
code = 204
if code == 204:
cached_profiles[server_id] = CachedProfile(
timestamp=now,
use_altauth=use_altauth,
ip=self.headers['X-Forwarded-For'] if behind_proxy else self.client_address[0],
uuid=selected_profile
)
self.send_response(code)
self.end_headers()
if code != 204 and data:
self.wfile.write(data)
except SilentException:
self.send_response(HTTPStatus.FORBIDDEN)
self.end_headers()
self.wfile.write(b'{"error":"ForbiddenOperationException","path":"/session/minecraft/join"}')
except BaseException:
self.log_message("Exception handling request: %s", format_exc())
# The client continues the login process with a 500 response, therefore 403 instead
self.send_response(HTTPStatus.FORBIDDEN)
self.end_headers()
self.wfile.write(b'{"error":"ForbiddenOperationException","path":"/session/minecraft/join"}')
def do_GET(self):
try:
self.close_connection = True
if not self.path.startswith("/session/minecraft/hasJoined?"):
self.send_response(HTTPStatus.NOT_FOUND)
self.end_headers()
return
query = {
attribute.split("=")[0]: attribute.split("=")[1]
for attribute in self.path.split("?", 1)[1].split("&")
}
server_id = query["serverId"]
username = query["username"]
altauth_client = server_id in cached_profiles
cached_profile = cached_profiles.pop(server_id) if altauth_client else None
if altauth_client and "ip" in query:
if query.pop("ip") != cached_profile.ip:
raise Exception("IPs don't match (prevent-proxy-connections)")
if altauth_client and cached_profile.use_altauth:
code, data = moj_request(
f"https://sessionserver.mojang.com/session/minecraft/profile/{cached_profile.uuid}"
)
if data:
profile = loads(data)
if profile["name"] != username: # Disarm server_id hash collisions and prevent username spoofing
raise Exception("Usernames don't match (potential hash collision)")
elif "ip" in query:
code, data = moj_request(
f"https://sessionserver.mojang.com/session/minecraft/hasJoined?username={username}&serverId={server_id}&ip={query['ip']}"
)
else:
code, data = moj_request(
f"https://sessionserver.mojang.com/session/minecraft/hasJoined?username={username}&serverId={server_id}"
)
self.send_response(code)
self.end_headers()
self.wfile.write(data)
except BaseException as e:
self.log_message("Exception handling request: %s", format_exc())
# 204 matches failure response for hasJoined
self.send_response(HTTPStatus.NO_CONTENT)
self.end_headers()
def log_request(self, code='-', size='-'):
# Don't log every request (may become spammy & may contain IPs from prevent-proxy-connections (GDPR)
pass
if __name__ == '__main__':
Thread(target=timeout_cleaner, name="TimeoutCleanup", daemon=True).start()
ThreadingHTTPServer(bind_to, AltAuthRequestHandler).serve_forever()