func (t *Device) edit(response http.ResponseWriter, request *http.Request) { // Get the current session user. myuser := t.Sessions.CurrentUser(response, request) // Get the device. macAddress := mux.Vars(request)["macAddress"] deviceDataStoreHelper, err := datastore.GetDeviceHelper() if err != nil { http.Error(response, err.Error(), 500) return } MACAddressHardware, err := net.ParseMAC(macAddress) if err != nil { http.Error(response, err.Error(), 500) return } mydevice, err := deviceDataStoreHelper.GetDevice(MACAddressHardware) // Check for error when loading device. if err != nil { http.Error(response, err.Error(), 500) return } // Get all users. userDataStoreHelper, err := datastore.GetUserHelper() if err != nil { log.Println("DB Error:" + err.Error()) http.Error(response, err.Error(), 500) return } allUsers, err := userDataStoreHelper.GetUsers() // Check for error when loading users. if err != nil { http.Error(response, err.Error(), 500) return } // Setup the data structure to pass to the page. data := struct { Action string Device datastore.Device User datastore.User AllUsers []datastore.User }{ "deviceSettings", mydevice, myuser, allUsers, } // Parse the page and execute the template. tpl, err := template.ParseFiles("static/main.tpl", "static/main_authenticated.tpl", "static/devices/edit.tpl") tpl.Execute(response, data) }
func (t *Internet) connection(response http.ResponseWriter, request *http.Request) { // Get the current session user. user := t.Sessions.CurrentUser(response, request) connectionhelper, err := datastore.GetConnectionHelper() if err != nil { log.Printf("Error: Failed to get Connections Helper \"%v\"", err.Error()) http.Error(response, err.Error(), 500) return } sourcePort, err := strconv.ParseUint(mux.Vars(request)["sourcePort"], 10, 16) if err != nil { log.Printf("Error: Failed to convert sourceport to uint16 \"%v\"", err.Error()) http.Error(response, err.Error(), 500) return } destinationPort, err := strconv.ParseUint(mux.Vars(request)["destinationPort"], 10, 16) if err != nil { log.Printf("Error: Failed to convert sourceport to uint16 \"%v\"", err.Error()) http.Error(response, err.Error(), 500) return } connection, err := connectionhelper.GetConnection(net.ParseIP(mux.Vars(request)["source"]), uint16(sourcePort), net.ParseIP(mux.Vars(request)["destination"]), uint16(destinationPort)) if err != nil { log.Printf("Error: Getting Connection \"%v\"", err.Error()) http.Error(response, err.Error(), 500) return } // Parse the page and execute the template. tpl, err := template.ParseFiles("static/main.tpl", "static/main_authenticated.tpl", "static/internet/connection.tpl") if err != nil { http.Error(response, err.Error(), 500) return } data := struct { Action string User datastore.User Connection *datastore.Connection }{ "internet", user, &connection, } err = tpl.Execute(response, data) if err != nil { log.Println(err) return } }
/** * Displays the device list. */ func (t *Device) index(response http.ResponseWriter, request *http.Request) { // Get the current session user. myuser := t.Sessions.CurrentUser(response, request) // If there isn't a current session user, redirect to login. if myuser.Username == "" { http.Redirect(response, request, "/login", http.StatusMovedPermanently) return } // Get the devices. deviceHelper, err := datastore.GetDeviceHelper() if err != nil { log.Println("DB Error:" + err.Error()) http.Error(response, err.Error(), 500) return } allDevices, err := deviceHelper.GetDevices() // Check for error when loading devices. if err != nil { http.Error(response, err.Error(), 500) return } // Retrieve the savecomplete parameter from the URL and determine if it is true or not. saveCompleteParam := request.URL.Query().Get("savecomplete") saveComplete := saveCompleteParam == "true" // Setup the data structure to pass to the page. data := struct { Action string User datastore.User AllDevices []datastore.Device SaveComplete bool }{ "deviceSettings", myuser, allDevices, saveComplete, } // Parse the page and execute the template. tpl, err := template.ParseFiles("static/main.tpl", "static/main_authenticated.tpl", "static/devices/index.tpl") // Check for error when loading devices. if err != nil { http.Error(response, err.Error(), 500) return } tpl.Execute(response, data) }
/** * Displays the user access list. */ func (t *UsersCollections) index(response http.ResponseWriter, request *http.Request) { // Get the current session user. myuser := t.Sessions.CurrentUser(response, request) // Get all users. userHelper, err := datastore.GetUserHelper() if err != nil { log.Println("DB Error:" + err.Error()) http.Error(response, err.Error(), 500) return } allUsers, err := userHelper.GetUsers() // Check for error when loading users. if err != nil { http.Error(response, err.Error(), 500) return } // Retrieve the savecomplete parameter from the URL and determine if it is true or not. saveCompleteParam := request.URL.Query().Get("savecomplete") saveComplete := saveCompleteParam == "true" // Retrieve the saveerror parameter from the URL and determine if it is true or not. saveErrorParam := request.URL.Query().Get("saveerror") saveError := saveErrorParam == "true" // Setup the data structure to pass to the page. data := struct { Action string User datastore.User AllUsers []datastore.User SaveComplete bool SaveError bool }{ "userAccessSettings", myuser, allUsers, saveComplete, saveError, } // Parse the page and execute the template. tpl, _ := template.ParseFiles("static/main.tpl", "static/main_authenticated.tpl", "static/access/index.tpl") tpl.Execute(response, data) }
/** * Handles creating a new collection. */ func (t *Collection) create(response http.ResponseWriter, request *http.Request) { // Get the current session user. myuser := t.Sessions.CurrentUser(response, request) // Setup the data structure to pass to the page. data := struct { Action string User datastore.User }{ "collectionSettings", myuser, } // Parse the page and execute the template. tpl, _ := template.ParseFiles("static/main.tpl", "static/main_authenticated.tpl", "static/collections/create.tpl") tpl.Execute(response, data) }
func (t *Root) index(response http.ResponseWriter, request *http.Request) { // Get the current session user. user := t.Sessions.CurrentUser(response, request) // If there isn't a current session user, redirect to login. if user.Username == "" { http.Redirect(response, request, "/login", http.StatusMovedPermanently) return } // Get the devices for this user. deviceDataStoreHelper, err := datastore.GetDeviceHelper() if err != nil { log.Println("DB Error:" + err.Error()) http.Error(response, err.Error(), 500) return } currentUserDevices, defaultUserDevices, err := deviceDataStoreHelper.GetDevicesForUser(user.Username) // Check for an error when loading devices. if err != nil { log.Println("Error Getting Devices:" + err.Error()) http.Error(response, err.Error(), 500) return } // Setup the data structure to pass to the page. data := struct { Action string User datastore.User CurrentDevices []datastore.Device DefaultDevices []datastore.Device }{ "index", user, currentUserDevices, defaultUserDevices, } // Parse the page and execute the template. tpl, _ := template.ParseFiles("static/main.tpl", "static/main_authenticated.tpl", "static/index.tpl") tpl.Execute(response, data) }
/** * Handles creating a new user. */ func (t *User) newuser(response http.ResponseWriter, request *http.Request) { // Get the current session user. user := t.Sessions.CurrentUser(response, request) // Setup the data structure to pass to the page. data := struct { Action string User datastore.User NewUser bool EditUser bool }{ "userSettings", user, true, false, } // Parse the page and execute the template. tpl, _ := template.ParseFiles("static/main.tpl", "static/main_authenticated.tpl", "static/users/edit.tpl") tpl.Execute(response, data) }
/** * Handles editing a user. */ func (t *User) edit(response http.ResponseWriter, request *http.Request) { // Get the current session user. user := t.Sessions.CurrentUser(response, request) // Get the user. username := mux.Vars(request)["username"] userDataStoreHelper, err := datastore.GetUserHelper() if err != nil { log.Println("DB Error:" + err.Error()) http.Error(response, err.Error(), 500) return } userToEdit, err := userDataStoreHelper.GetUser(username) // Check for error when loading user. if err != nil { http.Error(response, err.Error(), 500) return } // Setup the data structure to pass to the page. data := struct { Action string User datastore.User UserToEdit *datastore.User EditUser bool NewUser bool }{ "userSettings", user, &userToEdit, true, false, } // Parse the page and execute the template. tpl, _ := template.ParseFiles("static/main.tpl", "static/main_authenticated.tpl", "static/users/edit.tpl") tpl.Execute(response, data) }
/** * Handles adding and removing urls for the collection. */ func (t *Collection) edit(response http.ResponseWriter, request *http.Request) { // Get the current session user. myuser := t.Sessions.CurrentUser(response, request) // Get the collection. collectionName := mux.Vars(request)["collection"] filterCollectionDataStoreHelper, err := datastore.GetFilterCollectionHelper() if err != nil { log.Println("DB Error:" + err.Error()) http.Error(response, err.Error(), 500) return } collection, err := filterCollectionDataStoreHelper.GetFilterCollection(collectionName) // Check for error when loading collection. if err != nil { http.Error(response, err.Error(), 500) return } // Setup the data structure to pass to the page. data := struct { Action string User datastore.User Collection datastore.FilterCollection }{ "collectionSettings", myuser, collection, } // Parse the page and execute the template. tpl, _ := template.ParseFiles("static/main.tpl", "static/main_authenticated.tpl", "static/collections/edit.tpl") tpl.Execute(response, data) }
/** * Handles editing a user filter collections object. */ func (t *UsersCollections) edit(response http.ResponseWriter, request *http.Request) { // Get the current session user. myuser := t.Sessions.CurrentUser(response, request) // Get the user filter collections. username := mux.Vars(request)["username"] userFilterCollectionsHelper, err := datastore.GetUserFilterCollectionsHelper() if err != nil { log.Println("DB Error:" + err.Error()) http.Error(response, err.Error(), 500) return } userFilterCollections, err := userFilterCollectionsHelper.GetUserFilterCollections(username) // Check for error when loading user filter collection. if err != nil { http.Error(response, err.Error(), 500) return } // Get the user relating to the filter collection. userDataStoreHelper, err := datastore.GetUserHelper() if err != nil { log.Println("DB Error:" + err.Error()) http.Error(response, err.Error(), 500) return } filterCollectionsUser, err := userDataStoreHelper.GetUser(username) // Check for error when loading user. if err != nil { http.Error(response, err.Error(), 500) return } // Get all collections to display on the page. filterCollectionHelper, err := datastore.GetFilterCollectionHelper() allCollections, err := filterCollectionHelper.GetFilterCollections() // Check for error when loading collections. if err != nil { http.Error(response, err.Error(), 500) return } // Setup the data structure to pass to the page. data := struct { Action string User datastore.User UserFilterCollections datastore.UserFilterCollections FilterCollectionsUser *datastore.User AllCollections []datastore.FilterCollection }{ "userAccessSettings", myuser, userFilterCollections, &filterCollectionsUser, allCollections, } // Parse the page and execute the template. tpl, _ := template.ParseFiles("static/main.tpl", "static/main_authenticated.tpl", "static/access/edit.tpl") tpl.Execute(response, data) }
func (t *Internet) index(response http.ResponseWriter, request *http.Request) { // Get the current session user. myuser := t.Sessions.CurrentUser(response, request) connectionhelper, err := datastore.GetConnectionHelper() if err != nil { log.Printf("Error: Failed to get Connections Helper \"%v\"", err.Error()) http.Error(response, err.Error(), 500) return } // Get the devices. deviceHelper, err := datastore.GetDeviceHelper() if err != nil { log.Println("Error: DB Error:" + err.Error()) http.Error(response, err.Error(), 500) return } connections, err := connectionhelper.GetConnections() if err != nil { log.Printf("Error: Failed to get Connections \"%v\"", err.Error()) http.Error(response, err.Error(), 500) return } //TODO: Remove. connections = datastore.GetExampleConnections() devices, err := deviceHelper.GetDevices() // Check for error when loading devices. if err != nil { http.Error(response, err.Error(), 500) return } mappedDevices := make(map[string]datastore.Device) for _, device := range devices { mappedDevices[device.MACAddress.String()] = device } hostnames := make(map[string]string) cache, err := dnsimplementation.GetCache() if err != nil { http.Error(response, err.Error(), 500) return } for _, connection := range connections { //Lookup Source IP hostname, err := cache.GetHostname(connection.SourceIP()) if err != nil { log.Printf("Error: Looking Up Hostname \"%v\"", err.Error()) } else if hostname == "" { hostnames[connection.SourceIP().To4().String()] = connection.SourceIP().To4().String() } else { hostnames[connection.SourceIP().To4().String()] = hostname } //Lookup Destination IP hostname, err = cache.GetHostname(connection.DestinationIP()) if err != nil { log.Printf("Error: Looking Up Hostname \"%v\"", err.Error()) } else if hostname == "" { hostnames[connection.DestinationIP().To4().String()] = connection.DestinationIP().To4().String() } else { hostnames[connection.DestinationIP().To4().String()] = hostname } } connections = datastore.Connections(connections) // Setup the data structure to pass to the page. data := struct { Action string User datastore.User Connections datastore.Connections Devices map[string]datastore.Device Hostnames map[string]string }{ "internet", myuser, connections, mappedDevices, hostnames, } // Parse the page and execute the template. tpl, err := template.ParseFiles("static/main.tpl", "static/main_authenticated.tpl", "static/internet/index.tpl") if err != nil { http.Error(response, err.Error(), 500) return } err = tpl.Execute(response, data) if err != nil { log.Println(err) return } }
func (t *Root) login(response http.ResponseWriter, request *http.Request) { if request.Method != "POST" { // Retrieve the loginFailed parameter from the URL and determine if it is true or not. loginFailedParam := request.URL.Query().Get("loginFailed") loginFailed := loginFailedParam == "true" // Add the loginFailed value to a data struct to pass to the template. data := struct { LoginFailed bool }{ loginFailed, } tpl, _ := template.ParseFiles("static/main.tpl", "static/login.tpl") tpl.Execute(response, data) } else { request.ParseForm() hash := sha1.New() hash.Write([]byte(request.FormValue("password"))) passwordHash := fmt.Sprintf("%x", hash.Sum(nil)) helper, err := datastore.GetUserHelper() if err != nil { log.Println("DB Error:" + err.Error()) http.Error(response, err.Error(), 500) return } myuser, err := helper.GetUser(request.FormValue("username")) if err != nil { log.Println("Error Looking Up User:"******"" && myuser.Username == request.FormValue("username") && myuser.Password == passwordHash { //Setup our session here. t.Sessions.SetCurrentUser(response, request, myuser) remoteIP, _, err := net.SplitHostPort(request.RemoteAddr) //We now need to assosiate the user with the current device :D deviceHelper, err := datastore.GetDeviceHelper() if err != nil { log.Println("DB Error:" + err.Error()) http.Error(response, err.Error(), 500) return } device, err := deviceHelper.GetDeviceByIP(net.ParseIP(remoteIP)) if err != nil { log.Println("Error Looking Up Device:" + err.Error()) http.Error(response, err.Error(), 500) return } //Make sure the device is within our network. if device.MACAddress.String() != "" { //Device is within our network (It might have been external) device.CurrentUser = &myuser deviceHelper.SetDevice(&device) } // Login successful, display the user dashboard. t.Sessions.SessionInfo.SaveSession(response, request) http.Redirect(response, request, "/", http.StatusMovedPermanently) return } else { // Login unsuccessful, return to login screen. log.Println("Login Failed:" + myuser.Username) http.Redirect(response, request, "/login?loginFailed=true", http.StatusMovedPermanently) return } } }