This commit is contained in:
emdee@spm.plastiras.org 2024-01-17 14:12:46 +00:00
parent 96c453607f
commit 9c97bb9acb
7 changed files with 42 additions and 119 deletions

View File

@ -1,9 +1,9 @@
PREFIX=/usr/local PREFIX=/usr/local
PYTHON_EXE_MSYS=${PREFIX}/bin/python3.bash
LOCAL_DOCTEST=${PREFIX}/bin/toxcore_run_doctest3.bash LOCAL_DOCTEST=${PREFIX}/bin/toxcore_run_doctest3.bash
DOCTEST=${LOCAL_DOCTEST} DOCTEST=${LOCAL_DOCTEST}
MOD=exclude_badExits MOD=exclude_badExits
check:: check::
sh python3.sh -c "import ${MOD}" sh python3.sh -c "import ${MOD}"
@ -16,7 +16,10 @@ install::
rsync:: rsync::
bash .rsync.sh bash .rsync.sh
test:: doctest test::
${PYTHON_EXE_MSYS} exclude_badExits.py --help
TOR_CONTROLLER_PASSWORD=${PASS} ${PYTHON_EXE_MSYS} src/${MOD}/torcontactinfo.py
doctest:: doctest::
export PYTHONPATH=${PWD}/src/${MOD} export PYTHONPATH=${PWD}/src/${MOD}
${DOCTEST} ${MOD}.txt ${DOCTEST} ${MOD}.txt

View File

