forked from BSDCafe/checkmyip
Some HTTP Fixes
This commit is contained in:
parent
f3adef81e3
commit
febeb39adf
1 changed files with 24 additions and 69 deletions
93
checkmyip.py
Normal file → Executable file
93
checkmyip.py
Normal file → Executable file
|
@ -160,11 +160,11 @@ def telnet_talker(client, valdict):
|
||||||
log(j2format(j2log, valdict))
|
log(j2format(j2log, valdict))
|
||||||
client.send(j2format(j2send, valdict)) # echo
|
client.send(j2format(j2send, valdict)) # echo
|
||||||
client.close()
|
client.close()
|
||||||
#quit()
|
|
||||||
|
|
||||||
|
|
||||||
def ssh_talker(client, valdict):
|
def ssh_talker(client, valdict):
|
||||||
valdict.update({"proto": "ssh"})
|
valdict.update({"proto": "ssh"})
|
||||||
|
log(j2format(j2log, valdict))
|
||||||
t = paramiko.Transport(client, gss_kex=True)
|
t = paramiko.Transport(client, gss_kex=True)
|
||||||
t.set_gss_host(socket.getfqdn(""))
|
t.set_gss_host(socket.getfqdn(""))
|
||||||
t.load_server_moduli()
|
t.load_server_moduli()
|
||||||
|
@ -174,82 +174,37 @@ def ssh_talker(client, valdict):
|
||||||
chan = t.accept(20)
|
chan = t.accept(20)
|
||||||
if chan:
|
if chan:
|
||||||
server.event.wait(10)
|
server.event.wait(10)
|
||||||
#chan.send(j2format(j2log, valdict))
|
|
||||||
log(j2format(j2log, valdict))
|
|
||||||
chan.send('%s' % j2format(j2send, valdict))
|
chan.send('%s' % j2format(j2send, valdict))
|
||||||
chan.makefile('rU').readline().strip('\r\n')
|
chan.makefile('rU').readline().strip('\r\n')
|
||||||
chan.close()
|
chan.close()
|
||||||
#quit()
|
client.close()
|
||||||
|
|
||||||
|
|
||||||
def http_talker(client, valdict):
|
def http_talker(client, valdict):
|
||||||
def recv_all(sock):
|
valdict.update({"proto": "http"})
|
||||||
prev_timeout = sock.gettimeout()
|
log(j2format(j2log, valdict))
|
||||||
try:
|
response_body_raw = j2format(j2send, valdict)
|
||||||
sock.settimeout(0.01)
|
response_headers = {
|
||||||
rdata = []
|
'Content-Type': 'text/html; encoding=utf8',
|
||||||
while True:
|
'Content-Length': len(response_body_raw),
|
||||||
try:
|
'Connection': 'close',
|
||||||
rdata.append(sock.recv(MAX_PACKET))
|
}
|
||||||
except socket.timeout:
|
response_headers_raw = ''.join('%s: %s\n' % (k, v) for k, v in \
|
||||||
return ''.join(rdata)
|
response_headers.iteritems())
|
||||||
finally:
|
response_proto = 'HTTP/1.1'
|
||||||
sock.settimeout(prev_timeout)
|
response_status = '200'
|
||||||
def normalize_line_endings(s):
|
response_status_text = 'OK'
|
||||||
return ''.join((line + '\n') for line in s.splitlines())
|
client.send('%s %s %s' % (response_proto, response_status, response_status_text))
|
||||||
while True:
|
client.send(response_headers_raw)
|
||||||
# headers and body are divided with \n\n (or \r\n\r\n - that's why we
|
client.send('\n')
|
||||||
# normalize endings). In real application usage, you should handle
|
client.send(response_body_raw)
|
||||||
# all variations of line endings not to screw request body
|
client.close()
|
||||||
request = normalize_line_endings(recv_all(client)) # hack again
|
|
||||||
request_head, request_body = request.split('\n\n', 1)
|
|
||||||
# first line is request headline, and others are headers
|
|
||||||
request_head = request_head.splitlines()
|
|
||||||
request_headline = request_head[0]
|
|
||||||
# headers have their name up to first ': '. In real world uses, they
|
|
||||||
# could duplicate, and dict drops duplicates by default, so
|
|
||||||
# be aware of this.
|
|
||||||
request_headers = dict(x.split(': ', 1) for x in request_head[1:])
|
|
||||||
# headline has form of "POST /can/i/haz/requests HTTP/1.0"
|
|
||||||
request_method, request_uri, request_proto = request_headline.split(' ', 3)
|
|
||||||
response_body = [
|
|
||||||
'<html><body><h1>Hello, world!</h1>',
|
|
||||||
'<p>This page is in location %(request_uri)r, was requested ' % locals(),
|
|
||||||
'using %(request_method)r, and with %(request_proto)r.</p>' % locals(),
|
|
||||||
'<p>Request body is %(request_body)r</p>' % locals(),
|
|
||||||
'<p>Actual set of headers received:</p>',
|
|
||||||
'<ul>',
|
|
||||||
]
|
|
||||||
for request_header_name, request_header_value in request_headers.iteritems():
|
|
||||||
response_body.append('<li><b>%r</b> == %r</li>' % (request_header_name, \
|
|
||||||
request_header_value))
|
|
||||||
response_body.append('</ul></body></html>')
|
|
||||||
response_body_raw = ''.join(response_body)
|
|
||||||
# Clearly state that connection will be closed after this response,
|
|
||||||
# and specify length of response body
|
|
||||||
response_headers = {
|
|
||||||
'Content-Type': 'text/html; encoding=utf8',
|
|
||||||
'Content-Length': len(response_body_raw),
|
|
||||||
'Connection': 'close',
|
|
||||||
}
|
|
||||||
response_headers_raw = ''.join('%s: %s\n' % (k, v) for k, v in \
|
|
||||||
response_headers.iteritems())
|
|
||||||
# Reply as HTTP/1.1 server, saying "HTTP OK" (code 200).
|
|
||||||
response_proto = 'HTTP/1.1'
|
|
||||||
response_status = '200'
|
|
||||||
response_status_text = 'OK' # this can be random
|
|
||||||
# sending all this stuff
|
|
||||||
client.send('%s %s %s' % (response_proto, response_status, \
|
|
||||||
response_status_text))
|
|
||||||
client.send(response_headers_raw)
|
|
||||||
client.send('\n') # to separate headers from body
|
|
||||||
client.send(response_body_raw)
|
|
||||||
# and closing connection, as we stated before
|
|
||||||
client.close()
|
|
||||||
|
|
||||||
|
|
||||||
def start():
|
def start():
|
||||||
talkers = {22: ssh_talker, 23: telnet_talker, 80: telnet_talker}
|
talkers = {22: ssh_talker, 23: telnet_talker, 80: http_talker}
|
||||||
for talker in talkers:
|
for talker in talkers:
|
||||||
thread = threading.Thread(target=listener, args=(talker, talkers[talker]))
|
thread = threading.Thread(target=listener, args=(talker, talkers[talker]))
|
||||||
thread.daemon = True
|
thread.daemon = True
|
||||||
|
|
Loading…
Reference in a new issue