-
Notifications
You must be signed in to change notification settings - Fork 242
/
server.go
69 lines (65 loc) · 1.33 KB
/
server.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
package main
import (
"log"
"net"
"time"
"github.com/inconshreveable/go-vhost"
"github.com/progrium/duplex/poc2/duplex"
)
func server(backendListen, frontendListen string) {
// bind frontend
frontend, err := net.Listen("tcp", frontendListen)
if err != nil {
log.Fatal(err)
}
log.Println("frontend listening on", frontendListen, "...")
mux, err := vhost.NewHTTPMuxer(frontend, 1*time.Second)
if err != nil {
log.Fatal(err)
}
go func() {
for {
conn, err := mux.NextError()
log.Println("vhost:", err)
if conn != nil {
conn.Write([]byte("error: " + err.Error()))
conn.Close()
}
}
}()
// bind backend
backend := duplex.NewPeer()
err = backend.Bind("tcp://" + backendListen)
if err != nil {
log.Fatal(err)
}
log.Println("backend listening on", backendListen, "...")
for {
meta, ch := backend.Accept()
if meta == nil {
break
}
// register vhost
if meta.Service() == "tunnel" {
log.Println("registering", meta.RemotePeer())
vhostListener, err := mux.Listen(meta.RemotePeer())
if err != nil {
log.Println(err)
}
go func() {
for {
conn, err := vhostListener.Accept()
if err != nil {
log.Println(err)
}
tunnelCh, err := ch.Open("", nil)
if err != nil {
log.Println(err)
}
go tunnelCh.Join(conn)
}
}()
continue
}
}
}