def _prepare_render_args(self, arguments, client_form=None, ca=None): """ Prepare the arguments for the template. :param arguments: target variable :type arguments: dict """ arguments['PLUGIN_NAME'] = OpenvpnPlugin.PLUGIN_NAME arguments['PLUGIN_STYLES'] = OpenvpnPlugin.PLUGIN_STYLES arguments[ 'PLUGIN_STATIC_SCRIPTS'] = OpenvpnPlugin.PLUGIN_STATIC_SCRIPTS arguments[ 'PLUGIN_DYNAMIC_SCRIPTS'] = OpenvpnPlugin.PLUGIN_DYNAMIC_SCRIPTS status = current_state.backend.perform("openvpn", "get_status") arguments['ca_status'] = status["status"] client_certs = status["clients"] # translate certificat statuses for cert in client_certs: cert["status_msg"] = OpenvpnConfigHandler.TRANSLATION_MAP.get( cert["status"], cert["status"]) arguments['client_certs'] = client_certs arguments['config_form'] = self.form arguments[ 'client_form'] = client_form if client_form else self.get_client_form( ) arguments['address_form'] = self.get_address_form( {"server-address": self.backend_data["server_hostname"]}) # prepare current settings to display current = {} if self.form.data['enabled']: current['network'] = "%s/%d" % ( self.backend_data["network"], mask_to_prefix_4(self.backend_data["network_netmask"])) current['device'] = self.backend_data["device"] current['port'] = self.backend_data["port"] current['default_route'] = self.backend_data["route_all"] if self.backend_data["routes"]: current['route'] = "%s/%d" % ( self.backend_data["routes"][0]["network"], mask_to_prefix_4( self.backend_data["routes"][0]["netmask"])) else: current['route'] = "???" arguments['current'] = current
def network_preprocessor(data): """ :param data: Data obtained from the query :type data: nuci.modules.base.Data """ address_node = data.find_child(address_path) address = address_node.value if address_node else default_network netmask_node = data.find_child(netmask_path) netmask = netmask_node.value if netmask_node else default_netmask try: prefix = addresses.mask_to_prefix_4(netmask) except ValueError: netmask = default_netmask prefix = addresses.mask_to_prefix_4(default_netmask) try: address = addresses.normalize_subnet_4(address, netmask) except ValueError: address = addresses.normalize_subnet_4(default_network, netmask) return "%s/%d" % (address, prefix)
# GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import logging from foris.utils.addresses import mask_to_prefix_4 logger = logging.getLogger(__name__) DEFAULT_GUEST_IP = "10.111.222.1" DEFAULT_GUEST_NETWORK = "10.111.222.0" DEFAULT_GUEST_MASK = "255.255.255.0" DEFAULT_GUEST_PREFIX = mask_to_prefix_4(DEFAULT_GUEST_MASK) class BaseConfigHandler(object): def __init__(self, data=None): self.data = data self.__form_cache = None @property def form(self): if self.__form_cache is None: self.__form_cache = self.get_form() return self.__form_cache def get_form(self): """Get form. MUST be a single-section form.
from foris.nuci import client from foris.nuci.filters import create_config_filter, wifi_filter from foris.nuci.preprocessors import guest_network_enabled, generate_network_preprocessor from foris.nuci.modules.uci_raw import Uci, Config, Section, Option, List, Value, parse_uci_bool from foris.utils.addresses import ( ip_num_to_str_4, ip_str_to_num_4, prefix_to_mask_4, mask_to_prefix_4 ) from foris.utils.routing import reverse from .base import BaseConfigHandler DEFAULT_GUEST_NETWORK = "10.111.222.0" DEFAULT_GUEST_MASK = "255.255.255.0" DEFAULT_GUEST_PREFIX = mask_to_prefix_4(DEFAULT_GUEST_MASK) class LanHandler(BaseConfigHandler): userfriendly_title = gettext("LAN") def get_form(self): lan_form = fapi.ForisForm( "lan", self.data, filter=create_config_filter("dhcp", "network", "firewall", "sqm")) lan_main = lan_form.add_section( name="set_lan", title=_(self.userfriendly_title), description=_("This section contains settings for the local network (LAN). The provided" " defaults are suitable for most networks. <br><strong>Note:</strong> If " "you change the router IP address, all computers in LAN, probably " "including the one you are using now, will need to obtain a <strong>new "
def get_form(self): self.backend_data = current_state.backend.perform( "openvpn", "get_settings") data = { "enabled": self.backend_data["enabled"], "network": "%s/%d" % (self.backend_data["network"], mask_to_prefix_4(self.backend_data["network_netmask"])), "default_route": self.backend_data["route_all"], "dns": self.backend_data["use_dns"], "ipv6": self.backend_data["ipv6"], "protocol": self.backend_data["protocol"], } if self.data: # Update from post data.update(self.data) form = ForisForm("openvpn-configuration", data) config_section = form.add_section(name="config", title=_(self.userfriendly_title)) config_section.add_field( Checkbox, name="enabled", label=_("Configuration enabled"), ) config_section.add_field( Checkbox, name="ipv6", label=_("Listen on IPv6"), hint= _("This option enables openvpn server to listen on IPv6 address. " "It could be beneficial for users who does not have a public IPv4 address" " and want to use openvpn server."), ) config_section.add_field( Dropdown, name="protocol", label=_("Protocol"), args=[("udp", "UDP"), ("tcp", "TCP")], hint=_( "Choose a protocol which will be used when the clients are connecting to the " "server. "), ) config_section.add_field( Textbox, name="network", label=_("OpenVPN network"), validators=[IPv4Prefix()], hint=_( "This network should be different than any network directly " "reachable from the router and the clients."), ) config_section.add_field( Checkbox, name="default_route", label=_("All traffic through vpn"), hint=_("After enabling this option all traffic from your client " "will be routed through the vpn."), ) config_section.add_field( Checkbox, name="dns", label=_("Use DNS from vpn"), hint=_("After enabling this option your client should start " "to use DNS server on your router."), ) def form_callback(data): msg = {"enabled": data['enabled']} if msg["enabled"]: msg["network"], prefix = data['network'].split("/") mask = prefix_to_mask_4(int(prefix)) msg["network_netmask"] = mask msg["route_all"] = data['default_route'] msg["use_dns"] = data['dns'] msg["ipv6"] = data['ipv6'] msg["protocol"] = data['protocol'] res = current_state.backend.perform("openvpn", "update_settings", msg) if res["result"]: messages.success( _('OpenVPN server configuration was successfully %s.') % (_('enabled') if msg["enabled"] else _('disabled'))) else: messages.error( _('Failed to %s OpenVPN server configuration.') % (_('enable') if msg["enabled"] else _('disable'))) return "none", None form.add_callback(form_callback) return form