Added browser detection
This commit is contained in:
parent
9b754e8794
commit
f816747068
1 changed files with 29 additions and 13 deletions
42
checkmyip.py
42
checkmyip.py
|
@ -161,18 +161,18 @@ def listener(port, talker):
|
||||||
|
|
||||||
|
|
||||||
##### Telnet responder method. Is run in own thread for each telnet query #####
|
##### Telnet responder method. Is run in own thread for each telnet query #####
|
||||||
def telnet_talker(client, valdict):
|
def telnet_talker(client, valdict, proto="telnet"):
|
||||||
valdict.update({"proto": "telnet"}) # Add the protocol to the value dict
|
valdict.update({"proto": proto}) # Add the protocol to the value dict
|
||||||
log(j2format(j2log, valdict)) # Log the query to the console and logfile
|
log(j2format(j2log, valdict)) # Log the query to the console and logfile
|
||||||
client.send(j2format(j2send, valdict)) # Send the query response
|
client.send(j2format(j2send, valdict)) # Send the query response
|
||||||
client.close() # Close the channel
|
client.close() # Close the channel
|
||||||
|
|
||||||
|
|
||||||
##### SSH responder method. Gets run in own thread for each SSH query #####
|
##### SSH responder method. Gets run in own thread for each SSH query #####
|
||||||
def ssh_talker(client, valdict):
|
def ssh_talker(client, valdict, proto="ssh"):
|
||||||
def makefile(): # A hack to make Cisco SSH sessions work properly
|
def makefile(): # A hack to make Cisco SSH sessions work properly
|
||||||
chan.makefile('rU').readline().strip('\r\n')
|
chan.makefile('rU').readline().strip('\r\n')
|
||||||
valdict.update({"proto": "ssh"})
|
valdict.update({"proto": proto})
|
||||||
log(j2format(j2log, valdict))
|
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(""))
|
||||||
|
@ -192,27 +192,43 @@ def ssh_talker(client, valdict):
|
||||||
|
|
||||||
|
|
||||||
##### HTTP responder method. Gets run in own thread for each HTTP query #####
|
##### HTTP responder method. Gets run in own thread for each HTTP query #####
|
||||||
def http_talker(client, valdict):
|
##### Automatically detects if client is a browser or a telnet client #####
|
||||||
valdict.update({"proto": "http"})
|
def http_talker(client, valdict, proto="http"):
|
||||||
log(j2format(j2log, valdict))
|
time.sleep(.1) # Sleep to allow the client to send some data
|
||||||
response_body_raw = j2format(j2send, valdict)
|
client.setblocking(0) # Set the socket recv as non-blocking
|
||||||
response_headers_raw = """HTTP/1.1 200 OK
|
browser = False # Is the client using a browser?
|
||||||
|
try: # client.recv() will raise an error if the buffer is empty
|
||||||
|
data = client.recv(2048) # Recieve data from the buffer (if any)
|
||||||
|
print(data) # Print to stdout
|
||||||
|
browser = True # Set client browser to True
|
||||||
|
except: # If buffer was empty, then like a telnet client on TCP80
|
||||||
|
browser = False # Set client browser to False
|
||||||
|
if not browser: # If the client is not a browser
|
||||||
|
telnet_talker(client, valdict, "http-telnet") # Hand to telnet_talker
|
||||||
|
else: # If client is a browser
|
||||||
|
# Proceed with standard HTTP response (with headers)
|
||||||
|
log(j2format(j2log, valdict))
|
||||||
|
valdict.update({"proto": proto})
|
||||||
|
response_body_raw = j2format(j2send, valdict)
|
||||||
|
response_headers_raw = """HTTP/1.1 200 OK
|
||||||
Content-Length: %s
|
Content-Length: %s
|
||||||
Content-Type: application/json; encoding=utf8
|
Content-Type: application/json; encoding=utf8
|
||||||
Connection: close""" % str(len(response_body_raw)) # Response with headers
|
Connection: close""" % str(len(response_body_raw)) # Response with headers
|
||||||
client.send(response_headers_raw + "\n\n" + response_body_raw)
|
client.send(response_headers_raw + "\n\n" + response_body_raw)
|
||||||
client.close()
|
client.close()
|
||||||
|
|
||||||
|
|
||||||
##### Server startup method. Starts a listener thread for each TCP port #####
|
##### Server startup method. Starts a listener thread for each TCP port #####
|
||||||
def start():
|
def start():
|
||||||
talkers = {22: ssh_talker, 23: telnet_talker, 80: http_talker}
|
talkers = {22: ssh_talker, 23: telnet_talker,
|
||||||
|
80: http_talker} # Three listeners on different ports
|
||||||
for talker in talkers:
|
for talker in talkers:
|
||||||
|
# Launch a thread for each listener
|
||||||
thread = threading.Thread(target=listener,
|
thread = threading.Thread(target=listener,
|
||||||
args=(talker, talkers[talker]))
|
args=(talker, talkers[talker]))
|
||||||
thread.daemon = True
|
thread.daemon = True
|
||||||
thread.start()
|
thread.start()
|
||||||
while True:
|
while True: # While loop to allow a CTRL-C interrupt when interactive
|
||||||
try:
|
try:
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
|
|
Loading…
Reference in a new issue