forked from ochinchina/supervisord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmlrpc.go
146 lines (133 loc) · 5.84 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
package main
import (
"crypto/sha1"
"encoding/hex"
"io"
"net"
"net/http"
"os"
"strings"
"github.com/gorilla/rpc"
"github.com/ochinchina/gorilla-xmlrpc/xml"
log "github.com/sirupsen/logrus"
)
type XmlRPC struct {
listeners map[string]net.Listener
// true if RPC is started
started bool
}
type httpBasicAuth struct {
user string
password string
handler http.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()
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)
}
func NewXmlRPC() *XmlRPC {
return &XmlRPC{listeners: make(map[string]net.Listener), started: false}
}
// stop network listening
func (p *XmlRPC) Stop() {
log.Info("stop listening")
for _, listener := range p.listeners {
listener.Close()
}
p.started = false
}
func (p *XmlRPC) StartUnixHttpServer(user string, password string, listenAddr string, s *Supervisor) {
os.Remove(listenAddr)
p.startHttpServer(user, password, "unix", listenAddr, s)
}
func (p *XmlRPC) StartInetHttpServer(user string, password string, listenAddr string, s *Supervisor) {
p.startHttpServer(user, password, "tcp", listenAddr, s)
}
func (p *XmlRPC) startHttpServer(user string, password string, protocol string, listenAddr string, s *Supervisor) {
if p.started {
return
}
p.started = true
mux := http.NewServeMux()
mux.Handle("/RPC2", NewHttpBasicAuth(user, password, p.createRPCServer(s)))
prog_rest_handler := NewSupervisorRestful(s).CreateProgramHandler()
mux.Handle("/program/", NewHttpBasicAuth(user, password, prog_rest_handler))
supervisor_rest_handler := NewSupervisorRestful(s).CreateSupervisorHandler()
mux.Handle("/supervisor/", NewHttpBasicAuth(user, password, supervisor_rest_handler))
webgui_handler := NewSupervisorWebgui(s).CreateHandler()
mux.Handle("/", NewHttpBasicAuth(user, password, webgui_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
http.Serve(listener, mux)
} else {
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
}