Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Optional Private Keys #161

Merged
merged 15 commits into from
Mar 13, 2022
43 changes: 29 additions & 14 deletions handler/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,23 +171,38 @@ func NewClient(db store.IStore) echo.HandlerFunc {
client.ID = guid.String()

// gen Wireguard key pair
key, err := wgtypes.GeneratePrivateKey()
if err != nil {
log.Error("Cannot generate wireguard key pair: ", err)
return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, "Cannot generate Wireguard key pair"})
if client.PublicKey == "" {
key, err := wgtypes.GeneratePrivateKey()
if err != nil {
log.Error("Cannot generate wireguard key pair: ", err)
return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, "Cannot generate Wireguard key pair"})
}
client.PrivateKey = key.String()
client.PublicKey = key.PublicKey().String()
} else {
_, err := wgtypes.ParseKey(client.PublicKey)
if err != nil {
log.Error("Cannot verify wireguard public key: ", err)
return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, "Cannot verify Wireguard public key"})
}
}

presharedKey, err := wgtypes.GenerateKey()
if err != nil {
log.Error("Cannot generated preshared key: ", err)
return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{
false, "Cannot generate Wireguard preshared key",
})
if client.PresharedKey == "" {
presharedKey, err := wgtypes.GenerateKey()
if err != nil {
log.Error("Cannot generated preshared key: ", err)
return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{
false, "Cannot generate Wireguard preshared key",
})
}
client.PresharedKey = presharedKey.String()
} else {
_, err := wgtypes.ParseKey(client.PresharedKey)
if err != nil {
log.Error("Cannot verify wireguard preshared key: ", err)
return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, "Cannot verify Wireguard preshared key"})
}
}

client.PrivateKey = key.String()
client.PublicKey = key.PublicKey().String()
client.PresharedKey = presharedKey.String()
client.CreatedAt = time.Now().UTC()
client.UpdatedAt = client.CreatedAt

Expand Down
21 changes: 20 additions & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,20 @@ <h4 class="modal-title">New Wireguard Client</h4>
</label>
</div>
</div>
<div class="form-group">
<label for="client_public_key" class="control-label">
Public Key
</label>
<input type="text" class="form-control" id="client_public_key"
name="client_public_key" placeholder="Autogenerated">
</div>
<div class="form-group">
<label for="client_public_key" class="control-label">
ferrine marked this conversation as resolved.
Show resolved Hide resolved
Preshared Key
</label>
<input type="text" class="form-control" id="client_preshared_key"
name="client_preshared_key" placeholder="Autogenerated">
</div>
</div>
<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
Expand Down Expand Up @@ -314,9 +328,12 @@ <h1>{{template "page_title" .}}</h1>
if ($("#enabled").is(':checked')){
enabled = true;
}
const public_key = $("#client_public_key").val();
const preshared_key = $("#client_preshared_key").val();

const data = {"name": name, "email": email, "allocated_ips": allocated_ips, "allowed_ips": allowed_ips,
"extra_allowed_ips": extra_allowed_ips, "use_server_dns": use_server_dns, "enabled": enabled};
"extra_allowed_ips": extra_allowed_ips, "use_server_dns": use_server_dns, "enabled": enabled,
"public_key": public_key, "preshared_key": preshared_key};

$.ajax({
cache: false,
Expand Down Expand Up @@ -434,6 +451,8 @@ <h1>{{template "page_title" .}}</h1>
$("#modal_new_client").on('shown.bs.modal', function (e) {
$("#client_name").val("");
$("#client_email").val("");
$("#client_public_key").val("");
$("#client_preshared_key").val("");
$("#client_allocated_ips").importTags('');
$("#client_extra_allowed_ips").importTags('');
updateIPAllocationSuggestion();
Expand Down