add validation layer for public key
This commit is contained in:
		
							parent
							
								
									0caa8d6e32
								
							
						
					
					
						commit
						24d8a4044b
					
				| 
						 | 
					@ -171,11 +171,21 @@ func NewClient(db store.IStore) echo.HandlerFunc {
 | 
				
			||||||
		client.ID = guid.String()
 | 
							client.ID = guid.String()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// gen Wireguard key pair
 | 
							// gen Wireguard key pair
 | 
				
			||||||
 | 
							if client.PublicKey == "" {
 | 
				
			||||||
			key, err := wgtypes.GeneratePrivateKey()
 | 
								key, err := wgtypes.GeneratePrivateKey()
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				log.Error("Cannot generate wireguard key pair: ", err)
 | 
									log.Error("Cannot generate wireguard key pair: ", err)
 | 
				
			||||||
				return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, "Cannot generate Wireguard key pair"})
 | 
									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()
 | 
							presharedKey, err := wgtypes.GenerateKey()
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
| 
						 | 
					@ -185,8 +195,6 @@ func NewClient(db store.IStore) echo.HandlerFunc {
 | 
				
			||||||
			})
 | 
								})
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		client.PrivateKey = key.String()
 | 
					 | 
				
			||||||
		client.PublicKey = key.PublicKey().String()
 | 
					 | 
				
			||||||
		client.PresharedKey = presharedKey.String()
 | 
							client.PresharedKey = presharedKey.String()
 | 
				
			||||||
		client.CreatedAt = time.Now().UTC()
 | 
							client.CreatedAt = time.Now().UTC()
 | 
				
			||||||
		client.UpdatedAt = client.CreatedAt
 | 
							client.UpdatedAt = client.CreatedAt
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -328,9 +328,12 @@
 | 
				
			||||||
            if ($("#enabled").is(':checked')){
 | 
					            if ($("#enabled").is(':checked')){
 | 
				
			||||||
                enabled = true;
 | 
					                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,
 | 
					            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({
 | 
					            $.ajax({
 | 
				
			||||||
                cache: false,
 | 
					                cache: false,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue