initial
This commit is contained in:
commit
3ebcad0d20
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
__pycache__/
|
||||
ldap_cred.py
|
7
ldap_cred.py.sample
Normal file
7
ldap_cred.py.sample
Normal file
@ -0,0 +1,7 @@
|
||||
ldap_cred = dict(
|
||||
ldap_url = "ldap.example.org",
|
||||
ldap_user = "uid=minecraft,ou=services,dc=example,dc=org",
|
||||
ldap_pass = "Password!",
|
||||
ldap_search_base = ",ou=accounts,dc=example,dc=org",
|
||||
ldap_filter = "(objectclass=account)"
|
||||
)
|
10
whitelist.json
Normal file
10
whitelist.json
Normal file
@ -0,0 +1,10 @@
|
||||
[
|
||||
{
|
||||
"uuid": "f430dbb6-5d9a-444e-b542-e47329b2c5a0",
|
||||
"name": "username"
|
||||
},
|
||||
{
|
||||
"uuid": "e5aa0f99-2727-4a11-981f-dded8b1cd032",
|
||||
"name": "username2"
|
||||
}
|
||||
]
|
76
whitelist_gen.py
Executable file
76
whitelist_gen.py
Executable file
@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env python3
|
||||
import json
|
||||
import requests
|
||||
import argparse
|
||||
from ldap3 import Server, Connection, ALL
|
||||
from ldap_cred import ldap_cred
|
||||
|
||||
|
||||
class Person(dict):
|
||||
|
||||
def __init__(self, username, uuid):
|
||||
dict.__init__(self, name=username, uuid=uuid)
|
||||
|
||||
|
||||
def lookupLDAPUsers():
|
||||
server = Server(ldap_cred['ldap_url'], get_info=ALL)
|
||||
conn = Connection(server, ldap_cred['ldap_user'], ldap_cred['ldap_pass'],
|
||||
auto_bind=True)
|
||||
check = conn.search(ldap_cred['ldap_search_base'],
|
||||
ldap_cred['ldap_filter'], attributes=['uid'])
|
||||
if check is False:
|
||||
print("Error performing LDAP search")
|
||||
return []
|
||||
res = []
|
||||
for i in conn.entries:
|
||||
res.append(str(i['uid']))
|
||||
return res
|
||||
|
||||
|
||||
def lookupMCPlayer(username):
|
||||
url = "https://playerdb.co/api/player/minecraft/" + username
|
||||
response = requests.get(url)
|
||||
if (response.status_code != 200):
|
||||
print("Error looking up {}, reponse code {}"
|
||||
.format(username, response.status_code))
|
||||
return None
|
||||
uid = response.json()["data"]['player']["id"]
|
||||
return Person(username, uid)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--filename",
|
||||
help="Whitelist file location," +
|
||||
"defaults to ./whitelist.json",
|
||||
type=str)
|
||||
args = parser.parse_args()
|
||||
filename = args.filename
|
||||
if filename is None:
|
||||
filename = "whitelist.json"
|
||||
|
||||
print("Loading current whitelist")
|
||||
with open(filename) as json_file:
|
||||
players = json.load(json_file)
|
||||
|
||||
player_usernames = []
|
||||
for p in players:
|
||||
player_usernames.append(p['name'])
|
||||
|
||||
print("Loading players from LDAP")
|
||||
ldap_players = lookupLDAPUsers()
|
||||
for p in ldap_players:
|
||||
if p not in player_usernames:
|
||||
m = lookupMCPlayer(p)
|
||||
if m is not None:
|
||||
print("Added new player {}".format(p))
|
||||
players.append(m)
|
||||
else:
|
||||
print("Could not add new player {}".format(p))
|
||||
|
||||
print("Writing player whitelist to file")
|
||||
with open(filename, 'w') as outfile:
|
||||
json.dump(players, outfile, indent=4)
|
||||
|
||||
|
||||
main()
|
Loading…
Reference in New Issue
Block a user