2017-12-26 07:25:29 +00:00
#!/usr/bin/env python3
'''
2018-08-27 03:44:32 +00:00
Onionr - P2P Anonymous Storage Network
2018-01-14 00:07:13 +00:00
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-05-01 07:25:31 +00:00
import sys
2019-01-16 05:57:47 +00:00
MIN_PY_VERSION = 6
if sys . version_info [ 0 ] == 2 or sys . version_info [ 1 ] < MIN_PY_VERSION :
print ( ' Error, Onionr requires Python 3. %s + ' % ( MIN_PY_VERSION , ) )
2018-05-01 07:25:31 +00:00
sys . exit ( 1 )
2018-07-09 07:02:33 +00:00
import os , base64 , random , getpass , shutil , subprocess , requests , time , platform , datetime , re , json , getpass , sqlite3
2019-01-09 16:54:35 +00:00
import webbrowser , uuid , signal
2018-04-22 00:09:48 +00:00
from threading import Thread
2018-12-18 23:48:17 +00:00
import api , core , config , logger , onionrplugins as plugins , onionrevents as events
2018-05-11 02:05:56 +00:00
import onionrutils
2018-12-18 23:48:17 +00:00
import netcontroller
2018-01-19 09:16:38 +00:00
from netcontroller import NetController
2018-06-01 04:25:28 +00:00
from onionrblockapi import Block
2019-01-20 18:09:53 +00:00
import onionrproofs , onionrexceptions , onionrusers , communicator2
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) " )
2018-04-19 01:17:47 +00:00
ONIONR_TAGLINE = ' Anonymous P2P Platform - GPLv3 - https://Onionr.VoidNet.Tech '
2018-12-09 17:29:39 +00:00
ONIONR_VERSION = ' 0.5.0 ' # for debugging and stuff
2018-05-19 22:27:26 +00:00
ONIONR_VERSION_TUPLE = tuple ( ONIONR_VERSION . split ( ' . ' ) ) # (MAJOR, MINOR, VERSION)
2018-09-30 16:53:39 +00:00
API_VERSION = ' 5 ' # increments of 1; only change when something fundemental about how the API works changes. This way other nodes know how to communicate without learning too much information about you.
2018-02-04 01:11:35 +00:00
2017-12-26 07:25:29 +00:00
class Onionr :
def __init__ ( self ) :
2018-02-04 01:11:35 +00:00
'''
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-01-14 00:07:13 +00:00
'''
2018-09-04 18:56:05 +00:00
self . userRunDir = os . getcwd ( ) # Directory user runs the program from
2019-01-09 16:54:35 +00:00
self . killed = False
2018-01-15 08:52:45 +00:00
try :
os . chdir ( sys . path [ 0 ] )
except FileNotFoundError :
pass
2018-02-04 01:11:35 +00:00
2018-09-26 04:58:11 +00:00
try :
self . dataDir = os . environ [ ' ONIONR_HOME ' ]
if not self . dataDir . endswith ( ' / ' ) :
self . dataDir + = ' / '
except KeyError :
self . dataDir = ' data/ '
2018-02-23 01:58:36 +00:00
# Load global configuration data
2018-11-11 03:25:40 +00:00
data_exists = Onionr . setupConfig ( self . dataDir , self = self )
2017-12-27 01:13:19 +00:00
2019-01-18 05:34:13 +00:00
if netcontroller . torBinary ( ) is None :
logger . error ( ' Tor is not installed ' )
sys . exit ( 1 )
2019-01-20 18:09:53 +00:00
self . communicatorInst = None
2018-01-14 08:48:23 +00:00
self . onionrCore = core . Core ( )
2019-01-20 22:54:04 +00:00
self . onionrCore . onionrInst = self
2018-12-22 19:02:09 +00:00
#self.deleteRunFiles()
2018-10-27 03:29:25 +00:00
self . onionrUtils = onionrutils . OnionrUtils ( self . onionrCore )
2018-01-09 22:58:12 +00:00
2018-12-20 20:24:46 +00:00
self . clientAPIInst = ' ' # Client http api instance
self . publicAPIInst = ' ' # Public http api instance
2019-01-09 16:54:35 +00:00
signal . signal ( signal . SIGTERM , self . exitSigterm )
2018-02-23 01:58:36 +00:00
# Handle commands
2018-01-26 07:22:48 +00:00
2018-01-02 08:43:29 +00:00
self . debug = False # Whole application debugging
2017-12-27 01:13:19 +00:00
2018-10-30 22:22:06 +00:00
# If data folder does not exist
if not data_exists :
if not os . path . exists ( self . dataDir + ' blocks/ ' ) :
os . mkdir ( self . dataDir + ' blocks/ ' )
# Copy default plugins into plugins folder
if not os . path . exists ( plugins . get_plugins_folder ( ) ) :
if os . path . exists ( ' static-data/default-plugins/ ' ) :
names = [ f for f in os . listdir ( " static-data/default-plugins/ " ) if not os . path . isfile ( f ) ]
shutil . copytree ( ' static-data/default-plugins/ ' , plugins . get_plugins_folder ( ) )
# Enable plugins
for name in names :
if not name in plugins . get_enabled_plugins ( ) :
plugins . enable ( name , self )
2018-01-26 07:22:48 +00:00
2018-04-23 03:42:37 +00:00
for name in plugins . get_enabled_plugins ( ) :
if not os . path . exists ( plugins . get_plugin_data_folder ( name ) ) :
2018-04-23 04:25:54 +00:00
try :
os . mkdir ( plugins . get_plugin_data_folder ( name ) )
except :
plugins . disable ( name , onionr = self , stop_event = False )
2018-04-23 03:42:37 +00:00
2018-02-22 09:33:30 +00:00
if not os . path . exists ( self . onionrCore . peerDB ) :
2018-01-14 08:48:23 +00:00
self . onionrCore . createPeerDB ( )
2018-01-10 03:50:38 +00:00
pass
2018-02-22 09:33:30 +00:00
if not os . path . exists ( self . onionrCore . addressDB ) :
self . onionrCore . createAddressDB ( )
2017-12-28 00:18:00 +00:00
2017-12-27 05:00:02 +00:00
# Get configuration
2018-12-09 17:29:39 +00:00
if type ( config . get ( ' client.webpassword ' ) ) is type ( None ) :
config . set ( ' client.webpassword ' , base64 . b16encode ( os . urandom ( 32 ) ) . decode ( ' utf-8 ' ) , savefile = True )
2018-12-18 23:48:17 +00:00
if type ( config . get ( ' client.client.port ' ) ) is type ( None ) :
randomPort = netcontroller . getOpenPort ( )
config . set ( ' client.client.port ' , randomPort , savefile = True )
if type ( config . get ( ' client.public.port ' ) ) is type ( None ) :
randomPort = netcontroller . getOpenPort ( )
config . set ( ' client.public.port ' , randomPort , savefile = True )
2018-08-05 06:35:49 +00:00
if type ( config . get ( ' client.participate ' ) ) is type ( None ) :
config . set ( ' client.participate ' , True , savefile = True )
if type ( config . get ( ' client.api_version ' ) ) is type ( None ) :
config . set ( ' client.api_version ' , API_VERSION , savefile = True )
2018-09-07 04:57:10 +00:00
2018-03-03 07:00:43 +00:00
self . cmds = {
2018-03-03 04:19:01 +00:00
' ' : self . showHelpSuggestion ,
2018-02-23 02:25:05 +00:00
' help ' : self . showHelp ,
' version ' : self . version ,
' config ' : self . configure ,
2018-02-04 01:11:35 +00:00
' start ' : self . start ,
' stop ' : self . killDaemon ,
2018-05-11 03:19:48 +00:00
' status ' : self . showStats ,
' statistics ' : self . showStats ,
2018-02-23 02:25:05 +00:00
' stats ' : self . showStats ,
2018-11-10 07:17:19 +00:00
' details ' : self . showDetails ,
' detail ' : self . showDetails ,
' show-details ' : self . showDetails ,
' show-detail ' : self . showDetails ,
' showdetails ' : self . showDetails ,
' showdetail ' : self . showDetails ,
' get-details ' : self . showDetails ,
' get-detail ' : self . showDetails ,
' getdetails ' : self . showDetails ,
' getdetail ' : self . showDetails ,
2018-03-03 04:19:01 +00:00
' enable-plugin ' : self . enablePlugin ,
' enplugin ' : self . enablePlugin ,
' enableplugin ' : self . enablePlugin ,
' enmod ' : self . enablePlugin ,
' disable-plugin ' : self . disablePlugin ,
' displugin ' : self . disablePlugin ,
' disableplugin ' : self . disablePlugin ,
' dismod ' : self . disablePlugin ,
' reload-plugin ' : self . reloadPlugin ,
' reloadplugin ' : self . reloadPlugin ,
' reload-plugins ' : self . reloadPlugin ,
' reloadplugins ' : self . reloadPlugin ,
2018-04-21 03:10:50 +00:00
' create-plugin ' : self . createPlugin ,
' createplugin ' : self . createPlugin ,
' plugin-create ' : self . createPlugin ,
2018-03-03 04:19:01 +00:00
2018-04-18 03:43:33 +00:00
' listkeys ' : self . listKeys ,
' list-keys ' : self . listKeys ,
2018-03-03 04:19:01 +00:00
2018-02-04 01:11:35 +00:00
' addpeer ' : self . addPeer ,
2018-02-27 21:23:49 +00:00
' add-peer ' : self . addPeer ,
' add-address ' : self . addAddress ,
2018-04-19 01:47:35 +00:00
' add-addr ' : self . addAddress ,
' addaddr ' : self . addAddress ,
2018-03-03 04:19:01 +00:00
' addaddress ' : self . addAddress ,
2018-07-01 21:01:19 +00:00
' list-peers ' : self . listPeers ,
2018-06-05 02:26:04 +00:00
2018-08-10 22:13:58 +00:00
' blacklist-block ' : self . banBlock ,
2018-06-01 04:25:28 +00:00
' add-file ' : self . addFile ,
2018-04-23 01:43:17 +00:00
' addfile ' : self . addFile ,
2018-12-09 17:29:39 +00:00
' addhtml ' : self . addWebpage ,
' add-html ' : self . addWebpage ,
' add-site ' : self . addWebpage ,
' addsite ' : self . addWebpage ,
2018-11-11 02:10:58 +00:00
2019-01-13 22:20:10 +00:00
' openhome ' : self . openHome ,
' open-home ' : self . openHome ,
2018-09-04 18:56:05 +00:00
' get-file ' : self . getFile ,
' getfile ' : self . getFile ,
2018-06-13 22:22:48 +00:00
' listconn ' : self . listConn ,
2018-12-22 21:48:05 +00:00
' list-conn ' : self . listConn ,
2018-03-03 04:19:01 +00:00
2018-06-01 04:25:28 +00:00
' import-blocks ' : self . onionrUtils . importNewBlocks ,
2018-05-10 07:42:24 +00:00
' importblocks ' : self . onionrUtils . importNewBlocks ,
2018-04-21 03:10:50 +00:00
' introduce ' : self . onionrCore . introduceNode ,
2018-06-07 08:15:01 +00:00
' connect ' : self . addAddress ,
2018-08-02 20:18:01 +00:00
' pex ' : self . doPEX ,
2018-06-07 08:15:01 +00:00
2018-08-04 02:52:45 +00:00
' ui ' : self . openUI ,
' gui ' : self . openUI ,
2018-09-20 05:13:26 +00:00
2018-08-05 04:09:33 +00:00
' getpassword ' : self . printWebPassword ,
' get-password ' : self . printWebPassword ,
' getpwd ' : self . printWebPassword ,
' get-pwd ' : self . printWebPassword ,
' getpass ' : self . printWebPassword ,
' get-pass ' : self . printWebPassword ,
' getpasswd ' : self . printWebPassword ,
2018-11-11 02:10:58 +00:00
' get-passwd ' : self . printWebPassword ,
2018-12-09 17:29:39 +00:00
' friend ' : self . friendCmd ,
' add-id ' : self . addID ,
' change-id ' : self . changeID
2018-02-04 01:11:35 +00:00
}
2018-03-03 07:00:43 +00:00
self . cmdhelp = {
2018-02-22 07:24:25 +00:00
' 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-11-11 03:25:40 +00:00
2018-02-22 07:24:25 +00:00
' start ' : ' Starts the Onionr daemon ' ,
' stop ' : ' Stops the Onionr daemon ' ,
2018-11-11 03:25:40 +00:00
2018-02-22 07:24:25 +00:00
' stats ' : ' Displays node statistics ' ,
2018-11-11 03:25:40 +00:00
' details ' : ' Displays the web password, public key, and human readable public key ' ,
2018-03-03 04:19:01 +00:00
' enable-plugin ' : ' Enables and starts a plugin ' ,
' disable-plugin ' : ' Disables and stops a plugin ' ,
' reload-plugin ' : ' Reloads a plugin ' ,
2018-04-21 03:10:50 +00:00
' create-plugin ' : ' Creates directory structure for a plugin ' ,
2018-11-11 03:25:40 +00:00
2018-05-02 06:25:44 +00:00
' add-peer ' : ' Adds a peer to database ' ,
2018-04-19 01:47:35 +00:00
' list-peers ' : ' Displays a list of peers ' ,
2018-06-01 04:25:28 +00:00
' add-file ' : ' Create an Onionr block from a file ' ,
2018-09-04 18:56:05 +00:00
' get-file ' : ' Get a file from Onionr blocks ' ,
2018-06-01 04:25:28 +00:00
' import-blocks ' : ' import blocks from the disk (Onionr is transport-agnostic!) ' ,
2018-06-13 22:22:48 +00:00
' listconn ' : ' list connected peers ' ,
2018-08-02 20:18:01 +00:00
' pex ' : ' exchange addresses with peers (done automatically) ' ,
2018-08-11 05:23:59 +00:00
' blacklist-block ' : ' deletes a block by hash and permanently removes it from your node ' ,
2018-05-02 06:25:44 +00:00
' introduce ' : ' Introduce your node to the public Onionr network ' ,
2018-12-09 17:29:39 +00:00
' friend ' : ' [add|remove] [public key/id] ' ,
' add-id ' : ' Generate a new ID (key pair) ' ,
2019-01-13 22:20:10 +00:00
' change-id ' : ' Change active ID ' ,
' open-home ' : ' Open your node \' s home/info screen '
2018-02-22 07:24:25 +00:00
}
2018-04-21 03:10:50 +00:00
# initialize plugins
2018-05-06 23:00:28 +00:00
events . event ( ' init ' , onionr = self , threaded = False )
2018-04-21 03:10:50 +00:00
2018-03-03 07:00:43 +00:00
command = ' '
try :
command = sys . argv [ 1 ] . lower ( )
except IndexError :
command = ' '
finally :
self . execute ( command )
return
2019-01-09 16:54:35 +00:00
def exitSigterm ( self , signum , frame ) :
self . killed = True
2018-03-03 07:00:43 +00:00
'''
THIS SECTION HANDLES THE COMMANDS
'''
2018-11-10 07:17:19 +00:00
def showDetails ( self ) :
details = {
' Node Address ' : self . get_hostname ( ) ,
' Web Password ' : self . getWebPassword ( ) ,
' Public Key ' : self . onionrCore . _crypto . pubKey ,
' Human-readable Public Key ' : self . onionrCore . _utils . getHumanReadableID ( )
}
for detail in details :
logger . info ( ' %s %s : \n %s %s \n ' % ( logger . colors . fg . lightgreen , detail , logger . colors . fg . green , details [ detail ] ) , sensitive = True )
2019-01-13 22:20:10 +00:00
def openHome ( self ) :
try :
url = self . onionrUtils . getClientAPIServer ( )
except FileNotFoundError :
logger . error ( ' Onionr seems to not be running (could not get api host) ' )
else :
webbrowser . open_new_tab ( ' http:// %s /# %s ' % ( url , config . get ( ' client.webpassword ' ) ) )
2018-12-09 17:29:39 +00:00
def addID ( self ) :
try :
sys . argv [ 2 ]
assert sys . argv [ 2 ] == ' true '
except ( IndexError , AssertionError ) as e :
newID = self . onionrCore . _crypto . keyManager . addKey ( ) [ 0 ]
else :
logger . warn ( ' Deterministic keys require random and long passphrases. ' )
2019-01-18 05:34:13 +00:00
logger . warn ( ' If a good passphrase is not used, your key can be easily stolen. ' )
logger . warn ( ' You should use a series of hard to guess words, see this for reference: https://www.xkcd.com/936/ ' )
2018-12-09 17:29:39 +00:00
pass1 = getpass . getpass ( prompt = ' Enter at least %s characters: ' % ( self . onionrCore . _crypto . deterministicRequirement , ) )
pass2 = getpass . getpass ( prompt = ' Confirm entry: ' )
if self . onionrCore . _crypto . safeCompare ( pass1 , pass2 ) :
try :
logger . info ( ' Generating deterministic key. This can take a while. ' )
newID , privKey = self . onionrCore . _crypto . generateDeterministic ( pass1 )
except onionrexceptions . PasswordStrengthError :
logger . error ( ' Must use at least 25 characters. ' )
sys . exit ( 1 )
else :
logger . error ( ' Passwords do not match. ' )
sys . exit ( 1 )
self . onionrCore . _crypto . keyManager . addKey ( pubKey = newID ,
privKey = privKey )
logger . info ( ' Added ID: %s ' % ( self . onionrUtils . bytesToStr ( newID ) , ) )
def changeID ( self ) :
try :
key = sys . argv [ 2 ]
except IndexError :
logger . error ( ' Specify pubkey to use ' )
else :
if self . onionrUtils . validatePubKey ( key ) :
if key in self . onionrCore . _crypto . keyManager . getPubkeyList ( ) :
config . set ( ' general.public_key ' , key )
config . save ( )
logger . info ( ' Set active key to: %s ' % ( key , ) )
logger . info ( ' Restart Onionr if it is running. ' )
else :
logger . error ( ' That key does not exist ' )
else :
logger . error ( ' Invalid key %s ' % ( key , ) )
2018-03-03 07:00:43 +00:00
def getCommands ( self ) :
return self . cmds
2018-09-07 04:57:10 +00:00
2018-08-27 03:44:32 +00:00
def friendCmd ( self ) :
2018-08-29 01:09:27 +00:00
''' List, add, or remove friend(s)
Changes their peer DB entry .
'''
2018-08-27 03:44:32 +00:00
friend = ' '
try :
2018-08-29 01:09:27 +00:00
# Get the friend command
2018-08-27 03:44:32 +00:00
action = sys . argv [ 2 ]
except IndexError :
logger . info ( ' Syntax: friend add/remove/list [address] ' )
else :
action = action . lower ( )
if action == ' list ' :
2018-08-29 01:09:27 +00:00
# List out peers marked as our friend
2018-08-27 03:44:32 +00:00
for friend in self . onionrCore . listPeers ( randomOrder = False , trust = 1 ) :
2018-08-29 01:09:27 +00:00
if friend == self . onionrCore . _crypto . pubKey : # do not list our key
2018-08-28 04:45:31 +00:00
continue
friendProfile = onionrusers . OnionrUser ( self . onionrCore , friend )
logger . info ( friend + ' - ' + friendProfile . getName ( ) )
2018-08-27 03:44:32 +00:00
elif action in ( ' add ' , ' remove ' ) :
try :
friend = sys . argv [ 3 ]
if not self . onionrUtils . validatePubKey ( friend ) :
raise onionrexceptions . InvalidPubkey ( ' Public key is invalid ' )
if friend not in self . onionrCore . listPeers ( ) :
raise onionrexceptions . KeyNotKnown
friend = onionrusers . OnionrUser ( self . onionrCore , friend )
except IndexError :
logger . error ( ' Friend ID is required. ' )
except onionrexceptions . KeyNotKnown :
2018-12-29 00:52:46 +00:00
self . onionrCore . addPeer ( friend )
2018-11-11 02:10:58 +00:00
friend = onionrusers . OnionrUser ( self . onionrCore , friend )
2018-12-29 00:52:46 +00:00
finally :
2018-11-11 02:10:58 +00:00
if action == ' add ' :
friend . setTrust ( 1 )
logger . info ( ' Added %s as friend. ' % ( friend . publicKey , ) )
else :
friend . setTrust ( 0 )
logger . info ( ' Removed %s as friend. ' % ( friend . publicKey , ) )
else :
logger . info ( ' Syntax: friend add/remove/list [address] ' )
2018-12-22 19:02:09 +00:00
def deleteRunFiles ( self ) :
try :
os . remove ( self . onionrCore . publicApiHostFile )
except FileNotFoundError :
pass
try :
os . remove ( self . onionrCore . privateApiHostFile )
except FileNotFoundError :
pass
2018-08-10 22:13:58 +00:00
def banBlock ( self ) :
try :
ban = sys . argv [ 2 ]
except IndexError :
2018-08-11 05:23:59 +00:00
ban = logger . readline ( ' Enter a block hash: ' )
if self . onionrUtils . validateHash ( ban ) :
if not self . onionrCore . _blacklist . inBlacklist ( ban ) :
try :
self . onionrCore . _blacklist . addToDB ( ban )
self . onionrCore . removeBlock ( ban )
except Exception as error :
logger . error ( ' Could not blacklist block ' , error = error )
else :
logger . info ( ' Block blacklisted ' )
else :
logger . warn ( ' That block is already blacklisted ' )
else :
logger . error ( ' Invalid block hash ' )
2018-08-10 22:13:58 +00:00
return
2018-06-13 22:22:48 +00:00
def listConn ( self ) :
2019-01-07 05:50:20 +00:00
randID = str ( uuid . uuid4 ( ) )
self . onionrCore . daemonQueueAdd ( ' connectedPeers ' , responseID = randID )
while True :
2019-01-07 21:09:58 +00:00
try :
time . sleep ( 3 )
peers = self . onionrCore . daemonQueueGetResponse ( randID )
except KeyboardInterrupt :
2019-01-07 05:50:20 +00:00
break
2019-01-07 21:09:58 +00:00
if not type ( peers ) is None :
2019-01-13 22:20:10 +00:00
if peers not in ( ' ' , ' failure ' , None ) :
if peers != False :
print ( peers )
else :
print ( ' Daemon probably not running. Unable to list connected peers. ' )
2019-01-07 21:09:58 +00:00
break
2018-07-06 04:27:12 +00:00
2018-07-01 21:01:19 +00:00
def listPeers ( self ) :
logger . info ( ' Peer transport address list: ' )
for i in self . onionrCore . listAdders ( ) :
logger . info ( i )
2018-06-13 22:22:48 +00:00
2018-06-07 08:15:01 +00:00
def getWebPassword ( self ) :
2018-12-09 17:29:39 +00:00
return config . get ( ' client.webpassword ' )
2018-07-30 00:37:12 +00:00
2018-07-23 07:43:10 +00:00
def printWebPassword ( self ) :
2018-11-11 03:25:40 +00:00
logger . info ( self . getWebPassword ( ) , sensitive = True )
2018-03-03 07:00:43 +00:00
def getHelp ( self ) :
return self . cmdhelp
def addCommand ( self , command , function ) :
2018-04-21 01:20:26 +00:00
self . cmds [ str ( command ) . lower ( ) ] = function
2018-03-03 07:00:43 +00:00
def addHelp ( self , command , description ) :
2018-04-21 01:20:26 +00:00
self . cmdhelp [ str ( command ) . lower ( ) ] = str ( description )
2018-04-21 01:26:46 +00:00
2018-04-21 01:20:26 +00:00
def delCommand ( self , command ) :
return self . cmds . pop ( str ( command ) . lower ( ) , None )
2018-04-21 01:26:46 +00:00
2018-04-21 01:20:26 +00:00
def delHelp ( self , command ) :
return self . cmdhelp . pop ( str ( command ) . lower ( ) , None )
2018-03-03 07:00:43 +00:00
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> ' )
2018-02-04 01:11:35 +00:00
def execute ( self , argument ) :
2018-02-22 07:24:25 +00:00
'''
Executes a command
'''
2018-04-21 03:10:50 +00:00
2018-02-04 01:11:35 +00:00
argument = argument [ argument . startswith ( ' -- ' ) and len ( ' -- ' ) : ] # remove -- if it starts with it
# define commands
commands = self . getCommands ( )
command = commands . get ( argument , self . notFound )
command ( )
2018-04-21 01:26:46 +00:00
2018-04-21 01:20:26 +00:00
return
2018-02-04 01:11:35 +00:00
'''
THIS SECTION DEFINES THE COMMANDS
'''
2018-11-11 03:25:40 +00:00
def version ( self , verbosity = 5 , function = logger . info ) :
2018-02-22 07:24:25 +00:00
'''
Displays the Onionr version
'''
2018-04-21 03:10:50 +00:00
2018-11-11 03:25:40 +00:00
function ( ' Onionr v %s ( %s ) (API v %s ) ' % ( ONIONR_VERSION , platform . machine ( ) , API_VERSION ) )
2018-02-22 07:24:25 +00:00
if verbosity > = 1 :
2018-11-11 03:25:40 +00:00
function ( ONIONR_TAGLINE )
2018-02-22 07:24:25 +00:00
if verbosity > = 2 :
2018-11-11 03:25:40 +00:00
function ( ' Running on %s %s ' % ( platform . platform ( ) , platform . release ( ) ) )
2018-04-21 01:26:46 +00:00
2018-04-21 01:20:26 +00:00
return
2018-07-06 04:27:12 +00:00
2018-08-02 20:18:01 +00:00
def doPEX ( self ) :
''' make communicator do pex '''
logger . info ( ' Sending pex to command queue... ' )
self . onionrCore . daemonQueueAdd ( ' pex ' )
2018-04-18 03:43:33 +00:00
def listKeys ( self ) :
2018-02-22 07:24:25 +00:00
'''
2018-04-18 03:43:33 +00:00
Displays a list of keys ( used to be called peers ) ( ? )
2018-02-22 07:24:25 +00:00
'''
2018-11-10 07:17:19 +00:00
logger . info ( ' %s Public keys in database: \n %s %s ' % ( logger . colors . fg . lightgreen , logger . colors . fg . green , ' \n ' . join ( self . onionrCore . listPeers ( ) ) ) )
2018-02-04 01:11:35 +00:00
def addPeer ( self ) :
2018-02-22 07:24:25 +00:00
'''
Adds a peer ( ? )
'''
2018-02-04 01:11:35 +00:00
try :
newPeer = sys . argv [ 2 ]
except :
pass
else :
2018-06-09 06:21:14 +00:00
if self . onionrUtils . hasKey ( newPeer ) :
logger . info ( ' We already have that key ' )
return
if not ' - ' in newPeer :
logger . info ( ' Since no POW token was supplied for that key, one is being generated ' )
2018-07-13 06:14:09 +00:00
proof = onionrproofs . DataPOW ( newPeer )
2018-06-09 06:21:14 +00:00
while True :
result = proof . getResult ( )
if result == False :
time . sleep ( 0.5 )
else :
break
newPeer + = ' - ' + base64 . b64encode ( result [ 1 ] ) . decode ( )
logger . info ( newPeer )
2018-02-04 01:11:35 +00:00
logger . info ( " Adding peer: " + logger . colors . underline + newPeer )
2018-06-09 06:21:14 +00:00
if self . onionrUtils . mergeKeys ( newPeer ) :
logger . info ( ' Successfully added key ' )
else :
logger . error ( ' Failed to add key ' )
2018-03-03 04:19:01 +00:00
return
2018-02-27 21:23:49 +00:00
def addAddress ( self ) :
2018-04-19 01:47:35 +00:00
'''
Adds a Onionr node address
'''
2018-05-02 06:01:20 +00:00
2018-02-27 21:23:49 +00:00
try :
newAddress = sys . argv [ 2 ]
2018-12-09 17:29:39 +00:00
newAddress = newAddress . replace ( ' http: ' , ' ' ) . replace ( ' / ' , ' ' )
2018-02-27 21:23:49 +00:00
except :
pass
else :
logger . info ( " Adding address: " + logger . colors . underline + newAddress )
if self . onionrCore . addAddress ( newAddress ) :
2018-04-19 01:47:35 +00:00
logger . info ( " Successfully added address. " )
2018-02-27 21:23:49 +00:00
else :
2018-04-19 01:47:35 +00:00
logger . warn ( " Unable to add address. " )
2018-02-04 01:11:35 +00:00
2018-03-03 04:19:01 +00:00
return
2018-04-16 02:22:19 +00:00
def addMessage ( self , header = " txt " ) :
2018-02-22 07:24:25 +00:00
'''
Broadcasts a message to the Onionr network
'''
2018-02-04 01:11:35 +00:00
while True :
2018-05-02 06:01:20 +00:00
try :
messageToAdd = logger . readline ( ' Broadcast message to network: ' )
if len ( messageToAdd ) > = 1 :
break
except KeyboardInterrupt :
return
2018-02-22 07:24:25 +00:00
2018-06-21 07:29:51 +00:00
#addedHash = Block(type = 'txt', content = messageToAdd).save()
addedHash = self . onionrCore . insertBlock ( messageToAdd )
2018-07-10 03:09:45 +00:00
if addedHash != None and addedHash != False and addedHash != " " :
2018-05-07 06:55:03 +00:00
logger . info ( " Message inserted as as block %s " % addedHash )
2018-06-01 04:25:28 +00:00
else :
logger . error ( ' Failed to insert block. ' , timestamp = False )
2018-03-03 04:19:01 +00:00
return
2018-04-19 01:47:35 +00:00
2018-03-03 04:19:01 +00:00
def enablePlugin ( self ) :
'''
Enables and starts the given plugin
'''
if len ( sys . argv ) > = 3 :
plugin_name = sys . argv [ 2 ]
2018-05-02 06:01:20 +00:00
logger . info ( ' Enabling plugin " %s " ... ' % plugin_name )
2018-04-21 01:20:26 +00:00
plugins . enable ( plugin_name , self )
2018-03-03 04:19:01 +00:00
else :
2018-05-02 06:01:20 +00:00
logger . info ( ' %s %s <plugin> ' % ( sys . argv [ 0 ] , sys . argv [ 1 ] ) )
2018-03-03 04:19:01 +00:00
return
def disablePlugin ( self ) :
'''
Disables and stops the given plugin
'''
if len ( sys . argv ) > = 3 :
plugin_name = sys . argv [ 2 ]
2018-05-02 06:01:20 +00:00
logger . info ( ' Disabling plugin " %s " ... ' % plugin_name )
2018-04-21 01:20:26 +00:00
plugins . disable ( plugin_name , self )
2018-03-03 04:19:01 +00:00
else :
2018-05-02 06:01:20 +00:00
logger . info ( ' %s %s <plugin> ' % ( sys . argv [ 0 ] , sys . argv [ 1 ] ) )
2018-03-03 04:19:01 +00:00
return
def reloadPlugin ( self ) :
'''
Reloads ( stops and starts ) all plugins , or the given plugin
'''
if len ( sys . argv ) > = 3 :
plugin_name = sys . argv [ 2 ]
2018-05-02 06:01:20 +00:00
logger . info ( ' Reloading plugin " %s " ... ' % plugin_name )
2018-04-21 01:20:26 +00:00
plugins . stop ( plugin_name , self )
plugins . start ( plugin_name , self )
2018-03-03 04:19:01 +00:00
else :
logger . info ( ' Reloading all plugins... ' )
2018-04-21 01:20:26 +00:00
plugins . reload ( self )
2018-03-03 04:19:01 +00:00
return
2018-04-21 03:10:50 +00:00
def createPlugin ( self ) :
'''
Creates the directory structure for a plugin name
'''
if len ( sys . argv ) > = 3 :
try :
2018-12-09 17:29:39 +00:00
plugin_name = re . sub ( ' [^0-9a-zA-Z_]+ ' , ' ' , str ( sys . argv [ 2 ] ) . lower ( ) )
2018-04-21 03:10:50 +00:00
if not plugins . exists ( plugin_name ) :
2018-05-02 06:01:20 +00:00
logger . info ( ' Creating plugin " %s " ... ' % plugin_name )
2018-04-21 03:10:50 +00:00
os . makedirs ( plugins . get_plugins_folder ( plugin_name ) )
with open ( plugins . get_plugins_folder ( plugin_name ) + ' /main.py ' , ' a ' ) as main :
2018-06-05 02:26:04 +00:00
contents = ' '
with open ( ' static-data/default_plugin.py ' , ' rb ' ) as file :
2018-06-07 01:54:35 +00:00
contents = file . read ( ) . decode ( )
2018-06-05 02:26:04 +00:00
# TODO: Fix $user. os.getlogin() is B U G G Y
main . write ( contents . replace ( ' $user ' , ' some random developer ' ) . replace ( ' $date ' , datetime . datetime . now ( ) . strftime ( ' % Y- % m- %d ' ) ) . replace ( ' $name ' , plugin_name ) )
2018-05-14 04:11:31 +00:00
with open ( plugins . get_plugins_folder ( plugin_name ) + ' /info.json ' , ' a ' ) as main :
main . write ( json . dumps ( { ' author ' : ' anonymous ' , ' description ' : ' the default description of the plugin ' , ' version ' : ' 1.0 ' } ) )
2018-04-21 03:10:50 +00:00
2018-05-02 06:01:20 +00:00
logger . info ( ' Enabling plugin " %s " ... ' % plugin_name )
2018-04-21 03:10:50 +00:00
plugins . enable ( plugin_name , self )
else :
2018-05-02 06:01:20 +00:00
logger . warn ( ' Cannot create plugin directory structure; plugin " %s " exists. ' % plugin_name )
2018-04-21 03:10:50 +00:00
except Exception as e :
logger . error ( ' Failed to create plugin directory structure. ' , e )
else :
2018-05-02 06:01:20 +00:00
logger . info ( ' %s %s <plugin> ' % ( sys . argv [ 0 ] , sys . argv [ 1 ] ) )
2018-04-21 03:10:50 +00:00
return
2018-02-04 01:11:35 +00:00
def notFound ( self ) :
2018-02-22 07:24:25 +00:00
'''
Displays a " command not found " message
'''
2018-04-19 02:25:16 +00:00
logger . error ( ' Command not found. ' , timestamp = False )
2018-02-04 01:11:35 +00:00
def showHelpSuggestion ( self ) :
2018-02-22 07:24:25 +00:00
'''
Displays a message suggesting help
'''
2018-02-04 01:11:35 +00:00
logger . info ( ' Do ' + logger . colors . bold + sys . argv [ 0 ] + ' --help ' + logger . colors . reset + logger . colors . fg . green + ' for Onionr help. ' )
2018-06-14 04:17:58 +00:00
def start ( self , input = False , override = False ) :
2018-02-22 07:24:25 +00:00
'''
Starts the Onionr daemon
'''
2018-06-14 04:17:58 +00:00
if os . path . exists ( ' .onionr-lock ' ) and not override :
2018-02-04 01:11:35 +00:00
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 ( )
2018-04-22 00:09:48 +00:00
self . running = True
2018-02-04 01:11:35 +00:00
self . daemon ( )
2018-04-22 00:09:48 +00:00
self . running = False
2018-02-04 01:11:35 +00:00
if not self . debug and not self . _developmentMode :
2018-12-13 04:35:01 +00:00
try :
os . remove ( ' .onionr-lock ' )
except FileNotFoundError :
pass
2018-12-22 19:02:09 +00:00
def setClientAPIInst ( self , inst ) :
self . clientAPIInst = inst
def getClientApi ( self ) :
while self . clientAPIInst == ' ' :
time . sleep ( 0.5 )
return self . clientAPIInst
2018-02-04 01:11:35 +00:00
2017-12-26 07:25:29 +00:00
def daemon ( self ) :
2018-02-22 07:24:25 +00:00
'''
Starts the Onionr communication daemon
'''
2018-07-13 04:57:03 +00:00
communicatorDaemon = ' ./communicator2.py '
2018-02-04 01:11:35 +00:00
2018-11-10 07:17:19 +00:00
# remove runcheck if it exists
if os . path . isfile ( ' data/.runcheck ' ) :
logger . debug ( ' Runcheck file found on daemon start, deleting in advance. ' )
os . remove ( ' data/.runcheck ' )
2018-12-22 19:02:09 +00:00
Thread ( target = api . API , args = ( self , self . debug , API_VERSION ) ) . start ( )
Thread ( target = api . PublicAPI , args = [ self . getClientApi ( ) ] ) . start ( )
2018-08-06 07:50:08 +00:00
try :
2018-12-22 19:02:09 +00:00
time . sleep ( 0 )
2018-08-06 07:50:08 +00:00
except KeyboardInterrupt :
2018-11-11 03:25:40 +00:00
logger . debug ( ' Got keyboard interrupt, shutting down... ' )
2018-08-06 07:50:08 +00:00
time . sleep ( 1 )
self . onionrUtils . localCommand ( ' shutdown ' )
2018-12-22 19:02:09 +00:00
apiHost = ' '
while apiHost == ' ' :
try :
with open ( self . onionrCore . publicApiHostFile , ' r ' ) as hostFile :
apiHost = hostFile . read ( )
except FileNotFoundError :
pass
time . sleep ( 0.5 )
Onionr . setupConfig ( ' data/ ' , self = self )
if self . _developmentMode :
logger . warn ( ' DEVELOPMENT MODE ENABLED (LESS SECURE) ' , timestamp = False )
net = NetController ( config . get ( ' client.public.port ' , 59497 ) , apiServerIP = apiHost )
logger . debug ( ' Tor is starting... ' )
if not net . startTor ( ) :
self . onionrUtils . localCommand ( ' shutdown ' )
sys . exit ( 1 )
if len ( net . myID ) > 0 and config . get ( ' general.security_level ' ) == 0 :
logger . debug ( ' Started .onion service: %s ' % ( logger . colors . underline + net . myID ) )
2018-08-06 07:50:08 +00:00
else :
2018-12-22 19:02:09 +00:00
logger . debug ( ' .onion service disabled ' )
logger . debug ( ' Using public key: %s ' % ( logger . colors . underline + self . onionrCore . _crypto . pubKey ) )
time . sleep ( 1 )
2018-11-17 07:23:10 +00:00
2018-12-22 19:02:09 +00:00
# TODO: make runable on windows
2019-01-20 18:09:53 +00:00
#communicatorProc = subprocess.Popen([communicatorDaemon, 'run', str(net.socksPort)])
2019-01-20 22:54:04 +00:00
self . onionrCore . torPort = net . socksPort
2019-01-20 18:09:53 +00:00
communicatorThread = Thread ( target = communicator2 . startCommunicator , args = ( self , str ( net . socksPort ) ) )
communicatorThread . start ( )
while self . communicatorInst is None :
time . sleep ( 0.1 )
2018-11-11 03:25:40 +00:00
2018-12-22 19:02:09 +00:00
# print nice header thing :)
if config . get ( ' general.display_header ' , True ) :
self . header ( )
2018-11-11 03:25:40 +00:00
2018-12-22 19:02:09 +00:00
# print out debug info
self . version ( verbosity = 5 , function = logger . debug )
logger . debug ( ' Python version %s ' % platform . python_version ( ) )
2018-11-11 03:25:40 +00:00
2018-12-22 19:02:09 +00:00
logger . debug ( ' Started communicator. ' )
2018-11-11 03:25:40 +00:00
2018-12-22 19:02:09 +00:00
events . event ( ' daemon_start ' , onionr = self )
try :
while True :
2019-01-09 16:54:35 +00:00
time . sleep ( 3 )
2019-01-18 05:34:13 +00:00
# Debug to print out used FDs (regular and net)
#proc = psutil.Process()
#print('api-files:',proc.open_files(), len(psutil.net_connections()))
2018-12-22 19:02:09 +00:00
# Break if communicator process ends, so we don't have left over processes
2019-01-20 18:09:53 +00:00
if self . communicatorInst . shutdown :
2018-12-22 19:02:09 +00:00
break
2019-01-09 16:54:35 +00:00
if self . killed :
break # Break out if sigterm for clean exit
2018-12-22 19:02:09 +00:00
except KeyboardInterrupt :
2019-01-09 16:54:35 +00:00
pass
finally :
2018-12-22 19:02:09 +00:00
self . onionrCore . daemonQueueAdd ( ' shutdown ' )
self . onionrUtils . localCommand ( ' shutdown ' )
2019-01-09 16:54:35 +00:00
net . killTor ( )
2018-12-22 19:02:09 +00:00
time . sleep ( 3 )
self . deleteRunFiles ( )
2017-12-26 07:25:29 +00:00
return
2018-02-04 01:11:35 +00:00
2017-12-26 07:25:29 +00:00
def killDaemon ( self ) :
2018-02-22 07:24:25 +00:00
'''
Shutdown the Onionr daemon
'''
2018-02-04 01:11:35 +00:00
2018-12-09 17:29:39 +00:00
logger . warn ( ' Stopping the running daemon... ' , timestamp = False )
2018-01-14 08:48:23 +00:00
try :
2018-05-02 06:35:08 +00:00
events . event ( ' daemon_stop ' , onionr = self )
2018-06-14 04:17:58 +00:00
net = NetController ( config . get ( ' client.port ' , 59496 ) )
2018-07-09 07:02:33 +00:00
try :
self . onionrCore . daemonQueueAdd ( ' shutdown ' )
except sqlite3 . OperationalError :
pass
2018-05-02 06:35:08 +00:00
net . killTor ( )
except Exception as e :
logger . error ( ' Failed to shutdown daemon. ' , error = e , timestamp = False )
2017-12-26 07:25:29 +00:00
return
2018-02-04 01:11:35 +00:00
2017-12-26 07:25:29 +00:00
def showStats ( self ) :
2018-02-22 07:24:25 +00:00
'''
Displays statistics and exits
'''
2018-05-04 07:14:27 +00:00
2018-05-11 02:05:56 +00:00
try :
# define stats messages here
2019-01-16 05:57:47 +00:00
totalBlocks = len ( self . onionrCore . getBlockList ( ) )
2018-06-01 04:25:28 +00:00
signedBlocks = len ( Block . getBlocks ( signed = True ) )
2018-05-11 02:05:56 +00:00
messages = {
# info about local client
2018-09-26 04:58:11 +00:00
' Onionr Daemon Status ' : ( ( logger . colors . fg . green + ' Online ' ) if self . onionrUtils . isCommunicatorRunning ( timeout = 9 ) else logger . colors . fg . red + ' Offline ' ) ,
2018-05-11 02:05:56 +00:00
# file and folder size stats
' div1 ' : True , # this creates a solid line across the screen, a div
2018-09-26 04:58:11 +00:00
' Total Block Size ' : onionrutils . humanSize ( onionrutils . size ( self . dataDir + ' blocks/ ' ) ) ,
' Total Plugin Size ' : onionrutils . humanSize ( onionrutils . size ( self . dataDir + ' plugins/ ' ) ) ,
' Log File Size ' : onionrutils . humanSize ( onionrutils . size ( self . dataDir + ' output.log ' ) ) ,
2018-05-11 02:05:56 +00:00
# count stats
' div2 ' : True ,
2018-05-19 21:32:21 +00:00
' Known Peers Count ' : str ( len ( self . onionrCore . listPeers ( ) ) - 1 ) ,
2018-09-26 04:58:11 +00:00
' Enabled Plugins Count ' : str ( len ( config . get ( ' plugins.enabled ' , list ( ) ) ) ) + ' / ' + str ( len ( os . listdir ( self . dataDir + ' plugins/ ' ) ) ) ,
2018-06-01 04:25:28 +00:00
' Known Blocks Count ' : str ( totalBlocks ) ,
2018-06-13 05:50:55 +00:00
' Percent Blocks Signed ' : str ( round ( 100 * signedBlocks / max ( totalBlocks , 1 ) , 2 ) ) + ' % '
2018-05-11 02:05:56 +00:00
}
2018-05-11 02:20:14 +00:00
# color configuration
colors = {
' title ' : logger . colors . bold ,
' key ' : logger . colors . fg . lightgreen ,
' val ' : logger . colors . fg . green ,
' border ' : logger . colors . fg . lightblue ,
' reset ' : logger . colors . reset
}
2018-05-11 02:05:56 +00:00
# pre-processing
maxlength = 0
2018-06-13 06:09:55 +00:00
width = self . getConsoleWidth ( )
2018-05-11 02:05:56 +00:00
for key , val in messages . items ( ) :
if not ( type ( val ) is bool and val is True ) :
maxlength = max ( len ( key ) , maxlength )
2018-06-13 06:09:55 +00:00
prewidth = maxlength + len ( ' | ' )
groupsize = width - prewidth - len ( ' [+] ' )
2018-05-11 02:05:56 +00:00
# generate stats table
2018-05-11 02:20:14 +00:00
logger . info ( colors [ ' title ' ] + ' Onionr v %s Statistics ' % ONIONR_VERSION + colors [ ' reset ' ] )
2018-06-05 05:26:11 +00:00
logger . info ( colors [ ' border ' ] + ' - ' * ( maxlength + 1 ) + ' + ' + colors [ ' reset ' ] )
2018-05-11 02:05:56 +00:00
for key , val in messages . items ( ) :
if not ( type ( val ) is bool and val is True ) :
2018-06-13 06:09:55 +00:00
val = [ str ( val ) [ i : i + groupsize ] for i in range ( 0 , len ( str ( val ) ) , groupsize ) ]
logger . info ( colors [ ' key ' ] + str ( key ) . rjust ( maxlength ) + colors [ ' reset ' ] + colors [ ' border ' ] + ' | ' + colors [ ' reset ' ] + colors [ ' val ' ] + str ( val . pop ( 0 ) ) + colors [ ' reset ' ] )
for value in val :
logger . info ( ' ' * maxlength + colors [ ' border ' ] + ' | ' + colors [ ' reset ' ] + colors [ ' val ' ] + str ( value ) + colors [ ' reset ' ] )
2018-05-11 02:05:56 +00:00
else :
2018-06-05 05:26:11 +00:00
logger . info ( colors [ ' border ' ] + ' - ' * ( maxlength + 1 ) + ' + ' + colors [ ' reset ' ] )
logger . info ( colors [ ' border ' ] + ' - ' * ( maxlength + 1 ) + ' + ' + colors [ ' reset ' ] )
2018-05-11 02:05:56 +00:00
except Exception as e :
logger . error ( ' Failed to generate statistics table. ' , error = e , timestamp = False )
2017-12-26 07:25:29 +00:00
return
2018-02-04 01:11:35 +00:00
2018-02-22 07:24:25 +00:00
def showHelp ( self , command = None ) :
'''
Show help for Onionr
'''
helpmenu = self . getHelp ( )
2018-02-04 01:11:35 +00:00
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 :
2018-04-22 00:09:48 +00:00
logger . info ( logger . colors . bold + command + logger . colors . reset + logger . colors . fg . blue + ' : ' + logger . colors . reset + helpmenu [ command . lower ( ) ] , timestamp = False )
2018-02-22 07:24:25 +00:00
else :
2018-04-22 00:09:48 +00:00
logger . warn ( logger . colors . bold + command + logger . colors . reset + logger . colors . fg . blue + ' : ' + logger . colors . reset + ' No help menu entry was found ' , timestamp = False )
2018-02-22 07:24:25 +00:00
else :
self . version ( 0 )
for command , helpmessage in helpmenu . items ( ) :
self . showHelp ( command )
2017-12-26 07:25:29 +00:00
return
2018-02-04 01:11:35 +00:00
2018-04-22 00:09:48 +00:00
def get_hostname ( self ) :
2018-04-22 01:53:12 +00:00
try :
2018-09-26 04:58:11 +00:00
with open ( ' ./ ' + self . dataDir + ' hs/hostname ' , ' r ' ) as hostname :
2018-04-23 03:49:53 +00:00
return hostname . read ( ) . strip ( )
2018-12-09 17:29:39 +00:00
except FileNotFoundError :
return " Not Generated "
2018-04-23 03:49:53 +00:00
except Exception :
return None
2018-04-23 03:45:25 +00:00
2018-06-13 06:09:55 +00:00
def getConsoleWidth ( self ) :
'''
Returns an integer , the width of the terminal / cmd window
'''
columns = 80
try :
columns = int ( os . popen ( ' stty size ' , ' r ' ) . read ( ) . split ( ) [ 1 ] )
except :
# if it errors, it's probably windows, so default to 80.
pass
return columns
2018-09-04 18:56:05 +00:00
def getFile ( self ) :
'''
Get a file from onionr blocks
'''
2018-11-04 03:26:18 +00:00
try :
2018-09-04 18:56:05 +00:00
fileName = sys . argv [ 2 ]
2018-11-04 03:26:18 +00:00
bHash = sys . argv [ 3 ]
except IndexError :
logger . error ( " Syntax %s %s " % ( sys . argv [ 0 ] , ' /path/to/filename <blockhash> ' ) )
else :
2018-11-11 03:25:40 +00:00
logger . info ( fileName )
2018-11-17 07:23:10 +00:00
2018-09-04 18:56:05 +00:00
contents = None
if os . path . exists ( fileName ) :
logger . error ( " File already exists " )
return
if not self . onionrUtils . validateHash ( bHash ) :
logger . error ( ' Block hash is invalid ' )
return
2018-11-17 07:23:10 +00:00
2018-09-04 18:56:05 +00:00
Block . mergeChain ( bHash , fileName )
return
2018-12-09 17:29:39 +00:00
def addWebpage ( self ) :
'''
Add a webpage to the onionr network
'''
self . addFile ( singleBlock = True , blockType = ' html ' )
def addFile ( self , singleBlock = False , blockType = ' txt ' ) :
2018-06-01 04:25:28 +00:00
'''
Adds a file to the onionr network
'''
if len ( sys . argv ) > = 3 :
filename = sys . argv [ 2 ]
contents = None
2018-06-05 02:26:04 +00:00
2018-06-01 04:25:28 +00:00
if not os . path . exists ( filename ) :
2018-09-04 18:56:05 +00:00
logger . error ( ' That file does not exist. Improper path (specify full path)? ' )
return
logger . info ( ' Adding file... this might take a long time. ' )
2018-06-01 04:25:28 +00:00
try :
2018-12-09 17:29:39 +00:00
if singleBlock :
with open ( filename , ' rb ' ) as singleFile :
blockhash = self . onionrCore . insertBlock ( base64 . b64encode ( singleFile . read ( ) ) , header = blockType )
else :
blockhash = Block . createChain ( file = filename )
2018-06-01 04:25:28 +00:00
logger . info ( ' File %s saved in block %s . ' % ( filename , blockhash ) )
2018-06-05 05:26:11 +00:00
except :
2018-06-01 04:25:28 +00:00
logger . error ( ' Failed to save file in block. ' , timestamp = False )
else :
logger . error ( ' %s add-file <filename> ' % sys . argv [ 0 ] , timestamp = False )
2018-04-23 03:45:25 +00:00
2018-11-11 03:25:40 +00:00
def setupConfig ( dataDir , self = None ) :
data_exists = os . path . exists ( dataDir )
if not data_exists :
os . mkdir ( dataDir )
if os . path . exists ( ' static-data/default_config.json ' ) :
config . set_config ( json . loads ( open ( ' static-data/default_config.json ' ) . read ( ) ) ) # this is the default config, it will be overwritten if a config file already exists. Else, it saves it
else :
# the default config file doesn't exist, try hardcoded config
logger . warn ( ' Default configuration file does not exist, switching to hardcoded fallback configuration! ' )
config . set_config ( { ' dev_mode ' : True , ' log ' : { ' file ' : { ' output ' : True , ' path ' : dataDir + ' output.log ' } , ' console ' : { ' output ' : True , ' color ' : True } } } )
if not data_exists :
config . save ( )
config . reload ( ) # this will read the configuration file into memory
settings = 0b000
if config . get ( ' log.console.color ' , True ) :
settings = settings | logger . USE_ANSI
if config . get ( ' log.console.output ' , True ) :
settings = settings | logger . OUTPUT_TO_CONSOLE
if config . get ( ' log.file.output ' , True ) :
settings = settings | logger . OUTPUT_TO_FILE
logger . set_file ( config . get ( ' log.file.path ' , ' /tmp/onionr.log ' ) . replace ( ' data/ ' , dataDir ) )
logger . set_settings ( settings )
if not self is None :
if str ( config . get ( ' general.dev_mode ' , True ) ) . lower ( ) == ' true ' :
self . _developmentMode = True
logger . set_level ( logger . LEVEL_DEBUG )
else :
self . _developmentMode = False
logger . set_level ( logger . LEVEL_INFO )
verbosity = str ( config . get ( ' log.verbosity ' , ' default ' ) ) . lower ( ) . strip ( )
if not verbosity in [ ' default ' , ' null ' , ' none ' , ' nil ' ] :
map = {
str ( logger . LEVEL_DEBUG ) : logger . LEVEL_DEBUG ,
' verbose ' : logger . LEVEL_DEBUG ,
' debug ' : logger . LEVEL_DEBUG ,
str ( logger . LEVEL_INFO ) : logger . LEVEL_INFO ,
' info ' : logger . LEVEL_INFO ,
' information ' : logger . LEVEL_INFO ,
str ( logger . LEVEL_WARN ) : logger . LEVEL_WARN ,
' warn ' : logger . LEVEL_WARN ,
' warning ' : logger . LEVEL_WARN ,
' warnings ' : logger . LEVEL_WARN ,
str ( logger . LEVEL_ERROR ) : logger . LEVEL_ERROR ,
' err ' : logger . LEVEL_ERROR ,
' error ' : logger . LEVEL_ERROR ,
' errors ' : logger . LEVEL_ERROR ,
str ( logger . LEVEL_FATAL ) : logger . LEVEL_FATAL ,
' fatal ' : logger . LEVEL_FATAL ,
str ( logger . LEVEL_IMPORTANT ) : logger . LEVEL_IMPORTANT ,
' silent ' : logger . LEVEL_IMPORTANT ,
' quiet ' : logger . LEVEL_IMPORTANT ,
' important ' : logger . LEVEL_IMPORTANT
}
if verbosity in map :
logger . set_level ( map [ verbosity ] )
else :
logger . warn ( ' Verbosity level %s is not valid, using default verbosity. ' % verbosity )
return data_exists
2018-08-04 02:52:45 +00:00
def openUI ( self ) :
url = ' http://127.0.0.1: %s /ui/index.html?timingToken= %s ' % ( config . get ( ' client.port ' , 59496 ) , self . onionrUtils . getTimeBypassToken ( ) )
2018-11-11 03:25:40 +00:00
logger . info ( ' Opening %s ... ' % url )
2018-08-04 02:52:45 +00:00
webbrowser . open ( url , new = 1 , autoraise = True )
2018-08-23 17:48:49 +00:00
def header ( self , message = logger . colors . fg . pink + logger . colors . bold + ' Onionr ' + logger . colors . reset + logger . colors . fg . pink + ' has started. ' ) :
2018-11-11 03:25:40 +00:00
if os . path . exists ( ' static-data/header.txt ' ) and logger . get_level ( ) < = logger . LEVEL_INFO :
2018-08-23 17:48:49 +00:00
with open ( ' static-data/header.txt ' , ' rb ' ) as file :
# only to stdout, not file or log or anything
2018-11-11 03:25:40 +00:00
sys . stderr . write ( file . read ( ) . decode ( ) . replace ( ' P ' , logger . colors . fg . pink ) . replace ( ' W ' , logger . colors . reset + logger . colors . bold ) . replace ( ' G ' , logger . colors . fg . green ) . replace ( ' \n ' , logger . colors . reset + ' \n ' ) . replace ( ' B ' , logger . colors . bold ) . replace ( ' A ' , ' %s ' % API_VERSION ) . replace ( ' V ' , ONIONR_VERSION ) )
2018-08-23 17:48:49 +00:00
logger . info ( logger . colors . fg . lightgreen + ' -> ' + str ( message ) + logger . colors . reset + logger . colors . fg . lightgreen + ' <- \n ' )
2018-07-06 04:27:12 +00:00
if __name__ == " __main__ " :
Onionr ( )