-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthServer.py
More file actions
92 lines (71 loc) · 2.88 KB
/
AuthServer.py
File metadata and controls
92 lines (71 loc) · 2.88 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
"""
Example "auth" server
This server authenticates players with the mojang session server, then kicks
them. Useful for server websites that ask users for a valid Minecraft account.
"""
from twisted.internet import reactor
from quarry.net.server import ServerFactory, ServerProtocol
import psycopg2
from random import randint
from config import database, password, port, host, user
con = None
class AuthProtocol(ServerProtocol):
def player_joined(self):
global con
# This method gets called when a player successfully joins the server.
# If we're in online mode (the default), this means auth with the
# session server was successful and the user definitely owns the
# display name they claim to.
# Call super. This switches us to "play" mode, marks the player as
# in-game, and does some logging.
ServerProtocol.player_joined(self)
id = self.uuid.to_hex(with_dashes=False)
cur = con.cursor()
try:
cur.execute('SELECT pin FROM auths where minecraft_uuid=%s;', [id])
pin = cur.fetchone()
print(pin)
if not pin:
pin = randint(1000, 9999)
cur.execute('SELECT pin FROM auths where pin=%s;', [pin])
res = cur.fetchone()
while res:
pin = randint(1000, 9999)
cur.execute('SELECT pin FROM auths where pin=%s;', [pin])
res = cur.fetchone()
cur.execute('INSERT INTO auths (minecraft_uuid, pin) VALUES (%s, %s);', (str(id), str(pin)))
con.commit()
else:
pin = pin[0]
# Kick the player.
self.close(f"Send command to Curator bot: ,auth {pin}")
except Exception as e:
print(e)
self.close('No')
class AuthFactory(ServerFactory):
protocol = AuthProtocol
motd = "Curator - Auth Server"
def main(argv):
global con
# Parse options
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-a", "--host", default="", help="address to listen on")
parser.add_argument("-p", "--port", default=25565, type=int, help="port to listen on")
parser.add_argument("-db", "--database", default=False, type=bool, help="initializes the database")
args = parser.parse_args(argv)
con = con or psycopg2.connect(database=database, user=user, password=password,
host=host, port=port)
if args.database:
con.cursor().execute(
'CREATE TABLE auths(minecraft_uuid UUID, pin INTEGER UNIQUE, PRIMARY KEY (minecraft_uuid));')
con.commit()
# Create factory
factory = AuthFactory()
# Listen
factory.listen(args.host, args.port)
reactor.run()
if __name__ == "__main__":
import sys
print(f'Running {__file__}')
main(sys.argv[1:])