-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
58 lines (45 loc) · 1.09 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"github.com/miekg/dns"
g "github.com/pulsejet/geoipns/geoip"
)
type config struct {
g.Config
Address string `json:"address"`
Suffix string `json:"suffix"`
}
var mConfig config
// SetupEnvironment loads conig and initializes databases
func SetupEnvironment(configFile string) {
// Open config file
jsonFile, err := os.Open(configFile)
if err != nil {
log.Fatal(err)
}
// Read config
byteValue, _ := ioutil.ReadAll(jsonFile)
json.Unmarshal(byteValue, &mConfig)
// Setup Engine
g.SetupEngine(&mConfig.Config)
}
func main() {
// Set everything up
SetupEnvironment("config.json")
// Attach handler function
dns.HandleFunc(mConfig.Suffix+".", handleDNSRequest)
// Set up server
server := &dns.Server{Addr: mConfig.Address, Net: "udp"}
// Log that we are starting server
log.Println("Starting Geolocation DNS server at", mConfig.Address)
// Start listening
err := server.ListenAndServe()
// Shutdown when done
defer server.Shutdown()
if err != nil {
log.Fatalf("Failed to start server: %s\n ", err.Error())
}
}