Compare commits

..

No commits in common. "78305a063aee705461977d889b02f6a4ddaee4db" and "f08b63fa61a7fe823fa6494ecc4b5bbb6bb2f8a4" have entirely different histories.

2 changed files with 16 additions and 38 deletions

6
go.mod
View file

@ -2,13 +2,11 @@ module git.dc09.ru/mitm-archive/server
go 1.22.3
require (
github.com/mattn/go-sqlite3 v1.14.22
github.com/valyala/fasthttp v1.55.0
)
require github.com/valyala/fasthttp v1.55.0
require (
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
)

View file

@ -4,9 +4,7 @@ import (
"bufio"
"bytes"
"database/sql"
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
@ -15,15 +13,7 @@ import (
"github.com/valyala/fasthttp"
)
const pathsep = string(os.PathSeparator)
var (
bind = flag.String("bind", "127.0.0.1:4001", "Where to listen for connections")
proxy = flag.Bool("proxy", true, "Whether server is behind a reverse proxy")
verbose = flag.Bool("verbose", false, "Whether to log all URLs")
dbpath = flag.String("dbpath", "archive.db", "Path to SQLite3 DB generated by mitmproxy addon")
storage = flag.String("storage", "storage", "Path to archived responses storage")
)
const behindProxy = true
var removeHeaders = map[string]bool{
"date": true,
@ -35,9 +25,7 @@ var conn *sql.DB
var stmt *sql.Stmt
func main() {
flag.Parse()
db, err := sql.Open("sqlite3", *dbpath)
db, err := sql.Open("sqlite3", "archive.db")
if err != nil {
return
}
@ -51,10 +39,7 @@ func main() {
stmt = sel_stmt
defer sel_stmt.Close()
err1 := fasthttp.ListenAndServe(*bind, handler)
if err1 != nil {
log.Fatalln(err1)
}
fasthttp.ListenAndServe("127.0.0.1:4001", handler)
}
func handler(ctx *fasthttp.RequestCtx) {
@ -62,7 +47,7 @@ func handler(ctx *fasthttp.RequestCtx) {
uri := ctx.URI()
var scheme []byte
if *proxy {
if behindProxy {
scheme = ctx.Request.Header.Peek("X-Forwarded-Proto")
} else {
scheme = uri.Scheme()
@ -73,22 +58,17 @@ func handler(ctx *fasthttp.RequestCtx) {
bytes.Equal(scheme, []byte("https")),
)
urlToSearch := fmt.Sprintf(
"%s://%s:%s%s",
scheme,
host,
port,
ctx.RequestURI(),
)
row := stmt.QueryRow(
string(ctx.Method()),
urlToSearch,
fmt.Sprintf(
"%s://%s:%s%s",
scheme,
host,
port,
ctx.RequestURI(),
),
)
if *verbose {
log.Println(urlToSearch)
}
var id int
var code int
@ -105,9 +85,9 @@ func handler(ctx *fasthttp.RequestCtx) {
ctx.Response.SetStatusCode(code)
// -- find in FS and read headers+body
path := *storage + pathsep + strconv.Itoa(id)
path := "storage/" + strconv.Itoa(id)
fh, err := os.Open(path + pathsep + "headers")
fh, err := os.Open(path + "/headers")
if err != nil {
sendError(ctx, "Unable to read headers: ", err)
return
@ -123,7 +103,7 @@ func handler(ctx *fasthttp.RequestCtx) {
}
fh.Close()
fb, err := os.Open(path + pathsep + "body")
fb, err := os.Open(path + "/body")
if err != nil {
sendError(ctx, "Unable to read body: ", err)
return