fix some bugs in client mode, improve login ui

This commit is contained in:
Christoph Haas 2021-04-05 19:12:27 +02:00
parent 94ca177884
commit 39166250ea
11 changed files with 72 additions and 52 deletions

View File

@ -0,0 +1,3 @@
.navbar {
padding: 0.5rem 1rem;
}

View File

@ -137,35 +137,8 @@
<input type="hidden" name="mail" value="{{.AdminEmail}}">
<input type="hidden" name="devicetype" value="{{.Device.Type}}">
<input type="hidden" name="device" value="{{.Device.DeviceName}}">
{{if .EditableKeys}}
<div class="form-row">
<div class="form-group col-md-12">
<label for="client_PrivateKey">Private Key</label>
<input type="text" name="privkey" class="form-control" id="client_PrivateKey" value="{{.Peer.PrivateKey}}">
</div>
</div>
<div class="form-row">
<div class="form-group required col-md-12">
<label for="client_PublicKey">Peer Public Key</label>
<input type="text" name="pubkey" class="form-control" id="client_PublicKey" value="{{.Peer.PublicKey}}" required>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<label for="client_PresharedKey">Preshared Key</label>
<input type="text" name="presharedkey" class="form-control" id="client_PresharedKey" value="{{.Peer.PresharedKey}}">
</div>
</div>
{{else}}
<input type="hidden" name="privkey" value="{{.Peer.PrivateKey}}">
<input type="hidden" name="presharedkey" value="{{.Peer.PresharedKey}}">
<div class="form-row">
<div class="form-group col-md-12">
<label for="client_ro_PublicKey">Public Key</label>
<input type="text" name="pubkey" readonly class="form-control" id="client_ro_PublicKey" value="{{.Peer.PublicKey}}">
</div>
</div>
{{end}}
<input type="hidden" name="pubkey" value="{{.Peer.PublicKey}}">
<div class="form-row">
<div class="form-group required col-md-12">
<label for="client_Identifier">Endpoint Friendly Name</label>
@ -184,6 +157,12 @@
<input type="text" name="endpointpubkey" class="form-control" id="client_EndpointPublicKey" value="{{.Peer.EndpointPublicKey}}" required>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<label for="client_PresharedKey">Preshared Key</label>
<input type="text" name="presharedkey" class="form-control" id="client_PresharedKey" value="{{.Peer.PresharedKey}}">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<label for="client_AllowedIP">Allowed IPs</label>

View File

@ -163,7 +163,7 @@
<span title="Online status" class="online-status" id="online-{{$p.UID}}" data-pkey="{{$p.PublicKey}}"><i class="fas fa-unlink"></i></span>
</th>
<td>{{$p.Identifier}}</td>
<td>{{$p.PublicKey}}</td>
<td>{{if eq $.Device.Type "server"}}{{$p.PublicKey}}{{end}}{{if eq $.Device.Type "client"}}{{$p.EndpointPublicKey}}{{end}}</td>
<td>{{$p.Email}}</td>
<td>{{$p.IPsStr}}</td>
<td><span data-toggle="tooltip" data-placement="left" title="" data-original-title="{{$p.LastHandshakeTime}}">{{$p.LastHandshake}}</span></td>

View File

@ -13,8 +13,17 @@
<link rel="stylesheet" href="/css/signin.css">
</head>
<body class="bg-gradient-primary">
<div class="container">
<body id="page-top" class="d-flex flex-column min-vh-100">
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#topNavbar" aria-controls="topNavbar" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="/"><img src="{{$.static.WebsiteLogo}}" alt="{{$.static.CompanyName}}"/></a>
<div id="topNavbar" class="navbar-collapse collapse">
</div><!--/.navbar-collapse -->
</nav>
<div class="container mt-1">
<div class="card mt-5">
<div class="card-header">Please sign in</div>
<div class="card-body">
@ -28,15 +37,16 @@
<label for="inputPassword">Password</label>
<input type="password" name="password" class="form-control" id="inputPassword" placeholder="Password">
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
<button class="btn btn-lg btn-primary btn-block mt-5" type="submit">Sign in</button>
{{ if eq .error true }}
<hr>
<span class="text-danger">{{.message}}</span>
<div class="alert alert-danger" role="alert">
{{.message}}
</div>
{{end}}
</form>
<div class="card shadow-lg o-hidden border-0 my-5">
<div class="card o-hidden border-0 my-5">
<div class="card-body p-0">
<a href="/" class="btn btn-white btn-block text-primary btn-user">Go Home</a>
</div>

