mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-10 15:14:22 +08:00
feat(NET-817): add postup/down scripts for clients (#2810)
This commit is contained in:
parent
cba55e607b
commit
39fbb45cfe
3 changed files with 32 additions and 3 deletions
|
@ -7,7 +7,9 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/gravitl/netmaker/database"
|
"github.com/gravitl/netmaker/database"
|
||||||
"github.com/gravitl/netmaker/logger"
|
"github.com/gravitl/netmaker/logger"
|
||||||
|
@ -250,11 +252,24 @@ func getExtClientConf(w http.ResponseWriter, r *http.Request) {
|
||||||
if host.MTU != 0 {
|
if host.MTU != 0 {
|
||||||
defaultMTU = host.MTU
|
defaultMTU = host.MTU
|
||||||
}
|
}
|
||||||
|
|
||||||
|
postUp := strings.Builder{}
|
||||||
|
for _, loc := range strings.Split(client.PostUp, "\n") {
|
||||||
|
postUp.WriteString(fmt.Sprintf("PostUp = %s\n", loc))
|
||||||
|
}
|
||||||
|
|
||||||
|
postDown := strings.Builder{}
|
||||||
|
for _, loc := range strings.Split(client.PostDown, "\n") {
|
||||||
|
postDown.WriteString(fmt.Sprintf("PostDown = %s\n", loc))
|
||||||
|
}
|
||||||
|
|
||||||
config := fmt.Sprintf(`[Interface]
|
config := fmt.Sprintf(`[Interface]
|
||||||
Address = %s
|
Address = %s
|
||||||
PrivateKey = %s
|
PrivateKey = %s
|
||||||
MTU = %d
|
MTU = %d
|
||||||
%s
|
%s
|
||||||
|
%s
|
||||||
|
%s
|
||||||
|
|
||||||
[Peer]
|
[Peer]
|
||||||
PublicKey = %s
|
PublicKey = %s
|
||||||
|
@ -266,10 +281,13 @@ Endpoint = %s
|
||||||
client.PrivateKey,
|
client.PrivateKey,
|
||||||
defaultMTU,
|
defaultMTU,
|
||||||
defaultDNS,
|
defaultDNS,
|
||||||
|
postUp.String(),
|
||||||
|
postDown.String(),
|
||||||
host.PublicKey,
|
host.PublicKey,
|
||||||
newAllowedIPs,
|
newAllowedIPs,
|
||||||
gwendpoint,
|
gwendpoint,
|
||||||
keepalive)
|
keepalive,
|
||||||
|
)
|
||||||
|
|
||||||
if params["type"] == "qr" {
|
if params["type"] == "qr" {
|
||||||
bytes, err := qrcode.Encode(config, qrcode.Medium, 220)
|
bytes, err := qrcode.Encode(config, qrcode.Medium, 220)
|
||||||
|
@ -330,7 +348,6 @@ func createExtClient(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var customExtClient models.CustomExtClient
|
var customExtClient models.CustomExtClient
|
||||||
|
|
||||||
if err := json.NewDecoder(r.Body).Decode(&customExtClient); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&customExtClient); err != nil {
|
||||||
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
|
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
|
||||||
return
|
return
|
||||||
|
@ -499,7 +516,6 @@ func updateExtClient(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
newclient := logic.UpdateExtClient(&oldExtClient, &update)
|
newclient := logic.UpdateExtClient(&oldExtClient, &update)
|
||||||
if err := logic.DeleteExtClient(oldExtClient.Network, oldExtClient.ClientID); err != nil {
|
if err := logic.DeleteExtClient(oldExtClient.Network, oldExtClient.ClientID); err != nil {
|
||||||
|
|
||||||
slog.Error("failed to delete ext client", "user", r.Header.Get("user"), "id", oldExtClient.ClientID, "network", oldExtClient.Network, "error", err)
|
slog.Error("failed to delete ext client", "user", r.Header.Get("user"), "id", oldExtClient.ClientID, "network", oldExtClient.Network, "error", err)
|
||||||
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
|
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
|
||||||
return
|
return
|
||||||
|
@ -609,6 +625,11 @@ func deleteExtClient(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
// validateCustomExtClient Validates the extclient object
|
// validateCustomExtClient Validates the extclient object
|
||||||
func validateCustomExtClient(customExtClient *models.CustomExtClient, checkID bool) error {
|
func validateCustomExtClient(customExtClient *models.CustomExtClient, checkID bool) error {
|
||||||
|
v := validator.New()
|
||||||
|
err := v.Struct(customExtClient)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
//validate clientid
|
//validate clientid
|
||||||
if customExtClient.ClientID != "" {
|
if customExtClient.ClientID != "" {
|
||||||
if err := isValid(customExtClient.ClientID, checkID); err != nil {
|
if err := isValid(customExtClient.ClientID, checkID); err != nil {
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -276,6 +277,9 @@ func UpdateExtClient(old *models.ExtClient, update *models.CustomExtClient) mode
|
||||||
if update.DeniedACLs != nil && !reflect.DeepEqual(old.DeniedACLs, update.DeniedACLs) {
|
if update.DeniedACLs != nil && !reflect.DeepEqual(old.DeniedACLs, update.DeniedACLs) {
|
||||||
new.DeniedACLs = update.DeniedACLs
|
new.DeniedACLs = update.DeniedACLs
|
||||||
}
|
}
|
||||||
|
// replace any \r\n with \n in postup and postdown from HTTP request
|
||||||
|
new.PostUp = strings.Replace(update.PostUp, "\r\n", "\n", -1)
|
||||||
|
new.PostDown = strings.Replace(update.PostDown, "\r\n", "\n", -1)
|
||||||
return new
|
return new
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,6 +18,8 @@ type ExtClient struct {
|
||||||
OwnerID string `json:"ownerid" bson:"ownerid"`
|
OwnerID string `json:"ownerid" bson:"ownerid"`
|
||||||
DeniedACLs map[string]struct{} `json:"deniednodeacls" bson:"acls,omitempty"`
|
DeniedACLs map[string]struct{} `json:"deniednodeacls" bson:"acls,omitempty"`
|
||||||
RemoteAccessClientID string `json:"remote_access_client_id"` // unique ID (MAC address) of RAC machine
|
RemoteAccessClientID string `json:"remote_access_client_id"` // unique ID (MAC address) of RAC machine
|
||||||
|
PostUp string `json:"postup" bson:"postup"`
|
||||||
|
PostDown string `json:"postdown" bson:"postdown"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// CustomExtClient - struct for CustomExtClient params
|
// CustomExtClient - struct for CustomExtClient params
|
||||||
|
@ -29,4 +31,6 @@ type CustomExtClient struct {
|
||||||
Enabled bool `json:"enabled,omitempty"`
|
Enabled bool `json:"enabled,omitempty"`
|
||||||
DeniedACLs map[string]struct{} `json:"deniednodeacls" bson:"acls,omitempty"`
|
DeniedACLs map[string]struct{} `json:"deniednodeacls" bson:"acls,omitempty"`
|
||||||
RemoteAccessClientID string `json:"remote_access_client_id"` // unique ID (MAC address) of RAC machine
|
RemoteAccessClientID string `json:"remote_access_client_id"` // unique ID (MAC address) of RAC machine
|
||||||
|
PostUp string `json:"postup" bson:"postup" validate:"max=1024"`
|
||||||
|
PostDown string `json:"postdown" bson:"postdown" validate:"max=1024"`
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue