某种python对应cf的脚本文件

By | 2022-05-09

代码如下:

linux 下脚本运行
import sys
import http.client
import socket
from IPy import IP
from multiprocessing.dummy import Pool as ThreadPool

class MyHTTPSConnection(http.client.HTTPSConnection):
    def __init__(self, *args, server_hostname=None, **kwargs):
        self.server_hostname = server_hostname
        http.client.HTTPSConnection.__init__(self, *args, **kwargs)
    def connect(self):
        self.sock = self._create_connection((self.host,self.port), 0.5, self.source_address)
        self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        self.sock = self._context.wrap_socket(self.sock, server_hostname=self.server_hostname)

def lianjie(ip):
    domain = 'www.cloudflare.com'
    headers = {'host': domain}
    conn = MyHTTPSConnection(ip, server_hostname=domain)
    try:
        conn.request("GET", "/cdn-cgi/trace", headers=headers)
        r = conn.getresponse()
        print('Cloudflare-IP =','\u001b[35m',ip,'\u001b[0m')
    except Exception as e:
        pass

if __name__ == '__main__':

    banner = '''
   _____
  / ___/_________ _____  ____  ___  _____
  \__ \/ ___/ __ `/ __ \/ __ \/ _ \/ ___/
 ___/ / /__/ /_/ / / / / / / /  __/ /
/____/\___/\__,_/_/ /_/_/ /_/\___/_/ 

            '''

    print('\033[1;34m' + banner + '\033[0m')
    if len(sys.argv) != 2:
        print('usage: python {} 1.2.3.4/24'.format(sys.argv[0]))
        sys.exit(0)
    else:
        print(sys.argv[1])
        ipp = IP(sys.argv[1])
        ips = []
        if ipp.len() == 1:
            print('正在扫描IP-' + str(ipp))
            lianjie(sys.argv[1])
        else:
            print('共有', ipp.len(), '个IP')
            qz = 24 - (IP(ipp).prefixlen())
            for i in range(2 ** qz):
                ioi = IP(IP(IP(ipp).net()).int() + (256 * i))
                print('正在扫描' + str(ioi) + '段')
                for u in range(256):
                    ips.append(IP(IP(ioi).int() + u).strCompressed())
                pool = ThreadPool(processes=20)
                # get传递超时时间,用于捕捉ctrl+c
                pool.map_async(lianjie, ips)
                pool.close()
                pool.join()
                ips = []  # 每段分清
        print('扫描完毕,结果如上')

python 1.py 3.3.3.0/24
python 1.py 3.3.3.3