feature_v0.5_netclient

This commit is contained in:
afeiszli
2021-05-29 13:22:18 -04:00
parent a3c5602b4b
commit 6c13f06001
16 changed files with 232 additions and 41 deletions

View File

@@ -22,6 +22,7 @@ func intClientHandlers(r *mux.Router) {
r.HandleFunc("/api/intclients", securityCheck(http.HandlerFunc(getAllIntClients))).Methods("GET")
r.HandleFunc("/api/intclients/deleteall", securityCheck(http.HandlerFunc(deleteAllIntClients))).Methods("POST")
r.HandleFunc("/api/intclient/register", http.HandlerFunc(registerIntClient)).Methods("POST")
r.HandleFunc("/api/intclient/{clientid}", http.HandlerFunc(deleteIntClient)).Methods("DELETE")
}
func getAllIntClients(w http.ResponseWriter, r *http.Request) {
@@ -43,20 +44,39 @@ func deleteAllIntClients(w http.ResponseWriter, r *http.Request) {
returnErrorResponse(w, r, formatError(err, "internal"))
return
}
//Return all the extclients in JSON format
w.WriteHeader(http.StatusOK)
}
func deleteIntClient(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
// get params
var params = mux.Vars(r)
success, err := DeleteIntClient(params["clientid"])
if err != nil {
returnErrorResponse(w, r, formatError(err, "internal"))
return
} else if !success {
err = errors.New("Could not delete intclient " + params["clientid"])
returnErrorResponse(w, r, formatError(err, "internal"))
return
}
returnSuccessResponse(w, r, params["clientid"]+" deleted.")
}
func getIntClient(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
clients, err := functions.GetAllIntClients()
var params = mux.Vars(r)
client, err := GetIntClient(params["clientid"])
if err != nil {
returnErrorResponse(w, r, formatError(err, "internal"))
return
}
//Return all the extclients in JSON format
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(clients)
json.NewEncoder(w).Encode(client)
}
@@ -73,14 +93,14 @@ func RegisterIntClient(client models.IntClient) (models.IntClient, error) {
}
if client.Address == "" {
newAddress, err := functions.UniqueAddress6(client.Network)
newAddress, err := functions.UniqueAddress(client.Network)
if err != nil {
return client, err
}
if newAddress == "" {
return client, errors.New("Could not find an address.")
}
client.Address6 = newAddress
client.Address = newAddress
}
if client.Network == "" { client.Network = "comms" }
server, err := serverctl.GetServerWGConf()
@@ -92,6 +112,13 @@ func RegisterIntClient(client models.IntClient) (models.IntClient, error) {
client.ServerPort = server.ServerPort
client.ServerKey = server.ServerKey
if client.ClientID == "" {
clientid := StringWithCharset(7, charset)
clientname := "client-" + clientid
client.ClientID = clientname
}
collection := mongoconn.Client.Database("netmaker").Collection("intclients")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
// insert our network into the network table