Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
Added timeouts to requests and fixed login (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas committed Sep 11, 2021
1 parent 8105401 commit 7b11dd0
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 46 deletions.
2 changes: 1 addition & 1 deletion api/heart_beat.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ def heart_beat():
}
}

requests.post('https://swap.labrie.ca/api/ping/', json = data)
requests.post('https://swap.labrie.ca/api/ping/', json = data, timeout = 5)
4 changes: 2 additions & 2 deletions api/labrie_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def labrie_check(shaketag: str, type: str) -> dict:
'step': type
}

response = requests.post('https://swap.labrie.ca/api/', json = data)
response = requests.post('https://swap.labrie.ca/api/', json = data, timeout = 5)

if (response.ok):
response = response.json()
Expand All @@ -25,7 +25,7 @@ def labrie_check_multi(shaketags: list, type: str) -> dict:
'step': type
}

response = requests.post('https://swap.labrie.ca/api/multi/', json = data)
response = requests.post('https://swap.labrie.ca/api/multi/', json = data, timeout = 5)

if (response.ok):
response = response.json()
Expand Down
42 changes: 28 additions & 14 deletions api/login.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
import requests
import json
import socket

import globals

from api.exception import raise_exception
from utilities.log import log

login_headers = {
'X-Device-Brand': 'bot',
'X-Device-Model': socket.gethostname(),
'X-Device-System-Name': 'burntbot',
'X-Device-System-Version': str(globals.version),
'X-Device-Carrier': '',
'X-Device-Mac-Address': 'AA:BB:CC:DD:EE:FF',
'X-Device-Manufacturer': '@burnttoaster',
'X-Device-Id': '',
'X-Device-Ip-Address': '10.0.0.1',
'X-Device-Locale': 'en-CA',
'X-Device-Country': 'CA',
'X-Device-Name': f'@burnttoaster bot v{globals.version}',
'X-Device-Total-Disk-Capacity': '138764288',
'X-Device-Total-Memory': '37019976576',
'X-Device-Is-Tablet': 'false',
'X-Device-Has-Notch': 'false',
'X-Notification-Token': '',
'Content-Type': 'application/json'
}

# returns False on email verification
# returns accessToken for 2FA on success
def pre_login(email: str, password: str):
# copy headers to append content type
local_headers = globals.headers.copy()
local_headers['Content-Type'] = 'application/json'
local_headers['X-Device-Model'] = 'SM-G930W8'
local_headers['X-Device-System-Name'] = 'Android'
local_headers['X-Device-System-Version'] = '8.0.0'
# copy global headers so that we can append our own headers without affecting global
local_headers = {**globals.headers, **login_headers}

# pre 2FA POST
response = requests.post('https://api.shakepay.com/authentication', headers = local_headers, data = json.dumps({
'password': password,
'strategy': 'local',
'totpType': 'sms',
'username': email
}))
}), timeout = 5)

if (response.status_code == 403):
return False
Expand All @@ -35,19 +53,15 @@ def pre_login(email: str, password: str):
return response['accessToken']

def mfa_login(code: str, pre_token: str) -> str:
# copy headers to append content type
local_headers = globals.headers.copy()
local_headers['Content-Type'] = 'application/json'
# copy global headers so that we can append our own headers without affecting global
local_headers = {**globals.headers, **login_headers}
local_headers['Authorization'] = pre_token
local_headers['X-Device-Model'] = 'SM-G930W8'
local_headers['X-Device-System-Name'] = 'Android'
local_headers['X-Device-System-Version'] = '8.0.0'

# 2FA POST
response = requests.post('https://api.shakepay.com/authentication', headers = local_headers, data = json.dumps({
'mfaToken': code,
'strategy': 'mfa'
}))
}), timeout = 5)

# make sure we have 2xx status
if (not response.ok):
Expand Down
44 changes: 23 additions & 21 deletions api/shakingsats.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
import requests
import json
import socket

import globals

from utilities.log import log

def shaking_sats():
# all these headers are required for shaking sats
local_headers = globals.headers.copy()
local_headers['X-Device-Brand'] = 'samsung'
local_headers['X-Device-Model'] = 'SM-G930W8'
local_headers['X-Device-System-Name'] = 'Android'
local_headers['X-Device-System-Version'] = '8.0.0'
local_headers['X-Device-Carrier'] = 'Bell'
local_headers['X-Device-Mac-Address'] = 'AA:BB:CC:DD:EE:FF'
local_headers['X-Device-Manufacturer'] = 'samsung'
local_headers['X-Device-Id'] = 'universal8890'
local_headers['X-Device-Ip-Address'] = '10.0.0.0'
local_headers['X-Device-Locale'] = 'en-CA'
local_headers['X-Device-Country'] = 'CA'
local_headers['X-Device-Name'] = f'@burnttoaster bot v{globals.version}'
local_headers['X-Device-Total-Disk-Capacity'] = '138764288'
local_headers['X-Device-Total-Memory'] = '37019976576'
local_headers['X-Device-Is-Tablet'] = 'false'
local_headers['X-Device-Has-Notch'] = 'false'
local_headers['X-Notification-Token'] = ''
local_headers['Content-Type'] = 'application/json'
# copy global headers so that we can append our own headers without affecting global
local_headers = {**globals.headers, **{
'X-Device-Brand': 'bot',
'X-Device-Model': socket.gethostname(),
'X-Device-System-Name': 'burntbot',
'X-Device-System-Version': str(globals.version),
'X-Device-Carrier': '',
'X-Device-Mac-Address': 'AA:BB:CC:DD:EE:FF',
'X-Device-Manufacturer': '@burnttoaster',
'X-Device-Id': '',
'X-Device-Ip-Address': '10.0.0.1',
'X-Device-Locale': 'en-CA',
'X-Device-Country': 'CA',
'X-Device-Name': f'@burnttoaster bot v{globals.version}',
'X-Device-Total-Disk-Capacity': '138764288',
'X-Device-Total-Memory': '37019976576',
'X-Device-Is-Tablet': 'false',
'X-Device-Has-Notch': 'false',
'X-Notification-Token': '',
'Content-Type': 'application/json'
}}

response = requests.post('https://api.shakepay.com/shaking-sats', headers = local_headers, data = json.dumps({}))
response = requests.post('https://api.shakepay.com/shaking-sats', headers = local_headers, data = json.dumps({}), timeout = 5)

log(f'shaking-sats code: {response.status_code}', True)
log(f'shaking-sats data: {response.json()}', True)
4 changes: 2 additions & 2 deletions api/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def send_transaction(amount: float, shaketag: str, note: str) -> dict:
'toType': 'user'
}

response = requests.post('https://api.shakepay.com/transactions', headers = local_headers, data = json.dumps(body))
response = requests.post('https://api.shakepay.com/transactions', headers = local_headers, data = json.dumps(body), timeout = 5)

# make sure we have 2xx status
if (not response.ok):
Expand All @@ -33,7 +33,7 @@ def get_transactions(params: dict) -> tuple:
# copy headers to append content type
local_headers = globals.headers.copy()

response = requests.get('https://api.shakepay.com/transactions/history', headers = local_headers, params = params)
response = requests.get('https://api.shakepay.com/transactions/history', headers = local_headers, params = params, timeout = 5)
headers = response.headers

# make sure we have 2xx status
Expand Down
4 changes: 2 additions & 2 deletions api/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from utilities.log import log

def users(user_id: str) -> dict:
response = requests.get(f'https://api.shakepay.com/users/{user_id}', headers = globals.headers)
response = requests.get(f'https://api.shakepay.com/users/{user_id}', headers = globals.headers, timeout = 5)

# make sure we have 2xx status
if (not response.ok):
Expand All @@ -23,7 +23,7 @@ def search(shaketag: str) -> list:
if (local_shaketag[0] == '@'): local_shaketag = shaketag[1:]

if (shaketag != ''):
response = requests.get(f'https://api.shakepay.com/users?username={local_shaketag}', headers = globals.headers)
response = requests.get(f'https://api.shakepay.com/users?username={local_shaketag}', headers = globals.headers, timeout = 5)

if (not response.ok):
log('Something went wrong when fetching users: {}'.format(response.text))
Expand Down
2 changes: 1 addition & 1 deletion api/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from utilities.log import log

def get_master_version() -> bool:
response = requests.get('https://raw.githubusercontent.com/itslupus/burntbot/master/.version')
response = requests.get('https://raw.githubusercontent.com/itslupus/burntbot/master/.version', timeout = 5)

# make sure we have 2xx status
if (not response.ok):
Expand Down
6 changes: 5 additions & 1 deletion api/waitlist.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import requests
import time

import globals

Expand All @@ -11,12 +12,15 @@ def update_waitlist():
Update the local cache containing waitlist details (position, points, etc)
'''

# dont check waitlist if its been < 5 minutes since last check
if (globals.waitlist_last_check + (60 * 5) > time.time()): return

local_headers = globals.headers.copy()

# use etags to prevent redundant data transfer
if (globals.waitlist_etag): local_headers['If-None-Match'] = globals.waitlist_etag

response = requests.get('https://api.shakepay.com/card/waitlist', headers = local_headers)
response = requests.get('https://api.shakepay.com/card/waitlist', headers = local_headers, timeout = 5)

# make sure we have 2xx status
if (not response.ok):
Expand Down
2 changes: 1 addition & 1 deletion api/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from utilities.log import log

def get_wallet():
response = requests.get('https://api.shakepay.com/wallets', headers = globals.headers)
response = requests.get('https://api.shakepay.com/wallets', headers = globals.headers, timeout = 5)

# make sure we have 2xx status
if (not response.ok):
Expand Down
3 changes: 2 additions & 1 deletion classes/heartbeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def run(self):
while (not self.stop.is_set()) and (db.get_key_value('heart_beat', False)) and (SwapBot.bot_state) and (not globals.bot_flags['listen']):
heart_beat()

self.stop.wait(60 * 5)
# ping swapper database every 45 seconds
self.stop.wait(45)

db.close()
1 change: 1 addition & 0 deletions globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
waitlist_points = 0
waitlist_paddles = []
waitlist_swaps = 0
waitlist_last_check = 0

# bot specific flags
bot_flags = {
Expand Down

0 comments on commit 7b11dd0

Please sign in to comment.