-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote.go
61 lines (51 loc) · 1.3 KB
/
remote.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
package main
import (
"html/template"
"net"
"net/http"
log "github.com/sirupsen/logrus"
"golang.org/x/net/websocket"
)
func Remote(s *Settings) {
sec, err := NewSecurity(s.Passkey)
if err != nil {
log.Fatal(err)
}
srv := &http.Server{
Addr: ":" + s.Port,
}
fs := http.FileServer(http.Dir("asset"))
http.Handle("/static/", fs)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("asset/index.html"))
tmpl.Execute(w, nil)
})
http.Handle("/_a", websocket.Handler(secureHandler(sec)))
log.Info("Remote start on: ", srv.Addr)
log.Fatal("Remote start failure: ", srv.ListenAndServe())
}
func secureHandler(sec *Security) func(*websocket.Conn) {
return func(ws *websocket.Conn) {
wss := sec.secure(ws)
defer wss.Close()
addr, err := ReadAddr(wss, "tcp")
if err != nil {
log.Warn("Addr read failure: ", err)
return
}
// Relay to target
tc, err := net.Dial(addr.Network(), addr.String())
if err != nil {
log.Warn("Target dial failure: ", err)
return
}
defer tc.Close()
tc.(*net.TCPConn).SetKeepAlive(true)
if nout, nin, err := relay(tc, wss); err != nil {
log.Warn("Relay target failure: ", err)
return
} else {
log.Infof("Away: %s ~ %s <%d %d>", wss.RemoteAddr(), addr.String(), nin, nout)
}
}
}