web/unhar.go

54 lines
1.0 KiB
Go
Raw Normal View History

2024-02-07 22:33:16 +00:00
package main
import (
"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 {
Text string `json:"text"`
} `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-07 22:33:16 +00:00
if err := os.MkdirAll(filepath.Dir(outfile), 0777); err != nil {
return err
}
if err := os.WriteFile(outfile, []byte(entry.Response.Content.Text), 0666); err != nil {
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)
}
}