def handleSEND_CTRL_MSG(self): """ Send a message to the control port of selected Tor instances """ if not self.ctl_dst.myNodeMemberOf(): return if self.ctl_dst and self.ctl_msg : self.log.info("Sending control message to selected Tor nodes") #if not self.isSetup(): # self.log.warning("Tor not setup. Cannot send control port messages") # return self.log.warning("Looking for the control port") controlPort = None controlAddr = None f = open(self.TOR_RC,'r') for line in f: if line.startswith("ControlPort"): controlPort = line.split()[1] if line.startswith("ControlListenAddress"): controlAddr = line.split()[1] if controlPort is None: self.log.warning("%s is not running a control port" % (testbed.getNodeName())) return if controlAddr is None: controlAddr = '127.0.0.1' try: child = pexpect.spawn('nc %s %s' % (controlAddr, controlPort)) child.sendline('authenticate ""') exp_result = child.expect(['250 OK',pexpect.EOF,pexpect.TIMEOUT]) if exp_result == 1: raise Exception("Pexpect ended with EOF") elif exp_result == 2: raise Exception("Pexpect timed out") child.sendline('%s' % (self.ctl_msg)) i = child.expect(['2\d\d [\d\w]+','[56]\d\d [\d\w]+']) if i==0: self.log.info('Response: %s',child.after) child.close() else: child.close() raise Exception("Command failed: %s " % (child.before)) except Exception as e: self.log.warning("Unable to send control port message: %s" % e) return else: self.log.warning("Need both destination and message to send control port message. (dest: %s, msg: %s" %(self.ctl_dst, self.ctl_msg))
def handleSAVE_DATA(self): """Save log data from the tor instances to the directory specified by the 'save_data_dir' directory. Will not do anything if Tor is running.""" is_client = (self.clients and self.clients.myNodeMemberOf()) is_dir = (self.directory and self.directory.myNodeMemberOf()) is_relay = (self.relays and self.relays.myNodeMemberOf()) if not is_client and not is_relay and not is_dir: return if self.isRunning(): raise Exception("Will not save data while Tor is running") if not self.save_data_dir or not os.path.exists(self.save_data_dir): raise Exception("SAVE_DATA command requires 'save_data_dir' to be specified and valid") ts = time.gmtime() minute_round = (ts[4]/5) * 5 if ts[4] != 0 else 0 timesecs = time.mktime((ts[0],ts[1],ts[2],ts[3],minute_round + 5,0,ts[6],ts[7],ts[8])) path = "%s/%s/%s/%s" % (self.save_data_dir,testbed.experiment,int(timesecs),testbed.getNodeName()) failed = False try: shutil.copytree(self.DATA_DIR,path) except shutil.Error as e: self.log.warning("Error copying data: %s" % (",".join(e))) failed = True try: shutil.copy(self.TOR_LOG,path) except shutil.Error as e: self.log.warning("Error copying data: %s" % (",".join(e))) failed = True try: shutil.copy(self.TOR_RC,path) except shutil.Error as e: self.log.warning("Error copying data: %s" % (",".join(e))) failed = True try: shutil.copy("/local/logs/daemon.log",path) except shutil.Error as e: self.log.warning("Error copying data: %s" % (",".join(e))) failed = True if failed is True: raise Exception ("Failed to copy all items") self.log.info("Copied %s to %s" % (self.DATA_DIR, path))