Add wireguard server key pair generation
This commit is contained in:
		
							parent
							
								
									febf075f8d
								
							
						
					
					
						commit
						d5ff0cb704
					
				|  | @ -76,10 +76,11 @@ func NewClient() echo.HandlerFunc { | ||||||
| 		guid := xid.New() | 		guid := xid.New() | ||||||
| 		client.ID = guid.String() | 		client.ID = guid.String() | ||||||
| 
 | 
 | ||||||
| 		// gen Wireguard key pairs
 | 		// gen Wireguard key pair
 | ||||||
| 		key, err := wgtypes.GeneratePrivateKey() | 		key, err := wgtypes.GeneratePrivateKey() | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return err | 			log.Error("Cannot generate wireguard key pair: ", err) | ||||||
|  | 			return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, "Cannot generate Wireguard key pair"}) | ||||||
| 		} | 		} | ||||||
| 		client.PrivateKey = key.String() | 		client.PrivateKey = key.String() | ||||||
| 		client.PublicKey = key.PublicKey().String() | 		client.PublicKey = key.PublicKey().String() | ||||||
|  | @ -139,9 +140,15 @@ func WireGuardServer() echo.HandlerFunc { | ||||||
| 			log.Error("Cannot fetch server interface config from database: ", err) | 			log.Error("Cannot fetch server interface config from database: ", err) | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
|  | 		serverKeyPair := model.ServerKeypair{} | ||||||
|  | 		if err := db.Read("server", "keypair", &serverKeyPair); err != nil { | ||||||
|  | 			log.Error("Cannot fetch server key pair from database: ", err) | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
| 		return c.Render(http.StatusOK, "server.html", map[string]interface{}{ | 		return c.Render(http.StatusOK, "server.html", map[string]interface{}{ | ||||||
| 			"name":            "Khanh", | 			"name":            "Khanh", | ||||||
| 			"serverInterface": serverInterface, | 			"serverInterface": serverInterface, | ||||||
|  | 			"serverKeyPair":   serverKeyPair, | ||||||
| 		}) | 		}) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | @ -173,3 +180,32 @@ func WireGuardServerInterfaces() echo.HandlerFunc { | ||||||
| 		return c.JSON(http.StatusOK, jsonHTTPResponse{true, "Updated interface addresses successfully"}) | 		return c.JSON(http.StatusOK, jsonHTTPResponse{true, "Updated interface addresses successfully"}) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | // WireGuardServerKeyPair handler to generate private and public keys
 | ||||||
|  | func WireGuardServerKeyPair() echo.HandlerFunc { | ||||||
|  | 	return func(c echo.Context) error { | ||||||
|  | 		// gen Wireguard key pair
 | ||||||
|  | 		key, err := wgtypes.GeneratePrivateKey() | ||||||
|  | 		if err != nil { | ||||||
|  | 			log.Error("Cannot generate wireguard key pair: ", err) | ||||||
|  | 			return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, "Cannot generate Wireguard key pair"}) | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		serverKeyPair := new(model.ServerKeypair) | ||||||
|  | 		serverKeyPair.PrivateKey = key.String() | ||||||
|  | 		serverKeyPair.PublicKey = key.PublicKey().String() | ||||||
|  | 		serverKeyPair.UpdatedAt = time.Now().UTC() | ||||||
|  | 
 | ||||||
|  | 		// write config to the database
 | ||||||
|  | 		dir := "./db" | ||||||
|  | 		db, err := scribble.New(dir, nil) | ||||||
|  | 		if err != nil { | ||||||
|  | 			log.Error("Cannot initialize the database: ", err) | ||||||
|  | 			return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, "Cannot access database"}) | ||||||
|  | 		} | ||||||
|  | 		db.Write("server", "keypair", serverKeyPair) | ||||||
|  | 		log.Infof("Updated wireguard server interfaces settings: %v", serverKeyPair) | ||||||
|  | 
 | ||||||
|  | 		return c.JSON(http.StatusOK, serverKeyPair) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
							
								
								
									
										1
									
								
								main.go
								
								
								
								
							
							
						
						
									
										1
									
								
								main.go
								
								
								
								
							|  | @ -13,5 +13,6 @@ func main() { | ||||||
| 	app.POST("/remove-client", handler.RemoveClient()) | 	app.POST("/remove-client", handler.RemoveClient()) | ||||||
| 	app.GET("/wg-server", handler.WireGuardServer()) | 	app.GET("/wg-server", handler.WireGuardServer()) | ||||||
| 	app.POST("wg-server/interfaces", handler.WireGuardServerInterfaces()) | 	app.POST("wg-server/interfaces", handler.WireGuardServerInterfaces()) | ||||||
|  | 	app.POST("wg-server/keypair", handler.WireGuardServerKeyPair()) | ||||||
| 	app.Logger.Fatal(app.Start("127.0.0.1:5000")) | 	app.Logger.Fatal(app.Start("127.0.0.1:5000")) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -13,7 +13,7 @@ type Server struct { | ||||||
| // ServerKeypair model
 | // ServerKeypair model
 | ||||||
| type ServerKeypair struct { | type ServerKeypair struct { | ||||||
| 	PrivateKey string    `json:"private_key"` | 	PrivateKey string    `json:"private_key"` | ||||||
| 	PublicKey  string    `json:"pulbic_key"` | 	PublicKey  string    `json:"public_key"` | ||||||
| 	UpdatedAt  time.Time `json:"updated_at"` | 	UpdatedAt  time.Time `json:"updated_at"` | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -67,7 +67,7 @@ Wireguard Clients | ||||||
|             </div> |             </div> | ||||||
|             <div class="modal-footer justify-content-between"> |             <div class="modal-footer justify-content-between"> | ||||||
|                 <button type="button" class="btn btn-outline-light" data-dismiss="modal">Cancel</button> |                 <button type="button" class="btn btn-outline-light" data-dismiss="modal">Cancel</button> | ||||||
|                 <button type="button" class="btn btn-outline-light" id="remove_client_confirm" value="xxx">Apply</button> |                 <button type="button" class="btn btn-outline-light" id="remove_client_confirm">Apply</button> | ||||||
|             </div> |             </div> | ||||||
|         </div> |         </div> | ||||||
|         <!-- /.modal-content --> |         <!-- /.modal-content --> | ||||||
|  |  | ||||||
|  | @ -57,21 +57,25 @@ Wireguard Server Settings | ||||||
|                             <div class="form-group"> |                             <div class="form-group"> | ||||||
|                                 <label for="private_key">Private Key</label> |                                 <label for="private_key">Private Key</label> | ||||||
|                                 <div class="input-group input-group"> |                                 <div class="input-group input-group"> | ||||||
|                                     <input type="text" class="form-control" id="private_key" placeholder="Private Key"> |                                     <input type="password" class="form-control" id="private_key" placeholder="Private Key" | ||||||
|  |                                         value="{{ .serverKeyPair.PrivateKey }}" disabled> | ||||||
|                                     <span class="input-group-append"> |                                     <span class="input-group-append"> | ||||||
|                                         <button type="button" class="btn btn-danger btn-flat">Show</button> |                                         <button type="button" class="btn btn-danger btn-flat" | ||||||
|  |                                             id="btn_show_private_key">Show</button> | ||||||
|                                     </span> |                                     </span> | ||||||
|                                 </div> |                                 </div> | ||||||
|                             </div> |                             </div> | ||||||
|                             <div class="form-group"> |                             <div class="form-group"> | ||||||
|                                 <label for="public_key">Public Key</label> |                                 <label for="public_key">Public Key</label> | ||||||
|                                 <input type="text" class="form-control" id="public_key" placeholder="Public Key"> |                                 <input type="text" class="form-control" id="public_key" placeholder="Public Key" | ||||||
|  |                                     value="{{ .serverKeyPair.PublicKey }}" disabled> | ||||||
|                             </div> |                             </div> | ||||||
|                         </div> |                         </div> | ||||||
|                         <!-- /.card-body --> |                         <!-- /.card-body --> | ||||||
| 
 | 
 | ||||||
|                         <div class="card-footer"> |                         <div class="card-footer"> | ||||||
|                             <button type="submit" class="btn btn-danger">Regenerate</button> |                             <button type="button" class="btn btn-danger" data-toggle="modal" | ||||||
|  |                                 data-target="#modal_keypair_confirmation">Generate</button> | ||||||
|                         </div> |                         </div> | ||||||
|                     </form> |                     </form> | ||||||
|                 </div> |                 </div> | ||||||
|  | @ -81,6 +85,30 @@ Wireguard Server Settings | ||||||
|         <!-- /.row --> |         <!-- /.row --> | ||||||
|     </div> |     </div> | ||||||
| </section> | </section> | ||||||
|  | 
 | ||||||
|  | <div class="modal fade" id="modal_keypair_confirmation"> | ||||||
|  |     <div class="modal-dialog"> | ||||||
|  |         <div class="modal-content bg-warning"> | ||||||
|  |             <div class="modal-header"> | ||||||
|  |                 <h4 class="modal-title">KeyPair Generation</h4> | ||||||
|  |                 <button type="button" class="close" data-dismiss="modal" aria-label="Close"> | ||||||
|  |                     <span aria-hidden="true">×</span> | ||||||
|  |                 </button> | ||||||
|  |             </div> | ||||||
|  |             <div class="modal-body"> | ||||||
|  |                 <p>Are you sure to generate a new key pair for the Wireguard server?<br/> | ||||||
|  |                 The existing Clients's peer public key need to be updated to keep the connection working.</p> | ||||||
|  |             </div> | ||||||
|  |             <div class="modal-footer justify-content-between"> | ||||||
|  |                 <button type="button" class="btn btn-outline-dark" data-dismiss="modal">Cancel</button> | ||||||
|  |                 <button type="button" class="btn btn-outline-dark" id="btn_generate_confirm">Generate</button> | ||||||
|  |             </div> | ||||||
|  |         </div> | ||||||
|  |         <!-- /.modal-content --> | ||||||
|  |     </div> | ||||||
|  |     <!-- /.modal-dialog --> | ||||||
|  | </div> | ||||||
|  | <!-- /.modal --> | ||||||
| {{end}} | {{end}} | ||||||
| 
 | 
 | ||||||
| {{define "bottom_js"}} | {{define "bottom_js"}} | ||||||
|  | @ -161,5 +189,45 @@ Wireguard Server Settings | ||||||
|                 } |                 } | ||||||
|             }); |             }); | ||||||
|         }); |         }); | ||||||
|  | 
 | ||||||
