fix private subnets bug
This commit is contained in:
		
							parent
							
								
									2b7a8d01c7
								
							
						
					
					
						commit
						a89fb34555
					
				| 
						 | 
					@ -157,6 +157,12 @@ func NewClient() echo.HandlerFunc {
 | 
				
			||||||
			return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Allowed IPs must be in CIDR format"})
 | 
								return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Allowed IPs must be in CIDR format"})
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// validate the input PrivateSubnets
 | 
				
			||||||
 | 
							if util.ValidatePrivateSubnets(client.PrivateSubnets) == false {
 | 
				
			||||||
 | 
								log.Warnf("Invalid Private Subnets input from user: %v", client.PrivateSubnets)
 | 
				
			||||||
 | 
								return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Private Subnets must be in CIDR format"})
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// gen ID
 | 
							// gen ID
 | 
				
			||||||
		guid := xid.New()
 | 
							guid := xid.New()
 | 
				
			||||||
		client.ID = guid.String()
 | 
							client.ID = guid.String()
 | 
				
			||||||
| 
						 | 
					@ -180,6 +186,14 @@ func NewClient() echo.HandlerFunc {
 | 
				
			||||||
		client.CreatedAt = time.Now().UTC()
 | 
							client.CreatedAt = time.Now().UTC()
 | 
				
			||||||
		client.UpdatedAt = client.CreatedAt
 | 
							client.UpdatedAt = client.CreatedAt
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							client.HasPrivateSubnet = false
 | 
				
			||||||
 | 
							for _, privateSubnet := range client.PrivateSubnets {
 | 
				
			||||||
 | 
								if privateSubnet != "" {
 | 
				
			||||||
 | 
									client.HasPrivateSubnet = true
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// write client to the database
 | 
							// write client to the database
 | 
				
			||||||
		db.Write("clients", client.ID, client)
 | 
							db.Write("clients", client.ID, client)
 | 
				
			||||||
		log.Infof("Created wireguard client: %v", client)
 | 
							log.Infof("Created wireguard client: %v", client)
 | 
				
			||||||
| 
						 | 
					@ -229,14 +243,28 @@ func UpdateClient() echo.HandlerFunc {
 | 
				
			||||||
			return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Allowed IPs must be in CIDR format"})
 | 
								return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Allowed IPs must be in CIDR format"})
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// validate the input PrivateSubnets
 | 
				
			||||||
 | 
							if util.ValidatePrivateSubnets(_client.PrivateSubnets) == false {
 | 
				
			||||||
 | 
								log.Warnf("Invalid Private Subnets input from user: %v", _client.PrivateSubnets)
 | 
				
			||||||
 | 
								return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Private Subnets must be in CIDR format"})
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// map new data
 | 
							// map new data
 | 
				
			||||||
		client.Name = _client.Name
 | 
							client.Name = _client.Name
 | 
				
			||||||
		client.Email = _client.Email
 | 
							client.Email = _client.Email
 | 
				
			||||||
		client.Enabled = _client.Enabled
 | 
							client.Enabled = _client.Enabled
 | 
				
			||||||
		client.AllocatedIPs = _client.AllocatedIPs
 | 
							client.AllocatedIPs = _client.AllocatedIPs
 | 
				
			||||||
		client.AllowedIPs = _client.AllowedIPs
 | 
							client.AllowedIPs = _client.AllowedIPs
 | 
				
			||||||
 | 
							client.PrivateSubnets = _client.PrivateSubnets
 | 
				
			||||||
		client.UpdatedAt = time.Now().UTC()
 | 
							client.UpdatedAt = time.Now().UTC()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							client.HasPrivateSubnet = false
 | 
				
			||||||
 | 
							for _, privateSubnet := range client.PrivateSubnets {
 | 
				
			||||||
 | 
								if privateSubnet != "" {
 | 
				
			||||||
 | 
									client.HasPrivateSubnet = true
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// write to the database
 | 
							// write to the database
 | 
				
			||||||
		db.Write("clients", client.ID, &client)
 | 
							db.Write("clients", client.ID, &client)
 | 
				
			||||||
		log.Infof("Updated client information successfully => %v", client)
 | 
							log.Infof("Updated client information successfully => %v", client)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -14,6 +14,7 @@ type Client struct {
 | 
				
			||||||
	Email            string    `json:"email"`
 | 
						Email            string    `json:"email"`
 | 
				
			||||||
	AllocatedIPs     []string  `json:"allocated_ips"`
 | 
						AllocatedIPs     []string  `json:"allocated_ips"`
 | 
				
			||||||
	AllowedIPs       []string  `json:"allowed_ips"`
 | 
						AllowedIPs       []string  `json:"allowed_ips"`
 | 
				
			||||||
 | 
						HasPrivateSubnet bool      `json:"hasPrivateSubnet"`
 | 
				
			||||||
	PrivateSubnets   []string  `json:"private_subnets"`
 | 
						PrivateSubnets   []string  `json:"private_subnets"`
 | 
				
			||||||
	Enabled          bool      `json:"enabled"`
 | 
						Enabled          bool      `json:"enabled"`
 | 
				
			||||||
	CreatedAt        time.Time `json:"created_at"`
 | 
						CreatedAt        time.Time `json:"created_at"`
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -154,7 +154,7 @@
 | 
				
			||||||
                                    value="0.0.0.0/0">
 | 
					                                    value="0.0.0.0/0">
 | 
				
			||||||
                            </div>
 | 
					                            </div>
 | 
				
			||||||
                            <div class="form-group">
 | 
					                            <div class="form-group">
 | 
				
			||||||
                                <label for="client_private_subnets" class="control-label">Allowed IPs</label>
 | 
					                                <label for="client_private_subnets" class="control-label">Private Subnets</label>
 | 
				
			||||||
                                <input type="text" data-role="tagsinput" class="form-control" id="client_private_subnets">
 | 
					                                <input type="text" data-role="tagsinput" class="form-control" id="client_private_subnets">
 | 
				
			||||||
                            </div>
 | 
					                            </div>
 | 
				
			||||||
                            <div class="form-group">
 | 
					                            <div class="form-group">
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -20,5 +20,5 @@ PostDown = {{ .serverConfig.Interface.PostDown }}
 | 
				
			||||||
[Peer]
 | 
					[Peer]
 | 
				
			||||||
PublicKey = {{ .Client.PublicKey }}
 | 
					PublicKey = {{ .Client.PublicKey }}
 | 
				
			||||||
PresharedKey = {{ .Client.PresharedKey }}
 | 
					PresharedKey = {{ .Client.PresharedKey }}
 | 
				
			||||||
AllowedIPs = {{$first :=true}}{{range .Client.AllocatedIPs }}{{if $first}}{{$first = false}}{{else}},{{end}}{{.}}{{end}}{{if .Client.PrivateSubnets}},{{end}}{{$first :=true}}{{range .Client.PrivateSubnets }}{{if $first}}{{$first = false}}{{else}},{{end}}{{.}}{{end}}
 | 
					AllowedIPs = {{$first :=true}}{{range .Client.AllocatedIPs }}{{if $first}}{{$first = false}}{{else}},{{end}}{{.}}{{end}}{{if eq .Client.HasPrivateSubnet true}},{{$first :=true}}{{range .Client.PrivateSubnets }}{{if $first}}{{$first = false}}{{else}},{{end}}{{.}}{{end}}{{end}}
 | 
				
			||||||
{{end}}{{end}}
 | 
					{{end}}{{end}}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										10
									
								
								util/util.go
								
								
								
								
							
							
						
						
									
										10
									
								
								util/util.go
								
								
								
								
							| 
						 | 
					@ -57,7 +57,7 @@ func ValidateCIDR(cidr string) bool {
 | 
				
			||||||
// ValidateCIDRList to validate a list of network CIDR
 | 
					// ValidateCIDRList to validate a list of network CIDR
 | 
				
			||||||
func ValidateCIDRList(cidrs []string) bool {
 | 
					func ValidateCIDRList(cidrs []string) bool {
 | 
				
			||||||
	for _, cidr := range cidrs {
 | 
						for _, cidr := range cidrs {
 | 
				
			||||||
		if ValidateCIDR(cidr) == false {
 | 
							if cidr != "" && ValidateCIDR(cidr) == false {
 | 
				
			||||||
			return false
 | 
								return false
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -72,6 +72,14 @@ func ValidateAllowedIPs(cidrs []string) bool {
 | 
				
			||||||
	return true
 | 
						return true
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// ValidatePrivateSubnets to validate allowed ip addresses in CIDR format
 | 
				
			||||||
 | 
					func ValidatePrivateSubnets(cidrs []string) bool {
 | 
				
			||||||
 | 
						if ValidateCIDRList(cidrs) == false {
 | 
				
			||||||
 | 
							return false
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return true
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// ValidateServerAddresses to validate allowed ip addresses in CIDR format
 | 
					// ValidateServerAddresses to validate allowed ip addresses in CIDR format
 | 
				
			||||||
func ValidateServerAddresses(cidrs []string) bool {
 | 
					func ValidateServerAddresses(cidrs []string) bool {
 | 
				
			||||||
	if ValidateCIDRList(cidrs) == false {
 | 
						if ValidateCIDRList(cidrs) == false {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue