diff --git a/onionr/netcontroller/__init__.py b/onionr/netcontroller/__init__.py new file mode 100755 index 00000000..c9a0337e --- /dev/null +++ b/onionr/netcontroller/__init__.py @@ -0,0 +1,4 @@ +from . import torbinary, getopenport, netcontrol +tor_binary = torbinary.tor_binary +get_open_port = getopenport.get_open_port +NetController = netcontrol.NetController \ No newline at end of file diff --git a/onionr/netcontroller/getopenport.py b/onionr/netcontroller/getopenport.py new file mode 100644 index 00000000..97d64ff7 --- /dev/null +++ b/onionr/netcontroller/getopenport.py @@ -0,0 +1,29 @@ +''' + Onionr - Private P2P Communication + + get an open port +''' +''' + 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 . +''' +import socket +def get_open_port(): + # taken from (but modified) https://stackoverflow.com/a/2838309 by https://stackoverflow.com/users/133374/albert ccy-by-sa-3 https://creativecommons.org/licenses/by-sa/3.0/ + # changes from source: import moved to top of file, bind specifically to localhost + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.bind(("127.0.0.1",0)) + s.listen(1) + port = s.getsockname()[1] + s.close() + return port \ No newline at end of file diff --git a/onionr/netcontroller.py b/onionr/netcontroller/netcontrol.py old mode 100755 new mode 100644 similarity index 88% rename from onionr/netcontroller.py rename to onionr/netcontroller/netcontrol.py index 3d43db8c..365ea65f --- a/onionr/netcontroller.py +++ b/onionr/netcontroller/netcontrol.py @@ -17,27 +17,10 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' -import subprocess, os, sys, time, signal, base64, socket -from shutil import which -import logger, config +import os, sys, base64, subprocess +import config, logger +from . import getopenport config.reload() -def getOpenPort(): - # taken from (but modified) https://stackoverflow.com/a/2838309 by https://stackoverflow.com/users/133374/albert ccy-by-sa-3 https://creativecommons.org/licenses/by-sa/3.0/ - # changes from source: import moved to top of file, bind specifically to localhost - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.bind(("127.0.0.1",0)) - s.listen(1) - port = s.getsockname()[1] - s.close() - return port - -def torBinary(): - '''Return tor binary path or none if not exists''' - torPath = './tor' - if not os.path.exists(torPath): - torPath = which('tor') - return torPath - class NetController: ''' This class handles hidden service setup on Tor and I2P @@ -51,7 +34,7 @@ class NetController: self.torConfigLocation = self.dataDir + 'torrc' self.readyState = False - self.socksPort = getOpenPort() + self.socksPort = getopenport.get_open_port() self.hsPort = hsPort self._torInstnace = '' self.myID = '' @@ -80,7 +63,7 @@ class NetController: config.set('tor.controlpassword', plaintext, savefile=True) config.set('tor.socksport', self.socksPort, savefile=True) - controlPort = getOpenPort() + controlPort = getopenport.get_open_port() config.set('tor.controlPort', controlPort, savefile=True) diff --git a/onionr/netcontroller/torbinary.py b/onionr/netcontroller/torbinary.py new file mode 100644 index 00000000..fb61e2d6 --- /dev/null +++ b/onionr/netcontroller/torbinary.py @@ -0,0 +1,28 @@ +''' + Onionr - Private P2P Communication + + get the tor binary path +''' +''' + 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 . +''' +import os +from shutil import which + +def tor_binary(): + '''Return tor binary path or none if not exists''' + tor_path = './tor' + if not os.path.exists(tor_path): + tor_path = which('tor') + return tor_path \ No newline at end of file diff --git a/onionr/onionr.py b/onionr/onionr.py index 0c3a71f1..2a1cc30d 100755 --- a/onionr/onionr.py +++ b/onionr/onionr.py @@ -74,7 +74,7 @@ class Onionr: # Load global configuration data data_exists = Onionr.setupConfig(self.dataDir, self) - if netcontroller.torBinary() is None: + if netcontroller.tor_binary() is None: logger.error('Tor is not installed', terminal=True) sys.exit(1) diff --git a/onionr/onionrservices/bootstrapservice.py b/onionr/onionrservices/bootstrapservice.py index 752ce90b..2c23983b 100755 --- a/onionr/onionrservices/bootstrapservice.py +++ b/onionr/onionrservices/bootstrapservice.py @@ -22,7 +22,7 @@ from gevent.pywsgi import WSGIServer, WSGIHandler from stem.control import Controller from flask import Flask, Response import core -from netcontroller import getOpenPort +from netcontroller import get_open_port from . import httpheaders from onionrutils import stringvalidators, epoch @@ -36,7 +36,7 @@ def bootstrap_client_service(peer, core_inst=None, bootstrap_timeout=300): if not stringvalidators.validate_pub_key(peer): raise ValueError('Peer must be valid base32 ed25519 public key') - bootstrap_port = getOpenPort() + bootstrap_port = get_open_port() bootstrap_app = Flask(__name__) http_server = WSGIServer(('127.0.0.1', bootstrap_port), bootstrap_app, log=None) try: diff --git a/onionr/onionrservices/connectionserver.py b/onionr/onionrservices/connectionserver.py index e97b30e0..047fdcf1 100755 --- a/onionr/onionrservices/connectionserver.py +++ b/onionr/onionrservices/connectionserver.py @@ -22,7 +22,7 @@ from stem.control import Controller from flask import Flask import core, logger, httpapi import onionrexceptions -from netcontroller import getOpenPort +from netcontroller import get_open_port from httpapi import apiutils from onionrutils import stringvalidators, basicrequests, bytesconverter from . import httpheaders @@ -39,7 +39,7 @@ class ConnectionServer: socks = core_inst.config.get('tor.socksport') # Load config for Tor socks port for proxy service_app = Flask(__name__) # Setup Flask app for server. - service_port = getOpenPort() + service_port = get_open_port() service_ip = apiutils.setbindip.set_bind_IP(core_inst=self.core_inst) http_server = WSGIServer(('127.0.0.1', service_port), service_app, log=None) core_inst.onionrInst.communicatorInst.service_greenlets.append(http_server)