步骤 1:读取所有连接URL
读取所有连接URL,这些URL可能位于根节点、子节点或子节点的子节点中。
import requests
from urllib.parse import urlsplit
from smtplib import SMTPException
from smtplib import Email
import json
def read_connections():
connections = []
for line in open('c connection_list.txt', 'r'):
url = urlsplit(line.strip())
if 'c' in url.path:
connections.append(url)
return connections
connections = read_connections()
print("共有", len(connections), "个连接。")
步骤 2:解析每个连接的结构
解析每个连接的结构,确定是根节点、子节点还是子节点的子节点。
def parse_connection(url):
scheme = url.split('://')[]
host = urlsplit(url).split('://')[1]
if scheme == 'http' or scheme == 'https':
protocol = 'IPsec-Tunnels'
elif scheme == 'ftp':
protocol = 'IPsec'
elif scheme == 'servlet':
protocol = 'IPsec-Tunnels'
elif scheme == 'openvpn':
protocol = 'OpenVPN'
elif scheme == 'sop':
protocol = 'OpenVPN'
elif scheme == 'sop-encrypted':
protocol = 'OpenVPN'
else:
protocol = 'IPsec-Tunnels'
return protocol, url, scheme, host
步骤 3:解析IPsec包
使用IPsec包来解析每个连接,获取服务器的IP地址和端口。
def parse_ipsec包包包(url):
try:
with open(url, 'rb') as f:
conn = json.load(f)
if 'TEld' in conn:
# IPsec-Tunnels
scheme = conn['TEld']['protocol']
key = 'TEld'
conn = conn[key]
endport = conn['to']
host = conn['to']
elif 'Elin' in conn:
# IPsec Tunnel
scheme = conn['Elin']['protocol']
key = 'Elin'
conn = conn[key]
endport = conn['to']
host = conn['to']
elif 'Elin' in conn:
# IPsec Tunnel
scheme = conn['Elin']['protocol']
key = 'Elin'
conn = conn[key]
endport = conn['to']
host = conn['to']
elif 'TEld' in conn:
# IPsec-Tunnels
scheme = conn['TEld']['protocol']
key = 'TEld'
conn = conn[key]
endport = conn['to']
host = conn['to']
elif 'Elin' in conn:
# IPsec Tunnel
scheme = conn['Elin']['protocol']
key = 'Elin'
conn = conn[key]
endport = conn['to']
host = conn['to']
elif 'Elin' in conn:
# IPsec Tunnel
scheme = conn['Elin']['protocol']
key = 'Elin'
conn = conn[key]
endport = conn['to']
host = conn['to']
elif 'TEld' in conn:
# IPsec-Tunnels
scheme = conn['TEld']['protocol']
key = 'TEld'
conn = conn[key]
endport = conn['to']
host = conn['to']
else:
# Handle IPsec Tunnel
scheme = 'IPsec-Tunnels'
key = 'IPsec-Tunnels'
conn = conn[key]
endport = conn['to']
host = conn['to']
except json.JSONDecodeError:
print(f"JSONDecodeError in {url}")
return None, url, scheme, host
return scheme, url, host
scheme, url, host = parse_ipsec包包包(connections[])
print(f"根节点连接:{scheme},IP:{host}, 端口:{url.split(':')[1]}")
步骤 4:解析OpenVPN包
使用OpenVPN包来解析连接信息。
def parse_openvpn包包包(url):
try:
with open(url, 'rb') as f:
conn = json.load(f)
if 'TEld' in conn:
# IPsec-Tunnels
scheme = conn['TEld']['protocol']
key = 'TEld'
conn = conn[key]
endport = conn['to']
host = conn['to']
elif 'Elin' in conn:
# IPsec Tunnel
scheme = conn['Elin']['protocol']
key = 'Elin'
conn = conn[key]
endport = conn['to']
host = conn['to']
elif 'Elin' in conn:
# IPsec Tunnel
scheme = conn['Elin']['protocol']
key = 'Elin'
conn = conn[key]
endport = conn['to']
host = conn['to']
elif 'TEld' in conn:
# IPsec-Tunnels
scheme = conn['TEld']['protocol']
key = 'TEld'
conn = conn[key]
endport = conn['to']
host = conn['to']
elif 'Elin' in conn:
# IPsec Tunnel
scheme = conn['Elin']['protocol']
key = 'Elin'
conn = conn[key]
endport = conn['to']
host = conn['to']
elif 'Elin' in conn:
# IPsec Tunnel
scheme = conn['Elin']['protocol']
key = 'Elin'
conn = conn[key]
endport = conn['to']
host = conn['to']
elif 'TEld' in conn:
# IPsec-Tunnels
scheme = conn['TEld']['protocol']
key = 'TEld'
conn = conn[key]
endport = conn['to']
host = conn['to']
else:
# Handle IPsec Tunnel
scheme = 'IPsec-Tunnels'
key = 'IPsec-Tunnels'
conn = conn[key]
endport = conn['to']
host = conn['to']
except json.JSONDecodeError:
print(f"JSONDecodeError in {url}")
return None, url, scheme, host
except Exception as e:
print(f"Error: {str(e)}")
return None, url, scheme, host
scheme, url, host = parse_openvpn包包包(connections[])
print(f"根节点连接:{scheme},IP:{host}, 端口:{url.split(':')[1]}")
步骤 5:检测所有连接的有效性
遍历每个连接,检测其可用性。
def detect_connections(connections):
success = []
for conn in connections:
scheme, url, host = parse_ipsec包包包(conn)
if scheme == 'IPsec-Tunnels':
# 处理IPsec-Tunnels包
try:
with open(url, 'rb') as f:
conn_ipsec = json.load(f)
if 'TEld' in conn_ipsec:
scheme = conn_ipsec['TEld']['protocol']
key = 'TEld'
conn_ipsec = conn_ipsec[key]
endport = conn_ipsec['to']
host = conn_ipsec['to']
elif 'Elin' in conn_ipsec:
scheme = conn_ipsec['Elin']['protocol']
key = 'Elin'
conn_ipsec = conn_ipsec[key]
endport = conn_ipsec['to']
host = conn_ipsec['to']
elif 'Elin' in conn_ipsec:
scheme = conn_ipsec['Elin']['protocol']
key = 'Elin'
conn_ipsec = conn_ipsec[key]
endport = conn_ipsec['to']
host = conn_ipsec['to']
elif 'TEld' in conn_ipsec:
scheme = conn_ipsec['TEld']['protocol']
key = 'TEld'
conn_ipsec = conn_ipsec[key]
endport = conn_ipsec['to']
host = conn_ipsec['to']
else:
# 处理IPsec Tunnel包
scheme = 'IPsec-Tunnels'
key = 'IPsec-Tunnels'
conn_ipsec = conn_ipsec[key]
endport = conn_ipsec['to']
host = conn_ipsec['to']
# 确保端口在指定范围内
if int(host) >= 0 and int(host) < 256 and int(endport) >= 0 and int(endport) < 65536:
success.append((scheme, url, host, endport))
else:
print(f"端口无效:{endport}")
except Exception as








