Skip to content

Instantly share code, notes, and snippets.

@jgrahamc
Created April 20, 2015 16:19
Show Gist options
  • Save jgrahamc/4aa9b0ddfde384759f42 to your computer and use it in GitHub Desktop.
Save jgrahamc/4aa9b0ddfde384759f42 to your computer and use it in GitHub Desktop.
// Copyright (c) 2014 John Graham-Cumming
//
// Takes a CSV containing rows in the form
//
// ip,name
//
// Connects to 'ip' on port 443 using TLS presenting the SNI name 'name' and
// verifies the result.
package main
import (
"bufio"
"crypto/tls"
"flag"
"fmt"
"log"
"net"
"os"
"strings"
"sync"
)
type task interface {
process()
print()
}
type factory interface {
make(line string) task
}
type check struct {
ip string
name string
err error
success bool
}
var workers int
func (c *check) process() {
conf := &tls.Config{ServerName: c.name,
InsecureSkipVerify: false}
conn, err := tls.Dial("tcp", net.JoinHostPort(c.ip, "443"), conf)
c.err = err
c.success = c.err == nil
if conn != nil {
conn.Close()
}
}
func (c *check) print() {
if c.err == nil {
c.err = fmt.Errorf("OK")
}
fmt.Printf("%s,%s,%t,%s\n", c.ip, c.name, c.success, c.err)
}
type usine struct {
}
func (u *usine) make(line string) task {
parts := strings.Split(line, ",")
if len(parts) != 2 {
log.Fatalf("Don't understand line '%s'\n", line)
}
return &check{ip: parts[0], name: parts[1]}
}
func run(f factory) {
var wg sync.WaitGroup
in := make(chan task)
wg.Add(1)
go func() {
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
in <- f.make(s.Text())
}
if s.Err() != nil {
log.Fatalf("Error reading STDIN: %s", s.Err())
}
close(in)
wg.Done()
}()
out := make(chan task)
for i := 0; i < workers; i++ {
wg.Add(1)
go func() {
for t := range in {
t.process()
out <- t
}
wg.Done()
}()
}
go func() {
wg.Wait()
close(out)
}()
for t := range out {
t.print()
}
}
func main() {
flag.IntVar(&workers, "workers", 100, "Number of workers")
flag.Parse()
run(&usine{})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment