@@ -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
2827const (
@@ -234,25 +233,7 @@ func d4Copy(d4 *d4S, c chan string, k chan string) {
234233}
235234
236235func 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
258239func d4loadConfig (d4 * d4S ) bool {
@@ -369,7 +350,6 @@ func newD4Writer(writer io.Writer, key []byte) d4Writer {
369350
370351// TODO QUICK IMPLEM, REVISE
371352func 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-
490410func generateUUIDv4 () []byte {
491411 uuid , err := uuid .NewV4 ()
492412 if err != nil {
0 commit comments