View File

@ -27,8 +27,8 @@
<form class="form-inline my-2 my-lg-0" method="get">
<div class="form-group mr-sm-2">
<select name="device" id="inputDevice" class="form-control device-selector">
{{range $i, $d :=$.DeviceNames}}
<option value="{{$d}}" {{if eq $d $.Session.DeviceName}}selected{{end}}>{{$d}}</option>
{{range $d, $dn := $.DeviceNames}}
<option value="{{$d}}" {{if eq $d $.Session.DeviceName}}selected{{end}}>{{$d}} {{if and (ne $dn "") (ne $d $dn)}}({{$dn}}){{end}}</option>
{{end}}
</select>
</div>

View File

@ -23,7 +23,7 @@ func (s *Server) GetHandleError(c *gin.Context, code int, message, details strin
"Session": GetSessionData(c),
"Static": s.getStaticData(),
"Device": s.peers.GetDevice(currentSession.DeviceName),
"DeviceNames": s.wg.Cfg.DeviceNames,
"DeviceNames": s.GetDeviceNames(),
})
}
@ -36,7 +36,7 @@ func (s *Server) GetIndex(c *gin.Context) {
"Session": currentSession,
"Static": s.getStaticData(),
"Device": s.peers.GetDevice(currentSession.DeviceName),
"DeviceNames": s.wg.Cfg.DeviceNames,
"DeviceNames": s.GetDeviceNames(),
})
}
@ -104,7 +104,7 @@ func (s *Server) GetAdminIndex(c *gin.Context) {
"TotalPeers": len(s.peers.GetAllPeers(currentSession.DeviceName)),
"Users": s.users.GetUsers(),
"Device": device,
"DeviceNames": s.wg.Cfg.DeviceNames,
"DeviceNames": s.GetDeviceNames(),
})
}
@ -143,7 +143,7 @@ func (s *Server) GetUserIndex(c *gin.Context) {
"TotalPeers": len(peers),
"Users": []users.User{*s.users.GetUser(currentSession.Email)},
"Device": s.peers.GetDevice(currentSession.DeviceName),
"DeviceNames": s.wg.Cfg.DeviceNames,
"DeviceNames": s.GetDeviceNames(),
})
}

View File

@ -27,7 +27,7 @@ func (s *Server) GetAdminEditInterface(c *gin.Context) {
"Static": s.getStaticData(),
"Device": currentSession.FormData.(wireguard.Device),
"EditableKeys": s.config.Core.EditableKeys,
"DeviceNames": s.wg.Cfg.DeviceNames,
"DeviceNames": s.GetDeviceNames(),
"Csrf": csrf.GetToken(c),
})
}

View File

@ -39,7 +39,7 @@ func (s *Server) GetAdminEditPeer(c *gin.Context) {
"Peer": currentSession.FormData.(wireguard.Peer),
"EditableKeys": s.config.Core.EditableKeys,
"Device": s.peers.GetDevice(currentSession.DeviceName),
"DeviceNames": s.wg.Cfg.DeviceNames,
"DeviceNames": s.GetDeviceNames(),
"AdminEmail": s.config.Core.AdminUser,
"Csrf": csrf.GetToken(c),
})
@ -99,7 +99,7 @@ func (s *Server) GetAdminCreatePeer(c *gin.Context) {
"Peer": currentSession.FormData.(wireguard.Peer),
"EditableKeys": s.config.Core.EditableKeys,
"Device": s.peers.GetDevice(currentSession.DeviceName),
"DeviceNames": s.wg.Cfg.DeviceNames,
"DeviceNames": s.GetDeviceNames(),
"AdminEmail": s.config.Core.AdminUser,
"Csrf": csrf.GetToken(c),
})
@ -154,7 +154,7 @@ func (s *Server) GetAdminCreateLdapPeers(c *gin.Context) {
"Users": s.users.GetFilteredAndSortedUsers("lastname", "asc", ""),
"FormData": currentSession.FormData.(LdapCreateForm),
"Device": s.peers.GetDevice(currentSession.DeviceName),
"DeviceNames": s.wg.Cfg.DeviceNames,
"DeviceNames": s.GetDeviceNames(),
"Csrf": csrf.GetToken(c),
})
}

