Exemplo n.º 1
0
func main() {
	logger := log.New(os.Stdout, "", log.LstdFlags)
	connStr := fmt.Sprintf("user=postgres host=%v password=%v dbname=postgres sslmode=disable", *dbAddr, *dbPW)
	db, _ := sql.Open("postgres", connStr)

	// Load server cert
	cert, err := tls.LoadX509KeyPair(*certPath, *keyPath)
	if err != nil {
		logger.Fatal("Failed to open server cert and/or key: ", err)
	}

	// Load CA cert
	caCert, err := ioutil.ReadFile(*caPath)
	if err != nil {
		logger.Fatal("Failed to open CA cert: ", err)
	}
	caCertPool := x509.NewCertPool()
	if ok := caCertPool.AppendCertsFromPEM(caCert); !ok {
		logger.Fatal("Failed to parse CA cert")
	}

	tlsConf := &tls.Config{
		Certificates:       []tls.Certificate{cert},
		RootCAs:            caCertPool,
		ClientAuth:         tls.RequireAndVerifyClientCert,
		ClientCAs:          caCertPool,
		InsecureSkipVerify: true, //TODO dev only
		CipherSuites: []uint16{
			tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
			tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
		},
		PreferServerCipherSuites: true,
		MinVersion:               tls.VersionTLS12,
	}
	serverConf := &coelacanth.Config{
		DB: db,
		ListenerFunc: func(addr string) (net.Listener, error) {
			return tls.Listen("tcp", addr, tlsConf)
		},
		Logger: logger,
	}
	server := coelacanth.NewServer(serverConf)
	defer func() {
		if err := server.Close(); err != nil {
			logger.Println("Failed to close server: ", err)
		}
	}()

	logger.Fatal(server.Run(":13800", handler))
}
Exemplo n.º 2
0
// RunWithServer executes testFunc with a prepared server
func RunWithServer(t *testing.T, handler func(net.Conn, *coelacanth.Server), testFunc func(*coelacanth.Server, string)) {
	RunWithDB(t, func(db *TestDB) {
		// Setup server
		addrChan := make(chan string)
		conf := &coelacanth.Config{
			DB: db,
			ListenerFunc: func(addr string) (net.Listener, error) {
				l, err := net.Listen("tcp", addr)
				if err == nil {
					addrChan <- addr
				}
				return l, err
			},
			Logger: log.New(&logWriter{t}, "", log.Lmicroseconds),
		}
		s := coelacanth.NewServer(conf)
		defer func() {
			if err := s.Close(); err != nil {
				t.Fatal("Error closing server: ", err)
			}
		}()

		// Run server on a separate goroutine
		errs := make(chan error)
		go func() {
			errs <- s.Run(":13900", handler) // TODO not hardcoded
		}()
		select {
		case err := <-errs:
			t.Fatal(err)
		case <-time.NewTimer(5 * time.Second).C:
			t.Fatal("Timed out waiting for server to start")
		case addr := <-addrChan:
			testFunc(s, addr)
		}
	})
	return
}