Onionr/onionr/onionr.py

364 lines
12 KiB
Python
Raw Normal View History

2017-12-26 07:25:29 +00:00
#!/usr/bin/env python3
'''
2018-01-26 07:22:48 +00:00
Onionr - P2P Microblogging Platform & Social network.
Onionr is the name for both the protocol and the original/reference software.
Run with 'help' for usage.
'''
'''
2017-12-26 07:25:29 +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/>.
'''
2018-02-23 01:58:36 +00:00
import sys, os, base64, random, getpass, shutil, subprocess, requests, time, platform
import api, core, gui, config, logger
2018-01-09 22:58:12 +00:00
from onionrutils import OnionrUtils
2018-01-19 09:16:38 +00:00
from netcontroller import NetController
2017-12-28 19:30:15 +00:00
2018-01-28 01:53:24 +00:00
try:
from urllib3.contrib.socks import SOCKSProxyManager
except ImportError:
raise Exception("You need the PySocks module (for use with socks5 proxy to use Tor)")
ONIONR_VERSION = '0.0.0' # for debugging and stuff
API_VERSION = '1' # increments of 1; only change when something fundemental about how the API works changes. This way other nodes knows how to communicate without learning too much information about you.
2017-12-26 07:25:29 +00:00
class Onionr:
def __init__(self):
'''
Main Onionr class. This is for the CLI program, and does not handle much of the logic.
In general, external programs and plugins should not use this class.
'''
2018-02-23 01:58:36 +00:00
2018-01-15 08:52:45 +00:00
try:
os.chdir(sys.path[0])
except FileNotFoundError:
pass
2018-02-23 01:58:36 +00:00
# Load global configuration data
exists = os.path.exists(config.get_config_file())
2018-02-23 02:53:49 +00:00
config.set_config({'devmode': True, 'log.file': True, 'log.console': True, 'log.outputfile': 'data/output.log', 'log.color': True}) # this is the default config, it will be overwritten if a config file already exists. Else, it saves it
2018-02-23 01:58:36 +00:00
config.reload() # this will read the configuration file into memory
2018-02-23 02:53:49 +00:00
settings = 0b000
if config.get('log.color', True):
settings = settings | logger.USE_ANSI
if config.get('log.console', True):
settings = settings | logger.OUTPUT_TO_CONSOLE
if config.get('log.file', False):
settings = settings | logger.OUTPUT_TO_FILE
logger.set_file(config.get('log.outputfile', 'data/output.log'))
logger.set_settings(settings)
2018-02-23 01:58:36 +00:00
if config.get('devmode', True):
2018-01-10 03:50:38 +00:00
self._developmentMode = True
2018-01-26 07:22:48 +00:00
logger.set_level(logger.LEVEL_DEBUG)
2018-01-10 03:50:38 +00:00
else:
self._developmentMode = False
2018-01-26 07:22:48 +00:00
logger.set_level(logger.LEVEL_INFO)
self.onionrCore = core.Core()
2018-01-26 06:28:11 +00:00
self.onionrUtils = OnionrUtils(self.onionrCore)
2018-01-09 22:58:12 +00:00
2018-02-23 01:58:36 +00:00
# Handle commands
2018-01-26 07:22:48 +00:00
self.debug = False # Whole application debugging
2018-01-09 22:58:12 +00:00
if os.path.exists('data-encrypted.dat'):
while True:
print('Enter password to decrypt:')
password = getpass.getpass()
result = self.onionrCore.dataDirDecrypt(password)
2018-01-09 22:58:12 +00:00
if os.path.exists('data/'):
break
else:
2018-01-26 07:22:48 +00:00
logger.error('Failed to decrypt: ' + result[1])
2018-01-09 22:58:12 +00:00
else:
2018-01-10 03:50:38 +00:00
if not os.path.exists('data/'):
os.mkdir('data/')
2018-01-26 06:28:11 +00:00
os.mkdir('data/blocks/')
2018-01-26 07:22:48 +00:00
if not os.path.exists(self.onionrCore.peerDB):
self.onionrCore.createPeerDB()
2018-01-10 03:50:38 +00:00
pass
if not os.path.exists(self.onionrCore.addressDB):
self.onionrCore.createAddressDB()
2017-12-27 05:00:02 +00:00
# Get configuration
2018-02-23 01:58:36 +00:00
if not exists:
2017-12-27 05:00:02 +00:00
# Generate default config
# Hostname should only be set if different from 127.x.x.x. Important for DNS rebinding attack prevention.
if self.debug:
randomPort = 8080
else:
2018-01-20 07:23:09 +00:00
while True:
randomPort = random.randint(1024, 65535)
if self.onionrUtils.checkPort(randomPort):
break
2018-02-23 01:58:36 +00:00
config.set('CLIENT', {'participate': 'true', 'CLIENT HMAC': base64.b64encode(os.urandom(32)).decode('utf-8'), 'PORT': randomPort, 'API VERSION': API_VERSION}, True)
2017-12-26 07:25:29 +00:00
command = ''
try:
command = sys.argv[1].lower()
except IndexError:
2017-12-26 09:42:17 +00:00
command = ''
2017-12-28 19:30:15 +00:00
finally:
self.execute(command)
2018-01-26 07:22:48 +00:00
2018-01-10 03:50:38 +00:00
if not self._developmentMode:
2018-01-26 07:22:48 +00:00
encryptionPassword = self.onionrUtils.getPassword('Enter password to encrypt directory: ')
self.onionrCore.dataDirEncrypt(encryptionPassword)
2018-01-10 03:50:38 +00:00
shutil.rmtree('data/')
2017-12-26 07:25:29 +00:00
return
'''
THIS SECTION HANDLES THE COMMANDS
'''
def getCommands(self):
return {
2018-02-23 02:25:05 +00:00
'help': self.showHelp,
'version': self.version,
'config': self.configure,
'start': self.start,
'stop': self.killDaemon,
2018-02-23 02:25:05 +00:00
'stats': self.showStats,
'listpeers': self.listPeers,
'list-peers': self.listPeers,
'': self.showHelpSuggestion,
'addmsg': self.addMessage,
'addmessage': self.addMessage,
'add-msg': self.addMessage,
'add-message': self.addMessage,
'pm': self.sendEncrypt,
'gui': self.openGUI,
'addpeer': self.addPeer,
'add-peer': self.addPeer
}
2018-02-22 07:24:25 +00:00
def getHelp(self):
return {
'help': 'Displays this Onionr help menu',
'version': 'Displays the Onionr version',
2018-02-23 02:25:05 +00:00
'config': 'Configures something and adds it to the file',
2018-02-22 07:24:25 +00:00
'start': 'Starts the Onionr daemon',
'stop': 'Stops the Onionr daemon',
'stats': 'Displays node statistics',
'list-peers': 'Displays a list of peers (?)',
'add-peer': 'Adds a peer (?)',
'add-msg': 'Broadcasts a message to the Onionr network',
'pm': 'Adds a private message (?)',
'gui': 'Opens a graphical interface for Onionr'
}
2018-02-23 02:25:05 +00:00
def configure(self):
'''
Displays something from the configuration file, or sets it
'''
if len(sys.argv) >= 4:
config.reload()
config.set(sys.argv[2], sys.argv[3], True)
logger.debug('Configuration file updated.')
elif len(sys.argv) >= 3:
config.reload()
logger.info(logger.colors.bold + sys.argv[2] + ': ' + logger.colors.reset + str(config.get(sys.argv[2], logger.colors.fg.red + 'Not set.')))
else:
logger.info(logger.colors.bold + 'Get a value: ' + logger.colors.reset + sys.argv[0] + ' ' + sys.argv[1] + ' <key>')
logger.info(logger.colors.bold + 'Set a value: ' + logger.colors.reset + sys.argv[0] + ' ' + sys.argv[1] + ' <key> <value>')
def execute(self, argument):
2018-02-22 07:24:25 +00:00
'''
Executes a command
'''
argument = argument[argument.startswith('--') and len('--'):] # remove -- if it starts with it
# define commands
commands = self.getCommands()
command = commands.get(argument, self.notFound)
command()
'''
THIS SECTION DEFINES THE COMMANDS
'''
2018-02-22 07:24:25 +00:00
def version(self, verbosity=5):
'''
Displays the Onionr version
'''
logger.info('Onionr ' + ONIONR_VERSION + ' (' + platform.machine() + ') - API v' + API_VERSION)
if verbosity >= 1:
logger.info('Anonymous P2P Platform - GPLv3 - onionr.voidnet.tech')
if verbosity >= 2:
logger.info('Running on ' + platform.platform() + ' ' + platform.release())
def sendEncrypt(self):
2018-02-22 07:24:25 +00:00
'''
Create a private message and send it
'''
while True:
peer = logger.readline('Peer to send to: ')
if self.onionrUtils.validateID(peer):
break
else:
logger.error('Invalid peer ID')
message = logger.readline("Enter a message: ")
logger.info("Sending message to " + peer)
2018-02-08 22:58:39 +00:00
self.onionrUtils.sendPM(peer, message)
def openGUI(self):
2018-02-22 07:24:25 +00:00
'''
Opens a graphical interface for Onionr
'''
gui.OnionrGUI(self.onionrCore)
def listPeers(self):
2018-02-22 07:24:25 +00:00
'''
Displays a list of peers (?)
'''
logger.info('Peer list:\n')
for i in self.onionrCore.listPeers():
logger.info(i)
def addPeer(self):
2018-02-22 07:24:25 +00:00
'''
Adds a peer (?)
'''
try:
newPeer = sys.argv[2]
except:
pass
else:
logger.info("Adding peer: " + logger.colors.underline + newPeer)
self.onionrCore.addPeer(newPeer)
def addMessage(self):
2018-02-22 07:24:25 +00:00
'''
Broadcasts a message to the Onionr network
'''
while True:
messageToAdd = '-txt-' + logger.readline('Broadcast message to network: ')
if len(messageToAdd) >= 1:
break
2018-02-22 07:24:25 +00:00
addedHash = self.onionrCore.setData(messageToAdd)
self.onionrCore.addToBlockDB(addedHash, selfInsert=True)
self.onionrCore.setBlockType(addedHash, 'txt')
def notFound(self):
2018-02-22 07:24:25 +00:00
'''
Displays a "command not found" message
'''
logger.error('Command not found.')
def showHelpSuggestion(self):
2018-02-22 07:24:25 +00:00
'''
Displays a message suggesting help
'''
logger.info('Do ' + logger.colors.bold + sys.argv[0] + ' --help' + logger.colors.reset + logger.colors.fg.green + ' for Onionr help.')
def start(self):
2018-02-22 07:24:25 +00:00
'''
Starts the Onionr daemon
'''
if os.path.exists('.onionr-lock'):
logger.fatal('Cannot start. Daemon is already running, or it did not exit cleanly.\n(if you are sure that there is not a daemon running, delete .onionr-lock & try again).')
else:
if not self.debug and not self._developmentMode:
lockFile = open('.onionr-lock', 'w')
lockFile.write('')
lockFile.close()
self.daemon()
if not self.debug and not self._developmentMode:
os.remove('.onionr-lock')
2017-12-26 07:25:29 +00:00
def daemon(self):
2018-02-22 07:24:25 +00:00
'''
Starts the Onionr communication daemon
'''
if not os.environ.get("WERKZEUG_RUN_MAIN") == "true":
if self._developmentMode:
logger.warn('DEVELOPMENT MODE ENABLED (THIS IS LESS SECURE!)')
2018-02-23 01:58:36 +00:00
net = NetController(config.get('CLIENT')['PORT'])
2018-01-26 07:22:48 +00:00
logger.info('Tor is starting...')
2018-01-28 01:53:24 +00:00
if not net.startTor():
sys.exit(1)
2018-01-26 07:22:48 +00:00
logger.info('Started Tor .onion service: ' + logger.colors.underline + net.myID)
logger.info('Our Public key: ' + self.onionrCore._crypto.pubKey)
2018-01-19 21:28:34 +00:00
time.sleep(1)
2018-01-26 06:28:11 +00:00
subprocess.Popen(["./communicator.py", "run", str(net.socksPort)])
2018-01-26 07:22:48 +00:00
logger.debug('Started communicator')
2018-02-23 01:58:36 +00:00
api.API(self.debug)
2017-12-26 07:25:29 +00:00
return
2017-12-26 07:25:29 +00:00
def killDaemon(self):
2018-02-22 07:24:25 +00:00
'''
Shutdown the Onionr daemon
'''
2018-01-26 07:22:48 +00:00
logger.warn('Killing the running daemon')
2018-02-23 01:58:36 +00:00
net = NetController(config.get('CLIENT')['PORT'])
try:
self.onionrUtils.localCommand('shutdown')
except requests.exceptions.ConnectionError:
pass
self.onionrCore.daemonQueueAdd('shutdown')
2018-01-27 08:43:36 +00:00
net.killTor()
2017-12-26 07:25:29 +00:00
return
2017-12-26 07:25:29 +00:00
def showStats(self):
2018-02-22 07:24:25 +00:00
'''
Displays statistics and exits
'''
2017-12-26 07:25:29 +00:00
return
2018-02-22 07:24:25 +00:00
def showHelp(self, command = None):
'''
Show help for Onionr
'''
helpmenu = self.getHelp()
2018-02-22 07:24:25 +00:00
if command is None and len(sys.argv) >= 3:
for cmd in sys.argv[2:]:
self.showHelp(cmd)
elif not command is None:
if command.lower() in helpmenu:
logger.info(logger.colors.bold + command + logger.colors.reset + logger.colors.fg.blue + ' : ' + logger.colors.reset + helpmenu[command.lower()])
else:
logger.warn(logger.colors.bold + command + logger.colors.reset + logger.colors.fg.blue + ' : ' + logger.colors.reset + 'No help menu entry was found')
else:
self.version(0)
for command, helpmessage in helpmenu.items():
self.showHelp(command)
2017-12-26 07:25:29 +00:00
return
2018-01-29 06:01:36 +00:00
Onionr()