forked from xiaokangwang/BrowserBridge
-
Notifications
You must be signed in to change notification settings - Fork 5
/
handler.go
53 lines (45 loc) · 945 Bytes
/
handler.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
package handler
import (
"net/http"
"github.com/xtaci/smux"
)
type HandleSettings struct {
ListenAddr string
RemoteAddr string
}
func Handle(settings *HandleSettings) error {
var hdl = HTTPHandle{
link: new(BridgeLink),
}
hdl.link.RemoteAddr = settings.RemoteAddr
return http.ListenAndServe(settings.ListenAddr, hdl)
}
func NewHttpHandle() *HTTPHandle {
return &HTTPHandle{
link: new(BridgeLink),
}
}
type HTTPHandle struct {
link *BridgeLink
}
type BridgeLink struct {
bridgemux *smux.Session
RemoteAddr string
}
func (hs HTTPHandle) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
requestPath := r.URL.Path[1:]
switch requestPath {
case "":
fallthrough
case "index.js":
BridgeResource(rw, r, requestPath)
break
case "link":
hs.ServeBridge(rw, r)
case "forward":
hs.ServeClient(rw, r)
}
}
func BridgeResource(rw http.ResponseWriter, r *http.Request, path string) {
http.ServeFile(rw, r, path)
}