View File

@ -58,7 +58,7 @@ func (s *Server) GetAdminUsersIndex(c *gin.Context) {
"Users": dbUsers,
"TotalUsers": len(s.users.GetUsers()),
"Device": s.peers.GetDevice(currentSession.DeviceName),
"DeviceNames": s.wg.Cfg.DeviceNames,
"DeviceNames": s.GetDeviceNames(),
})
}
@ -78,7 +78,7 @@ func (s *Server) GetAdminUsersEdit(c *gin.Context) {
"Static": s.getStaticData(),
"User": currentSession.FormData.(users.User),
"Device": s.peers.GetDevice(currentSession.DeviceName),
"DeviceNames": s.wg.Cfg.DeviceNames,
"DeviceNames": s.GetDeviceNames(),
"Epoch": time.Time{},
"Csrf": csrf.GetToken(c),
})
@ -156,7 +156,7 @@ func (s *Server) GetAdminUsersCreate(c *gin.Context) {
"Static": s.getStaticData(),
"User": currentSession.FormData.(users.User),
"Device": s.peers.GetDevice(currentSession.DeviceName),
"DeviceNames": s.wg.Cfg.DeviceNames,
"DeviceNames": s.GetDeviceNames(),
"Epoch": time.Time{},
"Csrf": csrf.GetToken(c),
})

View File

@ -140,8 +140,13 @@ func (s *Server) UpdatePeer(peer wireguard.Peer, updateTime time.Time) error {
// Update WireGuard device
var err error
switch {
case peer.DeactivatedAt == &updateTime:
case peer.DeactivatedAt != nil && *peer.DeactivatedAt == updateTime:
switch dev.Type {
case wireguard.DeviceTypeServer:
err = s.wg.RemovePeer(peer.DeviceName, peer.PublicKey)
case wireguard.DeviceTypeClient:
err = s.wg.RemovePeer(peer.DeviceName, peer.EndpointPublicKey)
}
case peer.DeactivatedAt == nil && currentPeer.Peer != nil:
err = s.wg.UpdatePeer(peer.DeviceName, peer.GetConfig(&dev))
case peer.DeactivatedAt == nil && currentPeer.Peer == nil:
@ -161,8 +166,18 @@ func (s *Server) UpdatePeer(peer wireguard.Peer, updateTime time.Time) error {
// DeletePeer removes the peer from the physical WireGuard interface and the database.
func (s *Server) DeletePeer(peer wireguard.Peer) error {
dev := s.peers.GetDevice(peer.DeviceName)
var publicKey string
switch dev.Type {
case wireguard.DeviceTypeServer:
publicKey = peer.PublicKey
case wireguard.DeviceTypeClient:
publicKey = peer.EndpointPublicKey
}
// Delete WireGuard peer
if err := s.wg.RemovePeer(peer.DeviceName, peer.PublicKey); err != nil {
if err := s.wg.RemovePeer(peer.DeviceName, publicKey); err != nil {
return errors.WithMessage(err, "failed to remove WireGuard peer")
}
@ -308,3 +323,14 @@ func (s *Server) CreateUserDefaultPeer(email, device string) error {
return nil
}
func (s *Server) GetDeviceNames() map[string]string {
devNames := make(map[string]string, len(s.wg.Cfg.DeviceNames))
for _, devName := range s.wg.Cfg.DeviceNames {
dev := s.peers.GetDevice(devName)
devNames[devName] = dev.DisplayName
}
return devNames
}

View File

@ -19,7 +19,9 @@ MTU = {{.Peer.Mtu}}
[Peer]
PublicKey = {{ .Peer.EndpointPublicKey }}
Endpoint = {{ .Peer.Endpoint }}
{{- if .Peer.AllowedIPsStr}}
AllowedIPs = {{ .Peer.AllowedIPsStr }}
{{- end}}
{{- if .Peer.PresharedKey}}
PresharedKey = {{ .Peer.PresharedKey }}
{{- end}}