refactor(geoip): reconcile geoip system (#31)
This commit is contained in:
@@ -68,6 +68,10 @@ export const RpcRequest = z.discriminatedUnion('type', [
|
||||
type: z.literal('get_badge_counts'),
|
||||
user_ids: z.array(Int64Type),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('geoip_lookup'),
|
||||
ip: createStringType(1, 45),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('delete_push_subscriptions'),
|
||||
subscriptions: z.array(
|
||||
@@ -290,6 +294,10 @@ export const RpcResponse = z.discriminatedUnion('type', [
|
||||
type: z.literal('validate_custom_status'),
|
||||
data: RpcResponseValidateCustomStatus,
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('geoip_lookup'),
|
||||
data: z.object({country_code: z.string()}),
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('get_dm_channel'),
|
||||
data: z.object({
|
||||
|
||||
@@ -97,7 +97,7 @@ import {
|
||||
import {isUserAdult} from '~/utils/AgeUtils';
|
||||
import {deriveDominantAvatarColor} from '~/utils/AvatarColorUtils';
|
||||
import {calculateDistance, parseCoordinate} from '~/utils/GeoUtils';
|
||||
import {formatGeoipLocation, getCountryCodeDetailed} from '~/utils/IpUtils';
|
||||
import {lookupGeoip} from '~/utils/IpUtils';
|
||||
import type {VoiceAccessContext, VoiceAvailabilityService} from '~/voice/VoiceAvailabilityService';
|
||||
import type {VoiceService} from '~/voice/VoiceService';
|
||||
import type {IWebhookRepository} from '~/webhook/IWebhookRepository';
|
||||
@@ -284,6 +284,15 @@ export class RpcService {
|
||||
},
|
||||
};
|
||||
}
|
||||
case 'geoip_lookup': {
|
||||
const geoip = await lookupGeoip(request.ip);
|
||||
return {
|
||||
type: 'geoip_lookup',
|
||||
data: {
|
||||
country_code: geoip.countryCode,
|
||||
},
|
||||
};
|
||||
}
|
||||
case 'delete_push_subscriptions':
|
||||
return {
|
||||
type: 'delete_push_subscriptions',
|
||||
@@ -678,24 +687,9 @@ export class RpcService {
|
||||
});
|
||||
|
||||
let countryCode = 'US';
|
||||
let geoipReason: string | null = null;
|
||||
if (ip) {
|
||||
const geoip = await getCountryCodeDetailed(ip);
|
||||
const geoip = await lookupGeoip(ip);
|
||||
countryCode = geoip.countryCode;
|
||||
geoipReason = geoip.reason;
|
||||
if (geoipReason) {
|
||||
Logger.warn(
|
||||
{
|
||||
ip,
|
||||
normalized_ip: geoip.normalizedIp,
|
||||
reason: geoipReason,
|
||||
geoip_host: Config.geoip.host,
|
||||
geoip_provider: Config.geoip.provider,
|
||||
geoip_location: formatGeoipLocation(geoip),
|
||||
},
|
||||
'GeoIP lookup fell back to default country code',
|
||||
);
|
||||
}
|
||||
} else {
|
||||
Logger.warn({context: 'rpc_geoip', reason: 'ip_missing'}, 'RPC session request missing IP for GeoIP');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user