Skip to content

Commit 015ece0

Browse files
authored
Merge pull request #7 from D4-project/modular
Modular
2 parents dd42054 + 2db3aeb commit 015ece0

File tree

3 files changed

+13
-84
lines changed

3 files changed

+13
-84
lines changed

d4-goclient.go

Lines changed: 4 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ import (
1616
"net"
1717
"os"
1818
"os/signal"
19-
"regexp"
2019
"strconv"
2120
"strings"
2221
"time"
2322

24-
//BSD 3
25-
uuid "github.com/satori/go.uuid"
23+
config "github.com/D4-project/d4-golang-utils/config"
24+
uuid "github.com/D4-project/d4-golang-utils/crypto/hash"
2625
)
2726

2827
const (
@@ -234,25 +233,7 @@ func d4Copy(d4 *d4S, c chan string, k chan string) {
234233
}
235234

236235
func readConfFile(d4 *d4S, fileName string) []byte {
237-
f, err := os.OpenFile((*d4).confdir+"/"+fileName, os.O_RDWR|os.O_CREATE, 0666)
238-
defer f.Close()
239-
if err != nil {
240-
log.Fatal(err)
241-
}
242-
data := make([]byte, 100)
243-
count, err := f.Read(data)
244-
if err != nil {
245-
if err != io.EOF {
246-
log.Fatal(err)
247-
}
248-
}
249-
infof(fmt.Sprintf("read %d bytes: %q\n", count, data[:count]))
250-
if err := f.Close(); err != nil {
251-
log.Fatal(err)
252-
}
253-
// trim \r and \n if present
254-
r := bytes.TrimSuffix(data[:count], []byte("\n"))
255-
return bytes.TrimSuffix(r, []byte("\r"))
236+
return config.ReadConfigFile((*d4).confdir, fileName)
256237
}
257238

258239
func d4loadConfig(d4 *d4S) bool {
@@ -369,7 +350,6 @@ func newD4Writer(writer io.Writer, key []byte) d4Writer {
369350

370351
// TODO QUICK IMPLEM, REVISE
371352
func setReaderWriters(d4 *d4S) bool {
372-
373353
//TODO implement other destination file, fifo unix_socket ...
374354
switch (*d4).conf.source {
375355
case "stdin":
@@ -378,7 +358,7 @@ func setReaderWriters(d4 *d4S) bool {
378358
f, _ := os.Open("capture.pcap")
379359
(*d4).src = f
380360
}
381-
isn, dstnet := isNet((*d4).conf.destination)
361+
isn, dstnet := config.IsNet((*d4).conf.destination)
382362
if isn {
383363
dial := net.Dialer{
384364
Timeout: (*d4).ct,
@@ -427,66 +407,6 @@ func setReaderWriters(d4 *d4S) bool {
427407
return true
428408
}
429409

430-
func isNet(host string) (bool, string) {
431-
// DNS regex
432-
validDNS := regexp.MustCompile(`^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|([a-zA-Z0-9][a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]))\.([a-zA-Z]{2,6}|[a-zA-Z0-9-]{2,30}\.[a-zA-Z
433-
]{2,3})$`)
434-
// Check ipv6
435-
if strings.HasPrefix(host, "[") {
436-
// Parse an IP-Literal in RFC 3986 and RFC 6874.
437-
// E.g., "[fe80::1]:80".
438-
i := strings.LastIndex(host, "]")
439-
if i < 0 {
440-
infof("Unmatched [ in destination config")
441-
return false, ""
442-
}
443-
if !validPort(host[i+1:]) {
444-
infof("No valid port specified")
445-
return false, ""
446-
}
447-
// trim brackets
448-
if net.ParseIP(strings.Trim(host[:i+1], "[]")) != nil {
449-
infof(fmt.Sprintf("Server IP: %s, Server Port: %s\n", host[:i+1], host[i+1:]))
450-
return true, host
451-
}
452-
} else {
453-
// Ipv4 or DNS name
454-
ss := strings.Split(string(host), ":")
455-
if len(ss) > 1 {
456-
if !validPort(":" + ss[1]) {
457-
infof("No valid port specified")
458-
return false, ""
459-
}
460-
if net.ParseIP(ss[0]) != nil {
461-
infof(fmt.Sprintf("Server IP: %s, Server Port: %s\n", ss[0], ss[1]))
462-
return true, host
463-
} else if validDNS.MatchString(ss[0]) {
464-
infof(fmt.Sprintf("DNS: %s, Server Port: %s\n", ss[0], ss[1]))
465-
return true, host
466-
}
467-
}
468-
}
469-
return false, host
470-
}
471-
472-
// Reusing code from net.url
473-
// validOptionalPort reports whether port is either an empty string
474-
// or matches /^:\d*$/
475-
func validPort(port string) bool {
476-
if port == "" {
477-
return false
478-
}
479-
if port[0] != ':' {
480-
return false
481-
}
482-
for _, b := range port[1:] {
483-
if b < '0' || b > '9' {
484-
return false
485-
}
486-
}
487-
return true
488-
}
489-
490410
func generateUUIDv4() []byte {
491411
uuid, err := uuid.NewV4()
492412
if err != nil {

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/D4-project/d4-goclient
2+
3+
go 1.13
4+
5+
require github.com/D4-project/d4-golang-utils v0.0.0-20190603131519-c10ee092655c

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github.com/D4-project/d4-golang-utils v0.0.0-20190603131519-c10ee092655c h1:NfASgeIzH3ULEOYgDZwZCmq+C+LgrcSBOzNLsWT+RAc=
2+
github.com/D4-project/d4-golang-utils v0.0.0-20190603131519-c10ee092655c/go.mod h1:2rq8KBQnNNDocwc/49cnpaqoQA/komoSHKom7ynvqJc=
3+
github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE=
4+
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=

0 commit comments

Comments
 (0)