-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTheInfiniteWebsite.py
executable file
·137 lines (113 loc) · 5.09 KB
/
TheInfiniteWebsite.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python
from twisted.internet import reactor # drives the whole process, accepting TCP connections and moving bytes
from twisted.internet import defer
from twisted.web import http
from twisted.internet import protocol
from twisted.web.server import NOT_DONE_YET
import datetime
import curses
import logging
import argparse
# config of the log
logging.basicConfig(filename='theinfinitewebsite.log',level=logging.INFO,format='%(asctime)s %(message)s')
# Arg parser
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--port', help='Port where the webserver should listen.', action='store', required=False, type=int, default=8800)
args = parser.parse_args()
if args.port:
port = args.port
# Global Class. Oh my.
class cli():
def __init__(self):
self.connectionTime = -1
self.disconnectionTime = -1
self.amountTransfered = 0
self.y_pos = -1
# Global variables. YES GLOBAL
y_pos = 1
clients = {}
# Initialize the curses
stdscr = curses.initscr()
curses.start_color()
curses.use_default_colors()
new_screen = stdscr
curses.init_pair(1, curses.COLOR_GREEN, -1)
curses.init_pair(2, curses.COLOR_RED, -1)
curses.init_pair(3, curses.COLOR_CYAN, -1)
curses.init_pair(4, curses.COLOR_WHITE, -1)
stdscr.bkgd(' ')
curses.noecho()
curses.cbreak()
new_screen.keypad(1)
curses.curs_set(0)
new_screen.addstr(0,0, 'The Infinite Web Page. Live Log of captured clients.')
new_screen.refresh()
screen = new_screen
#####################
def wait(seconds, result=None):
"""Returns a deferred that will be fired later"""
d = defer.Deferred()
reactor.callLater(seconds, d.callback, result)
return d
class StreamHandler(http.Request):
global y_pos
global clients
@defer.inlineCallbacks
def process(self):
global y_pos
global clients
newcli = cli()
newcli.connectionTime = datetime.datetime.now()
clients[self.client] = newcli
clients[self.client].y_pos = y_pos
logging.info('New Client connected from {}:{}'.format(self.client.host, self.client.port))
y_pos += 1
try:
useragent = http.Request.getAllHeaders(self)['user-agent']
short_useragent = useragent[0:50]
except:
useragent = "Empty"
short_useragent = "Empty"
logging.info('Client {}:{}. User-Agent: {}'.format(self.client.host, self.client.port, useragent))
logging.info('Client {}:{}. Method: {}'.format(self.client.host, self.client.port, str(self.method)))
logging.info('Client {}:{}. Path: {}'.format(self.client.host, self.client.port, str(self.uri)))
# Print
screen.addstr(clients[self.client].y_pos,0, "Client "+str(self.client.host)+':'+str(self.client.port)+'. '+str(clients[self.client].connectionTime)+' '+str(self.method)+' '+str(self.uri)+' UA: '+short_useragent)
#screen.addstr(13,0,str(self.uri))
screen.refresh()
# For GET and POST it works fine
if 'GET' in self.method or 'POST' in self.method or 'CONNECT' in self.method or 'PUT' in self.method:
while not http.Request.finished:
self.setHeader('Connection', 'Keep-Alive')
s = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><html><head><title>This is a TL;DR page.</title></head><body>"
s += str("What you are looking for is in the next line<br>"*100)
newcli.amountTransfered += len(s)
# For some reason the connection is not stopped and continues to try to send data
screen.addstr(clients[self.client].y_pos,140, " Data {:>5.3f} MB".format(clients[self.client].amountTransfered/1024/1024.0)+" Duration "+str(datetime.datetime.now() - clients[self.client].connectionTime), curses.color_pair(2))
screen.refresh()
try:
self.write(s)
yield wait(0)
except:
return
# For HEAD we should do something different because they don't wait for any data.
elif 'HEAD' in self.method or 'OPTIONS' in self.method:
self.setHeader('Connection', 'Keep-Alive')
def connectionLost(self,reason):
global clients
disconnect_time = datetime.datetime.now()
try:
logging.info('Client {}:{}. Finished connection. Total Transfer: {:.3f} MB, Duration: {}'.format(self.client.host, self.client.port, clients[self.client].amountTransfered/1024/1024.0, str(disconnect_time - clients[self.client].connectionTime)))
except AttributeError:
logging.error('The client variable was not available. No more info.')
return
http.Request.notifyFinish(self)
http.Request.finish(self)
class StreamProtocol(http.HTTPChannel):
requestFactory = StreamHandler
class StreamFactory(http.HTTPFactory):
protocol = StreamProtocol
# Port is given by command parameter or defaults to 8800
reactor.listenTCP(port, StreamFactory())
logging.info('Listening on port {}'.format(port))
reactor.run()