Giter Club home page Giter Club logo

fapfon-proxy's Introduction

fapfon-proxy

Workaround for FRITZ!App Fon SIP via VPN

FRITZ!Box and FRITZ!App Fon are trademarks of AVM Computersysteme Vertriebs GmbH, Berlin, Germany.

Table of contents

Introduction

This is a special proxy to use FRITZ!App Fon via VPN. It manages multiple simultaneous FRITZ!App Fon connections, provided that each uses a different FRITZ!Box phone device user name.

In my home network I already have a Raspberry Pi acting as OpenVPN server, so I thought it should be possible to use FRITZ!App Fon remotely via OpenVPN. It worked to a certain degree, the registration process succeeded, but I could not hear any audio on outgoing calls, and the Fon app did not ring on incoming calls.

If you are interested in the technical details, see below.

Installation

Install fapfon-proxy on the system where your VPN server runs. The tested configuration is Raspbian 9 (stretch) with gcc 6.3.0. You can run it on your Box with Freetz if this is where your VPN server is located.

The example below clones the repository into the /usr/local/src/fapfon-proxy directory:

cd /usr/local/src/
git clone https://github.com/rolandgenske/fapfon-proxy.git
cd fapfon-proxy/
make

The installation I suggest uses a systemd service which invokes the fapfon-proxy.nat script to setup/cleanup either port redirection or destination NAT before fapfon-proxy is started and after it is stopped.

Port redirection is used if you run fapfon-proxy on your Box. Destination NAT is used if you run fapfon-proxy on a separate system with your VPN server.

Now install the fapfon-proxy executable along with the scripts and the configuration file:

cp -p fapfon-proxy /usr/local/bin/
cp -p install/fapfon-proxy.nat /usr/local/bin/
cp -p install/fapfon-proxy.service /etc/systemd/system/
cp -p install/fapfon-proxy /etc/default/

Next, reload the systemd manager configuration and enable the fapfon-proxy service:

systemctl daemon-reload
systemctl enable fapfon-proxy.service

Before starting the service, edit the /etc/default/fapfon-proxy configuration. Change BOX= to the Fritz!Box address in your network, change VPN= to the address range(s) of your VPN clients. The configuration example shows my two separate address ranges, one for OpenVPN TCP clients and the other for UDP clients.

If you run fapfon-proxy on your Box you need to uncomment the SIP_REDIRECT_PORT= setting so that fapfon-proxy binds to TCP/UDP ports other than 5060 (sip). The configuration example uses port 6060, use a different port number if 6060 is already in use.

If you want to see connect/disconnect messages in the log file, configure OPTIONS="--verbose=2".

Then start the service:

systemctl start fapfon-proxy.service

Check the log file whether fapfon-proxy has been started. If you have OPTIONS="--verbose=2" configured you can use this to watch while your FRITZ!App Fon connects and disconnects:

tail -f /var/log/fapfon-proxy.log

To check how port redirection or destination NAT is set up, use:

iptables -t nat -L PREROUTING

Technical Details

This is a dump of the initial SIP REGISTER message FRITZ!App Fon sends to my Box. The involved addresses are:

  • 10.81.179.54 : private mobile phone provider address (Telekom), FRITZ!App Fon binds to TCP/UDP port 61211
  • 172.20.11.6 : address of OpenVPN endpoint on mobile phone, TCP message sent from port 62895
  • 172.30.10.1 : Box address in my network, TCP message sent to SIP port 5060
  • 172.30.10.2 : Address of the Raspberry Pi in my network, running the VPN server and fapfon-proxy
REGISTER sip:172.30.10.1;transport=TCP SIP/2.0\r\n
Via: SIP/2.0/TCP 172.20.11.6:61211;rport;branch=***;alias\r\n
Max-Forwards: 70\r\n
From: <sip:[email protected]>;tag=***\r\n
To: <sip:[email protected]>\r\n
Call-ID: ***\r\n
CSeq: 24441 REGISTER\r\n
User-Agent: FRITZ!AppFon/2549 sip/1.16.0\r\n
Supported: outbound, path\r\n
Contact: <sip:[email protected]:61211;transport=TCP;ob>;reg-id=1;+sip.instance="<urn:uuid:***>"\r\n
Expires: 900\r\n
Allow: PRACK, INVITE, ACK, BYE, CANCEL, UPDATE, INFO, SUBSCRIBE, NOTIFY, REFER, MESSAGE, OPTIONS\r\n
Content-Length:  0\r\n
\r\n

