Пример #1
0
    def add_nat_rule(self, rule_type, original_ip, original_port, translated_ip, translated_port, protocol, interface=None):
        gatewayInterface = self._select_gateway_interface(interface)
        if not gatewayInterface:
            return
        natRules = self.get_nat_rules()

        maxId = 0
        minId = 65537
        for natRule in natRules:
            ruleId = natRule.get_Id()
            if ruleId > maxId:
                maxId = ruleId
            if ruleId < minId:
                minId = ruleId
        if maxId == 0:
            maxId = minId - 1

        port_changed = True
        original_port_modified = original_port
        while port_changed and len(natRules) > 0:
            for natRule in natRules:
                port_changed = False
                if rule_type == natRule.get_RuleType():
                    gatewayNatRule = natRule.get_GatewayNatRule()
                    if original_ip == gatewayNatRule.get_OriginalIp() and \
                       original_port_modified == gatewayNatRule.get_OriginalPort():
                        original_port_modified = str(int(original_port_modified) + 1)
                        port_changed = True
                        break

        rule = NatRuleType()
        rule.set_RuleType(rule_type)
        rule.set_IsEnabled(True)
        # note, the new id has to be greater than the last
        # apparently the same id can be reused by two different rules, but use a new one
        rule.set_Id(maxId + 1)
        gatewayRule = GatewayNatRuleType()
        gatewayInterfaceReference = ReferenceType()
        gatewayInterfaceReference.set_href(gatewayInterface.get_Network().get_href())
        gatewayInterfaceReference.set_type(gatewayInterface.get_Network().get_type())
        gatewayInterfaceReference.set_name(gatewayInterface.get_Network().get_name())
        gatewayRule.set_Interface(gatewayInterfaceReference)
        gatewayRule.set_OriginalIp(original_ip)
        gatewayRule.set_OriginalPort(original_port_modified)
        gatewayRule.set_TranslatedIp(translated_ip)
        gatewayRule.set_TranslatedPort(translated_port)
        gatewayRule.set_Protocol(protocol)
        rule.set_GatewayNatRule(gatewayRule)
        natRules.append(rule)
        natService = None
        edgeGatewayServiceConfiguration = self.me.get_Configuration().get_EdgeGatewayServiceConfiguration()
        natServices = filter(lambda service: service.__class__.__name__ == "NatServiceType", edgeGatewayServiceConfiguration.get_NetworkService())
        if len(natServices) > 0:
            natService = natServices[0]
            natService.set_NatRule(natRules)
        else:
            natService = NatServiceType(IsEnabled=True)
            natService.original_tagname_ = 'NatService'
            natService.set_NatRule(natRules)
            edgeGatewayServiceConfiguration.get_NetworkService().append(natService)
Пример #2
0
 def del_nat_rule(self,
                  rule_type,
                  original_ip,
                  original_port,
                  translated_ip,
                  translated_port,
                  protocol,
                  interface=None):
     gatewayInterface = self._select_gateway_interface(interface)
     if not gatewayInterface:
         return
     if not interface:
         interface = gatewayInterface.get_Network().get_name()
     interface = interface.lower()
     natRules = self.get_nat_rules()
     newRules = []
     found_rule = False
     for natRule in natRules:
         if rule_type == natRule.get_RuleType():
             gatewayNatRule = natRule.get_GatewayNatRule()
             gateway_interface_name = gatewayNatRule.get_Interface(
             ).get_name().lower()
             gateway_original_ip = gatewayNatRule.get_OriginalIp(
             ) if gatewayNatRule.get_OriginalIp() else 'any'
             gateway_original_port = gatewayNatRule.get_OriginalPort(
             ) if gatewayNatRule.get_OriginalPort() else 'any'
             gateway_translated_ip = gatewayNatRule.get_TranslatedIp(
             ) if gatewayNatRule.get_TranslatedIp() else 'any'
             gateway_translated_port = gatewayNatRule.get_TranslatedPort(
             ) if gatewayNatRule.get_TranslatedPort() else 'any'
             gateway_protocol = gatewayNatRule.get_Protocol(
             ) if gatewayNatRule.get_Protocol() else 'any'
             if original_ip == gateway_original_ip and \
                original_port == gateway_original_port and \
                translated_ip == gateway_translated_ip and \
                translated_port == gateway_translated_port and \
                protocol == gateway_protocol and \
                interface == gateway_interface_name:
                 found_rule = True
             else:
                 newRules.append(natRule)
         else:
             newRules.append(natRule)
     if found_rule:
         natService = None
         edgeGatewayServiceConfiguration = self.me.get_Configuration(
         ).get_EdgeGatewayServiceConfiguration()
         natServices = filter(
             lambda service: service.__class__.__name__ == "NatServiceType",
             edgeGatewayServiceConfiguration.get_NetworkService())
         if len(natServices) > 0:
             natService = natServices[0]
             natService.set_NatRule(newRules)
         else:
             natService = NatServiceType()
             natService.set_NatRule(newRules)
             edgeGatewayServiceConfiguration.get_NetworkService().append(
                 natService)
         return True
