Skip to content

Commit d66e391

Browse files
committed
continued refractoring
1 parent 127a784 commit d66e391

4 files changed

Lines changed: 43 additions & 39 deletions

File tree

certgraph.go

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var (
2323
gitDate = "none"
2424
certGraph = graph.NewCertGraph()
2525
gitHash = "DEADBEEF"
26+
startDomains = make([]string, 0, 1)
2627
)
2728

2829
// driver types
@@ -69,8 +70,6 @@ func init() {
6970
}
7071

7172
func main() {
72-
var err error
73-
7473
if config.printVersion {
7574
fmt.Println(version())
7675
return
@@ -80,42 +79,21 @@ func main() {
8079
flag.Usage()
8180
return
8281
}
82+
8383
if config.parallel < 1 {
8484
fmt.Fprintln(os.Stderr, "Must enter a positive number of parallel threads")
8585
flag.Usage()
8686
return
8787
}
8888

89-
startDomains := make([]string, 0, 1)
90-
9189
for _, domain := range flag.Args() {
9290
d := strings.ToLower(domain)
9391
if len(d) > 0 {
9492
startDomains = append(startDomains, d)
9593
}
9694
}
9795

98-
switch config.driver {
99-
case "google":
100-
config.ct = true
101-
ctDriver, err = google.NewCTDriver(50, config.savePath)
102-
case "crtsh":
103-
config.ct = true
104-
ctDriver, err = crtsh.NewCTDriver(1000, config.timeout, config.savePath)
105-
case "http":
106-
sslDriver, err = http.NewSSLDriver(config.timeout, config.savePath)
107-
case "smtp":
108-
sslDriver, err = smtp.NewSSLDriver(config.timeout, config.savePath)
109-
for _, domain := range startDomains {
110-
mx, err := smtp.GetMX(domain)
111-
if err == nil {
112-
startDomains = append(startDomains, mx...)
113-
}
114-
}
115-
default:
116-
fmt.Fprintln(os.Stderr, "Unknown driver name: "+config.driver)
117-
return
118-
}
96+
err := setDriver(config.driver)
11997
if err != nil {
12098
fmt.Fprintln(os.Stderr, err)
12199
return
@@ -124,7 +102,7 @@ func main() {
124102
if len(config.savePath) > 0 {
125103
err := os.MkdirAll(config.savePath, 0777)
126104
if err != nil {
127-
fmt.Println(err)
105+
fmt.Fprintln(os.Stderr, err)
128106
return
129107
}
130108
}
@@ -139,6 +117,32 @@ func main() {
139117
v("Graph Depth:", depth)
140118
}
141119

120+
// setDriver sets the driver variable for the provided driver string and does any necessary driver prep work
121+
func setDriver(driver string) error {
122+
var err error
123+
switch driver {
124+
case "google":
125+
config.ct = true
126+
ctDriver, err = google.NewCTDriver(50, config.savePath)
127+
case "crtsh":
128+
config.ct = true
129+
ctDriver, err = crtsh.NewCTDriver(1000, config.timeout, config.savePath)
130+
case "http":
131+
sslDriver, err = http.NewSSLDriver(config.timeout, config.savePath)
132+
case "smtp":
133+
sslDriver, err = smtp.NewSSLDriver(config.timeout, config.savePath)
134+
for _, domain := range startDomains {
135+
mx, err := smtp.GetMX(domain)
136+
if err == nil {
137+
startDomains = append(startDomains, mx...)
138+
}
139+
}
140+
default:
141+
return fmt.Errorf("Unknown driver name: %s", config.driver)
142+
}
143+
return err
144+
}
145+
142146
// verbose logging
143147
func v(a ...interface{}) {
144148
if config.verbose {
@@ -268,34 +272,34 @@ func visitCT(node *graph.DomainNode) {
268272
for _, fp := range fingerprints {
269273
// add certnode to graph
270274

271-
certnode, exists := certGraph.GetCert(fp)
275+
certNode, exists := certGraph.GetCert(fp)
272276

273277
if !exists {
274278
// get cert details
275-
certnode, err = ctDriver.QueryCert(fp)
279+
certNode, err = ctDriver.QueryCert(fp)
276280
if err != nil {
277281
v(err)
278282
continue
279283
}
280284

281-
certGraph.AddCert(certnode)
285+
certGraph.AddCert(certNode)
282286
}
283287

284-
node.AddCTFingerprint(certnode.Fingerprint)
288+
node.AddCTFingerprint(certNode.Fingerprint)
285289
}
286290
}
287291

288292
// visit nodes by connecting to them
289293
func visitSSL(node *graph.DomainNode) {
290-
dStatus, certnode, err := sslDriver.GetCert(node.Domain)
294+
domainStatus, certNode, err := sslDriver.GetCert(node.Domain)
291295
if err != nil {
292296
v(err)
293297
}
294-
node.Status = dStatus
298+
node.Status = domainStatus
295299

296-
if certnode != nil {
297-
certnode, _ = certGraph.LoadOrStoreCert(certnode)
298-
node.VisitedCert = certnode.Fingerprint
300+
if certNode != nil {
301+
certNode, _ = certGraph.LoadOrStoreCert(certNode)
302+
node.VisitedCert = certNode.Fingerprint
299303
}
300304
}
301305

driver/ct/google/google.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"github.com/lanrat/certgraph/graph"
2525
)
2626

27-
// BASE URLs for Googl'e CT API
27+
// Base URLs for Google's CT API
2828
const searchURL1 = "https://transparencyreport.google.com/transparencyreport/api/v3/httpsreport/ct/certsearch?include_expired=false&include_subdomains=false&domain=example.com"
2929
const searchURL2 = "https://transparencyreport.google.com/transparencyreport/api/v3/httpsreport/ct/certsearch/page?p=DEADBEEF"
3030
const certURL = "https://transparencyreport.google.com/transparencyreport/api/v3/httpsreport/ct/certbyhash?hash=DEADBEEF"

driver/ssl/http/http.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func NewSSLDriver(timeout time.Duration, savePath string) (ssl.Driver, error) {
3737
return d, nil
3838
}
3939

40-
// gets the certificates found for a given domain
40+
// GetCert gets the certificates found for a given domain
4141
func (d *httpDriver) GetCert(host string) (status.DomainStatus, *graph.CertNode, error) {
4242
addr := net.JoinHostPort(host, d.port)
4343
dialer := &net.Dialer{Timeout: d.timeout}
@@ -47,7 +47,7 @@ func (d *httpDriver) GetCert(host string) (status.DomainStatus, *graph.CertNode,
4747
domainStatus = status.CheckNetErr(err)
4848
if domainStatus != status.GOOD {
4949
//v(domainStatus, host)
50-
return domainStatus, nil, err // TODO might want to make this return a nil error
50+
return domainStatus, nil, err // might want to make this return a nil error
5151
}
5252
conn.Close()
5353
connState := conn.ConnectionState()

driver/ssl/smtp/smtp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func NewSSLDriver(timeout time.Duration, savePath string) (ssl.Driver, error) {
3434
return d, nil
3535
}
3636

37-
// gets the certificates found for a given domain
37+
// GetCert gets the certificates found for a given domain
3838
func (d *smtpDriver) GetCert(host string) (status.DomainStatus, *graph.CertNode, error) {
3939
addr := net.JoinHostPort(host, d.port)
4040
dialer := &net.Dialer{Timeout: d.timeout}

0 commit comments

Comments
 (0)