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
|
||||
# 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
|
||||
queue_depth = 10
|
||||
|
||||
# directory path
|
||||
# default is $HOME i.e. /home/username/sockbin
|
||||
output_directory = os.path.expanduser("~") + "sockbin"
|
||||
# default is $HOME i.e. /home/username/socksbin
|
||||
output_directory = os.path.join(os.path.expanduser("~"), "socksbin")
|
||||
# specify the length of generated filename. multiples of 2 only
|
||||
slug_size = 8
|
||||
|
||||
@@ -32,26 +32,35 @@ def main(theargs):
|
||||
# set between 4096 and 64000.
|
||||
buffer_size = 32768
|
||||
|
||||
log = False
|
||||
# path for log file
|
||||
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.
|
||||
|
||||
-n --hostname\t\tSet the hostname to listen for. 0.0.0.0 by default.
|
||||
-p --port\t\tExternel port number to listen on. 8888 by default
|
||||
-n --hostname\tSet the hostname to listen for. 0.0.0.0 by default.
|
||||
-p --port\tExternel port number to listen on. 8888 by default
|
||||
-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.
|
||||
-b --buffer_size\tPacket size in bytes.
|
||||
-l --log_file\t\tPath to log file.
|
||||
-h --help\t\tDisplay this message
|
||||
|
||||
"""
|
||||
|
||||
try:
|
||||
opts, args = getopt.getopt(theargs, "n:p:q:o:s:b:l:h:", [
|
||||
"hostname=", "port=", "queue_depth=", "output_directory=", "slug_size=", "buffer_size=", "log_file=", "help="])
|
||||
if theargs[0] == "-h" or theargs[0] == "--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:
|
||||
if opt in ['-n', '--hostname']:
|
||||
if arg.find('/') != -1:
|
||||
@@ -79,7 +88,7 @@ def main(theargs):
|
||||
"Incorrect port format. Choose a number between 1000 and 64738")
|
||||
sys.exit()
|
||||
else:
|
||||
port = arg
|
||||
port = int(arg)
|
||||
elif choice.lower() == "n":
|
||||
pass
|
||||
|
||||
@@ -89,6 +98,10 @@ def main(theargs):
|
||||
print(
|
||||
"Queue depth can only be an integer. Run with -h for more options")
|
||||
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)
|
||||
elif opt in ['-o', "--output_directory"]:
|
||||
directory = arg
|
||||
@@ -96,20 +109,24 @@ def main(theargs):
|
||||
permission = (isWritable(directory))
|
||||
|
||||
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()
|
||||
else:
|
||||
output_directory = arg
|
||||
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):
|
||||
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()
|
||||
permission = (isWritable(parent_dir))
|
||||
|
||||
|
||||
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()
|
||||
else:
|
||||
try:
|
||||
@@ -117,81 +134,133 @@ def main(theargs):
|
||||
print(f"> Directory {directory} created !")
|
||||
output_directory = arg
|
||||
except OSError:
|
||||
print("unable to create directory. Please check your permissions.")
|
||||
sys.exit()
|
||||
print(
|
||||
"unable to create directory. Please check your permissions.")
|
||||
sys.exit()
|
||||
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"]:
|
||||
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,
|
||||
"port": port,
|
||||
"queue": queue_depth,
|
||||
"output": output_directory,
|
||||
"slug": slug_size,
|
||||
"buff": buffer_size,
|
||||
"log": log_file,
|
||||
"queue_depth": queue_depth,
|
||||
"output_directory": output_directory,
|
||||
"slug_size": slug_size,
|
||||
"buffer_size": buffer_size,
|
||||
"log": log,
|
||||
"log_file": log_file,
|
||||
"base_url": base_url
|
||||
})
|
||||
|
||||
except getopt.GetoptError as err:
|
||||
print(err)
|
||||
print("Run with -h or --help to see the various options")
|
||||
|
||||
|
||||
server(constants)
|
||||
#######################################################################################
|
||||
|
||||
def server():
|
||||
|
||||
def server(config):
|
||||
# create the socket
|
||||
# AF_INET => IPv4
|
||||
# SOCK_STREAM => TCP Connections
|
||||
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:
|
||||
clientSocket, address = s.accept()
|
||||
print(f"Connection from {address} has been established")
|
||||
filepath = secrets.token_hex(8)
|
||||
clientSocket.sendall(
|
||||
bytes("https://static.magnum.wtf/"+filepath, "utf-8"))
|
||||
bytes(config['base_url']+filepath, "utf-8"))
|
||||
clientSocket.shutdown(socket.SHUT_WR)
|
||||
|
||||
full_message = ""
|
||||
while True:
|
||||
data = clientSocket.recv(4096)
|
||||
data = clientSocket.recv(config['buffer_size'])
|
||||
if len(data) <= 0:
|
||||
|
||||
break
|
||||
print("ingesting")
|
||||
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)
|
||||
|
||||
|
||||
|
||||
|
||||
def isWritable(directory):
|
||||
|
||||
try:
|
||||
tmp_prefix = "write_tester";
|
||||
tmp_prefix = "write_tester"
|
||||
count = 0
|
||||
filename = os.path.join(directory, tmp_prefix)
|
||||
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
|
||||
f = open(filename,"w")
|
||||
f = open(filename, "w")
|
||||
f.close()
|
||||
os.remove(filename)
|
||||
return True
|
||||
except Exception as e:
|
||||
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__":
|
||||
main(sys.argv[1:])
|
||||
|
||||
Reference in New Issue
Block a user