Skip to content

Commit b84e850

Browse files
committed
Improve codes
1 parent 9c652ca commit b84e850

File tree

5 files changed

+11
-109
lines changed

5 files changed

+11
-109
lines changed

cmd/file.go

+5-54
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"bufio"
54
"fmt"
65
"os"
76
"strconv"
@@ -13,6 +12,8 @@ import (
1312
model "github.com/hahwul/dalfox/v2/pkg/model"
1413
"github.com/hahwul/dalfox/v2/pkg/printing"
1514
"github.com/hahwul/dalfox/v2/pkg/scanning"
15+
voltFile "github.com/hahwul/volt/file"
16+
voltUtils "github.com/hahwul/volt/util"
1617
"github.com/spf13/cobra"
1718
)
1819

@@ -34,7 +35,7 @@ var fileCmd = &cobra.Command{
3435
rawdata, _ := cmd.Flags().GetBool("rawdata")
3536
if rawdata {
3637
printing.DalLog("SYSTEM", "Using file mode(rawdata)", options)
37-
ff, err := readLinesOrLiteral(args[0])
38+
ff, err := voltFile.ReadLinesOrLiteral(args[0])
3839
_ = err
3940
var path, body, host, target string
4041
bodyswitch := false
@@ -87,14 +88,14 @@ var fileCmd = &cobra.Command{
8788
if (!options.NoSpinner || !options.Silence) && !sf {
8889
options.SpinnerObject = spinner.New(spinner.CharSets[14], 100*time.Millisecond, spinner.WithWriter(os.Stderr)) // Build our new spinner
8990
}
90-
ff, err := readLinesOrLiteral(args[0])
91+
ff, err := voltFile.ReadLinesOrLiteral(args[0])
9192
_ = err
9293
for _, target := range ff {
9394
targets = append(targets, target)
9495
}
9596

9697
// Remove Deplicated value
97-
targets = unique(targets)
98+
targets = voltUtils.UniqueStringSlice(targets)
9899
printing.DalLog("SYSTEM", "Loaded "+strconv.Itoa(len(targets))+" target urls", options)
99100
multi, _ := cmd.Flags().GetBool("multicast")
100101
mass, _ := cmd.Flags().GetBool("mass")
@@ -203,53 +204,3 @@ func init() {
203204
fileCmd.Flags().Bool("silence-force", false, "Only print PoC (not print progress)")
204205
fileCmd.Flags().Int("mass-worker", 10, "Parallel worker of --mass and --multicast option")
205206
}
206-
207-
// a slice of strings, returning the slice and any error
208-
func readLines(filename string) ([]string, error) {
209-
f, err := os.Open(filename)
210-
if err != nil {
211-
return []string{}, err
212-
}
213-
defer f.Close()
214-
215-
lines := make([]string, 0)
216-
sc := bufio.NewScanner(f)
217-
for sc.Scan() {
218-
lines = append(lines, sc.Text())
219-
}
220-
221-
return lines, sc.Err()
222-
}
223-
224-
// readLinesOrLiteral tries to read lines from a file, returning
225-
// the arg in a string slice if the file doesn't exist, unless
226-
// the arg matches its default value
227-
func readLinesOrLiteral(arg string) ([]string, error) {
228-
if isFile(arg) {
229-
return readLines(arg)
230-
}
231-
232-
// if the argument isn't a file, but it is the default, don't
233-
// treat it as a literal value
234-
235-
return []string{arg}, nil
236-
}
237-
238-
// isFile returns true if its argument is a regular file
239-
func isFile(path string) bool {
240-
f, err := os.Stat(path)
241-
return err == nil && f.Mode().IsRegular()
242-
}
243-
244-
// unique is ..
245-
func unique(intSlice []string) []string {
246-
keys := make(map[string]bool)
247-
list := []string{}
248-
for _, entry := range intSlice {
249-
if _, value := keys[entry]; !value {
250-
keys[entry] = true
251-
list = append(list, entry)
252-
}
253-
}
254-
return list
255-
}

cmd/pipe.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
model "github.com/hahwul/dalfox/v2/pkg/model"
1313
"github.com/hahwul/dalfox/v2/pkg/printing"
1414
"github.com/hahwul/dalfox/v2/pkg/scanning"
15+
voltUtils "github.com/hahwul/volt/util"
1516
"github.com/spf13/cobra"
1617
)
1718

@@ -38,7 +39,7 @@ var pipeCmd = &cobra.Command{
3839
target := sc.Text()
3940
targets = append(targets, target)
4041
}
41-
targets = unique(targets)
42+
targets = voltUtils.UniqueStringSlice(targets)
4243
printing.DalLog("SYSTEM", "Loaded "+strconv.Itoa(len(targets))+" target urls", options)
4344

4445
multi, _ := cmd.Flags().GetBool("multicast")

pkg/scanning/parameterAnlaysis.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/hahwul/dalfox/v2/pkg/optimization"
1717
"github.com/hahwul/dalfox/v2/pkg/printing"
1818
"github.com/hahwul/dalfox/v2/pkg/verification"
19+
voltFile "github.com/hahwul/volt/file"
1920
vlogger "github.com/hahwul/volt/logger"
2021
)
2122

@@ -77,7 +78,7 @@ func ParameterAnalysis(target string, options model.Options, rl *rateLimiter) ma
7778
}
7879
} else {
7980
// Param mining with wordlist fil --mining-dict-word
80-
ff, err := readLinesOrLiteral(options.MiningWordlist)
81+
ff, err := voltFile.ReadLinesOrLiteral(options.MiningWordlist)
8182
if err != nil {
8283
printing.DalLog("SYSTEM", "Mining wordlist load fail..", options)
8384
} else {

pkg/scanning/scan.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/hahwul/dalfox/v2/pkg/optimization"
2020
"github.com/hahwul/dalfox/v2/pkg/printing"
2121
"github.com/hahwul/dalfox/v2/pkg/verification"
22+
voltFile "github.com/hahwul/volt/file"
2223
)
2324

2425
var (
@@ -286,7 +287,7 @@ func Scan(target string, options model.Options, sid string) (model.Result, error
286287

287288
// Custom Payload
288289
if isAllowType(policy["Content-Type"]) && options.CustomPayloadFile != "" {
289-
ff, err := readLinesOrLiteral(options.CustomPayloadFile)
290+
ff, err := voltFile.ReadLinesOrLiteral(options.CustomPayloadFile)
290291
if err != nil {
291292
printing.DalLog("SYSTEM", "Custom XSS payload load fail..", options)
292293
} else {

pkg/scanning/utils.go

-52
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,11 @@
11
package scanning
22

33
import (
4-
"bufio"
5-
"os"
64
"strings"
75

86
"github.com/hahwul/dalfox/v2/pkg/model"
97
)
108

11-
// a slice of strings, returning the slice and any error
12-
func readLines(filename string) ([]string, error) {
13-
f, err := os.Open(filename)
14-
if err != nil {
15-
return []string{}, err
16-
}
17-
defer f.Close()
18-
19-
lines := make([]string, 0)
20-
sc := bufio.NewScanner(f)
21-
for sc.Scan() {
22-
lines = append(lines, sc.Text())
23-
}
24-
25-
return lines, sc.Err()
26-
}
27-
28-
// readLinesOrLiteral tries to read lines from a file, returning
29-
// the arg in a string slice if the file doesn't exist, unless
30-
// the arg matches its default value
31-
func readLinesOrLiteral(arg string) ([]string, error) {
32-
if isFile(arg) {
33-
return readLines(arg)
34-
}
35-
36-
// if the argument isn't a file, but it is the default, don't
37-
// treat it as a literal value
38-
39-
return []string{arg}, nil
40-
}
41-
42-
// isFile returns true if its argument is a regular file
43-
func isFile(path string) bool {
44-
f, err := os.Stat(path)
45-
return err == nil && f.Mode().IsRegular()
46-
}
47-
48-
// unique is ..
49-
func unique(intSlice []string) []string {
50-
keys := make(map[string]bool)
51-
list := []string{}
52-
for _, entry := range intSlice {
53-
if _, value := keys[entry]; !value {
54-
keys[entry] = true
55-
list = append(list, entry)
56-
}
57-
}
58-
return list
59-
}
60-
619
func indexOf(element string, data []string) int {
6210
for k, v := range data {
6311
if element == v {

0 commit comments

Comments
 (0)