Fix wg config template
This commit is contained in:
		
							parent
							
								
									58f1bd65bb
								
							
						
					
					
						commit
						d00d7bd8fe
					
				|  | @ -2,7 +2,8 @@ version: '3' | ||||||
| 
 | 
 | ||||||
| services: | services: | ||||||
|   wg: |   wg: | ||||||
|     image: ngoduykhanh/wireguard-ui:latest |     #image: ngoduykhanh/wireguard-ui:latest | ||||||
|  |     image: wgui:latest | ||||||
|     container_name: wgui |     container_name: wgui | ||||||
|     ports: |     ports: | ||||||
|       - 5000:5000 |       - 5000:5000 | ||||||
|  | @ -12,4 +13,4 @@ services: | ||||||
|         max-size: 50m |         max-size: 50m | ||||||
|     volumes: |     volumes: | ||||||
|       - ./db:/app/db |       - ./db:/app/db | ||||||
|       - /etc/wireguard:/etc/wireguard | #      - /etc/wireguard:/etc/wireguard | ||||||
|  |  | ||||||
|  | @ -3,6 +3,7 @@ package handler | ||||||
| import ( | import ( | ||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	rice "github.com/GeertJohan/go.rice" | ||||||
| 	"net/http" | 	"net/http" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"time" | 	"time" | ||||||
|  | @ -429,7 +430,7 @@ func SuggestIPAllocation() echo.HandlerFunc { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // ApplyServerConfig handler to write config file and restart Wireguard server
 | // ApplyServerConfig handler to write config file and restart Wireguard server
 | ||||||
| func ApplyServerConfig() echo.HandlerFunc { | func ApplyServerConfig(tmplBox *rice.Box) echo.HandlerFunc { | ||||||
| 	return func(c echo.Context) error { | 	return func(c echo.Context) error { | ||||||
| 		// access validation
 | 		// access validation
 | ||||||
| 		validSession(c) | 		validSession(c) | ||||||
|  | @ -453,7 +454,7 @@ func ApplyServerConfig() echo.HandlerFunc { | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		// Write config file
 | 		// Write config file
 | ||||||
| 		err = util.WriteWireGuardServerConfig(server, clients, settings) | 		err = util.WriteWireGuardServerConfig(tmplBox, server, clients, settings) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			log.Error("Cannot apply server config: ", err) | 			log.Error("Cannot apply server config: ", err) | ||||||
| 			return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, fmt.Sprintf("Cannot apply server config: %v", err)}) | 			return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, fmt.Sprintf("Cannot apply server config: %v", err)}) | ||||||
|  |  | ||||||
							
								
								
									
										2
									
								
								main.go
								
								
								
								
							
							
						
						
									
										2
									
								
								main.go
								
								
								
								
							|  | @ -41,7 +41,7 @@ func main() { | ||||||
| 	app.POST("/global-settings", handler.GlobalSettingSubmit()) | 	app.POST("/global-settings", handler.GlobalSettingSubmit()) | ||||||
| 	app.GET("/api/machine-ips", handler.MachineIPAddresses()) | 	app.GET("/api/machine-ips", handler.MachineIPAddresses()) | ||||||
| 	app.GET("/api/suggest-client-ips", handler.SuggestIPAllocation()) | 	app.GET("/api/suggest-client-ips", handler.SuggestIPAllocation()) | ||||||
| 	app.GET("/api/apply-wg-config", handler.ApplyServerConfig()) | 	app.GET("/api/apply-wg-config", handler.ApplyServerConfig(tmplBox)) | ||||||
| 
 | 
 | ||||||
| 	// servers other static files
 | 	// servers other static files
 | ||||||
| 	app.GET("/static/*", echo.WrapHandler(http.StripPrefix("/static/", assetHandler))) | 	app.GET("/static/*", echo.WrapHandler(http.StripPrefix("/static/", assetHandler))) | ||||||
|  |  | ||||||
							
								
								
									
										10
									
								
								util/util.go
								
								
								
								
							
							
						
						
									
										10
									
								
								util/util.go
								
								
								
								
							|  | @ -313,15 +313,9 @@ func ValidateIPAllocation(serverAddresses []string, ipAllocatedList []string, ip | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // WriteWireGuardServerConfig to write Wireguard server config. e.g. wg0.conf
 | // WriteWireGuardServerConfig to write Wireguard server config. e.g. wg0.conf
 | ||||||
| func WriteWireGuardServerConfig(serverConfig model.Server, clientDataList []model.ClientData, globalSettings model.GlobalSetting) error { | func WriteWireGuardServerConfig(tmplBox *rice.Box, serverConfig model.Server, clientDataList []model.ClientData, globalSettings model.GlobalSetting) error { | ||||||
| 	// create go rice box for wireguard config
 |  | ||||||
| 	templateBox, err := rice.FindBox("../templates") |  | ||||||
| 	if err != nil { |  | ||||||
| 		return err |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	// read wg.conf template file to string
 | 	// read wg.conf template file to string
 | ||||||
| 	tmplWireguardConf, err := templateBox.String("wg.conf") | 	tmplWireguardConf, err := tmplBox.String("wg.conf") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue