epicyon/session.py

151 lines
5.3 KiB
Python
Raw Normal View History

2020-04-04 11:00:19 +00:00
__filename__ = "session.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.1.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
2019-06-28 18:55:29 +00:00
2019-07-16 14:23:06 +00:00
import os
2019-06-28 18:55:29 +00:00
import requests
2019-07-02 10:39:55 +00:00
from utils import urlPermitted
2019-06-28 18:55:29 +00:00
import json
2020-04-04 11:00:19 +00:00
baseDirectory = None
2019-06-30 18:23:18 +00:00
2019-11-13 10:50:16 +00:00
def createSession(onionRoute: bool):
2020-04-04 11:00:19 +00:00
session = requests.session()
2019-06-28 18:55:29 +00:00
if onionRoute:
2020-04-04 11:00:19 +00:00
session.proxies = {}
session.proxies['http'] = 'socks5h://localhost:9050'
session.proxies['https'] = 'socks5h://localhost:9050'
2019-06-28 18:55:29 +00:00
return session
2020-04-04 11:00:19 +00:00
def getJson(session, url: str, headers: {}, params: {},
version='1.0.0', httpPrefix='https',
domain='testdomain') -> {}:
2019-09-01 13:13:52 +00:00
if not isinstance(url, str):
2020-04-04 11:00:19 +00:00
print('url: ' + str(url))
2019-09-01 13:13:52 +00:00
print('ERROR: getJson url should be a string')
return None
2020-04-04 11:00:19 +00:00
sessionParams = {}
sessionHeaders = {}
2019-06-28 18:55:29 +00:00
if headers:
2020-04-04 11:00:19 +00:00
sessionHeaders = headers
2019-06-28 18:55:29 +00:00
if params:
2020-04-04 11:00:19 +00:00
sessionParams = params
sessionHeaders['User-Agent'] = 'Epicyon/' + version
2019-08-26 14:08:41 +00:00
if domain:
2020-04-04 11:00:19 +00:00
sessionHeaders['User-Agent'] += \
'; +' + httpPrefix + '://' + domain + '/'
2019-07-16 10:19:04 +00:00
if not session:
print('WARN: no session specified for getJson')
2019-07-04 17:31:41 +00:00
try:
2020-04-04 11:00:19 +00:00
result = session.get(url, headers=sessionHeaders, params=sessionParams)
2019-07-04 17:31:41 +00:00
return result.json()
except Exception as e:
2019-07-19 13:32:58 +00:00
print('ERROR: getJson failed')
2020-04-04 11:00:19 +00:00
print('url: ' + str(url))
print('headers: ' + str(sessionHeaders))
print('params: ' + str(sessionParams))
print(e)
2019-07-04 17:31:41 +00:00
return None
2019-06-28 19:36:39 +00:00
2020-04-04 11:00:19 +00:00
def postJson(session, postJsonObject: {}, federationList: [],
inboxUrl: str, headers: {}, capability: str) -> str:
2019-06-28 19:36:39 +00:00
"""Post a json message to the inbox of another person
2019-07-06 11:03:51 +00:00
Supplying a capability, such as "inbox:write"
2019-06-28 19:36:39 +00:00
"""
2019-07-06 11:03:51 +00:00
# always allow capability requests
2020-03-22 21:16:02 +00:00
if not capability.startswith('cap'):
2019-07-06 11:03:51 +00:00
# check that we are posting to a permitted domain
2020-04-04 11:00:19 +00:00
if not urlPermitted(inboxUrl, federationList, capability):
print('postJson: ' + inboxUrl + ' not permitted')
2019-07-06 11:03:51 +00:00
return None
2019-06-28 20:22:36 +00:00
2020-04-04 11:00:19 +00:00
postResult = \
session.post(url=inboxUrl,
data=json.dumps(postJsonObject),
2019-11-18 16:55:04 +00:00
headers=headers)
2019-10-26 12:01:22 +00:00
if postResult:
return postResult.text
return None
2019-07-16 14:23:06 +00:00
2020-04-04 11:00:19 +00:00
def postJsonString(session, postJsonStr: str,
federationList: [],
inboxUrl: str,
headers: {},
capability: str,
debug: bool) -> (bool, bool):
"""Post a json message string to the inbox of another person
Supplying a capability, such as "inbox:write"
2019-10-23 18:44:03 +00:00
The second boolean returned is true if the send is unauthorized
NOTE: Here we post a string rather than the original json so that
conversions between string and json format don't invalidate
the message body digest of http signatures
"""
# always allow capability requests
2020-03-22 21:16:02 +00:00
if not capability.startswith('cap'):
# check that we are posting to a permitted domain
2020-04-04 11:00:19 +00:00
if not urlPermitted(inboxUrl, federationList, capability):
print('postJson: ' + inboxUrl + ' not permitted by capabilities')
return None, None
postResult = \
session.post(url=inboxUrl, data=postJsonStr, headers=headers)
if postResult.status_code < 200 or postResult.status_code > 202:
if postResult.status_code >= 400 and \
postResult.status_code <= 405 and \
postResult.status_code != 404:
print('WARN: >>> Post to ' + inboxUrl +
' is unauthorized. Code ' +
str(postResult.status_code) + ' <<<')
return False, True
2019-08-26 12:46:08 +00:00
else:
2020-04-04 11:00:19 +00:00
print('WARN: Failed to post to ' + inboxUrl +
' with headers ' + str(headers))
print('status code ' + str(postResult.status_code))
return False, False
return True, False
2020-04-04 11:00:19 +00:00
def postImage(session, attachImageFilename: str, federationList: [],
inboxUrl: str, headers: {}, capability: str) -> str:
2019-07-16 14:23:06 +00:00
"""Post an image to the inbox of another person or outbox via c2s
Supplying a capability, such as "inbox:write"
"""
# always allow capability requests
2020-03-22 21:16:02 +00:00
if not capability.startswith('cap'):
2019-07-16 14:23:06 +00:00
# check that we are posting to a permitted domain
2020-04-04 11:00:19 +00:00
if not urlPermitted(inboxUrl, federationList, capability):
print('postJson: ' + inboxUrl + ' not permitted')
2019-07-16 14:23:06 +00:00
return None
2020-04-04 11:00:19 +00:00
if not (attachImageFilename.endswith('.jpg') or
attachImageFilename.endswith('.jpeg') or
attachImageFilename.endswith('.png') or
2019-07-16 14:23:06 +00:00
attachImageFilename.endswith('.gif')):
print('Image must be png, jpg, or gif')
return None
if not os.path.isfile(attachImageFilename):
2020-04-04 11:00:19 +00:00
print('Image not found: ' + attachImageFilename)
2019-07-16 14:23:06 +00:00
return None
2020-04-04 11:00:19 +00:00
contentType = 'image/jpeg'
2019-07-16 14:23:06 +00:00
if attachImageFilename.endswith('.png'):
2020-04-04 11:00:19 +00:00
contentType = 'image/png'
2019-07-16 14:23:06 +00:00
if attachImageFilename.endswith('.gif'):
2020-04-04 11:00:19 +00:00
contentType = 'image/gif'
headers['Content-type'] = contentType
2019-07-16 14:23:06 +00:00
with open(attachImageFilename, 'rb') as avFile:
2020-04-04 11:00:19 +00:00
mediaBinary = avFile.read()
postResult = session.post(url=inboxUrl, data=mediaBinary,
headers=headers)
2019-10-26 12:01:22 +00:00
if postResult:
return postResult.text
2019-07-16 14:23:06 +00:00
return None