Пример #1
0
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())
}
Пример #2
0
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
	}
}
Пример #3
0
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)
	}
}