Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Optional generation Preshared Key when creating a new client. #140

Merged
merged 3 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added support for Pre-shared Key.
Modified Create to generate a PSK should that be required.
Included Preshared key in Config/QR if it applies.
Show PSK on EditClient Page.
  • Loading branch information
gertdreyer committed Jul 27, 2021
commit d2f696288dcccf504e8bca7bccc868ca197382ea
47 changes: 32 additions & 15 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@ type UserConfig struct {

// ClientConfig represents a single client for a user
type ClientConfig struct {
Name string
PrivateKey string
PublicKey string
IP net.IP
Notes string
Created string
Modified string
Name string
PrivateKey string
PublicKey string
PresharedKey string
IP net.IP
Notes string
Created string
Modified string
}

// NewClient provides fields that should not be saved however is neccesary on creation of a new client
type NewClient struct {
ClientConfig
GeneratePSK bool
}

// NewServerConfig creates and returns a reference to a new ServerConfig
Expand Down Expand Up @@ -93,20 +100,30 @@ func (cfg *ServerConfig) GetUserConfig(user string) *UserConfig {
}

// NewClientConfig initiates a new client, returning a reference to the new config
func NewClientConfig(ip net.IP, Name, Notes string) *ClientConfig {
func NewClientConfig(ip net.IP, Name, Notes string, generatePSK bool) *ClientConfig {
key, err := wgtypes.GeneratePrivateKey()
if err != nil {
log.Fatal(err)
}

psk := ""
if generatePSK {
pskey, err := wgtypes.GenerateKey()
if err != nil {
log.Fatal(err)
}
psk = pskey.String()
}

cfg := ClientConfig{
Name: Name,
PrivateKey: key.String(),
PublicKey: key.PublicKey().String(),
IP: ip,
Notes: Notes,
Created: time.Now().Format(time.RFC3339),
Modified: time.Now().Format(time.RFC3339),
Name: Name,
PrivateKey: key.String(),
PublicKey: key.PublicKey().String(),
IP: ip,
PresharedKey: psk,
Notes: Notes,
Created: time.Now().Format(time.RFC3339),
Modified: time.Now().Format(time.RFC3339),
}

return &cfg
Expand Down
22 changes: 16 additions & 6 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,14 @@ func (s *Server) configureWireGuard() error {
return err
}

psk, err := wgtypes.ParseKey(dev.PresharedKey)
allowedIPs := make([]net.IPNet, 1)
allowedIPs[0] = *netlink.NewIPNet(dev.IP)
peer := wgtypes.PeerConfig{
PublicKey: pubKey,
ReplaceAllowedIPs: true,
AllowedIPs: allowedIPs,
PresharedKey: &psk,
}

log.WithFields(log.Fields{"user": user, "client": id, "key": dev.PublicKey, "allowedIPs": peer.AllowedIPs}).Debug("Adding wireguard peer")
Expand Down Expand Up @@ -537,6 +539,11 @@ func (s *Server) GetClient(w http.ResponseWriter, r *http.Request, ps httprouter
keepAlive = fmt.Sprint("PersistentKeepalive = ", *wgKeepAlive)
}

presharedKey := ""
if client.PresharedKey != "" {
presharedKey = fmt.Sprintf(`PresharedKey = %s`, client.PresharedKey)
}

configData := fmt.Sprintf(`[Interface]
Address = %s
PrivateKey = %s
Expand All @@ -547,7 +554,8 @@ PublicKey = %s
AllowedIPs = %s
Endpoint = %s
%s
`, client.IP.String(), client.PrivateKey, dns, s.Config.PublicKey, allowedIPs, *wgEndpoint, keepAlive)
%s
`, client.IP.String(), client.PrivateKey, dns, s.Config.PublicKey, allowedIPs, *wgEndpoint, keepAlive, presharedKey)

format := r.URL.Query().Get("format")

Expand Down Expand Up @@ -623,6 +631,8 @@ func (s *Server) EditClient(w http.ResponseWriter, r *http.Request, ps httproute
client.Notes = cfg.Notes
}

client.PresharedKey = cfg.PresharedKey

client.Modified = time.Now().Format(time.RFC3339)

s.reconfigure()
Expand Down Expand Up @@ -691,17 +701,17 @@ func (s *Server) CreateClient(w http.ResponseWriter, r *http.Request, ps httprou
}

decoder := json.NewDecoder(r.Body)
client := &ClientConfig{}
err := decoder.Decode(&client)
newclient := &NewClient{}
err := decoder.Decode(&newclient)
if err != nil {
log.Warn("Error parsing request: ", err)
w.WriteHeader(http.StatusBadRequest)
return
}

if client.Name == "" {
if newclient.Name == "" {
log.Debugf("No clientName:using default: \"Unnamed Client\"")
client.Name = "Unnamed Client"
newclient.Name = "Unnamed Client"
}

i := 0
Expand All @@ -719,7 +729,7 @@ func (s *Server) CreateClient(w http.ResponseWriter, r *http.Request, ps httprou
i = i + 1

ip := s.allocateIP()
client = NewClientConfig(ip, client.Name, client.Notes)
client := NewClientConfig(ip, newclient.Name, newclient.Notes, newclient.GeneratePSK)
c.Clients[strconv.Itoa(i)] = client

s.reconfigure()
Expand Down
54 changes: 54 additions & 0 deletions ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
"@smui/button": "^1.0.0",
"@smui/dialog": "^1.0.0",
"@smui/fab": "^1.0.0",
"@smui/form-field": "^1.0.0",
"@smui/icon-button": "^1.0.0",
"@smui/paper": "^1.0.0",
"@smui/switch": "^1.0.0",
"@smui/textfield": "^1.0.0",
"@smui/top-app-bar": "^1.0.0",
"babel-jest": "^24.9.0",
Expand Down
2 changes: 2 additions & 0 deletions ui/src/EditClient.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@
<dd>{client.PrivateKey}</dd>
<dt>Public Key</dt>
<dd>{client.PublicKey}</dd>
<dt>Preshared Key</dt>
<dd>{client.PresharedKey}</dd>
</dl>
</div>

Expand Down
13 changes: 11 additions & 2 deletions ui/src/NewClient.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import HelperText from '@smui/textfield/helper-text/index';
import Button, {Group, GroupItem} from '@smui/button';
import Paper, {Title, Subtitle, Content} from '@smui/paper';

import Switch from '@smui/switch';
import FormField from '@smui/form-field'
import Cookie from "cookie-universal";
import { onMount } from 'svelte';
import { link, navigate } from "svelte-routing";
Expand All @@ -17,11 +18,13 @@
let client = {};
let clientName = "";
let clientNotes = "";
let generatePSK = false;
let deleteDialog;

async function handleSubmit(event) {
client.Name = clientName;
client.Notes = clientNotes;
client.generatePSK = generatePSK;
const res = await fetch(clientsUrl, {
method: "POST",
headers: {
Expand Down Expand Up @@ -78,7 +81,13 @@
<Textfield input$id="notes" fullwidth textarea bind:value={clientNotes} label="Label" input$aria-controls="client-notes" input$aria-describedby="client-notes-help" />
<HelperText id="client-notes-help">Notes about the client.</HelperText>
</div>

<div class="margins">
<FormField style="margin-bottom: 2em;">
<Switch bind:checked={generatePSK} />
<span slot="label">Generate a Pre-shared Key</span>
</FormField>
</div>

<Button variant="raised"><Label>Create</Label></Button>
</form>
</div>
Expand Down