-
Notifications
You must be signed in to change notification settings - Fork 299
/
handlers.go
180 lines (148 loc) · 4.33 KB
/
handlers.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package kite
import (
"errors"
"fmt"
"net/http"
"net/url"
"os"
"os/exec"
"runtime"
"time"
"github.com/gorilla/websocket"
"github.com/koding/cache"
"github.com/koding/kite/protocol"
"github.com/koding/kite/sockjsclient"
"github.com/koding/kite/systeminfo"
"golang.org/x/crypto/ssh/terminal"
)
var (
errDstNotSet = errors.New("dst not set")
errDstNotRegistered = errors.New("dst not registered")
)
// WebRTCHandlerName provides the naming scheme for the handler
const WebRTCHandlerName = "kite.handleWebRTC"
type webRTCHandler struct {
kitesColl cache.Cache
}
// NewWebRCTHandler creates a new handler for web rtc signalling services.
func NewWebRCTHandler() *webRTCHandler {
return &webRTCHandler{
kitesColl: cache.NewMemory(),
}
}
func (w *webRTCHandler) registerSrc(src *Client) {
w.kitesColl.Set(src.ID, src)
src.OnDisconnect(func() {
time.Sleep(time.Second * 2)
id := src.ID
// delete from the collection
w.kitesColl.Delete(id)
})
}
func (w *webRTCHandler) getDst(dst string) (*Client, error) {
if dst == "" {
return nil, errDstNotSet
}
dstKite, err := w.kitesColl.Get(dst)
if err != nil {
return nil, errDstNotRegistered
}
return dstKite.(*Client), nil
}
// ServeKite implements Hander interface.
func (w *webRTCHandler) ServeKite(r *Request) (interface{}, error) {
var args protocol.WebRTCSignalMessage
if err := r.Args.One().Unmarshal(&args); err != nil {
return nil, fmt.Errorf("invalid query: %s", err)
}
args.Src = r.Client.ID
w.registerSrc(r.Client)
dst, err := w.getDst(args.Dst)
if err != nil {
return nil, err
}
return nil, dst.SendWebRTCRequest(&args)
}
func (k *Kite) addDefaultHandlers() {
// Default RPC methods
k.HandleFunc("kite.systemInfo", handleSystemInfo)
k.HandleFunc("kite.heartbeat", k.handleHeartbeat)
k.HandleFunc("kite.ping", handlePing).DisableAuthentication()
k.HandleFunc("kite.tunnel", handleTunnel)
k.HandleFunc("kite.log", k.handleLog)
k.HandleFunc("kite.print", handlePrint)
k.HandleFunc("kite.prompt", handlePrompt)
k.HandleFunc("kite.getPass", handleGetPass)
if runtime.GOOS == "darwin" {
k.HandleFunc("kite.notify", handleNotifyDarwin)
}
if k.WebRTCHandler != nil {
k.Handle(WebRTCHandlerName, k.WebRTCHandler)
}
}
// handleSystemInfo returns info about the system (CPU, memory, disk...).
func handleSystemInfo(r *Request) (interface{}, error) {
return systeminfo.New()
}
// handleLog prints a log message to stderr.
func (k *Kite) handleLog(r *Request) (interface{}, error) {
msg, err := r.Args.One().String()
if err != nil {
return nil, err
}
k.Log.Info("%s: %s", r.Client.Name, msg)
return nil, nil
}
//handlePing returns a simple "pong" string
func handlePing(r *Request) (interface{}, error) {
return "pong", nil
}
// handlePrint prints a message to stdout.
func handlePrint(r *Request) (interface{}, error) {
return fmt.Print(r.Args.One().MustString())
}
// handlePrompt asks user a single line input.
func handlePrompt(r *Request) (interface{}, error) {
fmt.Print(r.Args.One().MustString())
var s string
_, err := fmt.Scanln(&s)
return s, err
}
// handleGetPass reads a line of input from a terminal without local echo.
func handleGetPass(r *Request) (interface{}, error) {
fmt.Print(r.Args.One().MustString())
data, err := terminal.ReadPassword(int(os.Stdin.Fd())) // stdin
fmt.Println()
if err != nil {
return nil, err
}
return string(data), nil
}
// handleNotifyDarwin displays a desktop notification on OS X.
func handleNotifyDarwin(r *Request) (interface{}, error) {
args := r.Args.MustSliceOfLength(3)
cmd := exec.Command("osascript", "-e", fmt.Sprintf("display notification \"%s\" with title \"%s\" subtitle \"%s\"",
args[1].MustString(), args[2].MustString(), args[0].MustString()))
return nil, cmd.Start()
}
// handleTunnel opens two websockets, one to proxy kite and one to itself,
// then it copies the message between them.
func handleTunnel(r *Request) (interface{}, error) {
var args struct {
URL string
}
r.Args.One().MustUnmarshal(&args)
parsed, err := url.Parse(args.URL)
if err != nil {
return nil, err
}
requestHeader := http.Header{}
requestHeader.Add("Origin", "http://"+parsed.Host)
remoteConn, _, err := websocket.DefaultDialer.Dial(parsed.String(), requestHeader)
if err != nil {
return nil, err
}
session := sockjsclient.NewWebsocketSession(remoteConn)
go r.LocalKite.sockjsHandler(session)
return nil, nil
}