@herbertgao/surgio
Version:
Generating rules for Surge, Clash, Quantumult like a PRO
210 lines • 5.39 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isLoon = exports.isShadowrocket = exports.isQuantumultX = exports.isStash = exports.isClashMetaForAndroid = exports.isClashVerge = exports.isClash = exports.isSurgeMac = exports.isSurgeIOS = void 0;
const compare_versions_1 = require("compare-versions");
/**
* Exapmle:
* isSurge('Surge iOS/2920')
* isSurge('Surge/1129 CFNetwork/1335.0.3.2 Darwin/21.6.0')
*/
const isSurgeIOS = (ua, version) => {
if (!ua) {
return false;
}
const isClient = ua.toLowerCase().includes('surge ios');
if (!isClient) {
return false;
}
if (!version) {
return true;
}
const matcher = /(surge ios)\/([\w\.]+)/i;
const result = matcher.exec(ua.toLowerCase());
const clientVersion = result ? result[2] : '';
try {
return (0, compare_versions_1.satisfies)(clientVersion, version);
}
catch {
return false;
}
};
exports.isSurgeIOS = isSurgeIOS;
/**
* Exapmle:
* isSurge('Surge Mac/2408')
*/
const isSurgeMac = (ua, version) => {
if (!ua) {
return false;
}
const isClient = ua.toLowerCase().includes('surge mac');
if (!isClient) {
return false;
}
if (!version) {
return true;
}
const matcher = /(surge mac)\/([\w\.]+)/i;
const result = matcher.exec(ua.toLowerCase());
const clientVersion = result ? result[2] : '';
try {
return (0, compare_versions_1.satisfies)(clientVersion, version);
}
catch {
return false;
}
};
exports.isSurgeMac = isSurgeMac;
const isClash = (ua, version) => {
if (!ua) {
return false;
}
const isClient = ua.toLowerCase().includes('clash');
if (!isClient) {
return false;
}
if (!version) {
return true;
}
const matcher = /clash\/([\w\.]+)/i;
const result = matcher.exec(ua.toLowerCase());
const clientVersion = result ? result[1] : '';
try {
return (0, compare_versions_1.satisfies)(clientVersion, version);
}
catch {
return false;
}
};
exports.isClash = isClash;
const isClashVerge = (ua, version) => {
if (!ua) {
return false;
}
const matcher = /\bclash-verge\/v([0-9.]+)\b/i;
const isClient = matcher.exec(ua);
if (!isClient) {
return false;
}
if (!version) {
return true;
}
const clientVersion = isClient ? isClient[1] : '';
try {
return (0, compare_versions_1.satisfies)(clientVersion, version);
}
catch {
return false;
}
};
exports.isClashVerge = isClashVerge;
const isClashMetaForAndroid = (ua, version) => {
if (!ua) {
return false;
}
const matcher = /\bClashMetaForAndroid\/(.+)\b/i;
const isClient = matcher.exec(ua);
if (!isClient) {
return false;
}
if (!version) {
return true;
}
const clientVersion = isClient ? isClient[1].replace(/\.Meta.*/gi, '') : '';
try {
return (0, compare_versions_1.satisfies)(clientVersion, version);
}
catch {
return false;
}
};
exports.isClashMetaForAndroid = isClashMetaForAndroid;
const isStash = (ua, version) => {
if (!ua) {
return false;
}
const isClient = ua.toLowerCase().includes('stash');
if (!isClient) {
return false;
}
if (!version) {
return true;
}
const matcher = /(stash)\/([\w\.]+)/i;
const result = matcher.exec(ua.toLowerCase());
const clientVersion = result ? result[2] : '';
try {
return (0, compare_versions_1.satisfies)(clientVersion, version);
}
catch {
return false;
}
};
exports.isStash = isStash;
const isQuantumultX = (ua, version) => {
if (!ua) {
return false;
}
const isClient = ua.includes('Quantumult%20X');
if (!isClient) {
return false;
}
if (!version) {
return true;
}
const matcher = /(Quantumult%20X)\/([\w\.]+)/i;
const result = matcher.exec(ua.toLowerCase());
const clientVersion = result ? result[2] : '';
try {
return (0, compare_versions_1.satisfies)(clientVersion, version);
}
catch {
return false;
}
};
exports.isQuantumultX = isQuantumultX;
const isShadowrocket = (ua, version) => {
if (!ua) {
return false;
}
const isClient = ua.includes('Shadowrocket');
if (!isClient) {
return false;
}
if (!version) {
return true;
}
const matcher = /(Shadowrocket)\/([\w\.]+)/i;
const result = matcher.exec(ua.toLowerCase());
const clientVersion = result ? result[2] : '';
try {
return (0, compare_versions_1.satisfies)(clientVersion, version);
}
catch {
return false;
}
};
exports.isShadowrocket = isShadowrocket;
const isLoon = (ua, version) => {
if (!ua) {
return false;
}
const isClient = ua.includes('Loon');
if (!isClient) {
return false;
}
if (!version) {
return true;
}
const matcher = /(Loon)\/([\w\.]+)/i;
const result = matcher.exec(ua.toLowerCase());
const clientVersion = result ? result[2] : '';
try {
return (0, compare_versions_1.satisfies)(clientVersion, version);
}
catch {
return false;
}
};
exports.isLoon = isLoon;
//# sourceMappingURL=useragent.js.map