/
app.go
161 lines (141 loc) · 3.33 KB
/
app.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
package main
import (
"encoding/json"
"encoding/xml"
"github.com/godbus/dbus"
"github.com/godbus/dbus/introspect"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"io"
"net/http"
"os"
"path"
)
const pagedata = `
<!DOCTYPE html>
<html>
<head>
<title>DBUS Inspector</title>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
<body>
<div id="app"></div>
<script src="/app.js"></script>
</body>
</html>
`
func js(w http.ResponseWriter, data interface{}) {
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(data)
if err != nil {
panic(err)
}
}
func index(w http.ResponseWriter, req *http.Request) {
log.Println("index.html")
io.WriteString(w, pagedata)
}
func appjs(w http.ResponseWriter, req *http.Request) {
fd, err := os.Open("public/app.js")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer fd.Close()
w.Header().Set("Content-Type", "application/javascript")
io.Copy(w, fd)
}
func getBus(vars map[string]string) *dbus.Conn {
switch vars["bus"] {
case "system":
return SystemBus
case "session":
return SessionBus
default:
return nil
}
}
func handleListServices(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bus := getBus(vars)
if bus == nil {
http.NotFound(w, r)
return
}
names, err := ListServices(bus)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
js(w, &names)
}
func recurseObjects(bus *dbus.Conn, service, prefix string) ([]string, error) {
var s string
err := bus.Object(service, dbus.ObjectPath(prefix)).Call("org.freedesktop.DBus.Introspectable.Introspect", 0).Store(&s)
if err != nil {
return nil, err
}
var n introspect.Node
err = xml.Unmarshal([]byte(s), &n)
if err != nil {
return nil, err
}
names := make([]string, 0, 100)
if len(n.Interfaces) > 1 {
names = append(names, prefix)
}
for _, child := range n.Children {
childPath := path.Join(prefix, child.Name)
items, err := recurseObjects(bus, service, childPath)
if err != nil {
return nil, err
}
names = append(names, items...)
}
return names, nil
}
func handleListObjects(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bus := getBus(vars)
if bus == nil {
http.NotFound(w, r)
return
}
items, err := recurseObjects(bus, vars["service"], "/")
if err != nil {
http.Error(w, err.Error(), 500)
return
}
js(w, items)
}
func handleListInterfaces(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
bus := getBus(vars)
if bus == nil {
http.NotFound(w, r)
return
}
var s string
err := bus.Object(vars["service"], dbus.ObjectPath(r.URL.Query().Get("objectPath"))).Call("org.freedesktop.DBus.Introspectable.Introspect", 0).Store(&s)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
var n introspect.Node
err = xml.Unmarshal([]byte(s), &n)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
js(w, &n)
}
func ListenAndServe(addr string) error {
r := mux.NewRouter()
r.HandleFunc("/api/bus/{bus}", handleListServices)
r.HandleFunc("/api/bus/{bus}/{service}", handleListObjects)
r.HandleFunc("/api/bus/{bus}/{service}/interfaces", handleListInterfaces)
r.HandleFunc("/app.js", appjs)
r.HandleFunc("/", index)
log.Infoln("Listening:", addr)
return http.ListenAndServe(addr, r)
}