feat: add base clash api methods

This commit is contained in:
divocat
2025-10-05 16:09:26 +03:00
parent 341f260fcf
commit c75dd3e78b
11 changed files with 233 additions and 0 deletions

View File

@@ -556,6 +556,72 @@ function maskIP(ip = "") {
const ipv4Regex = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
return ip.replace(ipv4Regex, (_match, _p1, _p2, _p3, p4) => `XX.XX.XX.${p4}`);
}
// src/clash/methods/createBaseApiRequest.ts
async function createBaseApiRequest(fetchFn) {
try {
const response = await fetchFn();
if (!response.ok) {
return {
success: false,
message: `HTTP error ${response.status}: ${response.statusText}`
};
}
const data = await response.json();
return {
success: true,
data
};
} catch (e) {
return {
success: false,
message: e instanceof Error ? e.message : "Unknown error"
};
}
}
// src/clash/methods/getConfig.ts
async function getClashConfig() {
return createBaseApiRequest(
() => fetch("http://192.168.160.129:9090/configs", {
method: "GET",
headers: { "Content-Type": "application/json" }
})
);
}
// src/clash/methods/getGroupDelay.ts
async function getClashGroupDelay(group, url = "https://www.gstatic.com/generate_204", timeout = 2e3) {
const endpoint = `http://192.168.160.129:9090/group/${group}/delay?url=${encodeURIComponent(
url
)}&timeout=${timeout}`;
return createBaseApiRequest(
() => fetch(endpoint, {
method: "GET",
headers: { "Content-Type": "application/json" }
})
);
}
// src/clash/methods/getProxies.ts
async function getClashProxies() {
return createBaseApiRequest(
() => fetch("http://192.168.160.129:9090/proxies", {
method: "GET",
headers: { "Content-Type": "application/json" }
})
);
}
// src/clash/methods/getVersion.ts
async function getClashVersion() {
return createBaseApiRequest(
() => fetch("http://192.168.160.129:9090/version", {
method: "GET",
headers: { "Content-Type": "application/json" }
})
);
}
return baseclass.extend({
ALLOWED_WITH_RUSSIA_INSIDE,
BOOTSTRAP_DNS_SERVER_OPTIONS,
@@ -576,8 +642,13 @@ return baseclass.extend({
UPDATE_INTERVAL_OPTIONS,
bulkValidate,
copyToClipboard,
createBaseApiRequest,
executeShellCommand,
getBaseUrl,
getClashConfig,
getClashGroupDelay,
getClashProxies,
getClashVersion,
injectGlobalStyles,
maskIP,
parseValueList,

View File

@@ -12,6 +12,21 @@ const EntryNode = {
async render() {
main.injectGlobalStyles();
main.getClashVersion()
.then(result => console.log('getClashVersion - then', result))
.catch(err => console.log('getClashVersion - err', err))
.finally(() => console.log('getClashVersion - finish'));
main.getClashConfig()
.then(result => console.log('getClashConfig - then', result))
.catch(err => console.log('getClashConfig - err', err))
.finally(() => console.log('getClashConfig - finish'));
main.getClashProxies()
.then(result => console.log('getClashProxies - then', result))
.catch(err => console.log('getClashProxies - err', err))
.finally(() => console.log('getClashProxies - finish'));
const podkopFormMap = new form.Map('podkop', '', null, ['main', 'extra']);
// Main Section