Skip to content

Commit

Permalink
Merge pull request #7 from D4-project/modular
Browse files Browse the repository at this point in the history
Modular
  • Loading branch information
gallypette authored Jan 10, 2020
2 parents dd42054 + 2db3aeb commit 015ece0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 84 deletions.
88 changes: 4 additions & 84 deletions d4-goclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ import (
"net"
"os"
"os/signal"
"regexp"
"strconv"
"strings"
"time"

//BSD 3
uuid "github.com/satori/go.uuid"
config "github.com/D4-project/d4-golang-utils/config"
uuid "github.com/D4-project/d4-golang-utils/crypto/hash"
)

const (
Expand Down Expand Up @@ -234,25 +233,7 @@ func d4Copy(d4 *d4S, c chan string, k chan string) {
}

func readConfFile(d4 *d4S, fileName string) []byte {
f, err := os.OpenFile((*d4).confdir+"/"+fileName, os.O_RDWR|os.O_CREATE, 0666)
defer f.Close()
if err != nil {
log.Fatal(err)
}
data := make([]byte, 100)
count, err := f.Read(data)
if err != nil {
if err != io.EOF {
log.Fatal(err)
}
}
infof(fmt.Sprintf("read %d bytes: %q\n", count, data[:count]))
if err := f.Close(); err != nil {
log.Fatal(err)
}
// trim \r and \n if present
r := bytes.TrimSuffix(data[:count], []byte("\n"))
return bytes.TrimSuffix(r, []byte("\r"))
return config.ReadConfigFile((*d4).confdir, fileName)
}

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

// TODO QUICK IMPLEM, REVISE
func setReaderWriters(d4 *d4S) bool {

//TODO implement other destination file, fifo unix_socket ...
switch (*d4).conf.source {
case "stdin":
Expand All @@ -378,7 +358,7 @@ func setReaderWriters(d4 *d4S) bool {
f, _ := os.Open("capture.pcap")
(*d4).src = f
}
isn, dstnet := isNet((*d4).conf.destination)
isn, dstnet := config.IsNet((*d4).conf.destination)
if isn {
dial := net.Dialer{
Timeout: (*d4).ct,
Expand Down Expand Up @@ -427,66 +407,6 @@ func setReaderWriters(d4 *d4S) bool {
return true
}

func isNet(host string) (bool, string) {
// DNS regex
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
]{2,3})$`)
// Check ipv6
if strings.HasPrefix(host, "[") {
// Parse an IP-Literal in RFC 3986 and RFC 6874.
// E.g., "[fe80::1]:80".
i := strings.LastIndex(host, "]")
if i < 0 {
infof("Unmatched [ in destination config")
return false, ""
}
if !validPort(host[i+1:]) {
infof("No valid port specified")
return false, ""
}
// trim brackets
if net.ParseIP(strings.Trim(host[:i+1], "[]")) != nil {
infof(fmt.Sprintf("Server IP: %s, Server Port: %s\n", host[:i+1], host[i+1:]))
return true, host
}
} else {
// Ipv4 or DNS name
ss := strings.Split(string(host), ":")
if len(ss) > 1 {
if !validPort(":" + ss[1]) {
infof("No valid port specified")
return false, ""
}
if net.ParseIP(ss[0]) != nil {
infof(fmt.Sprintf("Server IP: %s, Server Port: %s\n", ss[0], ss[1]))
return true, host
} else if validDNS.MatchString(ss[0]) {
infof(fmt.Sprintf("DNS: %s, Server Port: %s\n", ss[0], ss[1]))
return true, host
}
}
}
return false, host
}

// Reusing code from net.url
// validOptionalPort reports whether port is either an empty string
// or matches /^:\d*$/
func validPort(port string) bool {
if port == "" {
return false
}
if port[0] != ':' {
return false
}
for _, b := range port[1:] {
if b < '0' || b > '9' {
return false
}
}
return true
}

func generateUUIDv4() []byte {
uuid, err := uuid.NewV4()
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/D4-project/d4-goclient

go 1.13

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

0 comments on commit 015ece0

Please sign in to comment.