Skip to content

Commit d4aa637

Browse files
committed
Print SHA256 fingerprint, don't require specification of http protocol
1 parent 4a37814 commit d4aa637

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

gen/gen.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/ecdsa"
66
"crypto/elliptic"
77
"crypto/rand"
8+
"crypto/sha256"
89
"crypto/x509"
910
"crypto/x509/pkix"
1011
"encoding/pem"
@@ -17,11 +18,11 @@ import (
1718

1819
// Keys generates a new P256 ECDSA public private key pair for TLS.
1920
// It returns a bytes buffer for the PEM encoded private key and certificate.
20-
func Keys(validFor time.Duration) (cert, key *bytes.Buffer, err error) {
21+
func Keys(validFor time.Duration) (cert, key *bytes.Buffer, fingerprint [32]byte, err error) {
2122
privKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
2223
if err != nil {
2324
log.Fatalf("failed to generate private key: %s", err)
24-
return nil, nil, err
25+
return nil, nil, fingerprint, err
2526
}
2627

2728
notBefore := time.Now()
@@ -31,7 +32,7 @@ func Keys(validFor time.Duration) (cert, key *bytes.Buffer, err error) {
3132
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
3233
if err != nil {
3334
log.Fatalf("failed to generate serial number: %s", err)
34-
return nil, nil, err
35+
return nil, nil, fingerprint, err
3536
}
3637

3738
template := x509.Certificate{
@@ -50,7 +51,7 @@ func Keys(validFor time.Duration) (cert, key *bytes.Buffer, err error) {
5051
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &privKey.PublicKey, privKey)
5152
if err != nil {
5253
log.Fatalf("Failed to create certificate: %s", err)
53-
return nil, nil, err
54+
return nil, nil, fingerprint, err
5455
}
5556

5657
// Encode and write certificate and key to bytes.Buffer
@@ -60,9 +61,9 @@ func Keys(validFor time.Duration) (cert, key *bytes.Buffer, err error) {
6061
key = bytes.NewBuffer([]byte{})
6162
pem.Encode(key, pemBlockForKey(privKey))
6263

63-
// log.Printf("% X", sha256.Sum256(derBytes))
64+
fingerprint = sha256.Sum256(derBytes)
6465

65-
return cert, key, nil
66+
return cert, key, fingerprint, nil //TODO: maybe return a struct instead of 4 multiple return items
6667
}
6768

6869
func pemBlockForKey(key *ecdsa.PrivateKey) *pem.Block {

main.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"os"
1010
"time"
1111

12+
"strings"
13+
1214
"github.com/suyashkumar/ssl-proxy/gen"
1315
)
1416

@@ -22,6 +24,8 @@ var (
2224
const (
2325
DefaultCertFile = "cert.pem"
2426
DefaultKeyFile = "key.pem"
27+
HTTPSPrefix = "https://"
28+
HTTPPrefix = "http://"
2529
)
2630

2731
func main() {
@@ -35,7 +39,7 @@ func main() {
3539
log.Printf("No existing cert or key specified, generating some self-signed certs for use (%s, %s)\n", *certFile, *keyFile)
3640

3741
// Generate new keys
38-
certBuf, keyBuf, err := gen.Keys(365 * 24 * time.Hour)
42+
certBuf, keyBuf, fingerprint, err := gen.Keys(365 * 24 * time.Hour)
3943
if err != nil {
4044
log.Fatal("Error generating default keys", err)
4145
}
@@ -52,6 +56,14 @@ func main() {
5256
}
5357
keyOut.Write(keyBuf.Bytes())
5458

59+
log.Printf("SHA256 Fingerprint: % X", fingerprint)
60+
61+
}
62+
63+
// Ensure the to URL is in the right form
64+
if !strings.HasPrefix(*to, HTTPPrefix) && !strings.HasPrefix(*to, HTTPSPrefix) {
65+
*to = HTTPPrefix + *to
66+
log.Println("Assuming -to URL is using http://")
5567
}
5668

5769
toURL, err := url.Parse(*to)
@@ -61,6 +73,6 @@ func main() {
6173

6274
localProxy := httputil.NewSingleHostReverseProxy(toURL)
6375
http.Handle("/", localProxy)
64-
log.Printf("Proxying calls from %s (SSL/TLS) to %s", *fromURL, toURL)
76+
log.Printf("Proxying calls from https://%s (SSL/TLS) to %s", *fromURL, toURL)
6577
log.Fatal(http.ListenAndServeTLS(*fromURL, *certFile, *keyFile, nil))
6678
}

0 commit comments

Comments
 (0)