-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutosubscribe.py
More file actions
90 lines (65 loc) · 2.3 KB
/
Copy pathAutosubscribe.py
File metadata and controls
90 lines (65 loc) · 2.3 KB
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
import os
import paramiko
import socket
import time
from qiniu import Auth, put_file, etag, CdnManager
import qiniu.config
hostname = "43.134.208.84"
username = "root"
password = ""
port = 2222
access_key = ''
secret_key = ''
def portstatus(hostname, port:int):
sockobj = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sockobj.settimeout(3)
if sockobj.connect_ex((hostname, port)) != 0:
return 1
return 0
def runcommand(hostname, username, password, port, cmd):
with paramiko.SSHClient() as session:
session.set_missing_host_key_policy(paramiko.AutoAddPolicy())
session.connect(hostname=hostname, username=username, password=password, port=port)
stdin, stdout, stderr = session.exec_command(cmd)
print(stdout.readline())
return stdin, stdout, stderr
def uploadqiniu():
# 构建鉴权对象
q = Auth(access_key, secret_key)
# 要上传的空间
bucket_name = 'service-pubic'
# 上传后保存的文件名
key = 'dao-vmess-node.yaml'
token = q.upload_token(bucket_name, key, 3600)
# 要上传文件的本地路径
localfile = '/opt/config/dao-vmess-node.yaml'
ret, info = put_file(token, key, localfile, version='v2')
print(info)
assert ret['key'] == key
assert ret['hash'] == etag(localfile)
def flushcdn():
auth = qiniu.Auth(access_key=access_key, secret_key=secret_key)
cdn_manager = CdnManager(auth)
# 需要刷新的文件链接
urls = [
'http://r0v6686yf.hb-bkt.clouddn.com/dao-vmess-node.yaml'
]
# 刷新链接
return cdn_manager.refresh_urls(urls)
def servicemonitor():
oldport = 53
while True:
if portstatus(hostname, oldport) != 0:
newport = oldport + 1
# v2ray主机配置文件修改
runcommand(hostname, username, password, port, F'''sed -e 's?port": {oldport}?port": {newport}?g' /etc/v2ray/config.json; v2ray.sh restart''')
time.sleep(6)
# 订阅文件端口修改
status = os.system(F'''sed -e "s?port: {oldport}?port: {newport}?g" /opt/config/dao-vmess-node-test.yaml''')
# 上传对象存储&刷新CDN
uploadqiniu()
flushcdn()
oldport = newport
time.sleep(30)
if __name__ == '__main__':
servicemonitor()