ip-log.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. // Shape of one entry in a client's IP log, as returned by
  2. // POST /panel/api/clients/ips/:email. `node` is the name of the node the IP is
  3. // connecting through, or '' when it is on this local panel (or unattributed).
  4. export type ClientIpInfo = {
  5. ip: string;
  6. time: string;
  7. node: string;
  8. };
  9. // normalizeClientIps accepts the API payload and returns typed entries. It also
  10. // tolerates the legacy shape (a plain array of "ip (time)" strings) so the UI
  11. // keeps working against older panels.
  12. export function normalizeClientIps(obj: unknown): ClientIpInfo[] {
  13. if (!Array.isArray(obj)) return [];
  14. const out: ClientIpInfo[] = [];
  15. for (const x of obj) {
  16. if (typeof x === 'string') {
  17. if (x.length > 0) out.push({ ip: x, time: '', node: '' });
  18. continue;
  19. }
  20. if (x && typeof x === 'object') {
  21. const o = x as Record<string, unknown>;
  22. const ip = typeof o.ip === 'string' ? o.ip : '';
  23. if (!ip) continue;
  24. out.push({
  25. ip,
  26. time: typeof o.time === 'string' ? o.time : '',
  27. node: typeof o.node === 'string' ? o.node : '',
  28. });
  29. }
  30. }
  31. return out;
  32. }