epicyon/session.py

132 lines
5.0 KiB
Python
Raw Normal View History

2019-06-28 18:55:29 +00:00
__filename__ = "session.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "0.0.1"
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
2019-07-16 14:23:06 +00:00
import os
2019-08-14 16:46:10 +00:00
import sys
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
2019-06-30 18:23:18 +00:00
baseDirectory=None
2019-07-02 09:25:29 +00:00
def createSession(domain: str, port: int, onionRoute: bool):
2019-06-28 18:55:29 +00:00
session = requests.session()
2019-07-02 17:11:59 +00:00
#if domain.startswith('127.') or domain.startswith('192.') or domain.startswith('10.'):
# session.mount('http://', SourceAddressAdapter(domain))
2019-07-02 09:25:29 +00:00
#session.mount('http://', SourceAddressAdapter((domain, port)))
2019-06-28 18:55:29 +00:00
if onionRoute:
session.proxies = {}
session.proxies['http'] = 'socks5h://localhost:9050'
session.proxies['https'] = 'socks5h://localhost:9050'
return session
2019-08-14 16:46:10 +00:00
def getJson(session,url: str,headers: {},params: {}, \
2019-08-14 20:12:27 +00:00
version='0.0.1',httpPrefix='https',domain='testdomain') -> {}:
2019-06-28 18:55:29 +00:00
sessionParams={}
sessionHeaders={}
if headers:
sessionHeaders=headers
if params:
sessionParams=params
2019-08-14 16:46:10 +00:00
pythonVersion=str(sys.version_info[0])+'.'+str(sys.version_info[1])+'.'+str(sys.version_info[2])
2019-08-26 14:08:41 +00:00
sessionHeaders['User-Agent']='Epicyon/'+version
if domain:
sessionHeaders['User-Agent']+='; +'+httpPrefix+'://'+domain+'/'
2019-08-14 16:46:10 +00:00
#"Mozilla/5.0 (Linux; Android 5.1.1; Nexus 5)"
2019-07-16 10:19:04 +00:00
if not session:
print('WARN: no session specified for getJson')
2019-08-18 12:52:39 +00:00
#session.cookies.clear()
2019-07-04 17:31:41 +00:00
try:
2019-08-15 21:34:25 +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')
print('url: '+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
2019-07-09 14:20:23 +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
if not capability.startswith('cap'):
# check that we are posting to a permitted domain
2019-07-09 14:20:23 +00:00
if not urlPermitted(inboxUrl,federationList,capability):
2019-07-16 11:33:40 +00:00
print('postJson: '+inboxUrl+' not permitted')
2019-07-06 11:03:51 +00:00
return None
2019-06-28 20:22:36 +00:00
2019-08-15 21:34:25 +00:00
postResult = session.post(url = inboxUrl, data = json.dumps(postJsonObject), headers=headers)
2019-06-28 19:36:39 +00:00
return postResult.text
2019-07-16 14:23:06 +00:00
2019-08-18 11:07:06 +00:00
def postJsonString(session,postJsonStr: str, \
federationList: [], \
inboxUrl: str, \
headers: {}, \
capability: str, \
2019-08-26 12:10:19 +00:00
debug: bool) -> bool:
"""Post a json message string to the inbox of another person
Supplying a capability, such as "inbox:write"
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
if not capability.startswith('cap'):
# check that we are posting to a permitted domain
if not urlPermitted(inboxUrl,federationList,capability):
2019-08-18 11:07:06 +00:00
print('postJson: '+inboxUrl+' not permitted by capabilities')
return None
postResult = session.post(url = inboxUrl, data = postJsonStr, headers=headers)
2019-08-26 12:04:09 +00:00
if postResult.status_code<200 or postResult.status_code>202:
2019-08-26 12:46:08 +00:00
if postResult.status_code==401:
print('WARN: >>> Post to '+inboxUrl+' is unauthorized <<<')
else:
print('WARN: Failed to post to '+inboxUrl)
print('status code '+str(postResult.status_code))
2019-08-26 12:10:19 +00:00
return False
return True
2019-07-16 14:23:06 +00:00
def postImage(session,attachImageFilename: str,federationList: [],inboxUrl: str,headers: {},capability: str) -> str:
"""Post an image to the inbox of another person or outbox via c2s
Supplying a capability, such as "inbox:write"
"""
# always allow capability requests
if not capability.startswith('cap'):
# check that we are posting to a permitted domain
if not urlPermitted(inboxUrl,federationList,capability):
print('postJson: '+inboxUrl+' not permitted')
return None
if not (attachImageFilename.endswith('.jpg') or \
attachImageFilename.endswith('.jpeg') or \
attachImageFilename.endswith('.png') or \
attachImageFilename.endswith('.gif')):
print('Image must be png, jpg, or gif')
return None
if not os.path.isfile(attachImageFilename):
print('Image not found: '+attachImageFilename)
return None
contentType='image/jpeg'
if attachImageFilename.endswith('.png'):
contentType='image/png'
if attachImageFilename.endswith('.gif'):
contentType='image/gif'
headers['Content-type']=contentType
with open(attachImageFilename, 'rb') as avFile:
mediaBinary = avFile.read()
postResult = session.post(url=inboxUrl, data=mediaBinary, headers=headers)
return postResult.text
return None