Skip to content

Commit

Permalink
[~] fix logging ignore (#89)
Browse files Browse the repository at this point in the history
* [~] fix logging ignore

* chore: Updated coverage badge.

---------

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
Noooste and actions-user committed May 8, 2024
1 parent c8cae73 commit 998ba41
Show file tree
Hide file tree
Showing 20 changed files with 67 additions and 43 deletions.
Empty file modified LICENSE 100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion README.md 100644 → 100755
@@ -1,6 +1,6 @@
# AzureTLS Client
[![GoDoc](https://godoc.org/github.com/Noooste/azuretls-client?status.svg)](https://godoc.org/github.com/Noooste/azuretls-client)
![Coverage](https://img.shields.io/badge/Coverage-80.2%25-brightgreen)
![Coverage](https://img.shields.io/badge/Coverage-79.0%25-brightgreen)
[![build](https://github.com/Noooste/azuretls-client/actions/workflows/push.yml/badge.svg)](https://github.com/Noooste/azuretls-client/actions/workflows/push.yml)
[![Go Report Card](https://goreportcard.com/badge/Noooste/azuretls-client)](https://goreportcard.com/report/Noooste/azuretls-client)
[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/Noooste/azuretls-client/blob/master/LICENSE)
Expand Down
Empty file modified connection.go 100644 → 100755
Empty file.
Empty file modified cookies.go 100644 → 100755
Empty file.
70 changes: 36 additions & 34 deletions dump.go
Expand Up @@ -94,50 +94,52 @@ func (s *Session) dumpRequest(request *Request, response *Response, err error) {
return
}

if !s.urlMatch(request.parsedUrl, s.dumpIgnore) {
reqUrl := request.parsedUrl.Path
if reqUrl == "" {
reqUrl = "/"
}
if s.urlMatch(request.parsedUrl, s.dumpIgnore) {
return
}

if reqUrl[len(reqUrl)-1] == '/' {
reqUrl += "index.html"
}
reqUrl := request.parsedUrl.Path
if reqUrl == "" {
reqUrl = "/"
}

if reqUrl[len(reqUrl)-1] == '/' {
reqUrl += "index.html"
}

pathSplit := strings.Split(reqUrl, "/")
length := len(pathSplit)
pathSplit := strings.Split(reqUrl, "/")
length := len(pathSplit)

for i := 0; i < length; i++ {
pathSplit[i] = url.PathEscape(pathSplit[i])
}
for i := 0; i < length; i++ {
pathSplit[i] = url.PathEscape(pathSplit[i])
}

folderPath := path.Join(s.dumpDir, request.parsedUrl.Hostname(), strings.Join(pathSplit[:length-1], "/"))
folderPath := path.Join(s.dumpDir, request.parsedUrl.Hostname(), strings.Join(pathSplit[:length-1], "/"))

_ = os.MkdirAll(folderPath, 0755)
_ = os.MkdirAll(folderPath, 0755)

fileName := path.Join(folderPath, pathSplit[length-1])
fileName := path.Join(folderPath, pathSplit[length-1])

iter := 1
for _, err2 := os.ReadFile(fileName); err2 == nil; _, err2 = os.ReadFile(fileName) {
fileName = path.Join(folderPath, pathSplit[length-1]+fmt.Sprintf(" (%d)", iter))
iter++
}
iter := 1
for _, err2 := os.ReadFile(fileName); err2 == nil; _, err2 = os.ReadFile(fileName) {
fileName = path.Join(folderPath, pathSplit[length-1]+fmt.Sprintf(" (%d)", iter))
iter++
}

request.proxy = s.Proxy
requestPart := request.toString()
request.proxy = s.Proxy
requestPart := request.toString()

var responsePart string
if response != nil {
responsePart = response.toString()
} else {
responsePart = "error : " + err.Error()
}
var responsePart string
if response != nil {
responsePart = response.toString()
} else {
responsePart = "error : " + err.Error()
}

if err2 := os.WriteFile(fileName, []byte(fmt.Sprintf(
"%s\n\n%s\n\n\n%s", requestPart, strings.Repeat("=", 80), responsePart,
)), 0755); err2 != nil {
return
}
if err2 := os.WriteFile(fileName, []byte(fmt.Sprintf(
"%s\n\n%s\n\n\n%s", requestPart, strings.Repeat("=", 80), responsePart,
)), 0755); err2 != nil {
return
}
}

Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Expand Up @@ -137,7 +137,7 @@ func ExampleSession_Log() {
session.Log("/any/path/to/ignore", "can.ignore.this", "*.all.subdomains")

session.Get("https://www.google.com")

session.Get("https://www.google.com/any/path/to/ignore")
}

func ExampleSession_SetProxy() {
Expand Down
Empty file modified header.go 100644 → 100755
Empty file.
Empty file modified http2.go 100644 → 100755
Empty file.
Empty file modified ja3.go 100644 → 100755
Empty file.
34 changes: 28 additions & 6 deletions logger.go
Expand Up @@ -4,19 +4,30 @@ import (
"fmt"
http "github.com/Noooste/fhttp"
"github.com/fatih/color"
"net/url"
"regexp"
"strings"
"time"
)

// Log will print the request and response to the console
//
// ignore (optional) is a list of uris to ignore,
// uris (optional) is a list of uris to ignore,
// if ignore is empty, all uris will be logged
func (s *Session) Log(ignore ...string) {
func (s *Session) Log(uris ...string) {
s.logging = true

s.loggingIgnore = make([]string, 0, len(ignore))
s.loggingIgnore = append(s.loggingIgnore, ignore...)
s.loggingIgnore = make([]*regexp.Regexp, 0, len(uris))

for _, v := range uris {
s.loggingIgnore = append(s.loggingIgnore, regexp.MustCompile(
fmt.Sprintf(".*%s.*",
strings.ReplaceAll(
replaceNonAlphaNumeric(v), "*\\.", ".*\\.?",
),
),
))
}
}

// DisableLog will disable request and response logging
Expand All @@ -29,6 +40,17 @@ func (s *Session) EnableLog() {
s.logging = true
}

// LogIgnore will check if the given uri is ignored from dumping
func (s *Session) LogIgnore(uri string) bool {
parsed, err := url.Parse(uri)

if err != nil {
return false
}

return s.urlMatch(parsed, s.loggingIgnore)
}

var colorMethodMap = map[string]*color.Color{
http.MethodGet: color.New(color.BgBlue, color.FgHiWhite),
http.MethodPost: color.New(color.BgHiBlue, color.FgHiWhite),
Expand All @@ -49,7 +71,7 @@ func centerString(s string, width int) string {
}

func (s *Session) logRequest(request *Request) {
if !s.logging {
if !s.logging || s.urlMatch(request.parsedUrl, s.loggingIgnore) {
return
}

Expand Down Expand Up @@ -77,7 +99,7 @@ func getColorStatus(status int) *color.Color {
}

func (s *Session) logResponse(response *Response, err error) {
if !s.logging {
if !s.logging || s.urlMatch(response.Request.parsedUrl, s.loggingIgnore) {
return
}

Expand Down
Empty file modified pinner.go 100644 → 100755
Empty file.
Empty file modified presets.go 100644 → 100755
Empty file.
Empty file modified profiles.go 100644 → 100755
Empty file.
Empty file modified redirect.go 100644 → 100755
Empty file.
Empty file modified request.go 100644 → 100755
Empty file.
Empty file modified response.go 100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion structs.go 100644 → 100755
Expand Up @@ -62,7 +62,7 @@ type Session struct {
dumpIgnore []*regexp.Regexp

logging bool
loggingIgnore []string
loggingIgnore []*regexp.Regexp

// If true, print detailed logs or debugging information. Deprecated: Use Dump instead.
Verbose bool
Expand Down
Empty file modified transport.go 100644 → 100755
Empty file.
Empty file modified utils.go 100644 → 100755
Empty file.
Empty file modified websocket.go 100644 → 100755
Empty file.

0 comments on commit 998ba41

Please sign in to comment.