forked from control-center/serviced
/
mux.go
113 lines (99 loc) · 2.82 KB
/
mux.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
package serviced
import (
"bufio"
"crypto/tls"
"fmt"
"github.com/zenoss/glog"
"io"
"net"
"net/textproto"
"strconv"
"strings"
)
type TCPMux struct {
Enabled bool
UseTLS bool
CertPEMFile string
KeyPEMFile string
Port int
}
type MuxConfig struct {
Proxies []Proxy
TCPMux TCPMux
ServiceId string
Command string
}
// sendMuxError logs an error message and attempts to write it to the connected
// endpoint
func sendMuxError(conn net.Conn, source, facility, msg string, err error) {
glog.Errorf("%s Error (%s): %v\n", source, facility, err)
if _, e := conn.Write([]byte(msg)); e != nil {
glog.Errorf("%s", e)
}
}
// muxConnection takes an inbound connection reads MIME headers from it and
// then attempts to set up a connection to the service specified by the
// Zen-Service header. If the Zen-Service header is missing or the requested
// service is not running (listening) on the local host and error message
// is sent to the requestor and its connection is closed. Otherwise data is
// proxied between the requestor and the local service.
func (mux TCPMux) MuxConnection(conn net.Conn) {
rdr := textproto.NewReader(bufio.NewReader(conn))
hdr, err := rdr.ReadMIMEHeader()
if err != nil {
sendMuxError(conn, "MuxConnection", "textproto.ReadMIMEHeader", "bad request (no headers)", err)
conn.Close()
return
}
zs, ok := hdr["Zen-Service"]
if ok == false {
sendMuxError(conn, "MuxConnection", "MIMEHeader", "bad request (no Zen-Service header)", err)
conn.Close()
return
}
port, err := strconv.Atoi(strings.Split(zs[0], "/")[1])
if err != nil {
sendMuxError(conn, "MuxConnection", "Zen-Service Header", "bad Zen-Service spec", err)
conn.Close()
return
}
svc, err := net.Dial("tcp4", fmt.Sprintf("172.17.42.1:%d", port))
if err != nil {
sendMuxError(conn, "MuxConnection", "net.Dial", "cannot connect to service", err)
conn.Close()
return
}
go io.Copy(conn, svc)
go io.Copy(svc, conn)
}
// listenAndMux listens for incoming connections and attempts to multiplex them
// to the local service that they request via a Zen-Service header in their
// initial message.
func (mux *TCPMux) ListenAndMux() {
var l net.Listener
var err error
if mux.UseTLS == false {
l, err = net.Listen("tcp4", fmt.Sprintf(":%d", mux.Port))
} else {
cert, cerr := tls.X509KeyPair([]byte(proxyCertPEM), []byte(proxyKeyPEM))
if cerr != nil {
glog.Error("ListenAndMux Error (tls.X509KeyPair): ", cerr)
return
}
tlsConfig := tls.Config{Certificates: []tls.Certificate{cert}}
l, err = tls.Listen("tcp4", fmt.Sprintf(":%d", mux.Port), &tlsConfig)
}
if err != nil {
glog.Error("ListenAndMux Error (net.Listen): ", err)
return
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
glog.Error("ListenAndMux Error (net.Accept): ", err)
return
}
go mux.MuxConnection(conn)
}
}