Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion packages/ctool-config/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export let _tools = {
feature: ["pinyin"],
parent_directory: "",
},
ipDecimal: {
feature: ["ipDecimal"],
parent_directory: "",
},
ip: {
feature: ["ip"],
parent_directory: "",
Expand Down Expand Up @@ -210,7 +214,7 @@ export const _categoryTool: Record<CategoryType, ToolType[]> = {
encryption: ["hash", "hmac", "aes", "des", "tripleDes", "rc4", "rabbit", "sm2", "sm4", "rsa", "sign", "base64", "bcrypt"],
check: ["sign", "regex", "diffs", "crontab", "bcrypt", "dataValidation"],
encoder_decoder: ["base64", "url", "unicode", "jwt", "hexString", "html", "gzip", "asn1", "punycode"],
conversion: ["json", "pinyin", "radix", "serialize", "unit", "time", "ascii", "variableConversion", "hexString", "arm", "httpSnippet", "color", "urlParse", "dockerCompose", "zhNumber"],
conversion: ["json", "pinyin", "ipDecimal", "radix", "serialize", "unit", "time", "ascii", "variableConversion", "hexString", "arm", "httpSnippet", "color", "urlParse", "dockerCompose", "zhNumber"],
generate: ["qrCode", "barcode", "randomString", "uuid", "binary", "ipcalc", "sqlFillParameter", "httpSnippet"],
other: ["ip", "code", "websocket", "unit", "text"],
};
Expand Down
3 changes: 3 additions & 0 deletions packages/ctool-core/src/i18n/locales/en/tool.i18n.json5
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
"qrCode_generate": "Generate",
"qrCode_parse": "Parse",
"pinyin": "Chinese Pinyin",
"ipDecimal": "Decimal IPv4 Converter",
"ipDecimal_ipDecimal": "Decimal IPv4 Converter",
"ipDecimal_ipDecimal_keywords": "ip,ipv4,decimal,dotted",
"ip": "Ip Query",
"code": "Code",
"code_code": "Formatter",
Expand Down
3 changes: 3 additions & 0 deletions packages/ctool-core/src/i18n/locales/zh_CN/tool.i18n.json5
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
"qrCode_parse": "解析",
"barcode": "条形码",
"pinyin": "汉字转拼音",
"ipDecimal": "十进制 IPv4 转换",
"ipDecimal_ipDecimal": "十进制 IPv4 转换",
"ipDecimal_ipDecimal_keywords": "ip,ipv4,十进制,整型,转换",
"ip": "IP地址查询",
"code": "代码",
"code_code": "格式化",
Expand Down
115 changes: 115 additions & 0 deletions packages/ctool-core/src/tools/ipDecimal/IpDecimal.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<template>
<HeightResize v-slot="{height}" v-row="'1-1'">
<Display position="top-right">
<Textarea
:height="height"
v-model="decimalValue"
:placeholder="$t('ipDecimal_decimal_placeholder')"
:copy="false"
/>
</Display>
<Display position="top-right">
<Textarea
:height="height"
v-model="dottedValue"
:placeholder="$t('ipDecimal_ipv4_placeholder')"
:copy="false"
/>
</Display>
</HeightResize>
</template>

<script lang="ts" setup>
import {initialize, useAction} from "@/store/action";
import {computed, ref, watch} from "vue";

const action = useAction(await initialize({
decimal: "",
dotted: "",
}, {
paste: (value) => /^[\s0-9.]*$/.test(value),
}))

const MAX_IPV4 = 0xffffffff
const DECIMAL_REG = /^\d+$/
const OCTET_REG = /^(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/

const normalize = (content: string) => content.replace(/\r\n/g, "\n")

const decimalLineToDotted = (line: string) => {
const trimmed = line.trim()
if (trimmed === "") {
return ""
}
if (!DECIMAL_REG.test(trimmed)) {
return $t('ipDecimal_invalid_decimal', [trimmed])
}
const numeric = Number.parseInt(trimmed, 10)
if (!Number.isFinite(numeric) || !Number.isSafeInteger(numeric) || numeric < 0 || numeric > MAX_IPV4) {
return $t('ipDecimal_invalid_decimal', [trimmed])
}
const value = numeric >>> 0
return [
(value >>> 24) & 0xff,
(value >>> 16) & 0xff,
(value >>> 8) & 0xff,
value & 0xff,
].join('.')
}

const dottedLineToDecimal = (line: string) => {
const trimmed = line.trim()
if (trimmed === "") {
return ""
}
const parts = trimmed.split('.')
if (parts.length !== 4 || parts.some((part) => !OCTET_REG.test(part))) {
return $t('ipDecimal_invalid_ipv4', [trimmed])
}
const [a, b, c, d] = parts.map((part) => Number.parseInt(part, 10))
const value = ((a * 256 + b) * 256 + c) * 256 + d
if (!Number.isFinite(value) || value < 0 || value > MAX_IPV4) {
return $t('ipDecimal_invalid_ipv4', [trimmed])
}
return `${value}`
}

const convertDecimalBlock = (content: string) => normalize(content).split("\n").map(decimalLineToDotted).join("\n")
const convertDottedBlock = (content: string) => normalize(content).split("\n").map(dottedLineToDecimal).join("\n")

const lastEdited = ref<"decimal" | "dotted" | "">("")

const decimalValue = computed({
get: () => action.current.decimal,
set: (value: string) => {
lastEdited.value = "decimal"
action.current.decimal = value
}
})

const dottedValue = computed({
get: () => action.current.dotted,
set: (value: string) => {
lastEdited.value = "dotted"
action.current.dotted = value
}
})

watch(() => action.current.decimal, (value) => {
if (lastEdited.value !== "decimal") {
return
}
action.current.dotted = convertDecimalBlock(value)
lastEdited.value = ""
action.save()
})

watch(() => action.current.dotted, (value) => {
if (lastEdited.value !== "dotted") {
return
}
action.current.decimal = convertDottedBlock(value)
lastEdited.value = ""
action.save()
})
</script>
6 changes: 6 additions & 0 deletions packages/ctool-core/src/tools/ipDecimal/i18n/en.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
decimal_placeholder: "Enter decimal IPv4 (one per line)",
ipv4_placeholder: "Enter dotted IPv4 (one per line)",
invalid_decimal: "Invalid decimal IPv4: {0}",
invalid_ipv4: "Invalid dotted IPv4: {0}"
}
6 changes: 6 additions & 0 deletions packages/ctool-core/src/tools/ipDecimal/i18n/zh_CN.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
decimal_placeholder: "输入十进制 IPv4(每行一个)",
ipv4_placeholder: "输入点分 IPv4(每行一个)",
invalid_decimal: "无效的十进制 IPv4: {0}",
invalid_ipv4: "无效的点分 IPv4: {0}"
}