Exemplo n.º 1
0
/*
 * Import the collections directory and Import the files as collection lists.
 */
func (t *Developer) importcollections(response http.ResponseWriter, request *http.Request) {
	filepath.Walk("import/collections/", func(path string, info os.FileInfo, err error) error {
		if !info.IsDir() {
			file, err := os.Open(path)
			defer file.Close()

			scanner := bufio.NewScanner(file)

			collectionHelper, err := datastore.GetFilterCollectionHelper()
			if err != nil {
				log.Println("Error: Getting Helper Error:" + err.Error())
				http.Error(response, err.Error(), 500)
				return err
			}

			collection, err := collectionHelper.GetFilterCollection(info.Name())
			if err != nil {
				log.Println("Error: Getting Collection:" + err.Error())
				http.Error(response, err.Error(), 500)
				return err
			}

			collection.Name = info.Name()

			if collection.Domains == nil {
				collection.Domains = make([]string, 0, 0)
				for scanner.Scan() {
					collection.Domains = append(collection.Domains, scanner.Text())
				}
			} else {
				for scanner.Scan() {
					for _, existingDomain := range collection.Domains {
						if existingDomain == scanner.Text() {
							//Yeah lets goto! Aka Break 2
							goto Break2
						}
					}
					collection.Domains = append(collection.Domains, scanner.Text())

				Break2:
				}
			}

			if err := scanner.Err(); err != nil {
				log.Println("Warning: Importing Collection \"" + path + "\":" + err.Error())
			}

			err = collectionHelper.SetFilterCollection(collection)
			if err != nil {
				log.Println("Warning: Saving Collection \"" + collection.Name + "\":" + err.Error())
			}
		}
		return nil
	})
}
Exemplo n.º 2
0
/*
 * Check if a user has access to a domain using the domain look up (Which should be quicker)
 */
func (t *Developer) quickaccess(response http.ResponseWriter, request *http.Request) {
	log.Println("Started")

	userFilterCollectionsHelper, err := datastore.GetUserFilterCollectionsHelper()
	if err != nil {
		log.Println("Error:" + err.Error())
		http.Error(response, err.Error(), 500)
		return
	}

	userFilterCollections, err := userFilterCollectionsHelper.GetUserFilterCollections(mux.Vars(request)["username"])
	if err != nil {
		log.Println("Error:" + err.Error())
		http.Error(response, err.Error(), 500)
		return
	}

	filterCollectionHelper, err := datastore.GetFilterCollectionHelper()
	if err != nil {
		log.Println("Error:" + err.Error())
		http.Error(response, err.Error(), 500)
		return
	}

	inCollections, err := filterCollectionHelper.GetFilterCollectionsWithDomain(mux.Vars(request)["domain"])
	if err != nil {
		log.Println("Error:" + err.Error())
		http.Error(response, err.Error(), 500)
		return
	}

	for _, collection := range inCollections {
		for _, userCollectionName := range userFilterCollections.Collections {
			if userCollectionName == collection.Name {
				log.Println("Blocked")
				return
			}
		}
	}

	log.Println("Not Blocked")
}
Exemplo n.º 3
0
/**
 * Displays the list of collections.
 */
func (t *Collection) index(response http.ResponseWriter, request *http.Request) {
	// Get the current session user.
	myuser := t.Sessions.CurrentUser(response, request)

	// Get all collections.
	filterCollectionDataStoreHelper, err := datastore.GetFilterCollectionHelper()
	allCollections, err := filterCollectionDataStoreHelper.GetFilterCollections()

	// Check for error when loading collections.
	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
		AllCollections []datastore.FilterCollection
		SaveComplete   bool
		SaveError      bool
	}{
		"collectionSettings",
		myuser,
		allCollections,
		saveComplete,
		saveError,
	}

	// Parse the page and execute the template.
	tpl, _ := template.ParseFiles("static/main.tpl", "static/main_authenticated.tpl", "static/collections/index.tpl")
	tpl.Execute(response, data)
}
Exemplo n.º 4
0
/**
 * Handles deleting a collection.
 */
func (t *Collection) remove(response http.ResponseWriter, request *http.Request) {
	// Try and load a filter collection using the collection name.
	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
	}

	err = filterCollectionDataStoreHelper.DeleteFilterCollection(collectionName)

	// Check for error when deleting collection.
	if err != nil {
		http.Error(response, err.Error(), 500)
		return
	}

	http.Redirect(response, request, t.Base(), http.StatusMovedPermanently)
}
Exemplo n.º 5
0
/**
 * 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)
}
Exemplo n.º 6
0
/**
 * Gets the list of blocked sites that match the given filter and returns as JSON response.
 */
func (t *Collection) getFilterSiteList(response http.ResponseWriter, request *http.Request) {
	name := request.FormValue("collection")
	filter := request.FormValue("filter")
	log.Println(name)
	log.Println(filter)
	filterCollectionDataStoreHelper, err := datastore.GetFilterCollectionHelper()
	collection, err := filterCollectionDataStoreHelper.GetFilterCollection(name)

	// Check for error when loading collection.
	if err != nil {
		http.Error(response, err.Error(), 500)
		return
	}

	// Setup the data structure to return.
	data := struct {
		Sites []string
	}{
		collection.Domains,
	}

	enc := json.NewEncoder(response)
	enc.Encode(data)
}
Exemplo n.º 7
0
/**
 * Handles saving a collection.
 */
