forked from LBRYCommunity/lbry-sdk
parent
b6f9b60683
commit
2e921437d8
1 changed files with 30 additions and 17 deletions
|
@ -424,12 +424,18 @@ class DHTComponent(Component):
|
||||||
node_id = CS.get_node_id()
|
node_id = CS.get_node_id()
|
||||||
if node_id is None:
|
if node_id is None:
|
||||||
node_id = generate_id()
|
node_id = generate_id()
|
||||||
|
external_ip = self.upnp_component.external_ip
|
||||||
|
if not external_ip:
|
||||||
|
log.warning("UPnP component failed to get external ip")
|
||||||
|
external_ip = CS.get_external_ip()
|
||||||
|
if not external_ip:
|
||||||
|
log.warning("failed to get external ip")
|
||||||
|
|
||||||
self.dht_node = node.Node(
|
self.dht_node = node.Node(
|
||||||
node_id=node_id,
|
node_id=node_id,
|
||||||
udpPort=GCS('dht_node_port'),
|
udpPort=GCS('dht_node_port'),
|
||||||
externalUDPPort=self.external_udp_port,
|
externalUDPPort=self.external_udp_port,
|
||||||
externalIP=self.upnp_component.external_ip,
|
externalIP=external_ip,
|
||||||
peerPort=self.external_peer_port
|
peerPort=self.external_peer_port
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -710,33 +716,39 @@ class UPnPComponent(Component):
|
||||||
log.info("found upnp gateway: %s", self.upnp.gateway.manufacturer_string)
|
log.info("found upnp gateway: %s", self.upnp.gateway.manufacturer_string)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
log.warning("upnp discovery failed: %s", err)
|
log.warning("upnp discovery failed: %s", err)
|
||||||
return
|
self.upnp = None
|
||||||
|
|
||||||
# update the external ip
|
# update the external ip
|
||||||
|
external_ip = None
|
||||||
|
if self.upnp:
|
||||||
try:
|
try:
|
||||||
external_ip = yield from_future(self.upnp.get_external_ip())
|
external_ip = yield from_future(self.upnp.get_external_ip())
|
||||||
if external_ip == "0.0.0.0":
|
if external_ip != "0.0.0.0":
|
||||||
log.warning("upnp doesn't know the external ip address (returned 0.0.0.0), using fallback")
|
log.info("got external ip from UPnP: %s", external_ip)
|
||||||
external_ip = CS.get_external_ip()
|
|
||||||
if self.external_ip and self.external_ip != external_ip:
|
|
||||||
log.info("external ip changed from %s to %s", self.external_ip, external_ip)
|
|
||||||
elif not self.external_ip:
|
|
||||||
log.info("got external ip: %s", external_ip)
|
|
||||||
self.external_ip = external_ip
|
|
||||||
except (asyncio.TimeoutError, UPnPError):
|
except (asyncio.TimeoutError, UPnPError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if not self.upnp_redirects: # setup missing redirects
|
if external_ip == "0.0.0.0" or not external_ip:
|
||||||
|
log.warning("unable to get external ip from UPnP, checking lbry.io fallback")
|
||||||
|
external_ip = CS.get_external_ip()
|
||||||
|
if self.external_ip and self.external_ip != external_ip:
|
||||||
|
log.info("external ip changed from %s to %s", self.external_ip, external_ip)
|
||||||
|
self.external_ip = external_ip
|
||||||
|
assert self.external_ip is not None # TODO: handle going/starting offline
|
||||||
|
|
||||||
|
if not self.upnp_redirects and self.upnp: # setup missing redirects
|
||||||
try:
|
try:
|
||||||
|
log.info("add UPnP port mappings")
|
||||||
upnp_redirects = yield DeferredDict({
|
upnp_redirects = yield DeferredDict({
|
||||||
"UDP": from_future(self.upnp.get_next_mapping(self._int_dht_node_port, "UDP", "LBRY DHT port")),
|
"UDP": from_future(self.upnp.get_next_mapping(self._int_dht_node_port, "UDP", "LBRY DHT port")),
|
||||||
"TCP": from_future(self.upnp.get_next_mapping(self._int_peer_port, "TCP", "LBRY peer port"))
|
"TCP": from_future(self.upnp.get_next_mapping(self._int_peer_port, "TCP", "LBRY peer port"))
|
||||||
})
|
})
|
||||||
|
log.info("set up redirects: %s", upnp_redirects)
|
||||||
self.upnp_redirects.update(upnp_redirects)
|
self.upnp_redirects.update(upnp_redirects)
|
||||||
except (asyncio.TimeoutError, UPnPError):
|
except (asyncio.TimeoutError, UPnPError):
|
||||||
self.upnp = None
|
self.upnp = None
|
||||||
return self._maintain_redirects()
|
return self._maintain_redirects()
|
||||||
else: # check existing redirects are still active
|
elif self.upnp: # check existing redirects are still active
|
||||||
found = set()
|
found = set()
|
||||||
mappings = yield from_future(self.upnp.get_redirects())
|
mappings = yield from_future(self.upnp.get_redirects())
|
||||||
for mapping in mappings:
|
for mapping in mappings:
|
||||||
|
@ -767,6 +779,7 @@ class UPnPComponent(Component):
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def start(self):
|
def start(self):
|
||||||
|
log.info("detecting external ip")
|
||||||
if not self.use_upnp:
|
if not self.use_upnp:
|
||||||
self.external_ip = CS.get_external_ip()
|
self.external_ip = CS.get_external_ip()
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in a new issue