|
1 | 1 | package vardump |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "strconv" |
5 | 4 | "strings" |
6 | 5 |
|
7 | 6 | "github.com/projectdiscovery/nuclei/v3/pkg/types" |
8 | 7 | mapsutil "github.com/projectdiscovery/utils/maps" |
| 8 | + "github.com/yassinebenaid/godump" |
9 | 9 | ) |
10 | 10 |
|
11 | | -// EnableVarDump enables var dump for debugging optionally |
12 | | -var EnableVarDump bool |
| 11 | +// variables is a map of variables |
| 12 | +type variables = map[string]any |
13 | 13 |
|
14 | | -// DumpVariables writes the truncated dump of variables to a string |
15 | | -// in a formatted key-value manner. |
16 | | -// |
17 | | -// The values are truncated to return 50 characters from start and end. |
18 | | -func DumpVariables(data map[string]interface{}) string { |
19 | | - var counter int |
| 14 | +// DumpVariables dumps the variables in a pretty format |
| 15 | +func DumpVariables(data variables) string { |
| 16 | + if !EnableVarDump { |
| 17 | + return "" |
| 18 | + } |
| 19 | + |
| 20 | + d := godump.Dumper{ |
| 21 | + Indentation: " ", |
| 22 | + HidePrivateFields: false, |
| 23 | + ShowPrimitiveNamedTypes: true, |
| 24 | + } |
| 25 | + |
| 26 | + d.Theme = godump.Theme{ |
| 27 | + String: godump.RGB{R: 138, G: 201, B: 38}, |
| 28 | + Quotes: godump.RGB{R: 112, G: 214, B: 255}, |
| 29 | + Bool: godump.RGB{R: 249, G: 87, B: 56}, |
| 30 | + Number: godump.RGB{R: 10, G: 178, B: 242}, |
| 31 | + Types: godump.RGB{R: 0, G: 150, B: 199}, |
| 32 | + Address: godump.RGB{R: 205, G: 93, B: 0}, |
| 33 | + PointerTag: godump.RGB{R: 110, G: 110, B: 110}, |
| 34 | + Nil: godump.RGB{R: 219, G: 57, B: 26}, |
| 35 | + Func: godump.RGB{R: 160, G: 90, B: 220}, |
| 36 | + Fields: godump.RGB{R: 189, G: 176, B: 194}, |
| 37 | + Chan: godump.RGB{R: 195, G: 154, B: 76}, |
| 38 | + UnsafePointer: godump.RGB{R: 89, G: 193, B: 180}, |
| 39 | + Braces: godump.RGB{R: 185, G: 86, B: 86}, |
| 40 | + } |
20 | 41 |
|
21 | | - buffer := &strings.Builder{} |
22 | | - buffer.Grow(len(data) * 78) // grow buffer to an approximate size |
| 42 | + return d.Sprint(process(data, Limit)) |
| 43 | +} |
23 | 44 |
|
24 | | - builder := &strings.Builder{} |
25 | | - // sort keys for deterministic output |
| 45 | +// process is a helper function that processes the variables |
| 46 | +// and returns a new map of variables |
| 47 | +func process(data variables, limit int) variables { |
26 | 48 | keys := mapsutil.GetSortedKeys(data) |
| 49 | + vars := make(variables) |
| 50 | + |
| 51 | + if limit == 0 { |
| 52 | + limit = 255 |
| 53 | + } |
27 | 54 |
|
28 | 55 | for _, k := range keys { |
29 | | - v := data[k] |
30 | | - valueString := types.ToString(v) |
31 | | - |
32 | | - counter++ |
33 | | - if len(valueString) > 50 { |
34 | | - builder.Grow(56) |
35 | | - builder.WriteString(valueString[0:25]) |
36 | | - builder.WriteString(" .... ") |
37 | | - builder.WriteString(valueString[len(valueString)-25:]) |
38 | | - valueString = builder.String() |
39 | | - builder.Reset() |
| 56 | + v := types.ToString(data[k]) |
| 57 | + v = strings.ReplaceAll(strings.ReplaceAll(v, "\r", " "), "\n", " ") |
| 58 | + if len(v) > limit { |
| 59 | + v = v[:limit] |
| 60 | + v += " [...]" |
40 | 61 | } |
41 | | - valueString = strings.ReplaceAll(strings.ReplaceAll(valueString, "\r", " "), "\n", " ") |
42 | | - |
43 | | - buffer.WriteString("\t") |
44 | | - buffer.WriteString(strconv.Itoa(counter)) |
45 | | - buffer.WriteString(". ") |
46 | | - buffer.WriteString(k) |
47 | | - buffer.WriteString(" => ") |
48 | | - buffer.WriteString(valueString) |
49 | | - buffer.WriteString("\n") |
| 62 | + |
| 63 | + vars[k] = v |
50 | 64 | } |
51 | | - final := buffer.String() |
52 | | - return final |
| 65 | + |
| 66 | + return vars |
53 | 67 | } |
0 commit comments