Skip to content

Instantly share code, notes, and snippets.

@mimura1133
Created May 16, 2021 20:01
Show Gist options
  • Save mimura1133/8a1204cf40970194ee8dd0be4c40742f to your computer and use it in GitHub Desktop.
Save mimura1133/8a1204cf40970194ee8dd0be4c40742f to your computer and use it in GitHub Desktop.
dlna_proxy.py
#!/bin/python3
#
# SSDP (DLNA の探索プロトコル) を吸って吐くプロキシ。
# Wireguard のように純粋にbridgeできないネットワーク間で使うと幸せになれる気がします。
# (使ったことでネットワークがダウン等しても保証は出来ませんので、自己責任でどうぞ。)
#
# Author : Satoshi Mimura (@mimura1133)
#
import socket
from contextlib import closing
def main():
multicast_group = '239.255.255.250'
src_adapter_ip = '' # DLNA クライアントがいるネットワークの IP アドレスを指定
dst_adapter_ip = '' # DLNA サーバがいるネットワークの IP アドレスを指定
port = 1900
timeout = 5.0
with closing(socket.socket(socket.AF_INET,socket.SOCK_DGRAM)) as src_sock:
src_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
src_sock.bind(('',port))
src_sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP,
socket.inet_aton(multicast_group) + socket.inet_aton(src_adapter_ip))
while True:
request_data, client_addr = src_sock.recvfrom(4096)
with closing(socket.socket(socket.AF_INET,socket.SOCK_DGRAM)) as dst_sock:
dst_sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF, socket.inet_aton(dst_adapter_ip))
dst_sock.settimeout(timeout)
dst_sock.sendto(request_data,(multicast_group,port))
print("\033[31m [FORWARDED:REQUEST]\033[0m {} -> {}".format(client_addr,(multicast_group,port)))
print(request_data)
while True:
try:
response_data, server_addr = dst_sock.recvfrom(4096)
src_sock.sendto(response_data,client_addr)
print("\033[32m [FORWARDED:RESPONSE]\033[0m {} -> {}".format(server_addr,client_addr))
print(response_data)
except Exception:
break
return
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment