dospin/net_freebsd.go

52 lines
1.3 KiB
Go
Raw Normal View History

2016-02-26 17:35:57 -06:00
/*
2017-01-25 17:59:25 -06:00
Copyright 2016-2017 gtalent2@gmail.com
2016-02-26 17:35:57 -06:00
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/.
*/
2016-02-14 23:38:28 -06:00
package main
import (
"bytes"
"log"
"os/exec"
"strconv"
)
func addPortForward(ruleName, ip, port string) {
pfrule := "\"rdr pass on $dospin_ext_if proto { tcp, udp } from any to any port {" + port + "} -> " + ip + "\""
in, err := exec.Command("pfctl", "-a", "\"dospin_"+ruleName+"\"", "-f", "-").StdinPipe()
defer in.Close()
if err != nil {
log.Println("Port Forwarding:", err)
}
_, err = in.Write([]byte(pfrule))
if err != nil {
log.Println("Port Forwarding:", err)
}
2016-02-14 23:38:28 -06:00
}
2016-02-27 18:15:37 -06:00
func rmPortForward(ruleName string) {
_, err := exec.Command("pfctl", "-a", "\"dospin_"+ruleName+"\"", "-F", "rules").Output()
if err != nil {
log.Println("Port Forwarding:", err)
}
}
2016-02-27 18:15:37 -06:00
func portUsageCount(ports ...int) int {
cmd := "sockstat"
2016-02-28 01:05:30 -06:00
args := []string{"-4c"}
2016-02-27 18:15:37 -06:00
for _, v := range ports {
2016-02-28 01:05:30 -06:00
args = append(args, "-p")
args = append(args, strconv.Itoa(v))
2016-02-27 18:15:37 -06:00
}
2016-02-28 01:05:30 -06:00
out, err := exec.Command(cmd, args...).Output()
2016-02-27 18:15:37 -06:00
if err != nil {
2016-02-28 01:05:30 -06:00
log.Println("Port Usage Check: Could not run \""+cmd+"\":", err)
2016-02-27 18:15:37 -06:00
}
return bytes.Count(out, []byte{'\n'}) - 1
}