forked from projectdiscovery/simplehttpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinder.go
More file actions
36 lines (30 loc) · 727 Bytes
/
binder.go
File metadata and controls
36 lines (30 loc) · 727 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package binder
import (
"fmt"
"net"
"github.com/phayes/freeport"
"github.com/projectdiscovery/gologger"
)
// CanListenOn the specified address
func CanListenOn(address string) bool {
listener, err := net.Listen("tcp4", address)
if err != nil {
return false
}
if err := listener.Close(); err != nil {
gologger.Info().Msgf("%s\n", err)
}
return true
}
// GetRandomListenAddress from the specified one
func GetRandomListenAddress(currentAddress string) (string, error) {
addrOrig, _, err := net.SplitHostPort(currentAddress)
if err != nil {
return "", err
}
newPort, err := freeport.GetFreePort()
if err != nil {
return "", err
}
return net.JoinHostPort(addrOrig, fmt.Sprintf("%d", newPort)), nil
}