Skip to content

Commit

Permalink
port: add ChildIP
Browse files Browse the repository at this point in the history
allow users to override the IP to use for the connection inside the
network namespace.

It is useful e.g. with slirp4netns to override the IP to "10.0.2.100".

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Jan 21, 2021
1 parent 9fd61ce commit 30cf294
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/google/uuid v1.1.3 h1:twObb+9XcuH5B9V1TBCvvvZoO6iEdILi2a76PYn5rJI=
github.com/google/uuid v1.1.3/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.4/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.5 h1:kxhtnfFVi+rYdOALN0B3k9UT86zVJKfBimRaciULW4I=
github.com/google/uuid v1.1.5/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
Expand Down
16 changes: 15 additions & 1 deletion pkg/port/builtin/child/child.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,21 @@ func (d *childDriver) handleConnectRequest(c *net.UnixConn, req *msg.Request) er
return errors.Errorf("unknown proto: %q", req.Proto)
}
var dialer net.Dialer
targetConn, err := dialer.Dial(req.Proto, fmt.Sprintf("127.0.0.1:%d", req.Port))
ip := req.IP
if ip == "" {
ip = "127.0.0.1"
} else {
p := net.ParseIP(ip)
if p == nil {
return errors.Errorf("invalid IP: %q", ip)
}
p = p.To4()
if p == nil {
return errors.Errorf("unsupported IP (v6?): %s", ip)
}
ip = p.String()
}
targetConn, err := dialer.Dial(req.Proto, fmt.Sprintf("%s:%d", ip, req.Port))
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/port/builtin/msg/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
type Request struct {
Type string // "init" or "connect"
Proto string // "tcp" or "udp"
IP string
Port int
}

Expand Down Expand Up @@ -53,6 +54,7 @@ func ConnectToChild(c *net.UnixConn, spec port.Spec) (int, error) {
Type: RequestTypeConnect,
Proto: spec.Proto,
Port: spec.ChildPort,
IP: spec.ChildIP,
}
if _, err := msgutil.MarshalToWriter(c, &req); err != nil {
return 0, err
Expand Down
1 change: 1 addition & 0 deletions pkg/port/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Spec struct {
ParentIP string `json:"parentIP,omitempty"` // IPv4 address. can be empty (0.0.0.0).
ParentPort int `json:"parentPort,omitempty"`
ChildPort int `json:"childPort,omitempty"`
ChildIP string `json:"childIP,omitempty"` // IPv4 address. If not specified, "127.0.0.1" is used.
}

type Status struct {
Expand Down
16 changes: 15 additions & 1 deletion pkg/port/slirp4netns/slirp4netns.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,27 @@ func (d *driver) AddPort(ctx context.Context, spec port.Spec) (*port.Status, err
if err != nil {
return nil, err
}
ip := spec.ChildIP
if ip == "" {
ip = d.childIP
} else {
p := net.ParseIP(ip)
if p == nil {
return nil, errors.Errorf("invalid IP: %q", ip)
}
p = p.To4()
if p == nil {
return nil, errors.Errorf("unsupported IP (v6?): %s", ip)
}
ip = p.String()
}
req := request{
Execute: "add_hostfwd",
Arguments: addHostFwdArguments{
Proto: spec.Proto,
HostAddr: spec.ParentIP,
HostPort: spec.ParentPort,
GuestAddr: d.childIP,
GuestAddr: ip,
GuestPort: spec.ChildPort,
},
}
Expand Down
18 changes: 16 additions & 2 deletions pkg/port/socat/socat.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,34 @@ func createSocatCmd(ctx context.Context, spec port.Spec, logWriter io.Writer, ch
if spec.ChildPort < 1 || spec.ChildPort > 65535 {
return nil, errors.Errorf("unsupported childPort: %d", spec.ChildPort)
}
ip := spec.ChildIP
if ip == "" {
ip = "127.0.0.1"
} else {
p := net.ParseIP(ip)
if p == nil {
return nil, errors.Errorf("invalid IP: %q", ip)
}
p = p.To4()
if p == nil {
return nil, errors.Errorf("unsupported IP (v6?): %s", ip)
}
ip = p.String()
}
var cmd *exec.Cmd
switch spec.Proto {
case "tcp":
cmd = exec.CommandContext(ctx,
"socat",
fmt.Sprintf("TCP-LISTEN:%d,bind=%s,reuseaddr,fork,rcvbuf=65536,sndbuf=65536", spec.ParentPort, ipStr),
fmt.Sprintf("EXEC:\"%s\",nofork",
fmt.Sprintf("nsenter -U -n --preserve-credentials -t %d socat STDIN TCP4:127.0.0.1:%d", childPID, spec.ChildPort)))
fmt.Sprintf("nsenter -U -n --preserve-credentials -t %d socat STDIN TCP4:%s:%d", childPID, ip, spec.ChildPort)))
case "udp":
cmd = exec.CommandContext(ctx,
"socat",
fmt.Sprintf("UDP-LISTEN:%d,bind=%s,reuseaddr,fork,rcvbuf=65536,sndbuf=65536", spec.ParentPort, ipStr),
fmt.Sprintf("EXEC:\"%s\",nofork",
fmt.Sprintf("nsenter -U -n --preserve-credentials -t %d socat STDIN UDP4:127.0.0.1:%d", childPID, spec.ChildPort)))
fmt.Sprintf("nsenter -U -n --preserve-credentials -t %d socat STDIN UDP4:%s:%d", childPID, ip, spec.ChildPort)))
}
cmd.Env = os.Environ()
cmd.Stdout = logWriter
Expand Down

0 comments on commit 30cf294

Please sign in to comment.