web.py 3.9 KB
Newer Older
H
hjdhnx 已提交
1 2 3 4 5 6
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# File  : web.py
# Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
# Date  : 2022/8/25

H
hjdhnx 已提交
7
import socket
H
hjdhnx 已提交
8
import hashlib
H
hjdhnx 已提交
9
from werkzeug.utils import import_string
H
hjdhnx 已提交
10
import psutil
H
hjdhnx 已提交
11
from flask import request
H
hjdhnx 已提交
12
from utils.log import logger
H
hjdhnx 已提交
13 14 15
MOBILE_UA = 'Mozilla/5.0 (Linux; Android 11; M2007J3SC Build/RKQ1.200826.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/77.0.3865.120 MQQBrowser/6.2 TBS/045714 Mobile Safari/537.36'
PC_UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36'
UA = 'Mozilla/5.0'
H
hjdhnx 已提交
16
UC_UA = 'Mozilla/5.0 (Linux; U; Android 9; zh-CN; MI 9 Build/PKQ1.181121.001) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.108 UCBrowser/12.5.5.1035 Mobile Safari/537.36'
H
hjdhnx 已提交
17 18 19 20
headers = {
        'Referer': 'https://www.baidu.com',
        'user-agent': UA,
}
H
hjdhnx 已提交
21
from time import time
22
import config as settings
H
hjdhnx 已提交
23

H
hjdhnx 已提交
24
def get_host_ip2(): # 获取局域网ip
H
hjdhnx 已提交
25 26 27 28 29 30 31 32
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        # print('8888')
        s.connect(('8.8.8.8', 80))  # 114.114.114.114也是dns地址
        ip = s.getsockname()[0]
    finally:
        s.close()
    return ip
H
hjdhnx 已提交
33

H
hjdhnx 已提交
34 35
def get_host_ip_old(): # 获取局域网ip
    from netifaces import interfaces, ifaddresses, AF_INET
H
hjdhnx 已提交
36 37 38 39 40
    ips = []
    for ifaceName in interfaces():
        addresses = ''.join([i['addr'] for i in ifaddresses(ifaceName).setdefault(AF_INET, [{'addr': ''}])])
        ips.append(addresses)
    real_ips = list(filter(lambda x:x and x!='127.0.0.1',ips))
41
    # logger.info(real_ips)
H
hjdhnx 已提交
42 43 44
    jyw = list(filter(lambda x:str(x).startswith('192.168'),real_ips))
    return real_ips[-1] if len(jyw) < 1 else jyw[0]

H
hjdhnx 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
def get_host_ip(): # 获取局域网ip
    info = psutil.net_if_addrs()
    # print(info)
    netcard_info = []
    ips = []
    for k, v in info.items():
        for item in v:
            if item[0] == 2:
                netcard_info.append((k, item[1]))
                ips.append(item[1])
    print(netcard_info)
    real_ips = list(filter(lambda x: x and x != '127.0.0.1', ips))
    jyw = list(filter(lambda x: str(x).startswith('192.168'), real_ips))
    return real_ips[-1] if len(jyw) < 1 else jyw[0]

H
hjdhnx 已提交
60 61
def getHost(mode=0,port=None):
    port = port or request.environ.get('SERVER_PORT')
H
hjdhnx 已提交
62 63
    # hostname = socket.gethostname()
    # ip = socket.gethostbyname(hostname)
H
hjdhnx 已提交
64 65 66 67
    # ip = request.remote_addr
    # print(ip)
    # mode 为0是本地,1是局域网 2是线上
    if mode == 0:
68
        host = f'http://localhost:{port}'
H
hjdhnx 已提交
69
    elif mode == 1:
70 71
        REAL_IP = get_host_ip()
        ip = REAL_IP
72
        host = f'http://{ip}:{port}'
H
hjdhnx 已提交
73
    else:
74
        host = get_conf(settings).get('PLAY_URL','http://cms.nokia.press')
H
hjdhnx 已提交
75 76 77 78 79 80 81 82 83 84
    return host

def get_conf(obj):
    new_conf = {}
    if isinstance(obj, str):
        config = import_string(obj)
    for key in dir(obj):
        if key.isupper():
            new_conf[key] = getattr(obj, key)
    # print(new_conf)
H
hjdhnx 已提交
85 86 87 88 89
    return new_conf

def get_interval(t):
    interval = time() - t
    interval = round(interval*1000,2)
H
hjdhnx 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    return interval

def md5(str):
    return hashlib.md5(str.encode(encoding='UTF-8')).hexdigest()

def verfy_token(token=''):
    if not token or len(str(token))!=32:
        return False
    cfg = get_conf(settings)
    username = cfg.get('UNAME','')
    pwd = cfg.get('PWD','')
    ctoken = md5(f'{username};{pwd}')
    # print(f'username:{username},pwd:{pwd},current_token:{ctoken},input_token:{ctoken}')
    if token != ctoken:
        return False
    return True
H
hjdhnx 已提交
106 107 108 109 110 111 112 113 114

def getHeaders(url):
    headers = {}
    if url:
        headers.setdefault("Referer", url)
    headers.setdefault("User-Agent", PC_UA)
    headers.setdefault("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
    headers.setdefault("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2")
    return headers