Skip to content

Commit

Permalink
Merge pull request #53 from Honeybrain/reconfig
Browse files Browse the repository at this point in the history
reconfig
  • Loading branch information
valentinbreiz authored Mar 20, 2024
2 parents 3112254 + 0504ca2 commit 2acd4f8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/components/dashboard/Others.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import HelpModal from '@components/HelpModal';
import { useTranslation } from 'react-i18next';
import { NightModeContext } from '@contexts/NightModeContext';
import { useContext } from "react";
import useReconfigRPC from '@hooks/backend/honeypotService/useReconfigRPC';

const getRandomDummyPcIPAddresses = (subnet: string, numServices: number) => {
const subnetParts = subnet.split('/');
Expand Down Expand Up @@ -50,8 +51,8 @@ const getTextFieldStyles = (isNightMode) => ({
});



const Others = () => {
const { reconfigure } = useReconfigRPC();
const [dummyPcNumServices, setDummyPcNumServices] = useState<number>(2);
const [ftpIPAddress, setFtpIPAddress] = useState<string>('192.168.1.10');
const [ftpPort, setFtpPort] = useState<string>('21');
Expand Down Expand Up @@ -81,7 +82,7 @@ const Others = () => {
setDummyPcIPAddresses(updatedIPAddresses);
};

const handleDownload = () => {
const handleReconfig = async (e) => {
const configData = {
dummy_pc: {
num_services: dummyPcNumServices,
Expand All @@ -96,12 +97,20 @@ const Others = () => {
docker: dockerPath,
};
const dataStr = 'data:text/json;charset=utf-8,' + encodeURIComponent(JSON.stringify(configData, null, 4));
const downloadAnchorNode = document.createElement('a');
/*const downloadAnchorNode = document.createElement('a');
downloadAnchorNode.setAttribute('href', dataStr);
downloadAnchorNode.setAttribute('download', 'honeybrain_config.json');
document.body.appendChild(downloadAnchorNode); // required for Firefox
downloadAnchorNode.click();
downloadAnchorNode.remove();
downloadAnchorNode.remove();*/
e.preventDefault();
try {
console.log(configData)
await reconfigure(JSON.stringify(configData, null, 4));
} catch (error) {
console.error("error is : " + error);
}

};

return (
Expand Down Expand Up @@ -210,7 +219,7 @@ const Others = () => {
</Grid>
<Grid item>
<Box width="100%" display="flex" justifyContent="center">
<Button variant="contained" color="primary" onClick={handleDownload}>
<Button variant="contained" color="primary" onClick={handleReconfig}>
{t('configGenerator.downloadConfiguration')}
</Button>
</Box>
Expand Down
20 changes: 20 additions & 0 deletions src/hooks/backend/honeypotService/useReconfigRPC.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from "react";
import { transport } from "../../../environment";
import { ReconfigureClient } from "@protos/reconfig.client";
import { ReconfigRequest } from '@protos/reconfig';

const useReconfigRPC = () => {
const client = React.useMemo(() => new ReconfigureClient(transport), []);

const reconfigure = React.useCallback(async (config: string) => {
const request: ReconfigRequest = ReconfigRequest.create();
request.config = config;
await client.reconfigHoneypot(request, {});
}, []);

return {
reconfigure,
};
};

export default useReconfigRPC;

0 comments on commit 2acd4f8

Please sign in to comment.