def test_extract_pattern(self): extract_pattern1 = util.extract_pattern("Connection on fd 4 originat" "ing from 444:0000", "Connec" "tion on fd [0-9]+ originati" "ng from [^:]+:([0-9]{1,5})") self.assertEqual(extract_pattern1, "0000") self.assertIsNone(util.extract_pattern("", "<https://atlas.torproj" "ect.org/#details>"))
def test_extract_pattern(self): extract_pattern1 = util.extract_pattern( "Connection on fd 4 originat" "ing from 444:0000", "Connec" "tion on fd [0-9]+ originati" "ng from [^:]+:([0-9]{1,5})") self.assertEqual(extract_pattern1, "0000")
def invoke_process(self, command): """ Run the command and wait for it to finish. If a callback was specified, it is called with the process' output as argument and with a function which can be used to kill the process. """ # Start process and redirect its stderr to stdout. That makes it more # convenient for us to parse the output. self.process = subprocess.Popen( command, env=os.environ, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, ) if self.output_callback: # Read the process' output line by line and pass it to the module's # callback function. keep_reading = True while keep_reading: line = self.process.stdout.readline() if not line: break else: line = line.strip() # Look for torsocks' source port before we pass the line on # to the module. pattern = "Connection on fd [0-9]+ originating " \ "from [^:]+:([0-9]{1,5})" port = util.extract_pattern(line, pattern) if port: # socket.socket is probably monkey-patched. We need, # however, the original implementation. tmpsock = socket.socket socket.socket = self.origsocket self.queue.put([self.circ_id, ("127.0.0.1", int(port))]) socket.socket = tmpsock keep_reading = self.output_callback(line, self.process.kill) # Wait for the process to finish. self.stdout, self.stderr = self.process.communicate()
def _invoke_process(self): """ Invoke the process and wait for it to finish. If a callback was specified, it is called with the process' output as argument and together with a function which can be used to terminate the process. """ # Start process and redirect stderr to stdout. That makes it much more # convenient for us to parse the output. self.process = subprocess.Popen( self.command, env=self.env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, ) if self.output_callback: # Read the process' output line by line and pass it to the # callback. while True: line = self.process.stdout.readline().strip() if not line: break # Look for torsocks' source port before we pass the line on # to the module. port = util.extract_pattern(line, self.pattern) if port is not None: # socket.socket is probably monkey-patched. We need, # however, the original implementation. tmpsock = socket.socket socket.socket = self._origsocket self.queue.put([self.circ_id, ("127.0.0.1", int(port))]) socket.socket = tmpsock self.output_callback(line, self.process.terminate) # Wait for the process to finish. self.stdout, self.stderr = self.process.communicate()
def test_extract_pattern(self): extract_pattern1 = util.extract_pattern("Connection on fd 4 originat" "ing from 444:0000", "Connec" "tion on fd [0-9]+ originati" "ng from [^:]+:([0-9]{1,5})") self.assertEqual(extract_pattern1, "0000")