mirror of
https://github.com/itdoginfo/podkop.git
synced 2025-12-06 11:36:50 +03:00
feat: migrate to proxied clash api methods
This commit is contained in:
@@ -1,4 +0,0 @@
|
||||
export function getBaseUrl(): string {
|
||||
const { protocol, hostname } = window.location;
|
||||
return `${protocol}//${hostname}`;
|
||||
}
|
||||
@@ -1,9 +1,3 @@
|
||||
export function getClashApiUrl(): string {
|
||||
const { hostname } = window.location;
|
||||
|
||||
return `http://${hostname}:9090`;
|
||||
}
|
||||
|
||||
export function getClashWsUrl(): string {
|
||||
const { hostname } = window.location;
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
export * from './getBaseUrl';
|
||||
export * from './parseValueList';
|
||||
export * from './injectGlobalStyles';
|
||||
export * from './withTimeout';
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
import { getClashApiUrl } from '../../../helpers';
|
||||
import { createBaseApiRequest, IBaseApiResponse } from '../../api';
|
||||
|
||||
export async function getGroupLatency(
|
||||
tag: string,
|
||||
timeout: number = 5000,
|
||||
url: string = 'https://www.gstatic.com/generate_204',
|
||||
): Promise<IBaseApiResponse<void>> {
|
||||
return createBaseApiRequest<void>(() =>
|
||||
fetch(
|
||||
`${getClashApiUrl()}/group/${tag}/delay?url=${encodeURIComponent(url)}&timeout=${timeout}`,
|
||||
{
|
||||
method: 'GET',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
import { ClashAPI } from '../../types';
|
||||
import { getClashApiUrl } from '../../../helpers';
|
||||
import { createBaseApiRequest, IBaseApiResponse } from '../../api';
|
||||
|
||||
export async function getProxies(): Promise<
|
||||
IBaseApiResponse<ClashAPI.Proxies>
|
||||
> {
|
||||
return createBaseApiRequest<ClashAPI.Proxies>(() =>
|
||||
fetch(`${getClashApiUrl()}/proxies`, {
|
||||
method: 'GET',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
import { getClashApiUrl } from '../../../helpers';
|
||||
import { createBaseApiRequest, IBaseApiResponse } from '../../api';
|
||||
|
||||
export async function getProxyLatency(
|
||||
tag: string,
|
||||
timeout: number = 2000,
|
||||
url: string = 'https://www.gstatic.com/generate_204',
|
||||
): Promise<IBaseApiResponse<void>> {
|
||||
return createBaseApiRequest<void>(() =>
|
||||
fetch(
|
||||
`${getClashApiUrl()}/proxies/${tag}/delay?url=${encodeURIComponent(url)}&timeout=${timeout}`,
|
||||
{
|
||||
method: 'GET',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
import { getGroupLatency } from './getGroupLatency';
|
||||
import { getProxies } from './getProxies';
|
||||
import { getProxyLatency } from './getProxyLatency';
|
||||
import { setProxy } from './setProxy';
|
||||
|
||||
export const ClashMethods = {
|
||||
getGroupLatency,
|
||||
getProxies,
|
||||
getProxyLatency,
|
||||
setProxy,
|
||||
};
|
||||
@@ -1,15 +0,0 @@
|
||||
import { getClashApiUrl } from '../../../helpers';
|
||||
import { createBaseApiRequest, IBaseApiResponse } from '../../api';
|
||||
|
||||
export async function setProxy(
|
||||
selector: string,
|
||||
outbound: string,
|
||||
): Promise<IBaseApiResponse<void>> {
|
||||
return createBaseApiRequest<void>(() =>
|
||||
fetch(`${getClashApiUrl()}/proxies/${selector}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ name: outbound }),
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { getConfigSections } from './getConfigSections';
|
||||
import { Podkop } from '../../types';
|
||||
import { ClashMethods } from '../clash';
|
||||
import { getProxyUrlName, splitProxyString } from '../../../helpers';
|
||||
import { PodkopShellMethods } from '../shell';
|
||||
|
||||
interface IGetDashboardSectionsResponse {
|
||||
success: boolean;
|
||||
@@ -10,7 +10,7 @@ interface IGetDashboardSectionsResponse {
|
||||
|
||||
export async function getDashboardSections(): Promise<IGetDashboardSectionsResponse> {
|
||||
const configSections = await getConfigSections();
|
||||
const clashProxies = await ClashMethods.getProxies();
|
||||
const clashProxies = await PodkopShellMethods.getClashApiProxies();
|
||||
|
||||
if (!clashProxies.success) {
|
||||
return {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
export * from './clash';
|
||||
export * from './custom';
|
||||
export * from './fakeip';
|
||||
export * from './shell';
|
||||
|
||||
@@ -3,10 +3,11 @@ import { Podkop } from '../../types';
|
||||
|
||||
export async function callBaseMethod<T>(
|
||||
method: Podkop.AvailableMethods,
|
||||
args: string[] = [],
|
||||
): Promise<Podkop.MethodResponse<T>> {
|
||||
const response = await executeShellCommand({
|
||||
command: '/usr/bin/podkop',
|
||||
args: [method],
|
||||
args: [method as string, ...args],
|
||||
timeout: 10000,
|
||||
});
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { callBaseMethod } from './callBaseMethod';
|
||||
import { Podkop } from '../../types';
|
||||
import { ClashAPI, Podkop } from '../../types';
|
||||
|
||||
export const PodkopShellMethods = {
|
||||
checkDNSAvailable: async () =>
|
||||
@@ -24,4 +24,24 @@ export const PodkopShellMethods = {
|
||||
callBaseMethod<Podkop.GetSingBoxStatus>(
|
||||
Podkop.AvailableMethods.GET_SING_BOX_STATUS,
|
||||
),
|
||||
getClashApiProxies: async () =>
|
||||
callBaseMethod<ClashAPI.Proxies>(Podkop.AvailableMethods.CLASH_API, [
|
||||
Podkop.AvailableClashAPIMethods.GET_PROXIES,
|
||||
]),
|
||||
getClashApiProxyLatency: async (tag: string) =>
|
||||
callBaseMethod<unknown>(Podkop.AvailableMethods.CLASH_API, [
|
||||
Podkop.AvailableClashAPIMethods.GET_PROXY_LATENCY,
|
||||
tag,
|
||||
]),
|
||||
getClashApiGroupLatency: async (tag: string) =>
|
||||
callBaseMethod<unknown>(Podkop.AvailableMethods.CLASH_API, [
|
||||
Podkop.AvailableClashAPIMethods.GET_GROUP_LATENCY,
|
||||
tag,
|
||||
]),
|
||||
setClashApiGroupProxy: async (group: string, proxy: string) =>
|
||||
callBaseMethod<unknown>(Podkop.AvailableMethods.CLASH_API, [
|
||||
Podkop.AvailableClashAPIMethods.SET_GROUP_PROXY,
|
||||
group,
|
||||
proxy,
|
||||
]),
|
||||
};
|
||||
|
||||
@@ -21,7 +21,16 @@ class SocketManager {
|
||||
connect(url: string): void {
|
||||
if (this.sockets.has(url)) return;
|
||||
|
||||
const ws = new WebSocket(url);
|
||||
let ws: WebSocket;
|
||||
|
||||
try {
|
||||
ws = new WebSocket(url);
|
||||
} catch (err) {
|
||||
console.error(`Failed to construct WebSocket for ${url}:`, err);
|
||||
this.triggerError(url, err instanceof Event ? err : String(err));
|
||||
return;
|
||||
}
|
||||
|
||||
this.sockets.set(url, ws);
|
||||
this.connected.set(url, false);
|
||||
this.listeners.set(url, new Set());
|
||||
@@ -58,15 +67,21 @@ class SocketManager {
|
||||
}
|
||||
|
||||
subscribe(url: string, listener: Listener, onError?: ErrorListener): void {
|
||||
if (!this.errorListeners.has(url)) {
|
||||
this.errorListeners.set(url, new Set());
|
||||
}
|
||||
if (onError) {
|
||||
this.errorListeners.get(url)?.add(onError);
|
||||
}
|
||||
|
||||
if (!this.sockets.has(url)) {
|
||||
this.connect(url);
|
||||
}
|
||||
|
||||
this.listeners.get(url)?.add(listener);
|
||||
|
||||
if (onError) {
|
||||
this.errorListeners.get(url)?.add(onError);
|
||||
if (!this.listeners.has(url)) {
|
||||
this.listeners.set(url, new Set());
|
||||
}
|
||||
this.listeners.get(url)?.add(listener);
|
||||
}
|
||||
|
||||
unsubscribe(url: string, listener: Listener, onError?: ErrorListener): void {
|
||||
|
||||
@@ -1,15 +1,10 @@
|
||||
import {
|
||||
getClashApiUrl,
|
||||
getClashWsUrl,
|
||||
onMount,
|
||||
preserveScrollForPage,
|
||||
} from '../../../helpers';
|
||||
import { prettyBytes } from '../../../helpers/prettyBytes';
|
||||
import {
|
||||
ClashMethods,
|
||||
CustomPodkopMethods,
|
||||
PodkopShellMethods,
|
||||
} from '../../methods';
|
||||
import { CustomPodkopMethods, PodkopShellMethods } from '../../methods';
|
||||
import { socket, store, StoreType } from '../../services';
|
||||
import { renderSections, renderWidget } from './partials';
|
||||
|
||||
@@ -28,7 +23,7 @@ async function fetchDashboardSections() {
|
||||
const { data, success } = await CustomPodkopMethods.getDashboardSections();
|
||||
|
||||
if (!success) {
|
||||
console.log('[fetchDashboardSections]: failed to fetch', getClashApiUrl());
|
||||
console.log('[fetchDashboardSections]: failed to fetch');
|
||||
}
|
||||
|
||||
store.set({
|
||||
@@ -148,7 +143,7 @@ async function connectToClashSockets() {
|
||||
// Handlers
|
||||
|
||||
async function handleChooseOutbound(selector: string, tag: string) {
|
||||
await ClashMethods.setProxy(selector, tag);
|
||||
await PodkopShellMethods.setClashApiGroupProxy(selector, tag);
|
||||
await fetchDashboardSections();
|
||||
}
|
||||
|
||||
@@ -160,7 +155,7 @@ async function handleTestGroupLatency(tag: string) {
|
||||
},
|
||||
});
|
||||
|
||||
await ClashMethods.getGroupLatency(tag);
|
||||
await PodkopShellMethods.getClashApiGroupLatency(tag);
|
||||
await fetchDashboardSections();
|
||||
|
||||
store.set({
|
||||
@@ -179,7 +174,7 @@ async function handleTestProxyLatency(tag: string) {
|
||||
},
|
||||
});
|
||||
|
||||
await ClashMethods.getProxyLatency(tag);
|
||||
await PodkopShellMethods.getClashApiProxyLatency(tag);
|
||||
await fetchDashboardSections();
|
||||
|
||||
store.set({
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Podkop } from '../../../types';
|
||||
import { getClashApiUrl } from '../../../../helpers';
|
||||
|
||||
interface IRenderSectionsProps {
|
||||
loading: boolean;
|
||||
@@ -17,10 +16,7 @@ function renderFailedState() {
|
||||
class: 'pdk_dashboard-page__outbound-section centered',
|
||||
style: 'height: 127px',
|
||||
},
|
||||
E('span', {}, [
|
||||
E('span', {}, _('Dashboard currently unavailable')),
|
||||
E('div', { style: 'text-align: center;' }, `API: ${getClashApiUrl()}`),
|
||||
]),
|
||||
E('span', {}, [E('span', {}, _('Dashboard currently unavailable'))]),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,14 @@ export namespace Podkop {
|
||||
GET_STATUS = 'get_status',
|
||||
CHECK_SING_BOX = 'check_sing_box',
|
||||
GET_SING_BOX_STATUS = 'get_sing_box_status',
|
||||
CLASH_API = 'clash_api',
|
||||
}
|
||||
|
||||
export enum AvailableClashAPIMethods {
|
||||
GET_PROXIES = 'get_proxies',
|
||||
GET_PROXY_LATENCY = 'get_proxy_latency',
|
||||
GET_GROUP_LATENCY = 'get_group_latency',
|
||||
SET_GROUP_PROXY = 'set_group_proxy',
|
||||
}
|
||||
|
||||
export interface Outbound {
|
||||
|
||||
Reference in New Issue
Block a user