Exemplo n.º 1
0
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
	}
}
Exemplo n.º 2
0
/*
 * Dump the current Connections Object to See the current open connections.
 */
func (t *Developer) dumpconnections(response http.ResponseWriter, request *http.Request) {
	connectionhelper, err := datastore.GetConnectionHelper()
	if err != nil {
		log.Printf("Error: Failed to get Connections Helper \"%v\"", err.Error())
		return
	}

	connections, err := connectionhelper.GetConnections()
	if err != nil {
		log.Printf("Error: Failed to get Connections \"%v\"", err.Error())
		return
	}

	log.Printf("Connections: \n %v \n", connections)
}
Exemplo n.º 3
0
func (t *connectionManager) Process() error {

	var wg sync.WaitGroup

	for i := 0; i < t.configuration.Manager.Agents; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()

			for {
				message := <-t.reader.output

				ipheader, err := ipv4.ParseHeader(message)
				if err != nil {
					log.Println("Error: Parsing IPHeader:" + err.Error())
					log.Printf("Message:%v\n", message)
					continue
				}

				if int(ipheader.Flags) < 2 {
					log.Printf("Warning: More Fragments Flag Is Set\n")
				}

				tcpheader, err := tcp.ParseHeader(message[ipheader.Len:])
				if err != nil {
					log.Println("Error: Parsing TCPHeader:" + err.Error())
					log.Printf("Message:%v\n", message)
					continue
				}

				connectionhelper, err := datastore.GetConnectionHelper()
				if err != nil {
					log.Printf("Error: Getting Connection Helper: %v\n", err.Error())
					return
				}

				connectionhelper.Lock()
				connection, err := connectionhelper.GetConnection(ipheader.Src, tcpheader.SourcePort, ipheader.Dst, tcpheader.DestinationPort)
				if err != nil {
					log.Printf("Error: Getting Connection: %v\n", err.Error())
					connectionhelper.Unlock()
					return
				}

				if bytes.Equal(connection.SourceIP(), net.IP{}) {
					//No connection found
					connection, err = datastore.NewConnectionFromPacketAndIPHeaderAndTCPHeader(message, ipheader, tcpheader)
					if err != nil {
						log.Printf("Error: Creating New Connection: %v\n", err.Error())
						connectionhelper.Unlock()
						continue
					}

					err = connectionhelper.SetConnection(connection)
					if err != nil {
						log.Printf("Error: Saving New Connection: %v\n", err.Error())
						connectionhelper.Unlock()
						continue
					}

				} else {
					//Connection found
					//We need to find out if the connection is still for this device and user
					devicehelper, err := datastore.GetDeviceHelper()
					if err != nil {
						log.Printf("Error: Getting Device Helper: %v\n", err.Error())
						connectionhelper.Unlock()
						return
					}

					device, err := devicehelper.GetDeviceByIP(ipheader.Src)
					if err != nil {
						log.Printf("Error: Getting Source Device By IP(%v) \"%v\"\n", ipheader.Src.String(), err.Error())
					}

					if bytes.Equal(device.MACAddress, net.HardwareAddr{}) {
						device, err = devicehelper.GetDeviceByIP(ipheader.Dst)
						if err != nil {
							log.Printf("Error: Getting Destination Device By IP(%v) \"%v\"\n", ipheader.Dst.String(), err.Error())
						}
					}

					//If we have a device, is now or it's empty
					currentuser := ""
					if device.GetActiveUser() != nil {
						currentuser = device.GetActiveUser().Username
					}

					//Device, User or Number of In Memory Packets Breached...
					if !bytes.Equal(device.MACAddress, connection.DeviceID()) || (connection.Username() != currentuser) || connection.NumberOfPackets() >= t.configuration.Manager.MaxPackets {
						//Device or User has changed.
						err = connectionhelper.RemoveConnection(connection)
						if err != nil {
							log.Printf("Error: Removing Connection: %v\n", err.Error())
							connectionhelper.Unlock()
							continue
						}

						//If a channel is registered and we had no errors closing it.
						if t.manager.output != nil {
							select {
							case t.manager.output <- connection:
							default:
								log.Println("Error: Manager Queue is full! Discarding Closed Connection..")
							}

						}

						//Create A New Connection
						connection, err := datastore.NewConnectionFromPacketAndIPHeaderAndTCPHeader(message, ipheader, tcpheader)
						if err != nil {
							log.Printf("Error: Creating New Connection After Remove: %v\n", err.Error())
							connectionhelper.Unlock()
							continue
						}

						err = connectionhelper.SetConnection(connection)
						if err != nil {
							log.Printf("Error: Saving New Connection After Remove: %v\n", err.Error())
							connectionhelper.Unlock()
							continue
						}
					} else {
						err = connection.AddPacket(message)
						if err != nil {
							log.Printf("Error: Adding Packet: %v\n", err.Error())
							connectionhelper.Unlock()
							continue
						}

						err = connectionhelper.SetConnection(connection)
						if err != nil {
							log.Printf("Error: Saving Packet after adding packet: %v\n", err.Error())
							connectionhelper.Unlock()
							continue
						}
					}

				}
				connectionhelper.Unlock()
			}

		}()
	}

	wg.Wait()
	log.Println("Error: We seem to be existing packet processing??")
	return errors.New("Error: Packet Processing Exited?")
}
Exemplo n.º 4
0
/*
Garbage Collection service for detecting connections that have closed.
*/
func (t *connectionManager) GC() error {

	nextrun := time.Now().Add(time.Second * time.Duration(t.configuration.Manager.Timeout))

	for {
		select {
		case <-time.After(time.Second * time.Duration(t.configuration.Manager.Timeout)):
		case <-time.After(nextrun.Sub(time.Now())):
		}

		//Run GC
		connectionhelper, err := datastore.GetConnectionHelper()
		if err != nil {
			log.Printf("Error: Getting GC Connection Helper: %v\n", err.Error())
			return err
		}

		//We don't want to lock the connections if we can help it.
		connections, err := connectionhelper.GetConnections()
		if err != nil {
			log.Printf("Error: Getting GC Connections: %v\n", err.Error())
			return err
		}

		for i := range connections {
			if connections[i].Updated().Add(time.Second * time.Duration(t.configuration.Manager.Timeout)).Before(time.Now()) {
				//We should GC this record.
				//Lock The records
				connectionhelper.Lock()
				//We Have to get the connection again as it might have changed.
				//Use the get connection as although it's just a loop again we only optimise one place.
				connection, err := connectionhelper.GetConnection(connections[i].SourceIP(), connections[i].SourcePort(), connections[i].DestinationIP(), connections[i].DestinationPort())
				if err != nil {
					log.Printf("Error: Getting GC Connection: %v\n", err.Error())
					connectionhelper.Unlock()
					continue
				}

				if !connection.SourceIP().Equal(connections[i].SourceIP()) {
					//The connection was removed by another thread.
					connectionhelper.Unlock()
					continue
				}

				//If the connection hasn't been updated by another thread.
				if connection.Updated().Add(time.Second * time.Duration(t.configuration.Manager.Timeout)).Before(time.Now()) {
					err := connectionhelper.RemoveConnection(connection)
					if err != nil {
						log.Printf("Error: GCing Connection: %v\n", err.Error())
					}

					//Pass closed connections on if they are wanted.
					if t.manager.output != nil {
						select {
						case t.manager.output <- connection:
						default:
							log.Println("Error: Manager Queue is full! Discarding Closed Connection..")
						}

					}

				}

				connectionhelper.Unlock()
			}

			nextrun = time.Now().Add(time.Second * time.Duration(t.configuration.Manager.Timeout))
			//We need to workout if nextrun should be less that the normal 15 seconds.
			if connections[i].Updated().Add(time.Second * time.Duration(t.configuration.Manager.Timeout)).Before(nextrun) {
				nextrun = connections[i].Updated().Add(time.Second * time.Duration(t.configuration.Manager.Timeout))
			}
		}
	}
	return nil
}
Exemplo n.º 5
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
	}

}