-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix ONVIF Camera Connectivity Behind NAT and Routers #15886
base: dev
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for frigate-docs canceled.
|
Because we recently updated the base image and python version for 0.16, we plan to upgrade to |
See #15894 |
@tibacher thanks for the PR I saw your comment. However this isn't enough to work with frigate. I had to override the transport. This is what worked for me on the latest version on frigate. Even though I didn't get the time to make a proper PR because I don't have the time, and still need to find a way to write a test for it before I can submit it. def contains_ip_port(s):
# Define a regex pattern for IP:port
ip_port_pattern = re.compile(
r'(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+\b)'
)
# Search for the pattern in the string
if ip_port_pattern.search(s):
return True
return False
def update_links(data, xaddr):
if isinstance(data, dict):
for key, value in data.items():
data[key] = update_links(value, xaddr)
elif isinstance(data, list):
for index, item in enumerate(data):
data[index] = update_links(item, xaddr)
elif isinstance(data, str):
if contains_ip_port(data):
_, bad_addr = data.split('://')
new_addr = str(xaddr) + '/' + bad_addr.split('/', 1)[1]
return new_addr
return data
class AddressOverrideTransport(Transport):
def __init__(self, xaddr, *args, **kwargs):
super().__init__(*args, **kwargs)
self.override_xaddr = xaddr
def post(self, address, message, headers):
response = super().post(address, message, headers)
json_resp = xmltodict.parse(response.text)
json_resp=update_links(json_resp, self.override_xaddr)
response._content = xmltodict.unparse(json_resp, pretty=False).encode('utf-8')
return response Then in class init I would have this: @safe_func
def __init__(self, xaddr, user, passwd, url,
encrypt=True, daemon=False, zeep_client=None, no_cache=False,
portType=None, dt_diff=None, binding_name='', transport=None):
if not os.path.isfile(url):
raise ONVIFError('%s doesn`t exist!' % url)
self.url = url
self.xaddr = xaddr
self.override_host = None
self.override_port = None
session = Session()
if transport is None:
self.transport = AddressOverrideTransport(self.xaddr, session=session) |
Hi @DrissiReda did you Tests my solution? If Yes which error / logs do you get. Because for me it worked after upgrading the onvif-zeep package for cameras behind a router with NAT forewarding.
Thanks @hawkeye217 I'll look into that but currently I have no time. |
I tested it on 0.15. Frigate shows that it fails to connect to the local address despite using the natted one in configuration. And with further debugging I realized that with your fix. If one wanted to create a new service using the python library the ips returned are all local. from onvif import ONVIFCamera
cam = ONVIFCamera('dns.example.com', 8120, 'user', 'pass')
cam.update_xaddrs() # we have natted addresses and ports after this
event = cam.create_events_service()
print(event.xaddr) # this will however be a local address And it's the same for other |
Ok as I mentioned, my fix is for 0.14.1. Maybe that it is not working for 0.15.* |
Proposed change
This pull request addresses an issue with ONVIF cameras located behind NAT (Network Address Translation) routers in Frigate. When queried, these cameras return their private IP addresses and internal ports, making it impossible to access them externally. This issue is particularly relevant for users with cameras installed behind routers.
The solution involves rewriting the
xaddrs
in the ONVIF response to utilize the external IP address and port provided during the connection setup, a method already implemented in thepython-onvif-zeep
package.I have tested this solution successfully in Frigate version 14.1, ensuring that it works as expected for cameras behind routers. This update will greatly improve connectivity in NAT environments and ensure that users can access their cameras from outside the local network.
Type of change
Additional information
Checklist
ruff format frigate
)Sorry had no time sofar to formt with ruff and run the local test pass.