2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-05-31 10:44:22 +02:00
|
|
|
import IPCIDR from 'ip-cidr';
|
|
|
|
|
2023-06-24 23:35:09 +02:00
|
|
|
export function getIpHash(ip: string): string {
|
2023-01-24 09:51:09 +01:00
|
|
|
try {
|
|
|
|
// because a single person may control many IPv6 addresses,
|
|
|
|
// only a /64 subnet prefix of any IP will be taken into account.
|
|
|
|
// (this means for IPv4 the entire address is used)
|
|
|
|
const prefix = IPCIDR.createAddress(ip).mask(64);
|
|
|
|
return 'ip-' + BigInt('0b' + prefix).toString(36);
|
|
|
|
} catch (e) {
|
|
|
|
const prefix = IPCIDR.createAddress(ip.replace(/:[0-9]+$/, '')).mask(64);
|
|
|
|
return 'ip-' + BigInt('0b' + prefix).toString(36);
|
|
|
|
}
|
2022-05-31 10:44:22 +02:00
|
|
|
}
|