Пример #3
0
 def del_nat_rule(
     self, rule_type, original_ip, original_port, translated_ip, translated_port, protocol, interface=None
 ):
     gatewayInterface = self._select_gateway_interface(interface)
     if not gatewayInterface:
         return
     if not interface:
         interface = gatewayInterface.get_Network().get_name()
     interface = interface.lower()
     natRules = self.get_nat_rules()
     newRules = []
     found_rule = False
     for natRule in natRules:
         if rule_type == natRule.get_RuleType():
             gatewayNatRule = natRule.get_GatewayNatRule()
             gateway_interface_name = gatewayNatRule.get_Interface().get_name().lower()
             gateway_original_ip = gatewayNatRule.get_OriginalIp() if gatewayNatRule.get_OriginalIp() else "any"
             gateway_original_port = (
                 gatewayNatRule.get_OriginalPort() if gatewayNatRule.get_OriginalPort() else "any"
             )
             gateway_translated_ip = (
                 gatewayNatRule.get_TranslatedIp() if gatewayNatRule.get_TranslatedIp() else "any"
             )
             gateway_translated_port = (
                 gatewayNatRule.get_TranslatedPort() if gatewayNatRule.get_TranslatedPort() else "any"
             )
             gateway_protocol = gatewayNatRule.get_Protocol() if gatewayNatRule.get_Protocol() else "any"
             if (
                 original_ip == gateway_original_ip
                 and original_port == gateway_original_port
                 and translated_ip == gateway_translated_ip
                 and translated_port == gateway_translated_port
                 and protocol == gateway_protocol
                 and interface == gateway_interface_name
             ):
                 found_rule = True
             else:
                 newRules.append(natRule)
         else:
             newRules.append(natRule)
     if found_rule:
         natService = None
         edgeGatewayServiceConfiguration = self.me.get_Configuration().get_EdgeGatewayServiceConfiguration()
         natServices = filter(
             lambda service: service.__class__.__name__ == "NatServiceType",
             edgeGatewayServiceConfiguration.get_NetworkService(),
         )
         if len(natServices) > 0:
             natService = natServices[0]
             natService.set_NatRule(newRules)
         else:
             natService = NatServiceType()
             natService.set_NatRule(newRules)
             edgeGatewayServiceConfiguration.get_NetworkService().append(natService)
         return True