The main problem here is that Contact uses the private mobile phone provider address 10.81.179.54 which does not route back to FRITZ!App Fon via OpenVPN and furthermore cannot be reached on the Internet. This is the reason why incoming calls cannot be routed to the Fon app.

If this would be the originating 172.20.11.6 address with port 62895 all would be fine.

Next comes a dump of a SIP INVITE message FRITZ!App Fon sends to my Box to initiate an outgoing call. This time I focus on the SDP data only:

INVITE sip:[email protected];transport=TCP SIP/2.0\r\n
...
Content-Type: application/sdp\r\n
Content-Length:   295\r\n
\r\n
v=0\r\n
o=- 3727522826 3727522826 IN IP4 10.81.179.54\r\n
s=pjmedia\r\n
c=IN IP4 10.81.179.54\r\n
t=0 0\r\n
a=X-nat:0\r\n
m=audio 4000 RTP/AVP 8 0 3 101\r\n
a=rtcp:4001 IN IP4 10.81.179.54\r\n
a=rtpmap:8 PCMA/8000\r\n
a=rtpmap:0 PCMU/8000\r\n
a=rtpmap:3 GSM/8000\r\n
a=sendrecv\r\n
a=rtpmap:101 telephone-event/8000\r\n
a=fmtp:101 0-15\r\n

Here, the originator / session identifier o= and the connection info c= as well as the RTP attribute a=rtcp all refer to the private mobile phone provider address 10.81.179.54, which is why RTP audio does not route back to FRITZ!App Fon via OpenVPN. The Box routes it to the Internet where it gets lost because the private 10.81.179.54 address cannot be reached.

So the goal is to modify the messages, which is what fapfon-proxy does. In the example above it replaces the 10.81.179.54[:61211] address in the SIP header with the fapfon-proxy local address and TCP/UDP port, so that SIP responses go back through the proxy, which then reverts the address replacement before the message is sent to the Fon app.

Furthermore, in SDP data it replaces the 10.81.179.54 address with the FRITZ!App Fon OpenVPN endpoint address (172.20.11.6 in this example) so that RTP audio goes directly to the Fon app, no need to send it through the proxy.

For messages from FRITZ!App Fon we do not touch the SIP Via header line, but on its way back the Box has added the rport field (RFC 3581) using the fapfon-proxy local TCP/UDP port:

Via: SIP/2.0/TCP 172.20.11.6:61211;rport=43202;branch=***;alias;received=172.30.10.2\r\n

Before sending this to the Fon app the rport is replaced with the original Via port, otherwise FRITZ!App Fon won't accept this message:

Via: SIP/2.0/TCP 172.20.11.6:61211;rport=61211;branch=***;alias;received=172.30.10.2\r\n

The USERNAME identifier obtained in the initial SIP REGISTER message is used to manage individual address/port replacement for multiple simultaneous connections from different devices. Set --verbose=3 to see this in the log.

Command Line Usage

usage: fapfon-proxy [options] BOX_ADDRESS[:SIP_PORT]
SIP_PORT default: 5060
options:
  -h            --help             This list
  -p PORT       --port=PORT        Server SIP_PORT, TCP and UDP
  -t PORT       --tcp-port=PORT    Server SIP_PORT, TCP
  -u PORT       --udp-port=PORT    Server SIP_PORT, UDP
  -v [level]    --verbose[=level]  Verbosity 0:ERROR 1:INFO 2:DETAIL 3:VERBOSE
  -l LOGFILE    --logfile=LOGFILE  Log file or - (stdout), default: stderr
  -D {FON|BOX}  --dump={FON|BOX}   Dump FON/BOX messages to stdout
  -V            --version          Version information

Copyright and license

(C) 2018 by Roland Genske. Code released under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. Please refer to the file COPYING for details.

