How to use the ICE protocol

1.7k Views Asked by At

I'm trying to make a connection between two computers behind NAT. I have a third computer that is accessible by those two.

I would like to use the ICE (Interactive Connectivity Establishment) protocol, but I can't find any example in Python. I heard about pjsip, which include a C library called pjnath, but that is also written in C.

Are there any tools to make it in Python? If not, is there another way to do what I described? If not, how to start ICE protocol in Python?

2

There are 2 best solutions below

0
On

PjSIP has a python module which you can use.

You can find the details and link to required tutorials here.

0
On

You can use the following Python-only library to establish your ICE connection:

https://github.com/jlaine/aioice

Here is an example of two ICE endpoints in the same process. In real life you need some signaling method to exchange the candidates, username ("ufrag") and password ("pwd") between the two.

import asyncio

import aioice


async def connect_using_ice():
    conn_a = aioice.Connection(ice_controlling=True)
    conn_b = aioice.Connection(ice_controlling=False)

    # invite
    await conn_a.gather_candidates()
    conn_b.remote_candidates = conn_a.local_candidates
    conn_b.remote_username = conn_a.local_username
    conn_b.remote_password = conn_a.local_password

    # accept
    await conn_b.gather_candidates()
    conn_a.remote_candidates = conn_b.local_candidates
    conn_a.remote_username = conn_b.local_username
    conn_a.remote_password = conn_b.local_password

    # connect
    await asyncio.gather(conn_a.connect(), conn_b.connect())

    # send data a -> b
    await conn_a.send(b'howdee')
    data = await conn_b.recv()
    print('B got', data)

    # send data b -> a
    await conn_b.send(b'gotcha')
    data = await conn_a.recv()
    print('A got', data)

    # close
    await asyncio.gather(conn_a.close(), conn_b.close())


asyncio.get_event_loop().run_until_complete(connect_using_ice())