'load config from proxy.ini'

def __init__(self):

    ConfigParser.RawConfigParser.OPTCRE = re.compile('(?P<option>\\S+)\\s+(?P<vi>[=])\\s+(?P<value>.*)$')
    self.CONFIG = ConfigParser.ConfigParser()
    self.CONFIG_FILENAME = (os.path.splitext(os.path.abspath(__file__))[0] + '.ini')
    self.CONFIG_USER_FILENAME = re.sub('\\.ini$', '.user.ini', self.CONFIG_FILENAME)
    self.CONFIG.read([self.CONFIG_FILENAME, self.CONFIG_USER_FILENAME])
    for (key, value) in os.environ.items():
        m = re.match(('^%s([A-Z]+)_([A-Z\\_\\-]+)$' % self.ENV_CONFIG_PREFIX), key)
        if m:
            self.CONFIG.set(m.group(1).lower(), m.group(2).lower(), value)
    self.LISTEN_IP = self.CONFIG.get('listen', 'ip')
    self.LISTEN_PORT = self.CONFIG.getint('listen', 'port')
    self.LISTEN_USERNAME = (self.CONFIG.get('listen', 'username') if self.CONFIG.has_option('listen', 'username') else '')
    self.LISTEN_PASSWORD = (self.CONFIG.get('listen', 'password') if self.CONFIG.has_option('listen', 'password') else '')
    self.LISTEN_VISIBLE = self.CONFIG.getint('listen', 'visible')
    self.LISTEN_DEBUGINFO = self.CONFIG.getint('listen', 'debuginfo')
    self.GAE_ENABLE = self.CONFIG.getint('gae', 'enable')
    self.GAE_APPIDS = re.findall('[\\w\\-\\.]+', self.CONFIG.get('gae', 'appid').replace('.appspot.com', ''))
    self.GAE_PASSWORD = self.CONFIG.get('gae', 'password').strip()
    self.GAE_PATH = self.CONFIG.get('gae', 'path')
    self.GAE_MODE = self.CONFIG.get('gae', 'mode')
    self.GAE_IPV6 = self.CONFIG.getint('gae', 'ipv6')
    self.GAE_WINDOW = self.CONFIG.getint('gae', 'window')
    self.GAE_KEEPALIVE = self.CONFIG.getint('gae', 'keepalive')
    self.GAE_CACHESOCK = self.CONFIG.getint('gae', 'cachesock')
    self.GAE_HEADFIRST = self.CONFIG.getint('gae', 'headfirst')
    self.GAE_OBFUSCATE = self.CONFIG.getint('gae', 'obfuscate')
    self.GAE_VALIDATE = self.CONFIG.getint('gae', 'validate')
    self.GAE_TRANSPORT = (self.CONFIG.getint('gae', 'transport') if self.CONFIG.has_option('gae', 'transport') else 0)
    self.GAE_OPTIONS = self.CONFIG.get('gae', 'options')
    self.GAE_REGIONS = set((x.upper() for x in self.CONFIG.get('gae', 'regions').split('|') if x.strip()))
    self.GAE_SSLVERSION = self.CONFIG.get('gae', 'sslversion')
    self.GAE_PAGESPEED = (self.CONFIG.getint('gae', 'pagespeed') if self.CONFIG.has_option('gae', 'pagespeed') else 0)
    if self.GAE_IPV6:
        sock = None
        try:
            sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
            sock.connect(('2001:4860:4860::8888', 53))
            logging.info('use    ipv6    interface    %s    for    gae', sock.getsockname()[0])
        except Exception as e:
            logging.info('Fail    try    use    ipv6    %r,    fallback    ipv4', e)
            self.GAE_IPV6 = 0
        finally:
            if sock:
                sock.close()
    if (('USERDNSDOMAIN' in os.environ) and re.match('^\\w+\\.\\w+$', os.environ['USERDNSDOMAIN'])):
        self.CONFIG.set('profile', ('.' + os.environ['USERDNSDOMAIN']), 'direct')
    withgae_sites = []
    withphp_sites = []
    crlf_sites = []
    nocrlf_sites = []
    forcehttps_sites = []
    noforcehttps_sites = []
    fakehttps_sites = []
    nofakehttps_sites = []
    dns_servers = []
    urlrewrite_map = collections.OrderedDict()
    rule_map = collections.OrderedDict()
    for (pattern, rule) in self.CONFIG.items('profile'):
        rules = [x.strip() for x in re.split('[,\\|]', rule) if x.strip()]
        if (rule.startswith(('file://', 'http://', 'https://')) or ('$1' in rule)):
            urlrewrite_map[pattern] = rule
            continue
        for (rule, sites) in [('withgae', withgae_sites), ('withphp', withphp_sites), ('crlf', crlf_sites), ('nocrlf', nocrlf_sites), ('forcehttps', forcehttps_sites), ('noforcehttps', noforcehttps_sites), ('fakehttps', fakehttps_sites), ('nofakehttps', nofakehttps_sites)]:
            if (rule in rules):
                sites.append(pattern)
                rules.remove(rule)
        if rules:
            rule_map[pattern] = rules[0]
    self.HTTP_DNS = dns_servers
    self.WITHGAE_SITES = tuple(withgae_sites)
    self.WITHPHP_SITES = tuple(withphp_sites)
    self.CRLF_SITES = tuple(crlf_sites)
    self.NOCRLF_SITES = set(nocrlf_sites)
    self.FORCEHTTPS_SITES = tuple(forcehttps_sites)
    self.NOFORCEHTTPS_SITES = set(noforcehttps_sites)
    self.FAKEHTTPS_SITES = tuple(fakehttps_sites)
    self.NOFAKEHTTPS_SITES = set(nofakehttps_sites)
    self.URLREWRITE_MAP = urlrewrite_map
    self.RULE_MAP = rule_map
    self.PAC_ENABLE = self.CONFIG.getint('pac', 'enable')
    self.PAC_IP = self.CONFIG.get('pac', 'ip')
    self.PAC_PORT = self.CONFIG.getint('pac', 'port')
    self.PAC_FILE = self.CONFIG.get('pac', 'file').lstrip('/')
    self.PAC_GFWLIST = self.CONFIG.get('pac', 'gfwlist')
    self.PAC_ADBLOCK = self.CONFIG.get('pac', 'adblock')
    self.PAC_ADMODE = self.CONFIG.getint('pac', 'admode')
    self.PAC_EXPIRED = self.CONFIG.getint('pac', 'expired')
    self.PHP_ENABLE = self.CONFIG.getint('php', 'enable')
    self.PHP_LISTEN = self.CONFIG.get('php', 'listen')
    self.PHP_PASSWORD = (self.CONFIG.get('php', 'password') if self.CONFIG.has_option('php', 'password') else '')
    self.PHP_CRLF = (self.CONFIG.getint('php', 'crlf') if self.CONFIG.has_option('php', 'crlf') else 1)
    self.PHP_VALIDATE = (self.CONFIG.getint('php', 'validate') if self.CONFIG.has_option('php', 'validate') else 0)
    self.PHP_KEEPALIVE = self.CONFIG.getint('php', 'keepalive')
    self.PHP_FETCHSERVER = self.CONFIG.get('php', 'fetchserver')
    self.PHP_HOSTS = (self.CONFIG.get('php', 'hosts').split('|') if self.CONFIG.get('php', 'hosts') else [])
    self.VPS_ENABLE = self.CONFIG.getint('vps', 'enable')
    self.VPS_LISTEN = self.CONFIG.get('vps', 'listen')
    self.VPS_FETCHSERVER = self.CONFIG.get('vps', 'fetchserver')
    self.PROXY_ENABLE = self.CONFIG.getint('proxy', 'enable')
    self.PROXY_AUTODETECT = (self.CONFIG.getint('proxy', 'autodetect') if self.CONFIG.has_option('proxy', 'autodetect') else 0)
    self.PROXY_HOST = self.CONFIG.get('proxy', 'host')
    self.PROXY_PORT = self.CONFIG.getint('proxy', 'port')
    self.PROXY_USERNAME = self.CONFIG.get('proxy', 'username')
    self.PROXY_PASSWROD = self.CONFIG.get('proxy', 'password')
    if ((not self.PROXY_ENABLE) and self.PROXY_AUTODETECT):
        system_proxy = ProxyUtil.get_system_proxy()
        if (system_proxy and (self.LISTEN_IP not in system_proxy)):
            (_, username, password, address) = ProxyUtil.parse_proxy(system_proxy)
            (proxyhost, _, proxyport) = address.rpartition(':')
            self.PROXY_ENABLE = 1
            self.PROXY_USERNAME = username
            self.PROXY_PASSWROD = password
            self.PROXY_HOST = proxyhost
            self.PROXY_PORT = int(proxyport)
    if self.PROXY_ENABLE:
        self.GAE_MODE = 'https'
    self.AUTORANGE_HOSTS = self.CONFIG.get('autorange', 'hosts').split('|')
    self.AUTORANGE_ENDSWITH = tuple(self.CONFIG.get('autorange', 'endswith').split('|'))
    self.AUTORANGE_NOENDSWITH = tuple(self.CONFIG.get('autorange', 'noendswith').split('|'))
    self.AUTORANGE_MAXSIZE = self.CONFIG.getint('autorange', 'maxsize')
    self.AUTORANGE_WAITSIZE = self.CONFIG.getint('autorange', 'waitsize')
    self.AUTORANGE_BUFSIZE = self.CONFIG.getint('autorange', 'bufsize')
    self.AUTORANGE_THREADS = self.CONFIG.getint('autorange', 'threads')
    self.FETCHMAX_LOCAL = (self.CONFIG.getint('fetchmax', 'local') if self.CONFIG.get('fetchmax', 'local') else 3)
    self.FETCHMAX_SERVER = self.CONFIG.get('fetchmax', 'server')
    self.DNS_ENABLE = self.CONFIG.getint('dns', 'enable')
    self.DNS_LISTEN = self.CONFIG.get('dns', 'listen')
    self.DNS_SERVERS = (self.HTTP_DNS or self.CONFIG.get('dns', 'servers').split('|'))
    self.DNS_BLACKLIST = set(self.CONFIG.get('dns', 'blacklist').split('|'))
    self.DNS_TCPOVER = (tuple(self.CONFIG.get('dns', 'tcpover').split('|')) if self.CONFIG.get('dns', 'tcpover').strip() else tuple())
    if self.GAE_IPV6:
        self.DNS_SERVERS = [x for x in self.DNS_SERVERS if (':' in x)]
    else:
        self.DNS_SERVERS = [x for x in self.DNS_SERVERS if (':' not in x)]
    self.USERAGENT_ENABLE = self.CONFIG.getint('useragent', 'enable')
    self.USERAGENT_STRING = self.CONFIG.get('useragent', 'string')
    self.LOVE_ENABLE = self.CONFIG.getint('love', 'enable')
    self.LOVE_TIP = self.CONFIG.get('love', 'tip').encode('utf8').decode('unicode-escape').split('|')
    self.GSCAN_TIME = time.time()
    self.GSCAN_IP_LIST = None
