Skip to content

Commit

Permalink
wip update existing instance
Browse files Browse the repository at this point in the history
  • Loading branch information
filipslezaklab committed Sep 21, 2023
1 parent 02ff6a3 commit 62e0296
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
11 changes: 5 additions & 6 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,17 @@ pub async fn all_instances(app_state: State<'_, AppState>) -> Result<Vec<Instanc
let connected = connection_ids
.iter()
.any(|item1| location_ids.iter().any(|item2| item1 == item2));
let keys = WireguardKeys::find_by_instance_id(&app_state.get_pool(), instance.id.unwrap())
.await
.map_err(|err| err.to_string())?
.unwrap();
instance_info.push(InstanceInfo {
id: instance.id,
uuid: instance.uuid.clone(),
name: instance.name.clone(),
url: instance.url.clone(),
connected,
pubkey: keys.pubkey,
})
}
Ok(instance_info)
Expand All @@ -157,7 +162,6 @@ pub struct LocationInfo {
pub address: String,
pub endpoint: String,
pub active: bool,
pub pubkey: String,
}

#[tauri::command(async)]
Expand All @@ -176,10 +180,6 @@ pub async fn all_locations(
.map(|con| con.location_id)
.collect();
let mut location_info = vec![];
let keys = WireguardKeys::find_by_instance_id(&app_state.get_pool(), instance_id)
.await
.map_err(|err| err.to_string())?
.unwrap();
for location in locations {
let info = LocationInfo {
id: location.id.unwrap(),
Expand All @@ -188,7 +188,6 @@ pub async fn all_locations(
address: location.address,
endpoint: location.endpoint,
active: active_locations_ids.contains(&location.id.unwrap()),
pubkey: keys.pubkey.clone(),
};
location_info.push(info);
}
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/database/models/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ pub struct InstanceInfo {
pub uuid: String,
pub url: String,
pub connected: bool,
pub pubkey: String,
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { shallow } from 'zustand/shallow';
import { useI18nContext } from '../../../../../i18n/i18n-react';
import { ModalWithTitle } from '../../../../../shared/defguard-ui/components/Layout/modals/ModalWithTitle/ModalWithTitle';
import { useAddInstanceModal } from './hooks/useAddInstanceModal';
import { AddInstanceDeviceStep } from './steps/AddInstanceDeviceStep';
import { AddInstanceModalInitStep } from './steps/AddInstanceInitStep';

export const AddInstanceModal = () => {
Expand Down Expand Up @@ -45,4 +46,7 @@ export const AddInstanceModal = () => {
);
};

const steps: ReactNode[] = [<AddInstanceModalInitStep key={0} />];
const steps: ReactNode[] = [
<AddInstanceModalInitStep key={0} />,
<AddInstanceDeviceStep key={1} />,
];
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { zodResolver } from '@hookform/resolvers/zod';
import { useQueryClient } from '@tanstack/react-query';
import { invoke } from '@tauri-apps/api';
import { useMemo, useState } from 'react';
import { SubmitHandler, useForm } from 'react-hook-form';
Expand All @@ -18,6 +19,7 @@ import {
CreateDeviceResponse,
} from '../../../../../../shared/hooks/api/types';
import { generateWGKeys } from '../../../../../../shared/utils/generateWGKeys';
import { clientQueryKeys } from '../../../../query';
import { useAddInstanceModal } from '../hooks/useAddInstanceModal';

type FormFields = {
Expand All @@ -34,6 +36,7 @@ export const AddInstanceDeviceStep = () => {
const toaster = useToaster();
const close = useAddInstanceModal((state) => state.close);
const [isLoading, setIsLoading] = useState(false);
const queryClient = useQueryClient();

const [proxyUrl] = useAddInstanceModal((state) => [state.proxyUrl], shallow);

Expand Down Expand Up @@ -78,6 +81,8 @@ export const AddInstanceDeviceStep = () => {
.then(() => {
setIsLoading(false);
toaster.success(componentLL.messages.success.add());
queryClient.invalidateQueries([clientQueryKeys.getInstances]);
queryClient.invalidateQueries([clientQueryKeys.getLocations]);
close();
})
.catch((e) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { zodResolver } from '@hookform/resolvers/zod';
import dayjs from 'dayjs';
import { isUndefined } from 'lodash-es';

Check failure on line 3 in src/pages/client/components/modals/AddInstanceModal/steps/AddInstanceInitStep.tsx

View workflow job for this annotation

GitHub Actions / lint-web

'isUndefined' is defined but never used
import { useMemo } from 'react';
import { SubmitHandler, useForm } from 'react-hook-form';
import { useNavigate } from 'react-router-dom';
Expand All @@ -16,6 +17,7 @@ import { useToaster } from '../../../../../../shared/defguard-ui/hooks/toasts/us
import { EnrollmentStartResponse } from '../../../../../../shared/hooks/api/types';
import { routes } from '../../../../../../shared/routes';
import { useEnrollmentStore } from '../../../../../enrollment/hooks/store/useEnrollmentStore';
import { clientApi } from '../../../../clientAPI/clientApi';
import { useAddInstanceModal } from '../hooks/useAddInstanceModal';

type FormFields = {
Expand Down Expand Up @@ -93,13 +95,29 @@ export const AddInstanceModalInitStep = () => {
setModalState({ loading: false });
return;
}
res.json().then((r: EnrollmentStartResponse) => {
setModalState({ loading: false });
res.json().then(async (r: EnrollmentStartResponse) => {
// get client registered instances
const clientInstances = await clientApi.getInstances();
const instance = clientInstances.find((i) => i.uuid === r.instance.id);
let proxy_api_url = import.meta.env.DEV ? '' : values.url;
if (proxy_api_url[proxy_api_url.length - 1] === '/') {
proxy_api_url = proxy_api_url.slice(0, -1);
}
proxy_api_url = proxy_api_url + '/api/v1';
setModalState({ loading: false });

if (instance) {
// update already registered instance instead
const instanceInfo = await fetch(`${proxy_api_url}/enrollment/network_info`, {

Check failure on line 111 in src/pages/client/components/modals/AddInstanceModal/steps/AddInstanceInitStep.tsx

View workflow job for this annotation

GitHub Actions / lint-web

'instanceInfo' is assigned a value but never used
method: 'POST',
headers,
body: JSON.stringify({
pubKey: instance.pubkey,
}),
});
return;
}
// register new instance
// is user in need of full enrollment ?
if (r.user.is_active) {
//no, only create new device for desktop client
Expand Down
2 changes: 1 addition & 1 deletion src/pages/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type DefguardInstance = {
name: string;
url: string;
connected: boolean;
pubkey: string;
};

export type DefguardLocation = {
Expand All @@ -12,7 +13,6 @@ export type DefguardLocation = {
network_id: number;
name: string;
address: string;
pubkey: string;
endpoint: string;
allowed_ips: string;
};

0 comments on commit 62e0296

Please sign in to comment.