| 1234567891011121314151617181920212223242526272829303132333435 |
- import { Injectable } from '@nestjs/common';
- import crypto from 'crypto';
- import type { FastifyRequest } from 'fastify';
- import Qqwry from 'lib-qqwry';
- import { isArray } from 'lodash';
- @Injectable()
- export class UtilsService {
- private qqwry = new Qqwry(true);
- getRealIp(req: FastifyRequest) {
- let clientIp = req.headers['x-forwarded-for'] || req.ip;
- if (isArray(clientIp)) {
- clientIp = clientIp[0];
- }
- clientIp = clientIp.split(',')[0];
- return clientIp;
- }
- getIpRegion(ip: string) {
- if (ip == null) {
- return;
- }
- try {
- const result = this.qqwry.searchIP(ip);
- return result.Country;
- } catch (err) {
- return '未知地区';
- }
- }
- getMd5(str: string) {
- return crypto.createHash('md5').update(str).digest('hex');
- }
- }
|