From 13cbd50dc26721792a36ab47c17e62b7bb965a85 Mon Sep 17 00:00:00 2001 From: alexkursell Date: Fri, 20 Nov 2020 04:26:50 -0500 Subject: [PATCH] Add last_seen_ip and last_seen_ts to /devices response (#1592) --- clientapi/routing/device.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/clientapi/routing/device.go b/clientapi/routing/device.go index d50c73b3..6adaa769 100644 --- a/clientapi/routing/device.go +++ b/clientapi/routing/device.go @@ -16,6 +16,7 @@ package routing import ( "io/ioutil" + "net" "net/http" "github.com/matrix-org/dendrite/clientapi/auth" @@ -32,7 +33,7 @@ type deviceJSON struct { DeviceID string `json:"device_id"` DisplayName string `json:"display_name"` LastSeenIP string `json:"last_seen_ip"` - LastSeenTS uint64 `json:"last_seen_ts"` + LastSeenTS int64 `json:"last_seen_ts"` } type devicesJSON struct { @@ -79,6 +80,8 @@ func GetDeviceByID( JSON: deviceJSON{ DeviceID: targetDevice.ID, DisplayName: targetDevice.DisplayName, + LastSeenIP: stripIPPort(targetDevice.LastSeenIP), + LastSeenTS: targetDevice.LastSeenTS, }, } } @@ -102,6 +105,8 @@ func GetDevicesByLocalpart( res.Devices = append(res.Devices, deviceJSON{ DeviceID: dev.ID, DisplayName: dev.DisplayName, + LastSeenIP: stripIPPort(dev.LastSeenIP), + LastSeenTS: dev.LastSeenTS, }) } @@ -230,3 +235,20 @@ func DeleteDevices( JSON: struct{}{}, } } + +// stripIPPort converts strings like "[::1]:12345" to "::1" +func stripIPPort(addr string) string { + ip := net.ParseIP(addr) + if ip != nil { + return addr + } + host, _, err := net.SplitHostPort(addr) + if err != nil { + return "" + } + ip = net.ParseIP(host) + if ip != nil { + return host + } + return "" +}