mirror of https://github.com/dnomd343/ProxyC
dnomd343
2 years ago
8 changed files with 87 additions and 92 deletions
@ -0,0 +1,37 @@ |
|||
#!/usr/bin/env python3 |
|||
# -*- coding: utf-8 -*- |
|||
|
|||
import time |
|||
import subprocess |
|||
from Basis.Logger import logging |
|||
|
|||
|
|||
def run(command: list) -> subprocess.Popen: |
|||
logging.debug('Start dnsproxy -> %s' % command) |
|||
return subprocess.Popen(command, stdout = subprocess.PIPE, stderr = subprocess.STDOUT) |
|||
|
|||
|
|||
def daemon(process: subprocess.Popen, command: list, gap: int = 2) -> None: # daemon dnsproxy process |
|||
while True: # start daemon |
|||
time.sleep(gap) # check time gap |
|||
if process.poll() is not None: # unexpected exit |
|||
logging.warning('dnsproxy unexpected exit') |
|||
logging.debug('output of dnsproxy\n%s' % process.stdout.read().decode('utf-8')) |
|||
process = run(command) |
|||
|
|||
|
|||
def start(servers: list or None, port: int = 53, cache: int = 4194304) -> None: # default cache size -> 4MiB |
|||
if servers is None or len(servers) == 0: # use origin dns server |
|||
logging.info('Skip dnsproxy process') |
|||
return |
|||
with open('/etc/resolv.conf', 'w') as dnsConfig: |
|||
dnsConfig.write('nameserver 127.0.0.1\n') # system dns settings |
|||
dnsCommand = [ |
|||
'dnsproxy', '--all-servers', |
|||
'--port', str(port), |
|||
'--cache', '--cache-size', str(cache) |
|||
] |
|||
for server in servers: |
|||
dnsCommand += ['--upstream', server] # upstream dns servers |
|||
logging.warning('Start dnsproxy at port %i -> %s' % (port, servers)) |
|||
daemon(run(dnsCommand), dnsCommand) |
@ -1,64 +1,39 @@ |
|||
#!/usr/bin/env python3 |
|||
# -*- coding: utf-8 -*- |
|||
|
|||
import os |
|||
import time |
|||
import _thread |
|||
import subprocess |
|||
from Basis import DnsProxy |
|||
from Basis.Check import Check |
|||
from Basis.Logger import logging |
|||
from Basis.Manager import Manager |
|||
from Basis.Api import startServer |
|||
from Basis.Constant import Version |
|||
from Basis.Compile import startCompile |
|||
|
|||
dnsServers = None |
|||
# dnsServers = ['223.5.5.5', '119.28.28.28'] |
|||
|
|||
|
|||
def startDnsproxy(command: list) -> subprocess.Popen: |
|||
logging.debug('start dnsproxy -> %s' % command) |
|||
return subprocess.Popen(command, stdout = subprocess.PIPE, stderr = subprocess.STDOUT) |
|||
# dnsServers = None |
|||
dnsServers = ['223.5.5.5', '119.28.28.28'] |
|||
|
|||
|
|||
def daemonDnsproxy(servers: list or None, port: int = 53, cache: int = 4194304) -> None: # default cache size -> 4MiB |
|||
if servers is None or len(servers) == 0: |
|||
logging.info('skip dnsproxy process') |
|||
return |
|||
logging.info('start dnsproxy at port %i -> %s' % (port, servers)) |
|||
os.system('echo "nameserver 127.0.0.1" > /etc/resolv.conf') # system dns settings |
|||
dnsCommand = [ |
|||
'dnsproxy', '--all-servers', |
|||
'--port', str(port), |
|||
'--cache', '--cache-size', str(cache) |
|||
] |
|||
for server in servers: |
|||
dnsCommand += ['--upstream', server] |
|||
dnsproxy = startDnsproxy(dnsCommand) |
|||
while True: |
|||
time.sleep(2) # daemon time gap |
|||
if dnsproxy.poll() is not None: # unexpected exit |
|||
logging.warning('dnsproxy unexpected exit') |
|||
logging.debug('output of dnsproxy\n%s' % dnsproxy.stdout.read().decode('utf-8')) |
|||
dnsproxy = startDnsproxy(dnsCommand) |
|||
def runCheck() -> None: # try to pop a task and check |
|||
try: |
|||
taskId, taskInfo = Manager.popTask() |
|||
logging.warning('[%s] Load new task' % taskId) |
|||
except: |
|||
return # no more task |
|||
checkResult = Check(taskId, taskInfo) |
|||
logging.warning('[%s] Task finish' % taskId) |
|||
Manager.finishTask(taskId, checkResult) |
|||
|
|||
|
|||
from Basis.Check import Check |
|||
from Basis.Manager import Manager |
|||
|
|||
def loopCheck() -> None: |
|||
while True: |
|||
time.sleep(2) # TODO: thread pool working |
|||
try: |
|||
taskId, taskInfo = Manager.popTask() |
|||
except: |
|||
continue |
|||
logging.info('new task %s -> %s' % (taskId, taskInfo)) |
|||
ret = Check(taskId, taskInfo) |
|||
logging.info('check result -> %s' % ret) |
|||
Manager.finishTask(taskId, ret) |
|||
while True: # TODO: thread pool working |
|||
runCheck() |
|||
time.sleep(2) |
|||
|
|||
|
|||
logging.warning('ProxyC starts running (%s)' % Version) |
|||
_thread.start_new_thread(startCompile, ('/usr', )) # python compile (generate .pyc file) |
|||
_thread.start_new_thread(daemonDnsproxy, (dnsServers, 53)) # set system's dns server |
|||
_thread.start_new_thread(DnsProxy.start, (dnsServers, 53)) # start dns server |
|||
_thread.start_new_thread(loopCheck, ()) # start loop check |
|||
startServer(apiToken = '') # start api server |
|||
|
Loading…
Reference in new issue