Created
December 2, 2024 19:51
-
-
Save servusdei2018/1913fe712e29a4d05285e7b4f5be9dd0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import requests | |
import time | |
""" | |
tform shifts string :m: by :shift: values up the ASCII mapping | |
""" | |
def tform(m,shift): | |
n = "" | |
for c in m: | |
n = n + str(chr(ord(c)+shift)) | |
return n | |
""" | |
listen_and_fetch starts up a server socket locally on port 1234 and if it is a potentially valid hostname, acts as a proxy, | |
returning the response generated by making a HTTP GET request thereto | |
""" | |
def listen_and_fetch(): | |
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
try: | |
server_socket.bind(('0.0.0.0', 1234)) | |
server_socket.listen(1) | |
except: | |
print("There was an error seting up Action 6. Please wait a few mins or restart your computer.") | |
return | |
print("Waiting for something...") | |
while True: | |
client_socket, client_address = server_socket.accept() | |
try: | |
client_socket.sendall("Try sending me some text and see what happens...".encode('utf-8')) | |
hostname = client_socket.recv(1024).decode('utf-8').strip() | |
if not hostname: | |
print("Input was empty, so skipping...") | |
continue | |
if not "." in hostname: | |
print("Conditioning input...") | |
client_socket.sendall("Next time try adding a TLD at the end of your URL!".encode("utf-8")) | |
hostname = hostname + ".com" | |
try: | |
response = requests.get(f"http://{hostname}") | |
client_socket.sendall(response.text.encode("utf-8")) | |
print("Congratulations on finding the surprise! Press Ctrl+C to close.") | |
except requests.exceptions.RequestException as e: | |
print("There was an error") | |
except Exception as e: | |
print("There was a different kind of error") | |
client_socket.close() | |
""" | |
connect_and_communicate sends a string to tcpbin.com's TCP echo service, which sends the same string back | |
""" | |
def connect_and_communicate(m,d=('tcpbin.com',4242),size=1024): | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | |
try: | |
s.connect(d) | |
s.sendall(m.encode('utf-8')) | |
response = s.recv(size).decode('utf-8') | |
except Exception as e: | |
print("A serious error occurred") | |
""" | |
connect_and_tform connects to localhost:42001, sends a message, and returns the message shifted by the group number | |
""" | |
def connect_and_tform(sh,d=('127.0.0.1',42001),size=1024): | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | |
try: | |
s.connect(d) | |
s.sendall("Try sending me text and see what happens...".encode('utf-8')) | |
response = s.recv(size).decode('utf-8') | |
s.sendall(tform(str(response),shift=sh).encode('utf-8')) | |
except Exception as e: | |
print("It seems this action has failed for some reason... :-)") | |
if __name__ == "__main__": | |
group = 12 | |
try: | |
print("Hello, Group " + str(group)) | |
print("Press Ctrl+C to close") | |
print("Performing Action 1...") | |
connect_and_communicate("Group " + str(group) + "") | |
print("Performing Action 2...") | |
t = requests.post('http://httpbin.org/post',data={"t":time.ctime()}) | |
print("Performing Action 3...") | |
s = requests.get('http://example.com') | |
print("Performing Action 4...") | |
connect_and_communicate(s.text) | |
print("Performing Action 5...") | |
connect_and_tform(group) | |
print("Performing Action 6...") | |
listen_and_fetch() | |
except KeyboardInterrupt: | |
print("Exiting") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment