Compare commits
3 commits
f08b63fa61
...
78305a063a
Author | SHA1 | Date | |
---|---|---|---|
78305a063a | |||
94897e7711 | |||
b1d2ba896c |
2 changed files with 38 additions and 16 deletions
6
go.mod
6
go.mod
|
@ -2,11 +2,13 @@ module git.dc09.ru/mitm-archive/server
|
||||||
|
|
||||||
go 1.22.3
|
go 1.22.3
|
||||||
|
|
||||||
require github.com/valyala/fasthttp v1.55.0
|
require (
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.22
|
||||||
|
github.com/valyala/fasthttp v1.55.0
|
||||||
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/andybalholm/brotli v1.1.0 // indirect
|
github.com/andybalholm/brotli v1.1.0 // indirect
|
||||||
github.com/klauspost/compress v1.17.9 // 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
|
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||||
)
|
)
|
||||||
|
|
48
server.go
48
server.go
|
@ -4,7 +4,9 @@ import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -13,7 +15,15 @@ import (
|
||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
const behindProxy = true
|
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")
|
||||||
|
)
|
||||||
|
|
||||||
var removeHeaders = map[string]bool{
|
var removeHeaders = map[string]bool{
|
||||||
"date": true,
|
"date": true,
|
||||||
|
@ -25,7 +35,9 @@ var conn *sql.DB
|
||||||
var stmt *sql.Stmt
|
var stmt *sql.Stmt
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
db, err := sql.Open("sqlite3", "archive.db")
|
flag.Parse()
|
||||||
|
|
||||||
|
db, err := sql.Open("sqlite3", *dbpath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -39,7 +51,10 @@ func main() {
|
||||||
stmt = sel_stmt
|
stmt = sel_stmt
|
||||||
defer sel_stmt.Close()
|
defer sel_stmt.Close()
|
||||||
|
|
||||||
fasthttp.ListenAndServe("127.0.0.1:4001", handler)
|
err1 := fasthttp.ListenAndServe(*bind, handler)
|
||||||
|
if err1 != nil {
|
||||||
|
log.Fatalln(err1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handler(ctx *fasthttp.RequestCtx) {
|
func handler(ctx *fasthttp.RequestCtx) {
|
||||||
|
@ -47,7 +62,7 @@ func handler(ctx *fasthttp.RequestCtx) {
|
||||||
uri := ctx.URI()
|
uri := ctx.URI()
|
||||||
|
|
||||||
var scheme []byte
|
var scheme []byte
|
||||||
if behindProxy {
|
if *proxy {
|
||||||
scheme = ctx.Request.Header.Peek("X-Forwarded-Proto")
|
scheme = ctx.Request.Header.Peek("X-Forwarded-Proto")
|
||||||
} else {
|
} else {
|
||||||
scheme = uri.Scheme()
|
scheme = uri.Scheme()
|
||||||
|
@ -58,17 +73,22 @@ func handler(ctx *fasthttp.RequestCtx) {
|
||||||
bytes.Equal(scheme, []byte("https")),
|
bytes.Equal(scheme, []byte("https")),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
urlToSearch := fmt.Sprintf(
|
||||||
|
"%s://%s:%s%s",
|
||||||
|
scheme,
|
||||||
|
host,
|
||||||
|
port,
|
||||||
|
ctx.RequestURI(),
|
||||||
|
)
|
||||||
row := stmt.QueryRow(
|
row := stmt.QueryRow(
|
||||||
string(ctx.Method()),
|
string(ctx.Method()),
|
||||||
fmt.Sprintf(
|
urlToSearch,
|
||||||
"%s://%s:%s%s",
|
|
||||||
scheme,
|
|
||||||
host,
|
|
||||||
port,
|
|
||||||
ctx.RequestURI(),
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if *verbose {
|
||||||
|
log.Println(urlToSearch)
|
||||||
|
}
|
||||||
|
|
||||||
var id int
|
var id int
|
||||||
var code int
|
var code int
|
||||||
|
|
||||||
|
@ -85,9 +105,9 @@ func handler(ctx *fasthttp.RequestCtx) {
|
||||||
ctx.Response.SetStatusCode(code)
|
ctx.Response.SetStatusCode(code)
|
||||||
|
|
||||||
// -- find in FS and read headers+body
|
// -- find in FS and read headers+body
|
||||||
path := "storage/" + strconv.Itoa(id)
|
path := *storage + pathsep + strconv.Itoa(id)
|
||||||
|
|
||||||
fh, err := os.Open(path + "/headers")
|
fh, err := os.Open(path + pathsep + "headers")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sendError(ctx, "Unable to read headers: ", err)
|
sendError(ctx, "Unable to read headers: ", err)
|
||||||
return
|
return
|
||||||
|
@ -103,7 +123,7 @@ func handler(ctx *fasthttp.RequestCtx) {
|
||||||
}
|
}
|
||||||
fh.Close()
|
fh.Close()
|
||||||
|
|
||||||
fb, err := os.Open(path + "/body")
|
fb, err := os.Open(path + pathsep + "body")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sendError(ctx, "Unable to read body: ", err)
|
sendError(ctx, "Unable to read body: ", err)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Add table
Reference in a new issue