Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ip validation #2987

Merged
merged 6 commits into from
Jun 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions packages/playground/src/dashboard/components/add_ip.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
></v-select>
<input-validator
:value="publicIP"
:rules="[validators.required('IP is required.'), validators.isIPRange('Not a valid IP'), ipcheck]"
:rules="[validators.required('IP is required.'), validators.isIPRange('Not a valid IP')]"
:async-rules="[ipcheck]"
#="{ props }"
>
<input-tooltip tooltip="IP address in CIDR format xxx.xxx.xxx.xxx/xx">
Expand All @@ -35,7 +36,8 @@
<input-validator
v-if="type === IPType.range"
:value="toPublicIP"
:rules="[validators.required('IP is required.'), validators.isIPRange('Not a valid IP'), toIpCheck]"
:rules="[validators.required('IP is required.'), validators.isIPRange('Not a valid IP')]"
:async-rules="[toIpCheck]"
#="{ props }"
>
<input-tooltip tooltip="IP address in CIDR format xxx.xxx.xxx.xxx/xx">
Expand Down Expand Up @@ -106,6 +108,7 @@ import { getIPRange } from "get-ip-range";
import { default as PrivateIp } from "private-ip";
import { ref, watch } from "vue";

import { gqlClient } from "@/clients";
import { IPType } from "@/utils/types";

import { useGrid } from "../../stores";
Expand Down Expand Up @@ -146,12 +149,19 @@ export default {
{ deep: true },
);

function ipcheck() {
async function ipcheck() {
if (PrivateIp(publicIP.value.split("/")[0])) {
return {
message: "IP is not public",
};
}

if (await IpExistsCheck(publicIP.value)) {
return {
message: "IP exists.",
};
}

return undefined;
}

Expand All @@ -162,8 +172,11 @@ export default {
},
{ deep: true },
);

function toIpCheck() {
async function IpExistsCheck(pubIp: string) {
const ips = await gqlClient.publicIps({ ip: true }, { where: { ip_eq: pubIp } });
return ips.length > 0;
}
async function toIpCheck() {
if (toPublicIP.value.split("/")[1] !== publicIP.value.split("/")[1]) {
return {
message: "Subnet is different.",
Expand Down Expand Up @@ -201,6 +214,11 @@ export default {
message: "IP is not public.",
};
}
if (await IpExistsCheck(toPublicIP.value)) {
return {
message: "IP exists.",
};
}
return undefined;
}

Expand Down
Loading