Skip to content

Commit

Permalink
fix a lot of things
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Dec 13, 2023
1 parent 44a50ab commit b4eac34
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
3 changes: 1 addition & 2 deletions src/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,10 @@ func NewCluster(
byoc: byoc,

cacheDir: cacheDir,
tmpDir: filepath.Join(cacheDir, ".tmp"),
tmpDir: filepath.Join(cacheDir, ".tmp"),
maxConn: 128,

client: &http.Client{
Timeout: time.Second * 120,
Transport: transport,
},
Server: &http.Server{
Expand Down
42 changes: 21 additions & 21 deletions src/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@ import (
"os"
"strconv"
"strings"
"time"
)

const maxZeroBufMB = 200
var zeroBuffer [1024 * 1024]byte

var zeroBuffer [maxZeroBufMB * 1024 * 1024]byte
type countReader struct {
io.ReadSeeker
n int64
}

func (r *countReader) Read(buf []byte) (n int, err error) {
n, err = r.ReadSeeker.Read(buf)
r.n += (int64)(n)
return
}

func (cr *Cluster) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
method := req.Method
Expand All @@ -31,9 +41,7 @@ func (cr *Cluster) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
return
}
}
if name := req.Form.Get("name"); name != "" {
rw.Header().Set("Content-Disposition", "attachment; filename="+name)
}
name := req.Form.Get("name")
rw.Header().Set("Cache-Control", "max-age=2592000") // 30 days
fd, err := os.Open(path)
if err != nil {
Expand All @@ -43,21 +51,10 @@ func (cr *Cluster) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
defer fd.Close()
rw.Header().Set("X-Bmclapi-Hash", hash)
rw.WriteHeader(http.StatusOK)
var buf []byte
{
buf0 := bufPool.Get().(*[]byte)
defer bufPool.Put(buf0)
buf = *buf0
}
n, err := io.CopyBuffer(rw, fd, buf)
counter := &countReader{ReadSeeker: fd}
http.ServeContent(rw, req, name, time.Time{}, counter)
cr.hits.Add(1)
cr.hbytes.Add(n)
if err != nil {
if !config.IgnoreServeError {
logError("Error when serving download:", err)
}
return
}
cr.hbytes.Add(counter.n)
return
}
case strings.HasPrefix(rawpath, "/measure/"):
Expand All @@ -67,12 +64,15 @@ func (cr *Cluster) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
return
}
n, e := strconv.Atoi(rawpath[len("/measure/"):])
if e != nil || n < 0 || n > maxZeroBufMB {
if e != nil || n < 0 || n > 200 {
rw.WriteHeader(http.StatusBadRequest)
return
}
rw.Header().Set("Content-Length", strconv.Itoa(n * len(zeroBuffer)))
rw.WriteHeader(http.StatusOK)
rw.Write(zeroBuffer[:n*1024*1024])
for i := 0; i < n; i++ {
rw.Write(zeroBuffer[:])
}
return
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/hijacker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http"
"os"
"path/filepath"
"time"
)

func getDialerWithDNS(dnsaddr string) *net.Dialer {
Expand All @@ -33,7 +32,6 @@ func NewHjProxy(dialer *net.Dialer, path string) (h *HjProxy) {
dialer: dialer,
path: path,
client: &http.Client{
Timeout: time.Second * 15,
Transport: &http.Transport{
DialContext: dialer.DialContext,
},
Expand Down

0 comments on commit b4eac34

Please sign in to comment.