-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
144 lines (121 loc) · 3.22 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"sync"
"time"
"github.com/Pallinder/go-randomdata"
"github.com/d1str0/hpfeeds"
"github.com/google/uuid"
"github.com/threatstream/agave"
)
const Version = "v0.2.4"
const AgaveApp = "Drupot"
type App struct {
Publish chan []byte
SeenIPLock *sync.RWMutex
SeenIP map[string]bool
SensorIP string
Config *AppConfig
SensorUUID string
Agave *agave.Client
}
func main() {
fmt.Println("///- Running Drupot")
fmt.Printf("///- %s\n", Version)
// All we take is a config file argument.
var configFilename string
flag.StringVar(&configFilename, "c", "config.toml", "load given config file")
flag.Parse()
fmt.Printf("//- Loading config file: %s\n", configFilename)
config := loadConfig(configFilename)
var app App
app.SeenIPLock = &sync.RWMutex{}
app.SensorIP = "127.0.0.1" // Default will be overwritten if public IP set to fetch.
app.Config = config
app.SeenIP = make(map[string]bool)
app.Publish = make(chan []byte)
if app.Config.Drupal.NameRandomizer {
app.Config.Drupal.SiteName = randomdata.SillyName()
}
// TODO: Persist UUID. Maybe a command line flag to refresh or overwrite.
uuid, err := uuid.NewUUID()
if err != nil {
log.Fatalf("Error generating UUID: %s\n", err.Error())
}
app.SensorUUID = uuid.String()
if config.Hpfeeds.Enabled {
enableHpfeeds(app)
}
if config.PublicIP.Enabled {
ip, err := getPublicIP(config.PublicIP)
if err != nil {
log.Fatalf("Error getting public IP: %s\n", err.Error())
}
app.SensorIP = ip
}
app.Agave = agave.NewClient(AgaveApp, config.Hpfeeds.Channel, app.SensorUUID, app.SensorIP, config.Drupal.Port)
// Load routes for the server
mux := routes(app)
addr := fmt.Sprintf(":%d", config.Drupal.Port)
s := http.Server{
Addr: addr,
Handler: mux,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
}
log.Fatal(s.ListenAndServe())
}
func enableHpfeeds(app App) {
c := app.Config.Hpfeeds
fmt.Printf("/- Connecting to hpfeeds server: %s\n", c.Host)
fmt.Printf("/-\tPort: %d\n", c.Port)
fmt.Printf("/-\tIdent: %s\n", c.Ident)
fmt.Printf("/-\tAuth: %s\n", c.Auth)
fmt.Printf("/-\tChannel: %s\n", c.Channel)
client := hpfeeds.NewClient(c.Host, c.Port, c.Ident, c.Auth)
go func() {
for {
err := client.Connect()
if err != nil {
log.Fatalf("Error connecting to hpfeeds server: %s\n", err.Error())
time.Sleep(5 * time.Second)
continue
}
client.Publish(c.Channel, app.Publish)
<-client.Disconnected
fmt.Printf("Attempting to reconnect...\n")
}
}()
}
// getPublicIP goes through a list of URLs to
func getPublicIP(c *PublicIPConfig) (string, error) {
var ip net.IP
for _, site := range c.URLs {
resp, err := http.Get(site)
if err != nil {
log.Print(err)
continue
}
defer resp.Body.Close()
var body []byte
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
// Try and parse response into a valid IP.
ip = net.ParseIP(string(body))
// ip will be nil if the parsing fails.
// if not nil, succesfully got back an IP and parsed it
if ip != nil {
return ip.String(), nil
}
}
return "", errors.New("Unable to get public IP")
}