Esempio n. 1
0
 def ipv4(self):
     """Returns value of IPv4 property."""
     ipv4 = self.settings['server']['ipv4']
     if ipv4 is None:
         value = _helper.fetch_external_ipv4()
         if _helper.is_valid_ipv4(value):
             ipv4 = value
     return ipv4
Esempio n. 2
0
    def setup(self):
        # pylint: disable=R0912
        # pylint: disable=R0914
        # pylint: disable=R0915
        # pylint: disable=W0125
        """Set up settings for Simplified OpenVPN on current system."""
        config = dict()
        config['server'] = dict()

        # Ask value for server_dir property.
        suggestion_source = self.sovpn_config_file if self.loaded else None
        suggestion = self.get_suggestion('server_dir', suggestion_source)

        while self.server_dir is None:
            prompt = _prompt.get('server_dir', suggestion)
            server_dir = input(prompt)
            if server_dir.strip() == '':
                server_dir = suggestion
            self.server_dir = server_dir

        config['server']['server_dir'] = self.server_dir

        # Ask value for easy_rsa_dir property.
        suggestion = self.server_dir + 'easy-rsa'

        while self.easy_rsa_dir is None:
            prompt = _prompt.get('easy_rsa_dir', suggestion)
            easy_rsa_dir = input(prompt)
            if easy_rsa_dir.strip() == '':
                easy_rsa_dir = suggestion
            self.easy_rsa_dir = easy_rsa_dir

        config['server']['easy_rsa_dir'] = self.easy_rsa_dir

        # Ask value for easy_rsa_ver property.
        suggestion_source = self.sovpn_config_file if self.loaded else None
        suggestion = self.get_suggestion('easy_rsa_ver', suggestion_source)

        while self.easy_rsa_ver is None:
            prompt = _prompt.get('easy_rsa_ver', suggestion)
            easy_rsa_ver = input(prompt)
            if easy_rsa_ver.strip() == '':
                easy_rsa_ver = suggestion
            self.easy_rsa_ver = easy_rsa_ver

        config['server']['easy_rsa_ver'] = self.easy_rsa_ver

        # Ask value for clients_dir property.
        suggestion_source = self.sovpn_config_file if self.loaded else None
        suggestion = self.get_suggestion('clients_dir', suggestion_source)

        while self.clients_dir is None:
            prompt = _prompt.get('clients_dir', suggestion)
            clients_dir = input(prompt)
            if clients_dir.strip() == '':
                clients_dir = suggestion
            self.clients_dir = clients_dir

        config['server']['clients_dir'] = self.clients_dir

        # Ask value for hostname property.
        suggestion_source = self.sovpn_config_file if self.loaded else None
        suggestion = self.get_suggestion('hostname', suggestion_source)

        if not self.hostname and self.sovpn_share_url and not suggestion:
            suggestion = '-'

        while self.hostname is None:
            prompt = _prompt.get('hostname', suggestion)
            hostname = input(prompt).strip()
            if hostname == '' and suggestion and suggestion != '-':
                hostname = suggestion
            elif hostname == '-' or (hostname == '' and suggestion == '-'):
                hostname = False
            self.hostname = hostname

        # If hostname is changes then in most cases we also want to change sharing URL.
        if suggestion != self.hostname and self.sovpn_share_url:
            self.sovpn_share_url = None

        config['server']['hostname'] = self.hostname

        # Ask value for port property.
        suggestion_source = self.sovpn_config_file if self.loaded else None
        suggestion = self.get_suggestion('port', suggestion_source)

        while self.port is None:
            prompt = _prompt.get('port', suggestion)
            port = input(prompt)
            if port.strip() == '':
                port = suggestion
            self.port = port

        config['server']['port'] = self.port

        # Ask value for protocol property.
        suggestion_source = self.sovpn_config_file if self.loaded else None
        suggestion = self.get_suggestion('protocol', suggestion_source)

        while self.protocol is None:
            prompt = _prompt.get('protocol', suggestion)
            protocol = input(prompt)
            if protocol.strip() == '':
                protocol = suggestion
            self.protocol = protocol

        config['server']['protocol'] = self.protocol

        # Ask if management interface will be used.
        suggestion_source = self.sovpn_config_file if self.loaded else None
        suggestion = self.get_suggestion('mgmt_used', suggestion_source)

        while self.mgmt_used is None:
            prompt = _prompt.get('mgmt_used', suggestion)
            mgmt_used = input(prompt)
            if mgmt_used.strip() == '':
                mgmt_used = suggestion
            self.mgmt_used = mgmt_used

        config['server']['mgmt_used'] = self.mgmt_used

        if self.mgmt_used:
            # Ask value for mgmt_address property.
            suggestion_source = self.sovpn_config_file if self.loaded else None
            suggestion = self.get_suggestion('mgmt_address', suggestion_source)

            while self.mgmt_address is None:
                prompt = _prompt.get('mgmt_address', suggestion)
                mgmt_address = input(prompt)
                if mgmt_address.strip() == '':
                    mgmt_address = suggestion
                self.mgmt_address = mgmt_address

            config['server']['mgmt_address'] = self.mgmt_address

            # Ask value for mgmt_port property.
            suggestion_source = self.sovpn_config_file if self.loaded else None
            suggestion = self.get_suggestion('mgmt_port', suggestion_source)

            while self.mgmt_port is None:
                prompt = _prompt.get('mgmt_port', suggestion)
                mgmt_port = input(prompt)
                if mgmt_port.strip() == '':
                    mgmt_port = suggestion
                self.mgmt_port = mgmt_port

            config['server']['mgmt_port'] = self.mgmt_port

        # Ask value for sovpn_share_salt property.
        suggestion_source = self.sovpn_config_file if self.loaded else None
        suggestion = self.get_suggestion('sovpn_share_salt', suggestion_source)

        while self.sovpn_share_salt is None:
            prompt = _prompt.get('sovpn_share_salt', suggestion)
            sovpn_share_salt = input(prompt)
            if sovpn_share_salt.strip() == '':
                sovpn_share_salt = suggestion
            self.sovpn_share_salt = sovpn_share_salt

        config['server']['sovpn_share_salt'] = self.sovpn_share_salt

        # If you changed share salt, then you need to rotate hashes for everybody.
        if self.loaded and suggestion != self.sovpn_share_salt:
            self.needs_rotation = True

        # Ask value for sovpn_share_address property.
        suggestion_source = self.sovpn_config_file if self.loaded else None
        suggestion = self.get_suggestion('sovpn_share_address',
                                         suggestion_source)

        while self.sovpn_share_address is None:
            prompt = _prompt.get('sovpn_share_address', suggestion)
            sovpn_share_address = input(prompt)
            if sovpn_share_address.strip() == '':
                sovpn_share_address = suggestion
            self.sovpn_share_address = sovpn_share_address

        config['server']['sovpn_share_address'] = self.sovpn_share_address

        # Ask value for sovpn_share_port property.
        suggestion_source = self.sovpn_config_file if self.loaded else None
        suggestion = self.get_suggestion('sovpn_share_port', suggestion_source)

        while self.sovpn_share_port is None:
            prompt = _prompt.get('sovpn_share_port', suggestion)
            sovpn_share_port = input(prompt)
            if sovpn_share_port.strip() == '':
                sovpn_share_port = suggestion

            # Make sure server and sharing port are different.
            if self.protocol == 'tcp' and self.port == sovpn_share_port:
                print('> Port ' + str(sovpn_share_port) +
                      '/TCP is already used by server.')
                sovpn_share_port = None
            self.sovpn_share_port = sovpn_share_port

        config['server']['sovpn_share_port'] = self.sovpn_share_port

        # Ask value for sovpn_share_url property.
        if self.hostname:
            if self.sovpn_share_url:
                suggestion = self.sovpn_share_url
                self.sovpn_share_url = None
            elif not self.hostname and self.hostname is False:
                suggestion = '-'
            else:
                if self.sovpn_share_port == 443:
                    suggestion = 'https://'
                else:
                    suggestion = 'http://'

                suggestion += self.hostname

                if self.sovpn_share_port != 443 and self.sovpn_share_port != 80:
                    suggestion += ':' + str(self.sovpn_share_port)

                suggestion += '/'

            while self.sovpn_share_url is None:
                prompt = _prompt.get('sovpn_share_url', suggestion)
                sovpn_share_url = input(prompt)
                if sovpn_share_url.strip() == '':
                    sovpn_share_url = suggestion
                self.sovpn_share_url = sovpn_share_url

            config['server']['sovpn_share_url'] = self.sovpn_share_url
        else:
            ipv4 = _helper.fetch_external_ipv4()
            if _helper.is_valid_ipv4(ipv4):
                self.sovpn_share_url = 'http://' + ipv4 + ':' + str(
                    self.sovpn_share_port) + '/'
                config['server']['sovpn_share_url'] = self.sovpn_share_url

        # Ask value for sovpn_config_file property.
        suggestion = self.server_dir + 'sovpn.json'

        while self.sovpn_config_file is None:
            prompt = _prompt.get('sovpn_config_file', suggestion)
            sovpn_config_file = input(prompt)
            if sovpn_config_file.strip() == '':
                sovpn_config_file = suggestion
            self.sovpn_config_file = sovpn_config_file

        # Write sovpn's config file path to pointer file.
        with open(self.sovpn_config_pointer, 'w') as config_path_file:
            config_path_file.write(self.sovpn_config_file + "\n")

        # Write config values to file.
        with open(self.sovpn_config_file, 'w') as config_file:
            config_file.write(json.dumps(config) + "\n")

        # Copy client's template to server's directory.
        copyfile(self.client_template_path,
                 self.server_dir + 'client.mustache')