mirror of
https://github.com/skidoodle/safebin.git
synced 2026-04-28 03:07:41 +02:00
fix: relax chunk limits, support proxies, optimize reads
Signed-off-by: skidoodle <contact@albert.lol>
This commit is contained in:
@@ -22,6 +22,7 @@ const (
|
|||||||
ShutdownTimeout = 10 * time.Second
|
ShutdownTimeout = 10 * time.Second
|
||||||
|
|
||||||
UploadChunkSize = 8 << 20
|
UploadChunkSize = 8 << 20
|
||||||
|
MinChunkSize = 1 << 20
|
||||||
MaxRequestOverhead = 10 << 20
|
MaxRequestOverhead = 10 << 20
|
||||||
PermUserRWX = 0o700
|
PermUserRWX = 0o700
|
||||||
MegaByte = 1 << 20
|
MegaByte = 1 << 20
|
||||||
|
|||||||
@@ -72,10 +72,13 @@ func (app *App) RespondWithLink(writer http.ResponseWriter, request *http.Reques
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
scheme := "https"
|
scheme := request.Header.Get("X-Forwarded-Proto")
|
||||||
|
if scheme == "" {
|
||||||
|
scheme = "https"
|
||||||
if request.TLS == nil {
|
if request.TLS == nil {
|
||||||
scheme = "http"
|
scheme = "http"
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := fmt.Fprintf(writer, "%s://%s\n", scheme, link); err != nil {
|
if _, err := fmt.Fprintf(writer, "%s://%s\n", scheme, link); err != nil {
|
||||||
app.Logger.Error("Failed to write response", "err", err)
|
app.Logger.Error("Failed to write response", "err", err)
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ func (app *App) HandleChunk(writer http.ResponseWriter, request *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
maxChunks := int((app.Conf.MaxMB*MegaByte)/UploadChunkSize) + ChunkSafetyMargin
|
maxChunks := int((app.Conf.MaxMB*MegaByte)/MinChunkSize) + ChunkSafetyMargin
|
||||||
|
|
||||||
if !reUploadID.MatchString(uid) || idx > maxChunks || idx < 0 {
|
if !reUploadID.MatchString(uid) || idx > maxChunks || idx < 0 {
|
||||||
app.SendError(writer, request, http.StatusBadRequest)
|
app.SendError(writer, request, http.StatusBadRequest)
|
||||||
@@ -148,7 +148,7 @@ func (app *App) HandleFinish(writer http.ResponseWriter, request *http.Request)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
maxChunks := int((app.Conf.MaxMB*MegaByte)/UploadChunkSize) + ChunkSafetyMargin
|
maxChunks := int((app.Conf.MaxMB*MegaByte)/MinChunkSize) + ChunkSafetyMargin
|
||||||
|
|
||||||
if !reUploadID.MatchString(uid) || total > maxChunks || total <= 0 {
|
if !reUploadID.MatchString(uid) || total > maxChunks || total <= 0 {
|
||||||
app.SendError(writer, request, http.StatusBadRequest)
|
app.SendError(writer, request, http.StatusBadRequest)
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ type Decryptor struct {
|
|||||||
aead cipher.AEAD
|
aead cipher.AEAD
|
||||||
size int64
|
size int64
|
||||||
offset int64
|
offset int64
|
||||||
|
phyOffset int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDecryptor(readSeeker io.ReadSeeker, aead cipher.AEAD, encryptedSize int64) *Decryptor {
|
func NewDecryptor(readSeeker io.ReadSeeker, aead cipher.AEAD, encryptedSize int64) *Decryptor {
|
||||||
@@ -35,6 +36,7 @@ func NewDecryptor(readSeeker io.ReadSeeker, aead cipher.AEAD, encryptedSize int6
|
|||||||
aead: aead,
|
aead: aead,
|
||||||
size: plainSize,
|
size: plainSize,
|
||||||
offset: 0,
|
offset: 0,
|
||||||
|
phyOffset: -1,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,14 +51,22 @@ func (d *Decryptor) Read(buf []byte) (int, error) {
|
|||||||
overhead := int64(d.aead.Overhead())
|
overhead := int64(d.aead.Overhead())
|
||||||
actualChunkSize := int64(GCMChunkSize) + overhead
|
actualChunkSize := int64(GCMChunkSize) + overhead
|
||||||
|
|
||||||
_, err := d.readSeeker.Seek(chunkIdx*actualChunkSize, io.SeekStart)
|
targetOffset := chunkIdx * actualChunkSize
|
||||||
if err != nil {
|
|
||||||
|
if d.phyOffset != targetOffset {
|
||||||
|
if _, err := d.readSeeker.Seek(targetOffset, io.SeekStart); err != nil {
|
||||||
return 0, fmt.Errorf("failed to seek: %w", err)
|
return 0, fmt.Errorf("failed to seek: %w", err)
|
||||||
}
|
}
|
||||||
|
d.phyOffset = targetOffset
|
||||||
|
}
|
||||||
|
|
||||||
encrypted := make([]byte, actualChunkSize)
|
encrypted := make([]byte, actualChunkSize)
|
||||||
|
|
||||||
bytesRead, err := io.ReadFull(d.readSeeker, encrypted)
|
bytesRead, err := io.ReadFull(d.readSeeker, encrypted)
|
||||||
|
if bytesRead > 0 {
|
||||||
|
d.phyOffset += int64(bytesRead)
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil && !errors.Is(err, io.ErrUnexpectedEOF) {
|
if err != nil && !errors.Is(err, io.ErrUnexpectedEOF) {
|
||||||
return 0, fmt.Errorf("failed to read encrypted data: %w", err)
|
return 0, fmt.Errorf("failed to read encrypted data: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user