-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
1,476 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package datastore | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net" | ||
|
||
"github.com/oschwald/geoip2-golang" | ||
) | ||
|
||
func (ds *DataStore) OpenGeoIP() { | ||
if ds.geoip != nil { | ||
ds.geoip.Close() | ||
ds.geoip = nil | ||
} | ||
if ds.MapConf.GeoIPPath == "" { | ||
return | ||
} | ||
var err error | ||
ds.geoip, err = geoip2.Open(ds.MapConf.GeoIPPath) | ||
if err != nil { | ||
log.Printf("Geoip open err=%v", err) | ||
} | ||
} | ||
|
||
func (ds *DataStore) GetLoc(sip string) string { | ||
if l, ok := ds.geoipMap[sip]; ok { | ||
return l | ||
} | ||
loc := "" | ||
ip := net.ParseIP(sip) | ||
if IsPrivateIP(ip) { | ||
loc = "LOCAL,0,0," | ||
} else { | ||
if ds.geoip == nil { | ||
return loc | ||
} | ||
record, err := ds.geoip.City(ip) | ||
if err == nil { | ||
loc = fmt.Sprintf("%s,%f,%f,%s", record.Country.IsoCode, record.Location.Latitude, record.Location.Longitude, record.City.Names["en"]) | ||
} else { | ||
log.Printf("getLoc err=%v", err) | ||
loc = "LOCAL,0,0," | ||
} | ||
} | ||
ds.geoipMap[sip] = loc | ||
return loc | ||
} | ||
|
||
var privateIPBlocks []*net.IPNet | ||
|
||
func IsPrivateIP(ip net.IP) bool { | ||
if !ip.IsGlobalUnicast() { | ||
return true | ||
} | ||
if len(privateIPBlocks) == 0 { | ||
for _, cidr := range []string{ | ||
"10.0.0.0/8", // RFC1918 | ||
"172.16.0.0/12", // RFC1918 | ||
"192.168.0.0/16", // RFC1918 | ||
} { | ||
_, block, err := net.ParseCIDR(cidr) | ||
if err == nil { | ||
privateIPBlocks = append(privateIPBlocks, block) | ||
} | ||
} | ||
} | ||
for _, block := range privateIPBlocks { | ||
if block.Contains(ip) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func IsGlobalUnicast(ips string) bool { | ||
ip := net.ParseIP(ips) | ||
return ip.IsGlobalUnicast() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package datastore | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"strings" | ||
) | ||
|
||
// OUI Map | ||
// Download oui.txt from | ||
// http://standards-oui.ieee.org/oui/oui.txt | ||
|
||
// LoadOUIMap : Load OUI Data from io.Reader | ||
func (ds *DataStore) LoadOUIMap(f io.Reader) error { | ||
s := bufio.NewScanner(f) | ||
for s.Scan() { | ||
l := strings.TrimSpace(s.Text()) | ||
if len(l) < 1 { | ||
continue | ||
} | ||
f := strings.Fields(l) | ||
if len(f) < 4 || f[1] != "(base" { | ||
continue | ||
} | ||
ds.ouiMap[f[0]] = strings.Join(f[3:], " ") | ||
} | ||
return nil | ||
} | ||
|
||
// FindVendor : Find Vendor Name from MAC Address | ||
func (ds *DataStore) FindVendor(mac string) string { | ||
mac = strings.TrimSpace(mac) | ||
mac = strings.ReplaceAll(mac, ":", "") | ||
mac = strings.ReplaceAll(mac, "-", "") | ||
if len(mac) > 6 { | ||
mac = strings.ToUpper(mac) | ||
if n, ok := ds.ouiMap[mac[:6]]; ok { | ||
return n | ||
} | ||
} | ||
return "Unknown" | ||
} |
Oops, something went wrong.