Repository navigation
Expand file tree
/
Copy pathserver.go
More file actions
301 lines (264 loc) · 7.77 KB
/
Copy pathserver.go
File metadata and controls
301 lines (264 loc) · 7.77 KB
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
299
300
301
// Copyright 2018 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libhttpserver
import (
"context"
"crypto/rand"
"encoding/base64"
"io"
"net/http"
"path"
"strings"
"sync"
"time"
lru "github.com/hashicorp/golang-lru"
"github.com/keybase/client/go/kbfs/data"
"github.com/keybase/client/go/kbfs/env"
"github.com/keybase/client/go/kbfs/libfs"
"github.com/keybase/client/go/kbfs/libkbfs"
"github.com/keybase/client/go/kbfs/libmime"
"github.com/keybase/client/go/kbfs/tlf"
"github.com/keybase/client/go/kbhttp"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/logger"
"github.com/keybase/client/go/protocol/keybase1"
)
const fsCacheSize = 64
// Server is a local HTTP server for serving KBFS content over HTTP.
type Server struct {
config libkbfs.Config
logger logger.Logger
vlog *libkb.VDebugLog
appStateUpdater env.AppStateUpdater
cancel func()
tokenLock sync.RWMutex
token string
tokenExpireTime time.Time
fs *lru.Cache
serverLock sync.RWMutex
server *kbhttp.Srv
}
const tokenByteSize = 32
const tokenValidTime = 10 * time.Minute
// CurrentToken returns the currently valid token that a HTTP client can use to
// load content from the server.
func (s *Server) CurrentToken() (token string, err error) {
s.tokenLock.RLock()
if s.config.Clock().Now().Before(s.tokenExpireTime) {
defer s.tokenLock.RUnlock()
return s.token, nil
}
s.tokenLock.RUnlock()
buf := make([]byte, tokenByteSize)
if _, err = rand.Read(buf); err != nil {
return "", err
}
token = base64.URLEncoding.EncodeToString(buf)
s.tokenLock.Lock()
defer s.tokenLock.Unlock()
if s.config.Clock().Now().Before(s.tokenExpireTime) {
return s.token, nil
}
s.token = token
s.tokenExpireTime = s.config.Clock().Now().Add(tokenValidTime)
return token, nil
}
func (s *Server) handleInvalidToken(w http.ResponseWriter) {
w.WriteHeader(http.StatusForbidden)
_, _ = io.WriteString(w, `
<html>
<head>
<title>KBFS HTTP Token Invalid</title>
</head>
<body>
token invalid
</body>
</html>
`)
}
func (s *Server) handleBadRequest(w http.ResponseWriter) {
w.WriteHeader(http.StatusBadRequest)
}
func (s *Server) handleInternalServerError(w http.ResponseWriter) {
w.WriteHeader(http.StatusInternalServerError)
}
type obsoleteTrackingFS struct {
fs *libfs.FS
ch <-chan struct{}
}
func (e obsoleteTrackingFS) isObsolete() bool {
select {
case <-e.ch:
return true
default:
return false
}
}
func (s *Server) getHTTPFileSystem(ctx context.Context, requestPath string) (
toStrip string, fs http.FileSystem, err error) {
fields := strings.Split(requestPath, "/")
if len(fields) < 2 {
return "", libfs.NewRootFS(s.config).ToHTTPFileSystem(ctx), nil
}
tlfType, err := tlf.ParseTlfTypeFromPath(fields[0])
if err != nil {
return "", nil, err
}
toStrip = path.Join(fields[0], fields[1])
if fsCached, ok := s.fs.Get(toStrip); ok {
if fsCachedTyped, ok := fsCached.(obsoleteTrackingFS); ok {
if !fsCachedTyped.isObsolete() {
return toStrip, fsCachedTyped.fs.ToHTTPFileSystem(ctx), nil
}
}
}
tlfHandle, err := libkbfs.GetHandleFromFolderNameAndType(ctx,
s.config.KBPKI(), s.config.MDOps(), s.config, fields[1], tlfType)
if err != nil {
return "", nil, err
}
tlfFS, err := libfs.NewFS(ctx,
s.config, tlfHandle, data.MasterBranch, "", "",
keybase1.MDPriorityNormal)
if err != nil {
return "", nil, err
}
fsLifeCh, err := tlfFS.SubscribeToObsolete()
if err != nil {
return "", nil, err
}
s.fs.Add(toStrip, obsoleteTrackingFS{fs: tlfFS, ch: fsLifeCh})
return toStrip, tlfFS.ToHTTPFileSystem(ctx), nil
}
// serve accepts "/<fs path>?token=<token>"
// For example:
//
// /team/keybase/file.txt?token=1234567890abcdef1234567890abcdef
func (s *Server) serve(w http.ResponseWriter, req *http.Request) {
s.vlog.Log(libkb.VLog1, "Incoming request from %q: %s", req.UserAgent(), req.URL)
addr, err := s.server.Addr()
if err != nil {
s.logger.Error("serve: failed to get HTTP server address: %s", err)
s.handleInternalServerError(w)
return
}
if req.Host != addr {
s.logger.Warning("Host %s didn't match addr %s, failing request to protect against DNS rebinding", req.Host, addr)
s.handleBadRequest(w)
return
}
token := req.URL.Query().Get("token")
currentToken, err := s.CurrentToken()
if err != nil {
s.logger.Error("serve: failed to get current token: %s", err)
s.handleInternalServerError(w)
return
}
if len(token) == 0 || token != currentToken {
s.vlog.Log(libkb.VLog1, "Invalid token %q", token)
s.handleInvalidToken(w)
return
}
toStrip, fs, err := s.getHTTPFileSystem(req.Context(), req.URL.Path)
if err != nil {
s.logger.Warning("Bad request; error=%v", err)
s.handleBadRequest(w)
return
}
viewTypeInvariance := req.URL.Query().Get("viewTypeInvariance")
if len(viewTypeInvariance) == 0 {
s.logger.Warning("Bad request; missing viewTypeInvariance")
s.handleBadRequest(w)
return
}
wrappedW := newContentTypeOverridingResponseWriter(w,
viewTypeInvariance)
http.StripPrefix(toStrip, http.FileServer(fs)).ServeHTTP(wrappedW, req)
}
const portStart = 16723
const portEnd = 60000
const requestPathRoot = "/files/"
func (s *Server) restart() (err error) {
s.serverLock.Lock()
defer s.serverLock.Unlock()
if s.server != nil {
s.server.Stop()
err = s.server.Start()
}
if s.server == nil ||
// If pinned port is in use, just pick a new one like we never had a
// server before.
err == kbhttp.ErrPinnedPortInUse {
s.server = kbhttp.NewSrv(s.logger,
kbhttp.NewRandomPortRangeListenerSource(portStart, portEnd))
err = s.server.Start()
}
if err != nil {
return err
}
// Have to start this first to populate the ServeMux object.
s.server.Handle(requestPathRoot,
http.StripPrefix(requestPathRoot, http.HandlerFunc(s.serve)))
return nil
}
func (s *Server) monitorAppState(ctx context.Context) {
state := keybase1.MobileAppState_FOREGROUND
for {
select {
case <-ctx.Done():
return
case state = <-s.appStateUpdater.NextAppStateUpdate(&state):
// Due to the way NextUpdate is designed, it's possible we miss an
// update if processing the last update takes too long. So it's
// possible to get consecutive FOREGROUND updates even if there are
// other states in-between. Since libkb/appstate.go already
// deduplicates, it'll never actually send consecutive identical
// states to us. In addition, apart from FOREGROUND/BACKGROUND,
// there are other possible states too, and potentially more in the
// future. So, we just restart the server under FOREGROUND instead
// of trying to listen on all state updates.
if state != keybase1.MobileAppState_FOREGROUND {
continue
}
if err := s.restart(); err != nil {
s.logger.Error("(Re)starting server failed: %v", err)
}
}
}
}
// New creates and starts a new server.
func New(appStateUpdater env.AppStateUpdater, config libkbfs.Config) (
s *Server, err error) {
logger := config.MakeLogger("HTTP")
s = &Server{
appStateUpdater: appStateUpdater,
config: config,
logger: logger,
vlog: config.MakeVLogger(logger),
}
if s.fs, err = lru.New(fsCacheSize); err != nil {
return nil, err
}
if err = s.restart(); err != nil {
return nil, err
}
ctx, cancel := context.WithCancel(context.Background())
go s.monitorAppState(ctx)
s.cancel = cancel
libmime.Patch(additionalMimeTypes)
return s, nil
}
// Address returns the address that the server is listening on.
func (s *Server) Address() (string, error) {
s.serverLock.RLock()
defer s.serverLock.RUnlock()
return s.server.Addr()
}
// Shutdown shuts down the server.
func (s *Server) Shutdown() {
s.serverLock.Lock()
defer s.serverLock.Unlock()
s.server.Stop()
s.cancel()
}