Exemplo n.º 1
0
    def post(self,userid):
        try:
            envs = {}
            for k, _  in self.request.body_arguments.items():
                envs[k] = self.get_body_argument(k)
            req = pusher()
            log = ''
            now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            tmp = req.cus_pusher_send(envs ,u'推送测试', now)
            if ('True' == tmp):
                if (envs['btn'] == 'regbtn'):
                    self.db.user.mod(userid, diypusher=json.dumps(envs))
            else:
                raise Exception(tmp)
            
            log = u'运行成功,请检查是否收到推送'
        except Exception as e:
            if (str(e).find('get user need id or email') > -1):
                e = u'请输入用户名/密码'
            traceback.print_exc()
            self.render('utils_run_result.html', log=str(e), title=u'设置失败', flg='danger')
            return

        self.render('utils_run_result.html', log=log, title=u'设置成功', flg='success')
        return
Exemplo n.º 2
0
    def do(self, task):
        task['note'] = self.db.task.get(task['id'], fields=('note'))['note']
        user = self.db.user.get(task['userid'],
                                fields=('id', 'email', 'email_verified',
                                        'nickname', 'logtime'))
        tpl = self.db.tpl.get(task['tplid'],
                              fields=('id', 'userid', 'sitename', 'siteurl',
                                      'tpl', 'interval', 'last_success'))
        ontime = self.db.task.get(task['id'],
                                  fields=('ontime', 'ontimeflg', 'pushsw',
                                          'newontime', 'next'))
        newontime = json.loads(ontime["newontime"])
        pushtool = pusher()
        caltool = cal()
        logtime = json.loads(user['logtime'])
        pushsw = json.loads(ontime['pushsw'])

        if 'ErrTolerateCnt' not in logtime: logtime['ErrTolerateCnt'] = 0

        if task['disabled']:
            self.db.tasklog.add(task['id'], False, msg='task disabled.')
            self.db.task.mod(task['id'], next=None, disabled=1)
            raise gen.Return(False)

        if not user:
            self.db.tasklog.add(task['id'],
                                False,
                                msg='no such user, disabled.')
            self.db.task.mod(task['id'], next=None, disabled=1)
            raise gen.Return(False)

        if not tpl:
            self.db.tasklog.add(task['id'],
                                False,
                                msg='tpl missing, task disabled.')
            self.db.task.mod(task['id'], next=None, disabled=1)
            raise gen.Return(False)

        if tpl['userid'] and tpl['userid'] != user['id']:
            self.db.tasklog.add(task['id'],
                                False,
                                msg='no permission error, task disabled.')
            self.db.task.mod(task['id'], next=None, disabled=1)
            raise gen.Return(False)

        start = time.time()
        try:
            fetch_tpl = self.db.user.decrypt(
                0 if not tpl['userid'] else task['userid'], tpl['tpl'])
            env = dict(
                variables=self.db.user.decrypt(task['userid'],
                                               task['init_env']),
                session=[],
            )

            url = utils.parse_url(env['variables'].get('_proxy'))
            if not url:
                new_env = yield self.fetcher.do_fetch(fetch_tpl, env)
            else:
                proxy = {
                    'host': url['host'],
                    'port': url['port'],
                }
                new_env = yield self.fetcher.do_fetch(fetch_tpl, env, [proxy])

            variables = self.db.user.encrypt(task['userid'],
                                             new_env['variables'])
            session = self.db.user.encrypt(
                task['userid'], new_env['session'].to_json() if hasattr(
                    new_env['session'], 'to_json') else new_env['session'])

            # todo next not mid night
            if (newontime['sw']):
                if ('mode' not in newontime):
                    newontime['mode'] = 'ontime'
                if (newontime['mode'] == 'ontime'):
                    newontime['date'] = (
                        datetime.datetime.now() +
                        datetime.timedelta(days=1)).strftime("%Y-%m-%d")
                next = caltool.calNextTs(newontime)['ts']
            else:
                next = time.time() + max(
                    (tpl['interval'] if tpl['interval'] else 24 * 60 * 60),
                    1 * 60)
                if tpl['interval'] is None:
                    next = self.fix_next_time(next)

            # success feedback
            self.db.tasklog.add(task['id'],
                                success=True,
                                msg=new_env['variables'].get('__log__'))
            self.db.task.mod(task['id'],
                             last_success=time.time(),
                             last_failed_count=0,
                             success_count=task['success_count'] + 1,
                             env=variables,
                             session=session,
                             mtime=time.time(),
                             next=next)
            self.db.tpl.incr_success(tpl['id'])

            t = datetime.datetime.now().strftime('%m-%d %H:%M:%S')
            title = u"签到任务 {0}-{1} 成功".format(tpl['sitename'], task['note'])
            logtemp = new_env['variables'].get('__log__')
            logtemp = u"{0}  日志:{1}".format(t, logtemp)
            pushtool.pusher(user['id'], pushsw, 0x2, title, logtemp)

            logger.info('taskid:%d tplid:%d successed! %.4fs', task['id'],
                        task['tplid'],
                        time.time() - start)
            # delete log
            self.ClearLog(task['id'])
        except Exception as e:
            # failed feedback
            next_time_delta = self.failed_count_to_time(
                task['last_failed_count'], tpl['interval'])

            t = datetime.datetime.now().strftime('%m-%d %H:%M:%S')
            title = u"签到任务 {0}-{1} 失败".format(tpl['sitename'], task['note'])
            content = u"日志:{log}".format(log=e)
            disabled = False
            if next_time_delta:
                next = time.time() + next_time_delta
                content = content + u"下次运行时间:{0}".format(
                    time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(next)))
                if (logtime['ErrTolerateCnt'] <= task['last_failed_count']):
                    pushtool.pusher(user['id'], pushsw, 0x1, title, content)
            else:
                disabled = True
                next = None
                content = u"任务已禁用"
                pushtool.pusher(user['id'], pushsw, 0x1, title, content)

            self.db.tasklog.add(task['id'], success=False, msg=unicode(e))
            self.db.task.mod(task['id'],
                             last_failed=time.time(),
                             failed_count=task['failed_count'] + 1,
                             last_failed_count=task['last_failed_count'] + 1,
                             disabled=disabled,
                             mtime=time.time(),
                             next=next)
            self.db.tpl.incr_failed(tpl['id'])

            logger.error('taskid:%d tplid:%d failed! %r %.4fs', task['id'],
                         task['tplid'], e,
                         time.time() - start)
            raise gen.Return(False)
        raise gen.Return(True)
