-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
171 lines (148 loc) · 5.17 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"crypto/tls"
"flag"
"fmt"
"github.com/ThalesIgnite/crypto11"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strings"
"time"
)
func timedLog(message string) {
fmt.Printf("%v - %s\n", time.Now(), message)
}
func listCertificates(pkcs11path, tokenSerial *string, pinVal string) {
config := crypto11.Config{
Path: *pkcs11path,
TokenSerial: *tokenSerial,
Pin: pinVal,
}
context, err := crypto11.Configure(&config)
if err != nil {
log.Fatalln(err)
}
certificates, err := context.FindAllPairedCertificates()
if err != nil {
log.Fatalln(err)
}
index := 0
for _, cert := range certificates {
fmt.Printf("Certificate index %d: %v\n", index, cert.Leaf.Subject)
index++
}
}
func main() {
listenAddress := flag.String("listen-addr", "127.0.0.1", "Address to listen on")
listenPort := flag.Int("listen-port", 8080, "Port to listen on")
pkcs11path := flag.String("pkcs11-path", "", "Path to the PKCS11 module. Use the card vendor-specific one, or run 'pkcs11-tool --help' and look for '--module' default value for a good one to use.")
tokenSerial := flag.String("token-serial", "", "Serial number of the token. Run 'pkcs11-tool --list-token-slots' to find it.")
certificateIndex := flag.Int("certificate-index", 0, fmt.Sprintf("Index of the certificate to use. Run '%s -token-serial ... [-pin/-pin-file] ... list-certificates' to find the index. By default, the first found certificate (index 0) will be used.", os.Args[0]))
pin := flag.String("pin", "", "PIN to access the card. Cannot be used with --pin-file.")
pinFile := flag.String("pin-file", "", "File containing the PIN to access the card (will be deleted after read!). Cannot be used with --pin.")
destinationUrl := flag.String("destination-url", "", "URL to forward requests to.")
noPreserveHost := flag.Bool("no-preserve-host", false, "Do not preserve the host header in the request.")
logRequests := flag.Bool("log-requests", false, "Log each request to stdout.")
listenTLS := flag.Bool("listen-tls", false, "Listen on TLS instead of plain HTTP (useful if your upstream sets 'secure' cookies")
listenTLSCertificate := flag.String("listen-tls-cert", "", "Path to the certificate or chain file for the TLS listener (required if --listen-tls is set)")
listenTLSPrivateKey := flag.String("listen-tls-key", "", "Path to the private key file for the TLS listener (required if --listen-tls is set)")
flag.Parse()
if *pkcs11path == "" {
fmt.Println("pkcs11-path is required")
flag.Usage()
return
}
if *tokenSerial == "" {
fmt.Println("token-serial is required")
flag.Usage()
return
}
if *pin == "" && *pinFile == "" {
fmt.Println("Either pin or pin-file is required")
flag.Usage()
return
}
if *pin != "" && *pinFile != "" {
fmt.Println("Both pin and pin-file are set. Please use only one")
flag.Usage()
return
}
pinVal := *pin
if *pinFile != "" {
pinBytes, err := os.ReadFile(*pinFile)
if err != nil {
log.Fatalf("Error reading pin file: %v", err)
}
pinVal = strings.TrimSpace(string(pinBytes))
err = os.Remove(*pinFile)
if err != nil {
log.Fatalf("Error deleting pin file: %v", err)
}
}
if flag.Arg(0) == "list-certificates" {
listCertificates(pkcs11path, tokenSerial, pinVal)
return
}
if *destinationUrl == "" {
fmt.Println("destination-url is required")
flag.Usage()
return
}
if *listenTLS {
if *listenTLSPrivateKey == "" || *listenTLSCertificate == "" {
fmt.Println("listen-tls-private-key and listen-tls-certificate are required when listen-tls is set")
flag.Usage()
return
}
}
timedLog("Reverse proxy is starting")
config := crypto11.Config{
Path: *pkcs11path,
TokenSerial: *tokenSerial,
Pin: pinVal,
}
context, err := crypto11.Configure(&config)
if err != nil {
log.Fatalln(err)
}
certificates, err := context.FindAllPairedCertificates()
if err != nil {
log.Fatalln(err)
}
if *certificateIndex >= len(certificates) {
log.Fatalf("Certificate index %d is out of range. Run '%s -token-serial ... [-pin/-pin-file] ... list-certificates' to find the index.\n", *certificateIndex, os.Args[0])
return
}
cert := certificates[*certificateIndex]
transport := &http.Transport{
TLSClientConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
Renegotiation: tls.RenegotiateOnceAsClient,
},
}
destUrl, err := url.Parse(*destinationUrl)
proxy := httputil.NewSingleHostReverseProxy(destUrl)
proxy.Transport = transport
handler := func(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if !*noPreserveHost {
r.Host = destUrl.Host
}
if *logRequests {
timedLog(fmt.Sprintf("Request: %s %s", r.Method, r.URL.String()))
}
p.ServeHTTP(w, r)
}
}
http.HandleFunc("/", handler(proxy))
if *listenTLS {
timedLog(fmt.Sprintf("Listening on %s:%d over TLS", *listenAddress, *listenPort))
log.Fatal(http.ListenAndServeTLS(fmt.Sprintf("%s:%d", *listenAddress, *listenPort), *listenTLSCertificate, *listenTLSPrivateKey, nil))
} else {
timedLog(fmt.Sprintf("Listening on %s:%d", *listenAddress, *listenPort))
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", *listenAddress, *listenPort), nil))
}
}