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 |
#!/usr/bin/env python3 |
||||
# -*- coding: utf-8 -*- |
# -*- coding: utf-8 -*- |
||||
|
|
||||
import os |
|
||||
import time |
import time |
||||
import _thread |
import _thread |
||||
import subprocess |
from Basis import DnsProxy |
||||
|
from Basis.Check import Check |
||||
from Basis.Logger import logging |
from Basis.Logger import logging |
||||
|
from Basis.Manager import Manager |
||||
from Basis.Api import startServer |
from Basis.Api import startServer |
||||
from Basis.Constant import Version |
from Basis.Constant import Version |
||||
from Basis.Compile import startCompile |
from Basis.Compile import startCompile |
||||
|
|
||||
dnsServers = None |
# dnsServers = None |
||||
# dnsServers = ['223.5.5.5', '119.28.28.28'] |
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) |
|
||||
|
|
||||
|
|
||||
def daemonDnsproxy(servers: list or None, port: int = 53, cache: int = 4194304) -> None: # default cache size -> 4MiB |
def runCheck() -> None: # try to pop a task and check |
||||
if servers is None or len(servers) == 0: |
try: |
||||
logging.info('skip dnsproxy process') |
taskId, taskInfo = Manager.popTask() |
||||
return |
logging.warning('[%s] Load new task' % taskId) |
||||
logging.info('start dnsproxy at port %i -> %s' % (port, servers)) |
except: |
||||
os.system('echo "nameserver 127.0.0.1" > /etc/resolv.conf') # system dns settings |
return # no more task |
||||
dnsCommand = [ |
checkResult = Check(taskId, taskInfo) |
||||
'dnsproxy', '--all-servers', |
logging.warning('[%s] Task finish' % taskId) |
||||
'--port', str(port), |
Manager.finishTask(taskId, checkResult) |
||||
'--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) |
|
||||
|
|
||||
|
|
||||
from Basis.Check import Check |
|
||||
from Basis.Manager import Manager |
|
||||
|
|
||||
def loopCheck() -> None: |
def loopCheck() -> None: |
||||
while True: |
while True: # TODO: thread pool working |
||||
time.sleep(2) # TODO: thread pool working |
runCheck() |
||||
try: |
time.sleep(2) |
||||
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) |
|
||||
|
|
||||
|
|
||||
logging.warning('ProxyC starts running (%s)' % Version) |
logging.warning('ProxyC starts running (%s)' % Version) |
||||
_thread.start_new_thread(startCompile, ('/usr', )) # python compile (generate .pyc file) |
_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 |
_thread.start_new_thread(loopCheck, ()) # start loop check |
||||
startServer(apiToken = '') # start api server |
startServer(apiToken = '') # start api server |
||||
|
Loading…
Reference in new issue