Skip to content

Commit a12c28d

Browse files
committed
fetch certs from api
1 parent f82e7da commit a12c28d

4 files changed

Lines changed: 36 additions & 128 deletions

File tree

main.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"context"
5-
"crypto/tls"
65
"fmt"
76
"net"
87
"os"
@@ -122,11 +121,7 @@ func main() {
122121
availableSubdomains = append(availableSubdomains, ipToSubdomain(addrIp))
123122
}
124123

125-
tlsCert, err := tls.X509KeyPair(cert, certKey)
126-
if err != nil {
127-
return err
128-
}
129-
return StartProxyService(ctx, tlsCert, addrIp, hostPort, port, availableSubdomains)
124+
return StartProxyService(ctx, addrIp, hostPort, port, availableSubdomains)
130125
},
131126
}
132127

proxy.go

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"crypto/tls"
66
_ "embed"
7+
"encoding/gob"
8+
"encoding/hex"
79
"fmt"
810
"net"
911
"net/http"
@@ -15,20 +17,47 @@ import (
1517
"github.com/sprisa/localhost/util"
1618
)
1719

18-
//go:embed ssl/cert.pem
19-
var cert []byte
20-
21-
//go:embed ssl/key.pem
22-
var certKey []byte
20+
type Certificate struct {
21+
Cert []byte
22+
Key []byte
23+
}
2324

2425
func StartProxyService(
2526
ctx context.Context,
26-
tlsCert tls.Certificate,
2727
addrIp net.IP,
2828
listenPort int,
2929
hostPort int,
3030
availableSubdomains []string,
3131
) error {
32+
// res, err := http.Get("http://localhost:8080/certs")
33+
res, err := http.Get("https://svc.host/certs")
34+
if err != nil {
35+
return util.WrapError(err, "error fetching certs")
36+
}
37+
decoder := gob.NewDecoder(res.Body)
38+
certificate := &Certificate{}
39+
err = decoder.Decode(certificate)
40+
if err != nil {
41+
return util.WrapError(err, "error decoding certificate")
42+
}
43+
44+
cert := make([]byte, hex.DecodedLen(len(certificate.Cert)))
45+
_, err = hex.Decode(cert, certificate.Cert)
46+
if err != nil {
47+
return util.WrapError(err, "error decoding certificate.Cert")
48+
}
49+
50+
certKey := make([]byte, hex.DecodedLen(len(certificate.Key)))
51+
_, err = hex.Decode(certKey, certificate.Key)
52+
if err != nil {
53+
return util.WrapError(err, "error decoding certificate.Key")
54+
}
55+
56+
tlsCert, err := tls.X509KeyPair(cert, certKey)
57+
if err != nil {
58+
return util.WrapError(err, "error creating tls cert")
59+
}
60+
3261
log := util.Log.With().Int("targetPort", hostPort).Logger()
3362
handler := http.NewServeMux()
3463

ssl/cert.pem

Lines changed: 0 additions & 64 deletions
This file was deleted.

ssl/key.pem

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)