forked from ochinchina/supervisord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlrpc.go
168 lines (154 loc) · 7.18 KB
/
xmlrpc.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"crypto/sha1" //nolint:gosec
"encoding/hex"
"io"
"net"
"net/http"
"os"
"strings"
"github.com/gorilla/rpc"
"github.com/ochinchina/gorilla-xmlrpc/xml"
"github.com/ochinchina/supervisord/process"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)
// XMLRPC mange the XML RPC servers
// start XML RPC servers to accept the XML RPC request from client side
type XMLRPC struct {
// all the listeners to accept the XML RPC request
listeners map[string]net.Listener
}
type httpBasicAuth struct {
user string
password string
handler http.Handler
}
// create a new HttpBasicAuth object with username, password and the http request handler
func newHTTPBasicAuth(user string, password string, handler http.Handler) *httpBasicAuth {
if user != "" && password != "" {
log.Debug("require authentication")
}
return &httpBasicAuth{user: user, password: password, handler: handler}
}
func (h *httpBasicAuth) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if h.user == "" || h.password == "" {
log.Debug("no auth required")
h.handler.ServeHTTP(w, r)
return
}
username, password, ok := r.BasicAuth()
if ok && username == h.user {
if strings.HasPrefix(h.password, "{SHA}") {
log.Debug("auth with SHA")
hash := sha1.New() //nolint:gosec
io.WriteString(hash, password)
if hex.EncodeToString(hash.Sum(nil)) == h.password[5:] {
h.handler.ServeHTTP(w, r)
return
}
} else if password == h.password {
log.Debug("Auth with normal password")
h.handler.ServeHTTP(w, r)
return
}
}
w.Header().Set("WWW-Authenticate", "Basic realm=\"supervisor\"")
w.WriteHeader(401)
}
// NewXMLRPC create a new XML RPC object
func NewXMLRPC() *XMLRPC {
return &XMLRPC{listeners: make(map[string]net.Listener)}
}
// Stop network listening
func (p *XMLRPC) Stop() {
log.Info("stop listening")
for _, listener := range p.listeners {
listener.Close()
}
p.listeners = make(map[string]net.Listener)
}
// StartUnixHTTPServer start http server on unix domain socket with path listenAddr. If both user and password are not empty, the user
// must provide user and password for basic authentication when making an XML RPC request.
func (p *XMLRPC) StartUnixHTTPServer(user string, password string, listenAddr string, s *Supervisor, startedCb func()) {
os.Remove(listenAddr)
p.startHTTPServer(user, password, "unix", listenAddr, s, startedCb)
}
// StartInetHTTPServer start http server on tcp with path listenAddr. If both user and password are not empty, the user
// must provide user and password for basic authentication when making an XML RPC request.
func (p *XMLRPC) StartInetHTTPServer(user string, password string, listenAddr string, s *Supervisor, startedCb func()) {
p.startHTTPServer(user, password, "tcp", listenAddr, s, startedCb)
}
func (p *XMLRPC) isHTTPServerStartedOnProtocol(protocol string) bool {
_, ok := p.listeners[protocol]
return ok
}
func (p *XMLRPC) startHTTPServer(user string, password string, protocol string, listenAddr string, s *Supervisor, startedCb func()) {
if p.isHTTPServerStartedOnProtocol(protocol) {
startedCb()
return
}
procCollector := process.NewProcCollector(s.procMgr)
prometheus.Register(procCollector)
mux := http.NewServeMux()
mux.Handle("/RPC2", newHTTPBasicAuth(user, password, p.createRPCServer(s)))
progRestHandler := NewSupervisorRestful(s).CreateProgramHandler()
mux.Handle("/program/", newHTTPBasicAuth(user, password, progRestHandler))
supervisorRestHandler := NewSupervisorRestful(s).CreateSupervisorHandler()
mux.Handle("/supervisor/", newHTTPBasicAuth(user, password, supervisorRestHandler))
logtailHandler := NewLogtail(s).CreateHandler()
mux.Handle("/logtail/", newHTTPBasicAuth(user, password, logtailHandler))
webguiHandler := NewSupervisorWebgui(s).CreateHandler()
mux.Handle("/", newHTTPBasicAuth(user, password, webguiHandler))
mux.Handle("/metrics", promhttp.Handler())
listener, err := net.Listen(protocol, listenAddr)
if err == nil {
log.WithFields(log.Fields{"addr": listenAddr, "protocol": protocol}).Info("success to listen on address")
p.listeners[protocol] = listener
startedCb()
http.Serve(listener, mux)
} else {
startedCb()
log.WithFields(log.Fields{"addr": listenAddr, "protocol": protocol}).Fatal("fail to listen on address")
}
}
func (p *XMLRPC) createRPCServer(s *Supervisor) *rpc.Server {
RPC := rpc.NewServer()
xmlrpcCodec := xml.NewCodec()
RPC.RegisterCodec(xmlrpcCodec, "text/xml")
RPC.RegisterService(s, "")
xmlrpcCodec.RegisterAlias("supervisor.getVersion", "Supervisor.GetVersion")
xmlrpcCodec.RegisterAlias("supervisor.getAPIVersion", "Supervisor.GetVersion")
xmlrpcCodec.RegisterAlias("supervisor.getIdentification", "Supervisor.GetIdentification")
xmlrpcCodec.RegisterAlias("supervisor.getState", "Supervisor.GetState")
xmlrpcCodec.RegisterAlias("supervisor.getPID", "Supervisor.GetPID")
xmlrpcCodec.RegisterAlias("supervisor.readLog", "Supervisor.ReadLog")
xmlrpcCodec.RegisterAlias("supervisor.clearLog", "Supervisor.ClearLog")
xmlrpcCodec.RegisterAlias("supervisor.shutdown", "Supervisor.Shutdown")
xmlrpcCodec.RegisterAlias("supervisor.restart", "Supervisor.Restart")
xmlrpcCodec.RegisterAlias("supervisor.getProcessInfo", "Supervisor.GetProcessInfo")
xmlrpcCodec.RegisterAlias("supervisor.getSupervisorVersion", "Supervisor.GetVersion")
xmlrpcCodec.RegisterAlias("supervisor.getAllProcessInfo", "Supervisor.GetAllProcessInfo")
xmlrpcCodec.RegisterAlias("supervisor.startProcess", "Supervisor.StartProcess")
xmlrpcCodec.RegisterAlias("supervisor.startAllProcesses", "Supervisor.StartAllProcesses")
xmlrpcCodec.RegisterAlias("supervisor.startProcessGroup", "Supervisor.StartProcessGroup")
xmlrpcCodec.RegisterAlias("supervisor.stopProcess", "Supervisor.StopProcess")
xmlrpcCodec.RegisterAlias("supervisor.stopProcessGroup", "Supervisor.StopProcessGroup")
xmlrpcCodec.RegisterAlias("supervisor.stopAllProcesses", "Supervisor.StopAllProcesses")
xmlrpcCodec.RegisterAlias("supervisor.signalProcess", "Supervisor.SignalProcess")
xmlrpcCodec.RegisterAlias("supervisor.signalProcessGroup", "Supervisor.SignalProcessGroup")
xmlrpcCodec.RegisterAlias("supervisor.signalAllProcesses", "Supervisor.SignalAllProcesses")
xmlrpcCodec.RegisterAlias("supervisor.sendProcessStdin", "Supervisor.SendProcessStdin")
xmlrpcCodec.RegisterAlias("supervisor.sendRemoteCommEvent", "Supervisor.SendRemoteCommEvent")
xmlrpcCodec.RegisterAlias("supervisor.reloadConfig", "Supervisor.ReloadConfig")
xmlrpcCodec.RegisterAlias("supervisor.addProcessGroup", "Supervisor.AddProcessGroup")
xmlrpcCodec.RegisterAlias("supervisor.removeProcessGroup", "Supervisor.RemoveProcessGroup")
xmlrpcCodec.RegisterAlias("supervisor.readProcessStdoutLog", "Supervisor.ReadProcessStdoutLog")
xmlrpcCodec.RegisterAlias("supervisor.readProcessStderrLog", "Supervisor.ReadProcessStderrLog")
xmlrpcCodec.RegisterAlias("supervisor.tailProcessStdoutLog", "Supervisor.TailProcessStdoutLog")
xmlrpcCodec.RegisterAlias("supervisor.tailProcessStderrLog", "Supervisor.TailProcessStderrLog")
xmlrpcCodec.RegisterAlias("supervisor.clearProcessLogs", "Supervisor.ClearProcessLogs")
xmlrpcCodec.RegisterAlias("supervisor.clearAllProcessLogs", "Supervisor.ClearAllProcessLogs")
return RPC
}