Browse Source

update: enhance check process

master
dnomd343 2 years ago
parent
commit
46134732fe
  1. 3
      Basis/Api.py
  2. 32
      Basis/Check.py
  3. 4
      Checker/Http.py
  4. 31
      Checker/__init__.py

3
Basis/Api.py

@ -3,6 +3,7 @@
import json import json
from gevent import pywsgi from gevent import pywsgi
from Checker import formatCheck
from Basis.Logger import logging from Basis.Logger import logging
from Basis.Manager import Manager from Basis.Manager import Manager
from Basis.Constant import Version from Basis.Constant import Version
@ -52,7 +53,7 @@ def createTask() -> Response:
return genError('Invalid token') return genError('Invalid token')
# TODO: format check and proxy list # TODO: format check and proxy list
checkList = request.json.get('check') checkList = formatCheck(request.json.get('check'))
proxyList = request.json.get('proxy') proxyList = request.json.get('proxy')
logging.debug('API create task -> check = %s | proxy = %s' % (checkList, proxyList)) logging.debug('API create task -> check = %s | proxy = %s' % (checkList, proxyList))

32
Basis/Check.py

@ -1,5 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import copy import copy
import time import time
from Checker import Checker from Checker import Checker
@ -7,14 +8,9 @@ from Basis.Logger import logging
from Builder import Builder, clientEntry from Builder import Builder, clientEntry
def Check(taskId: str, taskInfo: dict) -> dict: def buildClient(taskId: str, taskInfo: dict) -> Builder:
logging.info('[%s] Start checking process -> %s' % (taskId, taskInfo))
if taskInfo['type'] not in clientEntry:
logging.error('[%s] Unknown proxy type %s' % (taskId, taskInfo['type']))
raise RuntimeError('Unknown proxy type')
try: try:
client = Builder( return Builder(
proxyType = taskInfo['type'], proxyType = taskInfo['type'],
proxyInfo = taskInfo['info'], proxyInfo = taskInfo['info'],
bindAddr = '127.0.0.1', # socks5 exposed host bindAddr = '127.0.0.1', # socks5 exposed host
@ -23,25 +19,35 @@ def Check(taskId: str, taskInfo: dict) -> dict:
except Exception as reason: except Exception as reason:
logging.error('[%s] Client build error -> %s' % (taskId, reason)) logging.error('[%s] Client build error -> %s' % (taskId, reason))
raise RuntimeError('Client build error') raise RuntimeError('Client build error')
logging.info('[%s] Client loaded successfully' % taskId)
def waitClient(taskId: str, client: Builder):
# TODO: wait port occupied (client.socksPort) # TODO: wait port occupied (client.socksPort)
time.sleep(1) time.sleep(1) # TODO: simple delay for now
if not client.status(): # client unexpected exit if not client.status(): # client unexpected exit
logging.warning('[%s] Client unexpected exit' % taskId) logging.warning('[%s] Client unexpected exit' % taskId)
client.destroy() # remove file and kill sub process client.destroy() # remove file and kill sub process
logging.debug('[%s] Client output\n%s', (taskId, client.output)) logging.debug('[%s] Client output\n%s', (taskId, client.output))
raise RuntimeError('Client unexpected exit') raise RuntimeError('Client unexpected exit')
checkResult = Checker(taskId, taskInfo['check'], {
def Check(taskId: str, taskInfo: dict) -> dict:
logging.info('[%s] Start checking process -> %s' % (taskId, taskInfo))
if taskInfo['type'] not in clientEntry: # unknown proxy type
logging.error('[%s] Unknown proxy type %s' % (taskId, taskInfo['type']))
raise RuntimeError('Unknown proxy type')
client = buildClient(taskId, taskInfo) # build proxy client
logging.info('[%s] Client loaded successfully' % taskId)
waitClient(taskId, client) # wait for the client to start
checkResult = Checker(taskId, taskInfo['check'], { # start check process
'addr': client.socksAddr, 'addr': client.socksAddr,
'port': client.socksPort, 'port': client.socksPort,
}) })
logging.info('[%s] Client check result -> %s' % (taskId, checkResult)) logging.info('[%s] Client check result -> %s' % (taskId, checkResult))
client.destroy() # clean up the client client.destroy() # clean up client
taskInfo = copy.deepcopy(taskInfo) taskInfo = copy.deepcopy(taskInfo)
taskInfo.pop('check') taskInfo.pop('check') # remove check items
return { return {
**taskInfo, **taskInfo,
'result': checkResult, 'result': checkResult, # add check result
} }

4
Checker/Http.py

@ -28,7 +28,7 @@ def httpPing(taskId: str, url: str, socksAddr: str, socksPort: int, timeout: int
def check(taskId: str, socksInfo: dict, options: dict) -> dict: def check(taskId: str, socksInfo: dict, options: dict) -> dict:
# TODO: multi check # TODO: multi check (options -> times)
return { # TODO: just demo return {
'delay': httpPing(taskId, options['url'], socksInfo['addr'], socksInfo['port'], options['timeout']) 'delay': httpPing(taskId, options['url'], socksInfo['addr'], socksInfo['port'], options['timeout'])
} }

31
Checker/__init__.py

@ -1,18 +1,31 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import logging
from Checker import Http from Checker import Http
def Checker(taskId: str, checkInfo: dict, socksInfo: dict) -> dict: checkEntry = {
'http': Http.check
# TODO: ignore checkInfo for now }
httpRet = Http.check(taskId, socksInfo, { def formatCheck(rawInfo: list) -> dict:
'url': 'http://baidu.com/', # TODO: format check info
'timeout': 20, # TODO: rawInfo -> ['...', {'type': '...', ...}, ...]
})
return { return {
'http': httpRet # TODO: just check http delay for now 'http': {
'times': 3,
'url': 'http://baidu.com',
'timeout': 20,
}
} }
# TODO: return check result
def Checker(taskId: str, checkInfo: dict, socksInfo: dict) -> dict:
diffItems = {x for x in checkInfo} - {x for x in checkEntry}
if len(diffItems) != 0: # include unknown check items
logging.error('[%s] Unknown check items -> %s' % (taskId, diffItems))
raise RuntimeError('Unknown check items')
result = {}
for checkItem, checkOptions in checkInfo.items():
result[checkItem] = checkEntry[checkItem](taskId, socksInfo, checkOptions)
return result

Loading…
Cancel
Save