ding/ding

117 lines
3.0 KiB
Plaintext
Raw Permalink Normal View History

2017-09-30 12:51:23 +00:00
#!/usr/bin/env python3
2017-09-30 15:02:06 +00:00
# Author: Bandie Canis
# License: 2-Clause BSD License
import sys, ssl, socket, os, getopt
2017-09-30 12:51:23 +00:00
import configparser
2018-04-04 14:24:52 +00:00
host = None
port = 0
cafile = None
certfile = None
keyfile = None
2017-12-27 12:40:17 +00:00
exitcode = 1
def init(conf):
2018-02-01 13:09:35 +00:00
if(conf == None):
if(os.name == 'nt'):
CONFIG = "ding.win.cfg"
else:
CONFIG = "ding.cfg"
2018-02-01 13:09:35 +00:00
else:
CONFIG = conf
2018-02-01 13:09:35 +00:00
cfg = configparser.ConfigParser()
2017-09-30 12:51:23 +00:00
try:
cfg.read(CONFIG)
global host, port, cafile, certfile, keyfile
host = cfg.get("Client", "host")
port = int(cfg.get("Client", "port"))
cafile = cfg.get("Client", "cafile")
certfile = cfg.get("Client", "certfile")
keyfile = cfg.get("Client", "keyfile")
except configparser.NoSectionError:
2018-03-28 22:58:36 +00:00
print("No suitable config found. Expecting some config in", CONFIG, file=sys.stderr)
2017-09-30 12:51:23 +00:00
quit(3)
def send(conn, cmd):
conn.connect((host, port))
buf = conn.recv(1024)
2018-04-04 14:33:36 +00:00
if(buf == b"OK 1337\n"):
2017-09-30 12:51:23 +00:00
conn.sendall(cmd)
buf = conn.recv(1024)
if(buf == b"OK CMD"):
exitcode = 0
elif(buf == b"ERR NO_CMD"):
2018-02-01 13:09:35 +00:00
print("Error. Server said: The command isn't set on the server.", file=sys.stderr)
2017-09-30 12:51:23 +00:00
exitcode = 1
elif(buf == b"ERR CMD_ERR"):
2018-03-28 22:58:36 +00:00
print("Error. Server said: The command doesn't work because the file doesn't exist on the server.", file=sys.stderr)
2017-09-30 12:51:23 +00:00
exitcode = 2
2018-02-01 00:36:25 +00:00
elif(buf == b"ERR PW"):
2018-03-28 22:58:36 +00:00
print("Error. Password required. The password was wrong.", file=sys.stderr)
2018-02-01 00:36:25 +00:00
exitcode = 4
elif(b"OK PW" in buf):
bufr=str(buf.decode('utf-8'))
print("Password accepted. Timeout:", bufr.replace("OK PW ",""), "seconds.")
2017-09-30 12:51:23 +00:00
else:
conn.sendall(b"NO.")
print("The server seems to be crazy. Nothing sent.", file=sys.stderr)
conn.close()
2018-04-04 14:24:52 +00:00
quit(exitcode)
2017-09-30 12:51:23 +00:00
def main(arg):
2017-09-30 12:51:23 +00:00
try:
context = ssl.SSLContext(ssl.PROTOCOL_TLS)
context.verify_mode = ssl.CERT_REQUIRED
context.load_verify_locations(cafile)
context.load_cert_chain(certfile=certfile, keyfile=keyfile)
except FileNotFoundError as e:
print(e)
print("Please check your paths in the config file. (Have you forgotten to generate the Certificates?)", sep="", file=sys.stderr)
quit(2)
if(":" in host):
conn = context.wrap_socket(socket.socket(family=socket.AF_INET6))
else:
conn = context.wrap_socket(socket.socket(family=socket.AF_INET))
2017-09-30 12:51:23 +00:00
try:
send(conn, bytes(arg[0], sys.stdin.encoding))
2017-09-30 12:51:23 +00:00
except IndexError:
print(sys.argv[0], ": Missing argument.\nSyntax: ", sys.argv[0], " <COMMAND>", file=sys.stderr)
except ConnectionRefusedError:
print("Connection refused.", file=sys.stderr)
quit(1)
except ssl.SSLError:
print("Wrong certificate.", file=sys.stderr);
quit(3)
if(__name__ == "__main__"):
try:
conf = None
opts, args = getopt.getopt(sys.argv[1:], "c:")
for o, a in opts:
if o == "-c":
conf = a
init(conf)
main(args)
except getopt.GetoptError as e:
print("Error using options. Allowed options:\n-c [FILE] - Config file\n")
quit(2)
2017-09-30 12:51:23 +00:00
quit(exitcode)