Exemplo n.º 3
0
    def post(self, taskid):
        self.evil(+2)
        start_ts = int(time.time())
        user = self.current_user
        task = self.check_permission(
            self.db.task.get(taskid,
                             fields=('id', 'tplid', 'userid', 'init_env',
                                     'env', 'session', 'last_success',
                                     'last_failed', 'success_count', 'note',
                                     'failed_count', 'last_failed_count',
                                     'next', 'disabled', 'ontime', 'ontimeflg',
                                     'pushsw', 'newontime')), 'w')

        tpl = self.check_permission(
            self.db.tpl.get(task['tplid'],
                            fields=('id', 'userid', 'sitename', 'siteurl',
                                    'tpl', 'interval', 'last_success')))
        t = 0 if not tpl['userid'] else task['userid'], tpl['tpl']
        fetch_tpl = self.db.user.decrypt(
            0 if not tpl['userid'] else task['userid'], tpl['tpl'])
        env = dict(
            variables=self.db.user.decrypt(task['userid'], task['init_env']),
            session=[],
        )

        pushsw = json.loads(task['pushsw'])
        newontime = json.loads(task['newontime'])
        pushertool = pusher()
        caltool = cal()

        try:
            new_env = yield self.fetcher.do_fetch(fetch_tpl, env)
        except Exception as e:
            t = datetime.datetime.now().strftime('%m-%d %H:%M:%S')
            title = u"签到任务 {0}-{1} 失败".format(tpl['sitename'], task['note'])
            logtmp = u"{0} 日志:{1}".format(t, e)
            pushertool.pusher(user['id'], pushsw, 0x4, title, logtmp)

            self.db.tasklog.add(task['id'], success=False, msg=unicode(e))
            self.finish(
                '<h1 class="alert alert-danger text-center">签到失败</h1><div class="showbut well autowrap" id="errmsg">%s<button class="btn hljs-button" data-clipboard-target="#errmsg" >复制</button></div>'
                % e)
            return

        self.db.tasklog.add(task['id'],
                            success=True,
                            msg=new_env['variables'].get('__log__'))
        if (newontime["sw"]):
            if ('mode' not in newontime):
                newontime['mode'] = 'ontime'

            if (newontime['mode'] == 'ontime'):
                newontime['date'] = (
                    datetime.datetime.now() +
                    datetime.timedelta(days=1)).strftime("%Y-%m-%d")
            nextTime = caltool.calNextTs(newontime)['ts']
        else:
            nextTime = time.time() + (tpl['interval']
                                      if tpl['interval'] else 24 * 60 * 60)

        self.db.task.mod(task['id'],
                         disabled=False,
                         last_success=time.time(),
                         last_failed_count=0,
                         success_count=task['success_count'] + 1,
                         mtime=time.time(),
                         next=nextTime)

        t = datetime.datetime.now().strftime('%m-%d %H:%M:%S')
        title = u"签到任务 {0}-{1} 成功".format(tpl['sitename'], task['note'])
        logtmp = new_env['variables'].get('__log__')
        logtmp = u"{0}  日志:{1}".format(title, logtmp)
        pushertool.pusher(user['id'], pushsw, 0x8, title, logtmp)

        self.db.tpl.incr_success(tpl['id'])
        self.finish(
            '<h1 class="alert alert-success text-center">签到成功</h1><div class="showbut well autowrap" id="errmsg"><pre>%s</pre><button class="btn hljs-button" data-clipboard-target="#errmsg" >复制</button></div>'
            % logtmp)
        logDay = int(self.db.site.get(1, fields=('logDay'))['logDay'])
        for log in self.db.tasklog.list(taskid=taskid, fields=('id', 'ctime')):
            if (time.time() - log['ctime']) > (logDay * 24 * 60 * 60):
                self.db.tasklog.delete(log['id'])
        return