func (t *Collection) save(response http.ResponseWriter, request *http.Request) {
	filterCollectionDataStoreHelper, err := datastore.GetFilterCollectionHelper()

	if err != nil {
		log.Println("DB Error:" + err.Error())
		http.Error(response, err.Error(), 500)
		return
	}

	collection := datastore.FilterCollection{}
	collection.Name = request.FormValue("name")

	// Save the collection.
	err = filterCollectionDataStoreHelper.SetFilterCollection(collection)

	// Check for error when saving collection.
	if err != nil {
		// There was an error, so report that to the screen.
		http.Redirect(response, request, "/settings_collections.html?saveerror=true", http.StatusMovedPermanently)
	} else {
		// No error, so report a successful save to the screen
		http.Redirect(response, request, t.Base()+"?savecomplete=true", http.StatusMovedPermanently)
	}
}
Exemplo n.º 8
0
func Hijack(response dns.ResponseWriter, message *dns.Msg) (bool, error) {

	if (message.Question[0].Qtype == dns.TypeA || message.Question[0].Qtype == dns.TypeAAAA) && message.Question[0].Qclass == dns.ClassINET {

		ipString, _, err := net.SplitHostPort(response.RemoteAddr().String())
		if err != nil {
			log.Println("Warning: Received No IP4 Address:" + response.RemoteAddr().String())
			return false, err
		}

		ip := net.ParseIP(ipString)
		deviceHelper, err := datastore.GetDeviceHelper()
		if err != nil {
			log.Println("Error: Datastore Error (Open Access):" + err.Error())
			return false, err
		}

		device, err := deviceHelper.GetDeviceByIP(ip)
		if err != nil {
			log.Println("Error: Datastore Error (Open Access):" + err.Error())
			return false, err
		}

		if bytes.Equal(device.MACAddress, net.HardwareAddr{}) {
			//Device Doesn't exists?
			log.Println("Warning: IP:" + ipString + " MAC:" + device.MACAddress.String() + " Doesn't Exists As A Local Device But is Using Our DNS??")
			return false, nil
		} else {
			if activeUser := device.GetActiveUser(); activeUser != nil {
				//User is active check they have access.
				userFiltercollectionsHelper, err := datastore.GetUserFilterCollectionsHelper()
				if err != nil {
					return false, err
				}

				usersFiltercollection, err := userFiltercollectionsHelper.GetUserFilterCollections(activeUser.Username)
				if err != nil {
					return false, err
				}

				if usersFiltercollection.Username != "" {
					//Get the collections helper.
					filterCollectionsHelper, err := datastore.GetFilterCollectionHelper()
					if err != nil {
						return false, err
					}

					//Start Moving down the domain (i.e. removing subdomains etc)
					collections := make([]datastore.FilterCollection, 0)

					domain := strings.TrimSuffix(message.Question[0].Name, ".")

					for {
						additionalCollections, err := filterCollectionsHelper.GetFilterCollectionsWithDomain(domain)
						if err != nil {
							return false, err
						}

						if len(additionalCollections) > 0 {
							collections = append(collections, additionalCollections...)
						}

						if len(strings.SplitAfterN(domain, ".", 2)) == 2 {
							domain = strings.SplitAfterN(domain, ".", 2)[1]
						} else {
							break
						}
					}

					//Does the domain appear in any collections??
					for _, collection := range collections {
						if usersFiltercollection.ContainsCollection(collection.Name) {
							//The Url Should be blocked...
							//For Now lets just not respond with a DNS record...

							//localResponse := new(dns.Msg)
							//localResponse.SetReply(message)

							//rr_header := dns.RR_Header{Name: message.Question[0].Name, Class: dns.ClassINET, Ttl: 0}

							//Main Active IP
							//mainIPNetwork, err := networktools.MainActiveIPNetwork()
							//if err != nil {
							//	return false, err
							//}

							//switch message.Question[0].Qtype {

							//case dns.TypeA:
							//	rr_header.Rrtype = dns.TypeA
							//	a := &dns.A{rr_header, mainIPNetwork.IP}
							//	localResponse.Answer = append(localResponse.Answer, a)

							//case dns.TypeAAAA:
							//	rr_header.Rrtype = dns.TypeAAAA
							//}

							//err = response.WriteMsg(localResponse)
							//if err != nil {
							//	return false, err
							//}
							return true, nil
						}
					}

					return false, nil
				}
			} else {
				//User is not active redirect to login page.
				localResponse := new(dns.Msg)
				localResponse.SetReply(message)
				rr_header := dns.RR_Header{Name: message.Question[0].Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 0}
				//Main Active IP
				mainIPNetwork, err := networktools.MainActiveIPNetwork()
				if err != nil {
					return false, err
				}
				a := &dns.A{rr_header, mainIPNetwork.IP}
				localResponse.Answer = append(localResponse.Answer, a)
				err = response.WriteMsg(localResponse)
				if err != nil {
					return false, err
				}
				return true, nil
			}
		}
	}

	return false, nil
}
/**
 * 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)
}