index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C) 2020 Tencent Cloud.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. 'use strict';
  17. const crypto = require('crypto');
  18. const { encId, encKey } = require('./config.js');
  19. /**
  20. * 核查验证码票据结果
  21. * @param {*} params
  22. * @param {integer} params.domainName - 需要解析的域名
  23. * @param {string?} params.ip - 用户IP 默认取用户客户端ip
  24. * @param {number?} params.ttl - 是否递归服务器缓存时间 1:返回
  25. * @param {number?} params.clientip - 是否返回用户公网出口IP 1:返回
  26. * @param {boolean} param.isEnc - 是否加密 true(调用企业版API): 加密 false:不加密(调用免费版API)
  27. */
  28. async function describeDnsResult({ domainName, ip, ttl, clientip, isEnc }) {
  29. // 配置校验
  30. if (isEnc && (!encId || !encKey)) {
  31. throw new Error('请在云函数HTTPDNS模块中配置encId和encKey');
  32. }
  33. // 如果不传入ip,就取客户端ip
  34. if (!ip) {
  35. const auth = uniCloud.auth();
  36. ip = auth.getClientIP();
  37. }
  38. // 企业版API调用需加密
  39. if (isEnc) {
  40. domainName = encrypt(domainName);
  41. ip = encrypt(ip);
  42. }
  43. const params = {
  44. dn: domainName,
  45. ip,
  46. ttl,
  47. clientip,
  48. id: isEnc ? encId : undefined
  49. };
  50. const { status, statusText, data } = await uniCloud.httpclient.request('http://119.29.29.29/d', {
  51. method: 'GET',
  52. dataType: 'text',
  53. data: params
  54. });
  55. if (status !== 200) {
  56. throw new Error(`接口调用失败[${status} - ${statusText}]`);
  57. }
  58. if (!data) {
  59. throw new Error('域名解析失败');
  60. }
  61. if (isEnc) {
  62. return decrypt(data);
  63. }
  64. return data;
  65. }
  66. /**
  67. * 加密
  68. * @param {string} encString - 需要加密的字符串
  69. * @return {string} 加密数据
  70. */
  71. function encrypt(encString) {
  72. try {
  73. if (!encString) {
  74. throw new Error('请传入待加密数据');
  75. }
  76. const iv = Buffer.alloc(0);
  77. const cipher = crypto.createCipheriv('des-ecb', encKey, iv);
  78. let encrypText = cipher.update(encString, 'utf8', 'hex');
  79. encrypText += cipher.final('hex');
  80. return encrypText;
  81. } catch (e) {
  82. throw new Error('加密失败');
  83. }
  84. }
  85. /**
  86. * 解密
  87. * @param {string} decString - 需要解密的字符串
  88. * @return {string} 解密数据
  89. */
  90. function decrypt(decString) {
  91. try {
  92. if (!decString) {
  93. throw new Error('请传入待解密数据');
  94. }
  95. const iv = Buffer.alloc(0);
  96. const cipher = crypto.createDecipheriv('des-ecb', encKey, iv);
  97. let decryptText = cipher.update(decString, 'hex', 'utf8');
  98. decryptText += cipher.final('utf8');
  99. return decryptText;
  100. } catch (e) {
  101. throw new Error('解密失败');
  102. }
  103. }
  104. module.exports = {
  105. describeDnsResult
  106. };