Skip to content

Commit

Permalink
Merge pull request #51 from Honeybrain/GetBlockCountry
Browse files Browse the repository at this point in the history
Get block country
  • Loading branch information
valentinbreiz authored Jan 30, 2024
2 parents f6bde51 + 10fce15 commit 1784dc3
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 6 deletions.
58 changes: 57 additions & 1 deletion src/components/dashboard/BlockManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import { NightModeContext } from '@contexts/NightModeContext';
import { useContext } from "react";

const BlockManager = () => {
const { blacklist, putBlackList, putWhiteList, blockCountry } = useBlackListRPC();
const { blacklist, putBlackList, putWhiteList, blockCountry, getBlockedCountries } = useBlackListRPC();
const [ip, setIp] = React.useState('');
const [country, setCountry] = React.useState('');
const [open, setOpen] = React.useState(false);
const [alertText, setAlertText] = React.useState('');
const [blockedCountries, setBlockedCountries] = React.useState<string[]>([]);
const { t } = useTranslation();
const { isNightMode } = useContext(NightModeContext);
const nightModeBgColor1 = 'color1ForNightMode';
Expand Down Expand Up @@ -51,18 +52,46 @@ const BlockManager = () => {
}
};

// React.useEffect(() => {
// const fetchBlockedCountries = async () => {
// try {
// const countries = await getBlockedCountries();
// setBlockedCountries(countries);
// } catch (error) {
// console.error('Error fetching blocked countries:', error);
// }
// };

// fetchBlockedCountries();
// }, [getBlockedCountries]);

const refreshBlockedCountries = async () => {
try {
const countries = await getBlockedCountries();
setBlockedCountries(countries);
} catch (error) {
console.error('Error fetching blocked countries:', error);
}
};

const handleBlockCountry = async (e) => {
e.preventDefault();
try {
await blockCountry(country);
setCountry('');
setAlertText(t('blockManager.blockCountrySuccess'));
setOpen(true);
refreshBlockedCountries();
} catch (error) {
console.error(error);
}
};

React.useEffect(() => {
refreshBlockedCountries();
}, [refreshBlockedCountries]);


return (
<Grid container direction="column">
<Grid item>
Expand Down Expand Up @@ -98,6 +127,33 @@ const BlockManager = () => {
</Grid>
</Grid>
</Grid>
<Grid item xs>
<Typography variant="h6" component="h2" gutterBottom mt={2}>
{t('blockManager.currentlyBlockedCountries')}
</Typography>
<List>
{blockedCountries.length > 0 ? (
blockedCountries.map((country, index) => (
<ListItem key={index} sx={{
my: 1,
px: 2,
bgcolor: isNightMode
? (index % 2 === 0 ? nightModeBgColor1 : nightModeBgColor2)
: (index % 2 === 0 ? 'action.hover' : 'background.default'),
borderRadius: 1
}}>
<ListItemText primary={country} />
{/* Ajouter des actions pour chaque pays si nécessaire */}
</ListItem>
))
) : (
<Typography sx={{ px: 2 }}>{t('blockManager.noBlockedCountries')}</Typography>
)}
</List>
</Grid>

{/* Séparateur */}
<Divider sx={{ my: 4 }} />
<Grid item xs sx={{ marginBottom: 0.4 }}>
<Typography variant="h6" mb={2}>{t('blockManager.blockAnIP')}</Typography>
<Grid container spacing={2} direction="column" alignItems="stretch" component="form" onSubmit={handleSubmit}>
Expand Down
15 changes: 14 additions & 1 deletion src/hooks/backend/honeypotService/useBlackListRPC.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import { transport } from "../../../environment";
import { BlacklistClient } from '@protos/blacklist.client';
import { GetBlackListRequest, PutWhiteListRequest, PutBlackListRequest, BlockCountryRequest} from '@protos/blacklist';
import { GetBlackListRequest, PutWhiteListRequest, PutBlackListRequest, BlockCountryRequest, GetBlockCountryRequest} from '@protos/blacklist';

const useBlackListRPC = () => {
const client = React.useMemo(() => new BlacklistClient(transport), []);
Expand Down Expand Up @@ -35,6 +35,18 @@ const useBlackListRPC = () => {
await client.blockCountry(request, {});;
}, []);

const getBlockedCountries = React.useCallback(async () => {
const request = GetBlockCountryRequest.create();
try {
const call = client.getBlockCountry(request, {});
const response = await call.response;
return response.countries;
} catch (error) {
console.error('Error fetching blocked countries:', error);
throw error;
}
}, [client]);

React.useEffect(() => {
getBlackList();

Expand All @@ -48,6 +60,7 @@ const useBlackListRPC = () => {
blacklist,
putBlackList,
putWhiteList,
getBlockedCountries,
};
};

Expand Down
4 changes: 3 additions & 1 deletion src/locales/ch/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@
"blockCountrySuccess": "成功阻止国家",
"blockACountry": "阻止一个国家",
"blockCountry": "阻止国家",
"countryCode": "国家代码"
"countryCode": "国家代码",
"currentlyBlockedCountries": "目前被封鎖的國家",
"noBlockedCountries": "尚未有國家被封鎖"
},
"listConnections": {
"title": "传入连接",
Expand Down
4 changes: 3 additions & 1 deletion src/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@
"blockCountrySuccess": "Country successfully blocked",
"blockACountry": "Block a Country",
"blockCountry": "Block Country",
"countryCode": "Country Code"
"countryCode": "Country Code",
"currentlyBlockedCountries": "Countries currently blocked",
"noBlockedCountries": "No country has yet been blocked."
},
"listConnections": {
"title": "Incoming Connections",
Expand Down
4 changes: 3 additions & 1 deletion src/locales/es/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@
"blockCountrySuccess": "País bloqueado con éxito",
"blockACountry": "Bloquear un País",
"blockCountry": "Bloquear País",
"countryCode": "Código del País"
"countryCode": "Código del País",
"currentlyBlockedCountries": "Países actualmente bloqueados",
"noBlockedCountries": "Ningún país ha sido bloqueado todavía."
},
"listConnections": {
"title": "Conexiones entrantes",
Expand Down
4 changes: 3 additions & 1 deletion src/locales/fr/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@
"blockCountrySuccess": "Pays bloqué avec succès",
"blockACountry": "Bloquer un pays",
"blockCountry": "Bloquer le pays",
"countryCode": "Code du pays"
"countryCode": "Code du pays",
"currentlyBlockedCountries": "Pays actuellement bloqués",
"noBlockedCountries": "Aucun pays n'a pour l'instant été bloqué."
},
"listConnections": {
"title": "Connexions entrantes",
Expand Down

0 comments on commit 1784dc3

Please sign in to comment.