From ddf9c012a13db7cb02459d58962b7c8264598f17 Mon Sep 17 00:00:00 2001 From: Alejandro Date: Tue, 2 Aug 2016 19:50:59 +0200 Subject: [PATCH] Supporting sending requests through an HTTP proxy (#2358) * Added proxy support Added proxy support from config.json and command line. with parameters: -prxip | --proxy_ip or proxy_ip from config.json for ipv4 format as string "aaa.bbb.ccc.ddd" -prxp| --proxy_port or proxy_por from config.json as int from 1 to 65536. * Added proxy support Added two additional parameters (proxy_ip and proxy_port) to add support for proxy. proxy_ip must be as string of ipv4 format: "aaa.bbb.ccc.ddd" , proxy_port must be a int from 1 to 65536, if proxy_ip is null or set to "" and proxy port is null or set to 0 proxy will be ignored and not used. * Moved proxy function to a method. Moved proxy function to a method. * Changed the name of method Changed from set_proxy_if_exists to setup_proxy --- configs/config.json.example | 2 ++ pokecli.py | 30 +++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/configs/config.json.example b/configs/config.json.example index 8020f38a0c..64c559cb7e 100644 --- a/configs/config.json.example +++ b/configs/config.json.example @@ -4,6 +4,8 @@ "password": "YOUR_PASSWORD", "location": "SOME_LOCATION", "gmapkey": "GOOGLE_MAPS_API_KEY", + "proxy_ip": "" , + "proxy_port": 0, "tasks": [ { "type": "HandleSoftBan" diff --git a/pokecli.py b/pokecli.py index e0c982f5e3..0281d938b4 100755 --- a/pokecli.py +++ b/pokecli.py @@ -322,6 +322,26 @@ def init_config(): type=float, default=5.0 ) + add_config( + parser, + load, + short_flag="-prxip", + long_flag="--proxy_ip", + help="Proxy (https and http)", + required=required("proxy_ip"), + type=str, + default=None + ) + add_config( + parser, + load, + short_flag="-prxp", + long_flag="--proxy_port", + help="Proxy port", + required=required("proxy_port"), + type=int, + default=None + ) # Start to parse other attrs config = parser.parse_args() @@ -329,7 +349,9 @@ def init_config(): config.username = raw_input("Username: ") if not config.password and 'password' not in load: config.password = getpass("Password: ") - + + setup_proxy(config) + config.catch = load.get('catch', {}) config.release = load.get('release', {}) config.action_wait_max = load.get('action_wait_max', 4) @@ -401,6 +423,12 @@ def task_configuration_error(flag_name): fix_nested_config(config) return config +def setup_proxy(config): + if config.proxy_ip and len(config.proxy_ip)>0 and config.proxy_ip.count('.') == 3 and all(0<=int(num)<256 for num in config.proxy_ip.rstrip().split('.')): + if config.proxy_port and int(config.proxy_port )<65536 and int(config.proxy_port )>1: + os.environ['http_proxy']="http://"+config.proxy_ip+":"+str(config.proxy_port)+"/" + os.environ['https_proxy']="http://"+config.proxy_ip+":"+str(config.proxy_port)+"/" + def add_config(parser, json_config, short_flag=None, long_flag=None, **kwargs): if not long_flag: raise Exception('add_config calls requires long_flag parameter!')