diff --git a/back/api/auth.ts b/back/api/auth.ts index 01f1cd99..683bf3b8 100644 --- a/back/api/auth.ts +++ b/back/api/auth.ts @@ -3,7 +3,6 @@ import { Container } from 'typedi'; import { Logger } from 'winston'; import * as fs from 'fs'; import config from '../config'; -import { getNetIp } from '../config/util'; import AuthService from '../services/auth'; import { celebrate, Joi } from 'celebrate'; const route = Router(); @@ -22,8 +21,7 @@ export default (app: Router) => { const logger: Logger = Container.get('logger'); try { const authService = Container.get(AuthService); - const ipInfo = await getNetIp(req); - const data = await authService.login({ ...req.body, ...ipInfo }); + const data = await authService.login({ ...req.body }, req); return res.send(data); } catch (e) { logger.error('🔥 error: %o', e); diff --git a/back/config/util.ts b/back/config/util.ts index 13253d0e..ebb41434 100644 --- a/back/config/util.ts +++ b/back/config/util.ts @@ -132,7 +132,6 @@ export async function getNetIp(req: any) { ]), ]; let ip = ipArray[0]; - console.log(`访问的ip ${ipArray.toString()}`); if (ipArray.length > 1) { for (let i = 0; i < ipArray.length; i++) { @@ -156,22 +155,22 @@ export async function getNetIp(req: any) { if (ip.includes('127.0') || ip.includes('192.168') || ip.includes('10.7')) { ip = ''; } - try { - const { data } = await got - .get( - `https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=${ip}&co=&resource_id=6006&t=1555898284898&ie=utf8&oe=utf8&format=json&tn=baidu`, - ) - .json(); - return { address: data[0].location, ip }; - } catch (error) { - try { - const { country, regionName, city } = await got - .get(`http://ip-api.com/json/${ip}?lang=zh-CN`) - .json(); + const baiduApi = got.get( + `https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=${ip}&co=&resource_id=6006&t=1555898284898&ie=utf8&oe=utf8&format=json&tn=baidu`, + ); + const ipApi = got.get(`http://ip-api.com/json/${ip}?lang=zh-CN`); + const [{ data }, { country, regionName, city }] = await ( + await Promise.all([baiduApi, ipApi]) + ).map((x) => JSON.parse(x.body)); + if (data[0] && data[0].location) { + return { address: data[0].location, ip }; + } else if (country && regionName) { return { address: `${country} ${regionName} ${city}`, ip }; - } catch (err) { + } else { return { address: `获取失败`, ip }; } + } catch (error) { + return { address: `获取失败`, ip }; } } diff --git a/back/schedule.ts b/back/schedule.ts index 7a8047ae..08c71615 100644 --- a/back/schedule.ts +++ b/back/schedule.ts @@ -25,7 +25,7 @@ const run = async () => { if (docs && docs.length > 0) { for (let i = 0; i < docs.length; i++) { const task = docs[i]; - const _schedule = task.schedule && task.schedule.split(' '); + const _schedule = task.schedule && task.schedule.split(/ +/); if ( _schedule && _schedule.length > 5 && diff --git a/back/services/auth.ts b/back/services/auth.ts index 375efeeb..ea5ac154 100644 --- a/back/services/auth.ts +++ b/back/services/auth.ts @@ -1,6 +1,6 @@ import { Service, Inject } from 'typedi'; import winston from 'winston'; -import { createRandomString, getFileContentByName } from '../config/util'; +import { createRandomString, getNetIp } from '../config/util'; import config from '../config'; import * as fs from 'fs'; import _ from 'lodash'; @@ -10,17 +10,18 @@ import jwt from 'jsonwebtoken'; export default class AuthService { constructor(@Inject('logger') private logger: winston.Logger) {} - public async login(payloads: { - username: string; - password: string; - ip: string; - address: string; - }): Promise { + public async login( + payloads: { + username: string; + password: string; + }, + req: any, + ): Promise { if (!fs.existsSync(config.authConfigFile)) { return this.initAuthInfo(); } - let { username, password, ip, address } = payloads; + let { username, password } = payloads; const content = fs.readFileSync(config.authConfigFile, 'utf8'); const timestamp = Date.now(); if (content) { @@ -32,6 +33,7 @@ export default class AuthService { lastip, lastaddr, } = JSON.parse(content); + if ( (cUsername === 'admin' && cPassword === 'adminadmin') || !cUsername || @@ -39,6 +41,7 @@ export default class AuthService { ) { return this.initAuthInfo(); } + if (retries > 2 && Date.now() - lastlogon < Math.pow(3, retries) * 1000) { return { code: 410, @@ -50,6 +53,8 @@ export default class AuthService { ), }; } + + const { ip, address } = await getNetIp(req); if (username === cUsername && password === cPassword) { const data = createRandomString(50, 100); let token = jwt.sign({ data }, config.secret as any, { diff --git a/back/services/cron.ts b/back/services/cron.ts index d421ae69..0a2a966f 100644 --- a/back/services/cron.ts +++ b/back/services/cron.ts @@ -305,7 +305,7 @@ export default class CronService { if (doc.log_path) { return getFileContentByName(`${doc.log_path}`); } - const [, commandStr, url] = doc.command.split(' '); + const [, commandStr, url] = doc.command.split(/ +/); let logPath = this.getKey(commandStr); const isQlCommand = doc.command.startsWith('ql '); const key = @@ -345,7 +345,7 @@ export default class CronService { const tabs = await this.crontabs(); var crontab_string = ''; tabs.forEach((tab) => { - const _schedule = tab.schedule && tab.schedule.split(' '); + const _schedule = tab.schedule && tab.schedule.split(/ +/); if (tab.isDisabled === 1 || _schedule.length !== 5) { crontab_string += '# '; crontab_string += tab.schedule;