Skip to content

Commit

Permalink
refactor: move network interface detection to main process for improv…
Browse files Browse the repository at this point in the history
…ed security/reliability
  • Loading branch information
sajjadmrx committed Sep 24, 2023
1 parent d647b15 commit e2488f8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
3 changes: 3 additions & 0 deletions src/main/ipc/dialogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ ipcMain.handle(EventsKeys.PING, async function (event, server: Server) {
}
});

ipcMain.handle(EventsKeys.GET_NETWORK_INTERFACE_LIST, () => {
return dnsService.getInterfacesList();
});
function getCurrentLng(): Locales {
return store.get("settings").lng;
}
Expand Down
3 changes: 2 additions & 1 deletion src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export const uiPreload = {

export const osItems = {
os: os.platform(),
getInterfaces: () => os.networkInterfaces(),
getInterfaces: () =>
ipcRenderer.invoke(EventsKeys.GET_NETWORK_INTERFACE_LIST),
};

// Todo use 'set' in client
Expand Down
50 changes: 33 additions & 17 deletions src/renderer/component/modals/network-options.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,35 @@ export function NetworkOptionsModalComponent(props: Props) {
const { setNetwork, network } = useContext<ServersContext>(serversContext);

const handleOpen = () => props.setIsOpen((cur) => !cur);

const [loading, setLoading] = useState<boolean>(true);

const [networkInterface, setNetworkInterfaceInterface] = useState<string>();

const [networkAdapters, setNetworkAdapters] = useState<string[]>([]);
// const [currentNetwork, setCurrentNetwork] = useState<string>("Auto");

useEffect(() => {
if (props.isOpen) {
const current = window.storePreload.get("settings").network_interface;
//setCurrentNetwork(current);

setNetwork(current);
const interfaces = window.os.getInterfaces();
const networks = [...Object.keys(interfaces)];
networks.unshift("Auto");

setNetworkAdapters(networks);
window.os
.getInterfaces()
.then((interfaces) => {
const networks = interfaces.map((d) => d.name);
networks.unshift("Auto");
setNetworkAdapters(networks);
})
.finally(() => setLoading(false));
}
}, [props.isOpen]);

useEffect(() => {
if (networkInterface) {
settingStore.network_interface = networkInterface;
window.ipc.saveSettings(settingStore).catch();

setNetwork(networkInterface);
}
}, [networkInterface]);
Expand Down Expand Up @@ -80,17 +89,24 @@ export function NetworkOptionsModalComponent(props: Props) {
</div>

<div>
<Select
onChange={(value) =>
setNetworkInterfaceInterface(value.target.value)
}
>
{networkAdapters.map((item) => (
<Select.Option value={item} selected={item == network}>
{item}
</Select.Option>
))}
</Select>
{loading ? (
<div className={"flex flex-row gap-2"}>
<span className="loading loading-ring loading-xs"></span>
fetching interfaces...
</div>
) : (
<Select
onChange={(value) =>
setNetworkInterfaceInterface(value.target.value)
}
>
{networkAdapters.map((item) => (
<Select.Option value={item} selected={item == network}>
{item}
</Select.Option>
))}
</Select>
)}
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/shared/constants/eventsKeys.constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export enum EventsKeys {
TOGGLE_THEME = "ui:toggleTheme",
GET_SETTINGS = "setting:get",
SET_NETWORK_INTERFACE = "setting:set_network_interface",
GET_NETWORK_INTERFACE = "setting:get_network_interface",
GET_NETWORK_INTERFACE_LIST = "setting:get_network_interface_list",
SAVE_SETTINGS = "setting:save",
TOGGLE_START_UP = "setting:toggleStartUp",
FLUSHDNS = "dialogs:flushDns",
Expand Down

0 comments on commit e2488f8

Please sign in to comment.