|
1 | 1 | <script context="module" lang="ts"> |
2 | 2 | import { APIKey, Device, PreAuthKey, User } from '$lib/common/classes'; |
3 | | - import { deviceStore, userStore, apiTestStore } from '$lib/common/stores.js'; |
| 3 | + import { deviceStore, userStore, apiTestStore, APIMachineOrNode } from '$lib/common/stores.js'; |
4 | 4 | import { sortDevices, sortUsers } from '$lib/common/sorting.svelte'; |
5 | 5 | import { filterDevices, filterUsers } from './searching.svelte'; |
6 | 6 |
|
|
40 | 40 | }); |
41 | 41 |
|
42 | 42 | await headscaleUsersResponse.json().then((data) => { |
43 | | - headscaleUsers = data.users |
| 43 | + headscaleUsers = data.users; |
44 | 44 | // sort the users |
45 | 45 | headscaleUsers = sortUsers(headscaleUsers); |
46 | 46 | }); |
|
152 | 152 | } |
153 | 153 |
|
154 | 154 | export async function updateTags(deviceID: string, tags: string[]): Promise<any> { |
| 155 | + // test the API routes whether we should try to use 'machines' or 'nodes' |
| 156 | + await testMachineOrNode(); |
| 157 | +
|
155 | 158 | // variables in local storage |
156 | 159 | let headscaleURL = localStorage.getItem('headscaleURL') || ''; |
157 | 160 | let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || ''; |
| 161 | + let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine'; |
158 | 162 |
|
159 | 163 | // endpoint url for editing users |
160 | | - let endpointURL = '/api/v1/machine/' + deviceID + '/tags'; |
| 164 | + let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}/${deviceID}/tags`; |
161 | 165 |
|
162 | 166 | await fetch(headscaleURL + endpointURL, { |
163 | 167 | method: 'POST', |
|
244 | 248 | }); |
245 | 249 | } |
246 | 250 |
|
| 251 | + export async function testMachineOrNode() { |
| 252 | + // variables in local storage |
| 253 | + let headscaleURL = localStorage.getItem('headscaleURL') || ''; |
| 254 | + let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || ''; |
| 255 | + let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine'; |
| 256 | +
|
| 257 | + // endpoint url for getting devices |
| 258 | + let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}`; |
| 259 | + await fetch(headscaleURL + endpointURL, { |
| 260 | + method: 'GET', |
| 261 | + headers: { |
| 262 | + Accept: 'application/json', |
| 263 | + Authorization: `Bearer ${headscaleAPIKey}` |
| 264 | + } |
| 265 | + }).then((response) => { |
| 266 | + if (!response.ok) { |
| 267 | + // set APIMachineOrNode to the opposite value |
| 268 | + if (headscaleAPIMachineOrNode == 'machine') { |
| 269 | + APIMachineOrNode.set('node'); |
| 270 | + } else { |
| 271 | + APIMachineOrNode.set('machine'); |
| 272 | + } |
| 273 | + } |
| 274 | + }); |
| 275 | + } |
| 276 | +
|
247 | 277 | export async function getDevices(): Promise<any> { |
| 278 | + // test the API routes whether we should try to use 'machines' or 'nodes' |
| 279 | + await testMachineOrNode(); |
| 280 | +
|
248 | 281 | // variables in local storage |
249 | 282 | let headscaleURL = localStorage.getItem('headscaleURL') || ''; |
250 | 283 | let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || ''; |
| 284 | + let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine'; |
251 | 285 |
|
252 | | - // endpoint url for getting users |
253 | | - let endpointURL = '/api/v1/machine'; |
| 286 | + // endpoint url for getting devices |
| 287 | + let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}`; |
254 | 288 |
|
255 | 289 | //returning variables |
256 | 290 | let headscaleDevices = [new Device()]; |
|
281 | 315 | }); |
282 | 316 |
|
283 | 317 | await headscaleDeviceResponse.json().then((data) => { |
284 | | - headscaleDevices = data.machines; |
| 318 | + headscaleDevices = data[`${headscaleAPIMachineOrNode}s`]; |
285 | 319 | headscaleDevices = sortDevices(headscaleDevices); |
286 | 320 | }); |
287 | 321 | // set the stores |
|
291 | 325 | filterDevices(); |
292 | 326 | } |
293 | 327 |
|
294 | | -
|
295 | | -
|
296 | 328 | export async function getAPIKeys(): Promise<APIKey[]> { |
297 | 329 | // variables in local storage |
298 | 330 | let headscaleURL = localStorage.getItem('headscaleURL') || ''; |
|
435 | 467 | } |
436 | 468 |
|
437 | 469 | export async function newDevice(key: string, userName: string): Promise<any> { |
| 470 | + // test the API routes whether we should try to use 'machines' or 'nodes' |
| 471 | + await testMachineOrNode(); |
| 472 | +
|
438 | 473 | // variables in local storage |
439 | 474 | let headscaleURL = localStorage.getItem('headscaleURL') || ''; |
440 | 475 | let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || ''; |
| 476 | + let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine'; |
441 | 477 |
|
442 | 478 | // endpoint url for editing users |
443 | | - let endpointURL = '/api/v1/machine/register'; |
| 479 | + let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}/register`; |
444 | 480 |
|
445 | 481 | await fetch(headscaleURL + endpointURL + '?user=' + userName + '&key=' + key, { |
446 | 482 | method: 'POST', |
|
464 | 500 | } |
465 | 501 |
|
466 | 502 | export async function moveDevice(deviceID: string, user: string): Promise<any> { |
| 503 | + // test the API routes whether we should try to use 'machines' or 'nodes' |
| 504 | + await testMachineOrNode(); |
| 505 | +
|
467 | 506 | // variables in local storage |
468 | 507 | let headscaleURL = localStorage.getItem('headscaleURL') || ''; |
469 | 508 | let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || ''; |
| 509 | + let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine'; |
470 | 510 |
|
471 | 511 | // endpoint url for editing users |
472 | | - let endpointURL = '/api/v1/machine/' + deviceID + '/user?user=' + user; |
| 512 | + let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}/${deviceID}/user?user=${user}`; |
473 | 513 |
|
474 | 514 | await fetch(headscaleURL + endpointURL, { |
475 | 515 | method: 'POST', |
|
493 | 533 | } |
494 | 534 |
|
495 | 535 | export async function renameDevice(deviceID: string, name: string): Promise<any> { |
| 536 | + // test the API routes whether we should try to use 'machines' or 'nodes' |
| 537 | + await testMachineOrNode(); |
| 538 | +
|
496 | 539 | // variables in local storage |
497 | 540 | let headscaleURL = localStorage.getItem('headscaleURL') || ''; |
498 | 541 | let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || ''; |
| 542 | + let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine'; |
499 | 543 |
|
500 | 544 | // endpoint url for editing users |
501 | | - let endpointURL = '/api/v1/machine/' + deviceID + '/rename/' + name; |
| 545 | + let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}/${deviceID}/rename/${name}`; |
502 | 546 |
|
503 | 547 | await fetch(headscaleURL + endpointURL, { |
504 | 548 | method: 'POST', |
|
522 | 566 | } |
523 | 567 |
|
524 | 568 | export async function removeDevice(deviceID: string): Promise<any> { |
| 569 | + // test the API routes whether we should try to use 'machines' or 'nodes' |
| 570 | + await testMachineOrNode(); |
| 571 | + |
525 | 572 | // variables in local storage |
526 | 573 | let headscaleURL = localStorage.getItem('headscaleURL') || ''; |
527 | 574 | let headscaleAPIKey = localStorage.getItem('headscaleAPIKey') || ''; |
| 575 | + let headscaleAPIMachineOrNode = localStorage.getItem('headscaleAPIMachineOrNode') || 'machine'; |
528 | 576 |
|
529 | 577 | // endpoint url for removing devices |
530 | | - let endpointURL = '/api/v1/machine/' + deviceID; |
| 578 | + let endpointURL = `/api/v1/${headscaleAPIMachineOrNode}/${deviceID}`; |
531 | 579 |
|
532 | 580 | await fetch(headscaleURL + endpointURL, { |
533 | 581 | method: 'DELETE', |
|
0 commit comments