-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnwct_cpolar.py
173 lines (153 loc) · 5.08 KB
/
nwct_cpolar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# -*- coding: UTF-8 -*-
# Version: v1.4
# Created by lstcml on 2022/10/18
# 建议定时10分钟:*/10 * * * *
'''
cron: 0 0 23 * * *
new Env('cpolar内网穿透');
'''
'''
使用说明:
1、打开https://www.cpolar.com/注册登录后获取authtoken;
2、新增变量qlnwct_authtoken,值为你账户的authtoken,运行脚本
更新记录:
v1.4
1、兼容新版的青龙面板;
v1.3
1、移动仓库目录;
v1.2
1、新增CPU架构识别,自动下载对应cpolar程序;
2、默认开启自动更新,qlnwctupdate值为false则关闭自动更新;
v1.1
1、开放推送,仅支持PushPlus推送,每次触发启动穿透会推送一次地址;
'''
import os
import re
import sys
import json
import requests
from time import sleep
path = os.path.split(os.path.realpath(__file__))[0]
log_path = os.path.join(path, "nwct_cpolar_log")
log_name = os.path.join(log_path, "cpolar")
log_file = os.path.join(log_path, "cpolar.master.log")
app_path = os.path.join(path, "cpolar")
commond = "python3 " + os.path.join(path, "cpolar.py") + " &"
# 检查更新
def update():
print("当前运行的脚本版本:" + str(version))
try:
r1 = requests.get("https://jihulab.com/klcb2010/qlww/-/blob/main/nwct_cpolar.py").text
r2 = re.findall(re.compile("version = \d.\d"), r1)[0].split("=")[1].strip()
if float(r2) > version:
print("发现新版本:" + r2)
print("正在自动更新脚本...")
os.system("killall cpolar")
os.system("ql raw https://jihulab.com/klcb2010/qlww/-/blob/main/nwct_cpolar.py &")
except:
pass
# 判断CPU架构
def check_os():
r = os.popen('uname -m').read()
if 'aarch64' in r or 'arm' in r:
cpu = 'arm'
elif 'x86_64' in r or 'x64' in r:
cpu = 'amd64'
else:
print('穿透失败:不支持当前架构!')
return
print('获取CPU架构:' + r.replace('\n', ''))
download_cpolar(cpu)
# 下载主程序
def download_cpolar(cpu):
if not os.path.exists("cpolar.py"):
res = requests.get("https://jihulab.com/klcb2010/qlww/-/blob/main/cpolar.py")
with open("cpolar.py", "wb") as f:
f.write(res.content)
if not os.path.exists("cpolar"):
res = requests.get("https://static.cpolar.com/downloads/releases/3.2.88.2/cpolar-stable-linux-" + cpu + ".zip")
with open("cpolar.zip", "wb") as f:
f.write(res.content)
os.system("unzip cpolar.zip >/dev/null 2>&1&&rm -f cpolar.zip&&chmod +x cpolar&&" + app_path + " authtoken " + authtoken + ">/dev/null 2>&1")
start_nwct()
# 获取穿透url
def get_url():
try:
with open(log_file, encoding='utf-8') as f:
log_content = f.read()
reg = 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
for i in re.findall(reg, log_content):
if 'cpolar' in i:
print("获取穿透链接成功")
return i.replace('\\', '')
break
except:
return "https://jihulab.com/klcb2010/cpolar3.2"
# 进程守护
def process_daemon():
print("正在检测穿透状态")
global qlurl
qlurl = get_url()
try:
res = requests.get(qlurl + "/login").text
if "/images/g5.ico" in res or "/images/favicon.svg" in res:
return True
else:
return False
except:
return False
# 执行程序
def start_nwct():
if not process_daemon():
os.system("rm -rf " + log_path)
os.system("mkdir -p " + log_path)
os.system("killall cpolar >/dev/null 2>&1")
print("正在启动内网穿透")
os.system(commond)
sleep(10)
if process_daemon():
if load_send():
print("启动内网穿透成功\n青龙面板:%s" % qlurl)
send("内网穿透通知", "青龙面板访问地址:" + qlurl)
else:
print("启动内网穿透失败")
else:
print("穿透程序已在运行\n青龙面板:%s" % qlurl)
# 推送
def load_send():
global send
cur_path = os.path.abspath(os.path.dirname(__file__))
sys.path.append(cur_path)
sendNotifPath = cur_path + "/sendNotify.py"
if not os.path.exists(sendNotifPath):
res = requests.get("https://jihulab.com/klcb2010/cpolar3.2/-/blob/main/sendNotify.py")
with open(sendNotifPath, "wb") as f:
f.write(res.content)
try:
from sendNotify import send
return True
except:
print("加载通知服务失败")
return False
if __name__ == '__main__':
version = 1.4
try:
authtoken = os.environ['qlnwct_authtoken']
except:
authtoken = ""
try:
token = os.environ['PUSH_PLUS_TOKEN']
except:
token = ""
try:
check_update = os.environ['qlnwctupdate']
except:
check_update = "true"
if check_update != "false":
update()
else:
print("变量qlnwctupdate未设置,脚本自动更新未开启")
if len(authtoken ) < 1:
print("请新增变量qlnwct_authtoken")
else:
check_os()