Add PostUp and PostDown script for clients interface
This commit is contained in:
		
							parent
							
								
									efbc36d61f
								
							
						
					
					
						commit
						05cfe56ab9
					
				|  | @ -331,6 +331,8 @@ func UpdateClient(db store.IStore) echo.HandlerFunc { | ||||||
| 		client.AllocatedIPs = _client.AllocatedIPs | 		client.AllocatedIPs = _client.AllocatedIPs | ||||||
| 		client.AllowedIPs = _client.AllowedIPs | 		client.AllowedIPs = _client.AllowedIPs | ||||||
| 		client.ExtraAllowedIPs = _client.ExtraAllowedIPs | 		client.ExtraAllowedIPs = _client.ExtraAllowedIPs | ||||||
|  | 		client.PostUp = _client.PostUp | ||||||
|  | 		client.PostDown = _client.PostDown | ||||||
| 		client.UpdatedAt = time.Now().UTC() | 		client.UpdatedAt = time.Now().UTC() | ||||||
| 
 | 
 | ||||||
| 		// write to the database
 | 		// write to the database
 | ||||||
|  |  | ||||||
|  | @ -17,6 +17,8 @@ type Client struct { | ||||||
| 	ExtraAllowedIPs []string  `json:"extra_allowed_ips"` | 	ExtraAllowedIPs []string  `json:"extra_allowed_ips"` | ||||||
| 	UseServerDNS    bool      `json:"use_server_dns"` | 	UseServerDNS    bool      `json:"use_server_dns"` | ||||||
| 	Enabled         bool      `json:"enabled"` | 	Enabled         bool      `json:"enabled"` | ||||||
|  | 	PostUp          string    `json:"post_up"` | ||||||
|  | 	PostDown        string    `json:"post_down"` | ||||||
| 	CreatedAt       time.Time `json:"created_at"` | 	CreatedAt       time.Time `json:"created_at"` | ||||||
| 	UpdatedAt       time.Time `json:"updated_at"` | 	UpdatedAt       time.Time `json:"updated_at"` | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -186,6 +186,14 @@ | ||||||
|                                 </label> |                                 </label> | ||||||
|                                 <input type="text" data-role="tagsinput" class="form-control" id="client_extra_allowed_ips" value="{{ StringsJoin .client_defaults.ExtraAllowedIps "," }}"> |                                 <input type="text" data-role="tagsinput" class="form-control" id="client_extra_allowed_ips" value="{{ StringsJoin .client_defaults.ExtraAllowedIps "," }}"> | ||||||
|                             </div> |                             </div> | ||||||
|  |                             <div class="form-group"> | ||||||
|  |                                 <label for="client_post_up">Post Up Script</label> | ||||||
|  |                                 <input type="text" class="form-control" id="client_post_up" name="client_post_up" placeholder="Post Up Script"> | ||||||
|  |                             </div> | ||||||
|  |                             <div class="form-group"> | ||||||
|  |                                 <label for="client_post_down">Post Down Script</label> | ||||||
|  |                                 <input type="text" class="form-control" id="client_post_down" name="client_post_down" placeholder="Post Down Script"> | ||||||
|  |                             </div> | ||||||
|                             <div class="form-group"> |                             <div class="form-group"> | ||||||
|                                 <div class="icheck-primary d-inline"> |                                 <div class="icheck-primary d-inline"> | ||||||
|                                     <input type="checkbox" id="use_server_dns" {{ if .client_defaults.UseServerDNS }}checked{{ end }}> |                                     <input type="checkbox" id="use_server_dns" {{ if .client_defaults.UseServerDNS }}checked{{ end }}> | ||||||
|  | @ -343,6 +351,8 @@ | ||||||
|             const allowed_ips = $("#client_allowed_ips").val().split(","); |             const allowed_ips = $("#client_allowed_ips").val().split(","); | ||||||
|             let use_server_dns = false; |             let use_server_dns = false; | ||||||
|             let extra_allowed_ips = []; |             let extra_allowed_ips = []; | ||||||
|  |             const post_up = $("#client_post_up").val(); | ||||||
|  |             const post_down = $("#client_post_down").val(); | ||||||
| 
 | 
 | ||||||
|             if ($("#client_extra_allowed_ips").val() !== "") { |             if ($("#client_extra_allowed_ips").val() !== "") { | ||||||
|                 extra_allowed_ips = $("#client_extra_allowed_ips").val().split(","); |                 extra_allowed_ips = $("#client_extra_allowed_ips").val().split(","); | ||||||
|  | @ -362,8 +372,8 @@ | ||||||
|             const preshared_key = $("#client_preshared_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, "post_up": post_up, "post_down": post_down, "use_server_dns": use_server_dns, | ||||||
|                 "public_key": public_key, "preshared_key": preshared_key}; |                 "enabled": enabled, "public_key": public_key, "preshared_key": preshared_key}; | ||||||
| 
 | 
 | ||||||
|             $.ajax({ |             $.ajax({ | ||||||
|                 cache: false, |                 cache: false, | ||||||
|  | @ -485,6 +495,8 @@ | ||||||
|                 $("#client_preshared_key").val(""); |                 $("#client_preshared_key").val(""); | ||||||
|                 $("#client_allocated_ips").importTags(''); |                 $("#client_allocated_ips").importTags(''); | ||||||
|                 $("#client_extra_allowed_ips").importTags(''); |                 $("#client_extra_allowed_ips").importTags(''); | ||||||
|  |                 $("#client_post_up").val(""); | ||||||
|  |                 $("#client_post_down").val(""); | ||||||
|                 updateIPAllocationSuggestion(); |                 updateIPAllocationSuggestion(); | ||||||
|             }); |             }); | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|  | @ -109,6 +109,14 @@ Wireguard Clients | ||||||
|                         <input type="text" data-role="tagsinput" class="form-control" |                         <input type="text" data-role="tagsinput" class="form-control" | ||||||
|                                id="_client_extra_allowed_ips"> |                                id="_client_extra_allowed_ips"> | ||||||
|                     </div> |                     </div> | ||||||
|  |                     <div class="form-group"> | ||||||
|  |                         <label for="_client_post_up">Post Up Script</label> | ||||||
|  |                         <input type="text" class="form-control" id="_client_post_up"placeholder="Post Up Script"> | ||||||
|  |                     </div> | ||||||
|  |                     <div class="form-group"> | ||||||
|  |                         <label for="_client_post_down">Post Down Script</label> | ||||||
|  |                         <input type="text" class="form-control" id="_client_post_down" placeholder="Post Down Script"> | ||||||
|  |                     </div> | ||||||
|                     <div class="form-group"> |                     <div class="form-group"> | ||||||
|                         <div class="icheck-primary d-inline"> |                         <div class="icheck-primary d-inline"> | ||||||
|                             <input type="checkbox" id="_use_server_dns"> |                             <input type="checkbox" id="_use_server_dns"> | ||||||
|  | @ -365,6 +373,9 @@ Wireguard Clients | ||||||
|                             modal.find("#_client_extra_allowed_ips").addTag(obj); |                             modal.find("#_client_extra_allowed_ips").addTag(obj); | ||||||
|                         }); |                         }); | ||||||
| 
 | 
 | ||||||
