def generate(config, dnat=False, test=True): public_ip = config["public_ip"] current_ip = config["base_ip"] hosts = dict() for group in config["groups"].values(): for proxy in group["proxies"]: if not dnat: add_hosts(hosts, proxy["domain"], public_ip) elif not proxy["dnat"]: add_hosts(hosts, proxy["domain"], current_ip) if test: if not dnat: add_hosts(hosts, 'ptest.verdandi.is', public_ip) add_hosts(hosts, 'ptest2.verdandi.is', public_ip) else: add_hosts(hosts, 'ptest.verdandi.is', current_ip) add_hosts(hosts, 'ptest2.verdandi.is', current_ip) if dnat: for group in config["groups"].values(): for proxy in group["proxies"]: if proxy["dnat"]: current_ip = long2ip(ip2long(current_ip) + 1) add_hosts(hosts, proxy["domain"], current_ip) return generate_hosts_content(hosts)
def ipaddress_to_location(self, ipaddress): """ Look up the time zone for a given IP address. Use this method if you have a Region or City database. @param hostname: IP address @type hostname: str @return: location of lng and lat @rtype: string """ try: ipnum = ip2long(ipaddress) if not ipnum: raise ValueError("Invalid IP address: %s" % ipaddress) if not self._databaseType in (const.CITY_EDITION_REV0, const.CITY_EDITION_REV1): raise GeoIPError('Invalid database type; region_* methods expect ' \ 'Region or City database') lng = self._get_record(ipnum)['longitude'] lat = self._get_record(ipnum)['latitude'] return { 'lng': lng, 'lat': lat } except ValueError: raise GeoIPError( '*_by_addr methods only accept IP addresses. Use *_by_name for hostnames. (Address: %s)' % ipaddress)
def generate(config, dnat=False): public_ip = config["public_ip"] current_ip = config["base_ip"] dnsmasq_content = "" for group in config["groups"].values(): if not dnat: c = chunks([proxy["domain"] for proxy in group["proxies"]], 5) else: c = chunks([ proxy["domain"] for proxy in group["proxies"] if not proxy["dnat"] ], 5) for chunk in c: if not dnat: dnsmasq_content += generate_dns(chunk, public_ip) else: dnsmasq_content += generate_dns(chunk, current_ip) if dnat: for group in config["groups"].values(): for proxy in group["proxies"]: if proxy["dnat"]: current_ip = long2ip(ip2long(current_ip) + 1) dnsmasq_content += generate_dns(proxy["domain"], current_ip) return dnsmasq_content
def ipaddress_to_timezone(self, ipaddress): """ Look up the time zone for a given IP address. Use this method if you have a Region or City database. @param hostname: IP address @type hostname: str @return: A datetime.tzinfo implementation for the given timezone @rtype: datetime.tzinfo """ try: ipnum = ip2long(ipaddress) if not ipnum: raise ValueError("Invalid IP address: %s" % ipaddress) if not self._databaseType in (const.CITY_EDITION_REV0, const.CITY_EDITION_REV1): raise GeoIPError('Invalid database type; region_* methods expect ' \ 'Region or City database') tz_name = self._get_record(ipnum)['time_zone'] if tz_name: tz = pytz.timezone(tz_name) else: tz = None return tz except ValueError: raise GeoIPError( '*_by_addr methods only accept IP addresses. Use *_by_name for hostnames. (Address: %s)' % ipaddress)
def record_by_addr(self, addr): """ Look up the record for a given IP address. Use this method if you have a City database. @param addr: IP address @type addr: str @return: dict with country_code, country_code3, country_name, region, city, postal_code, latitude, longitude, dma_code, metro_code, area_code, region_name, time_zone @rtype: dict """ try: ipnum = ip2long(addr) if not ipnum: raise ValueError("Invalid IP address: %s" % addr) if not self._databaseType in (CITY_EDITION_REV0, CITY_EDITION_REV1): raise GeoIPError( 'Invalid database type; record_* methods expect City database' ) return self._get_record(ipnum) except ValueError: raise GeoIPError( '*_by_addr methods only accept IP addresses. Use *_by_name for hostnames.' )
def org_by_addr(self, addr): """ Lookup the organization (or ISP) for given IP address. Use this method if you have an Organization/ISP database. @param addr: IP address @type addr: str @return: organization or ISP name @rtype: str """ try: ipnum = ip2long(addr) if not ipnum: raise ValueError("Invalid IP address: %s" % addr) if self._databaseType != ORG_EDITION: raise GeoIPError('Invalid database type; org_* methods expect '\ 'Org/ISP database') return self._get_org(ipnum) except ValueError: raise GeoIPError( '*_by_addr methods only accept IP addresses. Use *_by_name for hostnames.' )
def region_by_addr(self, addr): """ Lookup the region for given IP address. Use this method if you have a Region database. @param addr: IP address @type addr: str @return: dict containing country_code, region, and region_name @rtype: dict """ try: ipnum = ip2long(addr) if not ipnum: raise ValueError("Invalid IP address: %s" % addr) if not self._databaseType in (REGION_EDITION_REV0, REGION_EDITION_REV1, CITY_EDITION_REV0, CITY_EDITION_REV1): raise GeoIPError('Invalid database type; region_* methods expect '\ 'Region or City database') return self._get_region(ipnum) except ValueError: raise GeoIPError('*_by_addr methods only accept IP addresses. Use *_by_name for hostnames. (Address: %s)' % addr)
def generate(config, dnat=False, test=True): public_ip = config["public_ip"] current_ip = config["base_ip"] hosts = dict() for group in config["groups"].values(): for proxy in group["proxies"]: if not dnat: add_hosts(hosts, proxy["domain"], public_ip) elif proxy["dnat"]: add_hosts(hosts, proxy["domain"], current_ip) if test: if not dnat: add_hosts(hosts, "ptest.verdandi.is", public_ip) add_hosts(hosts, "ptest2.verdandi.is", public_ip) else: add_hosts(hosts, "ptest.verdandi.is", current_ip) add_hosts(hosts, "ptest2.verdandi.is", current_ip) if dnat: for group in config["groups"].values(): for proxy in group["proxies"]: if not proxy["dnat"]: current_ip = long2ip(ip2long(current_ip) + 1) add_hosts(hosts, proxy["domain"], current_ip) return generate_hosts_content(hosts)
def generate(config, dnat=False, test=True): public_ip = config["public_ip"] current_ip = config["base_ip"] dnsmasq_content = "" for group in config["groups"].values(): if not dnat: c = chunks([proxy["domain"] for proxy in group["proxies"]], 5) else: c = chunks([proxy["domain"] for proxy in group["proxies"] if proxy["dnat"]], 5) for chunk in c: if not dnat: dnsmasq_content += generate_dns(chunk, public_ip) else: dnsmasq_content += generate_dns(chunk, current_ip) if test: if not dnat: dnsmasq_content += generate_dns('ptest.verdandi.is', public_ip) dnsmasq_content += generate_dns('ptest2.verdandi.is', public_ip) else: dnsmasq_content += generate_dns('ptest.verdandi.is', current_ip) dnsmasq_content += generate_dns('ptest2.verdandi.is', current_ip) if dnat: for group in config["groups"].values(): for proxy in group["proxies"]: if not proxy["dnat"]: current_ip = long2ip(ip2long(current_ip) + 1) dnsmasq_content += generate_dns(proxy["domain"], current_ip) return dnsmasq_content
def generate(json, catchall=True, test=True): public_ip = json["public_ip"] current_ip = json["base_ip"] dnsmasq_content = "" for proxy in json["proxies"]: if proxy["enabled"]: if catchall: dnsmasq_content += generate_dns(proxy["dest_addr"], public_ip) elif proxy["catchall"]: dnsmasq_content += generate_dns(proxy["dest_addr"], current_ip) if test: if catchall: dnsmasq_content += generate_dns('proxy-test.trick77.com', public_ip) dnsmasq_content += generate_dns('dns-test.trick77.com', public_ip) else: dnsmasq_content += generate_dns('proxy-test.trick77.com', current_ip) dnsmasq_content += generate_dns('dns-test.trick77.com', current_ip) if not catchall: for proxy in json["proxies"]: if proxy["enabled"] and not proxy["catchall"]: current_ip = long2ip(ip2long(current_ip) + 1) dnsmasq_content += generate_dns(proxy["dest_addr"], current_ip) return dnsmasq_content
def region_by_addr(self, addr): """ Lookup the region for given IP address. Use this method if you have a Region database. @param addr: IP address @type addr: str @return: dict containing country_code, region, and region_name @rtype: dict """ try: ipnum = ip2long(addr) if not ipnum: raise ValueError("Invalid IP address: %s" % addr) if not self._databaseType in (REGION_EDITION_REV0, REGION_EDITION_REV1, CITY_EDITION_REV0, CITY_EDITION_REV1): raise GeoIPError('Invalid database type; region_* methods expect '\ 'Region or City database') return self._get_region(ipnum) except ValueError: raise GeoIPError('*_by_addr methods only accept IP addresses. Use *_by_name for hostnames.')
def generate(json, catchall = True, test = True): public_ip = json["public_ip"] current_ip = json["base_ip"] dnsmasq_content = "" for proxy in json["proxies"]: if proxy["enabled"]: if catchall: dnsmasq_content += generate_dns(proxy["dest_addr"], public_ip) elif proxy["catchall"]: dnsmasq_content += generate_dns(proxy["dest_addr"], current_ip) if test: if catchall: dnsmasq_content += generate_dns('proxy-test.trick77.com', public_ip) dnsmasq_content += generate_dns('dns-test.trick77.com', public_ip) else: dnsmasq_content += generate_dns('proxy-test.trick77.com', current_ip) dnsmasq_content += generate_dns('dns-test.trick77.com', current_ip) if not catchall: for proxy in json["proxies"]: if proxy["enabled"] and not proxy["catchall"]: current_ip = long2ip(ip2long(current_ip) + 1) dnsmasq_content += generate_dns(proxy["dest_addr"], current_ip) return dnsmasq_content
def generate(config): current_ip = config["base_ip"] local_subnet = config["local_subnet"] local_device = config["local_device"] iproute2_content = generate_iproute2(current_ip, local_subnet, local_device) for group in config["groups"].values(): for proxy in group["proxies"]: if proxy["dnat"]: current_ip = long2ip(ip2long(current_ip) + 1) iproute2_content += generate_iproute2(current_ip, local_subnet, local_device) return iproute2_content
def generate(json, test=True): current_ip = json["base_ip"] hosts = dict() for proxy in json["proxies"]: if proxy["enabled"] and proxy["catchall"]: add_hosts(hosts, proxy["dest_addr"], current_ip) if test: add_hosts(hosts, 'proxy-test.trick77.com', current_ip) add_hosts(hosts, 'dns-test.trick77.com', current_ip) for proxy in json["proxies"]: if proxy["enabled"] and not proxy["catchall"]: current_ip = long2ip(ip2long(current_ip) + 1) add_hosts(hosts, proxy["dest_addr"], current_ip) return generate_hosts_content(hosts)
def generate(json): haproxy_bind_ip = json["haproxy_bind_ip"] current_ip = json["base_ip"] current_port = json["base_port"] netsh_content = generate_netsh('80', haproxy_bind_ip, current_ip, current_port) current_port += 1 netsh_content += generate_netsh('443', haproxy_bind_ip, current_ip, current_port) current_port += 1 for proxy in json["proxies"]: if proxy["enabled"] and not proxy["catchall"]: current_ip = long2ip(ip2long(current_ip) + 1) for mode in proxy["modes"]: netsh_content += generate_netsh(mode["port"], haproxy_bind_ip, current_ip, current_port) current_port += 1 return netsh_content
def generate(config): iptables_location = config["iptables_location"] public_ip = config["public_ip"] current_ip = config["base_ip"] current_port = config["base_port"] iptables_content = generate_iptables('80', public_ip, current_ip, current_port, iptables_location) current_port += 1 iptables_content += generate_iptables('443', public_ip, current_ip, current_port, iptables_location) current_port += 1 for group in config["groups"].values(): for proxy in group["proxies"]: if proxy["dnat"]: current_ip = long2ip(ip2long(current_ip) + 1) for protocol in proxy["protocols"]: iptables_content += generate_iptables(port(protocol), public_ip, current_ip, current_port, iptables_location) current_port += 1 return iptables_content
def generate(config): public_ip = config["public_ip"] current_ip = config["base_ip"] current_port = config["base_port"] rinetd_content = generate_rinetd('80', public_ip, current_ip, current_port) current_port += 1 rinetd_content += generate_rinetd('443', public_ip, current_ip, current_port) current_port += 1 for group in config["groups"].values(): for proxy in group["proxies"]: if proxy["dnat"]: current_ip = long2ip(ip2long(current_ip) + 1) for protocol in proxy["protocols"]: rinetd_content += generate_rinetd(port(protocol), public_ip, current_ip, current_port) current_port += 1 return rinetd_content
def generate(config): public_ip = config["public_ip"] current_ip = config["base_ip"] current_port = config["base_port"] netsh_content = generate_netsh('80', public_ip, current_ip, current_port) current_port += 1 netsh_content += generate_netsh('443', public_ip, current_ip, current_port) current_port += 1 for group in config["groups"].values(): for proxy in group["proxies"]: if proxy["dnat"]: current_ip = long2ip(ip2long(current_ip) + 1) for protocol in proxy["protocols"]: netsh_content += generate_netsh(port(protocol), public_ip, current_ip, current_port) current_port += 1 return netsh_content
def generate(json): iptables_location = json["iptables_location"] public_ip = json["public_ip"] current_ip = json["base_ip"] current_port = json["base_port"] rinetd_content = generate_rinetd('80', public_ip, current_ip, current_port) current_port += 1 rinetd_content += generate_rinetd('443', public_ip, current_ip, current_port) current_port += 1 for proxy in json["proxies"]: if proxy["enabled"] and not proxy["catchall"]: current_ip = long2ip(ip2long(current_ip) + 1) for mode in proxy["modes"]: rinetd_content += generate_rinetd(mode["port"], public_ip, current_ip, current_port) current_port += 1 return rinetd_content
def generate(json, test = True): current_ip = json["base_ip"] hosts = dict() for proxy in json["proxies"]: if proxy["enabled"] and proxy["catchall"]: add_hosts(hosts, proxy["dest_addr"], current_ip) if test: add_hosts(hosts, 'proxy-test.trick77.com', current_ip) add_hosts(hosts, 'dns-test.trick77.com', current_ip) for proxy in json["proxies"]: if proxy["enabled"] and not proxy["catchall"]: current_ip = long2ip(ip2long(current_ip) + 1) add_hosts(hosts, proxy["dest_addr"], current_ip) return generate_hosts_content(hosts)
def id_by_addr(self, addr): """ Get the country index. Looks up the index for the country which is the key for the code and name. @param addr: The IP address @type addr: str @return: network byte order 32-bit integer @rtype: int """ ipnum = util.ip2long(addr) if not ipnum: raise ValueError("Invalid IP address: %s" % addr) COUNTY_EDITIONS = (const.COUNTRY_EDITION, const.COUNTRY_EDITION_V6) if self._databaseType not in COUNTY_EDITIONS: message = 'Invalid database type, expected Country' raise GeoIPError(message) return self._seek_country(ipnum) - const.COUNTRY_BEGIN
def generate(config): iptables_location = config["iptables_location"] public_ip = config["public_ip"] current_ip = config["base_ip"] current_port = config["base_port"] iptables_content = generate_iptables('80', public_ip, current_ip, current_port, iptables_location) current_port += 1 iptables_content += generate_iptables('443', public_ip, current_ip, current_port, iptables_location) current_port += 1 for group in config["groups"].values(): for proxy in group["proxies"]: if proxy["dnat"]: current_ip = long2ip(ip2long(current_ip) + 1) for protocol in proxy["protocols"]: iptables_content += generate_iptables( port(protocol), public_ip, current_ip, current_port, iptables_location) current_port += 1 return iptables_content
def region_by_addr(self, addr): """ Lookup the region for given IP address. Use this method if you have a Region database. @param addr: IP address @type addr: str @return: Dictionary containing country_code, region and region_name @rtype: dict """ try: ipnum = util.ip2long(addr) if not ipnum: raise ValueError('Invalid IP address') if self._databaseType not in const.REGION_CITY_EDITIONS: message = 'Invalid database type, expected Region or City' raise GeoIPError(message) return self._get_region(ipnum) except ValueError: raise GeoIPError('Failed to lookup address %s' % addr)
def time_zone_by_addr(self, addr): """ Look up the time zone for a given IP address. Use this method if you have a Region or City database. @param addr: IP address @type addr: str @return: Time zone @rtype: str """ try: ipnum = util.ip2long(addr) if not ipnum: raise ValueError('Invalid IP address') if self._databaseType not in const.CITY_EDITIONS: message = 'Invalid database type, expected City' raise GeoIPError(message) return self._get_record(ipnum)['time_zone'] except ValueError: raise GeoIPError('Failed to lookup address %s' % addr)
def org_by_addr(self, addr): """ Lookup the organization (or ISP) for given IP address. Use this method if you have an Organization/ISP database. @param addr: IP address @type addr: str @return: organization or ISP name @rtype: str """ try: ipnum = ip2long(addr) if not ipnum: raise ValueError("Invalid IP address: %s" % addr) if self._databaseType != ORG_EDITION: raise GeoIPError('Invalid database type; org_* methods expect '\ 'Org/ISP database') return self._get_org(ipnum) except ValueError: raise GeoIPError('*_by_addr methods only accept IP addresses. Use *_by_name for hostnames.')
def org_by_addr(self, addr): """ Lookup Organization, ISP or ASNum for given IP address. Use this method if you have an Organization, ISP or ASNum database. @param addr: IP address @type addr: str @return: organization or ISP name @rtype: str """ try: ipnum = util.ip2long(addr) if not ipnum: raise ValueError('Invalid IP address') valid = (const.ORG_EDITION, const.ISP_EDITION, const.ASNUM_EDITION) if self._databaseType not in valid: message = 'Invalid database type, expected Org, ISP or ASNum' raise GeoIPError(message) return self._get_org(ipnum) except ValueError: raise GeoIPError('Failed to lookup address %s' % addr)
def record_by_addr(self, addr): """ Look up the record for a given IP address. Use this method if you have a City database. @param addr: IP address @type addr: str @return: dict with country_code, country_code3, country_name, region, city, postal_code, latitude, longitude, dma_code, metro_code, area_code, region_name, time_zone @rtype: dict """ try: ipnum = ip2long(addr) if not ipnum: raise ValueError("Invalid IP address: %s" % addr) if not self._databaseType in (CITY_EDITION_REV0, CITY_EDITION_REV1): raise GeoIPError('Invalid database type; record_* methods expect City database') return self._get_record(ipnum) except ValueError: raise GeoIPError('*_by_addr methods only accept IP addresses. Use *_by_name for hostnames.')
def _lookup_country_id(self, addr): """ Get the country index. This method is called by the _lookupCountryCode and _lookupCountryName methods. It looks up the index ('id') for the country which is the key for the code and name. @param addr: The IP address @type addr: str @return: network byte order 32-bit integer @rtype: int """ ipnum = ip2long(addr) if not ipnum: raise ValueError("Invalid IP address: %s" % addr) if self._databaseType != COUNTRY_EDITION: raise GeoIPError('Invalid database type; country_* methods expect '\ 'Country database') return self._seek_country(ipnum) - COUNTRY_BEGIN
def record_by_addr(self, addr): """ Look up the record for a given IP address. Use this method if you have a City database. @param addr: IP address @type addr: str @return: Dictionary with country_code, country_code3, country_name, region, city, postal_code, latitude, longitude, dma_code, metro_code, area_code, region_name, time_zone @rtype: dict """ try: ipnum = util.ip2long(addr) if not ipnum: raise ValueError('Invalid IP address') if self._databaseType not in const.CITY_EDITIONS: message = 'Invalid database type, expected City' raise GeoIPError(message) return self._get_record(ipnum) except ValueError: raise GeoIPError('Failed to lookup address %s' % addr)
pass except KeyError: print("CLIENT_REQ not provided. Exiting.") sock.sendto(b"", addr) sys.exit(0) CLIENT_ADDR = addr CLIENT_CONFIRMED = True resp = bytearray() resp.append(constants.TYPES["SOURCE_ID"]) resp.append(constants.IDS["SERVER"]) resp.append(constants.TYPES["DEST_ID"]) resp.append(constants.IDS["CLIENT"]) resp.append(constants.TYPES["CLIENT_ACK"]) ipint = ip2long(HOST_ADDR[0]) resp.append((ipint & 0x000000ff)) resp.append((ipint & 0x0000ff00) >> 8) resp.append((ipint & 0x00ff0000) >> 16) resp.append((ipint & 0xff000000) >> 24) resp.append((HOST_ADDR[1] & 0x000000ff)) resp.append((HOST_ADDR[1] & 0x0000ff00) >> 8) resp.append((HOST_ADDR[1] & 0x00ff0000) >> 16) resp.append((HOST_ADDR[1] & 0xff000000) >> 24) sock.sendto(resp, addr) # Provide client information to host. resp = bytearray() resp.append(constants.TYPES["SOURCE_ID"]) resp.append(constants.IDS["SERVER"])