File: server.go

package info (click to toggle)
tty-share 2.4.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,416 kB
  • sloc: javascript: 153; makefile: 20; sh: 10
file content (298 lines) | stat: -rw-r--r-- 7,874 bytes parent folder | download | duplicates (2)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package server

import (
	"encoding/json"
	"fmt"
	"html/template"
	"io"
	"mime"
	"net"
	"net/http"
	"os"
	"path/filepath"

	"github.com/gorilla/mux"
	"github.com/gorilla/websocket"
	"github.com/hashicorp/yamux"
	log "github.com/sirupsen/logrus"
)

const (
	errorNotFound   = iota
	errorNotAllowed = iota
)

type PTYHandler interface {
	Write(data []byte) (int, error)
	Refresh()
}

// SessionTemplateModel used for templating
type AASessionTemplateModel struct {
	SessionID string
	Salt      string
	WSPath    string
}

// TTYServerConfig is used to configure the tty server before it is started
type TTYServerConfig struct {
	FrontListenAddress string
	FrontendPath       string
	PTY                PTYHandler
	SessionID          string
	AllowTunneling     bool
}

// TTYServer represents the instance of a tty server
type TTYServer struct {
	httpServer       *http.Server
	config           TTYServerConfig
	session          *ttyShareSession
	muxTunnelSession *yamux.Session
}

func (server *TTYServer) serveContent(w http.ResponseWriter, r *http.Request, name string) {
	// If a path to the frontend resources was passed, serve from there, otherwise, serve from the
	// builtin bundle
	if server.config.FrontendPath == "" {
		file, err := Asset(name)

		if err != nil {
			w.WriteHeader(http.StatusNotFound)
			return
		}

		ctype := mime.TypeByExtension(filepath.Ext(name))
		if ctype == "" {
			ctype = http.DetectContentType(file)
		}
		w.Header().Set("Content-Type", ctype)
		w.Write(file)
	} else {
		filePath := server.config.FrontendPath + string(os.PathSeparator) + name
		_, err := os.Open(filePath)

		if err != nil {
			log.Errorf("Couldn't find resource: %s at %s", name, filePath)
			w.WriteHeader(http.StatusNotFound)
			return
		}
		log.Debugf("Serving %s from %s", name, filePath)

		http.ServeFile(w, r, filePath)
	}
}

// NewTTYServer creates a new instance
func NewTTYServer(config TTYServerConfig) (server *TTYServer) {
	server = &TTYServer{
		config: config,
	}
	server.httpServer = &http.Server{
		Addr: config.FrontListenAddress,
	}
	routesHandler := mux.NewRouter()

	installHandlers := func(session string) {
		// This function installs handlers for paths that contain the "session" passed as a
		// parameter. The paths are for the static files, websockets, and other.
		staticPath := "/s/" + session + "/static/"
		ttyWsPath := "/s/" + session + "/ws"
		tunnelWsPath := "/s/" + session + "/tws"
		pathPrefix := "/s/" + session

		routesHandler.PathPrefix(staticPath).Handler(http.StripPrefix(staticPath,
			http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				server.serveContent(w, r, r.URL.Path)
			})))

		routesHandler.HandleFunc(pathPrefix+"/", func(w http.ResponseWriter, r *http.Request) {
			// Check the frontend/templates/tty-share.in.html file to see where the template applies
			templateModel := struct {
				PathPrefix string
				WSPath     string
			}{pathPrefix, ttyWsPath}

			// TODO Extract these in constants
			w.Header().Add("TTYSHARE-VERSION", "2")

			// Deprecated HEADER (from prev version)
			// TODO: Find a proper way to stop handling backward versions
			w.Header().Add("TTYSHARE-WSPATH", ttyWsPath)

			w.Header().Add("TTYSHARE-TTY-WSPATH", ttyWsPath)
			w.Header().Add("TTYSHARE-TUNNEL-WSPATH", tunnelWsPath)

			server.handleWithTemplateHtml(w, r, "tty-share.in.html", templateModel)
		})
		routesHandler.HandleFunc(ttyWsPath, func(w http.ResponseWriter, r *http.Request) {
			server.handleTTYWebsocket(w, r)
		})
		if server.config.AllowTunneling {
			// tunnel websockets connection
			routesHandler.HandleFunc(tunnelWsPath, func(w http.ResponseWriter, r *http.Request) {
				server.handleTunnelWebsocket(w, r)
			})
		}
		routesHandler.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			templateModel := struct{ PathPrefix string }{fmt.Sprintf("/s/%s", session)}
			server.handleWithTemplateHtml(w, r, "404.in.html", templateModel)
		})
	}

	// Install the same routes on both the /local/ and /<SessionID>/. The session ID is received
	// from the tty-proxy server, if a public session is involved.
	installHandlers("local")
	installHandlers(config.SessionID)

	server.httpServer.Handler = routesHandler
	server.session = newTTYShareSession(config.PTY)

	return server
}

