mirror of https://github.com/dnomd343/ProxyC
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
2.8 KiB
110 lines
2.8 KiB
3 years ago
|
#!/usr/bin/python
|
||
|
# -*- coding:utf-8 -*-
|
||
|
|
||
|
import os
|
||
|
import time
|
||
|
|
||
|
import ProxyBuilder as Builder
|
||
|
import ProxyChecker as Checker
|
||
|
|
||
3 years ago
|
def __loadDir(folderPath): # 创建文件夹
|
||
3 years ago
|
try:
|
||
|
if os.path.exists(folderPath): # 文件 / 文件夹 存在
|
||
|
if not os.path.isdir(folderPath): # 文件
|
||
|
return False # 无法创建
|
||
|
else: # 不存在
|
||
|
os.makedirs(folderPath) # 递归创建文件夹
|
||
|
return True # 文件夹正常
|
||
|
except:
|
||
|
return False
|
||
|
|
||
3 years ago
|
def __proxyHttpCheck(socksPort, httpCheckUrl, httpCheckTimeout): # Http检测
|
||
3 years ago
|
try:
|
||
3 years ago
|
health, httpDelay = Checker.httpCheck(
|
||
|
socksPort,
|
||
|
url = httpCheckUrl,
|
||
|
timeout = httpCheckTimeout
|
||
|
)
|
||
3 years ago
|
if health == None: # 连接失败
|
||
|
return None
|
||
|
return {
|
||
|
'delay': httpDelay,
|
||
|
'health': health
|
||
|
}
|
||
|
except: # 未知错误
|
||
|
return None
|
||
|
|
||
3 years ago
|
def proxyTest(
|
||
|
rawInfo,
|
||
|
startDelay = 1,
|
||
|
workDir = '/tmp/ProxyC',
|
||
|
httpCheckUrl = 'http://gstatic.com/generate_204',
|
||
|
httpCheckTimeout = 12,
|
||
|
):
|
||
3 years ago
|
'''
|
||
|
代理检测入口
|
||
|
|
||
3 years ago
|
程序异常:
|
||
3 years ago
|
return None
|
||
|
|
||
|
启动失败:
|
||
|
return {
|
||
|
'success': False
|
||
|
}
|
||
|
|
||
|
测试完成:
|
||
|
return {
|
||
|
'success': True,
|
||
|
'result': checkResult
|
||
|
}
|
||
3 years ago
|
|
||
|
'''
|
||
3 years ago
|
if __loadDir(workDir) == False: # 工作文件夹无效
|
||
3 years ago
|
return None
|
||
|
if not 'info' in rawInfo: # 缺少代理服务器信息
|
||
|
return None
|
||
|
|
||
|
try:
|
||
|
status, client = Builder.build(rawInfo['info'], workDir)
|
||
|
except: # 构建发生未知错误
|
||
|
Builder.destroy(client)
|
||
|
return None
|
||
|
if status == None: # 构建错误
|
||
|
Builder.destroy(client)
|
||
|
return None
|
||
|
elif status == False: # 节点信息有误
|
||
|
return {
|
||
|
'success': False
|
||
|
}
|
||
|
|
||
|
time.sleep(startDelay) # 延迟等待客户端启动
|
||
|
status = Builder.check(client) # 检查客户端状态
|
||
|
if status == None: # 检测失败
|
||
|
Builder.destroy(client)
|
||
|
return None
|
||
|
elif status == False: # 客户端异常退出
|
||
|
Builder.destroy(client)
|
||
|
return {
|
||
|
'success': False
|
||
|
}
|
||
|
|
||
|
if not 'check' in rawInfo: # 缺少检测项目
|
||
|
return None
|
||
|
checkItem = rawInfo['check']
|
||
|
checkResult = {}
|
||
|
for item in checkItem:
|
||
|
if item == 'http': # http检测
|
||
3 years ago
|
result = __proxyHttpCheck(client['port'], httpCheckUrl, httpCheckTimeout)
|
||
3 years ago
|
else: # 未知检测项目
|
||
|
result = None
|
||
|
if result == None: # 检测出错
|
||
|
Builder.destroy(client)
|
||
|
return None
|
||
|
checkResult[item] = result
|
||
3 years ago
|
|
||
3 years ago
|
Builder.destroy(client) # 销毁客户端
|
||
|
return {
|
||
|
'success': True,
|
||
|
'result': checkResult
|
||
|
}
|