def get_listen_addr(): CONFIG = ConfigParser(inline_comment_prefixes=('#', ';')) CONFIG._optcre = re.compile(r'(?P<option>[^=\s]+)\s*(?P<vi>=?)\s*(?P<value>.*)') CONFIG.read([CONFIG_FILENAME, CONFIG_USER_FILENAME]) LISTEN_IP = CONFIG.get('listen', 'ip') if LISTEN_IP == '0.0.0.0': LISTEN_IP = '127.0.0.1' LISTEN_GAE = '%s:%d' % (LISTEN_IP, CONFIG.getint('listen', 'gae_port')) LISTEN_AUTO = '%s:%d' % (LISTEN_IP, CONFIG.getint('listen', 'auto_port')) return proxy_server(LISTEN_GAE, True), proxy_server(LISTEN_AUTO, True)
def load_config(): global LISTEN_GAE, LISTEN_AUTO CONFIG = ConfigParser(inline_comment_prefixes=('#', ';')) CONFIG._optcre = re.compile(r'(?P<option>[^=\s]+)\s*(?P<vi>=?)\s*(?P<value>.*)') CONFIG.read([CONFIG_FILENAME, CONFIG_USER_FILENAME]) LISTEN_IP = CONFIG.get('listen', 'ip') if LISTEN_IP == '0.0.0.0': LISTEN_IP = '127.0.0.1' elif LISTEN_IP == '::': LISTEN_IP = '::1' elif LISTEN_IP == '': LINK_PROFILE = CONFIG.get('link', 'profile') if LINK_PROFILE not in ('ipv4', 'ipv6', 'ipv46'): LINK_PROFILE = 'ipv4' LISTEN_IP = '127.0.0.1' if '4' in LINK_PROFILE else '::1' _LISTEN_GAE = '%s:%d' % (LISTEN_IP, CONFIG.getint('listen', 'gae_port')) _LISTEN_AUTO = '%s:%d' % (LISTEN_IP, CONFIG.getint('listen', 'auto_port')) LISTEN_GAE = proxy_server(_LISTEN_GAE, True) LISTEN_AUTO = proxy_server(_LISTEN_AUTO, True) LISTEN_DEBUGINFO = _LOGLv[min(CONFIG.getint('listen', 'debuginfo'), 3)] logging.setLevel(LISTEN_DEBUGINFO)
def load_config(): patch_configparser() import re from configparser import ConfigParser _LOGLv = { 0: logging.WARNING, 1: logging.INFO, 2: logging.TEST, 3: logging.DEBUG } CONFIG_FILENAME = os.path.join(config_dir, 'Config.ini') CONFIG_USER_FILENAME = re.sub(r'\.ini$', '.user.ini', CONFIG_FILENAME) CONFIG_AUTO_FILENAME = os.path.join(config_dir, 'ActionFilter.ini') CONFIG = ConfigParser(inline_comment_prefixes=('#', ';')) CONFIG._optcre = re.compile( r'(?P<option>[^=\s]+)\s*(?P<vi>=?)\s*(?P<value>.*)') CONFIG.read([CONFIG_FILENAME, CONFIG_USER_FILENAME]) LISTEN_IP = CONFIG.get('listen', 'ip') if LISTEN_IP == '0.0.0.0': LISTEN_IP = '127.0.0.1' elif LISTEN_IP == '::': LISTEN_IP = '::1' elif LISTEN_IP == '': LINK_PROFILE = CONFIG.get('link', 'profile') if LINK_PROFILE not in ('ipv4', 'ipv6', 'ipv46'): LINK_PROFILE = 'ipv4' LISTEN_IP = '127.0.0.1' if '4' in LINK_PROFILE else '::1' LISTEN_GAE_PORT = CONFIG.getint('listen', 'gae_port', fallback=8086) LISTEN_AUTO_PORT = CONFIG.getint('listen', 'auto_port', fallback=8087) LISTEN_GAE = '%s:%d' % (LISTEN_IP, LISTEN_GAE_PORT) LISTEN_AUTO = '%s:%d' % (LISTEN_IP, LISTEN_AUTO_PORT) LOG_PRINT = CONFIG.getboolean('log', 'print', fallback=True) LOG_LEVEL = _LOGLv[min(CONFIG.getint('log', 'level', fallback=1), 3)] log_config = {'level': LOG_LEVEL} if not LOG_PRINT: log_config['stream'] = logging.NULL_STREAM logging.basicConfig(**log_config) return LISTEN_GAE, LISTEN_AUTO
def load_config(): patch_configparser() import re from configparser import ConfigParser _LOGLv = { 0: logging.WARNING, 1: logging.INFO, 2: logging.TEST, 3: logging.DEBUG } CONFIG = ConfigParser(dict_type=dict, inline_comment_prefixes=('#', ';')) CONFIG._optcre = re.compile( r'(?P<option>[^=\s]+)\s*(?P<vi>=?)\s*(?P<value>.*)') CONFIG.read([config_filename, config_user_filename]) LISTEN_IP = CONFIG.get('listen', 'ip') if LISTEN_IP == '0.0.0.0': LISTEN_IP = '127.0.0.1' elif LISTEN_IP == '::': LISTEN_IP = '::1' elif LISTEN_IP == '': LINK_PROFILE = CONFIG.get('link', 'profile') if LINK_PROFILE not in ('ipv4', 'ipv6', 'ipv46'): LINK_PROFILE = 'ipv4' LISTEN_IP = '127.0.0.1' if '4' in LINK_PROFILE else '::1' LISTEN_AUTOPORT = CONFIG.getint('listen', 'autoport', fallback=8087) LISTEN_ACTPORT = CONFIG.getint('listen', 'actport', fallback=8086) LISTEN_ACTTYPE = CONFIG.get('listen', 'act', fallback='cfw').upper() LISTEN_AUTO = '%s:%d' % (LISTEN_IP, LISTEN_AUTOPORT) LISTEN_ACT = '%s:%d' % (LISTEN_IP, LISTEN_ACTPORT) LOG_PRINT = CONFIG.getboolean('log', 'print', fallback=True) LOG_LEVEL = _LOGLv[min(CONFIG.getint('log', 'level', fallback=1), 3)] log_config = {'level': LOG_LEVEL} if not LOG_PRINT: log_config['stream'] = logging.NULL_STREAM logging.basicConfig(**log_config) return LISTEN_AUTO, LISTEN_ACT, LISTEN_ACTTYPE
continue if isip(addr): try: port = int(port) except: port = default_port yield addr, port def servers_2_addresses(servers, default_port): default_port = int(default_port) return tuple(_servers_2_addresses(servers, default_port)) #load config from proxy.ini #ENV_CONFIG_PREFIX = 'GOTOX_' CONFIG = ConfigParser(dict_type=dict, inline_comment_prefixes=('#', ';')) CONFIG._optcre = re.compile(r'(?P<option>[^=\s]+)\s*(?P<vi>=?)\s*(?P<value>.*)') class GC: GEVENT_LOOP = None CONFIG_FILENAME = os.path.join(config_dir, 'Config.ini') CONFIG_USER_FILENAME = os.path.join(config_dir, 'Config.user.ini') CONFIG_IPDB = os.path.join(data_dir, 'ip.use') CONFIG.read([CONFIG_FILENAME, CONFIG_USER_FILENAME, CONFIG_IPDB]) #load config from environment #for key, value in os.environ.items(): # m = re.match(r'^%s([A-Z]+)_([A-Z\_\-]+)$' % ENV_CONFIG_PREFIX, key) # if m: # CONFIG.set(m.group(1).lower(), m.group(2).lower(), value)
def readconfig(self): CONFIG = ConfigParser(inline_comment_prefixes=('#', ';')) CONFIG._optcre = re.compile( r'(?P<option>[^\s]+)(?P<vi>\s+=)?\s*(?P<value>.*)') CONFIG.read(self.CONFIG_FILENAME) sections = CONFIG.sections() order_sections = [] for section in sections: try: order, action = isfiltername(section).group('order', 'action') order_sections.append((int(order), action, section)) except: continue order_sections.sort(key=lambda x: x[0]) self.clear() for order, action, section in order_sections: action = action.upper() if action not in actToNum: continue filters = classlist() filters.action = actToNum[action] for k, v in CONFIG.items(section): scheme = '' if k.find('://', 0, 9) > 0: scheme, _, k = k.partition('://') host, _, path = k.partition('/') if host and host[0] == '@': host = re.compile(host[1:]).search else: host = host.lower() if path and path[0] == '@': path = re.compile(path[1:]).search if filters.action == FAKECERT and v and '*' not in v: v = v.encode() if filters.action in (FORWARD, DIRECT): if v and v[0] == '@': p, _, v = v.partition(' ') else: p = None if isempty(v): v = None elif '|' in v: v = pickip(v.lower()) or None elif isipuse(v): v = [v] elif isip(v) or not (v in GC.IPLIST_MAP or v.find('.') > 0): v = None v = v, p elif filters.action in (REDIRECT, IREDIRECT): if v and v[0] == '!': v = v[1:].lstrip(' \t') mhost = False else: mhost = True if '>>' in v: patterns, _, replaces = v.partition('>>') patterns = patterns.rstrip(' \t') replaces = replaces.lstrip(' \t') if ' ' in replaces: raction, _, replaces = replaces.partition(' ') if raction in ('forward', 'direct', 'gae'): raction = 'do_' + raction.upper() elif raction.startswith('proxy='): raction = 'do_PROXY', raction[6:] else: raction = None replaces = replaces.rstrip(' \t') else: raction = None unquote = replaces[0] == '@' if unquote: replaces = replaces[1:].lstrip(' \t') if patterns[0] == '@': patterns = patterns[1:].lstrip(' \t') rule = partial(re.compile(patterns).sub, replaces) else: rule = patterns, replaces, 1 v = rule, unquote, mhost, raction else: v = v, None, mhost, None filters.append((scheme.lower(), host, path, v)) self.append(filters)