epicyon/session.py

285 lines
10 KiB
Python
Raw Normal View History

2020-04-04 11:00:19 +00:00
__filename__ = "session.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2021-01-26 10:07:42 +00:00
__version__ = "1.2.0"
2020-04-04 11:00:19 +00:00
__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-06-23 13:28:41 +00:00
from socket import error as SocketError
import errno
2019-06-28 18:55:29 +00:00
2020-04-04 11:00:19 +00:00
baseDirectory = None
2019-06-30 18:23:18 +00:00
2020-06-09 11:03:59 +00:00
def createSession(proxyType: str):
2020-06-08 17:10:53 +00:00
session = None
try:
2020-06-23 22:17:48 +00:00
session = requests.session()
2020-06-23 21:39:19 +00:00
except requests.exceptions.RequestException as e:
print('WARN: requests error during createSession')
2020-06-23 13:41:12 +00:00
print(e)
return None
2020-06-23 13:28:41 +00:00
except SocketError as e:
if e.errno == errno.ECONNRESET:
print('WARN: connection was reset during createSession')
else:
2020-06-23 13:41:12 +00:00
print('WARN: socket error during createSession')
print(e)
2020-06-08 17:10:53 +00:00
return None
2020-06-23 21:39:19 +00:00
except ValueError as e:
print('WARN: error during createSession')
print(e)
return None
2020-06-09 11:03:59 +00:00
if not session:
return None
if proxyType == 'tor':
2020-04-04 11:00:19 +00:00
session.proxies = {}
session.proxies['http'] = 'socks5h://localhost:9050'
session.proxies['https'] = 'socks5h://localhost:9050'
2020-06-09 11:03:59 +00:00
elif proxyType == 'i2p':
session.proxies = {}
2020-06-19 22:09:20 +00:00
session.proxies['http'] = 'socks5h://localhost:4447'
session.proxies['https'] = 'socks5h://localhost:4447'
2020-06-09 11:51:51 +00:00
elif proxyType == 'gnunet':
session.proxies = {}
session.proxies['http'] = 'socks5h://localhost:7777'
session.proxies['https'] = 'socks5h://localhost:7777'
2020-09-25 14:38:50 +00:00
# print('New session created with proxy ' + str(proxyType))
2019-06-28 18:55:29 +00:00
return session
2020-04-04 11:00:19 +00:00
def urlExists(session, url: str, timeoutSec=3,
httpPrefix='https', domain='testdomain') -> bool:
if not isinstance(url, str):
print('url: ' + str(url))
print('ERROR: urlExists failed, url should be a string')
return False
sessionParams = {}
sessionHeaders = {}
sessionHeaders['User-Agent'] = 'Epicyon/' + __version__
if domain:
sessionHeaders['User-Agent'] += \
'; +' + httpPrefix + '://' + domain + '/'
if not session:
print('WARN: urlExists failed, no session specified')
return True
try:
result = session.get(url, headers=sessionHeaders,
params=sessionParams,
timeout=timeoutSec)
if result:
if result.status_code == 200 or \
result.status_code == 304:
return True
2021-02-14 14:05:51 +00:00
else:
print('urlExists for ' + url + ' returned ' +
str(result.status_code))
except BaseException:
pass
return False
2021-03-14 20:55:37 +00:00
def getJson(session, url: str, headers: {}, params: {}, debug: bool,
2021-01-26 10:07:42 +00:00
version='1.2.0', httpPrefix='https',
2021-03-10 15:47:12 +00:00
domain='testdomain', timeoutSec=20, quiet=False) -> {}:
2019-09-01 13:13:52 +00:00
if not isinstance(url, str):
2021-03-14 20:55:37 +00:00
if debug and not quiet:
2021-03-14 20:15:44 +00:00
print('url: ' + str(url))
print('ERROR: getJson failed, url should be a string')
2019-09-01 13:13:52 +00:00
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:
2021-03-10 15:47:12 +00:00
if not quiet:
print('WARN: getJson failed, no session specified for getJson')
return None
2019-07-04 17:31:41 +00:00
try:
2021-03-02 18:02:29 +00:00
result = session.get(url, headers=sessionHeaders,
params=sessionParams, timeout=timeoutSec)
2019-07-04 17:31:41 +00:00
return result.json()
2020-06-23 21:39:19 +00:00
except requests.exceptions.RequestException as e:
sessionHeaders2 = sessionHeaders.copy()
if sessionHeaders2.get('Authorization'):
2021-03-02 20:44:00 +00:00
sessionHeaders2['Authorization'] = 'REDACTED'
2021-03-14 20:55:37 +00:00
if debug and not quiet:
2021-03-10 15:47:12 +00:00
print('ERROR: getJson failed\nurl: ' + str(url) + ' ' +
'headers: ' + str(sessionHeaders2) + ' ' +
'params: ' + str(sessionParams))
print(e)
2020-06-23 13:41:12 +00:00
except ValueError as e:
sessionHeaders2 = sessionHeaders.copy()
if sessionHeaders2.get('Authorization'):
2021-03-02 20:44:00 +00:00
sessionHeaders2['Authorization'] = 'REDACTED'
2021-03-14 20:55:37 +00:00
if debug and not quiet:
2021-03-10 15:47:12 +00:00
print('ERROR: getJson failed\nurl: ' + str(url) + ' ' +
'headers: ' + str(sessionHeaders2) + ' ' +
'params: ' + str(sessionParams) + ' ')
print(e)
2020-06-23 13:41:12 +00:00
except SocketError as e:
2021-03-10 15:47:12 +00:00
if not quiet:
if e.errno == errno.ECONNRESET:
print('WARN: getJson failed, ' +
'connection was reset during getJson')
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: [],
2021-03-10 19:24:52 +00:00
inboxUrl: str, headers: {}, timeoutSec=60, quiet=False) -> str:
2019-06-28 19:36:39 +00:00
"""Post a json message to the inbox of another person
"""
2020-09-27 19:27:24 +00:00
# check that we are posting to a permitted domain
if not urlPermitted(inboxUrl, federationList):
2021-03-10 19:24:52 +00:00
if not quiet:
print('postJson: ' + inboxUrl + ' not permitted')
2020-09-27 19:27:24 +00:00
return None
2019-06-28 20:22:36 +00:00
2020-06-08 18:05:36 +00:00
try:
postResult = \
session.post(url=inboxUrl,
data=json.dumps(postJsonObject),
2021-03-10 18:41:13 +00:00
headers=headers, timeout=timeoutSec)
2021-03-21 13:17:59 +00:00
except requests.Timeout as e:
if not quiet:
print('ERROR: postJson timeout ' + inboxUrl + ' ' +
json.dumps(postJsonObject) + ' ' + str(headers))
print(e)
return ''
2020-06-23 21:39:19 +00:00
except requests.exceptions.RequestException as e:
2021-03-10 19:24:52 +00:00
if not quiet:
print('ERROR: postJson requests failed ' + inboxUrl + ' ' +
json.dumps(postJsonObject) + ' ' + str(headers))
print(e)
2020-06-23 13:41:12 +00:00
return None
2020-06-23 13:28:41 +00:00
except SocketError as e:
2021-03-10 19:24:52 +00:00
if not quiet and e.errno == errno.ECONNRESET:
2020-06-23 13:28:41 +00:00
print('WARN: connection was reset during postJson')
2020-06-08 18:05:36 +00:00
return None
2020-06-23 21:39:19 +00:00
except ValueError as e:
2021-03-10 19:24:52 +00:00
if not quiet:
print('ERROR: postJson failed ' + inboxUrl + ' ' +
json.dumps(postJsonObject) + ' ' + str(headers))
print(e)
2020-06-23 21:39:19 +00:00
return None
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: {},
2021-03-10 19:11:14 +00:00
debug: bool,
2021-03-10 19:24:52 +00:00
timeoutSec=30,
quiet=False) -> (bool, bool):
"""Post a json message string to the inbox of another person
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
"""
2020-06-08 18:05:36 +00:00
try:
postResult = \
2021-03-10 19:11:14 +00:00
session.post(url=inboxUrl, data=postJsonStr,
headers=headers, timeout=timeoutSec)
2020-06-23 21:39:19 +00:00
except requests.exceptions.RequestException as e:
2021-03-10 19:24:52 +00:00
if not quiet:
print('WARN: error during postJsonString requests')
print(e)
2020-06-23 13:41:12 +00:00
return None, None
2020-06-23 13:28:41 +00:00
except SocketError as e:
2021-03-10 19:24:52 +00:00
if not quiet and e.errno == errno.ECONNRESET:
2020-06-23 13:28:41 +00:00
print('WARN: connection was reset during postJsonString')
2021-03-10 19:24:52 +00:00
if not quiet:
print('ERROR: postJsonString failed ' + inboxUrl + ' ' +
postJsonStr + ' ' + str(headers))
2020-06-08 18:05:36 +00:00
return None, None
2020-06-23 21:39:19 +00:00
except ValueError as e:
2021-03-10 19:24:52 +00:00
if not quiet:
print('WARN: error during postJsonString')
print(e)
2020-06-23 21:39:19 +00:00
return None, None
2020-04-04 11:00:19 +00:00
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:
2021-03-10 19:24:52 +00:00
if not quiet:
print('WARN: Post to ' + inboxUrl +
' is unauthorized. Code ' +
str(postResult.status_code))
2020-04-04 11:00:19 +00:00
return False, True
2019-08-26 12:46:08 +00:00
else:
2021-03-10 19:24:52 +00:00
if not quiet:
print('WARN: Failed to post to ' + inboxUrl +
' with headers ' + str(headers))
print('status code ' + str(postResult.status_code))
2020-04-04 11:00:19 +00:00
return False, False
return True, False
2020-04-04 11:00:19 +00:00
def postImage(session, attachImageFilename: str, federationList: [],
2020-09-27 19:27:24 +00:00
inboxUrl: str, headers: {}) -> str:
2019-07-16 14:23:06 +00:00
"""Post an image to the inbox of another person or outbox via c2s
"""
2020-09-27 19:27:24 +00:00
# check that we are posting to a permitted domain
if not urlPermitted(inboxUrl, federationList):
print('postJson: ' + inboxUrl + ' not permitted')
return None
2019-07-16 14:23:06 +00:00
2020-04-04 11:00:19 +00:00
if not (attachImageFilename.endswith('.jpg') or
attachImageFilename.endswith('.jpeg') or
attachImageFilename.endswith('.png') or
2021-01-11 22:27:57 +00:00
attachImageFilename.endswith('.svg') or
2019-07-16 14:23:06 +00:00
attachImageFilename.endswith('.gif')):
2021-01-11 22:27:57 +00:00
print('Image must be png, jpg, gif or svg')
2019-07-16 14:23:06 +00:00
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'
2021-01-11 22:27:57 +00:00
if attachImageFilename.endswith('.svg'):
contentType = 'image/svg+xml'
2020-04-04 11:00:19 +00:00
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()
2020-06-08 18:05:36 +00:00
try:
postResult = session.post(url=inboxUrl, data=mediaBinary,
headers=headers)
2020-06-23 21:39:19 +00:00
except requests.exceptions.RequestException as e:
print('WARN: error during postImage requests')
2020-06-23 13:41:12 +00:00
print(e)
return None
2020-06-23 13:28:41 +00:00
except SocketError as e:
if e.errno == errno.ECONNRESET:
print('WARN: connection was reset during postImage')
2020-06-08 18:05:36 +00:00
print('ERROR: postImage failed ' + inboxUrl + ' ' +
str(headers))
2020-06-23 13:41:12 +00:00
print(e)
2020-06-08 18:05:36 +00:00
return None
2020-06-23 21:39:19 +00:00
except ValueError as e:
print('WARN: error during postImage')
print(e)
return None
2019-10-26 12:01:22 +00:00
if postResult:
return postResult.text
2019-07-16 14:23:06 +00:00
return None