|
@@ -21,7 +21,7 @@ import (
|
21
|
21
|
"io"
|
22
|
22
|
"net"
|
23
|
23
|
"net/http"
|
24
|
|
- "net/url"
|
|
24
|
+ gourl "net/url"
|
25
|
25
|
"os"
|
26
|
26
|
"os/exec"
|
27
|
27
|
"regexp"
|
|
@@ -51,28 +51,62 @@ func (ec *errorCatcher) PrintErr(args ...interface{}) {
|
51
|
51
|
ec.UI.PrintErr(args...)
|
52
|
52
|
}
|
53
|
53
|
|
54
|
|
-func serveWebInterface(port int, p *profile.Profile, o *plugin.Options) error {
|
|
54
|
+func serveWebInterface(hostport string, p *profile.Profile, o *plugin.Options) error {
|
55
|
55
|
interactiveMode = true
|
56
|
56
|
ui := &webInterface{
|
57
|
57
|
prof: p,
|
58
|
58
|
options: o,
|
59
|
59
|
}
|
|
60
|
+
|
|
61
|
+ ln, url, isLocal, err := newListenerAndURL(hostport)
|
|
62
|
+ if err != nil {
|
|
63
|
+ return err
|
|
64
|
+ }
|
|
65
|
+
|
60
|
66
|
// authorization wrapper
|
61
|
67
|
wrap := o.HTTPWrapper
|
62
|
|
- if wrap == nil {
|
63
|
|
- // only allow requests from local host
|
|
68
|
+ if wrap == nil && isLocal {
|
|
69
|
+ // Only allow requests from local host.
|
64
|
70
|
wrap = checkLocalHost
|
65
|
71
|
}
|
66
|
|
- http.Handle("/", wrap(http.HandlerFunc(ui.dot)))
|
67
|
|
- http.Handle("/disasm", wrap(http.HandlerFunc(ui.disasm)))
|
68
|
|
- http.Handle("/weblist", wrap(http.HandlerFunc(ui.weblist)))
|
69
|
|
- go openBrowser(port, o)
|
70
|
|
- return http.ListenAndServe(fmt.Sprint(":", port), nil)
|
|
72
|
+
|
|
73
|
+ mux := http.NewServeMux()
|
|
74
|
+ mux.Handle("/", wrap(http.HandlerFunc(ui.dot)))
|
|
75
|
+ mux.Handle("/disasm", wrap(http.HandlerFunc(ui.disasm)))
|
|
76
|
+ mux.Handle("/weblist", wrap(http.HandlerFunc(ui.weblist)))
|
|
77
|
+
|
|
78
|
+ s := &http.Server{Handler: mux}
|
|
79
|
+ go openBrowser(url, o)
|
|
80
|
+ return s.Serve(ln)
|
|
81
|
+}
|
|
82
|
+
|
|
83
|
+func newListenerAndURL(hostport string) (ln net.Listener, url string, isLocal bool, err error) {
|
|
84
|
+ host, _, err := net.SplitHostPort(hostport)
|
|
85
|
+ if err != nil {
|
|
86
|
+ return nil, "", false, err
|
|
87
|
+ }
|
|
88
|
+ if host == "" {
|
|
89
|
+ host = "localhost"
|
|
90
|
+ }
|
|
91
|
+ if ln, err = net.Listen("tcp", hostport); err != nil {
|
|
92
|
+ return nil, "", false, err
|
|
93
|
+ }
|
|
94
|
+ url = fmt.Sprint("http://", host, ":", ln.Addr().(*net.TCPAddr).Port)
|
|
95
|
+ return ln, url, isLocalhost(host), nil
|
|
96
|
+}
|
|
97
|
+
|
|
98
|
+func isLocalhost(host string) bool {
|
|
99
|
+ for _, v := range []string{"localhost", "127.0.0.1", "[::1]"} {
|
|
100
|
+ if host == v {
|
|
101
|
+ return true
|
|
102
|
+ }
|
|
103
|
+ }
|
|
104
|
+ return false
|
71
|
105
|
}
|
72
|
106
|
|
73
|
|
-func openBrowser(port int, o *plugin.Options) {
|
|
107
|
+func openBrowser(url string, o *plugin.Options) {
|
74
|
108
|
// Construct URL.
|
75
|
|
- u, _ := url.Parse(fmt.Sprint("http://localhost:", port))
|
|
109
|
+ u, _ := gourl.Parse(url)
|
76
|
110
|
q := u.Query()
|
77
|
111
|
for _, p := range []struct{ param, key string }{
|
78
|
112
|
{"f", "focus"},
|
|
@@ -107,7 +141,7 @@ func openBrowser(port int, o *plugin.Options) {
|
107
|
141
|
func checkLocalHost(h http.Handler) http.Handler {
|
108
|
142
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
109
|
143
|
host, _, err := net.SplitHostPort(req.RemoteAddr)
|
110
|
|
- if err != nil || ((host != "127.0.0.1") && (host != "::1")) {
|
|
144
|
+ if err != nil || !isLocalhost(host) {
|
111
|
145
|
http.Error(w, "permission denied", http.StatusForbidden)
|
112
|
146
|
return
|
113
|
147
|
}
|