Пример #4
0
    def del_nat_rule(self, rule_type, original_ip, original_port,
                     translated_ip, translated_port, protocol):
        gatewayInterfaces = self.get_interfaces('uplink')
        if len(gatewayInterfaces) == 0:
            return False
        gatewayInterface = gatewayInterfaces[0]
        natRules = self.get_nat_rules()
        newRules = []

        found_rule = False
        for natRule in natRules:
            # todo check if the network interface is the same
            ruleId = natRule.get_Id()
            if rule_type == natRule.get_RuleType():
                gatewayNatRule = natRule.get_GatewayNatRule()
                # import sys; gatewayNatRule.export(sys.stdout, 0)
                gateway_original_ip = gatewayNatRule.get_OriginalIp(
                ) if gatewayNatRule.get_OriginalIp() else 'any'
                gateway_original_port = gatewayNatRule.get_OriginalPort(
                ) if gatewayNatRule.get_OriginalPort() else 'any'
                gateway_translated_ip = gatewayNatRule.get_TranslatedIp(
                ) if gatewayNatRule.get_TranslatedIp() else 'any'
                gateway_translated_port = gatewayNatRule.get_TranslatedPort(
                ) if gatewayNatRule.get_TranslatedPort() else 'any'
                gateway_protocol = gatewayNatRule.get_Protocol(
                ) if gatewayNatRule.get_Protocol() else 'any'
                if original_ip == gateway_original_ip and \
                   original_port == gateway_original_port and \
                   translated_ip == gateway_translated_ip and \
                   translated_port == gateway_translated_port and \
                   protocol == gateway_protocol:
                    found_rule = True
                else:
                    newRules.append(natRule)
            else:
                newRules.append(natRule)
        # return self._post_nat_rules(newRules)
        if found_rule:
            natService = None
            edgeGatewayServiceConfiguration = self.me.get_Configuration(
            ).get_EdgeGatewayServiceConfiguration()
            natServices = filter(
                lambda service: service.__class__.__name__ == "NatServiceType",
                edgeGatewayServiceConfiguration.get_NetworkService())
            if len(natServices) > 0:
                natService = natServices[0]
                natService.set_NatRule(newRules)
            else:
                natService = NatServiceType()
                natService.set_NatRule(newRules)
                edgeGatewayServiceConfiguration.get_NetworkService().append(
                    natService)
            # import sys; self.me.export(sys.stdout, 0)
            return True
Пример #5
0
 def del_all_nat_rules(self):
     natRules = self.get_nat_rules()
     newRules = []
     natService = None
     edgeGatewayServiceConfiguration = self.me.get_Configuration().get_EdgeGatewayServiceConfiguration()
     natServices = filter(lambda service: service.__class__.__name__ == "NatServiceType", edgeGatewayServiceConfiguration.get_NetworkService())
     if len(natServices) > 0:
         natService = natServices[0]
         natService.set_NatRule(newRules)
     else:
         natService = NatServiceType()
         natService.set_NatRule(newRules)
         edgeGatewayServiceConfiguration.get_NetworkService().append(natService)
     return True
Пример #6
0
 def del_all_nat_rules(self):
     natRules = self.get_nat_rules()
     newRules = []
     natService = None
     edgeGatewayServiceConfiguration = self.me.get_Configuration(
     ).get_EdgeGatewayServiceConfiguration()
     natServices = filter(
         lambda service: service.__class__.__name__ == "NatServiceType",
         edgeGatewayServiceConfiguration.get_NetworkService())
     if len(natServices) > 0:
         natService = natServices[0]
         natService.set_NatRule(newRules)
     else:
         natService = NatServiceType()
         natService.set_NatRule(newRules)
         edgeGatewayServiceConfiguration.get_NetworkService().append(
             natService)
     return True
