-
Notifications
You must be signed in to change notification settings - Fork 573
/
xmlrpc.go
247 lines (216 loc) · 8.74 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package main
import (
"crypto/sha1" //nolint:gosec
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"path/filepath"
"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 readFile(path string) ([]byte, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
return b, nil
}
func getProgramConfigPath(programName string, s *Supervisor) string {
c := s.config.GetProgram(programName)
if c == nil {
return ""
}
res := c.GetString("conf_file", "")
return res
}
func readLogHtml(writer http.ResponseWriter, request *http.Request) {
b, err := readFile("webgui/log.html")
if err != nil {
writer.WriteHeader(http.StatusNotFound)
return
}
writer.WriteHeader(http.StatusOK)
writer.Write(b)
}
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))
// 有bug已弃用
logtailHandler := NewLogtail(s).CreateHandler()
mux.Handle("/logtail/", newHTTPBasicAuth(user, password, logtailHandler))
webguiHandler := NewSupervisorWebgui(s).CreateHandler()
mux.Handle("/", newHTTPBasicAuth(user, password, webguiHandler))
// conf 文件
confHandler := NewConfApi(s).CreateHandler()
mux.Handle("/conf/", newHTTPBasicAuth(user, password, confHandler))
mux.HandleFunc("/confFile", func(writer http.ResponseWriter, request *http.Request) {
b, err := readFile("webgui/conf.html")
if err != nil {
writer.WriteHeader(http.StatusNotFound)
return
}
writer.WriteHeader(http.StatusOK)
writer.Write(b)
})
// 读log.html文件
mux.HandleFunc("/log", readLogHtml)
mux.Handle("/metrics", promhttp.Handler())
// 注册日志路由,可以查看日志目录
entryList := s.config.GetPrograms()
for _, c := range entryList {
realName := c.GetProgramName()
if realName == "" {
continue
}
filePath := c.GetString("stdout_logfile", "")
if filePath == "" {
continue
}
dir := filepath.Dir(filePath)
fmt.Println(dir)
mux.Handle("/log/"+realName+"/", http.StripPrefix("/log/"+realName+"/", http.FileServer(http.Dir(dir))))
}
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
}