|  |         // Wireguard Key Pair generation confirmation button | ||||||
|  |         $(document).ready(function () { | ||||||
|  |             $('#btn_generate_confirm').click(function () { | ||||||
|  |                 $.ajax({ | ||||||
|  |                     cache: false, | ||||||
|  |                     method: 'POST', | ||||||
|  |                     url: '/wg-server/keypair', | ||||||
|  |                     dataType: 'json', | ||||||
|  |                     contentType: "application/json", | ||||||
|  |                     success: function(data) { | ||||||
|  |                         $('#modal_keypair_confirmation').modal('hide'); | ||||||
|  |                         toastr.success('Generate new key pair successfully'); | ||||||
|  |                         // update the UI | ||||||
|  |                         $('#private_key').val(data['private_key']); | ||||||
|  |                         $('#public_key').val(data['public_key']); | ||||||
|  |                     }, | ||||||
|  |                     error: function(jqXHR, exception) { | ||||||
|  |                         var responseJson = jQuery.parseJSON(jqXHR.responseText); | ||||||
|  |                         toastr.error(responseJson['message']); | ||||||
|  |                     } | ||||||
|  |                 }); | ||||||
|  |             }); | ||||||
|  |         }); | ||||||
|  | 
 | ||||||
|  |         // Show private key button event | ||||||
|  |         $(document).ready(function () { | ||||||
|  |             $('#btn_show_private_key').click(function () { | ||||||
|  |                 var privateElement = document.getElementById("private_key"); | ||||||
|  |                 var btnElement = document.getElementById("btn_show_private_key"); | ||||||
|  |                 console.log(privateElement); | ||||||
|  |                 if (privateElement.type === 'password') { | ||||||
|  |                     privateElement.type = 'text'; | ||||||
|  |                     btnElement.innerText = 'Hide'; | ||||||
|  |                 } else { | ||||||
|  |                     privateElement.type = 'password'; | ||||||
|  |                     btnElement.innerText = 'Show'; | ||||||
|  |                 } | ||||||
|  |             }); | ||||||
|  |         }); | ||||||
|     </script> |     </script> | ||||||
| {{end}} | {{end}} | ||||||
		Loading…
	
		Reference in New Issue