Add preshared key in wireguard config (#15)
This commit is contained in:
		
							parent
							
								
									f76de28a17
								
							
						
					
					
						commit
						9617425033
					
				|  | @ -136,8 +136,16 @@ func NewClient() echo.HandlerFunc { | ||||||
| 			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"}) | ||||||
| 		} | 		} | ||||||
|  | 
 | ||||||
|  | 		presharedKey, err := wgtypes.GenerateKey() | ||||||
|  | 		if err != nil { | ||||||
|  | 			log.Error("Cannot generated preshared key: ", err) | ||||||
|  | 			return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, "Cannot generate Wireguard preshared key"}) | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
| 		client.PrivateKey = key.String() | 		client.PrivateKey = key.String() | ||||||
| 		client.PublicKey = key.PublicKey().String() | 		client.PublicKey = key.PublicKey().String() | ||||||
|  | 		client.PresharedKey = presharedKey.String() | ||||||
| 		client.CreatedAt = time.Now().UTC() | 		client.CreatedAt = time.Now().UTC() | ||||||
| 		client.UpdatedAt = client.CreatedAt | 		client.UpdatedAt = client.CreatedAt | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -8,7 +8,8 @@ import ( | ||||||
| type Client struct { | type Client struct { | ||||||
| 	ID           string    `json:"id"` | 	ID           string    `json:"id"` | ||||||
| 	PrivateKey   string    `json:"private_key"` | 	PrivateKey   string    `json:"private_key"` | ||||||
| 	PublicKey    string    `json:"pulbic_key"` | 	PublicKey    string    `json:"public_key"` | ||||||
|  | 	PresharedKey string	   `json:"preshared_key"` | ||||||
| 	Name         string    `json:"name"` | 	Name         string    `json:"name"` | ||||||
| 	Email        string    `json:"email"` | 	Email        string    `json:"email"` | ||||||
| 	AllocatedIPs []string  `json:"allocated_ips"` | 	AllocatedIPs []string  `json:"allocated_ips"` | ||||||
|  |  | ||||||
|  | @ -17,5 +17,6 @@ MTU = {{ .globalSettings.MTU }} | ||||||
| # Update at:    {{ .Client.UpdatedAt }} | # Update at:    {{ .Client.UpdatedAt }} | ||||||
| [Peer] | [Peer] | ||||||
| PublicKey = {{ .Client.PublicKey }} | PublicKey = {{ .Client.PublicKey }} | ||||||
|  | PresharedKey = {{ .Client.PresharedKey }} | ||||||
| AllowedIPs = {{$first :=true}}{{range .Client.AllocatedIPs }}{{if $first}}{{$first = false}}{{else}},{{end}}{{.}}{{end}} | AllowedIPs = {{$first :=true}}{{range .Client.AllocatedIPs }}{{if $first}}{{$first = false}}{{else}},{{end}}{{.}}{{end}} | ||||||
| {{end}}{{end}} | {{end}}{{end}} | ||||||
|  |  | ||||||
|  | @ -25,6 +25,7 @@ func BuildClientConfig(client model.Client, server model.Server, setting model.G | ||||||
| 
 | 
 | ||||||
| 	// Peer section
 | 	// Peer section
 | ||||||
| 	peerPublicKey := fmt.Sprintf("PublicKey = %s", server.KeyPair.PublicKey) | 	peerPublicKey := fmt.Sprintf("PublicKey = %s", server.KeyPair.PublicKey) | ||||||
|  | 	peerPresharedKey := fmt.Sprintf("PresharedKey = %s", client.PresharedKey) | ||||||
| 	peerAllowedIPs := fmt.Sprintf("AllowedIPs = %s", strings.Join(client.AllowedIPs, ",")) | 	peerAllowedIPs := fmt.Sprintf("AllowedIPs = %s", strings.Join(client.AllowedIPs, ",")) | ||||||
| 	peerEndpoint := fmt.Sprintf("Endpoint = %s:%d", setting.EndpointAddress, server.Interface.ListenPort) | 	peerEndpoint := fmt.Sprintf("Endpoint = %s:%d", setting.EndpointAddress, server.Interface.ListenPort) | ||||||
| 	peerPersistentKeepalive := fmt.Sprintf("PersistentKeepalive = %d", setting.PersistentKeepalive) | 	peerPersistentKeepalive := fmt.Sprintf("PersistentKeepalive = %d", setting.PersistentKeepalive) | ||||||
|  | @ -36,6 +37,7 @@ func BuildClientConfig(client model.Client, server model.Server, setting model.G | ||||||
| 		clientDNS + "\n\n" + | 		clientDNS + "\n\n" + | ||||||
| 		"[Peer]" + "\n" + | 		"[Peer]" + "\n" + | ||||||
| 		peerPublicKey + "\n" + | 		peerPublicKey + "\n" + | ||||||
|  | 		peerPresharedKey + "\n" + | ||||||
| 		peerAllowedIPs + "\n" + | 		peerAllowedIPs + "\n" + | ||||||
| 		peerEndpoint + "\n" + | 		peerEndpoint + "\n" + | ||||||
| 		peerPersistentKeepalive + "\n" | 		peerPersistentKeepalive + "\n" | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue