Skip to content

Commit

Permalink
fix request header issues (#10)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Toy <[email protected]>
  • Loading branch information
atoy3731 and Adam Toy authored Nov 2, 2022
1 parent abb3cc5 commit ed17dd3
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1
2.2
2 changes: 1 addition & 1 deletion src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def get_login_cookie(url, user, passwd):
'password': passwd
}

response = requests.post('{}/control/login'.format(url), data=json.dumps(creds))
response = requests.post('{}/control/login'.format(url), data=json.dumps(creds), headers=common.REQUEST_HEADERS)

if response.status_code != 200:
print('ERROR: Unable to acquire cookie.')
Expand Down
11 changes: 6 additions & 5 deletions src/block_allow_lists/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import requests
import json
from exceptions import UnauthenticatedError, SystemError
import common


def _get_block_allow_lists(filtering_status):
Expand Down Expand Up @@ -60,7 +61,7 @@ def _update_block_allow_lists(url, cookie, sync_block_allow_lists):
'url': del_allowlist['url'],
'whitelist': True
}
response = requests.post('{}/control/filtering/remove_url'.format(url), cookies=cookies, data=json.dumps(data))
response = requests.post('{}/control/filtering/remove_url'.format(url), cookies=cookies, data=json.dumps(data), headers=common.REQUEST_HEADERS)

if response.status_code == 403:
raise UnauthenticatedError
Expand All @@ -73,7 +74,7 @@ def _update_block_allow_lists(url, cookie, sync_block_allow_lists):
'url': del_blocklist['url'],
'whitelist': False
}
response = requests.post('{}/control/filtering/remove_url'.format(url), cookies=cookies, data=json.dumps(data))
response = requests.post('{}/control/filtering/remove_url'.format(url), cookies=cookies, data=json.dumps(data), headers=common.REQUEST_HEADERS)

if response.status_code == 403:
raise UnauthenticatedError
Expand All @@ -88,7 +89,7 @@ def _update_block_allow_lists(url, cookie, sync_block_allow_lists):
'url': add_allowlist['url'],
'whitelist': True
}
response = requests.post('{}/control/filtering/add_url'.format(url), cookies=cookies, data=json.dumps(data))
response = requests.post('{}/control/filtering/add_url'.format(url), cookies=cookies, data=json.dumps(data), headers=common.REQUEST_HEADERS)

if response.status_code == 403:
raise UnauthenticatedError
Expand All @@ -102,7 +103,7 @@ def _update_block_allow_lists(url, cookie, sync_block_allow_lists):
'url': add_blocklist['url'],
'whitelist': False
}
response = requests.post('{}/control/filtering/add_url'.format(url), cookies=cookies, data=json.dumps(data))
response = requests.post('{}/control/filtering/add_url'.format(url), cookies=cookies, data=json.dumps(data), headers=common.REQUEST_HEADERS)

if response.status_code == 403:
raise UnauthenticatedError
Expand All @@ -122,7 +123,7 @@ def _update_block_allow_lists(url, cookie, sync_block_allow_lists):
}

print(" - Updating modified entry ({})".format(mod['url']))
response = requests.post('{}/control/filtering/set_url'.format(url), cookies=cookies, data=json.dumps(data))
response = requests.post('{}/control/filtering/set_url'.format(url), cookies=cookies, data=json.dumps(data), headers=common.REQUEST_HEADERS)

if response.status_code == 403:
raise UnauthenticatedError
Expand Down
2 changes: 1 addition & 1 deletion src/blocked_services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def _update_blocked_services(url, cookie, sync_blocked_services):
}

print(" - Syncing blocked services")
response = requests.post('{}/control/blocked_services/set'.format(url), cookies=cookies, data=json.dumps(sync_blocked_services))
response = requests.post('{}/control/blocked_services/set'.format(url), cookies=cookies, data=json.dumps(sync_blocked_services), headers=common.REQUEST_HEADERS)

if response.status_code == 403:
raise UnauthenticatedError
Expand Down
4 changes: 3 additions & 1 deletion src/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import time
from exceptions import UnauthenticatedError, SystemError

REQUEST_HEADERS = {'Content-Type': 'application/json'}

def get_response(url, cookie):
"""
Helper function to handle errors and keep it DRY
Expand Down Expand Up @@ -37,7 +39,7 @@ def update_settings(setting, primary_settings, secondary_settings, url, cookie):

if primary_settings != secondary_settings:
print(" - Updating {} settings".format(setting))
response = requests.post(url, cookies=cookies, data=json.dumps(primary_settings))
response = requests.post(url, cookies=cookies, data=json.dumps(primary_settings), headers=REQUEST_HEADERS)

if response.status_code == 403:
raise UnauthenticatedError
Expand Down
4 changes: 2 additions & 2 deletions src/entries/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def _update_entries(url, cookie, sync_entries):
'domain': entry['domain'],
'answer': entry['answer']
}
response = requests.post('{}/control/rewrite/add'.format(url), cookies=cookies, data=json.dumps(data))
response = requests.post('{}/control/rewrite/add'.format(url), cookies=cookies, data=json.dumps(data), headers=common.REQUEST_HEADERS)
if response.status_code == 403:
raise UnauthenticatedError
elif response.status_code != 200:
Expand All @@ -51,7 +51,7 @@ def _update_entries(url, cookie, sync_entries):
'domain': entry['domain'],
'answer': entry['answer']
}
response = requests.post('{}/control/rewrite/delete'.format(url), cookies=cookies, data=json.dumps(data))
response = requests.post('{}/control/rewrite/delete'.format(url), cookies=cookies, data=json.dumps(data), headers=common.REQUEST_HEADERS)
if response.status_code == 403:
raise UnauthenticatedError
elif response.status_code != 200:
Expand Down
2 changes: 1 addition & 1 deletion src/settings/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def _update_protection_enabled(enabled, url, cookie):
else:
print(" - Disabling global protection")

response = requests.post('{}/control/dns_config'.format(url), data=json.dumps(data), cookies=cookies)
response = requests.post('{}/control/dns_config'.format(url), data=json.dumps(data), headers=common.REQUEST_HEADERS, cookies=cookies)

if response.status_code == 403:
raise UnauthenticatedError
Expand Down

0 comments on commit ed17dd3

Please sign in to comment.