forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic.go
182 lines (152 loc) · 4.14 KB
/
static.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
181
182
// Implement transparent asset decompression and caching.
// Most modern browsers can negotiate compression transfer with no
// issues. In this case we just deliver the compressed assets directly
// saving on both bandwidth and CPU cycles.
// However older browsers may not support brotli compression, so we
// need to decompress the asset for them and cache it for a short
// time.
package api
import (
"bytes"
"io"
"io/fs"
"net/http"
"os"
"strings"
"time"
"github.com/Velocidex/ttlcache/v2"
"github.com/andybalholm/brotli"
errors "github.com/go-errors/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
context "golang.org/x/net/context"
"www.velocidex.com/golang/velociraptor/services"
"www.velocidex.com/golang/velociraptor/utils"
)
var (
brotliDecompressionCounter = promauto.NewCounter(prometheus.CounterOpts{
Name: "gui_asset_decompression_count",
Help: "Number of times the GUI was forced to decompressed assets for browsers that do not support brotli compression.",
})
)
type memFileInfo struct {
name string
size int64
mode os.FileMode
modTime time.Time
}
func (f *memFileInfo) Name() string { return f.name }
func (f *memFileInfo) Size() int64 { return f.size }
func (f *memFileInfo) Mode() os.FileMode { return f.mode }
func (f *memFileInfo) ModTime() time.Time { return f.modTime }
func (f *memFileInfo) IsDir() bool { return f.mode.IsDir() }
func (f *memFileInfo) Sys() interface{} { return nil }
type brotliBuffer struct {
*bytes.Reader
name string
size int64
}
func (self *brotliBuffer) Close() error {
return nil
}
func (self *brotliBuffer) Readdir(count int) ([]fs.FileInfo, error) {
return nil, errors.New("Not Implemented")
}
func (self *brotliBuffer) Stat() (fs.FileInfo, error) {
return &memFileInfo{
name: self.name,
size: self.size,
mode: 0644,
}, nil
}
type CachedFilesystem struct {
http.FileSystem
lru *ttlcache.Cache
}
func (self *CachedFilesystem) getCachedBytes(name string) ([]byte, error) {
cached_any, err := self.lru.Get(name)
if err != nil {
return nil, err
}
cached, ok := cached_any.([]byte)
if !ok {
return nil, errors.New("Invalid cached item")
}
return cached, nil
}
func (self *CachedFilesystem) Open(name string) (http.File, error) {
// We do not support gz files at all - it is either brotli or
// uncompressed.
if strings.HasSuffix(name, ".gz") {
return nil, services.OrgNotFoundError
}
fd, err := self.FileSystem.Open(name)
if err != nil {
// If there is not brotli file, it is just not there.
if strings.HasSuffix(name, ".br") {
return nil, services.OrgNotFoundError
}
// Check if a compressed .br file exists
fd, err := self.FileSystem.Open(name + ".br")
if err == nil {
cached, err := self.getCachedBytes(name)
if err == nil {
return &brotliBuffer{
Reader: bytes.NewReader(cached),
name: name,
size: int64(len(cached)),
}, nil
}
out_fd := &bytes.Buffer{}
n, err := io.Copy(out_fd, brotli.NewReader(fd))
if err != nil {
return nil, err
}
brotliDecompressionCounter.Inc()
// Cache for next time.
self.lru.Set(name, out_fd.Bytes())
return &brotliBuffer{
Reader: bytes.NewReader(out_fd.Bytes()),
name: name,
size: n,
}, nil
}
}
return fd, err
}
func (self *CachedFilesystem) Exists(path string) bool {
fd, err := self.FileSystem.Open(path)
if err != nil {
return false
}
fd.Close()
return true
}
func NewCachedFilesystem(
ctx context.Context, fs http.FileSystem) *CachedFilesystem {
result := &CachedFilesystem{
FileSystem: fs,
lru: ttlcache.NewCache(),
}
result.lru.SetTTL(10 * time.Minute)
result.lru.SkipTTLExtensionOnHit(true)
go func() {
<-ctx.Done()
result.lru.Close()
}()
return result
}
func serveStaticAsset(fs http.FileSystem) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
fd, err := fs.Open(path)
if err != nil {
returnError(w, 404, "Not Found")
return
}
defer fd.Close()
w.Header().Set("Content-Type", "binary/octet-stream")
w.WriteHeader(200)
utils.Copy(r.Context(), w, fd)
})
}