Filled out port usage counter.

This commit is contained in:
Gary Talent 2016-02-27 18:15:37 -06:00
parent ffafcb08a5
commit 863f1d4621
3 changed files with 59 additions and 0 deletions

View File

@ -9,3 +9,20 @@ package main
func setupPortForward(ip, port string) { func setupPortForward(ip, port string) {
} }
func portUsageCount(ports ...[]int) int {
const CMD = "sockstat -4c -p 22"
return 0
}
func portUsageCount(ports ...int) int {
cmd := "sockstat -4c"
for _, v := range ports {
cmd += " -p " + strconv.Itoa(v)
}
out, err := exec.Command(cmd).Output()
if err != nil {
log.Println("Port Usage Check: Could not run ", cmd)
}
return bytes.Count(out, []byte{'\n'}) - 1
}

View File

@ -8,10 +8,25 @@
package main package main
import ( import (
"bytes"
"log" "log"
"os/exec"
"strconv"
) )
// just have this stub to allow building on Linux // just have this stub to allow building on Linux
func setupPortForward(ip, port string) { func setupPortForward(ip, port string) {
log.Print("Port forwarding not currently implemented for Linux/iptables") log.Print("Port forwarding not currently implemented for Linux/iptables")
} }
func portUsageCount(ports ...int) int {
cmd := "sockstat -4c"
for _, v := range ports {
cmd += " -p " + strconv.Itoa(v)
}
out, err := exec.Command(cmd).Output()
if err != nil {
log.Println("Port Usage Check: Could not run ", cmd)
}
return bytes.Count(out, []byte{'\n'}) - 1
}

27
net_test.go Normal file
View File

@ -0,0 +1,27 @@
/*
Copyright 2016 gtalent2@gmail.com
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package main
import (
"net"
"strconv"
"testing"
)
func TestPortCount(t *testing.T) {
port := 49214
// listen on some port that nothing should be using for the sake of the test
go func() {
addr, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:"+strconv.Itoa(port))
net.ListenTCP("tcp", addr)
}()
if portUsageCount(49214) != 1 {
t.Errorf("Port count usage reporting wrong number")
}
}