STarted to add website functionality

master
haddaway 2012-10-21 11:23:58 -07:00
parent 12959f2641
commit 7d6672f792
2 changed files with 74 additions and 18 deletions

View File

@ -1,6 +1,21 @@
import os
import httplib
from threading import Thread
USER_NAME_SIZE = 9
def get_web_site(visual, site):
''' This is a threaded message to query a website and create message to pass back '''
try:
connection = httplib.HTTPConnection(site)
connection.request("GET", "/")
result = connection.getresponse().read()
visual.message_quque.append(result)
except Exception:
pass
class ToorChatProtocol():
''' This is a class to allow for easy of use with anything to do with messaging '''
def __init__(self, device):
@ -8,27 +23,40 @@ class ToorChatProtocol():
self.PACKET_END = "\xFF\xBE\xEF\xFF"
self.device = device
def send_message(self, message = "", user = None):
def send_chat_message(self, message = "", user = None):
''' This is used to send a simple message over the toorchat protocol '''
msg = ToorChatMessage(message, user)
msg = ToorMessage(message, user)
self.device.RFxmit(msg.to_string())
return msg
def send_web_request(self, site = ""):
'''This is used to attempt to get anyone who is registered as a server to load a website on your behalf '''
if site != "":
request = ToorMessage(site, None, ToorChatProtocol.get_web_request_type())
self.device.RFxmit(request.to_string())
@classmethod
def parse_message(cls, raw_message):
message = ToorChatMessage()
message = ToorMessage()
message.raw = raw_message
start_index = raw_message.find(ToorChatProtocol.get_packet_start())
end_index = raw_message.find(ToorChatProtocol.get_packet_end())
if start_index == -1 or end_index == -1:
return None
message.start = raw_message[start_index:start_index + 4]
message.xid = raw_message[start_index + 4: start_index + 12]
message.user = raw_message[start_index + 12: start_index + 21]
message.data = raw_message[start_index + 21: end_index]
message.type = raw_message[start_index + 4: start_index + 5]
message.xid = raw_message[start_index + 5: start_index + 13]
message.user = raw_message[start_index + 13: start_index + 22]
message.data = raw_message[start_index + 22: end_index]
message.end = raw_message[end_index: end_index+4]
return message
@classmethod
def get_web_messages(cls, site, visual):
'''This is query the webpage and make thoose messages '''
thread = Thread(target=get_web_site, args=(visual, site))
thread.start()
@classmethod
def get_packet_start(cls):
return "\xFF\xDE\xAD\xFF"
@ -37,13 +65,22 @@ class ToorChatProtocol():
def get_packet_end(cls):
return "\xFF\xBE\xEF\xFF"
class ToorChatMessage():
@classmethod
def get_chat_type(cls):
return "1"
@classmethod
def get_web_request_type(cls):
return "2"
class ToorMessage():
''' This is a simple Message object wrapper to make things cleaner '''
def __init__(self, message = "", user = None):
def __init__(self, message = "", user = None, type=ToorChatProtocol.get_chat_type()):
self.raw = None
self.start = ToorChatProtocol.get_packet_start()
self.xid = self.get_random_xid()
self.type = type
if user != None:
self.user = user
if len(self.user) < USER_NAME_SIZE:
@ -54,10 +91,10 @@ class ToorChatMessage():
self.end = ToorChatProtocol.get_packet_end()
def get_random_xid(self):
return "44444444"
return os.urandom(8)
def __str__(self):
return self.start + self.xid + self.user + self.data + self.end
return self.start +self.type+ self.xid + self.user + self.data + self.end
def to_string(self):
return self.__str__()
return self.__str__()

View File

@ -16,9 +16,15 @@ def thread_run(visual):
while not visual.exit:
try:
msg, timestamp = visual.badge.RFrecv()
toor_message = ToorChatProtocol.parse_message(msg)
toor_message = ToorChatProtocol.parse_chat_message(msg)
if toor_message != None:
visual.message_queue.append(toor_message)
if toor_message.type == ToorChatProtocol.get_chat_type():
visual.message_queue.append(toor_message)
if toor_message.type == ToorChatProtocol.get_web_request_type():
#If we are registered as a server, lets typ to make that request
if visual.server:
ToorChatProtocol.get_web_messages(toor_message.data, visual)
except ChipconUsbTimeoutException:
pass
@ -26,8 +32,6 @@ class Visualizer():
def __init__(self):
self.screen = curses.initscr()
# curses.noecho()
# curses.cbreak()
self.screen.nodelay(1)
self.badge = RfCat(idx=0)
self.badge.setModeRX()
@ -36,6 +40,10 @@ class Visualizer():
self.user = None
#This when set to True will kill the thread
self.exit = False
self.server = False
def set_server(self):
self.server = True
def start(self):
self.__start_recv_thread__()
@ -54,7 +62,7 @@ class Visualizer():
self.last_message_index = 0
while True:
self.screen_max_y, self.screen_max_x = self.screen.getmaxyx()
self.screen.addstr(0, 1, "[S] Send Message [U] Set User Name")
self.screen.addstr(0, 1, "[S] Send Message [U] Set User Name [W] Load Website")
self.__add_message_to_screen__()
entry = self.screen.getch()
if entry == curses.KEY_RESIZE:
@ -62,7 +70,7 @@ class Visualizer():
if entry == ord('s'):
self.screen.nodelay(0)
user_input = self.screen.getstr(1, 1, 60)
old_message = self.protocol.send_message(user_input, self.user)
old_message = self.protocol.send_chat_message(user_input, self.user)
self.message_queue.append(old_message)
self.screen.nodelay(1)
self.screen.addstr(1,1," "*(self.screen_max_x-3))
@ -72,6 +80,12 @@ class Visualizer():
self.user = user_input[:USER_NAME_SIZE]
self.screen.nodelay(1)
self.screen.addstr(1,1," "*(self.screen_max_x-3))
if entry == ord('w'):
self.screen.nodelay(0)
user_input = self.screen.getstr(1, 1, 60)
self.protocol.send_web_request(user_input)
self.screen.nodelay(1)
self.screen.addstr(1,1," "*(self.screen_max_x-3))
except KeyboardInterrupt:
self.exit = True
@ -99,4 +113,9 @@ class Visualizer():
if __name__ == '__main__':
visual = Visualizer()
visual.start()
if len(sys.argv) > 1:
if sys.argv[1] == "-s":
visual.set_server()
visual.start()