Пример #7
0
    def del_nat_rule(self, rule_type, original_ip, original_port, translated_ip, translated_port, protocol):
        gatewayInterfaces = self.get_interfaces('uplink')
        if len(gatewayInterfaces) == 0:
            return False
        gatewayInterface = gatewayInterfaces[0]
        natRules = self.get_nat_rules()
        newRules = []

        found_rule = False
        for natRule in natRules:
            # todo check if the network interface is the same
            ruleId = natRule.get_Id()
            if rule_type == natRule.get_RuleType():
                gatewayNatRule = natRule.get_GatewayNatRule()
                # import sys; gatewayNatRule.export(sys.stdout, 0)
                gateway_original_ip = gatewayNatRule.get_OriginalIp() if gatewayNatRule.get_OriginalIp() else 'any'
                gateway_original_port = gatewayNatRule.get_OriginalPort() if gatewayNatRule.get_OriginalPort() else 'any'
                gateway_translated_ip = gatewayNatRule.get_TranslatedIp() if gatewayNatRule.get_TranslatedIp() else 'any'
                gateway_translated_port = gatewayNatRule.get_TranslatedPort() if gatewayNatRule.get_TranslatedPort() else 'any'
                gateway_protocol = gatewayNatRule.get_Protocol() if gatewayNatRule.get_Protocol() else 'any'
                if original_ip == gateway_original_ip and \
                   original_port == gateway_original_port and \
                   translated_ip == gateway_translated_ip and \
                   translated_port == gateway_translated_port and \
                   protocol == gateway_protocol:
                    found_rule = True
                else:
                    newRules.append(natRule)
            else:
                newRules.append(natRule)
        # return self._post_nat_rules(newRules)
        if found_rule:
            natService = None
            edgeGatewayServiceConfiguration = self.me.get_Configuration().get_EdgeGatewayServiceConfiguration()
            natServices = filter(lambda service: service.__class__.__name__ == "NatServiceType", edgeGatewayServiceConfiguration.get_NetworkService())
            if len(natServices) > 0:
                natService = natServices[0]
                natService.set_NatRule(newRules)
            else:
                natService = NatServiceType()
                natService.set_NatRule(newRules)
                edgeGatewayServiceConfiguration.get_NetworkService().append(natService)
            # import sys; self.me.export(sys.stdout, 0)
            return True
Пример #8
0
    def add_nat_rule(self, rule_type, original_ip, original_port, translated_ip, translated_port, protocol):
        gatewayInterfaces = self.get_interfaces('uplink')
        if len(gatewayInterfaces) == 0:
            return
        gatewayInterface = gatewayInterfaces[0]
        natRules = self.get_nat_rules()

        maxId = 0
        minId = 65537
        for natRule in natRules:
            ruleId = natRule.get_Id()
            if ruleId > maxId:
                maxId = ruleId
            if ruleId < minId:
                minId = ruleId
        if maxId == 0:
            maxId = minId - 1

        port_changed = True
        original_port_modified = original_port
        while port_changed and len(natRules) > 0:
            for natRule in natRules:
                port_changed = False
                if rule_type == natRule.get_RuleType():
                    gatewayNatRule = natRule.get_GatewayNatRule()
                    if original_ip == gatewayNatRule.get_OriginalIp() and \
                       original_port_modified == gatewayNatRule.get_OriginalPort():
                        original_port_modified = str(int(original_port_modified) + 1)
                        port_changed = True
                        break

        rule = NatRuleType()
        rule.set_RuleType(rule_type)
        rule.set_IsEnabled(True)
        # note, the new id has to be greater than the last
        # apparently the same id can be reused by two different rules, but use a new one
        rule.set_Id(maxId + 1)
        gatewayRule = GatewayNatRuleType()
        gatewayInterfaceReference = ReferenceType()
        gatewayInterfaceReference.set_href(gatewayInterface.get_Network().get_href())
        gatewayInterfaceReference.set_type(gatewayInterface.get_Network().get_type())
        gatewayInterfaceReference.set_name(gatewayInterface.get_Network().get_name())

        gatewayRule.set_Interface(gatewayInterfaceReference)
        gatewayRule.set_OriginalIp(original_ip)
        gatewayRule.set_OriginalPort(original_port_modified)
        gatewayRule.set_TranslatedIp(translated_ip)
        gatewayRule.set_TranslatedPort(translated_port)
        gatewayRule.set_Protocol(protocol)
        rule.set_GatewayNatRule(gatewayRule)
        natRules.append(rule)
        natService = None
        edgeGatewayServiceConfiguration = self.me.get_Configuration().get_EdgeGatewayServiceConfiguration()
        natServices = filter(lambda service: service.__class__.__name__ == "NatServiceType", edgeGatewayServiceConfiguration.get_NetworkService())
        if len(natServices) > 0:
            natService = natServices[0]
            natService.set_NatRule(natRules)
        else:
            natService = NatServiceType(IsEnabled=True)
            natService.original_tagname_ = 'NatService'
            natService.set_NatRule(natRules)
            edgeGatewayServiceConfiguration.get_NetworkService().append(natService)