| Conditions | 10 |
| Total Lines | 83 |
| Code Lines | 61 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like cmd.startScan often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | package cmd |
||
| 163 | func startScan(logger *logrus.Logger, cnf *scan.Config, u *url.URL) error { |
||
| 164 | c, err := client.NewClientFromConfig( |
||
| 165 | cnf.TimeoutInMilliseconds, |
||
| 166 | cnf.Socks5Url, |
||
| 167 | cnf.UserAgent, |
||
| 168 | cnf.UseCookieJar, |
||
| 169 | cnf.Cookies, |
||
| 170 | cnf.Headers, |
||
| 171 | cnf.CacheRequests, |
||
| 172 | u, |
||
| 173 | ) |
||
| 174 | if err != nil { |
||
| 175 | return errors.Wrap(err, "failed to build client") |
||
| 176 | } |
||
| 177 | |||
| 178 | dict, err := dictionary.NewDictionaryFrom(cnf.DictionaryPath, c) |
||
| 179 | if err != nil { |
||
| 180 | return errors.Wrap(err, "failed to build dictionary") |
||
| 181 | } |
||
| 182 | |||
| 183 | targetProducer := producer.NewDictionaryProducer(cnf.HTTPMethods, dict, cnf.ScanDepth) |
||
| 184 | reproducer := producer.NewReProducer(targetProducer) |
||
| 185 | |||
| 186 | resultFilter := filter.NewHTTPStatusResultFilter(cnf.HTTPStatusesToIgnore) |
||
| 187 | |||
| 188 | s := scan.NewScanner( |
||
| 189 | c, |
||
| 190 | targetProducer, |
||
| 191 | reproducer, |
||
| 192 | resultFilter, |
||
| 193 | logger, |
||
| 194 | ) |
||
| 195 | |||
| 196 | logger.WithFields(logrus.Fields{ |
||
| 197 | "url": u.String(), |
||
| 198 | "threads": cnf.Threads, |
||
| 199 | "dictionary-length": len(dict), |
||
| 200 | "scan-depth": cnf.ScanDepth, |
||
| 201 | "timeout": cnf.TimeoutInMilliseconds, |
||
| 202 | "socks5": cnf.Socks5Url, |
||
| 203 | "cookies": stringifyCookies(cnf.Cookies), |
||
| 204 | "cookie-jar": cnf.UseCookieJar, |
||
| 205 | "headers": stringifyHeaders(cnf.Headers), |
||
| 206 | "user-agent": cnf.UserAgent, |
||
| 207 | }).Info("Starting scan") |
||
| 208 | |||
| 209 | resultSummarizer := summarizer.NewResultSummarizer(logger) |
||
| 210 | |||
| 211 | osSigint := make(chan os.Signal, 1) |
||
| 212 | signal.Notify(osSigint, os.Interrupt) |
||
| 213 | |||
| 214 | outputSaver, err := newOutputSaver(cnf.Out) |
||
| 215 | if err != nil { |
||
| 216 | logger.WithError(err).Fatal("failed to create output saver") |
||
| 217 | } |
||
| 218 | |||
| 219 | finishFunc := func() { |
||
| 220 | resultSummarizer.Summarize() |
||
| 221 | err := outputSaver.Close() |
||
| 222 | if err != nil { |
||
| 223 | logger.WithError(err).Error("failed to close output file") |
||
| 224 | } |
||
| 225 | logger.Info("Finished scan") |
||
| 226 | } |
||
| 227 | |||
| 228 | for result := range s.Scan(u, cnf.Threads) { |
||
| 229 | select { |
||
| 230 | case <-osSigint: |
||
| 231 | logger.Info("Received sigint, terminating...") |
||
| 232 | finishFunc() |
||
| 233 | return nil |
||
| 234 | default: |
||
| 235 | resultSummarizer.Add(result) |
||
| 236 | err := outputSaver.Save(result) |
||
| 237 | if err != nil { |
||
| 238 | return errors.Wrap(err, "failed to add output to file") |
||
| 239 | } |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | finishFunc() |
||
| 244 | |||
| 245 | return nil |
||
| 246 | } |
||
| 275 |