105 lines
3.5 KiB
Python
105 lines
3.5 KiB
Python
import subprocess
|
||
import logging
|
||
import time
|
||
def run_adb_command(command_list):
|
||
process = subprocess.Popen(command_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||
stdout, stderr = process.communicate()
|
||
output = stdout.decode().strip()
|
||
status_code = process.returncode
|
||
error = stderr.decode().strip()
|
||
return output, status_code, error
|
||
|
||
def get_now_time(device):
|
||
# adb shell date +%s
|
||
output, status_code, error = run_adb_command(['adb', '-s', device, 'shell', 'date',"'+%Y-%m-%d %H:%M:%S'"])
|
||
if status_code == 0:
|
||
print(f"get_now_time output: {output}")
|
||
logging.info(f"get now time output: {output}")
|
||
now_time = output
|
||
return now_time
|
||
else:
|
||
print(error)
|
||
logging.error(f"get now time error: {error}")
|
||
return None
|
||
|
||
def get_record(device):
|
||
logging.debug(device)
|
||
# adb -s <设备序列号> pull <远程路径> <本地路径>
|
||
output, status_code, error = run_adb_command(['adb', '-s', device, 'pull', '/sdcard/rizhi/record.txt','./cache'])
|
||
if status_code == 0:
|
||
print(output)
|
||
logging.info(f"get record output: {output}")
|
||
return True
|
||
else:
|
||
print(error)
|
||
logging.error(f"do get_record failed,error:{error}")
|
||
return False
|
||
|
||
|
||
def stopAutojs(device):
|
||
print(f"{device},强行停止 Auto.js")
|
||
output, status_code, error = run_adb_command(['adb', '-s', device, 'shell', 'am', 'force-stop', 'org.autojs.autojs'])
|
||
if status_code == 0:
|
||
logging.info(f"stop Autojs status_code:{status_code}")
|
||
return True
|
||
else:
|
||
print(error)
|
||
logging.error(f"stop Auto.js error: {error}")
|
||
return False
|
||
|
||
def clearCache(device):
|
||
print(f"{device},清理缓存")
|
||
output, status_code, error = run_adb_command(['adb', '-s', device, 'shell', 'pm', 'clear', 'sperixlabs.proxyme'])
|
||
if status_code == 0:
|
||
logging.info(f"clear cache status_code: {status_code}")
|
||
return True
|
||
else:
|
||
print(error)
|
||
logging.error(f"clear cache error: {error}")
|
||
return False
|
||
|
||
|
||
def openProxyMe(device):
|
||
print(f"{device},打开 ProxyMe")
|
||
output, status_code, error = run_adb_command(['adb', '-s', device, 'shell', 'am', 'start', '-n', 'sperixlabs.proxyme/tun.proxy.MainActivity'])
|
||
if status_code == 0:
|
||
logging.info(f"open ProxyMe status_code: {status_code}")
|
||
return True
|
||
else:
|
||
print(error)
|
||
logging.error(f"open ProxyMe error: {error}")
|
||
return False
|
||
|
||
def click_allow(device):
|
||
output, status_code, error = run_adb_command(['adb', '-s', device, 'shell', 'input', 'tap', '540', '1170'])
|
||
if status_code == 0:
|
||
logging.info(f"click allow status_code: {status_code}")
|
||
return True
|
||
else:
|
||
print(error)
|
||
logging.error(f"click allow error: {error}")
|
||
return False
|
||
|
||
def restart_ProxyMe(device):
|
||
if stopAutojs(device):
|
||
if clearCache(device):
|
||
if openProxyMe(device):
|
||
time.sleep(3)
|
||
logging.info("restart_ProxyMe: wait 3 s")
|
||
if click_allow(device):
|
||
print(f"restart ProxyMe Success")
|
||
return True
|
||
else:
|
||
print(f"click allow failed")
|
||
return False
|
||
else:
|
||
print(f"open ProxyMe failed")
|
||
return False
|
||
else:
|
||
print(f"clear cache failed")
|
||
return False
|
||
else:
|
||
print(f"stop autojs failed")
|
||
return False
|
||
|