2017-12-26 07:25:29 +00:00
#!/usr/bin/env python3
'''
2018-01-14 00:07:13 +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-01-14 08:48:23 +00:00
import sys , os , configparser , base64 , random , getpass , shutil , subprocess , requests
2018-01-09 22:58:12 +00:00
import gui , api , colors , core
from onionrutils import OnionrUtils
2017-12-28 19:30:15 +00:00
from colors import Colors
2017-12-26 07:25:29 +00:00
class Onionr :
def __init__ ( self ) :
2018-01-14 00:07:13 +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-10 03:50:38 +00:00
if os . path . exists ( ' dev-enabled ' ) :
print ( ' DEVELOPMENT MODE ENABLED (THIS IS LESS SECURE!) ' )
self . _developmentMode = True
else :
self . _developmentMode = False
2018-01-02 08:43:29 +00:00
2017-12-28 19:30:15 +00:00
colors = Colors ( )
2017-12-27 01:13:19 +00:00
2018-01-14 08:48:23 +00:00
self . onionrCore = core . Core ( )
self . onionrUtils = OnionrUtils ( )
2018-01-09 22:58:12 +00:00
2017-12-27 05:00:02 +00:00
# Get configuration and Handle commands
2017-12-28 00:18:00 +00:00
2018-01-02 08:43:29 +00:00
self . debug = False # Whole application debugging
2018-01-14 00:07:13 +00:00
try :
os . chdir ( sys . path [ 0 ] )
except FileNotFoundError :
pass
2017-12-27 01:13:19 +00:00
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 ( )
2018-01-14 08:48:23 +00:00
result = self . onionrCore . dataDirDecrypt ( password )
2018-01-09 22:58:12 +00:00
if os . path . exists ( ' data/ ' ) :
break
else :
print ( ' Failed to decrypt: ' + result [ 1 ] )
else :
2018-01-10 03:50:38 +00:00
if not os . path . exists ( ' data/ ' ) :
os . mkdir ( ' data/ ' )
2018-01-10 08:40:25 +00:00
if not os . path . exists ( ' data/peers.db ' ) :
2018-01-14 08:48:23 +00:00
self . onionrCore . createPeerDB ( )
2018-01-10 03:50:38 +00:00
pass
2017-12-28 00:18:00 +00:00
2017-12-27 05:00:02 +00:00
# Get configuration
2017-12-27 01:13:19 +00:00
self . config = configparser . ConfigParser ( )
if os . path . exists ( ' data/config.ini ' ) :
self . config . read ( ' data/config.ini ' )
else :
2017-12-27 05:00:02 +00:00
# Generate default config
2017-12-27 01:13:19 +00:00
# Hostname should only be set if different from 127.x.x.x. Important for DNS rebinding attack prevention.
2017-12-28 00:18:00 +00:00
if self . debug :
2017-12-27 01:13:19 +00:00
randomPort = 8080
else :
randomPort = random . randint ( 1024 , 65535 )
2018-01-05 09:16:21 +00:00
self . config [ ' CLIENT ' ] = { ' CLIENT HMAC ' : base64 . b64encode ( os . urandom ( 32 ) ) . decode ( ' utf-8 ' ) , ' PORT ' : randomPort , ' API VERSION ' : ' 0.0.0 ' }
2017-12-27 01:13:19 +00:00
with open ( ' data/config.ini ' , ' w ' ) as configfile :
self . config . write ( configfile )
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 :
2017-12-26 07:25:29 +00:00
if command == ' start ' :
2018-01-14 08:48:23 +00:00
if os . path . exists ( ' .onionr-lock ' ) :
self . onionrUtils . printErr ( ' 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
elif command == ' stop ' :
self . killDaemon ( )
elif command == ' stats ' :
self . showStats ( )
elif command == ' help ' or command == ' --help ' :
self . showHelp ( )
2017-12-28 19:42:37 +00:00
elif command == ' ' :
print ( ' Do ' , sys . argv [ 0 ] , ' --help for Onionr help. ' )
2017-12-26 09:42:17 +00:00
else :
2017-12-28 19:30:15 +00:00
print ( colors . RED , ' Invalid Command ' , colors . RESET )
2018-01-10 03:50:38 +00:00
if not self . _developmentMode :
2018-01-14 08:48:23 +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
def daemon ( self ) :
2018-01-14 04:30:10 +00:00
''' Start the Onionr communication daemon
'''
2018-01-13 09:03:51 +00:00
if not os . environ . get ( " WERKZEUG_RUN_MAIN " ) == " true " :
2018-01-14 00:07:13 +00:00
subprocess . Popen ( [ " ./communicator.py " , " run " ] )
2018-01-13 09:03:51 +00:00
print ( ' Started communicator ' )
2017-12-27 01:13:19 +00:00
api . API ( self . config , self . debug )
2017-12-26 07:25:29 +00:00
return
def killDaemon ( self ) :
2018-01-14 08:48:23 +00:00
''' Shutdown the Onionr Daemon '''
print ( ' Killing the running daemon ' )
try :
self . onionrUtils . localCommand ( ' shutdown ' )
except requests . exceptions . ConnectionError :
pass
else :
self . onionrCore . daemonQueueAdd ( ' shutdown ' )
2017-12-26 07:25:29 +00:00
return
def showStats ( self ) :
2018-01-14 08:48:23 +00:00
''' Display statistics and exit '''
2017-12-26 07:25:29 +00:00
return
def showHelp ( self ) :
2018-01-14 08:48:23 +00:00
''' Show help for Onionr '''
2017-12-26 07:25:29 +00:00
return
2018-01-13 09:03:51 +00:00
Onionr ( )