-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Packet-Clearing-House/mysql-customer-db
Mysql customer db
- Loading branch information
Showing
12 changed files
with
239 additions
and
151 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# Binaries for programs and plugins | ||
.DS_Store | ||
*.exe | ||
*.dll | ||
*.so | ||
|
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 |
---|---|---|
|
@@ -2,46 +2,77 @@ package main | |
|
||
import ( | ||
"database/sql" | ||
"log" | ||
"github.com/asergeyev/nradix" | ||
"sync" | ||
_ "github.com/go-sql-driver/mysql" | ||
radix "github.com/armon/go-radix" | ||
_"github.com/lib/pq" | ||
) | ||
|
||
type Customer struct { | ||
Prefix string | ||
Name string | ||
PrefixMonit bool | ||
ASNMonit bool | ||
} | ||
var DB_URL = "root:pass@(127.0.0.1)/customers" | ||
|
||
var DB_URL = "postgres://[email protected]/pipeline?sslmode=disable" | ||
// Function that reverse a word (test.com -> moc.tset) | ||
func reverse(s string) string { | ||
r := []rune(s) | ||
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 { | ||
r[i], r[j] = r[j], r[i] | ||
} | ||
return string(r) | ||
} | ||
|
||
func getCustomerTree() (*nradix.Tree, error) { | ||
type CustomerDB struct { | ||
*sync.Mutex | ||
dburl string | ||
tree *radix.Tree | ||
} | ||
|
||
tree := nradix.NewTree(0) | ||
// Resolve the customer name from DNS qname | ||
// Returns Unknown if not found | ||
func (c *CustomerDB) Resolve(qname string) string { | ||
|
||
name := "Unknown" | ||
c.Lock() | ||
_, value, found := c.tree.LongestPrefix(reverse(qname)) | ||
c.Unlock() | ||
if found { | ||
name = value.(string) | ||
} | ||
return name | ||
} | ||
|
||
db, err := sql.Open("postgres", DB_URL) | ||
func (c *CustomerDB) Refresh() error { | ||
|
||
tree := radix.New() | ||
mysql, err := sql.Open("mysql", DB_URL) | ||
if err != nil { | ||
return nil, err | ||
return err | ||
} | ||
|
||
rows, err := db.Query("SELECT ip::cidr, name, asn, prefix FROM ns_customers;") | ||
rows, err := mysql.Query("SELECT group_name, zone FROM zones;") | ||
if err != nil { | ||
return nil, err | ||
return err | ||
} | ||
defer rows.Close() | ||
|
||
|
||
for rows.Next() { | ||
c := Customer{} | ||
err := rows.Scan(&c.Prefix, &c.Name, &c.ASNMonit, &c.PrefixMonit) | ||
var name, zone string | ||
err := rows.Scan(&name, &zone) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
err = tree.AddCIDR(c.Prefix, &c) | ||
if err != nil { | ||
log.Println(err) | ||
return err | ||
} | ||
tree.Insert(reverse(zone), name) | ||
} | ||
|
||
c.Lock() | ||
c.tree = tree | ||
c.Unlock() | ||
return nil | ||
} | ||
|
||
// Init the customer DB. Connects to mysql to fetch all data and build a radix tree | ||
func NewCustomerDB(path string) (*CustomerDB) { | ||
return &CustomerDB{ | ||
new(sync.Mutex), | ||
path, | ||
radix.New(), | ||
} | ||
return tree, nil | ||
} |
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 |
---|---|---|
@@ -1,13 +1,11 @@ | ||
customer-db = "postgres://user:[email protected]/postgres?sslmode=disable" | ||
influx-db = "http://127.0.0.1:8086/write?db=authdns" | ||
watch-dir = "/home/user/count" | ||
# URL for the MySQL instance to retreive customers | ||
customer-db = "root:pass@(127.0.0.1)/customers" | ||
|
||
# Refreshing interval (hours) of the customer database. | ||
customer-refresh = 10 | ||
|
||
#[bgp] | ||
# router-id = “116.121.4.10" | ||
# local-as = 53 | ||
# peer-addr = “176.53.11.25" | ||
# peer-as = 12 | ||
# connect-port = 179 | ||
# listen-port = 179 # need sudo to run (privilege port) | ||
# remove-as-from-path = [12, 4234] | ||
# The URL of the influx DB instance. | ||
influx-db = "http://127.0.0.1:8086/write?db=authdns" | ||
|
||
# The directory DNSAuth should watch for new log files coming in. | ||
watch-dir = "./" |
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
20 changes: 0 additions & 20 deletions
20
DNSAuth/tests/SZC_mon-01.lga.example.com_2018-02-25.05-32.dmp
This file was deleted.
Oops, something went wrong.
Binary file not shown.
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,2 @@ | ||
Q 2fd9:4d55:875b:bae4:b46f:51e1:5388:22a6 1501:0121:0800:0000:: 1 0 2 auction.com. 60 | ||
R 103.76.246.187 123.99.248.35 0 0 2 blabla.test.com. 595 0 |
Binary file not shown.
Oops, something went wrong.