fapfon-proxy's People

Contributors

rolandgenske avatar soulianis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

liphis

fapfon-proxy's Issues

Problems with incoming call

Hello,

at first: thanks for this great project. Wondering why it hasn't got more attention yet.
I've setup it in order to use Fritz!App Fon over a Wireguard VPN. Outgoing calls now work fine, but I'm struggling with incoming calls. They don't reach the Fritz!App Fon. According to the tcpdump logs below, it seems like the NOTIFY and INVITE SIP-messages have the wrong destination IP-Address.

Addresses

IPv4 Host
10.5.1.51 VPN Server
10.5.1.11 Fritz!Box
10.5.10.10 Fritz!App Fon vpn address
192.168.1.101 Fritz!App Fon local address

Logs

fapfon-proxy
Apr 01 16:47:01 wireguard fapfon-proxy[7822]: 200401 164701 V3 Box address 10.5.1.11:5060
Apr 01 16:47:01 wireguard fapfon-proxy[7822]: 200401 164701 V3 TCP: Server SIP port 5060
Apr 01 16:47:01 wireguard fapfon-proxy[7822]: 200401 164701 V3 UDP: Server SIP port 5060
Apr 01 16:47:01 wireguard fapfon-proxy[7822]: 200401 164701 V1 Start fapfon-proxy version 0.3.1221
Apr 01 16:47:31 wireguard fapfon-proxy[7822]: 200401 164731 V3 [1] Connect 10.5.10.10:59484/udp, contact 'JulnsiPhone_WIGI45ak'
REGISTER, SUBSCRIBE, NOTIFY tcpdump
IP 10.5.1.51.59741 > 10.5.1.11.5060: UDP, length 563
E..O.V@[email protected].
..3
....]...;..REGISTER sip:fritz.box SIP/2.0
Via: SIP/2.0/UDP 192.168.1.101:63346;rport;branch=z9hG4bKPjUh8D15qIqUsW7IFIVwvOCClSqsT.5jsU
Max-Forwards: 70
From: <sip:[email protected]>;tag=Wek6GoxtDZR92wpGs6MDK9Ye7ocjvBUn
To: <sip:[email protected]>
Call-ID: vnZTMwW3Cg0CynbSbFkHAvAyEHlSUtIe
CSeq: 2013 REGISTER
User-Agent: FRITZ!AppFon/2460 sip/2.8
Contact: <sip:[email protected]:63346;ob>
Expires: 3600
Allow: PRACK, INVITE, ACK, BYE, CANCEL, UPDATE, INFO, SUBSCRIBE, NOTIFY, REFER, MESSAGE, OPTIONS
Content-Length: 0


IP 10.5.1.11.5060 > 10.5.1.51.59741: UDP, length 465
E.......@...
...
..3...]....SIP/2.0 401 Unauthorized
Via: SIP/2.0/UDP 192.168.1.101:63346;rport=59741;branch=z9hG4bKPjUh8D15qIqUsW7IFIVwvOCClSqsT.5jsU;received=10.5.1.51
From: <sip:[email protected]>;tag=Wek6GoxtDZR92wpGs6MDK9Ye7ocjvBUn
To: <sip:[email protected]>;tag=584AD20568DEA182
Call-ID: vnZTMwW3Cg0CynbSbFkHAvAyEHlSUtIe
CSeq: 2013 REGISTER
WWW-Authenticate: <redacted>
User-Agent: FRITZ!OS
Content-Length: 0


IP 10.5.1.51.59741 > 10.5.1.11.5060: UDP, length 725
E....Y@[email protected][
..3
....].....gREGISTER sip:fritz.box SIP/2.0
Via: SIP/2.0/UDP 10.5.1.51:63346;rport;branch=z9hG4bKPj3yC9S0Zy-lL8Ka9Y2FpmE7xo11JjVaW1
Max-Forwards: 70
From: <sip:[email protected]>;tag=Wek6GoxtDZR92wpGs6MDK9Ye7ocjvBUn
To: <sip:[email protected]>
Call-ID: vnZTMwW3Cg0CynbSbFkHAvAyEHlSUtIe
CSeq: 2014 REGISTER
User-Agent: FRITZ!AppFon/2460 sip/2.8
Contact: <sip:[email protected]:63346;ob>
Expires: 3600
Allow: PRACK, INVITE, ACK, BYE, CANCEL, UPDATE, INFO, SUBSCRIBE, NOTIFY, REFER, MESSAGE, OPTIONS
Authorization: <redacted>
Content-Length: 0


