소스 검색

feat(rcs-number): 添加 smstiger 第二个通道并优化号码获取逻辑

- 在 rcs-number.entity.ts 中添加 smstiger02 作为新的短信通道
- 在 rcs-number.service.ts 中注入新的 smstiger02 服务
- 重构 smstiger.service.ts,支持多个 API 密钥和并行获取号码
- 使用 Promise.any 实现同时请求多个号码,提高获取有效号码的效率- 优化错误处理,当所有请求都失败时抛出异常
wui 8 달 전
부모
커밋
d7d377c689
3개의 변경된 파일59개의 추가작업 그리고 23개의 파일을 삭제
  1. 3 1
      src/rcs-number/entities/rcs-number.entity.ts
  2. 50 21
      src/rcs-number/impl/smstiger.service.ts
  3. 6 1
      src/rcs-number/rcs-number.service.ts

+ 3 - 1
src/rcs-number/entities/rcs-number.entity.ts

@@ -46,7 +46,9 @@ export enum RcsNumberSource {
     sms23256 = 'sms23256',
     svenkvint = 'svenkvint',
     smsapi = 'smsapi',
-    smstiger = 'smstiger'
+    smstiger = 'smstiger',
+    smstiger02 = 'smstiger02'
+
 }
 
 @Entity()

+ 50 - 21
src/rcs-number/impl/smstiger.service.ts

@@ -5,42 +5,71 @@ import axios from 'axios'
 const axiosInstance = axios.create({
     baseURL: 'https://api.tiger-sms.com/stubs/'
 })
-const api_key = 't6AV7f5KgwRmWsK9M5xN6uTTtHiog8EQ'
+const api_key_sz = 'Aq7XKGX7qnCf6y8CouchYJ0VK60AxISm'
+const api_key_xs = 't6AV7f5KgwRmWsK9M5xN6uTTtHiog8EQ'
 const service = 'gms'
 const countryCode = '187'
 
 export class smstiger extends GetNumberService {
-    source: RcsNumberSource = RcsNumberSource.smstiger
+    source: RcsNumberSource
+
+    api_key: string
+
+    constructor(key: string, source: RcsNumberSource) {
+        super()
+        this.api_key = key
+        this.source = source
+    }
 
     async getNumber(country: string, num?: number): Promise<GetNumberResponse> {
         if (country.toLocaleLowerCase() !== 'us') throw new Error(`Platform ${this.source} only support US.`)
-        const res = await axiosInstance.get(`handler_api.php`, {
-            params: {
-                api_key,
-                action: 'getNumber',
-                service,
-                country: countryCode
-            }
-        })
-        const data = res.data
-        const split = data.split(':')
-        if (split[0] !== 'ACCESS_NUMBER') {
-            throw new Error(`Platform ${this.source} get number error: ${data}`)
+
+        const requestPhoneInfo = async () => {
+            const res = await axiosInstance.get(`handler_api.php`, {
+                params: {
+                    api_key: this.api_key,
+                    action: 'getNumber',
+                    service,
+                    country: countryCode
+                }
+            })
+            return res.data
         }
 
-        return {
-            number: split[2],
-            orderId: split[1],
-            operatorCode: '',
-            operatorName: '',
-            rawResponse: data
+        const requests = Array(10)
+            .fill(null)
+            .map(() => requestPhoneInfo())
+
+        try {
+            const result = await Promise.any(
+                requests.map(async (request) => {
+                    const data = await request
+                    const split = data.split(':')
+                    if (split[0] === 'ACCESS_NUMBER') {
+                        return data
+                    }
+                    throw new Error(data)
+                })
+            )
+
+            const split = result.split(':')
+
+            return {
+                number: split[2],
+                orderId: split[1],
+                operatorCode: '',
+                operatorName: '',
+                rawResponse: result
+            }
+        } catch (e) {
+            throw new Error(`Platform ${this.source} Failed to get a valid number: ${e.message}`)
         }
     }
 
     async retriveMessage(orderId: string, num?: number): Promise<string> {
         const res = await axiosInstance.get(`handler_api.php`, {
             params: {
-                api_key,
+                api_key: this.api_key,
                 action: 'getStatus',
                 id: orderId
             }

+ 6 - 1
src/rcs-number/rcs-number.service.ts

@@ -56,6 +56,8 @@ export class RcsNumberService {
     private sms4verify02: sms4verify
     private smspva: smspva
     private smspva02: smspva
+    private smstiger: smstiger
+    private smstiger02: smstiger
 
     private readonly redis: Redis | null
 
@@ -86,7 +88,6 @@ export class RcsNumberService {
         private sms23256: sms23256,
         private svenkvint: svenkvint,
         private smsapi: smsapi,
-        private smstiger: smstiger,
         private readonly redisService: RedisService
     ) {
         this.durian = new durian('unsnap3094', 'U3Jma1hkbUxXblEyL0ZYai9WWFVvdz09', RcsNumberSource.durian)
@@ -102,6 +103,8 @@ export class RcsNumberService {
         this.sms797902 = new sms7979('BbrlHwT4IWrfxR1tbSP6O8RkzW7Gdh', 45, RcsNumberSource.sms797902)
         this.smspva = new smspva('uNW56fGr0zstfs87Xn0e1l2gCYVnb1', RcsNumberSource.smspva)
         this.smspva02 = new smspva('rTTL8pZtKkQ60zjU82bvbMEP7G6XGU', RcsNumberSource.smspva02)
+        this.smstiger = new smstiger('Aq7XKGX7qnCf6y8CouchYJ0VK60AxISm', RcsNumberSource.smstiger)
+        this.smstiger02 = new smstiger('t6AV7f5KgwRmWsK9M5xN6uTTtHiog8EQ', RcsNumberSource.smstiger02)
         this.redis = this.redisService.getOrThrow()
     }
 
@@ -369,6 +372,8 @@ export class RcsNumberService {
                 return this.smsapi
             case RcsNumberSource.smstiger:
                 return this.smstiger
+            case RcsNumberSource.smstiger02:
+                return this.smstiger02
             default:
                 throw new ServiceUnavailableException('不支持的短信通道')
         }