Remove hardcoded values in the client config generation
This commit is contained in:
		
							parent
							
								
									e99a5ba92b
								
							
						
					
					
						commit
						deecd9c267
					
				|  | @ -31,6 +31,28 @@ func WireGuardClients() echo.HandlerFunc { | ||||||
| 			log.Error("Cannot fetch clients from database: ", err) | 			log.Error("Cannot fetch clients from database: ", err) | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
|  | 		// read server information
 | ||||||
|  | 		serverInterface := model.ServerInterface{} | ||||||
|  | 		if err := db.Read("server", "interfaces", &serverInterface); err != nil { | ||||||
|  | 			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) | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		// read global settings
 | ||||||
|  | 		globalSettings := model.GlobalSetting{} | ||||||
|  | 		if err := db.Read("server", "global_settings", &globalSettings); err != nil { | ||||||
|  | 			log.Error("Cannot fetch global settings from database: ", err) | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		server := model.Server{} | ||||||
|  | 		server.Interface = &serverInterface | ||||||
|  | 		server.KeyPair = &serverKeyPair | ||||||
|  | 
 | ||||||
|  | 		// read client information and build a client list
 | ||||||
| 		clientDataList := []model.ClientData{} | 		clientDataList := []model.ClientData{} | ||||||
| 		for _, f := range records { | 		for _, f := range records { | ||||||
| 			client := model.Client{} | 			client := model.Client{} | ||||||
|  | @ -43,7 +65,7 @@ func WireGuardClients() echo.HandlerFunc { | ||||||
| 			clientData.Client = &client | 			clientData.Client = &client | ||||||
| 
 | 
 | ||||||
| 			// generate client qrcode image in base64
 | 			// generate client qrcode image in base64
 | ||||||
| 			png, err := qrcode.Encode(util.BuildClientConfig(client), qrcode.Medium, 256) | 			png, err := qrcode.Encode(util.BuildClientConfig(client, server, globalSettings), qrcode.Medium, 256) | ||||||
| 			if err != nil { | 			if err != nil { | ||||||
| 				log.Error("Cannot generate QRCode: ", err) | 				log.Error("Cannot generate QRCode: ", err) | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
							
								
								
									
										15
									
								
								util/util.go
								
								
								
								
							
							
						
						
									
										15
									
								
								util/util.go
								
								
								
								
							|  | @ -8,23 +8,18 @@ import ( | ||||||
| 	"github.com/ngoduykhanh/wireguard-ui/model" | 	"github.com/ngoduykhanh/wireguard-ui/model" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| const wgConfigDNS = "1.1.1.1, 8.8.8.8" |  | ||||||
| const wgConfigPersistentKeepalive = 15 |  | ||||||
| const wgConfigEndpoint = "wireguard.example.com:56231" |  | ||||||
| const wgConfigServerPublicKey = "/OKCBc8PxIqCpgqlE9G1kSaTecdAvYf3loEwFj6MXDc=" |  | ||||||
| 
 |  | ||||||
| // BuildClientConfig to create wireguard client config string
 | // BuildClientConfig to create wireguard client config string
 | ||||||
| func BuildClientConfig(client model.Client) string { | func BuildClientConfig(client model.Client, server model.Server, setting model.GlobalSetting) string { | ||||||
| 	// Interface section
 | 	// Interface section
 | ||||||
| 	clientAddress := fmt.Sprintf("Address = %s", strings.Join(client.AllocatedIPs, ",")) | 	clientAddress := fmt.Sprintf("Address = %s", strings.Join(client.AllocatedIPs, ",")) | ||||||
| 	clientPrivateKey := fmt.Sprintf("PrivateKey = %s", client.PrivateKey) | 	clientPrivateKey := fmt.Sprintf("PrivateKey = %s", client.PrivateKey) | ||||||
| 	clientDNS := fmt.Sprintf("DNS = %s", wgConfigDNS) | 	clientDNS := fmt.Sprintf("DNS = %s", strings.Join(setting.DNSServers, ",")) | ||||||
| 
 | 
 | ||||||
| 	// Peer section
 | 	// Peer section
 | ||||||
| 	peerPublicKey := fmt.Sprintf("PublicKey = %s", wgConfigServerPublicKey) | 	peerPublicKey := fmt.Sprintf("PublicKey = %s", server.KeyPair.PublicKey) | ||||||
| 	peerAllowedIPs := fmt.Sprintf("AllowedIPs = %s", strings.Join(client.AllowedIPs, ",")) | 	peerAllowedIPs := fmt.Sprintf("AllowedIPs = %s", strings.Join(client.AllowedIPs, ",")) | ||||||
| 	peerEndpoint := fmt.Sprintf("Endpoint = %s", wgConfigEndpoint) | 	peerEndpoint := fmt.Sprintf("Endpoint = %s:%d", setting.EndpointAddress, server.Interface.ListenPort) | ||||||
| 	peerPersistentKeepalive := fmt.Sprintf("PersistentKeepalive = %d", wgConfigPersistentKeepalive) | 	peerPersistentKeepalive := fmt.Sprintf("PersistentKeepalive = %d", setting.PersistentKeepalive) | ||||||
| 
 | 
 | ||||||
| 	// build the config as string
 | 	// build the config as string
 | ||||||
| 	strConfig := "[Interface]\n" + | 	strConfig := "[Interface]\n" + | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue