summaryrefslogtreecommitdiffstats
path: root/server/lib
diff options
context:
space:
mode:
authorJannik Schönartz2019-03-06 03:58:04 +0100
committerJannik Schönartz2019-03-06 03:58:04 +0100
commitbd6758f8951591ca57015ace09314c5d5e116810 (patch)
treeb9d8e679ee54dbffedee13851e50ba61064327ee /server/lib
parent[server/ipranges] Store ipranges as integers in the db. Add converting ip -> ... (diff)
downloadbas-bd6758f8951591ca57015ace09314c5d5e116810.tar.gz
bas-bd6758f8951591ca57015ace09314c5d5e116810.tar.xz
bas-bd6758f8951591ca57015ace09314c5d5e116810.zip
[server/ipranges] Forgot to commit the lib. ¯\_(ツ)_/¯
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/ip.js21
1 files changed, 21 insertions, 0 deletions
diff --git a/server/lib/ip.js b/server/lib/ip.js
new file mode 100644
index 0000000..87c2eb5
--- /dev/null
+++ b/server/lib/ip.js
@@ -0,0 +1,21 @@
+module.exports = { toInt, toString, isIpv4 }
+
+// Takes an ip address and converts it to an integer.
+function toInt(ipString) {
+ const ipArray = ipString.split('.')
+ if (ipArray.length !== 4) return false
+
+ let result = parseInt(ipArray[0]) * 256**3 + parseInt(ipArray[1]) * 256**2 + parseInt(ipArray[2]) * 256 + parseInt(ipArray[3])
+ return result
+}
+
+// Converts an integer value in a human readable typical ip address.
+function toString(ipInt) {
+ return [(ipInt>>24)&0xff, (ipInt>>16)&0xff, (ipInt>>8)&0xff, ipInt&0xff].join('.');
+}
+
+// Sanity check for an ipv4.
+function isIpv4(ipString) {
+ const re = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
+ return re.test(ipString)
+} \ No newline at end of file