mirror of
https://github.com/projectdiscovery/httpx.git
synced 2024-09-11 17:46:08 +03:00
Merge pull request #156 from melardev/port-parsing-refactor
Improved readability and error handling on customport.go
This commit is contained in:
commit
0c04b07430
@ -1,6 +1,7 @@
|
||||
package customport
|
||||
|
||||
import (
|
||||
"github.com/projectdiscovery/gologger"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@ -34,33 +35,48 @@ func (c *CustomPorts) Set(value string) error {
|
||||
// check if port is a single integer value or needs to be expanded further
|
||||
for _, potentialPort := range potentialPorts {
|
||||
protocol := httpx.HTTPorHTTPS
|
||||
potenialPort := strings.ToLower(potentialPort)
|
||||
if strings.HasPrefix(potenialPort, httpx.HTTP+":") {
|
||||
potentialPort = strings.TrimPrefix(potenialPort, httpx.HTTP+":")
|
||||
potentialPort = strings.TrimSpace(strings.ToLower(potentialPort))
|
||||
if strings.HasPrefix(potentialPort, httpx.HTTP+":") {
|
||||
potentialPort = strings.TrimPrefix(potentialPort, httpx.HTTP+":")
|
||||
protocol = httpx.HTTP
|
||||
}
|
||||
if strings.HasPrefix(potenialPort, httpx.HTTPS+":") {
|
||||
potentialPort = strings.TrimPrefix(potenialPort, httpx.HTTPS+":")
|
||||
} else if strings.HasPrefix(potentialPort, httpx.HTTPS+":") {
|
||||
potentialPort = strings.TrimPrefix(potentialPort, httpx.HTTPS+":")
|
||||
protocol = httpx.HTTPS
|
||||
}
|
||||
|
||||
potentialRange := strings.Split(strings.TrimSpace(potentialPort), "-")
|
||||
potentialRange := strings.Split(potentialPort, "-")
|
||||
// it's a single port?
|
||||
if len(potentialRange) < portRangeParts {
|
||||
if p, err := strconv.Atoi(potentialPort); err == nil {
|
||||
Ports[p] = protocol
|
||||
} else {
|
||||
gologger.Warningf("Could not cast port to integer, your value: %s, resulting error %s. Skipping it\n",
|
||||
potentialPort, err.Error())
|
||||
}
|
||||
} else {
|
||||
// expand range
|
||||
var lowP, highP int
|
||||
lowP, err := strconv.Atoi(potentialRange[0])
|
||||
if err != nil {
|
||||
gologger.Warningf("Could not cast first port of your port range(%s) to integer, your value: %s, resulting error %s. Skipping it\n",
|
||||
potentialPort, potentialRange[0], err.Error())
|
||||
continue
|
||||
}
|
||||
highP, err = strconv.Atoi(potentialRange[1])
|
||||
if err != nil {
|
||||
gologger.Warningf("Could not cast last port of your port range(%s) to integer, "+
|
||||
"your value: %s, resulting error %s. Skipping it\n",
|
||||
potentialPort, potentialRange[1], err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
if lowP > highP {
|
||||
gologger.Warningf("first value of port range should be lower than the last part port "+
|
||||
"in that range, your range: [%d, %d]. Skipping it\n",
|
||||
lowP, highP)
|
||||
continue
|
||||
}
|
||||
|
||||
for i := lowP; i <= highP; i++ {
|
||||
Ports[i] = protocol
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user