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) } }
// Network HTTP handler func Network(w http.ResponseWriter, r *http.Request) { authd, user := auth.Check(w, r) if !authd { errors.Write(w, errors.NotAuthenticated) return } args := strings.Split(r.RequestURI, "/")[2:] switch r.Method { case http.MethodDelete: deleteNetwork(w, r, args, user) case http.MethodPut: addNetwork(w, r, args, user) case http.MethodPost: editNetwork(w, r, args, user) default: w.Header().Add("Allow", http.MethodDelete+","+http.MethodPut+","+http.MethodPost) errors.Write(w, errors.InvalidMethod) } }
// Script HTTP handler func Script(w http.ResponseWriter, r *http.Request) { authd, user := auth.Check(w, r) if !authd { errors.Write(w, errors.NotAuthenticated) return } args := strings.Split(r.RequestURI, "/")[2:] switch r.Method { case http.MethodGet: getScripts(w, r, args, user) case http.MethodDelete: deleteScript(w, r, args, user) case http.MethodPut: putScript(w, r, args, user) case http.MethodPost: postScript(w, r, args, user) default: w.Header().Add("Allow", strings.Join([]string{http.MethodGet, http.MethodDelete, http.MethodPut, http.MethodPost}, ",")) errors.Write(w, errors.InvalidMethod) } }
// Register HTTP handler func Register(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { w.Header().Add("Allow", http.MethodPost) errors.Write(w, errors.InvalidMethod) return } dec := json.NewDecoder(r.Body) var af authform err := dec.Decode(&af) if err != nil || len(af.Email) == 0 || len(af.Password) == 0 { errors.Write(w, errors.MissingFields) return } user, token, timeStamp := config.CreateUser(af.Email, af.Password) if user == nil { log.Debugf("%s tried to register existing user %s\n", util.GetIP(r), af.Email) errors.Write(w, errors.EmailUsed) return } log.Debugf("%s registered %s\n", util.GetIP(r), af.Email) if config.GetMail().IsEnabled() { config.GetMail().Send(user.GetEmail(), "mauIRC account created", "account-created", map[string]interface{}{ "SenderIP": util.GetIP(r), "ServerAddr": config.GetExternalAddr(), "ServerIP": config.GetAddr(), "Token": token, "Expiry": timeStamp.Format(time.RFC1123), }) } else { w.WriteHeader(http.StatusOK) } }
// HTTPCheck handler func HTTPCheck(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { w.Header().Add("Allow", http.MethodGet) errors.Write(w, errors.InvalidMethod) return } success, _ := Check(w, r) log.Debugf("%s checked authentication (Authenticated: %t)\n", util.GetIP(r), success) w.WriteHeader(http.StatusOK) if !success { w.Write([]byte("{\"authenticated\": false}")) } else { w.Write([]byte("{\"authenticated\": true}")) } }