Skip to content
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

discovery service: dynamically replace 0.0.0.0 endpointUrl in server response #687

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 48 additions & 13 deletions opcua/server/internal_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
"""

from datetime import datetime, timedelta
from copy import copy
import pickle
import os
import logging
from threading import Lock
from enum import Enum
from socket import INADDR_ANY # IPv4 '0.0.0.0'
IN6ADDR_ANY = '::'
from ipaddress import ip_address
try:
from urllib.parse import urlparse
except ImportError:
Expand Down Expand Up @@ -168,19 +171,51 @@ def add_endpoint(self, endpoint):

def get_endpoints(self, params=None, sockname=None):
self.logger.info("get endpoint")
if sockname:
# return to client the ip address it has access to
edps = []
for edp in self.endpoints:
edp1 = copy(edp)
url = urlparse(edp1.EndpointUrl)
url = url._replace(netloc=sockname[0] + ":" + str(sockname[1]))
edp1.EndpointUrl = url.geturl()
edps.append(edp1)
return edps
return self.endpoints[:]
# return to client the endpoints it has access to
edps = pickle.loads(pickle.dumps(self.endpoints))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is that? why dumping then loading again?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shallow copy() does not copy the endpoints, so either deepcopy() or pickle...

netloc = self._get_netloc(params, sockname)
return [self._endpoint_replace_inaddr_any(edp, netloc) for edp in edps]

@staticmethod
def _get_netloc(params=None, sockname=None):
# find the ip:port as seen by our client.
netloc = None
if params and params.EndpointUrl:
# use ip:port as provided within client request params.
netloc = urlparse(params.EndpointUrl).netloc
if not netloc and sockname:
# use ip:port extracted from our local interface.
netloc = sockname[0] + ":" + str(sockname[1])
return netloc

@staticmethod
def _endpoint_replace_inaddr_any(edp, netloc):
edp.EndpointUrl = InternalServer._replace_inaddr_any(edp.EndpointUrl, netloc)
return edp

@staticmethod
def _replace_inaddr_any(urlStr, netloc):
# If urlStr is '0.0.0.0:port' or '[::]:port', use netloc ip:port.
parseResult = urlparse(urlStr)
try:
hostip = ip_address(parseResult.hostname)
except ValueError:
hostip = None
if not (hostip and netloc):
pass
elif ip_address(hostip) in (ip_address(INADDR_ANY), ip_address(IN6ADDR_ANY)):
urlStr = parseResult._replace(netloc=netloc).geturl()
return urlStr

def find_servers(self, params):
servers = self._filter_servers(params)
servers = pickle.loads(pickle.dumps(servers))
netloc = self._get_netloc(params)
for srv in servers:
srv.DiscoveryUrls = [self._replace_inaddr_any(url, netloc) for url in srv.DiscoveryUrls]
return servers

def _filter_servers(self, params):
if not params.ServerUris:
return [desc.Server for desc in self._known_servers.values()]
servers = []
Expand Down Expand Up @@ -311,7 +346,7 @@ def create_session(self, params, sockname=None):
result.MaxRequestMessageSize = 65536
self.nonce = utils.create_nonce(32)
result.ServerNonce = self.nonce
result.ServerEndpoints = self.get_endpoints(sockname=sockname)
result.ServerEndpoints = self.get_endpoints(params=params, sockname=sockname)

return result

Expand Down