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

Allows Go Hash proxy hash server. #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions aiopogo/__init__.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ def close_sessions():
HashServer.close_session()


def activate_hash_server(hash_token, conn_limit=300):
def activate_hash_server(hash_token, conn_limit=300, go_hash=False):
HashServer.set_token(hash_token)
HashServer.activate_session(conn_limit)
HashServer.activate_session(conn_limit, go_hash)
43 changes: 36 additions & 7 deletions aiopogo/hash_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
class HashServer:
_session = None
multi = False
goHash = False
loop = get_event_loop()
status = {}
log = getLogger('hashing')
endPointUrl = "http://pokehash.buddyauth.com/api/v137_1/hash"

def __init__(self):
try:
Expand All @@ -38,12 +40,20 @@ async def hash(self, timestamp, latitude, longitude, accuracy, authticket, sessi
iteration += 1
else:
self.log.info('Out of hashes, waiting for new period.')
await sleep(status['refresh'] - time() + 1, loop=self.loop)
break
if not self.goHash:
await sleep(status['refresh'] - time() + 1, loop=self.loop)
break
# Go Hash doesn't have an expiry period so you would top up credit, we will try again in 5 seconds
else:
await sleep(5, loop=self.loop)
break
except KeyError:
pass
headers = {'X-AuthToken': self.instance_token}


headers = {'X-AuthToken': self.instance_token}
if self.goHash:
# extra header ensures no more than 5000 hashes per minute when using Go Hash. You can up this if your CPU can handle it
headers = {'X-AuthToken': self.instance_token, 'X-RateLimit':'5000'}
payload = {
'Timestamp': timestamp,
'Latitude64': f2i(latitude),
Expand All @@ -57,7 +67,7 @@ async def hash(self, timestamp, latitude, longitude, accuracy, authticket, sessi
# request hashes from hashing server
for attempt in range(3):
try:
async with self._session.post("http://pokehash.buddyauth.com/api/v137_1/hash", headers=headers, json=payload) as resp:
async with self._session.post(self.endPointUrl, headers=headers, json=payload) as resp:
if resp.status == 400:
status['failures'] += 1

Expand Down Expand Up @@ -88,11 +98,23 @@ async def hash(self, timestamp, latitude, longitude, accuracy, authticket, sessi
except ClientResponseError as e:
if e.code == 403:
raise TempHashingBanException('Your IP was temporarily banned for sending too many requests with invalid keys')
elif e.code == 429:
# allow for 429 from bossland AND for 430 from goHash
elif e.code == 429 or e.code == 430:
status['remaining'] = 0
self.instance_token = self.auth_token
if e.code == 429:
if self.goHash:
self.log.warning("Error 429 - Artificial hash limit reached, consider a higher value for X-RateLimit header in Go Hash request.")
else:
self.log.warning("Error 429 - Out of hashes for this period.")
else:
self.log.warning("Error 430 - No credit remaining on the Go Hash key.")
return await self.hash(timestamp, latitude, longitude, accuracy, authticket, sessiondata, requests)
elif e.code >= 500 or e.code == 404:
if e.code == 503 and self.goHash:
self.log.warning("Error 503 - Go Hash server cannot handle the load.")
if e.code == 549 or e.code == 550 and self.goHash:
self.log.warning("Error 549|550 something bad happened between Bossland and Go Hash not successful after multiple retries.")
raise HashingOfflineException(
'Hashing server error {}: {}'.format(
e.code, e.message))
Expand Down Expand Up @@ -140,9 +162,15 @@ def _multi_status(self):
return self.key_statuses[self.instance_token]

@classmethod
def activate_session(cls, conn_limit=300):
def activate_session(cls, conn_limit=300, go_hash=False):
cls.goHash = go_hash
if cls._session and not cls._session.closed:
return
if cls.goHash:
cls.endPointUrl = "http://hash.goman.io/api/v137_1/hash"
cls.log.warning("Hash server set to Go Hash mode. Please ensure you are using a Go Hash NOT a Bossland hash key.")
else:
cls.log.warning("Hash server set to Bossland mode. Please ensure you are using a Bossland NOT a Go Hash hash key.")
conn = TimedConnector(loop=cls.loop,
limit=conn_limit,
verify_ssl=False)
Expand Down Expand Up @@ -186,3 +214,4 @@ def set_token(cls, token):
else:
cls.auth_token = token
cls.key_status = {'failures': 0}

4 changes: 2 additions & 2 deletions aiopogo/pgoapi.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ def create_request(self):
return PGoApiRequest(self)

@staticmethod
def activate_hash_server(hash_token, conn_limit=300):
def activate_hash_server(hash_token, conn_limit=300, go_hash=False):
HashServer.set_token(hash_token)
HashServer.activate_session(conn_limit)
HashServer.activate_session(conn_limit, go_hash)

@property
def position(self):
Expand Down