Feature (v1): add latest handshake data to API response (#203)

* feature: updated handshake-related fields type

* feature: updated handshake representations in templates

* feature: added handshake field to Swagger schema
This commit is contained in:
onyx-flame 2023-12-23 14:56:52 +03:00 committed by GitHub
parent 2f79dd04c0
commit e6b01a9903
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 39 additions and 12 deletions

View File

@ -181,7 +181,15 @@
{{if eq $.Device.Type "client"}} {{if eq $.Device.Type "client"}}
<td>{{$p.Endpoint}}</td> <td>{{$p.Endpoint}}</td>
{{end}} {{end}}
<td><span data-toggle="tooltip" data-placement="left" title="" data-original-title="{{$p.LastHandshakeTime}}">{{$p.LastHandshake}}</span></td> <td>
<span
data-toggle="tooltip"
data-placement="left"
title=""
data-original-title="{{if $p.LastHandshakeTime.IsZero}}Never connected, or user is disabled.{{else}}{{formatTime $p.LastHandshakeTime}}{{end}}">
{{$p.LastHandshakeRelativeTime}}
</span>
</td>
<td> <td>
{{if eq $.Session.IsAdmin true}} {{if eq $.Session.IsAdmin true}}
<a href="/admin/peer/edit?pkey={{$p.PublicKey}}" title="Edit peer"><i class="fas fa-cog"></i></a> <a href="/admin/peer/edit?pkey={{$p.PublicKey}}" title="Edit peer"><i class="fas fa-cog"></i></a>

View File

@ -55,7 +55,15 @@
<td>{{$p.Email}}</td> <td>{{$p.Email}}</td>
<td>{{$p.IPsStr}}</td> <td>{{$p.IPsStr}}</td>
<td>{{$p.DeviceName}}</td> <td>{{$p.DeviceName}}</td>
<td><span data-toggle="tooltip" data-placement="left" title="" data-original-title="{{$p.LastHandshakeTime}}">{{$p.LastHandshake}}</span></td> <td>
<span
data-toggle="tooltip"
data-placement="left"
title=""
data-original-title="{{if $p.LastHandshakeTime.IsZero}}Never connected, or user is disabled.{{else}}{{formatTime $p.LastHandshakeTime}}{{end}}">
{{$p.LastHandshakeRelativeTime}}
</span>
</td>
{{if eq $.UserManagePeers true}} {{if eq $.UserManagePeers true}}
<td> <td>
<a href="/user/peer/edit?pkey={{$p.PublicKey}}" title="Edit peer"><i class="fas fa-cog"></i></a> <a href="/user/peer/edit?pkey={{$p.PublicKey}}" title="Edit peer"><i class="fas fa-cog"></i></a>

View File

@ -93,3 +93,11 @@ func FormatDateHTML(t *time.Time) string {
return t.Format("2006-01-02") return t.Format("2006-01-02")
} }
func FormatToUnixTime(t *time.Time) string {
if t == nil {
return ""
}
return t.Format(time.UnixDate)
}

View File

@ -1512,6 +1512,9 @@ const docTemplate = `{
"IgnoreGlobalSettings": { "IgnoreGlobalSettings": {
"type": "boolean" "type": "boolean"
}, },
"LastHandshakeTime": {
"type": "string"
},
"Mtu": { "Mtu": {
"description": "Global Device Settings (can be ignored, only make sense if device is in server mode)", "description": "Global Device Settings (can be ignored, only make sense if device is in server mode)",
"type": "integer", "type": "integer",

View File

@ -128,6 +128,7 @@ func (s *Server) Setup(ctx context.Context) error {
s.server.Use(sessions.Sessions("authsession", cookieStore)) s.server.Use(sessions.Sessions("authsession", cookieStore))
s.server.SetFuncMap(template.FuncMap{ s.server.SetFuncMap(template.FuncMap{
"formatDate": common.FormatDateHTML, "formatDate": common.FormatDateHTML,
"formatTime": common.FormatToUnixTime,
"formatBytes": common.ByteCountSI, "formatBytes": common.ByteCountSI,
"urlEncode": url.QueryEscape, "urlEncode": url.QueryEscape,
"startsWith": strings.HasPrefix, "startsWith": strings.HasPrefix,

View File

@ -102,8 +102,8 @@ type Peer struct {
IsOnline bool `gorm:"-" json:"-"` IsOnline bool `gorm:"-" json:"-"`
IsNew bool `gorm:"-" json:"-"` IsNew bool `gorm:"-" json:"-"`
LastHandshake string `gorm:"-" json:"-"` LastHandshakeTime time.Time `gorm:"-"`
LastHandshakeTime string `gorm:"-" json:"-"` LastHandshakeRelativeTime string `gorm:"-" json:"-"`
// Core WireGuard Settings // Core WireGuard Settings
PublicKey string `gorm:"primaryKey" form:"pubkey" binding:"required,base64"` // the public key of the peer itself PublicKey string `gorm:"primaryKey" form:"pubkey" binding:"required,base64"` // the public key of the peer itself
@ -610,8 +610,7 @@ func (m *PeerManager) populatePeerData(peer *Peer) {
// set data from WireGuard interface // set data from WireGuard interface
peer.Peer, _ = m.wg.GetPeer(peer.DeviceName, peer.PublicKey) peer.Peer, _ = m.wg.GetPeer(peer.DeviceName, peer.PublicKey)
peer.LastHandshake = "never" peer.LastHandshakeRelativeTime = "never"
peer.LastHandshakeTime = "Never connected, or user is disabled."
if peer.Peer != nil { if peer.Peer != nil {
since := time.Since(peer.Peer.LastHandshakeTime) since := time.Since(peer.Peer.LastHandshakeTime)
sinceSeconds := int(since.Round(time.Second).Seconds()) sinceSeconds := int(since.Round(time.Second).Seconds())
@ -619,13 +618,13 @@ func (m *PeerManager) populatePeerData(peer *Peer) {
sinceSeconds -= sinceMinutes * 60 sinceSeconds -= sinceMinutes * 60
if sinceMinutes > 2*10080 { // 2 weeks if sinceMinutes > 2*10080 { // 2 weeks
peer.LastHandshake = "a while ago" peer.LastHandshakeRelativeTime = "a while ago"
} else if sinceMinutes > 10080 { // 1 week } else if sinceMinutes > 10080 { // 1 week
peer.LastHandshake = "a week ago" peer.LastHandshakeRelativeTime = "a week ago"
} else { } else {
peer.LastHandshake = fmt.Sprintf("%02dm %02ds", sinceMinutes, sinceSeconds) peer.LastHandshakeRelativeTime = fmt.Sprintf("%02dm %02ds", sinceMinutes, sinceSeconds)
} }
peer.LastHandshakeTime = peer.Peer.LastHandshakeTime.Format(time.UnixDate) peer.LastHandshakeTime = peer.Peer.LastHandshakeTime
} }
peer.IsOnline = false peer.IsOnline = false
} }