mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-21 05:40:29 +08:00
246 lines
9.9 KiB
JavaScript
246 lines
9.9 KiB
JavaScript
'use strict';Object.defineProperty(exports,Symbol.toStringTag,{value:'Module'});const surge=require('../../../_virtual/surge.cjs'),require$$0=require('foxts/append-set-elements-to-array'),base=require('./base.cjs'),appendArrayInPlace=require('../append-array-in-place.cjs'),require$$2=require('foxts/noop'),require$$2$1=require('foxts/is-probably-ip'),require$$0$1=require('picocolors'),normalizeDomain=require('../normalize-domain.cjs'),dir=require('../../constants/dir.cjs'),misc=require('../misc.cjs');var hasRequiredSurge;
|
|
|
|
function requireSurge () {
|
|
if (hasRequiredSurge) return surge.__exports;
|
|
hasRequiredSurge = 1;
|
|
(function (exports) {
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
function _export(target, all) {
|
|
for(var name in all)Object.defineProperty(target, name, {
|
|
enumerable: true,
|
|
get: all[name]
|
|
});
|
|
}
|
|
_export(exports, {
|
|
SurgeDomainSet: function() {
|
|
return SurgeDomainSet;
|
|
},
|
|
SurgeMitmSgmodule: function() {
|
|
return SurgeMitmSgmodule;
|
|
},
|
|
SurgeRuleSet: function() {
|
|
return SurgeRuleSet;
|
|
}
|
|
});
|
|
const _appendsetelementstoarray = require$$0;
|
|
const _base = /*@__PURE__*/ base.__require();
|
|
const _appendarrayinplace = /*@__PURE__*/ appendArrayInPlace.__require();
|
|
const _noop = require$$2;
|
|
const _isprobablyip = require$$2$1;
|
|
const _picocolors = /*#__PURE__*/ _interop_require_default(require$$0$1);
|
|
const _normalizedomain = /*@__PURE__*/ normalizeDomain.__require();
|
|
const _dir = /*@__PURE__*/ dir.__require();
|
|
const _misc = /*@__PURE__*/ misc.__require();
|
|
function _interop_require_default(obj) {
|
|
return obj && obj.__esModule ? obj : {
|
|
default: obj
|
|
};
|
|
}
|
|
class SurgeDomainSet extends _base.BaseWriteStrategy {
|
|
// readonly type = 'domainset';
|
|
fileExtension = 'conf';
|
|
type = 'domainset';
|
|
result = [
|
|
'this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'
|
|
];
|
|
constructor(outputDir = _dir.OUTPUT_SURGE_DIR){
|
|
super(outputDir);
|
|
}
|
|
withPadding = _misc.withBannerArray;
|
|
writeDomain(domain) {
|
|
this.result.push(domain);
|
|
}
|
|
writeDomainSuffix(domain) {
|
|
this.result.push('.' + domain);
|
|
}
|
|
writeDomainKeywords = _noop.noop;
|
|
writeDomainWildcards = _noop.noop;
|
|
writeUserAgents = _noop.noop;
|
|
writeProcessNames = _noop.noop;
|
|
writeProcessPaths = _noop.noop;
|
|
writeUrlRegexes = _noop.noop;
|
|
writeIpCidrs = _noop.noop;
|
|
writeIpCidr6s = _noop.noop;
|
|
writeGeoip = _noop.noop;
|
|
writeIpAsns = _noop.noop;
|
|
writeSourceIpCidrs = _noop.noop;
|
|
writeSourcePorts = _noop.noop;
|
|
writeDestinationPorts = _noop.noop;
|
|
writeOtherRules = _noop.noop;
|
|
}
|
|
class SurgeRuleSet extends _base.BaseWriteStrategy {
|
|
type;
|
|
outputDir;
|
|
fileExtension;
|
|
result;
|
|
constructor(/** Surge RULE-SET can be both ip or non_ip, so this needs to be specified */ type, outputDir = _dir.OUTPUT_SURGE_DIR){
|
|
super(outputDir), this.type = type, this.outputDir = outputDir, this.fileExtension = 'conf', this.result = [
|
|
'DOMAIN,this_ruleset_is_made_by_sukkaw.ruleset.skk.moe'
|
|
], this.withPadding = _misc.withBannerArray;
|
|
}
|
|
withPadding;
|
|
writeDomain(domain) {
|
|
this.result.push('DOMAIN,' + domain);
|
|
}
|
|
writeDomainSuffix(domain) {
|
|
this.result.push('DOMAIN-SUFFIX,' + domain);
|
|
}
|
|
writeDomainKeywords(keyword) {
|
|
(0, _appendsetelementstoarray.appendSetElementsToArray)(this.result, keyword, (i)=>`DOMAIN-KEYWORD,${i}`);
|
|
}
|
|
writeDomainWildcards(wildcard) {
|
|
(0, _appendsetelementstoarray.appendSetElementsToArray)(this.result, wildcard, (i)=>`DOMAIN-WILDCARD,${i}`);
|
|
}
|
|
writeUserAgents(userAgent) {
|
|
(0, _appendsetelementstoarray.appendSetElementsToArray)(this.result, userAgent, (i)=>`USER-AGENT,${i}`);
|
|
}
|
|
writeProcessNames(processName) {
|
|
(0, _appendsetelementstoarray.appendSetElementsToArray)(this.result, processName, (i)=>`PROCESS-NAME,${i}`);
|
|
}
|
|
writeProcessPaths(processPath) {
|
|
(0, _appendsetelementstoarray.appendSetElementsToArray)(this.result, processPath, (i)=>`PROCESS-NAME,${i}`);
|
|
}
|
|
writeUrlRegexes(urlRegex) {
|
|
(0, _appendsetelementstoarray.appendSetElementsToArray)(this.result, urlRegex, (i)=>`URL-REGEX,${i}`);
|
|
}
|
|
writeIpCidrs(ipCidr, noResolve) {
|
|
for(let i = 0, len = ipCidr.length; i < len; i++){
|
|
this.result.push(`IP-CIDR,${ipCidr[i]}${noResolve ? ',no-resolve' : ''}`);
|
|
}
|
|
}
|
|
writeIpCidr6s(ipCidr6, noResolve) {
|
|
for(let i = 0, len = ipCidr6.length; i < len; i++){
|
|
this.result.push(`IP-CIDR6,${ipCidr6[i]}${noResolve ? ',no-resolve' : ''}`);
|
|
}
|
|
}
|
|
writeGeoip(geoip, noResolve) {
|
|
(0, _appendsetelementstoarray.appendSetElementsToArray)(this.result, geoip, (i)=>`GEOIP,${i}${noResolve ? ',no-resolve' : ''}`);
|
|
}
|
|
writeIpAsns(asns, noResolve) {
|
|
(0, _appendsetelementstoarray.appendSetElementsToArray)(this.result, asns, (i)=>`IP-ASN,${i}${noResolve ? ',no-resolve' : ''}`);
|
|
}
|
|
writeSourceIpCidrs(sourceIpCidr) {
|
|
for(let i = 0, len = sourceIpCidr.length; i < len; i++){
|
|
this.result.push(`SRC-IP,${sourceIpCidr[i]}`);
|
|
}
|
|
}
|
|
writeSourcePorts(port) {
|
|
(0, _appendsetelementstoarray.appendSetElementsToArray)(this.result, port, (i)=>`SRC-PORT,${i}`);
|
|
}
|
|
writeDestinationPorts(port) {
|
|
(0, _appendsetelementstoarray.appendSetElementsToArray)(this.result, port, (i)=>`DEST-PORT,${i}`);
|
|
}
|
|
writeOtherRules(rule) {
|
|
(0, _appendarrayinplace.appendArrayInPlace)(this.result, rule);
|
|
}
|
|
}
|
|
class SurgeMitmSgmodule extends _base.BaseWriteStrategy {
|
|
// readonly type = 'domainset';
|
|
fileExtension = 'sgmodule';
|
|
type = '';
|
|
rules = new Set();
|
|
get result() {
|
|
if (this.rules.size === 0) {
|
|
return null;
|
|
}
|
|
return [
|
|
'#!name=[Sukka] Surge Reject MITM',
|
|
`#!desc=为 URL Regex 规则组启用 MITM (size: ${this.rules.size})`,
|
|
'',
|
|
'[MITM]',
|
|
'hostname = %APPEND% ' + Array.from(this.rules).join(', ')
|
|
];
|
|
}
|
|
withPadding = _misc.withIdentityContent;
|
|
constructor(moduleName, outputDir = _dir.OUTPUT_MODULES_DIR){
|
|
super(outputDir);
|
|
this.withFilename(moduleName);
|
|
}
|
|
writeDomain = _noop.noop;
|
|
writeDomainSuffix = _noop.noop;
|
|
writeDomainKeywords = _noop.noop;
|
|
writeDomainWildcards = _noop.noop;
|
|
writeUserAgents = _noop.noop;
|
|
writeProcessNames = _noop.noop;
|
|
writeProcessPaths = _noop.noop;
|
|
writeUrlRegexes(urlRegexes) {
|
|
const urlRegexResults = [];
|
|
const parsedFailures = [];
|
|
const parsed = [];
|
|
for (let urlRegex of urlRegexes){
|
|
if (urlRegex.startsWith('http://') || urlRegex.startsWith('^http://')) {
|
|
continue;
|
|
}
|
|
if (urlRegex.startsWith('^https?://')) {
|
|
urlRegex = urlRegex.slice(10);
|
|
}
|
|
if (urlRegex.startsWith('^https://')) {
|
|
urlRegex = urlRegex.slice(9);
|
|
}
|
|
const potentialHostname = urlRegex.split('/')[0]// pre process regex
|
|
.replaceAll(String.raw`\.`, '.').replaceAll('.+', '*').replaceAll(/([a-z])\?/g, '($1|)')// convert regex to surge hostlist syntax
|
|
.replaceAll('([a-z])', '?').replaceAll(String.raw`\d`, '?').replaceAll(/\*+/g, '*');
|
|
let processed = [
|
|
potentialHostname
|
|
];
|
|
const matches = [
|
|
...potentialHostname.matchAll(/\((?:([^()|]+)\|)+([^()|]*)\)/g)
|
|
];
|
|
if (matches.length > 0) {
|
|
const replaceVariant = (combinations, fullMatch, options)=>{
|
|
const newCombinations = [];
|
|
combinations.forEach((combination)=>{
|
|
options.forEach((option)=>{
|
|
newCombinations.push(combination.replace(fullMatch, option));
|
|
});
|
|
});
|
|
return newCombinations;
|
|
};
|
|
for(let i = 0; i < matches.length; i++){
|
|
const match = matches[i];
|
|
const [_, ...options] = match;
|
|
processed = replaceVariant(processed, _, options);
|
|
}
|
|
}
|
|
urlRegexResults.push({
|
|
origin: potentialHostname,
|
|
processed
|
|
});
|
|
}
|
|
for (const i of urlRegexResults){
|
|
for (const processed of i.processed){
|
|
if ((0, _normalizedomain.normalizeDomain)(processed.replaceAll('*', 'a').replaceAll('?', 'b'))) {
|
|
parsed.push([
|
|
i.origin,
|
|
processed
|
|
]);
|
|
} else if (!(0, _isprobablyip.isProbablyIpv4)(processed)) {
|
|
parsedFailures.push([
|
|
i.origin,
|
|
processed
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
if (parsedFailures.length > 0) {
|
|
console.error(_picocolors.default.bold('Parsed Failed'));
|
|
console.table(parsedFailures);
|
|
}
|
|
for(let i = 0, len = parsed.length; i < len; i++){
|
|
this.rules.add(parsed[i][1]);
|
|
}
|
|
}
|
|
writeIpCidrs = _noop.noop;
|
|
writeIpCidr6s = _noop.noop;
|
|
writeGeoip = _noop.noop;
|
|
writeIpAsns = _noop.noop;
|
|
writeSourceIpCidrs = _noop.noop;
|
|
writeSourcePorts = _noop.noop;
|
|
writeDestinationPorts = _noop.noop;
|
|
writeOtherRules = _noop.noop;
|
|
}
|
|
} (surge.__exports));
|
|
return surge.__exports;
|
|
}exports.__require=requireSurge; |