diff --git a/src/components/dashboard/Others.tsx b/src/components/dashboard/Others.tsx index 10d8c97..b2eff3b 100644 --- a/src/components/dashboard/Others.tsx +++ b/src/components/dashboard/Others.tsx @@ -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('/'); @@ -50,8 +51,8 @@ const getTextFieldStyles = (isNightMode) => ({ }); - const Others = () => { + const { reconfigure } = useReconfigRPC(); const [dummyPcNumServices, setDummyPcNumServices] = useState(2); const [ftpIPAddress, setFtpIPAddress] = useState('192.168.1.10'); const [ftpPort, setFtpPort] = useState('21'); @@ -81,7 +82,7 @@ const Others = () => { setDummyPcIPAddresses(updatedIPAddresses); }; - const handleDownload = () => { + const handleReconfig = async (e) => { const configData = { dummy_pc: { num_services: dummyPcNumServices, @@ -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 ( @@ -210,7 +219,7 @@ const Others = () => { - diff --git a/src/hooks/backend/honeypotService/useReconfigRPC.ts b/src/hooks/backend/honeypotService/useReconfigRPC.ts new file mode 100644 index 0000000..c9c372d --- /dev/null +++ b/src/hooks/backend/honeypotService/useReconfigRPC.ts @@ -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; \ No newline at end of file