implement combo config parser

allows all options to be overridden by command line.

e.g.
[watchd]
threads=10
debug=false

--watch.threads=50 --debug=true
This commit is contained in:
rofl0r
2019-01-08 02:17:04 +00:00
parent e7b8d526c0
commit f16f754b0e
5 changed files with 131 additions and 67 deletions

View File

@@ -4,12 +4,14 @@ import threading
import time, random, string, re, copy
#from geoip import geolite2
import config
from config import Config
import mysqlite
from misc import _log
import rocksock
config = Config()
_run_standalone = False
class WorkerJob():
@@ -24,7 +26,7 @@ class WorkerJob():
def connect_socket(self):
srv = random.choice(config.servers).strip()
protos = ['http', 'socks5', 'socks4'] if self.proto is None else [self.proto]
server_port = 6697 if config._watchd.use_ssl else 6667
server_port = 6697 if config.watchd.use_ssl else 6667
fail_inc = 1
@@ -37,12 +39,12 @@ class WorkerJob():
]
try:
sock = rocksock.Rocksock(host=srv, port=server_port, ssl=config._watchd.use_ssl, proxies=proxies, timeout=config._watchd.timeout)
sock = rocksock.Rocksock(host=srv, port=server_port, ssl=config.watchd.use_ssl, proxies=proxies, timeout=config.watchd.timeout)
sock.connect()
sock.send('%s\n' % random.choice(['NICK', 'USER', 'JOIN', 'MODE', 'PART', 'INVITE', 'KNOCK', 'WHOIS', 'WHO', 'NOTICE', 'PRIVMSG', 'PING', 'QUIT']))
return sock, proto, duration, torhost, srv, 0
except rocksock.RocksockException as e:
if config._watchd.debug:
if config.watchd.debug:
_log("proxy failed: %s://%s: %s"%(proto, self.proxy, e.get_errormessage()), 'debug')
et = e.get_errortype()
@@ -183,7 +185,7 @@ class Proxywatchd():
while not self.stopped.is_set(): time.sleep(0.1)
def _prep_db(self):
self.mysqlite = mysqlite.mysqlite(config.database, str)
self.mysqlite = mysqlite.mysqlite(config.common.database, str)
def _close_db(self):
if self.mysqlite:
self.mysqlite.close()
@@ -201,14 +203,14 @@ class Proxywatchd():
self.mysqlite.commit()
self._close_db()
self.submit_after = config._watchd.submit_after # number of collected jobs before writing db
self.submit_after = config.watchd.submit_after # number of collected jobs before writing db
self.jobs = []
self.collected = []
def prepare_jobs(self):
self._prep_db()
q = 'SELECT proxy,proto,failed,success_count,total_duration FROM proxylist WHERE failed<? and tested<? ORDER BY RANDOM()' # ' LIMIT ?'
rows = self.mysqlite.execute(q, (config._watchd.maxfail, time.time())).fetchall()
rows = self.mysqlite.execute(q, (config.watchd.max_fail, time.time())).fetchall()
for row in rows:
job = WorkerJob(row[0], row[1], row[2], row[3], row[4])
self.jobs.append(job)
@@ -254,7 +256,7 @@ class Proxywatchd():
return ret
def start(self):
if config._watchd.threads == 1 and _run_standalone:
if config.watchd.threads == 1 and _run_standalone:
return self._run()
else:
return self._run_background()
@@ -271,7 +273,7 @@ class Proxywatchd():
def _run(self):
_log('starting...', 'watchd')
for i in range(config._watchd.threads):
for i in range(config.watchd.threads):
threadid = ''.join( [ random.choice(string.letters) for x in range(5) ] )
wt = WorkerThread(threadid)
if self.in_background: