Skip to content

Commit

Permalink
support AllowIPs for peers in server config (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
h44z committed Jun 18, 2021
1 parent e1db939 commit e0968b3
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 3 deletions.
6 changes: 6 additions & 0 deletions assets/tpl/admin_edit_client.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ <h1>Edit client: <strong>{{.Peer.Identifier}}</strong></h1>
<input type="text" name="allowedip" class="form-control" id="server_AllowedIP" value="{{.Peer.AllowedIPsStr}}">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<label for="server_AllowedIPSrv">Extra Allowed IPs (Server sided)</label>
<input type="text" name="allowedipSrv" class="form-control" id="server_AllowedIPSrv" value="{{.Peer.AllowedIPsSrvStr}}">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12 global-config">
<label for="server_DNS">Client DNS Servers</label>
Expand Down
7 changes: 7 additions & 0 deletions internal/common/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ func init() {
return nil
},
})
migrations = append(migrations, Migration{
version: "1.0.8",
migrateFn: func(db *gorm.DB) error {
logrus.Infof("upgraded database format to version 1.0.8")
return nil
},
})
}

type SupportedDatabase string
Expand Down
2 changes: 2 additions & 0 deletions internal/server/handlers_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (s *Server) PostAdminEditPeer(c *gin.Context) {
// Clean list input
formPeer.IPsStr = common.ListToString(common.ParseStringList(formPeer.IPsStr))
formPeer.AllowedIPsStr = common.ListToString(common.ParseStringList(formPeer.AllowedIPsStr))
formPeer.AllowedIPsSrvStr = common.ListToString(common.ParseStringList(formPeer.AllowedIPsSrvStr))

disabled := c.PostForm("isdisabled") != ""
now := time.Now()
Expand Down Expand Up @@ -121,6 +122,7 @@ func (s *Server) PostAdminCreatePeer(c *gin.Context) {
// Clean list input
formPeer.IPsStr = common.ListToString(common.ParseStringList(formPeer.IPsStr))
formPeer.AllowedIPsStr = common.ListToString(common.ParseStringList(formPeer.AllowedIPsStr))
formPeer.AllowedIPsSrvStr = common.ListToString(common.ParseStringList(formPeer.AllowedIPsSrvStr))

disabled := c.PostForm("isdisabled") != ""
now := time.Now()
Expand Down
2 changes: 1 addition & 1 deletion internal/server/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server

var Version = "testbuild"
var DatabaseVersion = "1.0.7"
var DatabaseVersion = "1.0.8"
8 changes: 7 additions & 1 deletion internal/wireguard/peermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ type Peer struct {
// Core WireGuard Settings
PublicKey string `gorm:"primaryKey" form:"pubkey" binding:"required,base64"` // the public key of the peer itself
PresharedKey string `form:"presharedkey" binding:"omitempty,base64"`
AllowedIPsStr string `form:"allowedip" binding:"cidrlist"` // a comma separated list of IPs that are used in the client config file
AllowedIPsStr string `form:"allowedip" binding:"cidrlist"` // a comma separated list of IPs that are used in the client config file
AllowedIPsSrvStr string `form:"allowedipSrv" binding:"cidrlist"` // a comma separated list of IPs that are used in the server config file
Endpoint string `form:"endpoint" binding:"omitempty,hostname_port"`
PersistentKeepalive int `form:"keepalive" binding:"gte=0"`

Expand Down Expand Up @@ -123,6 +124,10 @@ func (p Peer) GetAllowedIPs() []string {
return common.ParseStringList(p.AllowedIPsStr)
}

func (p Peer) GetAllowedIPsSrv() []string {
return common.ParseStringList(p.AllowedIPsSrvStr)
}

func (p Peer) GetConfig(dev *Device) wgtypes.PeerConfig {
publicKey, _ := wgtypes.ParseKey(p.PublicKey)

Expand Down Expand Up @@ -153,6 +158,7 @@ func (p Peer) GetConfig(dev *Device) wgtypes.PeerConfig {
peerAllowedIPs = p.GetAllowedIPs()
case DeviceTypeServer:
peerAllowedIPs = p.GetIPAddresses()
peerAllowedIPs = append(peerAllowedIPs, p.GetAllowedIPsSrv()...)
}
for _, ip := range peerAllowedIPs {
_, ipNet, err := net.ParseCIDR(ip)
Expand Down
2 changes: 1 addition & 1 deletion internal/wireguard/tpl/interface.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ PublicKey = {{ .PublicKey }}
PresharedKey = {{ .PresharedKey }}
{{- end}}
{{- if eq $.Interface.Type "server"}}
AllowedIPs = {{ .IPsStr }}
AllowedIPs = {{ .IPsStr }}{{if ne .AllowedIPsSrvStr ""}}, {{ .AllowedIPsSrvStr }}{{end}}
{{- end}}
{{- if eq $.Interface.Type "client"}}
{{- if .AllowedIPsStr}}
Expand Down

0 comments on commit e0968b3

Please sign in to comment.