-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (67 loc) · 1.51 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"crypto/tls"
"crypto/x509"
"errors"
"io"
"log"
"net/http"
"os"
"github.com/flashbots/builder-tools/common"
cli "github.com/urfave/cli/v2"
)
var flags []cli.Flag = []cli.Flag{
&cli.StringFlag{
Name: "server-addr",
Value: "https://0.0.0.0:8080",
Usage: "address to serve certificate on",
},
}
func main() {
app := &cli.App{
Name: "https-client",
Usage: "Client to allow only specific self-signed server cert",
Flags: flags,
Action: runCli,
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func runCli(cCtx *cli.Context) error {
log := common.SetupLogger(&common.LoggingOpts{})
serverAddr := cCtx.String("server-addr")
certFile := "cert.pem"
certData, err := os.ReadFile(certFile)
if err != nil {
log.Error("could not read cert data", "err", err)
return err
}
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM(certData)
if !ok {
log.Error("invalid certificate received", "cert", string(certData))
return errors.New("invalid certificate")
}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: roots,
MinVersion: tls.VersionTLS13,
},
},
}
resp, err := client.Get(serverAddr)
if err != nil {
log.Error("http request error", "err", err)
return err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
log.Error("could not read proxied service body", "err", err)
return err
}
log.Info("Received", "resp", string(respBody))
return nil
}