-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (75 loc) · 1.34 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
package main
import (
"crypto/tls"
"fmt"
"net"
"os"
)
func chanFromConn(conn net.Conn) chan []byte {
c := make(chan []byte)
go func() {
for {
b := make([]byte, 1024)
n, err := conn.Read(b)
if n > 0 {
res := make([]byte, n)
copy(res, b[:n])
c <- res
}
if err != nil {
c <- nil
break
}
}
}()
return c
}
func main() {
certificate, err := tls.LoadX509KeyPair("/home/ian/ian.crt.pem",
"/home/ian/ian.key.pem")
if err != nil {
fmt.Printf("Failed to load certs: %s\n", err)
os.Exit(1)
}
config := &tls.Config{
Certificates: []tls.Certificate{certificate},
}
ln, err := net.Listen("tcp", ":8080")
fmt.Printf("Listening on :8080\n")
if err != nil {
fmt.Printf("Failed to listen: %s\n", err)
os.Exit(1)
}
for {
c, err := ln.Accept()
if err != nil {
fmt.Printf("Failed to accept: %s\n", err)
continue
}
go func(c net.Conn) {
client, err := tls.Dial("tcp", "secret.theknown.net:443", config)
if err != nil {
fmt.Printf("Failed to connect: %s\n", err)
return
}
upstream := chanFromConn(client)
local := chanFromConn(c)
go func() {
for {
select {
case b1 := <-upstream:
if b1 == nil {
return
}
c.Write(b1)
case b2 := <-local:
if b2 == nil {
return
}
client.Write(b2)
}
}
}()
}(c)
}
}