I have a multi-homed kam with a uas behind one private interface and a public interface for the uac+sip provider.

When the uac initiates a call, kam sends the invite to the private uas which sets up a b-leg call back to kam with a Contact header that has the uas' private ip address.  I want to change that private ip address to be Kamailio's advertised/public ip address before it gets forwarded to the sip provider.

I am rewriting Contact in a route from request_route like this:
> if ($rP != $null) {
>   $var(ct) = ";transport=" + $rP;
> }else{
>   $var(ct) = "";
> }
> $var(cr) = "<sip:" + $fU + "@" + $RAi + ":" + $RAp + $var(ct) + ">";
>
> remove_hf("Contact");
> append_hf("Contact: $var(cr)\r\n");

This seems to work fine for the INVITE going to the provider.

There's also the 200 OK that the uas sends for the uac (a-leg) with a private ip address in Contact -- I want to rewrite that before sending it to the UAC, too.

Using the same method as above (but inside onreply_route), what happens is the UAC receives the modified OK and sends back an ACK with the modified Contact (kam's public ip addr) in the ACK r-uri, and kam gets in a loop of sending an ACK from its private ip address to its public ip address and replying to itself.

call flows: https://imgur.com/a/Yj2DJpn

Is there a right way to do this?