diff options
author | RabsRincon <rubrinbla@gmail.com> | 2018-06-28 02:23:38 +0200 |
---|---|---|
committer | RabsRincon <rubrinbla@gmail.com> | 2018-06-28 02:23:38 +0200 |
commit | 12026f6f33a64b9f9d76fa6fbc9109b3061ab18b (patch) | |
tree | 0990ecd4d0eeab8ed8a37ba5cceae5af5d638e8d /lib/utils.js | |
parent | fe13a5477bb4c9feb2ed2a7033862c1d60465a05 (diff) | |
download | compiler-explorer-12026f6f33a64b9f9d76fa6fbc9109b3061ab18b.tar.gz compiler-explorer-12026f6f33a64b9f9d76fa6fbc9109b3061ab18b.zip |
Move anonymizeIp function to utils.js & add tests
Diffstat (limited to 'lib/utils.js')
-rw-r--r-- | lib/utils.js | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/utils.js b/lib/utils.js index a53ea05b8..3033c1e94 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -85,3 +85,20 @@ function padRight(name, len) { } exports.padRight = padRight; + +// Removes last 3 octets for IPv6 and last octet for IPv4 +// There's probably a better way to do this +function anonymizeIp(ip) { + if (ip.includes('localhost')) { + return ip; + } + else if (ip.includes(':')) { + // IPv6 + return ip.replace(/:[\da-fA-F]{0,4}:[\da-fA-F]{0,4}:[\da-fA-F]{0,4}$/, ':0:0:0'); + } else { + // IPv4 + return ip.replace(/\.\d{1,3}$/, '.0'); + } +} + +exports.anonymizeIp = anonymizeIp; |