Add a reorderpcap tool

This commit is contained in:
2026-10-05 20:35:47 +02:00
parent c11c893845
commit e70549337e
5 changed files with 46 additions and 28 deletions
+6 -22
View File
@@ -9,7 +9,6 @@ import (
"log"
"math"
"net/http"
"path"
"strconv"
"strings"
"time"
@@ -50,7 +49,7 @@ func (s *Server) splitPage(f splitForm) splitPage {
return splitPage{
Form: f,
Units: units,
MaxUpload: fmt.Sprintf("%d MB", s.cfg.MaxUploadBytes/1e6),
MaxUpload: s.maxUpload(),
MaxParts: maxSplitParts,
}
}
@@ -67,19 +66,12 @@ func (s *Server) handleSplit(w http.ResponseWriter, r *http.Request) {
s.render(w, status, "split.html", "/split", page)
}
// Allow some slack over the file size limit for the multipart framing and form fields.
r.Body = http.MaxBytesReader(w, r.Body, s.cfg.MaxUploadBytes+64<<10)
// Uploads bigger than 1 MB are spooled to temporary files rather than kept in memory.
if err := r.ParseMultipartForm(1 << 20); err != nil {
var tooBig *http.MaxBytesError
if errors.As(err, &tooBig) {
fail(http.StatusRequestEntityTooLarge, "file too large; the maximum upload is "+page.MaxUpload)
} else {
fail(http.StatusBadRequest, "bad upload: "+err.Error())
}
file, hdr, status, msg := s.parseUpload(w, r)
if status != 0 {
fail(status, msg)
return
}
defer r.MultipartForm.RemoveAll()
defer file.Close()
page.Form = splitForm{
Mode: r.FormValue("mode"),
@@ -92,13 +84,6 @@ func (s *Server) handleSplit(w http.ResponseWriter, r *http.Request) {
fail(http.StatusBadRequest, err.Error())
return
}
file, hdr, err := r.FormFile("file")
if err != nil {
fail(http.StatusBadRequest, "choose a pcap file to split")
return
}
defer file.Close()
// First pass: read the whole file and count the parts, so problems are
// reported on the page before any of the zip is sent.
st, err := pcap.Split(file, limit, func(part int) (io.Writer, error) {
@@ -116,8 +101,7 @@ func (s *Server) handleSplit(w http.ResponseWriter, r *http.Request) {
return
}
name := safeName(hdr.Filename, "capture.pcap")
base := strings.TrimSuffix(name, path.Ext(name))
base := uploadStem(hdr)
digits := max(3, len(strconv.Itoa(st.Parts)))
w.Header().Set("Content-Type", "application/zip")