| skipped 1 lines |
2 | 2 | | |
3 | 3 | | import ( |
4 | 4 | | "bufio" |
5 | | - | "fmt" |
6 | | - | "net/url" |
| 5 | + | "net/http" |
7 | 6 | | "os" |
8 | 7 | | "sync" |
9 | 8 | | |
| skipped 4 lines |
14 | 13 | | ) |
15 | 14 | | |
16 | 15 | | type Runner struct { |
| 16 | + | Client *http.Client |
17 | 17 | | Input chan string |
18 | 18 | | Output chan string |
19 | 19 | | InWg sync.WaitGroup |
| skipped 3 lines |
23 | 23 | | |
24 | 24 | | func New(options *input.Options) Runner { |
25 | 25 | | return Runner{ |
| 26 | + | Client: customClient(options.Timeout), |
26 | 27 | | Input: make(chan string), |
27 | 28 | | Output: make(chan string), |
28 | 29 | | InWg: sync.WaitGroup{}, |
| skipped 4 lines |
33 | 34 | | |
34 | 35 | | func (r *Runner) Run() { |
35 | 36 | | r.InWg.Add(1) |
36 | | - | go pushInput(&r.InWg, &r.Options, r.Input) |
| 37 | + | go pushInput(r) |
37 | 38 | | |
38 | 39 | | r.InWg.Add(1) |
39 | | - | go execute(&r.InWg, &r.Options, r.Input, r.Output) |
| 40 | + | go execute(r) |
40 | 41 | | |
41 | 42 | | r.OutWg.Add(1) |
42 | | - | go pullOutput(&r.OutWg, &r.Options, r.Output) |
| 43 | + | go pullOutput(r) |
43 | 44 | | |
44 | 45 | | r.InWg.Wait() |
45 | 46 | | |
| skipped 1 lines |
47 | 48 | | r.OutWg.Wait() |
48 | 49 | | } |
49 | 50 | | |
50 | | - | func pushInput(wg *sync.WaitGroup, options *input.Options, inputchan chan string) { |
51 | | - | defer wg.Done() |
| 51 | + | func pushInput(r *Runner) { |
| 52 | + | defer r.InWg.Done() |
52 | 53 | | |
53 | 54 | | if fileutil.HasStdin() { |
54 | 55 | | scanner := bufio.NewScanner(os.Stdin) |
55 | 56 | | for scanner.Scan() { |
56 | | - | inputchan <- scanner.Text() |
| 57 | + | r.Input <- scanner.Text() |
57 | 58 | | } |
58 | 59 | | } |
59 | 60 | | |
60 | | - | if options.FileInput != "" { |
61 | | - | for _, line := range golazy.ReadFileLineByLine(options.FileInput) { |
62 | | - | inputchan <- line |
| 61 | + | if r.Options.FileInput != "" { |
| 62 | + | for _, line := range golazy.ReadFileLineByLine(r.Options.FileInput) { |
| 63 | + | r.Input <- line |
63 | 64 | | } |
64 | 65 | | } |
65 | 66 | | |
66 | | - | if options.Input != "" { |
67 | | - | inputchan <- options.Input |
| 67 | + | if r.Options.Input != "" { |
| 68 | + | r.Input <- r.Options.Input |
68 | 69 | | } |
69 | 70 | | |
70 | | - | close(inputchan) |
| 71 | + | close(r.Input) |
71 | 72 | | } |
72 | 73 | | |
73 | | - | func execute(wg *sync.WaitGroup, options *input.Options, inputchan chan string, outputchan chan string) { |
74 | | - | defer wg.Done() |
75 | | - | for value := range inputchan { |
76 | | - | result, err := checkCSP(value) |
| 74 | + | func execute(r *Runner) { |
| 75 | + | defer r.InWg.Done() |
| 76 | + | for value := range r.Input { |
| 77 | + | result, err := checkCSP(value, r.Client) |
77 | 78 | | if err == nil { |
78 | | - | fmt.Println(result) |
79 | | - | outputchan <- *result |
| 79 | + | for _, res := range result { |
| 80 | + | r.Output <- res |
| 81 | + | } |
80 | 82 | | } |
81 | 83 | | } |
82 | 84 | | } |
83 | 85 | | |
84 | | - | func pullOutput(wg *sync.WaitGroup, options *input.Options, outputchan chan string) { |
85 | | - | defer wg.Done() |
| 86 | + | func pullOutput(r *Runner) { |
| 87 | + | defer r.OutWg.Done() |
86 | 88 | | |
87 | | - | for o := range outputchan { |
88 | | - | wg.Add(1) |
89 | | - | go writeOutput(wg, options, o) |
| 89 | + | for o := range r.Output { |
| 90 | + | r.OutWg.Add(1) |
| 91 | + | go writeOutput(&r.OutWg, &r.Options, o) |
90 | 92 | | } |
91 | 93 | | } |
92 | 94 | | |
93 | | - | func writeOutput(wg *sync.WaitGroup, options *input.Options, out url.URL) { |
| 95 | + | func writeOutput(wg *sync.WaitGroup, options *input.Options, out string) { |
94 | 96 | | defer wg.Done() |
95 | 97 | | if options.FileOutput != "" { |
96 | 98 | | file, err := os.OpenFile(options.FileOutput, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644) |
| skipped 3 lines |
100 | 102 | | options.Output = file |
101 | 103 | | } |
102 | 104 | | |
| 105 | + | //print output |
103 | 106 | | //write output to file |
104 | 107 | | } |
105 | 108 | | |