def findGatewayConnection(self): from baboossh.path import Path from baboossh.connection import Connection if not Path.hasDirectPath(self): paths = Path.getPath(None, self) if paths is None: return None else: prevHop = paths[-1].getSrc().getClosestEndpoint() return Connection.findWorkingByEndpoint(prevHop) return None
def getClosestEndpoint(self): from baboossh.path import Path endpoints = self.getEndpoints() shortestLen = None shortest = None for endpoint in endpoints: if Path.hasDirectPath(endpoint): return endpoint chain = Path.getPath(None, endpoint) if shortestLen is None or len(chain) < shortestLen: shortest = endpoint shortestLen = len(chain) return shortest
def threadConnect(self,verbose,endpoint,users,creds): try: loop = asyncio.get_event_loop() except: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) c = dbConn.get() if Path.hasDirectPath(endpoint): gw = None else: gateway = endpoint.findGatewayConnection() if gateway is not None: if verbose: print("Connecting to gateway "+str(gateway)+" to reach "+str(endpoint)+"...") gw = gateway.initConnect(verbose=verbose) else: gw = None workingQueue = [] dunnoQueue = [] for user in users: for cred in creds: connection = Connection(endpoint,user,cred) if connection.isWorking(): workingQueue.append(connection) else: dunnoQueue.append(connection) queue = workingQueue + dunnoQueue for connection in queue: try: working = connection.testConnect(gw,verbose=True) except: print("Due to timeout, subsequent connections to endpoint will be ignored.") break if working: break if gw is not None: gw.close() dbConn.close()
def findPath(self,dst): #DST is HOST #if dst in self.getHostsNames(): # hosts = Host.findByName(dst) # if len(hosts) > 1: # print("Several hosts corresponding. Please target endpoint.") # return False # dst = str(hosts[0].getClosestEndpoint()) try: dst = Endpoint.findByIpPort(dst) except: print("Please specify a valid endpoint in the IP:PORT form") return if dst is None: print("The endpoint provided doesn't exist in this workspace") return if Path.hasDirectPath(dst): print("The destination should be reachable directly from the host.") return workingDirect = dst.scan(gateway=None,silent=True) if workingDirect: p = Path(None,dst) p.save() print("Could reach target directly, path added.") return for h in Path.getHostsOrderedClosest(): e = h.getClosestEndpoint() gateway = Connection.findWorkingByEndpoint(e) working = dst.scan(gateway=gateway,silent=True) if working: p = Path(h,dst) p.save() print("Working with gw "+str(e)+" (host "+str(h)+")") return return
def getPathToDst(self,dst): if dst in self.getHostsNames(): hosts = Host.findByName(dst) if len(hosts) > 1: print("Several hosts corresponding. Please target endpoint.") return False dst = str(hosts[0].getClosestEndpoint()) try: dst = Endpoint.findByIpPort(dst) except: print("Please specify a valid endpoint in the IP:PORT form") return if dst is None: print("The endpoint provided doesn't exist in this workspace") return if Path.hasDirectPath(dst): print("The destination should be reachable from the host") return chain = Path.getPath(None,dst) if chain is None: print("No path could be found to the destination") return for path in chain: print(path)