...
Code Block | ||
---|---|---|
| ||
import http.client as http
import json
from urllib.parse import urlparse
from urllib.parse import urlencode
from base64 import b64encode
import hashlib
import socket
import shutil
import os
#Your server URL
server_url = 'http://example.com:55414/x'
#If you have basic authentication via .htpasswd
server_basic_username = ''
server_basic_password = ''
#user needs following rights
# "settings": "all"
# "status": "some"
# "add_client": "all"
server_username='admin'
server_password='foo'
session=""
def get_response(action, params):
global server_url;
global server_basic_username;
global server_basic_password;
global session;
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json; charset=UTF-8'
}
if('server_basic_username' in globals() and len(server_basic_username)>0):
userAndPass = b64encode(str.encode(server_basic_username+":"+server_basic_password)).decode("ascii")
headers['Authorization'] = 'Basic %s' % userAndPass
curr_server_url=server_url+"?"+urlencode({"a": action});
if(len(session)>0):
params["ses"]=session
curr_server_url+="&"+urlencode(params);
target = urlparse(curr_server_url)
method = 'GET'
body = ''
if(target.scheme=='http'):
h = http.HTTPConnection(target.hostname, target.port)
elif(target.scheme=='https'):
h = http.HTTPSConnection(target.hostname, target.port)
else:
print('Unkown scheme: '+target.scheme)
raise Exception("Unkown scheme: "+target.scheme)
h.request(
method,
target.path+"?"+target.query,
body,
headers)
return h.getresponse();
def get_json(action, params = {}):
response = get_response(action, params)
if(response.status != 200):
return ""
data = response.readall();
response.close()
return json.loads(data.decode("utf-8"))
def download_file(action, outputfn, params):
response = get_response(action, params);
if(response.status!=200):
return False
with open(outputfn, 'wb') as outputf:
shutil.copyfileobj(response, outputf)
return True
def md5(s):
return hashlib.md5(s.encode()).hexdigest()
print("Logging in...")
salt = get_json("salt", {"username": server_username})
if( not ('ses' in salt) ):
print('Username does not exist')
exit(1)
session = salt["ses"];
if( 'salt' in salt ):
password_md5 = md5(salt["rnd"]+md5(salt["salt"]+server_password));
login = get_json("login", { "username": server_username,
"password": password_md5 })
if('success' not in login or not login['success']):
print('Error during login. Password wrong?')
exit(1)
print("Creating client "+socket.gethostname()+"...")
status = get_json("status", { "clientname": socket.gethostname()})
for client in status["client_downloads"]:
if (client["name"] == socket.gethostname()):
print("Downloading Installer...")
if not download_file("download_client", "Client Installer.exe", {"clientid": client["id"]}):
print("Downloading client failed")
exit(1)
print("Sucessfully downloaded client")
os.startfile("Client Installer.exe")
exit(0)
print("Could not find client for download. No permission?")
exit(1) |
...