Skip to content

Commit fc2fa3c

Browse files
committed
Add a special vhost for health checking that uses /etc/nolvs
Also adds another special vhost for debugging/monitoring
1 parent dbd5b0b commit fc2fa3c

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

server/common/oursrc/scripts-proxy/main.go

+19-4
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,27 @@ func always(context.Context, string) bool {
2828
}
2929

3030
type ldapTarget struct {
31-
localPoolRange *net.IPNet
32-
ldap *ldap.Pool
31+
localPoolRange *net.IPNet
32+
ldap *ldap.Pool
33+
statuszServer *HijackedServer
34+
unavailableServer *HijackedServer
3335
}
3436

3537
func (l *ldapTarget) HandleConn(netConn net.Conn) {
3638
var pool string
3739
var err error
3840
if conn, ok := netConn.(*tcpproxy.Conn); ok {
41+
switch conn.HostName {
42+
case "proxy.scripts.scripts.mit.edu":
43+
// Special handling for proxy.scripts.scripts.mit.edu
44+
l.statuszServer.HandleConn(netConn)
45+
return
46+
case "heartbeat.scripts.scripts.mit.edu":
47+
if nolvsPresent() {
48+
l.unavailableServer.HandleConn(netConn)
49+
return
50+
}
51+
}
3952
pool, err = l.ldap.ResolvePool(conn.HostName)
4053
if err != nil {
4154
log.Printf("resolving %q: %v", conn.HostName, err)
@@ -87,8 +100,10 @@ func main() {
87100

88101
var p tcpproxy.Proxy
89102
t := &ldapTarget{
90-
localPoolRange: ipnet,
91-
ldap: ldapPool,
103+
localPoolRange: ipnet,
104+
ldap: ldapPool,
105+
statuszServer: NewHijackedServer(nil),
106+
unavailableServer: NewUnavailableServer(),
92107
}
93108
for _, addr := range strings.Split(*httpAddrs, ",") {
94109
p.AddHTTPHostMatchRoute(addr, always, t)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"net"
6+
"net/http"
7+
_ "net/http/pprof"
8+
"os"
9+
)
10+
11+
func nolvsPresent() bool {
12+
if _, err := os.Stat("/etc/nolvs"); err == nil {
13+
return true
14+
}
15+
return false
16+
}
17+
18+
type HijackedServer struct {
19+
connCh chan net.Conn
20+
}
21+
22+
func NewHijackedServer(handler http.Handler) *HijackedServer {
23+
s := &HijackedServer{
24+
connCh: make(chan net.Conn),
25+
}
26+
go http.Serve(s, handler)
27+
return s
28+
}
29+
30+
func (s *HijackedServer) Accept() (net.Conn, error) {
31+
c, ok := <-s.connCh
32+
if ok {
33+
return c, nil
34+
}
35+
return nil, errors.New("closed")
36+
}
37+
38+
func (s *HijackedServer) Close() error {
39+
close(s.connCh)
40+
return nil
41+
}
42+
43+
func (s *HijackedServer) Addr() net.Addr {
44+
return nil
45+
}
46+
47+
func (s *HijackedServer) HandleConn(c net.Conn) {
48+
s.connCh <- c
49+
}
50+
51+
func NewUnavailableServer() *HijackedServer {
52+
return NewHijackedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
53+
http.Error(w, "0 proxy nolvs", http.StatusServiceUnavailable)
54+
}))
55+
}

0 commit comments

Comments
 (0)