IP 10.5.1.11.5060 > 10.5.1.51.59741: UDP, length 854
E..r....@...
...
..3...].^.LSIP/2.0 200 OK
Via: SIP/2.0/UDP 10.5.1.51:63346;rport=59741;branch=z9hG4bKPj3yC9S0Zy-lL8Ka9Y2FpmE7xo11JjVaW1
From: <sip:[email protected]>;tag=Wek6GoxtDZR92wpGs6MDK9Ye7ocjvBUn
To: <sip:[email protected]>;tag=CAEA059669F625E9
Call-ID: vnZTMwW3Cg0CynbSbFkHAvAyEHlSUtIe
CSeq: 2014 REGISTER
Contact: <sip:[email protected]:64053;ob>;expires=2468
Contact: <sip:[email protected]:59484;ob>;expires=3009
Contact: <sip:[email protected]:63346;ob>;expires=3600
User-Agent: AVM FRITZ!Box 7490 113.07.12 (Jul  3 2019)
Supported: 100rel,replaces,timer
Allow-Events: telephone-event,refer,reg
Allow: INVITE,ACK,OPTIONS,CANCEL,BYE,UPDATE,PRACK,INFO,SUBSCRIBE,NOTIFY,REFER,MESSAGE,PUBLISH
Accept: application/sdp, multipart/mixed
Accept-Encoding: identity
Content-Length: 0


IP 10.5.1.51.59741 > 10.5.1.11.5060: UDP, length 649
E....`@[email protected].
..3
....]......SUBSCRIBE sip:[email protected] SIP/2.0
Via: SIP/2.0/UDP 10.5.1.51:63346;rport;branch=z9hG4bKPjkCp4PT3SDJLWE1rjbUPp0cgp51Fk4U05
Max-Forwards: 70
From: <sip:[email protected]>;tag=FI3m7cFpVlduMY0SDxyL4uNMhFDrxeZ3
To: <sip:[email protected]>
Contact: <sip:[email protected]:63346;ob>
Call-ID: I8QKBFAzYIvNuN8PKQFUmx6DZoZlgz7E
CSeq: 26407 SUBSCRIBE
Event: message-summary
Expires: 3600
Supported: replaces, 100rel, timer, norefersub
Accept: application/simple-message-summary
Allow-Events: presence, message-summary, refer
User-Agent: FRITZ!AppFon/2460 sip/2.8
Content-Length: 0


IP 10.5.1.11.5060 > 10.5.1.51.59741: UDP, length 444
E.......@...
...
..3...]....SIP/2.0 401 Unauthorized
Via: SIP/2.0/UDP 10.5.1.51:63346;rport=59741;branch=z9hG4bKPjkCp4PT3SDJLWE1rjbUPp0cgp51Fk4U05
From: <sip:[email protected]>;tag=FI3m7cFpVlduMY0SDxyL4uNMhFDrxeZ3
To: <sip:[email protected]>;tag=661836827C13CA6A
Call-ID: I8QKBFAzYIvNuN8PKQFUmx6DZoZlgz7E
CSeq: 26407 SUBSCRIBE
WWW-Authenticate: <redacted>
User-Agent: FRITZ!OS
Content-Length: 0


IP 10.5.1.51.59741 > 10.5.1.11.5060: UDP, length 836
E..`.d@[email protected].
..3
....]...L..SUBSCRIBE sip:[email protected] SIP/2.0
Via: SIP/2.0/UDP 10.5.1.51:63346;rport;branch=z9hG4bKPj2Sp4DhU52t-cksw9Amk4JMBgIdzmrG.N
Max-Forwards: 70
From: <sip:[email protected]>;tag=FI3m7cFpVlduMY0SDxyL4uNMhFDrxeZ3
To: <sip:[email protected]>
Contact: <sip:[email protected]:63346;ob>
Call-ID: I8QKBFAzYIvNuN8PKQFUmx6DZoZlgz7E
CSeq: 26408 SUBSCRIBE
Event: message-summary
Expires: 3600
Supported: replaces, 100rel, timer, norefersub
Accept: application/simple-message-summary
Allow-Events: presence, message-summary, refer
User-Agent: FRITZ!AppFon/2460 sip/2.8
Authorization: <redacted>
Content-Length: 0


