@neteasecloudmusicapienhanced/api 4.30.1 → 4.30.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/util/index.js CHANGED
@@ -1,36 +1,8 @@
1
1
  const logger = require('./logger')
2
- // 预先定义常量和函数引用
3
- // 中国 IP 段(来源:data/ChineseIPGenerate.csv)
4
- const chinaIPRangesRaw = [
5
- // 开始IP, 结束IP, IP个数, 位置
6
- ['1.0.1.0', '1.0.3.255', 768, '福州'],
7
- ['1.0.8.0', '1.0.15.255', 2048, '广州'],
8
- ['1.0.32.0', '1.0.63.255', 8192, '广州'],
9
- ['1.1.0.0', '1.1.0.255', 256, '福州'],
10
- ['1.1.2.0', '1.1.63.255', 15872, '广州'],
11
- ['1.2.0.0', '1.2.2.255', 768, '北京'],
12
- ['1.2.4.0', '1.2.127.255', 31744, '广州'],
13
- ['1.3.0.0', '1.3.255.255', 65536, '广州'],
14
- ['1.4.1.0', '1.4.127.255', 32512, '广州'],
15
- ['1.8.0.0', '1.8.255.255', 65536, '北京'],
16
- ['1.10.0.0', '1.10.9.255', 2560, '福州'],
17
- ['1.10.11.0', '1.10.127.255', 29952, '广州'],
18
- ['1.12.0.0', '1.15.255.255', 262144, '上海'],
19
- ['1.18.128.0', '1.18.128.255', 256, '北京'],
20
- ['1.24.0.0', '1.31.255.255', 524288, '赤峰'],
21
- ['1.45.0.0', '1.45.255.255', 65536, '北京'],
22
- ['1.48.0.0', '1.51.255.255', 262144, '济南'],
23
- ['1.56.0.0', '1.63.255.255', 524288, '伊春'],
24
- ['1.68.0.0', '1.71.255.255', 262144, '忻州'],
25
- ['1.80.0.0', '1.95.255.255', 1048576, '北京'],
26
- ['1.116.0.0', '1.117.255.255', 131072, '上海'],
27
- ['1.119.0.0', '1.119.255.255', 65536, '北京'],
28
- ['1.180.0.0', '1.185.255.255', 393216, '桂林'],
29
- ['1.188.0.0', '1.199.255.255', 786432, '洛阳'],
30
- ['1.202.0.0', '1.207.255.255', 393216, '铜仁'],
31
- ]
32
-
33
- // 将原始字符串段转换为数值段并计算总数(在模块初始化时完成一次)
2
+ const fs = require('fs')
3
+ const path = require('path')
4
+
5
+ // IP地址转换函数
34
6
  function ipToInt(ip) {
35
7
  const parts = ip.split('.').map(Number)
36
8
  const a = (parts[0] << 24) >>> 0
@@ -49,20 +21,56 @@ function intToIp(int) {
49
21
  ].join('.')
50
22
  }
51
23
 
