mirror of
https://github.com/itdoginfo/podkop.git
synced 2025-12-10 13:36:52 +03:00
feat: add base clash api methods
This commit is contained in:
2
fe-app-podkop/src/clash/index.ts
Normal file
2
fe-app-podkop/src/clash/index.ts
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
export * from './types';
|
||||||
|
export * from './methods';
|
||||||
28
fe-app-podkop/src/clash/methods/createBaseApiRequest.ts
Normal file
28
fe-app-podkop/src/clash/methods/createBaseApiRequest.ts
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import { IBaseApiResponse } from '../types';
|
||||||
|
|
||||||
|
export async function createBaseApiRequest<T>(
|
||||||
|
fetchFn: () => Promise<Response>,
|
||||||
|
): Promise<IBaseApiResponse<T>> {
|
||||||
|
try {
|
||||||
|
const response = await fetchFn();
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
return {
|
||||||
|
success: false as const,
|
||||||
|
message: `HTTP error ${response.status}: ${response.statusText}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const data: T = await response.json();
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true as const,
|
||||||
|
data,
|
||||||
|
};
|
||||||
|
} catch (e) {
|
||||||
|
return {
|
||||||
|
success: false as const,
|
||||||
|
message: e instanceof Error ? e.message : 'Unknown error',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
13
fe-app-podkop/src/clash/methods/getConfig.ts
Normal file
13
fe-app-podkop/src/clash/methods/getConfig.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { ClashAPI, IBaseApiResponse } from '../types';
|
||||||
|
import { createBaseApiRequest } from './createBaseApiRequest';
|
||||||
|
|
||||||
|
export async function getClashConfig(): Promise<
|
||||||
|
IBaseApiResponse<ClashAPI.Config>
|
||||||
|
> {
|
||||||
|
return createBaseApiRequest<ClashAPI.Config>(() =>
|
||||||
|
fetch('http://192.168.160.129:9090/configs', {
|
||||||
|
method: 'GET',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
19
fe-app-podkop/src/clash/methods/getGroupDelay.ts
Normal file
19
fe-app-podkop/src/clash/methods/getGroupDelay.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import { ClashAPI, IBaseApiResponse } from '../types';
|
||||||
|
import { createBaseApiRequest } from './createBaseApiRequest';
|
||||||
|
|
||||||
|
export async function getClashGroupDelay(
|
||||||
|
group: string,
|
||||||
|
url = 'https://www.gstatic.com/generate_204',
|
||||||
|
timeout = 2000,
|
||||||
|
): Promise<IBaseApiResponse<ClashAPI.Delays>> {
|
||||||
|
const endpoint = `http://192.168.160.129:9090/group/${group}/delay?url=${encodeURIComponent(
|
||||||
|
url,
|
||||||
|
)}&timeout=${timeout}`;
|
||||||
|
|
||||||
|
return createBaseApiRequest<ClashAPI.Delays>(() =>
|
||||||
|
fetch(endpoint, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
13
fe-app-podkop/src/clash/methods/getProxies.ts
Normal file
13
fe-app-podkop/src/clash/methods/getProxies.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { ClashAPI, IBaseApiResponse } from '../types';
|
||||||
|
import { createBaseApiRequest } from './createBaseApiRequest';
|
||||||
|
|
||||||
|
export async function getClashProxies(): Promise<
|
||||||
|
IBaseApiResponse<ClashAPI.Proxies>
|
||||||
|
> {
|
||||||
|
return createBaseApiRequest<ClashAPI.Proxies>(() =>
|
||||||
|
fetch('http://192.168.160.129:9090/proxies', {
|
||||||
|
method: 'GET',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
13
fe-app-podkop/src/clash/methods/getVersion.ts
Normal file
13
fe-app-podkop/src/clash/methods/getVersion.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { ClashAPI, IBaseApiResponse } from '../types';
|
||||||
|
import { createBaseApiRequest } from './createBaseApiRequest';
|
||||||
|
|
||||||
|
export async function getClashVersion(): Promise<
|
||||||
|
IBaseApiResponse<ClashAPI.Version>
|
||||||
|
> {
|
||||||
|
return createBaseApiRequest<ClashAPI.Version>(() =>
|
||||||
|
fetch('http://192.168.160.129:9090/version', {
|
||||||
|
method: 'GET',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
5
fe-app-podkop/src/clash/methods/index.ts
Normal file
5
fe-app-podkop/src/clash/methods/index.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
export * from './createBaseApiRequest';
|
||||||
|
export * from './getConfig';
|
||||||
|
export * from './getGroupDelay';
|
||||||
|
export * from './getProxies';
|
||||||
|
export * from './getVersion';
|
||||||
53
fe-app-podkop/src/clash/types.ts
Normal file
53
fe-app-podkop/src/clash/types.ts
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
export type IBaseApiResponse<T> =
|
||||||
|
| {
|
||||||
|
success: true;
|
||||||
|
data: T;
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
success: false;
|
||||||
|
message: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-namespace
|
||||||
|
export namespace ClashAPI {
|
||||||
|
export interface Version {
|
||||||
|
meta: boolean;
|
||||||
|
premium: boolean;
|
||||||
|
version: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Config {
|
||||||
|
port: number;
|
||||||
|
'socks-port': number;
|
||||||
|
'redir-port': number;
|
||||||
|
'tproxy-port': number;
|
||||||
|
'mixed-port': number;
|
||||||
|
'allow-lan': boolean;
|
||||||
|
'bind-address': string;
|
||||||
|
mode: 'Rule' | 'Global' | 'Direct';
|
||||||
|
'mode-list': string[];
|
||||||
|
'log-level': 'debug' | 'info' | 'warn' | 'error';
|
||||||
|
ipv6: boolean;
|
||||||
|
tun: null | Record<string, unknown>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ProxyHistoryEntry {
|
||||||
|
time: string;
|
||||||
|
delay: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ProxyBase {
|
||||||
|
type: string;
|
||||||
|
name: string;
|
||||||
|
udp: boolean;
|
||||||
|
history: ProxyHistoryEntry[];
|
||||||
|
now?: string;
|
||||||
|
all?: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Proxies {
|
||||||
|
proxies: Record<string, ProxyBase>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Delays = Record<string, number>;
|
||||||
|
}
|
||||||
@@ -4,4 +4,5 @@
|
|||||||
|
|
||||||
export * from './validators';
|
export * from './validators';
|
||||||
export * from './helpers';
|
export * from './helpers';
|
||||||
|
export * from './clash';
|
||||||
export * from './constants';
|
export * from './constants';
|
||||||
|
|||||||
@@ -556,6 +556,72 @@ function maskIP(ip = "") {
|
|||||||
const ipv4Regex = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
|
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}`);
|
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({
|
return baseclass.extend({
|
||||||
ALLOWED_WITH_RUSSIA_INSIDE,
|
ALLOWED_WITH_RUSSIA_INSIDE,
|
||||||
BOOTSTRAP_DNS_SERVER_OPTIONS,
|
BOOTSTRAP_DNS_SERVER_OPTIONS,
|
||||||
@@ -576,8 +642,13 @@ return baseclass.extend({
|
|||||||
UPDATE_INTERVAL_OPTIONS,
|
UPDATE_INTERVAL_OPTIONS,
|
||||||
bulkValidate,
|
bulkValidate,
|
||||||
copyToClipboard,
|
copyToClipboard,
|
||||||
|
createBaseApiRequest,
|
||||||
executeShellCommand,
|
executeShellCommand,
|
||||||
getBaseUrl,
|
getBaseUrl,
|
||||||
|
getClashConfig,
|
||||||
|
getClashGroupDelay,
|
||||||
|
getClashProxies,
|
||||||
|
getClashVersion,
|
||||||
injectGlobalStyles,
|
injectGlobalStyles,
|
||||||
maskIP,
|
maskIP,
|
||||||
parseValueList,
|
parseValueList,
|
||||||
|
|||||||
@@ -12,6 +12,21 @@ const EntryNode = {
|
|||||||
async render() {
|
async render() {
|
||||||
main.injectGlobalStyles();
|
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']);
|
const podkopFormMap = new form.Map('podkop', '', null, ['main', 'extra']);
|
||||||
|
|
||||||
// Main Section
|
// Main Section
|
||||||
|
|||||||
Reference in New Issue
Block a user