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 }
func StartDHCPService(configuration *Configuration) { //This allows us to deactivate DHCP if configuration.DHCP.Disabled { log.Println("Warning: DHCP is disabled by configuration (config.json)") return } leasePool, err := datastore.GetLeaseHelper() if err != nil { log.Fatal("Unable to Start Lease Pool") } //Lets work out the number of leases numLeases := dhcp4.IPRange(net.ParseIP(configuration.DHCP.Leases.Start), net.ParseIP(configuration.DHCP.Leases.End)) //Lets add a list of IPs to the pool these will be served to the clients so make sure they work for you. //So Create Array of IPs 192.168.1.1 to 192.168.1.30 for i := 0; i < numLeases; i++ { err = leasePool.AddLease(leasepool.Lease{IP: dhcp4.IPAdd(net.ParseIP(configuration.DHCP.Leases.Start), i)}) if err != nil { log.Fatalln("Error Adding IPs to pool:" + err.Error()) } } //If we don't have an IP configured then use the first Active IP interface (warn if there is more than 1) if configuration.DHCP.Server.IP == nil || configuration.DHCP.Server.IP.Equal(net.IPv4(0, 0, 0, 0)) { //Get IP And Subnet (Windows doesn't seem to give subnet) mainActiveIPNetwork, err := networktools.MainActiveIPNetwork() if err != nil { log.Fatalln("Error Getting Local Interfaces:" + err.Error()) } if mainActiveIPNetwork.IP.Equal(net.IP{}) { log.Fatal("No Interface Found With a Valid IP") } configuration.DHCP.Server.IP = mainActiveIPNetwork.IP if !bytes.Equal(mainActiveIPNetwork.Mask, net.IPMask{}) && (configuration.DHCP.Server.SubnetMask == nil || configuration.DHCP.Server.SubnetMask.Equal(net.IPv4(0, 0, 0, 0))) { configuration.DHCP.Server.SubnetMask = net.ParseIP(mainActiveIPNetwork.Mask.String()) } } //Make Sure we Now have a Subnet Mask if configuration.DHCP.Server.SubnetMask == nil || configuration.DHCP.Server.SubnetMask.Equal(net.IPv4(0, 0, 0, 0)) { log.Fatalln("DHCP SubnetMask Missing in Configuration and Unable to Calculate...") } //If For some reason We've setup alternative DNS servers (I Can't think why we should) use them otherwise add ourself. if configuration.DHCP.Server.DNSServers == nil || len(configuration.DHCP.Server.DNSServers) == 0 { configuration.DHCP.Server.DNSServers = append(configuration.DHCP.Server.DNSServers, configuration.DHCP.Server.IP) } else { log.Printf("Warning: Using Preconfigured DNS Servers %v\n", configuration.DHCP.Server.DNSServers) } //If For some reason We've setup alternative Gateway (I Can't think why we should) use them otherwise add ourself. if configuration.DHCP.Server.DefaultGateway == nil { configuration.DHCP.Server.DefaultGateway = configuration.DHCP.Server.IP } else { log.Printf("Warning: Using Preconfigured Default Gateway %v\n", configuration.DHCP.Server.DefaultGateway) } //Create The Server myServer := dhcp4server.Server{} myServer.Configuration = &configuration.DHCP.Server myServer.LeasePool = leasePool //Start GC on the Leases. go func() { for { select { case <-time.After(configuration.DHCP.Server.LeaseDuration): } err := myServer.GC() if err != nil { log.Printf("Error: DHCP GC Failed Error: \"%v\"\n", err) return } } }() //Start the Server... err = myServer.ListenAndServe() if err != nil { log.Fatalln("Error Starting Server:" + err.Error()) } }