Directory creation complete
This commit is contained in:
108
paster.py
108
paster.py
@@ -1,7 +1,11 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
import os,socket,secrets
|
import os
|
||||||
import sys,getopt,re
|
import socket
|
||||||
|
import secrets
|
||||||
|
import sys
|
||||||
|
import getopt
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
# Config
|
# Config
|
||||||
@@ -11,25 +15,19 @@ def main(theargs):
|
|||||||
# 0.0.0.0 => accessible to the internet, by any host
|
# 0.0.0.0 => accessible to the internet, by any host
|
||||||
hostname = "0.0.0.0"
|
hostname = "0.0.0.0"
|
||||||
|
|
||||||
|
|
||||||
# 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 = "1234"
|
||||||
|
|
||||||
|
|
||||||
# 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/sockbin
|
||||||
output_directory = os.path.expanduser("~") + "sockbin"
|
output_directory = os.path.expanduser("~") + "sockbin"
|
||||||
|
|
||||||
|
|
||||||
# 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
|
||||||
|
|
||||||
|
|
||||||
# amount of data transferred at a time, in bytes
|
# amount of data transferred at a time, in bytes
|
||||||
# set between 4096 and 64000.
|
# set between 4096 and 64000.
|
||||||
buffer_size = 32768
|
buffer_size = 32768
|
||||||
@@ -37,7 +35,6 @@ def main(theargs):
|
|||||||
# 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 !
|
helpmessage = """Welcome to SockBin, the command line pastebin !
|
||||||
Released under GNU GPL.
|
Released under GNU GPL.
|
||||||
|
|
||||||
@@ -53,7 +50,8 @@ def main(theargs):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
opts, args = getopt.getopt(theargs, "n:p:q:o:s:b:l:", ["hostname=","port=", "queue_depth=", "output_directory=", "slug_size=","buffer_size=", "log_file="])
|
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="])
|
||||||
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:
|
||||||
@@ -63,31 +61,64 @@ def main(theargs):
|
|||||||
hostname = arg
|
hostname = arg
|
||||||
elif opt in ['-p', "--port"]:
|
elif opt in ['-p', "--port"]:
|
||||||
if re.match("^ *\d[\d ]*$", arg) is None:
|
if re.match("^ *\d[\d ]*$", arg) is None:
|
||||||
print("Incorrect port format. Choose a number between 0 and 64738. Warning: ports below 1000 could interfere with system processes.")
|
print(
|
||||||
|
"Incorrect port format. Choose a number between 0 and 64738. Warning: ports below 1000 could interfere with system processes.")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
elif int(arg) > 64738:
|
elif int(arg) > 64738:
|
||||||
print("Incorrect port format. Choose a number between 0 and 64738. Warning: ports below 1000 could interfere with system processes.")
|
print(
|
||||||
|
"Incorrect port format. Choose a number between 0 and 64738. Warning: ports below 1000 could interfere with system processes.")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
elif int(arg) < 1000:
|
elif int(arg) < 1000:
|
||||||
choice = input("Warning: ports below 1000 could interfere with system processes. Do you want to choose a different port ? (y/n): ")
|
choice = input(
|
||||||
|
"Warning: ports below 1000 could interfere with system processes. Do you want to choose a different port ? (y/n): ")
|
||||||
|
|
||||||
if choice.lower() == "y":
|
if choice.lower() == "y":
|
||||||
arg = input("Please enter your new port number: ")
|
arg = input("Please enter your new port number: ")
|
||||||
if re.match("^ *\d[\d ]*$", arg) is None:
|
if re.match("^ *\d[\d ]*$", arg) is None:
|
||||||
print("Incorrect port format. Choose a number between 1000 and 64738")
|
print(
|
||||||
|
"Incorrect port format. Choose a number between 1000 and 64738")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
else:
|
else:
|
||||||
port = arg
|
port = arg
|
||||||
elif choice.lower() == "n":
|
elif choice.lower() == "n":
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
port = int(arg)
|
||||||
|
|
||||||
port = arg
|
|
||||||
elif opt in ['-q', "--queue_depth"]:
|
elif opt in ['-q', "--queue_depth"]:
|
||||||
queue_depth = arg
|
if not arg.isdigit():
|
||||||
|
print(
|
||||||
|
"Queue depth can only be an integer. Run with -h for more options")
|
||||||
|
sys.exit()
|
||||||
|
queue_depth = int(arg)
|
||||||
elif opt in ['-o', "--output_directory"]:
|
elif opt in ['-o', "--output_directory"]:
|
||||||
|
directory = arg
|
||||||
|
if os.path.isdir(directory):
|
||||||
|
permission = (isWritable(directory))
|
||||||
|
|
||||||
|
if not permission:
|
||||||
|
print(f"You do not have permissions to write to {directory}")
|
||||||
|
sys.exit()
|
||||||
|
else:
|
||||||
output_directory = arg
|
output_directory = arg
|
||||||
|
else:
|
||||||
|
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.")
|
||||||
|
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")
|
||||||
|
sys.exit()
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
os.makedirs(directory)
|
||||||
|
print(f"> Directory {directory} created !")
|
||||||
|
output_directory = arg
|
||||||
|
except OSError:
|
||||||
|
print("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
|
slug_size = arg
|
||||||
elif opt in ['-b', "--buffer_size"]:
|
elif opt in ['-b', "--buffer_size"]:
|
||||||
@@ -96,13 +127,13 @@ def main(theargs):
|
|||||||
log_file = arg
|
log_file = arg
|
||||||
|
|
||||||
print({
|
print({
|
||||||
"hostname":hostname,
|
"hostname": hostname,
|
||||||
"port":port,
|
"port": port,
|
||||||
"queue":queue_depth,
|
"queue": queue_depth,
|
||||||
"output":output_directory,
|
"output": output_directory,
|
||||||
"slug":slug_size,
|
"slug": slug_size,
|
||||||
"buff":buffer_size,
|
"buff": buffer_size,
|
||||||
"log":log_file,
|
"log": log_file,
|
||||||
})
|
})
|
||||||
|
|
||||||
except getopt.GetoptError as err:
|
except getopt.GetoptError as err:
|
||||||
@@ -110,8 +141,6 @@ def main(theargs):
|
|||||||
print("Run with -h or --help to see the various options")
|
print("Run with -h or --help to see the various options")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#######################################################################################
|
#######################################################################################
|
||||||
|
|
||||||
def server():
|
def server():
|
||||||
@@ -120,21 +149,18 @@ def server():
|
|||||||
# 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(("0.0.0.0", 1234))
|
||||||
|
|
||||||
s.listen(5)
|
s.listen(5)
|
||||||
|
|
||||||
|
|
||||||
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(bytes("https://static.magnum.wtf/"+filepath, "utf-8"))
|
clientSocket.sendall(
|
||||||
|
bytes("https://static.magnum.wtf/"+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(4096)
|
||||||
@@ -144,12 +170,28 @@ def server():
|
|||||||
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(filepath+".txt", 'w') as writer:
|
||||||
writer.write(full_message)
|
writer.write(full_message)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def isWritable(directory):
|
||||||
|
|
||||||
|
try:
|
||||||
|
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)
|
||||||
|
count = count + 1
|
||||||
|
f = open(filename,"w")
|
||||||
|
f.close()
|
||||||
|
os.remove(filename)
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main(sys.argv[1:])
|
main(sys.argv[1:])
|
||||||
Reference in New Issue
Block a user