-
Notifications
You must be signed in to change notification settings - Fork 12
/
go-sslterminator.go
88 lines (71 loc) · 1.74 KB
/
go-sslterminator.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
package main
import (
"crypto/tls"
"flag"
"io"
"log"
"net"
"runtime"
)
var localAddress string
var backendAddress string
var certificatePath string
var keyPath string
func init() {
flag.StringVar(&localAddress, "l", ":44300", "local address")
flag.StringVar(&backendAddress, "b", ":8000", "backend address")
flag.StringVar(&certificatePath, "c", "cert.pem", "SSL certificate path")
flag.StringVar(&keyPath, "k", "key.pem", "SSL key path")
}
func main() {
flag.Parse()
runtime.GOMAXPROCS(runtime.NumCPU())
cert, err := tls.LoadX509KeyPair(certificatePath, keyPath)
if err != nil {
log.Fatalf("error in tls.LoadX509KeyPair: %s", err)
}
config := tls.Config{Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true}
listener, err := tls.Listen("tcp", localAddress, &config)
if err != nil {
log.Fatalf("error in tls.Listen: %s", err)
}
log.Printf("local server on: %s, backend server on: %s", localAddress, backendAddress)
for {
conn, err := listener.Accept()
if err != nil {
log.Printf("error in listener.Accept: %s", err)
break
}
go handle(conn)
}
}
func handle(clientConn net.Conn) {
tlsconn, ok := clientConn.(*tls.Conn)
if ok {
err := tlsconn.Handshake()
if err != nil {
log.Printf("error in tls.Handshake: %s", err)
clientConn.Close()
return
}
backendConn, err := net.Dial("tcp", backendAddress)
if err != nil {
log.Printf("error in net.Dial: %s", err)
clientConn.Close()
return
}
go Tunnel(clientConn, backendConn)
go Tunnel(backendConn, clientConn)
}
}
func Tunnel(from, to io.ReadWriteCloser) {
defer func() {
if r := recover(); r != nil {
log.Printf("recovered while tunneling")
}
}()
io.Copy(from, to)
to.Close()
from.Close()
log.Printf("tunneling is done")
}