Configration section complete
This commit is contained in:
153
paster.py
153
paster.py
@@ -17,14 +17,14 @@ def main(theargs):
|
|||||||
|
|
||||||
# which port number to bind to
|
# which port number to bind to
|
||||||
# choose a port number > 1000, to prevent interference with system processes
|
# choose a port number > 1000, to prevent interference with system processes
|
||||||
port = "1234"
|
port = 8888
|
||||||
|
|
||||||
# maximum number of clients this can handle at any given time
|
# maximum number of clients this can handle at any given time
|
||||||
queue_depth = 10
|
queue_depth = 10
|
||||||
|
|
||||||
# directory path
|
# directory path
|
||||||
# default is $HOME i.e. /home/username/sockbin
|
# default is $HOME i.e. /home/username/socksbin
|
||||||
output_directory = os.path.expanduser("~") + "sockbin"
|
output_directory = os.path.join(os.path.expanduser("~"), "socksbin")
|
||||||
# specify the length of generated filename. multiples of 2 only
|
# specify the length of generated filename. multiples of 2 only
|
||||||
slug_size = 8
|
slug_size = 8
|
||||||
|
|
||||||
@@ -32,26 +32,35 @@ def main(theargs):
|
|||||||
# set between 4096 and 64000.
|
# set between 4096 and 64000.
|
||||||
buffer_size = 32768
|
buffer_size = 32768
|
||||||
|
|
||||||
|
log = False
|
||||||
# path for log file
|
# path for log file
|
||||||
log_file = "/tmp/socklog.txt"
|
log_file = "/tmp/socklog.txt"
|
||||||
|
|
||||||
helpmessage = """Welcome to SockBin, the command line pastebin !
|
# base url where the file will be served to the user
|
||||||
|
base_url = "https://socksbin.magnum.wtf/"
|
||||||
|
|
||||||
|
helpmessage = """
|
||||||
|
Welcome to SocksBin, the command line pastebin !
|
||||||
Released under GNU GPL.
|
Released under GNU GPL.
|
||||||
|
|
||||||
-n --hostname\t\tSet the hostname to listen for. 0.0.0.0 by default.
|
-n --hostname\tSet the hostname to listen for. 0.0.0.0 by default.
|
||||||
-p --port\t\tExternel port number to listen on. 8888 by default
|
-p --port\tExternel port number to listen on. 8888 by default
|
||||||
-q --queue_depth\tMax number of simultaneous connections to accept
|
-q --queue_depth\tMax number of simultaneous connections to accept
|
||||||
-o --output_directory\tFile storage location. $HOME/sockbin by default
|
-o --output_directory\tFile storage location. $HOME/socksbin by default
|
||||||
-s --slug_size\tLength of url to generate.
|
-s --slug_size\tLength of url to generate.
|
||||||
-b --buffer_size\tPacket size in bytes.
|
-b --buffer_size\tPacket size in bytes.
|
||||||
-l --log_file\t\tPath to log file.
|
-l --log_file\t\tPath to log file.
|
||||||
-h --help\t\tDisplay this message
|
-h --help\t\tDisplay this message
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
opts, args = getopt.getopt(theargs, "n:p:q:o:s:b:l:h:", [
|
if theargs[0] == "-h" or theargs[0] == "--help":
|
||||||
"hostname=", "port=", "queue_depth=", "output_directory=", "slug_size=", "buffer_size=", "log_file=", "help="])
|
print(helpmessage)
|
||||||
|
sys.exit()
|
||||||
|
except IndexError:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
opts, args = getopt.getopt(theargs, "n:p:q:o:s:b:l:u:", [
|
||||||
|
"hostname=", "port=", "queue_depth=", "output_directory=", "slug_size=", "buffer_size=", "log_file=", "url="])
|
||||||
for opt, arg in opts:
|
for opt, arg in opts:
|
||||||
if opt in ['-n', '--hostname']:
|
if opt in ['-n', '--hostname']:
|
||||||
if arg.find('/') != -1:
|
if arg.find('/') != -1:
|
||||||
@@ -79,7 +88,7 @@ def main(theargs):
|
|||||||
"Incorrect port format. Choose a number between 1000 and 64738")
|
"Incorrect port format. Choose a number between 1000 and 64738")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
else:
|
else:
|
||||||
port = arg
|
port = int(arg)
|
||||||
elif choice.lower() == "n":
|
elif choice.lower() == "n":
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -89,6 +98,10 @@ def main(theargs):
|
|||||||
print(
|
print(
|
||||||
"Queue depth can only be an integer. Run with -h for more options")
|
"Queue depth can only be an integer. Run with -h for more options")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
elif int(arg) > 1000 or int(arg) < 2:
|
||||||
|
print("Queue depth has to be between 2 and 1000")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
queue_depth = int(arg)
|
queue_depth = int(arg)
|
||||||
elif opt in ['-o', "--output_directory"]:
|
elif opt in ['-o', "--output_directory"]:
|
||||||
directory = arg
|
directory = arg
|
||||||
@@ -96,20 +109,24 @@ def main(theargs):
|
|||||||
permission = (isWritable(directory))
|
permission = (isWritable(directory))
|
||||||
|
|
||||||
if not permission:
|
if not permission:
|
||||||
print(f"You do not have permissions to write to {directory}")
|
print(
|
||||||
|
f"You do not have permissions to write to {directory}")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
else:
|
else:
|
||||||
output_directory = arg
|
output_directory = arg
|
||||||
else:
|
else:
|
||||||
parent_dir = (os.path.abspath(os.path.join(directory, os.pardir)))
|
parent_dir = (os.path.abspath(
|
||||||
|
os.path.join(directory, os.pardir)))
|
||||||
|
|
||||||
if not os.path.isdir(parent_dir):
|
if not os.path.isdir(parent_dir):
|
||||||
print(f"The parent folder {parent_dir} does not exist. Please try again after creating it.")
|
print(
|
||||||
|
f"The parent folder {parent_dir} does not exist. Please try again after creating it.")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
permission = (isWritable(parent_dir))
|
permission = (isWritable(parent_dir))
|
||||||
|
|
||||||
if not permission:
|
if not permission:
|
||||||
print(f"You do not have permissions to write to {directory}, or the parent folder {parent_dir} does not exist")
|
print(
|
||||||
|
f"You do not have permissions to write to {directory}, or the parent folder {parent_dir} does not exist")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
@@ -117,81 +134,133 @@ def main(theargs):
|
|||||||
print(f"> Directory {directory} created !")
|
print(f"> Directory {directory} created !")
|
||||||
output_directory = arg
|
output_directory = arg
|
||||||
except OSError:
|
except OSError:
|
||||||
print("unable to create directory. Please check your permissions.")
|
print(
|
||||||
sys.exit()
|
"unable to create directory. Please check your permissions.")
|
||||||
|
sys.exit()
|
||||||
elif opt in ['-s', "--slug_size"]:
|
elif opt in ['-s', "--slug_size"]:
|
||||||
slug_size = arg
|
if not arg.isdigit():
|
||||||
|
print("slug length can only be a integer, between 4 and 20")
|
||||||
|
sys.exit()
|
||||||
|
elif int(arg) > 20 or int(arg) < 2:
|
||||||
|
print("Slug length has to be between 4 and 20")
|
||||||
|
sys.exit()
|
||||||
|
else:
|
||||||
|
slug_size = int(arg)
|
||||||
elif opt in ['-b', "--buffer_size"]:
|
elif opt in ['-b', "--buffer_size"]:
|
||||||
buffer_size = arg
|
|
||||||
elif opt in ['-l', "--log_file"]:
|
|
||||||
log_file = arg
|
|
||||||
|
|
||||||
print({
|
if not arg.isdigit():
|
||||||
|
print("buffer length can only be a integer, between 1024 and 60000")
|
||||||
|
sys.exit()
|
||||||
|
elif int(arg) > 60000 or int(arg) < 1024:
|
||||||
|
print("buffer length has to be between 1024 and 60000")
|
||||||
|
sys.exit()
|
||||||
|
else:
|
||||||
|
buffer_size = int(arg)
|
||||||
|
elif opt in ['-l', "--log_file"]:
|
||||||
|
try:
|
||||||
|
parent_dir = (os.path.abspath(
|
||||||
|
os.path.join(arg, os.pardir)))
|
||||||
|
except:
|
||||||
|
print("Error while creating log file. Incorrect format.")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
if not os.path.isdir(parent_dir):
|
||||||
|
print(
|
||||||
|
f"The parent folder {parent_dir} does not exist. Please try again after creating it.")
|
||||||
|
sys.exit()
|
||||||
|
permission = (isWritable(parent_dir))
|
||||||
|
|
||||||
|
if not permission:
|
||||||
|
print(
|
||||||
|
f"You do not have permissions to write to {arg}, or the parent folder {parent_dir} does not exist")
|
||||||
|
sys.exit()
|
||||||
|
log_file = arg
|
||||||
|
log = True
|
||||||
|
elif opt in ['-h', '--help']:
|
||||||
|
print(helpmessage)
|
||||||
|
sys.exit()
|
||||||
|
elif opt in ['-u', '--url']:
|
||||||
|
base_url = arg
|
||||||
|
|
||||||
|
constants = ({
|
||||||
"hostname": hostname,
|
"hostname": hostname,
|
||||||
"port": port,
|
"port": port,
|
||||||
"queue": queue_depth,
|
"queue_depth": queue_depth,
|
||||||
"output": output_directory,
|
"output_directory": output_directory,
|
||||||
"slug": slug_size,
|
"slug_size": slug_size,
|
||||||
"buff": buffer_size,
|
"buffer_size": buffer_size,
|
||||||
"log": log_file,
|
"log": log,
|
||||||
|
"log_file": log_file,
|
||||||
|
"base_url": base_url
|
||||||
})
|
})
|
||||||
|
|
||||||
except getopt.GetoptError as err:
|
except getopt.GetoptError as err:
|
||||||
print(err)
|
print(err)
|
||||||
print("Run with -h or --help to see the various options")
|
print("Run with -h or --help to see the various options")
|
||||||
|
|
||||||
|
server(constants)
|
||||||
#######################################################################################
|
#######################################################################################
|
||||||
|
|
||||||
def server():
|
|
||||||
|
def server(config):
|
||||||
# create the socket
|
# create the socket
|
||||||
# AF_INET => IPv4
|
# AF_INET => IPv4
|
||||||
# SOCK_STREAM => TCP Connections
|
# SOCK_STREAM => TCP Connections
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
|
||||||
s.bind(("0.0.0.0", 1234))
|
s.bind((config['hostname'], config['port']))
|
||||||
|
|
||||||
s.listen(5)
|
s.listen(config['queue_depth'])
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
clientSocket, address = s.accept()
|
clientSocket, address = s.accept()
|
||||||
print(f"Connection from {address} has been established")
|
print(f"Connection from {address} has been established")
|
||||||
filepath = secrets.token_hex(8)
|
filepath = secrets.token_hex(8)
|
||||||
clientSocket.sendall(
|
clientSocket.sendall(
|
||||||
bytes("https://static.magnum.wtf/"+filepath, "utf-8"))
|
bytes(config['base_url']+filepath, "utf-8"))
|
||||||
clientSocket.shutdown(socket.SHUT_WR)
|
clientSocket.shutdown(socket.SHUT_WR)
|
||||||
|
|
||||||
full_message = ""
|
full_message = ""
|
||||||
while True:
|
while True:
|
||||||
data = clientSocket.recv(4096)
|
data = clientSocket.recv(config['buffer_size'])
|
||||||
if len(data) <= 0:
|
if len(data) <= 0:
|
||||||
|
|
||||||
break
|
break
|
||||||
print("ingesting")
|
print("ingesting")
|
||||||
full_message += data.decode('utf-8')
|
full_message += data.decode('utf-8')
|
||||||
|
|
||||||
with open(filepath+".txt", 'w') as writer:
|
with open(os.path.join(config['output_directory'], filepath), 'w') as writer:
|
||||||
writer.write(full_message)
|
writer.write(full_message)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def isWritable(directory):
|
def isWritable(directory):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
tmp_prefix = "write_tester";
|
tmp_prefix = "write_tester"
|
||||||
count = 0
|
count = 0
|
||||||
filename = os.path.join(directory, tmp_prefix)
|
filename = os.path.join(directory, tmp_prefix)
|
||||||
while(os.path.exists(filename)):
|
while(os.path.exists(filename)):
|
||||||
filename = "{}.{}".format(os.path.join(directory, tmp_prefix),count)
|
filename = "{}.{}".format(
|
||||||
|
os.path.join(directory, tmp_prefix), count)
|
||||||
count = count + 1
|
count = count + 1
|
||||||
f = open(filename,"w")
|
f = open(filename, "w")
|
||||||
f.close()
|
f.close()
|
||||||
os.remove(filename)
|
os.remove(filename)
|
||||||
return True
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def printTable(myDict, colList=None):
|
||||||
|
""" Pretty print a list of dictionaries (myDict) as a dynamically sized table.
|
||||||
|
If column names (colList) aren't specified, they will show in random order.
|
||||||
|
Author: Thierry Husson - Use it as you want but don't blame me.
|
||||||
|
"""
|
||||||
|
if not colList: colList = list(myDict[0].keys() if myDict else [])
|
||||||
|
myList = [colList] # 1st row = header
|
||||||
|
for item in myDict: myList.append([str(item[col] if item[col] is not None else '') for col in colList])
|
||||||
|
colSize = [max(map(len,col)) for col in zip(*myList)]
|
||||||
|
formatStr = ' | '.join(["{{:<{}}}".format(i) for i in colSize])
|
||||||
|
myList.insert(1, ['-' * i for i in colSize]) # Seperating line
|
||||||
|
for item in myList: print(formatStr.format(*item))
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main(sys.argv[1:])
|
main(sys.argv[1:])
|
||||||
|
|||||||
Reference in New Issue
Block a user