示例#1
0
 def startService(self):
     self.x_alice_config = ClientConfig()
     self.load_config(config_filename)
     self.x_alice = xAlice(self.x_alice_config)
     self.packet_data = packet_data()
     self.packet_data.init_visible_flows()
     self.client_status = 'running'
     print "Starting Switzerland client service..."
示例#2
0
class WebGUI():
                
    def __init__(self):
        self.random_key = base64.b64encode(os.urandom(64))
        self.web_app_config = dict()
        self.web_app_config['save_window'] = [60 * 60, 
            "Save window", "Number of seconds to save"]
        self.web_app_config['refresh_interval'] = [20, 
            "Refresh interval", "Number of seconds between refresh"]
        self.web_app_config['allow_wireshark'] = [True, 
            "Allow Wireshark", "Allow the client to launch Wireshark"]
        self.urls = (
            '/', 'index', 
            '', 'index',
            '/ajax_server', 'ajax_server',
            '/ajax_server/', 'ajax_server',
            '/config', 'config')
        self.client_status = 'stopped'
        self.next_immutable_config = dict()
        self.next_tweakable_config = dict()
        for opt, info in alice_options.items():
            default, type, mutable, visible = info
            if visible:
                if mutable:
                    self.next_tweakable_config[opt] = default
                else:
                    self.next_immutable_config[opt] = default
        
        
    def startService(self):
        self.x_alice_config = ClientConfig()
        self.load_config(config_filename)
        self.x_alice = xAlice(self.x_alice_config)
        self.packet_data = packet_data()
        self.packet_data.init_visible_flows()
        self.client_status = 'running'
        print "Starting Switzerland client service..."
        
    def stopService(self):
        self.x_alice_config = None
        self.x_alice = None
        self.packet_data = None
        self.client_status = 'stopped'
        print "Stopping Switzerland client service..."
        
    def main(self):

        self.startService()
        self.app = web.application(self.urls, globals())
 
        if len(sys.argv) > 1:
            alice_opts = sys.argv
            sys.argv[1:] = []
        # Use 127.0.0.1 instead of 0.0.0.0 to make accessible outside of localhost
        sys.argv.insert(1, web_py_server + ":" + web_py_port)
        self.app.run()

    def load_config(self, filename, override_cmdline=False):
        if os.path.isfile(filename):
            print "Attempting to load configuration file."
            print "If this causes problems, delete file config/config.xml to use defaults."
            config_dom = parse(filename)
            
            # Immutable
            for (option_name, option_type) in self.x_alice_config.immutable_options():
                temp = getText(config_dom.getElementsByTagName(option_name)[0].childNodes).strip()
                print "Setting " + option_name + ": " + temp
                if temp is not None and len(temp) > 0:
                    if option_type == int:
                        temp = int(temp)
                    if option_type == float:
                        temp = float(temp)
                    if option_type == bool:
                        temp = str2bool(temp)
                    self.x_alice_config.set_option(option_name, temp)
                
            # Mutable
            for (option_name, option_type) in self.x_alice_config.tweakable_options():
                temp = getText(config_dom.getElementsByTagName(option_name)[0].childNodes).strip()
                print "Setting " + option_name + ": " + temp
                if temp is not None and len(temp) > 0:
                    if option_type == int:
                        temp = int(temp)
                    if option_type == float:
                        temp = float(temp)
                    if option_type == bool:
                        temp = str2bool(temp)
                    self.x_alice_config.set_option(option_name, temp)

            # Web
            for key in self.web_app_config.keys():
                temp = getText(config_dom.getElementsByTagName(key)[0].childNodes).strip()
                print "Setting " + key + ": " + temp
                if key in ('allow_wireshark'):
                    temp = str2bool(temp)               
                self.web_app_config[key][0] = int(temp)
        
            # optlist, args = getopt.gnu_getopt(sys.argv[1:], short_opt_list, 
            #    long_opt_list)

            # newArgList = []
            # newArgList.append(sys.argv[0])

            # Workaround the fact that we call the Switzerland command line client
            # and must transfer some information as command line options
            #===================================================================
            # if override_cmdline:
            #    for option, argument in optlist:
            #        if option in ("-s", "--server", "-i", "--interface"):
            #            pass # Effectively delete old option
            #        else:
            #            newArgList.append(option)
            #            if argument is not None and len(argument) > 0:
            #                newArgList.append(argument)
            #        newArgList.append("--server")
            #        newArgList.append(self.x_alice_config.get_option('host'))
            #        newArgList.append("--interface")
            #        newArgList.append(self.x_alice_config.get_option('interface'))
            # else:
            #    for option, argument in optlist:
            #        newArgList.append(option)
            #        if argument is not None and len(argument) > 0:
            #            newArgList.append(argument)
            #    if not optlist_contains(optlist, ("-s", "--server")):
            #        print "appending server" + self.x_alice_config.get_option('host')
            #        newArgList.append("--server")
            #        newArgList.append(self.x_alice_config.get_option('host'))
            #    if not optlist_contains(optlist, ("-i", "--interface")):
            #        print "appending interface" + self.x_alice_config.get_option('interface')
            #        newArgList.append("--interface")
            #        newArgList.append(self.x_alice_config.get_option('interface'))
            #    
            # sys.argv = newArgList
            #===================================================================
            print "Configuration file loaded."
        else:
            print "No configuration file.  Using default values."
                
    def save_config(self, filename):
        impl = getDOMImplementation()
        new_config = impl.createDocument(None, "configuration", None)
        top_element = new_config.documentElement
        
        new_section = make_element(new_config, 
                              "tweakable_options", "")
        top_element.appendChild(new_section)
        for option_name in singleton_webgui.next_tweakable_config.keys():
            new_child = make_element(new_config, 
                                  option_name ,
                                  singleton_webgui.next_tweakable_config[option_name])
            new_section.appendChild(new_child)
            
        new_section = make_element(new_config, 
                              "immutable_options", "")
        top_element.appendChild(new_section)
        for option_name in singleton_webgui.next_immutable_config.keys():
            new_child = make_element(new_config, 
                                  option_name ,
                                  singleton_webgui.next_immutable_config[option_name])
            new_section.appendChild(new_child)
            
        new_section = make_element(new_config, 
                              "web_application", "")
        top_element.appendChild(new_section)  
        for option_name in self.web_app_config.keys():
            config_value = self.web_app_config.get(option_name)
            config_value = config_value[0]
            new_section.appendChild(make_element(new_config, 
                                  option_name ,
                                  str(config_value)))
            
        out_file = open(filename,"w")
        new_config.writexml(out_file, '', '    ', "\n", "ISO-8859-1")