forked from sibosend/openvpn-admin-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.py
171 lines (149 loc) · 6.26 KB
/
scheduler.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
#!haoliVPNEnv/bin/python
# -*- coding: utf-8 -*-
from application import app,db,mail,create_app
from firstApp.models import User
from firstApp import utils
from datetime import datetime
from sqlalchemy import and_
from sqlalchemy import or_
import thread
import time
import os
import subprocess
from daemon.runner import DaemonRunner
import logging
import logging.handlers
from flask_mail import Message
logger = None
class Scheduler(object):
def __init__(self):
self.stdin_path = "/dev/null"
self.stdout_path = os.path.join(app.config['BASE_DIR'], "scheduler.stdout")
self.stderr_path = os.path.join(app.config['BASE_DIR'], "scheduler.stderr")
self.pidfile_path = app.config['SCHEDULER_PID']
self.pidfile_timeout = 5
# 同步
def synchronize(self):
with app.app_context():
openvpn_users = utils.get_users() # 获取index.txt中所有用户
if openvpn_users:
clients = []
for item in openvpn_users: # 循环对比
client = item['client']
clients.append(client)
user = User.query.filter_by(client=client).first()
if user:
if user.status == item['status']:
continue
else:
user.status = item['status']
if item['status'] == 1:
user.addtime = datetime.utcnow()
elif item['status'] == 4:
user.deltime = datetime.utcnow()
db.session.commit()
else:
user = User(item['name'], item['email'], item['depart'], client, 'cmd', item['status'])
db.session.add(user)
db.session.commit()
if len(clients) > 0:
delete_users = list(User.query.filter(User.client.notin_(clients)).all())
else:
delete_users = list(User.query.all())
for user_item in delete_users:
if user_item.status != 4:
db.session.delete(user_item)
utils.zip_delete(user_item.client)
db.session.commit()
else:
logger.error('index.txt not exist')
print 'index.txt not exist'
if os.path.exists(app.config['CLIENT_VPN_HAOLI_DIR'] + '/crl.pem'):
stdout = subprocess.check_output(app.config['CMD_REVOKE_FILE_COPY'], shell=True)
if stdout:
logger.info('index.txt not exist')
print stdout
def operate_user(self):
with app.app_context():
users = list(User.query.filter(or_(User.status.in_((0, 3)), and_(User.status == 1, User.send_status == 1))).all())
for user in users:
if user.status == 0:
status, log = utils.buildKey([user.name, user.client, user.email, user.depart])
lastlog = log[-1]
if status:
user.status = 1
user.send_status = 0
user.addtime = datetime.utcnow()
#db.session.commit()
thread.start_new_thread(self.send_to_email,(user.client, user.email))
else:
user.status = 2
user.addtime = datetime.utcnow()
#db.session.commit()
logger.info(lastlog)
print lastlog
elif user.status == 3:
status, log = utils.revoke(user.client)
lastlog = log[-1]
if status:
user.status = 4
user.deltime = datetime.utcnow()
#db.session.commit()
utils.zip_delete(user.client)
else:
user.status = 1
#db.session.commit()
logger.info(lastlog)
print lastlog
elif user.status == 1 and user.send_status == 1:
user.send_status = 0
#db.session.commit()
thread.start_new_thread(self.send_to_email,(user.client, user.email))
else:
continue
db.session.commit()
def send_to_email(self, client, email):
logger.info("send to email:" + email)
with app.app_context():
status, output, password = utils.zip_compress(client)
if status:
msg = Message(u'好利58 VPN连接', recipients = [email])
msg.body = app.config['MAIL_SUBJECT'] % password
with app.open_resource(output) as fp:
msg.attach(client + '.zip', "application/zip", fp.read())
mail.send(msg)
else:
logger.info(output)
print output
# search users
def schedule_run(self, threadName, delay):
while True:
global app
app = create_app()
self.operate_user()
self.synchronize()
time.sleep(delay)
def run(self):
global logger
logger = logging.getLogger('Scheduler')
logger.setLevel(logging.DEBUG)
fh = logging.handlers.RotatingFileHandler(
os.path.join(app.config['BASE_DIR'], "scheduler.log"), maxBytes=10000000,backupCount=5)
fh.setLevel(logging.DEBUG)
formatter = logging.Formatter(u'%(asctime)s [%(levelname)s] %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.info("start scheduler:")
try:
with app.app_context():
thread.start_new_thread(self.schedule_run, ('haoliVPN', app.config['BACK_THREAD_SLEEP_TIME']))
except:
logger.error("Error: unable to start thread")
print "Error: unable to start thread"
while 1:
pass
if __name__ == '__main__':
run = DaemonRunner(Scheduler())
run.do_action()
# scheduler = Scheduler()
# scheduler.run()