Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mon files parsing #1

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
commit.txt
version.txt
dist/
.idea/
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ run:
# Tests the project.
.PHONY: test
test: generate
$(GOTEST) -v ./...
$(GOTEST) -v ./... -cover

# Cleans the project.
.PHONY: clean
Expand Down
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module jmpeax.com/sec/monica

go 1.22.4

require github.com/rs/zerolog v1.33.0

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/sys v0.21.0 // indirect
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Empty file added internal/.keep
Empty file.
18 changes: 18 additions & 0 deletions internal/logging/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package logging

import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"os"
"time"
)

var Log zerolog.Logger

func init() {
initLogger()
}

func initLogger() {
Log = log.Output(zerolog.ConsoleWriter{TimeFormat: time.RFC3339, Out: os.Stdout, NoColor: false})
}
22 changes: 20 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
package main

import (
"flag"
"github.com/rs/zerolog"
"jmpeax.com/sec/monica/internal/logging"
)

var (
verbosity = flag.Int("v", -1, "verbosity level")
)

func main() {
// This is a comment
// This is another comment
flag.Parse()
switch *verbosity {
case 1:
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case 2:
zerolog.SetGlobalLevel(zerolog.TraceLevel)
default:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
logging.Log.Info().Msg("Welcome to Monica!")
}
68 changes: 68 additions & 0 deletions pkg/request/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package request

import (
"bufio"
"jmpeax.com/sec/monica/internal/logging"
"log"
"os"
"strings"
)

// ParseRequest parses a raw HTTP request and returns a Request object.
func ParseRequest(raw string) *Request {
logging.Log.Debug().Msg("Parsing request")
var request = Request{}
scanner := bufio.NewScanner(strings.NewReader(raw))
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "#") {
logging.Log.Trace().Msgf("Skipping comment: %s", line)
continue
}
if request.Method == "" {
logging.Log.Trace().Msgf("Parsing request line: %s", line)
parseRequestLine(line, &request)
continue
}
if request.Headers == nil {
request.Headers = make(map[string]string)
for line != "" {
logging.Log.Trace().Msgf("Parsing request headers: %s", line)
parseRequestHeader(line, &request)
if !scanner.Scan() {
break // EOF
}
line = scanner.Text()
}
continue
}
request.Body = strings.Join([]string{request.Body, line}, "\n")
}
return &request
}

func ParseMonFile(file string) *Request {
logging.Log.Debug().Msgf("Parsing file: %s", file)
monFile, err := os.ReadFile(file)
if err != nil {
logging.Log.Error().Err(err).Msgf("Error reading file: %s", file)
return nil
}
return ParseRequest(string(monFile))
}

func parseRequestHeader(line string, r *Request) {
parts := strings.Split(line, ": ")
if len(parts) == 2 {
r.Headers[parts[0]] = parts[1]
}
}

func parseRequestLine(requestLine string, r *Request) {
log.Default().Println("Parsing request line")
parts := strings.Split(requestLine, " ")
if len(parts) >= 2 {
r.Method = parts[0]
r.URL = parts[1]
}
}
64 changes: 64 additions & 0 deletions pkg/request/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package request_test

import (
"jmpeax.com/sec/monica/pkg/request"
"testing"
)

func TestParseMonFile(t *testing.T) {
req := request.ParseMonFile("testdata/simple.mon")
if req == nil {
t.Fatal("Request is nil")
}
if req.Method != "GET" {
t.Fatalf("Method is %s, expected GET", req.Method)
}
if req.URL != "http://httpbin.org/status/200" {
t.Fatalf("URL is %s, expected http://httpbin.org/status/200", req.URL)
}
if len(req.Headers) != 2 {
t.Fatalf("Headers length is %d, expected 2", len(req.Headers))
}
if req.Headers["Host"] != "httpbin.org" {
t.Fatalf("Host header is %s, expected httpbin.org", req.Headers["Host"])
}
if req.Headers["User-Agent"] != "monica/0.1.0" {
t.Fatalf("User-Agent header is %s, expected monica/0.1.0", req.Headers["User-Agent"])
}
if len(req.Body) != 0 {
t.Fatalf("Body length is %d, expected 0", len(req.Body))
}
}

func TestParseRequest(t *testing.T) {
raw := `GET https://www.google.com HTTP/1.1
Host: www.google.com
User-Agent: monica/0.1.0

multi
line
body
`
req := request.ParseRequest(raw)
if req == nil {
t.Fatal("Request is nil")
}
if req.Method != "GET" {
t.Fatalf("Method is %s, expected GET", req.Method)
}
if req.URL != "https://www.google.com" {
t.Fatalf("URL is %s, expected https://www.google.com", req.URL)
}
if len(req.Headers) != 2 {
t.Fatalf("Headers length is %d, expected 2", len(req.Headers))
}
if req.Headers["Host"] != "www.google.com" {
t.Fatalf("Host header is %s, expected www.google.com", req.Headers["Host"])
}
if req.Headers["User-Agent"] != "monica/0.1.0" {
t.Fatalf("User-Agent header is %s, expected monica/0.1.0", req.Headers["User-Agent"])
}
if req.Body != "\nmulti\nline\nbody" {
t.Fatalf("Body is %s, expected \nmulti\nline\nbody", req.Body)
}
}
12 changes: 12 additions & 0 deletions pkg/request/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package request

type Request struct {
Method string
URL string
Headers map[string]string
Body string
}

func (r Request) String() string {
return r.Method + " " + r.URL
}
5 changes: 5 additions & 0 deletions pkg/request/testdata/simple.mon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Simple HTTP Request
GET http://httpbin.org/status/200
Host: httpbin.org
User-Agent: monica/0.1.0

Loading