Unable to Access Audio Stream via IP on 4G Network through ZeroTier

I’m building an application to generate audio streams and broadcast them using an IP:Port format. My setup includes a regular fiber optic internet connection on my computer, which is also connected to ZeroTier. My phone is on a 4G network and is also connected to the same ZeroTier network as my computer.

However, I’m unable to connect to the IP:Port provided by the app from my phone over 4G. The connection only works if my phone is on the same WiFi network as the computer.

Here’s the code I’m using to get IP addresses and start the server:


python

    @staticmethod
    def get_local_ip():
        try:
            # First try to get all network interfaces
            import netifaces
            
            # Get all interfaces
            interfaces = netifaces.interfaces()
            
            # Priority order for interfaces
            # 1. ZeroTier (usually starts with zt)
            # 2. LAN interfaces
            # 3. Fallback to localhost
            
            for iface in interfaces:
                # Look for ZeroTier interface first
                if iface.startswith('zt'):
                    addrs = netifaces.ifaddresses(iface)
                    if netifaces.AF_INET in addrs:
                        return addrs[netifaces.AF_INET][0]['addr']
            
            # If no ZeroTier, try other interfaces
            for iface in interfaces:
                if not iface.startswith(('lo', 'zt')):  # Skip loopback and ZeroTier
                    addrs = netifaces.ifaddresses(iface)
                    if netifaces.AF_INET in addrs:
                        return addrs[netifaces.AF_INET][0]['addr']
                        
            # Fallback to old method
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.connect(('8.8.8.8', 1))
            IP = s.getsockname()[0]
            s.close()
            return IP
            
        except Exception as e:
            print(f"Error getting IP: {str(e)}")
            return '127.0.0.1'  # Fallback to localhost

    @staticmethod
    def get_network_ips():
        ips = []
        try:
            # Get all network interfaces
            interfaces = socket.getaddrinfo(socket.gethostname(), None)
            
            for interface in interfaces:
                ip = interface[4][0]
                # Filter for IPv4 addresses and exclude localhost
                if ':' not in ip and ip != '127.0.0.1':
                    ips.append(ip)
                    
            # Also add specific interface IPs (like Zerotier)
            import netifaces
            for interface in netifaces.interfaces():
                addrs = netifaces.ifaddresses(interface)
                # Get IPv4 addresses
                if netifaces.AF_INET in addrs:
                    for addr in addrs[netifaces.AF_INET]:
                        ip = addr['addr']
                        if ip not in ips and ip != '127.0.0.1':
                            ips.append(ip)
        except Exception as e:
            print(f"Error getting network IPs: {str(e)}")
            # Fallback to basic local IP
            try:
                s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                s.connect(('8.8.8.8', 1))
                local_ip = s.getsockname()[0]
                ips.append(local_ip)
                s.close()
            except Exception:
                ips.append('127.0.0.1')
                
        return ips

    async def start_server(self):
        self.app = web.Application()
        self.app.router.add_get('/audio', self.stream_audio)
        runner = web.AppRunner(self.app)
        await runner.setup()
        self.site = web.TCPSite(runner, '0.0.0.0', self.port)
        await self.site.start()
        
        # Get all available IPs
        network_ips = self.get_network_ips()
        
        # Create URLs for all IPs
        urls = [f"http://{ip}:{self.port}/audio" for ip in network_ips]
        urls.append(f"http://localhost:{self.port}/audio")  # Always add localhost
        
        print("Máy chủ đã khởi động. Thử truy cập các địa chỉ sau:")
        for url in urls:
            print(f"- {url}")
        
        # Emit all URLs for the GUI to display
        self.server_started.emit("\n".join(urls), f"http://localhost:{self.port}/audio")
    def run(self):
        print("Khởi động AudioStreamWorker")
        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(self.loop)
        try:
            self.loop.run_until_complete(self.start_server())
            self.loop.run_forever()
        except Exception as e:
            print(f"Lỗi trong AudioStreamWorker: {str(e)}")
        finally:
            print("Đang dọn dẹp AudioStreamWorker...")
            self.loop.run_until_complete(self.loop.shutdown_asyncgens())
            self.loop.close()
            print("AudioStreamWorker đã dọn dẹp xong")

Additional methods to fetch IP addresses and start the server

Would anyone have insights on why this is happening or how to make the IP
accessible over 4G through ZeroTier? Could it be related to ZeroTier’s configuration, or is there a specific network setting I’m missing?

Thank you for your help!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.