2020-06-13 22:50:37 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-07-01 20:38:55 +00:00
|
|
|
|
2020-06-13 22:50:37 +00:00
|
|
|
view and interact with onionr sites
|
2019-09-29 20:39:03 +00:00
|
|
|
"""
|
2020-06-13 22:50:37 +00:00
|
|
|
import base64
|
|
|
|
import binascii
|
|
|
|
import mimetypes
|
|
|
|
|
|
|
|
import unpaddedbase32
|
|
|
|
|
|
|
|
from flask import Blueprint, Response, request, abort
|
|
|
|
|
2021-01-17 04:58:28 +00:00
|
|
|
from oldblocks import onionrblockapi
|
2020-06-13 22:50:37 +00:00
|
|
|
import onionrexceptions
|
|
|
|
from onionrutils import stringvalidators
|
|
|
|
from onionrutils import mnemonickeys
|
|
|
|
from . import sitefiles
|
2019-09-29 20:39:03 +00:00
|
|
|
"""
|
2019-07-01 20:38:55 +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/>.
|
2019-09-29 20:39:03 +00:00
|
|
|
"""
|
2019-09-28 05:37:24 +00:00
|
|
|
|
2019-07-01 20:38:55 +00:00
|
|
|
|
|
|
|
site_api = Blueprint('siteapi', __name__)
|
|
|
|
|
2019-11-12 05:32:18 +00:00
|
|
|
@site_api.route('/site/<name>/', endpoint='site')
|
2019-10-29 02:33:08 +00:00
|
|
|
def site(name: str)->Response:
|
|
|
|
"""Accept a site 'name', if pubkey then show multi-page site, if hash show single page site"""
|
|
|
|
resp: str = 'Not Found'
|
2019-11-01 18:35:28 +00:00
|
|
|
mime_type = 'text/html'
|
2019-10-29 02:33:08 +00:00
|
|
|
|
|
|
|
# If necessary convert the name to base32 from mnemonic
|
|
|
|
if mnemonickeys.DELIMITER in name:
|
2019-09-28 05:37:24 +00:00
|
|
|
name = mnemonickeys.get_base32(name)
|
2019-10-29 02:33:08 +00:00
|
|
|
|
|
|
|
# Now make sure the key is regardless a valid base32 format ed25519 key (readding padding if necessary)
|
2019-09-28 05:37:24 +00:00
|
|
|
if stringvalidators.validate_pub_key(name):
|
|
|
|
name = unpaddedbase32.repad(name)
|
2019-11-07 00:03:38 +00:00
|
|
|
resp = sitefiles.get_file(name, 'index.html')
|
2019-09-28 05:37:24 +00:00
|
|
|
|
2019-10-31 09:29:03 +00:00
|
|
|
elif stringvalidators.validate_hash(name):
|
2019-07-01 20:38:55 +00:00
|
|
|
try:
|
2019-10-29 02:33:08 +00:00
|
|
|
resp = onionrblockapi.Block(name).bcontent
|
2019-07-01 20:38:55 +00:00
|
|
|
except onionrexceptions.NoDataAvailable:
|
|
|
|
abort(404)
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
resp = base64.b64decode(resp)
|
2019-09-09 00:21:36 +00:00
|
|
|
except binascii.Error:
|
2019-07-01 20:38:55 +00:00
|
|
|
pass
|
|
|
|
if resp == 'Not Found' or not resp:
|
|
|
|
abort(404)
|
2019-09-28 05:37:24 +00:00
|
|
|
return Response(resp)
|
2019-11-12 05:32:18 +00:00
|
|
|
|
2019-11-14 03:06:04 +00:00
|
|
|
@site_api.route('/site/<name>/<path:file>', endpoint='siteFile')
|
2019-11-12 05:32:18 +00:00
|
|
|
def site_file(name: str, file: str)->Response:
|
|
|
|
"""Accept a site 'name', if pubkey then show multi-page site, if hash show single page site"""
|
|
|
|
resp: str = 'Not Found'
|
2019-11-14 03:06:04 +00:00
|
|
|
mime_type = mimetypes.MimeTypes().guess_type(file)[0]
|
2019-11-12 05:32:18 +00:00
|
|
|
|
|
|
|
# If necessary convert the name to base32 from mnemonic
|
|
|
|
if mnemonickeys.DELIMITER in name:
|
|
|
|
name = mnemonickeys.get_base32(name)
|
|
|
|
|
|
|
|
# Now make sure the key is regardless a valid base32 format ed25519 key (readding padding if necessary)
|
|
|
|
if stringvalidators.validate_pub_key(name):
|
|
|
|
name = unpaddedbase32.repad(name)
|
|
|
|
resp = sitefiles.get_file(name, file)
|
|
|
|
|
|
|
|
elif stringvalidators.validate_hash(name):
|
|
|
|
try:
|
|
|
|
resp = onionrblockapi.Block(name).bcontent
|
|
|
|
except onionrexceptions.NoDataAvailable:
|
|
|
|
abort(404)
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
resp = base64.b64decode(resp)
|
|
|
|
except binascii.Error:
|
|
|
|
pass
|
|
|
|
if resp == 'Not Found' or not resp:
|
|
|
|
abort(404)
|
2019-11-14 03:06:04 +00:00
|
|
|
return Response(resp, mimetype=mime_type)
|