Exemplo n.º 4
0
    def post(self, userid):
        envs = {}
        for key in self.request.body_arguments:
            envs[key] = self.get_body_arguments(key)
        env = json.loads(envs['env'][0])
        token = env["wxpusher_token"]
        uid = env["wxpusher_uid"]
        skey = env["skey"]
        barkurl = env["barkurl"]
        qywx_token = env["qywx_token"]
        log = ""
        if  ("reg" == self.get_body_argument('func')):
            try:
                if  (token != "") and (uid != ""):
                    temp = token + ";" + uid
                    self.db.user.mod(userid, wxpusher = temp)
                    if (self.db.user.get(userid, fields=("wxpusher"))["wxpusher"] == temp):
                        log = u"注册 wxpusher 成功\r\n"
                    else:
                        log = u"注册 wxpusher 失败\r\n"
                else:
                    log = u"wxpusher 未填写完整\r\n"

                if (skey != ""):
                    self.db.user.mod(userid, skey = skey)
                    if (self.db.user.get(userid, fields=("skey"))["skey"] == skey):
                        log = log+u"注册 S酱 成功\r\n"
                    else:
                        log = log+u"注册 S酱 失败\r\n"
                else:
                    log = log+u"skey 未填写完整\r\n"
                    
                if (barkurl != ""):
                    if (barkurl[-1] != '/'): 
                        barkurl=barkurl+'/'
                    self.db.user.mod(userid, barkurl = barkurl)
                    if (self.db.user.get(userid, fields=("barkurl"))["barkurl"] == barkurl):
                        log = log+u"注册 Bark 成功\r\n"
                    else:
                        log = log+u"注册 Bark 失败\r\n"
                else:
                    log = log+u"Bark 未填写完整\r\n"

                if (qywx_token != ""):
                    self.db.user.mod(userid, qywx_token = qywx_token)
                    if (self.db.user.get(userid, fields=("qywx_token"))["qywx_token"] == qywx_token):
                        log = log+u"注册 企业微信 成功\r\n"
                    else:
                        log = log+u"注册 企业微信 失败\r\n"
                else:
                    log = log+u"企业微信 未填写完整\r\n"

            except Exception as e:
                traceback.print_exc()
                self.render('tpl_run_failed.html', log=str(e))
                return
            
            self.render('utils_run_result.html', log=log, title=u'设置成功', flg='success')
            return

        else:
            try:
                f = pusher()
                t = datetime.datetime.now().strftime('%y-%m-%d %H:%M:%S')

                if  (token != "") and (uid != ""):
                    f.send2wxpusher("{0};{1}".format(token, uid),u"{t} 发送测试".format(t=t))
                    log = u"wxpusher 已推送,请检查是否收到\r\n"
                else:
                    log = u"wxpusher 未填写完整\r\n"

                if (skey != ""):
                    f.send2s(skey, u"正在测试S酱", u"{t} 发送测试".format(t=t))
                    log = log+u"S酱 已推送,请检查是否收到\r\n"
                else:
                    log = log+u"skey 未填写完整\r\n"

                if  (barkurl != ""):
                    f.send2bark(barkurl, u"正在测试Bark", u"{t} 发送测试".format(t=t))
                    log = log+u"Bark 已推送,请检查是否收到\r\n"
                else:
                    log = log+u"Bark 未填写完整\r\n"
                
                if (qywx_token != ""):
                    
                    f.qywx_pusher_send(qywx_token, "正在测试企业微信", u"{t} 发送测试".format(t=t))
                    log = log+u"企业微信 已推送,请检查是否收到\r\n"
                else:
                    log = log+u"企业微信 未填写完整\r\n"

            except Exception as e:
                traceback.print_exc()
                self.render('tpl_run_failed.html', log=str(e))
                return

            self.render('utils_run_result.html', log=log, title=u'设置成功', flg='success')
            return