web/unhar.go

69 lines
1.6 KiB
Go
Raw Normal View History

2024-02-07 22:33:16 +00:00
package main
import (
2024-02-07 23:12:27 +00:00
"encoding/base64"
2024-02-07 22:33:16 +00:00
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
2024-02-07 23:12:20 +00:00
"strings"
2024-02-07 22:33:16 +00:00
)
func extractHar(src io.Reader, outdir string) error {
bs, err := io.ReadAll(src)
if err != nil {
return err
}
var har struct {
Log struct {
Entries []struct {
Request struct {
URL string `json:"url"`
}
Response struct {
Content struct {
2024-02-09 01:03:03 +00:00
MIMEType string `json:"mimeType"`
2024-02-07 23:12:27 +00:00
Encoding string `json:"encoding"`
Text string `json:"text"`
2024-02-07 22:33:16 +00:00
} `json:"content"`
} `json:"response"`
} `json:"entries"`
} `json:"log"`
}
if err := json.Unmarshal(bs, &har); err != nil {
return err
}
for _, entry := range har.Log.Entries {
2024-02-07 23:12:20 +00:00
outfile := filepath.Join(outdir, strings.ReplaceAll(strings.TrimPrefix(entry.Request.URL, "https://"), "%20", "-"))
2024-02-09 01:03:03 +00:00
if entry.Response.Content.MIMEType == "text/html" && !strings.HasSuffix(outfile, ".html") {
outfile = filepath.Join(outfile, "index.html")
}
2024-02-07 22:33:16 +00:00
if err := os.MkdirAll(filepath.Dir(outfile), 0777); err != nil {
return err
}
2024-02-07 23:12:27 +00:00
var content []byte
switch entry.Response.Content.Encoding {
case "":
content = []byte(entry.Response.Content.Text)
case "base64":
content, err = base64.StdEncoding.DecodeString(entry.Response.Content.Text)
default:
return fmt.Errorf("unknown encoding for %q: %q", entry.Request.URL, entry.Response.Content.Encoding)
}
if err := os.WriteFile(outfile, content, 0666); err != nil {
2024-02-07 22:33:16 +00:00
return err
}
}
return nil
}
func main() {
if err := extractHar(os.Stdin, os.Args[1]); err != nil {
fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
os.Exit(1)
}
}