func getScripts(w http.ResponseWriter, r *http.Request, args []string, user interfaces.User) { var scripts []interfaces.Script if args[0] == all { scripts = user.GetGlobalScripts() user.GetNetworks().ForEach(func(net interfaces.Network) { scripts = append(scripts, net.GetScripts()...) }) } else if args[0] == global { scripts = user.GetGlobalScripts() } else { net := user.GetNetwork(args[0]) if net == nil { errors.Write(w, errors.NetworkNotFound) return } scripts = net.GetScripts() } data, err := json.Marshal(scripts) if err != nil { errors.Write(w, errors.Internal) return } w.WriteHeader(http.StatusOK) w.Write(data) }
func addNetwork(w http.ResponseWriter, r *http.Request, args []string, user interfaces.User) { data, err := ioutil.ReadAll(r.Body) if err != nil { errors.Write(w, errors.BodyNotFound) return } net, ok := user.CreateNetwork(args[0], data) if !ok { errors.Write(w, errors.RequestNotJSON) return } oldNet := user.GetNetwork(args[0]) if oldNet != nil { oldNet.Disconnect() user.DeleteNetwork(args[0]) } if !user.AddNetwork(net) { log.Debugf("%s tried to create network %s for %s, but failed for unknown reasons\n", getIP(r), net.GetName(), user.GetEmail()) errors.Write(w, errors.Internal) return } user.InitNetworks() user.SendNetworkData(net) log.Debugf("%s created network %s for %s\n", getIP(r), net.GetName(), user.GetEmail()) }
func editNetwork(w http.ResponseWriter, r *http.Request, args []string, user interfaces.User) { net := user.GetNetwork(args[0]) if net == nil { errors.Write(w, errors.NetworkNotFound) return } defer r.Body.Close() var data editRequest dec := json.NewDecoder(r.Body) err := dec.Decode(&data) if err != nil { errors.Write(w, errors.RequestNotJSON) return } var oldData = net.GetNetData() nameUpdates(net, data, oldData) addrUpdates(net, data, oldData) connectedUpdate(net, data, oldData) log.Debugf("%s edited network %s of %s\n", getIP(r), net.GetName(), user.GetEmail()) enc := json.NewEncoder(w) err = enc.Encode(editResponse{New: net.GetNetData(), Old: oldData}) if err != nil { errors.Write(w, errors.Internal) return } }
func postScript(w http.ResponseWriter, r *http.Request, args []string, user interfaces.User) { data, err := ioutil.ReadAll(r.Body) if err != nil { errors.Write(w, errors.BodyNotFound) return } newPath := string(data) parts := strings.Split(newPath, ",") if len(parts) != 2 { errors.Write(w, errors.InvalidBodyFormat) return } var scripts []interfaces.Script var success bool if args[0] == global { scripts = user.GetGlobalScripts() success = user.RemoveGlobalScript(args[1]) } else { net := user.GetNetwork(args[0]) if net == nil { errors.Write(w, errors.NetworkNotFound) return } scripts = net.GetScripts() success = net.RemoveScript(args[1]) } if !success { errors.Write(w, errors.ScriptNotFound) return } var script interfaces.Script for _, s := range scripts { if s.GetName() == args[1] { script = plugin.Script{Name: parts[1], TheScript: s.GetScript()} break } } if parts[0] == global { user.AddGlobalScript(script) } else { net := user.GetNetwork(parts[0]) if net == nil { errors.Write(w, errors.NetworkNotFound) return } net.AddScript(script) } }
func deleteNetwork(w http.ResponseWriter, r *http.Request, args []string, user interfaces.User) { net := user.GetNetwork(args[0]) if net != nil { if net.IsConnected() { net.ForceDisconnect() } if user.DeleteNetwork(args[0]) { log.Debugf("%s deleted network %s of %s\n", getIP(r), net.GetName(), user.GetEmail()) w.WriteHeader(http.StatusOK) } else { log.Debugf("%s tried to delete network %s of %s, but failed for unknown reasons\n", getIP(r), net.GetName(), user.GetEmail()) errors.Write(w, errors.Internal) } } else { errors.Write(w, errors.NetworkNotFound) } }
func putScript(w http.ResponseWriter, r *http.Request, args []string, user interfaces.User) { data, err := ioutil.ReadAll(r.Body) if err != nil { errors.Write(w, errors.BodyNotFound) return } script := plugin.Script{Name: args[1], TheScript: string(data)} if args[0] == global { user.AddGlobalScript(script) } else { net := user.GetNetwork(args[0]) if net == nil { errors.Write(w, errors.NetworkNotFound) return } net.AddScript(script) } }
func deleteScript(w http.ResponseWriter, r *http.Request, args []string, user interfaces.User) { if args[0] == "global" { if !user.RemoveGlobalScript(args[1]) { errors.Write(w, errors.ScriptNotFound) return } w.WriteHeader(http.StatusOK) } else { net := user.GetNetwork(args[0]) if net == nil { errors.Write(w, errors.NetworkNotFound) return } if !net.RemoveScript(args[1]) { errors.Write(w, errors.ScriptNotFound) return } w.WriteHeader(http.StatusOK) } }