mirror of
https://github.com/projectdiscovery/httpx
synced 2026-06-08 16:50:17 +00:00
d0081af3ec
* code quality: improve error handling and fix bugs - Fix bug: return err instead of closeErr in DecodeData error path (httpx.go) - Improve error handling for strconv.Atoi calls in multiple locations - Change hammingDistanceThreshold from var to const since it's never modified - Fix redundant return statement in ListFilesWithPattern - Improve port parsing to handle empty port strings explicitly * runner: keep input counters incrementing under -skip-dedupe Revert the defensive strconv.Atoi guard around the SkipDedupe branches: values stored in r.hm are always integer strings written by this same code path, so the parseErr/cnt>0 check is unreachable in practice, and when taken literally it skipped the numHosts/numTargets increment for duplicate inputs. --------- Co-authored-by: Mzack9999 <mzack9999@protonmail.com>
90 lines
2.0 KiB
Go
90 lines
2.0 KiB
Go
package fileutil
|
|
|
|
import (
|
|
"bufio"
|
|
"errors"
|
|
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
|
|
"github.com/projectdiscovery/httpx/common/stringz"
|
|
fileutil "github.com/projectdiscovery/utils/file"
|
|
)
|
|
|
|
// HasStdin determines if the user has piped input
|
|
func HasStdin() bool {
|
|
stat, err := os.Stdin.Stat()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
mode := stat.Mode()
|
|
|
|
isPipedFromChrDev := (mode & os.ModeCharDevice) == 0
|
|
isPipedFromFIFO := (mode & os.ModeNamedPipe) != 0
|
|
|
|
return isPipedFromChrDev || isPipedFromFIFO
|
|
}
|
|
|
|
// LoadFile content to slice
|
|
func LoadFile(filename string) (lines []string) {
|
|
f, err := os.Open(filename)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer f.Close() //nolint
|
|
s := bufio.NewScanner(f)
|
|
for s.Scan() {
|
|
lines = append(lines, s.Text())
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// ListFilesWithPattern in a root folder
|
|
func ListFilesWithPattern(rootpattern string) ([]string, error) {
|
|
files, err := filepath.Glob(rootpattern)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(files) == 0 {
|
|
return nil, errors.New("no files found")
|
|
}
|
|
return files, nil
|
|
}
|
|
|
|
// FileNameIsGlob check if the filanem is a pattern
|
|
func FileNameIsGlob(pattern string) bool {
|
|
_, err := regexp.Compile(pattern)
|
|
return err == nil
|
|
}
|
|
|
|
func LoadCidrsFromSliceOrFileWithMaxRecursion(option string, splitchar string, maxRecursion int) (networkList []string) {
|
|
if maxRecursion < 0 {
|
|
return
|
|
}
|
|
items := stringz.SplitByCharAndTrimSpace(option, splitchar)
|
|
for _, item := range items {
|
|
if net.ParseIP(item) != nil {
|
|
networkList = append(networkList, item)
|
|
} else if _, _, err := net.ParseCIDR(item); err == nil {
|
|
networkList = append(networkList, item)
|
|
} else if fileutil.FileExists(item) {
|
|
if filedata, err := os.ReadFile(item); err == nil && len(filedata) > 0 {
|
|
networkList = append(networkList, LoadCidrsFromSliceOrFileWithMaxRecursion(string(filedata), "\n", maxRecursion-1)...)
|
|
}
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func AbsPathOrDefault(p string) string {
|
|
if absPath, err := filepath.Abs(p); err == nil {
|
|
return absPath
|
|
}
|
|
return p
|
|
}
|