-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac1111a
commit 2ae2bb4
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package commands | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net" | ||
|
||
"github.com/cloudfoundry-incubator/garden" | ||
) | ||
|
||
type NetOut struct { | ||
Protocol string `short:"p" required:"true" long:"protocol" choice:"tcp" choice:"udp" description:"protocol to whitelist, only supports tcp or udp"` | ||
StartIP IPFlag `long:"ip-start" required:"true" description:"start of IP range to whitelist, inclusive"` | ||
EndIP IPFlag `long:"ip-end" required:"true" description:"end of IP range to whitelist, inclusive"` | ||
StartPort uint16 `long:"port-start" required:"true" description:"start of port range to whitelist, inclusive"` | ||
EndPort uint16 `long:"port-end" required:"true" description:"end of port range to whitelist, inclusive"` | ||
} | ||
|
||
func (command *NetOut) Execute(maybeHandle []string) error { | ||
container, err := globalClient().Lookup(handle(maybeHandle)) | ||
failIf(err) | ||
|
||
var protocol garden.Protocol | ||
switch command.Protocol { | ||
case "tcp": | ||
protocol = garden.ProtocolTCP | ||
case "udp": | ||
protocol = garden.ProtocolUDP | ||
default: | ||
fail(errors.New("unrecognised protocol")) | ||
} | ||
|
||
ipRange := garden.IPRange{ | ||
Start: command.StartIP.IP(), | ||
End: command.EndIP.IP(), | ||
} | ||
|
||
portRange := garden.PortRange{ | ||
Start: command.StartPort, | ||
End: command.EndPort, | ||
} | ||
|
||
netOutRule := garden.NetOutRule{ | ||
Protocol: protocol, | ||
Networks: []garden.IPRange{ipRange}, | ||
Ports: []garden.PortRange{portRange}, | ||
} | ||
|
||
err = container.NetOut(netOutRule) | ||
failIf(err) | ||
|
||
fmt.Println("applied") | ||
|
||
return nil | ||
} | ||
|
||
type IPFlag net.IP | ||
|
||
func (f *IPFlag) UnmarshalFlag(value string) error { | ||
parsedIP := net.ParseIP(value) | ||
if parsedIP == nil { | ||
return fmt.Errorf("invalid IP: '%s'", value) | ||
} | ||
|
||
*f = IPFlag(parsedIP) | ||
|
||
return nil | ||
} | ||
|
||
func (f IPFlag) IP() net.IP { | ||
return net.IP(f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters