2020-08-09 01:44:11 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
|
|
|
|
|
|
|
Generate a generate a torrc file for our Onionr instance
|
|
|
|
"""
|
2019-12-12 08:47:33 +00:00
|
|
|
import base64
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
from .. import getopenport
|
|
|
|
from . import customtorrc
|
|
|
|
from . import addbridges
|
|
|
|
from . import torbinary
|
|
|
|
from utils import identifyhome
|
|
|
|
import config
|
2020-08-09 01:44:11 +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/>.
|
|
|
|
"""
|
2019-12-12 08:47:33 +00:00
|
|
|
|
|
|
|
add_bridges = addbridges.add_bridges
|
|
|
|
|
|
|
|
|
|
|
|
def generate_torrc(net_controller, api_server_ip):
|
2020-08-09 01:44:11 +00:00
|
|
|
"""Generate a torrc file for our tor instance."""
|
2019-12-12 08:47:33 +00:00
|
|
|
socks_port = net_controller.socksPort
|
2019-12-20 07:21:43 +00:00
|
|
|
hs_port = net_controller.hsPort
|
2019-12-12 08:47:33 +00:00
|
|
|
home_dir = identifyhome.identify_home()
|
|
|
|
tor_config_location = home_dir + '/torrc'
|
|
|
|
|
2020-03-26 08:48:57 +00:00
|
|
|
hs_ver = 'HiddenServiceVersion 3'
|
2019-12-12 08:47:33 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
Set the Tor control password.
|
|
|
|
Meant to make it harder to manipulate our Tor instance
|
|
|
|
"""
|
2020-08-09 01:44:11 +00:00
|
|
|
plaintext = base64.b85encode(os.urandom(50)).decode()
|
2019-12-12 08:47:33 +00:00
|
|
|
config.set('tor.controlpassword', plaintext, savefile=True)
|
|
|
|
config.set('tor.socksport', socks_port, savefile=True)
|
|
|
|
|
|
|
|
controlPort = getopenport.get_open_port()
|
|
|
|
|
|
|
|
config.set('tor.controlPort', controlPort, savefile=True)
|
|
|
|
|
|
|
|
hashedPassword = subprocess.Popen([torbinary.tor_binary(),
|
|
|
|
'--hash-password',
|
|
|
|
plaintext],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
|
|
|
for line in iter(hashedPassword.stdout.readline, b''):
|
|
|
|
password = line.decode()
|
|
|
|
if 'warn' not in password:
|
|
|
|
break
|
|
|
|
|
|
|
|
torrc_data = """SocksPort """ + str(socks_port) + """ OnionTrafficOnly
|
|
|
|
DataDirectory """ + home_dir + """tordata/
|
|
|
|
CookieAuthentication 1
|
|
|
|
KeepalivePeriod 40
|
|
|
|
CircuitsAvailableTimeout 86400
|
|
|
|
ControlPort """ + str(controlPort) + """
|
|
|
|
HashedControlPassword """ + str(password) + """
|
|
|
|
"""
|
|
|
|
if config.get('general.security_level', 1) == 0:
|
|
|
|
torrc_data += """\nHiddenServiceDir """ + home_dir + """hs/
|
|
|
|
\n""" + hs_ver + """\n
|
|
|
|
HiddenServiceNumIntroductionPoints 6
|
|
|
|
HiddenServiceMaxStreams 100
|
|
|
|
HiddenServiceMaxStreamsCloseCircuit 1
|
2019-12-20 07:21:43 +00:00
|
|
|
HiddenServicePort 80 """ + api_server_ip + """:""" + str(hs_port)
|
2019-12-12 08:47:33 +00:00
|
|
|
|
|
|
|
torrc_data = add_bridges(torrc_data)
|
|
|
|
|
|
|
|
torrc_data += customtorrc.get_custom_torrc()
|
|
|
|
|
|
|
|
torrc = open(tor_config_location, 'w')
|
|
|
|
torrc.write(torrc_data)
|
|
|
|
torrc.close()
|