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"
|
|
|
|
|
|
|
|
import requests
|
|
|
|
import json
|
|
|
|
|
2019-06-30 18:23:18 +00:00
|
|
|
baseDirectory=None
|
|
|
|
|
2019-06-28 18:55:29 +00:00
|
|
|
def createSession(onionRoute: bool):
|
|
|
|
session = requests.session()
|
|
|
|
if onionRoute:
|
|
|
|
session.proxies = {}
|
|
|
|
session.proxies['http'] = 'socks5h://localhost:9050'
|
|
|
|
session.proxies['https'] = 'socks5h://localhost:9050'
|
|
|
|
return session
|
|
|
|
|
2019-07-01 11:09:09 +00:00
|
|
|
def getJson(session,url: str,headers: {},params: {}) -> {}:
|
2019-06-28 18:55:29 +00:00
|
|
|
sessionParams={}
|
|
|
|
sessionHeaders={}
|
|
|
|
if headers:
|
|
|
|
sessionHeaders=headers
|
|
|
|
if params:
|
|
|
|
sessionParams=params
|
2019-07-01 21:01:43 +00:00
|
|
|
sessionHeaders['User-agent'] = "Mozilla/5.0 (Linux; Android 5.1.1; Nexus 5 Build/LMY48B; wv)"
|
2019-06-28 18:55:29 +00:00
|
|
|
session.cookies.clear()
|
2019-07-01 21:01:43 +00:00
|
|
|
result=session.get(url, headers=sessionHeaders, params=sessionParams)
|
|
|
|
print("*****result "+url+' ' + str(result))
|
|
|
|
return result.json()
|
2019-06-28 19:36:39 +00:00
|
|
|
|
2019-07-01 11:09:09 +00:00
|
|
|
def postJson(session,postJsonObject: {},federationList: [],inboxUrl: str,headers: {}) -> str:
|
2019-06-28 19:36:39 +00:00
|
|
|
"""Post a json message to the inbox of another person
|
|
|
|
"""
|
2019-06-28 20:22:36 +00:00
|
|
|
# check that we are posting to a permitted domain
|
|
|
|
permittedDomain=False
|
2019-06-28 20:43:37 +00:00
|
|
|
for domain in federationList:
|
2019-06-28 20:22:36 +00:00
|
|
|
if domain in inboxUrl:
|
|
|
|
permittedDomain=True
|
|
|
|
break
|
|
|
|
if not permittedDomain:
|
|
|
|
return None
|
|
|
|
|
2019-06-30 11:07:39 +00:00
|
|
|
postResult = session.post(url = inboxUrl, data = json.dumps(postJsonObject), headers=headers)
|
2019-06-28 19:36:39 +00:00
|
|
|
return postResult.text
|