-
Notifications
You must be signed in to change notification settings - Fork 22
/
hijacker.go
256 lines (237 loc) · 6.61 KB
/
hijacker.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
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
/**
* OpenBmclAPI (Golang Edition)
* Copyright (C) 2023 Kevin Z <[email protected]>
* All rights reserved
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"context"
"encoding/json"
"io"
"net"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
"sync"
"time"
"github.com/LiterMC/go-openbmclapi/database"
"github.com/LiterMC/go-openbmclapi/utils"
)
func getDialerWithDNS(dnsaddr string) *net.Dialer {
resolver := &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
return net.Dial(network, dnsaddr)
},
}
return &net.Dialer{
Resolver: resolver,
}
}
type downloadHandlerFn = func(rw http.ResponseWriter, req *http.Request, hash string)
type HjProxy struct {
client *http.Client
fileMap database.DB
downloadHandler downloadHandlerFn
cacheMux sync.RWMutex
cache map[string]*cacheStat
saveTimer *time.Timer
}
func NewHjProxy(client *http.Client, fileMap database.DB, downloadHandler downloadHandlerFn) (h *HjProxy) {
cli := new(http.Client)
*cli = *client
cli.CheckRedirect = func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
}
h = &HjProxy{
client: cli,
fileMap: fileMap,
downloadHandler: downloadHandler,
}
h.loadCache()
return
}
func hjResponseWithCache(rw http.ResponseWriter, req *http.Request, c *cacheStat, force bool) (ok bool) {
if c == nil {
return false
}
cacheFileName := filepath.Join(config.Hijack.LocalCachePath, filepath.FromSlash(req.URL.Path))
age := c.ExpiresAt - time.Now().Unix()
if !force && age <= 0 {
return false
}
stat, err := os.Stat(cacheFileName)
if err != nil {
return false
}
if stat.Size() != c.Size {
return false
}
fd, err := os.Open(cacheFileName)
if err != nil {
return false
}
defer fd.Close()
if age > 0 {
rw.Header().Set("Cache-Control", "public, max-age="+strconv.FormatInt(age, 10))
}
http.ServeContent(rw, req, path.Base(req.URL.Path), stat.ModTime(), fd)
return true
}
const hijackingHost = "bmclapi2.bangbang93.com"
func (h *HjProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if !config.Hijack.Enable {
http.Error(rw, "Hijack is disabled in the config", http.StatusServiceUnavailable)
return
}
if config.Hijack.RequireAuth {
needAuth := true
user, passwd, ok := req.BasicAuth()
if ok {
for _, u := range config.Hijack.AuthUsers {
if u.Username == user && utils.ComparePasswd(u.Password, passwd) {
needAuth = false
return
}
}
}
if needAuth {
rw.Header().Set("WWW-Authenticate", `Basic realm="Login to access hijacked bmclapi", charset="UTF-8"`)
http.Error(rw, "403 Unauthorized", http.StatusUnauthorized)
return
}
}
if req.Method == http.MethodGet || req.Method == http.MethodHead {
if rec, err := h.fileMap.GetFileRecord(req.URL.Path); err == nil {
ctx := req.Context()
ctx = context.WithValue(ctx, "go-openbmclapi.handler.no.record.for.keepalive", true)
h.downloadHandler(rw, req.WithContext(ctx), rec.Hash)
return
}
}
nowUnix := time.Now().Unix()
cacheFileName := filepath.Join(config.Hijack.LocalCachePath, filepath.FromSlash(req.URL.Path))
cached := h.getCache(req.URL.Path)
if hjResponseWithCache(rw, req, cached, false) {
return
}
u := *req.URL
u.Scheme = "https"
u.Host = hijackingHost
req2, err := http.NewRequestWithContext(req.Context(), req.Method, u.String(), req.Body)
if err != nil {
http.Error(rw, "remote: "+err.Error(), http.StatusBadGateway)
return
}
for k, v := range req.Header {
req2.Header[k] = v
}
res, err := h.client.Do(req2)
if err != nil {
if hjResponseWithCache(rw, req, cached, true) {
return
}
http.Error(rw, "remote: "+err.Error(), http.StatusBadGateway)
return
}
defer res.Body.Close()
for k, v := range res.Header {
rw.Header()[k] = v
}
if res.StatusCode/100 == 3 {
// fix location header
location, err := url.Parse(res.Header.Get("Location"))
if err == nil && location.Host == "" && len(location.Path) >= 1 && location.Path[0] == '/' {
location.Path = "/bmclapi" + location.Path
}
rw.Header().Set("Location", location.String())
}
rw.WriteHeader(res.StatusCode)
var body io.Reader = res.Body
if config.Hijack.EnableLocalCache && res.StatusCode == http.StatusOK {
if exp, ok := utils.ParseCacheControl(res.Header.Get("Cache-Control")); ok {
if exp > 0 {
os.MkdirAll(filepath.Dir(cacheFileName), 0755)
fd, err := os.OpenFile(cacheFileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err == nil {
defer fd.Close()
var n int64
if n, err = io.Copy(fd, body); err == nil {
if _, err = fd.Seek(0, io.SeekStart); err == nil {
h.setCache(req.URL.Path, n, nowUnix+exp)
body = fd
}
}
if err != nil {
fd.Close()
os.Remove(cacheFileName)
}
}
}
}
}
io.Copy(rw, body)
}
type cacheStat struct {
Size int64 `json:"s"`
ExpiresAt int64 `json:"e"`
ValidateAt int64 `json:"v"`
}
func (h *HjProxy) loadCache() (err error) {
h.cache = make(map[string]*cacheStat)
fd, err := os.Open(filepath.Join(config.Hijack.LocalCachePath, "__cache.json"))
if err != nil {
return
}
defer fd.Close()
return json.NewDecoder(fd).Decode(&h.cache)
}
func (h *HjProxy) saveCache() (err error) {
fd, err := os.Create(filepath.Join(config.Hijack.LocalCachePath, "__cache.json"))
if err != nil {
return
}
defer fd.Close()
return json.NewEncoder(fd).Encode(h.cache)
}
func (h *HjProxy) getCache(path string) *cacheStat {
h.cacheMux.RLock()
defer h.cacheMux.RUnlock()
stat, _ := h.cache[path]
return stat
}
func (h *HjProxy) setCache(path string, size int64, exp int64) {
h.cacheMux.Lock()
defer h.cacheMux.Unlock()
h.cache[path] = &cacheStat{
Size: size,
ExpiresAt: exp,
}
if h.saveTimer == nil {
h.saveTimer = time.AfterFunc(time.Second, func() {
h.cacheMux.RLock()
defer h.cacheMux.RUnlock()
// it's safe to rlock here because the only other way to set it under a write lock
h.saveTimer = nil
h.saveCache()
})
} else {
h.saveTimer.Reset(time.Second)
}
}