Merge pull request #240 from mcarlton00/follow-redirects

Follow redirects when connecting to server
This commit is contained in:
Abby 2020-03-23 00:29:39 +00:00 committed by GitHub
commit 57582d234f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 13 deletions

View file

@ -412,7 +412,7 @@ class API(object):
return {} return {}
except Exception as e: # Find exceptions for likely cases i.e, server timeout, etc except Exception as e: # Find exceptions for likely cases i.e, server timeout, etc
LOG.error(e) LOG.error(e)
return {} return {}
@ -430,4 +430,13 @@ class API(object):
def get_public_info(self, server_address): def get_public_info(self, server_address):
response = self.send_request(server_address, "system/info/public") response = self.send_request(server_address, "system/info/public")
return response.json() if response.status_code == 200 else {} return response.json() if response.status_code == 200 else {}
def check_redirect(self, server_address):
''' Checks if the server is redirecting traffic to a new URL and
returns the URL the server prefers to use
'''
response = self.send_request(server_address, "system/info/public")
url = response.url.replace('/system/info/public', '')
return url

View file

@ -8,13 +8,13 @@ import logging
import socket import socket
import time import time
from datetime import datetime from datetime import datetime
from operator import itemgetter from operator import itemgetter
import urllib3 import urllib3
from .credentials import Credentials from .credentials import Credentials
from .http import HTTP # noqa: I201,I100 from .http import HTTP # noqa: I201,I100
from .api import API from .api import API
import traceback import traceback
################################################################################################# #################################################################################################
@ -68,7 +68,7 @@ class ConnectionManager(object):
servers = list(credentials['Servers']) servers = list(credentials['Servers'])
# Merges servers we already knew with newly found ones # Merges servers we already knew with newly found ones
for found_server in found_servers: for found_server in found_servers:
try: try:
self.credentials.add_update_server(servers, found_server) self.credentials.add_update_server(servers, found_server)
except KeyError: except KeyError:
@ -93,7 +93,7 @@ class ConnectionManager(object):
if not data: if not data:
LOG.info("Failed to login as `"+username+"`") LOG.info("Failed to login as `"+username+"`")
return {} return {}
LOG.info("Succesfully logged in as %s" % (username)) LOG.info("Succesfully logged in as %s" % (username))
# TODO Change when moving to database storage of server details # TODO Change when moving to database storage of server details
credentials = self.credentials.get() credentials = self.credentials.get()
@ -121,7 +121,7 @@ class ConnectionManager(object):
self.credentials.add_update_user(server, info) self.credentials.add_update_user(server, info)
self.credentials.set_credentials(credentials) self.credentials.set_credentials(credentials)
return data return data
@ -133,12 +133,13 @@ class ConnectionManager(object):
address = self._normalize_address(address) address = self._normalize_address(address)
try: try:
public_info = self.API.get_public_info(address) response_url = self.API.check_redirect(address)
if address != response_url:
address = response_url
LOG.info("connectToAddress %s succeeded", address) LOG.info("connectToAddress %s succeeded", address)
server = { server = {
'address': address, 'address': address,
} }
self._update_server_info(server, public_info)
server = self.connect_to_server(server, options) server = self.connect_to_server(server, options)
if server is False: if server is False:
LOG.error("connectToAddress %s failed", address) LOG.error("connectToAddress %s failed", address)
@ -164,6 +165,7 @@ class ConnectionManager(object):
LOG.info("calling onSuccessfulConnection with server %s", server.get('Name')) LOG.info("calling onSuccessfulConnection with server %s", server.get('Name'))
self._update_server_info(server, result)
credentials = self.credentials.get() credentials = self.credentials.get()
return self._after_connect_validated(server, credentials, result, True, options) return self._after_connect_validated(server, credentials, result, True, options)
@ -310,11 +312,11 @@ class ConnectionManager(object):
elif verify_authentication and server.get('AccessToken'): elif verify_authentication and server.get('AccessToken'):
system_info = self.API.validate_authentication_token(server) system_info = self.API.validate_authentication_token(server)
if system_info: if system_info:
self._update_server_info(server, system_info) self._update_server_info(server, system_info)
self.config.data['auth.user_id'] = server['UserId'] self.config.data['auth.user_id'] = server['UserId']
self.config.data['auth.token'] = server['AccessToken'] self.config.data['auth.token'] = server['AccessToken']
return self._after_connect_validated(server, credentials, system_info, False, options) return self._after_connect_validated(server, credentials, system_info, False, options)
server['UserId'] = None server['UserId'] = None
@ -341,7 +343,7 @@ class ConnectionManager(object):
result['State'] = CONNECTION_STATE['SignedIn'] if server.get('AccessToken') else CONNECTION_STATE['ServerSignIn'] result['State'] = CONNECTION_STATE['SignedIn'] if server.get('AccessToken') else CONNECTION_STATE['ServerSignIn']
# Connected # Connected
return result return result
def _update_server_info(self, server, system_info): def _update_server_info(self, server, system_info):
if server is None or system_info is None: if server is None or system_info is None:
@ -351,4 +353,4 @@ class ConnectionManager(object):
server['Id'] = system_info['Id'] server['Id'] = system_info['Id']
if system_info.get('address'): if system_info.get('address'):
server['address'] = system_info['address'] server['address'] = system_info['address']