def connect_ssr(ssr):
  result={}
  result['host']=ssr['server']
  result['remarks']=ssr['remarks']
  result['ip']=''
  result['download']=0
  result['upload']=0
  result['ping']=0
  result['ping_pc']=0
  result['youtube']=0
  result['state']="Fail"
  try:
    if not ssr['select']:
        # print ("ss_pass")
        result['state']="pass"
        return result
    port="6667"
    cmd="python shadowsocksr/shadowsocks/local.py -qq -s %s -p %s -k %s -m %s -O %s -o %s -b %s -l %s " % (ssr['server'],ssr['port'],ssr['password'],ssr['method'],ssr['protocol'],ssr['obfs'],"127.0.0.1",port)
    if(len(ssr.get('protoparam',""))>1):
        cmd+="-G %s " % ssr['protoparam']
    if(len(ssr.get('obfsparam',""))>1):
        cmd+="-g %s " % ssr['obfsparam']
    os.system(cmd + " -d stop")
    os.system(cmd + " -d start")
    print(ssr['remarks']+"/"+ssr['server'])

    print (test_option)

    if test_option['ping']:
        ping_len="7" if isIP(result['host']) else "8"
        cmd="ping -c 5 %s |grep 'time=' | awk '{print $%s}' |cut -b 6-"% (result['host'],ping_len)
        ping_pc=os.popen(cmd).readlines()
        if(len(ping_pc)):
          ping_pc=int(float(ping_pc[0].strip()))
        print("ping_test,localPing:",ping_pc)
        result['ping_pc']=ping_pc

    socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", int(port))
    socket.socket = socks.socksocket
    if test_option['network']:
        ip=requests.get('http://api.ip.sb/ip',timeout=15).text.strip()
        result['ip']=ip
        print("network_test,ip:",result['ip'])

    if test_option['speed']:
        import speedtest
        s = speedtest.Speedtest()
        s.get_best_server()
        s.download()
        s.upload()
        s.results.share()
        results_dict = s.results.dict()
        result['ping']=results_dict['ping']
        result['download']=round(results_dict['download'] / 1000.0 / 1000.0,2)
        result['upload']=round(results_dict['upload'] / 1000.0 / 1000.0 ,2)
        result['state']="Success"
        result['ip']=results_dict['client']['ip']
        print("speed_test,ping:%s,download:%s,upload:%s" % (result['ping'],result['download'],result['upload']))

    if test_option['youtube']:
        socket.socket=default_socket
        youtube=youtube_speed.test_speed(port,youtube_timeout)
        youtube=int(re.sub("\D", "", youtube))
        result['youtube']=youtube
        print("youtube_test,speed:",youtube)
    result['state']="Success"
    os.system(cmd + " -d stop")
    return result

  except Exception as e:
    print (e)
    os.system(cmd + " -d stop")
    return result
Exemplo n.º 2
0
# # cmd = shlex(cmd)
# ss_s = subprocess.Popen(cmd, shell=True)
# # os.system(cmd)
# # p=os.popen(cmd)
# print("sadsd")
# print(a.pid)
# time.sleep(5)

# # os.killpg(os.getpgid(a.pid+1), 9)
# os.kill(a.pid, signal.SIGKILL)
# os.kill(a.pid+1	, signal.SIGKILL)
# # a.send_signal(signal.CTRL_C_EVENT)
# # a.terminate()
# print("close")

# while True:
# 	print("asdsad")
# socket.socket = socks.socksocket
# socket.socket='socket.socket'
# print(default_socket)
# print(type('socket.socket'))
# # import socket
# import socks
# default_socket = socket.socket
# socket.socket=default_socket

# socket.shut_rdwr()
# help(socket)

youtube_speed.test_speed("1080")
Exemplo n.º 3
0
def connect_ssr(ssr):
    try:
        port = "6667"
        if ('protoparam' in ssr):
            cmd = "python shadowsocksr/shadowsocks/local.py -qq -s %s -p %s -k %s -m %s -O %s -G %s -o %s -g %s -b %s -l %s" % (
                ssr['server'], ssr['port'], ssr['password'], ssr['method'],
                ssr['protocol'], ssr['protoparam'], ssr['obfs'],
                ssr['obfsparam'], "127.0.0.1", port)
        else:
            cmd = "python shadowsocksr/shadowsocks/local.py -qq -s %s -p %s -k %s -m %s -O %s -o %s -g %s -b %s -l %s" % (
                ssr['server'], ssr['port'], ssr['password'], ssr['method'],
                ssr['protocol'], ssr['obfs'], ssr['obfsparam'], "127.0.0.1",
                port)
        os.system(cmd + " -d stop")
        os.system(cmd + " -d start")

        print("----------------------------")
        print(ssr['remarks'] + "/" + ssr['server'])

        socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", int(port))
        socket.socket = socks.socksocket
        ip = requests.get('http://api.ip.sb/ip', timeout=15).text

        result = {}
        result['host'] = ssr['server']
        result['remarks'] = ssr['remarks']
        result['ip'] = ip.strip()
        print(result['ip'])
        if isIP(result['host']):
            ping_len = "7"
        else:
            ping_len = "8"
        cmd = "ping -c 1 %s |grep 'time=' | awk '{print $%s}' |cut -b 6-" % (
            result['host'], ping_len)

        ping_pc = os.popen(cmd).readlines()  #.strip()
        if (len(ping_pc)):
            ping_pc = ping_pc[0].strip()
        print("ping_pc", ping_pc)
        result['ping_pc'] = ping_pc
        if not only_check_network:
            import speedtest
            s = speedtest.Speedtest()
            s.get_best_server()
            s.download()
            s.upload()
            s.results.share()
            results_dict = s.results.dict()
            result['ping'] = results_dict['ping']
            result['download'] = round(
                results_dict['download'] / 1000.0 / 1000.0, 2)
            result['upload'] = round(results_dict['upload'] / 1000.0 / 1000.0,
                                     2)
            result['state'] = "Success"
            print(result['ping'], "/", result['ping_pc'])
            print(result['download'], "Mbit/s")
            print(result['upload'], "Mbit/s")
            socket.socket = default_socket
            youtube = youtube_speed.test_speed(port)
            youtube = int(re.sub("\D", "", youtube))
            result['youtube'] = youtube
        else:
            socket.socket = default_socket
            result['youtube'] = youtube_speed.test_speed(port)
            result['download'] = 0
            result['upload'] = 0
            result['ping'] = 0
            result['youtube'] = 0
            result['state'] = "Success"
        return result

    except Exception as e:
        result = {}
        result['host'] = ssr['server']
        result['remarks'] = ssr['remarks']
        result['ip'] = ''
        result['download'] = 0
        result['upload'] = 0
        result['ping'] = 0
        result['youtube'] = 0
        result['state'] = "Fail"
        cmd = "ping -c 1 %s |grep 'time=' | awk '{print $8}' |cut -b 6-" % result[
            'host']
        ping_pc = os.popen(cmd).readlines()  #.strip()
        if (len(ping_pc)):
            ping_pc = ping_pc[0].strip()
        result['ping_pc'] = ping_pc
        print("ping_local", ping_pc)
        print(e)
        return result