def multi_get_ip_addr(org_id, user_ids): spec = { 'user_id': {'$in': user_ids}, } project = { '_id': False, 'user_id': True, 'server_id': True, 'address': True, } for doc in ServerIpPool.collection.find(spec, project): network = ipaddress.IPNetwork(doc['address']) network = str(network.network) + '/' + str(network.prefixlen) addr6 = utils.ip4to6x64(settings.vpn.ipv6_prefix, network, doc['address']) yield doc['user_id'], doc['server_id'], \ doc['address'].split('/')[0], addr6.split('/')[0]
def multi_get_ip_addr(org_id, user_ids): spec = { 'user_id': {'$in': user_ids}, } project = { '_id': False, 'user_id': True, 'server_id': True, 'address': True, } for doc in ServerIpPool.collection.find(spec, project): network = ipaddress.ip_network(doc['address'], strict=False) network = str(network.network_address) + '/' + str(network.prefixlen) addr6 = utils.ip4to6x64(settings.vpn.ipv6_prefix, network, doc['address']) yield doc['user_id'], doc['server_id'], \ doc['address'].split('/')[0], addr6.split('/')[0]
def ip4to6(self, addr): routed_subnet6 = settings.local.host.routed_subnet6 if routed_subnet6: return utils.ip4to6x96(routed_subnet6, self.network, addr) return utils.ip4to6x64(settings.vpn.ipv6_prefix, self.network, addr)
def start(self): global _loaded local_iface = settings.local.host.local_iface if not _loaded: _loaded = True try: utils.check_call_silent([ 'modprobe', 'vxlan', ]) except subprocess.CalledProcessError: pass self.remove_iface() if not local_iface: logger.error( 'Failed to find local interface for vxlan', 'vxlan', vxlan_id=self.vxlan_id, server_id=self.server_id, host_id=settings.local.host_id, local_addr=settings.local.host.local_addr, ) raise ValueError('Failed to find local interface for vxlan') utils.check_output_logged([ 'ip', 'link', 'add', self.iface_name, 'type', 'vxlan', 'id', str(settings.vpn.vxlan_id_start + self.vxlan_id), 'dstport', '4789', 'dev', local_iface['interface'], 'nolearning', ], ignore_states=['File exists']) self.vxlan_mac = utils.get_interface_mac_address(self.iface_name) self._init_host() self.vxlan_addr = self.get_host_addr(self.host_vxlan_id) if self.ipv6: self.vxlan_addr6 = utils.ip4to6x64( settings.vpn.ipv6_prefix, self.vxlan_net, self.vxlan_addr, ) utils.check_output_logged([ 'ip', 'address', 'add', self.vxlan_addr + '/24', 'dev', self.iface_name, ], ignore_states=['File exists']) if self.ipv6: utils.check_output_logged([ 'ip', '-6', 'address', 'add', self.vxlan_addr6 + '/64', 'dev', self.iface_name, ], ignore_states=['File exists']) utils.check_output_logged([ 'ip', 'link', 'set', 'up', self.iface_name, ]) self._init_hosts()
def get_host_addr6(self, host_vxlan_id): return utils.ip4to6x64( settings.vpn.ipv6_prefix, settings.vpn.vxlan_net_prefix + str(self.vxlan_id) + '.0/24', self.get_host_addr(host_vxlan_id), )
def start(self): global _loaded local_iface = settings.local.host.local_iface if not _loaded: _loaded = True try: utils.check_call_silent([ 'modprobe', 'vxlan', ]) except subprocess.CalledProcessError: pass self.remove_iface() if not local_iface: logger.error('Failed to find local interface for vxlan', 'vxlan', vxlan_id=self.vxlan_id, server_id=self.server_id, host_id=settings.local.host_id, local_addr=settings.local.host.local_addr, ) raise ValueError('Failed to find local interface for vxlan') utils.check_output_logged([ 'ip', 'link', 'add', self.iface_name, 'type', 'vxlan', 'id', str(settings.vpn.vxlan_id_start + self.vxlan_id), 'dstport', '4789', 'dev', local_iface['interface'], 'nolearning', ], ignore_states=['File exists']) self.vxlan_mac = utils.get_interface_mac_address(self.iface_name) self._init_host() self.vxlan_addr = self.get_host_addr(self.host_vxlan_id) if self.ipv6: self.vxlan_addr6 = utils.ip4to6x64( settings.vpn.ipv6_prefix, self.vxlan_net, self.vxlan_addr, ) utils.check_output_logged([ 'ip', 'address', 'add', self.vxlan_addr + '/24', 'dev', self.iface_name, ], ignore_states=['File exists']) if self.ipv6: utils.check_output_logged([ 'ip', '-6', 'address', 'add', self.vxlan_addr6 + '/64', 'dev', self.iface_name, ], ignore_states=['File exists']) utils.check_output_logged([ 'ip', 'link', 'set', 'up', self.iface_name, ]) self._init_hosts()