mirror of
https://github.com/SukkaW/Surge.git
synced 2026-09-12 18:44:36 +08:00
Refactor: Build AI CIDR
This commit is contained in:
@@ -6,22 +6,6 @@ import { task } from './trace';
|
|||||||
|
|
||||||
const OPENAI_VOICE_IP_URL = 'https://openai.com/chatgpt-voice.json';
|
const OPENAI_VOICE_IP_URL = 'https://openai.com/chatgpt-voice.json';
|
||||||
|
|
||||||
function readPrefix(
|
|
||||||
entry: Record<string, unknown>,
|
|
||||||
field: 'ipv4Prefix' | 'ipv6Prefix',
|
|
||||||
version: 4 | 6,
|
|
||||||
index: number
|
|
||||||
) {
|
|
||||||
const value = entry[field];
|
|
||||||
if (value === undefined) return null;
|
|
||||||
|
|
||||||
if (typeof value !== 'string' || fastIpVersion(value) !== version) {
|
|
||||||
throw new TypeError(`Invalid ${field} at prefixes[${index}]`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
function parseOpenAIVoiceJSON(data: unknown) {
|
function parseOpenAIVoiceJSON(data: unknown) {
|
||||||
if (
|
if (
|
||||||
data === null
|
data === null
|
||||||
@@ -46,23 +30,30 @@ function parseOpenAIVoiceJSON(data: unknown) {
|
|||||||
|
|
||||||
for (let i = 0, len = data.prefixes.length; i < len; i++) {
|
for (let i = 0, len = data.prefixes.length; i < len; i++) {
|
||||||
const entry = data.prefixes[i];
|
const entry = data.prefixes[i];
|
||||||
if (entry === null || typeof entry !== 'object') {
|
if (typeof entry !== 'object' || entry == null) {
|
||||||
throw new TypeError(`Invalid OpenAI Voice IP list entry at prefixes[${i}]`);
|
throw new TypeError(`Invalid OpenAI Voice IP list entry at prefixes[${i}]`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const ipv4 = readPrefix(entry as Record<string, unknown>, 'ipv4Prefix', 4, i);
|
const prefixes = Object.values(entry);
|
||||||
const ipv6 = readPrefix(entry as Record<string, unknown>, 'ipv6Prefix', 6, i);
|
if (prefixes.length === 0) {
|
||||||
|
continue;
|
||||||
if (ipv4 === null && ipv6 === null) {
|
|
||||||
throw new TypeError(`OpenAI Voice IP list entry prefixes[${i}] has no IP prefix`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ipv4 !== null) cidr4.add(ipv4);
|
for (let j = 0, prefixLen = prefixes.length; j < prefixLen; j++) {
|
||||||
if (ipv6 !== null) cidr6.add(ipv6);
|
const prefix = prefixes[j];
|
||||||
}
|
if (typeof prefix !== 'string') {
|
||||||
|
throw new TypeError(`Invalid IP prefix at prefixes[${i}]`);
|
||||||
|
}
|
||||||
|
|
||||||
if (cidr4.size + cidr6.size === 0) {
|
const version = fastIpVersion(prefix);
|
||||||
throw new TypeError('OpenAI Voice IP list is empty');
|
if (version === 4) {
|
||||||
|
cidr4.add(prefix);
|
||||||
|
} else if (version === 6) {
|
||||||
|
cidr6.add(prefix);
|
||||||
|
} else {
|
||||||
|
throw new TypeError(`Invalid IP prefix at prefixes[${i}]`);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -72,7 +63,7 @@ function parseOpenAIVoiceJSON(data: unknown) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export const buildOpenAIVoice = task(require.main === module, __filename)(async (span) => {
|
export const buildAICIDR = task(require.main === module, __filename)(async (span) => {
|
||||||
const { cidr4, cidr6, lastUpdated } = await span.traceChildAsync('get OpenAI Voice IP ranges', async () => {
|
const { cidr4, cidr6, lastUpdated } = await span.traceChildAsync('get OpenAI Voice IP ranges', async () => {
|
||||||
const response = await $$fetch(OPENAI_VOICE_IP_URL);
|
const response = await $$fetch(OPENAI_VOICE_IP_URL);
|
||||||
return parseOpenAIVoiceJSON(await response.json());
|
return parseOpenAIVoiceJSON(await response.json());
|
||||||
@@ -6,7 +6,7 @@ import { downloadPreviousBuild } from './download-previous-build';
|
|||||||
import { buildCommon } from './build-common';
|
import { buildCommon } from './build-common';
|
||||||
import { buildRejectIPList } from './build-reject-ip-list';
|
import { buildRejectIPList } from './build-reject-ip-list';
|
||||||
import { buildAppleCdn } from './build-apple-cdn';
|
import { buildAppleCdn } from './build-apple-cdn';
|
||||||
import { buildOpenAIVoice } from './build-openai-voice';
|
import { buildAICIDR } from './build-ai-cidr';
|
||||||
import { buildRejectDomainSet } from './build-reject-domainset';
|
import { buildRejectDomainSet } from './build-reject-domainset';
|
||||||
import { buildChnCidr } from './build-chn-cidr';
|
import { buildChnCidr } from './build-chn-cidr';
|
||||||
import { buildSpeedtestDomainSet } from './build-speedtest-domainset';
|
import { buildSpeedtestDomainSet } from './build-speedtest-domainset';
|
||||||
@@ -104,7 +104,7 @@ const buildFinishedLock = path.join(ROOT_DIR, '.BUILD_FINISHED');
|
|||||||
downloadPreviousBuildPromise.then(() => buildCommon()),
|
downloadPreviousBuildPromise.then(() => buildCommon()),
|
||||||
downloadPreviousBuildPromise.then(() => buildRejectIPList()),
|
downloadPreviousBuildPromise.then(() => buildRejectIPList()),
|
||||||
downloadPreviousBuildPromise.then(() => buildAppleCdn()),
|
downloadPreviousBuildPromise.then(() => buildAppleCdn()),
|
||||||
downloadPreviousBuildPromise.then(() => buildOpenAIVoice()),
|
downloadPreviousBuildPromise.then(() => buildAICIDR()),
|
||||||
downloadPreviousBuildPromise.then(() => cdnDownloadWorker.buildCdnDownloadConf()),
|
downloadPreviousBuildPromise.then(() => cdnDownloadWorker.buildCdnDownloadConf()),
|
||||||
downloadPreviousBuildPromise.then(() => buildRejectDomainSet()),
|
downloadPreviousBuildPromise.then(() => buildRejectDomainSet()),
|
||||||
downloadPreviousBuildPromise.then(() => telegramCidrWorker.buildTelegramCIDR()),
|
downloadPreviousBuildPromise.then(() => telegramCidrWorker.buildTelegramCIDR()),
|
||||||
|
|||||||
17
README.md
17
README.md
@@ -423,16 +423,22 @@ rules:
|
|||||||
|
|
||||||
#### AI
|
#### AI
|
||||||
|
|
||||||
- 域名规则 人工维护
|
- 域名和 IP 规则,人工维护 + 自动更新
|
||||||
- 包含 OpenAI、Google Gemini、Claude、Perplexity 等
|
- 包含 OpenAI、Google Gemini、Claude、Perplexity 等
|
||||||
|
|
||||||
**Surge**
|
**Surge**
|
||||||
|
|
||||||
```ini
|
```ini
|
||||||
|
# Non IP
|
||||||
RULE-SET,https://ruleset.skk.moe/List/non_ip/ai.conf,[Replace with your policy]
|
RULE-SET,https://ruleset.skk.moe/List/non_ip/ai.conf,[Replace with your policy]
|
||||||
RULE-SET,https://ruleset.skk.moe/List/non_ip/apple_intelligence.conf,[Replace with your policy],extended-matching
|
RULE-SET,https://ruleset.skk.moe/List/non_ip/apple_intelligence.conf,[Replace with your policy],extended-matching
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```ini
|
||||||
|
# IP
|
||||||
|
RULE-SET,https://ruleset.skk.moe/List/ip/ai.conf,[Replace with your policy]
|
||||||
|
```
|
||||||
|
|
||||||
**Mihomo**
|
**Mihomo**
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
@@ -451,10 +457,19 @@ rule-providers:
|
|||||||
interval: 43200
|
interval: 43200
|
||||||
url: https://ruleset.skk.moe/Clash/non_ip/apple_intelligence.txt
|
url: https://ruleset.skk.moe/Clash/non_ip/apple_intelligence.txt
|
||||||
path: ./sukkaw_ruleset/apple_intelligence_non_ip.txt
|
path: ./sukkaw_ruleset/apple_intelligence_non_ip.txt
|
||||||
|
ai_ip:
|
||||||
|
type: http
|
||||||
|
behavior: classical
|
||||||
|
format: text
|
||||||
|
interval: 43200
|
||||||
|
url: https://ruleset.skk.moe/Clash/ip/ai.txt
|
||||||
|
path: ./sukkaw_ruleset/ai_ip.txt
|
||||||
|
|
||||||
rules:
|
rules:
|
||||||
- RULE-SET,ai_non_ip,[Replace with your policy]
|
- RULE-SET,ai_non_ip,[Replace with your policy]
|
||||||
- RULE-SET,apple_intelligence_non_ip,[Replace with your policy]
|
- RULE-SET,apple_intelligence_non_ip,[Replace with your policy]
|
||||||
|
|
||||||
|
- RULE-SET,ai_ip,[Replace with your policy]
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Telegram
|
#### Telegram
|
||||||
|
|||||||
Reference in New Issue
Block a user