Onionr/static-data/default-plugins/chat/peerserver.py

64 lines
2.1 KiB
Python
Raw Normal View History

2020-04-03 09:27:37 +00:00
"""Onionr - Private P2P Communication.
2019-03-29 17:37:51 +00:00
2020-04-03 09:27:37 +00:00
HTTP endpoints for communicating with peers
"""
import sys
import os
from json import JSONDecodeError
2020-04-03 09:27:37 +00:00
import deadsimplekv as simplekv
import ujson as json
from flask import Response, request, redirect, Blueprint, abort, g
from utils import identifyhome
from onionrutils import localcommand
import filepaths
"""
2019-03-29 17:37:51 +00:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
2020-04-03 09:27:37 +00:00
"""
2019-03-31 04:50:54 +00:00
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
2019-07-27 21:56:06 +00:00
direct_blueprint = Blueprint('chat', __name__)
2019-03-29 17:37:51 +00:00
2019-07-20 06:02:30 +00:00
key_store = simplekv.DeadSimpleKV(filepaths.cached_storage, refresh_seconds=5)
storage_dir = identifyhome.identify_home()
2019-03-29 17:37:51 +00:00
@direct_blueprint.before_request
def request_setup():
2019-07-20 06:02:30 +00:00
key_store.refresh()
2019-03-29 17:37:51 +00:00
host = request.host
host = host.strip('.b32.i2p')
host = host.strip('.onion')
g.host = host
2019-07-20 06:02:30 +00:00
g.peer = key_store.get('dc-' + g.host)
2019-03-29 17:37:51 +00:00
2019-07-27 21:56:06 +00:00
@direct_blueprint.route('/chat/ping')
2019-03-29 17:37:51 +00:00
def pingdirect():
2019-03-31 04:50:54 +00:00
return 'pong!'
2019-07-27 21:56:06 +00:00
@direct_blueprint.route('/chat/sendto', methods=['POST', 'GET'])
2019-03-31 04:50:54 +00:00
def sendto():
"""Endpoint peers send chat messages to"""
2019-03-31 04:50:54 +00:00
try:
msg = request.get_json(force=True)
except JSONDecodeError:
2019-03-31 04:50:54 +00:00
msg = ''
else:
msg = json.dumps(msg)
2019-07-27 21:56:06 +00:00
localcommand.local_command('/chat/addrec/%s' % (g.peer,), post=True, postData=msg)
2019-03-31 04:50:54 +00:00
return Response('success')
2019-07-27 21:56:06 +00:00
@direct_blueprint.route('/chat/poll')
2019-03-31 04:50:54 +00:00
def poll_chat():
"""Endpoints peers get new messages from"""
2019-07-27 21:56:06 +00:00
return Response(localcommand.local_command('/chat/gets/%s' % (g.peer,)))