Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proxy usage option, take 2 #2302

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions docs/first-run/commandline.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
[-nr] [-nk] [-ss [SPAWNPOINT_SCANNING]] [-speed] [-spin]
[-ams ACCOUNT_MAX_SPINS] [-kph KPH] [-hkph HLVL_KPH]
[-ldur LURE_DURATION] [--dump-spawnpoints]
[-pd PURGE_DATA] [-px PROXY] [-pxsc]
[-pd PURGE_DATA] [-px PROXY] [-pxsc] [-pxu PROXY_USAGE]
[-pxt PROXY_TEST_TIMEOUT] [-pxre PROXY_TEST_RETRIES]
[-pxbf PROXY_TEST_BACKOFF_FACTOR]
[-pxc PROXY_TEST_CONCURRENCY] [-pxd PROXY_DISPLAY]
Expand Down Expand Up @@ -57,7 +57,7 @@
ConfigArgParse documentation. If an arg is specified in more than one place,
then commandline values override environment variables which override config
file values which override defaults.

optional arguments:
-h, --help show this help message and exit [env var:
POGOMAP_HELP]
Expand Down Expand Up @@ -285,6 +285,9 @@
-pxsc, --proxy-skip-check
Disable checking of proxies before start. [env var:
POGOMAP_PROXY_SKIP_CHECK]
-pxu PROXY_USAGE, --proxy-usage PROXY_USAGE
Select which connections use proxy: ptc, niantic
or all. [env var: POGOMAP_PROXY_USAGE]
-pxt PROXY_TEST_TIMEOUT, --proxy-test-timeout PROXY_TEST_TIMEOUT
Timeout settings for proxy checker in seconds. [env
var: POGOMAP_PROXY_TEST_TIMEOUT]
Expand Down Expand Up @@ -413,4 +416,3 @@
logs. [env var: POGOMAP_NO_FILE_LOGS]
--log-path LOG_PATH Defines directory to save log files to. [env var:
POGOMAP_LOG_PATH]

28 changes: 20 additions & 8 deletions pogom/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,10 @@ def setup_api(args, status, account):
api = PGoApiWrapper(PGoApi(device_info=device_info))

# New account - new proxy.
if args.proxy:
if args.proxy and args.proxy_usage != 'ptc':
# If proxy is not assigned yet or if proxy-rotation is defined
# - query for new proxy.
if ((not status['proxy_url']) or
(args.proxy_rotation != 'none')):
if not status['proxy_url'] or args.proxy_rotation != 'none':

proxy_num, status['proxy_url'] = get_new_proxy(args)
if args.proxy_display.upper() != 'FULL':
Expand All @@ -60,16 +59,16 @@ def setup_api(args, status, account):
'https': status['proxy_url']})
if (status['proxy_url'] not in args.proxy):
log.warning(
'Tried replacing proxy %s with a new proxy, but proxy ' +
'rotation is disabled ("none"). If this isn\'t intentional, ' +
'enable proxy rotation.',
'Tried replacing session proxy %s with a new proxy, but ' +
'proxy rotation is disabled ("none"). If this isn\'t ' +
'intentional, enable proxy rotation.',
status['proxy_url'])

return api


