77 "context"
88 "fmt"
99 "net"
10+ "net/netip"
1011 "os"
1112 "path/filepath"
1213 "strconv"
@@ -19,7 +20,6 @@ import (
1920 "github.com/datawire/dlib/dlog"
2021 "github.com/telepresenceio/telepresence/v2/pkg/agentconfig"
2122 "github.com/telepresenceio/telepresence/v2/pkg/dos"
22- "github.com/telepresenceio/telepresence/v2/pkg/iputil"
2323 "github.com/telepresenceio/telepresence/v2/pkg/version"
2424)
2525
@@ -45,7 +45,7 @@ func loadConfig(ctx context.Context) (*config, error) {
4545 return & c , nil
4646}
4747
48- func (c * config ) configureIptables (ctx context.Context , iptables * iptables.IPTables , loopback , localHostCIDR , podIP string ) error {
48+ func (c * config ) configureIptables (ctx context.Context , iptables * iptables.IPTables , loopback string , localHostCIDR netip. Prefix , podIP netip. Addr ) error {
4949 // These iptables rules implement routing such that a packet directed to the appPort will hit the agentPort instead.
5050 // If there's no mesh this is simply request -> agent -> app (or intercept)
5151 // However, if there's a service mesh we want to make sure we don't bypass the mesh, so the traffic
@@ -114,8 +114,8 @@ func (c *config) configureIptables(ctx context.Context, iptables *iptables.IPTab
114114 // loop it back into the agent.
115115 dlog .Debugf (ctx , "output DNAT %s:%d -> %s:%d" , podIP , ac .ProxyPort (ic ), podIP , ic .ContainerPort )
116116 err = iptables .AppendUnique (nat , outputChain ,
117- "-p" , lcProto , "-d" , podIP , "--dport" , strconv .Itoa (int (ac .ProxyPort (ic ))),
118- "-j" , "DNAT" , "--to-destination" , net . JoinHostPort (podIP , strconv . Itoa ( int ( ic .ContainerPort )) ))
117+ "-p" , lcProto , "-d" , podIP . String () , "--dport" , strconv .Itoa (int (ac .ProxyPort (ic ))),
118+ "-j" , "DNAT" , "--to-destination" , netip . AddrPortFrom (podIP , ic .ContainerPort ). String ( ))
119119 if err != nil {
120120 return fmt .Errorf ("failed to append rule to %s: %w" , outputChain , err )
121121 }
@@ -155,7 +155,7 @@ func (c *config) configureIptables(ctx context.Context, iptables *iptables.IPTab
155155 err = iptables .Insert (nat , "OUTPUT" , 1 ,
156156 "-o" , loopback ,
157157 "-p" , lcProto ,
158- "!" , "-d" , localHostCIDR ,
158+ "!" , "-d" , localHostCIDR . String () ,
159159 "-m" , "owner" , "--uid-owner" , agentUID ,
160160 "-j" , outputChain )
161161 if err != nil {
@@ -213,11 +213,15 @@ func Main(ctx context.Context, args ...string) error {
213213 return err
214214 }
215215 proto := iptables .ProtocolIPv4
216- localhostCIDR := "127.0.0.1/32"
217- podIP := os .Getenv ("POD_IP" )
218- if len (iputil .Parse (podIP )) == 16 {
216+ localhostCIDR := netip .PrefixFrom (netip .AddrFrom4 ([4 ]byte {127 , 0 , 0 , 1 }), 32 )
217+ podIP , err := netip .ParseAddr (os .Getenv ("POD_IP" ))
218+ if err != nil {
219+ dlog .Error (ctx , err )
220+ return err
221+ }
222+ if podIP .Is6 () {
219223 proto = iptables .ProtocolIPv6
220- localhostCIDR = "::1/ 128"
224+ localhostCIDR = netip . PrefixFrom ( netip . IPv6Loopback (), 128 )
221225 }
222226 it , err := iptables .NewWithProtocol (proto )
223227 if err != nil {
0 commit comments