surgio
Version:
Generating rules for Surge, Clash, Quantumult like a PRO
103 lines • 4.11 kB
JavaScript
"use strict";
// istanbul ignore file
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const assert_1 = __importDefault(require("assert"));
const zod_1 = require("zod");
const types_1 = require("../types");
const utils_1 = require("../utils");
const cache_1 = require("../utils/cache");
const env_flag_1 = require("../utils/env-flag");
const http_client_1 = __importDefault(require("../utils/http-client"));
const Provider_1 = __importDefault(require("./Provider"));
class BlackSSLProvider extends Provider_1.default {
username;
password;
constructor(name, config) {
super(name, config);
const schema = zod_1.z.object({
username: zod_1.z.string(),
password: zod_1.z.string(),
});
const result = schema.safeParse(config);
// istanbul ignore next
if (!result.success) {
throw new utils_1.SurgioError('BlackSSLProvider 配置校验失败', {
cause: result.error,
providerName: name,
});
}
this.username = result.data.username;
this.password = result.data.password;
this.supportGetSubscriptionUserInfo = true;
}
getSubscriptionUserInfo = async () => {
const { subscriptionUserInfo } = await this.getBlackSSLConfig(this.username, this.password);
if (subscriptionUserInfo) {
return subscriptionUserInfo;
}
return undefined;
};
getNodeList = async () => {
const { nodeList } = await this.getBlackSSLConfig(this.username, this.password);
if (this.config.hooks?.afterNodeListResponse) {
const newList = await this.config.hooks.afterNodeListResponse(nodeList, {});
if (newList) {
return newList;
}
}
return nodeList;
};
getNodeListV2 = async (params = {}) => {
const { nodeList, subscriptionUserInfo } = await this.getBlackSSLConfig(this.username, this.password);
if (this.config.hooks?.afterNodeListResponse) {
const newList = await this.config.hooks.afterNodeListResponse(nodeList, params);
if (newList) {
return { nodeList: newList, subscriptionUserInfo };
}
}
return { nodeList, subscriptionUserInfo };
};
// istanbul ignore next
async getBlackSSLConfig(username, password) {
(0, assert_1.default)(username, '未指定 BlackSSL username.');
(0, assert_1.default)(password, '未指定 BlackSSL password.');
const key = `blackssl_${username}`;
const cachedConfig = await cache_1.unifiedCache.get(key);
const response = cachedConfig
? JSON.parse(cachedConfig)
: await (async () => {
const res = await http_client_1.default.get('https://api.darkssl.com/v1/service/ssl_info', {
searchParams: {
username,
password,
},
headers: {
'user-agent': 'GoAgentX/774 CFNetwork/901.1 Darwin/17.6.0 (x86_64)',
},
});
await cache_1.unifiedCache.set(key, res.body, (0, env_flag_1.getProviderCacheMaxage)());
return JSON.parse(res.body);
})();
return {
nodeList: response.ssl_nodes.map((item) => ({
nodeName: item.name,
type: types_1.NodeTypeEnum.HTTPS,
hostname: item.server,
port: item.port,
username,
password,
})),
subscriptionUserInfo: {
upload: 0,
download: response.transfer_used,
total: response.transfer_enable,
expire: response.expired_at,
},
};
}
}
exports.default = BlackSSLProvider;
//# sourceMappingURL=BlackSSLProvider.js.map