修复cron分隔符,修复登录获取ip逻辑

This commit is contained in:
hanhh 2021-08-25 23:54:42 +08:00
parent 79e5b2fb84
commit 9b73392c5e
5 changed files with 30 additions and 28 deletions

View File

@ -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);

View File

@ -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<any>([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 };
}
}

View File

@ -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 &&

View File

@ -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<any> {
public async login(
payloads: {
username: string;
password: string;
},
req: any,
): Promise<any> {
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, {

View File

@ -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;