Skip to content

Commit 5d7dc82

Browse files
author
wintrmvte
committed
+ stuff related with port and hostname manipulation
1 parent 447d8bf commit 5d7dc82

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

net.go

+33-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313
//"syscall"
1414
"time"
15+
"syscall"
1516

1617
portscanner "github.com/anvie/port-scanner"
1718
"github.com/jackpal/gateway"
@@ -229,8 +230,29 @@ func PortscanSingle(target string, port int) bool {
229230
return len(opened_ports) != 0
230231
}
231232

233+
// Returns true if host is alive
234+
func Ping(target string) bool {
235+
open_counter := 0
236+
ports_to_check := []int{80, 443, 21, 22}
237+
ps := portscanner.NewPortScanner(target, 2*time.Second, 5)
238+
for _, port := range ports_to_check {
239+
if ps.IsOpen(port){
240+
open_counter += 1
241+
}
242+
}
243+
return true
244+
}
245+
246+
// Removes hosts from slice that did not respond to a ping request
247+
func RemoveInactive(targets []string) {
248+
for i, t := range(targets){
249+
if ! Ping(t){
250+
targets[i] = ""
251+
}
252+
}
253+
}
232254

233-
//PortFree returns a random free port
255+
// Returns a random free port
234256
func PortFree(port int) int {
235257
var a *net.TCPAddr
236258
a, err := net.ResolveTCPAddr("tcp", "localhost:0")
@@ -243,8 +265,14 @@ func PortFree(port int) int {
243265
return l.Addr().(*net.TCPAddr).Port
244266
}
245267

246-
// TargetProcessor returns a sorted list of targets based on their RRT pingback value
247-
//func TargetProcessor(targets []string) []string {
248-
249-
//}
268+
func PortReuse(network string, address string, conn syscall.RawConn) error {
269+
return portReuse(network, address, conn)
270+
}
250271

272+
// Gracefully closes an instance of net.Listener
273+
func CloseListener(lst net.Listener){
274+
if lst != nil {
275+
lst.Close()
276+
lst = nil
277+
}
278+
}

net_linux.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ func Networks() ([]string, error) {
2222
return wifi_names, nil
2323
}
2424

25-
// PortReuse sets SO_REUSEPORT on socket descriptor
26-
// Can be used as a control parameter to a &net.ListenConfig
27-
func PortReuse(network, address string, conn syscall.RawConn) error {
25+
func portReuse(network string, address string, conn syscall.RawConn) error {
2826
return conn.Control(func(descriptor uintptr){
2927
syscall.SetsockoptInt(descriptor, unix.SOL_SOCKET, unix.SO_REUSEADDR, 1)
3028
})

net_windows.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func networks() ([]string, error) {
2525

2626
// PortReuse sets SO_REUSEPORT on socket descriptor
2727
// Can be used as a control parameter to a &net.ListenConfig
28-
func PortReuse(network, address string, conn syscall.RawConn) error {
28+
func portReuse(network string, address string, conn syscall.RawConn) error {
2929
return conn.Control(func(descriptor uintptr){
3030
windows.SetsockoptInt(windows.Handle(descriptor), windows.SOL_SOCKET, windows.SO_REUSEADDR, 1)
3131
})

0 commit comments

Comments
 (0)