Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removed backwards compatibility check #153

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/lib/common/Stores.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
import { onMount } from 'svelte';
import { deviceSortStore, deviceSortDirectionStore, userSortStore, sortDirectionStore, themeStore, showACLPagesStore, APIMachineOrNode } from '$lib/common/stores.js';
import { deviceSortStore, deviceSortDirectionStore, userSortStore, sortDirectionStore, themeStore, showACLPagesStore} from '$lib/common/stores.js';
import { URLStore } from '$lib/common/stores.js';
import { APIKeyStore } from '$lib/common/stores.js';
import { preAuthHideStore } from '$lib/common/stores.js';
Expand Down Expand Up @@ -28,9 +28,6 @@
URLStore.subscribe((val) => localStorage.setItem('headscaleURL', val.replace(/\/+$/, '')));
APIKeyStore.set(localStorage.getItem('headscaleAPIKey') || '');
APIKeyStore.subscribe((val) => localStorage.setItem('headscaleAPIKey', val));
// stores api version compatibility info
APIMachineOrNode.set(localStorage.getItem('headscaleAPIMachineOrNode') || 'machine');
APIMachineOrNode.subscribe((val) => localStorage.setItem('headscaleAPIMachineOrNode', val));

// stores whether preauthkeys get hidden when expired/used
preAuthHideStore.set((localStorage.getItem('headscalePreAuthHide') || 'false') == 'true');
Expand Down
55 changes: 8 additions & 47 deletions src/lib/common/apiFunctions.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script context="module" lang="ts">
import { APIKey, Device, PreAuthKey, User } from '$lib/common/classes';
import { deviceStore, userStore, apiTestStore, APIMachineOrNode } from '$lib/common/stores.js';
import { deviceStore, userStore, apiTestStore} from '$lib/common/stores.js';
import { sortDevices, sortUsers } from '$lib/common/sorting.svelte';
import { filterDevices, filterUsers } from './searching.svelte';

Expand Down Expand Up @@ -152,16 +152,14 @@
}

export async function updateTags(deviceID: string, tags: string[]): Promise<any> {
// test the API routes whether we should try to use 'machines' or 'nodes'
await testMachineOrNode();

// variables in local storage
let headscaleURL = localStorage.getItem('headscaleURL') || '';
let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || '';
let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine';

// endpoint url for editing users
let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}/${deviceID}/tags`;
let endpointURL = `/api/v1/node/${deviceID}/tags`;

await fetch(headscaleURL + endpointURL, {
method: 'POST',
Expand Down Expand Up @@ -248,43 +246,14 @@
});
}

export async function testMachineOrNode() {
// variables in local storage
let headscaleURL = localStorage.getItem('headscaleURL') || '';
let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || '';
let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine';

// endpoint url for getting devices
let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}`;
await fetch(headscaleURL + endpointURL, {
method: 'GET',
headers: {
Accept: 'application/json',
Authorization: `Bearer ${headscaleAPIKey}`
}
}).then((response) => {
if (!response.ok) {
// set APIMachineOrNode to the opposite value
if (headscaleAPIMachineOrNode == 'machine') {
APIMachineOrNode.set('node');
} else {
APIMachineOrNode.set('machine');
}
}
});
}

export async function getDevices(): Promise<any> {
// test the API routes whether we should try to use 'machines' or 'nodes'
await testMachineOrNode();

// variables in local storage
let headscaleURL = localStorage.getItem('headscaleURL') || '';
let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || '';
let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine';

// endpoint url for getting devices
let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}`;
let endpointURL = `/api/v1/node`;

//returning variables
let headscaleDevices = [new Device()];
Expand Down Expand Up @@ -315,7 +284,7 @@
});

await headscaleDeviceResponse.json().then((data) => {
headscaleDevices = data[`${headscaleAPIMachineOrNode}s`];
headscaleDevices = data[`nodes`];
headscaleDevices = sortDevices(headscaleDevices);
});
// set the stores
Expand Down Expand Up @@ -467,16 +436,14 @@
}

export async function newDevice(key: string, userName: string): Promise<any> {
// test the API routes whether we should try to use 'machines' or 'nodes'
await testMachineOrNode();

// variables in local storage
let headscaleURL = localStorage.getItem('headscaleURL') || '';
let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || '';
let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine';

// endpoint url for editing users
let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}/register`;
let endpointURL = `/api/v1/node/register`;

await fetch(headscaleURL + endpointURL + '?user=' + userName + '&key=' + key, {
method: 'POST',
Expand All @@ -500,16 +467,14 @@
}

export async function moveDevice(deviceID: string, user: string): Promise<any> {
// test the API routes whether we should try to use 'machines' or 'nodes'
await testMachineOrNode();

// variables in local storage
let headscaleURL = localStorage.getItem('headscaleURL') || '';
let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || '';
let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine';

// endpoint url for editing users
let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}/${deviceID}/user?user=${user}`;
let endpointURL = `/api/v1/node/${deviceID}/user?user=${user}`;

await fetch(headscaleURL + endpointURL, {
method: 'POST',
Expand All @@ -533,16 +498,14 @@
}

export async function renameDevice(deviceID: string, name: string): Promise<any> {
// test the API routes whether we should try to use 'machines' or 'nodes'
await testMachineOrNode();

// variables in local storage
let headscaleURL = localStorage.getItem('headscaleURL') || '';
let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || '';
let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine';

// endpoint url for editing users
let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}/${deviceID}/rename/${name}`;
let endpointURL = `/api/v1/node/${deviceID}/rename/${name}`;

await fetch(headscaleURL + endpointURL, {
method: 'POST',
Expand All @@ -566,16 +529,14 @@
}

export async function removeDevice(deviceID: string): Promise<any> {
// test the API routes whether we should try to use 'machines' or 'nodes'
await testMachineOrNode();

// variables in local storage
let headscaleURL = localStorage.getItem('headscaleURL') || '';
let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || '';
let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine';

// endpoint url for removing devices
let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}/${deviceID}`;
let endpointURL = `/api/v1/node/${deviceID}`;

await fetch(headscaleURL + endpointURL, {
method: 'DELETE',
Expand Down
2 changes: 0 additions & 2 deletions src/lib/common/stores.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ export const themeStore = writable('');
// stores URL and API Key
export const URLStore = writable('');
export const APIKeyStore = writable('');
// stores the type of device api call made for version compatibility
export const APIMachineOrNode = writable('machine');
// stores sorting preferences
export const deviceSortStore = writable('id');
export const deviceSortDirectionStore = writable('ascending');
Expand Down
3 changes: 0 additions & 3 deletions src/lib/devices/DeviceCard/DeviceRoutesAPI.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
<script context="module" lang="ts">
import type { Route } from '$lib/common/classes';
import { testMachineOrNode } from '$lib/common/apiFunctions.svelte'

export async function getDeviceRoutes(deviceID: string): Promise<Route[]> {
// test the API routes whether we should try to use 'machines' or 'nodes'
await testMachineOrNode();

// variables in local storage
let headscaleURL = localStorage.getItem('headscaleURL') || '';
Expand Down