Skip to content

Commit

Permalink
Added ability to retrieve a user's devices
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Cooper committed May 4, 2020
1 parent 6271f05 commit ae1ce66
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
1 change: 1 addition & 0 deletions dataoperations.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func (d *dataOperations) CreateDevice(owner UserProfile, name, os string, device
OS: os,
PublicKey: credentials.PublicKey,
IPAddress: ipAddress.Address,
Owner: owner,
}
err = d.db.Create(&device).Error
if err != nil {
Expand Down
24 changes: 17 additions & 7 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (wh *WireguardHandlers) newDeviceHandler(c *gin.Context) {
}

client := wh.config.WireguardClient
deviceFunc := func (ipAddress IPAddress) (*wgrpcd.PeerConfigInfo, error) {
deviceFunc := func(ipAddress IPAddress) (*wgrpcd.PeerConfigInfo, error) {
_, network, err := net.ParseCIDR(fmt.Sprintf("%v/32", ipAddress.Address))
if err != nil {
return nil, err
Expand All @@ -143,21 +143,21 @@ func (wh *WireguardHandlers) newDeviceHandler(c *gin.Context) {

tmpl := template.Must(
template.New("peerconfig.tmpl").
Funcs(map[string]interface{}{"StringsJoin": strings.Join}).
ParseFiles(filepath.Join(wh.config.TemplatesDirectory, "ini/peerconfig.tmpl")),
)
Funcs(map[string]interface{}{"StringsJoin": strings.Join}).
ParseFiles(filepath.Join(wh.config.TemplatesDirectory, "ini/peerconfig.tmpl")),
)

if err != nil {
log.Println(err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}

peerConfigINI := &PeerConfigINI{
PublicKey: device.PublicKey,
PublicKey: device.PublicKey,
PrivateKey: credentials.PrivateKey,
AllowedIPs: ipNetsToStrings(credentials.AllowedIPs),
Addresses: ipNetsToStrings(credentials.AllowedIPs),
Addresses: ipNetsToStrings(credentials.AllowedIPs),
ServerName: wh.config.Endpoint.String(),
DNSServers: ipsToStrings(wh.config.DNSServers),
}
Expand All @@ -178,7 +178,17 @@ func (wh *WireguardHandlers) rekeyDeviceHandler(c *gin.Context) {
}

func (wh *WireguardHandlers) listUserDevicesHandler(c *gin.Context) {
devices, err := wh.config.Database.Devices(wh.user(c))
if err != nil {
log.Println(err)
if _, ok := err.(*RecordNotFoundError); ok {
c.AbortWithStatus(http.StatusNotFound)
}
c.AbortWithStatus(http.StatusInternalServerError)
return
}

c.JSON(http.StatusOK, devices)
}

func (wh *WireguardHandlers) getUserDeviceHandler(c *gin.Context) {
Expand Down
18 changes: 6 additions & 12 deletions http_entities.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
package wireguardhttps

import (
"net"
"net/url"
)


type DeviceRequest struct{
type DeviceRequest struct {
Name string `json:"name"`
OS string `json:"os"`
OS string `json:"os"`
}

type PeerConfigINI struct{
PublicKey string
type PeerConfigINI struct {
PublicKey string
PrivateKey string
AllowedIPs []string
Addresses []string
Addresses []string
DNSServers []string
ServerName string
}
}
3 changes: 1 addition & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net"
)


func ipNetsToStrings(nets []net.IPNet) []string {
rv := []string{}
for _, n := range nets {
Expand All @@ -21,4 +20,4 @@ func ipsToStrings(ips []net.IP) []string {
}

return rv
}
}

0 comments on commit ae1ce66

Please sign in to comment.