Homeassistant on Virtual Machine, Forward mDNS Traffic for HomeKit

Firstly, hello guys!

Secondly, this is my first topic, so please be lenient with me. I did tried to search but sadly found no solution and am searching for one for some time now.

Environment: I am running a docker hassio homeassistant(latest version) on a host running an windows 10 within a virtualbox(ubuntu) w/o host network(cannot activate it due to host permissions) and trying to link shelly devices with Apple Home App by using the “ios” integration.

Problem: the shelly devices are somewhat discovered(by using scripts) but disconnected. My assumption is that since I am using a virtual machine the mDNS discovery doesn’t work due to 2 networks(host + virtual).

I tried using the below scripts(not mine, cudos to the developer!) to forward the traffic, it worked for the first discovery, but then the devices were shown as “disconnected”.

I have only some basic networking knowledge, but it should be possible to somehow bidirectionally forward the traffic from one network to another one. Does anyone have an idea how I can achieve this? Thanks!

script 1

import socket
import struct
import sys
import os
import time

UDP_PUBLISH_PORT = 7001
UDP_PUBLISH_IP = sys.argv[1]

UDP_LISTEN_PORT = 5353
UDP_LISTEN_IP = "224.0.0.251"

print('FORWARDING ', UDP_LISTEN_IP, ":", str(UDP_LISTEN_PORT), " TO ", UDP_PUBLISH_IP, ":", str(UDP_PUBLISH_PORT))
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('0.0.0.0', UDP_LISTEN_PORT))
mreq = struct.pack("4sl", socket.inet_aton(UDP_LISTEN_IP), socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)

while True:
  try:
    #Receive CoAP message
    data, addr = sock.recvfrom(10240)
    #Tag and add device ip-address to message
    newdata = bytearray(b'prxy')
    newdata.extend(socket.inet_aton(addr[0]))
    newdata.extend(data)
    #Send to Shelly plugin
    print(data)
    sock.sendto(data, (UDP_PUBLISH_IP, UDP_PUBLISH_PORT))
    time.sleep(2)
  except Exception as e:
    print ('exception ' + str(e))

script 2

import socket
import struct
import sys
import os
import time

UDP_PUBLISH_PORT = 5353
UDP_PUBLISH_IP = "224.0.0.251"

UDP_LISTEN_PORT = 7001
UDP_LISTEN_IP = sys.argv[1]

print('FORWARDING ', UDP_LISTEN_IP, ":", str(UDP_LISTEN_PORT), " TO ", UDP_PUBLISH_IP, ":", str(UDP_PUBLISH_PORT))
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((UDP_LISTEN_IP, UDP_LISTEN_PORT))

while True:
  try:
    #Receive message
    data, addr = sock.recvfrom(10240)
    #Send it
    print(data)
    sock.sendto(data, (UDP_PUBLISH_IP, UDP_PUBLISH_PORT))
    time.sleep(2)
  except Exception as e:
    print ('exception ' + str(e))