@ -132,10 +132,12 @@ To be "good" the ContactInfo must:
```--torrc_output``` will write a file of the commands that it sends to ```--torrc_output``` will write a file of the commands that it sends to
the Tor controller, so you can include it in a ```/etc/toc/torrc```. the Tor controller, so you can include it in a ```/etc/toc/torrc```.
```--relays_output write the download relays in json to a file. The relays ```--relays_output``` write the download relays in json to a file. The relays
are downloaded from https://onionoo.torproject.org/details are downloaded from https://onionoo.torproject.org/details
For usage, do ```python3 exclude_badExits.py --help` For usage, do ```python3 exclude_badExits.py --help```
See [exclude_badExits.txt](./exclude_badExits.txt)
See [exclude_badExits.hlp](./exclude_badExits.hlp)
or there's a doctest file in [exclude_badExits.txt](./exclude_badExits.txt)

View File

@ -7,11 +7,15 @@ SOCKS_HOST=127.0.0.1
CAFILE=/etc/ssl/certs/ca-certificates.crt CAFILE=/etc/ssl/certs/ca-certificates.crt
# you may have a special python for installed packages # you may have a special python for installed packages
EXE=`which python3.bash` EXE=`which python3.bash`
MOD=exclude_badExits
[ -f exclude_badExits.hlp ] || \ [ -f exclude_badExits.hlp -a exclude_badExits.hlp -nt src/exclude_badExits/exclude_badExits.py] || \
$EXE exclude_badExits.py --help > exclude_badExits.hlp PYTHONPATH=$PWD/src \
[ -f README.md ] || \ $EXE src/${MOD}/exclude_badExits.py --help > exclude_badExits.hlp
$EXE -c 'from exclude_badExits import __doc__; print(__doc__)' > README.md [ -f README.md -a README.md -nt src/exclude_badExits/exclude_badExits.py] || \
PYTHONPATH=$PWD/src \
$EXE -c 'from exclude_badExits.exclude_badExits import __doc__; print(__doc__)' \
> README.md
# an example of running exclude_badExits with full debugging # an example of running exclude_badExits with full debugging
# expected to 20 minutes or so # expected to 20 minutes or so
declare -a LARGS declare -a LARGS

View File

@ -83,3 +83,5 @@ This may take a while:
>>> lArgs = ['--proxy_ctl', '9051'] >>> lArgs = ['--proxy_ctl', '9051']
>>> exclude_badExits.iMain(lArgs) >>> exclude_badExits.iMain(lArgs)
There is a doctest test document in exclude_badExits.txt

View File

@ -1,47 +0,0 @@
# -*-mode: python; indent-tabs-mode: nil; py-indent-offset: 4; coding: utf-8 -*
import re
from setuptools import setup, find_packages
with open("qasync/__init__.py") as f:
version = re.search(r'__version__\s+=\s+"(.*)"', f.read()).group(1)
long_description = "\n\n".join([
open("README.md").read(),
])
if __name__ == '__main__':
setup(
name="exclude_badExits",
version=__version__,
description="""A program to exclude bad exits on the Tor network""",
long_description=long_description,
author="Nusenu (originally)",
author_email='',
license="1clause BSD",
packages = find_packages(exclude=['test*']),
# url="",
# download_url="https://",
keywords=['exit nodes', 'Tor', 'tor onion controller'],
# maybe less - nothing fancy
python_requires="~=3.6",
# probably works on PyQt6 and PySide2 but untested
# https://github.com/CabbageDevelopment/qasync/
install_requires=['cryptography',
'rsa',
'stem',
'urllib3',
'yaml'],
entry_points={
'console_scripts': ['exclude_badExits = exclude_badExits.__main__:iMain', ]},
classifiers=[
'Development Status :: 4 - Beta',
'Environment :: Console',
'Intended Audience :: Developers',
'Natural Language :: English',
'Operating System :: OS Independent',
'Programming Language :: Python :: 3',
'Topic :: Security',
'Topic :: Software Development :: Libraries :: Python Modules',
],
)

View File

@ -224,51 +224,6 @@ def vwritefinale(oargs, lNOT_IN_RELAYS_DB) -> None:
LOG.info(f"For info on relays, try: https://onionoo.torproject.org/details") LOG.info(f"For info on relays, try: https://onionoo.torproject.org/details")
# https://onionoo.torproject.org/details # https://onionoo.torproject.org/details
def alt_vsetup_logging(theLOG, log_level, logfile='', stream=sys.stderr) -> None:
global LOG
LOG = theLOG
add = True
logging._defaultFormatter = logging.Formatter(datefmt='%m-%d %H:%M:%S')
logging._defaultFormatter.default_time_format = '%m-%d %H:%M:%S'
logging._defaultFormatter.default_msec_format = ''
if logfile:
add = logfile.startswith('+')
sub = logfile.startswith('-')
if add or sub:
logfile = logfile[1:]
kwargs['filename'] = logfile
if coloredlogs:
coloredlogs.DEFAULT_LEVEL_STYLES['info']=dict(color='white',bold=True)
coloredlogs.DEFAULT_LEVEL_STYLES['debug']=dict(color='cyan')
coloredlogs.DEFAULT_LEVEL_STYLES['warn']=dict(color='yellow',bold=True)
coloredlogs.DEFAULT_LEVEL_STYLES['error']=dict(color='red',bold=True)
coloredlogs.DEFAULT_FIELD_STYLES['levelname=']=dict(color='green', bold=True),
# https://pypi.org/project/coloredlogs/
aKw = dict(level=log_level,
logger=LOG,
stream=stream,
fmt='%(levelname)s %(message)s',
isatty=True,
milliseconds=False,
)
coloredlogs.install(**aKw)
if logfile:
oHandler = logging.FileHandler(logfile)
LOG.addHandler(oHandler)
LOG.info(f"Setting coloured log_level to {log_level} {stream}")
else:
kwargs = dict(level=log_level,
force=True,
format='%(levelname)s %(message)s')
logging.basicConfig(**kwargs)
if add and logfile:
oHandler = logging.StreamHandler(stream)
LOG.addHandler(oHandler)
LOG.info(f"SSetting log_level to {log_level!s}")
def vsetup_logging(theLOG, log_level, logfile='', stream=sys.stdout) -> None: def vsetup_logging(theLOG, log_level, logfile='', stream=sys.stdout) -> None:
global LOG global LOG
LOG = theLOG LOG = theLOG

View File

@ -20,6 +20,8 @@ import os
import re import re
import sys import sys
import json import json
import logging
import warnings
import requests import requests
import textwrap import textwrap
try: try:
@ -33,11 +35,9 @@ except ImportError:
# rprint = print # rprint = print
HAS_RICH = False HAS_RICH = False
import logging
import warnings
warnings.filterwarnings('ignore') warnings.filterwarnings('ignore')
from exclude_utils import vsetup_logging from exclude_badExits.exclude_utils import vsetup_logging
class TorContactInfoParser(object): class TorContactInfoParser(object):
email_regex = "^[a-zA-Z0-9.!#$%&*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$" email_regex = "^[a-zA-Z0-9.!#$%&*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$"
@ -518,6 +518,13 @@ def oparser():
{sys.argv[0]} parse -np -j "Privex Inc. email:noc[]privex.io url:https://www.privex.io proof:uri-rsa pgp:288DD1632F6E8951 keybase:privexinc twitter:PrivexInc" {sys.argv[0]} parse -np -j "Privex Inc. email:noc[]privex.io url:https://www.privex.io proof:uri-rsa pgp:288DD1632F6E8951 keybase:privexinc twitter:PrivexInc"
{{"email": "noc@privex.io", "url": "https://www.privex.io", "proof": "uri-rsa", "pgp": null, "keybase": "privexinc", "twitter": "PrivexInc"}} {{"email": "noc@privex.io", "url": "https://www.privex.io", "proof": "uri-rsa", "pgp": null, "keybase": "privexinc", "twitter": "PrivexInc"}}
""")) """))
cparser.add_argument('--relays_output', type=str,
dest='relays_output',
default=os.path.join(ETC_DIR, 'relays.json'),
help="Write the download relays in json to a file")
cparser.add_argument('-j', '--json', action='store_true',
default=False, dest='json',
help="Output real JSON, not Python dict format.")
cparser.set_defaults(func=cmd_scan, json=False, pretty=False) cparser.set_defaults(func=cmd_scan, json=False, pretty=False)
subparse = cparser.add_subparsers() subparse = cparser.add_subparsers()
subparse.required = False subparse.required = False
@ -527,13 +534,6 @@ def oparser():
sp_parse.add_argument('-np', '--no-pretty', sp_parse.add_argument('-np', '--no-pretty',
action='store_false', default=False, dest='pretty', action='store_false', default=False, dest='pretty',
help="Disable pretty printing JSON") help="Disable pretty printing JSON")
sp_parse.add_argument('--relays_output', type=str,
dest='relays_output',
default=os.path.join(ETC_DIR, 'relays.json'),
help="Write the download relays in json to a file")
sp_parse.add_argument('-j', '--json', action='store_true',
default=False, dest='json',
help="Output real JSON, not Python dict format.")
sp_parse.set_defaults(func=cmd_parse) sp_parse.set_defaults(func=cmd_parse)
sp_scan = subparse.add_parser('scan', help="Parse all contacts from https://onionoo.torproject.org/details") sp_scan = subparse.add_parser('scan', help="Parse all contacts from https://onionoo.torproject.org/details")
@ -544,6 +544,15 @@ def oparser():
return cparser return cparser
def iMain(lArgs=None)
cparser = oparser()
opts = cparser.parse_args(lArgs)
data = None
if opts.relays_output and os.path.exists(opts.relays_output):
data = open(opts.relays_output, 'rt').read()
i = cmd_scan(opts, data)
return i
if __name__ == "__main__": if __name__ == "__main__":
if os.environ.get('DEBUG', ''): if os.environ.get('DEBUG', ''):
log_level = 10 log_level = 10
@ -552,16 +561,11 @@ if __name__ == "__main__":
LOG = logging.getLogger() LOG = logging.getLogger()
vsetup_logging(LOG, log_level) vsetup_logging(LOG, log_level)
try: try:
cparser = oparser() i = iMain(sys.argv[1:])
opts = cparser.parse_args(sys.argv[1:])
data = None
if opts.relays_output and os.path.exists(opts.relays_output):
data = open(opts.relays_output, 'rt').read()
i = cmd_scan(opts, data)
except KeyboardInterrupt as e: except KeyboardInterrupt as e:
i = 0 i = 0
except (requests.exceptions.ProxyError, Exception,) as e: except (requests.exceptions.ProxyError, Exception,) as e:
LOG.error(f"{e}") LOG.exception(f"Exception: {e}", exc_info=True)
i = 0 i = 0
sys.exit(i) sys.exit(i)