Mercurial > libervia-backend
comparison sat/plugins/plugin_misc_ip.py @ 3132:b64dd7c1496d
plugin ip: fixed IP detection with external website
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 27 Jan 2020 19:53:31 +0100 |
parents | ab2696e34d29 |
children | 9d0df638c8b4 |
comparison
equal
deleted
inserted
replaced
3131:d6da17f6e4ce | 3132:b64dd7c1496d |
---|---|
162 return not ip_addr.startswith("127.") | 162 return not ip_addr.startswith("127.") |
163 | 163 |
164 def _insertFirst(self, addresses, ip_addr): | 164 def _insertFirst(self, addresses, ip_addr): |
165 """Insert ip_addr as first item in addresses | 165 """Insert ip_addr as first item in addresses |
166 | 166 |
167 @param addresses(list): list of IP addresses | |
167 @param ip_addr(str): IP addresse | 168 @param ip_addr(str): IP addresse |
168 @param addresses(list): list of IP addresses | |
169 """ | 169 """ |
170 if ip_addr in addresses: | 170 if ip_addr in addresses: |
171 if addresses[0] != ip_addr: | 171 if addresses[0] != ip_addr: |
172 addresses.remove(ip_addr) | 172 addresses.remove(ip_addr) |
173 addresses.insert(0, ip_addr) | 173 addresses.insert(0, ip_addr) |
174 else: | 174 else: |
175 addresses.insert(0, ip_addr) | 175 addresses.insert(0, ip_addr) |
176 | 176 |
177 def _getIPFromExternal(self, ext_url): | 177 async def _getIPFromExternal(self, ext_url): |
178 """Get local IP by doing a connection on an external url | 178 """Get local IP by doing a connection on an external url |
179 | 179 |
180 @param ext_utl(str): url to connect to | 180 @param ext_utl(str): url to connect to |
181 @return (D(str)): return local IP | 181 @return (str, None): return local IP, or None if it's not possible |
182 """ | 182 """ |
183 url = urllib.parse.urlparse(ext_url) | 183 url = urllib.parse.urlparse(ext_url) |
184 port = url.port | 184 port = url.port |
185 if port is None: | 185 if port is None: |
186 if url.scheme == "http": | 186 if url.scheme == "http": |
187 port = 80 | 187 port = 80 |
188 elif url.scheme == "https": | 188 elif url.scheme == "https": |
189 port = 443 | 189 port = 443 |
190 else: | 190 else: |
191 log.error("Unknown url scheme: {}".format(url.scheme)) | 191 log.error("Unknown url scheme: {}".format(url.scheme)) |
192 defer.returnValue(None) | 192 return None |
193 if url.hostname is None: | 193 if url.hostname is None: |
194 log.error("Can't find url hostname for {}".format(GET_IP_PAGE)) | 194 log.error("Can't find url hostname for {}".format(GET_IP_PAGE)) |
195 | 195 |
196 point = endpoints.TCP4ClientEndpoint(reactor, url.hostname, port) | 196 point = endpoints.TCP4ClientEndpoint(reactor, url.hostname, port) |
197 | 197 |
198 def gotConnection(p): | 198 p = await endpoints.connectProtocol(point, protocol.Protocol()) |
199 local_ip = p.transport.getHost().host | 199 local_ip = p.transport.getHost().host |
200 p.transport.loseConnection() | 200 p.transport.loseConnection() |
201 return local_ip | 201 return local_ip |
202 | |
203 d = endpoints.connectProtocol(point, protocol.Protocol()) | |
204 d.addCallback(gotConnection) | |
205 return d | |
206 | 202 |
207 @defer.inlineCallbacks | 203 @defer.inlineCallbacks |
208 def getLocalIPs(self, client): | 204 def getLocalIPs(self, client): |
209 """Try do discover local area network IPs | 205 """Try do discover local area network IPs |
210 | 206 |
253 | 249 |
254 if not allow_get_ip: | 250 if not allow_get_ip: |
255 defer.returnValue(addresses or localhost) | 251 defer.returnValue(addresses or localhost) |
256 | 252 |
257 try: | 253 try: |
258 ip_tuple = yield self._getIPFromExternal(GET_IP_PAGE) | 254 local_ip = yield defer.ensureDeferred(self._getIPFromExternal(GET_IP_PAGE)) |
259 except (internet_error.DNSLookupError, internet_error.TimeoutError): | 255 except (internet_error.DNSLookupError, internet_error.TimeoutError): |
260 log.warning("Can't access Domain Name System") | 256 log.warning("Can't access Domain Name System") |
261 defer.returnValue(addresses or localhost) | 257 else: |
262 self._insertFirst(addresses, ip_tuple.local) | 258 if local_ip is not None: |
263 defer.returnValue(addresses) | 259 self._insertFirst(addresses, local_ip) |
260 | |
261 defer.returnValue(addresses or localhost) | |
264 | 262 |
265 @defer.inlineCallbacks | 263 @defer.inlineCallbacks |
266 def getExternalIP(self, client): | 264 def getExternalIP(self, client): |
267 """Try to discover external IP | 265 """Try to discover external IP |
268 | 266 |