-
Notifications
You must be signed in to change notification settings - Fork 37
/
proxy.go
190 lines (165 loc) · 4.26 KB
/
proxy.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"io"
"log"
"math/big"
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"syscall"
"time"
"golang.org/x/net/http2"
)
func main() {
createX509Certificate()
defer cleanup()
util := newCertUtil()
runCommands(util.GetRegisterCertificateCommands("proxy.crt"))
defer runCommands(util.GetDeregisterCertificateCommands("proxy.crt"))
server := startServer()
defer server.Close()
waitForSigint()
}
func waitForSigint() {
log.Printf("Proxy server started. Press Ctrl+C to stop.")
log.Printf("Waiting for SIGINT...")
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT)
<-sigChan
}
func createX509Certificate() {
log.Printf("Creating X509 certificate and private key...")
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
panic(err)
}
notBefore := time.Now()
notAfter := notBefore.Add(365 * 24 * time.Hour)
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
panic(err)
}
template := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{"Your Organization"},
},
NotBefore: notBefore,
NotAfter: notAfter,
// KeyEncipherment is only used for RSA keys.
KeyUsage:/* x509.KeyUsageKeyEncipherment | */ x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
DNSNames: []string{"localhost"},
}
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
if err != nil {
panic(err)
}
privateKey, err := x509.MarshalPKCS8PrivateKey(priv)
if err != nil {
panic(err)
}
certOut, err := os.Create("proxy.crt")
if err != nil {
panic(err)
}
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
certOut.Close()
keyOut, err := os.OpenFile("proxy.key", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
panic(err)
}
pem.Encode(keyOut, &pem.Block{Type: "PRIVATE KEY", Bytes: privateKey})
keyOut.Close()
}
func cleanup() {
log.Printf("Cleaning up X509 certificate and private key...")
os.Remove("proxy.crt")
os.Remove("proxy.key")
}
func runCommands(cmds [][]string) {
for _, c := range cmds {
cmd := exec.Command(c[0], c[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
panic(err)
}
}
}
// The following code is a simple HTTPS proxy server.
func handleTunneling(w http.ResponseWriter, r *http.Request) {
log.Printf("Handling request: %s %s", r.Method, r.URL.String())
if r.URL.Path == "/ping" {
w.WriteHeader(http.StatusOK)
w.Write([]byte("pong"))
return
}
destConn, err := net.Dial("tcp", r.URL.Host)
if err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
w.WriteHeader(http.StatusOK)
hijacker, ok := w.(http.Hijacker)
if !ok {
http.Error(w, "HTTP Server does not support hijacking", http.StatusInternalServerError)
return
}
clientConn, _, err := hijacker.Hijack()
if err != nil {
http.Error(w, "Error hijacking connection: "+err.Error(), http.StatusInternalServerError)
return
}
go transfer(destConn, clientConn)
go transfer(clientConn, destConn)
}
func transfer(destination io.WriteCloser, source io.ReadCloser) {
defer destination.Close()
defer source.Close()
io.Copy(destination, source)
}
func startServer() *http.Server {
log.Printf("Starting server...")
server := &http.Server{
Addr: "localhost:8443",
Handler: http.HandlerFunc(handleTunneling),
}
err := http2.ConfigureServer(server, &http2.Server{})
if err != nil {
panic(err)
}
// Start the server
log.Printf("Starting proxy server on https://%s", server.Addr)
go func() {
err := server.ListenAndServeTLS("proxy.crt", "proxy.key")
if !errors.Is(err, http.ErrServerClosed) {
panic(err)
}
}()
log.Printf("Waiting for server to start...")
for {
_, err := http.Get("https://localhost:8443/ping")
if err == nil {
break
}
log.Printf("Sleeping for 1 second...")
time.Sleep(time.Second)
}
log.Printf("Server started.")
return server
}