@@ -20,11 +20,9 @@ import (
20
20
"io/ioutil"
21
21
"log"
22
22
"math/big"
23
- "net"
24
23
"os"
25
24
"os/signal"
26
25
"path/filepath"
27
- "regexp"
28
26
"strconv"
29
27
"strings"
30
28
"time"
@@ -33,6 +31,8 @@ import (
33
31
34
32
"github.com/gomodule/redigo/redis"
35
33
_ "github.com/lib/pq"
34
+
35
+ config "github.com/D4-project/d4-golang-utils/config"
36
36
)
37
37
38
38
type (
@@ -132,7 +132,7 @@ func main() {
132
132
}
133
133
134
134
// Parse DB Config
135
- tmp := readConfFile (* confdir , "postgres" )
135
+ tmp := config . ReadConfigFile (* confdir , "postgres" )
136
136
ss := strings .Split (string (tmp ), "/" )
137
137
if len (ss ) <= 1 {
138
138
log .Fatal ("Missing Database in Postgres config: should be user:pwd@host:port/database_name" )
@@ -148,7 +148,7 @@ func main() {
148
148
}
149
149
c .postgresUser = sssu [0 ]
150
150
c .postgresPWD = sssu [1 ]
151
- ret , ssh := isNet (sssat [1 ])
151
+ ret , ssh := config . IsNet (sssat [1 ])
152
152
if ! ret {
153
153
sssh := strings .Split (string (ssh ), ":" )
154
154
c .postgresHost = sssh [0 ]
@@ -157,7 +157,7 @@ func main() {
157
157
158
158
// Parse Certificate Folder
159
159
if ! * pull {
160
- c .certPath = string (readConfFile (* confdir , "certfolder" ))
160
+ c .certPath = string (config . ReadConfigFile (* confdir , "certfolder" ))
161
161
}
162
162
c .recursive = * recursive
163
163
c .tarball = * tarball
@@ -171,35 +171,35 @@ func main() {
171
171
172
172
if * pull { // Redis
173
173
// Parse Redis Config
174
- tmp := readConfFile (* confdir , "redis" )
174
+ tmp := config . ReadConfigFile (* confdir , "redis" )
175
175
ss := strings .Split (string (tmp ), "/" )
176
176
if len (ss ) <= 1 {
177
177
log .Fatal ("Missing Database in Redis config: should be host:port/database_name" )
178
178
}
179
179
c .redisDB , _ = strconv .Atoi (ss [1 ])
180
180
var ret bool
181
- ret , ss [0 ] = isNet (ss [0 ])
181
+ ret , ss [0 ] = config . IsNet (ss [0 ])
182
182
if ! ret {
183
183
sss := strings .Split (string (ss [0 ]), ":" )
184
184
c .redisHost = sss [0 ]
185
185
c .redisPort = sss [1 ]
186
186
}
187
- c .redisQueue = string (readConfFile (* confdir , "redis_queue" ))
187
+ c .redisQueue = string (config . ReadConfigFile (* confdir , "redis_queue" ))
188
188
initRedis (c .redisHost , c .redisPort , c .redisDB )
189
189
defer cr .Close ()
190
190
// pop redis queue
191
191
for {
192
192
err := errors .New ("" )
193
193
jsonPath , err = redis .String (cr .Do ("LPOP" , "analyzer:ja3-jl:" + c .redisQueue ))
194
+ if err != nil {
195
+ log .Fatal (err )
196
+ }
194
197
err = filepath .Walk (jsonPath ,
195
198
func (path string , info os.FileInfo , err error ) error {
196
- if err != nil {
197
- return err
198
- }
199
199
if ! info .IsDir () {
200
200
fd , err := os .Open (path )
201
201
if err != nil {
202
- log . Fatal ( err )
202
+ return err
203
203
}
204
204
bf := bufio .NewReader (fd )
205
205
fmt .Println (path )
@@ -709,86 +709,3 @@ func (t *sessionRecord) String() string {
709
709
buf .WriteString (fmt .Sprintf ("---------------SESSION END--------------------\n " ))
710
710
return buf .String ()
711
711
}
712
-
713
- func isNet (host string ) (bool , string ) {
714
- // DNS regex
715
- 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
716
- ]{2,3})$` )
717
- // Check ipv6
718
- if strings .HasPrefix (host , "[" ) {
719
- // Parse an IP-Literal in RFC 3986 and RFC 6874.
720
- // E.g., "[fe80::1]:80".
721
- i := strings .LastIndex (host , "]" )
722
- if i < 0 {
723
- log .Fatal ("Unmatched [ in destination config" )
724
- return false , ""
725
- }
726
- if ! validPort (host [i + 1 :]) {
727
- log .Fatal ("No valid port specified" )
728
- return false , ""
729
- }
730
- // trim brackets
731
- if net .ParseIP (strings .Trim (host [:i + 1 ], "[]" )) != nil {
732
- log .Fatal (fmt .Sprintf ("Server IP: %s, Server Port: %s\n " , host [:i + 1 ], host [i + 1 :]))
733
- return true , host
734
- }
735
- } else {
736
- // Ipv4 or DNS name
737
- ss := strings .Split (string (host ), ":" )
738
- if len (ss ) > 1 {
739
- if ! validPort (":" + ss [1 ]) {
740
- log .Fatal ("No valid port specified" )
741
- return false , ""
742
- }
743
- if net .ParseIP (ss [0 ]) != nil {
744
- log .Fatal (fmt .Sprintf ("Server IP: %s, Server Port: %s\n " , ss [0 ], ss [1 ]))
745
- return true , host
746
- } else if validDNS .MatchString (ss [0 ]) {
747
- log .Fatal (fmt .Sprintf ("DNS: %s, Server Port: %s\n " , ss [0 ], ss [1 ]))
748
- return true , host
749
- }
750
- }
751
- }
752
- return false , host
753
- }
754
-
755
- // Reusing code from net.url
756
- // validOptionalPort reports whether port is either an empty string
757
- // or matches /^:\d*$/
758
- func validPort (port string ) bool {
759
- if port == "" {
760
- return false
761
- }
762
- if port [0 ] != ':' {
763
- return false
764
- }
765
- for _ , b := range port [1 :] {
766
- if b < '0' || b > '9' {
767
- return false
768
- }
769
- }
770
- return true
771
- }
772
-
773
- func readConfFile (p string , fileName string ) []byte {
774
- f , err := os .OpenFile ("./" + p + "/" + fileName , os .O_RDWR | os .O_CREATE , 0666 )
775
- defer f .Close ()
776
- if err != nil {
777
- log .Fatal (err )
778
- }
779
- data := make ([]byte , 100 )
780
- count , err := f .Read (data )
781
- if err != nil {
782
- if err != io .EOF {
783
- log .Fatal (err )
784
- }
785
- }
786
- if count == 0 {
787
- log .Fatal (fileName + " is empty." )
788
- }
789
- if err := f .Close (); err != nil {
790
- log .Fatal (err )
791
- }
792
- // trim \n if present
793
- return bytes .TrimSuffix (data [:count ], []byte ("\n " ))
794
- }
0 commit comments