52
- const chinaIPRanges = (function buildRanges() {
53
- const arr = []
54
- let total = 0
55
- for (let i = 0; i < chinaIPRangesRaw.length; i++) {
56
- const r = chinaIPRangesRaw[i]
57
- const start = ipToInt(r[0])
58
- const end = ipToInt(r[1])
59
- const count = r[2] || end - start + 1
60
- arr.push({ start, end, count, location: r[3] || '' })
61
- total += count
24
+ // 解析CIDR格式的IP段
25
+ function parseCIDR(cidr) {
26
+ const [ipStr, prefixLengthStr] = cidr.split('/')
27
+ const prefixLength = parseInt(prefixLengthStr, 10)
28
+
29
+ const ipInt = ipToInt(ipStr)
30
+ const mask = (0xffffffff << (32 - prefixLength)) >>> 0
31
+ const start = (ipInt & mask) >>> 0
32
+ const end = (start | (~mask >>> 0)) >>> 0
33
+ const count = end - start + 1
34
+
35
+ return { start, end, count, cidr }
36
+ }
37
+
38
+ // 从china_ip_ranges.txt加载中国IP段(CIDR格式)
39
+ const chinaIPRanges = (function loadChinaIPRanges() {
40
+ try {
41
+ const filePath = path.join(__dirname, '../data/china_ip_ranges.txt')
42
+ const content = fs.readFileSync(filePath, 'utf-8')
43
+ const lines = content
44
+ .split('\n')
45
+ .filter((line) => line.trim() && !line.startsWith('#'))
46
+
47
+ const arr = []
48
+ let total = 0
49
+
50
+ for (let i = 0; i < lines.length; i++) {
51
+ const line = lines[i].trim()
52
+ if (!line) continue
53
+
54
+ const range = parseCIDR(line)
55
+ arr.push(range)
56
+ total += range.count
57
+ }
58
+
59
+ // 按IP段大小排序,提高随机选择效率
60
+ arr.sort((a, b) => b.count - a.count)
61
+
62
+ // attach total for convenience
63
+ arr.totalCount = total
64
+
65
+ logger.info(
66
+ `Loaded ${arr.length} Chinese IP ranges from china_ip_ranges.txt, total ${total} IPs`,
67
+ )
68
+ return arr
69
+ } catch (error) {
70
+ logger.error('Failed to load china_ip_ranges.txt:', error.message)
71
+ // 返回空数组,generateRandomChineseIP会使用兜底逻辑
72
+ return { totalCount: 0 }
62
73
  }
63
- // attach total for convenience
64
- arr.totalCount = total
65
- return arr
66
74
  })()
67
75
  const floor = Math.floor
68
76
  const random = Math.random
@@ -144,16 +152,11 @@ module.exports = {
144
152
  // 如果没有选中(理论上不应该发生),回退到最后一个段
145
153
  if (!chosen) chosen = chinaIPRanges[chinaIPRanges.length - 1]
146
154
 
147
- // 在段内随机生成一个 IP(使用段真实的数值范围,而非 csv 中的 count)
155
+ // 在段内随机生成一个 IP(使用段真实的数值范围)
148
156
  const segSize = chosen.end - chosen.start + 1
149
157
  const ipInt = chosen.start + Math.floor(random() * segSize)
150
158
  const ip = intToIp(ipInt)
151
- logger.info(
152
- 'Generated Random Chinese IP:',
153
- ip,
154
- 'location:',
155
- chosen.location,
156
- )
159
+ logger.info('Generated Random Chinese IP:', ip, 'from CIDR:', chosen.cidr)
157
160
  return ip
158
161
  },
159
162
  // 生成chainId的函数
@@ -1,26 +0,0 @@
1
- 开始IP,结束IP,IP个数,位置
2
- 1.0.1.0 ,1.0.3.255 ,768,福州
3
- 1.0.8.0 ,1.0.15.255 ,2048,广州
4
- 1.0.32.0 ,1.0.63.255 ,8192,广州
5
- 1.1.0.0 ,1.1.0.255 ,256,福州
6
- 1.1.2.0 ,1.1.63.255 ,15872,广州
7
- 1.2.0.0 ,1.2.2.255 ,768,北京
8
- 1.2.4.0 ,1.2.127.255 ,31744,广州
9
- 1.3.0.0 ,1.3.255.255 ,65536,广州
10
- 1.4.1.0 ,1.4.127.255 ,32512,广州
11
- 1.8.0.0 ,1.8.255.255 ,65536,北京
12
- 1.10.0.0 ,1.10.9.255 ,2560,福州
13
- 1.10.11.0 ,1.10.127.255 ,29952,广州
14
- 1.12.0.0 ,1.15.255.255 ,262144,上海
15
- 1.18.128.0 ,1.18.128.255 ,256,北京
16
- 1.24.0.0 ,1.31.255.255 ,524288,赤峰
17
- 1.45.0.0 ,1.45.255.255 ,65536,北京
18
- 1.48.0.0 ,1.51.255.255 ,262144,济南
19
- 1.56.0.0 ,1.63.255.255 ,524288,伊春
20
- 1.68.0.0 ,1.71.255.255 ,262144,忻州
21
- 1.80.0.0 ,1.95.255.255 ,1048576,北京
22
- 1.116.0.0 ,1.117.255.255 ,131072,上海
23
- 1.119.0.0 ,1.119.255.255 ,65536,北京
24
- 1.180.0.0 ,1.185.255.255 ,393216,桂林
25
- 1.188.0.0 ,1.199.255.255 ,786432,洛阳
26
- 1.202.0.0 ,1.207.255.255 ,393216,铜仁
Binary file