# Use API to check the login status, and retry the login if possible.
def check_login(args, account, api, proxy_url):
def check_login(args, account, api):
# Logged in? Enough time left? Cool!
if api._auth_provider and api._auth_provider._ticket_expire:
remaining_time = api._auth_provider._ticket_expire / 1000 - time.time()
Expand All @@ -86,7 +85,20 @@ def check_login(args, account, api, proxy_url):
# One initial try + login_retries.
while num_tries < (args.login_retries + 1):
try:
if proxy_url:
if args.proxy and args.proxy_usage != 'niantic':
# If we still dont have any auth proxy configured or
# proxy rotation is set, we change the proxy
if not api._auth_provider or args.proxy_rotation != 'none':
proxy_idx, proxy_url = get_new_proxy(args)
elif api._auth_provider:
proxy_url = api._auth_provider._session.proxies['http']
if proxy_url not in args.proxy:
log.warning(
'Tried replacing auth proxy %s with a new ' +
'proxy, but proxy rotation is disabled ' +
'("none"). If this isn\'t intentional, enable ' +
'proxy rotation.',
proxy_url)
api.set_authentication(
provider=account['auth_service'],
username=account['username'],
Expand Down
4 changes: 2 additions & 2 deletions pogom/captcha.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def captcha_solver_thread(args, account_queue, account_captchas, hash_key,
api.activate_hash_server(hash_key)

proxy_url = False
if args.proxy:
if args.proxy and args.proxy_usage != 'ptc':
# Try to fetch a new proxy
proxy_num, proxy_url = get_new_proxy(args)

Expand All @@ -138,7 +138,7 @@ def captcha_solver_thread(args, account_queue, account_captchas, hash_key,
location = jitter_location(location)

api.set_position(*location)
check_login(args, account, api, proxy_url)
check_login(args, account, api)

wh_message = {'status_name': args.status_name,
'status': 'error',
Expand Down
7 changes: 2 additions & 5 deletions pogom/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2386,15 +2386,12 @@ def encounter_pokemon(args, pokemon, account, api, account_sets, status,

# If the already existent API is using a proxy but
# it's not alive anymore, we need to get a new proxy.
elif (args.proxy and
elif (args.proxy and args.proxy_usage != 'ptc' and
(hlvl_api._session.proxies['http'] not in args.proxy)):
proxy_idx, proxy_new = get_new_proxy(args)
hlvl_api.set_proxy({
'http': proxy_new,
'https': proxy_new})
hlvl_api._auth_provider.set_proxy({
'http': proxy_new,
'https': proxy_new})

# Hashing key.
# TODO: Rework inefficient threading.
Expand All @@ -2411,7 +2408,7 @@ def encounter_pokemon(args, pokemon, account, api, account_sets, status,
hlvl_api.set_position(*scan_location)

# Log in.
check_login(args, hlvl_account, hlvl_api, status['proxy_url'])
check_login(args, hlvl_account, hlvl_api)
encounter_level = hlvl_account['level']

# User error -> we skip freeing the account.
Expand Down
4 changes: 2 additions & 2 deletions pogom/pgorequestwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ def call(self, *args, **kwargs):
# the proxy worked. The variable "rotate_proxy" may seem
# unnecessary, but it's here for readability and avoiding problems
# in the future.
if rotate_proxy and self.args.proxy:
if (rotate_proxy and self.args.proxy and
self.args.proxy_usage != 'ptc'):
proxy_idx, proxy_url = get_new_proxy(get_args())

if proxy_url:
Expand All @@ -103,7 +104,6 @@ def call(self, *args, **kwargs):
}
parent = self.request.__parent__
parent.set_proxy(proxy_config)
parent._auth_provider.set_proxy(proxy_config)

# If we've reached here, we have no retries left and an exception
# still occurred.
Expand Down
26 changes: 19 additions & 7 deletions pogom/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# Evaluates the status of PTC and Niantic request futures, and returns the
# result (optionally with an error).
# Warning: blocking! Can only get status code if request has finished.
def get_proxy_test_status(proxy, future_ptc, future_niantic):
def get_proxy_test_status(proxy, args, future_ptc, future_niantic):
# Start by assuming everything is OK.
check_result = check_result_ok
proxy_error = None
Expand All @@ -39,8 +39,10 @@ def get_proxy_test_status(proxy, future_ptc, future_niantic):

# Make sure both requests are completed.
try:
ptc_response = future_ptc.result()
niantic_response = future_niantic.result()
if args.proxy_usage != 'niantic':
ptc_response = future_ptc.result()
if args.proxy_usage != 'ptc':
niantic_response = future_niantic.result()
except requests.exceptions.ConnectTimeout:
proxy_error = ('Connection timeout for'
+ ' proxy {}.').format(proxy)
Expand All @@ -57,8 +59,15 @@ def get_proxy_test_status(proxy, future_ptc, future_niantic):
return (proxy_error, check_result)

# Evaluate response status code.
ptc_status = ptc_response.status_code
niantic_status = niantic_response.status_code
if args.proxy_usage == 'niantic':
ptc_status = 200
niantic_status = niantic_response.status_code
elif args.proxy_usage == 'ptc':
ptc_status = ptc_response.status_code
niantic_status = 200
else:
ptc_status = ptc_response.status_code
niantic_status = niantic_response.status_code

banned_status_codes = [403, 409]

Expand All @@ -81,8 +90,10 @@ def get_proxy_test_status(proxy, future_ptc, future_niantic):

# Explicitly release connection back to the pool, because we don't need
# or want to consume the content.
ptc_response.close()
niantic_response.close()
if args.proxy_usage != 'niantic':
ptc_response.close()
if args.proxy_usage != 'ptc':
niantic_response.close()

return (proxy_error, check_result)

Expand Down Expand Up @@ -220,6 +231,7 @@ def check_proxies(args, proxies):
# blocking to wait for one. The double loop is intentional.
for proxy, future_ptc, future_niantic in proxy_queue:
error, result = get_proxy_test_status(proxy,
args,
future_ptc,
future_niantic)

Expand Down
7 changes: 4 additions & 3 deletions pogom/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,8 @@ def search_worker_thread(args, account_queue, account_sets,
# If used proxy disappears from "live list" after background
# checking - switch account but do not freeze it (it's not an
# account failure).
if args.proxy and status['proxy_url'] not in args.proxy:
if (args.proxy and args.proxy_usage != 'ptc' and
status['proxy_url'] not in args.proxy):
status['message'] = (
'Account {} proxy {} is not in a live list any ' +
'more. Switching accounts...').format(
Expand Down Expand Up @@ -932,7 +933,7 @@ def search_worker_thread(args, account_queue, account_sets,

# Ok, let's get started -- check our login status.
status['message'] = 'Logging in...'
check_login(args, account, api, status['proxy_url'])
check_login(args, account, api)

# Only run this when it's the account's first login, after
# check_login().
Expand Down Expand Up @@ -1252,7 +1253,7 @@ def get_api_version(args):
"""
proxies = {}

if args.proxy:
if args.proxy and args.proxy_usage != 'ptc':
num, proxy = get_new_proxy(args)
proxies = {
'http': proxy,
Expand Down
4 changes: 4 additions & 0 deletions pogom/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ def get_args():
parser.add_argument('-px', '--proxy',
help='Proxy url (e.g. socks5://127.0.0.1:9050)',
action='append')
parser.add_argument('-pxu', '--proxy-usage',
help=('Select which connections use proxy ' +
'(all/ptc/niantic).'),
type=str, default='all')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the code I think that using a repeated value like wh-types it is better (also add valid values) that way the ifs would be if 'ptc' in args.proxy_usage instead of ìf args.proxy_usage != 'niantic'` also it is forward compatibe if in there future more proxy controls are needed (wno knows).
The problem is that I think you can not set the default value to both, but we can do that in code, set default to None and if there are no values defined then just add all of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact I chose that approach because I couldn't find a way to set default values to none in a repeated value. This way behaves the same as proxy rotation variable, even if you spell it wrong the default behaviour is the current one.

parser.add_argument('-pxsc', '--proxy-skip-check',
help='Disable checking of proxies before start.',
action='store_true', default=False)
Expand Down