This repository has been archived by the owner on Jan 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalboa.go
134 lines (120 loc) · 3.08 KB
/
balboa.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
package main
import (
"errors"
"flag"
"fmt"
"log"
"net"
"os"
"os/signal"
"strconv"
"strings"
"github.com/D4-project/d4-golang-utils/config"
"github.com/gomodule/redigo/redis"
)
type (
conf struct {
redisHost string
redisPort string
redisDB int
redisQueue string
balboaSocket string
}
)
var (
confdir = flag.String("c", "conf.sample", "configuration directory")
connectRedis = true
cr redis.Conn
)
func main() {
// Control Chan
s := make(chan os.Signal, 1)
signal.Notify(s, os.Interrupt, os.Kill)
// Usage and flags
flag.Usage = func() {
fmt.Printf("analyzer-d4-balboa - export D4 Type 8 to Balboa UNIX socket:\n\n")
fmt.Printf("\n")
fmt.Printf("Usage:\n\n analyzer-d4-balboa -c config_directory\n")
fmt.Printf("\n")
fmt.Printf("Configuration:\n\n")
fmt.Printf(" The configuration settings are stored in files in the configuration directory\n")
fmt.Printf(" specified with the -c command line switch.\n\n")
fmt.Printf("Files in the configuration directory:\n")
fmt.Printf("\n")
fmt.Printf(" redis - d4 server\n")
fmt.Printf(" | host:port/db\n")
fmt.Printf(" redis_queue - uuid of the redis queue\n")
fmt.Printf(" | uuid \n")
fmt.Printf(" balboa_socket - socket file to balboa\n")
fmt.Printf(" | /tmp/balboa.sock\n")
fmt.Printf("\n")
flag.PrintDefaults()
}
// Config
c := conf{}
flag.Parse()
if flag.NFlag() == 0 || *confdir == "" {
flag.Usage()
os.Exit(1)
} else {
*confdir = strings.TrimSuffix(*confdir, "/")
*confdir = strings.TrimSuffix(*confdir, "\\")
}
// Parse Redis Config
tmp := config.ReadConfigFile(*confdir, "redis")
ss := strings.Split(string(tmp), "/")
if len(ss) <= 1 {
log.Fatal("Missing Database in Redis config: should be host:port/database_name")
}
c.redisDB, _ = strconv.Atoi(ss[1])
var ret bool
ret, ss[0] = config.IsNet(ss[0])
if !ret {
sss := strings.Split(string(ss[0]), ":")
c.redisHost = sss[0]
c.redisPort = sss[1]
}
c.redisQueue = string(config.ReadConfigFile(*confdir, "redis_queue"))
c.balboaSocket = string(config.ReadConfigFile(*confdir, "balboa_socket"))
initRedis(c.redisHost, c.redisPort, c.redisDB)
defer cr.Close()
addr, err := net.ResolveUnixAddr("unix", c.balboaSocket)
if err != nil {
log.Fatalf("Failed to resolve: %v\n", err)
}
cs, err := net.DialUnix("unix", nil, addr)
if err != nil {
log.Fatalf("Failed to dial: %v\n", err)
}
defer cs.Close()
// pop redis queue
for {
dnsLine, err := redis.String(cr.Do("LPOP", "analyzer:8:"+c.redisQueue))
dnsLine = fmt.Sprintf("%s%s", dnsLine, "\n")
if err != nil {
fmt.Println("Queue processed.")
os.Exit(0)
}
// Write in Balboa socket
if _, err := cs.Write([]byte(dnsLine)); err != nil {
fmt.Printf("Write Error: %v\n", err)
} else {
fmt.Printf("Write: %v", dnsLine)
}
// Exit Signal Handle
select {
case <-s:
fmt.Println("Exiting...")
os.Exit(0)
default:
continue
}
}
}
func initRedis(host string, port string, d int) {
err := errors.New("")
cr, err = redis.Dial("tcp", host+":"+port, redis.DialDatabase(d))
if err != nil {
panic(err)
}
}