Przeglądaj źródła

feat: ip 白名单

zhangyuting 5 lat temu
rodzic
commit
762aff880e

+ 7 - 0
config/default.js

@@ -12,6 +12,13 @@ module.exports = {
     cache: {
       prefix: 'MINI_GAME_AUTO_AD_SERVER'
     },
+    ip_white_list: [
+      '49.64.179.155',
+      '218.4.127.242',
+      '::1',
+      'localhost',
+      '127.0.0.1'
+    ],
     // 灯塔系统-一键登陆小游戏 https://mp.weixin.qq.com/
     one_key_load: {
       token: 'd8d7a9139328f5f00f9b04302414a7e3',

+ 27 - 0
src/app/api/miniAd.ts

@@ -66,6 +66,33 @@ export class MiniAd {
     return ret
   }
 
+  async updateCampaignInfo (ctx: ExtendableContext): Promise<Response<ICampaignInfoResponse>> {
+    const params = ctx.request.body
+    const ret: Response<ICampaignInfoResponse> = {
+      code: 404,
+      msg: '没有数据',
+      data: null
+    }
+    try {
+      if (!params.args) {
+        throw new Error('not found params')
+      }
+      const form = new FormData()
+      form.append('args', JSON.stringify(params.args))
+      const data = await this.service.updateCampaignInfo(form)
+      if (data) {
+        ret.code = 200
+        ret.msg = 'ok'
+        ret.data = data
+      }
+    } catch (e) {
+      console.log(e)
+      ret.code = 500
+      ret.msg = '操作失败'
+    }
+    return ret
+  }
+
   async snsImage (ctx: ExtendableContext): Promise<Response<ICampaignInfoResponse>> {
     const params = ctx.request.body
     const ret: Response<ICampaignInfoResponse> = {

+ 0 - 0
src/app/db/redis.ts → src/app/database/redis.ts


+ 25 - 0
src/app/middleware/ip-filter.ts

@@ -0,0 +1,25 @@
+import { ExtendableContext, Next } from 'koa'
+import redis from '@/app/database/redis'
+import config from 'config'
+// 业务需求,登陆之后根据id判断 超级管理员和开发可以登陆,其他不可以
+// ip 白名单中间件
+const redisKey = config.get<string>('app.cache.prefix')
+const ipWhitesKey = `${redisKey}:IP_WHITE_LIST:ALLOW_IP`
+
+const defaultIpWhiteList = config.get<string[]>('app.ip_white_list')
+
+export class IpFilter {
+  static async handle (ctx: ExtendableContext, next: Next): Promise<unknown> {
+    const ip = ctx.header.ip
+    if (Array.isArray(ip)) {
+      ctx.body = { code: 403, msg: 'ip 地址有误' }
+      return
+    }
+    await redis.sadd(ipWhitesKey, ip)
+    const allowedIp = await redis.smembers(ipWhitesKey) || defaultIpWhiteList
+    if (ip && allowedIp.includes(ip)) {
+      return next()
+    }
+    ctx.body = { code: 403, msg: `未授权的ip:${ctx.header.ip},请联系管理员` }
+  }
+}

+ 2 - 0
src/app/provider/index.ts

@@ -1,11 +1,13 @@
 // 对外提供格式化好的中间件
 import { Request } from '@/app/middleware/request'
 import { Cookie } from '@/app/middleware/cookie'
+import { IpFilter } from '../middleware/ip-filter'
 
 export class Index {
   static handle ():any[] {
     return [
       Request.handle,
+      IpFilter.handle,
       Cookie.handle
     ]
   }

+ 0 - 3
src/app/service/miniAdBaseService.ts

@@ -20,9 +20,6 @@ export class MiniAdBaseService {
             options.headers['user-agent'] = 'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36'
             const { searchParams } = options.url
             searchParams.set('token', token)
-            console.log(options.request)
-            console.log(searchParams)
-            console.log(options.body)
           }
         ],
         afterResponse: [

+ 8 - 1
src/app/service/miniAdCampaignService.ts

@@ -7,7 +7,6 @@ import {
 } from '@/app/types/miniAdResponse'
 import { MiniAdBaseService } from './miniAdBaseService'
 import FormData from 'form-data'
-import { Response } from 'got'
 
 export class MiniAdCampaignService extends MiniAdBaseService {
   // 获取小程序信息
@@ -34,6 +33,14 @@ export class MiniAdCampaignService extends MiniAdBaseService {
     })
   }
 
+  // 各更新计划
+  async updateCampaignInfo (form:FormData):Promise<ICampaignInfoResponse> {
+    return this.api.post<ICampaignInfoResponse>('promotion/v3/update_campaign_info', {
+      body: form,
+      resolveBodyOnly: true
+    })
+  }
+
   // 上传创意图片
   async snsImage (searchParams:any, form:FormData):Promise<ISnsImageResponse> {
     return this.api.post<ISnsImageResponse>('promotion/landingpage/snsimage', {

+ 1 - 1
src/app/service/oneLoadKeyService.ts

@@ -3,7 +3,7 @@ import moment from 'moment'
 import { Encrypt } from '@/utils/encrypt'
 import config from 'config'
 import got from 'got'
-import redis from '@/app/db/redis'
+import redis from '@/app/database/redis'
 import { IOneLoadKeyResponse } from '../types/oneLoadKeyResponse'
 const token = config.get<string>('app.one_key_load.token')
 const client = config.get<string>('app.one_key_load.client')

+ 5 - 0
src/router/index.ts

@@ -30,6 +30,11 @@ router.post('/create_campaign_info', ...middleware, async (ctx) => {
   ctx.body = await controller.createCampaignInfo(ctx)
 })
 
+router.post('/update_campaign_info', ...middleware, async (ctx) => {
+  const controller = new MiniAd(ctx)
+  ctx.body = await controller.updateCampaignInfo(ctx)
+})
+
 router.post('/snsimage', ...middleware, async (ctx) => {
   const controller = new MiniAd(ctx)
   ctx.body = await controller.snsImage(ctx)