natapp.py 文件源码

python
阅读 17 收藏 0 点赞 0 评论 0

项目:python-ngrok 作者: hauntek 项目源码 文件源码
def natapp_auth(options):
    host = 'auth.natapp.cn'
    port = 443
    try:
        client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        ssl_client = ssl.wrap_socket(client, ssl_version=ssl.PROTOCOL_TLSv1) # ssl.PROTOCOL_TLSv1_2
        ssl_client.connect((host, port))
    except Exception:
        print('???????: https://auth.natapp.cn ??.')
        time.sleep(10)
        sys.exit()

    data = {
        'Authtoken': options['authtoken'],
        'Clienttoken': options['clienttoken'],
        'Token': 'fffeephptokenkhd672'
    }
    query = json.dumps(data)

    header = "POST " + "/auth" + " HTTP/1.1" + "\r\n"
    header += "Content-Type: text/html" + "\r\n"
    header += "Host: auth.natapp.cn" + "\r\n"
    header += "Content-Length: %d" + "\r\n"
    header += "\r\n" + "%s"
    buf = header % (len(query), query)
    ssl_client.sendall(buf.encode('utf-8')) # ?????

    fd = ssl_client.makefile('rb', 0)
    body = bytes()
    while True:
        line = fd.readline().decode('utf-8')
        if line == "\n" or line == "\r\n":
            chunk_size = int(fd.readline(), 16)
            if chunk_size > 0:
                body = fd.read(chunk_size).decode('utf-8')
                break

    ssl_client.close()

    authData = json.loads(body)
    if authData['success'] == False:
        print('????:%s, ErrorCode:%s' % (authData['msg'], authData['errorCode']))
        time.sleep(10)
        sys.exit()

    print('????,???????...')
    proto = authData['data']['ServerAddr'].split(':')
    return proto
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号