def find_proxies(self, url): """Parse proxy URL for provided URL Parse PAC and return proxy URL for given URL """ try: protocol = url.split('://')[0] except ValueError: raise ProxyConfigError('Invalid URL: %s' % url) try: pacparser.init() pacparser.parse_pac_string(str(self)) proxies = pacparser.find_proxy(url) pacparser.cleanup() except: raise ProxyConfigError('Error parsing PAC: %s' % self.pac_url) data = {} for v in [x.strip() for x in proxies.split(';')]: if v == 'DIRECT': continue if v[:6] == 'PROXY ': data[protocol] = v[6:] return data
def set_proxy(self): # proxy preference is used for all Pithos HTTP traffic # control proxy preference is used only for Pandora traffic and # overrides proxy # # If neither option is set, urllib2.build_opener uses urllib.getproxies() # by default handlers = [] global_proxy = self.preferences['proxy'] if global_proxy: handlers.append(urllib.request.ProxyHandler({'http': global_proxy, 'https': global_proxy})) global_opener = urllib.request.build_opener(*handlers) urllib.request.install_opener(global_opener) control_opener = global_opener control_proxy = self.preferences['control_proxy'] control_proxy_pac = self.preferences['control_proxy_pac'] if control_proxy: control_opener = urllib.request.build_opener(urllib.request.ProxyHandler({'http': control_proxy, 'https': control_proxy})) elif control_proxy_pac and pacparser_imported: pacparser.init() pacparser.parse_pac_string(urllib.request.urlopen(control_proxy_pac).read()) proxies = pacparser.find_proxy("http://pandora.com", "pandora.com").split(";") for proxy in proxies: match = re.search("PROXY (.*)", proxy) if match: control_proxy = match.group(1) break elif control_proxy_pac and not pacparser_imported: logging.warn("Disabled proxy auto-config support because python-pacparser module was not found.") self.worker_run('set_url_opener', (control_opener,))
def get_proxy_with_pac(url): proxy_str = None try: pacparser.init() pacparser.parse_pac('hk1.pac') proxy_str = pacparser.find_proxy(url) except: sys.stderr.write('could not find proxy for %s using this PAC file.\n' % url) return None # proxy_str = 'PROXY hkce01.hk.ibm.com:80; PROXY 9.181.193.210:80; DIRECT' proxy_list = proxy_str.split(';') proxies = {} for proxy in proxy_list: proxy = proxy.strip() if 'DIRECT' == proxy: continue if proxy[0:5].upper() == 'PROXY': proxy = proxy[6:].strip() if is_proxy_alive(proxy): proxies['http'] = proxy break sys.stdout.write('get proxy %s for %s\n' % (proxies, url)) return proxies
def set_proxy(self): # proxy preference is used for all Pithos HTTP traffic # control proxy preference is used only for Pandora traffic and # overrides proxy # # If neither option is set, urllib2.build_opener uses urllib.getproxies() # by default handlers = [] global_proxy = self.preferences["proxy"] if global_proxy: handlers.append(urllib.request.ProxyHandler({"http": global_proxy, "https": global_proxy})) global_opener = urllib.request.build_opener(*handlers) urllib.request.install_opener(global_opener) control_opener = global_opener control_proxy = self.preferences["control_proxy"] control_proxy_pac = self.preferences["control_proxy_pac"] if control_proxy: control_opener = urllib.request.build_opener( urllib.request.ProxyHandler({"http": control_proxy, "https": control_proxy}) ) elif control_proxy_pac and pacparser_imported: pacparser.init() pacparser.parse_pac_string(urllib.request.urlopen(control_proxy_pac).read()) proxies = pacparser.find_proxy("http://pandora.com", "pandora.com").split(";") for proxy in proxies: match = re.search("PROXY (.*)", proxy) if match: control_proxy = match.group(1) break self.worker_run("set_url_opener", (control_opener,))
def set_proxy(self): # proxy preference is used for all Pithos HTTP traffic # control proxy preference is used only for Pandora traffic and # overrides proxy # # If neither option is set, urllib2.build_opener uses urllib.getproxies() # by default handlers = [] global_proxy = self.preferences['proxy'] if global_proxy: handlers.append(urllib.request.ProxyHandler({'http': global_proxy, 'https': global_proxy})) global_opener = urllib.request.build_opener(*handlers) urllib.request.install_opener(global_opener) control_opener = global_opener control_proxy = self.preferences['control_proxy'] control_proxy_pac = self.preferences['control_proxy_pac'] if control_proxy: control_opener = urllib.request.build_opener(urllib.request.ProxyHandler({'http': control_proxy, 'https': control_proxy})) elif control_proxy_pac and pacparser_imported: pacparser.init() pacparser.parse_pac_string(urllib.request.urlopen(control_proxy_pac).read()) proxies = pacparser.find_proxy("http://pandora.com", "pandora.com").split(";") for proxy in proxies: match = re.search("PROXY (.*)", proxy) if match: control_proxy = match.group(1) break self.worker_run('set_url_opener', (control_opener,))
def find( site, pacfile, myip = "" ): pacparser.init() pac_file_cache = cache_pacfile.cache(pacfile) if not myip == "": pacparser.setmyip(myip) pacparser.parse_pac(pac_file_cache) return pacparser.find_proxy(site)
def proxy_test(pac_string, myip, url): pp.init() if myip: pp.setmyip(myip) if not url: url = 'http://www.google.com' pp._pacparser.parse_pac_string(pac_string) proxy = pp.find_proxy(url) return proxy
def main_test(filename, test_times): pacparser.init() pacparser.parse_pac(filename) beg_time = time.time() for i in xrange(test_times): ret_str = pacparser.find_proxy('http://www.coding.com', 'www.coding.com') # using the worst case end_time = time.time() print "%s:\nTotal Time: %s s\nAvg. Time: %s ms\n\n" % (filename, end_time - beg_time, (end_time - beg_time) * 1000.0 / test_times), pacparser.cleanup()
def set_proxy(self, *ignore, reconnect=True): # proxy preference is used for all Pithos HTTP traffic # control proxy preference is used only for Pandora traffic and # overrides proxy # # If neither option is set, urllib2.build_opener uses urllib.getproxies() # by default handlers = [] global_proxy = self.settings['proxy'] if global_proxy: handlers.append( urllib.request.ProxyHandler({ 'http': global_proxy, 'https': global_proxy })) global_opener = pandora.Pandora.build_opener(*handlers) urllib.request.install_opener(global_opener) control_opener = global_opener control_proxy = self.settings['control-proxy'] control_proxy_pac = self.settings['control-proxy-pac'] if not control_proxy and (control_proxy_pac and pacparser): pacparser.init() with urllib.request.urlopen(control_proxy_pac) as f: pacstring = f.read().decode('utf-8') try: pacparser.parse_pac_string(pacstring) except pacparser._pacparser.error: logging.warning('Failed to parse PAC.') try: proxies = pacparser.find_proxy("http://pandora.com", "pandora.com").split(";") for proxy in proxies: match = re.search("PROXY (.*)", proxy) if match: control_proxy = match.group(1) break except pacparser._pacparser.error: logging.warning('Failed to find proxy via PAC.') pacparser.cleanup() elif not control_proxy and (control_proxy_pac and not pacparser): logging.warning( "Disabled proxy auto-config support because python-pacparser module was not found." ) if control_proxy: control_opener = pandora.Pandora.build_opener( urllib.request.ProxyHandler({ 'http': control_proxy, 'https': control_proxy })) self.worker_run('set_url_opener', (control_opener, ), self.pandora_connect if reconnect else None)
def index(pacfile=None): myip = request.args.get('myip', '') url = request.args.get('url', 'http://www.google.com') pp.init() pp.setmyip(myip) pp.parse_pac('test.pac') proxy = pp.find_proxy(url) return proxy
def main_test(filename, test_times): pacparser.init() pacparser.parse_pac(filename) beg_time = time.time() for i in xrange(test_times): ret_str = pacparser.find_proxy( 'http://www.coding.com', 'www.coding.com') # using the worst case end_time = time.time() print "%s:\nTotal Time: %s s\nAvg. Time: %s ms\n\n" % ( filename, end_time - beg_time, (end_time - beg_time) * 1000.0 / test_times), pacparser.cleanup()
def runtests(pacfile, testdata, tests_dir): py_ver = '.'.join([str(x) for x in sys.version_info[0:2]]) if sys.platform == 'win32': pacparser_module_path = os.path.join( tests_dir, '..', 'src', 'pymod', 'pacparser-python%s' % sysconfig.get_config_vars('VERSION')[0]) if os.path.exists(os.path.join(pacparser_module_path, '_pacparser.pyd')): raise Exception('Tests failed. Could not determine pacparser path.') else: try: pacparser_module_path = glob.glob(os.path.join( tests_dir, '..', 'src', 'pymod', 'build', 'lib*%s' % py_ver))[0] except Exception: raise Exception('Tests failed. Could not determine pacparser path.') if 'DEBUG' in os.environ: print('Pacparser module path: %s' % pacparser_module_path) sys.path.insert(0, pacparser_module_path) try: import pacparser except ImportError: raise Exception('Tests failed. Could not import pacparser.') if 'DEBUG' in os.environ: print('Imported pacparser module: %s' % sys.modules['pacparser']) f = open(testdata) for line in f: comment = '' if '#' in line: comment = line.split('#', 1)[1] line = line.split('#', 1)[0].strip() if not line: continue if ('NO_INTERNET' in os.environ and os.environ['NO_INTERNET'] and 'INTERNET_REQUIRED' in comment): continue if 'DEBUG' in os.environ: print(line) (params, expected_result) = line.strip().split('|') args = dict(getopt.getopt(params.split(), 'eu:c:')[0]) if '-e' in args: pacparser.enable_microsoft_extensions() if '-c' in args: pacparser.setmyip(args['-c']) pacparser.init() pacparser.parse_pac_file(pacfile) result = pacparser.find_proxy(args['-u']) pacparser.cleanup() if result != expected_result: raise Exception('Tests failed. Got "%s", expected "%s"' % (result, expected_result)) print('All tests were successful.')
def runtests(pacfile, testdata, tests_dir): py_ver = '.'.join([str(x) for x in sys.version_info[0:2]]) if sys.platform == 'win32': pacparser_module_path = os.path.join(tests_dir, '..', 'src', 'pymod', 'dist') if os.path.exists(os.path.join(pacparser_module_path, '_pacparser.pyd')): raise Exception('Tests failed. Could not determine pacparser path.') else: try: pacparser_module_path = glob.glob(os.path.join( tests_dir, '..', 'src', 'pymod', 'build', 'lib*%s' % py_ver))[0] except Exception: raise Exception('Tests failed. Could not determine pacparser path.') if 'DEBUG' in os.environ: print('Pacparser module path: %s' % pacparser_module_path) sys.path.insert(0, pacparser_module_path) try: import pacparser except ImportError: raise Exception('Tests failed. Could not import pacparser.') if 'DEBUG' in os.environ: print('Imported pacparser module: %s' % sys.modules['pacparser']) f = open(testdata) for line in f: comment = '' if '#' in line: comment = line.split('#', 1)[1] line = line.split('#', 1)[0].strip() if not line: continue if ('NO_INTERNET' in os.environ and os.environ['NO_INTERNET'] and 'INTERNET_REQUIRED' in comment): continue if 'DEBUG' in os.environ: print(line) (params, expected_result) = line.strip().split('|') args = dict(getopt.getopt(params.split(), 'eu:c:')[0]) if '-e' in args: pacparser.enable_microsoft_extensions() if '-c' in args: pacparser.setmyip(args['-c']) pacparser.init() pacparser.parse_pac_file(pacfile) result = pacparser.find_proxy(args['-u']) pacparser.cleanup() if result != expected_result: raise Exception('Tests failed. Got "%s", expected "%s"' % (result, expected_result)) print('All tests were successful.')
def test_url(url, expected): """Test a URL against the PAC file. Returns 0 if the test passes, 1 if it fails""" # Find the proxy defined by the PAC file for the URL result = pacparser.find_proxy(url, urlparse(url).hostname) v_print(1, "result: {}".format(result)) # Compare result if (result == expected): v_print(2, "OK - {}".format(url)) return 0 else: v_print(2, "FAIL - {}".format(url)) return 1
def __get_proxies(self, url, host=None): """ :return: a string of proxies from FindProxyForURL() in pac file js or 'None' if we can't process the url. """ if not self.fetched: self.__parse_pac() try: proxy_str = pacparser.find_proxy(url, host) except pacparser.URLError as e: g_log.error(e) return None else: return parse_pac_value(proxy_str, self.proxy_auth)
def set_proxy(self, *ignore): # proxy preference is used for all Pithos HTTP traffic # control proxy preference is used only for Pandora traffic and # overrides proxy # # If neither option is set, urllib2.build_opener uses urllib.getproxies() # by default handlers = [] global_proxy = self.settings.get_string('proxy') if global_proxy: handlers.append(urllib.request.ProxyHandler({'http': global_proxy, 'https': global_proxy})) global_opener = urllib.request.build_opener(*handlers) urllib.request.install_opener(global_opener) control_opener = global_opener control_proxy = self.settings.get_string('control-proxy') control_proxy_pac = self.settings.get_string('control-proxy-pac') if not control_proxy and (control_proxy_pac and pacparser): pacparser.init() with urllib.request.urlopen(control_proxy_pac) as f: pacstring = f.read().decode('utf-8') try: pacparser.parse_pac_string(pacstring) except pacparser._pacparser.error: logging.warning('Failed to parse PAC.') try: proxies = pacparser.find_proxy("http://pandora.com", "pandora.com").split(";") for proxy in proxies: match = re.search("PROXY (.*)", proxy) if match: control_proxy = match.group(1) break except pacparser._pacparser.error: logging.warning('Failed to find proxy via PAC.') pacparser.cleanup() elif not control_proxy and (control_proxy_pac and not pacparser): logging.warning("Disabled proxy auto-config support because python-pacparser module was not found.") if control_proxy: control_opener = urllib.request.build_opener(urllib.request.ProxyHandler({'http': control_proxy, 'https': control_proxy})) self.worker_run('set_url_opener', (control_opener,))
def find_proxy(self, host): self.__lock.acquire() try: import pacparser pacparser.init() pacparser.parse_pac_string(self.__pac_string) results = pacparser.find_proxy('http://%s' % host, host) pacparser.cleanup() finally: self.__lock.release() dests = [] for result in results.split(';'): result = result.strip() if result.startswith('PROXY'): host, port = result.split(' ')[1].split(':') dests.append(Address(host, port)) elif result.startswith('DIRECT'): dests.append(None) getLogger().write('Proxy for "%s" -> %s' % (host, dests), Log.DEBUG) return dests
#!/usr/bin/python2.5 import pacparser pacparser.init() pacparser.parse_pac("wpad.dat") proxy = pacparser.find_proxy("http://www.manugarg.com") print proxy pacparser.cleanup() # Or simply, print pacparser.just_find_proxy("wpad.dat", "http://www2.manugarg.com")
def run_command(command): #print "bash: %s" % command p = subprocess.Popen(command, stdout=subprocess.PIPE, shell=isinstance(command, str)) return iter(p.stdout.readline, b'') if __name__ == '__main__': # get current proxy server pacparser.init() pacparser.parse_pac(pac_file) output = pacparser.find_proxy('http://captive.apple.com', 'captive.apple.com') proxies = output.split("; ") proxies = [p.replace('PROXY ', '') for p in proxies] (main_proxy_url, main_proxy_port) = proxies[0].split(":") print "### Proxy set to: %s, port %s" % (main_proxy_url, main_proxy_port) # Create a new network location so we don't break existing settings if location_name in run_command(list_locations_cmd): run_command(delete_location_cmd) run_command(create_location_cmd) run_command(switch_location_cmd) # Get list of all the interfaces list_network_services = run_command(list_interfaces_cmd) for net_service in list_network_services: net_service = net_service.strip()
def test(request): if not request.user.is_authenticated(): return HttpResponseRedirect("/logon/") if request.POST: c = {} c.update(csrf(request)) pac = "function FindProxyForURL(url, host){\n" form = Form_test(request.POST) if form.is_valid(): networks = source.objects.all() network =[] ipsource = form.cleaned_data['address_ip'] destination = form.cleaned_data['destination'] network_save = 0 match2 =[] cidr2 =[] for network in networks: cidr = [str(network.address_ip) +"/"+ str(network.mask)] match = smallest_matching_cidr(ipsource,cidr) match2.append(match) cidr2.append(cidr) if match: try: if ((int(str(network.mask))) > (int(str(network_save.mask)))): network_save = network del match except AttributeError: network_save = network del match try: tree = network_save.get_ancestors(include_self=True,ascending=True) for network_node in tree: rulepac = rules.objects.get(ref_address_ip = network_node) pac += rulepac.pac +"\n" pac += "}" p = re.compile('[\r\n\t]+') requette = p.sub( "", pac) p = re.compile('[\s]') requette = p.sub( " ", requette) except AttributeError: if (IPNetwork(str(ipsource)).version == 6): network_save = source.objects.get(address_ip='fe80::') else: network_save = source.objects.get(address_ip='0.0.0.0') rulepac = rules.objects.get(ref_address_ip = network_save) pac += rulepac.pac +"\n" pac += "}" p = re.compile('[\r\n\t]+') requette = p.sub( "", pac) p = re.compile('[\s]') requette = p.sub( " ", requette) pacparser.init() parse = pacparser.parse_pac_string(str(requette)) proxy = pacparser.find_proxy(str(destination)) pacparser.cleanup() return render_to_response('test.html', locals(),context_instance=RequestContext(request)) else: return render_to_response('test.html', {'form': form,},context_instance=RequestContext(request)) else: form = Form_test() return render_to_response('test.html', {'form': form,},context_instance=RequestContext(request))
def get_pac_result(filename, url, host): pacparser.init() pacparser.parse_pac(filename) ret_str = pacparser.find_proxy(url, host) pacparser.cleanup() return ret_str
#!/usr/bin/env python import pacparser,sys print sys.argv[1] if len(sys.argv) >= 3: pacfile=sys.argv[2] print "use: ", pacfile else: pacfile="proxy.pac" pacparser.init() pacparser.parse_pac(pacfile) proxy = pacparser.find_proxy(sys.argv[1]) print proxy pacparser.cleanup() # Or simply, print pacparser.just_find_proxy(pacfile, sys.argv[1])
#!/usr/bin/env python import pacparser, sys print sys.argv[1] if len(sys.argv) >= 3: pacfile = sys.argv[2] print "use: ", pacfile else: pacfile = "proxy.pac" pacparser.init() pacparser.parse_pac(pacfile) proxy = pacparser.find_proxy(sys.argv[1]) print proxy pacparser.cleanup() # Or simply, print pacparser.just_find_proxy(pacfile, sys.argv[1])
def process(self): method = self.method.decode('ascii') uri = self.uri.decode('ascii') if method == 'CONNECT': host, port = split_host_port(uri) port = int(port) else: parsed = urllib_parse.urlparse(self.uri) decoded = parsed[1].decode('ascii') host, port = split_host_port(decoded) if port is None: port = 80 rest = urllib_parse.urlunparse((b'', b'') + parsed[2:]) if not rest: rest = rest + b'/' headers = self.getAllHeaders().copy() self.content.seek(0, 0) s = self.content.read() proxy_suggestion = (self.force_proxy or self.force_direct or pacparser.find_proxy('http://{}'.format(host))) proxy_suggestions = proxy_suggestion.split(";") parsed_proxy_suggestion = self.proxy_suggestion_parser.match( proxy_suggestions[0]) if parsed_proxy_suggestion: connect_method, destination = parsed_proxy_suggestion.groups() if connect_method == 'PROXY': proxy_host, proxy_port = destination.split(":") proxy_port = int(proxy_port) if method != 'CONNECT': clientFactory = proxy.ProxyClientFactory( self.method, self.uri, self.clientproto, headers, s, self, ) logger.info('%s %s; forwarding request to %s:%s', method, uri, proxy_host, proxy_port) else: self.transport.unregisterProducer() self.transport.pauseProducing() rawConnectionProtocol = portforward.Proxy() rawConnectionProtocol.transport = self.transport self.transport.protocol = rawConnectionProtocol clientFactory = CONNECTProtocolForwardFactory(host, port) clientFactory.setServer(rawConnectionProtocol) logger.info('%s %s; establishing tunnel through %s:%s', method, uri, proxy_host, proxy_port) self.reactor.connectTCP(proxy_host, proxy_port, clientFactory) return else: # can this be anything else? Let's fall back to the DIRECT # codepath. pass if method != 'CONNECT': if b'host' not in headers: headers[b'host'] = host.encode('ascii') clientFactory = proxy.ProxyClientFactory( self.method, rest, self.clientproto, headers, s, self, ) logger.info('%s %s; forwarding request', method, uri) self.reactor.connectTCP(host, port, clientFactory) else: # hack/trick to move responsibility for this connection # away from a HTTP protocol class hierarchy and to a # port forward hierarchy self.transport.unregisterProducer() self.transport.pauseProducing() rawConnectionProtocol = portforward.Proxy() rawConnectionProtocol.transport = self.transport self.transport.protocol = rawConnectionProtocol clientFactory = portforward.ProxyClientFactory() clientFactory.setServer(rawConnectionProtocol) clientFactory.protocol = CONNECTProtocolClient # we don't do connectSSL, as the handshake is taken # care of by the client, and we only forward it logger.info('%s %s; establishing tunnel to %s:%s', method, uri, host, port) self.reactor.connectTCP(host, port, clientFactory)
def main(args): ''' Parse arguments into options dictionary ''' parser = argparse.ArgumentParser(prog='todoist-add', description='Add task to Todoist') parser.add_argument("--config", action="store", help="Configuration file with API") parser.add_argument("--item", action="store", help="Item text", required=True) parser.add_argument("--note", action="store", help="Note text") parser.add_argument("--date", action="store", help="Date text - any format Todoist can parse") options = vars(parser.parse_args(sys.argv[1:])) ''' Load user configuration file for API key etal ''' if options['config']: cf = options['config'] else: cf = os.getenv('HOME') + '/.todoist-cli' config = ConfigParser.RawConfigParser() files_read = config.read(cf) if files_read == []: print "Unable to open configuration file " + cf + "- aborting" sys.exit(1) ''' [Authentication] api=xxxx [Network] proxy_pac=zzzz http_proxy=xxxx https_proxy=yyyy ''' api_key = config.get('Authentication', 'api') if api_key == '': print "Unable to read API value from " + cf + "- aborting" sys.exit(1) if config.has_section('Network'): if config.has_option('Network', 'proxy_pac'): proxy_pac = config.get('Network', 'proxy_pac') if proxy_pac != '': ''' Check if proxy pac exists - if so use it to set proxy ''' try: response = urllib2.urlopen(proxy_pac, None, 1) pac_file_contents = response.read() pacparser.init() pacparser.parse_pac_string(pac_file_contents) https_proxy = pacparser.find_proxy('https://todoist.com', 'todoist.com').split(' ')[1] pacparser.cleanup() ''' Set the proxy environment ''' os.environ['HTTP_PROXY'] = https_proxy os.environ['HTTPS_PROXY'] = https_proxy except IOError as e: print e else: ''' Check if explicit proxy exists and use ''' http_proxy = config.get('Network', 'http_proxy') if http_proxy != '': os.environ['HTTP_PROXY'] = http_proxy https_proxy = config.get('Network', 'https_proxy') if https_proxy != '': os.environ['HTTPS_PROXY'] = https_proxy ''' Use the user Todoist API key to connect ''' api = todoist.TodoistAPI(api_key) if api: if options['date']: item = api.items.add(options['item'], 0, date_string=options['date']) else: item = api.items.add(options['item'], 0) if debug: print item if options['note']: note = api.notes.add(item['id'], options['note']) if debug: print note ''' Commit the transaction to Todoist ''' result = api.commit() if debug: print "Result:" print result print "Settings:" print "API Key=" + api_key if proxy_pac: print "PAC=" + proxy_pac print "HTTP_PROXY=" + os.getenv('HTTP_PROXY', "Not set") print "HTTPS_PROXY=" + os.getenv('HTTPS_PROXY', "Not set") else: print "Unable to connect to Todoist with API key - aborting" print api sys.exit(1)