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 './helpers';
|
||||
export * from './clash';
|
||||
export * from './constants';
|
||||
|
||||
Reference in New Issue
Block a user