Ejemplo n.º 1
0
func StartDNSService(configuration *dnsforwarder.Configuration) {
	cache, err := dnsimplementation.GetCache()
	if err != nil {
		log.Printf("Error: DNS Cache:%v\n", err)
	}

	hosts := dnsimplementation.Hosts{}
	hosts.Devices = make(map[string]net.IP)

	activeIPs, err := networktools.ActiveIPInterfaces()
	if err == nil {
		for _, activeIP := range activeIPs {
			switch activeIP.(type) {
			case *net.IPAddr:
				hosts.Add("cloudpathway", activeIP.(*net.IPAddr).IP)
				break
			case *net.IPNet:
				hosts.Add("cloudpathway", activeIP.(*net.IPNet).IP)
				break
			}
		}
	} else {
		log.Printf("Warning: Error Getting Active Interfaces:" + err.Error())
	}

	server := dnsforwarder.Server{}
	server.Configuration = configuration
	server.Cache = cache
	server.Hosts = &hosts
	server.Hijacker = dnsimplementation.Hijack

	//Start GC on the DNS Cache.
	go func() {
		for {
			select {
			case <-time.After(24 * time.Hour):
			}

			err := cache.GC()
			if err != nil {
				log.Printf("Error: DNS GC Failed Error: \"%v\"\n", err)
				return
			}
		}
	}()

	go func() {
		err := server.ListenAndServeUDP(net.UDPAddr{IP: net.IPv4(0, 0, 0, 0), Port: 53})
		if err != nil {
			log.Printf("UDP Error:%v\n", err)
		}
	}()

	err = server.ListenAndServeTCP(net.TCPAddr{IP: net.IPv4(0, 0, 0, 0), Port: 53})
	if err != nil {
		log.Printf("TCP Error:%v\n", err)
	}
}
Ejemplo n.º 2
0
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
	}

}