Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import sys
- import _thread
- import requests as re
- HOST = ''
- PORT = int(sys.argv[1])
- buffer = 4096
- Bad_Words = ["spongebob","norrköping","paris hilton","britney spears", "norrk??ping"]
- # Bad word has been found, now we've got to relocate.
- def redirectWebsite(conn):
- print("302 Found")
- relocate = "HTTP/1.1 302 Found\r\nLocation: http://www.ida.liu.se/~TDTS04/labs/2011/ass2/error2.html\r\n\r\n"
- conn.sendall(relocate.encode('utf-8'))
- print("relocation sent to connection stream")
- #conn.close()
- # Check for bad words
- def checkWords(conn, data):
- redirect = False
- for bad in Bad_Words:
- if(str(data).find(bad) != -1):
- redirect = True
- break
- if redirect:
- redirectWebsite(conn)
- return redirect
- else:
- return redirect
- def proxy_server(webserver, port, conn, data, addr):
- try:
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Open socket to communicate with website
- s.connect((webserver, port)) # Connecting to web server with url and port nr.
- print("connected to webserver")
- s.sendall(data) # Sends all the data we want, in multiple TCP segments if necessary
- temp = re.get("http://"+webserver) # Get content within website, import requests
- if checkWords(conn, temp.text.lower()): # Call to see if bad words are present in the website.
- print("The content of the website was not family friendly and you have been redirected to another page.")
- else:
- while 1:
- reply = s.recv(buffer) # Recieve data we asked for, possibly in multiple loops
- if len(reply) > 0:
- conn.sendall(reply) # Sends the data we just recieved back to the client.
- rad = float(len(reply)) # Formating
- rad = float(rad / 1024) # Formating
- rad = "%.3s" % (str(rad)) # Formating
- rad = "%s KB" % (rad) # Formating
- print("[*] Request diddelydone: %s => %s <= " % (str(data[0]),str(rad))) # Showing how big of a request we recieved.
- print("Proxy server finished beep boop")
- s.close() # Closing socket to website.
- conn.close()
- except socket.error:
- s.close()
- conn.close()
- def conn_string(conn, data, addr):
- try:
- # line = data.split('\n')[1]
- # address = line.split(' ')[1]
- # port = 80
- # print(str(data))
- temp1 = str(data)
- line = temp1.split('\n')[0]
- address = line.split(' ')[1]
- http = address.find("://")
- if (http==-1):
- temp = address
- else:
- temp = address[(http+3):]
- ifport = temp.find(":")
- webserver_pos = temp.find("/")
- if webserver_pos == -1:
- webserver_pos = len(temp)
- webserver = ""
- port = -1
- if ifport ==-1 or webserver_pos < ifport:
- port = 80
- webserver = temp[:webserver_pos]
- else:
- port = int((temp[(ifport+1):])[:webserver_pos-ifport-1])
- webserver = temp[:ifport]
- if(checkWords(conn, data.lower())): # Checking if HTTP GET REQUEST header has bad words
- print("The content of the website was not family friendly and you have been redirected to another page.")
- conn.close() # If header contains bad words, close connection stream.
- else:
- print("We are now heading into proxy_server")
- proxy_server(webserver, port, conn, data, addr) # No bad words found, start connecting to webserver.
- except Exception as e:
- print(e) # Prints whichever exception we had
- pass # Continue as usual.
- def main():
- try:
- serverSocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # Create socket between proxy and client
- print("Socket initialized")
- serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- print("sockopt works")
- serverSocket.bind((HOST,PORT)) # Binds socket to localhost with chosen port.
- print("Socket successfully bound")
- print("We're now *BEEP BOOP* listening")
- serverSocket.listen(5) # Queue of how many requests whom can fit in the queue at one time.
- except Exception as e:
- print(e)
- sys.exit()
- while 1:
- try:
- conn, addr = serverSocket.accept() # Accept returns a tuple, which is of type connection stream and host address.
- data = conn.recv(buffer) # Recieve HTTP GET REQUEST header, to be sent to webserver later.
- _thread.start_new_thread(conn_string, (conn, data, addr)) # Starts a new thread, call to continue process.
- except Exception as e:
- serverSocket.close()
- print(e)
- sys.exit()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement