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"}}
<td>{{$p.Endpoint}}</td>
{{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>
{{if eq $.Session.IsAdmin true}}
<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.IPsStr}}</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}}
<td>
<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")
}
func FormatToUnixTime(t *time.Time) string {
if t == nil {
return ""
}
return t.Format(time.UnixDate)
}

View File

@ -1512,6 +1512,9 @@ const docTemplate = `{
"IgnoreGlobalSettings": {
"type": "boolean"
},
"LastHandshakeTime": {
"type": "string"
},
"Mtu": {
"description": "Global Device Settings (can be ignored, only make sense if device is in server mode)",
"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.SetFuncMap(template.FuncMap{
"formatDate": common.FormatDateHTML,
"formatTime": common.FormatToUnixTime,
"formatBytes": common.ByteCountSI,
"urlEncode": url.QueryEscape,
"startsWith": strings.HasPrefix,

View File

@ -100,10 +100,10 @@ type Peer struct {
Email string `gorm:"index" form:"mail" binding:"required,email"`
IgnoreGlobalSettings bool `form:"ignoreglobalsettings"`
IsOnline bool `gorm:"-" json:"-"`
IsNew bool `gorm:"-" json:"-"`
LastHandshake string `gorm:"-" json:"-"`
LastHandshakeTime string `gorm:"-" json:"-"`
IsOnline bool `gorm:"-" json:"-"`
IsNew bool `gorm:"-" json:"-"`
LastHandshakeTime time.Time `gorm:"-"`
LastHandshakeRelativeTime string `gorm:"-" json:"-"`
// Core WireGuard Settings
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
peer.Peer, _ = m.wg.GetPeer(peer.DeviceName, peer.PublicKey)
peer.LastHandshake = "never"
peer.LastHandshakeTime = "Never connected, or user is disabled."
peer.LastHandshakeRelativeTime = "never"
if peer.Peer != nil {
since := time.Since(peer.Peer.LastHandshakeTime)
sinceSeconds := int(since.Round(time.Second).Seconds())
@ -619,13 +618,13 @@ func (m *PeerManager) populatePeerData(peer *Peer) {
sinceSeconds -= sinceMinutes * 60
if sinceMinutes > 2*10080 { // 2 weeks
peer.LastHandshake = "a while ago"
peer.LastHandshakeRelativeTime = "a while ago"
} else if sinceMinutes > 10080 { // 1 week
peer.LastHandshake = "a week ago"
peer.LastHandshakeRelativeTime = "a week ago"
} 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
}