IP 10.5.1.11.5060 > 10.5.1.51.59741: UDP, length 413
E.......@...
...
..3...]....SIP/2.0 200 OK
Via: SIP/2.0/UDP 10.5.1.51:63346;rport=59741;branch=z9hG4bKPj2Sp4DhU52t-cksw9Amk4JMBgIdzmrG.N
From: <sip:[email protected]>;tag=FI3m7cFpVlduMY0SDxyL4uNMhFDrxeZ3
To: <sip:[email protected]>;tag=EDB7DB8D63E63FB4
Call-ID: I8QKBFAzYIvNuN8PKQFUmx6DZoZlgz7E
CSeq: 26408 SUBSCRIBE
Expires: 3600
User-Agent: AVM FRITZ!Box 7490 113.07.12 (Jul  3 2019)
Content-Length: 0


IP 10.5.1.11.5060 > 192.168.1.101.63346: UDP, length 627
E.......@...
......e...r.{.nNOTIFY sip:[email protected]:63346;ob SIP/2.0
Via: SIP/2.0/UDP 10.5.1.11:5060;branch=z9hG4bK38EBE33FB3E47D6E
From: <sip:[email protected]>;tag=EDB7DB8D63E63FB4
To: <sip:[email protected]>;tag=FI3m7cFpVlduMY0SDxyL4uNMhFDrxeZ3
Call-ID: I8QKBFAzYIvNuN8PKQFUmx6DZoZlgz7E
CSeq: 26409 NOTIFY
Contact: <sip:[email protected]>
Event: message-summary
Subscription-State: active;expires=3600
Max-Forwards: 70
User-Agent: AVM FRITZ!Box 7490 113.07.12 (Jul  3 2019)
Content-Type: application/simple-message-summary
Content-Length:    22

Messages-Waiting: no
INVITE tcpdump
IP 10.5.1.11.5060 > 192.168.1.101.64053: UDP, length 1191
[email protected]
......e...5....INVITE sip:[email protected]:64053;ob SIP/2.0
Via: SIP/2.0/UDP 10.5.1.11:5060;branch=z9hG4bK44DCAD7B042159A1
From: "Julian Neureuther" <sip:[email protected]>;tag=2E72CAD7107ABBB3
To: <sip:[email protected]:64053;ob>
Call-ID: [email protected]
CSeq: 8 INVITE
Contact: <sip:[email protected]>
Max-Forwards: 70
P-Called-Party-ID: <sip:[email protected]>
Expires: 120
Session-Expires: 600;refresher=uac
Min-SE: 90
User-Agent: AVM FRITZ!Box 7490 113.07.12 (Jul  3 2019)
Supported: 100rel,replaces,timer
Allow-Events: telephone-event,refer
Allow: INVITE,ACK,OPTIONS,CANCEL,BYE,UPDATE,PRACK,INFO,SUBSCRIBE,NOTIFY,REFER,MESSAGE,PUBLISH
Content-Type: application/sdp
Accept: application/sdp, multipart/mixed
Accept-Encoding: identity
Content-Length:   351

v=0
o=user 9358401 9358401 IN IP4 10.5.1.11
s=call
c=IN IP4 10.5.1.11
t=0 0
m=audio 7082 RTP/AVP 8 0 2 102 100 99 97 101
a=sendrecv
a=rtpmap:2 G726-32/8000
a=rtpmap:102 G726-32/8000
a=rtpmap:100 G726-40/8000
a=rtpmap:99 G726-24/8000
a=rtpmap:97 iLBC/8000
a=fmtp:97 mode=30
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-15
a=rtcp:7083

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.