|
@ -1,104 +1,51 @@ |
|
|
export type APIClient = { |
|
|
|
|
|
id: string; |
|
|
|
|
|
name: string; |
|
|
|
|
|
enabled: boolean; |
|
|
|
|
|
address: string; |
|
|
|
|
|
publicKey: string; |
|
|
|
|
|
createdAt: string; |
|
|
|
|
|
updatedAt: string; |
|
|
|
|
|
downloadableConfig: boolean; |
|
|
|
|
|
persistentKeepalive: string; |
|
|
|
|
|
latestHandshakeAt: null; |
|
|
|
|
|
transferRx: number; |
|
|
|
|
|
transferTx: number; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
class API { |
|
|
class API { |
|
|
async call({ |
|
|
|
|
|
method, |
|
|
|
|
|
path, |
|
|
|
|
|
body, |
|
|
|
|
|
}: { |
|
|
|
|
|
method: string; |
|
|
|
|
|
path: string; |
|
|
|
|
|
body?: Record<string, unknown>; |
|
|
|
|
|
}) { |
|
|
|
|
|
const res = await fetch(`./api${path}`, { |
|
|
|
|
|
method, |
|
|
|
|
|
headers: { |
|
|
|
|
|
'Content-Type': 'application/json', |
|
|
|
|
|
}, |
|
|
|
|
|
body: body ? JSON.stringify(body) : undefined, |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if (res.status === 204) { |
|
|
|
|
|
return undefined; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const json = await res.json(); |
|
|
|
|
|
|
|
|
|
|
|
if (!res.ok) { |
|
|
|
|
|
throw new Error(json.message || res.statusText); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return json; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async getRelease() { |
|
|
async getRelease() { |
|
|
return this.call({ |
|
|
return $fetch('/api/release', { |
|
|
method: 'get', |
|
|
method: 'get', |
|
|
path: '/release', |
|
|
|
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async getLang() { |
|
|
async getLang() { |
|
|
return this.call({ |
|
|
return $fetch('/api/lang', { |
|
|
method: 'get', |
|
|
method: 'get', |
|
|
path: '/lang', |
|
|
|
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async getuiTrafficStats() { |
|
|
async getUITrafficStats() { |
|
|
return this.call({ |
|
|
return $fetch('/api/ui-traffic-stats', { |
|
|
method: 'get', |
|
|
method: 'get', |
|
|
path: '/ui-traffic-stats', |
|
|
|
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async getChartType() { |
|
|
async getChartType() { |
|
|
return this.call({ |
|
|
return $fetch('/api/ui-chart-type', { |
|
|
method: 'get', |
|
|
method: 'get', |
|
|
path: '/ui-chart-type', |
|
|
|
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async getSession() { |
|
|
async getSession() { |
|
|
return this.call({ |
|
|
return $fetch('/api/session', { |
|
|
method: 'get', |
|
|
method: 'get', |
|
|
path: '/session', |
|
|
|
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async createSession({ password }: { password: string | null }) { |
|
|
async createSession({ password }: { password: string | null }) { |
|
|
return this.call({ |
|
|
return $fetch('/api/session', { |
|
|
method: 'post', |
|
|
method: 'post', |
|
|
path: '/session', |
|
|
|
|
|
body: { password }, |
|
|
body: { password }, |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async deleteSession() { |
|
|
async deleteSession() { |
|
|
return this.call({ |
|
|
return $fetch('/api/session', { |
|
|
method: 'delete', |
|
|
method: 'delete', |
|
|
path: '/session', |
|
|
|
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async getClients() { |
|
|
async getClients() { |
|
|
return this.call({ |
|
|
return $fetch('/api/wireguard/client', { |
|
|
method: 'get', |
|
|
method: 'get', |
|
|
path: '/wireguard/client', |
|
|
}).then((clients) => |
|
|
}).then((clients: APIClient[]) => |
|
|
|
|
|
clients.map((client) => ({ |
|
|
clients.map((client) => ({ |
|
|
...client, |
|
|
...client, |
|
|
createdAt: new Date(client.createdAt), |
|
|
createdAt: new Date(client.createdAt), |
|
@ -112,31 +59,27 @@ class API { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async createClient({ name }: { name: string }) { |
|
|
async createClient({ name }: { name: string }) { |
|
|
return this.call({ |
|
|
return $fetch('/api/wireguard/client', { |
|
|
method: 'post', |
|
|
method: 'POST', |
|
|
path: '/wireguard/client', |
|
|
|
|
|
body: { name }, |
|
|
body: { name }, |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async deleteClient({ clientId }: { clientId: string }) { |
|
|
async deleteClient({ clientId }: { clientId: string }) { |
|
|
return this.call({ |
|
|
return $fetch(`/api/wireguard/client/${clientId}`, { |
|
|
method: 'delete', |
|
|
method: 'DELETE', |
|
|
path: `/wireguard/client/${clientId}`, |
|
|
|
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async enableClient({ clientId }: { clientId: string }) { |
|
|
async enableClient({ clientId }: { clientId: string }) { |
|
|
return this.call({ |
|
|
return $fetch(`/api/wireguard/client/${clientId}/enable`, { |
|
|
method: 'post', |
|
|
method: 'POST', |
|
|
path: `/wireguard/client/${clientId}/enable`, |
|
|
|
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async disableClient({ clientId }: { clientId: string }) { |
|
|
async disableClient({ clientId }: { clientId: string }) { |
|
|
return this.call({ |
|
|
return $fetch(`/api/wireguard/client/${clientId}/disable`, { |
|
|
method: 'post', |
|
|
method: 'POST', |
|
|
path: `/wireguard/client/${clientId}/disable`, |
|
|
|
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -147,9 +90,8 @@ class API { |
|
|
clientId: string; |
|
|
clientId: string; |
|
|
name: string; |
|
|
name: string; |
|
|
}) { |
|
|
}) { |
|
|
return this.call({ |
|
|
return $fetch(`/api/wireguard/client/${clientId}/name`, { |
|
|
method: 'put', |
|
|
method: 'PUT', |
|
|
path: `/wireguard/client/${clientId}/name/`, |
|
|
|
|
|
body: { name }, |
|
|
body: { name }, |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
@ -161,17 +103,15 @@ class API { |
|
|
clientId: string; |
|
|
clientId: string; |
|
|
address: string; |
|
|
address: string; |
|
|
}) { |
|
|
}) { |
|
|
return this.call({ |
|
|
return $fetch(`/api/wireguard/client/${clientId}/address`, { |
|
|
method: 'put', |
|
|
method: 'PUT', |
|
|
path: `/wireguard/client/${clientId}/address/`, |
|
|
|
|
|
body: { address }, |
|
|
body: { address }, |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
async restoreConfiguration(file: string) { |
|
|
async restoreConfiguration(file: string) { |
|
|
return this.call({ |
|
|
return $fetch('/api/wireguard/restore', { |
|
|
method: 'put', |
|
|
method: 'PUT', |
|
|
path: '/wireguard/restore', |
|
|
|
|
|
body: { file }, |
|
|
body: { file }, |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|