|  |                         modal.find("#_client_post_up").val(client.post_up); | ||||||
|  |                         modal.find("#_client_post_down").val(client.post_down); | ||||||
|  | 
 | ||||||
|                         modal.find("#_use_server_dns").prop("checked", client.use_server_dns); |                         modal.find("#_use_server_dns").prop("checked", client.use_server_dns); | ||||||
|                         modal.find("#_enabled").prop("checked", client.enabled); |                         modal.find("#_enabled").prop("checked", client.enabled); | ||||||
|                     }, |                     }, | ||||||
|  | @ -408,6 +419,8 @@ Wireguard Clients | ||||||
|             const email = $("#_client_email").val(); |             const email = $("#_client_email").val(); | ||||||
|             const allocated_ips = $("#_client_allocated_ips").val().split(","); |             const allocated_ips = $("#_client_allocated_ips").val().split(","); | ||||||
|             const allowed_ips = $("#_client_allowed_ips").val().split(","); |             const allowed_ips = $("#_client_allowed_ips").val().split(","); | ||||||
|  |             const post_up = $("#_client_post_up").val(); | ||||||
|  |             const post_down = $("#_client_post_down").val(); | ||||||
|             let use_server_dns = false; |             let use_server_dns = false; | ||||||
|             let extra_allowed_ips = []; |             let extra_allowed_ips = []; | ||||||
| 
 | 
 | ||||||
|  | @ -426,7 +439,8 @@ Wireguard Clients | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             const data = {"id": client_id, "name": name, "email": email, "allocated_ips": allocated_ips, |             const data = {"id": client_id, "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}; |                 "allowed_ips": allowed_ips, "extra_allowed_ips": extra_allowed_ips, "post_up": post_up, "post_down": post_down, | ||||||
|  |                 "use_server_dns": use_server_dns, "enabled": enabled}; | ||||||
| 
 | 
 | ||||||
|             $.ajax({ |             $.ajax({ | ||||||
|                 cache: false, |                 cache: false, | ||||||
|  |  | ||||||
							
								
								
									
										10
									
								
								util/util.go
								
								
								
								
							
							
						
						
									
										10
									
								
								util/util.go
								
								
								
								
							|  | @ -28,6 +28,14 @@ func BuildClientConfig(client model.Client, server model.Server, setting model.G | ||||||
| 	if client.UseServerDNS { | 	if client.UseServerDNS { | ||||||
| 		clientDNS = fmt.Sprintf("DNS = %s\n", strings.Join(setting.DNSServers, ",")) | 		clientDNS = fmt.Sprintf("DNS = %s\n", strings.Join(setting.DNSServers, ",")) | ||||||
| 	} | 	} | ||||||
|  | 	clientPostUp := "" | ||||||
|  | 	if strings.TrimSpace(client.PostUp) != "" { | ||||||
|  | 		clientPostUp = fmt.Sprintf("PostUp = %s\n", client.PostUp) | ||||||
|  | 	} | ||||||
|  | 	clientPostDown := "" | ||||||
|  | 	if strings.TrimSpace(client.PostDown) != "" { | ||||||
|  | 		clientPostDown = fmt.Sprintf("PostDown = %s\n", client.PostDown) | ||||||
|  | 	} | ||||||
| 
 | 
 | ||||||
| 	// Peer section
 | 	// Peer section
 | ||||||
| 	peerPublicKey := fmt.Sprintf("PublicKey = %s\n", server.KeyPair.PublicKey) | 	peerPublicKey := fmt.Sprintf("PublicKey = %s\n", server.KeyPair.PublicKey) | ||||||
|  | @ -67,6 +75,8 @@ func BuildClientConfig(client model.Client, server model.Server, setting model.G | ||||||
| 		clientPrivateKey + | 		clientPrivateKey + | ||||||
| 		clientDNS + | 		clientDNS + | ||||||
| 		forwardMark + | 		forwardMark + | ||||||
|  | 		clientPostUp + | ||||||
|  | 		clientPostDown + | ||||||
| 		"\n[Peer]\n" + | 		"\n[Peer]\n" + | ||||||
| 		peerPublicKey + | 		peerPublicKey + | ||||||
| 		peerPresharedKey + | 		peerPresharedKey + | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue