Skip to content

Commit 7bffb82

Browse files
committed
chg [modules] going modular
1 parent ecb6fbf commit 7bffb82

File tree

7 files changed

+356
-104
lines changed

7 files changed

+356
-104
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*.dll
55
*.so
66
*.dylib
7+
analyzer-d4-passivessl
78

89
# Test binary, build with `go test -c`
910
*.test

go.mod

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module github.com/D4-project/analyzer-d4-passivessl
2+
3+
go 1.13
4+
5+
require (
6+
github.com/D4-project/d4-golang-utils v0.0.0-20200108150548-740f16240125
7+
github.com/gallypette/certificate-transparency-go v1.0.21
8+
github.com/gomodule/redigo v2.0.0+incompatible
9+
github.com/google/certificate-transparency-go v1.1.0 // indirect
10+
github.com/lib/pq v1.3.0
11+
golang.org/x/crypto v0.0.0-20200109152110-61a87790db17 // indirect
12+
)

go.sum

Lines changed: 323 additions & 0 deletions
Large diffs are not rendered by default.

main.go

Lines changed: 12 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@ import (
2020
"io/ioutil"
2121
"log"
2222
"math/big"
23-
"net"
2423
"os"
2524
"os/signal"
2625
"path/filepath"
27-
"regexp"
2826
"strconv"
2927
"strings"
3028
"time"
@@ -33,6 +31,8 @@ import (
3331

3432
"github.com/gomodule/redigo/redis"
3533
_ "github.com/lib/pq"
34+
35+
config "github.com/D4-project/d4-golang-utils/config"
3636
)
3737

3838
type (
@@ -132,7 +132,7 @@ func main() {
132132
}
133133

134134
// Parse DB Config
135-
tmp := readConfFile(*confdir, "postgres")
135+
tmp := config.ReadConfigFile(*confdir, "postgres")
136136
ss := strings.Split(string(tmp), "/")
137137
if len(ss) <= 1 {
138138
log.Fatal("Missing Database in Postgres config: should be user:pwd@host:port/database_name")
@@ -148,7 +148,7 @@ func main() {
148148
}
149149
c.postgresUser = sssu[0]
150150
c.postgresPWD = sssu[1]
151-
ret, ssh := isNet(sssat[1])
151+
ret, ssh := config.IsNet(sssat[1])
152152
if !ret {
153153
sssh := strings.Split(string(ssh), ":")
154154
c.postgresHost = sssh[0]
@@ -157,7 +157,7 @@ func main() {
157157

158158
// Parse Certificate Folder
159159
if !*pull {
160-
c.certPath = string(readConfFile(*confdir, "certfolder"))
160+
c.certPath = string(config.ReadConfigFile(*confdir, "certfolder"))
161161
}
162162
c.recursive = *recursive
163163
c.tarball = *tarball
@@ -171,35 +171,35 @@ func main() {
171171

172172
if *pull { // Redis
173173
// Parse Redis Config
174-
tmp := readConfFile(*confdir, "redis")
174+
tmp := config.ReadConfigFile(*confdir, "redis")
175175
ss := strings.Split(string(tmp), "/")
176176
if len(ss) <= 1 {
177177
log.Fatal("Missing Database in Redis config: should be host:port/database_name")
178178
}
179179
c.redisDB, _ = strconv.Atoi(ss[1])
180180
var ret bool
181-
ret, ss[0] = isNet(ss[0])
181+
ret, ss[0] = config.IsNet(ss[0])
182182
if !ret {
183183
sss := strings.Split(string(ss[0]), ":")
184184
c.redisHost = sss[0]
185185
c.redisPort = sss[1]
186186
}
187-
c.redisQueue = string(readConfFile(*confdir, "redis_queue"))
187+
c.redisQueue = string(config.ReadConfigFile(*confdir, "redis_queue"))
188188
initRedis(c.redisHost, c.redisPort, c.redisDB)
189189
defer cr.Close()
190190
// pop redis queue
191191
for {
192192
err := errors.New("")
193193
jsonPath, err = redis.String(cr.Do("LPOP", "analyzer:ja3-jl:"+c.redisQueue))
194+
if err != nil {
195+
log.Fatal(err)
196+
}
194197
err = filepath.Walk(jsonPath,
195198
func(path string, info os.FileInfo, err error) error {
196-
if err != nil {
197-
return err
198-
}
199199
if !info.IsDir() {
200200
fd, err := os.Open(path)
201201
if err != nil {
202-
log.Fatal(err)
202+
return err
203203
}
204204
bf := bufio.NewReader(fd)
205205
fmt.Println(path)
@@ -709,86 +709,3 @@ func (t *sessionRecord) String() string {
709709
buf.WriteString(fmt.Sprintf("---------------SESSION END--------------------\n"))
710710
return buf.String()
711711
}
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-
}

passivessl.sql

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@
77
SET check_function_bodies = false;
88
-- ddl-end --
99

10-
-- object: cia_dev | type: ROLE --
11-
-- DROP ROLE IF EXISTS cia_dev;
12-
CREATE ROLE cia_dev WITH
13-
INHERIT
14-
LOGIN
15-
ENCRYPTED PASSWORD '********';
16-
-- ddl-end --
17-
18-
1910
-- Database creation must be done outside a multicommand file.
2011
-- These commands were put in this file only as a convenience.
2112
-- -- object: passive_ssl | type: DATABASE --

passivesslCreate.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE DATABASE p2
2+
ENCODING = 'UTF8'
3+
LC_COLLATE = 'en_US.UTF-8'
4+
LC_CTYPE = 'en_US.UTF-8'
5+
TABLESPACE = pg_default
6+
OWNER = postgres;

populate.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#!/bin/bash
22
# Set PGPASSWORD first
3+
# export PGPASSWORD=postgres
4+
psql -hlocalhost -p5432 -Upostgres -f passivesslCreate.sql
35
psql -hlocalhost -p5432 -Upostgres -d p2 -f passivessl.sql

0 commit comments

Comments
 (0)