func (server *TTYServer) handleTTYWebsocket(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		w.WriteHeader(http.StatusForbidden)
		return
	}

	upgrader := websocket.Upgrader{
		ReadBufferSize:  1024,
		WriteBufferSize: 1024,
	}
	conn, err := upgrader.Upgrade(w, r, nil)

	if err != nil {
		log.Error("Cannot create the WS connection: ", err.Error())
		return
	}

	// On a new connection, ask for a refresh/redraw of the terminal app
	server.config.PTY.Refresh()
	server.session.HandleWSConnection(conn)
}

func (server *TTYServer) handleTunnelWebsocket(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		w.WriteHeader(http.StatusForbidden)
		return
	}

	upgrader := websocket.Upgrader{
		ReadBufferSize:  1024,
		WriteBufferSize: 1024,
	}
	wsConn, err := upgrader.Upgrade(w, r, nil)

	if err != nil {
		log.Error("Cannot upgrade to WS for tunnel route connection: ", err.Error())
		return
	}
	defer wsConn.Close()

	// Read the first message on this ws route, and expect it to be a json containing the address
	// to tunnel to. After that first message, will follow the raw connection data
	_, wsReader, err := wsConn.NextReader()
	if err != nil {
		log.Error("Cannot read from the tunnel WS connection ", err.Error())
		return
	}

	var tunInitMsg TunInitMsg
	err = json.NewDecoder(wsReader).Decode(&tunInitMsg)

	if err != nil {
		log.Error("Cannot decode the tunnel init message ", err.Error())
		return
	}

	wsRW := &WSConnReadWriteCloser{
		WsConn: wsConn,
	}

	server.muxTunnelSession, err = yamux.Server(wsRW, nil)

	if err != nil {
		log.Errorf("Could not open a mux server: ", err.Error())
		return
	}

	for {
		muxStream, err := server.muxTunnelSession.Accept()

		if err != nil {
			if err != io.EOF {
				log.Warnf("Mux cannot accept new connections: %s", err.Error())
			}
			return
		}

		localConn, err := net.Dial("tcp", tunInitMsg.Address)
		if err != nil {
			log.Error("Cannot create local connection ", err.Error())
			return
		}

		go func() {
			io.Copy(muxStream, localConn)
			// Not sure yet which of the two io.Copy finishes first, so just close everything in both cases
			defer localConn.Close()
			defer muxStream.Close()
		}()

		go func() {
			io.Copy(localConn, muxStream)
			// Not sure yet which of the two io.Copy finishes first, so just close everything in both cases
			defer muxStream.Close()
			defer localConn.Close()
		}()
	}

}

func panicIfErr(err error) {
	if err != nil {
		panic(err.Error())
	}
}

func (server *TTYServer) handleWithTemplateHtml(responseWriter http.ResponseWriter, r *http.Request, templateFile string, templateInterface interface{}) {
	var t *template.Template
	var err error
	if server.config.FrontendPath == "" {
		templateDta, err := Asset(templateFile)
		panicIfErr(err)
		t = template.New(templateFile)
		_, err = t.Parse(string(templateDta))
	} else {
		t, err = template.ParseFiles(server.config.FrontendPath + string(os.PathSeparator) + templateFile)
	}
	panicIfErr(err)

	err = t.Execute(responseWriter, templateInterface)
	panicIfErr(err)

}

func (server *TTYServer) Run() (err error) {
	err = server.httpServer.ListenAndServe()
	log.Debug("Server finished")
	return
}

func (server *TTYServer) Write(buff []byte) (written int, err error) {
	return server.session.Write(buff)
}

func (server *TTYServer) WindowSize(cols, rows int) (err error) {
	return server.session.WindowSize(cols, rows)
}

func (server *TTYServer) Stop() error {
	log.Debug("Stopping the server")
	if server.muxTunnelSession != nil {
		server.muxTunnelSession.Close()
	}
	return server.httpServer.Close()
}