refactored netcontroller into a module

This commit is contained in:
Kevin Froman 2019-07-14 01:51:43 -05:00
parent 9298b3716b
commit 7aadd6a3b5
7 changed files with 71 additions and 27 deletions

View File

@ -0,0 +1,4 @@
from . import torbinary, getopenport, netcontrol
tor_binary = torbinary.tor_binary
get_open_port = getopenport.get_open_port
NetController = netcontrol.NetController

View File

@ -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 <https://www.gnu.org/licenses/>.
'''
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

View File

@ -17,27 +17,10 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
import subprocess, os, sys, time, signal, base64, socket import os, sys, base64, subprocess, signal
from shutil import which import config, logger
import logger, config from . import getopenport
config.reload() 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: class NetController:
''' '''
This class handles hidden service setup on Tor and I2P This class handles hidden service setup on Tor and I2P
@ -51,7 +34,7 @@ class NetController:
self.torConfigLocation = self.dataDir + 'torrc' self.torConfigLocation = self.dataDir + 'torrc'
self.readyState = False self.readyState = False
self.socksPort = getOpenPort() self.socksPort = getopenport.get_open_port()
self.hsPort = hsPort self.hsPort = hsPort
self._torInstnace = '' self._torInstnace = ''
self.myID = '' self.myID = ''
@ -80,7 +63,7 @@ class NetController:
config.set('tor.controlpassword', plaintext, savefile=True) config.set('tor.controlpassword', plaintext, savefile=True)
config.set('tor.socksport', self.socksPort, savefile=True) config.set('tor.socksport', self.socksPort, savefile=True)
controlPort = getOpenPort() controlPort = getopenport.get_open_port()
config.set('tor.controlPort', controlPort, savefile=True) config.set('tor.controlPort', controlPort, savefile=True)

View File

@ -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 <https://www.gnu.org/licenses/>.
'''
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

View File

@ -74,7 +74,7 @@ class Onionr:
# Load global configuration data # Load global configuration data
data_exists = Onionr.setupConfig(self.dataDir, self) 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) logger.error('Tor is not installed', terminal=True)
sys.exit(1) sys.exit(1)

View File

@ -22,7 +22,7 @@ from gevent.pywsgi import WSGIServer, WSGIHandler
from stem.control import Controller from stem.control import Controller
from flask import Flask, Response from flask import Flask, Response
import core import core
from netcontroller import getOpenPort from netcontroller import get_open_port
from . import httpheaders from . import httpheaders
from onionrutils import stringvalidators, epoch 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): if not stringvalidators.validate_pub_key(peer):
raise ValueError('Peer must be valid base32 ed25519 public key') raise ValueError('Peer must be valid base32 ed25519 public key')
bootstrap_port = getOpenPort() bootstrap_port = get_open_port()
bootstrap_app = Flask(__name__) bootstrap_app = Flask(__name__)
http_server = WSGIServer(('127.0.0.1', bootstrap_port), bootstrap_app, log=None) http_server = WSGIServer(('127.0.0.1', bootstrap_port), bootstrap_app, log=None)
try: try:

View File

@ -22,7 +22,7 @@ from stem.control import Controller
from flask import Flask from flask import Flask
import core, logger, httpapi import core, logger, httpapi
import onionrexceptions import onionrexceptions
from netcontroller import getOpenPort from netcontroller import get_open_port
from httpapi import apiutils from httpapi import apiutils
from onionrutils import stringvalidators, basicrequests, bytesconverter from onionrutils import stringvalidators, basicrequests, bytesconverter
from . import httpheaders from . import httpheaders
@ -39,7 +39,7 @@ class ConnectionServer:
socks = core_inst.config.get('tor.socksport') # Load config for Tor socks port for proxy 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_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) 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) http_server = WSGIServer(('127.0.0.1', service_port), service_app, log=None)
core_inst.onionrInst.communicatorInst.service_greenlets.append(http_server) core_inst.onionrInst.communicatorInst.service_greenlets.append(http_server)