forked from terra-farm/go-virtualbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pfrule.go
51 lines (44 loc) · 1.17 KB
/
pfrule.go
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package virtualbox
import (
"fmt"
"net"
)
// PFRule represents a port forwarding rule.
type PFRule struct {
Proto PFProto
HostIP net.IP // can be nil to match any host interface
GuestIP net.IP // can be nil if guest IP is leased from built-in DHCP
HostPort uint16
GuestPort uint16
}
// PFProto represents the protocol of a port forwarding rule.
type PFProto string
const (
// PFTCP when forwarding a TCP port.
PFTCP = PFProto("tcp")
// PFUDP when forwarding an UDP port.
PFUDP = PFProto("udp")
)
// String returns a human-friendly representation of the port forwarding rule.
func (r PFRule) String() string {
hostip, guestip := grab(r)
return fmt.Sprintf("%s://%s:%d --> %s:%d",
r.Proto, hostip, r.HostPort,
guestip, r.GuestPort)
}
// Format returns the string needed as a command-line argument to VBoxManage.
func (r PFRule) Format() string {
hostip, guestip := grab(r)
return fmt.Sprintf("%s,%s,%d,%s,%d", r.Proto, hostip, r.HostPort, guestip, r.GuestPort)
}
func grab(r PFRule) (string, string) {
hostip := ""
if r.HostIP != nil {
hostip = r.HostIP.String()
}
guestip := ""
if r.GuestIP != nil {
guestip = r.GuestIP.String()
}
return hostip, guestip
}